1 //===- DWARFContext.cpp ---------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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/DebugInfo/DWARF/DWARFAcceleratorTable.h"
17 #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
18 #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
19 #include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h"
20 #include "llvm/DebugInfo/DWARF/DWARFDebugAranges.h"
21 #include "llvm/DebugInfo/DWARF/DWARFDebugFrame.h"
22 #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
23 #include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h"
24 #include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h"
25 #include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h"
26 #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
27 #include "llvm/DebugInfo/DWARF/DWARFDie.h"
28 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
29 #include "llvm/DebugInfo/DWARF/DWARFGdbIndex.h"
30 #include "llvm/DebugInfo/DWARF/DWARFSection.h"
31 #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
32 #include "llvm/DebugInfo/DWARF/DWARFVerifier.h"
33 #include "llvm/Object/Decompressor.h"
34 #include "llvm/Object/MachO.h"
35 #include "llvm/Object/ObjectFile.h"
36 #include "llvm/Object/RelocVisitor.h"
37 #include "llvm/Support/Casting.h"
38 #include "llvm/Support/DataExtractor.h"
39 #include "llvm/Support/Debug.h"
40 #include "llvm/Support/Error.h"
41 #include "llvm/Support/Format.h"
42 #include "llvm/Support/MemoryBuffer.h"
43 #include "llvm/Support/raw_ostream.h"
53 using namespace dwarf;
54 using namespace object;
56 #define DEBUG_TYPE "dwarf"
58 typedef DWARFDebugLine::LineTable DWARFLineTable;
59 typedef DILineInfoSpecifier::FileLineInfoKind FileLineInfoKind;
60 typedef DILineInfoSpecifier::FunctionNameKind FunctionNameKind;
62 uint64_t llvm::getRelocatedValue(const DataExtractor &Data, uint32_t Size,
63 uint32_t *Off, const RelocAddrMap *Relocs) {
65 return Data.getUnsigned(Off, Size);
66 RelocAddrMap::const_iterator AI = Relocs->find(*Off);
67 if (AI == Relocs->end())
68 return Data.getUnsigned(Off, Size);
69 return Data.getUnsigned(Off, Size) + AI->second.second;
72 static void dumpAccelSection(raw_ostream &OS, StringRef Name,
73 const DWARFSection& Section, StringRef StringSection,
75 DataExtractor AccelSection(Section.Data, LittleEndian, 0);
76 DataExtractor StrData(StringSection, LittleEndian, 0);
77 OS << "\n." << Name << " contents:\n";
78 DWARFAcceleratorTable Accel(AccelSection, StrData, Section.Relocs);
84 void DWARFContext::dump(raw_ostream &OS, DIDumpType DumpType, bool DumpEH,
85 bool SummarizeTypes) {
86 if (DumpType == DIDT_All || DumpType == DIDT_Abbrev) {
87 OS << ".debug_abbrev contents:\n";
88 getDebugAbbrev()->dump(OS);
91 if (DumpType == DIDT_All || DumpType == DIDT_AbbrevDwo)
92 if (const DWARFDebugAbbrev *D = getDebugAbbrevDWO()) {
93 OS << "\n.debug_abbrev.dwo contents:\n";
97 if (DumpType == DIDT_All || DumpType == DIDT_Info) {
98 OS << "\n.debug_info contents:\n";
99 for (const auto &CU : compile_units())
103 if ((DumpType == DIDT_All || DumpType == DIDT_InfoDwo) &&
104 getNumDWOCompileUnits()) {
105 OS << "\n.debug_info.dwo contents:\n";
106 for (const auto &DWOCU : dwo_compile_units())
110 if ((DumpType == DIDT_All || DumpType == DIDT_Types) && getNumTypeUnits()) {
111 OS << "\n.debug_types contents:\n";
112 for (const auto &TUS : type_unit_sections())
113 for (const auto &TU : TUS)
114 TU->dump(OS, SummarizeTypes);
117 if ((DumpType == DIDT_All || DumpType == DIDT_TypesDwo) &&
118 getNumDWOTypeUnits()) {
119 OS << "\n.debug_types.dwo contents:\n";
120 for (const auto &DWOTUS : dwo_type_unit_sections())
121 for (const auto &DWOTU : DWOTUS)
122 DWOTU->dump(OS, SummarizeTypes);
125 if (DumpType == DIDT_All || DumpType == DIDT_Loc) {
126 OS << "\n.debug_loc contents:\n";
127 getDebugLoc()->dump(OS);
130 if (DumpType == DIDT_All || DumpType == DIDT_LocDwo) {
131 OS << "\n.debug_loc.dwo contents:\n";
132 getDebugLocDWO()->dump(OS);
135 if (DumpType == DIDT_All || DumpType == DIDT_Frames) {
136 OS << "\n.debug_frame contents:\n";
137 getDebugFrame()->dump(OS);
139 OS << "\n.eh_frame contents:\n";
140 getEHFrame()->dump(OS);
144 if (DumpType == DIDT_All || DumpType == DIDT_Macro) {
145 OS << "\n.debug_macinfo contents:\n";
146 getDebugMacro()->dump(OS);
150 if (DumpType == DIDT_All || DumpType == DIDT_Aranges) {
151 OS << "\n.debug_aranges contents:\n";
152 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
153 DWARFDebugArangeSet set;
154 while (set.extract(arangesData, &offset))
158 uint8_t savedAddressByteSize = 0;
159 if (DumpType == DIDT_All || DumpType == DIDT_Line) {
160 OS << "\n.debug_line contents:\n";
161 for (const auto &CU : compile_units()) {
162 savedAddressByteSize = CU->getAddressByteSize();
163 auto CUDIE = CU->getUnitDIE();
166 if (auto StmtOffset = toSectionOffset(CUDIE.find(DW_AT_stmt_list))) {
167 DataExtractor lineData(getLineSection().Data, isLittleEndian(),
168 savedAddressByteSize);
169 DWARFDebugLine::LineTable LineTable;
170 uint32_t Offset = *StmtOffset;
171 LineTable.parse(lineData, &getLineSection().Relocs, &Offset);
177 if (DumpType == DIDT_All || DumpType == DIDT_CUIndex) {
178 OS << "\n.debug_cu_index contents:\n";
179 getCUIndex().dump(OS);
182 if (DumpType == DIDT_All || DumpType == DIDT_TUIndex) {
183 OS << "\n.debug_tu_index contents:\n";
184 getTUIndex().dump(OS);
187 if (DumpType == DIDT_All || DumpType == DIDT_LineDwo) {
188 OS << "\n.debug_line.dwo contents:\n";
189 unsigned stmtOffset = 0;
190 DataExtractor lineData(getLineDWOSection().Data, isLittleEndian(),
191 savedAddressByteSize);
192 DWARFDebugLine::LineTable LineTable;
193 while (LineTable.Prologue.parse(lineData, &stmtOffset)) {
199 if (DumpType == DIDT_All || DumpType == DIDT_Str) {
200 OS << "\n.debug_str contents:\n";
201 DataExtractor strData(getStringSection(), isLittleEndian(), 0);
203 uint32_t strOffset = 0;
204 while (const char *s = strData.getCStr(&offset)) {
205 OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
210 if ((DumpType == DIDT_All || DumpType == DIDT_StrDwo) &&
211 !getStringDWOSection().empty()) {
212 OS << "\n.debug_str.dwo contents:\n";
213 DataExtractor strDWOData(getStringDWOSection(), isLittleEndian(), 0);
215 uint32_t strDWOOffset = 0;
216 while (const char *s = strDWOData.getCStr(&offset)) {
217 OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
218 strDWOOffset = offset;
222 if (DumpType == DIDT_All || DumpType == DIDT_Ranges) {
223 OS << "\n.debug_ranges contents:\n";
224 // In fact, different compile units may have different address byte
225 // sizes, but for simplicity we just use the address byte size of the last
226 // compile unit (there is no easy and fast way to associate address range
227 // list and the compile unit it describes).
228 DataExtractor rangesData(getRangeSection().Data, isLittleEndian(),
229 savedAddressByteSize);
231 DWARFDebugRangeList rangeList;
232 while (rangeList.extract(rangesData, &offset, getRangeSection().Relocs))
236 if (DumpType == DIDT_All || DumpType == DIDT_Pubnames)
237 DWARFDebugPubTable(getPubNamesSection(), isLittleEndian(), false)
238 .dump("debug_pubnames", OS);
240 if (DumpType == DIDT_All || DumpType == DIDT_Pubtypes)
241 DWARFDebugPubTable(getPubTypesSection(), isLittleEndian(), false)
242 .dump("debug_pubtypes", OS);
244 if (DumpType == DIDT_All || DumpType == DIDT_GnuPubnames)
245 DWARFDebugPubTable(getGnuPubNamesSection(), isLittleEndian(),
247 .dump("debug_gnu_pubnames", OS);
249 if (DumpType == DIDT_All || DumpType == DIDT_GnuPubtypes)
250 DWARFDebugPubTable(getGnuPubTypesSection(), isLittleEndian(),
252 .dump("debug_gnu_pubtypes", OS);
254 if ((DumpType == DIDT_All || DumpType == DIDT_StrOffsetsDwo) &&
255 !getStringOffsetDWOSection().empty()) {
256 OS << "\n.debug_str_offsets.dwo contents:\n";
257 DataExtractor strOffsetExt(getStringOffsetDWOSection(), isLittleEndian(),
260 uint64_t size = getStringOffsetDWOSection().size();
261 while (offset < size) {
262 OS << format("0x%8.8x: ", offset);
263 OS << format("%8.8x\n", strOffsetExt.getU32(&offset));
267 if ((DumpType == DIDT_All || DumpType == DIDT_GdbIndex) &&
268 !getGdbIndexSection().empty()) {
269 OS << "\n.gnu_index contents:\n";
270 getGdbIndex().dump(OS);
273 if (DumpType == DIDT_All || DumpType == DIDT_AppleNames)
274 dumpAccelSection(OS, "apple_names", getAppleNamesSection(),
275 getStringSection(), isLittleEndian());
277 if (DumpType == DIDT_All || DumpType == DIDT_AppleTypes)
278 dumpAccelSection(OS, "apple_types", getAppleTypesSection(),
279 getStringSection(), isLittleEndian());
281 if (DumpType == DIDT_All || DumpType == DIDT_AppleNamespaces)
282 dumpAccelSection(OS, "apple_namespaces", getAppleNamespacesSection(),
283 getStringSection(), isLittleEndian());
285 if (DumpType == DIDT_All || DumpType == DIDT_AppleObjC)
286 dumpAccelSection(OS, "apple_objc", getAppleObjCSection(),
287 getStringSection(), isLittleEndian());
290 DWARFDie DWARFContext::getDIEForOffset(uint32_t Offset) {
292 if (auto *CU = CUs.getUnitForOffset(Offset))
293 return CU->getDIEForOffset(Offset);
303 Verifier(raw_ostream &S, DWARFContext &D) : OS(S), DCtx(D) {}
305 bool HandleDebugInfo() {
307 // A map that tracks all references (converted absolute references) so we
308 // can verify each reference points to a valid DIE and not an offset that
309 // lies between to valid DIEs.
310 std::map<uint64_t, std::set<uint32_t>> ReferenceToDIEOffsets;
312 OS << "Verifying .debug_info...\n";
313 for (const auto &CU : DCtx.compile_units()) {
314 unsigned NumDies = CU->getNumDIEs();
315 for (unsigned I = 0; I < NumDies; ++I) {
316 auto Die = CU->getDIEAtIndex(I);
317 const auto Tag = Die.getTag();
318 if (Tag == DW_TAG_null)
320 for (auto AttrValue : Die.attributes()) {
321 const auto Attr = AttrValue.Attr;
322 const auto Form = AttrValue.Value.getForm();
325 // Make sure the offset in the DW_AT_ranges attribute is valid.
326 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
327 if (*SectionOffset >= DCtx.getRangeSection().Data.size()) {
329 OS << "error: DW_AT_ranges offset is beyond .debug_ranges "
336 OS << "error: DIE has invalid DW_AT_ranges encoding:\n";
341 case DW_AT_stmt_list:
342 // Make sure the offset in the DW_AT_stmt_list attribute is valid.
343 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
344 if (*SectionOffset >= DCtx.getLineSection().Data.size()) {
346 OS << "error: DW_AT_stmt_list offset is beyond .debug_line "
348 << format("0x%08" PRIx32, *SectionOffset) << "\n";
349 CU->getUnitDIE().dump(OS, 0);
354 OS << "error: DIE has invalid DW_AT_stmt_list encoding:\n";
368 case DW_FORM_ref_udata: {
369 // Verify all CU relative references are valid CU offsets.
370 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
373 auto DieCU = Die.getDwarfUnit();
374 auto CUSize = DieCU->getNextUnitOffset() - DieCU->getOffset();
375 auto CUOffset = AttrValue.Value.getRawUValue();
376 if (CUOffset >= CUSize) {
378 OS << "error: " << FormEncodingString(Form) << " CU offset "
379 << format("0x%08" PRIx32, CUOffset)
380 << " is invalid (must be less than CU size of "
381 << format("0x%08" PRIx32, CUSize) << "):\n";
385 // Valid reference, but we will verify it points to an actual
387 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
392 case DW_FORM_ref_addr: {
393 // Verify all absolute DIE references have valid offsets in the
394 // .debug_info section.
395 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
398 if(*RefVal >= DCtx.getInfoSection().Data.size()) {
400 OS << "error: DW_FORM_ref_addr offset beyond .debug_info "
405 // Valid reference, but we will verify it points to an actual
407 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
413 auto SecOffset = AttrValue.Value.getAsSectionOffset();
414 assert(SecOffset); // DW_FORM_strp is a section offset.
415 if (SecOffset && *SecOffset >= DCtx.getStringSection().size()) {
417 OS << "error: DW_FORM_strp offset beyond .debug_str bounds:\n";
430 // Take all references and make sure they point to an actual DIE by
431 // getting the DIE by offset and emitting an error
432 OS << "Verifying .debug_info references...\n";
433 for (auto Pair: ReferenceToDIEOffsets) {
434 auto Die = DCtx.getDIEForOffset(Pair.first);
438 OS << "error: invalid DIE reference " << format("0x%08" PRIx64, Pair.first)
439 << ". Offset is in between DIEs:\n";
440 for (auto Offset: Pair.second) {
441 auto ReferencingDie = DCtx.getDIEForOffset(Offset);
442 ReferencingDie.dump(OS, 0);
450 bool HandleDebugLine() {
451 std::map<uint64_t, DWARFDie> StmtListToDie;
453 OS << "Verifying .debug_line...\n";
454 for (const auto &CU : DCtx.compile_units()) {
455 uint32_t LineTableOffset = 0;
456 auto CUDie = CU->getUnitDIE();
457 auto StmtFormValue = CUDie.find(DW_AT_stmt_list);
458 if (!StmtFormValue) {
459 // No line table for this compile unit.
462 // Get the attribute value as a section offset. No need to produce an
463 // error here if the encoding isn't correct because we validate this in
464 // the .debug_info verifier.
465 if (auto StmtSectionOffset = toSectionOffset(StmtFormValue)) {
466 LineTableOffset = *StmtSectionOffset;
467 if (LineTableOffset >= DCtx.getLineSection().Data.size()) {
468 // Make sure we don't get a valid line table back if the offset
470 assert(DCtx.getLineTableForUnit(CU.get()) == nullptr);
471 // Skip this line table as it isn't valid. No need to create an error
472 // here because we validate this in the .debug_info verifier.
475 auto Iter = StmtListToDie.find(LineTableOffset);
476 if (Iter != StmtListToDie.end()) {
478 OS << "error: two compile unit DIEs, "
479 << format("0x%08" PRIx32, Iter->second.getOffset()) << " and "
480 << format("0x%08" PRIx32, CUDie.getOffset())
481 << ", have the same DW_AT_stmt_list section offset:\n";
482 Iter->second.dump(OS, 0);
485 // Already verified this line table before, no need to do it again.
488 StmtListToDie[LineTableOffset] = CUDie;
491 auto LineTable = DCtx.getLineTableForUnit(CU.get());
494 OS << "error: .debug_line[" << format("0x%08" PRIx32, LineTableOffset)
495 << "] was not able to be parsed for CU:\n";
500 uint32_t MaxFileIndex = LineTable->Prologue.FileNames.size();
501 uint64_t PrevAddress = 0;
502 uint32_t RowIndex = 0;
503 for (const auto &Row : LineTable->Rows) {
504 if (Row.Address < PrevAddress) {
506 OS << "error: .debug_line[" << format("0x%08" PRIx32, LineTableOffset)
507 << "] row[" << RowIndex
508 << "] decreases in address from previous row:\n";
510 DWARFDebugLine::Row::dumpTableHeader(OS);
512 LineTable->Rows[RowIndex - 1].dump(OS);
517 if (Row.File > MaxFileIndex) {
519 OS << "error: .debug_line[" << format("0x%08" PRIx32, LineTableOffset)
520 << "][" << RowIndex << "] has invalid file index " << Row.File
521 << " (valid values are [1," << MaxFileIndex << "]):\n";
522 DWARFDebugLine::Row::dumpTableHeader(OS);
529 PrevAddress = Row.Address;
537 } // anonymous namespace
539 bool DWARFContext::verify(raw_ostream &OS, DIDumpType DumpType) {
541 DWARFVerifier verifier(OS, *this);
542 if (DumpType == DIDT_All || DumpType == DIDT_Info) {
543 if (!verifier.handleDebugInfo())
546 if (DumpType == DIDT_All || DumpType == DIDT_Line) {
547 if (!verifier.handleDebugLine())
552 const DWARFUnitIndex &DWARFContext::getCUIndex() {
556 DataExtractor CUIndexData(getCUIndexSection(), isLittleEndian(), 0);
558 CUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_INFO);
559 CUIndex->parse(CUIndexData);
563 const DWARFUnitIndex &DWARFContext::getTUIndex() {
567 DataExtractor TUIndexData(getTUIndexSection(), isLittleEndian(), 0);
569 TUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_TYPES);
570 TUIndex->parse(TUIndexData);
574 DWARFGdbIndex &DWARFContext::getGdbIndex() {
578 DataExtractor GdbIndexData(getGdbIndexSection(), true /*LE*/, 0);
579 GdbIndex = llvm::make_unique<DWARFGdbIndex>();
580 GdbIndex->parse(GdbIndexData);
584 const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
588 DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
590 Abbrev.reset(new DWARFDebugAbbrev());
591 Abbrev->extract(abbrData);
595 const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
597 return AbbrevDWO.get();
599 DataExtractor abbrData(getAbbrevDWOSection(), isLittleEndian(), 0);
600 AbbrevDWO.reset(new DWARFDebugAbbrev());
601 AbbrevDWO->extract(abbrData);
602 return AbbrevDWO.get();
605 const DWARFDebugLoc *DWARFContext::getDebugLoc() {
609 DataExtractor LocData(getLocSection().Data, isLittleEndian(), 0);
610 Loc.reset(new DWARFDebugLoc(getLocSection().Relocs));
611 // assume all compile units have the same address byte size
612 if (getNumCompileUnits())
613 Loc->parse(LocData, getCompileUnitAtIndex(0)->getAddressByteSize());
617 const DWARFDebugLocDWO *DWARFContext::getDebugLocDWO() {
621 DataExtractor LocData(getLocDWOSection().Data, isLittleEndian(), 0);
622 LocDWO.reset(new DWARFDebugLocDWO());
623 LocDWO->parse(LocData);
627 const DWARFDebugAranges *DWARFContext::getDebugAranges() {
629 return Aranges.get();
631 Aranges.reset(new DWARFDebugAranges());
632 Aranges->generate(this);
633 return Aranges.get();
636 const DWARFDebugFrame *DWARFContext::getDebugFrame() {
638 return DebugFrame.get();
640 // There's a "bug" in the DWARFv3 standard with respect to the target address
641 // size within debug frame sections. While DWARF is supposed to be independent
642 // of its container, FDEs have fields with size being "target address size",
643 // which isn't specified in DWARF in general. It's only specified for CUs, but
644 // .eh_frame can appear without a .debug_info section. Follow the example of
645 // other tools (libdwarf) and extract this from the container (ObjectFile
646 // provides this information). This problem is fixed in DWARFv4
647 // See this dwarf-discuss discussion for more details:
648 // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
649 DataExtractor debugFrameData(getDebugFrameSection(), isLittleEndian(),
651 DebugFrame.reset(new DWARFDebugFrame(false /* IsEH */));
652 DebugFrame->parse(debugFrameData);
653 return DebugFrame.get();
656 const DWARFDebugFrame *DWARFContext::getEHFrame() {
658 return EHFrame.get();
660 DataExtractor debugFrameData(getEHFrameSection(), isLittleEndian(),
662 DebugFrame.reset(new DWARFDebugFrame(true /* IsEH */));
663 DebugFrame->parse(debugFrameData);
664 return DebugFrame.get();
667 const DWARFDebugMacro *DWARFContext::getDebugMacro() {
671 DataExtractor MacinfoData(getMacinfoSection(), isLittleEndian(), 0);
672 Macro.reset(new DWARFDebugMacro());
673 Macro->parse(MacinfoData);
677 const DWARFLineTable *
678 DWARFContext::getLineTableForUnit(DWARFUnit *U) {
680 Line.reset(new DWARFDebugLine(&getLineSection().Relocs));
682 auto UnitDIE = U->getUnitDIE();
686 auto Offset = toSectionOffset(UnitDIE.find(DW_AT_stmt_list));
688 return nullptr; // No line table for this compile unit.
690 uint32_t stmtOffset = *Offset + U->getLineTableOffset();
691 // See if the line table is cached.
692 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
695 // We have to parse it first.
696 DataExtractor lineData(U->getLineSection(), isLittleEndian(),
697 U->getAddressByteSize());
698 return Line->getOrParseLineTable(lineData, stmtOffset);
701 void DWARFContext::parseCompileUnits() {
702 CUs.parse(*this, getInfoSection());
705 void DWARFContext::parseTypeUnits() {
708 for (const auto &I : getTypesSections()) {
710 TUs.back().parse(*this, I.second);
714 void DWARFContext::parseDWOCompileUnits() {
715 DWOCUs.parseDWO(*this, getInfoDWOSection());
718 void DWARFContext::parseDWOTypeUnits() {
721 for (const auto &I : getTypesDWOSections()) {
722 DWOTUs.emplace_back();
723 DWOTUs.back().parseDWO(*this, I.second);
727 DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
729 return CUs.getUnitForOffset(Offset);
732 DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
733 // First, get the offset of the compile unit.
734 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
735 // Retrieve the compile unit.
736 return getCompileUnitForOffset(CUOffset);
739 static bool getFunctionNameAndStartLineForAddress(DWARFCompileUnit *CU,
741 FunctionNameKind Kind,
742 std::string &FunctionName,
743 uint32_t &StartLine) {
744 // The address may correspond to instruction in some inlined function,
745 // so we have to build the chain of inlined functions and take the
746 // name of the topmost function in it.
747 SmallVector<DWARFDie, 4> InlinedChain;
748 CU->getInlinedChainForAddress(Address, InlinedChain);
749 if (InlinedChain.empty())
752 const DWARFDie &DIE = InlinedChain[0];
753 bool FoundResult = false;
754 const char *Name = nullptr;
755 if (Kind != FunctionNameKind::None && (Name = DIE.getSubroutineName(Kind))) {
759 if (auto DeclLineResult = DIE.getDeclLine()) {
760 StartLine = DeclLineResult;
767 DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
768 DILineInfoSpecifier Spec) {
771 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
774 getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind,
777 if (Spec.FLIKind != FileLineInfoKind::None) {
778 if (const DWARFLineTable *LineTable = getLineTableForUnit(CU))
779 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
780 Spec.FLIKind, Result);
786 DWARFContext::getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
787 DILineInfoSpecifier Spec) {
788 DILineInfoTable Lines;
789 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
793 std::string FunctionName = "<invalid>";
794 uint32_t StartLine = 0;
795 getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind, FunctionName,
798 // If the Specifier says we don't need FileLineInfo, just
799 // return the top-most function at the starting address.
800 if (Spec.FLIKind == FileLineInfoKind::None) {
802 Result.FunctionName = FunctionName;
803 Result.StartLine = StartLine;
804 Lines.push_back(std::make_pair(Address, Result));
808 const DWARFLineTable *LineTable = getLineTableForUnit(CU);
810 // Get the index of row we're looking for in the line table.
811 std::vector<uint32_t> RowVector;
812 if (!LineTable->lookupAddressRange(Address, Size, RowVector))
815 for (uint32_t RowIndex : RowVector) {
816 // Take file number and line/column from the row.
817 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
819 LineTable->getFileNameByIndex(Row.File, CU->getCompilationDir(),
820 Spec.FLIKind, Result.FileName);
821 Result.FunctionName = FunctionName;
822 Result.Line = Row.Line;
823 Result.Column = Row.Column;
824 Result.StartLine = StartLine;
825 Lines.push_back(std::make_pair(Row.Address, Result));
832 DWARFContext::getInliningInfoForAddress(uint64_t Address,
833 DILineInfoSpecifier Spec) {
834 DIInliningInfo InliningInfo;
836 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
840 const DWARFLineTable *LineTable = nullptr;
841 SmallVector<DWARFDie, 4> InlinedChain;
842 CU->getInlinedChainForAddress(Address, InlinedChain);
843 if (InlinedChain.size() == 0) {
844 // If there is no DIE for address (e.g. it is in unavailable .dwo file),
845 // try to at least get file/line info from symbol table.
846 if (Spec.FLIKind != FileLineInfoKind::None) {
848 LineTable = getLineTableForUnit(CU);
850 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
851 Spec.FLIKind, Frame))
852 InliningInfo.addFrame(Frame);
857 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0, CallDiscriminator = 0;
858 for (uint32_t i = 0, n = InlinedChain.size(); i != n; i++) {
859 DWARFDie &FunctionDIE = InlinedChain[i];
861 // Get function name if necessary.
862 if (const char *Name = FunctionDIE.getSubroutineName(Spec.FNKind))
863 Frame.FunctionName = Name;
864 if (auto DeclLineResult = FunctionDIE.getDeclLine())
865 Frame.StartLine = DeclLineResult;
866 if (Spec.FLIKind != FileLineInfoKind::None) {
868 // For the topmost frame, initialize the line table of this
869 // compile unit and fetch file/line info from it.
870 LineTable = getLineTableForUnit(CU);
871 // For the topmost routine, get file/line info from line table.
873 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
874 Spec.FLIKind, Frame);
876 // Otherwise, use call file, call line and call column from
877 // previous DIE in inlined chain.
879 LineTable->getFileNameByIndex(CallFile, CU->getCompilationDir(),
880 Spec.FLIKind, Frame.FileName);
881 Frame.Line = CallLine;
882 Frame.Column = CallColumn;
883 Frame.Discriminator = CallDiscriminator;
885 // Get call file/line/column of a current DIE.
887 FunctionDIE.getCallerFrame(CallFile, CallLine, CallColumn,
891 InliningInfo.addFrame(Frame);
896 static Error createError(const Twine &Reason, llvm::Error E) {
897 return make_error<StringError>(Reason + toString(std::move(E)),
898 inconvertibleErrorCode());
901 /// Returns the address of symbol relocation used against. Used for futher
902 /// relocations computation. Symbol's section load address is taken in account if
903 /// LoadedObjectInfo interface is provided.
904 static Expected<uint64_t> getSymbolAddress(const object::ObjectFile &Obj,
905 const RelocationRef &Reloc,
906 const LoadedObjectInfo *L) {
908 object::section_iterator RSec = Obj.section_end();
909 object::symbol_iterator Sym = Reloc.getSymbol();
911 // First calculate the address of the symbol or section as it appears
912 // in the object file
913 if (Sym != Obj.symbol_end()) {
914 Expected<uint64_t> SymAddrOrErr = Sym->getAddress();
916 return createError("error: failed to compute symbol address: ",
917 SymAddrOrErr.takeError());
919 // Also remember what section this symbol is in for later
920 auto SectOrErr = Sym->getSection();
922 return createError("error: failed to get symbol section: ",
923 SectOrErr.takeError());
927 } else if (auto *MObj = dyn_cast<MachOObjectFile>(&Obj)) {
928 RSec = MObj->getRelocationSection(Reloc.getRawDataRefImpl());
929 Ret = RSec->getAddress();
932 // If we are given load addresses for the sections, we need to adjust:
933 // SymAddr = (Address of Symbol Or Section in File) -
934 // (Address of Section in File) +
935 // (Load Address of Section)
936 // RSec is now either the section being targeted or the section
937 // containing the symbol being targeted. In either case,
938 // we need to perform the same computation.
939 if (L && RSec != Obj.section_end())
940 if (uint64_t SectionLoadAddress = L->getSectionLoadAddress(*RSec))
941 Ret += SectionLoadAddress - RSec->getAddress();
945 static bool isRelocScattered(const object::ObjectFile &Obj,
946 const RelocationRef &Reloc) {
947 const MachOObjectFile *MachObj = dyn_cast<MachOObjectFile>(&Obj);
950 // MachO also has relocations that point to sections and
951 // scattered relocations.
952 auto RelocInfo = MachObj->getRelocation(Reloc.getRawDataRefImpl());
953 return MachObj->isRelocationScattered(RelocInfo);
956 DWARFContextInMemory::DWARFContextInMemory(const object::ObjectFile &Obj,
957 const LoadedObjectInfo *L)
958 : IsLittleEndian(Obj.isLittleEndian()),
959 AddressSize(Obj.getBytesInAddress()) {
960 for (const SectionRef &Section : Obj.sections()) {
962 Section.getName(name);
963 // Skip BSS and Virtual sections, they aren't interesting.
964 bool IsBSS = Section.isBSS();
967 bool IsVirtual = Section.isVirtual();
972 section_iterator RelocatedSection = Section.getRelocatedSection();
973 // Try to obtain an already relocated version of this section.
974 // Else use the unrelocated section from the object file. We'll have to
975 // apply relocations ourselves later.
976 if (!L || !L->getLoadedSectionContents(*RelocatedSection,data))
977 Section.getContents(data);
979 if (Decompressor::isCompressed(Section)) {
980 Expected<Decompressor> Decompressor =
981 Decompressor::create(name, data, IsLittleEndian, AddressSize == 8);
985 if (auto Err = Decompressor->decompress(Out))
987 UncompressedSections.emplace_back(std::move(Out));
988 data = UncompressedSections.back();
991 // Compressed sections names in GNU style starts from ".z",
992 // at this point section is decompressed and we drop compression prefix.
994 name.find_first_not_of("._z")); // Skip ".", "z" and "_" prefixes.
996 if (StringRef *SectionData = MapSectionToMember(name)) {
998 if (name == "debug_ranges") {
999 // FIXME: Use the other dwo range section when we emit it.
1000 RangeDWOSection.Data = data;
1002 } else if (name == "debug_types") {
1003 // Find debug_types data by section rather than name as there are
1004 // multiple, comdat grouped, debug_types sections.
1005 TypesSections[Section].Data = data;
1006 } else if (name == "debug_types.dwo") {
1007 TypesDWOSections[Section].Data = data;
1010 if (RelocatedSection == Obj.section_end())
1013 StringRef RelSecName;
1014 StringRef RelSecData;
1015 RelocatedSection->getName(RelSecName);
1017 // If the section we're relocating was relocated already by the JIT,
1018 // then we used the relocated version above, so we do not need to process
1019 // relocations for it now.
1020 if (L && L->getLoadedSectionContents(*RelocatedSection,RelSecData))
1023 // In Mach-o files, the relocations do not need to be applied if
1024 // there is no load offset to apply. The value read at the
1025 // relocation point already factors in the section address
1026 // (actually applying the relocations will produce wrong results
1027 // as the section address will be added twice).
1028 if (!L && isa<MachOObjectFile>(&Obj))
1031 RelSecName = RelSecName.substr(
1032 RelSecName.find_first_not_of("._")); // Skip . and _ prefixes.
1034 // TODO: Add support for relocations in other sections as needed.
1035 // Record relocations for the debug_info and debug_line sections.
1036 RelocAddrMap *Map = StringSwitch<RelocAddrMap*>(RelSecName)
1037 .Case("debug_info", &InfoSection.Relocs)
1038 .Case("debug_loc", &LocSection.Relocs)
1039 .Case("debug_info.dwo", &InfoDWOSection.Relocs)
1040 .Case("debug_line", &LineSection.Relocs)
1041 .Case("debug_ranges", &RangeSection.Relocs)
1042 .Case("apple_names", &AppleNamesSection.Relocs)
1043 .Case("apple_types", &AppleTypesSection.Relocs)
1044 .Case("apple_namespaces", &AppleNamespacesSection.Relocs)
1045 .Case("apple_namespac", &AppleNamespacesSection.Relocs)
1046 .Case("apple_objc", &AppleObjCSection.Relocs)
1049 // Find debug_types relocs by section rather than name as there are
1050 // multiple, comdat grouped, debug_types sections.
1051 if (RelSecName == "debug_types")
1052 Map = &TypesSections[*RelocatedSection].Relocs;
1053 else if (RelSecName == "debug_types.dwo")
1054 Map = &TypesDWOSections[*RelocatedSection].Relocs;
1059 if (Section.relocation_begin() != Section.relocation_end()) {
1060 uint64_t SectionSize = RelocatedSection->getSize();
1061 for (const RelocationRef &Reloc : Section.relocations()) {
1062 // FIXME: it's not clear how to correctly handle scattered
1064 if (isRelocScattered(Obj, Reloc))
1067 Expected<uint64_t> SymAddrOrErr = getSymbolAddress(Obj, Reloc, L);
1068 if (!SymAddrOrErr) {
1069 errs() << toString(SymAddrOrErr.takeError()) << '\n';
1073 object::RelocVisitor V(Obj);
1074 object::RelocToApply R(V.visit(Reloc.getType(), Reloc, *SymAddrOrErr));
1076 SmallString<32> Name;
1077 Reloc.getTypeName(Name);
1078 errs() << "error: failed to compute relocation: "
1082 uint64_t Address = Reloc.getOffset();
1083 if (Address + R.Width > SectionSize) {
1084 errs() << "error: " << R.Width << "-byte relocation starting "
1085 << Address << " bytes into section " << name << " which is "
1086 << SectionSize << " bytes long.\n";
1090 errs() << "error: can't handle a relocation of more than 8 bytes at "
1094 DEBUG(dbgs() << "Writing " << format("%p", R.Value)
1095 << " at " << format("%p", Address)
1096 << " with width " << format("%d", R.Width)
1098 Map->insert(std::make_pair(Address, std::make_pair(R.Width, R.Value)));
1104 DWARFContextInMemory::DWARFContextInMemory(
1105 const StringMap<std::unique_ptr<MemoryBuffer>> &Sections, uint8_t AddrSize,
1106 bool isLittleEndian)
1107 : IsLittleEndian(isLittleEndian), AddressSize(AddrSize) {
1108 for (const auto &SecIt : Sections) {
1109 if (StringRef *SectionData = MapSectionToMember(SecIt.first()))
1110 *SectionData = SecIt.second->getBuffer();
1114 StringRef *DWARFContextInMemory::MapSectionToMember(StringRef Name) {
1115 return StringSwitch<StringRef *>(Name)
1116 .Case("debug_info", &InfoSection.Data)
1117 .Case("debug_abbrev", &AbbrevSection)
1118 .Case("debug_loc", &LocSection.Data)
1119 .Case("debug_line", &LineSection.Data)
1120 .Case("debug_aranges", &ARangeSection)
1121 .Case("debug_frame", &DebugFrameSection)
1122 .Case("eh_frame", &EHFrameSection)
1123 .Case("debug_str", &StringSection)
1124 .Case("debug_ranges", &RangeSection.Data)
1125 .Case("debug_macinfo", &MacinfoSection)
1126 .Case("debug_pubnames", &PubNamesSection)
1127 .Case("debug_pubtypes", &PubTypesSection)
1128 .Case("debug_gnu_pubnames", &GnuPubNamesSection)
1129 .Case("debug_gnu_pubtypes", &GnuPubTypesSection)
1130 .Case("debug_info.dwo", &InfoDWOSection.Data)
1131 .Case("debug_abbrev.dwo", &AbbrevDWOSection)
1132 .Case("debug_loc.dwo", &LocDWOSection.Data)
1133 .Case("debug_line.dwo", &LineDWOSection.Data)
1134 .Case("debug_str.dwo", &StringDWOSection)
1135 .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
1136 .Case("debug_addr", &AddrSection)
1137 .Case("apple_names", &AppleNamesSection.Data)
1138 .Case("apple_types", &AppleTypesSection.Data)
1139 .Case("apple_namespaces", &AppleNamespacesSection.Data)
1140 .Case("apple_namespac", &AppleNamespacesSection.Data)
1141 .Case("apple_objc", &AppleObjCSection.Data)
1142 .Case("debug_cu_index", &CUIndexSection)
1143 .Case("debug_tu_index", &TUIndexSection)
1144 .Case("gdb_index", &GdbIndexSection)
1145 // Any more debug info sections go here.
1149 void DWARFContextInMemory::anchor() {}