LLVM  5.0.0
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"
18 #include <cstdint>
19 
20 namespace llvm {
21 
22 class DWARFUnit;
23 class raw_ostream;
24 
25 /// A helper struct for DWARFFormValue methods, providing information that
26 /// allows it to know the byte size of DW_FORM values that vary in size
27 /// depending on the DWARF version, address byte size, or DWARF32/DWARF64.
29  uint16_t Version;
30  uint8_t AddrSize;
32 
33  /// The definition of the size of form DW_FORM_ref_addr depends on the
34  /// version. In DWARF v2 it's the size of an address; after that, it's the
35  /// size of a reference.
36  uint8_t getRefAddrByteSize() const {
37  if (Version == 2)
38  return AddrSize;
39  return getDwarfOffsetByteSize();
40  }
41 
42  /// The size of a reference is determined by the DWARF 32/64-bit format.
43  uint8_t getDwarfOffsetByteSize() const {
44  switch (Format) {
46  return 4;
48  return 8;
49  }
50  llvm_unreachable("Invalid Format value");
51  }
52 };
53 
55 public:
56  enum FormClass {
67  };
68 
69 private:
70  struct ValueType {
71  ValueType() { uval = 0; }
72 
73  union {
74  uint64_t uval;
75  int64_t sval;
76  const char *cstr;
77  };
78  const uint8_t *data = nullptr;
79  uint64_t SectionIndex; /// Section index for reference forms.
80  };
81 
82  dwarf::Form Form; /// Form for this value.
83  ValueType Value; /// Contains all data for the form.
84  const DWARFUnit *U = nullptr; /// Remember the DWARFUnit at extract time.
85 
86 public:
88 
89  dwarf::Form getForm() const { return Form; }
90  uint64_t getRawUValue() const { return Value.uval; }
91  uint64_t getSectionIndex() const { return Value.SectionIndex; }
92  void setForm(dwarf::Form F) { Form = F; }
93  void setUValue(uint64_t V) { Value.uval = V; }
94  void setSValue(int64_t V) { Value.sval = V; }
95  void setPValue(const char *V) { Value.cstr = V; }
96 
98  Value.data = Data.data();
99  setUValue(Data.size());
100  }
101 
102  bool isFormClass(FormClass FC) const;
103  const DWARFUnit *getUnit() const { return U; }
104  void dump(raw_ostream &OS) const;
105 
106  /// Extracts a value in \p Data at offset \p *OffsetPtr.
107  ///
108  /// The passed DWARFUnit is allowed to be nullptr, in which case some
109  /// kind of forms that depend on Unit information are disallowed.
110  /// \param Data The DWARFDataExtractor to use.
111  /// \param OffsetPtr The offset within \p Data where the data starts.
112  /// \param U The optional DWARFUnit supplying information for some forms.
113  /// \returns whether the extraction succeeded.
114  bool extractValue(const DWARFDataExtractor &Data, uint32_t *OffsetPtr,
115  const DWARFUnit *U);
116 
117  bool isInlinedCStr() const {
118  return Value.data != nullptr && Value.data == (const uint8_t *)Value.cstr;
119  }
120 
121  /// getAsFoo functions below return the extracted value as Foo if only
122  /// DWARFFormValue has form class is suitable for representing Foo.
132 
133  /// Get the fixed byte size for a given form.
134  ///
135  /// If the form has a fixed byte size, then an Optional with a value will be
136  /// returned. If the form is always encoded using a variable length storage
137  /// format (ULEB or SLEB numbers or blocks) then None will be returned.
138  ///
139  /// \param Form DWARF form to get the fixed byte size for.
140  /// \param FormParams DWARF parameters to help interpret forms.
141  /// \returns Optional<uint8_t> value with the fixed byte size or None if
142  /// \p Form doesn't have a fixed byte size.
144  const DWARFFormParams FormParams);
145 
146  /// Skip a form's value in \p DebugInfoData at the offset specified by
147  /// \p OffsetPtr.
148  ///
149  /// Skips the bytes for the current form and updates the offset.
150  ///
151  /// \param DebugInfoData The data where we want to skip the value.
152  /// \param OffsetPtr A reference to the offset that will be updated.
153  /// \param Params DWARF parameters to help interpret forms.
154  /// \returns true on success, false if the form was not skipped.
155  bool skipValue(DataExtractor DebugInfoData, uint32_t *OffsetPtr,
156  const DWARFFormParams Params) const {
157  return DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, Params);
158  }
159 
160  /// Skip a form's value in \p DebugInfoData at the offset specified by
161  /// \p OffsetPtr.
162  ///
163  /// Skips the bytes for the specified form and updates the offset.
164  ///
165  /// \param Form The DW_FORM enumeration that indicates the form to skip.
166  /// \param DebugInfoData The data where we want to skip the value.
167  /// \param OffsetPtr A reference to the offset that will be updated.
168  /// \param FormParams DWARF parameters to help interpret forms.
169  /// \returns true on success, false if the form was not skipped.
170  static bool skipValue(dwarf::Form Form, DataExtractor DebugInfoData,
171  uint32_t *OffsetPtr, const DWARFFormParams FormParams);
172 
173 private:
174  void dumpString(raw_ostream &OS) const;
175 };
176 
177 namespace dwarf {
178 
179 /// Take an optional DWARFFormValue and try to extract a string value from it.
180 ///
181 /// \param V and optional DWARFFormValue to attempt to extract the value from.
182 /// \returns an optional value that contains a value if the form value
183 /// was valid and was a string.
185  if (V)
186  return V->getAsCString();
187  return None;
188 }
189 
190 /// Take an optional DWARFFormValue and extract a string value from it.
191 ///
192 /// \param V and optional DWARFFormValue to attempt to extract the value from.
193 /// \param Default the default value to return in case of failure.
194 /// \returns the string value or Default if the V doesn't have a value or the
195 /// form value's encoding wasn't a string.
196 inline const char *toString(const Optional<DWARFFormValue> &V,
197  const char *Default) {
198  return toString(V).getValueOr(Default);
199 }
200 
201 /// Take an optional DWARFFormValue and try to extract an unsigned constant.
202 ///
203 /// \param V and optional DWARFFormValue to attempt to extract the value from.
204 /// \returns an optional value that contains a value if the form value
205 /// was valid and has a unsigned constant form.
207  if (V)
208  return V->getAsUnsignedConstant();
209  return None;
210 }
211 
212 /// Take an optional DWARFFormValue and extract a unsigned constant.
213 ///
214 /// \param V and optional DWARFFormValue to attempt to extract the value from.
215 /// \param Default the default value to return in case of failure.
216 /// \returns the extracted unsigned value or Default if the V doesn't have a
217 /// value or the form value's encoding wasn't an unsigned constant form.
218 inline uint64_t toUnsigned(const Optional<DWARFFormValue> &V,
219  uint64_t Default) {
220  return toUnsigned(V).getValueOr(Default);
221 }
222 
223 /// Take an optional DWARFFormValue and try to extract an reference.
224 ///
225 /// \param V and optional DWARFFormValue to attempt to extract the value from.
226 /// \returns an optional value that contains a value if the form value
227 /// was valid and has a reference form.
229  if (V)
230  return V->getAsReference();
231  return None;
232 }
233 
234 /// Take an optional DWARFFormValue and extract a reference.
235 ///
236 /// \param V and optional DWARFFormValue to attempt to extract the value from.
237 /// \param Default the default value to return in case of failure.
238 /// \returns the extracted reference value or Default if the V doesn't have a
239 /// value or the form value's encoding wasn't a reference form.
240 inline uint64_t toReference(const Optional<DWARFFormValue> &V,
241  uint64_t Default) {
242  return toReference(V).getValueOr(Default);
243 }
244 
245 /// Take an optional DWARFFormValue and try to extract an signed constant.
246 ///
247 /// \param V and optional DWARFFormValue to attempt to extract the value from.
248 /// \returns an optional value that contains a value if the form value
249 /// was valid and has a signed constant form.
251  if (V)
252  return V->getAsSignedConstant();
253  return None;
254 }
255 
256 /// Take an optional DWARFFormValue and extract a signed integer.
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 signed integer value or Default if the V doesn't
261 /// have a value or the form value's encoding wasn't a signed integer form.
262 inline int64_t toSigned(const Optional<DWARFFormValue> &V, int64_t Default) {
263  return toSigned(V).getValueOr(Default);
264 }
265 
266 /// Take an optional DWARFFormValue and try to extract an address.
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 address form.
272  if (V)
273  return V->getAsAddress();
274  return None;
275 }
276 
277 /// Take an optional DWARFFormValue and extract a address.
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 address value or Default if the V doesn't have a
282 /// value or the form value's encoding wasn't an address form.
283 inline uint64_t toAddress(const Optional<DWARFFormValue> &V, uint64_t Default) {
284  return toAddress(V).getValueOr(Default);
285 }
286 
287 /// Take an optional DWARFFormValue and try to extract an section offset.
288 ///
289 /// \param V and optional DWARFFormValue to attempt to extract the value from.
290 /// \returns an optional value that contains a value if the form value
291 /// was valid and has a section offset form.
293  if (V)
294  return V->getAsSectionOffset();
295  return None;
296 }
297 
298 /// Take an optional DWARFFormValue and extract a section offset.
299 ///
300 /// \param V and optional DWARFFormValue to attempt to extract the value from.
301 /// \param Default the default value to return in case of failure.
302 /// \returns the extracted section offset value or Default if the V doesn't
303 /// have a value or the form value's encoding wasn't a section offset form.
304 inline uint64_t toSectionOffset(const Optional<DWARFFormValue> &V,
305  uint64_t Default) {
306  return toSectionOffset(V).getValueOr(Default);
307 }
308 
309 /// Take an optional DWARFFormValue and try to extract block data.
310 ///
311 /// \param V and optional DWARFFormValue to attempt to extract the value from.
312 /// \returns an optional value that contains a value if the form value
313 /// was valid and has a block form.
315  if (V)
316  return V->getAsBlock();
317  return None;
318 }
319 
320 } // end namespace dwarf
321 
322 } // end namespace llvm
323 
324 #endif // LLVM_DEBUGINFO_DWARFFORMVALUE_H
const NoneType None
Definition: None.h:24
void setBlockValue(const ArrayRef< uint8_t > &Data)
PointerUnion< const Value *, const PseudoSourceValue * > ValueType
Optional< ArrayRef< uint8_t > > toBlock(const Optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract block data.
constexpr T getValueOr(U &&value) const LLVM_LVALUE_FUNCTION
Definition: Optional.h:140
void setUValue(uint64_t V)
Optional< uint64_t > getAsCStringOffset() const
dwarf::DwarfFormat Format
Optional< uint64_t > toAddress(const Optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an address.
Optional< const char * > getAsCString() const
static Optional< uint8_t > getFixedByteSize(dwarf::Form Form, const DWARFFormParams FormParams)
Get the fixed byte size for a given form.
DwarfFormat
Constants that define the DWARF format as 32 or 64 bit.
Definition: Dwarf.h:490
Optional< uint64_t > getAsReference() const
getAsFoo functions below return the extracted value as Foo if only DWARFFormValue has form class is s...
Optional< ArrayRef< uint8_t > > getAsBlock() const
uint64_t getSectionIndex() const
void setForm(dwarf::Form F)
#define F(x, y, z)
Definition: MD5.cpp:55
bool extractValue(const DWARFDataExtractor &Data, uint32_t *OffsetPtr, const DWARFUnit *U)
Extracts a value in Data at offset *OffsetPtr.
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
bool skipValue(DataExtractor DebugInfoData, uint32_t *OffsetPtr, const DWARFFormParams Params) const
Skip a form's value in DebugInfoData at the offset specified by OffsetPtr.
Optional< uint64_t > toSectionOffset(const Optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an section offset.
void setPValue(const char *V)
Optional< uint64_t > toReference(const Optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an reference.
bool isInlinedCStr() const
Optional< uint64_t > getAsUnsignedConstant() const
Optional< uint64_t > getAsAddress() const
bool isFormClass(FormClass FC) const
uint64_t getRawUValue() const
A DataExtractor (typically for an in-memory copy of an object-file section) plus a relocation map for...
Optional< int64_t > getAsSignedConstant() const
A helper struct for DWARFFormValue methods, providing information that allows it to know the byte siz...
Optional< const char * > toString(const Optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
DWARFFormValue(dwarf::Form F=dwarf::Form(0))
Remember the DWARFUnit at extract time.
llvm_unreachable("Unknown RegBank!")
uint8_t getRefAddrByteSize() const
The definition of the size of form DW_FORM_ref_addr depends on the version.
Optional< int64_t > toSigned(const Optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an signed constant.
uint8_t getDwarfOffsetByteSize() const
The size of a reference is determined by the DWARF 32/64-bit format.
Optional< uint64_t > getAsSectionOffset() const
Optional< uint64_t > getAsReferenceUVal() const
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:44
dwarf::Form getForm() const
const DWARFUnit * getUnit() const
const T * data() const
Definition: ArrayRef.h:146
Optional< uint64_t > toUnsigned(const Optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an unsigned constant.
void dump(raw_ostream &OS) const