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