DWARFFormValue.h revision 314564
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"
14#include "llvm/Support/DataExtractor.h"
15#include "llvm/Support/Dwarf.h"
16
17namespace llvm {
18
19template <typename T> class ArrayRef;
20class DWARFUnit;
21class raw_ostream;
22
23class DWARFFormValue {
24public:
25  enum FormClass {
26    FC_Unknown,
27    FC_Address,
28    FC_Block,
29    FC_Constant,
30    FC_String,
31    FC_Flag,
32    FC_Reference,
33    FC_Indirect,
34    FC_SectionOffset,
35    FC_Exprloc
36  };
37
38private:
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
56public:
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.
81  Optional<uint64_t> getAsReference() const;
82  Optional<uint64_t> getAsUnsignedConstant() const;
83  Optional<int64_t> getAsSignedConstant() const;
84  Optional<const char *> getAsCString() const;
85  Optional<uint64_t> getAsAddress() const;
86  Optional<uint64_t> getAsSectionOffset() const;
87  Optional<ArrayRef<uint8_t>> getAsBlock() const;
88  Optional<uint64_t> getAsCStringOffset() const;
89  Optional<uint64_t> getAsReferenceUVal() const;
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.
106  static Optional<uint8_t> getFixedByteSize(dwarf::Form Form,
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.
121  static Optional<uint8_t> getFixedByteSize(dwarf::Form Form, uint16_t Version,
122                                            uint8_t AddrSize,
123                                            llvm::dwarf::DwarfFormat Format);
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
163private:
164  void dumpString(raw_ostream &OS) const;
165};
166
167}
168
169#endif
170