1 //===- DWARFDataExtractor.cpp ---------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
10 #include "llvm/DebugInfo/DWARF/DWARFObject.h"
11 #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
12 #include "llvm/Support/Errc.h"
16 std::pair
<uint64_t, dwarf::DwarfFormat
>
17 DWARFDataExtractor::getInitialLength(uint64_t *Off
, Error
*Err
) const {
18 ErrorAsOutParameter
ErrAsOut(Err
);
20 return {0, dwarf::DWARF32
};
23 uint64_t Length
= getRelocatedValue(C
, 4);
24 dwarf::DwarfFormat Format
= dwarf::DWARF32
;
25 if (Length
== dwarf::DW_LENGTH_DWARF64
) {
26 Length
= getRelocatedValue(C
, 8);
27 Format
= dwarf::DWARF64
;
28 } else if (Length
>= dwarf::DW_LENGTH_lo_reserved
) {
29 cantFail(C
.takeError());
31 *Err
= createStringError(
32 errc::invalid_argument
,
33 "unsupported reserved unit length of value 0x%8.8" PRIx64
, Length
);
34 return {0, dwarf::DWARF32
};
39 return {Length
, Format
};
44 consumeError(C
.takeError());
45 return {0, dwarf::DWARF32
};
48 uint64_t DWARFDataExtractor::getRelocatedValue(uint32_t Size
, uint64_t *Off
,
52 *SecNdx
= object::SectionedAddress::UndefSection
;
54 return getUnsigned(Off
, Size
, Err
);
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
))
62 *SecNdx
= E
->SectionIndex
;
65 object::resolveRelocation(E
->Resolver
, E
->Reloc
, E
->SymbolValue
, LocData
);
67 R
= object::resolveRelocation(E
->Resolver
, *E
->Reloc2
, E
->SymbolValue2
, R
);
71 std::optional
<uint64_t>
72 DWARFDataExtractor::getEncodedPointer(uint64_t *Offset
, uint8_t Encoding
,
73 uint64_t PCRelOffset
) const {
74 if (Encoding
== dwarf::DW_EH_PE_omit
)
78 uint64_t OldOffset
= *Offset
;
80 switch (Encoding
& 0x0F) {
81 case dwarf::DW_EH_PE_absptr
:
82 switch (getAddressSize()) {
86 Result
= getUnsigned(Offset
, getAddressSize());
92 case dwarf::DW_EH_PE_uleb128
:
93 Result
= getULEB128(Offset
);
95 case dwarf::DW_EH_PE_sleb128
:
96 Result
= getSLEB128(Offset
);
98 case dwarf::DW_EH_PE_udata2
:
99 Result
= getUnsigned(Offset
, 2);
101 case dwarf::DW_EH_PE_udata4
:
102 Result
= getUnsigned(Offset
, 4);
104 case dwarf::DW_EH_PE_udata8
:
105 Result
= getUnsigned(Offset
, 8);
107 case dwarf::DW_EH_PE_sdata2
:
108 Result
= getSigned(Offset
, 2);
110 case dwarf::DW_EH_PE_sdata4
:
111 Result
= SignExtend64
<32>(getRelocatedValue(4, Offset
));
113 case dwarf::DW_EH_PE_sdata8
:
114 Result
= getRelocatedValue(8, Offset
);
119 // Then add relative offset, if required
120 switch (Encoding
& 0x70) {
121 case dwarf::DW_EH_PE_absptr
:
124 case dwarf::DW_EH_PE_pcrel
:
125 Result
+= PCRelOffset
;
127 case dwarf::DW_EH_PE_datarel
:
128 case dwarf::DW_EH_PE_textrel
:
129 case dwarf::DW_EH_PE_funcrel
:
130 case dwarf::DW_EH_PE_aligned
: