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.DisplayRawContents) 119 dumpRawEntry(E, OS, Indent); 120 if (Loc && *Loc) { 121 OS << "\n"; 122 OS.indent(Indent); 123 if (DumpOpts.DisplayRawContents) 124 OS << " => "; 125 126 DIDumpOptions RangeDumpOpts(DumpOpts); 127 RangeDumpOpts.DisplayRawContents = false; 128 const DWARFObject *Obj = nullptr; 129 if (U) 130 Obj = &U->getContext().getDWARFObj(); 131 Loc.get()->Range->dump(OS, Data.getAddressSize(), RangeDumpOpts, Obj); 132 } 133 if (!Loc) 134 consumeError(Loc.takeError()); 135 136 if (E.Kind != dwarf::DW_LLE_base_address && 137 E.Kind != dwarf::DW_LLE_base_addressx && 138 E.Kind != dwarf::DW_LLE_end_of_list) { 139 OS << ": "; 140 dumpExpression(OS, E.Loc, Data.isLittleEndian(), Data.getAddressSize(), 141 MRI, U); 142 } 143 return true; 144 }); 145 if (E) { 146 OS << "\n"; 147 OS.indent(Indent); 148 OS << "error: " << toString(std::move(E)); 149 return false; 150 } 151 return true; 152 } 153 154 Error DWARFLocationTable::visitAbsoluteLocationList( 155 uint64_t Offset, Optional<SectionedAddress> BaseAddr, 156 std::function<Optional<SectionedAddress>(uint32_t)> LookupAddr, 157 function_ref<bool(Expected<DWARFLocationExpression>)> Callback) const { 158 DWARFLocationInterpreter Interp(BaseAddr, std::move(LookupAddr)); 159 return visitLocationList(&Offset, [&](const DWARFLocationEntry &E) { 160 Expected<Optional<DWARFLocationExpression>> Loc = Interp.Interpret(E); 161 if (!Loc) 162 return Callback(Loc.takeError()); 163 if (*Loc) 164 return Callback(**Loc); 165 return true; 166 }); 167 } 168 169 DWARFDebugLoc::LocationList const * 170 DWARFDebugLoc::getLocationListAtOffset(uint64_t Offset) const { 171 auto It = partition_point( 172 Locations, [=](const LocationList &L) { return L.Offset < Offset; }); 173 if (It != Locations.end() && It->Offset == Offset) 174 return &(*It); 175 return nullptr; 176 } 177 178 void DWARFDebugLoc::dump(raw_ostream &OS, const MCRegisterInfo *MRI, 179 DIDumpOptions DumpOpts, 180 Optional<uint64_t> DumpOffset) const { 181 auto BaseAddr = None; 182 unsigned Indent = 12; 183 if (DumpOffset) { 184 dumpLocationList(&*DumpOffset, OS, BaseAddr, MRI, nullptr, DumpOpts, 185 Indent); 186 } else { 187 uint64_t Offset = 0; 188 StringRef Separator; 189 bool CanContinue = true; 190 while (CanContinue && Data.isValidOffset(Offset)) { 191 OS << Separator; 192 Separator = "\n"; 193 194 CanContinue = dumpLocationList(&Offset, OS, BaseAddr, MRI, nullptr, 195 DumpOpts, Indent); 196 OS << '\n'; 197 } 198 } 199 } 200 201 Error DWARFDebugLoc::visitLocationList( 202 uint64_t *Offset, 203 function_ref<bool(const DWARFLocationEntry &)> Callback) const { 204 DataExtractor::Cursor C(*Offset); 205 while (true) { 206 uint64_t Value0 = Data.getRelocatedAddress(C); 207 uint64_t Value1 = Data.getRelocatedAddress(C); 208 209 DWARFLocationEntry E; 210 211 // The end of any given location list is marked by an end of list entry, 212 // which consists of a 0 for the beginning address offset and a 0 for the 213 // ending address offset. A beginning offset of 0xff...f marks the base 214 // address selection entry. 215 if (Value0 == 0 && Value1 == 0) { 216 E.Kind = dwarf::DW_LLE_end_of_list; 217 } else if (Value0 == (Data.getAddressSize() == 4 ? -1U : -1ULL)) { 218 E.Kind = dwarf::DW_LLE_base_address; 219 E.Value0 = Value1; 220 } else { 221 E.Kind = dwarf::DW_LLE_offset_pair; 222 E.Value0 = Value0; 223 E.Value1 = Value1; 224 unsigned Bytes = Data.getU16(C); 225 // A single location description describing the location of the object... 226 Data.getU8(C, E.Loc, Bytes); 227 } 228 229 if (!C) 230 return C.takeError(); 231 if (!Callback(E) || E.Kind == dwarf::DW_LLE_end_of_list) 232 break; 233 } 234 *Offset = C.tell(); 235 return Error::success(); 236 } 237 238 Expected<DWARFDebugLoc::LocationList> 239 DWARFDebugLoc::parseOneLocationList(uint64_t *Offset) { 240 LocationList LL; 241 LL.Offset = *Offset; 242 243 Error E = visitLocationList(Offset, [&](const DWARFLocationEntry &E) { 244 LL.Entries.push_back(E); 245 return true; 246 }); 247 if (E) 248 return std::move(E); 249 return std::move(LL); 250 } 251 252 void DWARFDebugLoc::parse() { 253 uint64_t Offset = 0; 254 while (Offset < Data.getData().size()) { 255 if (auto LL = parseOneLocationList(&Offset)) 256 Locations.push_back(std::move(*LL)); 257 else { 258 logAllUnhandledErrors(LL.takeError(), WithColor::error()); 259 break; 260 } 261 } 262 } 263 264 void DWARFDebugLoc::dumpRawEntry(const DWARFLocationEntry &Entry, 265 raw_ostream &OS, unsigned Indent) const { 266 uint64_t Value0, Value1; 267 switch (Entry.Kind) { 268 case dwarf::DW_LLE_base_address: 269 Value0 = Data.getAddressSize() == 4 ? -1U : -1ULL; 270 Value1 = Entry.Value0; 271 break; 272 case dwarf::DW_LLE_offset_pair: 273 Value0 = Entry.Value0; 274 Value1 = Entry.Value1; 275 break; 276 case dwarf::DW_LLE_end_of_list: 277 Value0 = Value1 = 0; 278 return; 279 default: 280 llvm_unreachable("Not possible in DWARF4!"); 281 } 282 OS << '\n'; 283 OS.indent(Indent); 284 OS << '(' << format_hex(Value0, 2 + Data.getAddressSize() * 2) << ", " 285 << format_hex(Value1, 2 + Data.getAddressSize() * 2) << ')'; 286 } 287 288 Error DWARFDebugLoclists::visitLocationList( 289 uint64_t *Offset, function_ref<bool(const DWARFLocationEntry &)> F) const { 290 291 DataExtractor::Cursor C(*Offset); 292 bool Continue = true; 293 while (Continue) { 294 DWARFLocationEntry E; 295 E.Kind = Data.getU8(C); 296 switch (E.Kind) { 297 case dwarf::DW_LLE_end_of_list: 298 break; 299 case dwarf::DW_LLE_base_addressx: 300 E.Value0 = Data.getULEB128(C); 301 break; 302 case dwarf::DW_LLE_startx_length: 303 E.Value0 = Data.getULEB128(C); 304 // Pre-DWARF 5 has different interpretation of the length field. We have 305 // to support both pre- and standartized styles for the compatibility. 306 if (Version < 5) 307 E.Value1 = Data.getU32(C); 308 else 309 E.Value1 = Data.getULEB128(C); 310 break; 311 case dwarf::DW_LLE_offset_pair: 312 E.Value0 = Data.getULEB128(C); 313 E.Value1 = Data.getULEB128(C); 314 break; 315 case dwarf::DW_LLE_base_address: 316 E.Value0 = Data.getRelocatedAddress(C); 317 break; 318 case dwarf::DW_LLE_start_length: 319 E.Value0 = Data.getRelocatedAddress(C); 320 E.Value1 = Data.getULEB128(C); 321 break; 322 case dwarf::DW_LLE_startx_endx: 323 case dwarf::DW_LLE_default_location: 324 case dwarf::DW_LLE_start_end: 325 default: 326 cantFail(C.takeError()); 327 return createStringError(errc::illegal_byte_sequence, 328 "LLE of kind %x not supported", (int)E.Kind); 329 } 330 331 if (E.Kind != dwarf::DW_LLE_base_address && 332 E.Kind != dwarf::DW_LLE_base_addressx && 333 E.Kind != dwarf::DW_LLE_end_of_list) { 334 unsigned Bytes = Version >= 5 ? Data.getULEB128(C) : Data.getU16(C); 335 // A single location description describing the location of the object... 336 Data.getU8(C, E.Loc, Bytes); 337 } 338 339 if (!C) 340 return C.takeError(); 341 Continue = F(E) && E.Kind != dwarf::DW_LLE_end_of_list; 342 } 343 *Offset = C.tell(); 344 return Error::success(); 345 } 346 347 void DWARFDebugLoclists::dumpRawEntry(const DWARFLocationEntry &Entry, 348 raw_ostream &OS, unsigned Indent) const { 349 size_t MaxEncodingStringLength = 0; 350 #define HANDLE_DW_LLE(ID, NAME) \ 351 MaxEncodingStringLength = std::max(MaxEncodingStringLength, \ 352 dwarf::LocListEncodingString(ID).size()); 353 #include "llvm/BinaryFormat/Dwarf.def" 354 355 OS << "\n"; 356 OS.indent(Indent); 357 StringRef EncodingString = dwarf::LocListEncodingString(Entry.Kind); 358 // Unsupported encodings should have been reported during parsing. 359 assert(!EncodingString.empty() && "Unknown loclist entry encoding"); 360 OS << format("%-*s(", MaxEncodingStringLength, EncodingString.data()); 361 unsigned FieldSize = 2 + 2 * Data.getAddressSize(); 362 switch (Entry.Kind) { 363 case dwarf::DW_LLE_startx_length: 364 case dwarf::DW_LLE_start_length: 365 case dwarf::DW_LLE_offset_pair: 366 OS << format_hex(Entry.Value0, FieldSize) << ", " 367 << format_hex(Entry.Value1, FieldSize); 368 break; 369 case dwarf::DW_LLE_base_addressx: 370 case dwarf::DW_LLE_base_address: 371 OS << format_hex(Entry.Value0, FieldSize); 372 break; 373 case dwarf::DW_LLE_end_of_list: 374 break; 375 } 376 OS << ')'; 377 } 378 379 void DWARFDebugLoclists::dumpRange(uint64_t StartOffset, uint64_t Size, 380 raw_ostream &OS, const MCRegisterInfo *MRI, 381 DIDumpOptions DumpOpts) { 382 if (!Data.isValidOffsetForDataOfSize(StartOffset, Size)) { 383 OS << "Invalid dump range\n"; 384 return; 385 } 386 uint64_t Offset = StartOffset; 387 StringRef Separator; 388 bool CanContinue = true; 389 while (CanContinue && Offset < StartOffset + Size) { 390 OS << Separator; 391 Separator = "\n"; 392 393 CanContinue = dumpLocationList(&Offset, OS, /*BaseAddr=*/None, MRI, nullptr, 394 DumpOpts, /*Indent=*/12); 395 OS << '\n'; 396 } 397 } 398