LLVM 22.0.0git
DWARFDataExtractorSimple.h
Go to the documentation of this file.
1//===- DWARFDataExtractorSimple.h -------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_DEBUGINFO_DWARF_LOWLEVEL_DWARFDATAEXTRACTORSIMPLE_H
10#define LLVM_DEBUGINFO_DWARF_LOWLEVEL_DWARFDATAEXTRACTORSIMPLE_H
11
15#include "llvm/Support/Errc.h"
17
18namespace llvm {
19
20/// A DataExtractor suitable use for parsing dwarf from memory. Clients use
21/// Relocator::getRelocatedValueImpl to relocate values as appropriate.
22
23template <typename Relocator>
25
26public:
27 DWARFDataExtractorBase(StringRef Data, bool IsLittleEndian,
28 uint8_t AddressSize)
29 : DataExtractor(Data, IsLittleEndian, AddressSize) {}
30 DWARFDataExtractorBase(ArrayRef<uint8_t> Data, bool IsLittleEndian,
31 uint8_t AddressSize)
33 StringRef(reinterpret_cast<const char *>(Data.data()), Data.size()),
34 IsLittleEndian, AddressSize) {}
35
36 /// Truncating constructor
40
41 /// Extracts a value and returns it as adjusted by the Relocator
43 uint64_t *SectionIndex = nullptr,
44 Error *Err = nullptr) const {
45 return static_cast<const Relocator *>(this)->getRelocatedValueImpl(
46 Size, Off, SectionIndex, Err);
47 }
48
50 uint64_t *SectionIndex = nullptr) const {
51 return getRelocatedValue(Size, &getOffset(C), SectionIndex, &getError(C));
52 }
53
54 /// Extracts an address-sized value.
55 uint64_t getRelocatedAddress(uint64_t *Off, uint64_t *SecIx = nullptr) const {
56 return getRelocatedValue(getAddressSize(), Off, SecIx);
57 }
58
59 uint64_t getRelocatedAddress(Cursor &C, uint64_t *SecIx = nullptr) const {
61 &getError(C));
62 }
63
64 /// Extracts the DWARF "initial length" field, which can either be a 32-bit
65 /// value smaller than 0xfffffff0, or the value 0xffffffff followed by a
66 /// 64-bit length. Returns the actual length, and the DWARF format which is
67 /// encoded in the field. In case of errors, it returns {0, DWARF32} and
68 /// leaves the offset unchanged.
69 std::pair<uint64_t, dwarf::DwarfFormat>
70 getInitialLength(uint64_t *Off, Error *Err = nullptr) const {
71 ErrorAsOutParameter ErrAsOut(Err);
72 if (Err && *Err)
73 return {0, dwarf::DWARF32};
74
75 Cursor C(*Off);
82 cantFail(C.takeError());
83 if (Err)
84 *Err = createStringError(
85 std::errc::invalid_argument,
86 "unsupported reserved unit length of value 0x%8.8" PRIx64, Length);
87 return {0, dwarf::DWARF32};
88 }
89
90 if (C) {
91 *Off = C.tell();
92 return {Length, Format};
93 }
94 if (Err)
95 *Err = C.takeError();
96 else
97 consumeError(C.takeError());
98 return {0, dwarf::DWARF32};
99 }
100
101 std::pair<uint64_t, dwarf::DwarfFormat> getInitialLength(Cursor &C) const {
103 }
104
105 /// Extracts a DWARF-encoded pointer in \p Offset using \p Encoding.
106 /// There is a DWARF encoding that uses a PC-relative adjustment.
107 /// For these values, \p AbsPosOffset is used to fix them, which should
108 /// reflect the absolute address of this pointer.
109 std::optional<uint64_t> getEncodedPointer(uint64_t *Offset, uint8_t Encoding,
110 uint64_t PCRelOffset) const {
111 if (Encoding == dwarf::DW_EH_PE_omit)
112 return std::nullopt;
113
114 uint64_t Result = 0;
115 uint64_t OldOffset = *Offset;
116 // First get value
117 switch (Encoding & 0x0F) {
119 switch (getAddressSize()) {
120 case 2:
121 case 4:
122 case 8:
123 Result = getUnsigned(Offset, getAddressSize());
124 break;
125 default:
126 return std::nullopt;
127 }
128 break;
130 Result = getULEB128(Offset);
131 break;
133 Result = getSLEB128(Offset);
134 break;
136 Result = getUnsigned(Offset, 2);
137 break;
139 Result = getUnsigned(Offset, 4);
140 break;
142 Result = getUnsigned(Offset, 8);
143 break;
145 Result = getSigned(Offset, 2);
146 break;
148 Result = SignExtend64<32>(getRelocatedValue(4, Offset));
149 break;
151 Result = getRelocatedValue(8, Offset);
152 break;
153 default:
154 return std::nullopt;
155 }
156 // Then add relative offset, if required
157 switch (Encoding & 0x70) {
159 // do nothing
160 break;
162 Result += PCRelOffset;
163 break;
168 default:
169 *Offset = OldOffset;
170 return std::nullopt;
171 }
172
173 return Result;
174 }
175};
176
177// Non relocating, low-level dwarf-data extractor. Suitable for use from
178// libraries that cannot have build-time dependencies on relocation providers.
179
181 : public DWARFDataExtractorBase<DWARFDataExtractorSimple> {
183
184 LLVM_ABI uint64_t getRelocatedValueImpl(uint32_t Size, uint64_t *Off,
185 uint64_t *SectionIndex = nullptr,
186 Error *Err = nullptr) const {
187 assert(SectionIndex == nullptr &&
188 "DWARFDATAExtractorSimple cannot take section indices.");
189 return getUnsigned(Off, Size, Err);
190 }
191};
192
193} // end namespace llvm
194#endif // LLVM_DEBUGINFO_DWARF_LOWLEVEL_DWARFDATAEXTRACTORSIMPLE_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
#define LLVM_ABI
Definition: Compiler.h:213
This file contains constants used for implementing Dwarf debug support.
uint64_t Size
static StringRef substr(StringRef Str, uint64_t Len)
static Split data
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
A DataExtractor suitable use for parsing dwarf from memory.
DWARFDataExtractorBase(StringRef Data, bool IsLittleEndian, uint8_t AddressSize)
DWARFDataExtractorBase(const DWARFDataExtractorBase &Other, size_t Length)
Truncating constructor.
std::pair< uint64_t, dwarf::DwarfFormat > getInitialLength(uint64_t *Off, Error *Err=nullptr) const
Extracts the DWARF "initial length" field, which can either be a 32-bit value smaller than 0xfffffff0...
std::optional< uint64_t > getEncodedPointer(uint64_t *Offset, uint8_t Encoding, uint64_t PCRelOffset) const
Extracts a DWARF-encoded pointer in Offset using Encoding.
uint64_t getRelocatedAddress(uint64_t *Off, uint64_t *SecIx=nullptr) const
Extracts an address-sized value.
uint64_t getRelocatedValue(Cursor &C, uint32_t Size, uint64_t *SectionIndex=nullptr) const
DWARFDataExtractorBase(ArrayRef< uint8_t > Data, bool IsLittleEndian, uint8_t AddressSize)
uint64_t getRelocatedAddress(Cursor &C, uint64_t *SecIx=nullptr) const
uint64_t getRelocatedValue(uint32_t Size, uint64_t *Off, uint64_t *SectionIndex=nullptr, Error *Err=nullptr) const
Extracts a value and returns it as adjusted by the Relocator.
std::pair< uint64_t, dwarf::DwarfFormat > getInitialLength(Cursor &C) const
A class representing a position in a DataExtractor, as well as any error encountered during extractio...
Definition: DataExtractor.h:55
LLVM_ABI uint64_t getUnsigned(uint64_t *offset_ptr, uint32_t byte_size, Error *Err=nullptr) const
Extract an unsigned integer of size byte_size from *offset_ptr.
size_t size() const
Return the number of bytes in the underlying buffer.
static uint64_t & getOffset(Cursor &C)
LLVM_ABI int64_t getSigned(uint64_t *offset_ptr, uint32_t size) const
Extract an signed integer of size byte_size from *offset_ptr.
LLVM_ABI uint64_t getULEB128(uint64_t *offset_ptr, llvm::Error *Err=nullptr) const
Extract a unsigned LEB128 value from *offset_ptr.
uint8_t getAddressSize() const
Get the address size for this extractor.
StringRef getData() const
Get the data pointed to by this extractor.
Definition: DataExtractor.h:96
LLVM_ABI int64_t getSLEB128(uint64_t *OffsetPtr, Error *Err=nullptr) const
Extract a signed LEB128 value from *offset_ptr.
static Error & getError(Cursor &C)
bool isLittleEndian() const
Get the endianness for this extractor.
Definition: DataExtractor.h:98
Helper for Errors used as out-parameters.
Definition: Error.h:1144
Lightweight error class with error context and mandatory checking.
Definition: Error.h:159
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
DwarfFormat
Constants that define the DWARF format as 32 or 64 bit.
Definition: Dwarf.h:92
@ DWARF64
Definition: Dwarf.h:92
@ DWARF32
Definition: Dwarf.h:92
@ DW_EH_PE_textrel
Definition: Dwarf.h:866
@ DW_EH_PE_datarel
Definition: Dwarf.h:867
@ DW_EH_PE_pcrel
Definition: Dwarf.h:865
@ DW_EH_PE_sdata4
Definition: Dwarf.h:862
@ DW_EH_PE_funcrel
Definition: Dwarf.h:868
@ DW_EH_PE_aligned
Definition: Dwarf.h:869
@ DW_EH_PE_udata2
Definition: Dwarf.h:857
@ DW_EH_PE_sdata8
Definition: Dwarf.h:863
@ DW_EH_PE_absptr
Definition: Dwarf.h:854
@ DW_EH_PE_sdata2
Definition: Dwarf.h:861
@ DW_EH_PE_udata4
Definition: Dwarf.h:858
@ DW_EH_PE_udata8
Definition: Dwarf.h:859
@ DW_EH_PE_uleb128
Definition: Dwarf.h:856
@ DW_EH_PE_sleb128
Definition: Dwarf.h:860
@ DW_EH_PE_omit
Definition: Dwarf.h:855
@ DW_LENGTH_lo_reserved
Special values for an initial length field.
Definition: Dwarf.h:55
@ DW_LENGTH_DWARF64
Indicator of 64-bit DWARF format.
Definition: Dwarf.h:56
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:477
@ Length
Definition: DWP.cpp:477
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1305
@ Other
Any other memory.
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:769
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1083