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 "DWARFContext.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/ADT/StringSwitch.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/Support/Compression.h"
15 #include "llvm/Support/Dwarf.h"
16 #include "llvm/Support/Format.h"
17 #include "llvm/Support/Path.h"
18 #include "llvm/Support/raw_ostream.h"
21 using namespace dwarf;
22 using namespace object;
24 typedef DWARFDebugLine::LineTable DWARFLineTable;
26 void DWARFContext::dump(raw_ostream &OS, DIDumpType DumpType) {
27 if (DumpType == DIDT_All || DumpType == DIDT_Abbrev) {
28 OS << ".debug_abbrev contents:\n";
29 getDebugAbbrev()->dump(OS);
32 if (DumpType == DIDT_All || DumpType == DIDT_Info) {
33 OS << "\n.debug_info contents:\n";
34 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i)
35 getCompileUnitAtIndex(i)->dump(OS);
38 if (DumpType == DIDT_All || DumpType == DIDT_Frames) {
39 OS << "\n.debug_frame contents:\n";
40 getDebugFrame()->dump(OS);
44 if (DumpType == DIDT_All || DumpType == DIDT_Aranges) {
45 OS << "\n.debug_aranges contents:\n";
46 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
47 DWARFDebugArangeSet set;
48 while (set.extract(arangesData, &offset))
52 uint8_t savedAddressByteSize = 0;
53 if (DumpType == DIDT_All || DumpType == DIDT_Line) {
54 OS << "\n.debug_line contents:\n";
55 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i) {
56 DWARFCompileUnit *cu = getCompileUnitAtIndex(i);
57 savedAddressByteSize = cu->getAddressByteSize();
59 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
61 if (stmtOffset != -1U) {
62 DataExtractor lineData(getLineSection(), isLittleEndian(),
63 savedAddressByteSize);
64 DWARFDebugLine::DumpingState state(OS);
65 DWARFDebugLine::parseStatementTable(lineData, &lineRelocMap(), &stmtOffset, state);
70 if (DumpType == DIDT_All || DumpType == DIDT_Str) {
71 OS << "\n.debug_str contents:\n";
72 DataExtractor strData(getStringSection(), isLittleEndian(), 0);
74 uint32_t strOffset = 0;
75 while (const char *s = strData.getCStr(&offset)) {
76 OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
81 if (DumpType == DIDT_All || DumpType == DIDT_Ranges) {
82 OS << "\n.debug_ranges contents:\n";
83 // In fact, different compile units may have different address byte
84 // sizes, but for simplicity we just use the address byte size of the last
85 // compile unit (there is no easy and fast way to associate address range
86 // list and the compile unit it describes).
87 DataExtractor rangesData(getRangeSection(), isLittleEndian(),
88 savedAddressByteSize);
90 DWARFDebugRangeList rangeList;
91 while (rangeList.extract(rangesData, &offset))
95 if (DumpType == DIDT_All || DumpType == DIDT_Pubnames) {
96 OS << "\n.debug_pubnames contents:\n";
97 DataExtractor pubNames(getPubNamesSection(), isLittleEndian(), 0);
99 OS << "Length: " << pubNames.getU32(&offset) << "\n";
100 OS << "Version: " << pubNames.getU16(&offset) << "\n";
101 OS << "Offset in .debug_info: " << pubNames.getU32(&offset) << "\n";
102 OS << "Size: " << pubNames.getU32(&offset) << "\n";
103 OS << "\n Offset Name\n";
104 while (offset < getPubNamesSection().size()) {
105 uint32_t n = pubNames.getU32(&offset);
108 OS << format("%8x ", n);
109 OS << pubNames.getCStr(&offset) << "\n";
113 if (DumpType == DIDT_All || DumpType == DIDT_AbbrevDwo) {
114 const DWARFDebugAbbrev *D = getDebugAbbrevDWO();
116 OS << "\n.debug_abbrev.dwo contents:\n";
117 getDebugAbbrevDWO()->dump(OS);
121 if (DumpType == DIDT_All || DumpType == DIDT_InfoDwo)
122 if (getNumDWOCompileUnits()) {
123 OS << "\n.debug_info.dwo contents:\n";
124 for (unsigned i = 0, e = getNumDWOCompileUnits(); i != e; ++i)
125 getDWOCompileUnitAtIndex(i)->dump(OS);
128 if (DumpType == DIDT_All || DumpType == DIDT_StrDwo)
129 if (!getStringDWOSection().empty()) {
130 OS << "\n.debug_str.dwo contents:\n";
131 DataExtractor strDWOData(getStringDWOSection(), isLittleEndian(), 0);
133 uint32_t strDWOOffset = 0;
134 while (const char *s = strDWOData.getCStr(&offset)) {
135 OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
136 strDWOOffset = offset;
140 if (DumpType == DIDT_All || DumpType == DIDT_StrOffsetsDwo)
141 if (!getStringOffsetDWOSection().empty()) {
142 OS << "\n.debug_str_offsets.dwo contents:\n";
143 DataExtractor strOffsetExt(getStringOffsetDWOSection(), isLittleEndian(), 0);
145 uint64_t size = getStringOffsetDWOSection().size();
146 while (offset < size) {
147 OS << format("0x%8.8x: ", offset);
148 OS << format("%8.8x\n", strOffsetExt.getU32(&offset));
153 const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
157 DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
159 Abbrev.reset(new DWARFDebugAbbrev());
160 Abbrev->parse(abbrData);
164 const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
166 return AbbrevDWO.get();
168 DataExtractor abbrData(getAbbrevDWOSection(), isLittleEndian(), 0);
169 AbbrevDWO.reset(new DWARFDebugAbbrev());
170 AbbrevDWO->parse(abbrData);
171 return AbbrevDWO.get();
174 const DWARFDebugAranges *DWARFContext::getDebugAranges() {
176 return Aranges.get();
178 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
180 Aranges.reset(new DWARFDebugAranges());
181 Aranges->extract(arangesData);
182 // Generate aranges from DIEs: even if .debug_aranges section is present,
183 // it may describe only a small subset of compilation units, so we need to
184 // manually build aranges for the rest of them.
185 Aranges->generate(this);
186 return Aranges.get();
189 const DWARFDebugFrame *DWARFContext::getDebugFrame() {
191 return DebugFrame.get();
193 // There's a "bug" in the DWARFv3 standard with respect to the target address
194 // size within debug frame sections. While DWARF is supposed to be independent
195 // of its container, FDEs have fields with size being "target address size",
196 // which isn't specified in DWARF in general. It's only specified for CUs, but
197 // .eh_frame can appear without a .debug_info section. Follow the example of
198 // other tools (libdwarf) and extract this from the container (ObjectFile
199 // provides this information). This problem is fixed in DWARFv4
200 // See this dwarf-discuss discussion for more details:
201 // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
202 DataExtractor debugFrameData(getDebugFrameSection(), isLittleEndian(),
204 DebugFrame.reset(new DWARFDebugFrame());
205 DebugFrame->parse(debugFrameData);
206 return DebugFrame.get();
209 const DWARFLineTable *
210 DWARFContext::getLineTableForCompileUnit(DWARFCompileUnit *cu) {
212 Line.reset(new DWARFDebugLine(&lineRelocMap()));
214 unsigned stmtOffset =
215 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
217 if (stmtOffset == -1U)
218 return 0; // No line table for this compile unit.
220 // See if the line table is cached.
221 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
224 // We have to parse it first.
225 DataExtractor lineData(getLineSection(), isLittleEndian(),
226 cu->getAddressByteSize());
227 return Line->getOrParseLineTable(lineData, stmtOffset);
230 void DWARFContext::parseCompileUnits() {
232 const DataExtractor &DIData = DataExtractor(getInfoSection(),
233 isLittleEndian(), 0);
234 while (DIData.isValidOffset(offset)) {
235 CUs.push_back(DWARFCompileUnit(getDebugAbbrev(), getInfoSection(),
236 getAbbrevSection(), getRangeSection(),
237 getStringSection(), StringRef(),
241 if (!CUs.back().extract(DIData, &offset)) {
246 offset = CUs.back().getNextCompileUnitOffset();
250 void DWARFContext::parseDWOCompileUnits() {
252 const DataExtractor &DIData = DataExtractor(getInfoDWOSection(),
253 isLittleEndian(), 0);
254 while (DIData.isValidOffset(offset)) {
255 DWOCUs.push_back(DWARFCompileUnit(getDebugAbbrevDWO(), getInfoDWOSection(),
256 getAbbrevDWOSection(),
257 getRangeDWOSection(),
258 getStringDWOSection(),
259 getStringOffsetDWOSection(),
263 if (!DWOCUs.back().extract(DIData, &offset)) {
268 offset = DWOCUs.back().getNextCompileUnitOffset();
273 struct OffsetComparator {
274 bool operator()(const DWARFCompileUnit &LHS,
275 const DWARFCompileUnit &RHS) const {
276 return LHS.getOffset() < RHS.getOffset();
278 bool operator()(const DWARFCompileUnit &LHS, uint32_t RHS) const {
279 return LHS.getOffset() < RHS;
281 bool operator()(uint32_t LHS, const DWARFCompileUnit &RHS) const {
282 return LHS < RHS.getOffset();
287 DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
291 DWARFCompileUnit *CU = std::lower_bound(CUs.begin(), CUs.end(), Offset,
298 DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
299 // First, get the offset of the compile unit.
300 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
301 // Retrieve the compile unit.
302 return getCompileUnitForOffset(CUOffset);
305 static bool getFileNameForCompileUnit(DWARFCompileUnit *CU,
306 const DWARFLineTable *LineTable,
308 bool NeedsAbsoluteFilePath,
309 std::string &FileName) {
312 !LineTable->getFileNameByIndex(FileIndex, NeedsAbsoluteFilePath,
315 if (NeedsAbsoluteFilePath && sys::path::is_relative(FileName)) {
316 // We may still need to append compilation directory of compile unit.
317 SmallString<16> AbsolutePath;
318 if (const char *CompilationDir = CU->getCompilationDir()) {
319 sys::path::append(AbsolutePath, CompilationDir);
321 sys::path::append(AbsolutePath, FileName);
322 FileName = AbsolutePath.str();
327 static bool getFileLineInfoForCompileUnit(DWARFCompileUnit *CU,
328 const DWARFLineTable *LineTable,
330 bool NeedsAbsoluteFilePath,
331 std::string &FileName,
332 uint32_t &Line, uint32_t &Column) {
333 if (CU == 0 || LineTable == 0)
335 // Get the index of row we're looking for in the line table.
336 uint32_t RowIndex = LineTable->lookupAddress(Address);
339 // Take file number and line/column from the row.
340 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
341 if (!getFileNameForCompileUnit(CU, LineTable, Row.File,
342 NeedsAbsoluteFilePath, FileName))
349 DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
350 DILineInfoSpecifier Specifier) {
351 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
354 std::string FileName = "<invalid>";
355 std::string FunctionName = "<invalid>";
358 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
359 // The address may correspond to instruction in some inlined function,
360 // so we have to build the chain of inlined functions and take the
361 // name of the topmost function in it.
362 const DWARFDebugInfoEntryMinimal::InlinedChain &InlinedChain =
363 CU->getInlinedChainForAddress(Address);
364 if (InlinedChain.size() > 0) {
365 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain[0];
366 if (const char *Name = TopFunctionDIE.getSubroutineName(CU))
370 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
371 const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
372 const bool NeedsAbsoluteFilePath =
373 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
374 getFileLineInfoForCompileUnit(CU, LineTable, Address,
375 NeedsAbsoluteFilePath,
376 FileName, Line, Column);
378 return DILineInfo(StringRef(FileName), StringRef(FunctionName),
382 DILineInfoTable DWARFContext::getLineInfoForAddressRange(uint64_t Address,
384 DILineInfoSpecifier Specifier) {
385 DILineInfoTable Lines;
386 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
390 std::string FunctionName = "<invalid>";
391 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
392 // The address may correspond to instruction in some inlined function,
393 // so we have to build the chain of inlined functions and take the
394 // name of the topmost function in it.
395 const DWARFDebugInfoEntryMinimal::InlinedChain &InlinedChain =
396 CU->getInlinedChainForAddress(Address);
397 if (InlinedChain.size() > 0) {
398 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain[0];
399 if (const char *Name = TopFunctionDIE.getSubroutineName(CU))
404 StringRef FuncNameRef = StringRef(FunctionName);
406 // If the Specifier says we don't need FileLineInfo, just
407 // return the top-most function at the starting address.
408 if (!Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
409 Lines.push_back(std::make_pair(Address,
410 DILineInfo(StringRef("<invalid>"),
411 FuncNameRef, 0, 0)));
415 const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
416 const bool NeedsAbsoluteFilePath =
417 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
419 // Get the index of row we're looking for in the line table.
420 std::vector<uint32_t> RowVector;
421 if (!LineTable->lookupAddressRange(Address, Size, RowVector))
424 uint32_t NumRows = RowVector.size();
425 for (uint32_t i = 0; i < NumRows; ++i) {
426 uint32_t RowIndex = RowVector[i];
427 // Take file number and line/column from the row.
428 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
429 std::string FileName = "<invalid>";
430 getFileNameForCompileUnit(CU, LineTable, Row.File,
431 NeedsAbsoluteFilePath, FileName);
432 Lines.push_back(std::make_pair(Row.Address,
433 DILineInfo(StringRef(FileName),
434 FuncNameRef, Row.Line, Row.Column)));
440 DIInliningInfo DWARFContext::getInliningInfoForAddress(uint64_t Address,
441 DILineInfoSpecifier Specifier) {
442 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
444 return DIInliningInfo();
446 const DWARFDebugInfoEntryMinimal::InlinedChain &InlinedChain =
447 CU->getInlinedChainForAddress(Address);
448 if (InlinedChain.size() == 0)
449 return DIInliningInfo();
451 DIInliningInfo InliningInfo;
452 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0;
453 const DWARFLineTable *LineTable = 0;
454 for (uint32_t i = 0, n = InlinedChain.size(); i != n; i++) {
455 const DWARFDebugInfoEntryMinimal &FunctionDIE = InlinedChain[i];
456 std::string FileName = "<invalid>";
457 std::string FunctionName = "<invalid>";
460 // Get function name if necessary.
461 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
462 if (const char *Name = FunctionDIE.getSubroutineName(CU))
465 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
466 const bool NeedsAbsoluteFilePath =
467 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
469 // For the topmost frame, initialize the line table of this
470 // compile unit and fetch file/line info from it.
471 LineTable = getLineTableForCompileUnit(CU);
472 // For the topmost routine, get file/line info from line table.
473 getFileLineInfoForCompileUnit(CU, LineTable, Address,
474 NeedsAbsoluteFilePath,
475 FileName, Line, Column);
477 // Otherwise, use call file, call line and call column from
478 // previous DIE in inlined chain.
479 getFileNameForCompileUnit(CU, LineTable, CallFile,
480 NeedsAbsoluteFilePath, FileName);
484 // Get call file/line/column of a current DIE.
486 FunctionDIE.getCallerFrame(CU, CallFile, CallLine, CallColumn);
489 DILineInfo Frame(StringRef(FileName), StringRef(FunctionName),
491 InliningInfo.addFrame(Frame);
496 static bool consumeCompressedDebugSectionHeader(StringRef &data,
497 uint64_t &OriginalSize) {
498 // Consume "ZLIB" prefix.
499 if (!data.startswith("ZLIB"))
501 data = data.substr(4);
502 // Consume uncompressed section size (big-endian 8 bytes).
503 DataExtractor extractor(data, false, 8);
505 OriginalSize = extractor.getU64(&Offset);
508 data = data.substr(Offset);
512 DWARFContextInMemory::DWARFContextInMemory(object::ObjectFile *Obj) :
513 IsLittleEndian(Obj->isLittleEndian()),
514 AddressSize(Obj->getBytesInAddress()) {
516 for (object::section_iterator i = Obj->begin_sections(),
517 e = Obj->end_sections();
518 i != e; i.increment(ec)) {
522 i->getContents(data);
524 name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes.
526 // Check if debug info section is compressed with zlib.
527 if (name.startswith("zdebug_")) {
528 uint64_t OriginalSize;
529 if (!zlib::isAvailable() ||
530 !consumeCompressedDebugSectionHeader(data, OriginalSize))
532 OwningPtr<MemoryBuffer> UncompressedSection;
533 if (zlib::uncompress(data, UncompressedSection, OriginalSize) !=
536 // Make data point to uncompressed section contents and save its contents.
537 name = name.substr(1);
538 data = UncompressedSection->getBuffer();
539 UncompressedSections.push_back(UncompressedSection.take());
542 StringRef *Section = StringSwitch<StringRef*>(name)
543 .Case("debug_info", &InfoSection)
544 .Case("debug_abbrev", &AbbrevSection)
545 .Case("debug_line", &LineSection)
546 .Case("debug_aranges", &ARangeSection)
547 .Case("debug_frame", &DebugFrameSection)
548 .Case("debug_str", &StringSection)
549 .Case("debug_ranges", &RangeSection)
550 .Case("debug_pubnames", &PubNamesSection)
551 .Case("debug_info.dwo", &InfoDWOSection)
552 .Case("debug_abbrev.dwo", &AbbrevDWOSection)
553 .Case("debug_str.dwo", &StringDWOSection)
554 .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
555 .Case("debug_addr", &AddrSection)
556 // Any more debug info sections go here.
560 if (name == "debug_ranges") {
561 // FIXME: Use the other dwo range section when we emit it.
562 RangeDWOSection = data;
566 section_iterator RelocatedSection = i->getRelocatedSection();
567 if (RelocatedSection == Obj->end_sections())
570 StringRef RelSecName;
571 RelocatedSection->getName(RelSecName);
572 RelSecName = RelSecName.substr(
573 RelSecName.find_first_not_of("._")); // Skip . and _ prefixes.
575 // TODO: Add support for relocations in other sections as needed.
576 // Record relocations for the debug_info and debug_line sections.
577 RelocAddrMap *Map = StringSwitch<RelocAddrMap*>(RelSecName)
578 .Case("debug_info", &InfoRelocMap)
579 .Case("debug_info.dwo", &InfoDWORelocMap)
580 .Case("debug_line", &LineRelocMap)
585 if (i->begin_relocations() != i->end_relocations()) {
586 uint64_t SectionSize;
587 RelocatedSection->getSize(SectionSize);
588 for (object::relocation_iterator reloc_i = i->begin_relocations(),
589 reloc_e = i->end_relocations();
590 reloc_i != reloc_e; reloc_i.increment(ec)) {
592 reloc_i->getOffset(Address);
594 reloc_i->getType(Type);
595 uint64_t SymAddr = 0;
596 // ELF relocations may need the symbol address
598 object::SymbolRef Sym;
599 reloc_i->getSymbol(Sym);
600 Sym.getAddress(SymAddr);
603 object::RelocVisitor V(Obj->getFileFormatName());
604 // The section address is always 0 for debug sections.
605 object::RelocToApply R(V.visit(Type, *reloc_i, 0, SymAddr));
607 SmallString<32> Name;
608 error_code ec(reloc_i->getTypeName(Name));
610 errs() << "Aaaaaa! Nameless relocation! Aaaaaa!\n";
612 errs() << "error: failed to compute relocation: "
617 if (Address + R.Width > SectionSize) {
618 errs() << "error: " << R.Width << "-byte relocation starting "
619 << Address << " bytes into section " << name << " which is "
620 << SectionSize << " bytes long.\n";
624 errs() << "error: can't handle a relocation of more than 8 bytes at "
628 DEBUG(dbgs() << "Writing " << format("%p", R.Value)
629 << " at " << format("%p", Address)
630 << " with width " << format("%d", R.Width)
632 Map->insert(std::make_pair(Address, std::make_pair(R.Width, R.Value)));
638 DWARFContextInMemory::~DWARFContextInMemory() {
639 DeleteContainerPointers(UncompressedSections);
642 void DWARFContextInMemory::anchor() { }