LLVM  10.0.0
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"
16 #include "llvm/Support/Compiler.h"
17 #include "llvm/Support/Format.h"
18 #include "llvm/Support/WithColor.h"
20 #include <algorithm>
21 #include <cinttypes>
22 #include <cstdint>
23 
24 using namespace llvm;
26 
27 namespace {
28 class DWARFLocationInterpreter {
30  std::function<Optional<object::SectionedAddress>(uint32_t)> LookupAddr;
31 
32 public:
33  DWARFLocationInterpreter(
36  : Base(Base), LookupAddr(std::move(LookupAddr)) {}
37 
39  Interpret(const DWARFLocationEntry &E);
40 };
41 } // namespace
42 
45  "Unable to resolve indirect address %u for: %s",
46  Index, dwarf::LocListEncodingString(Kind).data());
47 }
48 
50 DWARFLocationInterpreter::Interpret(const DWARFLocationEntry &E) {
51  switch (E.Kind) {
52  case dwarf::DW_LLE_end_of_list:
53  return None;
54  case dwarf::DW_LLE_base_addressx: {
55  Base = LookupAddr(E.Value0);
56  if (!Base)
57  return createResolverError(E.Value0, E.Kind);
58  return None;
59  }
60  case dwarf::DW_LLE_startx_endx: {
61  Optional<SectionedAddress> LowPC = LookupAddr(E.Value0);
62  if (!LowPC)
63  return createResolverError(E.Value0, E.Kind);
64  Optional<SectionedAddress> HighPC = LookupAddr(E.Value1);
65  if (!HighPC)
66  return createResolverError(E.Value1, E.Kind);
68  DWARFAddressRange{LowPC->Address, HighPC->Address, LowPC->SectionIndex},
69  E.Loc};
70  }
71  case dwarf::DW_LLE_startx_length: {
72  Optional<SectionedAddress> LowPC = LookupAddr(E.Value0);
73  if (!LowPC)
74  return createResolverError(E.Value0, E.Kind);
76  LowPC->Address + E.Value1,
77  LowPC->SectionIndex},
78  E.Loc};
79  }
80  case dwarf::DW_LLE_offset_pair: {
81  if (!Base) {
83  "Unable to resolve location list offset pair: "
84  "Base address not defined");
85  }
86  DWARFAddressRange Range{Base->Address + E.Value0, Base->Address + E.Value1,
87  Base->SectionIndex};
88  if (Range.SectionIndex == SectionedAddress::UndefSection)
89  Range.SectionIndex = E.SectionIndex;
90  return DWARFLocationExpression{Range, E.Loc};
91  }
92  case dwarf::DW_LLE_default_location:
93  return DWARFLocationExpression{None, E.Loc};
94  case dwarf::DW_LLE_base_address:
96  return None;
97  case dwarf::DW_LLE_start_end:
100  case dwarf::DW_LLE_start_length:
103  E.Loc};
104  default:
105  llvm_unreachable("unreachable locations list kind");
106  }
107 }
108 
109 // When directly dumping the .debug_loc without a compile unit, we have to guess
110 // at the DWARF version. This only affects DW_OP_call_ref, which is a rare
111 // expression that LLVM doesn't produce. Guessing the wrong version means we
112 // won't be able to pretty print expressions in DWARF2 binaries produced by
113 // non-LLVM tools.
115  bool IsLittleEndian, unsigned AddressSize,
116  const MCRegisterInfo *MRI, DWARFUnit *U) {
117  DWARFDataExtractor Extractor(toStringRef(Data), IsLittleEndian, AddressSize);
118  DWARFExpression(Extractor, dwarf::DWARF_VERSION, AddressSize).print(OS, MRI, U);
119 }
120 
123  const MCRegisterInfo *MRI,
124  const DWARFObject &Obj, DWARFUnit *U,
125  DIDumpOptions DumpOpts,
126  unsigned Indent) const {
127  DWARFLocationInterpreter Interp(
128  BaseAddr, [U](uint32_t Index) -> Optional<SectionedAddress> {
129  if (U)
130  return U->getAddrOffsetSectionItem(Index);
131  return None;
132  });
133  OS << format("0x%8.8" PRIx64 ": ", *Offset);
134  Error E = visitLocationList(Offset, [&](const DWARFLocationEntry &E) {
135  Expected<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, E.Loc, Data.isLittleEndian(), Data.getAddressSize(),
159  MRI, U);
160  }
161  return true;
162  });
163  if (E) {
164  OS << "\n";
165  OS.indent(Indent);
166  OS << "error: " << toString(std::move(E));
167  return false;
168  }
169  return true;
170 }
171 
173  uint64_t Offset, Optional<SectionedAddress> BaseAddr,
175  function_ref<bool(Expected<DWARFLocationExpression>)> Callback) const {
176  DWARFLocationInterpreter Interp(BaseAddr, std::move(LookupAddr));
177  return visitLocationList(&Offset, [&](const DWARFLocationEntry &E) {
178  Expected<Optional<DWARFLocationExpression>> Loc = Interp.Interpret(E);
179  if (!Loc)
180  return Callback(Loc.takeError());
181  if (*Loc)
182  return Callback(**Loc);
183  return true;
184  });
185 }
186 
188  const DWARFObject &Obj, DIDumpOptions DumpOpts,
189  Optional<uint64_t> DumpOffset) const {
190  auto BaseAddr = None;
191  unsigned Indent = 12;
192  if (DumpOffset) {
193  dumpLocationList(&*DumpOffset, OS, BaseAddr, MRI, Obj, nullptr, DumpOpts,
194  Indent);
195  } else {
196  uint64_t Offset = 0;
197  StringRef Separator;
198  bool CanContinue = true;
199  while (CanContinue && Data.isValidOffset(Offset)) {
200  OS << Separator;
201  Separator = "\n";
202 
203  CanContinue = dumpLocationList(&Offset, OS, BaseAddr, MRI, Obj, nullptr,
204  DumpOpts, Indent);
205  OS << '\n';
206  }
207  }
208 }
209 
211  uint64_t *Offset,
212  function_ref<bool(const DWARFLocationEntry &)> Callback) const {
213  DataExtractor::Cursor C(*Offset);
214  while (true) {
215  uint64_t SectionIndex;
216  uint64_t Value0 = Data.getRelocatedAddress(C);
217  uint64_t Value1 = Data.getRelocatedAddress(C, &SectionIndex);
218 
220 
221  // The end of any given location list is marked by an end of list entry,
222  // which consists of a 0 for the beginning address offset and a 0 for the
223  // ending address offset. A beginning offset of 0xff...f marks the base
224  // address selection entry.
225  if (Value0 == 0 && Value1 == 0) {
226  E.Kind = dwarf::DW_LLE_end_of_list;
227  } else if (Value0 == (Data.getAddressSize() == 4 ? -1U : -1ULL)) {
228  E.Kind = dwarf::DW_LLE_base_address;
229  E.Value0 = Value1;
230  E.SectionIndex = SectionIndex;
231  } else {
232  E.Kind = dwarf::DW_LLE_offset_pair;
233  E.Value0 = Value0;
234  E.Value1 = Value1;
235  E.SectionIndex = SectionIndex;
236  unsigned Bytes = Data.getU16(C);
237  // A single location description describing the location of the object...
238  Data.getU8(C, E.Loc, Bytes);
239  }
240 
241  if (!C)
242  return C.takeError();
243  if (!Callback(E) || E.Kind == dwarf::DW_LLE_end_of_list)
244  break;
245  }
246  *Offset = C.tell();
247  return Error::success();
248 }
249 
251  raw_ostream &OS, unsigned Indent,
252  DIDumpOptions DumpOpts,
253  const DWARFObject &Obj) const {
254  uint64_t Value0, Value1;
255  switch (Entry.Kind) {
256  case dwarf::DW_LLE_base_address:
257  Value0 = Data.getAddressSize() == 4 ? -1U : -1ULL;
258  Value1 = Entry.Value0;
259  break;
260  case dwarf::DW_LLE_offset_pair:
261  Value0 = Entry.Value0;
262  Value1 = Entry.Value1;
263  break;
264  case dwarf::DW_LLE_end_of_list:
265  Value0 = Value1 = 0;
266  return;
267  default:
268  llvm_unreachable("Not possible in DWARF4!");
269  }
270  OS << '\n';
271  OS.indent(Indent);
272  OS << '(' << format_hex(Value0, 2 + Data.getAddressSize() * 2) << ", "
273  << format_hex(Value1, 2 + Data.getAddressSize() * 2) << ')';
274  DWARFFormValue::dumpAddressSection(Obj, OS, DumpOpts, Entry.SectionIndex);
275 }
276 
278  uint64_t *Offset, function_ref<bool(const DWARFLocationEntry &)> F) const {
279 
280  DataExtractor::Cursor C(*Offset);
281  bool Continue = true;
282  while (Continue) {
284  E.Kind = Data.getU8(C);
285  switch (E.Kind) {
286  case dwarf::DW_LLE_end_of_list:
287  break;
288  case dwarf::DW_LLE_base_addressx:
289  E.Value0 = Data.getULEB128(C);
290  break;
291  case dwarf::DW_LLE_startx_endx:
292  E.Value0 = Data.getULEB128(C);
293  E.Value1 = Data.getULEB128(C);
294  break;
295  case dwarf::DW_LLE_startx_length:
296  E.Value0 = Data.getULEB128(C);
297  // Pre-DWARF 5 has different interpretation of the length field. We have
298  // to support both pre- and standartized styles for the compatibility.
299  if (Version < 5)
300  E.Value1 = Data.getU32(C);
301  else
302  E.Value1 = Data.getULEB128(C);
303  break;
304  case dwarf::DW_LLE_offset_pair:
305  E.Value0 = Data.getULEB128(C);
306  E.Value1 = Data.getULEB128(C);
307  E.SectionIndex = SectionedAddress::UndefSection;
308  break;
309  case dwarf::DW_LLE_default_location:
310  break;
311  case dwarf::DW_LLE_base_address:
312  E.Value0 = Data.getRelocatedAddress(C, &E.SectionIndex);
313  break;
314  case dwarf::DW_LLE_start_end:
315  E.Value0 = Data.getRelocatedAddress(C, &E.SectionIndex);
316  E.Value1 = Data.getRelocatedAddress(C);
317  break;
318  case dwarf::DW_LLE_start_length:
319  E.Value0 = Data.getRelocatedAddress(C, &E.SectionIndex);
320  E.Value1 = Data.getULEB128(C);
321  break;
322  default:
323  cantFail(C.takeError());
325  "LLE of kind %x not supported", (int)E.Kind);
326  }
327 
328  if (E.Kind != dwarf::DW_LLE_base_address &&
329  E.Kind != dwarf::DW_LLE_base_addressx &&
330  E.Kind != dwarf::DW_LLE_end_of_list) {
331  unsigned Bytes = Version >= 5 ? Data.getULEB128(C) : Data.getU16(C);
332  // A single location description describing the location of the object...
333  Data.getU8(C, E.Loc, Bytes);
334  }
335 
336  if (!C)
337  return C.takeError();
338  Continue = F(E) && E.Kind != dwarf::DW_LLE_end_of_list;
339  }
340  *Offset = C.tell();
341  return Error::success();
342 }
343 
345  raw_ostream &OS, unsigned Indent,
346  DIDumpOptions DumpOpts,
347  const DWARFObject &Obj) const {
348  size_t MaxEncodingStringLength = 0;
349 #define HANDLE_DW_LLE(ID, NAME) \
350  MaxEncodingStringLength = std::max(MaxEncodingStringLength, \
351  dwarf::LocListEncodingString(ID).size());
352 #include "llvm/BinaryFormat/Dwarf.def"
353 
354  OS << "\n";
355  OS.indent(Indent);
356  StringRef EncodingString = dwarf::LocListEncodingString(Entry.Kind);
357  // Unsupported encodings should have been reported during parsing.
358  assert(!EncodingString.empty() && "Unknown loclist entry encoding");
359  OS << format("%-*s(", MaxEncodingStringLength, EncodingString.data());
360  unsigned FieldSize = 2 + 2 * Data.getAddressSize();
361  switch (Entry.Kind) {
362  case dwarf::DW_LLE_end_of_list:
363  case dwarf::DW_LLE_default_location:
364  break;
365  case dwarf::DW_LLE_startx_endx:
366  case dwarf::DW_LLE_startx_length:
367  case dwarf::DW_LLE_offset_pair:
368  case dwarf::DW_LLE_start_end:
369  case dwarf::DW_LLE_start_length:
370  OS << format_hex(Entry.Value0, FieldSize) << ", "
371  << format_hex(Entry.Value1, FieldSize);
372  break;
373  case dwarf::DW_LLE_base_addressx:
374  case dwarf::DW_LLE_base_address:
375  OS << format_hex(Entry.Value0, FieldSize);
376  break;
377  }
378  OS << ')';
379  switch (Entry.Kind) {
380  case dwarf::DW_LLE_base_address:
381  case dwarf::DW_LLE_start_end:
382  case dwarf::DW_LLE_start_length:
383  DWARFFormValue::dumpAddressSection(Obj, OS, DumpOpts, Entry.SectionIndex);
384  break;
385  default:
386  break;
387  }
388 }
389 
390 void DWARFDebugLoclists::dumpRange(uint64_t StartOffset, uint64_t Size,
391  raw_ostream &OS, const MCRegisterInfo *MRI,
392  const DWARFObject &Obj,
393  DIDumpOptions DumpOpts) {
394  if (!Data.isValidOffsetForDataOfSize(StartOffset, Size)) {
395  OS << "Invalid dump range\n";
396  return;
397  }
398  uint64_t Offset = StartOffset;
399  StringRef Separator;
400  bool CanContinue = true;
401  while (CanContinue && Offset < StartOffset + Size) {
402  OS << Separator;
403  Separator = "\n";
404 
405  CanContinue = dumpLocationList(&Offset, OS, /*BaseAddr=*/None, MRI, Obj,
406  nullptr, DumpOpts, /*Indent=*/12);
407  OS << '\n';
408  }
409 }
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:703
uint64_t CallInst * C
Error visitAbsoluteLocationList(uint64_t Offset, Optional< object::SectionedAddress > BaseAddr, std::function< Optional< object::SectionedAddress >(uint32_t)> LookupAddr, function_ref< bool(Expected< DWARFLocationExpression >)> Callback) const
This class represents lattice values for constants.
Definition: AllocatorList.h:23
bool dumpLocationList(uint64_t *Offset, raw_ostream &OS, Optional< object::SectionedAddress > BaseAddr, const MCRegisterInfo *MRI, const DWARFObject &Obj, DWARFUnit *U, DIDumpOptions DumpOpts, unsigned Indent) const
Dump the location list at the given Offset.
uint64_t tell() const
Return the current position of this Cursor.
Definition: DataExtractor.h:71
FormattedNumber format_hex(uint64_t N, unsigned Width, bool Upper=false)
format_hex - Output N as a fixed width hexadecimal.
Definition: Format.h:186
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
raw_ostream & indent(unsigned NumSpaces)
indent - Insert &#39;NumSpaces&#39; spaces.
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:99
void dumpRange(uint64_t StartOffset, uint64_t Size, raw_ostream &OS, const MCRegisterInfo *MRI, const DWARFObject &Obj, DIDumpOptions DumpOpts)
Dump all location lists within the given range.
F(f)
A single location within a location list.
Definition: DWARFDebugLoc.h:27
Error takeError()
Take ownership of the stored error.
Definition: Error.h:552
static Error createResolverError(uint32_t Index, unsigned Kind)
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...
uint8_t Kind
The entry kind (DW_LLE_***).
Definition: DWARFDebugLoc.h:29
Definition: BitVector.h:937
std::string toString(Error E)
Write all error messages (if any) in E to a string.
Definition: Error.h:986
uint64_t Value1
The second value of the location entry (if applicable).
Definition: DWARFDebugLoc.h:35
Error takeError()
Return error contained inside this Cursor, if any.
Definition: DataExtractor.h:75
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:147
uint64_t Value0
The first value of the location entry (if applicable).
Definition: DWARFDebugLoc.h:32
SmallVector< uint8_t, 4 > Loc
The location expression itself (if applicable).
Definition: DWARFDebugLoc.h:41
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:171
unsigned const MachineRegisterInfo * MRI
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
void dumpRawEntry(const DWARFLocationEntry &Entry, raw_ostream &OS, unsigned Indent, DIDumpOptions DumpOpts, const DWARFObject &Obj) const override
static void dumpAddressSection(const DWARFObject &Obj, raw_ostream &OS, DIDumpOptions DumpOpts, uint64_t SectionIndex)
A class representing a position in a DataExtractor, as well as any error encountered during extractio...
Definition: DataExtractor.h:54
StringRef toStringRef(bool B)
Construct a string ref from a boolean.
Definition: StringExtras.h:52
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1001
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void dumpRawEntry(const DWARFLocationEntry &Entry, raw_ostream &OS, unsigned Indent, DIDumpOptions DumpOpts, const DWARFObject &Obj) const override
void dump(raw_ostream &OS, const MCRegisterInfo *RegInfo, const DWARFObject &Obj, DIDumpOptions DumpOpts, Optional< uint64_t > Offset) const
Print the location lists found within the debug_loc section.
A DataExtractor (typically for an in-memory copy of an object-file section) plus a relocation map for...
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
Represents a single DWARF expression, whose value is location-dependent.
static void dumpExpression(raw_ostream &OS, ArrayRef< uint8_t > Data, bool IsLittleEndian, unsigned AddressSize, const MCRegisterInfo *MRI, DWARFUnit *U)
void print(raw_ostream &OS, const MCRegisterInfo *RegInfo, DWARFUnit *U, bool IsEH=false) const
reference get()
Returns a reference to the stored T value.
Definition: Error.h:532
This file contains constants used for implementing Dwarf debug support.
uint32_t Size
Definition: Profile.cpp:46
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:143
StringRef LocListEncodingString(unsigned Encoding)
Definition: Dwarf.cpp:491
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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...
uint64_t SectionIndex
The index of the section this entry is relative to (if applicable).
Definition: DWARFDebugLoc.h:38
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
print Print MemDeps of function
Optional< object::SectionedAddress > getAddrOffsetSectionItem(uint32_t Index) const
Definition: DWARFUnit.cpp:220
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
const uint64_t Version
Definition: InstrProf.h:980
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1197
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:77