1 //===- DWARFFormValue.h -----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_DEBUGINFO_DWARFFORMVALUE_H
11 #define LLVM_DEBUGINFO_DWARFFORMVALUE_H
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/None.h"
15 #include "llvm/ADT/Optional.h"
16 #include "llvm/Support/DataExtractor.h"
17 #include "llvm/Support/Dwarf.h"
25 class DWARFFormValue {
42 ValueType() { uval = 0; }
49 const uint8_t *data = nullptr;
50 uint64_t SectionIndex; /// Section index for reference forms.
53 dwarf::Form Form; /// Form for this value.
54 ValueType Value; /// Contains all data for the form.
55 const DWARFUnit *U = nullptr; /// Remember the DWARFUnit at extract time.
58 DWARFFormValue(dwarf::Form F = dwarf::Form(0)) : Form(F) {}
60 dwarf::Form getForm() const { return Form; }
61 uint64_t getRawUValue() const { return Value.uval; }
62 uint64_t getSectionIndex() const { return Value.SectionIndex; }
63 void setForm(dwarf::Form F) { Form = F; }
64 void setUValue(uint64_t V) { Value.uval = V; }
65 void setSValue(int64_t V) { Value.sval = V; }
66 void setPValue(const char *V) { Value.cstr = V; }
68 void setBlockValue(const ArrayRef<uint8_t> &Data) {
69 Value.data = Data.data();
70 setUValue(Data.size());
73 bool isFormClass(FormClass FC) const;
74 const DWARFUnit *getUnit() const { return U; }
75 void dump(raw_ostream &OS) const;
77 /// Extracts a value in \p Data at offset \p *OffsetPtr.
79 /// The passed DWARFUnit is allowed to be nullptr, in which
80 /// case no relocation processing will be performed and some
81 /// kind of forms that depend on Unit information are disallowed.
82 /// \param Data The DataExtractor to use.
83 /// \param OffsetPtr The offset within DataExtractor where the data starts.
84 /// \param U The optional DWARFUnit supplying information for some forms.
85 /// \returns whether the extraction succeeded.
86 bool extractValue(const DataExtractor &Data, uint32_t *OffsetPtr,
89 bool isInlinedCStr() const {
90 return Value.data != nullptr && Value.data == (const uint8_t *)Value.cstr;
93 /// getAsFoo functions below return the extracted value as Foo if only
94 /// DWARFFormValue has form class is suitable for representing Foo.
95 Optional<uint64_t> getAsReference() const;
96 Optional<uint64_t> getAsUnsignedConstant() const;
97 Optional<int64_t> getAsSignedConstant() const;
98 Optional<const char *> getAsCString() const;
99 Optional<uint64_t> getAsAddress() const;
100 Optional<uint64_t> getAsSectionOffset() const;
101 Optional<ArrayRef<uint8_t>> getAsBlock() const;
102 Optional<uint64_t> getAsCStringOffset() const;
103 Optional<uint64_t> getAsReferenceUVal() const;
105 /// Get the fixed byte size for a given form.
107 /// If the form always has a fixed valid byte size that doesn't depend on a
108 /// DWARFUnit, then an Optional with a value will be returned. If the form
109 /// can vary in size depending on the DWARFUnit (DWARF version, address byte
110 /// size, or DWARF 32/64) and the DWARFUnit is valid, then an Optional with a
111 /// valid value is returned. If the form is always encoded using a variable
112 /// length storage format (ULEB or SLEB numbers or blocks) or the size
113 /// depends on a DWARFUnit and the DWARFUnit is NULL, then None will be
115 /// \param Form The DWARF form to get the fixed byte size for
116 /// \param U The DWARFUnit that can be used to help determine the byte size.
118 /// \returns Optional<uint8_t> value with the fixed byte size or None if
119 /// \p Form doesn't have a fixed byte size or a DWARFUnit wasn't supplied
120 /// and was needed to calculate the byte size.
121 static Optional<uint8_t> getFixedByteSize(dwarf::Form Form,
122 const DWARFUnit *U = nullptr);
124 /// Get the fixed byte size for a given form.
126 /// If the form has a fixed byte size given a valid DWARF version and address
127 /// byte size, then an Optional with a valid value is returned. If the form
128 /// is always encoded using a variable length storage format (ULEB or SLEB
129 /// numbers or blocks) then None will be returned.
131 /// \param Form DWARF form to get the fixed byte size for
132 /// \param Version DWARF version number.
133 /// \param AddrSize size of an address in bytes.
134 /// \param Format enum value from llvm::dwarf::DwarfFormat.
135 /// \returns Optional<uint8_t> value with the fixed byte size or None if
136 /// \p Form doesn't have a fixed byte size.
137 static Optional<uint8_t> getFixedByteSize(dwarf::Form Form, uint16_t Version,
139 llvm::dwarf::DwarfFormat Format);
141 /// Skip a form in \p DebugInfoData at offset specified by \p OffsetPtr.
143 /// Skips the bytes for this form in the debug info and updates the offset.
145 /// \param DebugInfoData the .debug_info data to use to skip the value.
146 /// \param OffsetPtr a reference to the offset that will be updated.
147 /// \param U the DWARFUnit to use when skipping the form in case the form
148 /// size differs according to data in the DWARFUnit.
149 /// \returns true on success, false if the form was not skipped.
150 bool skipValue(DataExtractor DebugInfoData, uint32_t *OffsetPtr,
151 const DWARFUnit *U) const;
153 /// Skip a form in \p DebugInfoData at offset specified by \p OffsetPtr.
155 /// Skips the bytes for this form in the debug info and updates the offset.
157 /// \param Form the DW_FORM enumeration that indicates the form to skip.
158 /// \param DebugInfoData the .debug_info data to use to skip the value.
159 /// \param OffsetPtr a reference to the offset that will be updated.
160 /// \param U the DWARFUnit to use when skipping the form in case the form
161 /// size differs according to data in the DWARFUnit.
162 /// \returns true on success, false if the form was not skipped.
163 static bool skipValue(dwarf::Form Form, DataExtractor DebugInfoData,
164 uint32_t *OffsetPtr, const DWARFUnit *U);
166 /// Skip a form in \p DebugInfoData at offset specified by \p OffsetPtr.
168 /// Skips the bytes for this form in the debug info and updates the offset.
170 /// \param Form the DW_FORM enumeration that indicates the form to skip.
171 /// \param DebugInfoData the .debug_info data to use to skip the value.
172 /// \param OffsetPtr a reference to the offset that will be updated.
173 /// \param Version DWARF version number.
174 /// \param AddrSize size of an address in bytes.
175 /// \param Format enum value from llvm::dwarf::DwarfFormat.
176 /// \returns true on success, false if the form was not skipped.
177 static bool skipValue(dwarf::Form Form, DataExtractor DebugInfoData,
178 uint32_t *OffsetPtr, uint16_t Version, uint8_t AddrSize,
179 llvm::dwarf::DwarfFormat Format);
182 void dumpString(raw_ostream &OS) const;
187 /// Take an optional DWARFFormValue and try to extract a string value from it.
189 /// \param V and optional DWARFFormValue to attempt to extract the value from.
190 /// \returns an optional value that contains a value if the form value
191 /// was valid and was a string.
192 inline Optional<const char *> toString(const Optional<DWARFFormValue> &V) {
194 return V->getAsCString();
198 /// Take an optional DWARFFormValue and extract a string value from it.
200 /// \param V and optional DWARFFormValue to attempt to extract the value from.
201 /// \param Default the default value to return in case of failure.
202 /// \returns the string value or Default if the V doesn't have a value or the
203 /// form value's encoding wasn't a string.
204 inline const char *toString(const Optional<DWARFFormValue> &V,
205 const char *Default) {
206 return toString(V).getValueOr(Default);
209 /// Take an optional DWARFFormValue and try to extract an unsigned constant.
211 /// \param V and optional DWARFFormValue to attempt to extract the value from.
212 /// \returns an optional value that contains a value if the form value
213 /// was valid and has a unsigned constant form.
214 inline Optional<uint64_t> toUnsigned(const Optional<DWARFFormValue> &V) {
216 return V->getAsUnsignedConstant();
220 /// Take an optional DWARFFormValue and extract a unsigned constant.
222 /// \param V and optional DWARFFormValue to attempt to extract the value from.
223 /// \param Default the default value to return in case of failure.
224 /// \returns the extracted unsigned value or Default if the V doesn't have a
225 /// value or the form value's encoding wasn't an unsigned constant form.
226 inline uint64_t toUnsigned(const Optional<DWARFFormValue> &V,
228 return toUnsigned(V).getValueOr(Default);
231 /// Take an optional DWARFFormValue and try to extract an reference.
233 /// \param V and optional DWARFFormValue to attempt to extract the value from.
234 /// \returns an optional value that contains a value if the form value
235 /// was valid and has a reference form.
236 inline Optional<uint64_t> toReference(const Optional<DWARFFormValue> &V) {
238 return V->getAsReference();
242 /// Take an optional DWARFFormValue and extract a reference.
244 /// \param V and optional DWARFFormValue to attempt to extract the value from.
245 /// \param Default the default value to return in case of failure.
246 /// \returns the extracted reference value or Default if the V doesn't have a
247 /// value or the form value's encoding wasn't a reference form.
248 inline uint64_t toReference(const Optional<DWARFFormValue> &V,
250 return toReference(V).getValueOr(Default);
253 /// Take an optional DWARFFormValue and try to extract an signed constant.
255 /// \param V and optional DWARFFormValue to attempt to extract the value from.
256 /// \returns an optional value that contains a value if the form value
257 /// was valid and has a signed constant form.
258 inline Optional<int64_t> toSigned(const Optional<DWARFFormValue> &V) {
260 return V->getAsSignedConstant();
264 /// Take an optional DWARFFormValue and extract a signed integer.
266 /// \param V and optional DWARFFormValue to attempt to extract the value from.
267 /// \param Default the default value to return in case of failure.
268 /// \returns the extracted signed integer value or Default if the V doesn't
269 /// have a value or the form value's encoding wasn't a signed integer form.
270 inline int64_t toSigned(const Optional<DWARFFormValue> &V, int64_t Default) {
271 return toSigned(V).getValueOr(Default);
274 /// Take an optional DWARFFormValue and try to extract an address.
276 /// \param V and optional DWARFFormValue to attempt to extract the value from.
277 /// \returns an optional value that contains a value if the form value
278 /// was valid and has a address form.
279 inline Optional<uint64_t> toAddress(const Optional<DWARFFormValue> &V) {
281 return V->getAsAddress();
285 /// Take an optional DWARFFormValue and extract a address.
287 /// \param V and optional DWARFFormValue to attempt to extract the value from.
288 /// \param Default the default value to return in case of failure.
289 /// \returns the extracted address value or Default if the V doesn't have a
290 /// value or the form value's encoding wasn't an address form.
291 inline uint64_t toAddress(const Optional<DWARFFormValue> &V, uint64_t Default) {
292 return toAddress(V).getValueOr(Default);
295 /// Take an optional DWARFFormValue and try to extract an section offset.
297 /// \param V and optional DWARFFormValue to attempt to extract the value from.
298 /// \returns an optional value that contains a value if the form value
299 /// was valid and has a section offset form.
300 inline Optional<uint64_t> toSectionOffset(const Optional<DWARFFormValue> &V) {
302 return V->getAsSectionOffset();
306 /// Take an optional DWARFFormValue and extract a section offset.
308 /// \param V and optional DWARFFormValue to attempt to extract the value from.
309 /// \param Default the default value to return in case of failure.
310 /// \returns the extracted section offset value or Default if the V doesn't
311 /// have a value or the form value's encoding wasn't a section offset form.
312 inline uint64_t toSectionOffset(const Optional<DWARFFormValue> &V,
314 return toSectionOffset(V).getValueOr(Default);
317 /// Take an optional DWARFFormValue and try to extract block data.
319 /// \param V and optional DWARFFormValue to attempt to extract the value from.
320 /// \returns an optional value that contains a value if the form value
321 /// was valid and has a block form.
322 inline Optional<ArrayRef<uint8_t>> toBlock(const Optional<DWARFFormValue> &V) {
324 return V->getAsBlock();
328 } // end namespace dwarf
330 } // end namespace llvm
332 #endif // LLVM_DEBUGINFO_DWARFFORMVALUE_H