LLVM  4.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/Optional.h"
15 #include "llvm/Support/Dwarf.h"
16 
17 namespace llvm {
18 
19 template <typename T> class ArrayRef;
20 class DWARFUnit;
21 class raw_ostream;
22 
24 public:
25  enum FormClass {
36  };
37 
38 private:
39  struct ValueType {
40  ValueType() : data(nullptr) {
41  uval = 0;
42  }
43 
44  union {
45  uint64_t uval;
46  int64_t sval;
47  const char* cstr;
48  };
49  const uint8_t* data;
50  };
51 
52  dwarf::Form Form; // Form for this value.
53  ValueType Value; // Contains all data for the form.
54  const DWARFUnit *U; // Remember the DWARFUnit at extract time.
55 
56 public:
57  DWARFFormValue(dwarf::Form F = dwarf::Form(0)) : Form(F), U(nullptr) {}
58  dwarf::Form getForm() const { return Form; }
59  void setForm(dwarf::Form F) { Form = F; }
60  void setUValue(uint64_t V) { Value.uval = V; }
61  void setSValue(int64_t V) { Value.sval = V; }
62  void setPValue(const char *V) { Value.cstr = V; }
63  bool isFormClass(FormClass FC) const;
64  const DWARFUnit *getUnit() const { return U; }
65  void dump(raw_ostream &OS) const;
66 
67  /// \brief extracts a value in data at offset *offset_ptr.
68  ///
69  /// The passed DWARFUnit is allowed to be nullptr, in which
70  /// case no relocation processing will be performed and some
71  /// kind of forms that depend on Unit information are disallowed.
72  /// \returns whether the extraction succeeded.
73  bool extractValue(const DataExtractor &Data, uint32_t *OffsetPtr,
74  const DWARFUnit *U);
75  bool isInlinedCStr() const {
76  return Value.data != nullptr && Value.data == (const uint8_t*)Value.cstr;
77  }
78 
79  /// getAsFoo functions below return the extracted value as Foo if only
80  /// DWARFFormValue has form class is suitable for representing Foo.
90  /// Get the fixed byte size for a given form.
91  ///
92  /// If the form always has a fixed valid byte size that doesn't depend on a
93  /// DWARFUnit, then an Optional with a value will be returned. If the form
94  /// can vary in size depending on the DWARFUnit (DWARF version, address byte
95  /// size, or DWARF 32/64) and the DWARFUnit is valid, then an Optional with a
96  /// valid value is returned. If the form is always encoded using a variable
97  /// length storage format (ULEB or SLEB numbers or blocks) or the size
98  /// depends on a DWARFUnit and the DWARFUnit is NULL, then None will be
99  /// returned.
100  /// \param Form The DWARF form to get the fixed byte size for
101  /// \param U The DWARFUnit that can be used to help determine the byte size.
102  ///
103  /// \returns Optional<uint8_t> value with the fixed byte size or None if
104  /// \p Form doesn't have a fixed byte size or a DWARFUnit wasn't supplied
105  /// and was needed to calculate the byte size.
107  const DWARFUnit *U = nullptr);
108  /// Get the fixed byte size for a given form.
109  ///
110  /// If the form has a fixed byte size given a valid DWARF version and address
111  /// byte size, then an Optional with a valid value is returned. If the form
112  /// is always encoded using a variable length storage format (ULEB or SLEB
113  /// numbers or blocks) then None will be returned.
114  ///
115  /// \param Form DWARF form to get the fixed byte size for
116  /// \param Version DWARF version number.
117  /// \param AddrSize size of an address in bytes.
118  /// \param Format enum value from llvm::dwarf::DwarfFormat.
119  /// \returns Optional<uint8_t> value with the fixed byte size or None if
120  /// \p Form doesn't have a fixed byte size.
122  uint8_t AddrSize,
124 
125  /// Skip a form in \p debug_info_data at offset specified by \p offset_ptr.
126  ///
127  /// Skips the bytes for this form in the debug info and updates the offset.
128  ///
129  /// \param debug_info_data the .debug_info data to use to skip the value.
130  /// \param offset_ptr a reference to the offset that will be updated.
131  /// \param U the DWARFUnit to use when skipping the form in case the form
132  /// size differs according to data in the DWARFUnit.
133  /// \returns true on success, false if the form was not skipped.
134  bool skipValue(DataExtractor debug_info_data, uint32_t *offset_ptr,
135  const DWARFUnit *U) const;
136  /// Skip a form in \p debug_info_data at offset specified by \p offset_ptr.
137  ///
138  /// Skips the bytes for this form in the debug info and updates the offset.
139  ///
140  /// \param form the DW_FORM enumeration that indicates the form to skip.
141  /// \param debug_info_data the .debug_info data to use to skip the value.
142  /// \param offset_ptr a reference to the offset that will be updated.
143  /// \param U the DWARFUnit to use when skipping the form in case the form
144  /// size differs according to data in the DWARFUnit.
145  /// \returns true on success, false if the form was not skipped.
146  static bool skipValue(dwarf::Form form, DataExtractor debug_info_data,
147  uint32_t *offset_ptr, const DWARFUnit *U);
148  /// Skip a form in \p debug_info_data at offset specified by \p offset_ptr.
149  ///
150  /// Skips the bytes for this form in the debug info and updates the offset.
151  ///
152  /// \param form the DW_FORM enumeration that indicates the form to skip.
153  /// \param debug_info_data the .debug_info data to use to skip the value.
154  /// \param offset_ptr a reference to the offset that will be updated.
155  /// \param Version DWARF version number.
156  /// \param AddrSize size of an address in bytes.
157  /// \param Format enum value from llvm::dwarf::DwarfFormat.
158  /// \returns true on success, false if the form was not skipped.
159  static bool skipValue(dwarf::Form form, DataExtractor debug_info_data,
160  uint32_t *offset_ptr, uint16_t Version,
161  uint8_t AddrSize, llvm::dwarf::DwarfFormat Format);
162 
163 private:
164  void dumpString(raw_ostream &OS) const;
165 };
166 
167 }
168 
169 #endif
bool extractValue(const DataExtractor &Data, uint32_t *OffsetPtr, const DWARFUnit *U)
extracts a value in data at offset *offset_ptr.
void setUValue(uint64_t V)
Optional< uint64_t > getAsCStringOffset() const
Optional< const char * > getAsCString() const
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
void setForm(dwarf::Form F)
#define F(x, y, z)
Definition: MD5.cpp:51
void setPValue(const char *V)
DwarfFormat
Constants that define the DWARF format as 32 or 64 bit.
Definition: Dwarf.h:440
bool isInlinedCStr() const
Optional< uint64_t > getAsUnsignedConstant() const
Optional< uint64_t > getAsAddress() const
bool isFormClass(FormClass FC) const
Optional< int64_t > getAsSignedConstant() const
static Optional< uint8_t > getFixedByteSize(dwarf::Form Form, const DWARFUnit *U=nullptr)
Get the fixed byte size for a given form.
DWARFFormValue(dwarf::Form F=dwarf::Form(0))
Optional< uint64_t > getAsSectionOffset() const
Optional< uint64_t > getAsReferenceUVal() const
void setSValue(int64_t V)
LLVM Value Representation.
Definition: Value.h:71
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
PointerUnion< const Value *, const PseudoSourceValue * > ValueType
bool skipValue(DataExtractor debug_info_data, uint32_t *offset_ptr, const DWARFUnit *U) const
Skip a form in debug_info_data at offset specified by offset_ptr.
const uint64_t Version
Definition: InstrProf.h:799
void dump(raw_ostream &OS) const