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 "llvm/DebugInfo/DWARF/DWARFContext.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/ADT/StringSwitch.h"
16 #include "llvm/BinaryFormat/Dwarf.h"
17 #include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
18 #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
19 #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
20 #include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h"
21 #include "llvm/DebugInfo/DWARF/DWARFDebugAranges.h"
22 #include "llvm/DebugInfo/DWARF/DWARFDebugFrame.h"
23 #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
24 #include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h"
25 #include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h"
26 #include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h"
27 #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
28 #include "llvm/DebugInfo/DWARF/DWARFDebugRnglists.h"
29 #include "llvm/DebugInfo/DWARF/DWARFDie.h"
30 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
31 #include "llvm/DebugInfo/DWARF/DWARFGdbIndex.h"
32 #include "llvm/DebugInfo/DWARF/DWARFSection.h"
33 #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
34 #include "llvm/DebugInfo/DWARF/DWARFVerifier.h"
35 #include "llvm/MC/MCRegisterInfo.h"
36 #include "llvm/Object/Decompressor.h"
37 #include "llvm/Object/MachO.h"
38 #include "llvm/Object/ObjectFile.h"
39 #include "llvm/Object/RelocVisitor.h"
40 #include "llvm/Support/Casting.h"
41 #include "llvm/Support/DataExtractor.h"
42 #include "llvm/Support/Error.h"
43 #include "llvm/Support/Format.h"
44 #include "llvm/Support/MemoryBuffer.h"
45 #include "llvm/Support/Path.h"
46 #include "llvm/Support/TargetRegistry.h"
47 #include "llvm/Support/raw_ostream.h"
48 #include <algorithm>
49 #include <cstdint>
50 #include <map>
51 #include <string>
52 #include <utility>
53 #include <vector>
54 
55 using namespace llvm;
56 using namespace dwarf;
57 using namespace object;
58 
59 #define DEBUG_TYPE "dwarf"
60 
61 using DWARFLineTable = DWARFDebugLine::LineTable;
62 using FileLineInfoKind = DILineInfoSpecifier::FileLineInfoKind;
63 using FunctionNameKind = DILineInfoSpecifier::FunctionNameKind;
64 
65 DWARFContext::DWARFContext(std::unique_ptr<const DWARFObject> DObj,
66                            std::string DWPName)
67     : DIContext(CK_DWARF), DWPName(std::move(DWPName)), DObj(std::move(DObj)) {}
68 
69 DWARFContext::~DWARFContext() = default;
70 
71 /// Dump the UUID load command.
72 static void dumpUUID(raw_ostream &OS, const ObjectFile &Obj) {
73   auto *MachO = dyn_cast<MachOObjectFile>(&Obj);
74   if (!MachO)
75     return;
76   for (auto LC : MachO->load_commands()) {
77     raw_ostream::uuid_t UUID;
78     if (LC.C.cmd == MachO::LC_UUID) {
79       if (LC.C.cmdsize < sizeof(UUID) + sizeof(LC.C)) {
80         OS << "error: UUID load command is too short.\n";
81         return;
82       }
83       OS << "UUID: ";
84       memcpy(&UUID, LC.Ptr+sizeof(LC.C), sizeof(UUID));
85       OS.write_uuid(UUID);
86       Triple T = MachO->getArchTriple();
87       OS << " (" << T.getArchName() << ')';
88       OS << ' ' << MachO->getFileName() << '\n';
89     }
90   }
91 }
92 
93 using ContributionCollection =
94     std::vector<Optional<StrOffsetsContributionDescriptor>>;
95 
96 // Collect all the contributions to the string offsets table from all units,
97 // sort them by their starting offsets and remove duplicates.
98 static ContributionCollection
99 collectContributionData(DWARFContext::cu_iterator_range CUs,
100                         DWARFContext::tu_section_iterator_range TUSs) {
101   ContributionCollection Contributions;
102   for (const auto &CU : CUs)
103     Contributions.push_back(CU->getStringOffsetsTableContribution());
104   for (const auto &TUS : TUSs)
105     for (const auto &TU : TUS)
106       Contributions.push_back(TU->getStringOffsetsTableContribution());
107 
108   // Sort the contributions so that any invalid ones are placed at
109   // the start of the contributions vector. This way they are reported
110   // first.
111   llvm::sort(Contributions.begin(), Contributions.end(),
112              [](const Optional<StrOffsetsContributionDescriptor> &L,
113                 const Optional<StrOffsetsContributionDescriptor> &R) {
114                if (L && R) return L->Base < R->Base;
115                return R.hasValue();
116              });
117 
118   // Uniquify contributions, as it is possible that units (specifically
119   // type units in dwo or dwp files) share contributions. We don't want
120   // to report them more than once.
121   Contributions.erase(
122       std::unique(Contributions.begin(), Contributions.end(),
123                   [](const Optional<StrOffsetsContributionDescriptor> &L,
124                      const Optional<StrOffsetsContributionDescriptor> &R) {
125                     if (L && R)
126                       return L->Base == R->Base && L->Size == R->Size;
127                     return false;
128                   }),
129       Contributions.end());
130   return Contributions;
131 }
132 
133 static void dumpDWARFv5StringOffsetsSection(
134     raw_ostream &OS, StringRef SectionName, const DWARFObject &Obj,
135     const DWARFSection &StringOffsetsSection, StringRef StringSection,
136     DWARFContext::cu_iterator_range CUs,
137     DWARFContext::tu_section_iterator_range TUSs, bool LittleEndian) {
138   auto Contributions = collectContributionData(CUs, TUSs);
139   DWARFDataExtractor StrOffsetExt(Obj, StringOffsetsSection, LittleEndian, 0);
140   DataExtractor StrData(StringSection, LittleEndian, 0);
141   uint64_t SectionSize = StringOffsetsSection.Data.size();
142   uint32_t Offset = 0;
143   for (auto &Contribution : Contributions) {
144     // Report an ill-formed contribution.
145     if (!Contribution) {
146       OS << "error: invalid contribution to string offsets table in section ."
147          << SectionName << ".\n";
148       return;
149     }
150 
151     dwarf::DwarfFormat Format = Contribution->getFormat();
152     uint16_t Version = Contribution->getVersion();
153     uint64_t ContributionHeader = Contribution->Base;
154     // In DWARF v5 there is a contribution header that immediately precedes
155     // the string offsets base (the location we have previously retrieved from
156     // the CU DIE's DW_AT_str_offsets attribute). The header is located either
157     // 8 or 16 bytes before the base, depending on the contribution's format.
158     if (Version >= 5)
159       ContributionHeader -= Format == DWARF32 ? 8 : 16;
160 
161     // Detect overlapping contributions.
162     if (Offset > ContributionHeader) {
163       OS << "error: overlapping contributions to string offsets table in "
164             "section ."
165          << SectionName << ".\n";
166       return;
167     }
168     // Report a gap in the table.
169     if (Offset < ContributionHeader) {
170       OS << format("0x%8.8x: Gap, length = ", Offset);
171       OS << (ContributionHeader - Offset) << "\n";
172     }
173     OS << format("0x%8.8x: ", (uint32_t)ContributionHeader);
174     OS << "Contribution size = " << Contribution->Size
175        << ", Format = " << (Format == DWARF32 ? "DWARF32" : "DWARF64")
176        << ", Version = " << Version << "\n";
177 
178     Offset = Contribution->Base;
179     unsigned EntrySize = Contribution->getDwarfOffsetByteSize();
180     while (Offset - Contribution->Base < Contribution->Size) {
181       OS << format("0x%8.8x: ", Offset);
182       // FIXME: We can only extract strings if the offset fits in 32 bits.
183       uint64_t StringOffset =
184           StrOffsetExt.getRelocatedValue(EntrySize, &Offset);
185       // Extract the string if we can and display it. Otherwise just report
186       // the offset.
187       if (StringOffset <= std::numeric_limits<uint32_t>::max()) {
188         uint32_t StringOffset32 = (uint32_t)StringOffset;
189         OS << format("%8.8x ", StringOffset32);
190         const char *S = StrData.getCStr(&StringOffset32);
191         if (S)
192           OS << format("\"%s\"", S);
193       } else
194         OS << format("%16.16" PRIx64 " ", StringOffset);
195       OS << "\n";
196     }
197   }
198   // Report a gap at the end of the table.
199   if (Offset < SectionSize) {
200     OS << format("0x%8.8x: Gap, length = ", Offset);
201     OS << (SectionSize - Offset) << "\n";
202   }
203 }
204 
205 // Dump a DWARF string offsets section. This may be a DWARF v5 formatted
206 // string offsets section, where each compile or type unit contributes a
207 // number of entries (string offsets), with each contribution preceded by
208 // a header containing size and version number. Alternatively, it may be a
209 // monolithic series of string offsets, as generated by the pre-DWARF v5
210 // implementation of split DWARF.
211 static void dumpStringOffsetsSection(
212     raw_ostream &OS, StringRef SectionName, const DWARFObject &Obj,
213     const DWARFSection &StringOffsetsSection, StringRef StringSection,
214     DWARFContext::cu_iterator_range CUs,
215     DWARFContext::tu_section_iterator_range TUSs, bool LittleEndian,
216     unsigned MaxVersion) {
217   // If we have at least one (compile or type) unit with DWARF v5 or greater,
218   // we assume that the section is formatted like a DWARF v5 string offsets
219   // section.
220   if (MaxVersion >= 5)
221     dumpDWARFv5StringOffsetsSection(OS, SectionName, Obj, StringOffsetsSection,
222                                     StringSection, CUs, TUSs, LittleEndian);
223   else {
224     DataExtractor strOffsetExt(StringOffsetsSection.Data, LittleEndian, 0);
225     uint32_t offset = 0;
226     uint64_t size = StringOffsetsSection.Data.size();
227     // Ensure that size is a multiple of the size of an entry.
228     if (size & ((uint64_t)(sizeof(uint32_t) - 1))) {
229       OS << "error: size of ." << SectionName << " is not a multiple of "
230          << sizeof(uint32_t) << ".\n";
231       size &= -(uint64_t)sizeof(uint32_t);
232     }
233     DataExtractor StrData(StringSection, LittleEndian, 0);
234     while (offset < size) {
235       OS << format("0x%8.8x: ", offset);
236       uint32_t StringOffset = strOffsetExt.getU32(&offset);
237       OS << format("%8.8x  ", StringOffset);
238       const char *S = StrData.getCStr(&StringOffset);
239       if (S)
240         OS << format("\"%s\"", S);
241       OS << "\n";
242     }
243   }
244 }
245 
246 // We want to supply the Unit associated with a .debug_line[.dwo] table when
247 // we dump it, if possible, but still dump the table even if there isn't a Unit.
248 // Therefore, collect up handles on all the Units that point into the
249 // line-table section.
250 typedef std::map<uint64_t, DWARFUnit *> LineToUnitMap;
251 
252 static LineToUnitMap
253 buildLineToUnitMap(DWARFContext::cu_iterator_range CUs,
254                    DWARFContext::tu_section_iterator_range TUSections) {
255   LineToUnitMap LineToUnit;
256   for (const auto &CU : CUs)
257     if (auto CUDIE = CU->getUnitDIE())
258       if (auto StmtOffset = toSectionOffset(CUDIE.find(DW_AT_stmt_list)))
259         LineToUnit.insert(std::make_pair(*StmtOffset, &*CU));
260   for (const auto &TUS : TUSections)
261     for (const auto &TU : TUS)
262       if (auto TUDIE = TU->getUnitDIE())
263         if (auto StmtOffset = toSectionOffset(TUDIE.find(DW_AT_stmt_list)))
264           LineToUnit.insert(std::make_pair(*StmtOffset, &*TU));
265   return LineToUnit;
266 }
267 
268 void DWARFContext::dump(
269     raw_ostream &OS, DIDumpOptions DumpOpts,
270     std::array<Optional<uint64_t>, DIDT_ID_Count> DumpOffsets) {
271 
272   Optional<uint64_t> DumpOffset;
273   uint64_t DumpType = DumpOpts.DumpType;
274 
275   StringRef Extension = sys::path::extension(DObj->getFileName());
276   bool IsDWO = (Extension == ".dwo") || (Extension == ".dwp");
277 
278   // Print UUID header.
279   const auto *ObjFile = DObj->getFile();
280   if (DumpType & DIDT_UUID)
281     dumpUUID(OS, *ObjFile);
282 
283   // Print a header for each explicitly-requested section.
284   // Otherwise just print one for non-empty sections.
285   // Only print empty .dwo section headers when dumping a .dwo file.
286   bool Explicit = DumpType != DIDT_All && !IsDWO;
287   bool ExplicitDWO = Explicit && IsDWO;
288   auto shouldDump = [&](bool Explicit, const char *Name, unsigned ID,
289                         StringRef Section) {
290     DumpOffset = DumpOffsets[ID];
291     unsigned Mask = 1U << ID;
292     bool Should = (DumpType & Mask) && (Explicit || !Section.empty());
293     if (Should)
294       OS << "\n" << Name << " contents:\n";
295     return Should;
296   };
297 
298   // Dump individual sections.
299   if (shouldDump(Explicit, ".debug_abbrev", DIDT_ID_DebugAbbrev,
300                  DObj->getAbbrevSection()))
301     getDebugAbbrev()->dump(OS);
302   if (shouldDump(ExplicitDWO, ".debug_abbrev.dwo", DIDT_ID_DebugAbbrev,
303                  DObj->getAbbrevDWOSection()))
304     getDebugAbbrevDWO()->dump(OS);
305 
306   auto dumpDebugInfo = [&](bool IsExplicit, const char *Name,
307                            DWARFSection Section, cu_iterator_range CUs) {
308     if (shouldDump(IsExplicit, Name, DIDT_ID_DebugInfo, Section.Data)) {
309       if (DumpOffset)
310         getDIEForOffset(DumpOffset.getValue())
311             .dump(OS, 0, DumpOpts.noImplicitRecursion());
312       else
313         for (const auto &CU : CUs)
314           CU->dump(OS, DumpOpts);
315     }
316   };
317   dumpDebugInfo(Explicit, ".debug_info", DObj->getInfoSection(),
318                 compile_units());
319   dumpDebugInfo(ExplicitDWO, ".debug_info.dwo", DObj->getInfoDWOSection(),
320                 dwo_compile_units());
321 
322   auto dumpDebugType = [&](const char *Name,
323                            tu_section_iterator_range TUSections) {
324     OS << '\n' << Name << " contents:\n";
325     DumpOffset = DumpOffsets[DIDT_ID_DebugTypes];
326     for (const auto &TUS : TUSections)
327       for (const auto &TU : TUS)
328         if (DumpOffset)
329           TU->getDIEForOffset(*DumpOffset)
330               .dump(OS, 0, DumpOpts.noImplicitRecursion());
331         else
332           TU->dump(OS, DumpOpts);
333   };
334   if ((DumpType & DIDT_DebugTypes)) {
335     if (Explicit || getNumTypeUnits())
336       dumpDebugType(".debug_types", type_unit_sections());
337     if (ExplicitDWO || getNumDWOTypeUnits())
338       dumpDebugType(".debug_types.dwo", dwo_type_unit_sections());
339   }
340 
341   if (shouldDump(Explicit, ".debug_loc", DIDT_ID_DebugLoc,
342                  DObj->getLocSection().Data)) {
343     getDebugLoc()->dump(OS, getRegisterInfo(), DumpOffset);
344   }
345   if (shouldDump(ExplicitDWO, ".debug_loc.dwo", DIDT_ID_DebugLoc,
346                  DObj->getLocDWOSection().Data)) {
347     getDebugLocDWO()->dump(OS, getRegisterInfo(), DumpOffset);
348   }
349 
350   if (shouldDump(Explicit, ".debug_frame", DIDT_ID_DebugFrame,
351                  DObj->getDebugFrameSection()))
352     getDebugFrame()->dump(OS, getRegisterInfo(), DumpOffset);
353 
354   if (shouldDump(Explicit, ".eh_frame", DIDT_ID_DebugFrame,
355                  DObj->getEHFrameSection()))
356     getEHFrame()->dump(OS, getRegisterInfo(), DumpOffset);
357 
358   if (DumpType & DIDT_DebugMacro) {
359     if (Explicit || !getDebugMacro()->empty()) {
360       OS << "\n.debug_macinfo contents:\n";
361       getDebugMacro()->dump(OS);
362     }
363   }
364 
365   if (shouldDump(Explicit, ".debug_aranges", DIDT_ID_DebugAranges,
366                  DObj->getARangeSection())) {
367     uint32_t offset = 0;
368     DataExtractor arangesData(DObj->getARangeSection(), isLittleEndian(), 0);
369     DWARFDebugArangeSet set;
370     while (set.extract(arangesData, &offset))
371       set.dump(OS);
372   }
373 
374   if (shouldDump(Explicit, ".debug_line", DIDT_ID_DebugLine,
375                  DObj->getLineSection().Data)) {
376     LineToUnitMap LineToUnit =
377         buildLineToUnitMap(compile_units(), type_unit_sections());
378     unsigned Offset = 0;
379     DWARFDataExtractor LineData(*DObj, DObj->getLineSection(), isLittleEndian(),
380                                 0);
381     while (Offset < LineData.getData().size()) {
382       DWARFUnit *U = nullptr;
383       auto It = LineToUnit.find(Offset);
384       if (It != LineToUnit.end())
385         U = It->second;
386       LineData.setAddressSize(U ? U->getAddressByteSize() : 0);
387       DWARFDebugLine::LineTable LineTable;
388       if (DumpOffset && Offset != *DumpOffset) {
389         // Find the size of this part of the line table section and skip it.
390         unsigned OldOffset = Offset;
391         LineTable.Prologue.parse(LineData, &Offset, *this, U);
392         Offset = OldOffset + LineTable.Prologue.TotalLength +
393                  LineTable.Prologue.sizeofTotalLength();
394         continue;
395       }
396       // Verbose dumping is done during parsing and not on the intermediate
397       // representation.
398       OS << "debug_line[" << format("0x%8.8x", Offset) << "]\n";
399       unsigned OldOffset = Offset;
400       if (DumpOpts.Verbose) {
401         LineTable.parse(LineData, &Offset, *this, U, &OS);
402       } else {
403         LineTable.parse(LineData, &Offset, *this, U);
404         LineTable.dump(OS, DIDumpOptions());
405       }
406       // Check for unparseable prologue, to avoid infinite loops.
407       if (OldOffset == Offset)
408         break;
409     }
410   }
411 
412   if (shouldDump(ExplicitDWO, ".debug_line.dwo", DIDT_ID_DebugLine,
413                  DObj->getLineDWOSection().Data)) {
414     LineToUnitMap LineToUnit =
415         buildLineToUnitMap(dwo_compile_units(), dwo_type_unit_sections());
416     unsigned Offset = 0;
417     DWARFDataExtractor LineData(*DObj, DObj->getLineDWOSection(),
418                                 isLittleEndian(), 0);
419     while (Offset < LineData.getData().size()) {
420       DWARFUnit *U = nullptr;
421       auto It = LineToUnit.find(Offset);
422       if (It != LineToUnit.end())
423         U = It->second;
424       DWARFDebugLine::LineTable LineTable;
425       unsigned OldOffset = Offset;
426       if (!LineTable.Prologue.parse(LineData, &Offset, *this, U))
427         break;
428       if (!DumpOffset || OldOffset == *DumpOffset)
429         LineTable.dump(OS, DumpOpts);
430     }
431   }
432 
433   if (shouldDump(Explicit, ".debug_cu_index", DIDT_ID_DebugCUIndex,
434                  DObj->getCUIndexSection())) {
435     getCUIndex().dump(OS);
436   }
437 
438   if (shouldDump(Explicit, ".debug_tu_index", DIDT_ID_DebugTUIndex,
439                  DObj->getTUIndexSection())) {
440     getTUIndex().dump(OS);
441   }
442 
443   if (shouldDump(Explicit, ".debug_str", DIDT_ID_DebugStr,
444                  DObj->getStringSection())) {
445     DataExtractor strData(DObj->getStringSection(), isLittleEndian(), 0);
446     uint32_t offset = 0;
447     uint32_t strOffset = 0;
448     while (const char *s = strData.getCStr(&offset)) {
449       OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
450       strOffset = offset;
451     }
452   }
453   if (shouldDump(ExplicitDWO, ".debug_str.dwo", DIDT_ID_DebugStr,
454                  DObj->getStringDWOSection())) {
455     DataExtractor strDWOData(DObj->getStringDWOSection(), isLittleEndian(), 0);
456     uint32_t offset = 0;
457     uint32_t strDWOOffset = 0;
458     while (const char *s = strDWOData.getCStr(&offset)) {
459       OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
460       strDWOOffset = offset;
461     }
462   }
463   if (shouldDump(Explicit, ".debug_line_str", DIDT_ID_DebugLineStr,
464                  DObj->getLineStringSection())) {
465     DataExtractor strData(DObj->getLineStringSection(), isLittleEndian(), 0);
466     uint32_t offset = 0;
467     uint32_t strOffset = 0;
468     while (const char *s = strData.getCStr(&offset)) {
469       OS << format("0x%8.8x: \"", strOffset);
470       OS.write_escaped(s);
471       OS << "\"\n";
472       strOffset = offset;
473     }
474   }
475 
476   if (shouldDump(Explicit, ".debug_ranges", DIDT_ID_DebugRanges,
477                  DObj->getRangeSection().Data)) {
478     // In fact, different compile units may have different address byte
479     // sizes, but for simplicity we just use the address byte size of the
480     // last compile unit (there is no easy and fast way to associate address
481     // range list and the compile unit it describes).
482     // FIXME: savedAddressByteSize seems sketchy.
483     uint8_t savedAddressByteSize = 0;
484     for (const auto &CU : compile_units()) {
485       savedAddressByteSize = CU->getAddressByteSize();
486       break;
487     }
488     DWARFDataExtractor rangesData(*DObj, DObj->getRangeSection(),
489                                   isLittleEndian(), savedAddressByteSize);
490     uint32_t offset = 0;
491     DWARFDebugRangeList rangeList;
492     while (rangeList.extract(rangesData, &offset))
493       rangeList.dump(OS);
494   }
495 
496   if (shouldDump(Explicit, ".debug_rnglists", DIDT_ID_DebugRnglists,
497                  DObj->getRnglistsSection().Data)) {
498     DWARFDataExtractor rnglistData(*DObj, DObj->getRnglistsSection(),
499                                    isLittleEndian(), 0);
500     uint32_t Offset = 0;
501     while (rnglistData.isValidOffset(Offset)) {
502       DWARFDebugRnglistTable Rnglists;
503       uint32_t TableOffset = Offset;
504       if (Error Err = Rnglists.extract(rnglistData, &Offset)) {
505         errs() << "error: " + toString(std::move(Err)) << '\n';
506         uint64_t Length = Rnglists.length();
507         // Keep going after an error, if we can, assuming that the length field
508         // could be read. If it couldn't, stop reading the section.
509         if (Length == 0)
510           break;
511         Offset = TableOffset + Length;
512       } else {
513         Rnglists.dump(OS, DumpOpts);
514       }
515     }
516   }
517 
518   if (shouldDump(Explicit, ".debug_pubnames", DIDT_ID_DebugPubnames,
519                  DObj->getPubNamesSection()))
520     DWARFDebugPubTable(DObj->getPubNamesSection(), isLittleEndian(), false)
521         .dump(OS);
522 
523   if (shouldDump(Explicit, ".debug_pubtypes", DIDT_ID_DebugPubtypes,
524                  DObj->getPubTypesSection()))
525     DWARFDebugPubTable(DObj->getPubTypesSection(), isLittleEndian(), false)
526         .dump(OS);
527 
528   if (shouldDump(Explicit, ".debug_gnu_pubnames", DIDT_ID_DebugGnuPubnames,
529                  DObj->getGnuPubNamesSection()))
530     DWARFDebugPubTable(DObj->getGnuPubNamesSection(), isLittleEndian(),
531                        true /* GnuStyle */)
532         .dump(OS);
533 
534   if (shouldDump(Explicit, ".debug_gnu_pubtypes", DIDT_ID_DebugGnuPubtypes,
535                  DObj->getGnuPubTypesSection()))
536     DWARFDebugPubTable(DObj->getGnuPubTypesSection(), isLittleEndian(),
537                        true /* GnuStyle */)
538         .dump(OS);
539 
540   if (shouldDump(Explicit, ".debug_str_offsets", DIDT_ID_DebugStrOffsets,
541                  DObj->getStringOffsetSection().Data))
542     dumpStringOffsetsSection(
543         OS, "debug_str_offsets", *DObj, DObj->getStringOffsetSection(),
544         DObj->getStringSection(), compile_units(), type_unit_sections(),
545         isLittleEndian(), getMaxVersion());
546   if (shouldDump(ExplicitDWO, ".debug_str_offsets.dwo", DIDT_ID_DebugStrOffsets,
547                  DObj->getStringOffsetDWOSection().Data))
548     dumpStringOffsetsSection(
549         OS, "debug_str_offsets.dwo", *DObj, DObj->getStringOffsetDWOSection(),
550         DObj->getStringDWOSection(), dwo_compile_units(),
551         dwo_type_unit_sections(), isLittleEndian(), getMaxVersion());
552 
553   if (shouldDump(Explicit, ".gnu_index", DIDT_ID_GdbIndex,
554                  DObj->getGdbIndexSection())) {
555     getGdbIndex().dump(OS);
556   }
557 
558   if (shouldDump(Explicit, ".apple_names", DIDT_ID_AppleNames,
559                  DObj->getAppleNamesSection().Data))
560     getAppleNames().dump(OS);
561 
562   if (shouldDump(Explicit, ".apple_types", DIDT_ID_AppleTypes,
563                  DObj->getAppleTypesSection().Data))
564     getAppleTypes().dump(OS);
565 
566   if (shouldDump(Explicit, ".apple_namespaces", DIDT_ID_AppleNamespaces,
567                  DObj->getAppleNamespacesSection().Data))
568     getAppleNamespaces().dump(OS);
569 
570   if (shouldDump(Explicit, ".apple_objc", DIDT_ID_AppleObjC,
571                  DObj->getAppleObjCSection().Data))
572     getAppleObjC().dump(OS);
573   if (shouldDump(Explicit, ".debug_names", DIDT_ID_DebugNames,
574                  DObj->getDebugNamesSection().Data))
575     getDebugNames().dump(OS);
576 }
577 
578 DWARFCompileUnit *DWARFContext::getDWOCompileUnitForHash(uint64_t Hash) {
579   DWOCUs.parseDWO(*this, DObj->getInfoDWOSection(), true);
580 
581   if (const auto &CUI = getCUIndex()) {
582     if (const auto *R = CUI.getFromHash(Hash))
583       return DWOCUs.getUnitForIndexEntry(*R);
584     return nullptr;
585   }
586 
587   // If there's no index, just search through the CUs in the DWO - there's
588   // probably only one unless this is something like LTO - though an in-process
589   // built/cached lookup table could be used in that case to improve repeated
590   // lookups of different CUs in the DWO.
591   for (const auto &DWOCU : dwo_compile_units())
592     if (DWOCU->getDWOId() == Hash)
593       return DWOCU.get();
594   return nullptr;
595 }
596 
597 DWARFDie DWARFContext::getDIEForOffset(uint32_t Offset) {
598   parseCompileUnits();
599   if (auto *CU = CUs.getUnitForOffset(Offset))
600     return CU->getDIEForOffset(Offset);
601   return DWARFDie();
602 }
603 
604 bool DWARFContext::verify(raw_ostream &OS, DIDumpOptions DumpOpts) {
605   bool Success = true;
606   DWARFVerifier verifier(OS, *this, DumpOpts);
607 
608   Success &= verifier.handleDebugAbbrev();
609   if (DumpOpts.DumpType & DIDT_DebugInfo)
610     Success &= verifier.handleDebugInfo();
611   if (DumpOpts.DumpType & DIDT_DebugLine)
612     Success &= verifier.handleDebugLine();
613   Success &= verifier.handleAccelTables();
614   return Success;
615 }
616 
617 const DWARFUnitIndex &DWARFContext::getCUIndex() {
618   if (CUIndex)
619     return *CUIndex;
620 
621   DataExtractor CUIndexData(DObj->getCUIndexSection(), isLittleEndian(), 0);
622 
623   CUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_INFO);
624   CUIndex->parse(CUIndexData);
625   return *CUIndex;
626 }
627 
628 const DWARFUnitIndex &DWARFContext::getTUIndex() {
629   if (TUIndex)
630     return *TUIndex;
631 
632   DataExtractor TUIndexData(DObj->getTUIndexSection(), isLittleEndian(), 0);
633 
634   TUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_TYPES);
635   TUIndex->parse(TUIndexData);
636   return *TUIndex;
637 }
638 
639 DWARFGdbIndex &DWARFContext::getGdbIndex() {
640   if (GdbIndex)
641     return *GdbIndex;
642 
643   DataExtractor GdbIndexData(DObj->getGdbIndexSection(), true /*LE*/, 0);
644   GdbIndex = llvm::make_unique<DWARFGdbIndex>();
645   GdbIndex->parse(GdbIndexData);
646   return *GdbIndex;
647 }
648 
649 const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
650   if (Abbrev)
651     return Abbrev.get();
652 
653   DataExtractor abbrData(DObj->getAbbrevSection(), isLittleEndian(), 0);
654 
655   Abbrev.reset(new DWARFDebugAbbrev());
656   Abbrev->extract(abbrData);
657   return Abbrev.get();
658 }
659 
660 const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
661   if (AbbrevDWO)
662     return AbbrevDWO.get();
663 
664   DataExtractor abbrData(DObj->getAbbrevDWOSection(), isLittleEndian(), 0);
665   AbbrevDWO.reset(new DWARFDebugAbbrev());
666   AbbrevDWO->extract(abbrData);
667   return AbbrevDWO.get();
668 }
669 
670 const DWARFDebugLoc *DWARFContext::getDebugLoc() {
671   if (Loc)
672     return Loc.get();
673 
674   Loc.reset(new DWARFDebugLoc);
675   // Assume all compile units have the same address byte size.
676   if (getNumCompileUnits()) {
677     DWARFDataExtractor LocData(*DObj, DObj->getLocSection(), isLittleEndian(),
678                                getCompileUnitAtIndex(0)->getAddressByteSize());
679     Loc->parse(LocData);
680   }
681   return Loc.get();
682 }
683 
684 const DWARFDebugLocDWO *DWARFContext::getDebugLocDWO() {
685   if (LocDWO)
686     return LocDWO.get();
687 
688   LocDWO.reset(new DWARFDebugLocDWO());
689   // Assume all compile units have the same address byte size.
690   if (getNumCompileUnits()) {
691     DataExtractor LocData(DObj->getLocDWOSection().Data, isLittleEndian(),
692                           getCompileUnitAtIndex(0)->getAddressByteSize());
693     LocDWO->parse(LocData);
694   }
695   return LocDWO.get();
696 }
697 
698 const DWARFDebugAranges *DWARFContext::getDebugAranges() {
699   if (Aranges)
700     return Aranges.get();
701 
702   Aranges.reset(new DWARFDebugAranges());
703   Aranges->generate(this);
704   return Aranges.get();
705 }
706 
707 const DWARFDebugFrame *DWARFContext::getDebugFrame() {
708   if (DebugFrame)
709     return DebugFrame.get();
710 
711   // There's a "bug" in the DWARFv3 standard with respect to the target address
712   // size within debug frame sections. While DWARF is supposed to be independent
713   // of its container, FDEs have fields with size being "target address size",
714   // which isn't specified in DWARF in general. It's only specified for CUs, but
715   // .eh_frame can appear without a .debug_info section. Follow the example of
716   // other tools (libdwarf) and extract this from the container (ObjectFile
717   // provides this information). This problem is fixed in DWARFv4
718   // See this dwarf-discuss discussion for more details:
719   // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
720   DWARFDataExtractor debugFrameData(DObj->getDebugFrameSection(),
721                                     isLittleEndian(), DObj->getAddressSize());
722   DebugFrame.reset(new DWARFDebugFrame(false /* IsEH */));
723   DebugFrame->parse(debugFrameData);
724   return DebugFrame.get();
725 }
726 
727 const DWARFDebugFrame *DWARFContext::getEHFrame() {
728   if (EHFrame)
729     return EHFrame.get();
730 
731   DWARFDataExtractor debugFrameData(DObj->getEHFrameSection(), isLittleEndian(),
732                                     DObj->getAddressSize());
733   DebugFrame.reset(new DWARFDebugFrame(true /* IsEH */));
734   DebugFrame->parse(debugFrameData);
735   return DebugFrame.get();
736 }
737 
738 const DWARFDebugMacro *DWARFContext::getDebugMacro() {
739   if (Macro)
740     return Macro.get();
741 
742   DataExtractor MacinfoData(DObj->getMacinfoSection(), isLittleEndian(), 0);
743   Macro.reset(new DWARFDebugMacro());
744   Macro->parse(MacinfoData);
745   return Macro.get();
746 }
747 
748 template <typename T>
749 static T &getAccelTable(std::unique_ptr<T> &Cache, const DWARFObject &Obj,
750                         const DWARFSection &Section, StringRef StringSection,
751                         bool IsLittleEndian) {
752   if (Cache)
753     return *Cache;
754   DWARFDataExtractor AccelSection(Obj, Section, IsLittleEndian, 0);
755   DataExtractor StrData(StringSection, IsLittleEndian, 0);
756   Cache.reset(new T(AccelSection, StrData));
757   if (Error E = Cache->extract())
758     llvm::consumeError(std::move(E));
759   return *Cache;
760 }
761 
762 const DWARFDebugNames &DWARFContext::getDebugNames() {
763   return getAccelTable(Names, *DObj, DObj->getDebugNamesSection(),
764                        DObj->getStringSection(), isLittleEndian());
765 }
766 
767 const AppleAcceleratorTable &DWARFContext::getAppleNames() {
768   return getAccelTable(AppleNames, *DObj, DObj->getAppleNamesSection(),
769                        DObj->getStringSection(), isLittleEndian());
770 }
771 
772 const AppleAcceleratorTable &DWARFContext::getAppleTypes() {
773   return getAccelTable(AppleTypes, *DObj, DObj->getAppleTypesSection(),
774                        DObj->getStringSection(), isLittleEndian());
775 }
776 
777 const AppleAcceleratorTable &DWARFContext::getAppleNamespaces() {
778   return getAccelTable(AppleNamespaces, *DObj,
779                        DObj->getAppleNamespacesSection(),
780                        DObj->getStringSection(), isLittleEndian());
781 }
782 
783 const AppleAcceleratorTable &DWARFContext::getAppleObjC() {
784   return getAccelTable(AppleObjC, *DObj, DObj->getAppleObjCSection(),
785                        DObj->getStringSection(), isLittleEndian());
786 }
787 
788 const DWARFLineTable *
789 DWARFContext::getLineTableForUnit(DWARFUnit *U) {
790   if (!Line)
791     Line.reset(new DWARFDebugLine);
792 
793   auto UnitDIE = U->getUnitDIE();
794   if (!UnitDIE)
795     return nullptr;
796 
797   auto Offset = toSectionOffset(UnitDIE.find(DW_AT_stmt_list));
798   if (!Offset)
799     return nullptr; // No line table for this compile unit.
800 
801   uint32_t stmtOffset = *Offset + U->getLineTableOffset();
802   // See if the line table is cached.
803   if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
804     return lt;
805 
806   // Make sure the offset is good before we try to parse.
807   if (stmtOffset >= U->getLineSection().Data.size())
808     return nullptr;
809 
810   // We have to parse it first.
811   DWARFDataExtractor lineData(*DObj, U->getLineSection(), isLittleEndian(),
812                               U->getAddressByteSize());
813   return Line->getOrParseLineTable(lineData, stmtOffset, *this, U);
814 }
815 
816 void DWARFContext::parseCompileUnits() {
817   CUs.parse(*this, DObj->getInfoSection());
818 }
819 
820 void DWARFContext::parseTypeUnits() {
821   if (!TUs.empty())
822     return;
823   DObj->forEachTypesSections([&](const DWARFSection &S) {
824     TUs.emplace_back();
825     TUs.back().parse(*this, S);
826   });
827 }
828 
829 void DWARFContext::parseDWOCompileUnits() {
830   DWOCUs.parseDWO(*this, DObj->getInfoDWOSection());
831 }
832 
833 void DWARFContext::parseDWOTypeUnits() {
834   if (!DWOTUs.empty())
835     return;
836   DObj->forEachTypesDWOSections([&](const DWARFSection &S) {
837     DWOTUs.emplace_back();
838     DWOTUs.back().parseDWO(*this, S);
839   });
840 }
841 
842 DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
843   parseCompileUnits();
844   return CUs.getUnitForOffset(Offset);
845 }
846 
847 DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
848   // First, get the offset of the compile unit.
849   uint32_t CUOffset = getDebugAranges()->findAddress(Address);
850   // Retrieve the compile unit.
851   return getCompileUnitForOffset(CUOffset);
852 }
853 
854 DWARFContext::DIEsForAddress DWARFContext::getDIEsForAddress(uint64_t Address) {
855   DIEsForAddress Result;
856 
857   DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
858   if (!CU)
859     return Result;
860 
861   Result.CompileUnit = CU;
862   Result.FunctionDIE = CU->getSubroutineForAddress(Address);
863 
864   std::vector<DWARFDie> Worklist;
865   Worklist.push_back(Result.FunctionDIE);
866   while (!Worklist.empty()) {
867     DWARFDie DIE = Worklist.back();
868     Worklist.pop_back();
869 
870     if (DIE.getTag() == DW_TAG_lexical_block &&
871         DIE.addressRangeContainsAddress(Address)) {
872       Result.BlockDIE = DIE;
873       break;
874     }
875 
876     for (auto Child : DIE)
877       Worklist.push_back(Child);
878   }
879 
880   return Result;
881 }
882 
883 static bool getFunctionNameAndStartLineForAddress(DWARFCompileUnit *CU,
884                                                   uint64_t Address,
885                                                   FunctionNameKind Kind,
886                                                   std::string &FunctionName,
887                                                   uint32_t &StartLine) {
888   // The address may correspond to instruction in some inlined function,
889   // so we have to build the chain of inlined functions and take the
890   // name of the topmost function in it.
891   SmallVector<DWARFDie, 4> InlinedChain;
892   CU->getInlinedChainForAddress(Address, InlinedChain);
893   if (InlinedChain.empty())
894     return false;
895 
896   const DWARFDie &DIE = InlinedChain[0];
897   bool FoundResult = false;
898   const char *Name = nullptr;
899   if (Kind != FunctionNameKind::None && (Name = DIE.getSubroutineName(Kind))) {
900     FunctionName = Name;
901     FoundResult = true;
902   }
903   if (auto DeclLineResult = DIE.getDeclLine()) {
904     StartLine = DeclLineResult;
905     FoundResult = true;
906   }
907 
908   return FoundResult;
909 }
910 
911 DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
912                                                DILineInfoSpecifier Spec) {
913   DILineInfo Result;
914 
915   DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
916   if (!CU)
917     return Result;
918   getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind,
919                                         Result.FunctionName,
920                                         Result.StartLine);
921   if (Spec.FLIKind != FileLineInfoKind::None) {
922     if (const DWARFLineTable *LineTable = getLineTableForUnit(CU))
923       LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
924                                            Spec.FLIKind, Result);
925   }
926   return Result;
927 }
928 
929 DILineInfoTable
930 DWARFContext::getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
931                                          DILineInfoSpecifier Spec) {
932   DILineInfoTable  Lines;
933   DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
934   if (!CU)
935     return Lines;
936 
937   std::string FunctionName = "<invalid>";
938   uint32_t StartLine = 0;
939   getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind, FunctionName,
940                                         StartLine);
941 
942   // If the Specifier says we don't need FileLineInfo, just
943   // return the top-most function at the starting address.
944   if (Spec.FLIKind == FileLineInfoKind::None) {
945     DILineInfo Result;
946     Result.FunctionName = FunctionName;
947     Result.StartLine = StartLine;
948     Lines.push_back(std::make_pair(Address, Result));
949     return Lines;
950   }
951 
952   const DWARFLineTable *LineTable = getLineTableForUnit(CU);
953 
954   // Get the index of row we're looking for in the line table.
955   std::vector<uint32_t> RowVector;
956   if (!LineTable->lookupAddressRange(Address, Size, RowVector))
957     return Lines;
958 
959   for (uint32_t RowIndex : RowVector) {
960     // Take file number and line/column from the row.
961     const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
962     DILineInfo Result;
963     LineTable->getFileNameByIndex(Row.File, CU->getCompilationDir(),
964                                   Spec.FLIKind, Result.FileName);
965     Result.FunctionName = FunctionName;
966     Result.Line = Row.Line;
967     Result.Column = Row.Column;
968     Result.StartLine = StartLine;
969     Lines.push_back(std::make_pair(Row.Address, Result));
970   }
971 
972   return Lines;
973 }
974 
975 DIInliningInfo
976 DWARFContext::getInliningInfoForAddress(uint64_t Address,
977                                         DILineInfoSpecifier Spec) {
978   DIInliningInfo InliningInfo;
979 
980   DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
981   if (!CU)
982     return InliningInfo;
983 
984   const DWARFLineTable *LineTable = nullptr;
985   SmallVector<DWARFDie, 4> InlinedChain;
986   CU->getInlinedChainForAddress(Address, InlinedChain);
987   if (InlinedChain.size() == 0) {
988     // If there is no DIE for address (e.g. it is in unavailable .dwo file),
989     // try to at least get file/line info from symbol table.
990     if (Spec.FLIKind != FileLineInfoKind::None) {
991       DILineInfo Frame;
992       LineTable = getLineTableForUnit(CU);
993       if (LineTable &&
994           LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
995                                                Spec.FLIKind, Frame))
996         InliningInfo.addFrame(Frame);
997     }
998     return InliningInfo;
999   }
1000 
1001   uint32_t CallFile = 0, CallLine = 0, CallColumn = 0, CallDiscriminator = 0;
1002   for (uint32_t i = 0, n = InlinedChain.size(); i != n; i++) {
1003     DWARFDie &FunctionDIE = InlinedChain[i];
1004     DILineInfo Frame;
1005     // Get function name if necessary.
1006     if (const char *Name = FunctionDIE.getSubroutineName(Spec.FNKind))
1007       Frame.FunctionName = Name;
1008     if (auto DeclLineResult = FunctionDIE.getDeclLine())
1009       Frame.StartLine = DeclLineResult;
1010     if (Spec.FLIKind != FileLineInfoKind::None) {
1011       if (i == 0) {
1012         // For the topmost frame, initialize the line table of this
1013         // compile unit and fetch file/line info from it.
1014         LineTable = getLineTableForUnit(CU);
1015         // For the topmost routine, get file/line info from line table.
1016         if (LineTable)
1017           LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
1018                                                Spec.FLIKind, Frame);
1019       } else {
1020         // Otherwise, use call file, call line and call column from
1021         // previous DIE in inlined chain.
1022         if (LineTable)
1023           LineTable->getFileNameByIndex(CallFile, CU->getCompilationDir(),
1024                                         Spec.FLIKind, Frame.FileName);
1025         Frame.Line = CallLine;
1026         Frame.Column = CallColumn;
1027         Frame.Discriminator = CallDiscriminator;
1028       }
1029       // Get call file/line/column of a current DIE.
1030       if (i + 1 < n) {
1031         FunctionDIE.getCallerFrame(CallFile, CallLine, CallColumn,
1032                                    CallDiscriminator);
1033       }
1034     }
1035     InliningInfo.addFrame(Frame);
1036   }
1037   return InliningInfo;
1038 }
1039 
1040 std::shared_ptr<DWARFContext>
1041 DWARFContext::getDWOContext(StringRef AbsolutePath) {
1042   if (auto S = DWP.lock()) {
1043     DWARFContext *Ctxt = S->Context.get();
1044     return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
1045   }
1046 
1047   std::weak_ptr<DWOFile> *Entry = &DWOFiles[AbsolutePath];
1048 
1049   if (auto S = Entry->lock()) {
1050     DWARFContext *Ctxt = S->Context.get();
1051     return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
1052   }
1053 
1054   Expected<OwningBinary<ObjectFile>> Obj = [&] {
1055     if (!CheckedForDWP) {
1056       SmallString<128> DWPName;
1057       auto Obj = object::ObjectFile::createObjectFile(
1058           this->DWPName.empty()
1059               ? (DObj->getFileName() + ".dwp").toStringRef(DWPName)
1060               : StringRef(this->DWPName));
1061       if (Obj) {
1062         Entry = &DWP;
1063         return Obj;
1064       } else {
1065         CheckedForDWP = true;
1066         // TODO: Should this error be handled (maybe in a high verbosity mode)
1067         // before falling back to .dwo files?
1068         consumeError(Obj.takeError());
1069       }
1070     }
1071 
1072     return object::ObjectFile::createObjectFile(AbsolutePath);
1073   }();
1074 
1075   if (!Obj) {
1076     // TODO: Actually report errors helpfully.
1077     consumeError(Obj.takeError());
1078     return nullptr;
1079   }
1080 
1081   auto S = std::make_shared<DWOFile>();
1082   S->File = std::move(Obj.get());
1083   S->Context = DWARFContext::create(*S->File.getBinary());
1084   *Entry = S;
1085   auto *Ctxt = S->Context.get();
1086   return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
1087 }
1088 
1089 static Error createError(const Twine &Reason, llvm::Error E) {
1090   return make_error<StringError>(Reason + toString(std::move(E)),
1091                                  inconvertibleErrorCode());
1092 }
1093 
1094 /// SymInfo contains information about symbol: it's address
1095 /// and section index which is -1LL for absolute symbols.
1096 struct SymInfo {
1097   uint64_t Address;
1098   uint64_t SectionIndex;
1099 };
1100 
1101 /// Returns the address of symbol relocation used against and a section index.
1102 /// Used for futher relocations computation. Symbol's section load address is
1103 static Expected<SymInfo> getSymbolInfo(const object::ObjectFile &Obj,
1104                                        const RelocationRef &Reloc,
1105                                        const LoadedObjectInfo *L,
1106                                        std::map<SymbolRef, SymInfo> &Cache) {
1107   SymInfo Ret = {0, (uint64_t)-1LL};
1108   object::section_iterator RSec = Obj.section_end();
1109   object::symbol_iterator Sym = Reloc.getSymbol();
1110 
1111   std::map<SymbolRef, SymInfo>::iterator CacheIt = Cache.end();
1112   // First calculate the address of the symbol or section as it appears
1113   // in the object file
1114   if (Sym != Obj.symbol_end()) {
1115     bool New;
1116     std::tie(CacheIt, New) = Cache.insert({*Sym, {0, 0}});
1117     if (!New)
1118       return CacheIt->second;
1119 
1120     Expected<uint64_t> SymAddrOrErr = Sym->getAddress();
1121     if (!SymAddrOrErr)
1122       return createError("failed to compute symbol address: ",
1123                          SymAddrOrErr.takeError());
1124 
1125     // Also remember what section this symbol is in for later
1126     auto SectOrErr = Sym->getSection();
1127     if (!SectOrErr)
1128       return createError("failed to get symbol section: ",
1129                          SectOrErr.takeError());
1130 
1131     RSec = *SectOrErr;
1132     Ret.Address = *SymAddrOrErr;
1133   } else if (auto *MObj = dyn_cast<MachOObjectFile>(&Obj)) {
1134     RSec = MObj->getRelocationSection(Reloc.getRawDataRefImpl());
1135     Ret.Address = RSec->getAddress();
1136   }
1137 
1138   if (RSec != Obj.section_end())
1139     Ret.SectionIndex = RSec->getIndex();
1140 
1141   // If we are given load addresses for the sections, we need to adjust:
1142   // SymAddr = (Address of Symbol Or Section in File) -
1143   //           (Address of Section in File) +
1144   //           (Load Address of Section)
1145   // RSec is now either the section being targeted or the section
1146   // containing the symbol being targeted. In either case,
1147   // we need to perform the same computation.
1148   if (L && RSec != Obj.section_end())
1149     if (uint64_t SectionLoadAddress = L->getSectionLoadAddress(*RSec))
1150       Ret.Address += SectionLoadAddress - RSec->getAddress();
1151 
1152   if (CacheIt != Cache.end())
1153     CacheIt->second = Ret;
1154 
1155   return Ret;
1156 }
1157 
1158 static bool isRelocScattered(const object::ObjectFile &Obj,
1159                              const RelocationRef &Reloc) {
1160   const MachOObjectFile *MachObj = dyn_cast<MachOObjectFile>(&Obj);
1161   if (!MachObj)
1162     return false;
1163   // MachO also has relocations that point to sections and
1164   // scattered relocations.
1165   auto RelocInfo = MachObj->getRelocation(Reloc.getRawDataRefImpl());
1166   return MachObj->isRelocationScattered(RelocInfo);
1167 }
1168 
1169 ErrorPolicy DWARFContext::defaultErrorHandler(Error E) {
1170   errs() << "error: " + toString(std::move(E)) << '\n';
1171   return ErrorPolicy::Continue;
1172 }
1173 
1174 namespace {
1175 struct DWARFSectionMap final : public DWARFSection {
1176   RelocAddrMap Relocs;
1177 };
1178 
1179 class DWARFObjInMemory final : public DWARFObject {
1180   bool IsLittleEndian;
1181   uint8_t AddressSize;
1182   StringRef FileName;
1183   const object::ObjectFile *Obj = nullptr;
1184   std::vector<SectionName> SectionNames;
1185 
1186   using TypeSectionMap = MapVector<object::SectionRef, DWARFSectionMap,
1187                                    std::map<object::SectionRef, unsigned>>;
1188 
1189   TypeSectionMap TypesSections;
1190   TypeSectionMap TypesDWOSections;
1191 
1192   DWARFSectionMap InfoSection;
1193   DWARFSectionMap LocSection;
1194   DWARFSectionMap LineSection;
1195   DWARFSectionMap RangeSection;
1196   DWARFSectionMap RnglistsSection;
1197   DWARFSectionMap StringOffsetSection;
1198   DWARFSectionMap InfoDWOSection;
1199   DWARFSectionMap LineDWOSection;
1200   DWARFSectionMap LocDWOSection;
1201   DWARFSectionMap StringOffsetDWOSection;
1202   DWARFSectionMap RangeDWOSection;
1203   DWARFSectionMap AddrSection;
1204   DWARFSectionMap AppleNamesSection;
1205   DWARFSectionMap AppleTypesSection;
1206   DWARFSectionMap AppleNamespacesSection;
1207   DWARFSectionMap AppleObjCSection;
1208   DWARFSectionMap DebugNamesSection;
1209 
1210   DWARFSectionMap *mapNameToDWARFSection(StringRef Name) {
1211     return StringSwitch<DWARFSectionMap *>(Name)
1212         .Case("debug_info", &InfoSection)
1213         .Case("debug_loc", &LocSection)
1214         .Case("debug_line", &LineSection)
1215         .Case("debug_str_offsets", &StringOffsetSection)
1216         .Case("debug_ranges", &RangeSection)
1217         .Case("debug_rnglists", &RnglistsSection)
1218         .Case("debug_info.dwo", &InfoDWOSection)
1219         .Case("debug_loc.dwo", &LocDWOSection)
1220         .Case("debug_line.dwo", &LineDWOSection)
1221         .Case("debug_names", &DebugNamesSection)
1222         .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
1223         .Case("debug_addr", &AddrSection)
1224         .Case("apple_names", &AppleNamesSection)
1225         .Case("apple_types", &AppleTypesSection)
1226         .Case("apple_namespaces", &AppleNamespacesSection)
1227         .Case("apple_namespac", &AppleNamespacesSection)
1228         .Case("apple_objc", &AppleObjCSection)
1229         .Default(nullptr);
1230   }
1231 
1232   StringRef AbbrevSection;
1233   StringRef ARangeSection;
1234   StringRef DebugFrameSection;
1235   StringRef EHFrameSection;
1236   StringRef StringSection;
1237   StringRef MacinfoSection;
1238   StringRef PubNamesSection;
1239   StringRef PubTypesSection;
1240   StringRef GnuPubNamesSection;
1241   StringRef AbbrevDWOSection;
1242   StringRef StringDWOSection;
1243   StringRef GnuPubTypesSection;
1244   StringRef CUIndexSection;
1245   StringRef GdbIndexSection;
1246   StringRef TUIndexSection;
1247   StringRef LineStringSection;
1248 
1249   SmallVector<SmallString<32>, 4> UncompressedSections;
1250 
1251   StringRef *mapSectionToMember(StringRef Name) {
1252     if (DWARFSection *Sec = mapNameToDWARFSection(Name))
1253       return &Sec->Data;
1254     return StringSwitch<StringRef *>(Name)
1255         .Case("debug_abbrev", &AbbrevSection)
1256         .Case("debug_aranges", &ARangeSection)
1257         .Case("debug_frame", &DebugFrameSection)
1258         .Case("eh_frame", &EHFrameSection)
1259         .Case("debug_str", &StringSection)
1260         .Case("debug_macinfo", &MacinfoSection)
1261         .Case("debug_pubnames", &PubNamesSection)
1262         .Case("debug_pubtypes", &PubTypesSection)
1263         .Case("debug_gnu_pubnames", &GnuPubNamesSection)
1264         .Case("debug_gnu_pubtypes", &GnuPubTypesSection)
1265         .Case("debug_abbrev.dwo", &AbbrevDWOSection)
1266         .Case("debug_str.dwo", &StringDWOSection)
1267         .Case("debug_cu_index", &CUIndexSection)
1268         .Case("debug_tu_index", &TUIndexSection)
1269         .Case("gdb_index", &GdbIndexSection)
1270         .Case("debug_line_str", &LineStringSection)
1271         // Any more debug info sections go here.
1272         .Default(nullptr);
1273   }
1274 
1275   /// If Sec is compressed section, decompresses and updates its contents
1276   /// provided by Data. Otherwise leaves it unchanged.
1277   Error maybeDecompress(const object::SectionRef &Sec, StringRef Name,
1278                         StringRef &Data) {
1279     if (!Decompressor::isCompressed(Sec))
1280       return Error::success();
1281 
1282     Expected<Decompressor> Decompressor =
1283         Decompressor::create(Name, Data, IsLittleEndian, AddressSize == 8);
1284     if (!Decompressor)
1285       return Decompressor.takeError();
1286 
1287     SmallString<32> Out;
1288     if (auto Err = Decompressor->resizeAndDecompress(Out))
1289       return Err;
1290 
1291     UncompressedSections.emplace_back(std::move(Out));
1292     Data = UncompressedSections.back();
1293 
1294     return Error::success();
1295   }
1296 
1297 public:
1298   DWARFObjInMemory(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections,
1299                    uint8_t AddrSize, bool IsLittleEndian)
1300       : IsLittleEndian(IsLittleEndian) {
1301     for (const auto &SecIt : Sections) {
1302       if (StringRef *SectionData = mapSectionToMember(SecIt.first()))
1303         *SectionData = SecIt.second->getBuffer();
1304     }
1305   }
1306   DWARFObjInMemory(const object::ObjectFile &Obj, const LoadedObjectInfo *L,
1307                    function_ref<ErrorPolicy(Error)> HandleError)
1308       : IsLittleEndian(Obj.isLittleEndian()),
1309         AddressSize(Obj.getBytesInAddress()), FileName(Obj.getFileName()),
1310         Obj(&Obj) {
1311 
1312     StringMap<unsigned> SectionAmountMap;
1313     for (const SectionRef &Section : Obj.sections()) {
1314       StringRef Name;
1315       Section.getName(Name);
1316       ++SectionAmountMap[Name];
1317       SectionNames.push_back({ Name, true });
1318 
1319       // Skip BSS and Virtual sections, they aren't interesting.
1320       if (Section.isBSS() || Section.isVirtual())
1321         continue;
1322 
1323       // Skip sections stripped by dsymutil.
1324       if (Section.isStripped())
1325         continue;
1326 
1327       StringRef Data;
1328       section_iterator RelocatedSection = Section.getRelocatedSection();
1329       // Try to obtain an already relocated version of this section.
1330       // Else use the unrelocated section from the object file. We'll have to
1331       // apply relocations ourselves later.
1332       if (!L || !L->getLoadedSectionContents(*RelocatedSection, Data))
1333         Section.getContents(Data);
1334 
1335       if (auto Err = maybeDecompress(Section, Name, Data)) {
1336         ErrorPolicy EP = HandleError(createError(
1337             "failed to decompress '" + Name + "', ", std::move(Err)));
1338         if (EP == ErrorPolicy::Halt)
1339           return;
1340         continue;
1341       }
1342 
1343       // Compressed sections names in GNU style starts from ".z",
1344       // at this point section is decompressed and we drop compression prefix.
1345       Name = Name.substr(
1346           Name.find_first_not_of("._z")); // Skip ".", "z" and "_" prefixes.
1347 
1348       // Map platform specific debug section names to DWARF standard section
1349       // names.
1350       Name = Obj.mapDebugSectionName(Name);
1351 
1352       if (StringRef *SectionData = mapSectionToMember(Name)) {
1353         *SectionData = Data;
1354         if (Name == "debug_ranges") {
1355           // FIXME: Use the other dwo range section when we emit it.
1356           RangeDWOSection.Data = Data;
1357         }
1358       } else if (Name == "debug_types") {
1359         // Find debug_types data by section rather than name as there are
1360         // multiple, comdat grouped, debug_types sections.
1361         TypesSections[Section].Data = Data;
1362       } else if (Name == "debug_types.dwo") {
1363         TypesDWOSections[Section].Data = Data;
1364       }
1365 
1366       if (RelocatedSection == Obj.section_end())
1367         continue;
1368 
1369       StringRef RelSecName;
1370       StringRef RelSecData;
1371       RelocatedSection->getName(RelSecName);
1372 
1373       // If the section we're relocating was relocated already by the JIT,
1374       // then we used the relocated version above, so we do not need to process
1375       // relocations for it now.
1376       if (L && L->getLoadedSectionContents(*RelocatedSection, RelSecData))
1377         continue;
1378 
1379       // In Mach-o files, the relocations do not need to be applied if
1380       // there is no load offset to apply. The value read at the
1381       // relocation point already factors in the section address
1382       // (actually applying the relocations will produce wrong results
1383       // as the section address will be added twice).
1384       if (!L && isa<MachOObjectFile>(&Obj))
1385         continue;
1386 
1387       RelSecName = RelSecName.substr(
1388           RelSecName.find_first_not_of("._z")); // Skip . and _ prefixes.
1389 
1390       // TODO: Add support for relocations in other sections as needed.
1391       // Record relocations for the debug_info and debug_line sections.
1392       DWARFSectionMap *Sec = mapNameToDWARFSection(RelSecName);
1393       RelocAddrMap *Map = Sec ? &Sec->Relocs : nullptr;
1394       if (!Map) {
1395         // Find debug_types relocs by section rather than name as there are
1396         // multiple, comdat grouped, debug_types sections.
1397         if (RelSecName == "debug_types")
1398           Map =
1399               &static_cast<DWARFSectionMap &>(TypesSections[*RelocatedSection])
1400                    .Relocs;
1401         else if (RelSecName == "debug_types.dwo")
1402           Map = &static_cast<DWARFSectionMap &>(
1403                      TypesDWOSections[*RelocatedSection])
1404                      .Relocs;
1405         else
1406           continue;
1407       }
1408 
1409       if (Section.relocation_begin() == Section.relocation_end())
1410         continue;
1411 
1412       // Symbol to [address, section index] cache mapping.
1413       std::map<SymbolRef, SymInfo> AddrCache;
1414       for (const RelocationRef &Reloc : Section.relocations()) {
1415         // FIXME: it's not clear how to correctly handle scattered
1416         // relocations.
1417         if (isRelocScattered(Obj, Reloc))
1418           continue;
1419 
1420         Expected<SymInfo> SymInfoOrErr =
1421             getSymbolInfo(Obj, Reloc, L, AddrCache);
1422         if (!SymInfoOrErr) {
1423           if (HandleError(SymInfoOrErr.takeError()) == ErrorPolicy::Halt)
1424             return;
1425           continue;
1426         }
1427 
1428         object::RelocVisitor V(Obj);
1429         uint64_t Val = V.visit(Reloc.getType(), Reloc, SymInfoOrErr->Address);
1430         if (V.error()) {
1431           SmallString<32> Type;
1432           Reloc.getTypeName(Type);
1433           ErrorPolicy EP = HandleError(
1434               createError("failed to compute relocation: " + Type + ", ",
1435                           errorCodeToError(object_error::parse_failed)));
1436           if (EP == ErrorPolicy::Halt)
1437             return;
1438           continue;
1439         }
1440         RelocAddrEntry Rel = {SymInfoOrErr->SectionIndex, Val};
1441         Map->insert({Reloc.getOffset(), Rel});
1442       }
1443     }
1444 
1445     for (SectionName &S : SectionNames)
1446       if (SectionAmountMap[S.Name] > 1)
1447         S.IsNameUnique = false;
1448   }
1449 
1450   Optional<RelocAddrEntry> find(const DWARFSection &S,
1451                                 uint64_t Pos) const override {
1452     auto &Sec = static_cast<const DWARFSectionMap &>(S);
1453     RelocAddrMap::const_iterator AI = Sec.Relocs.find(Pos);
1454     if (AI == Sec.Relocs.end())
1455       return None;
1456     return AI->second;
1457   }
1458 
1459   const object::ObjectFile *getFile() const override { return Obj; }
1460 
1461   ArrayRef<SectionName> getSectionNames() const override {
1462     return SectionNames;
1463   }
1464 
1465   bool isLittleEndian() const override { return IsLittleEndian; }
1466   StringRef getAbbrevDWOSection() const override { return AbbrevDWOSection; }
1467   const DWARFSection &getLineDWOSection() const override {
1468     return LineDWOSection;
1469   }
1470   const DWARFSection &getLocDWOSection() const override {
1471     return LocDWOSection;
1472   }
1473   StringRef getStringDWOSection() const override { return StringDWOSection; }
1474   const DWARFSection &getStringOffsetDWOSection() const override {
1475     return StringOffsetDWOSection;
1476   }
1477   const DWARFSection &getRangeDWOSection() const override {
1478     return RangeDWOSection;
1479   }
1480   const DWARFSection &getAddrSection() const override { return AddrSection; }
1481   StringRef getCUIndexSection() const override { return CUIndexSection; }
1482   StringRef getGdbIndexSection() const override { return GdbIndexSection; }
1483   StringRef getTUIndexSection() const override { return TUIndexSection; }
1484 
1485   // DWARF v5
1486   const DWARFSection &getStringOffsetSection() const override {
1487     return StringOffsetSection;
1488   }
1489   StringRef getLineStringSection() const override { return LineStringSection; }
1490 
1491   // Sections for DWARF5 split dwarf proposal.
1492   const DWARFSection &getInfoDWOSection() const override {
1493     return InfoDWOSection;
1494   }
1495   void forEachTypesDWOSections(
1496       function_ref<void(const DWARFSection &)> F) const override {
1497     for (auto &P : TypesDWOSections)
1498       F(P.second);
1499   }
1500 
1501   StringRef getAbbrevSection() const override { return AbbrevSection; }
1502   const DWARFSection &getLocSection() const override { return LocSection; }
1503   StringRef getARangeSection() const override { return ARangeSection; }
1504   StringRef getDebugFrameSection() const override { return DebugFrameSection; }
1505   StringRef getEHFrameSection() const override { return EHFrameSection; }
1506   const DWARFSection &getLineSection() const override { return LineSection; }
1507   StringRef getStringSection() const override { return StringSection; }
1508   const DWARFSection &getRangeSection() const override { return RangeSection; }
1509   const DWARFSection &getRnglistsSection() const override {
1510     return RnglistsSection;
1511   }
1512   StringRef getMacinfoSection() const override { return MacinfoSection; }
1513   StringRef getPubNamesSection() const override { return PubNamesSection; }
1514   StringRef getPubTypesSection() const override { return PubTypesSection; }
1515   StringRef getGnuPubNamesSection() const override {
1516     return GnuPubNamesSection;
1517   }
1518   StringRef getGnuPubTypesSection() const override {
1519     return GnuPubTypesSection;
1520   }
1521   const DWARFSection &getAppleNamesSection() const override {
1522     return AppleNamesSection;
1523   }
1524   const DWARFSection &getAppleTypesSection() const override {
1525     return AppleTypesSection;
1526   }
1527   const DWARFSection &getAppleNamespacesSection() const override {
1528     return AppleNamespacesSection;
1529   }
1530   const DWARFSection &getAppleObjCSection() const override {
1531     return AppleObjCSection;
1532   }
1533   const DWARFSection &getDebugNamesSection() const override {
1534     return DebugNamesSection;
1535   }
1536 
1537   StringRef getFileName() const override { return FileName; }
1538   uint8_t getAddressSize() const override { return AddressSize; }
1539   const DWARFSection &getInfoSection() const override { return InfoSection; }
1540   void forEachTypesSections(
1541       function_ref<void(const DWARFSection &)> F) const override {
1542     for (auto &P : TypesSections)
1543       F(P.second);
1544   }
1545 };
1546 } // namespace
1547 
1548 std::unique_ptr<DWARFContext>
1549 DWARFContext::create(const object::ObjectFile &Obj, const LoadedObjectInfo *L,
1550                      function_ref<ErrorPolicy(Error)> HandleError,
1551                      std::string DWPName) {
1552   auto DObj = llvm::make_unique<DWARFObjInMemory>(Obj, L, HandleError);
1553   return llvm::make_unique<DWARFContext>(std::move(DObj), std::move(DWPName));
1554 }
1555 
1556 std::unique_ptr<DWARFContext>
1557 DWARFContext::create(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections,
1558                      uint8_t AddrSize, bool isLittleEndian) {
1559   auto DObj =
1560       llvm::make_unique<DWARFObjInMemory>(Sections, AddrSize, isLittleEndian);
1561   return llvm::make_unique<DWARFContext>(std::move(DObj), "");
1562 }
1563 
1564 Error DWARFContext::loadRegisterInfo(const object::ObjectFile &Obj) {
1565   // Detect the architecture from the object file. We usually don't need OS
1566   // info to lookup a target and create register info.
1567   Triple TT;
1568   TT.setArch(Triple::ArchType(Obj.getArch()));
1569   TT.setVendor(Triple::UnknownVendor);
1570   TT.setOS(Triple::UnknownOS);
1571   std::string TargetLookupError;
1572   const Target *TheTarget =
1573       TargetRegistry::lookupTarget(TT.str(), TargetLookupError);
1574   if (!TargetLookupError.empty())
1575     return make_error<StringError>(TargetLookupError, inconvertibleErrorCode());
1576   RegInfo.reset(TheTarget->createMCRegInfo(TT.str()));
1577   return Error::success();
1578 }
1579