LLVM 23.0.0git
DWARFDebugRnglists.cpp
Go to the documentation of this file.
1//===- DWARFDebugRnglists.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
13#include "llvm/Support/Errc.h"
14#include "llvm/Support/Error.h"
15#include "llvm/Support/Format.h"
19
20using namespace llvm;
21
23 Offset = *OffsetPtr;
24 SectionIndex = -1ULL;
25 // The caller should guarantee that we have at least 1 byte available, so
26 // we just assert instead of revalidate.
27 assert(*OffsetPtr < Data.size() &&
28 "not enough space to extract a rangelist encoding");
29 uint8_t Encoding = Data.getU8(OffsetPtr);
30
31 DataExtractor::Cursor C(*OffsetPtr);
32 switch (Encoding) {
33 case dwarf::DW_RLE_end_of_list:
34 Value0 = Value1 = 0;
35 break;
36 // TODO: Support other encodings.
37 case dwarf::DW_RLE_base_addressx: {
38 Value0 = Data.getULEB128(C);
39 break;
40 }
41 case dwarf::DW_RLE_startx_endx:
42 Value0 = Data.getULEB128(C);
43 Value1 = Data.getULEB128(C);
44 break;
45 case dwarf::DW_RLE_startx_length: {
46 Value0 = Data.getULEB128(C);
47 Value1 = Data.getULEB128(C);
48 break;
49 }
50 case dwarf::DW_RLE_offset_pair: {
51 Value0 = Data.getULEB128(C);
52 Value1 = Data.getULEB128(C);
53 break;
54 }
55 case dwarf::DW_RLE_base_address: {
56 Value0 = Data.getRelocatedAddress(C, &SectionIndex);
57 break;
58 }
59 case dwarf::DW_RLE_start_end: {
60 Value0 = Data.getRelocatedAddress(C, &SectionIndex);
61 Value1 = Data.getRelocatedAddress(C);
62 break;
63 }
64 case dwarf::DW_RLE_start_length: {
65 Value0 = Data.getRelocatedAddress(C, &SectionIndex);
66 Value1 = Data.getULEB128(C);
67 break;
68 }
69 default:
70 consumeError(C.takeError());
72 "unknown rnglists encoding 0x%" PRIx32
73 " at offset 0x%" PRIx64,
74 uint32_t(Encoding), Offset);
75 }
76
77 if (!C) {
78 consumeError(C.takeError());
79 return createStringError(
81 "read past end of table when reading %s encoding at offset 0x%" PRIx64,
82 dwarf::RLEString(Encoding).data(), Offset);
83 }
84
85 *OffsetPtr = C.tell();
86 EntryKind = Encoding;
87 return Error::success();
88}
89
91 std::optional<object::SectionedAddress> BaseAddr, DWARFUnit &U) const {
92 return getAbsoluteRanges(
93 BaseAddr, U.getAddressByteSize(),
94 [&](uint32_t Index) { return U.getAddrOffsetSectionItem(Index); });
95}
96
98 std::optional<object::SectionedAddress> BaseAddr, uint8_t AddressByteSize,
99 function_ref<std::optional<object::SectionedAddress>(uint32_t)>
100 LookupPooledAddress) const {
103 for (const RangeListEntry &RLE : Entries) {
104 if (RLE.EntryKind == dwarf::DW_RLE_end_of_list)
105 break;
106 if (RLE.EntryKind == dwarf::DW_RLE_base_addressx) {
107 BaseAddr = LookupPooledAddress(RLE.Value0);
108 if (!BaseAddr)
109 BaseAddr = {RLE.Value0, -1ULL};
110 continue;
111 }
112 if (RLE.EntryKind == dwarf::DW_RLE_base_address) {
113 BaseAddr = {RLE.Value0, RLE.SectionIndex};
114 continue;
115 }
116
118 E.SectionIndex = RLE.SectionIndex;
119 if (BaseAddr && E.SectionIndex == -1ULL)
120 E.SectionIndex = BaseAddr->SectionIndex;
121
122 switch (RLE.EntryKind) {
123 case dwarf::DW_RLE_offset_pair:
124 E.LowPC = RLE.Value0;
125 if (E.LowPC == Tombstone)
126 continue;
127 E.HighPC = RLE.Value1;
128 if (BaseAddr) {
129 if (BaseAddr->Address == Tombstone)
130 continue;
131 E.LowPC += BaseAddr->Address;
132 E.HighPC += BaseAddr->Address;
133 }
134 break;
135 case dwarf::DW_RLE_start_end:
136 E.LowPC = RLE.Value0;
137 E.HighPC = RLE.Value1;
138 break;
139 case dwarf::DW_RLE_start_length:
140 E.LowPC = RLE.Value0;
141 E.HighPC = E.LowPC + RLE.Value1;
142 break;
143 case dwarf::DW_RLE_startx_length: {
144 auto Start = LookupPooledAddress(RLE.Value0);
145 if (!Start)
146 Start = {0, -1ULL};
147 E.SectionIndex = Start->SectionIndex;
148 E.LowPC = Start->Address;
149 E.HighPC = E.LowPC + RLE.Value1;
150 break;
151 }
152 case dwarf::DW_RLE_startx_endx: {
153 auto Start = LookupPooledAddress(RLE.Value0);
154 if (!Start)
155 Start = {0, -1ULL};
156 auto End = LookupPooledAddress(RLE.Value1);
157 if (!End)
158 End = {0, -1ULL};
159 // FIXME: Some error handling if Start.SectionIndex != End.SectionIndex
160 E.SectionIndex = Start->SectionIndex;
161 E.LowPC = Start->Address;
162 E.HighPC = End->Address;
163 break;
164 }
165 default:
166 // Unsupported encodings should have been reported during extraction,
167 // so we should not run into any here.
168 llvm_unreachable("Unsupported range list encoding");
169 }
170 if (E.LowPC == Tombstone)
171 continue;
172 Res.push_back(E);
173 }
174 return Res;
175}
176
178 raw_ostream &OS, uint8_t AddrSize, uint8_t MaxEncodingStringLength,
179 uint64_t &CurrentBase, DIDumpOptions DumpOpts,
180 llvm::function_ref<std::optional<object::SectionedAddress>(uint32_t)>
181 LookupPooledAddress) const {
182 auto PrintRawEntry = [](raw_ostream &OS, const RangeListEntry &Entry,
183 uint8_t AddrSize, DIDumpOptions DumpOpts) {
184 if (DumpOpts.Verbose) {
185 DumpOpts.DisplayRawContents = true;
186 DWARFAddressRange(Entry.Value0, Entry.Value1)
187 .dump(OS, AddrSize, DumpOpts);
188 OS << " => ";
189 }
190 };
191
192 if (DumpOpts.Verbose) {
193 // Print the section offset in verbose mode.
194 OS << formatv("{0:x8}:", Offset);
195 auto EncodingString = dwarf::RangeListEncodingString(EntryKind);
196 // Unsupported encodings should have been reported during parsing.
197 assert(!EncodingString.empty() && "Unknown range entry encoding");
198 OS << formatv(" [{0}]",
199 fmt_pad(EncodingString.data(), 0,
200 MaxEncodingStringLength - EncodingString.size()));
201 if (EntryKind != dwarf::DW_RLE_end_of_list)
202 OS << ": ";
203 }
204
206
207 switch (EntryKind) {
208 case dwarf::DW_RLE_end_of_list:
209 OS << (DumpOpts.Verbose ? "" : "<End of list>");
210 break;
211 case dwarf::DW_RLE_base_addressx: {
212 if (auto SA = LookupPooledAddress(Value0))
213 CurrentBase = SA->Address;
214 else
215 CurrentBase = Value0;
216 if (!DumpOpts.Verbose)
217 return;
218 DWARFFormValue::dumpAddress(OS << ' ', AddrSize, Value0);
219 break;
220 }
221 case dwarf::DW_RLE_base_address:
222 // In non-verbose mode we do not print anything for this entry.
223 CurrentBase = Value0;
224 if (!DumpOpts.Verbose)
225 return;
226 DWARFFormValue::dumpAddress(OS << ' ', AddrSize, Value0);
227 break;
228 case dwarf::DW_RLE_start_length:
229 PrintRawEntry(OS, *this, AddrSize, DumpOpts);
230 DWARFAddressRange(Value0, Value0 + Value1).dump(OS, AddrSize, DumpOpts);
231 break;
232 case dwarf::DW_RLE_offset_pair:
233 PrintRawEntry(OS, *this, AddrSize, DumpOpts);
234 if (CurrentBase != Tombstone)
235 DWARFAddressRange(Value0 + CurrentBase, Value1 + CurrentBase)
236 .dump(OS, AddrSize, DumpOpts);
237 else
238 OS << "dead code";
239 break;
240 case dwarf::DW_RLE_start_end:
241 DWARFAddressRange(Value0, Value1).dump(OS, AddrSize, DumpOpts);
242 break;
243 case dwarf::DW_RLE_startx_length: {
244 PrintRawEntry(OS, *this, AddrSize, DumpOpts);
245 uint64_t Start = 0;
246 if (auto SA = LookupPooledAddress(Value0))
247 Start = SA->Address;
248 DWARFAddressRange(Start, Start + Value1).dump(OS, AddrSize, DumpOpts);
249 break;
250 }
251 case dwarf::DW_RLE_startx_endx: {
252 PrintRawEntry(OS, *this, AddrSize, DumpOpts);
253 uint64_t Start = 0;
254 if (auto SA = LookupPooledAddress(Value0))
255 Start = SA->Address;
256 uint64_t End = 0;
257 if (auto SA = LookupPooledAddress(Value1))
258 End = SA->Address;
259 DWARFAddressRange(Start, End).dump(OS, AddrSize, DumpOpts);
260 break;
261 }
262 default:
263 llvm_unreachable("Unsupported range list encoding");
264 }
265 OS << "\n";
266}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file contains constants used for implementing Dwarf debug support.
static Split data
LocallyHashedType DenseMapInfo< LocallyHashedType >::Tombstone
A DWARFDataExtractor (typically for an in-memory copy of an object-file section) plus a relocation ma...
LLVM_ABI DWARFAddressRangesVector getAbsoluteRanges(std::optional< object::SectionedAddress > BaseAddr, uint8_t AddressByteSize, function_ref< std::optional< object::SectionedAddress >(uint32_t)> LookupPooledAddress) const
Build a DWARFAddressRangesVector from a rangelist.
LLVM_ABI void dumpAddress(raw_ostream &OS, uint64_t Address) const
A class representing a position in a DataExtractor, as well as any error encountered during extractio...
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
An efficient, type-erasing, non-owning reference to a callable.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
LLVM_ABI StringRef RangeListEncodingString(unsigned Encoding)
Definition Dwarf.cpp:742
LLVM_ABI StringRef RLEString(unsigned RLE)
Definition Dwarf.cpp:1037
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
uint64_t computeTombstoneAddress(uint8_t AddressByteSize)
Definition Dwarf.h:1242
This is an optimization pass for GlobalISel generic memory operations.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1321
@ not_supported
Definition Errc.h:69
@ invalid_argument
Definition Errc.h:56
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
support::detail::PadAdapter< T > fmt_pad(T &&Item, size_t Left, size_t Right)
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:221
void consumeError(Error Err)
Consume a Error without doing anything.
Definition Error.h:1106
std::vector< DWARFAddressRange > DWARFAddressRangesVector
DWARFAddressRangesVector - represents a set of absolute address ranges.
Container for dump options that control which debug information will be dumped.
Definition DIContext.h:196
LLVM_ABI void dump(raw_ostream &OS, uint32_t AddressSize, DIDumpOptions DumpOpts={}, const DWARFObject *Obj=nullptr) const
uint64_t SectionIndex
The index of the section this entry belongs to.
uint8_t EntryKind
The DWARF encoding (DW_RLE_* or DW_LLE_*).
uint64_t Offset
The offset at which the entry is located in the section.
A class representing a single range list entry.
LLVM_ABI Error extract(DWARFDataExtractor Data, uint64_t *OffsetPtr)
LLVM_ABI void dump(raw_ostream &OS, uint8_t AddrSize, uint8_t MaxEncodingStringLength, uint64_t &CurrentBase, DIDumpOptions DumpOpts, llvm::function_ref< std::optional< object::SectionedAddress >(uint32_t)> LookupPooledAddress) const
uint64_t Value0
The values making up the range list entry.