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/SmallString.h" 12 #include "llvm/ADT/StringSwitch.h" 13 #include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h" 14 #include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h" 15 #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h" 16 #include "llvm/Object/MachO.h" 17 #include "llvm/Object/RelocVisitor.h" 18 #include "llvm/Support/Compression.h" 19 #include "llvm/Support/Dwarf.h" 20 #include "llvm/Support/ELF.h" 21 #include "llvm/Support/Format.h" 22 #include "llvm/Support/Path.h" 23 #include "llvm/Support/raw_ostream.h" 24 #include <algorithm> 25 using namespace llvm; 26 using namespace dwarf; 27 using namespace object; 28 29 #define DEBUG_TYPE "dwarf" 30 31 typedef DWARFDebugLine::LineTable DWARFLineTable; 32 typedef DILineInfoSpecifier::FileLineInfoKind FileLineInfoKind; 33 typedef DILineInfoSpecifier::FunctionNameKind FunctionNameKind; 34 35 static void dumpPubSection(raw_ostream &OS, StringRef Name, StringRef Data, 36 bool LittleEndian, bool GnuStyle) { 37 OS << "\n." << Name << " contents:\n"; 38 DataExtractor pubNames(Data, LittleEndian, 0); 39 uint32_t offset = 0; 40 while (pubNames.isValidOffset(offset)) { 41 OS << "length = " << format("0x%08x", pubNames.getU32(&offset)); 42 OS << " version = " << format("0x%04x", pubNames.getU16(&offset)); 43 OS << " unit_offset = " << format("0x%08x", pubNames.getU32(&offset)); 44 OS << " unit_size = " << format("0x%08x", pubNames.getU32(&offset)) << '\n'; 45 if (GnuStyle) 46 OS << "Offset Linkage Kind Name\n"; 47 else 48 OS << "Offset Name\n"; 49 50 while (offset < Data.size()) { 51 uint32_t dieRef = pubNames.getU32(&offset); 52 if (dieRef == 0) 53 break; 54 OS << format("0x%8.8x ", dieRef); 55 if (GnuStyle) { 56 PubIndexEntryDescriptor desc(pubNames.getU8(&offset)); 57 OS << format("%-8s", 58 dwarf::GDBIndexEntryLinkageString(desc.Linkage).data()) 59 << ' ' 60 << format("%-8s", dwarf::GDBIndexEntryKindString(desc.Kind).data()) 61 << ' '; 62 } 63 OS << '\"' << pubNames.getCStr(&offset) << "\"\n"; 64 } 65 } 66 } 67 68 static void dumpAccelSection(raw_ostream &OS, StringRef Name, 69 const DWARFSection& Section, StringRef StringSection, 70 bool LittleEndian) { 71 DataExtractor AccelSection(Section.Data, LittleEndian, 0); 72 DataExtractor StrData(StringSection, LittleEndian, 0); 73 OS << "\n." << Name << " contents:\n"; 74 DWARFAcceleratorTable Accel(AccelSection, StrData, Section.Relocs); 75 if (!Accel.extract()) 76 return; 77 Accel.dump(OS); 78 } 79 80 void DWARFContext::dump(raw_ostream &OS, DIDumpType DumpType, bool DumpEH) { 81 if (DumpType == DIDT_All || DumpType == DIDT_Abbrev) { 82 OS << ".debug_abbrev contents:\n"; 83 getDebugAbbrev()->dump(OS); 84 } 85 86 if (DumpType == DIDT_All || DumpType == DIDT_AbbrevDwo) 87 if (const DWARFDebugAbbrev *D = getDebugAbbrevDWO()) { 88 OS << "\n.debug_abbrev.dwo contents:\n"; 89 D->dump(OS); 90 } 91 92 if (DumpType == DIDT_All || DumpType == DIDT_Info) { 93 OS << "\n.debug_info contents:\n"; 94 for (const auto &CU : compile_units()) 95 CU->dump(OS); 96 } 97 98 if ((DumpType == DIDT_All || DumpType == DIDT_InfoDwo) && 99 getNumDWOCompileUnits()) { 100 OS << "\n.debug_info.dwo contents:\n"; 101 for (const auto &DWOCU : dwo_compile_units()) 102 DWOCU->dump(OS); 103 } 104 105 if ((DumpType == DIDT_All || DumpType == DIDT_Types) && getNumTypeUnits()) { 106 OS << "\n.debug_types contents:\n"; 107 for (const auto &TUS : type_unit_sections()) 108 for (const auto &TU : TUS) 109 TU->dump(OS); 110 } 111 112 if ((DumpType == DIDT_All || DumpType == DIDT_TypesDwo) && 113 getNumDWOTypeUnits()) { 114 OS << "\n.debug_types.dwo contents:\n"; 115 for (const auto &DWOTUS : dwo_type_unit_sections()) 116 for (const auto &DWOTU : DWOTUS) 117 DWOTU->dump(OS); 118 } 119 120 if (DumpType == DIDT_All || DumpType == DIDT_Loc) { 121 OS << "\n.debug_loc contents:\n"; 122 getDebugLoc()->dump(OS); 123 } 124 125 if (DumpType == DIDT_All || DumpType == DIDT_LocDwo) { 126 OS << "\n.debug_loc.dwo contents:\n"; 127 getDebugLocDWO()->dump(OS); 128 } 129 130 if (DumpType == DIDT_All || DumpType == DIDT_Frames) { 131 OS << "\n.debug_frame contents:\n"; 132 getDebugFrame()->dump(OS); 133 if (DumpEH) { 134 OS << "\n.eh_frame contents:\n"; 135 getEHFrame()->dump(OS); 136 } 137 } 138 139 if (DumpType == DIDT_All || DumpType == DIDT_Macro) { 140 OS << "\n.debug_macinfo contents:\n"; 141 getDebugMacro()->dump(OS); 142 } 143 144 uint32_t offset = 0; 145 if (DumpType == DIDT_All || DumpType == DIDT_Aranges) { 146 OS << "\n.debug_aranges contents:\n"; 147 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0); 148 DWARFDebugArangeSet set; 149 while (set.extract(arangesData, &offset)) 150 set.dump(OS); 151 } 152 153 uint8_t savedAddressByteSize = 0; 154 if (DumpType == DIDT_All || DumpType == DIDT_Line) { 155 OS << "\n.debug_line contents:\n"; 156 for (const auto &CU : compile_units()) { 157 savedAddressByteSize = CU->getAddressByteSize(); 158 const auto *CUDIE = CU->getUnitDIE(); 159 if (CUDIE == nullptr) 160 continue; 161 unsigned stmtOffset = CUDIE->getAttributeValueAsSectionOffset( 162 CU.get(), DW_AT_stmt_list, -1U); 163 if (stmtOffset != -1U) { 164 DataExtractor lineData(getLineSection().Data, isLittleEndian(), 165 savedAddressByteSize); 166 DWARFDebugLine::LineTable LineTable; 167 LineTable.parse(lineData, &getLineSection().Relocs, &stmtOffset); 168 LineTable.dump(OS); 169 } 170 } 171 } 172 173 if (DumpType == DIDT_All || DumpType == DIDT_CUIndex) { 174 OS << "\n.debug_cu_index contents:\n"; 175 getCUIndex().dump(OS); 176 } 177 178 if (DumpType == DIDT_All || DumpType == DIDT_TUIndex) { 179 OS << "\n.debug_tu_index contents:\n"; 180 getTUIndex().dump(OS); 181 } 182 183 if (DumpType == DIDT_All || DumpType == DIDT_LineDwo) { 184 OS << "\n.debug_line.dwo contents:\n"; 185 unsigned stmtOffset = 0; 186 DataExtractor lineData(getLineDWOSection().Data, isLittleEndian(), 187 savedAddressByteSize); 188 DWARFDebugLine::LineTable LineTable; 189 while (LineTable.Prologue.parse(lineData, &stmtOffset)) { 190 LineTable.dump(OS); 191 LineTable.clear(); 192 } 193 } 194 195 if (DumpType == DIDT_All || DumpType == DIDT_Str) { 196 OS << "\n.debug_str contents:\n"; 197 DataExtractor strData(getStringSection(), isLittleEndian(), 0); 198 offset = 0; 199 uint32_t strOffset = 0; 200 while (const char *s = strData.getCStr(&offset)) { 201 OS << format("0x%8.8x: \"%s\"\n", strOffset, s); 202 strOffset = offset; 203 } 204 } 205 206 if ((DumpType == DIDT_All || DumpType == DIDT_StrDwo) && 207 !getStringDWOSection().empty()) { 208 OS << "\n.debug_str.dwo contents:\n"; 209 DataExtractor strDWOData(getStringDWOSection(), isLittleEndian(), 0); 210 offset = 0; 211 uint32_t strDWOOffset = 0; 212 while (const char *s = strDWOData.getCStr(&offset)) { 213 OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s); 214 strDWOOffset = offset; 215 } 216 } 217 218 if (DumpType == DIDT_All || DumpType == DIDT_Ranges) { 219 OS << "\n.debug_ranges contents:\n"; 220 // In fact, different compile units may have different address byte 221 // sizes, but for simplicity we just use the address byte size of the last 222 // compile unit (there is no easy and fast way to associate address range 223 // list and the compile unit it describes). 224 DataExtractor rangesData(getRangeSection(), isLittleEndian(), 225 savedAddressByteSize); 226 offset = 0; 227 DWARFDebugRangeList rangeList; 228 while (rangeList.extract(rangesData, &offset)) 229 rangeList.dump(OS); 230 } 231 232 if (DumpType == DIDT_All || DumpType == DIDT_Pubnames) 233 dumpPubSection(OS, "debug_pubnames", getPubNamesSection(), 234 isLittleEndian(), false); 235 236 if (DumpType == DIDT_All || DumpType == DIDT_Pubtypes) 237 dumpPubSection(OS, "debug_pubtypes", getPubTypesSection(), 238 isLittleEndian(), false); 239 240 if (DumpType == DIDT_All || DumpType == DIDT_GnuPubnames) 241 dumpPubSection(OS, "debug_gnu_pubnames", getGnuPubNamesSection(), 242 isLittleEndian(), true /* GnuStyle */); 243 244 if (DumpType == DIDT_All || DumpType == DIDT_GnuPubtypes) 245 dumpPubSection(OS, "debug_gnu_pubtypes", getGnuPubTypesSection(), 246 isLittleEndian(), true /* GnuStyle */); 247 248 if ((DumpType == DIDT_All || DumpType == DIDT_StrOffsetsDwo) && 249 !getStringOffsetDWOSection().empty()) { 250 OS << "\n.debug_str_offsets.dwo contents:\n"; 251 DataExtractor strOffsetExt(getStringOffsetDWOSection(), isLittleEndian(), 252 0); 253 offset = 0; 254 uint64_t size = getStringOffsetDWOSection().size(); 255 while (offset < size) { 256 OS << format("0x%8.8x: ", offset); 257 OS << format("%8.8x\n", strOffsetExt.getU32(&offset)); 258 } 259 } 260 261 if ((DumpType == DIDT_All || DumpType == DIDT_GdbIndex) && 262 !getGdbIndexSection().empty()) { 263 OS << "\n.gnu_index contents:\n"; 264 getGdbIndex().dump(OS); 265 } 266 267 if (DumpType == DIDT_All || DumpType == DIDT_AppleNames) 268 dumpAccelSection(OS, "apple_names", getAppleNamesSection(), 269 getStringSection(), isLittleEndian()); 270 271 if (DumpType == DIDT_All || DumpType == DIDT_AppleTypes) 272 dumpAccelSection(OS, "apple_types", getAppleTypesSection(), 273 getStringSection(), isLittleEndian()); 274 275 if (DumpType == DIDT_All || DumpType == DIDT_AppleNamespaces) 276 dumpAccelSection(OS, "apple_namespaces", getAppleNamespacesSection(), 277 getStringSection(), isLittleEndian()); 278 279 if (DumpType == DIDT_All || DumpType == DIDT_AppleObjC) 280 dumpAccelSection(OS, "apple_objc", getAppleObjCSection(), 281 getStringSection(), isLittleEndian()); 282 } 283 284 const DWARFUnitIndex &DWARFContext::getCUIndex() { 285 if (CUIndex) 286 return *CUIndex; 287 288 DataExtractor CUIndexData(getCUIndexSection(), isLittleEndian(), 0); 289 290 CUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_INFO); 291 CUIndex->parse(CUIndexData); 292 return *CUIndex; 293 } 294 295 const DWARFUnitIndex &DWARFContext::getTUIndex() { 296 if (TUIndex) 297 return *TUIndex; 298 299 DataExtractor TUIndexData(getTUIndexSection(), isLittleEndian(), 0); 300 301 TUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_TYPES); 302 TUIndex->parse(TUIndexData); 303 return *TUIndex; 304 } 305 306 DWARFGdbIndex &DWARFContext::getGdbIndex() { 307 if (GdbIndex) 308 return *GdbIndex; 309 310 DataExtractor GdbIndexData(getGdbIndexSection(), true /*LE*/, 0); 311 GdbIndex = llvm::make_unique<DWARFGdbIndex>(); 312 GdbIndex->parse(GdbIndexData); 313 return *GdbIndex; 314 } 315 316 const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() { 317 if (Abbrev) 318 return Abbrev.get(); 319 320 DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0); 321 322 Abbrev.reset(new DWARFDebugAbbrev()); 323 Abbrev->extract(abbrData); 324 return Abbrev.get(); 325 } 326 327 const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() { 328 if (AbbrevDWO) 329 return AbbrevDWO.get(); 330 331 DataExtractor abbrData(getAbbrevDWOSection(), isLittleEndian(), 0); 332 AbbrevDWO.reset(new DWARFDebugAbbrev()); 333 AbbrevDWO->extract(abbrData); 334 return AbbrevDWO.get(); 335 } 336 337 const DWARFDebugLoc *DWARFContext::getDebugLoc() { 338 if (Loc) 339 return Loc.get(); 340 341 DataExtractor LocData(getLocSection().Data, isLittleEndian(), 0); 342 Loc.reset(new DWARFDebugLoc(getLocSection().Relocs)); 343 // assume all compile units have the same address byte size 344 if (getNumCompileUnits()) 345 Loc->parse(LocData, getCompileUnitAtIndex(0)->getAddressByteSize()); 346 return Loc.get(); 347 } 348 349 const DWARFDebugLocDWO *DWARFContext::getDebugLocDWO() { 350 if (LocDWO) 351 return LocDWO.get(); 352 353 DataExtractor LocData(getLocDWOSection().Data, isLittleEndian(), 0); 354 LocDWO.reset(new DWARFDebugLocDWO()); 355 LocDWO->parse(LocData); 356 return LocDWO.get(); 357 } 358 359 const DWARFDebugAranges *DWARFContext::getDebugAranges() { 360 if (Aranges) 361 return Aranges.get(); 362 363 Aranges.reset(new DWARFDebugAranges()); 364 Aranges->generate(this); 365 return Aranges.get(); 366 } 367 368 const DWARFDebugFrame *DWARFContext::getDebugFrame() { 369 if (DebugFrame) 370 return DebugFrame.get(); 371 372 // There's a "bug" in the DWARFv3 standard with respect to the target address 373 // size within debug frame sections. While DWARF is supposed to be independent 374 // of its container, FDEs have fields with size being "target address size", 375 // which isn't specified in DWARF in general. It's only specified for CUs, but 376 // .eh_frame can appear without a .debug_info section. Follow the example of 377 // other tools (libdwarf) and extract this from the container (ObjectFile 378 // provides this information). This problem is fixed in DWARFv4 379 // See this dwarf-discuss discussion for more details: 380 // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html 381 DataExtractor debugFrameData(getDebugFrameSection(), isLittleEndian(), 382 getAddressSize()); 383 DebugFrame.reset(new DWARFDebugFrame(false /* IsEH */)); 384 DebugFrame->parse(debugFrameData); 385 return DebugFrame.get(); 386 } 387 388 const DWARFDebugFrame *DWARFContext::getEHFrame() { 389 if (EHFrame) 390 return EHFrame.get(); 391 392 DataExtractor debugFrameData(getEHFrameSection(), isLittleEndian(), 393 getAddressSize()); 394 DebugFrame.reset(new DWARFDebugFrame(true /* IsEH */)); 395 DebugFrame->parse(debugFrameData); 396 return DebugFrame.get(); 397 } 398 399 const DWARFDebugMacro *DWARFContext::getDebugMacro() { 400 if (Macro) 401 return Macro.get(); 402 403 DataExtractor MacinfoData(getMacinfoSection(), isLittleEndian(), 0); 404 Macro.reset(new DWARFDebugMacro()); 405 Macro->parse(MacinfoData); 406 return Macro.get(); 407 } 408 409 const DWARFLineTable * 410 DWARFContext::getLineTableForUnit(DWARFUnit *U) { 411 if (!Line) 412 Line.reset(new DWARFDebugLine(&getLineSection().Relocs)); 413 414 const auto *UnitDIE = U->getUnitDIE(); 415 if (UnitDIE == nullptr) 416 return nullptr; 417 418 unsigned stmtOffset = 419 UnitDIE->getAttributeValueAsSectionOffset(U, DW_AT_stmt_list, -1U); 420 if (stmtOffset == -1U) 421 return nullptr; // No line table for this compile unit. 422 423 stmtOffset += U->getLineTableOffset(); 424 // See if the line table is cached. 425 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset)) 426 return lt; 427 428 // We have to parse it first. 429 DataExtractor lineData(U->getLineSection(), isLittleEndian(), 430 U->getAddressByteSize()); 431 return Line->getOrParseLineTable(lineData, stmtOffset); 432 } 433 434 void DWARFContext::parseCompileUnits() { 435 CUs.parse(*this, getInfoSection()); 436 } 437 438 void DWARFContext::parseTypeUnits() { 439 if (!TUs.empty()) 440 return; 441 for (const auto &I : getTypesSections()) { 442 TUs.emplace_back(); 443 TUs.back().parse(*this, I.second); 444 } 445 } 446 447 void DWARFContext::parseDWOCompileUnits() { 448 DWOCUs.parseDWO(*this, getInfoDWOSection()); 449 } 450 451 void DWARFContext::parseDWOTypeUnits() { 452 if (!DWOTUs.empty()) 453 return; 454 for (const auto &I : getTypesDWOSections()) { 455 DWOTUs.emplace_back(); 456 DWOTUs.back().parseDWO(*this, I.second); 457 } 458 } 459 460 DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) { 461 parseCompileUnits(); 462 return CUs.getUnitForOffset(Offset); 463 } 464 465 DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) { 466 // First, get the offset of the compile unit. 467 uint32_t CUOffset = getDebugAranges()->findAddress(Address); 468 // Retrieve the compile unit. 469 return getCompileUnitForOffset(CUOffset); 470 } 471 472 static bool getFunctionNameForAddress(DWARFCompileUnit *CU, uint64_t Address, 473 FunctionNameKind Kind, 474 std::string &FunctionName) { 475 if (Kind == FunctionNameKind::None) 476 return false; 477 // The address may correspond to instruction in some inlined function, 478 // so we have to build the chain of inlined functions and take the 479 // name of the topmost function in it. 480 const DWARFDebugInfoEntryInlinedChain &InlinedChain = 481 CU->getInlinedChainForAddress(Address); 482 if (InlinedChain.DIEs.size() == 0) 483 return false; 484 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain.DIEs[0]; 485 if (const char *Name = 486 TopFunctionDIE.getSubroutineName(InlinedChain.U, Kind)) { 487 FunctionName = Name; 488 return true; 489 } 490 return false; 491 } 492 493 DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address, 494 DILineInfoSpecifier Spec) { 495 DILineInfo Result; 496 497 DWARFCompileUnit *CU = getCompileUnitForAddress(Address); 498 if (!CU) 499 return Result; 500 getFunctionNameForAddress(CU, Address, Spec.FNKind, Result.FunctionName); 501 if (Spec.FLIKind != FileLineInfoKind::None) { 502 if (const DWARFLineTable *LineTable = getLineTableForUnit(CU)) 503 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(), 504 Spec.FLIKind, Result); 505 } 506 return Result; 507 } 508 509 DILineInfoTable 510 DWARFContext::getLineInfoForAddressRange(uint64_t Address, uint64_t Size, 511 DILineInfoSpecifier Spec) { 512 DILineInfoTable Lines; 513 DWARFCompileUnit *CU = getCompileUnitForAddress(Address); 514 if (!CU) 515 return Lines; 516 517 std::string FunctionName = "<invalid>"; 518 getFunctionNameForAddress(CU, Address, Spec.FNKind, FunctionName); 519 520 // If the Specifier says we don't need FileLineInfo, just 521 // return the top-most function at the starting address. 522 if (Spec.FLIKind == FileLineInfoKind::None) { 523 DILineInfo Result; 524 Result.FunctionName = FunctionName; 525 Lines.push_back(std::make_pair(Address, Result)); 526 return Lines; 527 } 528 529 const DWARFLineTable *LineTable = getLineTableForUnit(CU); 530 531 // Get the index of row we're looking for in the line table. 532 std::vector<uint32_t> RowVector; 533 if (!LineTable->lookupAddressRange(Address, Size, RowVector)) 534 return Lines; 535 536 for (uint32_t RowIndex : RowVector) { 537 // Take file number and line/column from the row. 538 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex]; 539 DILineInfo Result; 540 LineTable->getFileNameByIndex(Row.File, CU->getCompilationDir(), 541 Spec.FLIKind, Result.FileName); 542 Result.FunctionName = FunctionName; 543 Result.Line = Row.Line; 544 Result.Column = Row.Column; 545 Lines.push_back(std::make_pair(Row.Address, Result)); 546 } 547 548 return Lines; 549 } 550 551 DIInliningInfo 552 DWARFContext::getInliningInfoForAddress(uint64_t Address, 553 DILineInfoSpecifier Spec) { 554 DIInliningInfo InliningInfo; 555 556 DWARFCompileUnit *CU = getCompileUnitForAddress(Address); 557 if (!CU) 558 return InliningInfo; 559 560 const DWARFLineTable *LineTable = nullptr; 561 const DWARFDebugInfoEntryInlinedChain &InlinedChain = 562 CU->getInlinedChainForAddress(Address); 563 if (InlinedChain.DIEs.size() == 0) { 564 // If there is no DIE for address (e.g. it is in unavailable .dwo file), 565 // try to at least get file/line info from symbol table. 566 if (Spec.FLIKind != FileLineInfoKind::None) { 567 DILineInfo Frame; 568 LineTable = getLineTableForUnit(CU); 569 if (LineTable && 570 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(), 571 Spec.FLIKind, Frame)) 572 InliningInfo.addFrame(Frame); 573 } 574 return InliningInfo; 575 } 576 577 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0; 578 for (uint32_t i = 0, n = InlinedChain.DIEs.size(); i != n; i++) { 579 const DWARFDebugInfoEntryMinimal &FunctionDIE = InlinedChain.DIEs[i]; 580 DILineInfo Frame; 581 // Get function name if necessary. 582 if (const char *Name = 583 FunctionDIE.getSubroutineName(InlinedChain.U, Spec.FNKind)) 584 Frame.FunctionName = Name; 585 if (Spec.FLIKind != FileLineInfoKind::None) { 586 if (i == 0) { 587 // For the topmost frame, initialize the line table of this 588 // compile unit and fetch file/line info from it. 589 LineTable = getLineTableForUnit(CU); 590 // For the topmost routine, get file/line info from line table. 591 if (LineTable) 592 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(), 593 Spec.FLIKind, Frame); 594 } else { 595 // Otherwise, use call file, call line and call column from 596 // previous DIE in inlined chain. 597 if (LineTable) 598 LineTable->getFileNameByIndex(CallFile, CU->getCompilationDir(), 599 Spec.FLIKind, Frame.FileName); 600 Frame.Line = CallLine; 601 Frame.Column = CallColumn; 602 } 603 // Get call file/line/column of a current DIE. 604 if (i + 1 < n) { 605 FunctionDIE.getCallerFrame(InlinedChain.U, CallFile, CallLine, 606 CallColumn); 607 } 608 } 609 InliningInfo.addFrame(Frame); 610 } 611 return InliningInfo; 612 } 613 614 static bool consumeCompressedGnuHeader(StringRef &data, 615 uint64_t &OriginalSize) { 616 // Consume "ZLIB" prefix. 617 if (!data.startswith("ZLIB")) 618 return false; 619 data = data.substr(4); 620 // Consume uncompressed section size (big-endian 8 bytes). 621 DataExtractor extractor(data, false, 8); 622 uint32_t Offset = 0; 623 OriginalSize = extractor.getU64(&Offset); 624 if (Offset == 0) 625 return false; 626 data = data.substr(Offset); 627 return true; 628 } 629 630 static bool consumeCompressedZLibHeader(StringRef &Data, uint64_t &OriginalSize, 631 bool IsLE, bool Is64Bit) { 632 using namespace ELF; 633 uint64_t HdrSize = Is64Bit ? sizeof(Elf64_Chdr) : sizeof(Elf32_Chdr); 634 if (Data.size() < HdrSize) 635 return false; 636 637 DataExtractor Extractor(Data, IsLE, 0); 638 uint32_t Offset = 0; 639 if (Extractor.getUnsigned(&Offset, Is64Bit ? sizeof(Elf64_Word) 640 : sizeof(Elf32_Word)) != 641 ELFCOMPRESS_ZLIB) 642 return false; 643 644 // Skip Elf64_Chdr::ch_reserved field. 645 if (Is64Bit) 646 Offset += sizeof(Elf64_Word); 647 648 OriginalSize = Extractor.getUnsigned(&Offset, Is64Bit ? sizeof(Elf64_Xword) 649 : sizeof(Elf32_Word)); 650 Data = Data.substr(HdrSize); 651 return true; 652 } 653 654 static bool tryDecompress(StringRef &Name, StringRef &Data, 655 SmallString<32> &Out, bool ZLibStyle, bool IsLE, 656 bool Is64Bit) { 657 if (!zlib::isAvailable()) 658 return false; 659 660 uint64_t OriginalSize; 661 bool Result = 662 ZLibStyle ? consumeCompressedZLibHeader(Data, OriginalSize, IsLE, Is64Bit) 663 : consumeCompressedGnuHeader(Data, OriginalSize); 664 665 if (!Result || zlib::uncompress(Data, Out, OriginalSize) != zlib::StatusOK) 666 return false; 667 668 // gnu-style names are started from "z", consume that. 669 if (!ZLibStyle) 670 Name = Name.substr(1); 671 return true; 672 } 673 674 DWARFContextInMemory::DWARFContextInMemory(const object::ObjectFile &Obj, 675 const LoadedObjectInfo *L) 676 : IsLittleEndian(Obj.isLittleEndian()), 677 AddressSize(Obj.getBytesInAddress()) { 678 for (const SectionRef &Section : Obj.sections()) { 679 StringRef name; 680 Section.getName(name); 681 // Skip BSS and Virtual sections, they aren't interesting. 682 bool IsBSS = Section.isBSS(); 683 if (IsBSS) 684 continue; 685 bool IsVirtual = Section.isVirtual(); 686 if (IsVirtual) 687 continue; 688 StringRef data; 689 690 section_iterator RelocatedSection = Section.getRelocatedSection(); 691 // Try to obtain an already relocated version of this section. 692 // Else use the unrelocated section from the object file. We'll have to 693 // apply relocations ourselves later. 694 if (!L || !L->getLoadedSectionContents(*RelocatedSection,data)) 695 Section.getContents(data); 696 697 name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes. 698 699 bool ZLibStyleCompressed = Section.isCompressed(); 700 if (ZLibStyleCompressed || name.startswith("zdebug_")) { 701 SmallString<32> Out; 702 if (!tryDecompress(name, data, Out, ZLibStyleCompressed, IsLittleEndian, 703 AddressSize == 8)) 704 continue; 705 UncompressedSections.emplace_back(std::move(Out)); 706 data = UncompressedSections.back(); 707 } 708 709 StringRef *SectionData = 710 StringSwitch<StringRef *>(name) 711 .Case("debug_info", &InfoSection.Data) 712 .Case("debug_abbrev", &AbbrevSection) 713 .Case("debug_loc", &LocSection.Data) 714 .Case("debug_line", &LineSection.Data) 715 .Case("debug_aranges", &ARangeSection) 716 .Case("debug_frame", &DebugFrameSection) 717 .Case("eh_frame", &EHFrameSection) 718 .Case("debug_str", &StringSection) 719 .Case("debug_ranges", &RangeSection) 720 .Case("debug_macinfo", &MacinfoSection) 721 .Case("debug_pubnames", &PubNamesSection) 722 .Case("debug_pubtypes", &PubTypesSection) 723 .Case("debug_gnu_pubnames", &GnuPubNamesSection) 724 .Case("debug_gnu_pubtypes", &GnuPubTypesSection) 725 .Case("debug_info.dwo", &InfoDWOSection.Data) 726 .Case("debug_abbrev.dwo", &AbbrevDWOSection) 727 .Case("debug_loc.dwo", &LocDWOSection.Data) 728 .Case("debug_line.dwo", &LineDWOSection.Data) 729 .Case("debug_str.dwo", &StringDWOSection) 730 .Case("debug_str_offsets.dwo", &StringOffsetDWOSection) 731 .Case("debug_addr", &AddrSection) 732 .Case("apple_names", &AppleNamesSection.Data) 733 .Case("apple_types", &AppleTypesSection.Data) 734 .Case("apple_namespaces", &AppleNamespacesSection.Data) 735 .Case("apple_namespac", &AppleNamespacesSection.Data) 736 .Case("apple_objc", &AppleObjCSection.Data) 737 .Case("debug_cu_index", &CUIndexSection) 738 .Case("debug_tu_index", &TUIndexSection) 739 .Case("gdb_index", &GdbIndexSection) 740 // Any more debug info sections go here. 741 .Default(nullptr); 742 if (SectionData) { 743 *SectionData = data; 744 if (name == "debug_ranges") { 745 // FIXME: Use the other dwo range section when we emit it. 746 RangeDWOSection = data; 747 } 748 } else if (name == "debug_types") { 749 // Find debug_types data by section rather than name as there are 750 // multiple, comdat grouped, debug_types sections. 751 TypesSections[Section].Data = data; 752 } else if (name == "debug_types.dwo") { 753 TypesDWOSections[Section].Data = data; 754 } 755 756 if (RelocatedSection == Obj.section_end()) 757 continue; 758 759 StringRef RelSecName; 760 StringRef RelSecData; 761 RelocatedSection->getName(RelSecName); 762 763 // If the section we're relocating was relocated already by the JIT, 764 // then we used the relocated version above, so we do not need to process 765 // relocations for it now. 766 if (L && L->getLoadedSectionContents(*RelocatedSection,RelSecData)) 767 continue; 768 769 // In Mach-o files, the relocations do not need to be applied if 770 // there is no load offset to apply. The value read at the 771 // relocation point already factors in the section address 772 // (actually applying the relocations will produce wrong results 773 // as the section address will be added twice). 774 if (!L && isa<MachOObjectFile>(&Obj)) 775 continue; 776 777 RelSecName = RelSecName.substr( 778 RelSecName.find_first_not_of("._")); // Skip . and _ prefixes. 779 780 // TODO: Add support for relocations in other sections as needed. 781 // Record relocations for the debug_info and debug_line sections. 782 RelocAddrMap *Map = StringSwitch<RelocAddrMap*>(RelSecName) 783 .Case("debug_info", &InfoSection.Relocs) 784 .Case("debug_loc", &LocSection.Relocs) 785 .Case("debug_info.dwo", &InfoDWOSection.Relocs) 786 .Case("debug_line", &LineSection.Relocs) 787 .Case("apple_names", &AppleNamesSection.Relocs) 788 .Case("apple_types", &AppleTypesSection.Relocs) 789 .Case("apple_namespaces", &AppleNamespacesSection.Relocs) 790 .Case("apple_namespac", &AppleNamespacesSection.Relocs) 791 .Case("apple_objc", &AppleObjCSection.Relocs) 792 .Default(nullptr); 793 if (!Map) { 794 // Find debug_types relocs by section rather than name as there are 795 // multiple, comdat grouped, debug_types sections. 796 if (RelSecName == "debug_types") 797 Map = &TypesSections[*RelocatedSection].Relocs; 798 else if (RelSecName == "debug_types.dwo") 799 Map = &TypesDWOSections[*RelocatedSection].Relocs; 800 else 801 continue; 802 } 803 804 if (Section.relocation_begin() != Section.relocation_end()) { 805 uint64_t SectionSize = RelocatedSection->getSize(); 806 for (const RelocationRef &Reloc : Section.relocations()) { 807 uint64_t Address = Reloc.getOffset(); 808 uint64_t Type = Reloc.getType(); 809 uint64_t SymAddr = 0; 810 uint64_t SectionLoadAddress = 0; 811 object::symbol_iterator Sym = Reloc.getSymbol(); 812 object::section_iterator RSec = Obj.section_end(); 813 814 // First calculate the address of the symbol or section as it appears 815 // in the objct file 816 if (Sym != Obj.symbol_end()) { 817 Expected<uint64_t> SymAddrOrErr = Sym->getAddress(); 818 if (!SymAddrOrErr) { 819 std::string Buf; 820 raw_string_ostream OS(Buf); 821 logAllUnhandledErrors(SymAddrOrErr.takeError(), OS, ""); 822 OS.flush(); 823 errs() << "error: failed to compute symbol address: " 824 << Buf << '\n'; 825 continue; 826 } 827 SymAddr = *SymAddrOrErr; 828 // Also remember what section this symbol is in for later 829 auto SectOrErr = Sym->getSection(); 830 if (!SectOrErr) { 831 std::string Buf; 832 raw_string_ostream OS(Buf); 833 logAllUnhandledErrors(SectOrErr.takeError(), OS, ""); 834 OS.flush(); 835 errs() << "error: failed to get symbol section: " 836 << Buf << '\n'; 837 continue; 838 } 839 RSec = *SectOrErr; 840 } else if (auto *MObj = dyn_cast<MachOObjectFile>(&Obj)) { 841 // MachO also has relocations that point to sections and 842 // scattered relocations. 843 auto RelocInfo = MObj->getRelocation(Reloc.getRawDataRefImpl()); 844 if (MObj->isRelocationScattered(RelocInfo)) { 845 // FIXME: it's not clear how to correctly handle scattered 846 // relocations. 847 continue; 848 } else { 849 RSec = MObj->getRelocationSection(Reloc.getRawDataRefImpl()); 850 SymAddr = RSec->getAddress(); 851 } 852 } 853 854 // If we are given load addresses for the sections, we need to adjust: 855 // SymAddr = (Address of Symbol Or Section in File) - 856 // (Address of Section in File) + 857 // (Load Address of Section) 858 if (L != nullptr && RSec != Obj.section_end()) { 859 // RSec is now either the section being targeted or the section 860 // containing the symbol being targeted. In either case, 861 // we need to perform the same computation. 862 StringRef SecName; 863 RSec->getName(SecName); 864 // llvm::dbgs() << "Name: '" << SecName 865 // << "', RSec: " << RSec->getRawDataRefImpl() 866 // << ", Section: " << Section.getRawDataRefImpl() << "\n"; 867 SectionLoadAddress = L->getSectionLoadAddress(*RSec); 868 if (SectionLoadAddress != 0) 869 SymAddr += SectionLoadAddress - RSec->getAddress(); 870 } 871 872 object::RelocVisitor V(Obj); 873 object::RelocToApply R(V.visit(Type, Reloc, SymAddr)); 874 if (V.error()) { 875 SmallString<32> Name; 876 Reloc.getTypeName(Name); 877 errs() << "error: failed to compute relocation: " 878 << Name << "\n"; 879 continue; 880 } 881 882 if (Address + R.Width > SectionSize) { 883 errs() << "error: " << R.Width << "-byte relocation starting " 884 << Address << " bytes into section " << name << " which is " 885 << SectionSize << " bytes long.\n"; 886 continue; 887 } 888 if (R.Width > 8) { 889 errs() << "error: can't handle a relocation of more than 8 bytes at " 890 "a time.\n"; 891 continue; 892 } 893 DEBUG(dbgs() << "Writing " << format("%p", R.Value) 894 << " at " << format("%p", Address) 895 << " with width " << format("%d", R.Width) 896 << "\n"); 897 Map->insert(std::make_pair(Address, std::make_pair(R.Width, R.Value))); 898 } 899 } 900 } 901 } 902 903 void DWARFContextInMemory::anchor() { } 904