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);
70  static DWARFFormValue createFromUValue(dwarf::Form F, uint64_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  bool isInlinedCStr() const {
106  return Value.data != nullptr && Value.data == (const uint8_t *)Value.cstr;
107  }
108 
109  /// getAsFoo functions below return the extracted value as Foo if only
110  /// DWARFFormValue has form class is suitable for representing Foo.
112  struct UnitOffset {
114  uint64_t Offset;
115  };
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 current form and updates the offset.
131  ///
132  /// \param DebugInfoData The data where we want to skip the value.
133  /// \param OffsetPtr A reference to the offset that will be updated.
134  /// \param Params DWARF parameters to help interpret forms.
135  /// \returns true on success, false if the form was not skipped.
136  bool skipValue(DataExtractor DebugInfoData, uint64_t *OffsetPtr,
137  const dwarf::FormParams Params) const {
138  return DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, Params);
139  }
140 
141  /// Skip a form's value in \p DebugInfoData at the offset specified by
142  /// \p OffsetPtr.
143  ///
144  /// Skips the bytes for the specified form and updates the offset.
145  ///
146  /// \param Form The DW_FORM enumeration that indicates the form to skip.
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 FormParams DWARF parameters to help interpret forms.
150  /// \returns true on success, false if the form was not skipped.
151  static bool skipValue(dwarf::Form Form, DataExtractor DebugInfoData,
152  uint64_t *OffsetPtr,
153  const dwarf::FormParams FormParams);
154 
155 private:
156  void dumpString(raw_ostream &OS) const;
157 };
158 
159 namespace dwarf {
160 
161 /// Take an optional DWARFFormValue and try to extract a string value from it.
162 ///
163 /// \param V and optional DWARFFormValue to attempt to extract the value from.
164 /// \returns an optional value that contains a value if the form value
165 /// was valid and was a string.
167  if (V)
168  return V->getAsCString();
169  return None;
170 }
171 
172 /// Take an optional DWARFFormValue and try to extract a string value from it.
173 ///
174 /// \param V and optional DWARFFormValue to attempt to extract the value from.
175 /// \returns an optional value that contains a value if the form value
176 /// was valid and was a string.
178  StringRef Default = {}) {
179  if (V)
180  if (auto S = V->getAsCString())
181  return *S;
182  return Default;
183 }
184 
185 /// Take an optional DWARFFormValue and extract a string value from it.
186 ///
187 /// \param V and optional DWARFFormValue to attempt to extract the value from.
188 /// \param Default the default value to return in case of failure.
189 /// \returns the string value or Default if the V doesn't have a value or the
190 /// form value's encoding wasn't a string.
191 inline const char *toString(const Optional<DWARFFormValue> &V,
192  const char *Default) {
193  return toString(V).getValueOr(Default);
194 }
195 
196 /// Take an optional DWARFFormValue and try to extract an unsigned constant.
197 ///
198 /// \param V and optional DWARFFormValue to attempt to extract the value from.
199 /// \returns an optional value that contains a value if the form value
200 /// was valid and has a unsigned constant form.
202  if (V)
203  return V->getAsUnsignedConstant();
204  return None;
205 }
206 
207 /// Take an optional DWARFFormValue and extract a unsigned constant.
208 ///
209 /// \param V and optional DWARFFormValue to attempt to extract the value from.
210 /// \param Default the default value to return in case of failure.
211 /// \returns the extracted unsigned value or Default if the V doesn't have a
212 /// value or the form value's encoding wasn't an unsigned constant form.
213 inline uint64_t toUnsigned(const Optional<DWARFFormValue> &V,
214  uint64_t Default) {
215  return toUnsigned(V).getValueOr(Default);
216 }
217 
218 /// Take an optional DWARFFormValue and try to extract an reference.
219 ///
220 /// \param V and optional DWARFFormValue to attempt to extract the value from.
221 /// \returns an optional value that contains a value if the form value
222 /// was valid and has a reference form.
224  if (V)
225  return V->getAsReference();
226  return None;
227 }
228 
229 /// Take an optional DWARFFormValue and extract a reference.
230 ///
231 /// \param V and optional DWARFFormValue to attempt to extract the value from.
232 /// \param Default the default value to return in case of failure.
233 /// \returns the extracted reference value or Default if the V doesn't have a
234 /// value or the form value's encoding wasn't a reference form.
235 inline uint64_t toReference(const Optional<DWARFFormValue> &V,
236  uint64_t Default) {
237  return toReference(V).getValueOr(Default);
238 }
239 
240 /// Take an optional DWARFFormValue and try to extract an signed constant.
241 ///
242 /// \param V and optional DWARFFormValue to attempt to extract the value from.
243 /// \returns an optional value that contains a value if the form value
244 /// was valid and has a signed constant form.
246  if (V)
247  return V->getAsSignedConstant();
248  return None;
249 }
250 
251 /// Take an optional DWARFFormValue and extract a signed integer.
252 ///
253 /// \param V and optional DWARFFormValue to attempt to extract the value from.
254 /// \param Default the default value to return in case of failure.
255 /// \returns the extracted signed integer value or Default if the V doesn't
256 /// have a value or the form value's encoding wasn't a signed integer form.
257 inline int64_t toSigned(const Optional<DWARFFormValue> &V, int64_t Default) {
258  return toSigned(V).getValueOr(Default);
259 }
260 
261 /// Take an optional DWARFFormValue and try to extract an address.
262 ///
263 /// \param V and optional DWARFFormValue to attempt to extract the value from.
264 /// \returns an optional value that contains a value if the form value
265 /// was valid and has a address form.
267  if (V)
268  return V->getAsAddress();
269  return None;
270 }
271 
274  if (V)
275  return V->getAsSectionedAddress();
276  return None;
277 }
278 
279 /// Take an optional DWARFFormValue and extract a address.
280 ///
281 /// \param V and optional DWARFFormValue to attempt to extract the value from.
282 /// \param Default the default value to return in case of failure.
283 /// \returns the extracted address value or Default if the V doesn't have a
284 /// value or the form value's encoding wasn't an address form.
285 inline uint64_t toAddress(const Optional<DWARFFormValue> &V, uint64_t Default) {
286  return toAddress(V).getValueOr(Default);
287 }
288 
289 /// Take an optional DWARFFormValue and try to extract an section offset.
290 ///
291 /// \param V and optional DWARFFormValue to attempt to extract the value from.
292 /// \returns an optional value that contains a value if the form value
293 /// was valid and has a section offset form.
295  if (V)
296  return V->getAsSectionOffset();
297  return None;
298 }
299 
300 /// Take an optional DWARFFormValue and extract a section offset.
301 ///
302 /// \param V and optional DWARFFormValue to attempt to extract the value from.
303 /// \param Default the default value to return in case of failure.
304 /// \returns the extracted section offset value or Default if the V doesn't
305 /// have a value or the form value's encoding wasn't a section offset form.
306 inline uint64_t toSectionOffset(const Optional<DWARFFormValue> &V,
307  uint64_t Default) {
308  return toSectionOffset(V).getValueOr(Default);
309 }
310 
311 /// Take an optional DWARFFormValue and try to extract block data.
312 ///
313 /// \param V and optional DWARFFormValue to attempt to extract the value from.
314 /// \returns an optional value that contains a value if the form value
315 /// was valid and has a block form.
317  if (V)
318  return V->getAsBlock();
319  return None;
320 }
321 
322 } // end namespace dwarf
323 
324 } // end namespace llvm
325 
326 #endif // LLVM_DEBUGINFO_DWARF_DWARFFORMVALUE_H
llvm::DWARFFormValue::getUnit
const DWARFUnit * getUnit() const
Definition: DWARFFormValue.h:81
llvm
---------------------— PointerInfo ------------------------------------—
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::isInlinedCStr
bool isInlinedCStr() const
Definition: DWARFFormValue.h:105
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:273
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:316
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:56
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:266
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:113
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:177
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:294
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::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:136
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:223
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
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:166
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:201
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::UnitOffset
Definition: DWARFFormValue.h:112
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:114
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
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:245
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