1 //===- DWARFContext.cpp ---------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/DebugInfo/DWARF/DWARFContext.h" 11 #include "llvm/ADT/STLExtras.h" 12 #include "llvm/ADT/SmallString.h" 13 #include "llvm/ADT/SmallVector.h" 14 #include "llvm/ADT/StringRef.h" 15 #include "llvm/ADT/StringSwitch.h" 16 #include "llvm/BinaryFormat/Dwarf.h" 17 #include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h" 18 #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h" 19 #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.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/RelocVisitor.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/MemoryBuffer.h" 45 #include "llvm/Support/Path.h" 46 #include "llvm/Support/TargetRegistry.h" 47 #include "llvm/Support/WithColor.h" 48 #include "llvm/Support/raw_ostream.h" 49 #include <algorithm> 50 #include <cstdint> 51 #include <map> 52 #include <string> 53 #include <utility> 54 #include <vector> 55 56 using namespace llvm; 57 using namespace dwarf; 58 using namespace object; 59 60 #define DEBUG_TYPE "dwarf" 61 62 using DWARFLineTable = DWARFDebugLine::LineTable; 63 using FileLineInfoKind = DILineInfoSpecifier::FileLineInfoKind; 64 using FunctionNameKind = DILineInfoSpecifier::FunctionNameKind; 65 66 DWARFContext::DWARFContext(std::unique_ptr<const DWARFObject> DObj, 67 std::string DWPName) 68 : DIContext(CK_DWARF), DWPName(std::move(DWPName)), DObj(std::move(DObj)) {} 69 70 DWARFContext::~DWARFContext() = default; 71 72 /// Dump the UUID load command. 73 static void dumpUUID(raw_ostream &OS, const ObjectFile &Obj) { 74 auto *MachO = dyn_cast<MachOObjectFile>(&Obj); 75 if (!MachO) 76 return; 77 for (auto LC : MachO->load_commands()) { 78 raw_ostream::uuid_t UUID; 79 if (LC.C.cmd == MachO::LC_UUID) { 80 if (LC.C.cmdsize < sizeof(UUID) + sizeof(LC.C)) { 81 OS << "error: UUID load command is too short.\n"; 82 return; 83 } 84 OS << "UUID: "; 85 memcpy(&UUID, LC.Ptr+sizeof(LC.C), sizeof(UUID)); 86 OS.write_uuid(UUID); 87 Triple T = MachO->getArchTriple(); 88 OS << " (" << T.getArchName() << ')'; 89 OS << ' ' << MachO->getFileName() << '\n'; 90 } 91 } 92 } 93 94 using ContributionCollection = 95 std::vector<Optional<StrOffsetsContributionDescriptor>>; 96 97 // Collect all the contributions to the string offsets table from all units, 98 // sort them by their starting offsets and remove duplicates. 99 static ContributionCollection 100 collectContributionData(DWARFContext::cu_iterator_range CUs, 101 DWARFContext::tu_section_iterator_range TUSs) { 102 ContributionCollection Contributions; 103 for (const auto &CU : CUs) 104 Contributions.push_back(CU->getStringOffsetsTableContribution()); 105 for (const auto &TUS : TUSs) 106 for (const auto &TU : TUS) 107 Contributions.push_back(TU->getStringOffsetsTableContribution()); 108 109 // Sort the contributions so that any invalid ones are placed at 110 // the start of the contributions vector. This way they are reported 111 // first. 112 llvm::sort(Contributions.begin(), Contributions.end(), 113 [](const Optional<StrOffsetsContributionDescriptor> &L, 114 const Optional<StrOffsetsContributionDescriptor> &R) { 115 if (L && R) return L->Base < R->Base; 116 return R.hasValue(); 117 }); 118 119 // Uniquify contributions, as it is possible that units (specifically 120 // type units in dwo or dwp files) share contributions. We don't want 121 // to report them more than once. 122 Contributions.erase( 123 std::unique(Contributions.begin(), Contributions.end(), 124 [](const Optional<StrOffsetsContributionDescriptor> &L, 125 const Optional<StrOffsetsContributionDescriptor> &R) { 126 if (L && R) 127 return L->Base == R->Base && L->Size == R->Size; 128 return false; 129 }), 130 Contributions.end()); 131 return Contributions; 132 } 133 134 static void dumpDWARFv5StringOffsetsSection( 135 raw_ostream &OS, StringRef SectionName, const DWARFObject &Obj, 136 const DWARFSection &StringOffsetsSection, StringRef StringSection, 137 DWARFContext::cu_iterator_range CUs, 138 DWARFContext::tu_section_iterator_range TUSs, bool LittleEndian) { 139 auto Contributions = collectContributionData(CUs, TUSs); 140 DWARFDataExtractor StrOffsetExt(Obj, StringOffsetsSection, LittleEndian, 0); 141 DataExtractor StrData(StringSection, LittleEndian, 0); 142 uint64_t SectionSize = StringOffsetsSection.Data.size(); 143 uint32_t Offset = 0; 144 for (auto &Contribution : Contributions) { 145 // Report an ill-formed contribution. 146 if (!Contribution) { 147 OS << "error: invalid contribution to string offsets table in section ." 148 << SectionName << ".\n"; 149 return; 150 } 151 152 dwarf::DwarfFormat Format = Contribution->getFormat(); 153 uint16_t Version = Contribution->getVersion(); 154 uint64_t ContributionHeader = Contribution->Base; 155 // In DWARF v5 there is a contribution header that immediately precedes 156 // the string offsets base (the location we have previously retrieved from 157 // the CU DIE's DW_AT_str_offsets attribute). The header is located either 158 // 8 or 16 bytes before the base, depending on the contribution's format. 159 if (Version >= 5) 160 ContributionHeader -= Format == DWARF32 ? 8 : 16; 161 162 // Detect overlapping contributions. 163 if (Offset > ContributionHeader) { 164 OS << "error: overlapping contributions to string offsets table in " 165 "section ." 166 << SectionName << ".\n"; 167 return; 168 } 169 // Report a gap in the table. 170 if (Offset < ContributionHeader) { 171 OS << format("0x%8.8x: Gap, length = ", Offset); 172 OS << (ContributionHeader - Offset) << "\n"; 173 } 174 OS << format("0x%8.8x: ", (uint32_t)ContributionHeader); 175 OS << "Contribution size = " << Contribution->Size 176 << ", Format = " << (Format == DWARF32 ? "DWARF32" : "DWARF64") 177 << ", Version = " << Version << "\n"; 178 179 Offset = Contribution->Base; 180 unsigned EntrySize = Contribution->getDwarfOffsetByteSize(); 181 while (Offset - Contribution->Base < Contribution->Size) { 182 OS << format("0x%8.8x: ", Offset); 183 // FIXME: We can only extract strings if the offset fits in 32 bits. 184 uint64_t StringOffset = 185 StrOffsetExt.getRelocatedValue(EntrySize, &Offset); 186 // Extract the string if we can and display it. Otherwise just report 187 // the offset. 188 if (StringOffset <= std::numeric_limits<uint32_t>::max()) { 189 uint32_t StringOffset32 = (uint32_t)StringOffset; 190 OS << format("%8.8x ", StringOffset32); 191 const char *S = StrData.getCStr(&StringOffset32); 192 if (S) 193 OS << format("\"%s\"", S); 194 } else 195 OS << format("%16.16" PRIx64 " ", StringOffset); 196 OS << "\n"; 197 } 198 } 199 // Report a gap at the end of the table. 200 if (Offset < SectionSize) { 201 OS << format("0x%8.8x: Gap, length = ", Offset); 202 OS << (SectionSize - Offset) << "\n"; 203 } 204 } 205 206 // Dump a DWARF string offsets section. This may be a DWARF v5 formatted 207 // string offsets section, where each compile or type unit contributes a 208 // number of entries (string offsets), with each contribution preceded by 209 // a header containing size and version number. Alternatively, it may be a 210 // monolithic series of string offsets, as generated by the pre-DWARF v5 211 // implementation of split DWARF. 212 static void dumpStringOffsetsSection( 213 raw_ostream &OS, StringRef SectionName, const DWARFObject &Obj, 214 const DWARFSection &StringOffsetsSection, StringRef StringSection, 215 DWARFContext::cu_iterator_range CUs, 216 DWARFContext::tu_section_iterator_range TUSs, bool LittleEndian, 217 unsigned MaxVersion) { 218 // If we have at least one (compile or type) unit with DWARF v5 or greater, 219 // we assume that the section is formatted like a DWARF v5 string offsets 220 // section. 221 if (MaxVersion >= 5) 222 dumpDWARFv5StringOffsetsSection(OS, SectionName, Obj, StringOffsetsSection, 223 StringSection, CUs, TUSs, LittleEndian); 224 else { 225 DataExtractor strOffsetExt(StringOffsetsSection.Data, LittleEndian, 0); 226 uint32_t offset = 0; 227 uint64_t size = StringOffsetsSection.Data.size(); 228 // Ensure that size is a multiple of the size of an entry. 229 if (size & ((uint64_t)(sizeof(uint32_t) - 1))) { 230 OS << "error: size of ." << SectionName << " is not a multiple of " 231 << sizeof(uint32_t) << ".\n"; 232 size &= -(uint64_t)sizeof(uint32_t); 233 } 234 DataExtractor StrData(StringSection, LittleEndian, 0); 235 while (offset < size) { 236 OS << format("0x%8.8x: ", offset); 237 uint32_t StringOffset = strOffsetExt.getU32(&offset); 238 OS << format("%8.8x ", StringOffset); 239 const char *S = StrData.getCStr(&StringOffset); 240 if (S) 241 OS << format("\"%s\"", S); 242 OS << "\n"; 243 } 244 } 245 } 246 247 void DWARFContext::dump( 248 raw_ostream &OS, DIDumpOptions DumpOpts, 249 std::array<Optional<uint64_t>, DIDT_ID_Count> DumpOffsets) { 250 251 Optional<uint64_t> DumpOffset; 252 uint64_t DumpType = DumpOpts.DumpType; 253 254 StringRef Extension = sys::path::extension(DObj->getFileName()); 255 bool IsDWO = (Extension == ".dwo") || (Extension == ".dwp"); 256 257 // Print UUID header. 258 const auto *ObjFile = DObj->getFile(); 259 if (DumpType & DIDT_UUID) 260 dumpUUID(OS, *ObjFile); 261 262 // Print a header for each explicitly-requested section. 263 // Otherwise just print one for non-empty sections. 264 // Only print empty .dwo section headers when dumping a .dwo file. 265 bool Explicit = DumpType != DIDT_All && !IsDWO; 266 bool ExplicitDWO = Explicit && IsDWO; 267 auto shouldDump = [&](bool Explicit, const char *Name, unsigned ID, 268 StringRef Section) { 269 DumpOffset = DumpOffsets[ID]; 270 unsigned Mask = 1U << ID; 271 bool Should = (DumpType & Mask) && (Explicit || !Section.empty()); 272 if (Should) 273 OS << "\n" << Name << " contents:\n"; 274 return Should; 275 }; 276 277 // Dump individual sections. 278 if (shouldDump(Explicit, ".debug_abbrev", DIDT_ID_DebugAbbrev, 279 DObj->getAbbrevSection())) 280 getDebugAbbrev()->dump(OS); 281 if (shouldDump(ExplicitDWO, ".debug_abbrev.dwo", DIDT_ID_DebugAbbrev, 282 DObj->getAbbrevDWOSection())) 283 getDebugAbbrevDWO()->dump(OS); 284 285 auto dumpDebugInfo = [&](bool IsExplicit, const char *Name, 286 DWARFSection Section, cu_iterator_range CUs) { 287 if (shouldDump(IsExplicit, Name, DIDT_ID_DebugInfo, Section.Data)) { 288 if (DumpOffset) 289 getDIEForOffset(DumpOffset.getValue()) 290 .dump(OS, 0, DumpOpts.noImplicitRecursion()); 291 else 292 for (const auto &CU : CUs) 293 CU->dump(OS, DumpOpts); 294 } 295 }; 296 dumpDebugInfo(Explicit, ".debug_info", DObj->getInfoSection(), 297 compile_units()); 298 dumpDebugInfo(ExplicitDWO, ".debug_info.dwo", DObj->getInfoDWOSection(), 299 dwo_compile_units()); 300 301 auto dumpDebugType = [&](const char *Name, 302 tu_section_iterator_range TUSections) { 303 OS << '\n' << Name << " contents:\n"; 304 DumpOffset = DumpOffsets[DIDT_ID_DebugTypes]; 305 for (const auto &TUS : TUSections) 306 for (const auto &TU : TUS) 307 if (DumpOffset) 308 TU->getDIEForOffset(*DumpOffset) 309 .dump(OS, 0, DumpOpts.noImplicitRecursion()); 310 else 311 TU->dump(OS, DumpOpts); 312 }; 313 if ((DumpType & DIDT_DebugTypes)) { 314 if (Explicit || getNumTypeUnits()) 315 dumpDebugType(".debug_types", type_unit_sections()); 316 if (ExplicitDWO || getNumDWOTypeUnits()) 317 dumpDebugType(".debug_types.dwo", dwo_type_unit_sections()); 318 } 319 320 if (shouldDump(Explicit, ".debug_loc", DIDT_ID_DebugLoc, 321 DObj->getLocSection().Data)) { 322 getDebugLoc()->dump(OS, getRegisterInfo(), DumpOffset); 323 } 324 if (shouldDump(ExplicitDWO, ".debug_loc.dwo", DIDT_ID_DebugLoc, 325 DObj->getLocDWOSection().Data)) { 326 getDebugLocDWO()->dump(OS, getRegisterInfo(), DumpOffset); 327 } 328 329 if (shouldDump(Explicit, ".debug_frame", DIDT_ID_DebugFrame, 330 DObj->getDebugFrameSection())) 331 getDebugFrame()->dump(OS, getRegisterInfo(), DumpOffset); 332 333 if (shouldDump(Explicit, ".eh_frame", DIDT_ID_DebugFrame, 334 DObj->getEHFrameSection())) 335 getEHFrame()->dump(OS, getRegisterInfo(), DumpOffset); 336 337 if (DumpType & DIDT_DebugMacro) { 338 if (Explicit || !getDebugMacro()->empty()) { 339 OS << "\n.debug_macinfo contents:\n"; 340 getDebugMacro()->dump(OS); 341 } 342 } 343 344 if (shouldDump(Explicit, ".debug_aranges", DIDT_ID_DebugAranges, 345 DObj->getARangeSection())) { 346 uint32_t offset = 0; 347 DataExtractor arangesData(DObj->getARangeSection(), isLittleEndian(), 0); 348 DWARFDebugArangeSet set; 349 while (set.extract(arangesData, &offset)) 350 set.dump(OS); 351 } 352 353 auto DumpLineSection = [&](DWARFDebugLine::SectionParser Parser, 354 DIDumpOptions DumpOpts) { 355 while (!Parser.done()) { 356 if (DumpOffset && Parser.getOffset() != *DumpOffset) { 357 Parser.skip(); 358 continue; 359 } 360 OS << "debug_line[" << format("0x%8.8x", Parser.getOffset()) << "]\n"; 361 if (DumpOpts.Verbose) { 362 Parser.parseNext(DWARFDebugLine::warn, DWARFDebugLine::warnForError, 363 &OS); 364 } else { 365 DWARFDebugLine::LineTable LineTable = Parser.parseNext(); 366 LineTable.dump(OS, DumpOpts); 367 } 368 } 369 }; 370 371 if (shouldDump(Explicit, ".debug_line", DIDT_ID_DebugLine, 372 DObj->getLineSection().Data)) { 373 DWARFDataExtractor LineData(*DObj, DObj->getLineSection(), isLittleEndian(), 374 0); 375 DWARFDebugLine::SectionParser Parser(LineData, *this, compile_units(), 376 type_unit_sections()); 377 DumpLineSection(Parser, DumpOpts); 378 } 379 380 if (shouldDump(ExplicitDWO, ".debug_line.dwo", DIDT_ID_DebugLine, 381 DObj->getLineDWOSection().Data)) { 382 DWARFDataExtractor LineData(*DObj, DObj->getLineDWOSection(), 383 isLittleEndian(), 0); 384 DWARFDebugLine::SectionParser Parser(LineData, *this, dwo_compile_units(), 385 dwo_type_unit_sections()); 386 DumpLineSection(Parser, DumpOpts); 387 } 388 389 if (shouldDump(Explicit, ".debug_cu_index", DIDT_ID_DebugCUIndex, 390 DObj->getCUIndexSection())) { 391 getCUIndex().dump(OS); 392 } 393 394 if (shouldDump(Explicit, ".debug_tu_index", DIDT_ID_DebugTUIndex, 395 DObj->getTUIndexSection())) { 396 getTUIndex().dump(OS); 397 } 398 399 if (shouldDump(Explicit, ".debug_str", DIDT_ID_DebugStr, 400 DObj->getStringSection())) { 401 DataExtractor strData(DObj->getStringSection(), isLittleEndian(), 0); 402 uint32_t offset = 0; 403 uint32_t strOffset = 0; 404 while (const char *s = strData.getCStr(&offset)) { 405 OS << format("0x%8.8x: \"%s\"\n", strOffset, s); 406 strOffset = offset; 407 } 408 } 409 if (shouldDump(ExplicitDWO, ".debug_str.dwo", DIDT_ID_DebugStr, 410 DObj->getStringDWOSection())) { 411 DataExtractor strDWOData(DObj->getStringDWOSection(), isLittleEndian(), 0); 412 uint32_t offset = 0; 413 uint32_t strDWOOffset = 0; 414 while (const char *s = strDWOData.getCStr(&offset)) { 415 OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s); 416 strDWOOffset = offset; 417 } 418 } 419 if (shouldDump(Explicit, ".debug_line_str", DIDT_ID_DebugLineStr, 420 DObj->getLineStringSection())) { 421 DataExtractor strData(DObj->getLineStringSection(), isLittleEndian(), 0); 422 uint32_t offset = 0; 423 uint32_t strOffset = 0; 424 while (const char *s = strData.getCStr(&offset)) { 425 OS << format("0x%8.8x: \"", strOffset); 426 OS.write_escaped(s); 427 OS << "\"\n"; 428 strOffset = offset; 429 } 430 } 431 432 if (shouldDump(Explicit, ".debug_ranges", DIDT_ID_DebugRanges, 433 DObj->getRangeSection().Data)) { 434 // In fact, different compile units may have different address byte 435 // sizes, but for simplicity we just use the address byte size of the 436 // last compile unit (there is no easy and fast way to associate address 437 // range list and the compile unit it describes). 438 // FIXME: savedAddressByteSize seems sketchy. 439 uint8_t savedAddressByteSize = 0; 440 for (const auto &CU : compile_units()) { 441 savedAddressByteSize = CU->getAddressByteSize(); 442 break; 443 } 444 DWARFDataExtractor rangesData(*DObj, DObj->getRangeSection(), 445 isLittleEndian(), savedAddressByteSize); 446 uint32_t offset = 0; 447 DWARFDebugRangeList rangeList; 448 while (rangeList.extract(rangesData, &offset)) 449 rangeList.dump(OS); 450 } 451 452 if (shouldDump(Explicit, ".debug_rnglists", DIDT_ID_DebugRnglists, 453 DObj->getRnglistsSection().Data)) { 454 DWARFDataExtractor rnglistData(*DObj, DObj->getRnglistsSection(), 455 isLittleEndian(), 0); 456 uint32_t Offset = 0; 457 while (rnglistData.isValidOffset(Offset)) { 458 DWARFDebugRnglistTable Rnglists; 459 uint32_t TableOffset = Offset; 460 if (Error Err = Rnglists.extract(rnglistData, &Offset)) { 461 WithColor::error() << toString(std::move(Err)) << '\n'; 462 uint64_t Length = Rnglists.length(); 463 // Keep going after an error, if we can, assuming that the length field 464 // could be read. If it couldn't, stop reading the section. 465 if (Length == 0) 466 break; 467 Offset = TableOffset + Length; 468 } else { 469 Rnglists.dump(OS, DumpOpts); 470 } 471 } 472 } 473 474 if (shouldDump(Explicit, ".debug_pubnames", DIDT_ID_DebugPubnames, 475 DObj->getPubNamesSection())) 476 DWARFDebugPubTable(DObj->getPubNamesSection(), isLittleEndian(), false) 477 .dump(OS); 478 479 if (shouldDump(Explicit, ".debug_pubtypes", DIDT_ID_DebugPubtypes, 480 DObj->getPubTypesSection())) 481 DWARFDebugPubTable(DObj->getPubTypesSection(), isLittleEndian(), false) 482 .dump(OS); 483 484 if (shouldDump(Explicit, ".debug_gnu_pubnames", DIDT_ID_DebugGnuPubnames, 485 DObj->getGnuPubNamesSection())) 486 DWARFDebugPubTable(DObj->getGnuPubNamesSection(), isLittleEndian(), 487 true /* GnuStyle */) 488 .dump(OS); 489 490 if (shouldDump(Explicit, ".debug_gnu_pubtypes", DIDT_ID_DebugGnuPubtypes, 491 DObj->getGnuPubTypesSection())) 492 DWARFDebugPubTable(DObj->getGnuPubTypesSection(), isLittleEndian(), 493 true /* GnuStyle */) 494 .dump(OS); 495 496 if (shouldDump(Explicit, ".debug_str_offsets", DIDT_ID_DebugStrOffsets, 497 DObj->getStringOffsetSection().Data)) 498 dumpStringOffsetsSection( 499 OS, "debug_str_offsets", *DObj, DObj->getStringOffsetSection(), 500 DObj->getStringSection(), compile_units(), type_unit_sections(), 501 isLittleEndian(), getMaxVersion()); 502 if (shouldDump(ExplicitDWO, ".debug_str_offsets.dwo", DIDT_ID_DebugStrOffsets, 503 DObj->getStringOffsetDWOSection().Data)) 504 dumpStringOffsetsSection( 505 OS, "debug_str_offsets.dwo", *DObj, DObj->getStringOffsetDWOSection(), 506 DObj->getStringDWOSection(), dwo_compile_units(), 507 dwo_type_unit_sections(), isLittleEndian(), getMaxVersion()); 508 509 if (shouldDump(Explicit, ".gnu_index", DIDT_ID_GdbIndex, 510 DObj->getGdbIndexSection())) { 511 getGdbIndex().dump(OS); 512 } 513 514 if (shouldDump(Explicit, ".apple_names", DIDT_ID_AppleNames, 515 DObj->getAppleNamesSection().Data)) 516 getAppleNames().dump(OS); 517 518 if (shouldDump(Explicit, ".apple_types", DIDT_ID_AppleTypes, 519 DObj->getAppleTypesSection().Data)) 520 getAppleTypes().dump(OS); 521 522 if (shouldDump(Explicit, ".apple_namespaces", DIDT_ID_AppleNamespaces, 523 DObj->getAppleNamespacesSection().Data)) 524 getAppleNamespaces().dump(OS); 525 526 if (shouldDump(Explicit, ".apple_objc", DIDT_ID_AppleObjC, 527 DObj->getAppleObjCSection().Data)) 528 getAppleObjC().dump(OS); 529 if (shouldDump(Explicit, ".debug_names", DIDT_ID_DebugNames, 530 DObj->getDebugNamesSection().Data)) 531 getDebugNames().dump(OS); 532 } 533 534 DWARFCompileUnit *DWARFContext::getDWOCompileUnitForHash(uint64_t Hash) { 535 DWOCUs.parseDWO(*this, DObj->getInfoDWOSection(), true); 536 537 if (const auto &CUI = getCUIndex()) { 538 if (const auto *R = CUI.getFromHash(Hash)) 539 return DWOCUs.getUnitForIndexEntry(*R); 540 return nullptr; 541 } 542 543 // If there's no index, just search through the CUs in the DWO - there's 544 // probably only one unless this is something like LTO - though an in-process 545 // built/cached lookup table could be used in that case to improve repeated 546 // lookups of different CUs in the DWO. 547 for (const auto &DWOCU : dwo_compile_units()) 548 if (DWOCU->getDWOId() == Hash) 549 return DWOCU.get(); 550 return nullptr; 551 } 552 553 DWARFDie DWARFContext::getDIEForOffset(uint32_t Offset) { 554 parseCompileUnits(); 555 if (auto *CU = CUs.getUnitForOffset(Offset)) 556 return CU->getDIEForOffset(Offset); 557 return DWARFDie(); 558 } 559 560 bool DWARFContext::verify(raw_ostream &OS, DIDumpOptions DumpOpts) { 561 bool Success = true; 562 DWARFVerifier verifier(OS, *this, DumpOpts); 563 564 Success &= verifier.handleDebugAbbrev(); 565 if (DumpOpts.DumpType & DIDT_DebugInfo) 566 Success &= verifier.handleDebugInfo(); 567 if (DumpOpts.DumpType & DIDT_DebugLine) 568 Success &= verifier.handleDebugLine(); 569 Success &= verifier.handleAccelTables(); 570 return Success; 571 } 572 573 const DWARFUnitIndex &DWARFContext::getCUIndex() { 574 if (CUIndex) 575 return *CUIndex; 576 577 DataExtractor CUIndexData(DObj->getCUIndexSection(), isLittleEndian(), 0); 578 579 CUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_INFO); 580 CUIndex->parse(CUIndexData); 581 return *CUIndex; 582 } 583 584 const DWARFUnitIndex &DWARFContext::getTUIndex() { 585 if (TUIndex) 586 return *TUIndex; 587 588 DataExtractor TUIndexData(DObj->getTUIndexSection(), isLittleEndian(), 0); 589 590 TUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_TYPES); 591 TUIndex->parse(TUIndexData); 592 return *TUIndex; 593 } 594 595 DWARFGdbIndex &DWARFContext::getGdbIndex() { 596 if (GdbIndex) 597 return *GdbIndex; 598 599 DataExtractor GdbIndexData(DObj->getGdbIndexSection(), true /*LE*/, 0); 600 GdbIndex = llvm::make_unique<DWARFGdbIndex>(); 601 GdbIndex->parse(GdbIndexData); 602 return *GdbIndex; 603 } 604 605 const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() { 606 if (Abbrev) 607 return Abbrev.get(); 608 609 DataExtractor abbrData(DObj->getAbbrevSection(), isLittleEndian(), 0); 610 611 Abbrev.reset(new DWARFDebugAbbrev()); 612 Abbrev->extract(abbrData); 613 return Abbrev.get(); 614 } 615 616 const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() { 617 if (AbbrevDWO) 618 return AbbrevDWO.get(); 619 620 DataExtractor abbrData(DObj->getAbbrevDWOSection(), isLittleEndian(), 0); 621 AbbrevDWO.reset(new DWARFDebugAbbrev()); 622 AbbrevDWO->extract(abbrData); 623 return AbbrevDWO.get(); 624 } 625 626 const DWARFDebugLoc *DWARFContext::getDebugLoc() { 627 if (Loc) 628 return Loc.get(); 629 630 Loc.reset(new DWARFDebugLoc); 631 // Assume all compile units have the same address byte size. 632 if (getNumCompileUnits()) { 633 DWARFDataExtractor LocData(*DObj, DObj->getLocSection(), isLittleEndian(), 634 getCompileUnitAtIndex(0)->getAddressByteSize()); 635 Loc->parse(LocData); 636 } 637 return Loc.get(); 638 } 639 640 const DWARFDebugLocDWO *DWARFContext::getDebugLocDWO() { 641 if (LocDWO) 642 return LocDWO.get(); 643 644 LocDWO.reset(new DWARFDebugLocDWO()); 645 // Assume all compile units have the same address byte size. 646 if (getNumCompileUnits()) { 647 DataExtractor LocData(DObj->getLocDWOSection().Data, isLittleEndian(), 648 getCompileUnitAtIndex(0)->getAddressByteSize()); 649 LocDWO->parse(LocData); 650 } 651 return LocDWO.get(); 652 } 653 654 const DWARFDebugAranges *DWARFContext::getDebugAranges() { 655 if (Aranges) 656 return Aranges.get(); 657 658 Aranges.reset(new DWARFDebugAranges()); 659 Aranges->generate(this); 660 return Aranges.get(); 661 } 662 663 const DWARFDebugFrame *DWARFContext::getDebugFrame() { 664 if (DebugFrame) 665 return DebugFrame.get(); 666 667 // There's a "bug" in the DWARFv3 standard with respect to the target address 668 // size within debug frame sections. While DWARF is supposed to be independent 669 // of its container, FDEs have fields with size being "target address size", 670 // which isn't specified in DWARF in general. It's only specified for CUs, but 671 // .eh_frame can appear without a .debug_info section. Follow the example of 672 // other tools (libdwarf) and extract this from the container (ObjectFile 673 // provides this information). This problem is fixed in DWARFv4 674 // See this dwarf-discuss discussion for more details: 675 // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html 676 DWARFDataExtractor debugFrameData(DObj->getDebugFrameSection(), 677 isLittleEndian(), DObj->getAddressSize()); 678 DebugFrame.reset(new DWARFDebugFrame(false /* IsEH */)); 679 DebugFrame->parse(debugFrameData); 680 return DebugFrame.get(); 681 } 682 683 const DWARFDebugFrame *DWARFContext::getEHFrame() { 684 if (EHFrame) 685 return EHFrame.get(); 686 687 DWARFDataExtractor debugFrameData(DObj->getEHFrameSection(), isLittleEndian(), 688 DObj->getAddressSize()); 689 DebugFrame.reset(new DWARFDebugFrame(true /* IsEH */)); 690 DebugFrame->parse(debugFrameData); 691 return DebugFrame.get(); 692 } 693 694 const DWARFDebugMacro *DWARFContext::getDebugMacro() { 695 if (Macro) 696 return Macro.get(); 697 698 DataExtractor MacinfoData(DObj->getMacinfoSection(), isLittleEndian(), 0); 699 Macro.reset(new DWARFDebugMacro()); 700 Macro->parse(MacinfoData); 701 return Macro.get(); 702 } 703 704 template <typename T> 705 static T &getAccelTable(std::unique_ptr<T> &Cache, const DWARFObject &Obj, 706 const DWARFSection &Section, StringRef StringSection, 707 bool IsLittleEndian) { 708 if (Cache) 709 return *Cache; 710 DWARFDataExtractor AccelSection(Obj, Section, IsLittleEndian, 0); 711 DataExtractor StrData(StringSection, IsLittleEndian, 0); 712 Cache.reset(new T(AccelSection, StrData)); 713 if (Error E = Cache->extract()) 714 llvm::consumeError(std::move(E)); 715 return *Cache; 716 } 717 718 const DWARFDebugNames &DWARFContext::getDebugNames() { 719 return getAccelTable(Names, *DObj, DObj->getDebugNamesSection(), 720 DObj->getStringSection(), isLittleEndian()); 721 } 722 723 const AppleAcceleratorTable &DWARFContext::getAppleNames() { 724 return getAccelTable(AppleNames, *DObj, DObj->getAppleNamesSection(), 725 DObj->getStringSection(), isLittleEndian()); 726 } 727 728 const AppleAcceleratorTable &DWARFContext::getAppleTypes() { 729 return getAccelTable(AppleTypes, *DObj, DObj->getAppleTypesSection(), 730 DObj->getStringSection(), isLittleEndian()); 731 } 732 733 const AppleAcceleratorTable &DWARFContext::getAppleNamespaces() { 734 return getAccelTable(AppleNamespaces, *DObj, 735 DObj->getAppleNamespacesSection(), 736 DObj->getStringSection(), isLittleEndian()); 737 } 738 739 const AppleAcceleratorTable &DWARFContext::getAppleObjC() { 740 return getAccelTable(AppleObjC, *DObj, DObj->getAppleObjCSection(), 741 DObj->getStringSection(), isLittleEndian()); 742 } 743 744 const DWARFDebugLine::LineTable * 745 DWARFContext::getLineTableForUnit(DWARFUnit *U) { 746 Expected<const DWARFDebugLine::LineTable *> ExpectedLineTable = 747 getLineTableForUnit(U, DWARFDebugLine::warn); 748 if (!ExpectedLineTable) { 749 DWARFDebugLine::warnForError(ExpectedLineTable.takeError()); 750 return nullptr; 751 } 752 return *ExpectedLineTable; 753 } 754 755 Expected<const DWARFDebugLine::LineTable *> 756 DWARFContext::getLineTableForUnit(DWARFUnit *U, 757 std::function<void(StringRef)> WarnCallback) { 758 if (!Line) 759 Line.reset(new DWARFDebugLine); 760 761 auto UnitDIE = U->getUnitDIE(); 762 if (!UnitDIE) 763 return nullptr; 764 765 auto Offset = toSectionOffset(UnitDIE.find(DW_AT_stmt_list)); 766 if (!Offset) 767 return nullptr; // No line table for this compile unit. 768 769 uint32_t stmtOffset = *Offset + U->getLineTableOffset(); 770 // See if the line table is cached. 771 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset)) 772 return lt; 773 774 // Make sure the offset is good before we try to parse. 775 if (stmtOffset >= U->getLineSection().Data.size()) 776 return nullptr; 777 778 // We have to parse it first. 779 DWARFDataExtractor lineData(*DObj, U->getLineSection(), isLittleEndian(), 780 U->getAddressByteSize()); 781 return Line->getOrParseLineTable(lineData, stmtOffset, *this, U, 782 WarnCallback); 783 } 784 785 void DWARFContext::parseCompileUnits() { 786 CUs.parse(*this, DObj->getInfoSection()); 787 } 788 789 void DWARFContext::parseTypeUnits() { 790 if (!TUs.empty()) 791 return; 792 DObj->forEachTypesSections([&](const DWARFSection &S) { 793 TUs.emplace_back(); 794 TUs.back().parse(*this, S); 795 }); 796 } 797 798 void DWARFContext::parseDWOCompileUnits() { 799 DWOCUs.parseDWO(*this, DObj->getInfoDWOSection()); 800 } 801 802 void DWARFContext::parseDWOTypeUnits() { 803 if (!DWOTUs.empty()) 804 return; 805 DObj->forEachTypesDWOSections([&](const DWARFSection &S) { 806 DWOTUs.emplace_back(); 807 DWOTUs.back().parseDWO(*this, S); 808 }); 809 } 810 811 DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) { 812 parseCompileUnits(); 813 return CUs.getUnitForOffset(Offset); 814 } 815 816 DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) { 817 // First, get the offset of the compile unit. 818 uint32_t CUOffset = getDebugAranges()->findAddress(Address); 819 // Retrieve the compile unit. 820 return getCompileUnitForOffset(CUOffset); 821 } 822 823 DWARFContext::DIEsForAddress DWARFContext::getDIEsForAddress(uint64_t Address) { 824 DIEsForAddress Result; 825 826 DWARFCompileUnit *CU = getCompileUnitForAddress(Address); 827 if (!CU) 828 return Result; 829 830 Result.CompileUnit = CU; 831 Result.FunctionDIE = CU->getSubroutineForAddress(Address); 832 833 std::vector<DWARFDie> Worklist; 834 Worklist.push_back(Result.FunctionDIE); 835 while (!Worklist.empty()) { 836 DWARFDie DIE = Worklist.back(); 837 Worklist.pop_back(); 838 839 if (DIE.getTag() == DW_TAG_lexical_block && 840 DIE.addressRangeContainsAddress(Address)) { 841 Result.BlockDIE = DIE; 842 break; 843 } 844 845 for (auto Child : DIE) 846 Worklist.push_back(Child); 847 } 848 849 return Result; 850 } 851 852 static bool getFunctionNameAndStartLineForAddress(DWARFCompileUnit *CU, 853 uint64_t Address, 854 FunctionNameKind Kind, 855 std::string &FunctionName, 856 uint32_t &StartLine) { 857 // The address may correspond to instruction in some inlined function, 858 // so we have to build the chain of inlined functions and take the 859 // name of the topmost function in it. 860 SmallVector<DWARFDie, 4> InlinedChain; 861 CU->getInlinedChainForAddress(Address, InlinedChain); 862 if (InlinedChain.empty()) 863 return false; 864 865 const DWARFDie &DIE = InlinedChain[0]; 866 bool FoundResult = false; 867 const char *Name = nullptr; 868 if (Kind != FunctionNameKind::None && (Name = DIE.getSubroutineName(Kind))) { 869 FunctionName = Name; 870 FoundResult = true; 871 } 872 if (auto DeclLineResult = DIE.getDeclLine()) { 873 StartLine = DeclLineResult; 874 FoundResult = true; 875 } 876 877 return FoundResult; 878 } 879 880 DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address, 881 DILineInfoSpecifier Spec) { 882 DILineInfo Result; 883 884 DWARFCompileUnit *CU = getCompileUnitForAddress(Address); 885 if (!CU) 886 return Result; 887 getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind, 888 Result.FunctionName, 889 Result.StartLine); 890 if (Spec.FLIKind != FileLineInfoKind::None) { 891 if (const DWARFLineTable *LineTable = getLineTableForUnit(CU)) 892 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(), 893 Spec.FLIKind, Result); 894 } 895 return Result; 896 } 897 898 DILineInfoTable 899 DWARFContext::getLineInfoForAddressRange(uint64_t Address, uint64_t Size, 900 DILineInfoSpecifier Spec) { 901 DILineInfoTable Lines; 902 DWARFCompileUnit *CU = getCompileUnitForAddress(Address); 903 if (!CU) 904 return Lines; 905 906 std::string FunctionName = "<invalid>"; 907 uint32_t StartLine = 0; 908 getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind, FunctionName, 909 StartLine); 910 911 // If the Specifier says we don't need FileLineInfo, just 912 // return the top-most function at the starting address. 913 if (Spec.FLIKind == FileLineInfoKind::None) { 914 DILineInfo Result; 915 Result.FunctionName = FunctionName; 916 Result.StartLine = StartLine; 917 Lines.push_back(std::make_pair(Address, Result)); 918 return Lines; 919 } 920 921 const DWARFLineTable *LineTable = getLineTableForUnit(CU); 922 923 // Get the index of row we're looking for in the line table. 924 std::vector<uint32_t> RowVector; 925 if (!LineTable->lookupAddressRange(Address, Size, RowVector)) 926 return Lines; 927 928 for (uint32_t RowIndex : RowVector) { 929 // Take file number and line/column from the row. 930 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex]; 931 DILineInfo Result; 932 LineTable->getFileNameByIndex(Row.File, CU->getCompilationDir(), 933 Spec.FLIKind, Result.FileName); 934 Result.FunctionName = FunctionName; 935 Result.Line = Row.Line; 936 Result.Column = Row.Column; 937 Result.StartLine = StartLine; 938 Lines.push_back(std::make_pair(Row.Address, Result)); 939 } 940 941 return Lines; 942 } 943 944 DIInliningInfo 945 DWARFContext::getInliningInfoForAddress(uint64_t Address, 946 DILineInfoSpecifier Spec) { 947 DIInliningInfo InliningInfo; 948 949 DWARFCompileUnit *CU = getCompileUnitForAddress(Address); 950 if (!CU) 951 return InliningInfo; 952 953 const DWARFLineTable *LineTable = nullptr; 954 SmallVector<DWARFDie, 4> InlinedChain; 955 CU->getInlinedChainForAddress(Address, InlinedChain); 956 if (InlinedChain.size() == 0) { 957 // If there is no DIE for address (e.g. it is in unavailable .dwo file), 958 // try to at least get file/line info from symbol table. 959 if (Spec.FLIKind != FileLineInfoKind::None) { 960 DILineInfo Frame; 961 LineTable = getLineTableForUnit(CU); 962 if (LineTable && 963 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(), 964 Spec.FLIKind, Frame)) 965 InliningInfo.addFrame(Frame); 966 } 967 return InliningInfo; 968 } 969 970 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0, CallDiscriminator = 0; 971 for (uint32_t i = 0, n = InlinedChain.size(); i != n; i++) { 972 DWARFDie &FunctionDIE = InlinedChain[i]; 973 DILineInfo Frame; 974 // Get function name if necessary. 975 if (const char *Name = FunctionDIE.getSubroutineName(Spec.FNKind)) 976 Frame.FunctionName = Name; 977 if (auto DeclLineResult = FunctionDIE.getDeclLine()) 978 Frame.StartLine = DeclLineResult; 979 if (Spec.FLIKind != FileLineInfoKind::None) { 980 if (i == 0) { 981 // For the topmost frame, initialize the line table of this 982 // compile unit and fetch file/line info from it. 983 LineTable = getLineTableForUnit(CU); 984 // For the topmost routine, get file/line info from line table. 985 if (LineTable) 986 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(), 987 Spec.FLIKind, Frame); 988 } else { 989 // Otherwise, use call file, call line and call column from 990 // previous DIE in inlined chain. 991 if (LineTable) 992 LineTable->getFileNameByIndex(CallFile, CU->getCompilationDir(), 993 Spec.FLIKind, Frame.FileName); 994 Frame.Line = CallLine; 995 Frame.Column = CallColumn; 996 Frame.Discriminator = CallDiscriminator; 997 } 998 // Get call file/line/column of a current DIE. 999 if (i + 1 < n) { 1000 FunctionDIE.getCallerFrame(CallFile, CallLine, CallColumn, 1001 CallDiscriminator); 1002 } 1003 } 1004 InliningInfo.addFrame(Frame); 1005 } 1006 return InliningInfo; 1007 } 1008 1009 std::shared_ptr<DWARFContext> 1010 DWARFContext::getDWOContext(StringRef AbsolutePath) { 1011 if (auto S = DWP.lock()) { 1012 DWARFContext *Ctxt = S->Context.get(); 1013 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt); 1014 } 1015 1016 std::weak_ptr<DWOFile> *Entry = &DWOFiles[AbsolutePath]; 1017 1018 if (auto S = Entry->lock()) { 1019 DWARFContext *Ctxt = S->Context.get(); 1020 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt); 1021 } 1022 1023 Expected<OwningBinary<ObjectFile>> Obj = [&] { 1024 if (!CheckedForDWP) { 1025 SmallString<128> DWPName; 1026 auto Obj = object::ObjectFile::createObjectFile( 1027 this->DWPName.empty() 1028 ? (DObj->getFileName() + ".dwp").toStringRef(DWPName) 1029 : StringRef(this->DWPName)); 1030 if (Obj) { 1031 Entry = &DWP; 1032 return Obj; 1033 } else { 1034 CheckedForDWP = true; 1035 // TODO: Should this error be handled (maybe in a high verbosity mode) 1036 // before falling back to .dwo files? 1037 consumeError(Obj.takeError()); 1038 } 1039 } 1040 1041 return object::ObjectFile::createObjectFile(AbsolutePath); 1042 }(); 1043 1044 if (!Obj) { 1045 // TODO: Actually report errors helpfully. 1046 consumeError(Obj.takeError()); 1047 return nullptr; 1048 } 1049 1050 auto S = std::make_shared<DWOFile>(); 1051 S->File = std::move(Obj.get()); 1052 S->Context = DWARFContext::create(*S->File.getBinary()); 1053 *Entry = S; 1054 auto *Ctxt = S->Context.get(); 1055 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt); 1056 } 1057 1058 static Error createError(const Twine &Reason, llvm::Error E) { 1059 return make_error<StringError>(Reason + toString(std::move(E)), 1060 inconvertibleErrorCode()); 1061 } 1062 1063 /// SymInfo contains information about symbol: it's address 1064 /// and section index which is -1LL for absolute symbols. 1065 struct SymInfo { 1066 uint64_t Address; 1067 uint64_t SectionIndex; 1068 }; 1069 1070 /// Returns the address of symbol relocation used against and a section index. 1071 /// Used for futher relocations computation. Symbol's section load address is 1072 static Expected<SymInfo> getSymbolInfo(const object::ObjectFile &Obj, 1073 const RelocationRef &Reloc, 1074 const LoadedObjectInfo *L, 1075 std::map<SymbolRef, SymInfo> &Cache) { 1076 SymInfo Ret = {0, (uint64_t)-1LL}; 1077 object::section_iterator RSec = Obj.section_end(); 1078 object::symbol_iterator Sym = Reloc.getSymbol(); 1079 1080 std::map<SymbolRef, SymInfo>::iterator CacheIt = Cache.end(); 1081 // First calculate the address of the symbol or section as it appears 1082 // in the object file 1083 if (Sym != Obj.symbol_end()) { 1084 bool New; 1085 std::tie(CacheIt, New) = Cache.insert({*Sym, {0, 0}}); 1086 if (!New) 1087 return CacheIt->second; 1088 1089 Expected<uint64_t> SymAddrOrErr = Sym->getAddress(); 1090 if (!SymAddrOrErr) 1091 return createError("failed to compute symbol address: ", 1092 SymAddrOrErr.takeError()); 1093 1094 // Also remember what section this symbol is in for later 1095 auto SectOrErr = Sym->getSection(); 1096 if (!SectOrErr) 1097 return createError("failed to get symbol section: ", 1098 SectOrErr.takeError()); 1099 1100 RSec = *SectOrErr; 1101 Ret.Address = *SymAddrOrErr; 1102 } else if (auto *MObj = dyn_cast<MachOObjectFile>(&Obj)) { 1103 RSec = MObj->getRelocationSection(Reloc.getRawDataRefImpl()); 1104 Ret.Address = RSec->getAddress(); 1105 } 1106 1107 if (RSec != Obj.section_end()) 1108 Ret.SectionIndex = RSec->getIndex(); 1109 1110 // If we are given load addresses for the sections, we need to adjust: 1111 // SymAddr = (Address of Symbol Or Section in File) - 1112 // (Address of Section in File) + 1113 // (Load Address of Section) 1114 // RSec is now either the section being targeted or the section 1115 // containing the symbol being targeted. In either case, 1116 // we need to perform the same computation. 1117 if (L && RSec != Obj.section_end()) 1118 if (uint64_t SectionLoadAddress = L->getSectionLoadAddress(*RSec)) 1119 Ret.Address += SectionLoadAddress - RSec->getAddress(); 1120 1121 if (CacheIt != Cache.end()) 1122 CacheIt->second = Ret; 1123 1124 return Ret; 1125 } 1126 1127 static bool isRelocScattered(const object::ObjectFile &Obj, 1128 const RelocationRef &Reloc) { 1129 const MachOObjectFile *MachObj = dyn_cast<MachOObjectFile>(&Obj); 1130 if (!MachObj) 1131 return false; 1132 // MachO also has relocations that point to sections and 1133 // scattered relocations. 1134 auto RelocInfo = MachObj->getRelocation(Reloc.getRawDataRefImpl()); 1135 return MachObj->isRelocationScattered(RelocInfo); 1136 } 1137 1138 ErrorPolicy DWARFContext::defaultErrorHandler(Error E) { 1139 WithColor::error() << toString(std::move(E)) << '\n'; 1140 return ErrorPolicy::Continue; 1141 } 1142 1143 namespace { 1144 struct DWARFSectionMap final : public DWARFSection { 1145 RelocAddrMap Relocs; 1146 }; 1147 1148 class DWARFObjInMemory final : public DWARFObject { 1149 bool IsLittleEndian; 1150 uint8_t AddressSize; 1151 StringRef FileName; 1152 const object::ObjectFile *Obj = nullptr; 1153 std::vector<SectionName> SectionNames; 1154 1155 using TypeSectionMap = MapVector<object::SectionRef, DWARFSectionMap, 1156 std::map<object::SectionRef, unsigned>>; 1157 1158 TypeSectionMap TypesSections; 1159 TypeSectionMap TypesDWOSections; 1160 1161 DWARFSectionMap InfoSection; 1162 DWARFSectionMap LocSection; 1163 DWARFSectionMap LineSection; 1164 DWARFSectionMap RangeSection; 1165 DWARFSectionMap RnglistsSection; 1166 DWARFSectionMap StringOffsetSection; 1167 DWARFSectionMap InfoDWOSection; 1168 DWARFSectionMap LineDWOSection; 1169 DWARFSectionMap LocDWOSection; 1170 DWARFSectionMap StringOffsetDWOSection; 1171 DWARFSectionMap RangeDWOSection; 1172 DWARFSectionMap AddrSection; 1173 DWARFSectionMap AppleNamesSection; 1174 DWARFSectionMap AppleTypesSection; 1175 DWARFSectionMap AppleNamespacesSection; 1176 DWARFSectionMap AppleObjCSection; 1177 DWARFSectionMap DebugNamesSection; 1178 1179 DWARFSectionMap *mapNameToDWARFSection(StringRef Name) { 1180 return StringSwitch<DWARFSectionMap *>(Name) 1181 .Case("debug_info", &InfoSection) 1182 .Case("debug_loc", &LocSection) 1183 .Case("debug_line", &LineSection) 1184 .Case("debug_str_offsets", &StringOffsetSection) 1185 .Case("debug_ranges", &RangeSection) 1186 .Case("debug_rnglists", &RnglistsSection) 1187 .Case("debug_info.dwo", &InfoDWOSection) 1188 .Case("debug_loc.dwo", &LocDWOSection) 1189 .Case("debug_line.dwo", &LineDWOSection) 1190 .Case("debug_names", &DebugNamesSection) 1191 .Case("debug_str_offsets.dwo", &StringOffsetDWOSection) 1192 .Case("debug_addr", &AddrSection) 1193 .Case("apple_names", &AppleNamesSection) 1194 .Case("apple_types", &AppleTypesSection) 1195 .Case("apple_namespaces", &AppleNamespacesSection) 1196 .Case("apple_namespac", &AppleNamespacesSection) 1197 .Case("apple_objc", &AppleObjCSection) 1198 .Default(nullptr); 1199 } 1200 1201 StringRef AbbrevSection; 1202 StringRef ARangeSection; 1203 StringRef DebugFrameSection; 1204 StringRef EHFrameSection; 1205 StringRef StringSection; 1206 StringRef MacinfoSection; 1207 StringRef PubNamesSection; 1208 StringRef PubTypesSection; 1209 StringRef GnuPubNamesSection; 1210 StringRef AbbrevDWOSection; 1211 StringRef StringDWOSection; 1212 StringRef GnuPubTypesSection; 1213 StringRef CUIndexSection; 1214 StringRef GdbIndexSection; 1215 StringRef TUIndexSection; 1216 StringRef LineStringSection; 1217 1218 SmallVector<SmallString<32>, 4> UncompressedSections; 1219 1220 StringRef *mapSectionToMember(StringRef Name) { 1221 if (DWARFSection *Sec = mapNameToDWARFSection(Name)) 1222 return &Sec->Data; 1223 return StringSwitch<StringRef *>(Name) 1224 .Case("debug_abbrev", &AbbrevSection) 1225 .Case("debug_aranges", &ARangeSection) 1226 .Case("debug_frame", &DebugFrameSection) 1227 .Case("eh_frame", &EHFrameSection) 1228 .Case("debug_str", &StringSection) 1229 .Case("debug_macinfo", &MacinfoSection) 1230 .Case("debug_pubnames", &PubNamesSection) 1231 .Case("debug_pubtypes", &PubTypesSection) 1232 .Case("debug_gnu_pubnames", &GnuPubNamesSection) 1233 .Case("debug_gnu_pubtypes", &GnuPubTypesSection) 1234 .Case("debug_abbrev.dwo", &AbbrevDWOSection) 1235 .Case("debug_str.dwo", &StringDWOSection) 1236 .Case("debug_cu_index", &CUIndexSection) 1237 .Case("debug_tu_index", &TUIndexSection) 1238 .Case("gdb_index", &GdbIndexSection) 1239 .Case("debug_line_str", &LineStringSection) 1240 // Any more debug info sections go here. 1241 .Default(nullptr); 1242 } 1243 1244 /// If Sec is compressed section, decompresses and updates its contents 1245 /// provided by Data. Otherwise leaves it unchanged. 1246 Error maybeDecompress(const object::SectionRef &Sec, StringRef Name, 1247 StringRef &Data) { 1248 if (!Decompressor::isCompressed(Sec)) 1249 return Error::success(); 1250 1251 Expected<Decompressor> Decompressor = 1252 Decompressor::create(Name, Data, IsLittleEndian, AddressSize == 8); 1253 if (!Decompressor) 1254 return Decompressor.takeError(); 1255 1256 SmallString<32> Out; 1257 if (auto Err = Decompressor->resizeAndDecompress(Out)) 1258 return Err; 1259 1260 UncompressedSections.emplace_back(std::move(Out)); 1261 Data = UncompressedSections.back(); 1262 1263 return Error::success(); 1264 } 1265 1266 public: 1267 DWARFObjInMemory(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections, 1268 uint8_t AddrSize, bool IsLittleEndian) 1269 : IsLittleEndian(IsLittleEndian) { 1270 for (const auto &SecIt : Sections) { 1271 if (StringRef *SectionData = mapSectionToMember(SecIt.first())) 1272 *SectionData = SecIt.second->getBuffer(); 1273 } 1274 } 1275 DWARFObjInMemory(const object::ObjectFile &Obj, const LoadedObjectInfo *L, 1276 function_ref<ErrorPolicy(Error)> HandleError) 1277 : IsLittleEndian(Obj.isLittleEndian()), 1278 AddressSize(Obj.getBytesInAddress()), FileName(Obj.getFileName()), 1279 Obj(&Obj) { 1280 1281 StringMap<unsigned> SectionAmountMap; 1282 for (const SectionRef &Section : Obj.sections()) { 1283 StringRef Name; 1284 Section.getName(Name); 1285 ++SectionAmountMap[Name]; 1286 SectionNames.push_back({ Name, true }); 1287 1288 // Skip BSS and Virtual sections, they aren't interesting. 1289 if (Section.isBSS() || Section.isVirtual()) 1290 continue; 1291 1292 // Skip sections stripped by dsymutil. 1293 if (Section.isStripped()) 1294 continue; 1295 1296 StringRef Data; 1297 section_iterator RelocatedSection = Section.getRelocatedSection(); 1298 // Try to obtain an already relocated version of this section. 1299 // Else use the unrelocated section from the object file. We'll have to 1300 // apply relocations ourselves later. 1301 if (!L || !L->getLoadedSectionContents(*RelocatedSection, Data)) 1302 Section.getContents(Data); 1303 1304 if (auto Err = maybeDecompress(Section, Name, Data)) { 1305 ErrorPolicy EP = HandleError(createError( 1306 "failed to decompress '" + Name + "', ", std::move(Err))); 1307 if (EP == ErrorPolicy::Halt) 1308 return; 1309 continue; 1310 } 1311 1312 // Compressed sections names in GNU style starts from ".z", 1313 // at this point section is decompressed and we drop compression prefix. 1314 Name = Name.substr( 1315 Name.find_first_not_of("._z")); // Skip ".", "z" and "_" prefixes. 1316 1317 // Map platform specific debug section names to DWARF standard section 1318 // names. 1319 Name = Obj.mapDebugSectionName(Name); 1320 1321 if (StringRef *SectionData = mapSectionToMember(Name)) { 1322 *SectionData = Data; 1323 if (Name == "debug_ranges") { 1324 // FIXME: Use the other dwo range section when we emit it. 1325 RangeDWOSection.Data = Data; 1326 } 1327 } else if (Name == "debug_types") { 1328 // Find debug_types data by section rather than name as there are 1329 // multiple, comdat grouped, debug_types sections. 1330 TypesSections[Section].Data = Data; 1331 } else if (Name == "debug_types.dwo") { 1332 TypesDWOSections[Section].Data = Data; 1333 } 1334 1335 if (RelocatedSection == Obj.section_end()) 1336 continue; 1337 1338 StringRef RelSecName; 1339 StringRef RelSecData; 1340 RelocatedSection->getName(RelSecName); 1341 1342 // If the section we're relocating was relocated already by the JIT, 1343 // then we used the relocated version above, so we do not need to process 1344 // relocations for it now. 1345 if (L && L->getLoadedSectionContents(*RelocatedSection, RelSecData)) 1346 continue; 1347 1348 // In Mach-o files, the relocations do not need to be applied if 1349 // there is no load offset to apply. The value read at the 1350 // relocation point already factors in the section address 1351 // (actually applying the relocations will produce wrong results 1352 // as the section address will be added twice). 1353 if (!L && isa<MachOObjectFile>(&Obj)) 1354 continue; 1355 1356 RelSecName = RelSecName.substr( 1357 RelSecName.find_first_not_of("._z")); // Skip . and _ prefixes. 1358 1359 // TODO: Add support for relocations in other sections as needed. 1360 // Record relocations for the debug_info and debug_line sections. 1361 DWARFSectionMap *Sec = mapNameToDWARFSection(RelSecName); 1362 RelocAddrMap *Map = Sec ? &Sec->Relocs : nullptr; 1363 if (!Map) { 1364 // Find debug_types relocs by section rather than name as there are 1365 // multiple, comdat grouped, debug_types sections. 1366 if (RelSecName == "debug_types") 1367 Map = 1368 &static_cast<DWARFSectionMap &>(TypesSections[*RelocatedSection]) 1369 .Relocs; 1370 else if (RelSecName == "debug_types.dwo") 1371 Map = &static_cast<DWARFSectionMap &>( 1372 TypesDWOSections[*RelocatedSection]) 1373 .Relocs; 1374 else 1375 continue; 1376 } 1377 1378 if (Section.relocation_begin() == Section.relocation_end()) 1379 continue; 1380 1381 // Symbol to [address, section index] cache mapping. 1382 std::map<SymbolRef, SymInfo> AddrCache; 1383 for (const RelocationRef &Reloc : Section.relocations()) { 1384 // FIXME: it's not clear how to correctly handle scattered 1385 // relocations. 1386 if (isRelocScattered(Obj, Reloc)) 1387 continue; 1388 1389 Expected<SymInfo> SymInfoOrErr = 1390 getSymbolInfo(Obj, Reloc, L, AddrCache); 1391 if (!SymInfoOrErr) { 1392 if (HandleError(SymInfoOrErr.takeError()) == ErrorPolicy::Halt) 1393 return; 1394 continue; 1395 } 1396 1397 object::RelocVisitor V(Obj); 1398 uint64_t Val = V.visit(Reloc.getType(), Reloc, SymInfoOrErr->Address); 1399 if (V.error()) { 1400 SmallString<32> Type; 1401 Reloc.getTypeName(Type); 1402 ErrorPolicy EP = HandleError( 1403 createError("failed to compute relocation: " + Type + ", ", 1404 errorCodeToError(object_error::parse_failed))); 1405 if (EP == ErrorPolicy::Halt) 1406 return; 1407 continue; 1408 } 1409 RelocAddrEntry Rel = {SymInfoOrErr->SectionIndex, Val}; 1410 Map->insert({Reloc.getOffset(), Rel}); 1411 } 1412 } 1413 1414 for (SectionName &S : SectionNames) 1415 if (SectionAmountMap[S.Name] > 1) 1416 S.IsNameUnique = false; 1417 } 1418 1419 Optional<RelocAddrEntry> find(const DWARFSection &S, 1420 uint64_t Pos) const override { 1421 auto &Sec = static_cast<const DWARFSectionMap &>(S); 1422 RelocAddrMap::const_iterator AI = Sec.Relocs.find(Pos); 1423 if (AI == Sec.Relocs.end()) 1424 return None; 1425 return AI->second; 1426 } 1427 1428 const object::ObjectFile *getFile() const override { return Obj; } 1429 1430 ArrayRef<SectionName> getSectionNames() const override { 1431 return SectionNames; 1432 } 1433 1434 bool isLittleEndian() const override { return IsLittleEndian; } 1435 StringRef getAbbrevDWOSection() const override { return AbbrevDWOSection; } 1436 const DWARFSection &getLineDWOSection() const override { 1437 return LineDWOSection; 1438 } 1439 const DWARFSection &getLocDWOSection() const override { 1440 return LocDWOSection; 1441 } 1442 StringRef getStringDWOSection() const override { return StringDWOSection; } 1443 const DWARFSection &getStringOffsetDWOSection() const override { 1444 return StringOffsetDWOSection; 1445 } 1446 const DWARFSection &getRangeDWOSection() const override { 1447 return RangeDWOSection; 1448 } 1449 const DWARFSection &getAddrSection() const override { return AddrSection; } 1450 StringRef getCUIndexSection() const override { return CUIndexSection; } 1451 StringRef getGdbIndexSection() const override { return GdbIndexSection; } 1452 StringRef getTUIndexSection() const override { return TUIndexSection; } 1453 1454 // DWARF v5 1455 const DWARFSection &getStringOffsetSection() const override { 1456 return StringOffsetSection; 1457 } 1458 StringRef getLineStringSection() const override { return LineStringSection; } 1459 1460 // Sections for DWARF5 split dwarf proposal. 1461 const DWARFSection &getInfoDWOSection() const override { 1462 return InfoDWOSection; 1463 } 1464 void forEachTypesDWOSections( 1465 function_ref<void(const DWARFSection &)> F) const override { 1466 for (auto &P : TypesDWOSections) 1467 F(P.second); 1468 } 1469 1470 StringRef getAbbrevSection() const override { return AbbrevSection; } 1471 const DWARFSection &getLocSection() const override { return LocSection; } 1472 StringRef getARangeSection() const override { return ARangeSection; } 1473 StringRef getDebugFrameSection() const override { return DebugFrameSection; } 1474 StringRef getEHFrameSection() const override { return EHFrameSection; } 1475 const DWARFSection &getLineSection() const override { return LineSection; } 1476 StringRef getStringSection() const override { return StringSection; } 1477 const DWARFSection &getRangeSection() const override { return RangeSection; } 1478 const DWARFSection &getRnglistsSection() const override { 1479 return RnglistsSection; 1480 } 1481 StringRef getMacinfoSection() const override { return MacinfoSection; } 1482 StringRef getPubNamesSection() const override { return PubNamesSection; } 1483 StringRef getPubTypesSection() const override { return PubTypesSection; } 1484 StringRef getGnuPubNamesSection() const override { 1485 return GnuPubNamesSection; 1486 } 1487 StringRef getGnuPubTypesSection() const override { 1488 return GnuPubTypesSection; 1489 } 1490 const DWARFSection &getAppleNamesSection() const override { 1491 return AppleNamesSection; 1492 } 1493 const DWARFSection &getAppleTypesSection() const override { 1494 return AppleTypesSection; 1495 } 1496 const DWARFSection &getAppleNamespacesSection() const override { 1497 return AppleNamespacesSection; 1498 } 1499 const DWARFSection &getAppleObjCSection() const override { 1500 return AppleObjCSection; 1501 } 1502 const DWARFSection &getDebugNamesSection() const override { 1503 return DebugNamesSection; 1504 } 1505 1506 StringRef getFileName() const override { return FileName; } 1507 uint8_t getAddressSize() const override { return AddressSize; } 1508 const DWARFSection &getInfoSection() const override { return InfoSection; } 1509 void forEachTypesSections( 1510 function_ref<void(const DWARFSection &)> F) const override { 1511 for (auto &P : TypesSections) 1512 F(P.second); 1513 } 1514 }; 1515 } // namespace 1516 1517 std::unique_ptr<DWARFContext> 1518 DWARFContext::create(const object::ObjectFile &Obj, const LoadedObjectInfo *L, 1519 function_ref<ErrorPolicy(Error)> HandleError, 1520 std::string DWPName) { 1521 auto DObj = llvm::make_unique<DWARFObjInMemory>(Obj, L, HandleError); 1522 return llvm::make_unique<DWARFContext>(std::move(DObj), std::move(DWPName)); 1523 } 1524 1525 std::unique_ptr<DWARFContext> 1526 DWARFContext::create(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections, 1527 uint8_t AddrSize, bool isLittleEndian) { 1528 auto DObj = 1529 llvm::make_unique<DWARFObjInMemory>(Sections, AddrSize, isLittleEndian); 1530 return llvm::make_unique<DWARFContext>(std::move(DObj), ""); 1531 } 1532 1533 Error DWARFContext::loadRegisterInfo(const object::ObjectFile &Obj) { 1534 // Detect the architecture from the object file. We usually don't need OS 1535 // info to lookup a target and create register info. 1536 Triple TT; 1537 TT.setArch(Triple::ArchType(Obj.getArch())); 1538 TT.setVendor(Triple::UnknownVendor); 1539 TT.setOS(Triple::UnknownOS); 1540 std::string TargetLookupError; 1541 const Target *TheTarget = 1542 TargetRegistry::lookupTarget(TT.str(), TargetLookupError); 1543 if (!TargetLookupError.empty()) 1544 return make_error<StringError>(TargetLookupError, inconvertibleErrorCode()); 1545 RegInfo.reset(TheTarget->createMCRegInfo(TT.str())); 1546 return Error::success(); 1547 } 1548