OSDN Git Service

[ObjectYAML] Support for DWARF debug_info section
[android-x86/external-llvm.git] / include / llvm / DebugInfo / DWARF / DWARFFormValue.h
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
17 namespace llvm {
18
19 template <typename T> class ArrayRef;
20 class DWARFUnit;
21 class raw_ostream;
22
23 class DWARFFormValue {
24 public:
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
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   bool isFormClass(FormClass FC) const;
61   const DWARFUnit *getUnit() const { return U; }
62   void dump(raw_ostream &OS) const;
63
64   /// \brief extracts a value in data at offset *offset_ptr.
65   ///
66   /// The passed DWARFUnit is allowed to be nullptr, in which
67   /// case no relocation processing will be performed and some
68   /// kind of forms that depend on Unit information are disallowed.
69   /// \returns whether the extraction succeeded.
70   bool extractValue(const DataExtractor &Data, uint32_t *OffsetPtr,
71                     const DWARFUnit *U);
72   bool isInlinedCStr() const {
73     return Value.data != nullptr && Value.data == (const uint8_t*)Value.cstr;
74   }
75
76   /// getAsFoo functions below return the extracted value as Foo if only
77   /// DWARFFormValue has form class is suitable for representing Foo.
78   Optional<uint64_t> getAsReference() const;
79   Optional<uint64_t> getAsUnsignedConstant() const;
80   Optional<int64_t> getAsSignedConstant() const;
81   Optional<const char *> getAsCString() const;
82   Optional<uint64_t> getAsAddress() const;
83   Optional<uint64_t> getAsSectionOffset() const;
84   Optional<ArrayRef<uint8_t>> getAsBlock() const;
85   Optional<uint64_t> getAsCStringOffset() const;
86   Optional<uint64_t> getAsReferenceUVal() const;
87   /// Get the fixed byte size for a given form.
88   ///
89   /// If the form always has a fixed valid byte size that doesn't depend on a
90   /// DWARFUnit, then an Optional with a value will be returned. If the form
91   /// can vary in size depending on the DWARFUnit (DWARF version, address byte
92   /// size, or DWARF 32/64) and the DWARFUnit is valid, then an Optional with a
93   /// valid value is returned. If the form is always encoded using a variable
94   /// length storage format (ULEB or SLEB numbers or blocks) or the size
95   /// depends on a DWARFUnit and the DWARFUnit is NULL, then None will be
96   /// returned.
97   /// \param Form The DWARF form to get the fixed byte size for
98   /// \param U The DWARFUnit that can be used to help determine the byte size.
99   ///
100   /// \returns Optional<uint8_t> value with the fixed byte size or None if
101   /// \p Form doesn't have a fixed byte size or a DWARFUnit wasn't supplied
102   /// and was needed to calculate the byte size.
103   static Optional<uint8_t> getFixedByteSize(dwarf::Form Form,
104                                             const DWARFUnit *U = nullptr);
105   /// Get the fixed byte size for a given form.
106   ///
107   /// If the form has a fixed byte size given a valid DWARF version and address
108   /// byte size, then an Optional with a valid value is returned. If the form
109   /// is always encoded using a variable length storage format (ULEB or SLEB
110   /// numbers or blocks) then None will be returned.
111   ///
112   /// \param Form DWARF form to get the fixed byte size for
113   /// \param Version DWARF version number.
114   /// \param AddrSize size of an address in bytes.
115   /// \param Format enum value from llvm::dwarf::DwarfFormat.
116   /// \returns Optional<uint8_t> value with the fixed byte size or None if
117   /// \p Form doesn't have a fixed byte size.
118   static Optional<uint8_t> getFixedByteSize(dwarf::Form Form, uint16_t Version,
119                                             uint8_t AddrSize,
120                                             llvm::dwarf::DwarfFormat Format);
121
122   /// Skip a form in \p debug_info_data at offset specified by \p offset_ptr.
123   ///
124   /// Skips the bytes for this form in the debug info and updates the offset.
125   ///
126   /// \param debug_info_data the .debug_info data to use to skip the value.
127   /// \param offset_ptr a reference to the offset that will be updated.
128   /// \param U the DWARFUnit to use when skipping the form in case the form
129   /// size differs according to data in the DWARFUnit.
130   /// \returns true on success, false if the form was not skipped.
131   bool skipValue(DataExtractor debug_info_data, uint32_t *offset_ptr,
132                  const DWARFUnit *U) const;
133   /// Skip a form in \p debug_info_data at offset specified by \p offset_ptr.
134   ///
135   /// Skips the bytes for this form in the debug info and updates the offset.
136   ///
137   /// \param form the DW_FORM enumeration that indicates the form to skip.
138   /// \param debug_info_data the .debug_info data to use to skip the value.
139   /// \param offset_ptr a reference to the offset that will be updated.
140   /// \param U the DWARFUnit to use when skipping the form in case the form
141   /// size differs according to data in the DWARFUnit.
142   /// \returns true on success, false if the form was not skipped.
143   static bool skipValue(dwarf::Form form, DataExtractor debug_info_data,
144                         uint32_t *offset_ptr, const DWARFUnit *U);
145   /// Skip a form in \p debug_info_data at offset specified by \p offset_ptr.
146   ///
147   /// Skips the bytes for this form in the debug info and updates the offset.
148   ///
149   /// \param form the DW_FORM enumeration that indicates the form to skip.
150   /// \param debug_info_data the .debug_info data to use to skip the value.
151   /// \param offset_ptr a reference to the offset that will be updated.
152   /// \param Version DWARF version number.
153   /// \param AddrSize size of an address in bytes.
154   /// \param Format enum value from llvm::dwarf::DwarfFormat.
155   /// \returns true on success, false if the form was not skipped.
156   static bool skipValue(dwarf::Form form, DataExtractor debug_info_data,
157                         uint32_t *offset_ptr, uint16_t Version,
158                         uint8_t AddrSize, llvm::dwarf::DwarfFormat Format);
159
160 private:
161   void dumpString(raw_ostream &OS) const;
162 };
163
164 }
165
166 #endif