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 26 // When directly dumping the .debug_loc without a compile unit, we have to guess 27 // at the DWARF version. This only affects DW_OP_call_ref, which is a rare 28 // expression that LLVM doesn't produce. Guessing the wrong version means we 29 // won't be able to pretty print expressions in DWARF2 binaries produced by 30 // non-LLVM tools. 31 static void dumpExpression(raw_ostream &OS, ArrayRef<uint8_t> Data, 32 bool IsLittleEndian, unsigned AddressSize, 33 const MCRegisterInfo *MRI, DWARFUnit *U) { 34 DWARFDataExtractor Extractor(toStringRef(Data), IsLittleEndian, AddressSize); 35 DWARFExpression(Extractor, dwarf::DWARF_VERSION, AddressSize).print(OS, MRI, U); 36 } 37 38 void DWARFDebugLoc::LocationList::dump(raw_ostream &OS, uint64_t BaseAddress, 39 bool IsLittleEndian, 40 unsigned AddressSize, 41 const MCRegisterInfo *MRI, DWARFUnit *U, 42 DIDumpOptions DumpOpts, 43 unsigned Indent) const { 44 for (const Entry &E : Entries) { 45 OS << '\n'; 46 OS.indent(Indent); 47 OS << format("[0x%*.*" PRIx64 ", ", AddressSize * 2, AddressSize * 2, 48 BaseAddress + E.Begin); 49 OS << format(" 0x%*.*" PRIx64 ")", AddressSize * 2, AddressSize * 2, 50 BaseAddress + E.End); 51 OS << ": "; 52 53 dumpExpression(OS, E.Loc, IsLittleEndian, AddressSize, MRI, U); 54 } 55 } 56 57 DWARFDebugLoc::LocationList const * 58 DWARFDebugLoc::getLocationListAtOffset(uint64_t Offset) const { 59 auto It = partition_point( 60 Locations, [=](const LocationList &L) { return L.Offset < Offset; }); 61 if (It != Locations.end() && It->Offset == Offset) 62 return &(*It); 63 return nullptr; 64 } 65 66 void DWARFDebugLoc::dump(raw_ostream &OS, const MCRegisterInfo *MRI, DIDumpOptions DumpOpts, 67 Optional<uint64_t> Offset) const { 68 auto DumpLocationList = [&](const LocationList &L) { 69 OS << format("0x%8.8" PRIx64 ": ", L.Offset); 70 L.dump(OS, 0, IsLittleEndian, AddressSize, MRI, nullptr, DumpOpts, 12); 71 OS << "\n"; 72 }; 73 74 if (Offset) { 75 if (auto *L = getLocationListAtOffset(*Offset)) 76 DumpLocationList(*L); 77 return; 78 } 79 80 for (const LocationList &L : Locations) { 81 DumpLocationList(L); 82 if (&L != &Locations.back()) 83 OS << '\n'; 84 } 85 } 86 87 Expected<DWARFDebugLoc::LocationList> 88 DWARFDebugLoc::parseOneLocationList(const DWARFDataExtractor &Data, 89 uint64_t *Offset) { 90 LocationList LL; 91 LL.Offset = *Offset; 92 AddressSize = Data.getAddressSize(); 93 DataExtractor::Cursor C(*Offset); 94 95 // 2.6.2 Location Lists 96 // A location list entry consists of: 97 while (true) { 98 Entry E; 99 100 // 1. A beginning address offset. ... 101 E.Begin = Data.getRelocatedAddress(C); 102 103 // 2. An ending address offset. ... 104 E.End = Data.getRelocatedAddress(C); 105 106 if (Error Err = C.takeError()) 107 return std::move(Err); 108 109 // The end of any given location list is marked by an end of list entry, 110 // which consists of a 0 for the beginning address offset and a 0 for the 111 // ending address offset. 112 if (E.Begin == 0 && E.End == 0) { 113 *Offset = C.tell(); 114 return LL; 115 } 116 117 if (E.Begin != (AddressSize == 4 ? -1U : -1ULL)) { 118 unsigned Bytes = Data.getU16(C); 119 // A single location description describing the location of the object... 120 Data.getU8(C, E.Loc, Bytes); 121 } 122 123 LL.Entries.push_back(std::move(E)); 124 } 125 } 126 127 void DWARFDebugLoc::parse(const DWARFDataExtractor &data) { 128 IsLittleEndian = data.isLittleEndian(); 129 AddressSize = data.getAddressSize(); 130 131 uint64_t Offset = 0; 132 while (Offset < data.getData().size()) { 133 if (auto LL = parseOneLocationList(data, &Offset)) 134 Locations.push_back(std::move(*LL)); 135 else { 136 logAllUnhandledErrors(LL.takeError(), WithColor::error()); 137 break; 138 } 139 } 140 } 141 142 Expected<DWARFDebugLoclists::LocationList> 143 DWARFDebugLoclists::parseOneLocationList(const DWARFDataExtractor &Data, 144 uint64_t *Offset, unsigned Version) { 145 LocationList LL; 146 LL.Offset = *Offset; 147 DataExtractor::Cursor C(*Offset); 148 149 // dwarf::DW_LLE_end_of_list_entry is 0 and indicates the end of the list. 150 while (auto Kind = Data.getU8(C)) { 151 Entry E; 152 E.Kind = Kind; 153 E.Offset = C.tell() - 1; 154 switch (Kind) { 155 case dwarf::DW_LLE_base_addressx: 156 E.Value0 = Data.getULEB128(C); 157 break; 158 case dwarf::DW_LLE_startx_length: 159 E.Value0 = Data.getULEB128(C); 160 // Pre-DWARF 5 has different interpretation of the length field. We have 161 // to support both pre- and standartized styles for the compatibility. 162 if (Version < 5) 163 E.Value1 = Data.getU32(C); 164 else 165 E.Value1 = Data.getULEB128(C); 166 break; 167 case dwarf::DW_LLE_start_length: 168 E.Value0 = Data.getRelocatedAddress(C); 169 E.Value1 = Data.getULEB128(C); 170 break; 171 case dwarf::DW_LLE_offset_pair: 172 E.Value0 = Data.getULEB128(C); 173 E.Value1 = Data.getULEB128(C); 174 break; 175 case dwarf::DW_LLE_base_address: 176 E.Value0 = Data.getRelocatedAddress(C); 177 break; 178 default: 179 cantFail(C.takeError()); 180 return createStringError(errc::illegal_byte_sequence, 181 "LLE of kind %x not supported", (int)Kind); 182 } 183 184 if (Kind != dwarf::DW_LLE_base_address && 185 Kind != dwarf::DW_LLE_base_addressx) { 186 unsigned Bytes = Version >= 5 ? Data.getULEB128(C) : Data.getU16(C); 187 // A single location description describing the location of the object... 188 Data.getU8(C, E.Loc, Bytes); 189 } 190 191 LL.Entries.push_back(std::move(E)); 192 } 193 if (Error Err = C.takeError()) 194 return std::move(Err); 195 Entry E; 196 E.Kind = dwarf::DW_LLE_end_of_list; 197 E.Offset = C.tell() - 1; 198 LL.Entries.push_back(E); 199 *Offset = C.tell(); 200 return LL; 201 } 202 203 void DWARFDebugLoclists::parse(const DWARFDataExtractor &data, uint64_t Offset, 204 uint64_t EndOffset, uint16_t Version) { 205 IsLittleEndian = data.isLittleEndian(); 206 AddressSize = data.getAddressSize(); 207 208 while (Offset < EndOffset) { 209 if (auto LL = parseOneLocationList(data, &Offset, Version)) 210 Locations.push_back(std::move(*LL)); 211 else { 212 logAllUnhandledErrors(LL.takeError(), WithColor::error()); 213 return; 214 } 215 } 216 } 217 218 DWARFDebugLoclists::LocationList const * 219 DWARFDebugLoclists::getLocationListAtOffset(uint64_t Offset) const { 220 auto It = partition_point( 221 Locations, [=](const LocationList &L) { return L.Offset < Offset; }); 222 if (It != Locations.end() && It->Offset == Offset) 223 return &(*It); 224 return nullptr; 225 } 226 227 void DWARFDebugLoclists::Entry::dump(raw_ostream &OS, uint64_t &BaseAddr, 228 bool IsLittleEndian, unsigned AddressSize, 229 const MCRegisterInfo *MRI, DWARFUnit *U, 230 DIDumpOptions DumpOpts, unsigned Indent, 231 size_t MaxEncodingStringLength) const { 232 if (DumpOpts.Verbose) { 233 OS << "\n"; 234 OS.indent(Indent); 235 auto EncodingString = dwarf::LocListEncodingString(Kind); 236 // Unsupported encodings should have been reported during parsing. 237 assert(!EncodingString.empty() && "Unknown loclist entry encoding"); 238 OS << format("%s%*c", EncodingString.data(), 239 MaxEncodingStringLength - EncodingString.size() + 1, '('); 240 switch (Kind) { 241 case dwarf::DW_LLE_startx_length: 242 case dwarf::DW_LLE_start_length: 243 case dwarf::DW_LLE_offset_pair: 244 OS << format("0x%*.*" PRIx64 ", 0x%*.*" PRIx64, AddressSize * 2, 245 AddressSize * 2, Value0, AddressSize * 2, AddressSize * 2, 246 Value1); 247 break; 248 case dwarf::DW_LLE_base_addressx: 249 case dwarf::DW_LLE_base_address: 250 OS << format("0x%*.*" PRIx64, AddressSize * 2, AddressSize * 2, 251 Value0); 252 break; 253 case dwarf::DW_LLE_end_of_list: 254 break; 255 } 256 OS << ')'; 257 } 258 auto PrintPrefix = [&] { 259 OS << "\n"; 260 OS.indent(Indent); 261 if (DumpOpts.Verbose) 262 OS << format("%*s", MaxEncodingStringLength, (const char *)"=> "); 263 }; 264 switch (Kind) { 265 case dwarf::DW_LLE_startx_length: 266 PrintPrefix(); 267 OS << "Addr idx " << Value0 << " (w/ length " << Value1 << "): "; 268 break; 269 case dwarf::DW_LLE_start_length: 270 PrintPrefix(); 271 DWARFAddressRange(Value0, Value0 + Value1) 272 .dump(OS, AddressSize, DumpOpts); 273 OS << ": "; 274 break; 275 case dwarf::DW_LLE_offset_pair: 276 PrintPrefix(); 277 DWARFAddressRange(BaseAddr + Value0, BaseAddr + Value1) 278 .dump(OS, AddressSize, DumpOpts); 279 OS << ": "; 280 break; 281 case dwarf::DW_LLE_base_addressx: 282 if (!DumpOpts.Verbose) 283 return; 284 break; 285 case dwarf::DW_LLE_end_of_list: 286 if (!DumpOpts.Verbose) 287 return; 288 break; 289 case dwarf::DW_LLE_base_address: 290 BaseAddr = Value0; 291 if (!DumpOpts.Verbose) 292 return; 293 break; 294 default: 295 llvm_unreachable("unreachable locations list kind"); 296 } 297 298 dumpExpression(OS, Loc, IsLittleEndian, AddressSize, MRI, U); 299 } 300 void DWARFDebugLoclists::LocationList::dump(raw_ostream &OS, uint64_t BaseAddr, 301 bool IsLittleEndian, 302 unsigned AddressSize, 303 const MCRegisterInfo *MRI, 304 DWARFUnit *U, 305 DIDumpOptions DumpOpts, 306 unsigned Indent) const { 307 size_t MaxEncodingStringLength = 0; 308 if (DumpOpts.Verbose) 309 for (const auto &Entry : Entries) 310 MaxEncodingStringLength = 311 std::max(MaxEncodingStringLength, 312 dwarf::LocListEncodingString(Entry.Kind).size()); 313 314 for (const Entry &E : Entries) 315 E.dump(OS, BaseAddr, IsLittleEndian, AddressSize, MRI, U, DumpOpts, Indent, 316 MaxEncodingStringLength); 317 } 318 319 void DWARFDebugLoclists::dump(raw_ostream &OS, uint64_t BaseAddr, 320 const MCRegisterInfo *MRI, DIDumpOptions DumpOpts, 321 Optional<uint64_t> Offset) const { 322 auto DumpLocationList = [&](const LocationList &L) { 323 OS << format("0x%8.8" PRIx64 ": ", L.Offset); 324 L.dump(OS, BaseAddr, IsLittleEndian, AddressSize, MRI, nullptr, DumpOpts, 325 /*Indent=*/12); 326 OS << "\n"; 327 }; 328 329 if (Offset) { 330 if (auto *L = getLocationListAtOffset(*Offset)) 331 DumpLocationList(*L); 332 return; 333 } 334 335 for (const LocationList &L : Locations) { 336 DumpLocationList(L); 337 if (&L != &Locations.back()) 338 OS << '\n'; 339 } 340 } 341