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