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