1 //===-- DWARFFormValue.h ----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_DEBUGINFO_DWARFFORMVALUE_H
11 #define LLVM_DEBUGINFO_DWARFFORMVALUE_H
13 #include "llvm/ADT/Optional.h"
14 #include "llvm/Support/DataExtractor.h"
15 #include "llvm/Support/Dwarf.h"
19 template <typename T> class ArrayRef;
23 class DWARFFormValue {
40 ValueType() : data(nullptr) {
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.
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;
64 /// \brief extracts a value in data at offset *offset_ptr.
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,
72 bool isInlinedCStr() const {
73 return Value.data != nullptr && Value.data == (const uint8_t*)Value.cstr;
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.
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
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.
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.
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.
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,
120 llvm::dwarf::DwarfFormat Format);
122 /// Skip a form in \p debug_info_data at offset specified by \p offset_ptr.
124 /// Skips the bytes for this form in the debug info and updates the offset.
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.
135 /// Skips the bytes for this form in the debug info and updates the offset.
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.
147 /// Skips the bytes for this form in the debug info and updates the offset.
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);
161 void dumpString(raw_ostream &OS) const;