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 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;
67 /// \brief extracts a value in data at offset *offset_ptr.
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,
75 bool isInlinedCStr() const {
76 return Value.data != nullptr && Value.data == (const uint8_t*)Value.cstr;
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.
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
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.
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.
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.
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,
123 llvm::dwarf::DwarfFormat Format);
125 /// Skip a form in \p debug_info_data at offset specified by \p offset_ptr.
127 /// Skips the bytes for this form in the debug info and updates the offset.
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.
138 /// Skips the bytes for this form in the debug info and updates the offset.
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.
150 /// Skips the bytes for this form in the debug info and updates the offset.
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);
164 void dumpString(raw_ostream &OS) const;