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 
9 #include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h"
10 #include "llvm/ADT/StringRef.h"
11 #include "llvm/BinaryFormat/Dwarf.h"
12 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
13 #include "llvm/DebugInfo/DWARF/DWARFExpression.h"
14 #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
15 #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
16 #include "llvm/Support/Compiler.h"
17 #include "llvm/Support/Format.h"
18 #include "llvm/Support/WithColor.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include <algorithm>
21 #include <cinttypes>
22 #include <cstdint>
23 
24 using namespace llvm;
25 using object::SectionedAddress;
26 
27 namespace {
28 class DWARFLocationInterpreter {
29   Optional<object::SectionedAddress> Base;
30   std::function<Optional<object::SectionedAddress>(uint32_t)> LookupAddr;
31 
32 public:
33   DWARFLocationInterpreter(
34       Optional<object::SectionedAddress> Base,
35       std::function<Optional<object::SectionedAddress>(uint32_t)> LookupAddr)
36       : Base(Base), LookupAddr(std::move(LookupAddr)) {}
37 
38   Expected<Optional<DWARFLocationExpression>>
39   Interpret(const DWARFLocationEntry &E);
40 };
41 } // namespace
42 
43 static Error createResolverError(uint32_t Index, unsigned Kind) {
44   return createStringError(errc::invalid_argument,
45                            "Unable to resolve indirect address %u for: %s",
46                            Index, dwarf::LocListEncodingString(Kind).data());
47 }
48 
49 Expected<Optional<DWARFLocationExpression>>
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_length: {
61     Optional<SectionedAddress> LowPC = LookupAddr(E.Value0);
62     if (!LowPC)
63       return createResolverError(E.Value0, E.Kind);
64     return DWARFLocationExpression{DWARFAddressRange{LowPC->Address,
65                                                      LowPC->Address + E.Value1,
66                                                      LowPC->SectionIndex},
67                                    E.Loc};
68   }
69   case dwarf::DW_LLE_offset_pair: {
70     if (!Base) {
71       return createStringError(
72           inconvertibleErrorCode(),
73           "Unable to resolve DW_LLE_offset_pair: base address unknown");
74     }
75     DWARFAddressRange Range{Base->Address + E.Value0, Base->Address + E.Value1,
76                             Base->SectionIndex};
77     if (Range.SectionIndex == SectionedAddress::UndefSection)
78       Range.SectionIndex = E.SectionIndex;
79     return DWARFLocationExpression{Range, E.Loc};
80   }
81   case dwarf::DW_LLE_base_address:
82     Base = SectionedAddress{E.Value0, E.SectionIndex};
83     return None;
84   case dwarf::DW_LLE_start_length:
85     return DWARFLocationExpression{
86         DWARFAddressRange{E.Value0, E.Value0 + E.Value1, E.SectionIndex},
87         E.Loc};
88   default:
89     llvm_unreachable("unreachable locations list kind");
90   }
91 }
92 
93 // When directly dumping the .debug_loc without a compile unit, we have to guess
94 // at the DWARF version. This only affects DW_OP_call_ref, which is a rare
95 // expression that LLVM doesn't produce. Guessing the wrong version means we
96 // won't be able to pretty print expressions in DWARF2 binaries produced by
97 // non-LLVM tools.
98 static void dumpExpression(raw_ostream &OS, ArrayRef<uint8_t> Data,
99                            bool IsLittleEndian, unsigned AddressSize,
100                            const MCRegisterInfo *MRI, DWARFUnit *U) {
101   DWARFDataExtractor Extractor(toStringRef(Data), IsLittleEndian, AddressSize);
102   DWARFExpression(Extractor, dwarf::DWARF_VERSION, AddressSize).print(OS, MRI, U);
103 }
104 
105 bool DWARFLocationTable::dumpLocationList(uint64_t *Offset, raw_ostream &OS,
106                                           Optional<SectionedAddress> BaseAddr,
107                                           const MCRegisterInfo *MRI,
108                                           const DWARFObject &Obj, DWARFUnit *U,
109                                           DIDumpOptions DumpOpts,
110                                           unsigned Indent) const {
111   DWARFLocationInterpreter Interp(
112       BaseAddr, [U](uint32_t Index) -> Optional<SectionedAddress> {
113         if (U)
114           return U->getAddrOffsetSectionItem(Index);
115         return None;
116       });
117   OS << format("0x%8.8" PRIx64 ": ", *Offset);
118   Error E = visitLocationList(Offset, [&](const DWARFLocationEntry &E) {
119     Expected<Optional<DWARFLocationExpression>> Loc = Interp.Interpret(E);
120     if (!Loc || DumpOpts.DisplayRawContents)
121       dumpRawEntry(E, OS, Indent, DumpOpts, Obj);
122     if (Loc && *Loc) {
123       OS << "\n";
124       OS.indent(Indent);
125       if (DumpOpts.DisplayRawContents)
126         OS << "          => ";
127 
128       DIDumpOptions RangeDumpOpts(DumpOpts);
129       RangeDumpOpts.DisplayRawContents = false;
130       Loc.get()->Range->dump(OS, Data.getAddressSize(), RangeDumpOpts, &Obj);
131     }
132     if (!Loc)
133       consumeError(Loc.takeError());
134 
135     if (E.Kind != dwarf::DW_LLE_base_address &&
136         E.Kind != dwarf::DW_LLE_base_addressx &&
137         E.Kind != dwarf::DW_LLE_end_of_list) {
138       OS << ": ";
139       dumpExpression(OS, E.Loc, Data.isLittleEndian(), Data.getAddressSize(),
140                      MRI, U);
141     }
142     return true;
143   });
144   if (E) {
145     OS << "\n";
146     OS.indent(Indent);
147     OS << "error: " << toString(std::move(E));
148     return false;
149   }
150   return true;
151 }
152 
153 Error DWARFLocationTable::visitAbsoluteLocationList(
154     uint64_t Offset, Optional<SectionedAddress> BaseAddr,
155     std::function<Optional<SectionedAddress>(uint32_t)> LookupAddr,
156     function_ref<bool(Expected<DWARFLocationExpression>)> Callback) const {
157   DWARFLocationInterpreter Interp(BaseAddr, std::move(LookupAddr));
158   return visitLocationList(&Offset, [&](const DWARFLocationEntry &E) {
159     Expected<Optional<DWARFLocationExpression>> Loc = Interp.Interpret(E);
160     if (!Loc)
161       return Callback(Loc.takeError());
162     if (*Loc)
163       return Callback(**Loc);
164     return true;
165   });
166 }
167 
168 void DWARFDebugLoc::dump(raw_ostream &OS, const MCRegisterInfo *MRI,
169                          const DWARFObject &Obj, DIDumpOptions DumpOpts,
170                          Optional<uint64_t> DumpOffset) const {
171   auto BaseAddr = None;
172   unsigned Indent = 12;
173   if (DumpOffset) {
174     dumpLocationList(&*DumpOffset, OS, BaseAddr, MRI, Obj, nullptr, DumpOpts,
175                      Indent);
176   } else {
177     uint64_t Offset = 0;
178     StringRef Separator;
179     bool CanContinue = true;
180     while (CanContinue && Data.isValidOffset(Offset)) {
181       OS << Separator;
182       Separator = "\n";
183 
184       CanContinue = dumpLocationList(&Offset, OS, BaseAddr, MRI, Obj, nullptr,
185                                      DumpOpts, Indent);
186       OS << '\n';
187     }
188   }
189 }
190 
191 Error DWARFDebugLoc::visitLocationList(
192     uint64_t *Offset,
193     function_ref<bool(const DWARFLocationEntry &)> Callback) const {
194   DataExtractor::Cursor C(*Offset);
195   while (true) {
196     uint64_t SectionIndex;
197     uint64_t Value0 = Data.getRelocatedAddress(C);
198     uint64_t Value1 = Data.getRelocatedAddress(C, &SectionIndex);
199 
200     DWARFLocationEntry E;
201 
202     // The end of any given location list is marked by an end of list entry,
203     // which consists of a 0 for the beginning address offset and a 0 for the
204     // ending address offset. A beginning offset of 0xff...f marks the base
205     // address selection entry.
206     if (Value0 == 0 && Value1 == 0) {
207       E.Kind = dwarf::DW_LLE_end_of_list;
208     } else if (Value0 == (Data.getAddressSize() == 4 ? -1U : -1ULL)) {
209       E.Kind = dwarf::DW_LLE_base_address;
210       E.Value0 = Value1;
211       E.SectionIndex = SectionIndex;
212     } else {
213       E.Kind = dwarf::DW_LLE_offset_pair;
214       E.Value0 = Value0;
215       E.Value1 = Value1;
216       E.SectionIndex = SectionIndex;
217       unsigned Bytes = Data.getU16(C);
218       // A single location description describing the location of the object...
219       Data.getU8(C, E.Loc, Bytes);
220     }
221 
222     if (!C)
223       return C.takeError();
224     if (!Callback(E) || E.Kind == dwarf::DW_LLE_end_of_list)
225       break;
226   }
227   *Offset = C.tell();
228   return Error::success();
229 }
230 
231 void DWARFDebugLoc::dumpRawEntry(const DWARFLocationEntry &Entry,
232                                  raw_ostream &OS, unsigned Indent,
233                                  DIDumpOptions DumpOpts,
234                                  const DWARFObject &Obj) const {
235   uint64_t Value0, Value1;
236   switch (Entry.Kind) {
237   case dwarf::DW_LLE_base_address:
238     Value0 = Data.getAddressSize() == 4 ? -1U : -1ULL;
239     Value1 = Entry.Value0;
240     break;
241   case dwarf::DW_LLE_offset_pair:
242     Value0 = Entry.Value0;
243     Value1 = Entry.Value1;
244     break;
245   case dwarf::DW_LLE_end_of_list:
246     Value0 = Value1 = 0;
247     return;
248   default:
249     llvm_unreachable("Not possible in DWARF4!");
250   }
251   OS << '\n';
252   OS.indent(Indent);
253   OS << '(' << format_hex(Value0, 2 + Data.getAddressSize() * 2) << ", "
254      << format_hex(Value1, 2 + Data.getAddressSize() * 2) << ')';
255   DWARFFormValue::dumpAddressSection(Obj, OS, DumpOpts, Entry.SectionIndex);
256 }
257 
258 Error DWARFDebugLoclists::visitLocationList(
259     uint64_t *Offset, function_ref<bool(const DWARFLocationEntry &)> F) const {
260 
261   DataExtractor::Cursor C(*Offset);
262   bool Continue = true;
263   while (Continue) {
264     DWARFLocationEntry E;
265     E.Kind = Data.getU8(C);
266     switch (E.Kind) {
267     case dwarf::DW_LLE_end_of_list:
268       break;
269     case dwarf::DW_LLE_base_addressx:
270       E.Value0 = Data.getULEB128(C);
271       break;
272     case dwarf::DW_LLE_startx_length:
273       E.Value0 = Data.getULEB128(C);
274       // Pre-DWARF 5 has different interpretation of the length field. We have
275       // to support both pre- and standartized styles for the compatibility.
276       if (Version < 5)
277         E.Value1 = Data.getU32(C);
278       else
279         E.Value1 = Data.getULEB128(C);
280       break;
281     case dwarf::DW_LLE_offset_pair:
282       E.Value0 = Data.getULEB128(C);
283       E.Value1 = Data.getULEB128(C);
284       E.SectionIndex = SectionedAddress::UndefSection;
285       break;
286     case dwarf::DW_LLE_base_address:
287       E.Value0 = Data.getRelocatedAddress(C, &E.SectionIndex);
288       break;
289     case dwarf::DW_LLE_start_length:
290       E.Value0 = Data.getRelocatedAddress(C, &E.SectionIndex);
291       E.Value1 = Data.getULEB128(C);
292       break;
293     case dwarf::DW_LLE_startx_endx:
294     case dwarf::DW_LLE_default_location:
295     case dwarf::DW_LLE_start_end:
296     default:
297       cantFail(C.takeError());
298       return createStringError(errc::illegal_byte_sequence,
299                                "LLE of kind %x not supported", (int)E.Kind);
300     }
301 
302     if (E.Kind != dwarf::DW_LLE_base_address &&
303         E.Kind != dwarf::DW_LLE_base_addressx &&
304         E.Kind != dwarf::DW_LLE_end_of_list) {
305       unsigned Bytes = Version >= 5 ? Data.getULEB128(C) : Data.getU16(C);
306       // A single location description describing the location of the object...
307       Data.getU8(C, E.Loc, Bytes);
308     }
309 
310     if (!C)
311       return C.takeError();
312     Continue = F(E) && E.Kind != dwarf::DW_LLE_end_of_list;
313   }
314   *Offset = C.tell();
315   return Error::success();
316 }
317 
318 void DWARFDebugLoclists::dumpRawEntry(const DWARFLocationEntry &Entry,
319                                       raw_ostream &OS, unsigned Indent,
320                                       DIDumpOptions DumpOpts,
321                                       const DWARFObject &Obj) const {
322   size_t MaxEncodingStringLength = 0;
323 #define HANDLE_DW_LLE(ID, NAME)                                                \
324   MaxEncodingStringLength = std::max(MaxEncodingStringLength,                  \
325                                      dwarf::LocListEncodingString(ID).size());
326 #include "llvm/BinaryFormat/Dwarf.def"
327 
328   OS << "\n";
329   OS.indent(Indent);
330   StringRef EncodingString = dwarf::LocListEncodingString(Entry.Kind);
331   // Unsupported encodings should have been reported during parsing.
332   assert(!EncodingString.empty() && "Unknown loclist entry encoding");
333   OS << format("%-*s(", MaxEncodingStringLength, EncodingString.data());
334   unsigned FieldSize = 2 + 2 * Data.getAddressSize();
335   switch (Entry.Kind) {
336   case dwarf::DW_LLE_startx_length:
337   case dwarf::DW_LLE_start_length:
338   case dwarf::DW_LLE_offset_pair:
339     OS << format_hex(Entry.Value0, FieldSize) << ", "
340        << format_hex(Entry.Value1, FieldSize);
341     break;
342   case dwarf::DW_LLE_base_addressx:
343   case dwarf::DW_LLE_base_address:
344     OS << format_hex(Entry.Value0, FieldSize);
345     break;
346   case dwarf::DW_LLE_end_of_list:
347     break;
348   }
349   OS << ')';
350   switch (Entry.Kind) {
351   case dwarf::DW_LLE_start_length:
352   case dwarf::DW_LLE_base_address:
353     DWARFFormValue::dumpAddressSection(Obj, OS, DumpOpts, Entry.SectionIndex);
354     break;
355   default:
356     break;
357   }
358 }
359 
360 void DWARFDebugLoclists::dumpRange(uint64_t StartOffset, uint64_t Size,
361                                    raw_ostream &OS, const MCRegisterInfo *MRI,
362                                    const DWARFObject &Obj,
363                                    DIDumpOptions DumpOpts) {
364   if (!Data.isValidOffsetForDataOfSize(StartOffset, Size))  {
365     OS << "Invalid dump range\n";
366     return;
367   }
368   uint64_t Offset = StartOffset;
369   StringRef Separator;
370   bool CanContinue = true;
371   while (CanContinue && Offset < StartOffset + Size) {
372     OS << Separator;
373     Separator = "\n";
374 
375     CanContinue = dumpLocationList(&Offset, OS, /*BaseAddr=*/None, MRI, Obj,
376                                    nullptr, DumpOpts, /*Indent=*/12);
377     OS << '\n';
378   }
379 }
380