LLVM 22.0.0git
DWARFFormValue.h
Go to the documentation of this file.
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"
17#include <cstdint>
18
19namespace llvm {
20
21class DWARFContext;
22class DWARFObject;
23class DWARFDataExtractor;
24class DWARFUnit;
25class raw_ostream;
26
28public:
29 enum FormClass {
40 };
41
42 struct ValueType {
43 ValueType() { uval = 0; }
44 ValueType(int64_t V) : sval(V) {}
46 ValueType(const char *V) : cstr(V) {}
47
48 union {
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
57private:
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
65 DWARFFormValue(dwarf::Form F, const ValueType &V) : Form(F), Value(V) {}
66
67public:
69
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
81 dwarf::Form getForm() const { return Form; }
82 uint64_t getRawUValue() const { return Value.uval; }
83
84 LLVM_ABI bool isFormClass(FormClass FC) const;
85 const DWARFUnit *getUnit() const { return U; }
87 DIDumpOptions DumpOpts = DIDumpOptions()) const;
91 LLVM_ABI static void dumpAddress(raw_ostream &OS, uint8_t AddressSize,
93 LLVM_ABI static void dumpAddressSection(const DWARFObject &Obj,
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).
103 uint64_t *OffsetPtr, dwarf::FormParams FormParams,
104 const DWARFContext *Context = nullptr,
105 const DWARFUnit *Unit = nullptr);
106
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;
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>
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.
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
170private:
171 void dumpString(raw_ostream &OS) const;
172};
173
174namespace 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.
181inline std::optional<const char *>
182toString(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.
198inline 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.
218inline 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.
230inline std::optional<uint64_t>
231toUnsigned(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.
243inline uint64_t toUnsigned(const std::optional<DWARFFormValue> &V,
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.
254inline std::optional<uint64_t>
255toRelativeReference(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.
267inline uint64_t toRelativeReference(const std::optional<DWARFFormValue> &V,
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.
278inline std::optional<uint64_t>
279toDebugInfoReference(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.
293inline uint64_t toDebugInfoReference(const std::optional<DWARFFormValue> &V,
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.
303inline std::optional<uint64_t>
304toSignatureReference(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.
316inline uint64_t toSignatureReference(const std::optional<DWARFFormValue> &V,
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.
327inline std::optional<uint64_t>
328toSupplementaryReference(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.
341inline uint64_t toSupplementaryReference(const std::optional<DWARFFormValue> &V,
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.
351inline 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.
363inline 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.
373inline std::optional<uint64_t>
374toAddress(const std::optional<DWARFFormValue> &V) {
375 if (V)
376 return V->getAsAddress();
377 return std::nullopt;
378}
379
380inline std::optional<object::SectionedAddress>
381toSectionedAddress(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.
393inline uint64_t toAddress(const std::optional<DWARFFormValue> &V,
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.
403inline std::optional<uint64_t>
404toSectionOffset(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.
416inline uint64_t toSectionOffset(const std::optional<DWARFFormValue> &V,
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.
426inline std::optional<ArrayRef<uint8_t>>
427toBlock(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.
440 uint16_t DwarfVersion);
441
442} // end namespace dwarf
443
444} // end namespace llvm
445
446#endif // LLVM_DEBUGINFO_DWARF_DWARFFORMVALUE_H
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_ABI
Definition: Compiler.h:213
This file contains constants used for implementing Dwarf debug support.
#define F(x, y, z)
Definition: MD5.cpp:55
raw_pwrite_stream & OS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
DWARFContext This data structure is the top level entity that deals with dwarf debug information pars...
Definition: DWARFContext.h:49
A DWARFDataExtractor (typically for an in-memory copy of an object-file section) plus a relocation ma...
static LLVM_ABI void dumpAddressSection(const DWARFObject &Obj, raw_ostream &OS, DIDumpOptions DumpOpts, uint64_t SectionIndex)
static LLVM_ABI DWARFFormValue createFromPValue(dwarf::Form F, const char *V)
LLVM_ABI std::optional< uint64_t > getAsCStringOffset() const
static LLVM_ABI DWARFFormValue createFromUValue(dwarf::Form F, uint64_t V)
LLVM_ABI std::optional< uint64_t > getAsSupplementaryReference() const
LLVM_ABI std::optional< std::string > getAsFile(DILineInfoSpecifier::FileLineInfoKind Kind) const
Correctly extract any file paths from a form value.
LLVM_ABI std::optional< ArrayRef< uint8_t > > getAsBlock() const
LLVM_ABI std::optional< uint64_t > getAsSectionOffset() const
LLVM_ABI void dumpSectionedAddress(raw_ostream &OS, DIDumpOptions DumpOpts, object::SectionedAddress SA) const
LLVM_ABI bool isFormClass(FormClass FC) const
LLVM_ABI std::optional< object::SectionedAddress > getAsSectionedAddress() const
LLVM_ABI std::optional< uint64_t > getAsReferenceUVal() const
LLVM_ABI void dumpAddress(raw_ostream &OS, uint64_t Address) const
LLVM_ABI std::optional< int64_t > getAsSignedConstant() const
LLVM_ABI std::optional< uint64_t > getAsAddress() const
LLVM_ABI bool extractValue(const DWARFDataExtractor &Data, uint64_t *OffsetPtr, dwarf::FormParams FormParams, const DWARFContext *Context=nullptr, const DWARFUnit *Unit=nullptr)
Extracts a value in Data at offset *OffsetPtr.
LLVM_ABI std::optional< uint64_t > getAsRelativeReference() const
getAsFoo functions below return the extracted value as Foo if only DWARFFormValue has form class is s...
LLVM_ABI std::optional< uint64_t > getAsSignatureReference() const
bool extractValue(const DWARFDataExtractor &Data, uint64_t *OffsetPtr, dwarf::FormParams FormParams, const DWARFUnit *U)
LLVM_ABI std::optional< uint64_t > getAsDebugInfoReference() const
static LLVM_ABI DWARFFormValue createFromBlockValue(dwarf::Form F, ArrayRef< uint8_t > D)
LLVM_ABI void dump(raw_ostream &OS, DIDumpOptions DumpOpts=DIDumpOptions()) const
static LLVM_ABI DWARFFormValue createFromSValue(dwarf::Form F, int64_t V)
LLVM_ABI std::optional< uint64_t > getAsUnsignedConstant() const
bool skipValue(DataExtractor DebugInfoData, uint64_t *OffsetPtr, const dwarf::FormParams Params) const
Skip a form's value in DebugInfoData at the offset specified by OffsetPtr.
static LLVM_ABI DWARFFormValue createFromUnit(dwarf::Form F, const DWARFUnit *Unit, uint64_t *OffsetPtr)
LLVM_ABI Expected< const char * > getAsCString() const
DWARFFormValue(dwarf::Form F=dwarf::Form(0))
const DWARFUnit * getUnit() const
dwarf::Form getForm() const
uint64_t getRawUValue() const
Tagged union holding either a T or a Error.
Definition: Error.h:485
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
LLVM Value Representation.
Definition: Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
std::optional< uint64_t > toAddress(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an address.
std::optional< const char * > toString(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
std::optional< object::SectionedAddress > toSectionedAddress(const std::optional< DWARFFormValue > &V)
std::optional< int64_t > toSigned(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an signed constant.
std::optional< uint64_t > toSignatureReference(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a signature reference.
DwarfFormat
Constants that define the DWARF format as 32 or 64 bit.
Definition: Dwarf.h:92
@ DWARF32
Definition: Dwarf.h:92
std::optional< uint64_t > toDebugInfoReference(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an absolute debug info offset reference.
LLVM_ABI bool doesFormBelongToClass(dwarf::Form Form, DWARFFormValue::FormClass FC, uint16_t DwarfVersion)
Check whether specified Form belongs to the FC class.
std::optional< uint64_t > toSectionOffset(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an section offset.
StringRef toStringRef(const std::optional< DWARFFormValue > &V, StringRef Default={})
Take an optional DWARFFormValue and try to extract a string value from it.
std::optional< ArrayRef< uint8_t > > toBlock(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract block data.
std::optional< uint64_t > toRelativeReference(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a relative offset reference.
std::optional< uint64_t > toSupplementaryReference(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a supplementary debug info reference.
std::optional< uint64_t > toUnsigned(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an unsigned constant.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Default
The result values are uniform if and only if all operands are uniform.
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1083
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:196
A helper struct providing information about the byte size of DW_FORM values that vary in size dependi...
Definition: Dwarf.h:1093