Change how we iterate over relocations on ELF.
[oota-llvm.git] / lib / DebugInfo / DWARFContext.cpp
1 //===-- DWARFContext.cpp --------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "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"
19 #include <algorithm>
20 using namespace llvm;
21 using namespace dwarf;
22 using namespace object;
23
24 typedef DWARFDebugLine::LineTable DWARFLineTable;
25
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);
30   }
31
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);
36   }
37
38   if (DumpType == DIDT_All || DumpType == DIDT_Frames) {
39     OS << "\n.debug_frame contents:\n";
40     getDebugFrame()->dump(OS);
41   }
42
43   uint32_t offset = 0;
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))
49       set.dump(OS);
50   }
51
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();
58       unsigned stmtOffset =
59         cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
60                                                              -1U);
61       if (stmtOffset != -1U) {
62         DataExtractor lineData(getLineSection(), isLittleEndian(),
63                                savedAddressByteSize);
64         DWARFDebugLine::DumpingState state(OS);
65         DWARFDebugLine::parseStatementTable(lineData, &lineRelocMap(), &stmtOffset, state);
66       }
67     }
68   }
69
70   if (DumpType == DIDT_All || DumpType == DIDT_Str) {
71     OS << "\n.debug_str contents:\n";
72     DataExtractor strData(getStringSection(), isLittleEndian(), 0);
73     offset = 0;
74     uint32_t strOffset = 0;
75     while (const char *s = strData.getCStr(&offset)) {
76       OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
77       strOffset = offset;
78     }
79   }
80
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);
89     offset = 0;
90     DWARFDebugRangeList rangeList;
91     while (rangeList.extract(rangesData, &offset))
92       rangeList.dump(OS);
93   }
94
95   if (DumpType == DIDT_All || DumpType == DIDT_Pubnames) {
96     OS << "\n.debug_pubnames contents:\n";
97     DataExtractor pubNames(getPubNamesSection(), isLittleEndian(), 0);
98     offset = 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);
106       if (n == 0)
107         break;
108       OS << format("%8x    ", n);
109       OS << pubNames.getCStr(&offset) << "\n";
110     }
111   }
112
113   if (DumpType == DIDT_All || DumpType == DIDT_AbbrevDwo) {
114     const DWARFDebugAbbrev *D = getDebugAbbrevDWO();
115     if (D) {
116       OS << "\n.debug_abbrev.dwo contents:\n";
117       getDebugAbbrevDWO()->dump(OS);
118     }
119   }
120
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);
126     }
127
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);
132       offset = 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;
137       }
138     }
139
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);
144       offset = 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));
149       }
150     }
151 }
152
153 const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
154   if (Abbrev)
155     return Abbrev.get();
156
157   DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
158
159   Abbrev.reset(new DWARFDebugAbbrev());
160   Abbrev->parse(abbrData);
161   return Abbrev.get();
162 }
163
164 const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
165   if (AbbrevDWO)
166     return AbbrevDWO.get();
167
168   DataExtractor abbrData(getAbbrevDWOSection(), isLittleEndian(), 0);
169   AbbrevDWO.reset(new DWARFDebugAbbrev());
170   AbbrevDWO->parse(abbrData);
171   return AbbrevDWO.get();
172 }
173
174 const DWARFDebugAranges *DWARFContext::getDebugAranges() {
175   if (Aranges)
176     return Aranges.get();
177
178   DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
179
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();
187 }
188
189 const DWARFDebugFrame *DWARFContext::getDebugFrame() {
190   if (DebugFrame)
191     return DebugFrame.get();
192
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(),
203                                getAddressSize());
204   DebugFrame.reset(new DWARFDebugFrame());
205   DebugFrame->parse(debugFrameData);
206   return DebugFrame.get();
207 }
208
209 const DWARFLineTable *
210 DWARFContext::getLineTableForCompileUnit(DWARFCompileUnit *cu) {
211   if (!Line)
212     Line.reset(new DWARFDebugLine(&lineRelocMap()));
213
214   unsigned stmtOffset =
215     cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
216                                                          -1U);
217   if (stmtOffset == -1U)
218     return 0; // No line table for this compile unit.
219
220   // See if the line table is cached.
221   if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
222     return lt;
223
224   // We have to parse it first.
225   DataExtractor lineData(getLineSection(), isLittleEndian(),
226                          cu->getAddressByteSize());
227   return Line->getOrParseLineTable(lineData, stmtOffset);
228 }
229
230 void DWARFContext::parseCompileUnits() {
231   uint32_t offset = 0;
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(),
238                                    getAddrSection(),
239                                    &infoRelocMap(),
240                                    isLittleEndian()));
241     if (!CUs.back().extract(DIData, &offset)) {
242       CUs.pop_back();
243       break;
244     }
245
246     offset = CUs.back().getNextCompileUnitOffset();
247   }
248 }
249
250 void DWARFContext::parseDWOCompileUnits() {
251   uint32_t offset = 0;
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(),
260                                       getAddrSection(),
261                                       &infoDWORelocMap(),
262                                       isLittleEndian()));
263     if (!DWOCUs.back().extract(DIData, &offset)) {
264       DWOCUs.pop_back();
265       break;
266     }
267
268     offset = DWOCUs.back().getNextCompileUnitOffset();
269   }
270 }
271
272 namespace {
273   struct OffsetComparator {
274     bool operator()(const DWARFCompileUnit &LHS,
275                     const DWARFCompileUnit &RHS) const {
276       return LHS.getOffset() < RHS.getOffset();
277     }
278     bool operator()(const DWARFCompileUnit &LHS, uint32_t RHS) const {
279       return LHS.getOffset() < RHS;
280     }
281     bool operator()(uint32_t LHS, const DWARFCompileUnit &RHS) const {
282       return LHS < RHS.getOffset();
283     }
284   };
285 }
286
287 DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
288   if (CUs.empty())
289     parseCompileUnits();
290
291   DWARFCompileUnit *CU = std::lower_bound(CUs.begin(), CUs.end(), Offset,
292                                           OffsetComparator());
293   if (CU != CUs.end())
294     return &*CU;
295   return 0;
296 }
297
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);
303 }
304
305 static bool getFileNameForCompileUnit(DWARFCompileUnit *CU,
306                                       const DWARFLineTable *LineTable,
307                                       uint64_t FileIndex,
308                                       bool NeedsAbsoluteFilePath,
309                                       std::string &FileName) {
310   if (CU == 0 ||
311       LineTable == 0 ||
312       !LineTable->getFileNameByIndex(FileIndex, NeedsAbsoluteFilePath,
313                                      FileName))
314     return false;
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);
320     }
321     sys::path::append(AbsolutePath, FileName);
322     FileName = AbsolutePath.str();
323   }
324   return true;
325 }
326
327 static bool getFileLineInfoForCompileUnit(DWARFCompileUnit *CU,
328                                           const DWARFLineTable *LineTable,
329                                           uint64_t Address,
330                                           bool NeedsAbsoluteFilePath,
331                                           std::string &FileName,
332                                           uint32_t &Line, uint32_t &Column) {
333   if (CU == 0 || LineTable == 0)
334     return false;
335   // Get the index of row we're looking for in the line table.
336   uint32_t RowIndex = LineTable->lookupAddress(Address);
337   if (RowIndex == -1U)
338     return false;
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))
343     return false;
344   Line = Row.Line;
345   Column = Row.Column;
346   return true;
347 }
348
349 DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
350     DILineInfoSpecifier Specifier) {
351   DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
352   if (!CU)
353     return DILineInfo();
354   std::string FileName = "<invalid>";
355   std::string FunctionName = "<invalid>";
356   uint32_t Line = 0;
357   uint32_t Column = 0;
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))
367         FunctionName = Name;
368     }
369   }
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);
377   }
378   return DILineInfo(StringRef(FileName), StringRef(FunctionName),
379                     Line, Column);
380 }
381
382 DILineInfoTable DWARFContext::getLineInfoForAddressRange(uint64_t Address,
383     uint64_t Size,
384     DILineInfoSpecifier Specifier) {
385   DILineInfoTable  Lines;
386   DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
387   if (!CU)
388     return Lines;
389
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))
400         FunctionName = Name;
401     }
402   }
403
404   StringRef  FuncNameRef = StringRef(FunctionName);
405
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)));
412     return Lines;
413   }
414
415   const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
416   const bool NeedsAbsoluteFilePath =
417       Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
418
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))
422     return Lines;
423
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)));
435   }
436
437   return Lines;
438 }
439
440 DIInliningInfo DWARFContext::getInliningInfoForAddress(uint64_t Address,
441     DILineInfoSpecifier Specifier) {
442   DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
443   if (!CU)
444     return DIInliningInfo();
445
446   const DWARFDebugInfoEntryMinimal::InlinedChain &InlinedChain =
447       CU->getInlinedChainForAddress(Address);
448   if (InlinedChain.size() == 0)
449     return DIInliningInfo();
450
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>";
458     uint32_t Line = 0;
459     uint32_t Column = 0;
460     // Get function name if necessary.
461     if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
462       if (const char *Name = FunctionDIE.getSubroutineName(CU))
463         FunctionName = Name;
464     }
465     if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
466       const bool NeedsAbsoluteFilePath =
467           Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
468       if (i == 0) {
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);
476       } else {
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);
481         Line = CallLine;
482         Column = CallColumn;
483       }
484       // Get call file/line/column of a current DIE.
485       if (i + 1 < n) {
486         FunctionDIE.getCallerFrame(CU, CallFile, CallLine, CallColumn);
487       }
488     }
489     DILineInfo Frame(StringRef(FileName), StringRef(FunctionName),
490                      Line, Column);
491     InliningInfo.addFrame(Frame);
492   }
493   return InliningInfo;
494 }
495
496 static bool consumeCompressedDebugSectionHeader(StringRef &data,
497                                                 uint64_t &OriginalSize) {
498   // Consume "ZLIB" prefix.
499   if (!data.startswith("ZLIB"))
500     return false;
501   data = data.substr(4);
502   // Consume uncompressed section size (big-endian 8 bytes).
503   DataExtractor extractor(data, false, 8);
504   uint32_t Offset = 0;
505   OriginalSize = extractor.getU64(&Offset);
506   if (Offset == 0)
507     return false;
508   data = data.substr(Offset);
509   return true;
510 }
511
512 DWARFContextInMemory::DWARFContextInMemory(object::ObjectFile *Obj) :
513   IsLittleEndian(Obj->isLittleEndian()),
514   AddressSize(Obj->getBytesInAddress()) {
515   error_code ec;
516   for (object::section_iterator i = Obj->begin_sections(),
517          e = Obj->end_sections();
518        i != e; i.increment(ec)) {
519     StringRef name;
520     i->getName(name);
521     StringRef data;
522     i->getContents(data);
523
524     name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes.
525
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))
531         continue;
532       OwningPtr<MemoryBuffer> UncompressedSection;
533       if (zlib::uncompress(data, UncompressedSection, OriginalSize) !=
534           zlib::StatusOK)
535         continue;
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());
540     }
541
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.
557         .Default(0);
558     if (Section) {
559       *Section = data;
560       if (name == "debug_ranges") {
561         // FIXME: Use the other dwo range section when we emit it.
562         RangeDWOSection = data;
563       }
564     }
565
566     section_iterator RelocatedSection = i->getRelocatedSection();
567     if (RelocatedSection == Obj->end_sections())
568       continue;
569
570     StringRef RelSecName;
571     RelocatedSection->getName(RelSecName);
572     RelSecName = RelSecName.substr(
573         RelSecName.find_first_not_of("._")); // Skip . and _ prefixes.
574
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)
581         .Default(0);
582     if (!Map)
583       continue;
584
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)) {
591         uint64_t Address;
592         reloc_i->getOffset(Address);
593         uint64_t Type;
594         reloc_i->getType(Type);
595         uint64_t SymAddr = 0;
596         // ELF relocations may need the symbol address
597         if (Obj->isELF()) {
598           object::SymbolRef Sym;
599           reloc_i->getSymbol(Sym);
600           Sym.getAddress(SymAddr);
601         }
602
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));
606         if (V.error()) {
607           SmallString<32> Name;
608           error_code ec(reloc_i->getTypeName(Name));
609           if (ec) {
610             errs() << "Aaaaaa! Nameless relocation! Aaaaaa!\n";
611           }
612           errs() << "error: failed to compute relocation: "
613                  << Name << "\n";
614           continue;
615         }
616
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";
621           continue;
622         }
623         if (R.Width > 8) {
624           errs() << "error: can't handle a relocation of more than 8 bytes at "
625                     "a time.\n";
626           continue;
627         }
628         DEBUG(dbgs() << "Writing " << format("%p", R.Value)
629                      << " at " << format("%p", Address)
630                      << " with width " << format("%d", R.Width)
631                      << "\n");
632         Map->insert(std::make_pair(Address, std::make_pair(R.Width, R.Value)));
633       }
634     }
635   }
636 }
637
638 DWARFContextInMemory::~DWARFContextInMemory() {
639   DeleteContainerPointers(UncompressedSections);
640 }
641
642 void DWARFContextInMemory::anchor() { }