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