1 //===- DWARFContext.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/DWARFContext.h" 10 #include "llvm/ADT/STLExtras.h" 11 #include "llvm/ADT/SmallString.h" 12 #include "llvm/ADT/SmallVector.h" 13 #include "llvm/ADT/StringRef.h" 14 #include "llvm/ADT/StringSwitch.h" 15 #include "llvm/BinaryFormat/Dwarf.h" 16 #include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h" 17 #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h" 18 #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h" 19 #include "llvm/DebugInfo/DWARF/DWARFDebugAddr.h" 20 #include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h" 21 #include "llvm/DebugInfo/DWARF/DWARFDebugAranges.h" 22 #include "llvm/DebugInfo/DWARF/DWARFDebugFrame.h" 23 #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h" 24 #include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h" 25 #include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h" 26 #include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h" 27 #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h" 28 #include "llvm/DebugInfo/DWARF/DWARFDebugRnglists.h" 29 #include "llvm/DebugInfo/DWARF/DWARFDie.h" 30 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" 31 #include "llvm/DebugInfo/DWARF/DWARFGdbIndex.h" 32 #include "llvm/DebugInfo/DWARF/DWARFSection.h" 33 #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h" 34 #include "llvm/DebugInfo/DWARF/DWARFVerifier.h" 35 #include "llvm/MC/MCRegisterInfo.h" 36 #include "llvm/Object/Decompressor.h" 37 #include "llvm/Object/MachO.h" 38 #include "llvm/Object/ObjectFile.h" 39 #include "llvm/Object/RelocationResolver.h" 40 #include "llvm/Support/Casting.h" 41 #include "llvm/Support/DataExtractor.h" 42 #include "llvm/Support/Error.h" 43 #include "llvm/Support/Format.h" 44 #include "llvm/Support/LEB128.h" 45 #include "llvm/Support/MemoryBuffer.h" 46 #include "llvm/Support/Path.h" 47 #include "llvm/Support/TargetRegistry.h" 48 #include "llvm/Support/raw_ostream.h" 49 #include <algorithm> 50 #include <cstdint> 51 #include <deque> 52 #include <map> 53 #include <string> 54 #include <utility> 55 #include <vector> 56 57 using namespace llvm; 58 using namespace dwarf; 59 using namespace object; 60 61 #define DEBUG_TYPE "dwarf" 62 63 using DWARFLineTable = DWARFDebugLine::LineTable; 64 using FileLineInfoKind = DILineInfoSpecifier::FileLineInfoKind; 65 using FunctionNameKind = DILineInfoSpecifier::FunctionNameKind; 66 67 DWARFContext::DWARFContext(std::unique_ptr<const DWARFObject> DObj, 68 std::string DWPName, 69 std::function<void(Error)> RecoverableErrorHandler, 70 std::function<void(Error)> WarningHandler) 71 : DIContext(CK_DWARF), DWPName(std::move(DWPName)), 72 RecoverableErrorHandler(RecoverableErrorHandler), 73 WarningHandler(WarningHandler), DObj(std::move(DObj)) {} 74 75 DWARFContext::~DWARFContext() = default; 76 77 /// Dump the UUID load command. 78 static void dumpUUID(raw_ostream &OS, const ObjectFile &Obj) { 79 auto *MachO = dyn_cast<MachOObjectFile>(&Obj); 80 if (!MachO) 81 return; 82 for (auto LC : MachO->load_commands()) { 83 raw_ostream::uuid_t UUID; 84 if (LC.C.cmd == MachO::LC_UUID) { 85 if (LC.C.cmdsize < sizeof(UUID) + sizeof(LC.C)) { 86 OS << "error: UUID load command is too short.\n"; 87 return; 88 } 89 OS << "UUID: "; 90 memcpy(&UUID, LC.Ptr+sizeof(LC.C), sizeof(UUID)); 91 OS.write_uuid(UUID); 92 Triple T = MachO->getArchTriple(); 93 OS << " (" << T.getArchName() << ')'; 94 OS << ' ' << MachO->getFileName() << '\n'; 95 } 96 } 97 } 98 99 using ContributionCollection = 100 std::vector<Optional<StrOffsetsContributionDescriptor>>; 101 102 // Collect all the contributions to the string offsets table from all units, 103 // sort them by their starting offsets and remove duplicates. 104 static ContributionCollection 105 collectContributionData(DWARFContext::unit_iterator_range Units) { 106 ContributionCollection Contributions; 107 for (const auto &U : Units) 108 if (const auto &C = U->getStringOffsetsTableContribution()) 109 Contributions.push_back(C); 110 // Sort the contributions so that any invalid ones are placed at 111 // the start of the contributions vector. This way they are reported 112 // first. 113 llvm::sort(Contributions, 114 [](const Optional<StrOffsetsContributionDescriptor> &L, 115 const Optional<StrOffsetsContributionDescriptor> &R) { 116 if (L && R) 117 return L->Base < R->Base; 118 return R.hasValue(); 119 }); 120 121 // Uniquify contributions, as it is possible that units (specifically 122 // type units in dwo or dwp files) share contributions. We don't want 123 // to report them more than once. 124 Contributions.erase( 125 std::unique(Contributions.begin(), Contributions.end(), 126 [](const Optional<StrOffsetsContributionDescriptor> &L, 127 const Optional<StrOffsetsContributionDescriptor> &R) { 128 if (L && R) 129 return L->Base == R->Base && L->Size == R->Size; 130 return false; 131 }), 132 Contributions.end()); 133 return Contributions; 134 } 135 136 // Dump a DWARF string offsets section. This may be a DWARF v5 formatted 137 // string offsets section, where each compile or type unit contributes a 138 // number of entries (string offsets), with each contribution preceded by 139 // a header containing size and version number. Alternatively, it may be a 140 // monolithic series of string offsets, as generated by the pre-DWARF v5 141 // implementation of split DWARF; however, in that case we still need to 142 // collect contributions of units because the size of the offsets (4 or 8 143 // bytes) depends on the format of the referencing unit (DWARF32 or DWARF64). 144 static void dumpStringOffsetsSection(raw_ostream &OS, DIDumpOptions DumpOpts, 145 StringRef SectionName, 146 const DWARFObject &Obj, 147 const DWARFSection &StringOffsetsSection, 148 StringRef StringSection, 149 DWARFContext::unit_iterator_range Units, 150 bool LittleEndian) { 151 auto Contributions = collectContributionData(Units); 152 DWARFDataExtractor StrOffsetExt(Obj, StringOffsetsSection, LittleEndian, 0); 153 DataExtractor StrData(StringSection, LittleEndian, 0); 154 uint64_t SectionSize = StringOffsetsSection.Data.size(); 155 uint64_t Offset = 0; 156 for (auto &Contribution : Contributions) { 157 // Report an ill-formed contribution. 158 if (!Contribution) { 159 OS << "error: invalid contribution to string offsets table in section ." 160 << SectionName << ".\n"; 161 return; 162 } 163 164 dwarf::DwarfFormat Format = Contribution->getFormat(); 165 int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(Format); 166 uint16_t Version = Contribution->getVersion(); 167 uint64_t ContributionHeader = Contribution->Base; 168 // In DWARF v5 there is a contribution header that immediately precedes 169 // the string offsets base (the location we have previously retrieved from 170 // the CU DIE's DW_AT_str_offsets attribute). The header is located either 171 // 8 or 16 bytes before the base, depending on the contribution's format. 172 if (Version >= 5) 173 ContributionHeader -= Format == DWARF32 ? 8 : 16; 174 175 // Detect overlapping contributions. 176 if (Offset > ContributionHeader) { 177 DumpOpts.RecoverableErrorHandler(createStringError( 178 errc::invalid_argument, 179 "overlapping contributions to string offsets table in section .%s.", 180 SectionName.data())); 181 } 182 // Report a gap in the table. 183 if (Offset < ContributionHeader) { 184 OS << format("0x%8.8" PRIx64 ": Gap, length = ", Offset); 185 OS << (ContributionHeader - Offset) << "\n"; 186 } 187 OS << format("0x%8.8" PRIx64 ": ", ContributionHeader); 188 // In DWARF v5 the contribution size in the descriptor does not equal 189 // the originally encoded length (it does not contain the length of the 190 // version field and the padding, a total of 4 bytes). Add them back in 191 // for reporting. 192 OS << "Contribution size = " << (Contribution->Size + (Version < 5 ? 0 : 4)) 193 << ", Format = " << dwarf::FormatString(Format) 194 << ", Version = " << Version << "\n"; 195 196 Offset = Contribution->Base; 197 unsigned EntrySize = Contribution->getDwarfOffsetByteSize(); 198 while (Offset - Contribution->Base < Contribution->Size) { 199 OS << format("0x%8.8" PRIx64 ": ", Offset); 200 uint64_t StringOffset = 201 StrOffsetExt.getRelocatedValue(EntrySize, &Offset); 202 OS << format("%0*" PRIx64 " ", OffsetDumpWidth, StringOffset); 203 const char *S = StrData.getCStr(&StringOffset); 204 if (S) 205 OS << format("\"%s\"", S); 206 OS << "\n"; 207 } 208 } 209 // Report a gap at the end of the table. 210 if (Offset < SectionSize) { 211 OS << format("0x%8.8" PRIx64 ": Gap, length = ", Offset); 212 OS << (SectionSize - Offset) << "\n"; 213 } 214 } 215 216 // Dump the .debug_addr section. 217 static void dumpAddrSection(raw_ostream &OS, DWARFDataExtractor &AddrData, 218 DIDumpOptions DumpOpts, uint16_t Version, 219 uint8_t AddrSize) { 220 uint64_t Offset = 0; 221 while (AddrData.isValidOffset(Offset)) { 222 DWARFDebugAddrTable AddrTable; 223 uint64_t TableOffset = Offset; 224 if (Error Err = AddrTable.extract(AddrData, &Offset, Version, AddrSize, 225 DumpOpts.WarningHandler)) { 226 DumpOpts.RecoverableErrorHandler(std::move(Err)); 227 // Keep going after an error, if we can, assuming that the length field 228 // could be read. If it couldn't, stop reading the section. 229 if (auto TableLength = AddrTable.getFullLength()) { 230 Offset = TableOffset + *TableLength; 231 continue; 232 } 233 break; 234 } 235 AddrTable.dump(OS, DumpOpts); 236 } 237 } 238 239 // Dump the .debug_rnglists or .debug_rnglists.dwo section (DWARF v5). 240 static void dumpRnglistsSection( 241 raw_ostream &OS, DWARFDataExtractor &rnglistData, 242 llvm::function_ref<Optional<object::SectionedAddress>(uint32_t)> 243 LookupPooledAddress, 244 DIDumpOptions DumpOpts) { 245 uint64_t Offset = 0; 246 while (rnglistData.isValidOffset(Offset)) { 247 llvm::DWARFDebugRnglistTable Rnglists; 248 uint64_t TableOffset = Offset; 249 if (Error Err = Rnglists.extract(rnglistData, &Offset)) { 250 DumpOpts.RecoverableErrorHandler(std::move(Err)); 251 uint64_t Length = Rnglists.length(); 252 // Keep going after an error, if we can, assuming that the length field 253 // could be read. If it couldn't, stop reading the section. 254 if (Length == 0) 255 break; 256 Offset = TableOffset + Length; 257 } else { 258 Rnglists.dump(OS, LookupPooledAddress, DumpOpts); 259 } 260 } 261 } 262 263 std::unique_ptr<DWARFDebugMacro> 264 DWARFContext::parseMacroOrMacinfo(MacroSecType SectionType) { 265 auto Macro = std::make_unique<DWARFDebugMacro>(); 266 auto ParseAndDump = [&](DWARFDataExtractor &Data, bool IsMacro) { 267 if (Error Err = IsMacro ? Macro->parseMacro(SectionType == MacroSection 268 ? compile_units() 269 : dwo_compile_units(), 270 SectionType == MacroSection 271 ? getStringExtractor() 272 : getStringDWOExtractor(), 273 Data) 274 : Macro->parseMacinfo(Data)) { 275 RecoverableErrorHandler(std::move(Err)); 276 Macro = nullptr; 277 } 278 }; 279 switch (SectionType) { 280 case MacinfoSection: { 281 DWARFDataExtractor Data(DObj->getMacinfoSection(), isLittleEndian(), 0); 282 ParseAndDump(Data, /*IsMacro=*/false); 283 break; 284 } 285 case MacinfoDwoSection: { 286 DWARFDataExtractor Data(DObj->getMacinfoDWOSection(), isLittleEndian(), 0); 287 ParseAndDump(Data, /*IsMacro=*/false); 288 break; 289 } 290 case MacroSection: { 291 DWARFDataExtractor Data(*DObj, DObj->getMacroSection(), isLittleEndian(), 292 0); 293 ParseAndDump(Data, /*IsMacro=*/true); 294 break; 295 } 296 case MacroDwoSection: { 297 DWARFDataExtractor Data(DObj->getMacroDWOSection(), isLittleEndian(), 0); 298 ParseAndDump(Data, /*IsMacro=*/true); 299 break; 300 } 301 } 302 return Macro; 303 } 304 305 static void dumpLoclistsSection(raw_ostream &OS, DIDumpOptions DumpOpts, 306 DWARFDataExtractor Data, 307 const MCRegisterInfo *MRI, 308 const DWARFObject &Obj, 309 Optional<uint64_t> DumpOffset) { 310 uint64_t Offset = 0; 311 312 while (Data.isValidOffset(Offset)) { 313 DWARFListTableHeader Header(".debug_loclists", "locations"); 314 if (Error E = Header.extract(Data, &Offset)) { 315 DumpOpts.RecoverableErrorHandler(std::move(E)); 316 return; 317 } 318 319 Header.dump(OS, DumpOpts); 320 321 uint64_t EndOffset = Header.length() + Header.getHeaderOffset(); 322 Data.setAddressSize(Header.getAddrSize()); 323 DWARFDebugLoclists Loc(Data, Header.getVersion()); 324 if (DumpOffset) { 325 if (DumpOffset >= Offset && DumpOffset < EndOffset) { 326 Offset = *DumpOffset; 327 Loc.dumpLocationList(&Offset, OS, /*BaseAddr=*/None, MRI, Obj, nullptr, 328 DumpOpts, /*Indent=*/0); 329 OS << "\n"; 330 return; 331 } 332 } else { 333 Loc.dumpRange(Offset, EndOffset - Offset, OS, MRI, Obj, DumpOpts); 334 } 335 Offset = EndOffset; 336 } 337 } 338 339 void DWARFContext::dump( 340 raw_ostream &OS, DIDumpOptions DumpOpts, 341 std::array<Optional<uint64_t>, DIDT_ID_Count> DumpOffsets) { 342 uint64_t DumpType = DumpOpts.DumpType; 343 344 StringRef Extension = sys::path::extension(DObj->getFileName()); 345 bool IsDWO = (Extension == ".dwo") || (Extension == ".dwp"); 346 347 // Print UUID header. 348 const auto *ObjFile = DObj->getFile(); 349 if (DumpType & DIDT_UUID) 350 dumpUUID(OS, *ObjFile); 351 352 // Print a header for each explicitly-requested section. 353 // Otherwise just print one for non-empty sections. 354 // Only print empty .dwo section headers when dumping a .dwo file. 355 bool Explicit = DumpType != DIDT_All && !IsDWO; 356 bool ExplicitDWO = Explicit && IsDWO; 357 auto shouldDump = [&](bool Explicit, const char *Name, unsigned ID, 358 StringRef Section) -> Optional<uint64_t> * { 359 unsigned Mask = 1U << ID; 360 bool Should = (DumpType & Mask) && (Explicit || !Section.empty()); 361 if (!Should) 362 return nullptr; 363 OS << "\n" << Name << " contents:\n"; 364 return &DumpOffsets[ID]; 365 }; 366 367 // Dump individual sections. 368 if (shouldDump(Explicit, ".debug_abbrev", DIDT_ID_DebugAbbrev, 369 DObj->getAbbrevSection())) 370 getDebugAbbrev()->dump(OS); 371 if (shouldDump(ExplicitDWO, ".debug_abbrev.dwo", DIDT_ID_DebugAbbrev, 372 DObj->getAbbrevDWOSection())) 373 getDebugAbbrevDWO()->dump(OS); 374 375 auto dumpDebugInfo = [&](const char *Name, unit_iterator_range Units) { 376 OS << '\n' << Name << " contents:\n"; 377 if (auto DumpOffset = DumpOffsets[DIDT_ID_DebugInfo]) 378 for (const auto &U : Units) 379 U->getDIEForOffset(DumpOffset.getValue()) 380 .dump(OS, 0, DumpOpts.noImplicitRecursion()); 381 else 382 for (const auto &U : Units) 383 U->dump(OS, DumpOpts); 384 }; 385 if ((DumpType & DIDT_DebugInfo)) { 386 if (Explicit || getNumCompileUnits()) 387 dumpDebugInfo(".debug_info", info_section_units()); 388 if (ExplicitDWO || getNumDWOCompileUnits()) 389 dumpDebugInfo(".debug_info.dwo", dwo_info_section_units()); 390 } 391 392 auto dumpDebugType = [&](const char *Name, unit_iterator_range Units) { 393 OS << '\n' << Name << " contents:\n"; 394 for (const auto &U : Units) 395 if (auto DumpOffset = DumpOffsets[DIDT_ID_DebugTypes]) 396 U->getDIEForOffset(*DumpOffset) 397 .dump(OS, 0, DumpOpts.noImplicitRecursion()); 398 else 399 U->dump(OS, DumpOpts); 400 }; 401 if ((DumpType & DIDT_DebugTypes)) { 402 if (Explicit || getNumTypeUnits()) 403 dumpDebugType(".debug_types", types_section_units()); 404 if (ExplicitDWO || getNumDWOTypeUnits()) 405 dumpDebugType(".debug_types.dwo", dwo_types_section_units()); 406 } 407 408 DIDumpOptions LLDumpOpts = DumpOpts; 409 if (LLDumpOpts.Verbose) 410 LLDumpOpts.DisplayRawContents = true; 411 412 if (const auto *Off = shouldDump(Explicit, ".debug_loc", DIDT_ID_DebugLoc, 413 DObj->getLocSection().Data)) { 414 getDebugLoc()->dump(OS, getRegisterInfo(), *DObj, LLDumpOpts, *Off); 415 } 416 if (const auto *Off = 417 shouldDump(Explicit, ".debug_loclists", DIDT_ID_DebugLoclists, 418 DObj->getLoclistsSection().Data)) { 419 DWARFDataExtractor Data(*DObj, DObj->getLoclistsSection(), isLittleEndian(), 420 0); 421 dumpLoclistsSection(OS, LLDumpOpts, Data, getRegisterInfo(), *DObj, *Off); 422 } 423 if (const auto *Off = 424 shouldDump(ExplicitDWO, ".debug_loclists.dwo", DIDT_ID_DebugLoclists, 425 DObj->getLoclistsDWOSection().Data)) { 426 DWARFDataExtractor Data(*DObj, DObj->getLoclistsDWOSection(), 427 isLittleEndian(), 0); 428 dumpLoclistsSection(OS, LLDumpOpts, Data, getRegisterInfo(), *DObj, *Off); 429 } 430 431 if (const auto *Off = 432 shouldDump(ExplicitDWO, ".debug_loc.dwo", DIDT_ID_DebugLoc, 433 DObj->getLocDWOSection().Data)) { 434 DWARFDataExtractor Data(*DObj, DObj->getLocDWOSection(), isLittleEndian(), 435 4); 436 DWARFDebugLoclists Loc(Data, /*Version=*/4); 437 if (*Off) { 438 uint64_t Offset = **Off; 439 Loc.dumpLocationList(&Offset, OS, 440 /*BaseAddr=*/None, getRegisterInfo(), *DObj, nullptr, 441 LLDumpOpts, /*Indent=*/0); 442 OS << "\n"; 443 } else { 444 Loc.dumpRange(0, Data.getData().size(), OS, getRegisterInfo(), *DObj, 445 LLDumpOpts); 446 } 447 } 448 449 if (const Optional<uint64_t> *Off = 450 shouldDump(Explicit, ".debug_frame", DIDT_ID_DebugFrame, 451 DObj->getFrameSection().Data)) { 452 if (Expected<const DWARFDebugFrame *> DF = getDebugFrame()) 453 (*DF)->dump(OS, getRegisterInfo(), *Off); 454 else 455 RecoverableErrorHandler(DF.takeError()); 456 } 457 458 if (const Optional<uint64_t> *Off = 459 shouldDump(Explicit, ".eh_frame", DIDT_ID_DebugFrame, 460 DObj->getEHFrameSection().Data)) { 461 if (Expected<const DWARFDebugFrame *> DF = getEHFrame()) 462 (*DF)->dump(OS, getRegisterInfo(), *Off); 463 else 464 RecoverableErrorHandler(DF.takeError()); 465 } 466 467 if (shouldDump(Explicit, ".debug_macro", DIDT_ID_DebugMacro, 468 DObj->getMacroSection().Data)) { 469 if (auto Macro = getDebugMacro()) 470 Macro->dump(OS); 471 } 472 473 if (shouldDump(Explicit, ".debug_macro.dwo", DIDT_ID_DebugMacro, 474 DObj->getMacroDWOSection())) { 475 if (auto MacroDWO = getDebugMacroDWO()) 476 MacroDWO->dump(OS); 477 } 478 479 if (shouldDump(Explicit, ".debug_macinfo", DIDT_ID_DebugMacro, 480 DObj->getMacinfoSection())) { 481 if (auto Macinfo = getDebugMacinfo()) 482 Macinfo->dump(OS); 483 } 484 485 if (shouldDump(Explicit, ".debug_macinfo.dwo", DIDT_ID_DebugMacro, 486 DObj->getMacinfoDWOSection())) { 487 if (auto MacinfoDWO = getDebugMacinfoDWO()) 488 MacinfoDWO->dump(OS); 489 } 490 491 if (shouldDump(Explicit, ".debug_aranges", DIDT_ID_DebugAranges, 492 DObj->getArangesSection())) { 493 uint64_t offset = 0; 494 DWARFDataExtractor arangesData(DObj->getArangesSection(), isLittleEndian(), 495 0); 496 DWARFDebugArangeSet set; 497 while (arangesData.isValidOffset(offset)) { 498 if (Error E = set.extract(arangesData, &offset)) { 499 RecoverableErrorHandler(std::move(E)); 500 break; 501 } 502 set.dump(OS); 503 } 504 } 505 506 auto DumpLineSection = [&](DWARFDebugLine::SectionParser Parser, 507 DIDumpOptions DumpOpts, 508 Optional<uint64_t> DumpOffset) { 509 while (!Parser.done()) { 510 if (DumpOffset && Parser.getOffset() != *DumpOffset) { 511 Parser.skip(DumpOpts.WarningHandler, DumpOpts.WarningHandler); 512 continue; 513 } 514 OS << "debug_line[" << format("0x%8.8" PRIx64, Parser.getOffset()) 515 << "]\n"; 516 OS.flush(); 517 if (DumpOpts.Verbose) { 518 Parser.parseNext(DumpOpts.WarningHandler, DumpOpts.WarningHandler, &OS); 519 } else { 520 DWARFDebugLine::LineTable LineTable = 521 Parser.parseNext(DumpOpts.WarningHandler, DumpOpts.WarningHandler); 522 LineTable.dump(OS, DumpOpts); 523 } 524 OS.flush(); 525 } 526 }; 527 528 if (const auto *Off = shouldDump(Explicit, ".debug_line", DIDT_ID_DebugLine, 529 DObj->getLineSection().Data)) { 530 DWARFDataExtractor LineData(*DObj, DObj->getLineSection(), isLittleEndian(), 531 0); 532 DWARFDebugLine::SectionParser Parser(LineData, *this, compile_units(), 533 type_units()); 534 DumpLineSection(Parser, DumpOpts, *Off); 535 } 536 537 if (const auto *Off = 538 shouldDump(ExplicitDWO, ".debug_line.dwo", DIDT_ID_DebugLine, 539 DObj->getLineDWOSection().Data)) { 540 DWARFDataExtractor LineData(*DObj, DObj->getLineDWOSection(), 541 isLittleEndian(), 0); 542 DWARFDebugLine::SectionParser Parser(LineData, *this, dwo_compile_units(), 543 dwo_type_units()); 544 DumpLineSection(Parser, DumpOpts, *Off); 545 } 546 547 if (shouldDump(Explicit, ".debug_cu_index", DIDT_ID_DebugCUIndex, 548 DObj->getCUIndexSection())) { 549 getCUIndex().dump(OS); 550 } 551 552 if (shouldDump(Explicit, ".debug_tu_index", DIDT_ID_DebugTUIndex, 553 DObj->getTUIndexSection())) { 554 getTUIndex().dump(OS); 555 } 556 557 if (shouldDump(Explicit, ".debug_str", DIDT_ID_DebugStr, 558 DObj->getStrSection())) { 559 DataExtractor strData(DObj->getStrSection(), isLittleEndian(), 0); 560 uint64_t offset = 0; 561 uint64_t strOffset = 0; 562 while (const char *s = strData.getCStr(&offset)) { 563 OS << format("0x%8.8" PRIx64 ": \"%s\"\n", strOffset, s); 564 strOffset = offset; 565 } 566 } 567 if (shouldDump(ExplicitDWO, ".debug_str.dwo", DIDT_ID_DebugStr, 568 DObj->getStrDWOSection())) { 569 DataExtractor strDWOData(DObj->getStrDWOSection(), isLittleEndian(), 0); 570 uint64_t offset = 0; 571 uint64_t strDWOOffset = 0; 572 while (const char *s = strDWOData.getCStr(&offset)) { 573 OS << format("0x%8.8" PRIx64 ": \"%s\"\n", strDWOOffset, s); 574 strDWOOffset = offset; 575 } 576 } 577 if (shouldDump(Explicit, ".debug_line_str", DIDT_ID_DebugLineStr, 578 DObj->getLineStrSection())) { 579 DataExtractor strData(DObj->getLineStrSection(), isLittleEndian(), 0); 580 uint64_t offset = 0; 581 uint64_t strOffset = 0; 582 while (const char *s = strData.getCStr(&offset)) { 583 OS << format("0x%8.8" PRIx64 ": \"", strOffset); 584 OS.write_escaped(s); 585 OS << "\"\n"; 586 strOffset = offset; 587 } 588 } 589 590 if (shouldDump(Explicit, ".debug_addr", DIDT_ID_DebugAddr, 591 DObj->getAddrSection().Data)) { 592 DWARFDataExtractor AddrData(*DObj, DObj->getAddrSection(), 593 isLittleEndian(), 0); 594 dumpAddrSection(OS, AddrData, DumpOpts, getMaxVersion(), getCUAddrSize()); 595 } 596 597 if (shouldDump(Explicit, ".debug_ranges", DIDT_ID_DebugRanges, 598 DObj->getRangesSection().Data)) { 599 uint8_t savedAddressByteSize = getCUAddrSize(); 600 DWARFDataExtractor rangesData(*DObj, DObj->getRangesSection(), 601 isLittleEndian(), savedAddressByteSize); 602 uint64_t offset = 0; 603 DWARFDebugRangeList rangeList; 604 while (rangesData.isValidOffset(offset)) { 605 if (Error E = rangeList.extract(rangesData, &offset)) { 606 DumpOpts.RecoverableErrorHandler(std::move(E)); 607 break; 608 } 609 rangeList.dump(OS); 610 } 611 } 612 613 auto LookupPooledAddress = [&](uint32_t Index) -> Optional<SectionedAddress> { 614 const auto &CUs = compile_units(); 615 auto I = CUs.begin(); 616 if (I == CUs.end()) 617 return None; 618 return (*I)->getAddrOffsetSectionItem(Index); 619 }; 620 621 if (shouldDump(Explicit, ".debug_rnglists", DIDT_ID_DebugRnglists, 622 DObj->getRnglistsSection().Data)) { 623 DWARFDataExtractor RnglistData(*DObj, DObj->getRnglistsSection(), 624 isLittleEndian(), 0); 625 dumpRnglistsSection(OS, RnglistData, LookupPooledAddress, DumpOpts); 626 } 627 628 if (shouldDump(ExplicitDWO, ".debug_rnglists.dwo", DIDT_ID_DebugRnglists, 629 DObj->getRnglistsDWOSection().Data)) { 630 DWARFDataExtractor RnglistData(*DObj, DObj->getRnglistsDWOSection(), 631 isLittleEndian(), 0); 632 dumpRnglistsSection(OS, RnglistData, LookupPooledAddress, DumpOpts); 633 } 634 635 if (shouldDump(Explicit, ".debug_pubnames", DIDT_ID_DebugPubnames, 636 DObj->getPubnamesSection().Data)) 637 DWARFDebugPubTable(*DObj, DObj->getPubnamesSection(), isLittleEndian(), false) 638 .dump(OS); 639 640 if (shouldDump(Explicit, ".debug_pubtypes", DIDT_ID_DebugPubtypes, 641 DObj->getPubtypesSection().Data)) 642 DWARFDebugPubTable(*DObj, DObj->getPubtypesSection(), isLittleEndian(), false) 643 .dump(OS); 644 645 if (shouldDump(Explicit, ".debug_gnu_pubnames", DIDT_ID_DebugGnuPubnames, 646 DObj->getGnuPubnamesSection().Data)) 647 DWARFDebugPubTable(*DObj, DObj->getGnuPubnamesSection(), isLittleEndian(), 648 true /* GnuStyle */) 649 .dump(OS); 650 651 if (shouldDump(Explicit, ".debug_gnu_pubtypes", DIDT_ID_DebugGnuPubtypes, 652 DObj->getGnuPubtypesSection().Data)) 653 DWARFDebugPubTable(*DObj, DObj->getGnuPubtypesSection(), isLittleEndian(), 654 true /* GnuStyle */) 655 .dump(OS); 656 657 if (shouldDump(Explicit, ".debug_str_offsets", DIDT_ID_DebugStrOffsets, 658 DObj->getStrOffsetsSection().Data)) 659 dumpStringOffsetsSection( 660 OS, DumpOpts, "debug_str_offsets", *DObj, DObj->getStrOffsetsSection(), 661 DObj->getStrSection(), normal_units(), isLittleEndian()); 662 if (shouldDump(ExplicitDWO, ".debug_str_offsets.dwo", DIDT_ID_DebugStrOffsets, 663 DObj->getStrOffsetsDWOSection().Data)) 664 dumpStringOffsetsSection(OS, DumpOpts, "debug_str_offsets.dwo", *DObj, 665 DObj->getStrOffsetsDWOSection(), 666 DObj->getStrDWOSection(), dwo_units(), 667 isLittleEndian()); 668 669 if (shouldDump(Explicit, ".gdb_index", DIDT_ID_GdbIndex, 670 DObj->getGdbIndexSection())) { 671 getGdbIndex().dump(OS); 672 } 673 674 if (shouldDump(Explicit, ".apple_names", DIDT_ID_AppleNames, 675 DObj->getAppleNamesSection().Data)) 676 getAppleNames().dump(OS); 677 678 if (shouldDump(Explicit, ".apple_types", DIDT_ID_AppleTypes, 679 DObj->getAppleTypesSection().Data)) 680 getAppleTypes().dump(OS); 681 682 if (shouldDump(Explicit, ".apple_namespaces", DIDT_ID_AppleNamespaces, 683 DObj->getAppleNamespacesSection().Data)) 684 getAppleNamespaces().dump(OS); 685 686 if (shouldDump(Explicit, ".apple_objc", DIDT_ID_AppleObjC, 687 DObj->getAppleObjCSection().Data)) 688 getAppleObjC().dump(OS); 689 if (shouldDump(Explicit, ".debug_names", DIDT_ID_DebugNames, 690 DObj->getNamesSection().Data)) 691 getDebugNames().dump(OS); 692 } 693 694 DWARFCompileUnit *DWARFContext::getDWOCompileUnitForHash(uint64_t Hash) { 695 parseDWOUnits(LazyParse); 696 697 if (const auto &CUI = getCUIndex()) { 698 if (const auto *R = CUI.getFromHash(Hash)) 699 return dyn_cast_or_null<DWARFCompileUnit>( 700 DWOUnits.getUnitForIndexEntry(*R)); 701 return nullptr; 702 } 703 704 // If there's no index, just search through the CUs in the DWO - there's 705 // probably only one unless this is something like LTO - though an in-process 706 // built/cached lookup table could be used in that case to improve repeated 707 // lookups of different CUs in the DWO. 708 for (const auto &DWOCU : dwo_compile_units()) { 709 // Might not have parsed DWO ID yet. 710 if (!DWOCU->getDWOId()) { 711 if (Optional<uint64_t> DWOId = 712 toUnsigned(DWOCU->getUnitDIE().find(DW_AT_GNU_dwo_id))) 713 DWOCU->setDWOId(*DWOId); 714 else 715 // No DWO ID? 716 continue; 717 } 718 if (DWOCU->getDWOId() == Hash) 719 return dyn_cast<DWARFCompileUnit>(DWOCU.get()); 720 } 721 return nullptr; 722 } 723 724 DWARFDie DWARFContext::getDIEForOffset(uint64_t Offset) { 725 parseNormalUnits(); 726 if (auto *CU = NormalUnits.getUnitForOffset(Offset)) 727 return CU->getDIEForOffset(Offset); 728 return DWARFDie(); 729 } 730 731 bool DWARFContext::verify(raw_ostream &OS, DIDumpOptions DumpOpts) { 732 bool Success = true; 733 DWARFVerifier verifier(OS, *this, DumpOpts); 734 735 Success &= verifier.handleDebugAbbrev(); 736 if (DumpOpts.DumpType & DIDT_DebugInfo) 737 Success &= verifier.handleDebugInfo(); 738 if (DumpOpts.DumpType & DIDT_DebugLine) 739 Success &= verifier.handleDebugLine(); 740 Success &= verifier.handleAccelTables(); 741 return Success; 742 } 743 744 const DWARFUnitIndex &DWARFContext::getCUIndex() { 745 if (CUIndex) 746 return *CUIndex; 747 748 DataExtractor CUIndexData(DObj->getCUIndexSection(), isLittleEndian(), 0); 749 750 CUIndex = std::make_unique<DWARFUnitIndex>(DW_SECT_INFO); 751 CUIndex->parse(CUIndexData); 752 return *CUIndex; 753 } 754 755 const DWARFUnitIndex &DWARFContext::getTUIndex() { 756 if (TUIndex) 757 return *TUIndex; 758 759 DataExtractor TUIndexData(DObj->getTUIndexSection(), isLittleEndian(), 0); 760 761 TUIndex = std::make_unique<DWARFUnitIndex>(DW_SECT_EXT_TYPES); 762 TUIndex->parse(TUIndexData); 763 return *TUIndex; 764 } 765 766 DWARFGdbIndex &DWARFContext::getGdbIndex() { 767 if (GdbIndex) 768 return *GdbIndex; 769 770 DataExtractor GdbIndexData(DObj->getGdbIndexSection(), true /*LE*/, 0); 771 GdbIndex = std::make_unique<DWARFGdbIndex>(); 772 GdbIndex->parse(GdbIndexData); 773 return *GdbIndex; 774 } 775 776 const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() { 777 if (Abbrev) 778 return Abbrev.get(); 779 780 DataExtractor abbrData(DObj->getAbbrevSection(), isLittleEndian(), 0); 781 782 Abbrev.reset(new DWARFDebugAbbrev()); 783 Abbrev->extract(abbrData); 784 return Abbrev.get(); 785 } 786 787 const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() { 788 if (AbbrevDWO) 789 return AbbrevDWO.get(); 790 791 DataExtractor abbrData(DObj->getAbbrevDWOSection(), isLittleEndian(), 0); 792 AbbrevDWO.reset(new DWARFDebugAbbrev()); 793 AbbrevDWO->extract(abbrData); 794 return AbbrevDWO.get(); 795 } 796 797 const DWARFDebugLoc *DWARFContext::getDebugLoc() { 798 if (Loc) 799 return Loc.get(); 800 801 // Assume all units have the same address byte size. 802 auto LocData = 803 getNumCompileUnits() 804 ? DWARFDataExtractor(*DObj, DObj->getLocSection(), isLittleEndian(), 805 getUnitAtIndex(0)->getAddressByteSize()) 806 : DWARFDataExtractor("", isLittleEndian(), 0); 807 Loc.reset(new DWARFDebugLoc(std::move(LocData))); 808 return Loc.get(); 809 } 810 811 const DWARFDebugAranges *DWARFContext::getDebugAranges() { 812 if (Aranges) 813 return Aranges.get(); 814 815 Aranges.reset(new DWARFDebugAranges()); 816 Aranges->generate(this); 817 return Aranges.get(); 818 } 819 820 Expected<const DWARFDebugFrame *> DWARFContext::getDebugFrame() { 821 if (DebugFrame) 822 return DebugFrame.get(); 823 824 // There's a "bug" in the DWARFv3 standard with respect to the target address 825 // size within debug frame sections. While DWARF is supposed to be independent 826 // of its container, FDEs have fields with size being "target address size", 827 // which isn't specified in DWARF in general. It's only specified for CUs, but 828 // .eh_frame can appear without a .debug_info section. Follow the example of 829 // other tools (libdwarf) and extract this from the container (ObjectFile 830 // provides this information). This problem is fixed in DWARFv4 831 // See this dwarf-discuss discussion for more details: 832 // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html 833 DWARFDataExtractor debugFrameData(*DObj, DObj->getFrameSection(), 834 isLittleEndian(), DObj->getAddressSize()); 835 auto DF = std::make_unique<DWARFDebugFrame>(getArch(), /*IsEH=*/false); 836 if (Error E = DF->parse(debugFrameData)) 837 return std::move(E); 838 839 DebugFrame.swap(DF); 840 return DebugFrame.get(); 841 } 842 843 Expected<const DWARFDebugFrame *> DWARFContext::getEHFrame() { 844 if (EHFrame) 845 return EHFrame.get(); 846 847 DWARFDataExtractor debugFrameData(*DObj, DObj->getEHFrameSection(), 848 isLittleEndian(), DObj->getAddressSize()); 849 850 auto DF = std::make_unique<DWARFDebugFrame>(getArch(), /*IsEH=*/true); 851 if (Error E = DF->parse(debugFrameData)) 852 return std::move(E); 853 DebugFrame.swap(DF); 854 return DebugFrame.get(); 855 } 856 857 const DWARFDebugMacro *DWARFContext::getDebugMacro() { 858 if (!Macro) 859 Macro = parseMacroOrMacinfo(MacroSection); 860 return Macro.get(); 861 } 862 863 const DWARFDebugMacro *DWARFContext::getDebugMacroDWO() { 864 if (!MacroDWO) 865 MacroDWO = parseMacroOrMacinfo(MacroDwoSection); 866 return MacroDWO.get(); 867 } 868 869 const DWARFDebugMacro *DWARFContext::getDebugMacinfo() { 870 if (!Macinfo) 871 Macinfo = parseMacroOrMacinfo(MacinfoSection); 872 return Macinfo.get(); 873 } 874 875 const DWARFDebugMacro *DWARFContext::getDebugMacinfoDWO() { 876 if (!MacinfoDWO) 877 MacinfoDWO = parseMacroOrMacinfo(MacinfoDwoSection); 878 return MacinfoDWO.get(); 879 } 880 881 template <typename T> 882 static T &getAccelTable(std::unique_ptr<T> &Cache, const DWARFObject &Obj, 883 const DWARFSection &Section, StringRef StringSection, 884 bool IsLittleEndian) { 885 if (Cache) 886 return *Cache; 887 DWARFDataExtractor AccelSection(Obj, Section, IsLittleEndian, 0); 888 DataExtractor StrData(StringSection, IsLittleEndian, 0); 889 Cache.reset(new T(AccelSection, StrData)); 890 if (Error E = Cache->extract()) 891 llvm::consumeError(std::move(E)); 892 return *Cache; 893 } 894 895 const DWARFDebugNames &DWARFContext::getDebugNames() { 896 return getAccelTable(Names, *DObj, DObj->getNamesSection(), 897 DObj->getStrSection(), isLittleEndian()); 898 } 899 900 const AppleAcceleratorTable &DWARFContext::getAppleNames() { 901 return getAccelTable(AppleNames, *DObj, DObj->getAppleNamesSection(), 902 DObj->getStrSection(), isLittleEndian()); 903 } 904 905 const AppleAcceleratorTable &DWARFContext::getAppleTypes() { 906 return getAccelTable(AppleTypes, *DObj, DObj->getAppleTypesSection(), 907 DObj->getStrSection(), isLittleEndian()); 908 } 909 910 const AppleAcceleratorTable &DWARFContext::getAppleNamespaces() { 911 return getAccelTable(AppleNamespaces, *DObj, 912 DObj->getAppleNamespacesSection(), 913 DObj->getStrSection(), isLittleEndian()); 914 } 915 916 const AppleAcceleratorTable &DWARFContext::getAppleObjC() { 917 return getAccelTable(AppleObjC, *DObj, DObj->getAppleObjCSection(), 918 DObj->getStrSection(), isLittleEndian()); 919 } 920 921 const DWARFDebugLine::LineTable * 922 DWARFContext::getLineTableForUnit(DWARFUnit *U) { 923 Expected<const DWARFDebugLine::LineTable *> ExpectedLineTable = 924 getLineTableForUnit(U, WarningHandler); 925 if (!ExpectedLineTable) { 926 WarningHandler(ExpectedLineTable.takeError()); 927 return nullptr; 928 } 929 return *ExpectedLineTable; 930 } 931 932 Expected<const DWARFDebugLine::LineTable *> DWARFContext::getLineTableForUnit( 933 DWARFUnit *U, function_ref<void(Error)> RecoverableErrorHandler) { 934 if (!Line) 935 Line.reset(new DWARFDebugLine); 936 937 auto UnitDIE = U->getUnitDIE(); 938 if (!UnitDIE) 939 return nullptr; 940 941 auto Offset = toSectionOffset(UnitDIE.find(DW_AT_stmt_list)); 942 if (!Offset) 943 return nullptr; // No line table for this compile unit. 944 945 uint64_t stmtOffset = *Offset + U->getLineTableOffset(); 946 // See if the line table is cached. 947 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset)) 948 return lt; 949 950 // Make sure the offset is good before we try to parse. 951 if (stmtOffset >= U->getLineSection().Data.size()) 952 return nullptr; 953 954 // We have to parse it first. 955 DWARFDataExtractor lineData(*DObj, U->getLineSection(), isLittleEndian(), 956 U->getAddressByteSize()); 957 return Line->getOrParseLineTable(lineData, stmtOffset, *this, U, 958 RecoverableErrorHandler); 959 } 960 961 void DWARFContext::parseNormalUnits() { 962 if (!NormalUnits.empty()) 963 return; 964 DObj->forEachInfoSections([&](const DWARFSection &S) { 965 NormalUnits.addUnitsForSection(*this, S, DW_SECT_INFO); 966 }); 967 NormalUnits.finishedInfoUnits(); 968 DObj->forEachTypesSections([&](const DWARFSection &S) { 969 NormalUnits.addUnitsForSection(*this, S, DW_SECT_EXT_TYPES); 970 }); 971 } 972 973 void DWARFContext::parseDWOUnits(bool Lazy) { 974 if (!DWOUnits.empty()) 975 return; 976 DObj->forEachInfoDWOSections([&](const DWARFSection &S) { 977 DWOUnits.addUnitsForDWOSection(*this, S, DW_SECT_INFO, Lazy); 978 }); 979 DWOUnits.finishedInfoUnits(); 980 DObj->forEachTypesDWOSections([&](const DWARFSection &S) { 981 DWOUnits.addUnitsForDWOSection(*this, S, DW_SECT_EXT_TYPES, Lazy); 982 }); 983 } 984 985 DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint64_t Offset) { 986 parseNormalUnits(); 987 return dyn_cast_or_null<DWARFCompileUnit>( 988 NormalUnits.getUnitForOffset(Offset)); 989 } 990 991 DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) { 992 // First, get the offset of the compile unit. 993 uint64_t CUOffset = getDebugAranges()->findAddress(Address); 994 // Retrieve the compile unit. 995 return getCompileUnitForOffset(CUOffset); 996 } 997 998 DWARFContext::DIEsForAddress DWARFContext::getDIEsForAddress(uint64_t Address) { 999 DIEsForAddress Result; 1000 1001 DWARFCompileUnit *CU = getCompileUnitForAddress(Address); 1002 if (!CU) 1003 return Result; 1004 1005 Result.CompileUnit = CU; 1006 Result.FunctionDIE = CU->getSubroutineForAddress(Address); 1007 1008 std::vector<DWARFDie> Worklist; 1009 Worklist.push_back(Result.FunctionDIE); 1010 while (!Worklist.empty()) { 1011 DWARFDie DIE = Worklist.back(); 1012 Worklist.pop_back(); 1013 1014 if (!DIE.isValid()) 1015 continue; 1016 1017 if (DIE.getTag() == DW_TAG_lexical_block && 1018 DIE.addressRangeContainsAddress(Address)) { 1019 Result.BlockDIE = DIE; 1020 break; 1021 } 1022 1023 for (auto Child : DIE) 1024 Worklist.push_back(Child); 1025 } 1026 1027 return Result; 1028 } 1029 1030 /// TODO: change input parameter from "uint64_t Address" 1031 /// into "SectionedAddress Address" 1032 static bool getFunctionNameAndStartLineForAddress(DWARFCompileUnit *CU, 1033 uint64_t Address, 1034 FunctionNameKind Kind, 1035 std::string &FunctionName, 1036 uint32_t &StartLine) { 1037 // The address may correspond to instruction in some inlined function, 1038 // so we have to build the chain of inlined functions and take the 1039 // name of the topmost function in it. 1040 SmallVector<DWARFDie, 4> InlinedChain; 1041 CU->getInlinedChainForAddress(Address, InlinedChain); 1042 if (InlinedChain.empty()) 1043 return false; 1044 1045 const DWARFDie &DIE = InlinedChain[0]; 1046 bool FoundResult = false; 1047 const char *Name = nullptr; 1048 if (Kind != FunctionNameKind::None && (Name = DIE.getSubroutineName(Kind))) { 1049 FunctionName = Name; 1050 FoundResult = true; 1051 } 1052 if (auto DeclLineResult = DIE.getDeclLine()) { 1053 StartLine = DeclLineResult; 1054 FoundResult = true; 1055 } 1056 1057 return FoundResult; 1058 } 1059 1060 static Optional<uint64_t> getTypeSize(DWARFDie Type, uint64_t PointerSize) { 1061 if (auto SizeAttr = Type.find(DW_AT_byte_size)) 1062 if (Optional<uint64_t> Size = SizeAttr->getAsUnsignedConstant()) 1063 return Size; 1064 1065 switch (Type.getTag()) { 1066 case DW_TAG_pointer_type: 1067 case DW_TAG_reference_type: 1068 case DW_TAG_rvalue_reference_type: 1069 return PointerSize; 1070 case DW_TAG_ptr_to_member_type: { 1071 if (DWARFDie BaseType = Type.getAttributeValueAsReferencedDie(DW_AT_type)) 1072 if (BaseType.getTag() == DW_TAG_subroutine_type) 1073 return 2 * PointerSize; 1074 return PointerSize; 1075 } 1076 case DW_TAG_const_type: 1077 case DW_TAG_volatile_type: 1078 case DW_TAG_restrict_type: 1079 case DW_TAG_typedef: { 1080 if (DWARFDie BaseType = Type.getAttributeValueAsReferencedDie(DW_AT_type)) 1081 return getTypeSize(BaseType, PointerSize); 1082 break; 1083 } 1084 case DW_TAG_array_type: { 1085 DWARFDie BaseType = Type.getAttributeValueAsReferencedDie(DW_AT_type); 1086 if (!BaseType) 1087 return Optional<uint64_t>(); 1088 Optional<uint64_t> BaseSize = getTypeSize(BaseType, PointerSize); 1089 if (!BaseSize) 1090 return Optional<uint64_t>(); 1091 uint64_t Size = *BaseSize; 1092 for (DWARFDie Child : Type) { 1093 if (Child.getTag() != DW_TAG_subrange_type) 1094 continue; 1095 1096 if (auto ElemCountAttr = Child.find(DW_AT_count)) 1097 if (Optional<uint64_t> ElemCount = 1098 ElemCountAttr->getAsUnsignedConstant()) 1099 Size *= *ElemCount; 1100 if (auto UpperBoundAttr = Child.find(DW_AT_upper_bound)) 1101 if (Optional<int64_t> UpperBound = 1102 UpperBoundAttr->getAsSignedConstant()) { 1103 int64_t LowerBound = 0; 1104 if (auto LowerBoundAttr = Child.find(DW_AT_lower_bound)) 1105 LowerBound = LowerBoundAttr->getAsSignedConstant().getValueOr(0); 1106 Size *= *UpperBound - LowerBound + 1; 1107 } 1108 } 1109 return Size; 1110 } 1111 default: 1112 break; 1113 } 1114 return Optional<uint64_t>(); 1115 } 1116 1117 static Optional<int64_t> 1118 getExpressionFrameOffset(ArrayRef<uint8_t> Expr, 1119 Optional<unsigned> FrameBaseReg) { 1120 if (!Expr.empty() && 1121 (Expr[0] == DW_OP_fbreg || 1122 (FrameBaseReg && Expr[0] == DW_OP_breg0 + *FrameBaseReg))) { 1123 unsigned Count; 1124 int64_t Offset = decodeSLEB128(Expr.data() + 1, &Count, Expr.end()); 1125 // A single DW_OP_fbreg or DW_OP_breg. 1126 if (Expr.size() == Count + 1) 1127 return Offset; 1128 // Same + DW_OP_deref (Fortran arrays look like this). 1129 if (Expr.size() == Count + 2 && Expr[Count + 1] == DW_OP_deref) 1130 return Offset; 1131 // Fallthrough. Do not accept ex. (DW_OP_breg W29, DW_OP_stack_value) 1132 } 1133 return None; 1134 } 1135 1136 void DWARFContext::addLocalsForDie(DWARFCompileUnit *CU, DWARFDie Subprogram, 1137 DWARFDie Die, std::vector<DILocal> &Result) { 1138 if (Die.getTag() == DW_TAG_variable || 1139 Die.getTag() == DW_TAG_formal_parameter) { 1140 DILocal Local; 1141 if (const char *Name = Subprogram.getSubroutineName(DINameKind::ShortName)) 1142 Local.FunctionName = Name; 1143 1144 Optional<unsigned> FrameBaseReg; 1145 if (auto FrameBase = Subprogram.find(DW_AT_frame_base)) 1146 if (Optional<ArrayRef<uint8_t>> Expr = FrameBase->getAsBlock()) 1147 if (!Expr->empty() && (*Expr)[0] >= DW_OP_reg0 && 1148 (*Expr)[0] <= DW_OP_reg31) { 1149 FrameBaseReg = (*Expr)[0] - DW_OP_reg0; 1150 } 1151 1152 if (Expected<std::vector<DWARFLocationExpression>> Loc = 1153 Die.getLocations(DW_AT_location)) { 1154 for (const auto &Entry : *Loc) { 1155 if (Optional<int64_t> FrameOffset = 1156 getExpressionFrameOffset(Entry.Expr, FrameBaseReg)) { 1157 Local.FrameOffset = *FrameOffset; 1158 break; 1159 } 1160 } 1161 } else { 1162 // FIXME: missing DW_AT_location is OK here, but other errors should be 1163 // reported to the user. 1164 consumeError(Loc.takeError()); 1165 } 1166 1167 if (auto TagOffsetAttr = Die.find(DW_AT_LLVM_tag_offset)) 1168 Local.TagOffset = TagOffsetAttr->getAsUnsignedConstant(); 1169 1170 if (auto Origin = 1171 Die.getAttributeValueAsReferencedDie(DW_AT_abstract_origin)) 1172 Die = Origin; 1173 if (auto NameAttr = Die.find(DW_AT_name)) 1174 if (Optional<const char *> Name = NameAttr->getAsCString()) 1175 Local.Name = *Name; 1176 if (auto Type = Die.getAttributeValueAsReferencedDie(DW_AT_type)) 1177 Local.Size = getTypeSize(Type, getCUAddrSize()); 1178 if (auto DeclFileAttr = Die.find(DW_AT_decl_file)) { 1179 if (const auto *LT = CU->getContext().getLineTableForUnit(CU)) 1180 LT->getFileNameByIndex( 1181 DeclFileAttr->getAsUnsignedConstant().getValue(), 1182 CU->getCompilationDir(), 1183 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, 1184 Local.DeclFile); 1185 } 1186 if (auto DeclLineAttr = Die.find(DW_AT_decl_line)) 1187 Local.DeclLine = DeclLineAttr->getAsUnsignedConstant().getValue(); 1188 1189 Result.push_back(Local); 1190 return; 1191 } 1192 1193 if (Die.getTag() == DW_TAG_inlined_subroutine) 1194 if (auto Origin = 1195 Die.getAttributeValueAsReferencedDie(DW_AT_abstract_origin)) 1196 Subprogram = Origin; 1197 1198 for (auto Child : Die) 1199 addLocalsForDie(CU, Subprogram, Child, Result); 1200 } 1201 1202 std::vector<DILocal> 1203 DWARFContext::getLocalsForAddress(object::SectionedAddress Address) { 1204 std::vector<DILocal> Result; 1205 DWARFCompileUnit *CU = getCompileUnitForAddress(Address.Address); 1206 if (!CU) 1207 return Result; 1208 1209 DWARFDie Subprogram = CU->getSubroutineForAddress(Address.Address); 1210 if (Subprogram.isValid()) 1211 addLocalsForDie(CU, Subprogram, Subprogram, Result); 1212 return Result; 1213 } 1214 1215 DILineInfo DWARFContext::getLineInfoForAddress(object::SectionedAddress Address, 1216 DILineInfoSpecifier Spec) { 1217 DILineInfo Result; 1218 1219 DWARFCompileUnit *CU = getCompileUnitForAddress(Address.Address); 1220 if (!CU) 1221 return Result; 1222 1223 getFunctionNameAndStartLineForAddress(CU, Address.Address, Spec.FNKind, 1224 Result.FunctionName, Result.StartLine); 1225 if (Spec.FLIKind != FileLineInfoKind::None) { 1226 if (const DWARFLineTable *LineTable = getLineTableForUnit(CU)) { 1227 LineTable->getFileLineInfoForAddress( 1228 {Address.Address, Address.SectionIndex}, CU->getCompilationDir(), 1229 Spec.FLIKind, Result); 1230 } 1231 } 1232 return Result; 1233 } 1234 1235 DILineInfoTable DWARFContext::getLineInfoForAddressRange( 1236 object::SectionedAddress Address, uint64_t Size, DILineInfoSpecifier Spec) { 1237 DILineInfoTable Lines; 1238 DWARFCompileUnit *CU = getCompileUnitForAddress(Address.Address); 1239 if (!CU) 1240 return Lines; 1241 1242 uint32_t StartLine = 0; 1243 std::string FunctionName(DILineInfo::BadString); 1244 getFunctionNameAndStartLineForAddress(CU, Address.Address, Spec.FNKind, 1245 FunctionName, StartLine); 1246 1247 // If the Specifier says we don't need FileLineInfo, just 1248 // return the top-most function at the starting address. 1249 if (Spec.FLIKind == FileLineInfoKind::None) { 1250 DILineInfo Result; 1251 Result.FunctionName = FunctionName; 1252 Result.StartLine = StartLine; 1253 Lines.push_back(std::make_pair(Address.Address, Result)); 1254 return Lines; 1255 } 1256 1257 const DWARFLineTable *LineTable = getLineTableForUnit(CU); 1258 1259 // Get the index of row we're looking for in the line table. 1260 std::vector<uint32_t> RowVector; 1261 if (!LineTable->lookupAddressRange({Address.Address, Address.SectionIndex}, 1262 Size, RowVector)) { 1263 return Lines; 1264 } 1265 1266 for (uint32_t RowIndex : RowVector) { 1267 // Take file number and line/column from the row. 1268 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex]; 1269 DILineInfo Result; 1270 LineTable->getFileNameByIndex(Row.File, CU->getCompilationDir(), 1271 Spec.FLIKind, Result.FileName); 1272 Result.FunctionName = FunctionName; 1273 Result.Line = Row.Line; 1274 Result.Column = Row.Column; 1275 Result.StartLine = StartLine; 1276 Lines.push_back(std::make_pair(Row.Address.Address, Result)); 1277 } 1278 1279 return Lines; 1280 } 1281 1282 DIInliningInfo 1283 DWARFContext::getInliningInfoForAddress(object::SectionedAddress Address, 1284 DILineInfoSpecifier Spec) { 1285 DIInliningInfo InliningInfo; 1286 1287 DWARFCompileUnit *CU = getCompileUnitForAddress(Address.Address); 1288 if (!CU) 1289 return InliningInfo; 1290 1291 const DWARFLineTable *LineTable = nullptr; 1292 SmallVector<DWARFDie, 4> InlinedChain; 1293 CU->getInlinedChainForAddress(Address.Address, InlinedChain); 1294 if (InlinedChain.size() == 0) { 1295 // If there is no DIE for address (e.g. it is in unavailable .dwo file), 1296 // try to at least get file/line info from symbol table. 1297 if (Spec.FLIKind != FileLineInfoKind::None) { 1298 DILineInfo Frame; 1299 LineTable = getLineTableForUnit(CU); 1300 if (LineTable && LineTable->getFileLineInfoForAddress( 1301 {Address.Address, Address.SectionIndex}, 1302 CU->getCompilationDir(), Spec.FLIKind, Frame)) 1303 InliningInfo.addFrame(Frame); 1304 } 1305 return InliningInfo; 1306 } 1307 1308 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0, CallDiscriminator = 0; 1309 for (uint32_t i = 0, n = InlinedChain.size(); i != n; i++) { 1310 DWARFDie &FunctionDIE = InlinedChain[i]; 1311 DILineInfo Frame; 1312 // Get function name if necessary. 1313 if (const char *Name = FunctionDIE.getSubroutineName(Spec.FNKind)) 1314 Frame.FunctionName = Name; 1315 if (auto DeclLineResult = FunctionDIE.getDeclLine()) 1316 Frame.StartLine = DeclLineResult; 1317 if (Spec.FLIKind != FileLineInfoKind::None) { 1318 if (i == 0) { 1319 // For the topmost frame, initialize the line table of this 1320 // compile unit and fetch file/line info from it. 1321 LineTable = getLineTableForUnit(CU); 1322 // For the topmost routine, get file/line info from line table. 1323 if (LineTable) 1324 LineTable->getFileLineInfoForAddress( 1325 {Address.Address, Address.SectionIndex}, CU->getCompilationDir(), 1326 Spec.FLIKind, Frame); 1327 } else { 1328 // Otherwise, use call file, call line and call column from 1329 // previous DIE in inlined chain. 1330 if (LineTable) 1331 LineTable->getFileNameByIndex(CallFile, CU->getCompilationDir(), 1332 Spec.FLIKind, Frame.FileName); 1333 Frame.Line = CallLine; 1334 Frame.Column = CallColumn; 1335 Frame.Discriminator = CallDiscriminator; 1336 } 1337 // Get call file/line/column of a current DIE. 1338 if (i + 1 < n) { 1339 FunctionDIE.getCallerFrame(CallFile, CallLine, CallColumn, 1340 CallDiscriminator); 1341 } 1342 } 1343 InliningInfo.addFrame(Frame); 1344 } 1345 return InliningInfo; 1346 } 1347 1348 std::shared_ptr<DWARFContext> 1349 DWARFContext::getDWOContext(StringRef AbsolutePath) { 1350 if (auto S = DWP.lock()) { 1351 DWARFContext *Ctxt = S->Context.get(); 1352 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt); 1353 } 1354 1355 std::weak_ptr<DWOFile> *Entry = &DWOFiles[AbsolutePath]; 1356 1357 if (auto S = Entry->lock()) { 1358 DWARFContext *Ctxt = S->Context.get(); 1359 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt); 1360 } 1361 1362 Expected<OwningBinary<ObjectFile>> Obj = [&] { 1363 if (!CheckedForDWP) { 1364 SmallString<128> DWPName; 1365 auto Obj = object::ObjectFile::createObjectFile( 1366 this->DWPName.empty() 1367 ? (DObj->getFileName() + ".dwp").toStringRef(DWPName) 1368 : StringRef(this->DWPName)); 1369 if (Obj) { 1370 Entry = &DWP; 1371 return Obj; 1372 } else { 1373 CheckedForDWP = true; 1374 // TODO: Should this error be handled (maybe in a high verbosity mode) 1375 // before falling back to .dwo files? 1376 consumeError(Obj.takeError()); 1377 } 1378 } 1379 1380 return object::ObjectFile::createObjectFile(AbsolutePath); 1381 }(); 1382 1383 if (!Obj) { 1384 // TODO: Actually report errors helpfully. 1385 consumeError(Obj.takeError()); 1386 return nullptr; 1387 } 1388 1389 auto S = std::make_shared<DWOFile>(); 1390 S->File = std::move(Obj.get()); 1391 S->Context = DWARFContext::create(*S->File.getBinary()); 1392 *Entry = S; 1393 auto *Ctxt = S->Context.get(); 1394 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt); 1395 } 1396 1397 static Error createError(const Twine &Reason, llvm::Error E) { 1398 return make_error<StringError>(Reason + toString(std::move(E)), 1399 inconvertibleErrorCode()); 1400 } 1401 1402 /// SymInfo contains information about symbol: it's address 1403 /// and section index which is -1LL for absolute symbols. 1404 struct SymInfo { 1405 uint64_t Address; 1406 uint64_t SectionIndex; 1407 }; 1408 1409 /// Returns the address of symbol relocation used against and a section index. 1410 /// Used for futher relocations computation. Symbol's section load address is 1411 static Expected<SymInfo> getSymbolInfo(const object::ObjectFile &Obj, 1412 const RelocationRef &Reloc, 1413 const LoadedObjectInfo *L, 1414 std::map<SymbolRef, SymInfo> &Cache) { 1415 SymInfo Ret = {0, (uint64_t)-1LL}; 1416 object::section_iterator RSec = Obj.section_end(); 1417 object::symbol_iterator Sym = Reloc.getSymbol(); 1418 1419 std::map<SymbolRef, SymInfo>::iterator CacheIt = Cache.end(); 1420 // First calculate the address of the symbol or section as it appears 1421 // in the object file 1422 if (Sym != Obj.symbol_end()) { 1423 bool New; 1424 std::tie(CacheIt, New) = Cache.insert({*Sym, {0, 0}}); 1425 if (!New) 1426 return CacheIt->second; 1427 1428 Expected<uint64_t> SymAddrOrErr = Sym->getAddress(); 1429 if (!SymAddrOrErr) 1430 return createError("failed to compute symbol address: ", 1431 SymAddrOrErr.takeError()); 1432 1433 // Also remember what section this symbol is in for later 1434 auto SectOrErr = Sym->getSection(); 1435 if (!SectOrErr) 1436 return createError("failed to get symbol section: ", 1437 SectOrErr.takeError()); 1438 1439 RSec = *SectOrErr; 1440 Ret.Address = *SymAddrOrErr; 1441 } else if (auto *MObj = dyn_cast<MachOObjectFile>(&Obj)) { 1442 RSec = MObj->getRelocationSection(Reloc.getRawDataRefImpl()); 1443 Ret.Address = RSec->getAddress(); 1444 } 1445 1446 if (RSec != Obj.section_end()) 1447 Ret.SectionIndex = RSec->getIndex(); 1448 1449 // If we are given load addresses for the sections, we need to adjust: 1450 // SymAddr = (Address of Symbol Or Section in File) - 1451 // (Address of Section in File) + 1452 // (Load Address of Section) 1453 // RSec is now either the section being targeted or the section 1454 // containing the symbol being targeted. In either case, 1455 // we need to perform the same computation. 1456 if (L && RSec != Obj.section_end()) 1457 if (uint64_t SectionLoadAddress = L->getSectionLoadAddress(*RSec)) 1458 Ret.Address += SectionLoadAddress - RSec->getAddress(); 1459 1460 if (CacheIt != Cache.end()) 1461 CacheIt->second = Ret; 1462 1463 return Ret; 1464 } 1465 1466 static bool isRelocScattered(const object::ObjectFile &Obj, 1467 const RelocationRef &Reloc) { 1468 const MachOObjectFile *MachObj = dyn_cast<MachOObjectFile>(&Obj); 1469 if (!MachObj) 1470 return false; 1471 // MachO also has relocations that point to sections and 1472 // scattered relocations. 1473 auto RelocInfo = MachObj->getRelocation(Reloc.getRawDataRefImpl()); 1474 return MachObj->isRelocationScattered(RelocInfo); 1475 } 1476 1477 namespace { 1478 struct DWARFSectionMap final : public DWARFSection { 1479 RelocAddrMap Relocs; 1480 }; 1481 1482 class DWARFObjInMemory final : public DWARFObject { 1483 bool IsLittleEndian; 1484 uint8_t AddressSize; 1485 StringRef FileName; 1486 const object::ObjectFile *Obj = nullptr; 1487 std::vector<SectionName> SectionNames; 1488 1489 using InfoSectionMap = MapVector<object::SectionRef, DWARFSectionMap, 1490 std::map<object::SectionRef, unsigned>>; 1491 1492 InfoSectionMap InfoSections; 1493 InfoSectionMap TypesSections; 1494 InfoSectionMap InfoDWOSections; 1495 InfoSectionMap TypesDWOSections; 1496 1497 DWARFSectionMap LocSection; 1498 DWARFSectionMap LoclistsSection; 1499 DWARFSectionMap LoclistsDWOSection; 1500 DWARFSectionMap LineSection; 1501 DWARFSectionMap RangesSection; 1502 DWARFSectionMap RnglistsSection; 1503 DWARFSectionMap StrOffsetsSection; 1504 DWARFSectionMap LineDWOSection; 1505 DWARFSectionMap FrameSection; 1506 DWARFSectionMap EHFrameSection; 1507 DWARFSectionMap LocDWOSection; 1508 DWARFSectionMap StrOffsetsDWOSection; 1509 DWARFSectionMap RangesDWOSection; 1510 DWARFSectionMap RnglistsDWOSection; 1511 DWARFSectionMap AddrSection; 1512 DWARFSectionMap AppleNamesSection; 1513 DWARFSectionMap AppleTypesSection; 1514 DWARFSectionMap AppleNamespacesSection; 1515 DWARFSectionMap AppleObjCSection; 1516 DWARFSectionMap NamesSection; 1517 DWARFSectionMap PubnamesSection; 1518 DWARFSectionMap PubtypesSection; 1519 DWARFSectionMap GnuPubnamesSection; 1520 DWARFSectionMap GnuPubtypesSection; 1521 DWARFSectionMap MacroSection; 1522 1523 DWARFSectionMap *mapNameToDWARFSection(StringRef Name) { 1524 return StringSwitch<DWARFSectionMap *>(Name) 1525 .Case("debug_loc", &LocSection) 1526 .Case("debug_loclists", &LoclistsSection) 1527 .Case("debug_loclists.dwo", &LoclistsDWOSection) 1528 .Case("debug_line", &LineSection) 1529 .Case("debug_frame", &FrameSection) 1530 .Case("eh_frame", &EHFrameSection) 1531 .Case("debug_str_offsets", &StrOffsetsSection) 1532 .Case("debug_ranges", &RangesSection) 1533 .Case("debug_rnglists", &RnglistsSection) 1534 .Case("debug_loc.dwo", &LocDWOSection) 1535 .Case("debug_line.dwo", &LineDWOSection) 1536 .Case("debug_names", &NamesSection) 1537 .Case("debug_rnglists.dwo", &RnglistsDWOSection) 1538 .Case("debug_str_offsets.dwo", &StrOffsetsDWOSection) 1539 .Case("debug_addr", &AddrSection) 1540 .Case("apple_names", &AppleNamesSection) 1541 .Case("debug_pubnames", &PubnamesSection) 1542 .Case("debug_pubtypes", &PubtypesSection) 1543 .Case("debug_gnu_pubnames", &GnuPubnamesSection) 1544 .Case("debug_gnu_pubtypes", &GnuPubtypesSection) 1545 .Case("apple_types", &AppleTypesSection) 1546 .Case("apple_namespaces", &AppleNamespacesSection) 1547 .Case("apple_namespac", &AppleNamespacesSection) 1548 .Case("apple_objc", &AppleObjCSection) 1549 .Case("debug_macro", &MacroSection) 1550 .Default(nullptr); 1551 } 1552 1553 StringRef AbbrevSection; 1554 StringRef ArangesSection; 1555 StringRef StrSection; 1556 StringRef MacinfoSection; 1557 StringRef MacinfoDWOSection; 1558 StringRef MacroDWOSection; 1559 StringRef AbbrevDWOSection; 1560 StringRef StrDWOSection; 1561 StringRef CUIndexSection; 1562 StringRef GdbIndexSection; 1563 StringRef TUIndexSection; 1564 StringRef LineStrSection; 1565 1566 // A deque holding section data whose iterators are not invalidated when 1567 // new decompressed sections are inserted at the end. 1568 std::deque<SmallString<0>> UncompressedSections; 1569 1570 StringRef *mapSectionToMember(StringRef Name) { 1571 if (DWARFSection *Sec = mapNameToDWARFSection(Name)) 1572 return &Sec->Data; 1573 return StringSwitch<StringRef *>(Name) 1574 .Case("debug_abbrev", &AbbrevSection) 1575 .Case("debug_aranges", &ArangesSection) 1576 .Case("debug_str", &StrSection) 1577 .Case("debug_macinfo", &MacinfoSection) 1578 .Case("debug_macinfo.dwo", &MacinfoDWOSection) 1579 .Case("debug_macro.dwo", &MacroDWOSection) 1580 .Case("debug_abbrev.dwo", &AbbrevDWOSection) 1581 .Case("debug_str.dwo", &StrDWOSection) 1582 .Case("debug_cu_index", &CUIndexSection) 1583 .Case("debug_tu_index", &TUIndexSection) 1584 .Case("gdb_index", &GdbIndexSection) 1585 .Case("debug_line_str", &LineStrSection) 1586 // Any more debug info sections go here. 1587 .Default(nullptr); 1588 } 1589 1590 /// If Sec is compressed section, decompresses and updates its contents 1591 /// provided by Data. Otherwise leaves it unchanged. 1592 Error maybeDecompress(const object::SectionRef &Sec, StringRef Name, 1593 StringRef &Data) { 1594 if (!Decompressor::isCompressed(Sec)) 1595 return Error::success(); 1596 1597 Expected<Decompressor> Decompressor = 1598 Decompressor::create(Name, Data, IsLittleEndian, AddressSize == 8); 1599 if (!Decompressor) 1600 return Decompressor.takeError(); 1601 1602 SmallString<0> Out; 1603 if (auto Err = Decompressor->resizeAndDecompress(Out)) 1604 return Err; 1605 1606 UncompressedSections.push_back(std::move(Out)); 1607 Data = UncompressedSections.back(); 1608 1609 return Error::success(); 1610 } 1611 1612 public: 1613 DWARFObjInMemory(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections, 1614 uint8_t AddrSize, bool IsLittleEndian) 1615 : IsLittleEndian(IsLittleEndian) { 1616 for (const auto &SecIt : Sections) { 1617 if (StringRef *SectionData = mapSectionToMember(SecIt.first())) 1618 *SectionData = SecIt.second->getBuffer(); 1619 else if (SecIt.first() == "debug_info") 1620 // Find debug_info and debug_types data by section rather than name as 1621 // there are multiple, comdat grouped, of these sections. 1622 InfoSections[SectionRef()].Data = SecIt.second->getBuffer(); 1623 else if (SecIt.first() == "debug_info.dwo") 1624 InfoDWOSections[SectionRef()].Data = SecIt.second->getBuffer(); 1625 else if (SecIt.first() == "debug_types") 1626 TypesSections[SectionRef()].Data = SecIt.second->getBuffer(); 1627 else if (SecIt.first() == "debug_types.dwo") 1628 TypesDWOSections[SectionRef()].Data = SecIt.second->getBuffer(); 1629 } 1630 } 1631 DWARFObjInMemory(const object::ObjectFile &Obj, const LoadedObjectInfo *L, 1632 function_ref<void(Error)> HandleError, function_ref<void(Error)> HandleWarning ) 1633 : IsLittleEndian(Obj.isLittleEndian()), 1634 AddressSize(Obj.getBytesInAddress()), FileName(Obj.getFileName()), 1635 Obj(&Obj) { 1636 1637 StringMap<unsigned> SectionAmountMap; 1638 for (const SectionRef &Section : Obj.sections()) { 1639 StringRef Name; 1640 if (auto NameOrErr = Section.getName()) 1641 Name = *NameOrErr; 1642 else 1643 consumeError(NameOrErr.takeError()); 1644 1645 ++SectionAmountMap[Name]; 1646 SectionNames.push_back({ Name, true }); 1647 1648 // Skip BSS and Virtual sections, they aren't interesting. 1649 if (Section.isBSS() || Section.isVirtual()) 1650 continue; 1651 1652 // Skip sections stripped by dsymutil. 1653 if (Section.isStripped()) 1654 continue; 1655 1656 StringRef Data; 1657 Expected<section_iterator> SecOrErr = Section.getRelocatedSection(); 1658 if (!SecOrErr) { 1659 HandleError(createError("failed to get relocated section: ", 1660 SecOrErr.takeError())); 1661 continue; 1662 } 1663 1664 // Try to obtain an already relocated version of this section. 1665 // Else use the unrelocated section from the object file. We'll have to 1666 // apply relocations ourselves later. 1667 section_iterator RelocatedSection = *SecOrErr; 1668 if (!L || !L->getLoadedSectionContents(*RelocatedSection, Data)) { 1669 Expected<StringRef> E = Section.getContents(); 1670 if (E) 1671 Data = *E; 1672 else 1673 // maybeDecompress below will error. 1674 consumeError(E.takeError()); 1675 } 1676 1677 if (auto Err = maybeDecompress(Section, Name, Data)) { 1678 HandleError(createError("failed to decompress '" + Name + "', ", 1679 std::move(Err))); 1680 continue; 1681 } 1682 1683 // Compressed sections names in GNU style starts from ".z", 1684 // at this point section is decompressed and we drop compression prefix. 1685 Name = Name.substr( 1686 Name.find_first_not_of("._z")); // Skip ".", "z" and "_" prefixes. 1687 1688 // Map platform specific debug section names to DWARF standard section 1689 // names. 1690 Name = Obj.mapDebugSectionName(Name); 1691 1692 if (StringRef *SectionData = mapSectionToMember(Name)) { 1693 *SectionData = Data; 1694 if (Name == "debug_ranges") { 1695 // FIXME: Use the other dwo range section when we emit it. 1696 RangesDWOSection.Data = Data; 1697 } 1698 } else if (Name == "debug_info") { 1699 // Find debug_info and debug_types data by section rather than name as 1700 // there are multiple, comdat grouped, of these sections. 1701 InfoSections[Section].Data = Data; 1702 } else if (Name == "debug_info.dwo") { 1703 InfoDWOSections[Section].Data = Data; 1704 } else if (Name == "debug_types") { 1705 TypesSections[Section].Data = Data; 1706 } else if (Name == "debug_types.dwo") { 1707 TypesDWOSections[Section].Data = Data; 1708 } 1709 1710 if (RelocatedSection == Obj.section_end()) 1711 continue; 1712 1713 StringRef RelSecName; 1714 if (auto NameOrErr = RelocatedSection->getName()) 1715 RelSecName = *NameOrErr; 1716 else 1717 consumeError(NameOrErr.takeError()); 1718 1719 // If the section we're relocating was relocated already by the JIT, 1720 // then we used the relocated version above, so we do not need to process 1721 // relocations for it now. 1722 StringRef RelSecData; 1723 if (L && L->getLoadedSectionContents(*RelocatedSection, RelSecData)) 1724 continue; 1725 1726 // In Mach-o files, the relocations do not need to be applied if 1727 // there is no load offset to apply. The value read at the 1728 // relocation point already factors in the section address 1729 // (actually applying the relocations will produce wrong results 1730 // as the section address will be added twice). 1731 if (!L && isa<MachOObjectFile>(&Obj)) 1732 continue; 1733 1734 RelSecName = RelSecName.substr( 1735 RelSecName.find_first_not_of("._z")); // Skip . and _ prefixes. 1736 1737 // TODO: Add support for relocations in other sections as needed. 1738 // Record relocations for the debug_info and debug_line sections. 1739 DWARFSectionMap *Sec = mapNameToDWARFSection(RelSecName); 1740 RelocAddrMap *Map = Sec ? &Sec->Relocs : nullptr; 1741 if (!Map) { 1742 // Find debug_info and debug_types relocs by section rather than name 1743 // as there are multiple, comdat grouped, of these sections. 1744 if (RelSecName == "debug_info") 1745 Map = &static_cast<DWARFSectionMap &>(InfoSections[*RelocatedSection]) 1746 .Relocs; 1747 else if (RelSecName == "debug_info.dwo") 1748 Map = &static_cast<DWARFSectionMap &>( 1749 InfoDWOSections[*RelocatedSection]) 1750 .Relocs; 1751 else if (RelSecName == "debug_types") 1752 Map = 1753 &static_cast<DWARFSectionMap &>(TypesSections[*RelocatedSection]) 1754 .Relocs; 1755 else if (RelSecName == "debug_types.dwo") 1756 Map = &static_cast<DWARFSectionMap &>( 1757 TypesDWOSections[*RelocatedSection]) 1758 .Relocs; 1759 else 1760 continue; 1761 } 1762 1763 if (Section.relocation_begin() == Section.relocation_end()) 1764 continue; 1765 1766 // Symbol to [address, section index] cache mapping. 1767 std::map<SymbolRef, SymInfo> AddrCache; 1768 bool (*Supports)(uint64_t); 1769 RelocationResolver Resolver; 1770 std::tie(Supports, Resolver) = getRelocationResolver(Obj); 1771 for (const RelocationRef &Reloc : Section.relocations()) { 1772 // FIXME: it's not clear how to correctly handle scattered 1773 // relocations. 1774 if (isRelocScattered(Obj, Reloc)) 1775 continue; 1776 1777 Expected<SymInfo> SymInfoOrErr = 1778 getSymbolInfo(Obj, Reloc, L, AddrCache); 1779 if (!SymInfoOrErr) { 1780 HandleError(SymInfoOrErr.takeError()); 1781 continue; 1782 } 1783 1784 // Check if Resolver can handle this relocation type early so as not to 1785 // handle invalid cases in DWARFDataExtractor. 1786 // 1787 // TODO Don't store Resolver in every RelocAddrEntry. 1788 if (Supports && Supports(Reloc.getType())) { 1789 auto I = Map->try_emplace( 1790 Reloc.getOffset(), 1791 RelocAddrEntry{SymInfoOrErr->SectionIndex, Reloc, 1792 SymInfoOrErr->Address, 1793 Optional<object::RelocationRef>(), 0, Resolver}); 1794 // If we didn't successfully insert that's because we already had a 1795 // relocation for that offset. Store it as a second relocation in the 1796 // same RelocAddrEntry instead. 1797 if (!I.second) { 1798 RelocAddrEntry &entry = I.first->getSecond(); 1799 if (entry.Reloc2) { 1800 HandleError(createError( 1801 "At most two relocations per offset are supported")); 1802 } 1803 entry.Reloc2 = Reloc; 1804 entry.SymbolValue2 = SymInfoOrErr->Address; 1805 } 1806 } else { 1807 SmallString<32> Type; 1808 Reloc.getTypeName(Type); 1809 // FIXME: Support more relocations & change this to an error 1810 HandleWarning( 1811 createError("failed to compute relocation: " + Type + ", ", 1812 errorCodeToError(object_error::parse_failed))); 1813 } 1814 } 1815 } 1816 1817 for (SectionName &S : SectionNames) 1818 if (SectionAmountMap[S.Name] > 1) 1819 S.IsNameUnique = false; 1820 } 1821 1822 Optional<RelocAddrEntry> find(const DWARFSection &S, 1823 uint64_t Pos) const override { 1824 auto &Sec = static_cast<const DWARFSectionMap &>(S); 1825 RelocAddrMap::const_iterator AI = Sec.Relocs.find(Pos); 1826 if (AI == Sec.Relocs.end()) 1827 return None; 1828 return AI->second; 1829 } 1830 1831 const object::ObjectFile *getFile() const override { return Obj; } 1832 1833 ArrayRef<SectionName> getSectionNames() const override { 1834 return SectionNames; 1835 } 1836 1837 bool isLittleEndian() const override { return IsLittleEndian; } 1838 StringRef getAbbrevDWOSection() const override { return AbbrevDWOSection; } 1839 const DWARFSection &getLineDWOSection() const override { 1840 return LineDWOSection; 1841 } 1842 const DWARFSection &getLocDWOSection() const override { 1843 return LocDWOSection; 1844 } 1845 StringRef getStrDWOSection() const override { return StrDWOSection; } 1846 const DWARFSection &getStrOffsetsDWOSection() const override { 1847 return StrOffsetsDWOSection; 1848 } 1849 const DWARFSection &getRangesDWOSection() const override { 1850 return RangesDWOSection; 1851 } 1852 const DWARFSection &getRnglistsDWOSection() const override { 1853 return RnglistsDWOSection; 1854 } 1855 const DWARFSection &getLoclistsDWOSection() const override { 1856 return LoclistsDWOSection; 1857 } 1858 const DWARFSection &getAddrSection() const override { return AddrSection; } 1859 StringRef getCUIndexSection() const override { return CUIndexSection; } 1860 StringRef getGdbIndexSection() const override { return GdbIndexSection; } 1861 StringRef getTUIndexSection() const override { return TUIndexSection; } 1862 1863 // DWARF v5 1864 const DWARFSection &getStrOffsetsSection() const override { 1865 return StrOffsetsSection; 1866 } 1867 StringRef getLineStrSection() const override { return LineStrSection; } 1868 1869 // Sections for DWARF5 split dwarf proposal. 1870 void forEachInfoDWOSections( 1871 function_ref<void(const DWARFSection &)> F) const override { 1872 for (auto &P : InfoDWOSections) 1873 F(P.second); 1874 } 1875 void forEachTypesDWOSections( 1876 function_ref<void(const DWARFSection &)> F) const override { 1877 for (auto &P : TypesDWOSections) 1878 F(P.second); 1879 } 1880 1881 StringRef getAbbrevSection() const override { return AbbrevSection; } 1882 const DWARFSection &getLocSection() const override { return LocSection; } 1883 const DWARFSection &getLoclistsSection() const override { return LoclistsSection; } 1884 StringRef getArangesSection() const override { return ArangesSection; } 1885 const DWARFSection &getFrameSection() const override { 1886 return FrameSection; 1887 } 1888 const DWARFSection &getEHFrameSection() const override { 1889 return EHFrameSection; 1890 } 1891 const DWARFSection &getLineSection() const override { return LineSection; } 1892 StringRef getStrSection() const override { return StrSection; } 1893 const DWARFSection &getRangesSection() const override { return RangesSection; } 1894 const DWARFSection &getRnglistsSection() const override { 1895 return RnglistsSection; 1896 } 1897 const DWARFSection &getMacroSection() const override { return MacroSection; } 1898 StringRef getMacroDWOSection() const override { return MacroDWOSection; } 1899 StringRef getMacinfoSection() const override { return MacinfoSection; } 1900 StringRef getMacinfoDWOSection() const override { return MacinfoDWOSection; } 1901 const DWARFSection &getPubnamesSection() const override { return PubnamesSection; } 1902 const DWARFSection &getPubtypesSection() const override { return PubtypesSection; } 1903 const DWARFSection &getGnuPubnamesSection() const override { 1904 return GnuPubnamesSection; 1905 } 1906 const DWARFSection &getGnuPubtypesSection() const override { 1907 return GnuPubtypesSection; 1908 } 1909 const DWARFSection &getAppleNamesSection() const override { 1910 return AppleNamesSection; 1911 } 1912 const DWARFSection &getAppleTypesSection() const override { 1913 return AppleTypesSection; 1914 } 1915 const DWARFSection &getAppleNamespacesSection() const override { 1916 return AppleNamespacesSection; 1917 } 1918 const DWARFSection &getAppleObjCSection() const override { 1919 return AppleObjCSection; 1920 } 1921 const DWARFSection &getNamesSection() const override { 1922 return NamesSection; 1923 } 1924 1925 StringRef getFileName() const override { return FileName; } 1926 uint8_t getAddressSize() const override { return AddressSize; } 1927 void forEachInfoSections( 1928 function_ref<void(const DWARFSection &)> F) const override { 1929 for (auto &P : InfoSections) 1930 F(P.second); 1931 } 1932 void forEachTypesSections( 1933 function_ref<void(const DWARFSection &)> F) const override { 1934 for (auto &P : TypesSections) 1935 F(P.second); 1936 } 1937 }; 1938 } // namespace 1939 1940 std::unique_ptr<DWARFContext> 1941 DWARFContext::create(const object::ObjectFile &Obj, const LoadedObjectInfo *L, 1942 std::string DWPName, 1943 std::function<void(Error)> RecoverableErrorHandler, 1944 std::function<void(Error)> WarningHandler) { 1945 auto DObj = 1946 std::make_unique<DWARFObjInMemory>(Obj, L, RecoverableErrorHandler, WarningHandler); 1947 return std::make_unique<DWARFContext>(std::move(DObj), std::move(DWPName), 1948 RecoverableErrorHandler, 1949 WarningHandler); 1950 } 1951 1952 std::unique_ptr<DWARFContext> 1953 DWARFContext::create(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections, 1954 uint8_t AddrSize, bool isLittleEndian, 1955 std::function<void(Error)> RecoverableErrorHandler, 1956 std::function<void(Error)> WarningHandler) { 1957 auto DObj = 1958 std::make_unique<DWARFObjInMemory>(Sections, AddrSize, isLittleEndian); 1959 return std::make_unique<DWARFContext>( 1960 std::move(DObj), "", RecoverableErrorHandler, WarningHandler); 1961 } 1962 1963 Error DWARFContext::loadRegisterInfo(const object::ObjectFile &Obj) { 1964 // Detect the architecture from the object file. We usually don't need OS 1965 // info to lookup a target and create register info. 1966 Triple TT; 1967 TT.setArch(Triple::ArchType(Obj.getArch())); 1968 TT.setVendor(Triple::UnknownVendor); 1969 TT.setOS(Triple::UnknownOS); 1970 std::string TargetLookupError; 1971 const Target *TheTarget = 1972 TargetRegistry::lookupTarget(TT.str(), TargetLookupError); 1973 if (!TargetLookupError.empty()) 1974 return createStringError(errc::invalid_argument, 1975 TargetLookupError.c_str()); 1976 RegInfo.reset(TheTarget->createMCRegInfo(TT.str())); 1977 return Error::success(); 1978 } 1979 1980 uint8_t DWARFContext::getCUAddrSize() { 1981 // In theory, different compile units may have different address byte 1982 // sizes, but for simplicity we just use the address byte size of the 1983 // first compile unit. In practice the address size field is repeated across 1984 // various DWARF headers (at least in version 5) to make it easier to dump 1985 // them independently, not to enable varying the address size. 1986 unit_iterator_range CUs = compile_units(); 1987 return CUs.empty() ? 0 : (*CUs.begin())->getAddressByteSize(); 1988 } 1989