1 //===- DWARFDebugLoc.cpp --------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
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
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);
40 void DWARFDebugLoc::LocationList::dump(raw_ostream &OS, bool IsLittleEndian,
42 const MCRegisterInfo *MRI,
44 unsigned Indent) const {
45 for (const Entry &E : Entries) {
48 OS << format("[0x%*.*" PRIx64 ", ", AddressSize * 2, AddressSize * 2,
49 BaseAddress + E.Begin);
50 OS << format(" 0x%*.*" PRIx64 ")", AddressSize * 2, AddressSize * 2,
54 dumpExpression(OS, E.Loc, IsLittleEndian, AddressSize, MRI);
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)
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);
77 if (auto *L = getLocationListAtOffset(*Offset))
82 for (const LocationList &L : Locations) {
87 Optional<DWARFDebugLoc::LocationList>
88 DWARFDebugLoc::parseOneLocationList(DWARFDataExtractor Data, unsigned *Offset) {
92 // 2.6.2 Location Lists
93 // A location list entry consists of:
96 if (!Data.isValidOffsetForDataOfSize(*Offset, 2 * Data.getAddressSize())) {
97 WithColor::error() << "location list overflows the debug_loc section.\n";
101 // 1. A beginning address offset. ...
102 E.Begin = Data.getRelocatedAddress(Offset);
104 // 2. An ending address offset. ...
105 E.End = Data.getRelocatedAddress(Offset);
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)
113 if (!Data.isValidOffsetForDataOfSize(*Offset, 2)) {
114 WithColor::error() << "location list overflows the debug_loc section.\n";
118 unsigned Bytes = Data.getU16(Offset);
119 if (!Data.isValidOffsetForDataOfSize(*Offset, Bytes)) {
120 WithColor::error() << "location list overflows the debug_loc section.\n";
123 // A single location description describing the location of the object...
124 StringRef str = Data.getData().substr(*Offset, Bytes);
126 E.Loc.reserve(str.size());
127 llvm::copy(str, std::back_inserter(E.Loc));
128 LL.Entries.push_back(std::move(E));
132 void DWARFDebugLoc::parse(const DWARFDataExtractor &data) {
133 IsLittleEndian = data.isLittleEndian();
134 AddressSize = data.getAddressSize();
137 while (data.isValidOffset(Offset + data.getAddressSize() - 1)) {
138 if (auto LL = parseOneLocationList(data, &Offset))
139 Locations.push_back(std::move(*LL));
143 if (data.isValidOffset(Offset))
144 WithColor::error() << "failed to consume entire .debug_loc section\n";
147 Optional<DWARFDebugLoclists::LocationList>
148 DWARFDebugLoclists::parseOneLocationList(DataExtractor Data, unsigned *Offset,
153 // dwarf::DW_LLE_end_of_list_entry is 0 and indicates the end of the list.
155 static_cast<dwarf::LocationListEntry>(Data.getU8(Offset))) {
160 case dwarf::DW_LLE_startx_length:
161 E.Value0 = Data.getULEB128(Offset);
162 // Pre-DWARF 5 has different interpretation of the length field. We have
163 // to support both pre- and standartized styles for the compatibility.
165 E.Value1 = Data.getU32(Offset);
167 E.Value1 = Data.getULEB128(Offset);
169 case dwarf::DW_LLE_start_length:
170 E.Value0 = Data.getAddress(Offset);
171 E.Value1 = Data.getULEB128(Offset);
173 case dwarf::DW_LLE_offset_pair:
174 E.Value0 = Data.getULEB128(Offset);
175 E.Value1 = Data.getULEB128(Offset);
177 case dwarf::DW_LLE_base_address:
178 E.Value0 = Data.getAddress(Offset);
181 WithColor::error() << "dumping support for LLE of kind " << (int)Kind
182 << " not implemented\n";
186 if (Kind != dwarf::DW_LLE_base_address) {
188 Version >= 5 ? Data.getULEB128(Offset) : Data.getU16(Offset);
189 // A single location description describing the location of the object...
190 StringRef str = Data.getData().substr(*Offset, Bytes);
192 E.Loc.resize(str.size());
193 llvm::copy(str, E.Loc.begin());
196 LL.Entries.push_back(std::move(E));
201 void DWARFDebugLoclists::parse(DataExtractor data, unsigned Version) {
202 IsLittleEndian = data.isLittleEndian();
203 AddressSize = data.getAddressSize();
206 while (data.isValidOffset(Offset)) {
207 if (auto LL = parseOneLocationList(data, &Offset, Version))
208 Locations.push_back(std::move(*LL));
214 DWARFDebugLoclists::LocationList const *
215 DWARFDebugLoclists::getLocationListAtOffset(uint64_t Offset) const {
216 auto It = std::lower_bound(
217 Locations.begin(), Locations.end(), Offset,
218 [](const LocationList &L, uint64_t Offset) { return L.Offset < Offset; });
219 if (It != Locations.end() && It->Offset == Offset)
224 void DWARFDebugLoclists::LocationList::dump(raw_ostream &OS, uint64_t BaseAddr,
226 unsigned AddressSize,
227 const MCRegisterInfo *MRI,
228 unsigned Indent) const {
229 for (const Entry &E : Entries) {
231 case dwarf::DW_LLE_startx_length:
234 OS << "Addr idx " << E.Value0 << " (w/ length " << E.Value1 << "): ";
236 case dwarf::DW_LLE_start_length:
239 OS << format("[0x%*.*" PRIx64 ", 0x%*.*" PRIx64 "): ", AddressSize * 2,
240 AddressSize * 2, E.Value0, AddressSize * 2, AddressSize * 2,
241 E.Value0 + E.Value1);
243 case dwarf::DW_LLE_offset_pair:
246 OS << format("[0x%*.*" PRIx64 ", 0x%*.*" PRIx64 "): ", AddressSize * 2,
247 AddressSize * 2, BaseAddr + E.Value0, AddressSize * 2,
248 AddressSize * 2, BaseAddr + E.Value1);
250 case dwarf::DW_LLE_base_address:
254 llvm_unreachable("unreachable locations list kind");
257 dumpExpression(OS, E.Loc, IsLittleEndian, AddressSize, MRI);
261 void DWARFDebugLoclists::dump(raw_ostream &OS, uint64_t BaseAddr,
262 const MCRegisterInfo *MRI,
263 Optional<uint64_t> Offset) const {
264 auto DumpLocationList = [&](const LocationList &L) {
265 OS << format("0x%8.8x: ", L.Offset);
266 L.dump(OS, BaseAddr, IsLittleEndian, AddressSize, MRI, /*Indent=*/12);
271 if (auto *L = getLocationListAtOffset(*Offset))
272 DumpLocationList(*L);
276 for (const LocationList &L : Locations) {