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/STLExtras.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/StringSwitch.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);
30 DeleteContainerPointers(DWOTUs);
33 static void dumpPubSection(raw_ostream &OS, StringRef Name, StringRef Data,
34 bool LittleEndian, bool GnuStyle) {
35 OS << "\n." << Name << " contents:\n";
36 DataExtractor pubNames(Data, LittleEndian, 0);
38 while (pubNames.isValidOffset(offset)) {
39 OS << "length = " << format("0x%08x", pubNames.getU32(&offset));
40 OS << " version = " << format("0x%04x", pubNames.getU16(&offset));
41 OS << " unit_offset = " << format("0x%08x", pubNames.getU32(&offset));
42 OS << " unit_size = " << format("0x%08x", pubNames.getU32(&offset)) << '\n';
44 OS << "Offset Linkage Kind Name\n";
46 OS << "Offset Name\n";
48 while (offset < Data.size()) {
49 uint32_t dieRef = pubNames.getU32(&offset);
52 OS << format("0x%8.8x ", dieRef);
54 PubIndexEntryDescriptor desc(pubNames.getU8(&offset));
55 OS << format("%-8s", dwarf::GDBIndexEntryLinkageString(desc.Linkage))
56 << ' ' << format("%-8s", dwarf::GDBIndexEntryKindString(desc.Kind))
59 OS << '\"' << pubNames.getCStr(&offset) << "\"\n";
64 void DWARFContext::dump(raw_ostream &OS, DIDumpType DumpType) {
65 if (DumpType == DIDT_All || DumpType == DIDT_Abbrev) {
66 OS << ".debug_abbrev contents:\n";
67 getDebugAbbrev()->dump(OS);
70 if (DumpType == DIDT_All || DumpType == DIDT_AbbrevDwo)
71 if (const DWARFDebugAbbrev *D = getDebugAbbrevDWO()) {
72 OS << "\n.debug_abbrev.dwo contents:\n";
76 if (DumpType == DIDT_All || DumpType == DIDT_Info) {
77 OS << "\n.debug_info contents:\n";
78 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i)
79 getCompileUnitAtIndex(i)->dump(OS);
82 if ((DumpType == DIDT_All || DumpType == DIDT_InfoDwo) &&
83 getNumDWOCompileUnits()) {
84 OS << "\n.debug_info.dwo contents:\n";
85 for (unsigned i = 0, e = getNumDWOCompileUnits(); i != e; ++i)
86 getDWOCompileUnitAtIndex(i)->dump(OS);
89 if ((DumpType == DIDT_All || DumpType == DIDT_Types) && getNumTypeUnits()) {
90 OS << "\n.debug_types contents:\n";
91 for (unsigned i = 0, e = getNumTypeUnits(); i != e; ++i)
92 getTypeUnitAtIndex(i)->dump(OS);
95 if (DumpType == DIDT_All || DumpType == DIDT_TypesDwo)
96 if (getNumDWOTypeUnits()) {
97 OS << "\n.debug_types.dwo contents:\n";
98 for (unsigned i = 0, e = getNumDWOTypeUnits(); i != e; ++i)
99 getDWOTypeUnitAtIndex(i)->dump(OS);
102 if (DumpType == DIDT_All || DumpType == DIDT_Loc) {
103 OS << "\n.debug_loc contents:\n";
104 getDebugLoc()->dump(OS);
107 if (DumpType == DIDT_All || DumpType == DIDT_Frames) {
108 OS << "\n.debug_frame contents:\n";
109 getDebugFrame()->dump(OS);
113 if (DumpType == DIDT_All || DumpType == DIDT_Aranges) {
114 OS << "\n.debug_aranges contents:\n";
115 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
116 DWARFDebugArangeSet set;
117 while (set.extract(arangesData, &offset))
121 uint8_t savedAddressByteSize = 0;
122 if (DumpType == DIDT_All || DumpType == DIDT_Line) {
123 OS << "\n.debug_line contents:\n";
124 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i) {
125 DWARFCompileUnit *cu = getCompileUnitAtIndex(i);
126 savedAddressByteSize = cu->getAddressByteSize();
127 unsigned stmtOffset =
128 cu->getCompileUnitDIE()->getAttributeValueAsSectionOffset(
129 cu, DW_AT_stmt_list, -1U);
130 if (stmtOffset != -1U) {
131 DataExtractor lineData(getLineSection().Data, isLittleEndian(),
132 savedAddressByteSize);
133 DWARFDebugLine::DumpingState state(OS);
134 DWARFDebugLine::parseStatementTable(lineData, &getLineSection().Relocs, &stmtOffset, state);
139 if (DumpType == DIDT_All || DumpType == DIDT_LineDwo) {
140 OS << "\n.debug_line.dwo contents:\n";
141 unsigned stmtOffset = 0;
142 DataExtractor lineData(getLineDWOSection().Data, isLittleEndian(),
143 savedAddressByteSize);
144 DWARFDebugLine::DumpingState state(OS);
145 while (DWARFDebugLine::parsePrologue(lineData, &stmtOffset, &state.Prologue))
149 if (DumpType == DIDT_All || DumpType == DIDT_Str) {
150 OS << "\n.debug_str contents:\n";
151 DataExtractor strData(getStringSection(), isLittleEndian(), 0);
153 uint32_t strOffset = 0;
154 while (const char *s = strData.getCStr(&offset)) {
155 OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
160 if ((DumpType == DIDT_All || DumpType == DIDT_StrDwo) &&
161 !getStringDWOSection().empty()) {
162 OS << "\n.debug_str.dwo contents:\n";
163 DataExtractor strDWOData(getStringDWOSection(), isLittleEndian(), 0);
165 uint32_t strDWOOffset = 0;
166 while (const char *s = strDWOData.getCStr(&offset)) {
167 OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
168 strDWOOffset = offset;
172 if (DumpType == DIDT_All || DumpType == DIDT_Ranges) {
173 OS << "\n.debug_ranges contents:\n";
174 // In fact, different compile units may have different address byte
175 // sizes, but for simplicity we just use the address byte size of the last
176 // compile unit (there is no easy and fast way to associate address range
177 // list and the compile unit it describes).
178 DataExtractor rangesData(getRangeSection(), isLittleEndian(),
179 savedAddressByteSize);
181 DWARFDebugRangeList rangeList;
182 while (rangeList.extract(rangesData, &offset))
186 if (DumpType == DIDT_All || DumpType == DIDT_Pubnames)
187 dumpPubSection(OS, "debug_pubnames", getPubNamesSection(),
188 isLittleEndian(), false);
190 if (DumpType == DIDT_All || DumpType == DIDT_Pubtypes)
191 dumpPubSection(OS, "debug_pubtypes", getPubTypesSection(),
192 isLittleEndian(), false);
194 if (DumpType == DIDT_All || DumpType == DIDT_GnuPubnames)
195 dumpPubSection(OS, "debug_gnu_pubnames", getGnuPubNamesSection(),
196 isLittleEndian(), true /* GnuStyle */);
198 if (DumpType == DIDT_All || DumpType == DIDT_GnuPubtypes)
199 dumpPubSection(OS, "debug_gnu_pubtypes", getGnuPubTypesSection(),
200 isLittleEndian(), true /* GnuStyle */);
202 if ((DumpType == DIDT_All || DumpType == DIDT_StrOffsetsDwo) &&
203 !getStringOffsetDWOSection().empty()) {
204 OS << "\n.debug_str_offsets.dwo contents:\n";
205 DataExtractor strOffsetExt(getStringOffsetDWOSection(), isLittleEndian(),
208 uint64_t size = getStringOffsetDWOSection().size();
209 while (offset < size) {
210 OS << format("0x%8.8x: ", offset);
211 OS << format("%8.8x\n", strOffsetExt.getU32(&offset));
216 const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
220 DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
222 Abbrev.reset(new DWARFDebugAbbrev());
223 Abbrev->parse(abbrData);
227 const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
229 return AbbrevDWO.get();
231 DataExtractor abbrData(getAbbrevDWOSection(), isLittleEndian(), 0);
232 AbbrevDWO.reset(new DWARFDebugAbbrev());
233 AbbrevDWO->parse(abbrData);
234 return AbbrevDWO.get();
237 const DWARFDebugLoc *DWARFContext::getDebugLoc() {
241 DataExtractor LocData(getLocSection().Data, isLittleEndian(), 0);
242 Loc.reset(new DWARFDebugLoc(getLocSection().Relocs));
243 // assume all compile units have the same address byte size
244 if (getNumCompileUnits())
245 Loc->parse(LocData, getCompileUnitAtIndex(0)->getAddressByteSize());
249 const DWARFDebugAranges *DWARFContext::getDebugAranges() {
251 return Aranges.get();
253 Aranges.reset(new DWARFDebugAranges());
254 Aranges->generate(this);
255 return Aranges.get();
258 const DWARFDebugFrame *DWARFContext::getDebugFrame() {
260 return DebugFrame.get();
262 // There's a "bug" in the DWARFv3 standard with respect to the target address
263 // size within debug frame sections. While DWARF is supposed to be independent
264 // of its container, FDEs have fields with size being "target address size",
265 // which isn't specified in DWARF in general. It's only specified for CUs, but
266 // .eh_frame can appear without a .debug_info section. Follow the example of
267 // other tools (libdwarf) and extract this from the container (ObjectFile
268 // provides this information). This problem is fixed in DWARFv4
269 // See this dwarf-discuss discussion for more details:
270 // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
271 DataExtractor debugFrameData(getDebugFrameSection(), isLittleEndian(),
273 DebugFrame.reset(new DWARFDebugFrame());
274 DebugFrame->parse(debugFrameData);
275 return DebugFrame.get();
278 const DWARFLineTable *
279 DWARFContext::getLineTableForCompileUnit(DWARFCompileUnit *cu) {
281 Line.reset(new DWARFDebugLine(&getLineSection().Relocs));
283 unsigned stmtOffset =
284 cu->getCompileUnitDIE()->getAttributeValueAsSectionOffset(
285 cu, DW_AT_stmt_list, -1U);
286 if (stmtOffset == -1U)
287 return 0; // No line table for this compile unit.
289 // See if the line table is cached.
290 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
293 // We have to parse it first.
294 DataExtractor lineData(getLineSection().Data, isLittleEndian(),
295 cu->getAddressByteSize());
296 return Line->getOrParseLineTable(lineData, stmtOffset);
299 void DWARFContext::parseCompileUnits() {
301 const DataExtractor &DIData = DataExtractor(getInfoSection().Data,
302 isLittleEndian(), 0);
303 while (DIData.isValidOffset(offset)) {
304 OwningPtr<DWARFCompileUnit> CU(new DWARFCompileUnit(
305 getDebugAbbrev(), getInfoSection().Data, getAbbrevSection(),
306 getRangeSection(), getStringSection(), StringRef(), getAddrSection(),
307 &getInfoSection().Relocs, isLittleEndian()));
308 if (!CU->extract(DIData, &offset)) {
311 CUs.push_back(CU.take());
312 offset = CUs.back()->getNextUnitOffset();
316 void DWARFContext::parseTypeUnits() {
317 const TypeSectionMap &Sections = getTypesSections();
318 for (TypeSectionMap::const_iterator I = Sections.begin(), E = Sections.end();
321 const DataExtractor &DIData =
322 DataExtractor(I->second.Data, isLittleEndian(), 0);
323 while (DIData.isValidOffset(offset)) {
324 OwningPtr<DWARFTypeUnit> TU(new DWARFTypeUnit(
325 getDebugAbbrev(), I->second.Data, getAbbrevSection(),
326 getRangeSection(), getStringSection(), StringRef(), getAddrSection(),
327 &I->second.Relocs, isLittleEndian()));
328 if (!TU->extract(DIData, &offset))
330 TUs.push_back(TU.take());
331 offset = TUs.back()->getNextUnitOffset();
336 void DWARFContext::parseDWOCompileUnits() {
338 const DataExtractor &DIData =
339 DataExtractor(getInfoDWOSection().Data, isLittleEndian(), 0);
340 while (DIData.isValidOffset(offset)) {
341 OwningPtr<DWARFCompileUnit> DWOCU(new DWARFCompileUnit(
342 getDebugAbbrevDWO(), getInfoDWOSection().Data, getAbbrevDWOSection(),
343 getRangeDWOSection(), getStringDWOSection(),
344 getStringOffsetDWOSection(), getAddrSection(),
345 &getInfoDWOSection().Relocs, isLittleEndian()));
346 if (!DWOCU->extract(DIData, &offset)) {
349 DWOCUs.push_back(DWOCU.take());
350 offset = DWOCUs.back()->getNextUnitOffset();
354 void DWARFContext::parseDWOTypeUnits() {
355 const TypeSectionMap &Sections = getTypesDWOSections();
356 for (TypeSectionMap::const_iterator I = Sections.begin(), E = Sections.end();
359 const DataExtractor &DIData =
360 DataExtractor(I->second.Data, isLittleEndian(), 0);
361 while (DIData.isValidOffset(offset)) {
362 OwningPtr<DWARFTypeUnit> TU(new DWARFTypeUnit(
363 getDebugAbbrevDWO(), I->second.Data, getAbbrevDWOSection(),
364 getRangeDWOSection(), getStringDWOSection(),
365 getStringOffsetDWOSection(), getAddrSection(), &I->second.Relocs,
367 if (!TU->extract(DIData, &offset))
369 DWOTUs.push_back(TU.take());
370 offset = DWOTUs.back()->getNextUnitOffset();
376 struct OffsetComparator {
377 bool operator()(const DWARFCompileUnit *LHS,
378 const DWARFCompileUnit *RHS) const {
379 return LHS->getOffset() < RHS->getOffset();
381 bool operator()(const DWARFCompileUnit *LHS, uint32_t RHS) const {
382 return LHS->getOffset() < RHS;
384 bool operator()(uint32_t LHS, const DWARFCompileUnit *RHS) const {
385 return LHS < RHS->getOffset();
390 DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
394 DWARFCompileUnit **CU =
395 std::lower_bound(CUs.begin(), CUs.end(), Offset, OffsetComparator());
396 if (CU != CUs.end()) {
402 DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
403 // First, get the offset of the compile unit.
404 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
405 // Retrieve the compile unit.
406 return getCompileUnitForOffset(CUOffset);
409 static bool getFileNameForCompileUnit(DWARFCompileUnit *CU,
410 const DWARFLineTable *LineTable,
412 bool NeedsAbsoluteFilePath,
413 std::string &FileName) {
416 !LineTable->getFileNameByIndex(FileIndex, NeedsAbsoluteFilePath,
419 if (NeedsAbsoluteFilePath && sys::path::is_relative(FileName)) {
420 // We may still need to append compilation directory of compile unit.
421 SmallString<16> AbsolutePath;
422 if (const char *CompilationDir = CU->getCompilationDir()) {
423 sys::path::append(AbsolutePath, CompilationDir);
425 sys::path::append(AbsolutePath, FileName);
426 FileName = AbsolutePath.str();
431 static bool getFileLineInfoForCompileUnit(DWARFCompileUnit *CU,
432 const DWARFLineTable *LineTable,
434 bool NeedsAbsoluteFilePath,
435 std::string &FileName,
436 uint32_t &Line, uint32_t &Column) {
437 if (CU == 0 || LineTable == 0)
439 // Get the index of row we're looking for in the line table.
440 uint32_t RowIndex = LineTable->lookupAddress(Address);
443 // Take file number and line/column from the row.
444 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
445 if (!getFileNameForCompileUnit(CU, LineTable, Row.File,
446 NeedsAbsoluteFilePath, FileName))
453 DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
454 DILineInfoSpecifier Specifier) {
455 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
458 std::string FileName = "<invalid>";
459 std::string FunctionName = "<invalid>";
462 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
463 // The address may correspond to instruction in some inlined function,
464 // so we have to build the chain of inlined functions and take the
465 // name of the topmost function in it.
466 const DWARFDebugInfoEntryInlinedChain &InlinedChain =
467 CU->getInlinedChainForAddress(Address);
468 if (InlinedChain.DIEs.size() > 0) {
469 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain.DIEs[0];
470 if (const char *Name = TopFunctionDIE.getSubroutineName(InlinedChain.U))
474 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
475 const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
476 const bool NeedsAbsoluteFilePath =
477 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
478 getFileLineInfoForCompileUnit(CU, LineTable, Address,
479 NeedsAbsoluteFilePath,
480 FileName, Line, Column);
482 return DILineInfo(StringRef(FileName), StringRef(FunctionName),
486 DILineInfoTable DWARFContext::getLineInfoForAddressRange(uint64_t Address,
488 DILineInfoSpecifier Specifier) {
489 DILineInfoTable Lines;
490 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
494 std::string FunctionName = "<invalid>";
495 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
496 // The address may correspond to instruction in some inlined function,
497 // so we have to build the chain of inlined functions and take the
498 // name of the topmost function in it.
499 const DWARFDebugInfoEntryInlinedChain &InlinedChain =
500 CU->getInlinedChainForAddress(Address);
501 if (InlinedChain.DIEs.size() > 0) {
502 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain.DIEs[0];
503 if (const char *Name = TopFunctionDIE.getSubroutineName(InlinedChain.U))
508 // If the Specifier says we don't need FileLineInfo, just
509 // return the top-most function at the starting address.
510 if (!Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
512 std::make_pair(Address, DILineInfo("<invalid>", FunctionName, 0, 0)));
516 const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
517 const bool NeedsAbsoluteFilePath =
518 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
520 // Get the index of row we're looking for in the line table.
521 std::vector<uint32_t> RowVector;
522 if (!LineTable->lookupAddressRange(Address, Size, RowVector))
525 uint32_t NumRows = RowVector.size();
526 for (uint32_t i = 0; i < NumRows; ++i) {
527 uint32_t RowIndex = RowVector[i];
528 // Take file number and line/column from the row.
529 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
530 std::string FileName = "<invalid>";
531 getFileNameForCompileUnit(CU, LineTable, Row.File,
532 NeedsAbsoluteFilePath, FileName);
533 Lines.push_back(std::make_pair(
534 Row.Address, DILineInfo(FileName, FunctionName, Row.Line, Row.Column)));
540 DIInliningInfo DWARFContext::getInliningInfoForAddress(uint64_t Address,
541 DILineInfoSpecifier Specifier) {
542 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
544 return DIInliningInfo();
546 const DWARFDebugInfoEntryInlinedChain &InlinedChain =
547 CU->getInlinedChainForAddress(Address);
548 if (InlinedChain.DIEs.size() == 0)
549 return DIInliningInfo();
551 DIInliningInfo InliningInfo;
552 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0;
553 const DWARFLineTable *LineTable = 0;
554 for (uint32_t i = 0, n = InlinedChain.DIEs.size(); i != n; i++) {
555 const DWARFDebugInfoEntryMinimal &FunctionDIE = InlinedChain.DIEs[i];
556 std::string FileName = "<invalid>";
557 std::string FunctionName = "<invalid>";
560 // Get function name if necessary.
561 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
562 if (const char *Name = FunctionDIE.getSubroutineName(InlinedChain.U))
565 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
566 const bool NeedsAbsoluteFilePath =
567 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
569 // For the topmost frame, initialize the line table of this
570 // compile unit and fetch file/line info from it.
571 LineTable = getLineTableForCompileUnit(CU);
572 // For the topmost routine, get file/line info from line table.
573 getFileLineInfoForCompileUnit(CU, LineTable, Address,
574 NeedsAbsoluteFilePath,
575 FileName, Line, Column);
577 // Otherwise, use call file, call line and call column from
578 // previous DIE in inlined chain.
579 getFileNameForCompileUnit(CU, LineTable, CallFile,
580 NeedsAbsoluteFilePath, FileName);
584 // Get call file/line/column of a current DIE.
586 FunctionDIE.getCallerFrame(InlinedChain.U, CallFile, CallLine,
590 DILineInfo Frame(StringRef(FileName), StringRef(FunctionName),
592 InliningInfo.addFrame(Frame);
597 static bool consumeCompressedDebugSectionHeader(StringRef &data,
598 uint64_t &OriginalSize) {
599 // Consume "ZLIB" prefix.
600 if (!data.startswith("ZLIB"))
602 data = data.substr(4);
603 // Consume uncompressed section size (big-endian 8 bytes).
604 DataExtractor extractor(data, false, 8);
606 OriginalSize = extractor.getU64(&Offset);
609 data = data.substr(Offset);
613 DWARFContextInMemory::DWARFContextInMemory(object::ObjectFile *Obj) :
614 IsLittleEndian(Obj->isLittleEndian()),
615 AddressSize(Obj->getBytesInAddress()) {
616 for (object::section_iterator i = Obj->section_begin(),
617 e = Obj->section_end();
622 i->getContents(data);
624 name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes.
626 // Check if debug info section is compressed with zlib.
627 if (name.startswith("zdebug_")) {
628 uint64_t OriginalSize;
629 if (!zlib::isAvailable() ||
630 !consumeCompressedDebugSectionHeader(data, OriginalSize))
632 OwningPtr<MemoryBuffer> UncompressedSection;
633 if (zlib::uncompress(data, UncompressedSection, OriginalSize) !=
636 // Make data point to uncompressed section contents and save its contents.
637 name = name.substr(1);
638 data = UncompressedSection->getBuffer();
639 UncompressedSections.push_back(UncompressedSection.take());
643 StringSwitch<StringRef *>(name)
644 .Case("debug_info", &InfoSection.Data)
645 .Case("debug_abbrev", &AbbrevSection)
646 .Case("debug_loc", &LocSection.Data)
647 .Case("debug_line", &LineSection.Data)
648 .Case("debug_aranges", &ARangeSection)
649 .Case("debug_frame", &DebugFrameSection)
650 .Case("debug_str", &StringSection)
651 .Case("debug_ranges", &RangeSection)
652 .Case("debug_pubnames", &PubNamesSection)
653 .Case("debug_pubtypes", &PubTypesSection)
654 .Case("debug_gnu_pubnames", &GnuPubNamesSection)
655 .Case("debug_gnu_pubtypes", &GnuPubTypesSection)
656 .Case("debug_info.dwo", &InfoDWOSection.Data)
657 .Case("debug_abbrev.dwo", &AbbrevDWOSection)
658 .Case("debug_line.dwo", &LineDWOSection.Data)
659 .Case("debug_str.dwo", &StringDWOSection)
660 .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
661 .Case("debug_addr", &AddrSection)
662 // Any more debug info sections go here.
666 if (name == "debug_ranges") {
667 // FIXME: Use the other dwo range section when we emit it.
668 RangeDWOSection = data;
670 } else if (name == "debug_types") {
671 // Find debug_types data by section rather than name as there are
672 // multiple, comdat grouped, debug_types sections.
673 TypesSections[*i].Data = data;
674 } else if (name == "debug_types.dwo") {
675 TypesDWOSections[*i].Data = data;
678 section_iterator RelocatedSection = i->getRelocatedSection();
679 if (RelocatedSection == Obj->section_end())
682 StringRef RelSecName;
683 RelocatedSection->getName(RelSecName);
684 RelSecName = RelSecName.substr(
685 RelSecName.find_first_not_of("._")); // Skip . and _ prefixes.
687 // TODO: Add support for relocations in other sections as needed.
688 // Record relocations for the debug_info and debug_line sections.
689 RelocAddrMap *Map = StringSwitch<RelocAddrMap*>(RelSecName)
690 .Case("debug_info", &InfoSection.Relocs)
691 .Case("debug_loc", &LocSection.Relocs)
692 .Case("debug_info.dwo", &InfoDWOSection.Relocs)
693 .Case("debug_line", &LineSection.Relocs)
696 // Find debug_types relocs by section rather than name as there are
697 // multiple, comdat grouped, debug_types sections.
698 if (RelSecName == "debug_types")
699 Map = &TypesSections[*RelocatedSection].Relocs;
700 else if (RelSecName == "debug_types.dwo")
701 Map = &TypesDWOSections[*RelocatedSection].Relocs;
706 if (i->relocation_begin() != i->relocation_end()) {
707 uint64_t SectionSize;
708 RelocatedSection->getSize(SectionSize);
709 for (object::relocation_iterator reloc_i = i->relocation_begin(),
710 reloc_e = i->relocation_end();
711 reloc_i != reloc_e; ++reloc_i) {
713 reloc_i->getOffset(Address);
715 reloc_i->getType(Type);
716 uint64_t SymAddr = 0;
717 // ELF relocations may need the symbol address
719 object::symbol_iterator Sym = reloc_i->getSymbol();
720 Sym->getAddress(SymAddr);
723 object::RelocVisitor V(Obj->getFileFormatName());
724 // The section address is always 0 for debug sections.
725 object::RelocToApply R(V.visit(Type, *reloc_i, 0, SymAddr));
727 SmallString<32> Name;
728 error_code ec(reloc_i->getTypeName(Name));
730 errs() << "Aaaaaa! Nameless relocation! Aaaaaa!\n";
732 errs() << "error: failed to compute relocation: "
737 if (Address + R.Width > SectionSize) {
738 errs() << "error: " << R.Width << "-byte relocation starting "
739 << Address << " bytes into section " << name << " which is "
740 << SectionSize << " bytes long.\n";
744 errs() << "error: can't handle a relocation of more than 8 bytes at "
748 DEBUG(dbgs() << "Writing " << format("%p", R.Value)
749 << " at " << format("%p", Address)
750 << " with width " << format("%d", R.Width)
752 Map->insert(std::make_pair(Address, std::make_pair(R.Width, R.Value)));
758 DWARFContextInMemory::~DWARFContextInMemory() {
759 DeleteContainerPointers(UncompressedSections);
762 void DWARFContextInMemory::anchor() { }