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/DebugInfo/DIContext.h"
18 #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
27 class DWARFFormValue {
44 ValueType() { uval = 0; }
51 const uint8_t *data = nullptr;
52 uint64_t SectionIndex; /// Section index for reference forms.
55 dwarf::Form Form; /// Form for this value.
56 ValueType Value; /// Contains all data for the form.
57 const DWARFUnit *U = nullptr; /// Remember the DWARFUnit at extract time.
58 const DWARFContext *C = nullptr; /// Context for extract time.
60 DWARFFormValue(dwarf::Form F = dwarf::Form(0)) : Form(F) {}
62 dwarf::Form getForm() const { return Form; }
63 uint64_t getRawUValue() const { return Value.uval; }
64 void setForm(dwarf::Form F) { Form = F; }
65 void setUValue(uint64_t V) { Value.uval = V; }
66 void setSValue(int64_t V) { Value.sval = V; }
67 void setPValue(const char *V) { Value.cstr = V; }
69 void setBlockValue(const ArrayRef<uint8_t> &Data) {
70 Value.data = Data.data();
71 setUValue(Data.size());
74 bool isFormClass(FormClass FC) const;
75 const DWARFUnit *getUnit() const { return U; }
76 void dump(raw_ostream &OS, DIDumpOptions DumpOpts = DIDumpOptions()) const;
77 void dumpSectionedAddress(raw_ostream &OS, DIDumpOptions DumpOpts,
78 SectionedAddress SA) const;
79 static void dumpAddressSection(const DWARFObject &Obj, raw_ostream &OS,
80 DIDumpOptions DumpOpts, uint64_t SectionIndex);
82 /// Extracts a value in \p Data at offset \p *OffsetPtr. The information
83 /// in \p FormParams is needed to interpret some forms. The optional
84 /// \p Context and \p Unit allows extracting information if the form refers
85 /// to other sections (e.g., .debug_str).
86 bool extractValue(const DWARFDataExtractor &Data, uint32_t *OffsetPtr,
87 dwarf::FormParams FormParams,
88 const DWARFContext *Context = nullptr,
89 const DWARFUnit *Unit = nullptr);
91 bool extractValue(const DWARFDataExtractor &Data, uint32_t *OffsetPtr,
92 dwarf::FormParams FormParams, const DWARFUnit *U) {
93 return extractValue(Data, OffsetPtr, FormParams, nullptr, U);
96 bool isInlinedCStr() const {
97 return Value.data != nullptr && Value.data == (const uint8_t *)Value.cstr;
100 /// getAsFoo functions below return the extracted value as Foo if only
101 /// DWARFFormValue has form class is suitable for representing Foo.
102 Optional<uint64_t> getAsReference() const;
103 Optional<uint64_t> getAsUnsignedConstant() const;
104 Optional<int64_t> getAsSignedConstant() const;
105 Optional<const char *> getAsCString() const;
106 Optional<uint64_t> getAsAddress() const;
107 Optional<SectionedAddress> getAsSectionedAddress() const;
108 Optional<uint64_t> getAsSectionOffset() const;
109 Optional<ArrayRef<uint8_t>> getAsBlock() const;
110 Optional<uint64_t> getAsCStringOffset() const;
111 Optional<uint64_t> getAsReferenceUVal() const;
113 /// Skip a form's value in \p DebugInfoData at the offset specified by
116 /// Skips the bytes for the current form and updates the offset.
118 /// \param DebugInfoData The data where we want to skip the value.
119 /// \param OffsetPtr A reference to the offset that will be updated.
120 /// \param Params DWARF parameters to help interpret forms.
121 /// \returns true on success, false if the form was not skipped.
122 bool skipValue(DataExtractor DebugInfoData, uint32_t *OffsetPtr,
123 const dwarf::FormParams Params) const {
124 return DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, Params);
127 /// Skip a form's value in \p DebugInfoData at the offset specified by
130 /// Skips the bytes for the specified form and updates the offset.
132 /// \param Form The DW_FORM enumeration that indicates the form to skip.
133 /// \param DebugInfoData The data where we want to skip the value.
134 /// \param OffsetPtr A reference to the offset that will be updated.
135 /// \param FormParams DWARF parameters to help interpret forms.
136 /// \returns true on success, false if the form was not skipped.
137 static bool skipValue(dwarf::Form Form, DataExtractor DebugInfoData,
139 const dwarf::FormParams FormParams);
142 void dumpString(raw_ostream &OS) const;
147 /// Take an optional DWARFFormValue and try to extract a string value from it.
149 /// \param V and optional DWARFFormValue to attempt to extract the value from.
150 /// \returns an optional value that contains a value if the form value
151 /// was valid and was a string.
152 inline Optional<const char *> toString(const Optional<DWARFFormValue> &V) {
154 return V->getAsCString();
158 /// Take an optional DWARFFormValue and extract a string value from it.
160 /// \param V and optional DWARFFormValue to attempt to extract the value from.
161 /// \param Default the default value to return in case of failure.
162 /// \returns the string value or Default if the V doesn't have a value or the
163 /// form value's encoding wasn't a string.
164 inline const char *toString(const Optional<DWARFFormValue> &V,
165 const char *Default) {
166 return toString(V).getValueOr(Default);
169 /// Take an optional DWARFFormValue and try to extract an unsigned constant.
171 /// \param V and optional DWARFFormValue to attempt to extract the value from.
172 /// \returns an optional value that contains a value if the form value
173 /// was valid and has a unsigned constant form.
174 inline Optional<uint64_t> toUnsigned(const Optional<DWARFFormValue> &V) {
176 return V->getAsUnsignedConstant();
180 /// Take an optional DWARFFormValue and extract a unsigned constant.
182 /// \param V and optional DWARFFormValue to attempt to extract the value from.
183 /// \param Default the default value to return in case of failure.
184 /// \returns the extracted unsigned value or Default if the V doesn't have a
185 /// value or the form value's encoding wasn't an unsigned constant form.
186 inline uint64_t toUnsigned(const Optional<DWARFFormValue> &V,
188 return toUnsigned(V).getValueOr(Default);
191 /// Take an optional DWARFFormValue and try to extract an reference.
193 /// \param V and optional DWARFFormValue to attempt to extract the value from.
194 /// \returns an optional value that contains a value if the form value
195 /// was valid and has a reference form.
196 inline Optional<uint64_t> toReference(const Optional<DWARFFormValue> &V) {
198 return V->getAsReference();
202 /// Take an optional DWARFFormValue and extract a reference.
204 /// \param V and optional DWARFFormValue to attempt to extract the value from.
205 /// \param Default the default value to return in case of failure.
206 /// \returns the extracted reference value or Default if the V doesn't have a
207 /// value or the form value's encoding wasn't a reference form.
208 inline uint64_t toReference(const Optional<DWARFFormValue> &V,
210 return toReference(V).getValueOr(Default);
213 /// Take an optional DWARFFormValue and try to extract an signed constant.
215 /// \param V and optional DWARFFormValue to attempt to extract the value from.
216 /// \returns an optional value that contains a value if the form value
217 /// was valid and has a signed constant form.
218 inline Optional<int64_t> toSigned(const Optional<DWARFFormValue> &V) {
220 return V->getAsSignedConstant();
224 /// Take an optional DWARFFormValue and extract a signed integer.
226 /// \param V and optional DWARFFormValue to attempt to extract the value from.
227 /// \param Default the default value to return in case of failure.
228 /// \returns the extracted signed integer value or Default if the V doesn't
229 /// have a value or the form value's encoding wasn't a signed integer form.
230 inline int64_t toSigned(const Optional<DWARFFormValue> &V, int64_t Default) {
231 return toSigned(V).getValueOr(Default);
234 /// Take an optional DWARFFormValue and try to extract an address.
236 /// \param V and optional DWARFFormValue to attempt to extract the value from.
237 /// \returns an optional value that contains a value if the form value
238 /// was valid and has a address form.
239 inline Optional<uint64_t> toAddress(const Optional<DWARFFormValue> &V) {
241 return V->getAsAddress();
245 inline Optional<SectionedAddress>
246 toSectionedAddress(const Optional<DWARFFormValue> &V) {
248 return V->getAsSectionedAddress();
252 /// Take an optional DWARFFormValue and extract a address.
254 /// \param V and optional DWARFFormValue to attempt to extract the value from.
255 /// \param Default the default value to return in case of failure.
256 /// \returns the extracted address value or Default if the V doesn't have a
257 /// value or the form value's encoding wasn't an address form.
258 inline uint64_t toAddress(const Optional<DWARFFormValue> &V, uint64_t Default) {
259 return toAddress(V).getValueOr(Default);
262 /// Take an optional DWARFFormValue and try to extract an section offset.
264 /// \param V and optional DWARFFormValue to attempt to extract the value from.
265 /// \returns an optional value that contains a value if the form value
266 /// was valid and has a section offset form.
267 inline Optional<uint64_t> toSectionOffset(const Optional<DWARFFormValue> &V) {
269 return V->getAsSectionOffset();
273 /// Take an optional DWARFFormValue and extract a section offset.
275 /// \param V and optional DWARFFormValue to attempt to extract the value from.
276 /// \param Default the default value to return in case of failure.
277 /// \returns the extracted section offset value or Default if the V doesn't
278 /// have a value or the form value's encoding wasn't a section offset form.
279 inline uint64_t toSectionOffset(const Optional<DWARFFormValue> &V,
281 return toSectionOffset(V).getValueOr(Default);
284 /// Take an optional DWARFFormValue and try to extract block data.
286 /// \param V and optional DWARFFormValue to attempt to extract the value from.
287 /// \returns an optional value that contains a value if the form value
288 /// was valid and has a block form.
289 inline Optional<ArrayRef<uint8_t>> toBlock(const Optional<DWARFFormValue> &V) {
291 return V->getAsBlock();
295 } // end namespace dwarf
297 } // end namespace llvm
299 #endif // LLVM_DEBUGINFO_DWARFFORMVALUE_H