1 //===- DWARFFormValue.h -----------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_DEBUGINFO_DWARF_DWARFFORMVALUE_H
10 #define LLVM_DEBUGINFO_DWARF_DWARFFORMVALUE_H
11
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/BinaryFormat/Dwarf.h"
14 #include "llvm/DebugInfo/DIContext.h"
15 #include "llvm/Support/Compiler.h"
16 #include "llvm/Support/DataExtractor.h"
17 #include <cstdint>
18
19 namespace llvm {
20
21 class DWARFContext;
22 class DWARFObject;
23 class DWARFDataExtractor;
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 struct ValueType {
ValueTypeValueType43 ValueType() { uval = 0; }
ValueTypeValueType44 ValueType(int64_t V) : sval(V) {}
ValueTypeValueType45 ValueType(uint64_t V) : uval(V) {}
ValueTypeValueType46 ValueType(const char *V) : cstr(V) {}
47
48 union {
49 uint64_t uval;
50 int64_t sval;
51 const char *cstr;
52 };
53 const uint8_t *data = nullptr;
54 uint64_t SectionIndex; /// Section index for reference forms.
55 };
56
57 private:
58 dwarf::Form Form; /// Form for this value.
59 dwarf::DwarfFormat Format =
60 dwarf::DWARF32; /// Remember the DWARF format at extract time.
61 ValueType Value; /// Contains all data for the form.
62 const DWARFUnit *U = nullptr; /// Remember the DWARFUnit at extract time.
63 const DWARFContext *C = nullptr; /// Context for extract time.
64
DWARFFormValue(dwarf::Form F,const ValueType & V)65 DWARFFormValue(dwarf::Form F, const ValueType &V) : Form(F), Value(V) {}
66
67 public:
Form(F)68 DWARFFormValue(dwarf::Form F = dwarf::Form(0)) : Form(F) {}
69
70 LLVM_ABI static DWARFFormValue createFromSValue(dwarf::Form F, int64_t V);
71 LLVM_ABI static DWARFFormValue createFromUValue(dwarf::Form F, uint64_t V);
72 LLVM_ABI static DWARFFormValue createFromPValue(dwarf::Form F, const char *V);
73 LLVM_ABI static DWARFFormValue createFromBlockValue(dwarf::Form F,
74 ArrayRef<uint8_t> D);
75 LLVM_ABI static DWARFFormValue
76 createFromUnit(dwarf::Form F, const DWARFUnit *Unit, uint64_t *OffsetPtr);
77 LLVM_ABI static std::optional<object::SectionedAddress>
78 getAsSectionedAddress(const ValueType &Val, const dwarf::Form Form,
79 const DWARFUnit *U);
80
getForm()81 dwarf::Form getForm() const { return Form; }
getRawUValue()82 uint64_t getRawUValue() const { return Value.uval; }
83
84 LLVM_ABI bool isFormClass(FormClass FC) const;
getUnit()85 const DWARFUnit *getUnit() const { return U; }
86 LLVM_ABI void dump(raw_ostream &OS,
87 DIDumpOptions DumpOpts = DIDumpOptions()) const;
88 LLVM_ABI void dumpSectionedAddress(raw_ostream &OS, DIDumpOptions DumpOpts,
89 object::SectionedAddress SA) const;
90 LLVM_ABI void dumpAddress(raw_ostream &OS, uint64_t Address) const;
91 LLVM_ABI static void dumpAddress(raw_ostream &OS, uint8_t AddressSize,
92 uint64_t Address);
93 LLVM_ABI static void dumpAddressSection(const DWARFObject &Obj,
94 raw_ostream &OS,
95 DIDumpOptions DumpOpts,
96 uint64_t SectionIndex);
97
98 /// Extracts a value in \p Data at offset \p *OffsetPtr. The information
99 /// in \p FormParams is needed to interpret some forms. The optional
100 /// \p Context and \p Unit allows extracting information if the form refers
101 /// to other sections (e.g., .debug_str).
102 LLVM_ABI bool extractValue(const DWARFDataExtractor &Data,
103 uint64_t *OffsetPtr, dwarf::FormParams FormParams,
104 const DWARFContext *Context = nullptr,
105 const DWARFUnit *Unit = nullptr);
106
extractValue(const DWARFDataExtractor & Data,uint64_t * OffsetPtr,dwarf::FormParams FormParams,const DWARFUnit * U)107 bool extractValue(const DWARFDataExtractor &Data, uint64_t *OffsetPtr,
108 dwarf::FormParams FormParams, const DWARFUnit *U) {
109 return extractValue(Data, OffsetPtr, FormParams, nullptr, U);
110 }
111
112 /// getAsFoo functions below return the extracted value as Foo if only
113 /// DWARFFormValue has form class is suitable for representing Foo.
114 LLVM_ABI std::optional<uint64_t> getAsRelativeReference() const;
115 LLVM_ABI std::optional<uint64_t> getAsDebugInfoReference() const;
116 LLVM_ABI std::optional<uint64_t> getAsSignatureReference() const;
117 LLVM_ABI std::optional<uint64_t> getAsSupplementaryReference() const;
118 LLVM_ABI std::optional<uint64_t> getAsUnsignedConstant() const;
119 LLVM_ABI std::optional<int64_t> getAsSignedConstant() const;
120 LLVM_ABI Expected<const char *> getAsCString() const;
121 LLVM_ABI std::optional<uint64_t> getAsAddress() const;
122 LLVM_ABI std::optional<object::SectionedAddress>
123 getAsSectionedAddress() const;
124 LLVM_ABI std::optional<uint64_t> getAsSectionOffset() const;
125 LLVM_ABI std::optional<ArrayRef<uint8_t>> getAsBlock() const;
126 LLVM_ABI std::optional<uint64_t> getAsCStringOffset() const;
127 LLVM_ABI std::optional<uint64_t> getAsReferenceUVal() const;
128 /// Correctly extract any file paths from a form value.
129 ///
130 /// These attributes can be in the from DW_AT_decl_file or DW_AT_call_file
131 /// attributes. We need to use the file index in the correct DWARFUnit's line
132 /// table prologue, and each DWARFFormValue has the DWARFUnit the form value
133 /// was extracted from.
134 ///
135 /// \param Kind The kind of path to extract.
136 ///
137 /// \returns A valid string value on success, or std::nullopt if the form
138 /// class is not FC_Constant, or if the file index is not valid.
139 LLVM_ABI std::optional<std::string>
140 getAsFile(DILineInfoSpecifier::FileLineInfoKind Kind) const;
141
142 /// Skip a form's value in \p DebugInfoData at the offset specified by
143 /// \p OffsetPtr.
144 ///
145 /// Skips the bytes for the current form and updates the offset.
146 ///
147 /// \param DebugInfoData The data where we want to skip the value.
148 /// \param OffsetPtr A reference to the offset that will be updated.
149 /// \param Params DWARF parameters to help interpret forms.
150 /// \returns true on success, false if the form was not skipped.
skipValue(DataExtractor DebugInfoData,uint64_t * OffsetPtr,const dwarf::FormParams Params)151 bool skipValue(DataExtractor DebugInfoData, uint64_t *OffsetPtr,
152 const dwarf::FormParams Params) const {
153 return DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, Params);
154 }
155
156 /// Skip a form's value in \p DebugInfoData at the offset specified by
157 /// \p OffsetPtr.
158 ///
159 /// Skips the bytes for the specified form and updates the offset.
160 ///
161 /// \param Form The DW_FORM enumeration that indicates the form to skip.
162 /// \param DebugInfoData The data where we want to skip the value.
163 /// \param OffsetPtr A reference to the offset that will be updated.
164 /// \param FormParams DWARF parameters to help interpret forms.
165 /// \returns true on success, false if the form was not skipped.
166 LLVM_ABI static bool skipValue(dwarf::Form Form, DataExtractor DebugInfoData,
167 uint64_t *OffsetPtr,
168 const dwarf::FormParams FormParams);
169
170 private:
171 void dumpString(raw_ostream &OS) const;
172 };
173
174 namespace dwarf {
175
176 /// Take an optional DWARFFormValue and try to extract a string value from it.
177 ///
178 /// \param V and optional DWARFFormValue to attempt to extract the value from.
179 /// \returns an optional value that contains a value if the form value
180 /// was valid and was a string.
181 inline std::optional<const char *>
toString(const std::optional<DWARFFormValue> & V)182 toString(const std::optional<DWARFFormValue> &V) {
183 if (!V)
184 return std::nullopt;
185 Expected<const char*> E = V->getAsCString();
186 if (!E) {
187 consumeError(E.takeError());
188 return std::nullopt;
189 }
190 return *E;
191 }
192
193 /// Take an optional DWARFFormValue and try to extract a string value from it.
194 ///
195 /// \param V and optional DWARFFormValue to attempt to extract the value from.
196 /// \returns an optional value that contains a value if the form value
197 /// was valid and was a string.
198 inline StringRef toStringRef(const std::optional<DWARFFormValue> &V,
199 StringRef Default = {}) {
200 if (!V)
201 return Default;
202 auto S = V->getAsCString();
203 if (!S) {
204 consumeError(S.takeError());
205 return Default;
206 }
207 if (!*S)
208 return Default;
209 return *S;
210 }
211
212 /// Take an optional DWARFFormValue and extract a string value from it.
213 ///
214 /// \param V and optional DWARFFormValue to attempt to extract the value from.
215 /// \param Default the default value to return in case of failure.
216 /// \returns the string value or Default if the V doesn't have a value or the
217 /// form value's encoding wasn't a string.
toString(const std::optional<DWARFFormValue> & V,const char * Default)218 inline const char *toString(const std::optional<DWARFFormValue> &V,
219 const char *Default) {
220 if (auto E = toString(V))
221 return *E;
222 return Default;
223 }
224
225 /// Take an optional DWARFFormValue and try to extract an unsigned constant.
226 ///
227 /// \param V and optional DWARFFormValue to attempt to extract the value from.
228 /// \returns an optional value that contains a value if the form value
229 /// was valid and has a unsigned constant form.
230 inline std::optional<uint64_t>
toUnsigned(const std::optional<DWARFFormValue> & V)231 toUnsigned(const std::optional<DWARFFormValue> &V) {
232 if (V)
233 return V->getAsUnsignedConstant();
234 return std::nullopt;
235 }
236
237 /// Take an optional DWARFFormValue and extract a unsigned constant.
238 ///
239 /// \param V and optional DWARFFormValue to attempt to extract the value from.
240 /// \param Default the default value to return in case of failure.
241 /// \returns the extracted unsigned value or Default if the V doesn't have a
242 /// value or the form value's encoding wasn't an unsigned constant form.
toUnsigned(const std::optional<DWARFFormValue> & V,uint64_t Default)243 inline uint64_t toUnsigned(const std::optional<DWARFFormValue> &V,
244 uint64_t Default) {
245 return toUnsigned(V).value_or(Default);
246 }
247
248 /// Take an optional DWARFFormValue and try to extract a relative offset
249 /// reference.
250 ///
251 /// \param V an optional DWARFFormValue to attempt to extract the value from.
252 /// \returns an optional value that contains a value if the form value
253 /// was valid and has a relative reference form.
254 inline std::optional<uint64_t>
toRelativeReference(const std::optional<DWARFFormValue> & V)255 toRelativeReference(const std::optional<DWARFFormValue> &V) {
256 if (V)
257 return V->getAsRelativeReference();
258 return std::nullopt;
259 }
260
261 /// Take an optional DWARFFormValue and extract a relative offset reference.
262 ///
263 /// \param V an optional DWARFFormValue to attempt to extract the value from.
264 /// \param Default the default value to return in case of failure.
265 /// \returns the extracted reference value or Default if the V doesn't have a
266 /// value or the form value's encoding wasn't a relative offset reference form.
toRelativeReference(const std::optional<DWARFFormValue> & V,uint64_t Default)267 inline uint64_t toRelativeReference(const std::optional<DWARFFormValue> &V,
268 uint64_t Default) {
269 return toRelativeReference(V).value_or(Default);
270 }
271
272 /// Take an optional DWARFFormValue and try to extract an absolute debug info
273 /// offset reference.
274 ///
275 /// \param V an optional DWARFFormValue to attempt to extract the value from.
276 /// \returns an optional value that contains a value if the form value
277 /// was valid and has an (absolute) debug info offset reference form.
278 inline std::optional<uint64_t>
toDebugInfoReference(const std::optional<DWARFFormValue> & V)279 toDebugInfoReference(const std::optional<DWARFFormValue> &V) {
280 if (V)
281 return V->getAsDebugInfoReference();
282 return std::nullopt;
283 }
284
285 /// Take an optional DWARFFormValue and extract an absolute debug info offset
286 /// reference.
287 ///
288 /// \param V an optional DWARFFormValue to attempt to extract the value from.
289 /// \param Default the default value to return in case of failure.
290 /// \returns the extracted reference value or Default if the V doesn't have a
291 /// value or the form value's encoding wasn't an absolute debug info offset
292 /// reference form.
toDebugInfoReference(const std::optional<DWARFFormValue> & V,uint64_t Default)293 inline uint64_t toDebugInfoReference(const std::optional<DWARFFormValue> &V,
294 uint64_t Default) {
295 return toDebugInfoReference(V).value_or(Default);
296 }
297
298 /// Take an optional DWARFFormValue and try to extract a signature reference.
299 ///
300 /// \param V an optional DWARFFormValue to attempt to extract the value from.
301 /// \returns an optional value that contains a value if the form value
302 /// was valid and has a signature reference form.
303 inline std::optional<uint64_t>
toSignatureReference(const std::optional<DWARFFormValue> & V)304 toSignatureReference(const std::optional<DWARFFormValue> &V) {
305 if (V)
306 return V->getAsSignatureReference();
307 return std::nullopt;
308 }
309
310 /// Take an optional DWARFFormValue and extract a signature reference.
311 ///
312 /// \param V an optional DWARFFormValue to attempt to extract the value from.
313 /// \param Default the default value to return in case of failure.
314 /// \returns the extracted reference value or Default if the V doesn't have a
315 /// value or the form value's encoding wasn't a signature reference form.
toSignatureReference(const std::optional<DWARFFormValue> & V,uint64_t Default)316 inline uint64_t toSignatureReference(const std::optional<DWARFFormValue> &V,
317 uint64_t Default) {
318 return toSignatureReference(V).value_or(Default);
319 }
320
321 /// Take an optional DWARFFormValue and try to extract a supplementary debug
322 /// info reference.
323 ///
324 /// \param V an optional DWARFFormValue to attempt to extract the value from.
325 /// \returns an optional value that contains a value if the form value
326 /// was valid and has a supplementary reference form.
327 inline std::optional<uint64_t>
toSupplementaryReference(const std::optional<DWARFFormValue> & V)328 toSupplementaryReference(const std::optional<DWARFFormValue> &V) {
329 if (V)
330 return V->getAsSupplementaryReference();
331 return std::nullopt;
332 }
333
334 /// Take an optional DWARFFormValue and extract a supplementary debug info
335 /// reference.
336 ///
337 /// \param V an optional DWARFFormValue to attempt to extract the value from.
338 /// \param Default the default value to return in case of failure.
339 /// \returns the extracted reference value or Default if the V doesn't have a
340 /// value or the form value's encoding wasn't a supplementary reference form.
toSupplementaryReference(const std::optional<DWARFFormValue> & V,uint64_t Default)341 inline uint64_t toSupplementaryReference(const std::optional<DWARFFormValue> &V,
342 uint64_t Default) {
343 return toSupplementaryReference(V).value_or(Default);
344 }
345
346 /// Take an optional DWARFFormValue and try to extract an signed constant.
347 ///
348 /// \param V and optional DWARFFormValue to attempt to extract the value from.
349 /// \returns an optional value that contains a value if the form value
350 /// was valid and has a signed constant form.
toSigned(const std::optional<DWARFFormValue> & V)351 inline std::optional<int64_t> toSigned(const std::optional<DWARFFormValue> &V) {
352 if (V)
353 return V->getAsSignedConstant();
354 return std::nullopt;
355 }
356
357 /// Take an optional DWARFFormValue and extract a signed integer.
358 ///
359 /// \param V and optional DWARFFormValue to attempt to extract the value from.
360 /// \param Default the default value to return in case of failure.
361 /// \returns the extracted signed integer value or Default if the V doesn't
362 /// have a value or the form value's encoding wasn't a signed integer form.
toSigned(const std::optional<DWARFFormValue> & V,int64_t Default)363 inline int64_t toSigned(const std::optional<DWARFFormValue> &V,
364 int64_t Default) {
365 return toSigned(V).value_or(Default);
366 }
367
368 /// Take an optional DWARFFormValue and try to extract an address.
369 ///
370 /// \param V and optional DWARFFormValue to attempt to extract the value from.
371 /// \returns an optional value that contains a value if the form value
372 /// was valid and has a address form.
373 inline std::optional<uint64_t>
toAddress(const std::optional<DWARFFormValue> & V)374 toAddress(const std::optional<DWARFFormValue> &V) {
375 if (V)
376 return V->getAsAddress();
377 return std::nullopt;
378 }
379
380 inline std::optional<object::SectionedAddress>
toSectionedAddress(const std::optional<DWARFFormValue> & V)381 toSectionedAddress(const std::optional<DWARFFormValue> &V) {
382 if (V)
383 return V->getAsSectionedAddress();
384 return std::nullopt;
385 }
386
387 /// Take an optional DWARFFormValue and extract a address.
388 ///
389 /// \param V and optional DWARFFormValue to attempt to extract the value from.
390 /// \param Default the default value to return in case of failure.
391 /// \returns the extracted address value or Default if the V doesn't have a
392 /// value or the form value's encoding wasn't an address form.
toAddress(const std::optional<DWARFFormValue> & V,uint64_t Default)393 inline uint64_t toAddress(const std::optional<DWARFFormValue> &V,
394 uint64_t Default) {
395 return toAddress(V).value_or(Default);
396 }
397
398 /// Take an optional DWARFFormValue and try to extract an section offset.
399 ///
400 /// \param V and optional DWARFFormValue to attempt to extract the value from.
401 /// \returns an optional value that contains a value if the form value
402 /// was valid and has a section offset form.
403 inline std::optional<uint64_t>
toSectionOffset(const std::optional<DWARFFormValue> & V)404 toSectionOffset(const std::optional<DWARFFormValue> &V) {
405 if (V)
406 return V->getAsSectionOffset();
407 return std::nullopt;
408 }
409
410 /// Take an optional DWARFFormValue and extract a section offset.
411 ///
412 /// \param V and optional DWARFFormValue to attempt to extract the value from.
413 /// \param Default the default value to return in case of failure.
414 /// \returns the extracted section offset value or Default if the V doesn't
415 /// have a value or the form value's encoding wasn't a section offset form.
toSectionOffset(const std::optional<DWARFFormValue> & V,uint64_t Default)416 inline uint64_t toSectionOffset(const std::optional<DWARFFormValue> &V,
417 uint64_t Default) {
418 return toSectionOffset(V).value_or(Default);
419 }
420
421 /// Take an optional DWARFFormValue and try to extract block data.
422 ///
423 /// \param V and optional DWARFFormValue to attempt to extract the value from.
424 /// \returns an optional value that contains a value if the form value
425 /// was valid and has a block form.
426 inline std::optional<ArrayRef<uint8_t>>
toBlock(const std::optional<DWARFFormValue> & V)427 toBlock(const std::optional<DWARFFormValue> &V) {
428 if (V)
429 return V->getAsBlock();
430 return std::nullopt;
431 }
432
433 /// Check whether specified \p Form belongs to the \p FC class.
434 /// \param Form an attribute form.
435 /// \param FC an attribute form class to check.
436 /// \param DwarfVersion the version of DWARF debug info keeping the attribute.
437 /// \returns true if specified \p Form belongs to the \p FC class.
438 LLVM_ABI bool doesFormBelongToClass(dwarf::Form Form,
439 DWARFFormValue::FormClass FC,
440 uint16_t DwarfVersion);
441
442 } // end namespace dwarf
443
444 } // end namespace llvm
445
446 #endif // LLVM_DEBUGINFO_DWARF_DWARFFORMVALUE_H
447