LLVM 19.0.0git
DWARFDebugLoc.cpp
Go to the documentation of this file.
1//===- DWARFDebugLoc.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
10#include "llvm/ADT/StringRef.h"
18#include "llvm/Support/Format.h"
20#include <algorithm>
21#include <cinttypes>
22#include <cstdint>
23
24using namespace llvm;
26
27namespace llvm {
28class DWARFObject;
29}
30
31namespace {
32class DWARFLocationInterpreter {
33 std::optional<object::SectionedAddress> Base;
34 std::function<std::optional<object::SectionedAddress>(uint32_t)> LookupAddr;
35
36public:
37 DWARFLocationInterpreter(
38 std::optional<object::SectionedAddress> Base,
39 std::function<std::optional<object::SectionedAddress>(uint32_t)>
40 LookupAddr)
41 : Base(Base), LookupAddr(std::move(LookupAddr)) {}
42
44 Interpret(const DWARFLocationEntry &E);
45};
46} // namespace
47
48static Error createResolverError(uint32_t Index, unsigned Kind) {
49 return make_error<ResolverError>(Index, (dwarf::LoclistEntries)Kind);
50}
51
53DWARFLocationInterpreter::Interpret(const DWARFLocationEntry &E) {
54 switch (E.Kind) {
55 case dwarf::DW_LLE_end_of_list:
56 return std::nullopt;
57 case dwarf::DW_LLE_base_addressx: {
58 Base = LookupAddr(E.Value0);
59 if (!Base)
60 return createResolverError(E.Value0, E.Kind);
61 return std::nullopt;
62 }
63 case dwarf::DW_LLE_startx_endx: {
64 std::optional<SectionedAddress> LowPC = LookupAddr(E.Value0);
65 if (!LowPC)
66 return createResolverError(E.Value0, E.Kind);
67 std::optional<SectionedAddress> HighPC = LookupAddr(E.Value1);
68 if (!HighPC)
69 return createResolverError(E.Value1, E.Kind);
71 DWARFAddressRange{LowPC->Address, HighPC->Address, LowPC->SectionIndex},
72 E.Loc};
73 }
74 case dwarf::DW_LLE_startx_length: {
75 std::optional<SectionedAddress> LowPC = LookupAddr(E.Value0);
76 if (!LowPC)
77 return createResolverError(E.Value0, E.Kind);
78 return DWARFLocationExpression{DWARFAddressRange{LowPC->Address,
79 LowPC->Address + E.Value1,
80 LowPC->SectionIndex},
81 E.Loc};
82 }
83 case dwarf::DW_LLE_offset_pair: {
84 if (!Base) {
86 "Unable to resolve location list offset pair: "
87 "Base address not defined");
88 }
89 DWARFAddressRange Range{Base->Address + E.Value0, Base->Address + E.Value1,
90 Base->SectionIndex};
91 if (Range.SectionIndex == SectionedAddress::UndefSection)
92 Range.SectionIndex = E.SectionIndex;
93 return DWARFLocationExpression{Range, E.Loc};
94 }
95 case dwarf::DW_LLE_default_location:
96 return DWARFLocationExpression{std::nullopt, E.Loc};
97 case dwarf::DW_LLE_base_address:
98 Base = SectionedAddress{E.Value0, E.SectionIndex};
99 return std::nullopt;
100 case dwarf::DW_LLE_start_end:
102 DWARFAddressRange{E.Value0, E.Value1, E.SectionIndex}, E.Loc};
103 case dwarf::DW_LLE_start_length:
105 DWARFAddressRange{E.Value0, E.Value0 + E.Value1, E.SectionIndex},
106 E.Loc};
107 default:
108 llvm_unreachable("unreachable locations list kind");
109 }
110}
111
113 ArrayRef<uint8_t> Data, bool IsLittleEndian,
114 unsigned AddressSize, DWARFUnit *U) {
115 DWARFDataExtractor Extractor(Data, IsLittleEndian, AddressSize);
116 // Note. We do not pass any format to DWARFExpression, even if the
117 // corresponding unit is known. For now, there is only one operation,
118 // DW_OP_call_ref, which depends on the format; it is rarely used, and
119 // is unexpected in location tables.
120 DWARFExpression(Extractor, AddressSize).print(OS, DumpOpts, U);
121}
122
124 uint64_t *Offset, raw_ostream &OS, std::optional<SectionedAddress> BaseAddr,
125 const DWARFObject &Obj, DWARFUnit *U, DIDumpOptions DumpOpts,
126 unsigned Indent) const {
127 DWARFLocationInterpreter Interp(
128 BaseAddr, [U](uint32_t Index) -> std::optional<SectionedAddress> {
129 if (U)
130 return U->getAddrOffsetSectionItem(Index);
131 return std::nullopt;
132 });
133 OS << format("0x%8.8" PRIx64 ": ", *Offset);
135 Expected<std::optional<DWARFLocationExpression>> Loc = Interp.Interpret(E);
136 if (!Loc || DumpOpts.DisplayRawContents)
137 dumpRawEntry(E, OS, Indent, DumpOpts, Obj);
138 if (Loc && *Loc) {
139 OS << "\n";
140 OS.indent(Indent);
141 if (DumpOpts.DisplayRawContents)
142 OS << " => ";
143
144 DIDumpOptions RangeDumpOpts(DumpOpts);
145 RangeDumpOpts.DisplayRawContents = false;
146 if (Loc.get()->Range)
147 Loc.get()->Range->dump(OS, Data.getAddressSize(), RangeDumpOpts, &Obj);
148 else
149 OS << "<default>";
150 }
151 if (!Loc)
152 consumeError(Loc.takeError());
153
154 if (E.Kind != dwarf::DW_LLE_base_address &&
155 E.Kind != dwarf::DW_LLE_base_addressx &&
156 E.Kind != dwarf::DW_LLE_end_of_list) {
157 OS << ": ";
158 dumpExpression(OS, DumpOpts, E.Loc, Data.isLittleEndian(),
159 Data.getAddressSize(), U);
160 }
161 return true;
162 });
163 if (E) {
164 DumpOpts.RecoverableErrorHandler(std::move(E));
165 return false;
166 }
167 return true;
168}
169
171 uint64_t Offset, std::optional<SectionedAddress> BaseAddr,
172 std::function<std::optional<SectionedAddress>(uint32_t)> LookupAddr,
173 function_ref<bool(Expected<DWARFLocationExpression>)> Callback) const {
174 DWARFLocationInterpreter Interp(BaseAddr, std::move(LookupAddr));
175 return visitLocationList(&Offset, [&](const DWARFLocationEntry &E) {
176 Expected<std::optional<DWARFLocationExpression>> Loc = Interp.Interpret(E);
177 if (!Loc)
178 return Callback(Loc.takeError());
179 if (*Loc)
180 return Callback(**Loc);
181 return true;
182 });
183}
184
186 DIDumpOptions DumpOpts,
187 std::optional<uint64_t> DumpOffset) const {
188 auto BaseAddr = std::nullopt;
189 unsigned Indent = 12;
190 if (DumpOffset) {
191 dumpLocationList(&*DumpOffset, OS, BaseAddr, Obj, nullptr, DumpOpts,
192 Indent);
193 } else {
194 uint64_t Offset = 0;
195 StringRef Separator;
196 bool CanContinue = true;
197 while (CanContinue && Data.isValidOffset(Offset)) {
198 OS << Separator;
199 Separator = "\n";
200
201 CanContinue = dumpLocationList(&Offset, OS, BaseAddr, Obj, nullptr,
202 DumpOpts, Indent);
203 OS << '\n';
204 }
205 }
206}
207
210 function_ref<bool(const DWARFLocationEntry &)> Callback) const {
212 while (true) {
213 uint64_t SectionIndex;
215 uint64_t Value1 = Data.getRelocatedAddress(C, &SectionIndex);
216
218
219 // The end of any given location list is marked by an end of list entry,
220 // which consists of a 0 for the beginning address offset and a 0 for the
221 // ending address offset. A beginning offset of 0xff...f marks the base
222 // address selection entry.
223 if (Value0 == 0 && Value1 == 0) {
224 E.Kind = dwarf::DW_LLE_end_of_list;
225 } else if (Value0 == (Data.getAddressSize() == 4 ? -1U : -1ULL)) {
226 E.Kind = dwarf::DW_LLE_base_address;
227 E.Value0 = Value1;
228 E.SectionIndex = SectionIndex;
229 } else {
230 E.Kind = dwarf::DW_LLE_offset_pair;
231 E.Value0 = Value0;
232 E.Value1 = Value1;
233 E.SectionIndex = SectionIndex;
234 unsigned Bytes = Data.getU16(C);
235 // A single location description describing the location of the object...
236 Data.getU8(C, E.Loc, Bytes);
237 }
238
239 if (!C)
240 return C.takeError();
241 if (!Callback(E) || E.Kind == dwarf::DW_LLE_end_of_list)
242 break;
243 }
244 *Offset = C.tell();
245 return Error::success();
246}
247
249 raw_ostream &OS, unsigned Indent,
250 DIDumpOptions DumpOpts,
251 const DWARFObject &Obj) const {
252 uint64_t Value0, Value1;
253 switch (Entry.Kind) {
254 case dwarf::DW_LLE_base_address:
255 Value0 = Data.getAddressSize() == 4 ? -1U : -1ULL;
256 Value1 = Entry.Value0;
257 break;
258 case dwarf::DW_LLE_offset_pair:
259 Value0 = Entry.Value0;
260 Value1 = Entry.Value1;
261 break;
262 case dwarf::DW_LLE_end_of_list:
263 return;
264 default:
265 llvm_unreachable("Not possible in DWARF4!");
266 }
267 OS << '\n';
268 OS.indent(Indent);
269 OS << '(' << format_hex(Value0, 2 + Data.getAddressSize() * 2) << ", "
270 << format_hex(Value1, 2 + Data.getAddressSize() * 2) << ')';
271 DWARFFormValue::dumpAddressSection(Obj, OS, DumpOpts, Entry.SectionIndex);
272}
273
275 uint64_t *Offset, function_ref<bool(const DWARFLocationEntry &)> F) const {
276
278 bool Continue = true;
279 while (Continue) {
281 E.Kind = Data.getU8(C);
282 switch (E.Kind) {
283 case dwarf::DW_LLE_end_of_list:
284 break;
285 case dwarf::DW_LLE_base_addressx:
286 E.Value0 = Data.getULEB128(C);
287 break;
288 case dwarf::DW_LLE_startx_endx:
289 E.Value0 = Data.getULEB128(C);
290 E.Value1 = Data.getULEB128(C);
291 break;
292 case dwarf::DW_LLE_startx_length:
293 E.Value0 = Data.getULEB128(C);
294 // Pre-DWARF 5 has different interpretation of the length field. We have
295 // to support both pre- and standartized styles for the compatibility.
296 if (Version < 5)
297 E.Value1 = Data.getU32(C);
298 else
299 E.Value1 = Data.getULEB128(C);
300 break;
301 case dwarf::DW_LLE_offset_pair:
302 E.Value0 = Data.getULEB128(C);
303 E.Value1 = Data.getULEB128(C);
304 E.SectionIndex = SectionedAddress::UndefSection;
305 break;
306 case dwarf::DW_LLE_default_location:
307 break;
308 case dwarf::DW_LLE_base_address:
309 E.Value0 = Data.getRelocatedAddress(C, &E.SectionIndex);
310 break;
311 case dwarf::DW_LLE_start_end:
312 E.Value0 = Data.getRelocatedAddress(C, &E.SectionIndex);
313 E.Value1 = Data.getRelocatedAddress(C);
314 break;
315 case dwarf::DW_LLE_start_length:
316 E.Value0 = Data.getRelocatedAddress(C, &E.SectionIndex);
317 E.Value1 = Data.getULEB128(C);
318 break;
319 default:
320 cantFail(C.takeError());
322 "LLE of kind %x not supported", (int)E.Kind);
323 }
324
325 if (E.Kind != dwarf::DW_LLE_base_address &&
326 E.Kind != dwarf::DW_LLE_base_addressx &&
327 E.Kind != dwarf::DW_LLE_end_of_list) {
328 unsigned Bytes = Version >= 5 ? Data.getULEB128(C) : Data.getU16(C);
329 // A single location description describing the location of the object...
330 Data.getU8(C, E.Loc, Bytes);
331 }
332
333 if (!C)
334 return C.takeError();
335 Continue = F(E) && E.Kind != dwarf::DW_LLE_end_of_list;
336 }
337 *Offset = C.tell();
338 return Error::success();
339}
340
342 raw_ostream &OS, unsigned Indent,
343 DIDumpOptions DumpOpts,
344 const DWARFObject &Obj) const {
345 size_t MaxEncodingStringLength = 0;
346#define HANDLE_DW_LLE(ID, NAME) \
347 MaxEncodingStringLength = std::max(MaxEncodingStringLength, \
348 dwarf::LocListEncodingString(ID).size());
349#include "llvm/BinaryFormat/Dwarf.def"
350
351 OS << "\n";
352 OS.indent(Indent);
353 StringRef EncodingString = dwarf::LocListEncodingString(Entry.Kind);
354 // Unsupported encodings should have been reported during parsing.
355 assert(!EncodingString.empty() && "Unknown loclist entry encoding");
356 OS << format("%-*s(", MaxEncodingStringLength, EncodingString.data());
357 unsigned FieldSize = 2 + 2 * Data.getAddressSize();
358 switch (Entry.Kind) {
359 case dwarf::DW_LLE_end_of_list:
360 case dwarf::DW_LLE_default_location:
361 break;
362 case dwarf::DW_LLE_startx_endx:
363 case dwarf::DW_LLE_startx_length:
364 case dwarf::DW_LLE_offset_pair:
365 case dwarf::DW_LLE_start_end:
366 case dwarf::DW_LLE_start_length:
367 OS << format_hex(Entry.Value0, FieldSize) << ", "
368 << format_hex(Entry.Value1, FieldSize);
369 break;
370 case dwarf::DW_LLE_base_addressx:
371 case dwarf::DW_LLE_base_address:
372 OS << format_hex(Entry.Value0, FieldSize);
373 break;
374 }
375 OS << ')';
376 switch (Entry.Kind) {
377 case dwarf::DW_LLE_base_address:
378 case dwarf::DW_LLE_start_end:
379 case dwarf::DW_LLE_start_length:
380 DWARFFormValue::dumpAddressSection(Obj, OS, DumpOpts, Entry.SectionIndex);
381 break;
382 default:
383 break;
384 }
385}
386
388 raw_ostream &OS, const DWARFObject &Obj,
389 DIDumpOptions DumpOpts) {
390 if (!Data.isValidOffsetForDataOfSize(StartOffset, Size)) {
391 OS << "Invalid dump range\n";
392 return;
393 }
394 uint64_t Offset = StartOffset;
395 StringRef Separator;
396 bool CanContinue = true;
397 while (CanContinue && Offset < StartOffset + Size) {
398 OS << Separator;
399 Separator = "\n";
400
401 CanContinue = dumpLocationList(&Offset, OS, /*BaseAddr=*/std::nullopt, Obj,
402 nullptr, DumpOpts, /*Indent=*/12);
403 OS << '\n';
404 }
405}
406
408 OS << format("unable to resolve indirect address %u for: %s", Index,
409 dwarf::LocListEncodingString(Kind).data());
410}
411
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static void dumpExpression(raw_ostream &OS, DIDumpOptions DumpOpts, ArrayRef< uint8_t > Data, bool IsLittleEndian, unsigned AddressSize, DWARFUnit *U)
static Error createResolverError(uint32_t Index, unsigned Kind)
This file contains constants used for implementing Dwarf debug support.
uint64_t Size
#define F(x, y, z)
Definition: MD5.cpp:55
if(VerifyEach)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
A DataExtractor (typically for an in-memory copy of an object-file section) plus a relocation map for...
uint64_t getRelocatedAddress(uint64_t *Off, uint64_t *SecIx=nullptr) const
Extracts an address-sized value and applies a relocation to the result if one exists for the given of...
void dumpRawEntry(const DWARFLocationEntry &Entry, raw_ostream &OS, unsigned Indent, DIDumpOptions DumpOpts, const DWARFObject &Obj) const override
void dump(raw_ostream &OS, const DWARFObject &Obj, DIDumpOptions DumpOpts, std::optional< uint64_t > Offset) const
Print the location lists found within the debug_loc section.
Error visitLocationList(uint64_t *Offset, function_ref< bool(const DWARFLocationEntry &)> Callback) const override
Call the user-provided callback for each entry (including the end-of-list entry) in the location list...
void dumpRange(uint64_t StartOffset, uint64_t Size, raw_ostream &OS, const DWARFObject &Obj, DIDumpOptions DumpOpts)
Dump all location lists within the given range.
Error visitLocationList(uint64_t *Offset, function_ref< bool(const DWARFLocationEntry &)> Callback) const override
Call the user-provided callback for each entry (including the end-of-list entry) in the location list...
void dumpRawEntry(const DWARFLocationEntry &Entry, raw_ostream &OS, unsigned Indent, DIDumpOptions DumpOpts, const DWARFObject &Obj) const override
void print(raw_ostream &OS, DIDumpOptions DumpOpts, DWARFUnit *U, bool IsEH=false) const
static void dumpAddressSection(const DWARFObject &Obj, raw_ostream &OS, DIDumpOptions DumpOpts, uint64_t SectionIndex)
virtual void dumpRawEntry(const DWARFLocationEntry &Entry, raw_ostream &OS, unsigned Indent, DIDumpOptions DumpOpts, const DWARFObject &Obj) const =0
DWARFDataExtractor Data
Definition: DWARFDebugLoc.h:81
virtual Error visitLocationList(uint64_t *Offset, function_ref< bool(const DWARFLocationEntry &)> Callback) const =0
Call the user-provided callback for each entry (including the end-of-list entry) in the location list...
Error visitAbsoluteLocationList(uint64_t Offset, std::optional< object::SectionedAddress > BaseAddr, std::function< std::optional< object::SectionedAddress >(uint32_t)> LookupAddr, function_ref< bool(Expected< DWARFLocationExpression >)> Callback) const
bool dumpLocationList(uint64_t *Offset, raw_ostream &OS, std::optional< object::SectionedAddress > BaseAddr, const DWARFObject &Obj, DWARFUnit *U, DIDumpOptions DumpOpts, unsigned Indent) const
Dump the location list at the given Offset.
A class representing a position in a DataExtractor, as well as any error encountered during extractio...
Definition: DataExtractor.h:54
uint32_t getU32(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint32_t value from *offset_ptr.
uint8_t getU8(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint8_t value 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
uint16_t getU16(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint16_t value from *offset_ptr.
bool isValidOffset(uint64_t offset) const
Test the validity of offset.
bool isValidOffsetForDataOfSize(uint64_t offset, uint64_t length) const
Test the availability of length bytes of data from offset.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:334
Tagged union holding either a T or a Error.
Definition: Error.h:474
Error takeError()
Take ownership of the stored error.
Definition: Error.h:601
reference get()
Returns a reference to the stored T value.
Definition: Error.h:571
void log(raw_ostream &OS) const override
Print an error message to an output stream.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:131
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:52
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
StringRef LocListEncodingString(unsigned Encoding)
Definition: Dwarf.cpp:560
#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
LoclistEntries
DWARF v5 loc list entry encoding values.
Definition: Dwarf.h:502
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:456
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:90
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1244
@ illegal_byte_sequence
FormattedNumber format_hex(uint64_t N, unsigned Width, bool Upper=false)
format_hex - Output N as a fixed width hexadecimal.
Definition: Format.h:187
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:125
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:749
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1858
@ Continue
Definition: DWP.h:21
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1041
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:193
std::function< void(Error)> RecoverableErrorHandler
Definition: DIContext.h:230
A single location within a location list.
Definition: DWARFDebugLoc.h:30
Represents a single DWARF expression, whose value is location-dependent.
static const uint64_t UndefSection
Definition: ObjectFile.h:146