LLVM  7.0.0svn
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 DWARFUnit;
24 class raw_ostream;
25 
26 /// A helper struct for DWARFFormValue methods, providing information that
27 /// allows it to know the byte size of DW_FORM values that vary in size
28 /// depending on the DWARF version, address byte size, or DWARF32/DWARF64.
30  uint16_t Version;
31  uint8_t AddrSize;
33 
34  /// The definition of the size of form DW_FORM_ref_addr depends on the
35  /// version. In DWARF v2 it's the size of an address; after that, it's the
36  /// size of a reference.
37  uint8_t getRefAddrByteSize() const {
38  if (Version == 2)
39  return AddrSize;
40  return getDwarfOffsetByteSize();
41  }
42 
43  /// The size of a reference is determined by the DWARF 32/64-bit format.
44  uint8_t getDwarfOffsetByteSize() const {
45  switch (Format) {
47  return 4;
49  return 8;
50  }
51  llvm_unreachable("Invalid Format value");
52  }
53 
54  explicit operator bool() const { return Version && AddrSize; }
55 };
56 
58 public:
59  enum FormClass {
69  FC_Exprloc
70  };
71 
72 private:
73  struct ValueType {
74  ValueType() { uval = 0; }
75 
76  union {
77  uint64_t uval;
78  int64_t sval;
79  const char *cstr;
80  };
81  const uint8_t *data = nullptr;
82  uint64_t SectionIndex; /// Section index for reference forms.
83  };
84 
85  dwarf::Form Form; /// Form for this value.
86  ValueType Value; /// Contains all data for the form.
87  const DWARFUnit *U = nullptr; /// Remember the DWARFUnit at extract time.
88 
89 public:
91 
92  dwarf::Form getForm() const { return Form; }
93  uint64_t getRawUValue() const { return Value.uval; }
94  uint64_t getSectionIndex() const { return Value.SectionIndex; }
95  void setForm(dwarf::Form F) { Form = F; }
96  void setUValue(uint64_t V) { Value.uval = V; }
97  void setSValue(int64_t V) { Value.sval = V; }
98  void setPValue(const char *V) { Value.cstr = V; }
99 
101  Value.data = Data.data();
102  setUValue(Data.size());
103  }
104 
105  bool isFormClass(FormClass FC) const;
106  const DWARFUnit *getUnit() const { return U; }
107  void dump(raw_ostream &OS, DIDumpOptions DumpOpts = DIDumpOptions()) const;
108 
109  /// Extracts a value in \p Data at offset \p *OffsetPtr. The information
110  /// in \p FormParams is needed to interpret some forms. The optional
111  /// \p Unit allows extracting information if the form refers to other
112  /// sections (e.g., .debug_str).
113  bool extractValue(const DWARFDataExtractor &Data, uint32_t *OffsetPtr,
114  DWARFFormParams FormParams, const DWARFUnit *U = nullptr);
115 
116  bool isInlinedCStr() const {
117  return Value.data != nullptr && Value.data == (const uint8_t *)Value.cstr;
118  }
119 
120  /// getAsFoo functions below return the extracted value as Foo if only
121  /// DWARFFormValue has form class is suitable for representing Foo.
122  Optional<uint64_t> getAsReference() const;
123  Optional<uint64_t> getAsUnsignedConstant() const;
124  Optional<int64_t> getAsSignedConstant() const;
125  Optional<const char *> getAsCString() const;
126  Optional<uint64_t> getAsAddress() const;
127  Optional<uint64_t> getAsSectionOffset() const;
128  Optional<ArrayRef<uint8_t>> getAsBlock() const;
129  Optional<uint64_t> getAsCStringOffset() const;
130  Optional<uint64_t> getAsReferenceUVal() const;
131 
132  /// Get the fixed byte size for a given form.
133  ///
134  /// If the form has a fixed byte size, then an Optional with a value will be
135  /// returned. If the form is always encoded using a variable length storage
136  /// format (ULEB or SLEB numbers or blocks) then None will be returned.
137  ///
138  /// \param Form DWARF form to get the fixed byte size for.
139  /// \param FormParams DWARF parameters to help interpret forms.
140  /// \returns Optional<uint8_t> value with the fixed byte size or None if
141  /// \p Form doesn't have a fixed byte size.
142  static Optional<uint8_t> getFixedByteSize(dwarf::Form Form,
143  const DWARFFormParams FormParams);
144 
145  /// Skip a form's value in \p DebugInfoData at the offset specified by
146  /// \p OffsetPtr.
147  ///
148  /// Skips the bytes for the current form and updates the offset.
149  ///
150  /// \param DebugInfoData The data where we want to skip the value.
151  /// \param OffsetPtr A reference to the offset that will be updated.
152  /// \param Params DWARF parameters to help interpret forms.
153  /// \returns true on success, false if the form was not skipped.
154  bool skipValue(DataExtractor DebugInfoData, uint32_t *OffsetPtr,
155  const DWARFFormParams Params) const {
156  return DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, Params);
157  }
158 
159  /// Skip a form's value in \p DebugInfoData at the offset specified by
160  /// \p OffsetPtr.
161  ///
162  /// Skips the bytes for the specified form and updates the offset.
163  ///
164  /// \param Form The DW_FORM enumeration that indicates the form to skip.
165  /// \param DebugInfoData The data where we want to skip the value.
166  /// \param OffsetPtr A reference to the offset that will be updated.
167  /// \param FormParams DWARF parameters to help interpret forms.
168  /// \returns true on success, false if the form was not skipped.
169  static bool skipValue(dwarf::Form Form, DataExtractor DebugInfoData,
170  uint32_t *OffsetPtr, const DWARFFormParams FormParams);
171 
172 private:
173  void dumpString(raw_ostream &OS) const;
174 };
175 
176 namespace dwarf {
177 
178 /// Take an optional DWARFFormValue and try to extract a string value from it.
179 ///
180 /// \param V and optional DWARFFormValue to attempt to extract the value from.
181 /// \returns an optional value that contains a value if the form value
182 /// was valid and was a string.
184  if (V)
185  return V->getAsCString();
186  return None;
187 }
188 
189 /// Take an optional DWARFFormValue and extract a string value from it.
190 ///
191 /// \param V and optional DWARFFormValue to attempt to extract the value from.
192 /// \param Default the default value to return in case of failure.
193 /// \returns the string value or Default if the V doesn't have a value or the
194 /// form value's encoding wasn't a string.
195 inline const char *toString(const Optional<DWARFFormValue> &V,
196  const char *Default) {
197  return toString(V).getValueOr(Default);
198 }
199 
200 /// Take an optional DWARFFormValue and try to extract an unsigned constant.
201 ///
202 /// \param V and optional DWARFFormValue to attempt to extract the value from.
203 /// \returns an optional value that contains a value if the form value
204 /// was valid and has a unsigned constant form.
206  if (V)
207  return V->getAsUnsignedConstant();
208  return None;
209 }
210 
211 /// Take an optional DWARFFormValue and extract a unsigned constant.
212 ///
213 /// \param V and optional DWARFFormValue to attempt to extract the value from.
214 /// \param Default the default value to return in case of failure.
215 /// \returns the extracted unsigned value or Default if the V doesn't have a
216 /// value or the form value's encoding wasn't an unsigned constant form.
217 inline uint64_t toUnsigned(const Optional<DWARFFormValue> &V,
218  uint64_t Default) {
219  return toUnsigned(V).getValueOr(Default);
220 }
221 
222 /// Take an optional DWARFFormValue and try to extract an reference.
223 ///
224 /// \param V and optional DWARFFormValue to attempt to extract the value from.
225 /// \returns an optional value that contains a value if the form value
226 /// was valid and has a reference form.
228  if (V)
229  return V->getAsReference();
230  return None;
231 }
232 
233 /// Take an optional DWARFFormValue and extract a reference.
234 ///
235 /// \param V and optional DWARFFormValue to attempt to extract the value from.
236 /// \param Default the default value to return in case of failure.
237 /// \returns the extracted reference value or Default if the V doesn't have a
238 /// value or the form value's encoding wasn't a reference form.
239 inline uint64_t toReference(const Optional<DWARFFormValue> &V,
240  uint64_t Default) {
241  return toReference(V).getValueOr(Default);
242 }
243 
244 /// Take an optional DWARFFormValue and try to extract an signed constant.
245 ///
246 /// \param V and optional DWARFFormValue to attempt to extract the value from.
247 /// \returns an optional value that contains a value if the form value
248 /// was valid and has a signed constant form.
250  if (V)
251  return V->getAsSignedConstant();
252  return None;
253 }
254 
255 /// Take an optional DWARFFormValue and extract a signed integer.
256 ///
257 /// \param V and optional DWARFFormValue to attempt to extract the value from.
258 /// \param Default the default value to return in case of failure.
259 /// \returns the extracted signed integer value or Default if the V doesn't
260 /// have a value or the form value's encoding wasn't a signed integer form.
261 inline int64_t toSigned(const Optional<DWARFFormValue> &V, int64_t Default) {
262  return toSigned(V).getValueOr(Default);
263 }
264 
265 /// Take an optional DWARFFormValue and try to extract an address.
266 ///
267 /// \param V and optional DWARFFormValue to attempt to extract the value from.
268 /// \returns an optional value that contains a value if the form value
269 /// was valid and has a address form.
271  if (V)
272  return V->getAsAddress();
273  return None;
274 }
275 
276 /// Take an optional DWARFFormValue and extract a address.
277 ///
278 /// \param V and optional DWARFFormValue to attempt to extract the value from.
279 /// \param Default the default value to return in case of failure.
280 /// \returns the extracted address value or Default if the V doesn't have a
281 /// value or the form value's encoding wasn't an address form.
282 inline uint64_t toAddress(const Optional<DWARFFormValue> &V, uint64_t Default) {
283  return toAddress(V).getValueOr(Default);
284 }
285 
286 /// Take an optional DWARFFormValue and try to extract an section offset.
287 ///
288 /// \param V and optional DWARFFormValue to attempt to extract the value from.
289 /// \returns an optional value that contains a value if the form value
290 /// was valid and has a section offset form.
292  if (V)
293  return V->getAsSectionOffset();
294  return None;
295 }
296 
297 /// Take an optional DWARFFormValue and extract a section offset.
298 ///
299 /// \param V and optional DWARFFormValue to attempt to extract the value from.
300 /// \param Default the default value to return in case of failure.
301 /// \returns the extracted section offset value or Default if the V doesn't
302 /// have a value or the form value's encoding wasn't a section offset form.
303 inline uint64_t toSectionOffset(const Optional<DWARFFormValue> &V,
304  uint64_t Default) {
305  return toSectionOffset(V).getValueOr(Default);
306 }
307 
308 /// Take an optional DWARFFormValue and try to extract block data.
309 ///
310 /// \param V and optional DWARFFormValue to attempt to extract the value from.
311 /// \returns an optional value that contains a value if the form value
312 /// was valid and has a block form.
314  if (V)
315  return V->getAsBlock();
316  return None;
317 }
318 
319 } // end namespace dwarf
320 
321 } // end namespace llvm
322 
323 #endif // LLVM_DEBUGINFO_DWARFFORMVALUE_H
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.
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
uint64_t toUnsigned(const Optional< DWARFFormValue > &V, uint64_t Default)
Take an optional DWARFFormValue and extract a unsigned constant.
uint64_t getSectionIndex() const
void setUValue(uint64_t V)
F(f)
dwarf::DwarfFormat Format
dwarf::Form getForm() const
std::string toString(Error E)
Write all error messages (if any) in E to a string.
Definition: Error.h:947
DwarfFormat
Constants that define the DWARF format as 32 or 64 bit.
Definition: Dwarf.h:518
uint64_t getRawUValue() const
void setForm(dwarf::Form F)
uint8_t getRefAddrByteSize() const
The definition of the size of form DW_FORM_ref_addr depends on the version.
bool skipValue(DataExtractor DebugInfoData, uint32_t *OffsetPtr, const DWARFFormParams Params) const
Skip a form&#39;s value in DebugInfoData at the offset specified by OffsetPtr.
const DWARFUnit * getUnit() const
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:153
void setPValue(const char *V)
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
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:150
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
const T * data() const
Definition: ArrayRef.h:147
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.
A helper struct for DWARFFormValue methods, providing information that allows it to know the byte siz...
This file contains constants used for implementing Dwarf debug support.
DWARFFormValue(dwarf::Form F=dwarf::Form(0))
Remember the DWARFUnit at extract time.
uint64_t toAddress(const Optional< DWARFFormValue > &V, uint64_t Default)
Take an optional DWARFFormValue and extract a address.
void setSValue(int64_t V)
uint8_t getDwarfOffsetByteSize() const
The size of a reference is determined by the DWARF 32/64-bit format.
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