LLVM 20.0.0git
DWARFDataExtractor.cpp
Go to the documentation of this file.
1//===- DWARFDataExtractor.cpp ---------------------------------------------===//
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
12#include "llvm/Support/Errc.h"
13
14using namespace llvm;
15
16std::pair<uint64_t, dwarf::DwarfFormat>
18 ErrorAsOutParameter ErrAsOut(Err);
19 if (Err && *Err)
20 return {0, dwarf::DWARF32};
21
22 Cursor C(*Off);
29 cantFail(C.takeError());
30 if (Err)
31 *Err = createStringError(
33 "unsupported reserved unit length of value 0x%8.8" PRIx64, Length);
34 return {0, dwarf::DWARF32};
35 }
36
37 if (C) {
38 *Off = C.tell();
39 return {Length, Format};
40 }
41 if (Err)
42 *Err = C.takeError();
43 else
44 consumeError(C.takeError());
45 return {0, dwarf::DWARF32};
46}
47
49 uint64_t *SecNdx,
50 Error *Err) const {
51 if (SecNdx)
53 if (!Section)
54 return getUnsigned(Off, Size, Err);
55
56 ErrorAsOutParameter ErrAsOut(Err);
57 std::optional<RelocAddrEntry> E = Obj->find(*Section, *Off);
58 uint64_t LocData = getUnsigned(Off, Size, Err);
59 if (!E || (Err && *Err))
60 return LocData;
61 if (SecNdx)
62 *SecNdx = E->SectionIndex;
63
64 uint64_t R =
65 object::resolveRelocation(E->Resolver, E->Reloc, E->SymbolValue, LocData);
66 if (E->Reloc2)
67 R = object::resolveRelocation(E->Resolver, *E->Reloc2, E->SymbolValue2, R);
68 return R;
69}
70
71std::optional<uint64_t>
73 uint64_t PCRelOffset) const {
74 if (Encoding == dwarf::DW_EH_PE_omit)
75 return std::nullopt;
76
77 uint64_t Result = 0;
78 uint64_t OldOffset = *Offset;
79 // First get value
80 switch (Encoding & 0x0F) {
82 switch (getAddressSize()) {
83 case 2:
84 case 4:
85 case 8:
87 break;
88 default:
89 return std::nullopt;
90 }
91 break;
93 Result = getULEB128(Offset);
94 break;
96 Result = getSLEB128(Offset);
97 break;
99 Result = getUnsigned(Offset, 2);
100 break;
102 Result = getUnsigned(Offset, 4);
103 break;
105 Result = getUnsigned(Offset, 8);
106 break;
108 Result = getSigned(Offset, 2);
109 break;
111 Result = SignExtend64<32>(getRelocatedValue(4, Offset));
112 break;
114 Result = getRelocatedValue(8, Offset);
115 break;
116 default:
117 return std::nullopt;
118 }
119 // Then add relative offset, if required
120 switch (Encoding & 0x70) {
122 // do nothing
123 break;
125 Result += PCRelOffset;
126 break;
131 default:
132 *Offset = OldOffset;
133 return std::nullopt;
134 }
135
136 return Result;
137}
uint64_t Size
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...
uint64_t getRelocatedValue(uint32_t Size, uint64_t *Off, uint64_t *SectionIndex=nullptr, Error *Err=nullptr) const
Extracts a value and applies a relocation to the result if one exists for the given offset.
std::optional< uint64_t > getEncodedPointer(uint64_t *Offset, uint8_t Encoding, uint64_t AbsPosOffset=0) const
Extracts a DWARF-encoded pointer in Offset using Encoding.
virtual std::optional< RelocAddrEntry > find(const DWARFSection &Sec, uint64_t Pos) const =0
A class representing a position in a DataExtractor, as well as any error encountered during extractio...
Definition: DataExtractor.h:54
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.
int64_t getSigned(uint64_t *offset_ptr, uint32_t size) const
Extract an signed integer of size byte_size from *offset_ptr.
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.
Definition: DataExtractor.h:99
int64_t getSLEB128(uint64_t *OffsetPtr, Error *Err=nullptr) const
Extract a signed LEB128 value from *offset_ptr.
Helper for Errors used as out-parameters.
Definition: Error.h:1130
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
@ 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:91
@ DWARF64
Definition: Dwarf.h:91
@ DWARF32
Definition: Dwarf.h:91
@ DW_EH_PE_textrel
Definition: Dwarf.h:852
@ DW_EH_PE_datarel
Definition: Dwarf.h:853
@ DW_EH_PE_pcrel
Definition: Dwarf.h:851
@ DW_EH_PE_sdata4
Definition: Dwarf.h:848
@ DW_EH_PE_funcrel
Definition: Dwarf.h:854
@ DW_EH_PE_aligned
Definition: Dwarf.h:855
@ DW_EH_PE_udata2
Definition: Dwarf.h:843
@ DW_EH_PE_sdata8
Definition: Dwarf.h:849
@ DW_EH_PE_absptr
Definition: Dwarf.h:840
@ DW_EH_PE_sdata2
Definition: Dwarf.h:847
@ DW_EH_PE_udata4
Definition: Dwarf.h:844
@ DW_EH_PE_udata8
Definition: Dwarf.h:845
@ DW_EH_PE_uleb128
Definition: Dwarf.h:842
@ DW_EH_PE_sleb128
Definition: Dwarf.h:846
@ DW_EH_PE_omit
Definition: Dwarf.h:841
@ DW_LENGTH_lo_reserved
Special values for an initial length field.
Definition: Dwarf.h:54
@ DW_LENGTH_DWARF64
Indicator of 64-bit DWARF format.
Definition: Dwarf.h:55
uint64_t resolveRelocation(RelocationResolver Resolver, const RelocationRef &R, uint64_t S, uint64_t LocData)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
@ Length
Definition: DWP.cpp:480
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1286
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:756
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1069
static const uint64_t UndefSection
Definition: ObjectFile.h:146