LLVM  9.0.0svn
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_DWARFFORMVALUE_H
10 #define LLVM_DEBUGINFO_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  ValueType Value; /// Contains all data for the form.
59  const DWARFUnit *U = nullptr; /// Remember the DWARFUnit at extract time.
60  const DWARFContext *C = nullptr; /// Context for extract time.
61 
62  DWARFFormValue(dwarf::Form F, ValueType V) : Form(F), Value(V) {}
63 
64 public:
65  DWARFFormValue(dwarf::Form F = dwarf::Form(0)) : Form(F) {}
66 
67  static DWARFFormValue createFromSValue(dwarf::Form F, int64_t V);
68  static DWARFFormValue createFromUValue(dwarf::Form F, uint64_t V);
69  static DWARFFormValue createFromPValue(dwarf::Form F, const char *V);
72  static DWARFFormValue createFromUnit(dwarf::Form F, const DWARFUnit *Unit,
73  uint32_t *OffsetPtr);
74 
75  dwarf::Form getForm() const { return Form; }
76  uint64_t getRawUValue() const { return Value.uval; }
77 
78  bool isFormClass(FormClass FC) const;
79  const DWARFUnit *getUnit() const { return U; }
80  void dump(raw_ostream &OS, DIDumpOptions DumpOpts = DIDumpOptions()) const;
82  object::SectionedAddress SA) const;
83  static void dumpAddressSection(const DWARFObject &Obj, raw_ostream &OS,
84  DIDumpOptions DumpOpts, uint64_t SectionIndex);
85 
86  /// Extracts a value in \p Data at offset \p *OffsetPtr. The information
87  /// in \p FormParams is needed to interpret some forms. The optional
88  /// \p Context and \p Unit allows extracting information if the form refers
89  /// to other sections (e.g., .debug_str).
90  bool extractValue(const DWARFDataExtractor &Data, uint32_t *OffsetPtr,
91  dwarf::FormParams FormParams,
92  const DWARFContext *Context = nullptr,
93  const DWARFUnit *Unit = nullptr);
94 
95  bool extractValue(const DWARFDataExtractor &Data, uint32_t *OffsetPtr,
96  dwarf::FormParams FormParams, const DWARFUnit *U) {
97  return extractValue(Data, OffsetPtr, FormParams, nullptr, U);
98  }
99 
100  bool isInlinedCStr() const {
101  return Value.data != nullptr && Value.data == (const uint8_t *)Value.cstr;
102  }
103 
104  /// getAsFoo functions below return the extracted value as Foo if only
105  /// DWARFFormValue has form class is suitable for representing Foo.
116 
117  /// Skip a form's value in \p DebugInfoData at the offset specified by
118  /// \p OffsetPtr.
119  ///
120  /// Skips the bytes for the current form and updates the offset.
121  ///
122  /// \param DebugInfoData The data where we want to skip the value.
123  /// \param OffsetPtr A reference to the offset that will be updated.
124  /// \param Params DWARF parameters to help interpret forms.
125  /// \returns true on success, false if the form was not skipped.
126  bool skipValue(DataExtractor DebugInfoData, uint32_t *OffsetPtr,
127  const dwarf::FormParams Params) const {
128  return DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, Params);
129  }
130 
131  /// Skip a form's value in \p DebugInfoData at the offset specified by
132  /// \p OffsetPtr.
133  ///
134  /// Skips the bytes for the specified form and updates the offset.
135  ///
136  /// \param Form The DW_FORM enumeration that indicates the form to skip.
137  /// \param DebugInfoData The data where we want to skip the value.
138  /// \param OffsetPtr A reference to the offset that will be updated.
139  /// \param FormParams DWARF parameters to help interpret forms.
140  /// \returns true on success, false if the form was not skipped.
141  static bool skipValue(dwarf::Form Form, DataExtractor DebugInfoData,
142  uint32_t *OffsetPtr,
143  const dwarf::FormParams FormParams);
144 
145 private:
146  void dumpString(raw_ostream &OS) const;
147 };
148 
149 namespace dwarf {
150 
151 /// Take an optional DWARFFormValue and try to extract a string value from it.
152 ///
153 /// \param V and optional DWARFFormValue to attempt to extract the value from.
154 /// \returns an optional value that contains a value if the form value
155 /// was valid and was a string.
157  if (V)
158  return V->getAsCString();
159  return None;
160 }
161 
162 /// Take an optional DWARFFormValue and extract a string value from it.
163 ///
164 /// \param V and optional DWARFFormValue to attempt to extract the value from.
165 /// \param Default the default value to return in case of failure.
166 /// \returns the string value or Default if the V doesn't have a value or the
167 /// form value's encoding wasn't a string.
168 inline const char *toString(const Optional<DWARFFormValue> &V,
169  const char *Default) {
170  return toString(V).getValueOr(Default);
171 }
172 
173 /// Take an optional DWARFFormValue and try to extract an unsigned constant.
174 ///
175 /// \param V and optional DWARFFormValue to attempt to extract the value from.
176 /// \returns an optional value that contains a value if the form value
177 /// was valid and has a unsigned constant form.
179  if (V)
180  return V->getAsUnsignedConstant();
181  return None;
182 }
183 
184 /// Take an optional DWARFFormValue and extract a unsigned constant.
185 ///
186 /// \param V and optional DWARFFormValue to attempt to extract the value from.
187 /// \param Default the default value to return in case of failure.
188 /// \returns the extracted unsigned value or Default if the V doesn't have a
189 /// value or the form value's encoding wasn't an unsigned constant form.
190 inline uint64_t toUnsigned(const Optional<DWARFFormValue> &V,
191  uint64_t Default) {
192  return toUnsigned(V).getValueOr(Default);
193 }
194 
195 /// Take an optional DWARFFormValue and try to extract an reference.
196 ///
197 /// \param V and optional DWARFFormValue to attempt to extract the value from.
198 /// \returns an optional value that contains a value if the form value
199 /// was valid and has a reference form.
201  if (V)
202  return V->getAsReference();
203  return None;
204 }
205 
206 /// Take an optional DWARFFormValue and extract a reference.
207 ///
208 /// \param V and optional DWARFFormValue to attempt to extract the value from.
209 /// \param Default the default value to return in case of failure.
210 /// \returns the extracted reference value or Default if the V doesn't have a
211 /// value or the form value's encoding wasn't a reference form.
212 inline uint64_t toReference(const Optional<DWARFFormValue> &V,
213  uint64_t Default) {
214  return toReference(V).getValueOr(Default);
215 }
216 
217 /// Take an optional DWARFFormValue and try to extract an signed constant.
218 ///
219 /// \param V and optional DWARFFormValue to attempt to extract the value from.
220 /// \returns an optional value that contains a value if the form value
221 /// was valid and has a signed constant form.
223  if (V)
224  return V->getAsSignedConstant();
225  return None;
226 }
227 
228 /// Take an optional DWARFFormValue and extract a signed integer.
229 ///
230 /// \param V and optional DWARFFormValue to attempt to extract the value from.
231 /// \param Default the default value to return in case of failure.
232 /// \returns the extracted signed integer value or Default if the V doesn't
233 /// have a value or the form value's encoding wasn't a signed integer form.
234 inline int64_t toSigned(const Optional<DWARFFormValue> &V, int64_t Default) {
235  return toSigned(V).getValueOr(Default);
236 }
237 
238 /// Take an optional DWARFFormValue and try to extract an address.
239 ///
240 /// \param V and optional DWARFFormValue to attempt to extract the value from.
241 /// \returns an optional value that contains a value if the form value
242 /// was valid and has a address form.
244  if (V)
245  return V->getAsAddress();
246  return None;
247 }
248 
251  if (V)
252  return V->getAsSectionedAddress();
253  return None;
254 }
255 
256 /// Take an optional DWARFFormValue and extract a address.
257 ///
258 /// \param V and optional DWARFFormValue to attempt to extract the value from.
259 /// \param Default the default value to return in case of failure.
260 /// \returns the extracted address value or Default if the V doesn't have a
261 /// value or the form value's encoding wasn't an address form.
262 inline uint64_t toAddress(const Optional<DWARFFormValue> &V, uint64_t Default) {
263  return toAddress(V).getValueOr(Default);
264 }
265 
266 /// Take an optional DWARFFormValue and try to extract an section offset.
267 ///
268 /// \param V and optional DWARFFormValue to attempt to extract the value from.
269 /// \returns an optional value that contains a value if the form value
270 /// was valid and has a section offset form.
272  if (V)
273  return V->getAsSectionOffset();
274  return None;
275 }
276 
277 /// Take an optional DWARFFormValue and extract a section offset.
278 ///
279 /// \param V and optional DWARFFormValue to attempt to extract the value from.
280 /// \param Default the default value to return in case of failure.
281 /// \returns the extracted section offset value or Default if the V doesn't
282 /// have a value or the form value's encoding wasn't a section offset form.
283 inline uint64_t toSectionOffset(const Optional<DWARFFormValue> &V,
284  uint64_t Default) {
285  return toSectionOffset(V).getValueOr(Default);
286 }
287 
288 /// Take an optional DWARFFormValue and try to extract block data.
289 ///
290 /// \param V and optional DWARFFormValue to attempt to extract the value from.
291 /// \returns an optional value that contains a value if the form value
292 /// was valid and has a block form.
294  if (V)
295  return V->getAsBlock();
296  return None;
297 }
298 
299 } // end namespace dwarf
300 
301 } // end namespace llvm
302 
303 #endif // LLVM_DEBUGINFO_DWARFFORMVALUE_H
void dump(raw_ostream &OS, DIDumpOptions DumpOpts=DIDumpOptions()) const
A helper struct providing information about the byte size of DW_FORM values that vary in size dependi...
Definition: Dwarf.h:499
PointerUnion< const Value *, const PseudoSourceValue * > ValueType
LLVMContext & Context
Optional< ArrayRef< uint8_t > > toBlock(const Optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract block data.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
uint64_t toUnsigned(const Optional< DWARFFormValue > &V, uint64_t Default)
Take an optional DWARFFormValue and extract a unsigned constant.
static DWARFFormValue createFromBlockValue(dwarf::Form F, ArrayRef< uint8_t > D)
static DWARFFormValue createFromUnit(dwarf::Form F, const DWARFUnit *Unit, uint32_t *OffsetPtr)
F(f)
bool isFormClass(FormClass FC) const
dwarf::Form getForm() const
Optional< uint64_t > getAsReference() const
getAsFoo functions below return the extracted value as Foo if only DWARFFormValue has form class is s...
std::string toString(Error E)
Write all error messages (if any) in E to a string.
Definition: Error.h:966
uint64_t getRawUValue() const
Optional< ArrayRef< uint8_t > > getAsBlock() const
Optional< object::SectionedAddress > toSectionedAddress(const Optional< DWARFFormValue > &V)
const DWARFUnit * getUnit() const
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:158
void dumpSectionedAddress(raw_ostream &OS, DIDumpOptions DumpOpts, object::SectionedAddress SA) const
uint64_t toSectionOffset(const Optional< DWARFFormValue > &V, uint64_t Default)
Take an optional DWARFFormValue and extract a section offset.
int64_t toSigned(const Optional< DWARFFormValue > &V, int64_t Default)
Take an optional DWARFFormValue and extract a signed integer.
bool isInlinedCStr() const
Optional< object::SectionedAddress > getAsSectionedAddress() const
static void dumpAddressSection(const DWARFObject &Obj, raw_ostream &OS, DIDumpOptions DumpOpts, uint64_t SectionIndex)
Optional< uint64_t > getAsAddress() const
Optional< uint64_t > getAsUnsignedConstant() const
bool skipValue(DataExtractor DebugInfoData, uint32_t *OffsetPtr, const dwarf::FormParams Params) const
Skip a form&#39;s value in DebugInfoData at the offset specified by OffsetPtr.
static DWARFFormValue createFromUValue(dwarf::Form F, uint64_t V)
Optional< uint64_t > getAsReferenceUVal() const
A DataExtractor (typically for an in-memory copy of an object-file section) plus a relocation map for...
uint64_t toReference(const Optional< DWARFFormValue > &V, uint64_t Default)
Take an optional DWARFFormValue and extract a reference.
DWARFContext This data structure is the top level entity that deals with dwarf debug information pars...
Definition: DWARFContext.h:58
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
Optional< uint64_t > getAsCStringOffset() const
This file contains constants used for implementing Dwarf debug support.
DWARFFormValue(dwarf::Form F=dwarf::Form(0))
static DWARFFormValue createFromPValue(dwarf::Form F, const char *V)
bool extractValue(const DWARFDataExtractor &Data, uint32_t *OffsetPtr, dwarf::FormParams FormParams, const DWARFContext *Context=nullptr, const DWARFUnit *Unit=nullptr)
Extracts a value in Data at offset *OffsetPtr.
uint64_t toAddress(const Optional< DWARFFormValue > &V, uint64_t Default)
Take an optional DWARFFormValue and extract a address.
Optional< const char * > getAsCString() const
Optional< int64_t > getAsSignedConstant() const
bool extractValue(const DWARFDataExtractor &Data, uint32_t *OffsetPtr, dwarf::FormParams FormParams, const DWARFUnit *U)
LLVM Value Representation.
Definition: Value.h:72
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
static DWARFFormValue createFromSValue(dwarf::Form F, int64_t V)
Optional< uint64_t > getAsSectionOffset() const