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/BinaryFormat/Dwarf.h"
17 #include "llvm/Support/DataExtractor.h"
25 /// A helper struct for DWARFFormValue methods, providing information that
26 /// allows it to know the byte size of DW_FORM values that vary in size
27 /// depending on the DWARF version, address byte size, or DWARF32/DWARF64.
28 struct DWARFFormParams {
31 dwarf::DwarfFormat Format;
33 /// The definition of the size of form DW_FORM_ref_addr depends on the
34 /// version. In DWARF v2 it's the size of an address; after that, it's the
35 /// size of a reference.
36 uint8_t getRefAddrByteSize() const {
39 return getDwarfOffsetByteSize();
42 /// The size of a reference is determined by the DWARF 32/64-bit format.
43 uint8_t getDwarfOffsetByteSize() const {
45 case dwarf::DwarfFormat::DWARF32:
47 case dwarf::DwarfFormat::DWARF64:
50 llvm_unreachable("Invalid Format value");
54 class DWARFFormValue {
71 ValueType() { uval = 0; }
78 const uint8_t *data = nullptr;
79 uint64_t SectionIndex; /// Section index for reference forms.
82 dwarf::Form Form; /// Form for this value.
83 ValueType Value; /// Contains all data for the form.
84 const DWARFUnit *U = nullptr; /// Remember the DWARFUnit at extract time.
87 DWARFFormValue(dwarf::Form F = dwarf::Form(0)) : Form(F) {}
89 dwarf::Form getForm() const { return Form; }
90 uint64_t getRawUValue() const { return Value.uval; }
91 uint64_t getSectionIndex() const { return Value.SectionIndex; }
92 void setForm(dwarf::Form F) { Form = F; }
93 void setUValue(uint64_t V) { Value.uval = V; }
94 void setSValue(int64_t V) { Value.sval = V; }
95 void setPValue(const char *V) { Value.cstr = V; }
97 void setBlockValue(const ArrayRef<uint8_t> &Data) {
98 Value.data = Data.data();
99 setUValue(Data.size());
102 bool isFormClass(FormClass FC) const;
103 const DWARFUnit *getUnit() const { return U; }
104 void dump(raw_ostream &OS) const;
106 /// Extracts a value in \p Data at offset \p *OffsetPtr.
108 /// The passed DWARFUnit is allowed to be nullptr, in which
109 /// case no relocation processing will be performed and some
110 /// kind of forms that depend on Unit information are disallowed.
111 /// \param Data The DataExtractor to use.
112 /// \param OffsetPtr The offset within DataExtractor where the data starts.
113 /// \param U The optional DWARFUnit supplying information for some forms.
114 /// \returns whether the extraction succeeded.
115 bool extractValue(const DataExtractor &Data, uint32_t *OffsetPtr,
118 bool isInlinedCStr() const {
119 return Value.data != nullptr && Value.data == (const uint8_t *)Value.cstr;
122 /// getAsFoo functions below return the extracted value as Foo if only
123 /// DWARFFormValue has form class is suitable for representing Foo.
124 Optional<uint64_t> getAsReference() const;
125 Optional<uint64_t> getAsUnsignedConstant() const;
126 Optional<int64_t> getAsSignedConstant() const;
127 Optional<const char *> getAsCString() const;
128 Optional<uint64_t> getAsAddress() const;
129 Optional<uint64_t> getAsSectionOffset() const;
130 Optional<ArrayRef<uint8_t>> getAsBlock() const;
131 Optional<uint64_t> getAsCStringOffset() const;
132 Optional<uint64_t> getAsReferenceUVal() const;
134 /// Get the fixed byte size for a given form.
136 /// If the form has a fixed byte size, then an Optional with a value will be
137 /// returned. If the form is always encoded using a variable length storage
138 /// format (ULEB or SLEB numbers or blocks) then None will be returned.
140 /// \param Form DWARF form to get the fixed byte size for.
141 /// \param FormParams DWARF parameters to help interpret forms.
142 /// \returns Optional<uint8_t> value with the fixed byte size or None if
143 /// \p Form doesn't have a fixed byte size.
144 static Optional<uint8_t> getFixedByteSize(dwarf::Form Form,
145 const DWARFFormParams FormParams);
147 /// Skip a form's value in \p DebugInfoData at the offset specified by
150 /// Skips the bytes for the current form and updates the offset.
152 /// \param DebugInfoData The data where we want to skip the value.
153 /// \param OffsetPtr A reference to the offset that will be updated.
154 /// \param Params DWARF parameters to help interpret forms.
155 /// \returns true on success, false if the form was not skipped.
156 bool skipValue(DataExtractor DebugInfoData, uint32_t *OffsetPtr,
157 const DWARFFormParams Params) const {
158 return DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, Params);
161 /// Skip a form's value in \p DebugInfoData at the offset specified by
164 /// Skips the bytes for the specified form and updates the offset.
166 /// \param Form The DW_FORM enumeration that indicates the form to skip.
167 /// \param DebugInfoData The data where we want to skip the value.
168 /// \param OffsetPtr A reference to the offset that will be updated.
169 /// \param FormParams DWARF parameters to help interpret forms.
170 /// \returns true on success, false if the form was not skipped.
171 static bool skipValue(dwarf::Form Form, DataExtractor DebugInfoData,
172 uint32_t *OffsetPtr, const DWARFFormParams FormParams);
175 void dumpString(raw_ostream &OS) const;
180 /// Take an optional DWARFFormValue and try to extract a string value from it.
182 /// \param V and optional DWARFFormValue to attempt to extract the value from.
183 /// \returns an optional value that contains a value if the form value
184 /// was valid and was a string.
185 inline Optional<const char *> toString(const Optional<DWARFFormValue> &V) {
187 return V->getAsCString();
191 /// Take an optional DWARFFormValue and extract a string value from it.
193 /// \param V and optional DWARFFormValue to attempt to extract the value from.
194 /// \param Default the default value to return in case of failure.
195 /// \returns the string value or Default if the V doesn't have a value or the
196 /// form value's encoding wasn't a string.
197 inline const char *toString(const Optional<DWARFFormValue> &V,
198 const char *Default) {
199 return toString(V).getValueOr(Default);
202 /// Take an optional DWARFFormValue and try to extract an unsigned constant.
204 /// \param V and optional DWARFFormValue to attempt to extract the value from.
205 /// \returns an optional value that contains a value if the form value
206 /// was valid and has a unsigned constant form.
207 inline Optional<uint64_t> toUnsigned(const Optional<DWARFFormValue> &V) {
209 return V->getAsUnsignedConstant();
213 /// Take an optional DWARFFormValue and extract a unsigned constant.
215 /// \param V and optional DWARFFormValue to attempt to extract the value from.
216 /// \param Default the default value to return in case of failure.
217 /// \returns the extracted unsigned value or Default if the V doesn't have a
218 /// value or the form value's encoding wasn't an unsigned constant form.
219 inline uint64_t toUnsigned(const Optional<DWARFFormValue> &V,
221 return toUnsigned(V).getValueOr(Default);
224 /// Take an optional DWARFFormValue and try to extract an reference.
226 /// \param V and optional DWARFFormValue to attempt to extract the value from.
227 /// \returns an optional value that contains a value if the form value
228 /// was valid and has a reference form.
229 inline Optional<uint64_t> toReference(const Optional<DWARFFormValue> &V) {
231 return V->getAsReference();
235 /// Take an optional DWARFFormValue and extract a reference.
237 /// \param V and optional DWARFFormValue to attempt to extract the value from.
238 /// \param Default the default value to return in case of failure.
239 /// \returns the extracted reference value or Default if the V doesn't have a
240 /// value or the form value's encoding wasn't a reference form.
241 inline uint64_t toReference(const Optional<DWARFFormValue> &V,
243 return toReference(V).getValueOr(Default);
246 /// Take an optional DWARFFormValue and try to extract an signed constant.
248 /// \param V and optional DWARFFormValue to attempt to extract the value from.
249 /// \returns an optional value that contains a value if the form value
250 /// was valid and has a signed constant form.
251 inline Optional<int64_t> toSigned(const Optional<DWARFFormValue> &V) {
253 return V->getAsSignedConstant();
257 /// Take an optional DWARFFormValue and extract a signed integer.
259 /// \param V and optional DWARFFormValue to attempt to extract the value from.
260 /// \param Default the default value to return in case of failure.
261 /// \returns the extracted signed integer value or Default if the V doesn't
262 /// have a value or the form value's encoding wasn't a signed integer form.
263 inline int64_t toSigned(const Optional<DWARFFormValue> &V, int64_t Default) {
264 return toSigned(V).getValueOr(Default);
267 /// Take an optional DWARFFormValue and try to extract an address.
269 /// \param V and optional DWARFFormValue to attempt to extract the value from.
270 /// \returns an optional value that contains a value if the form value
271 /// was valid and has a address form.
272 inline Optional<uint64_t> toAddress(const Optional<DWARFFormValue> &V) {
274 return V->getAsAddress();
278 /// Take an optional DWARFFormValue and extract a address.
280 /// \param V and optional DWARFFormValue to attempt to extract the value from.
281 /// \param Default the default value to return in case of failure.
282 /// \returns the extracted address value or Default if the V doesn't have a
283 /// value or the form value's encoding wasn't an address form.
284 inline uint64_t toAddress(const Optional<DWARFFormValue> &V, uint64_t Default) {
285 return toAddress(V).getValueOr(Default);
288 /// Take an optional DWARFFormValue and try to extract an section offset.
290 /// \param V and optional DWARFFormValue to attempt to extract the value from.
291 /// \returns an optional value that contains a value if the form value
292 /// was valid and has a section offset form.
293 inline Optional<uint64_t> toSectionOffset(const Optional<DWARFFormValue> &V) {
295 return V->getAsSectionOffset();
299 /// Take an optional DWARFFormValue and extract a section offset.
301 /// \param V and optional DWARFFormValue to attempt to extract the value from.
302 /// \param Default the default value to return in case of failure.
303 /// \returns the extracted section offset value or Default if the V doesn't
304 /// have a value or the form value's encoding wasn't a section offset form.
305 inline uint64_t toSectionOffset(const Optional<DWARFFormValue> &V,
307 return toSectionOffset(V).getValueOr(Default);
310 /// Take an optional DWARFFormValue and try to extract block data.
312 /// \param V and optional DWARFFormValue to attempt to extract the value from.
313 /// \returns an optional value that contains a value if the form value
314 /// was valid and has a block form.
315 inline Optional<ArrayRef<uint8_t>> toBlock(const Optional<DWARFFormValue> &V) {
317 return V->getAsBlock();
321 } // end namespace dwarf
323 } // end namespace llvm
325 #endif // LLVM_DEBUGINFO_DWARFFORMVALUE_H