1 //===- DWARFDebugLoc.cpp --------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h"
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/BinaryFormat/Dwarf.h"
13 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
14 #include "llvm/DebugInfo/DWARF/DWARFExpression.h"
15 #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
16 #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
17 #include "llvm/Support/Compiler.h"
18 #include "llvm/Support/Format.h"
19 #include "llvm/Support/WithColor.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include <algorithm>
22 #include <cinttypes>
23 #include <cstdint>
24 
25 using namespace llvm;
26 
27 // When directly dumping the .debug_loc without a compile unit, we have to guess
28 // at the DWARF version. This only affects DW_OP_call_ref, which is a rare
29 // expression that LLVM doesn't produce. Guessing the wrong version means we
30 // won't be able to pretty print expressions in DWARF2 binaries produced by
31 // non-LLVM tools.
32 static void dumpExpression(raw_ostream &OS, ArrayRef<char> Data,
33                            bool IsLittleEndian, unsigned AddressSize,
34                            const MCRegisterInfo *MRI) {
35   DWARFDataExtractor Extractor(StringRef(Data.data(), Data.size()),
36                                IsLittleEndian, AddressSize);
37   DWARFExpression(Extractor, dwarf::DWARF_VERSION, AddressSize).print(OS, MRI);
38 }
39 
40 void DWARFDebugLoc::LocationList::dump(raw_ostream &OS, bool IsLittleEndian,
41                                        unsigned AddressSize,
42                                        const MCRegisterInfo *MRI,
43                                        uint64_t BaseAddress,
44                                        unsigned Indent) const {
45   for (const Entry &E : Entries) {
46     OS << '\n';
47     OS.indent(Indent);
48     OS << format("[0x%*.*" PRIx64 ", ", AddressSize * 2, AddressSize * 2,
49                  BaseAddress + E.Begin);
50     OS << format(" 0x%*.*" PRIx64 ")", AddressSize * 2, AddressSize * 2,
51                  BaseAddress + E.End);
52     OS << ": ";
53 
54     dumpExpression(OS, E.Loc, IsLittleEndian, AddressSize, MRI);
55   }
56 }
57 
58 DWARFDebugLoc::LocationList const *
59 DWARFDebugLoc::getLocationListAtOffset(uint64_t Offset) const {
60   auto It = std::lower_bound(
61       Locations.begin(), Locations.end(), Offset,
62       [](const LocationList &L, uint64_t Offset) { return L.Offset < Offset; });
63   if (It != Locations.end() && It->Offset == Offset)
64     return &(*It);
65   return nullptr;
66 }
67 
68 void DWARFDebugLoc::dump(raw_ostream &OS, const MCRegisterInfo *MRI,
69                          Optional<uint64_t> Offset) const {
70   auto DumpLocationList = [&](const LocationList &L) {
71     OS << format("0x%8.8x: ", L.Offset);
72     L.dump(OS, IsLittleEndian, AddressSize, MRI, 0, 12);
73     OS << "\n\n";
74   };
75 
76   if (Offset) {
77     if (auto *L = getLocationListAtOffset(*Offset))
78       DumpLocationList(*L);
79     return;
80   }
81 
82   for (const LocationList &L : Locations) {
83     DumpLocationList(L);
84   }
85 }
86 
87 Optional<DWARFDebugLoc::LocationList>
88 DWARFDebugLoc::parseOneLocationList(DWARFDataExtractor Data, unsigned *Offset) {
89   LocationList LL;
90   LL.Offset = *Offset;
91 
92   // 2.6.2 Location Lists
93   // A location list entry consists of:
94   while (true) {
95     Entry E;
96     if (!Data.isValidOffsetForDataOfSize(*Offset, 2 * Data.getAddressSize())) {
97       WithColor::error() << "location list overflows the debug_loc section.\n";
98       return None;
99     }
100 
101     // 1. A beginning address offset. ...
102     E.Begin = Data.getRelocatedAddress(Offset);
103 
104     // 2. An ending address offset. ...
105     E.End = Data.getRelocatedAddress(Offset);
106 
107     // The end of any given location list is marked by an end of list entry,
108     // which consists of a 0 for the beginning address offset and a 0 for the
109     // ending address offset.
110     if (E.Begin == 0 && E.End == 0)
111       return LL;
112 
113     if (!Data.isValidOffsetForDataOfSize(*Offset, 2)) {
114       WithColor::error() << "location list overflows the debug_loc section.\n";
115       return None;
116     }
117 
118     unsigned Bytes = Data.getU16(Offset);
119     if (!Data.isValidOffsetForDataOfSize(*Offset, Bytes)) {
120       WithColor::error() << "location list overflows the debug_loc section.\n";
121       return None;
122     }
123     // A single location description describing the location of the object...
124     StringRef str = Data.getData().substr(*Offset, Bytes);
125     *Offset += Bytes;
126     E.Loc.reserve(str.size());
127     std::copy(str.begin(), str.end(), std::back_inserter(E.Loc));
128     LL.Entries.push_back(std::move(E));
129   }
130 }
131 
132 void DWARFDebugLoc::parse(const DWARFDataExtractor &data) {
133   IsLittleEndian = data.isLittleEndian();
134   AddressSize = data.getAddressSize();
135 
136   uint32_t Offset = 0;
137   while (data.isValidOffset(Offset + data.getAddressSize() - 1)) {
138     if (auto LL = parseOneLocationList(data, &Offset))
139       Locations.push_back(std::move(*LL));
140     else
141       break;
142   }
143   if (data.isValidOffset(Offset))
144     WithColor::error() << "failed to consume entire .debug_loc section\n";
145 }
146 
147 Optional<DWARFDebugLoclists::LocationList>
148 DWARFDebugLoclists::parseOneLocationList(DataExtractor Data, unsigned *Offset) {
149   LocationList LL;
150   LL.Offset = *Offset;
151 
152   // dwarf::DW_LLE_end_of_list_entry is 0 and indicates the end of the list.
153   while (auto Kind =
154              static_cast<dwarf::LocationListEntry>(Data.getU8(Offset))) {
155 
156     Entry E;
157     E.Kind = Kind;
158     switch (Kind) {
159     case dwarf::DW_LLE_startx_length:
160       E.Value0 = Data.getULEB128(Offset);
161       E.Value1 = Data.getU32(Offset);
162       break;
163     case dwarf::DW_LLE_start_length:
164       E.Value0 = Data.getAddress(Offset);
165       E.Value1 = Data.getULEB128(Offset);
166       break;
167     case dwarf::DW_LLE_offset_pair:
168       E.Value0 = Data.getULEB128(Offset);
169       E.Value1 = Data.getULEB128(Offset);
170       break;
171     case dwarf::DW_LLE_base_address:
172       E.Value0 = Data.getAddress(Offset);
173       break;
174     default:
175       WithColor::error() << "dumping support for LLE of kind " << (int)Kind
176                          << " not implemented\n";
177       return None;
178     }
179 
180     unsigned Bytes = Data.getU16(Offset);
181     // A single location description describing the location of the object...
182     StringRef str = Data.getData().substr(*Offset, Bytes);
183     *Offset += Bytes;
184     E.Loc.resize(str.size());
185     std::copy(str.begin(), str.end(), E.Loc.begin());
186 
187     LL.Entries.push_back(std::move(E));
188   }
189   return LL;
190 }
191 
192 void DWARFDebugLoclists::parse(DataExtractor data) {
193   IsLittleEndian = data.isLittleEndian();
194   AddressSize = data.getAddressSize();
195 
196   uint32_t Offset = 0;
197   while (data.isValidOffset(Offset)) {
198     if (auto LL = parseOneLocationList(data, &Offset))
199       Locations.push_back(std::move(*LL));
200     else
201       return;
202   }
203 }
204 
205 DWARFDebugLoclists::LocationList const *
206 DWARFDebugLoclists::getLocationListAtOffset(uint64_t Offset) const {
207   auto It = std::lower_bound(
208       Locations.begin(), Locations.end(), Offset,
209       [](const LocationList &L, uint64_t Offset) { return L.Offset < Offset; });
210   if (It != Locations.end() && It->Offset == Offset)
211     return &(*It);
212   return nullptr;
213 }
214 
215 void DWARFDebugLoclists::LocationList::dump(raw_ostream &OS, uint64_t BaseAddr,
216                                             bool IsLittleEndian,
217                                             unsigned AddressSize,
218                                             const MCRegisterInfo *MRI,
219                                             unsigned Indent) const {
220   for (const Entry &E : Entries) {
221     switch (E.Kind) {
222     case dwarf::DW_LLE_startx_length:
223       OS << '\n';
224       OS.indent(Indent);
225       OS << "Addr idx " << E.Value0 << " (w/ length " << E.Value1 << "): ";
226       break;
227     case dwarf::DW_LLE_start_length:
228       OS << '\n';
229       OS.indent(Indent);
230       OS << format("[0x%*.*" PRIx64 ", 0x%*.*x): ", AddressSize * 2,
231                    AddressSize * 2, E.Value0, AddressSize * 2, AddressSize * 2,
232                    E.Value0 + E.Value1);
233       break;
234     case dwarf::DW_LLE_offset_pair:
235       OS << '\n';
236       OS.indent(Indent);
237       OS << format("[0x%*.*" PRIx64 ", 0x%*.*x): ", AddressSize * 2,
238                    AddressSize * 2, BaseAddr + E.Value0, AddressSize * 2,
239                    AddressSize * 2, BaseAddr + E.Value1);
240       break;
241     case dwarf::DW_LLE_base_address:
242       BaseAddr = E.Value0;
243       break;
244     default:
245       llvm_unreachable("unreachable locations list kind");
246     }
247 
248     dumpExpression(OS, E.Loc, IsLittleEndian, AddressSize, MRI);
249   }
250 }
251 
252 void DWARFDebugLoclists::dump(raw_ostream &OS, uint64_t BaseAddr,
253                               const MCRegisterInfo *MRI,
254                               Optional<uint64_t> Offset) const {
255   auto DumpLocationList = [&](const LocationList &L) {
256     OS << format("0x%8.8x: ", L.Offset);
257     L.dump(OS, BaseAddr, IsLittleEndian, AddressSize, MRI, /*Indent=*/12);
258     OS << "\n\n";
259   };
260 
261   if (Offset) {
262     if (auto *L = getLocationListAtOffset(*Offset))
263       DumpLocationList(*L);
264     return;
265   }
266 
267   for (const LocationList &L : Locations) {
268     DumpLocationList(L);
269   }
270 }
271