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