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