1 //===- DWARFFormValue.h -----------------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_DEBUGINFO_DWARFFORMVALUE_H
11 #define LLVM_DEBUGINFO_DWARFFORMVALUE_H
12
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"
19 #include <cstdint>
20
21 namespace llvm {
22
23 class DWARFContext;
24 class DWARFUnit;
25 class raw_ostream;
26
27 class DWARFFormValue {
28 public:
29 enum FormClass {
30 FC_Unknown,
31 FC_Address,
32 FC_Block,
33 FC_Constant,
34 FC_String,
35 FC_Flag,
36 FC_Reference,
37 FC_Indirect,
38 FC_SectionOffset,
39 FC_Exprloc
40 };
41
42 private:
43 struct ValueType {
ValueTypeValueType44 ValueType() { uval = 0; }
45
46 union {
47 uint64_t uval;
48 int64_t sval;
49 const char *cstr;
50 };
51 const uint8_t *data = nullptr;
52 uint64_t SectionIndex; /// Section index for reference forms.
53 };
54
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.
59 public:
Form(F)60 DWARFFormValue(dwarf::Form F = dwarf::Form(0)) : Form(F) {}
61
getForm()62 dwarf::Form getForm() const { return Form; }
getRawUValue()63 uint64_t getRawUValue() const { return Value.uval; }
setForm(dwarf::Form F)64 void setForm(dwarf::Form F) { Form = F; }
setUValue(uint64_t V)65 void setUValue(uint64_t V) { Value.uval = V; }
setSValue(int64_t V)66 void setSValue(int64_t V) { Value.sval = V; }
setPValue(const char * V)67 void setPValue(const char *V) { Value.cstr = V; }
68
setBlockValue(const ArrayRef<uint8_t> & Data)69 void setBlockValue(const ArrayRef<uint8_t> &Data) {
70 Value.data = Data.data();
71 setUValue(Data.size());
72 }
73
74 bool isFormClass(FormClass FC) const;
getUnit()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);
81
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);
90
extractValue(const DWARFDataExtractor & Data,uint32_t * OffsetPtr,dwarf::FormParams FormParams,const DWARFUnit * U)91 bool extractValue(const DWARFDataExtractor &Data, uint32_t *OffsetPtr,
92 dwarf::FormParams FormParams, const DWARFUnit *U) {
93 return extractValue(Data, OffsetPtr, FormParams, nullptr, U);
94 }
95
isInlinedCStr()96 bool isInlinedCStr() const {
97 return Value.data != nullptr && Value.data == (const uint8_t *)Value.cstr;
98 }
99
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;
112
113 /// Skip a form's value in \p DebugInfoData at the offset specified by
114 /// \p OffsetPtr.
115 ///
116 /// Skips the bytes for the current form and updates the offset.
117 ///
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.
skipValue(DataExtractor DebugInfoData,uint32_t * OffsetPtr,const dwarf::FormParams Params)122 bool skipValue(DataExtractor DebugInfoData, uint32_t *OffsetPtr,
123 const dwarf::FormParams Params) const {
124 return DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, Params);
125 }
126
127 /// Skip a form's value in \p DebugInfoData at the offset specified by
128 /// \p OffsetPtr.
129 ///
130 /// Skips the bytes for the specified form and updates the offset.
131 ///
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,
138 uint32_t *OffsetPtr,
139 const dwarf::FormParams FormParams);
140
141 private:
142 void dumpString(raw_ostream &OS) const;
143 };
144
145 namespace dwarf {
146
147 /// Take an optional DWARFFormValue and try to extract a string value from it.
148 ///
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.
toString(const Optional<DWARFFormValue> & V)152 inline Optional<const char *> toString(const Optional<DWARFFormValue> &V) {
153 if (V)
154 return V->getAsCString();
155 return None;
156 }
157
158 /// Take an optional DWARFFormValue and extract a string value from it.
159 ///
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.
toString(const Optional<DWARFFormValue> & V,const char * Default)164 inline const char *toString(const Optional<DWARFFormValue> &V,
165 const char *Default) {
166 return toString(V).getValueOr(Default);
167 }
168
169 /// Take an optional DWARFFormValue and try to extract an unsigned constant.
170 ///
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.
toUnsigned(const Optional<DWARFFormValue> & V)174 inline Optional<uint64_t> toUnsigned(const Optional<DWARFFormValue> &V) {
175 if (V)
176 return V->getAsUnsignedConstant();
177 return None;
178 }
179
180 /// Take an optional DWARFFormValue and extract a unsigned constant.
181 ///
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.
toUnsigned(const Optional<DWARFFormValue> & V,uint64_t Default)186 inline uint64_t toUnsigned(const Optional<DWARFFormValue> &V,
187 uint64_t Default) {
188 return toUnsigned(V).getValueOr(Default);
189 }
190
191 /// Take an optional DWARFFormValue and try to extract an reference.
192 ///
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.
toReference(const Optional<DWARFFormValue> & V)196 inline Optional<uint64_t> toReference(const Optional<DWARFFormValue> &V) {
197 if (V)
198 return V->getAsReference();
199 return None;
200 }
201
202 /// Take an optional DWARFFormValue and extract a reference.
203 ///
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.
toReference(const Optional<DWARFFormValue> & V,uint64_t Default)208 inline uint64_t toReference(const Optional<DWARFFormValue> &V,
209 uint64_t Default) {
210 return toReference(V).getValueOr(Default);
211 }
212
213 /// Take an optional DWARFFormValue and try to extract an signed constant.
214 ///
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.
toSigned(const Optional<DWARFFormValue> & V)218 inline Optional<int64_t> toSigned(const Optional<DWARFFormValue> &V) {
219 if (V)
220 return V->getAsSignedConstant();
221 return None;
222 }
223
224 /// Take an optional DWARFFormValue and extract a signed integer.
225 ///
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.
toSigned(const Optional<DWARFFormValue> & V,int64_t Default)230 inline int64_t toSigned(const Optional<DWARFFormValue> &V, int64_t Default) {
231 return toSigned(V).getValueOr(Default);
232 }
233
234 /// Take an optional DWARFFormValue and try to extract an address.
235 ///
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.
toAddress(const Optional<DWARFFormValue> & V)239 inline Optional<uint64_t> toAddress(const Optional<DWARFFormValue> &V) {
240 if (V)
241 return V->getAsAddress();
242 return None;
243 }
244
245 inline Optional<SectionedAddress>
toSectionedAddress(const Optional<DWARFFormValue> & V)246 toSectionedAddress(const Optional<DWARFFormValue> &V) {
247 if (V)
248 return V->getAsSectionedAddress();
249 return None;
250 }
251
252 /// Take an optional DWARFFormValue and extract a address.
253 ///
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.
toAddress(const Optional<DWARFFormValue> & V,uint64_t Default)258 inline uint64_t toAddress(const Optional<DWARFFormValue> &V, uint64_t Default) {
259 return toAddress(V).getValueOr(Default);
260 }
261
262 /// Take an optional DWARFFormValue and try to extract an section offset.
263 ///
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.
toSectionOffset(const Optional<DWARFFormValue> & V)267 inline Optional<uint64_t> toSectionOffset(const Optional<DWARFFormValue> &V) {
268 if (V)
269 return V->getAsSectionOffset();
270 return None;
271 }
272
273 /// Take an optional DWARFFormValue and extract a section offset.
274 ///
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.
toSectionOffset(const Optional<DWARFFormValue> & V,uint64_t Default)279 inline uint64_t toSectionOffset(const Optional<DWARFFormValue> &V,
280 uint64_t Default) {
281 return toSectionOffset(V).getValueOr(Default);
282 }
283
284 /// Take an optional DWARFFormValue and try to extract block data.
285 ///
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.
toBlock(const Optional<DWARFFormValue> & V)289 inline Optional<ArrayRef<uint8_t>> toBlock(const Optional<DWARFFormValue> &V) {
290 if (V)
291 return V->getAsBlock();
292 return None;
293 }
294
295 } // end namespace dwarf
296
297 } // end namespace llvm
298
299 #endif // LLVM_DEBUGINFO_DWARFFORMVALUE_H
300