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
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"
13
14using namespace llvm;
15
16std::pair<uint64_t, dwarf::DwarfFormat>
17DWARFDataExtractor::getInitialLength(uint64_t *Off, Error *Err) const {
18 ErrorAsOutParameter ErrAsOut(Err);
19 if (Err && *Err)
20 return {0, dwarf::DWARF32};
21
22 Cursor C(*Off);
23 uint64_t Length = getRelocatedValue(C, Size: 4);
24 dwarf::DwarfFormat Format = dwarf::DWARF32;
25 if (Length == dwarf::DW_LENGTH_DWARF64) {
26 Length = getRelocatedValue(C, Size: 8);
27 Format = dwarf::DWARF64;
28 } else if (Length >= dwarf::DW_LENGTH_lo_reserved) {
29 cantFail(Err: C.takeError());
30 if (Err)
31 *Err = createStringError(
32 EC: errc::invalid_argument,
33 Fmt: "unsupported reserved unit length of value 0x%8.8" PRIx64, Vals: 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(Err: C.takeError());
45 return {0, dwarf::DWARF32};
46}
47
48uint64_t DWARFDataExtractor::getRelocatedValue(uint32_t Size, uint64_t *Off,
49 uint64_t *SecNdx,
50 Error *Err) const {
51 if (SecNdx)
52 *SecNdx = object::SectionedAddress::UndefSection;
53 if (!Section)
54 return getUnsigned(offset_ptr: Off, byte_size: Size, Err);
55
56 ErrorAsOutParameter ErrAsOut(Err);
57 std::optional<RelocAddrEntry> E = Obj->find(Sec: *Section, Pos: *Off);
58 uint64_t LocData = getUnsigned(offset_ptr: Off, byte_size: 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(Resolver: E->Resolver, R: E->Reloc, S: E->SymbolValue, LocData);
66 if (E->Reloc2)
67 R = object::resolveRelocation(Resolver: E->Resolver, R: *E->Reloc2, S: E->SymbolValue2, LocData: R);
68 return R;
69}
70
71std::optional<uint64_t>
72DWARFDataExtractor::getEncodedPointer(uint64_t *Offset, uint8_t Encoding,
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) {
81 case dwarf::DW_EH_PE_absptr:
82 switch (getAddressSize()) {
83 case 2:
84 case 4:
85 case 8:
86 Result = getUnsigned(offset_ptr: Offset, byte_size: getAddressSize());
87 break;
88 default:
89 return std::nullopt;
90 }
91 break;
92 case dwarf::DW_EH_PE_uleb128:
93 Result = getULEB128(offset_ptr: Offset);
94 break;
95 case dwarf::DW_EH_PE_sleb128:
96 Result = getSLEB128(OffsetPtr: Offset);
97 break;
98 case dwarf::DW_EH_PE_udata2:
99 Result = getUnsigned(offset_ptr: Offset, byte_size: 2);
100 break;
101 case dwarf::DW_EH_PE_udata4:
102 Result = getUnsigned(offset_ptr: Offset, byte_size: 4);
103 break;
104 case dwarf::DW_EH_PE_udata8:
105 Result = getUnsigned(offset_ptr: Offset, byte_size: 8);
106 break;
107 case dwarf::DW_EH_PE_sdata2:
108 Result = getSigned(offset_ptr: Offset, size: 2);
109 break;
110 case dwarf::DW_EH_PE_sdata4:
111 Result = SignExtend64<32>(x: getRelocatedValue(Size: 4, Off: Offset));
112 break;
113 case dwarf::DW_EH_PE_sdata8:
114 Result = getRelocatedValue(Size: 8, Off: Offset);
115 break;
116 default:
117 return std::nullopt;
118 }
119 // Then add relative offset, if required
120 switch (Encoding & 0x70) {
121 case dwarf::DW_EH_PE_absptr:
122 // do nothing
123 break;
124 case dwarf::DW_EH_PE_pcrel:
125 Result += PCRelOffset;
126 break;
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:
131 default:
132 *Offset = OldOffset;
133 return std::nullopt;
134 }
135
136 return Result;
137}
138