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     return DWARFLocationExpression{DWARFAddressRange{Base->Address + E.Value0,
76                                                      Base->Address + E.Value1,
77                                                      Base->SectionIndex},
78                                    E.Loc};
79   case dwarf::DW_LLE_base_address:
80     Base = SectionedAddress{E.Value0, SectionedAddress::UndefSection};
81     return None;
82   case dwarf::DW_LLE_start_length:
83     return DWARFLocationExpression{
84         DWARFAddressRange{E.Value0, E.Value0 + E.Value1,
85                           SectionedAddress::UndefSection},
86         E.Loc};
87   default:
88     llvm_unreachable("unreachable locations list kind");
89   }
90 }
91 
92 // When directly dumping the .debug_loc without a compile unit, we have to guess
93 // at the DWARF version. This only affects DW_OP_call_ref, which is a rare
94 // expression that LLVM doesn't produce. Guessing the wrong version means we
95 // won't be able to pretty print expressions in DWARF2 binaries produced by
96 // non-LLVM tools.
97 static void dumpExpression(raw_ostream &OS, ArrayRef<uint8_t> Data,
98                            bool IsLittleEndian, unsigned AddressSize,
99                            const MCRegisterInfo *MRI, DWARFUnit *U) {
100   DWARFDataExtractor Extractor(toStringRef(Data), IsLittleEndian, AddressSize);
101   DWARFExpression(Extractor, dwarf::DWARF_VERSION, AddressSize).print(OS, MRI, U);
102 }
103 
104 bool DWARFLocationTable::dumpLocationList(uint64_t *Offset, raw_ostream &OS,
105                                           Optional<SectionedAddress> BaseAddr,
106                                           const MCRegisterInfo *MRI,
107                                           DWARFUnit *U, DIDumpOptions DumpOpts,
108                                           unsigned Indent) const {
109   DWARFLocationInterpreter Interp(
110       BaseAddr, [U](uint32_t Index) -> Optional<SectionedAddress> {
111         if (U)
112           return U->getAddrOffsetSectionItem(Index);
113         return None;
114       });
115   OS << format("0x%8.8" PRIx64 ": ", *Offset);
116   Error E = visitLocationList(Offset, [&](const DWARFLocationEntry &E) {
117     Expected<Optional<DWARFLocationExpression>> Loc = Interp.Interpret(E);
118     if (!Loc || DumpOpts.Verbose)
119       dumpRawEntry(E, OS, Indent);
120     if (Loc && *Loc) {
121       OS << "\n";
122       OS.indent(Indent);
123       if (DumpOpts.Verbose)
124         OS << "          => ";
125       Loc.get()->Range->dump(OS, Data.getAddressSize(), DumpOpts);
126     }
127     if (!Loc)
128       consumeError(Loc.takeError());
129 
130     if (E.Kind != dwarf::DW_LLE_base_address &&
131         E.Kind != dwarf::DW_LLE_base_addressx &&
132         E.Kind != dwarf::DW_LLE_end_of_list) {
133       OS << ": ";
134       dumpExpression(OS, E.Loc, Data.isLittleEndian(), Data.getAddressSize(),
135                      MRI, U);
136     }
137     return true;
138   });
139   if (E) {
140     OS << "\n";
141     OS.indent(Indent);
142     OS << "error: " << toString(std::move(E));
143     return false;
144   }
145   return true;
146 }
147 
148 void DWARFDebugLoc::LocationList::dump(raw_ostream &OS, uint64_t BaseAddress,
149                                        bool IsLittleEndian,
150                                        unsigned AddressSize,
151                                        const MCRegisterInfo *MRI, DWARFUnit *U,
152                                        DIDumpOptions DumpOpts,
153                                        unsigned Indent) const {
154   for (const Entry &E : Entries) {
155     OS << '\n';
156     OS.indent(Indent);
157     OS << format("[0x%*.*" PRIx64 ", ", AddressSize * 2, AddressSize * 2,
158                  BaseAddress + E.Begin);
159     OS << format(" 0x%*.*" PRIx64 ")", AddressSize * 2, AddressSize * 2,
160                  BaseAddress + E.End);
161     OS << ": ";
162 
163     dumpExpression(OS, E.Loc, IsLittleEndian, AddressSize, MRI, U);
164   }
165 }
166 
167 DWARFDebugLoc::LocationList const *
168 DWARFDebugLoc::getLocationListAtOffset(uint64_t Offset) const {
169   auto It = partition_point(
170       Locations, [=](const LocationList &L) { return L.Offset < Offset; });
171   if (It != Locations.end() && It->Offset == Offset)
172     return &(*It);
173   return nullptr;
174 }
175 
176 void DWARFDebugLoc::dump(raw_ostream &OS, const MCRegisterInfo *MRI, DIDumpOptions DumpOpts,
177                          Optional<uint64_t> Offset) const {
178   auto DumpLocationList = [&](const LocationList &L) {
179     OS << format("0x%8.8" PRIx64 ": ", L.Offset);
180     L.dump(OS, 0, IsLittleEndian, AddressSize, MRI, nullptr, DumpOpts, 12);
181     OS << "\n";
182   };
183 
184   if (Offset) {
185     if (auto *L = getLocationListAtOffset(*Offset))
186       DumpLocationList(*L);
187     return;
188   }
189 
190   for (const LocationList &L : Locations) {
191     DumpLocationList(L);
192     if (&L != &Locations.back())
193       OS << '\n';
194   }
195 }
196 
197 Expected<DWARFDebugLoc::LocationList>
198 DWARFDebugLoc::parseOneLocationList(const DWARFDataExtractor &Data,
199                                     uint64_t *Offset) {
200   LocationList LL;
201   LL.Offset = *Offset;
202   AddressSize = Data.getAddressSize();
203   DataExtractor::Cursor C(*Offset);
204 
205   // 2.6.2 Location Lists
206   // A location list entry consists of:
207   while (true) {
208     Entry E;
209 
210     // 1. A beginning address offset. ...
211     E.Begin = Data.getRelocatedAddress(C);
212 
213     // 2. An ending address offset. ...
214     E.End = Data.getRelocatedAddress(C);
215 
216     if (Error Err = C.takeError())
217       return std::move(Err);
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.
222     if (E.Begin == 0 && E.End == 0) {
223       *Offset = C.tell();
224       return LL;
225     }
226 
227     if (E.Begin != (AddressSize == 4 ? -1U : -1ULL)) {
228       unsigned Bytes = Data.getU16(C);
229       // A single location description describing the location of the object...
230       Data.getU8(C, E.Loc, Bytes);
231     }
232 
233     LL.Entries.push_back(std::move(E));
234   }
235 }
236 
237 void DWARFDebugLoc::parse(const DWARFDataExtractor &data) {
238   IsLittleEndian = data.isLittleEndian();
239   AddressSize = data.getAddressSize();
240 
241   uint64_t Offset = 0;
242   while (Offset < data.getData().size()) {
243     if (auto LL = parseOneLocationList(data, &Offset))
244       Locations.push_back(std::move(*LL));
245     else {
246       logAllUnhandledErrors(LL.takeError(), WithColor::error());
247       break;
248     }
249   }
250 }
251 
252 Error DWARFDebugLoclists::visitLocationList(
253     uint64_t *Offset, function_ref<bool(const DWARFLocationEntry &)> F) const {
254 
255   DataExtractor::Cursor C(*Offset);
256   bool Continue = true;
257   while (Continue) {
258     DWARFLocationEntry E;
259     E.Kind = Data.getU8(C);
260     switch (E.Kind) {
261     case dwarf::DW_LLE_end_of_list:
262       break;
263     case dwarf::DW_LLE_base_addressx:
264       E.Value0 = Data.getULEB128(C);
265       break;
266     case dwarf::DW_LLE_startx_length:
267       E.Value0 = Data.getULEB128(C);
268       // Pre-DWARF 5 has different interpretation of the length field. We have
269       // to support both pre- and standartized styles for the compatibility.
270       if (Version < 5)
271         E.Value1 = Data.getU32(C);
272       else
273         E.Value1 = Data.getULEB128(C);
274       break;
275     case dwarf::DW_LLE_offset_pair:
276       E.Value0 = Data.getULEB128(C);
277       E.Value1 = Data.getULEB128(C);
278       break;
279     case dwarf::DW_LLE_base_address:
280       E.Value0 = Data.getRelocatedAddress(C);
281       break;
282     case dwarf::DW_LLE_start_length:
283       E.Value0 = Data.getRelocatedAddress(C);
284       E.Value1 = Data.getULEB128(C);
285       break;
286     case dwarf::DW_LLE_startx_endx:
287     case dwarf::DW_LLE_default_location:
288     case dwarf::DW_LLE_start_end:
289     default:
290       cantFail(C.takeError());
291       return createStringError(errc::illegal_byte_sequence,
292                                "LLE of kind %x not supported", (int)E.Kind);
293     }
294 
295     if (E.Kind != dwarf::DW_LLE_base_address &&
296         E.Kind != dwarf::DW_LLE_base_addressx &&
297         E.Kind != dwarf::DW_LLE_end_of_list) {
298       unsigned Bytes = Version >= 5 ? Data.getULEB128(C) : Data.getU16(C);
299       // A single location description describing the location of the object...
300       Data.getU8(C, E.Loc, Bytes);
301     }
302 
303     if (!C)
304       return C.takeError();
305     Continue = F(E) && E.Kind != dwarf::DW_LLE_end_of_list;
306   }
307   *Offset = C.tell();
308   return Error::success();
309 }
310 
311 void DWARFDebugLoclists::dumpRawEntry(const DWARFLocationEntry &Entry,
312                                       raw_ostream &OS, unsigned Indent) const {
313   size_t MaxEncodingStringLength = 0;
314 #define HANDLE_DW_LLE(ID, NAME)                                                \
315   MaxEncodingStringLength = std::max(MaxEncodingStringLength,                  \
316                                      dwarf::LocListEncodingString(ID).size());
317 #include "llvm/BinaryFormat/Dwarf.def"
318 
319   OS << "\n";
320   OS.indent(Indent);
321   StringRef EncodingString = dwarf::LocListEncodingString(Entry.Kind);
322   // Unsupported encodings should have been reported during parsing.
323   assert(!EncodingString.empty() && "Unknown loclist entry encoding");
324   OS << format("%-*s(", MaxEncodingStringLength, EncodingString.data());
325   unsigned FieldSize = 2 + 2 * Data.getAddressSize();
326   switch (Entry.Kind) {
327   case dwarf::DW_LLE_startx_length:
328   case dwarf::DW_LLE_start_length:
329   case dwarf::DW_LLE_offset_pair:
330     OS << format_hex(Entry.Value0, FieldSize) << ", "
331        << format_hex(Entry.Value1, FieldSize);
332     break;
333   case dwarf::DW_LLE_base_addressx:
334   case dwarf::DW_LLE_base_address:
335     OS << format_hex(Entry.Value0, FieldSize);
336     break;
337   case dwarf::DW_LLE_end_of_list:
338     break;
339   }
340   OS << ')';
341 }
342 
343 void DWARFDebugLoclists::dumpRange(uint64_t StartOffset, uint64_t Size,
344                                    raw_ostream &OS, const MCRegisterInfo *MRI,
345                                    DIDumpOptions DumpOpts) {
346   if (!Data.isValidOffsetForDataOfSize(StartOffset, Size))  {
347     OS << "Invalid dump range\n";
348     return;
349   }
350   uint64_t Offset = StartOffset;
351   StringRef Separator;
352   bool CanContinue = true;
353   while (CanContinue && Offset < StartOffset + Size) {
354     OS << Separator;
355     Separator = "\n";
356 
357     CanContinue = dumpLocationList(&Offset, OS, /*BaseAddr=*/None, MRI, nullptr,
358                                    DumpOpts, /*Indent=*/12);
359     OS << '\n';
360   }
361 }
362