LLVM  16.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 DWARFObject;
24 class DWARFDataExtractor;
25 class DWARFUnit;
26 class raw_ostream;
27 
29 public:
30  enum FormClass {
41  };
42 
43 private:
44  struct ValueType {
45  ValueType() { uval = 0; }
46  ValueType(int64_t V) : sval(V) {}
47  ValueType(uint64_t V) : uval(V) {}
48  ValueType(const char *V) : cstr(V) {}
49 
50  union {
51  uint64_t uval;
52  int64_t sval;
53  const char *cstr;
54  };
55  const uint8_t *data = nullptr;
56  uint64_t SectionIndex; /// Section index for reference forms.
57  };
58 
59  dwarf::Form Form; /// Form for this value.
60  dwarf::DwarfFormat Format =
61  dwarf::DWARF32; /// Remember the DWARF format at extract time.
62  ValueType Value; /// Contains all data for the form.
63  const DWARFUnit *U = nullptr; /// Remember the DWARFUnit at extract time.
64  const DWARFContext *C = nullptr; /// Context for extract time.
65 
66  DWARFFormValue(dwarf::Form F, ValueType V) : Form(F), Value(V) {}
67 
68 public:
70 
71  static DWARFFormValue createFromSValue(dwarf::Form F, int64_t V);
73  static DWARFFormValue createFromPValue(dwarf::Form F, const char *V);
77  uint64_t *OffsetPtr);
78 
79  dwarf::Form getForm() const { return Form; }
80  uint64_t getRawUValue() const { return Value.uval; }
81 
82  bool isFormClass(FormClass FC) const;
83  const DWARFUnit *getUnit() const { return U; }
84  void dump(raw_ostream &OS, DIDumpOptions DumpOpts = DIDumpOptions()) const;
86  object::SectionedAddress SA) const;
87  void dumpAddress(raw_ostream &OS, uint64_t Address) const;
88  static void dumpAddress(raw_ostream &OS, uint8_t AddressSize,
89  uint64_t Address);
90  static void dumpAddressSection(const DWARFObject &Obj, raw_ostream &OS,
91  DIDumpOptions DumpOpts, uint64_t SectionIndex);
92 
93  /// Extracts a value in \p Data at offset \p *OffsetPtr. The information
94  /// in \p FormParams is needed to interpret some forms. The optional
95  /// \p Context and \p Unit allows extracting information if the form refers
96  /// to other sections (e.g., .debug_str).
97  bool extractValue(const DWARFDataExtractor &Data, uint64_t *OffsetPtr,
99  const DWARFContext *Context = nullptr,
100  const DWARFUnit *Unit = nullptr);
101 
102  bool extractValue(const DWARFDataExtractor &Data, uint64_t *OffsetPtr,
104  return extractValue(Data, OffsetPtr, FormParams, nullptr, U);
105  }
106 
107  /// getAsFoo functions below return the extracted value as Foo if only
108  /// DWARFFormValue has form class is suitable for representing Foo.
110  struct UnitOffset {
113  };
124  /// Correctly extract any file paths from a form value.
125  ///
126  /// These attributes can be in the from DW_AT_decl_file or DW_AT_call_file
127  /// attributes. We need to use the file index in the correct DWARFUnit's line
128  /// table prologue, and each DWARFFormValue has the DWARFUnit the form value
129  /// was extracted from.
130  ///
131  /// \param Kind The kind of path to extract.
132  ///
133  /// \returns A valid string value on success, or llvm::None if the form class
134  /// is not FC_Constant, or if the file index is not valid.
137 
138  /// Skip a form's value in \p DebugInfoData at the offset specified by
139  /// \p OffsetPtr.
140  ///
141  /// Skips the bytes for the current form and updates the offset.
142  ///
143  /// \param DebugInfoData The data where we want to skip the value.
144  /// \param OffsetPtr A reference to the offset that will be updated.
145  /// \param Params DWARF parameters to help interpret forms.
146  /// \returns true on success, false if the form was not skipped.
147  bool skipValue(DataExtractor DebugInfoData, uint64_t *OffsetPtr,
148  const dwarf::FormParams Params) const {
149  return DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, Params);
150  }
151 
152  /// Skip a form's value in \p DebugInfoData at the offset specified by
153  /// \p OffsetPtr.
154  ///
155  /// Skips the bytes for the specified form and updates the offset.
156  ///
157  /// \param Form The DW_FORM enumeration that indicates the form to skip.
158  /// \param DebugInfoData The data where we want to skip the value.
159  /// \param OffsetPtr A reference to the offset that will be updated.
160  /// \param FormParams DWARF parameters to help interpret forms.
161  /// \returns true on success, false if the form was not skipped.
162  static bool skipValue(dwarf::Form Form, DataExtractor DebugInfoData,
163  uint64_t *OffsetPtr,
165 
166 private:
167  void dumpString(raw_ostream &OS) const;
168 };
169 
170 namespace dwarf {
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  if (!V)
179  return None;
180  Expected<const char*> E = V->getAsCString();
181  if (!E) {
182  consumeError(E.takeError());
183  return None;
184  }
185  return *E;
186 }
187 
188 /// Take an optional DWARFFormValue and try to extract a string value from it.
189 ///
190 /// \param V and optional DWARFFormValue to attempt to extract the value from.
191 /// \returns an optional value that contains a value if the form value
192 /// was valid and was a string.
194  StringRef Default = {}) {
195  if (!V)
196  return Default;
197  auto S = V->getAsCString();
198  if (!S) {
199  consumeError(S.takeError());
200  return Default;
201  }
202  if (!*S)
203  return Default;
204  return *S;
205 }
206 
207 /// Take an optional DWARFFormValue and extract a string value from it.
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 string value or Default if the V doesn't have a value or the
212 /// form value's encoding wasn't a string.
213 inline const char *toString(const Optional<DWARFFormValue> &V,
214  const char *Default) {
215  if (auto E = toString(V))
216  return *E;
217  return Default;
218 }
219 
220 /// Take an optional DWARFFormValue and try to extract an unsigned constant.
221 ///
222 /// \param V and optional DWARFFormValue to attempt to extract the value from.
223 /// \returns an optional value that contains a value if the form value
224 /// was valid and has a unsigned constant form.
226  if (V)
227  return V->getAsUnsignedConstant();
228  return None;
229 }
230 
231 /// Take an optional DWARFFormValue and extract a unsigned constant.
232 ///
233 /// \param V and optional DWARFFormValue to attempt to extract the value from.
234 /// \param Default the default value to return in case of failure.
235 /// \returns the extracted unsigned value or Default if the V doesn't have a
236 /// value or the form value's encoding wasn't an unsigned constant form.
238  uint64_t Default) {
239  return toUnsigned(V).value_or(Default);
240 }
241 
242 /// Take an optional DWARFFormValue and try to extract an reference.
243 ///
244 /// \param V and optional DWARFFormValue to attempt to extract the value from.
245 /// \returns an optional value that contains a value if the form value
246 /// was valid and has a reference form.
248  if (V)
249  return V->getAsReference();
250  return None;
251 }
252 
253 /// Take an optional DWARFFormValue and extract a reference.
254 ///
255 /// \param V and optional DWARFFormValue to attempt to extract the value from.
256 /// \param Default the default value to return in case of failure.
257 /// \returns the extracted reference value or Default if the V doesn't have a
258 /// value or the form value's encoding wasn't a reference form.
260  uint64_t Default) {
261  return toReference(V).value_or(Default);
262 }
263 
264 /// Take an optional DWARFFormValue and try to extract an signed constant.
265 ///
266 /// \param V and optional DWARFFormValue to attempt to extract the value from.
267 /// \returns an optional value that contains a value if the form value
268 /// was valid and has a signed constant form.
270  if (V)
271  return V->getAsSignedConstant();
272  return None;
273 }
274 
275 /// Take an optional DWARFFormValue and extract a signed integer.
276 ///
277 /// \param V and optional DWARFFormValue to attempt to extract the value from.
278 /// \param Default the default value to return in case of failure.
279 /// \returns the extracted signed integer value or Default if the V doesn't
280 /// have a value or the form value's encoding wasn't a signed integer form.
281 inline int64_t toSigned(const Optional<DWARFFormValue> &V, int64_t Default) {
282  return toSigned(V).value_or(Default);
283 }
284 
285 /// Take an optional DWARFFormValue and try to extract an address.
286 ///
287 /// \param V and optional DWARFFormValue to attempt to extract the value from.
288 /// \returns an optional value that contains a value if the form value
289 /// was valid and has a address form.
291  if (V)
292  return V->getAsAddress();
293  return None;
294 }
295 
298  if (V)
299  return V->getAsSectionedAddress();
300  return None;
301 }
302 
303 /// Take an optional DWARFFormValue and extract a address.
304 ///
305 /// \param V and optional DWARFFormValue to attempt to extract the value from.
306 /// \param Default the default value to return in case of failure.
307 /// \returns the extracted address value or Default if the V doesn't have a
308 /// value or the form value's encoding wasn't an address form.
310  return toAddress(V).value_or(Default);
311 }
312 
313 /// Take an optional DWARFFormValue and try to extract an section offset.
314 ///
315 /// \param V and optional DWARFFormValue to attempt to extract the value from.
316 /// \returns an optional value that contains a value if the form value
317 /// was valid and has a section offset form.
319  if (V)
320  return V->getAsSectionOffset();
321  return None;
322 }
323 
324 /// Take an optional DWARFFormValue and extract a section offset.
325 ///
326 /// \param V and optional DWARFFormValue to attempt to extract the value from.
327 /// \param Default the default value to return in case of failure.
328 /// \returns the extracted section offset value or Default if the V doesn't
329 /// have a value or the form value's encoding wasn't a section offset form.
331  uint64_t Default) {
332  return toSectionOffset(V).value_or(Default);
333 }
334 
335 /// Take an optional DWARFFormValue and try to extract block data.
336 ///
337 /// \param V and optional DWARFFormValue to attempt to extract the value from.
338 /// \returns an optional value that contains a value if the form value
339 /// was valid and has a block form.
341  if (V)
342  return V->getAsBlock();
343  return None;
344 }
345 
346 } // end namespace dwarf
347 
348 } // end namespace llvm
349 
350 #endif // LLVM_DEBUGINFO_DWARF_DWARFFORMVALUE_H
llvm::DWARFFormValue::getUnit
const DWARFUnit * getUnit() const
Definition: DWARFFormValue.h:83
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Optional.h
llvm::DWARFFormValue::getAsSectionedAddress
Optional< object::SectionedAddress > getAsSectionedAddress() const
Definition: DWARFFormValue.cpp:676
llvm::DWARFFormValue::dumpAddressSection
static void dumpAddressSection(const DWARFObject &Obj, raw_ostream &OS, DIDumpOptions DumpOpts, uint64_t SectionIndex)
Definition: DWARFFormValue.cpp:394
llvm::dwarf::Form
Form
Definition: Dwarf.h:132
llvm::DWARFFormValue::getAsAddress
Optional< uint64_t > getAsAddress() const
Definition: DWARFFormValue.cpp:669
llvm::DWARFFormValue::FC_Constant
@ FC_Constant
Definition: DWARFFormValue.h:34
R600_InstFlag::FC
@ FC
Definition: R600Defines.h:32
llvm::DWARFFormValue::getAsSignedConstant
Optional< int64_t > getAsSignedConstant() const
Definition: DWARFFormValue.cpp:735
llvm::dwarf::toSectionedAddress
Optional< object::SectionedAddress > toSectionedAddress(const Optional< DWARFFormValue > &V)
Definition: DWARFFormValue.h:297
llvm::DWARFFormValue::createFromUValue
static DWARFFormValue createFromUValue(dwarf::Form F, uint64_t V)
Definition: DWARFFormValue.cpp:88
llvm::DWARFFormValue::getAsUnsignedConstant
Optional< uint64_t > getAsUnsignedConstant() const
Definition: DWARFFormValue.cpp:728
llvm::DWARFContext
DWARFContext This data structure is the top level entity that deals with dwarf debug information pars...
Definition: DWARFContext.h:47
llvm::DWARFFormValue::FC_Exprloc
@ FC_Exprloc
Definition: DWARFFormValue.h:40
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:340
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
llvm::DWARFFormValue::FC_Reference
@ FC_Reference
Definition: DWARFFormValue.h:37
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::consumeError
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1042
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm::DWARFFormValue::FC_Indirect
@ FC_Indirect
Definition: DWARFFormValue.h:38
llvm::dwarf::toAddress
Optional< uint64_t > toAddress(const Optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an address.
Definition: DWARFFormValue.h:290
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::DWARFFormValue::createFromPValue
static DWARFFormValue createFromPValue(dwarf::Form F, const char *V)
Definition: DWARFFormValue.cpp:92
llvm::DWARFFormValue::DWARFFormValue
DWARFFormValue(dwarf::Form F=dwarf::Form(0))
Definition: DWARFFormValue.h:69
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
llvm::DWARFFormValue::createFromSValue
static DWARFFormValue createFromSValue(dwarf::Form F, int64_t V)
Definition: DWARFFormValue.cpp:84
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::DWARFFormValue::FC_SectionOffset
@ FC_SectionOffset
Definition: DWARFFormValue.h:39
llvm::DWARFFormValue::dump
void dump(raw_ostream &OS, DIDumpOptions DumpOpts=DIDumpOptions()) const
Definition: DWARFFormValue.cpp:409
llvm::DWARFFormValue::UnitOffset::Unit
DWARFUnit * Unit
Definition: DWARFFormValue.h:111
llvm::DWARFFormValue::getAsSectionOffset
Optional< uint64_t > getAsSectionOffset() const
Definition: DWARFFormValue.cpp:722
llvm::DWARFFormValue::getAsCStringOffset
Optional< uint64_t > getAsCStringOffset() const
Definition: DWARFFormValue.cpp:761
llvm::DWARFFormValue::getAsCString
Expected< const char * > getAsCString() const
Definition: DWARFFormValue.cpp:628
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:193
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:318
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:26
llvm::DWARFFormValue
Definition: DWARFFormValue.h:28
llvm::dwarf::DwarfFormat
DwarfFormat
Constants that define the DWARF format as 32 or 64 bit.
Definition: Dwarf.h:93
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:147
llvm::DWARFFormValue::getAsRelativeReference
Optional< UnitOffset > getAsRelativeReference() const
Definition: DWARFFormValue.cpp:701
llvm::dwarf::toReference
Optional< uint64_t > toReference(const Optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an reference.
Definition: DWARFFormValue.h:247
uint64_t
D
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
llvm::DWARFFormValue::FC_String
@ FC_String
Definition: DWARFFormValue.h:35
llvm::DWARFFormValue::createFromBlockValue
static DWARFFormValue createFromBlockValue(dwarf::Form F, ArrayRef< uint8_t > D)
Definition: DWARFFormValue.cpp:96
llvm::DWARFFormValue::FormClass
FormClass
Definition: DWARFFormValue.h:30
llvm::DILineInfoSpecifier::FileLineInfoKind
FileLineInfoKind
Definition: DIContext.h:140
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:653
llvm::DWARFFormValue::FC_Block
@ FC_Block
Definition: DWARFFormValue.h:33
llvm::DWARFFormValue::FC_Flag
@ FC_Flag
Definition: DWARFFormValue.h:36
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:177
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:50
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:80
llvm::ValueType
PointerUnion< const Value *, const PseudoSourceValue * > ValueType
Definition: ScheduleDAGInstrs.h:107
llvm::PointerUnion< const Value *, const PseudoSourceValue * >
DIContext.h
llvm::None
constexpr std::nullopt_t None
Definition: None.h:27
llvm::DWARFUnit
Definition: DWARFUnit.h:207
llvm::DWARFFormValue::extractValue
bool extractValue(const DWARFDataExtractor &Data, uint64_t *OffsetPtr, dwarf::FormParams FormParams, const DWARFUnit *U)
Definition: DWARFFormValue.h:102
llvm::DWARFFormValue::getAsBlock
Optional< ArrayRef< uint8_t > > getAsBlock() const
Definition: DWARFFormValue.cpp:754
DataExtractor.h
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
llvm::DWARFFormValue::FC_Address
@ FC_Address
Definition: DWARFFormValue.h:32
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:225
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:248
llvm::DWARFFormValue::FC_Unknown
@ FC_Unknown
Definition: DWARFFormValue.h:31
llvm::DataExtractor
Definition: DataExtractor.h:41
llvm::DWARFFormValue::isFormClass
bool isFormClass(FormClass FC) const
Definition: DWARFFormValue.cpp:216
llvm::object::SectionedAddress
Definition: ObjectFile.h:144
llvm::DWARFFormValue::getForm
dwarf::Form getForm() const
Definition: DWARFFormValue.h:79
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:695
llvm::DWARFFormValue::getAsReferenceUVal
Optional< uint64_t > getAsReferenceUVal() const
Definition: DWARFFormValue.cpp:767
llvm::DWARFFormValue::createFromUnit
static DWARFFormValue createFromUnit(dwarf::Form F, const DWARFUnit *Unit, uint64_t *OffsetPtr)
Definition: DWARFFormValue.cpp:104
llvm::DWARFFormValue::getAsFile
Optional< std::string > getAsFile(DILineInfoSpecifier::FileLineInfoKind Kind) const
Correctly extract any file paths from a form value.
Definition: DWARFFormValue.cpp:774
llvm::DWARFFormValue::UnitOffset
Definition: DWARFFormValue.h:110
llvm::DWARFFormValue::dumpSectionedAddress
void dumpSectionedAddress(raw_ostream &OS, DIDumpOptions DumpOpts, object::SectionedAddress SA) const
Definition: DWARFFormValue.cpp:386
llvm::DWARFFormValue::UnitOffset::Offset
uint64_t Offset
Definition: DWARFFormValue.h:112
llvm::Optional::value_or
constexpr T value_or(U &&alt) const &
Definition: Optional.h:291
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:269
llvm::dwarf::DWARF32
@ DWARF32
Definition: Dwarf.h:93
llvm::DIDumpOptions
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:188