LLVM  8.0.1
DWARFFormValue.h
Go to the documentation of this file.
1 //===- DWARFFormValue.h -----------------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_DEBUGINFO_DWARFFORMVALUE_H
11 #define LLVM_DEBUGINFO_DWARFFORMVALUE_H
12 
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/None.h"
15 #include "llvm/ADT/Optional.h"
19 #include <cstdint>
20 
21 namespace llvm {
22 
23 class DWARFContext;
24 class DWARFUnit;
25 class raw_ostream;
26 
28 public:
29  enum FormClass {
40  };
41 
42 private:
43  struct ValueType {
44  ValueType() { uval = 0; }
45 
46  union {
47  uint64_t uval;
48  int64_t sval;
49  const char *cstr;
50  };
51  const uint8_t *data = nullptr;
52  uint64_t SectionIndex; /// Section index for reference forms.
53  };
54 
55  dwarf::Form Form; /// Form for this value.
56  ValueType Value; /// Contains all data for the form.
57  const DWARFUnit *U = nullptr; /// Remember the DWARFUnit at extract time.
58  const DWARFContext *C = nullptr; /// Context for extract time.
59 public:
61 
62  dwarf::Form getForm() const { return Form; }
63  uint64_t getRawUValue() const { return Value.uval; }
64  void setForm(dwarf::Form F) { Form = F; }
65  void setUValue(uint64_t V) { Value.uval = V; }
66  void setSValue(int64_t V) { Value.sval = V; }
67  void setPValue(const char *V) { Value.cstr = V; }
68 
70  Value.data = Data.data();
71  setUValue(Data.size());
72  }
73 
74  bool isFormClass(FormClass FC) const;
75  const DWARFUnit *getUnit() const { return U; }
76  void dump(raw_ostream &OS, DIDumpOptions DumpOpts = DIDumpOptions()) const;
78  SectionedAddress SA) const;
79  static void dumpAddressSection(const DWARFObject &Obj, raw_ostream &OS,
80  DIDumpOptions DumpOpts, uint64_t SectionIndex);
81 
82  /// Extracts a value in \p Data at offset \p *OffsetPtr. The information
83  /// in \p FormParams is needed to interpret some forms. The optional
84  /// \p Context and \p Unit allows extracting information if the form refers
85  /// to other sections (e.g., .debug_str).
86  bool extractValue(const DWARFDataExtractor &Data, uint32_t *OffsetPtr,
87  dwarf::FormParams FormParams,
88  const DWARFContext *Context = nullptr,
89  const DWARFUnit *Unit = nullptr);
90 
91  bool extractValue(const DWARFDataExtractor &Data, uint32_t *OffsetPtr,
92  dwarf::FormParams FormParams, const DWARFUnit *U) {
93  return extractValue(Data, OffsetPtr, FormParams, nullptr, U);
94  }
95 
96  bool isInlinedCStr() const {
97  return Value.data != nullptr && Value.data == (const uint8_t *)Value.cstr;
98  }
99 
100  /// getAsFoo functions below return the extracted value as Foo if only
101  /// DWARFFormValue has form class is suitable for representing Foo.
112 
113  /// Skip a form's value in \p DebugInfoData at the offset specified by
114  /// \p OffsetPtr.
115  ///
116  /// Skips the bytes for the current form and updates the offset.
117  ///
118  /// \param DebugInfoData The data where we want to skip the value.
119  /// \param OffsetPtr A reference to the offset that will be updated.
120  /// \param Params DWARF parameters to help interpret forms.
121  /// \returns true on success, false if the form was not skipped.
122  bool skipValue(DataExtractor DebugInfoData, uint32_t *OffsetPtr,
123  const dwarf::FormParams Params) const {
124  return DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, Params);
125  }
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 specified form and updates the offset.
131  ///
132  /// \param Form The DW_FORM enumeration that indicates the form to skip.
133  /// \param DebugInfoData The data where we want to skip the value.
134  /// \param OffsetPtr A reference to the offset that will be updated.
135  /// \param FormParams DWARF parameters to help interpret forms.
136  /// \returns true on success, false if the form was not skipped.
137  static bool skipValue(dwarf::Form Form, DataExtractor DebugInfoData,
138  uint32_t *OffsetPtr,
139  const dwarf::FormParams FormParams);
140 
141 private:
142  void dumpString(raw_ostream &OS) const;
143 };
144 
145 namespace dwarf {
146 
147 /// Take an optional DWARFFormValue and try to extract a string value from it.
148 ///
149 /// \param V and optional DWARFFormValue to attempt to extract the value from.
150 /// \returns an optional value that contains a value if the form value
151 /// was valid and was a string.
153  if (V)
154  return V->getAsCString();
155  return None;
156 }
157 
158 /// Take an optional DWARFFormValue and extract a string value from it.
159 ///
160 /// \param V and optional DWARFFormValue to attempt to extract the value from.
161 /// \param Default the default value to return in case of failure.
162 /// \returns the string value or Default if the V doesn't have a value or the
163 /// form value's encoding wasn't a string.
164 inline const char *toString(const Optional<DWARFFormValue> &V,
165  const char *Default) {
166  return toString(V).getValueOr(Default);
167 }
168 
169 /// Take an optional DWARFFormValue and try to extract an unsigned constant.
170 ///
171 /// \param V and optional DWARFFormValue to attempt to extract the value from.
172 /// \returns an optional value that contains a value if the form value
173 /// was valid and has a unsigned constant form.
175  if (V)
176  return V->getAsUnsignedConstant();
177  return None;
178 }
179 
180 /// Take an optional DWARFFormValue and extract a unsigned constant.
181 ///
182 /// \param V and optional DWARFFormValue to attempt to extract the value from.
183 /// \param Default the default value to return in case of failure.
184 /// \returns the extracted unsigned value or Default if the V doesn't have a
185 /// value or the form value's encoding wasn't an unsigned constant form.
186 inline uint64_t toUnsigned(const Optional<DWARFFormValue> &V,
187  uint64_t Default) {
188  return toUnsigned(V).getValueOr(Default);
189 }
190 
191 /// Take an optional DWARFFormValue and try to extract an reference.
192 ///
193 /// \param V and optional DWARFFormValue to attempt to extract the value from.
194 /// \returns an optional value that contains a value if the form value
195 /// was valid and has a reference form.
197  if (V)
198  return V->getAsReference();
199  return None;
200 }
201 
202 /// Take an optional DWARFFormValue and extract a reference.
203 ///
204 /// \param V and optional DWARFFormValue to attempt to extract the value from.
205 /// \param Default the default value to return in case of failure.
206 /// \returns the extracted reference value or Default if the V doesn't have a
207 /// value or the form value's encoding wasn't a reference form.
208 inline uint64_t toReference(const Optional<DWARFFormValue> &V,
209  uint64_t Default) {
210  return toReference(V).getValueOr(Default);
211 }
212 
213 /// Take an optional DWARFFormValue and try to extract an signed constant.
214 ///
215 /// \param V and optional DWARFFormValue to attempt to extract the value from.
216 /// \returns an optional value that contains a value if the form value
217 /// was valid and has a signed constant form.
219  if (V)
220  return V->getAsSignedConstant();
221  return None;
222 }
223 
224 /// Take an optional DWARFFormValue and extract a signed integer.
225 ///
226 /// \param V and optional DWARFFormValue to attempt to extract the value from.
227 /// \param Default the default value to return in case of failure.
228 /// \returns the extracted signed integer value or Default if the V doesn't
229 /// have a value or the form value's encoding wasn't a signed integer form.
230 inline int64_t toSigned(const Optional<DWARFFormValue> &V, int64_t Default) {
231  return toSigned(V).getValueOr(Default);
232 }
233 
234 /// Take an optional DWARFFormValue and try to extract an address.
235 ///
236 /// \param V and optional DWARFFormValue to attempt to extract the value from.
237 /// \returns an optional value that contains a value if the form value
238 /// was valid and has a address form.
240  if (V)
241  return V->getAsAddress();
242  return None;
243 }
244 
247  if (V)
248  return V->getAsSectionedAddress();
249  return None;
250 }
251 
252 /// Take an optional DWARFFormValue and extract a address.
253 ///
254 /// \param V and optional DWARFFormValue to attempt to extract the value from.
255 /// \param Default the default value to return in case of failure.
256 /// \returns the extracted address value or Default if the V doesn't have a
257 /// value or the form value's encoding wasn't an address form.
258 inline uint64_t toAddress(const Optional<DWARFFormValue> &V, uint64_t Default) {
259  return toAddress(V).getValueOr(Default);
260 }
261 
262 /// Take an optional DWARFFormValue and try to extract an section offset.
263 ///
264 /// \param V and optional DWARFFormValue to attempt to extract the value from.
265 /// \returns an optional value that contains a value if the form value
266 /// was valid and has a section offset form.
268  if (V)
269  return V->getAsSectionOffset();
270  return None;
271 }
272 
273 /// Take an optional DWARFFormValue and extract a section offset.
274 ///
275 /// \param V and optional DWARFFormValue to attempt to extract the value from.
276 /// \param Default the default value to return in case of failure.
277 /// \returns the extracted section offset value or Default if the V doesn't
278 /// have a value or the form value's encoding wasn't a section offset form.
279 inline uint64_t toSectionOffset(const Optional<DWARFFormValue> &V,
280  uint64_t Default) {
281  return toSectionOffset(V).getValueOr(Default);
282 }
283 
284 /// Take an optional DWARFFormValue and try to extract block data.
285 ///
286 /// \param V and optional DWARFFormValue to attempt to extract the value from.
287 /// \returns an optional value that contains a value if the form value
288 /// was valid and has a block form.
290  if (V)
291  return V->getAsBlock();
292  return None;
293 }
294 
295 } // end namespace dwarf
296 
297 } // end namespace llvm
298 
299 #endif // LLVM_DEBUGINFO_DWARFFORMVALUE_H
void dump(raw_ostream &OS, DIDumpOptions DumpOpts=DIDumpOptions()) const
void setBlockValue(const ArrayRef< uint8_t > &Data)
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:24
uint64_t toUnsigned(const Optional< DWARFFormValue > &V, uint64_t Default)
Take an optional DWARFFormValue and extract a unsigned constant.
void setUValue(uint64_t V)
F(f)
bool isFormClass(FormClass FC) const
Optional< SectionedAddress > getAsSectionedAddress() 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:967
uint64_t getRawUValue() const
Optional< ArrayRef< uint8_t > > getAsBlock() const
void setForm(dwarf::Form F)
void dumpSectionedAddress(raw_ostream &OS, DIDumpOptions DumpOpts, SectionedAddress SA) const
const DWARFUnit * getUnit() const
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:159
void setPValue(const char *V)
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
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
struct UnitT Unit
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.
Optional< uint64_t > getAsReferenceUVal() const
const T * data() const
Definition: ArrayRef.h:146
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:59
Optional< uint64_t > getAsCStringOffset() const
This file contains constants used for implementing Dwarf debug support.
DWARFFormValue(dwarf::Form F=dwarf::Form(0))
Context for extract time.
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)
void setSValue(int64_t V)
LLVM Value Representation.
Definition: Value.h:73
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
Optional< uint64_t > getAsSectionOffset() const
Optional< SectionedAddress > toSectionedAddress(const Optional< DWARFFormValue > &V)