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 DWARFContext::~DWARFContext() {
27 DeleteContainerPointers(CUs);
28 DeleteContainerPointers(TUs);
29 DeleteContainerPointers(DWOCUs);
32 static void dumpPubSection(raw_ostream &OS, StringRef Name, StringRef Data,
33 bool LittleEndian, bool GnuStyle) {
34 OS << "\n." << Name << " contents:\n";
35 DataExtractor pubNames(Data, LittleEndian, 0);
37 OS << "Length: " << pubNames.getU32(&offset) << "\n";
38 OS << "Version: " << pubNames.getU16(&offset) << "\n";
39 OS << "Offset in .debug_info: " << pubNames.getU32(&offset) << "\n";
40 OS << "Size: " << pubNames.getU32(&offset) << "\n";
42 OS << "Offset Linkage Kind Name\n";
44 OS << "Offset Name\n";
46 while (offset < Data.size()) {
47 uint32_t dieRef = pubNames.getU32(&offset);
50 OS << format("0x%8.8x ", dieRef);
52 PubIndexEntryDescriptor desc(pubNames.getU8(&offset));
53 OS << format("%-8s", dwarf::GDBIndexEntryLinkageString(desc.Linkage))
54 << ' ' << format("%-8s", dwarf::GDBIndexEntryKindString(desc.Kind))
57 OS << '\"' << pubNames.getCStr(&offset) << "\"\n";
61 void DWARFContext::dump(raw_ostream &OS, DIDumpType DumpType) {
62 if (DumpType == DIDT_All || DumpType == DIDT_Abbrev) {
63 OS << ".debug_abbrev contents:\n";
64 getDebugAbbrev()->dump(OS);
67 if (DumpType == DIDT_All || DumpType == DIDT_Info) {
68 OS << "\n.debug_info contents:\n";
69 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i)
70 getCompileUnitAtIndex(i)->dump(OS);
73 if (DumpType == DIDT_All || DumpType == DIDT_Types) {
74 OS << "\n.debug_types contents:\n";
75 for (unsigned i = 0, e = getNumTypeUnits(); i != e; ++i)
76 getTypeUnitAtIndex(i)->dump(OS);
79 if (DumpType == DIDT_All || DumpType == DIDT_Loc) {
80 OS << "\n.debug_loc contents:\n";
81 getDebugLoc()->dump(OS);
84 if (DumpType == DIDT_All || DumpType == DIDT_Frames) {
85 OS << "\n.debug_frame contents:\n";
86 getDebugFrame()->dump(OS);
90 if (DumpType == DIDT_All || DumpType == DIDT_Aranges) {
91 OS << "\n.debug_aranges contents:\n";
92 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
93 DWARFDebugArangeSet set;
94 while (set.extract(arangesData, &offset))
98 uint8_t savedAddressByteSize = 0;
99 if (DumpType == DIDT_All || DumpType == DIDT_Line) {
100 OS << "\n.debug_line contents:\n";
101 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i) {
102 DWARFCompileUnit *cu = getCompileUnitAtIndex(i);
103 savedAddressByteSize = cu->getAddressByteSize();
104 unsigned stmtOffset =
105 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
107 if (stmtOffset != -1U) {
108 DataExtractor lineData(getLineSection().Data, isLittleEndian(),
109 savedAddressByteSize);
110 DWARFDebugLine::DumpingState state(OS);
111 DWARFDebugLine::parseStatementTable(lineData, &getLineSection().Relocs, &stmtOffset, state);
116 if (DumpType == DIDT_All || DumpType == DIDT_Str) {
117 OS << "\n.debug_str contents:\n";
118 DataExtractor strData(getStringSection(), isLittleEndian(), 0);
120 uint32_t strOffset = 0;
121 while (const char *s = strData.getCStr(&offset)) {
122 OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
127 if (DumpType == DIDT_All || DumpType == DIDT_Ranges) {
128 OS << "\n.debug_ranges contents:\n";
129 // In fact, different compile units may have different address byte
130 // sizes, but for simplicity we just use the address byte size of the last
131 // compile unit (there is no easy and fast way to associate address range
132 // list and the compile unit it describes).
133 DataExtractor rangesData(getRangeSection(), isLittleEndian(),
134 savedAddressByteSize);
136 DWARFDebugRangeList rangeList;
137 while (rangeList.extract(rangesData, &offset))
141 if (DumpType == DIDT_All || DumpType == DIDT_Pubnames)
142 dumpPubSection(OS, "debug_pubnames", getPubNamesSection(),
143 isLittleEndian(), false);
145 if (DumpType == DIDT_All || DumpType == DIDT_Pubtypes)
146 dumpPubSection(OS, "debug_pubtypes", getPubTypesSection(),
147 isLittleEndian(), false);
149 if (DumpType == DIDT_All || DumpType == DIDT_GnuPubnames)
150 dumpPubSection(OS, "debug_gnu_pubnames", getGnuPubNamesSection(),
151 isLittleEndian(), true /* GnuStyle */);
153 if (DumpType == DIDT_All || DumpType == DIDT_GnuPubtypes)
154 dumpPubSection(OS, "debug_gnu_pubtypes", getGnuPubTypesSection(),
155 isLittleEndian(), true /* GnuStyle */);
157 if (DumpType == DIDT_All || DumpType == DIDT_AbbrevDwo) {
158 const DWARFDebugAbbrev *D = getDebugAbbrevDWO();
160 OS << "\n.debug_abbrev.dwo contents:\n";
161 getDebugAbbrevDWO()->dump(OS);
165 if (DumpType == DIDT_All || DumpType == DIDT_InfoDwo)
166 if (getNumDWOCompileUnits()) {
167 OS << "\n.debug_info.dwo contents:\n";
168 for (unsigned i = 0, e = getNumDWOCompileUnits(); i != e; ++i)
169 getDWOCompileUnitAtIndex(i)->dump(OS);
172 if (DumpType == DIDT_All || DumpType == DIDT_StrDwo)
173 if (!getStringDWOSection().empty()) {
174 OS << "\n.debug_str.dwo contents:\n";
175 DataExtractor strDWOData(getStringDWOSection(), isLittleEndian(), 0);
177 uint32_t strDWOOffset = 0;
178 while (const char *s = strDWOData.getCStr(&offset)) {
179 OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
180 strDWOOffset = offset;
184 if (DumpType == DIDT_All || DumpType == DIDT_StrOffsetsDwo)
185 if (!getStringOffsetDWOSection().empty()) {
186 OS << "\n.debug_str_offsets.dwo contents:\n";
187 DataExtractor strOffsetExt(getStringOffsetDWOSection(), isLittleEndian(), 0);
189 uint64_t size = getStringOffsetDWOSection().size();
190 while (offset < size) {
191 OS << format("0x%8.8x: ", offset);
192 OS << format("%8.8x\n", strOffsetExt.getU32(&offset));
197 const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
201 DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
203 Abbrev.reset(new DWARFDebugAbbrev());
204 Abbrev->parse(abbrData);
208 const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
210 return AbbrevDWO.get();
212 DataExtractor abbrData(getAbbrevDWOSection(), isLittleEndian(), 0);
213 AbbrevDWO.reset(new DWARFDebugAbbrev());
214 AbbrevDWO->parse(abbrData);
215 return AbbrevDWO.get();
218 const DWARFDebugLoc *DWARFContext::getDebugLoc() {
222 DataExtractor LocData(getLocSection().Data, isLittleEndian(), 0);
223 Loc.reset(new DWARFDebugLoc(getLocSection().Relocs));
224 // assume all compile units have the same address byte size
225 if (getNumCompileUnits())
226 Loc->parse(LocData, getCompileUnitAtIndex(0)->getAddressByteSize());
230 const DWARFDebugAranges *DWARFContext::getDebugAranges() {
232 return Aranges.get();
234 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
236 Aranges.reset(new DWARFDebugAranges());
237 Aranges->extract(arangesData);
238 // Generate aranges from DIEs: even if .debug_aranges section is present,
239 // it may describe only a small subset of compilation units, so we need to
240 // manually build aranges for the rest of them.
241 Aranges->generate(this);
242 return Aranges.get();
245 const DWARFDebugFrame *DWARFContext::getDebugFrame() {
247 return DebugFrame.get();
249 // There's a "bug" in the DWARFv3 standard with respect to the target address
250 // size within debug frame sections. While DWARF is supposed to be independent
251 // of its container, FDEs have fields with size being "target address size",
252 // which isn't specified in DWARF in general. It's only specified for CUs, but
253 // .eh_frame can appear without a .debug_info section. Follow the example of
254 // other tools (libdwarf) and extract this from the container (ObjectFile
255 // provides this information). This problem is fixed in DWARFv4
256 // See this dwarf-discuss discussion for more details:
257 // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
258 DataExtractor debugFrameData(getDebugFrameSection(), isLittleEndian(),
260 DebugFrame.reset(new DWARFDebugFrame());
261 DebugFrame->parse(debugFrameData);
262 return DebugFrame.get();
265 const DWARFLineTable *
266 DWARFContext::getLineTableForCompileUnit(DWARFCompileUnit *cu) {
268 Line.reset(new DWARFDebugLine(&getLineSection().Relocs));
270 unsigned stmtOffset =
271 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
273 if (stmtOffset == -1U)
274 return 0; // No line table for this compile unit.
276 // See if the line table is cached.
277 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
280 // We have to parse it first.
281 DataExtractor lineData(getLineSection().Data, isLittleEndian(),
282 cu->getAddressByteSize());
283 return Line->getOrParseLineTable(lineData, stmtOffset);
286 void DWARFContext::parseCompileUnits() {
288 const DataExtractor &DIData = DataExtractor(getInfoSection().Data,
289 isLittleEndian(), 0);
290 while (DIData.isValidOffset(offset)) {
291 OwningPtr<DWARFCompileUnit> CU(new DWARFCompileUnit(
292 getDebugAbbrev(), getInfoSection().Data, getAbbrevSection(),
293 getRangeSection(), getStringSection(), StringRef(), getAddrSection(),
294 &getInfoSection().Relocs, isLittleEndian()));
295 if (!CU->extract(DIData, &offset)) {
298 CUs.push_back(CU.take());
299 offset = CUs.back()->getNextUnitOffset();
303 void DWARFContext::parseTypeUnits() {
304 const std::map<object::SectionRef, Section> &Sections = getTypesSections();
305 for (std::map<object::SectionRef, Section>::const_iterator
306 I = Sections.begin(),
310 const DataExtractor &DIData =
311 DataExtractor(I->second.Data, isLittleEndian(), 0);
312 while (DIData.isValidOffset(offset)) {
313 OwningPtr<DWARFTypeUnit> TU(new DWARFTypeUnit(
314 getDebugAbbrev(), I->second.Data, getAbbrevSection(),
315 getRangeSection(), getStringSection(), StringRef(), getAddrSection(),
316 &I->second.Relocs, isLittleEndian()));
317 if (!TU->extract(DIData, &offset))
319 TUs.push_back(TU.take());
320 offset = TUs.back()->getNextUnitOffset();
325 void DWARFContext::parseDWOCompileUnits() {
327 const DataExtractor &DIData =
328 DataExtractor(getInfoDWOSection().Data, isLittleEndian(), 0);
329 while (DIData.isValidOffset(offset)) {
330 OwningPtr<DWARFCompileUnit> DWOCU(new DWARFCompileUnit(
331 getDebugAbbrevDWO(), getInfoDWOSection().Data, getAbbrevDWOSection(),
332 getRangeDWOSection(), getStringDWOSection(),
333 getStringOffsetDWOSection(), getAddrSection(),
334 &getInfoDWOSection().Relocs, isLittleEndian()));
335 if (!DWOCU->extract(DIData, &offset)) {
338 DWOCUs.push_back(DWOCU.take());
339 offset = DWOCUs.back()->getNextUnitOffset();
344 struct OffsetComparator {
345 bool operator()(const DWARFCompileUnit *LHS,
346 const DWARFCompileUnit *RHS) const {
347 return LHS->getOffset() < RHS->getOffset();
349 bool operator()(const DWARFCompileUnit *LHS, uint32_t RHS) const {
350 return LHS->getOffset() < RHS;
352 bool operator()(uint32_t LHS, const DWARFCompileUnit *RHS) const {
353 return LHS < RHS->getOffset();
358 DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
362 DWARFCompileUnit **CU =
363 std::lower_bound(CUs.begin(), CUs.end(), Offset, OffsetComparator());
364 if (CU != CUs.end()) {
370 DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
371 // First, get the offset of the compile unit.
372 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
373 // Retrieve the compile unit.
374 return getCompileUnitForOffset(CUOffset);
377 static bool getFileNameForCompileUnit(DWARFCompileUnit *CU,
378 const DWARFLineTable *LineTable,
380 bool NeedsAbsoluteFilePath,
381 std::string &FileName) {
384 !LineTable->getFileNameByIndex(FileIndex, NeedsAbsoluteFilePath,
387 if (NeedsAbsoluteFilePath && sys::path::is_relative(FileName)) {
388 // We may still need to append compilation directory of compile unit.
389 SmallString<16> AbsolutePath;
390 if (const char *CompilationDir = CU->getCompilationDir()) {
391 sys::path::append(AbsolutePath, CompilationDir);
393 sys::path::append(AbsolutePath, FileName);
394 FileName = AbsolutePath.str();
399 static bool getFileLineInfoForCompileUnit(DWARFCompileUnit *CU,
400 const DWARFLineTable *LineTable,
402 bool NeedsAbsoluteFilePath,
403 std::string &FileName,
404 uint32_t &Line, uint32_t &Column) {
405 if (CU == 0 || LineTable == 0)
407 // Get the index of row we're looking for in the line table.
408 uint32_t RowIndex = LineTable->lookupAddress(Address);
411 // Take file number and line/column from the row.
412 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
413 if (!getFileNameForCompileUnit(CU, LineTable, Row.File,
414 NeedsAbsoluteFilePath, FileName))
421 DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
422 DILineInfoSpecifier Specifier) {
423 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
426 std::string FileName = "<invalid>";
427 std::string FunctionName = "<invalid>";
430 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
431 // The address may correspond to instruction in some inlined function,
432 // so we have to build the chain of inlined functions and take the
433 // name of the topmost function in it.
434 const DWARFDebugInfoEntryInlinedChain &InlinedChain =
435 CU->getInlinedChainForAddress(Address);
436 if (InlinedChain.DIEs.size() > 0) {
437 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain.DIEs[0];
438 if (const char *Name = TopFunctionDIE.getSubroutineName(InlinedChain.U))
442 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
443 const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
444 const bool NeedsAbsoluteFilePath =
445 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
446 getFileLineInfoForCompileUnit(CU, LineTable, Address,
447 NeedsAbsoluteFilePath,
448 FileName, Line, Column);
450 return DILineInfo(StringRef(FileName), StringRef(FunctionName),
454 DILineInfoTable DWARFContext::getLineInfoForAddressRange(uint64_t Address,
456 DILineInfoSpecifier Specifier) {
457 DILineInfoTable Lines;
458 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
462 std::string FunctionName = "<invalid>";
463 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
464 // The address may correspond to instruction in some inlined function,
465 // so we have to build the chain of inlined functions and take the
466 // name of the topmost function in it.
467 const DWARFDebugInfoEntryInlinedChain &InlinedChain =
468 CU->getInlinedChainForAddress(Address);
469 if (InlinedChain.DIEs.size() > 0) {
470 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain.DIEs[0];
471 if (const char *Name = TopFunctionDIE.getSubroutineName(InlinedChain.U))
476 // If the Specifier says we don't need FileLineInfo, just
477 // return the top-most function at the starting address.
478 if (!Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
480 std::make_pair(Address, DILineInfo("<invalid>", FunctionName, 0, 0)));
484 const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
485 const bool NeedsAbsoluteFilePath =
486 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
488 // Get the index of row we're looking for in the line table.
489 std::vector<uint32_t> RowVector;
490 if (!LineTable->lookupAddressRange(Address, Size, RowVector))
493 uint32_t NumRows = RowVector.size();
494 for (uint32_t i = 0; i < NumRows; ++i) {
495 uint32_t RowIndex = RowVector[i];
496 // Take file number and line/column from the row.
497 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
498 std::string FileName = "<invalid>";
499 getFileNameForCompileUnit(CU, LineTable, Row.File,
500 NeedsAbsoluteFilePath, FileName);
501 Lines.push_back(std::make_pair(
502 Row.Address, DILineInfo(FileName, FunctionName, Row.Line, Row.Column)));
508 DIInliningInfo DWARFContext::getInliningInfoForAddress(uint64_t Address,
509 DILineInfoSpecifier Specifier) {
510 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
512 return DIInliningInfo();
514 const DWARFDebugInfoEntryInlinedChain &InlinedChain =
515 CU->getInlinedChainForAddress(Address);
516 if (InlinedChain.DIEs.size() == 0)
517 return DIInliningInfo();
519 DIInliningInfo InliningInfo;
520 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0;
521 const DWARFLineTable *LineTable = 0;
522 for (uint32_t i = 0, n = InlinedChain.DIEs.size(); i != n; i++) {
523 const DWARFDebugInfoEntryMinimal &FunctionDIE = InlinedChain.DIEs[i];
524 std::string FileName = "<invalid>";
525 std::string FunctionName = "<invalid>";
528 // Get function name if necessary.
529 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
530 if (const char *Name = FunctionDIE.getSubroutineName(InlinedChain.U))
533 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
534 const bool NeedsAbsoluteFilePath =
535 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
537 // For the topmost frame, initialize the line table of this
538 // compile unit and fetch file/line info from it.
539 LineTable = getLineTableForCompileUnit(CU);
540 // For the topmost routine, get file/line info from line table.
541 getFileLineInfoForCompileUnit(CU, LineTable, Address,
542 NeedsAbsoluteFilePath,
543 FileName, Line, Column);
545 // Otherwise, use call file, call line and call column from
546 // previous DIE in inlined chain.
547 getFileNameForCompileUnit(CU, LineTable, CallFile,
548 NeedsAbsoluteFilePath, FileName);
552 // Get call file/line/column of a current DIE.
554 FunctionDIE.getCallerFrame(InlinedChain.U, CallFile, CallLine,
558 DILineInfo Frame(StringRef(FileName), StringRef(FunctionName),
560 InliningInfo.addFrame(Frame);
565 static bool consumeCompressedDebugSectionHeader(StringRef &data,
566 uint64_t &OriginalSize) {
567 // Consume "ZLIB" prefix.
568 if (!data.startswith("ZLIB"))
570 data = data.substr(4);
571 // Consume uncompressed section size (big-endian 8 bytes).
572 DataExtractor extractor(data, false, 8);
574 OriginalSize = extractor.getU64(&Offset);
577 data = data.substr(Offset);
581 DWARFContextInMemory::DWARFContextInMemory(object::ObjectFile *Obj) :
582 IsLittleEndian(Obj->isLittleEndian()),
583 AddressSize(Obj->getBytesInAddress()) {
585 for (object::section_iterator i = Obj->begin_sections(),
586 e = Obj->end_sections();
587 i != e; i.increment(ec)) {
591 i->getContents(data);
593 name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes.
595 // Check if debug info section is compressed with zlib.
596 if (name.startswith("zdebug_")) {
597 uint64_t OriginalSize;
598 if (!zlib::isAvailable() ||
599 !consumeCompressedDebugSectionHeader(data, OriginalSize))
601 OwningPtr<MemoryBuffer> UncompressedSection;
602 if (zlib::uncompress(data, UncompressedSection, OriginalSize) !=
605 // Make data point to uncompressed section contents and save its contents.
606 name = name.substr(1);
607 data = UncompressedSection->getBuffer();
608 UncompressedSections.push_back(UncompressedSection.take());
612 StringSwitch<StringRef *>(name)
613 .Case("debug_info", &InfoSection.Data)
614 .Case("debug_abbrev", &AbbrevSection)
615 .Case("debug_loc", &LocSection.Data)
616 .Case("debug_line", &LineSection.Data)
617 .Case("debug_aranges", &ARangeSection)
618 .Case("debug_frame", &DebugFrameSection)
619 .Case("debug_str", &StringSection)
620 .Case("debug_ranges", &RangeSection)
621 .Case("debug_pubnames", &PubNamesSection)
622 .Case("debug_pubtypes", &PubTypesSection)
623 .Case("debug_gnu_pubnames", &GnuPubNamesSection)
624 .Case("debug_gnu_pubtypes", &GnuPubTypesSection)
625 .Case("debug_info.dwo", &InfoDWOSection.Data)
626 .Case("debug_abbrev.dwo", &AbbrevDWOSection)
627 .Case("debug_str.dwo", &StringDWOSection)
628 .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
629 .Case("debug_addr", &AddrSection)
630 // Any more debug info sections go here.
634 if (name == "debug_ranges") {
635 // FIXME: Use the other dwo range section when we emit it.
636 RangeDWOSection = data;
638 } else if (name == "debug_types") {
639 // Find debug_types data by section rather than name as there are
640 // multiple, comdat grouped, debug_types sections.
641 TypesSections[*i].Data = data;
644 section_iterator RelocatedSection = i->getRelocatedSection();
645 if (RelocatedSection == Obj->end_sections())
648 StringRef RelSecName;
649 RelocatedSection->getName(RelSecName);
650 RelSecName = RelSecName.substr(
651 RelSecName.find_first_not_of("._")); // Skip . and _ prefixes.
653 // TODO: Add support for relocations in other sections as needed.
654 // Record relocations for the debug_info and debug_line sections.
655 RelocAddrMap *Map = StringSwitch<RelocAddrMap*>(RelSecName)
656 .Case("debug_info", &InfoSection.Relocs)
657 .Case("debug_loc", &LocSection.Relocs)
658 .Case("debug_info.dwo", &InfoDWOSection.Relocs)
659 .Case("debug_line", &LineSection.Relocs)
662 if (RelSecName != "debug_types")
664 // Find debug_types relocs by section rather than name as there are
665 // multiple, comdat grouped, debug_types sections.
666 Map = &TypesSections[*RelocatedSection].Relocs;
669 if (i->begin_relocations() != i->end_relocations()) {
670 uint64_t SectionSize;
671 RelocatedSection->getSize(SectionSize);
672 for (object::relocation_iterator reloc_i = i->begin_relocations(),
673 reloc_e = i->end_relocations();
674 reloc_i != reloc_e; reloc_i.increment(ec)) {
676 reloc_i->getOffset(Address);
678 reloc_i->getType(Type);
679 uint64_t SymAddr = 0;
680 // ELF relocations may need the symbol address
682 object::symbol_iterator Sym = reloc_i->getSymbol();
683 Sym->getAddress(SymAddr);
686 object::RelocVisitor V(Obj->getFileFormatName());
687 // The section address is always 0 for debug sections.
688 object::RelocToApply R(V.visit(Type, *reloc_i, 0, SymAddr));
690 SmallString<32> Name;
691 error_code ec(reloc_i->getTypeName(Name));
693 errs() << "Aaaaaa! Nameless relocation! Aaaaaa!\n";
695 errs() << "error: failed to compute relocation: "
700 if (Address + R.Width > SectionSize) {
701 errs() << "error: " << R.Width << "-byte relocation starting "
702 << Address << " bytes into section " << name << " which is "
703 << SectionSize << " bytes long.\n";
707 errs() << "error: can't handle a relocation of more than 8 bytes at "
711 DEBUG(dbgs() << "Writing " << format("%p", R.Value)
712 << " at " << format("%p", Address)
713 << " with width " << format("%d", R.Width)
715 Map->insert(std::make_pair(Address, std::make_pair(R.Width, R.Value)));
721 DWARFContextInMemory::~DWARFContextInMemory() {
722 DeleteContainerPointers(UncompressedSections);
725 void DWARFContextInMemory::anchor() { }