LLVM  14.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"
13 #include "llvm/ADT/None.h"
14 #include "llvm/ADT/Optional.h"
18 #include <cstdint>
19 
20 namespace llvm {
21 
22 class DWARFContext;
23 class DWARFUnit;
24 class raw_ostream;
25 
27 public:
28  enum FormClass {
39  };
40 
41 private:
42  struct ValueType {
43  ValueType() { uval = 0; }
44  ValueType(int64_t V) : sval(V) {}
45  ValueType(uint64_t V) : uval(V) {}
46  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  dwarf::Form Form; /// Form for this value.
58  dwarf::DwarfFormat Format =
59  dwarf::DWARF32; /// Remember the DWARF format at extract time.
60  ValueType Value; /// Contains all data for the form.
61  const DWARFUnit *U = nullptr; /// Remember the DWARFUnit at extract time.
62  const DWARFContext *C = nullptr; /// Context for extract time.
63 
64  DWARFFormValue(dwarf::Form F, ValueType V) : Form(F), Value(V) {}
65 
66 public:
68 
69  static DWARFFormValue createFromSValue(dwarf::Form F, int64_t V);
71  static DWARFFormValue createFromPValue(dwarf::Form F, const char *V);
75  uint64_t *OffsetPtr);
76 
77  dwarf::Form getForm() const { return Form; }
78  uint64_t getRawUValue() const { return Value.uval; }
79 
80  bool isFormClass(FormClass FC) const;
81  const DWARFUnit *getUnit() const { return U; }
82  void dump(raw_ostream &OS, DIDumpOptions DumpOpts = DIDumpOptions()) const;
84  object::SectionedAddress SA) const;
85  void dumpAddress(raw_ostream &OS, uint64_t Address) const;
86  static void dumpAddress(raw_ostream &OS, uint8_t AddressSize,
87  uint64_t Address);
88  static void dumpAddressSection(const DWARFObject &Obj, raw_ostream &OS,
89  DIDumpOptions DumpOpts, uint64_t SectionIndex);
90 
91  /// Extracts a value in \p Data at offset \p *OffsetPtr. The information
92  /// in \p FormParams is needed to interpret some forms. The optional
93  /// \p Context and \p Unit allows extracting information if the form refers
94  /// to other sections (e.g., .debug_str).
95  bool extractValue(const DWARFDataExtractor &Data, uint64_t *OffsetPtr,
96  dwarf::FormParams FormParams,
97  const DWARFContext *Context = nullptr,
98  const DWARFUnit *Unit = nullptr);
99 
100  bool extractValue(const DWARFDataExtractor &Data, uint64_t *OffsetPtr,
101  dwarf::FormParams FormParams, const DWARFUnit *U) {
102  return extractValue(Data, OffsetPtr, FormParams, nullptr, U);
103  }
104 
105  /// getAsFoo functions below return the extracted value as Foo if only
106  /// DWARFFormValue has form class is suitable for representing Foo.
108  struct UnitOffset {
111  };
122  /// Correctly extract any file paths from a form value.
123  ///
124  /// These attributes can be in the from DW_AT_decl_file or DW_AT_call_file
125  /// attributes. We need to use the file index in the correct DWARFUnit's line
126  /// table prologue, and each DWARFFormValue has the DWARFUnit the form value
127  /// was extracted from.
128  ///
129  /// \param Kind The kind of path to extract.
130  ///
131  /// \returns A valid string value on success, or llvm::None if the form class
132  /// is not FC_Constant, or if the file index is not valid.
135 
136  /// Skip a form's value in \p DebugInfoData at the offset specified by
137  /// \p OffsetPtr.
138  ///
139  /// Skips the bytes for the current form and updates the offset.
140  ///
141  /// \param DebugInfoData The data where we want to skip the value.
142  /// \param OffsetPtr A reference to the offset that will be updated.
143  /// \param Params DWARF parameters to help interpret forms.
144  /// \returns true on success, false if the form was not skipped.
145  bool skipValue(DataExtractor DebugInfoData, uint64_t *OffsetPtr,
146  const dwarf::FormParams Params) const {
147  return DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, Params);
148  }
149 
150  /// Skip a form's value in \p DebugInfoData at the offset specified by
151  /// \p OffsetPtr.
152  ///
153  /// Skips the bytes for the specified form and updates the offset.
154  ///
155  /// \param Form The DW_FORM enumeration that indicates the form to skip.
156  /// \param DebugInfoData The data where we want to skip the value.
157  /// \param OffsetPtr A reference to the offset that will be updated.
158  /// \param FormParams DWARF parameters to help interpret forms.
159  /// \returns true on success, false if the form was not skipped.
160  static bool skipValue(dwarf::Form Form, DataExtractor DebugInfoData,
161  uint64_t *OffsetPtr,
162  const dwarf::FormParams FormParams);
163 
164 private:
165  void dumpString(raw_ostream &OS) const;
166 };
167 
168 namespace dwarf {
169 
170 /// Take an optional DWARFFormValue and try to extract a string value from it.
171 ///
172 /// \param V and optional DWARFFormValue to attempt to extract the value from.
173 /// \returns an optional value that contains a value if the form value
174 /// was valid and was a string.
176  if (V)
177  return V->getAsCString();
178  return None;
179 }
180 
181 /// Take an optional DWARFFormValue and try to extract a string value from it.
182 ///
183 /// \param V and optional DWARFFormValue to attempt to extract the value from.
184 /// \returns an optional value that contains a value if the form value
185 /// was valid and was a string.
187  StringRef Default = {}) {
188  if (V)
189  if (auto S = V->getAsCString())
190  return *S;
191  return Default;
192 }
193 
194 /// Take an optional DWARFFormValue and extract a string value from it.
195 ///
196 /// \param V and optional DWARFFormValue to attempt to extract the value from.
197 /// \param Default the default value to return in case of failure.
198 /// \returns the string value or Default if the V doesn't have a value or the
199 /// form value's encoding wasn't a string.
200 inline const char *toString(const Optional<DWARFFormValue> &V,
201  const char *Default) {
202  return toString(V).getValueOr(Default);
203 }
204 
205 /// Take an optional DWARFFormValue and try to extract an unsigned constant.
206 ///
207 /// \param V and optional DWARFFormValue to attempt to extract the value from.
208 /// \returns an optional value that contains a value if the form value
209 /// was valid and has a unsigned constant form.
211  if (V)
212  return V->getAsUnsignedConstant();
213  return None;
214 }
215 
216 /// Take an optional DWARFFormValue and extract a unsigned constant.
217 ///
218 /// \param V and optional DWARFFormValue to attempt to extract the value from.
219 /// \param Default the default value to return in case of failure.
220 /// \returns the extracted unsigned value or Default if the V doesn't have a
221 /// value or the form value's encoding wasn't an unsigned constant form.
223  uint64_t Default) {
224  return toUnsigned(V).getValueOr(Default);
225 }
226 
227 /// Take an optional DWARFFormValue and try to extract an reference.
228 ///
229 /// \param V and optional DWARFFormValue to attempt to extract the value from.
230 /// \returns an optional value that contains a value if the form value
231 /// was valid and has a reference form.
233  if (V)
234  return V->getAsReference();
235  return None;
236 }
237 
238 /// Take an optional DWARFFormValue and extract a reference.
239 ///
240 /// \param V and optional DWARFFormValue to attempt to extract the value from.
241 /// \param Default the default value to return in case of failure.
242 /// \returns the extracted reference value or Default if the V doesn't have a
243 /// value or the form value's encoding wasn't a reference form.
245  uint64_t Default) {
246  return toReference(V).getValueOr(Default);
247 }
248 
249 /// Take an optional DWARFFormValue and try to extract an signed constant.
250 ///
251 /// \param V and 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 signed constant form.
255  if (V)
256  return V->getAsSignedConstant();
257  return None;
258 }
259 
260 /// Take an optional DWARFFormValue and extract a signed integer.
261 ///
262 /// \param V and optional DWARFFormValue to attempt to extract the value from.
263 /// \param Default the default value to return in case of failure.
264 /// \returns the extracted signed integer value or Default if the V doesn't
265 /// have a value or the form value's encoding wasn't a signed integer form.
266 inline int64_t toSigned(const Optional<DWARFFormValue> &V, int64_t Default) {
267  return toSigned(V).getValueOr(Default);
268 }
269 
270 /// Take an optional DWARFFormValue and try to extract an address.
271 ///
272 /// \param V and optional DWARFFormValue to attempt to extract the value from.
273 /// \returns an optional value that contains a value if the form value
274 /// was valid and has a address form.
276  if (V)
277  return V->getAsAddress();
278  return None;
279 }
280 
283  if (V)
284  return V->getAsSectionedAddress();
285  return None;
286 }
287 
288 /// Take an optional DWARFFormValue and extract a address.
289 ///
290 /// \param V and optional DWARFFormValue to attempt to extract the value from.
291 /// \param Default the default value to return in case of failure.
292 /// \returns the extracted address value or Default if the V doesn't have a
293 /// value or the form value's encoding wasn't an address form.
295  return toAddress(V).getValueOr(Default);
296 }
297 
298 /// Take an optional DWARFFormValue and try to extract an section offset.
299 ///
300 /// \param V and 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 section offset form.
304  if (V)
305  return V->getAsSectionOffset();
306  return None;
307 }
308 
309 /// Take an optional DWARFFormValue and extract a section offset.
310 ///
311 /// \param V and optional DWARFFormValue to attempt to extract the value from.
312 /// \param Default the default value to return in case of failure.
313 /// \returns the extracted section offset value or Default if the V doesn't
314 /// have a value or the form value's encoding wasn't a section offset form.
316  uint64_t Default) {
317  return toSectionOffset(V).getValueOr(Default);
318 }
319 
320 /// Take an optional DWARFFormValue and try to extract block data.
321 ///
322 /// \param V and optional DWARFFormValue to attempt to extract the value from.
323 /// \returns an optional value that contains a value if the form value
324 /// was valid and has a block form.
326  if (V)
327  return V->getAsBlock();
328  return None;
329 }
330 
331 } // end namespace dwarf
332 
333 } // end namespace llvm
334 
335 #endif // LLVM_DEBUGINFO_DWARF_DWARFFORMVALUE_H
llvm::DWARFFormValue::getUnit
const DWARFUnit * getUnit() const
Definition: DWARFFormValue.h:81
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
Optional.h
llvm::DWARFFormValue::getAsSectionedAddress
Optional< object::SectionedAddress > getAsSectionedAddress() const
Definition: DWARFFormValue.cpp:669
llvm::DWARFFormValue::dumpAddressSection
static void dumpAddressSection(const DWARFObject &Obj, raw_ostream &OS, DIDumpOptions DumpOpts, uint64_t SectionIndex)
Definition: DWARFFormValue.cpp:390
llvm::dwarf::Form
Form
Definition: Dwarf.h:131
llvm::DWARFFormValue::getAsAddress
Optional< uint64_t > getAsAddress() const
Definition: DWARFFormValue.cpp:662
llvm::DWARFFormValue::FC_Constant
@ FC_Constant
Definition: DWARFFormValue.h:32
R600_InstFlag::FC
@ FC
Definition: R600Defines.h:32
llvm::DWARFFormValue::getAsSignedConstant
Optional< int64_t > getAsSignedConstant() const
Definition: DWARFFormValue.cpp:728
llvm::dwarf::toSectionedAddress
Optional< object::SectionedAddress > toSectionedAddress(const Optional< DWARFFormValue > &V)
Definition: DWARFFormValue.h:282
llvm::DWARFFormValue::getAsCString
Optional< const char * > getAsCString() const
Definition: DWARFFormValue.cpp:625
llvm::DWARFFormValue::createFromUValue
static DWARFFormValue createFromUValue(dwarf::Form F, uint64_t V)
Definition: DWARFFormValue.cpp:84
llvm::DWARFFormValue::getAsUnsignedConstant
Optional< uint64_t > getAsUnsignedConstant() const
Definition: DWARFFormValue.cpp:721
llvm::DWARFContext
DWARFContext This data structure is the top level entity that deals with dwarf debug information pars...
Definition: DWARFContext.h:53
llvm::DWARFFormValue::FC_Exprloc
@ FC_Exprloc
Definition: DWARFFormValue.h:38
llvm::Optional< uint64_t >
llvm::dwarf::toBlock
Optional< ArrayRef< uint8_t > > toBlock(const Optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract block data.
Definition: DWARFFormValue.h:325
llvm::DWARFFormValue::FC_Reference
@ FC_Reference
Definition: DWARFFormValue.h:35
llvm::DWARFDataExtractor
A DataExtractor (typically for an in-memory copy of an object-file section) plus a relocation map for...
Definition: DWARFDataExtractor.h:21
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm::DWARFFormValue::FC_Indirect
@ FC_Indirect
Definition: DWARFFormValue.h:36
llvm::dwarf::toAddress
Optional< uint64_t > toAddress(const Optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an address.
Definition: DWARFFormValue.h:275
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::DWARFFormValue::createFromPValue
static DWARFFormValue createFromPValue(dwarf::Form F, const char *V)
Definition: DWARFFormValue.cpp:88
llvm::DWARFFormValue::DWARFFormValue
DWARFFormValue(dwarf::Form F=dwarf::Form(0))
Definition: DWARFFormValue.h:67
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
llvm::DWARFFormValue::createFromSValue
static DWARFFormValue createFromSValue(dwarf::Form F, int64_t V)
Definition: DWARFFormValue.cpp:80
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::DWARFFormValue::FC_SectionOffset
@ FC_SectionOffset
Definition: DWARFFormValue.h:37
llvm::DWARFFormValue::dump
void dump(raw_ostream &OS, DIDumpOptions DumpOpts=DIDumpOptions()) const
Definition: DWARFFormValue.cpp:405
llvm::DWARFFormValue::UnitOffset::Unit
DWARFUnit * Unit
Definition: DWARFFormValue.h:109
llvm::DWARFFormValue::getAsSectionOffset
Optional< uint64_t > getAsSectionOffset() const
Definition: DWARFFormValue.cpp:715
llvm::DWARFFormValue::getAsCStringOffset
Optional< uint64_t > getAsCStringOffset() const
Definition: DWARFFormValue.cpp:754
llvm::dwarf::toStringRef
StringRef toStringRef(const Optional< DWARFFormValue > &V, StringRef Default={})
Take an optional DWARFFormValue and try to extract a string value from it.
Definition: DWARFFormValue.h:186
llvm::dwarf::toSectionOffset
Optional< uint64_t > toSectionOffset(const Optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an section offset.
Definition: DWARFFormValue.h:303
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::DWARFObject
Definition: DWARFObject.h:25
llvm::DWARFFormValue
Definition: DWARFFormValue.h:26
llvm::dwarf::DwarfFormat
DwarfFormat
Constants that define the DWARF format as 32 or 64 bit.
Definition: Dwarf.h:92
llvm::None
const NoneType None
Definition: None.h:23
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::DWARFFormValue::skipValue
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.
Definition: DWARFFormValue.h:145
llvm::DWARFFormValue::getAsRelativeReference
Optional< UnitOffset > getAsRelativeReference() const
Definition: DWARFFormValue.cpp:694
llvm::Optional::getValueOr
constexpr T getValueOr(U &&value) const LLVM_LVALUE_FUNCTION
Definition: Optional.h:297
llvm::dwarf::toReference
Optional< uint64_t > toReference(const Optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an reference.
Definition: DWARFFormValue.h:232
uint64_t
D
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
llvm::DWARFFormValue::FC_String
@ FC_String
Definition: DWARFFormValue.h:33
llvm::DWARFFormValue::createFromBlockValue
static DWARFFormValue createFromBlockValue(dwarf::Form F, ArrayRef< uint8_t > D)
Definition: DWARFFormValue.cpp:92
llvm::DWARFFormValue::FormClass
FormClass
Definition: DWARFFormValue.h:28
llvm::DILineInfoSpecifier::FileLineInfoKind
FileLineInfoKind
Definition: DIContext.h:136
ArrayRef.h
llvm::dwarf::FormParams
A helper struct providing information about the byte size of DW_FORM values that vary in size dependi...
Definition: Dwarf.h:648
llvm::DWARFFormValue::FC_Block
@ FC_Block
Definition: DWARFFormValue.h:31
llvm::DWARFFormValue::FC_Flag
@ FC_Flag
Definition: DWARFFormValue.h:34
llvm::dwarf::toString
Optional< const char * > toString(const Optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
Definition: DWARFFormValue.h:175
llvm::ArrayRef< uint8_t >
None.h
llvm::DWARFFormValue::dumpAddress
void dumpAddress(raw_ostream &OS, uint64_t Address) const
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
Dwarf.h
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::DWARFFormValue::getRawUValue
uint64_t getRawUValue() const
Definition: DWARFFormValue.h:78
llvm::ValueType
PointerUnion< const Value *, const PseudoSourceValue * > ValueType
Definition: ScheduleDAGInstrs.h:106
llvm::PointerUnion< const Value *, const PseudoSourceValue * >
DIContext.h
llvm::DWARFUnit
Definition: DWARFUnit.h:203
llvm::DWARFFormValue::extractValue
bool extractValue(const DWARFDataExtractor &Data, uint64_t *OffsetPtr, dwarf::FormParams FormParams, const DWARFUnit *U)
Definition: DWARFFormValue.h:100
llvm::DWARFFormValue::getAsBlock
Optional< ArrayRef< uint8_t > > getAsBlock() const
Definition: DWARFFormValue.cpp:747
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
llvm::DWARFFormValue::FC_Address
@ FC_Address
Definition: DWARFFormValue.h:30
llvm::dwarf::toUnsigned
Optional< uint64_t > toUnsigned(const Optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an unsigned constant.
Definition: DWARFFormValue.h:210
llvm::DWARFFormValue::extractValue
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.
Definition: DWARFFormValue.cpp:244
llvm::DWARFFormValue::FC_Unknown
@ FC_Unknown
Definition: DWARFFormValue.h:29
llvm::DataExtractor
Definition: DataExtractor.h:41
llvm::DWARFFormValue::isFormClass
bool isFormClass(FormClass FC) const
Definition: DWARFFormValue.cpp:212
llvm::object::SectionedAddress
Definition: ObjectFile.h:144
llvm::DWARFFormValue::getForm
dwarf::Form getForm() const
Definition: DWARFFormValue.h:77
llvm::DWARFFormValue::getAsReference
Optional< uint64_t > getAsReference() const
getAsFoo functions below return the extracted value as Foo if only DWARFFormValue has form class is s...
Definition: DWARFFormValue.cpp:688
llvm::DWARFFormValue::getAsReferenceUVal
Optional< uint64_t > getAsReferenceUVal() const
Definition: DWARFFormValue.cpp:760
llvm::DWARFFormValue::createFromUnit
static DWARFFormValue createFromUnit(dwarf::Form F, const DWARFUnit *Unit, uint64_t *OffsetPtr)
Definition: DWARFFormValue.cpp:100
llvm::DWARFFormValue::getAsFile
Optional< std::string > getAsFile(DILineInfoSpecifier::FileLineInfoKind Kind) const
Correctly extract any file paths from a form value.
Definition: DWARFFormValue.cpp:767
llvm::DWARFFormValue::UnitOffset
Definition: DWARFFormValue.h:108
llvm::DWARFFormValue::dumpSectionedAddress
void dumpSectionedAddress(raw_ostream &OS, DIDumpOptions DumpOpts, object::SectionedAddress SA) const
Definition: DWARFFormValue.cpp:382
llvm::DWARFFormValue::UnitOffset::Offset
uint64_t Offset
Definition: DWARFFormValue.h:110
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::dwarf::toSigned
Optional< int64_t > toSigned(const Optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an signed constant.
Definition: DWARFFormValue.h:254
llvm::dwarf::DWARF32
@ DWARF32
Definition: Dwarf.h:92
DWARFDataExtractor.h
llvm::DIDumpOptions
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:180