1 //===- DWARFContext.cpp ---------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
10 #include "llvm/ADT/STLExtras.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/ADT/StringSwitch.h"
15 #include "llvm/BinaryFormat/Dwarf.h"
16 #include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
17 #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
18 #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
19 #include "llvm/DebugInfo/DWARF/DWARFDebugAddr.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/MC/MCTargetOptions.h"
37 #include "llvm/Object/Decompressor.h"
38 #include "llvm/Object/MachO.h"
39 #include "llvm/Object/ObjectFile.h"
40 #include "llvm/Object/RelocationResolver.h"
41 #include "llvm/Support/Casting.h"
42 #include "llvm/Support/DataExtractor.h"
43 #include "llvm/Support/Error.h"
44 #include "llvm/Support/Format.h"
45 #include "llvm/Support/LEB128.h"
46 #include "llvm/Support/MemoryBuffer.h"
47 #include "llvm/Support/Path.h"
48 #include "llvm/Support/TargetRegistry.h"
49 #include "llvm/Support/raw_ostream.h"
50 #include <algorithm>
51 #include <cstdint>
52 #include <deque>
53 #include <map>
54 #include <string>
55 #include <utility>
56 #include <vector>
57 
58 using namespace llvm;
59 using namespace dwarf;
60 using namespace object;
61 
62 #define DEBUG_TYPE "dwarf"
63 
64 using DWARFLineTable = DWARFDebugLine::LineTable;
65 using FileLineInfoKind = DILineInfoSpecifier::FileLineInfoKind;
66 using FunctionNameKind = DILineInfoSpecifier::FunctionNameKind;
67 
DWARFContext(std::unique_ptr<const DWARFObject> DObj,std::string DWPName,std::function<void (Error)> RecoverableErrorHandler,std::function<void (Error)> WarningHandler)68 DWARFContext::DWARFContext(std::unique_ptr<const DWARFObject> DObj,
69                            std::string DWPName,
70                            std::function<void(Error)> RecoverableErrorHandler,
71                            std::function<void(Error)> WarningHandler)
72     : DIContext(CK_DWARF), DWPName(std::move(DWPName)),
73       RecoverableErrorHandler(RecoverableErrorHandler),
74       WarningHandler(WarningHandler), DObj(std::move(DObj)) {}
75 
76 DWARFContext::~DWARFContext() = default;
77 
78 /// Dump the UUID load command.
dumpUUID(raw_ostream & OS,const ObjectFile & Obj)79 static void dumpUUID(raw_ostream &OS, const ObjectFile &Obj) {
80   auto *MachO = dyn_cast<MachOObjectFile>(&Obj);
81   if (!MachO)
82     return;
83   for (auto LC : MachO->load_commands()) {
84     raw_ostream::uuid_t UUID;
85     if (LC.C.cmd == MachO::LC_UUID) {
86       if (LC.C.cmdsize < sizeof(UUID) + sizeof(LC.C)) {
87         OS << "error: UUID load command is too short.\n";
88         return;
89       }
90       OS << "UUID: ";
91       memcpy(&UUID, LC.Ptr+sizeof(LC.C), sizeof(UUID));
92       OS.write_uuid(UUID);
93       Triple T = MachO->getArchTriple();
94       OS << " (" << T.getArchName() << ')';
95       OS << ' ' << MachO->getFileName() << '\n';
96     }
97   }
98 }
99 
100 using ContributionCollection =
101     std::vector<Optional<StrOffsetsContributionDescriptor>>;
102 
103 // Collect all the contributions to the string offsets table from all units,
104 // sort them by their starting offsets and remove duplicates.
105 static ContributionCollection
collectContributionData(DWARFContext::unit_iterator_range Units)106 collectContributionData(DWARFContext::unit_iterator_range Units) {
107   ContributionCollection Contributions;
108   for (const auto &U : Units)
109     if (const auto &C = U->getStringOffsetsTableContribution())
110       Contributions.push_back(C);
111   // Sort the contributions so that any invalid ones are placed at
112   // the start of the contributions vector. This way they are reported
113   // first.
114   llvm::sort(Contributions,
115              [](const Optional<StrOffsetsContributionDescriptor> &L,
116                 const Optional<StrOffsetsContributionDescriptor> &R) {
117                if (L && R)
118                  return L->Base < R->Base;
119                return R.hasValue();
120              });
121 
122   // Uniquify contributions, as it is possible that units (specifically
123   // type units in dwo or dwp files) share contributions. We don't want
124   // to report them more than once.
125   Contributions.erase(
126       std::unique(Contributions.begin(), Contributions.end(),
127                   [](const Optional<StrOffsetsContributionDescriptor> &L,
128                      const Optional<StrOffsetsContributionDescriptor> &R) {
129                     if (L && R)
130                       return L->Base == R->Base && L->Size == R->Size;
131                     return false;
132                   }),
133       Contributions.end());
134   return Contributions;
135 }
136 
137 // Dump a DWARF string offsets section. This may be a DWARF v5 formatted
138 // string offsets section, where each compile or type unit contributes a
139 // number of entries (string offsets), with each contribution preceded by
140 // a header containing size and version number. Alternatively, it may be a
141 // monolithic series of string offsets, as generated by the pre-DWARF v5
142 // implementation of split DWARF; however, in that case we still need to
143 // collect contributions of units because the size of the offsets (4 or 8
144 // bytes) depends on the format of the referencing unit (DWARF32 or DWARF64).
dumpStringOffsetsSection(raw_ostream & OS,DIDumpOptions DumpOpts,StringRef SectionName,const DWARFObject & Obj,const DWARFSection & StringOffsetsSection,StringRef StringSection,DWARFContext::unit_iterator_range Units,bool LittleEndian)145 static void dumpStringOffsetsSection(raw_ostream &OS, DIDumpOptions DumpOpts,
146                                      StringRef SectionName,
147                                      const DWARFObject &Obj,
148                                      const DWARFSection &StringOffsetsSection,
149                                      StringRef StringSection,
150                                      DWARFContext::unit_iterator_range Units,
151                                      bool LittleEndian) {
152   auto Contributions = collectContributionData(Units);
153   DWARFDataExtractor StrOffsetExt(Obj, StringOffsetsSection, LittleEndian, 0);
154   DataExtractor StrData(StringSection, LittleEndian, 0);
155   uint64_t SectionSize = StringOffsetsSection.Data.size();
156   uint64_t Offset = 0;
157   for (auto &Contribution : Contributions) {
158     // Report an ill-formed contribution.
159     if (!Contribution) {
160       OS << "error: invalid contribution to string offsets table in section ."
161          << SectionName << ".\n";
162       return;
163     }
164 
165     dwarf::DwarfFormat Format = Contribution->getFormat();
166     int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(Format);
167     uint16_t Version = Contribution->getVersion();
168     uint64_t ContributionHeader = Contribution->Base;
169     // In DWARF v5 there is a contribution header that immediately precedes
170     // the string offsets base (the location we have previously retrieved from
171     // the CU DIE's DW_AT_str_offsets attribute). The header is located either
172     // 8 or 16 bytes before the base, depending on the contribution's format.
173     if (Version >= 5)
174       ContributionHeader -= Format == DWARF32 ? 8 : 16;
175 
176     // Detect overlapping contributions.
177     if (Offset > ContributionHeader) {
178       DumpOpts.RecoverableErrorHandler(createStringError(
179           errc::invalid_argument,
180           "overlapping contributions to string offsets table in section .%s.",
181           SectionName.data()));
182     }
183     // Report a gap in the table.
184     if (Offset < ContributionHeader) {
185       OS << format("0x%8.8" PRIx64 ": Gap, length = ", Offset);
186       OS << (ContributionHeader - Offset) << "\n";
187     }
188     OS << format("0x%8.8" PRIx64 ": ", ContributionHeader);
189     // In DWARF v5 the contribution size in the descriptor does not equal
190     // the originally encoded length (it does not contain the length of the
191     // version field and the padding, a total of 4 bytes). Add them back in
192     // for reporting.
193     OS << "Contribution size = " << (Contribution->Size + (Version < 5 ? 0 : 4))
194        << ", Format = " << dwarf::FormatString(Format)
195        << ", Version = " << Version << "\n";
196 
197     Offset = Contribution->Base;
198     unsigned EntrySize = Contribution->getDwarfOffsetByteSize();
199     while (Offset - Contribution->Base < Contribution->Size) {
200       OS << format("0x%8.8" PRIx64 ": ", Offset);
201       uint64_t StringOffset =
202           StrOffsetExt.getRelocatedValue(EntrySize, &Offset);
203       OS << format("%0*" PRIx64 " ", OffsetDumpWidth, StringOffset);
204       const char *S = StrData.getCStr(&StringOffset);
205       if (S)
206         OS << format("\"%s\"", S);
207       OS << "\n";
208     }
209   }
210   // Report a gap at the end of the table.
211   if (Offset < SectionSize) {
212     OS << format("0x%8.8" PRIx64 ": Gap, length = ", Offset);
213     OS << (SectionSize - Offset) << "\n";
214   }
215 }
216 
217 // Dump the .debug_addr section.
dumpAddrSection(raw_ostream & OS,DWARFDataExtractor & AddrData,DIDumpOptions DumpOpts,uint16_t Version,uint8_t AddrSize)218 static void dumpAddrSection(raw_ostream &OS, DWARFDataExtractor &AddrData,
219                             DIDumpOptions DumpOpts, uint16_t Version,
220                             uint8_t AddrSize) {
221   uint64_t Offset = 0;
222   while (AddrData.isValidOffset(Offset)) {
223     DWARFDebugAddrTable AddrTable;
224     uint64_t TableOffset = Offset;
225     if (Error Err = AddrTable.extract(AddrData, &Offset, Version, AddrSize,
226                                       DumpOpts.WarningHandler)) {
227       DumpOpts.RecoverableErrorHandler(std::move(Err));
228       // Keep going after an error, if we can, assuming that the length field
229       // could be read. If it couldn't, stop reading the section.
230       if (auto TableLength = AddrTable.getFullLength()) {
231         Offset = TableOffset + *TableLength;
232         continue;
233       }
234       break;
235     }
236     AddrTable.dump(OS, DumpOpts);
237   }
238 }
239 
240 // Dump the .debug_rnglists or .debug_rnglists.dwo section (DWARF v5).
dumpRnglistsSection(raw_ostream & OS,DWARFDataExtractor & rnglistData,llvm::function_ref<Optional<object::SectionedAddress> (uint32_t)> LookupPooledAddress,DIDumpOptions DumpOpts)241 static void dumpRnglistsSection(
242     raw_ostream &OS, DWARFDataExtractor &rnglistData,
243     llvm::function_ref<Optional<object::SectionedAddress>(uint32_t)>
244         LookupPooledAddress,
245     DIDumpOptions DumpOpts) {
246   uint64_t Offset = 0;
247   while (rnglistData.isValidOffset(Offset)) {
248     llvm::DWARFDebugRnglistTable Rnglists;
249     uint64_t TableOffset = Offset;
250     if (Error Err = Rnglists.extract(rnglistData, &Offset)) {
251       DumpOpts.RecoverableErrorHandler(std::move(Err));
252       uint64_t Length = Rnglists.length();
253       // Keep going after an error, if we can, assuming that the length field
254       // could be read. If it couldn't, stop reading the section.
255       if (Length == 0)
256         break;
257       Offset = TableOffset + Length;
258     } else {
259       Rnglists.dump(OS, LookupPooledAddress, DumpOpts);
260     }
261   }
262 }
263 
264 std::unique_ptr<DWARFDebugMacro>
parseMacroOrMacinfo(MacroSecType SectionType)265 DWARFContext::parseMacroOrMacinfo(MacroSecType SectionType) {
266   auto Macro = std::make_unique<DWARFDebugMacro>();
267   auto ParseAndDump = [&](DWARFDataExtractor &Data, bool IsMacro) {
268     if (Error Err = IsMacro ? Macro->parseMacro(SectionType == MacroSection
269                                                     ? compile_units()
270                                                     : dwo_compile_units(),
271                                                 SectionType == MacroSection
272                                                     ? getStringExtractor()
273                                                     : getStringDWOExtractor(),
274                                                 Data)
275                             : Macro->parseMacinfo(Data)) {
276       RecoverableErrorHandler(std::move(Err));
277       Macro = nullptr;
278     }
279   };
280   switch (SectionType) {
281   case MacinfoSection: {
282     DWARFDataExtractor Data(DObj->getMacinfoSection(), isLittleEndian(), 0);
283     ParseAndDump(Data, /*IsMacro=*/false);
284     break;
285   }
286   case MacinfoDwoSection: {
287     DWARFDataExtractor Data(DObj->getMacinfoDWOSection(), isLittleEndian(), 0);
288     ParseAndDump(Data, /*IsMacro=*/false);
289     break;
290   }
291   case MacroSection: {
292     DWARFDataExtractor Data(*DObj, DObj->getMacroSection(), isLittleEndian(),
293                             0);
294     ParseAndDump(Data, /*IsMacro=*/true);
295     break;
296   }
297   case MacroDwoSection: {
298     DWARFDataExtractor Data(DObj->getMacroDWOSection(), isLittleEndian(), 0);
299     ParseAndDump(Data, /*IsMacro=*/true);
300     break;
301   }
302   }
303   return Macro;
304 }
305 
dumpLoclistsSection(raw_ostream & OS,DIDumpOptions DumpOpts,DWARFDataExtractor Data,const MCRegisterInfo * MRI,const DWARFObject & Obj,Optional<uint64_t> DumpOffset)306 static void dumpLoclistsSection(raw_ostream &OS, DIDumpOptions DumpOpts,
307                                 DWARFDataExtractor Data,
308                                 const MCRegisterInfo *MRI,
309                                 const DWARFObject &Obj,
310                                 Optional<uint64_t> DumpOffset) {
311   uint64_t Offset = 0;
312 
313   while (Data.isValidOffset(Offset)) {
314     DWARFListTableHeader Header(".debug_loclists", "locations");
315     if (Error E = Header.extract(Data, &Offset)) {
316       DumpOpts.RecoverableErrorHandler(std::move(E));
317       return;
318     }
319 
320     Header.dump(OS, DumpOpts);
321 
322     uint64_t EndOffset = Header.length() + Header.getHeaderOffset();
323     Data.setAddressSize(Header.getAddrSize());
324     DWARFDebugLoclists Loc(Data, Header.getVersion());
325     if (DumpOffset) {
326       if (DumpOffset >= Offset && DumpOffset < EndOffset) {
327         Offset = *DumpOffset;
328         Loc.dumpLocationList(&Offset, OS, /*BaseAddr=*/None, MRI, Obj, nullptr,
329                              DumpOpts, /*Indent=*/0);
330         OS << "\n";
331         return;
332       }
333     } else {
334       Loc.dumpRange(Offset, EndOffset - Offset, OS, MRI, Obj, DumpOpts);
335     }
336     Offset = EndOffset;
337   }
338 }
339 
dumpPubTableSection(raw_ostream & OS,DIDumpOptions DumpOpts,DWARFDataExtractor Data,bool GnuStyle)340 static void dumpPubTableSection(raw_ostream &OS, DIDumpOptions DumpOpts,
341                                 DWARFDataExtractor Data, bool GnuStyle) {
342   DWARFDebugPubTable Table;
343   Table.extract(Data, GnuStyle, DumpOpts.RecoverableErrorHandler);
344   Table.dump(OS);
345 }
346 
dump(raw_ostream & OS,DIDumpOptions DumpOpts,std::array<Optional<uint64_t>,DIDT_ID_Count> DumpOffsets)347 void DWARFContext::dump(
348     raw_ostream &OS, DIDumpOptions DumpOpts,
349     std::array<Optional<uint64_t>, DIDT_ID_Count> DumpOffsets) {
350   uint64_t DumpType = DumpOpts.DumpType;
351 
352   StringRef Extension = sys::path::extension(DObj->getFileName());
353   bool IsDWO = (Extension == ".dwo") || (Extension == ".dwp");
354 
355   // Print UUID header.
356   const auto *ObjFile = DObj->getFile();
357   if (DumpType & DIDT_UUID)
358     dumpUUID(OS, *ObjFile);
359 
360   // Print a header for each explicitly-requested section.
361   // Otherwise just print one for non-empty sections.
362   // Only print empty .dwo section headers when dumping a .dwo file.
363   bool Explicit = DumpType != DIDT_All && !IsDWO;
364   bool ExplicitDWO = Explicit && IsDWO;
365   auto shouldDump = [&](bool Explicit, const char *Name, unsigned ID,
366                         StringRef Section) -> Optional<uint64_t> * {
367     unsigned Mask = 1U << ID;
368     bool Should = (DumpType & Mask) && (Explicit || !Section.empty());
369     if (!Should)
370       return nullptr;
371     OS << "\n" << Name << " contents:\n";
372     return &DumpOffsets[ID];
373   };
374 
375   // Dump individual sections.
376   if (shouldDump(Explicit, ".debug_abbrev", DIDT_ID_DebugAbbrev,
377                  DObj->getAbbrevSection()))
378     getDebugAbbrev()->dump(OS);
379   if (shouldDump(ExplicitDWO, ".debug_abbrev.dwo", DIDT_ID_DebugAbbrev,
380                  DObj->getAbbrevDWOSection()))
381     getDebugAbbrevDWO()->dump(OS);
382 
383   auto dumpDebugInfo = [&](const char *Name, unit_iterator_range Units) {
384     OS << '\n' << Name << " contents:\n";
385     if (auto DumpOffset = DumpOffsets[DIDT_ID_DebugInfo])
386       for (const auto &U : Units)
387         U->getDIEForOffset(DumpOffset.getValue())
388             .dump(OS, 0, DumpOpts.noImplicitRecursion());
389     else
390       for (const auto &U : Units)
391         U->dump(OS, DumpOpts);
392   };
393   if ((DumpType & DIDT_DebugInfo)) {
394     if (Explicit || getNumCompileUnits())
395       dumpDebugInfo(".debug_info", info_section_units());
396     if (ExplicitDWO || getNumDWOCompileUnits())
397       dumpDebugInfo(".debug_info.dwo", dwo_info_section_units());
398   }
399 
400   auto dumpDebugType = [&](const char *Name, unit_iterator_range Units) {
401     OS << '\n' << Name << " contents:\n";
402     for (const auto &U : Units)
403       if (auto DumpOffset = DumpOffsets[DIDT_ID_DebugTypes])
404         U->getDIEForOffset(*DumpOffset)
405             .dump(OS, 0, DumpOpts.noImplicitRecursion());
406       else
407         U->dump(OS, DumpOpts);
408   };
409   if ((DumpType & DIDT_DebugTypes)) {
410     if (Explicit || getNumTypeUnits())
411       dumpDebugType(".debug_types", types_section_units());
412     if (ExplicitDWO || getNumDWOTypeUnits())
413       dumpDebugType(".debug_types.dwo", dwo_types_section_units());
414   }
415 
416   DIDumpOptions LLDumpOpts = DumpOpts;
417   if (LLDumpOpts.Verbose)
418     LLDumpOpts.DisplayRawContents = true;
419 
420   if (const auto *Off = shouldDump(Explicit, ".debug_loc", DIDT_ID_DebugLoc,
421                                    DObj->getLocSection().Data)) {
422     getDebugLoc()->dump(OS, getRegisterInfo(), *DObj, LLDumpOpts, *Off);
423   }
424   if (const auto *Off =
425           shouldDump(Explicit, ".debug_loclists", DIDT_ID_DebugLoclists,
426                      DObj->getLoclistsSection().Data)) {
427     DWARFDataExtractor Data(*DObj, DObj->getLoclistsSection(), isLittleEndian(),
428                             0);
429     dumpLoclistsSection(OS, LLDumpOpts, Data, getRegisterInfo(), *DObj, *Off);
430   }
431   if (const auto *Off =
432           shouldDump(ExplicitDWO, ".debug_loclists.dwo", DIDT_ID_DebugLoclists,
433                      DObj->getLoclistsDWOSection().Data)) {
434     DWARFDataExtractor Data(*DObj, DObj->getLoclistsDWOSection(),
435                             isLittleEndian(), 0);
436     dumpLoclistsSection(OS, LLDumpOpts, Data, getRegisterInfo(), *DObj, *Off);
437   }
438 
439   if (const auto *Off =
440           shouldDump(ExplicitDWO, ".debug_loc.dwo", DIDT_ID_DebugLoc,
441                      DObj->getLocDWOSection().Data)) {
442     DWARFDataExtractor Data(*DObj, DObj->getLocDWOSection(), isLittleEndian(),
443                             4);
444     DWARFDebugLoclists Loc(Data, /*Version=*/4);
445     if (*Off) {
446       uint64_t Offset = **Off;
447       Loc.dumpLocationList(&Offset, OS,
448                            /*BaseAddr=*/None, getRegisterInfo(), *DObj, nullptr,
449                            LLDumpOpts, /*Indent=*/0);
450       OS << "\n";
451     } else {
452       Loc.dumpRange(0, Data.getData().size(), OS, getRegisterInfo(), *DObj,
453                     LLDumpOpts);
454     }
455   }
456 
457   if (const Optional<uint64_t> *Off =
458           shouldDump(Explicit, ".debug_frame", DIDT_ID_DebugFrame,
459                      DObj->getFrameSection().Data)) {
460     if (Expected<const DWARFDebugFrame *> DF = getDebugFrame())
461       (*DF)->dump(OS, getRegisterInfo(), *Off);
462     else
463       RecoverableErrorHandler(DF.takeError());
464   }
465 
466   if (const Optional<uint64_t> *Off =
467           shouldDump(Explicit, ".eh_frame", DIDT_ID_DebugFrame,
468                      DObj->getEHFrameSection().Data)) {
469     if (Expected<const DWARFDebugFrame *> DF = getEHFrame())
470       (*DF)->dump(OS, getRegisterInfo(), *Off);
471     else
472       RecoverableErrorHandler(DF.takeError());
473   }
474 
475   if (shouldDump(Explicit, ".debug_macro", DIDT_ID_DebugMacro,
476                  DObj->getMacroSection().Data)) {
477     if (auto Macro = getDebugMacro())
478       Macro->dump(OS);
479   }
480 
481   if (shouldDump(Explicit, ".debug_macro.dwo", DIDT_ID_DebugMacro,
482                  DObj->getMacroDWOSection())) {
483     if (auto MacroDWO = getDebugMacroDWO())
484       MacroDWO->dump(OS);
485   }
486 
487   if (shouldDump(Explicit, ".debug_macinfo", DIDT_ID_DebugMacro,
488                  DObj->getMacinfoSection())) {
489     if (auto Macinfo = getDebugMacinfo())
490       Macinfo->dump(OS);
491   }
492 
493   if (shouldDump(Explicit, ".debug_macinfo.dwo", DIDT_ID_DebugMacro,
494                  DObj->getMacinfoDWOSection())) {
495     if (auto MacinfoDWO = getDebugMacinfoDWO())
496       MacinfoDWO->dump(OS);
497   }
498 
499   if (shouldDump(Explicit, ".debug_aranges", DIDT_ID_DebugAranges,
500                  DObj->getArangesSection())) {
501     uint64_t offset = 0;
502     DWARFDataExtractor arangesData(DObj->getArangesSection(), isLittleEndian(),
503                                    0);
504     DWARFDebugArangeSet set;
505     while (arangesData.isValidOffset(offset)) {
506       if (Error E = set.extract(arangesData, &offset)) {
507         RecoverableErrorHandler(std::move(E));
508         break;
509       }
510       set.dump(OS);
511     }
512   }
513 
514   auto DumpLineSection = [&](DWARFDebugLine::SectionParser Parser,
515                              DIDumpOptions DumpOpts,
516                              Optional<uint64_t> DumpOffset) {
517     while (!Parser.done()) {
518       if (DumpOffset && Parser.getOffset() != *DumpOffset) {
519         Parser.skip(DumpOpts.WarningHandler, DumpOpts.WarningHandler);
520         continue;
521       }
522       OS << "debug_line[" << format("0x%8.8" PRIx64, Parser.getOffset())
523          << "]\n";
524       Parser.parseNext(DumpOpts.WarningHandler, DumpOpts.WarningHandler, &OS,
525                        DumpOpts.Verbose);
526     }
527   };
528 
529   if (const auto *Off = shouldDump(Explicit, ".debug_line", DIDT_ID_DebugLine,
530                                    DObj->getLineSection().Data)) {
531     DWARFDataExtractor LineData(*DObj, DObj->getLineSection(), isLittleEndian(),
532                                 0);
533     DWARFDebugLine::SectionParser Parser(LineData, *this, compile_units(),
534                                          type_units());
535     DumpLineSection(Parser, DumpOpts, *Off);
536   }
537 
538   if (const auto *Off =
539           shouldDump(ExplicitDWO, ".debug_line.dwo", DIDT_ID_DebugLine,
540                      DObj->getLineDWOSection().Data)) {
541     DWARFDataExtractor LineData(*DObj, DObj->getLineDWOSection(),
542                                 isLittleEndian(), 0);
543     DWARFDebugLine::SectionParser Parser(LineData, *this, dwo_compile_units(),
544                                          dwo_type_units());
545     DumpLineSection(Parser, DumpOpts, *Off);
546   }
547 
548   if (shouldDump(Explicit, ".debug_cu_index", DIDT_ID_DebugCUIndex,
549                  DObj->getCUIndexSection())) {
550     getCUIndex().dump(OS);
551   }
552 
553   if (shouldDump(Explicit, ".debug_tu_index", DIDT_ID_DebugTUIndex,
554                  DObj->getTUIndexSection())) {
555     getTUIndex().dump(OS);
556   }
557 
558   if (shouldDump(Explicit, ".debug_str", DIDT_ID_DebugStr,
559                  DObj->getStrSection())) {
560     DataExtractor strData(DObj->getStrSection(), isLittleEndian(), 0);
561     uint64_t offset = 0;
562     uint64_t strOffset = 0;
563     while (const char *s = strData.getCStr(&offset)) {
564       OS << format("0x%8.8" PRIx64 ": \"%s\"\n", strOffset, s);
565       strOffset = offset;
566     }
567   }
568   if (shouldDump(ExplicitDWO, ".debug_str.dwo", DIDT_ID_DebugStr,
569                  DObj->getStrDWOSection())) {
570     DataExtractor strDWOData(DObj->getStrDWOSection(), isLittleEndian(), 0);
571     uint64_t offset = 0;
572     uint64_t strDWOOffset = 0;
573     while (const char *s = strDWOData.getCStr(&offset)) {
574       OS << format("0x%8.8" PRIx64 ": \"%s\"\n", strDWOOffset, s);
575       strDWOOffset = offset;
576     }
577   }
578   if (shouldDump(Explicit, ".debug_line_str", DIDT_ID_DebugLineStr,
579                  DObj->getLineStrSection())) {
580     DataExtractor strData(DObj->getLineStrSection(), isLittleEndian(), 0);
581     uint64_t offset = 0;
582     uint64_t strOffset = 0;
583     while (const char *s = strData.getCStr(&offset)) {
584       OS << format("0x%8.8" PRIx64 ": \"", strOffset);
585       OS.write_escaped(s);
586       OS << "\"\n";
587       strOffset = offset;
588     }
589   }
590 
591   if (shouldDump(Explicit, ".debug_addr", DIDT_ID_DebugAddr,
592                  DObj->getAddrSection().Data)) {
593     DWARFDataExtractor AddrData(*DObj, DObj->getAddrSection(),
594                                    isLittleEndian(), 0);
595     dumpAddrSection(OS, AddrData, DumpOpts, getMaxVersion(), getCUAddrSize());
596   }
597 
598   if (shouldDump(Explicit, ".debug_ranges", DIDT_ID_DebugRanges,
599                  DObj->getRangesSection().Data)) {
600     uint8_t savedAddressByteSize = getCUAddrSize();
601     DWARFDataExtractor rangesData(*DObj, DObj->getRangesSection(),
602                                   isLittleEndian(), savedAddressByteSize);
603     uint64_t offset = 0;
604     DWARFDebugRangeList rangeList;
605     while (rangesData.isValidOffset(offset)) {
606       if (Error E = rangeList.extract(rangesData, &offset)) {
607         DumpOpts.RecoverableErrorHandler(std::move(E));
608         break;
609       }
610       rangeList.dump(OS);
611     }
612   }
613 
614   auto LookupPooledAddress = [&](uint32_t Index) -> Optional<SectionedAddress> {
615     const auto &CUs = compile_units();
616     auto I = CUs.begin();
617     if (I == CUs.end())
618       return None;
619     return (*I)->getAddrOffsetSectionItem(Index);
620   };
621 
622   if (shouldDump(Explicit, ".debug_rnglists", DIDT_ID_DebugRnglists,
623                  DObj->getRnglistsSection().Data)) {
624     DWARFDataExtractor RnglistData(*DObj, DObj->getRnglistsSection(),
625                                    isLittleEndian(), 0);
626     dumpRnglistsSection(OS, RnglistData, LookupPooledAddress, DumpOpts);
627   }
628 
629   if (shouldDump(ExplicitDWO, ".debug_rnglists.dwo", DIDT_ID_DebugRnglists,
630                  DObj->getRnglistsDWOSection().Data)) {
631     DWARFDataExtractor RnglistData(*DObj, DObj->getRnglistsDWOSection(),
632                                    isLittleEndian(), 0);
633     dumpRnglistsSection(OS, RnglistData, LookupPooledAddress, DumpOpts);
634   }
635 
636   if (shouldDump(Explicit, ".debug_pubnames", DIDT_ID_DebugPubnames,
637                  DObj->getPubnamesSection().Data)) {
638     DWARFDataExtractor PubTableData(*DObj, DObj->getPubnamesSection(),
639                                     isLittleEndian(), 0);
640     dumpPubTableSection(OS, DumpOpts, PubTableData, /*GnuStyle=*/false);
641   }
642 
643   if (shouldDump(Explicit, ".debug_pubtypes", DIDT_ID_DebugPubtypes,
644                  DObj->getPubtypesSection().Data)) {
645     DWARFDataExtractor PubTableData(*DObj, DObj->getPubtypesSection(),
646                                     isLittleEndian(), 0);
647     dumpPubTableSection(OS, DumpOpts, PubTableData, /*GnuStyle=*/false);
648   }
649 
650   if (shouldDump(Explicit, ".debug_gnu_pubnames", DIDT_ID_DebugGnuPubnames,
651                  DObj->getGnuPubnamesSection().Data)) {
652     DWARFDataExtractor PubTableData(*DObj, DObj->getGnuPubnamesSection(),
653                                     isLittleEndian(), 0);
654     dumpPubTableSection(OS, DumpOpts, PubTableData, /*GnuStyle=*/true);
655   }
656 
657   if (shouldDump(Explicit, ".debug_gnu_pubtypes", DIDT_ID_DebugGnuPubtypes,
658                  DObj->getGnuPubtypesSection().Data)) {
659     DWARFDataExtractor PubTableData(*DObj, DObj->getGnuPubtypesSection(),
660                                     isLittleEndian(), 0);
661     dumpPubTableSection(OS, DumpOpts, PubTableData, /*GnuStyle=*/true);
662   }
663 
664   if (shouldDump(Explicit, ".debug_str_offsets", DIDT_ID_DebugStrOffsets,
665                  DObj->getStrOffsetsSection().Data))
666     dumpStringOffsetsSection(
667         OS, DumpOpts, "debug_str_offsets", *DObj, DObj->getStrOffsetsSection(),
668         DObj->getStrSection(), normal_units(), isLittleEndian());
669   if (shouldDump(ExplicitDWO, ".debug_str_offsets.dwo", DIDT_ID_DebugStrOffsets,
670                  DObj->getStrOffsetsDWOSection().Data))
671     dumpStringOffsetsSection(OS, DumpOpts, "debug_str_offsets.dwo", *DObj,
672                              DObj->getStrOffsetsDWOSection(),
673                              DObj->getStrDWOSection(), dwo_units(),
674                              isLittleEndian());
675 
676   if (shouldDump(Explicit, ".gdb_index", DIDT_ID_GdbIndex,
677                  DObj->getGdbIndexSection())) {
678     getGdbIndex().dump(OS);
679   }
680 
681   if (shouldDump(Explicit, ".apple_names", DIDT_ID_AppleNames,
682                  DObj->getAppleNamesSection().Data))
683     getAppleNames().dump(OS);
684 
685   if (shouldDump(Explicit, ".apple_types", DIDT_ID_AppleTypes,
686                  DObj->getAppleTypesSection().Data))
687     getAppleTypes().dump(OS);
688 
689   if (shouldDump(Explicit, ".apple_namespaces", DIDT_ID_AppleNamespaces,
690                  DObj->getAppleNamespacesSection().Data))
691     getAppleNamespaces().dump(OS);
692 
693   if (shouldDump(Explicit, ".apple_objc", DIDT_ID_AppleObjC,
694                  DObj->getAppleObjCSection().Data))
695     getAppleObjC().dump(OS);
696   if (shouldDump(Explicit, ".debug_names", DIDT_ID_DebugNames,
697                  DObj->getNamesSection().Data))
698     getDebugNames().dump(OS);
699 }
700 
getDWOCompileUnitForHash(uint64_t Hash)701 DWARFCompileUnit *DWARFContext::getDWOCompileUnitForHash(uint64_t Hash) {
702   parseDWOUnits(LazyParse);
703 
704   if (const auto &CUI = getCUIndex()) {
705     if (const auto *R = CUI.getFromHash(Hash))
706       return dyn_cast_or_null<DWARFCompileUnit>(
707           DWOUnits.getUnitForIndexEntry(*R));
708     return nullptr;
709   }
710 
711   // If there's no index, just search through the CUs in the DWO - there's
712   // probably only one unless this is something like LTO - though an in-process
713   // built/cached lookup table could be used in that case to improve repeated
714   // lookups of different CUs in the DWO.
715   for (const auto &DWOCU : dwo_compile_units()) {
716     // Might not have parsed DWO ID yet.
717     if (!DWOCU->getDWOId()) {
718       if (Optional<uint64_t> DWOId =
719           toUnsigned(DWOCU->getUnitDIE().find(DW_AT_GNU_dwo_id)))
720         DWOCU->setDWOId(*DWOId);
721       else
722         // No DWO ID?
723         continue;
724     }
725     if (DWOCU->getDWOId() == Hash)
726       return dyn_cast<DWARFCompileUnit>(DWOCU.get());
727   }
728   return nullptr;
729 }
730 
getDIEForOffset(uint64_t Offset)731 DWARFDie DWARFContext::getDIEForOffset(uint64_t Offset) {
732   parseNormalUnits();
733   if (auto *CU = NormalUnits.getUnitForOffset(Offset))
734     return CU->getDIEForOffset(Offset);
735   return DWARFDie();
736 }
737 
verify(raw_ostream & OS,DIDumpOptions DumpOpts)738 bool DWARFContext::verify(raw_ostream &OS, DIDumpOptions DumpOpts) {
739   bool Success = true;
740   DWARFVerifier verifier(OS, *this, DumpOpts);
741 
742   Success &= verifier.handleDebugAbbrev();
743   if (DumpOpts.DumpType & DIDT_DebugInfo)
744     Success &= verifier.handleDebugInfo();
745   if (DumpOpts.DumpType & DIDT_DebugLine)
746     Success &= verifier.handleDebugLine();
747   Success &= verifier.handleAccelTables();
748   return Success;
749 }
750 
getCUIndex()751 const DWARFUnitIndex &DWARFContext::getCUIndex() {
752   if (CUIndex)
753     return *CUIndex;
754 
755   DataExtractor CUIndexData(DObj->getCUIndexSection(), isLittleEndian(), 0);
756 
757   CUIndex = std::make_unique<DWARFUnitIndex>(DW_SECT_INFO);
758   CUIndex->parse(CUIndexData);
759   return *CUIndex;
760 }
761 
getTUIndex()762 const DWARFUnitIndex &DWARFContext::getTUIndex() {
763   if (TUIndex)
764     return *TUIndex;
765 
766   DataExtractor TUIndexData(DObj->getTUIndexSection(), isLittleEndian(), 0);
767 
768   TUIndex = std::make_unique<DWARFUnitIndex>(DW_SECT_EXT_TYPES);
769   TUIndex->parse(TUIndexData);
770   return *TUIndex;
771 }
772 
getGdbIndex()773 DWARFGdbIndex &DWARFContext::getGdbIndex() {
774   if (GdbIndex)
775     return *GdbIndex;
776 
777   DataExtractor GdbIndexData(DObj->getGdbIndexSection(), true /*LE*/, 0);
778   GdbIndex = std::make_unique<DWARFGdbIndex>();
779   GdbIndex->parse(GdbIndexData);
780   return *GdbIndex;
781 }
782 
getDebugAbbrev()783 const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
784   if (Abbrev)
785     return Abbrev.get();
786 
787   DataExtractor abbrData(DObj->getAbbrevSection(), isLittleEndian(), 0);
788 
789   Abbrev.reset(new DWARFDebugAbbrev());
790   Abbrev->extract(abbrData);
791   return Abbrev.get();
792 }
793 
getDebugAbbrevDWO()794 const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
795   if (AbbrevDWO)
796     return AbbrevDWO.get();
797 
798   DataExtractor abbrData(DObj->getAbbrevDWOSection(), isLittleEndian(), 0);
799   AbbrevDWO.reset(new DWARFDebugAbbrev());
800   AbbrevDWO->extract(abbrData);
801   return AbbrevDWO.get();
802 }
803 
getDebugLoc()804 const DWARFDebugLoc *DWARFContext::getDebugLoc() {
805   if (Loc)
806     return Loc.get();
807 
808   // Assume all units have the same address byte size.
809   auto LocData =
810       getNumCompileUnits()
811           ? DWARFDataExtractor(*DObj, DObj->getLocSection(), isLittleEndian(),
812                                getUnitAtIndex(0)->getAddressByteSize())
813           : DWARFDataExtractor("", isLittleEndian(), 0);
814   Loc.reset(new DWARFDebugLoc(std::move(LocData)));
815   return Loc.get();
816 }
817 
getDebugAranges()818 const DWARFDebugAranges *DWARFContext::getDebugAranges() {
819   if (Aranges)
820     return Aranges.get();
821 
822   Aranges.reset(new DWARFDebugAranges());
823   Aranges->generate(this);
824   return Aranges.get();
825 }
826 
getDebugFrame()827 Expected<const DWARFDebugFrame *> DWARFContext::getDebugFrame() {
828   if (DebugFrame)
829     return DebugFrame.get();
830 
831   // There's a "bug" in the DWARFv3 standard with respect to the target address
832   // size within debug frame sections. While DWARF is supposed to be independent
833   // of its container, FDEs have fields with size being "target address size",
834   // which isn't specified in DWARF in general. It's only specified for CUs, but
835   // .eh_frame can appear without a .debug_info section. Follow the example of
836   // other tools (libdwarf) and extract this from the container (ObjectFile
837   // provides this information). This problem is fixed in DWARFv4
838   // See this dwarf-discuss discussion for more details:
839   // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
840   DWARFDataExtractor debugFrameData(*DObj, DObj->getFrameSection(),
841                                     isLittleEndian(), DObj->getAddressSize());
842   auto DF = std::make_unique<DWARFDebugFrame>(getArch(), /*IsEH=*/false);
843   if (Error E = DF->parse(debugFrameData))
844     return std::move(E);
845 
846   DebugFrame.swap(DF);
847   return DebugFrame.get();
848 }
849 
getEHFrame()850 Expected<const DWARFDebugFrame *> DWARFContext::getEHFrame() {
851   if (EHFrame)
852     return EHFrame.get();
853 
854   DWARFDataExtractor debugFrameData(*DObj, DObj->getEHFrameSection(),
855                                     isLittleEndian(), DObj->getAddressSize());
856 
857   auto DF = std::make_unique<DWARFDebugFrame>(getArch(), /*IsEH=*/true);
858   if (Error E = DF->parse(debugFrameData))
859     return std::move(E);
860   DebugFrame.swap(DF);
861   return DebugFrame.get();
862 }
863 
getDebugMacro()864 const DWARFDebugMacro *DWARFContext::getDebugMacro() {
865   if (!Macro)
866     Macro = parseMacroOrMacinfo(MacroSection);
867   return Macro.get();
868 }
869 
getDebugMacroDWO()870 const DWARFDebugMacro *DWARFContext::getDebugMacroDWO() {
871   if (!MacroDWO)
872     MacroDWO = parseMacroOrMacinfo(MacroDwoSection);
873   return MacroDWO.get();
874 }
875 
getDebugMacinfo()876 const DWARFDebugMacro *DWARFContext::getDebugMacinfo() {
877   if (!Macinfo)
878     Macinfo = parseMacroOrMacinfo(MacinfoSection);
879   return Macinfo.get();
880 }
881 
getDebugMacinfoDWO()882 const DWARFDebugMacro *DWARFContext::getDebugMacinfoDWO() {
883   if (!MacinfoDWO)
884     MacinfoDWO = parseMacroOrMacinfo(MacinfoDwoSection);
885   return MacinfoDWO.get();
886 }
887 
888 template <typename T>
getAccelTable(std::unique_ptr<T> & Cache,const DWARFObject & Obj,const DWARFSection & Section,StringRef StringSection,bool IsLittleEndian)889 static T &getAccelTable(std::unique_ptr<T> &Cache, const DWARFObject &Obj,
890                         const DWARFSection &Section, StringRef StringSection,
891                         bool IsLittleEndian) {
892   if (Cache)
893     return *Cache;
894   DWARFDataExtractor AccelSection(Obj, Section, IsLittleEndian, 0);
895   DataExtractor StrData(StringSection, IsLittleEndian, 0);
896   Cache.reset(new T(AccelSection, StrData));
897   if (Error E = Cache->extract())
898     llvm::consumeError(std::move(E));
899   return *Cache;
900 }
901 
getDebugNames()902 const DWARFDebugNames &DWARFContext::getDebugNames() {
903   return getAccelTable(Names, *DObj, DObj->getNamesSection(),
904                        DObj->getStrSection(), isLittleEndian());
905 }
906 
getAppleNames()907 const AppleAcceleratorTable &DWARFContext::getAppleNames() {
908   return getAccelTable(AppleNames, *DObj, DObj->getAppleNamesSection(),
909                        DObj->getStrSection(), isLittleEndian());
910 }
911 
getAppleTypes()912 const AppleAcceleratorTable &DWARFContext::getAppleTypes() {
913   return getAccelTable(AppleTypes, *DObj, DObj->getAppleTypesSection(),
914                        DObj->getStrSection(), isLittleEndian());
915 }
916 
getAppleNamespaces()917 const AppleAcceleratorTable &DWARFContext::getAppleNamespaces() {
918   return getAccelTable(AppleNamespaces, *DObj,
919                        DObj->getAppleNamespacesSection(),
920                        DObj->getStrSection(), isLittleEndian());
921 }
922 
getAppleObjC()923 const AppleAcceleratorTable &DWARFContext::getAppleObjC() {
924   return getAccelTable(AppleObjC, *DObj, DObj->getAppleObjCSection(),
925                        DObj->getStrSection(), isLittleEndian());
926 }
927 
928 const DWARFDebugLine::LineTable *
getLineTableForUnit(DWARFUnit * U)929 DWARFContext::getLineTableForUnit(DWARFUnit *U) {
930   Expected<const DWARFDebugLine::LineTable *> ExpectedLineTable =
931       getLineTableForUnit(U, WarningHandler);
932   if (!ExpectedLineTable) {
933     WarningHandler(ExpectedLineTable.takeError());
934     return nullptr;
935   }
936   return *ExpectedLineTable;
937 }
938 
getLineTableForUnit(DWARFUnit * U,function_ref<void (Error)> RecoverableErrorHandler)939 Expected<const DWARFDebugLine::LineTable *> DWARFContext::getLineTableForUnit(
940     DWARFUnit *U, function_ref<void(Error)> RecoverableErrorHandler) {
941   if (!Line)
942     Line.reset(new DWARFDebugLine);
943 
944   auto UnitDIE = U->getUnitDIE();
945   if (!UnitDIE)
946     return nullptr;
947 
948   auto Offset = toSectionOffset(UnitDIE.find(DW_AT_stmt_list));
949   if (!Offset)
950     return nullptr; // No line table for this compile unit.
951 
952   uint64_t stmtOffset = *Offset + U->getLineTableOffset();
953   // See if the line table is cached.
954   if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
955     return lt;
956 
957   // Make sure the offset is good before we try to parse.
958   if (stmtOffset >= U->getLineSection().Data.size())
959     return nullptr;
960 
961   // We have to parse it first.
962   DWARFDataExtractor lineData(*DObj, U->getLineSection(), isLittleEndian(),
963                               U->getAddressByteSize());
964   return Line->getOrParseLineTable(lineData, stmtOffset, *this, U,
965                                    RecoverableErrorHandler);
966 }
967 
parseNormalUnits()968 void DWARFContext::parseNormalUnits() {
969   if (!NormalUnits.empty())
970     return;
971   DObj->forEachInfoSections([&](const DWARFSection &S) {
972     NormalUnits.addUnitsForSection(*this, S, DW_SECT_INFO);
973   });
974   NormalUnits.finishedInfoUnits();
975   DObj->forEachTypesSections([&](const DWARFSection &S) {
976     NormalUnits.addUnitsForSection(*this, S, DW_SECT_EXT_TYPES);
977   });
978 }
979 
parseDWOUnits(bool Lazy)980 void DWARFContext::parseDWOUnits(bool Lazy) {
981   if (!DWOUnits.empty())
982     return;
983   DObj->forEachInfoDWOSections([&](const DWARFSection &S) {
984     DWOUnits.addUnitsForDWOSection(*this, S, DW_SECT_INFO, Lazy);
985   });
986   DWOUnits.finishedInfoUnits();
987   DObj->forEachTypesDWOSections([&](const DWARFSection &S) {
988     DWOUnits.addUnitsForDWOSection(*this, S, DW_SECT_EXT_TYPES, Lazy);
989   });
990 }
991 
getCompileUnitForOffset(uint64_t Offset)992 DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint64_t Offset) {
993   parseNormalUnits();
994   return dyn_cast_or_null<DWARFCompileUnit>(
995       NormalUnits.getUnitForOffset(Offset));
996 }
997 
getCompileUnitForAddress(uint64_t Address)998 DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
999   // First, get the offset of the compile unit.
1000   uint64_t CUOffset = getDebugAranges()->findAddress(Address);
1001   // Retrieve the compile unit.
1002   return getCompileUnitForOffset(CUOffset);
1003 }
1004 
getDIEsForAddress(uint64_t Address)1005 DWARFContext::DIEsForAddress DWARFContext::getDIEsForAddress(uint64_t Address) {
1006   DIEsForAddress Result;
1007 
1008   DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
1009   if (!CU)
1010     return Result;
1011 
1012   Result.CompileUnit = CU;
1013   Result.FunctionDIE = CU->getSubroutineForAddress(Address);
1014 
1015   std::vector<DWARFDie> Worklist;
1016   Worklist.push_back(Result.FunctionDIE);
1017   while (!Worklist.empty()) {
1018     DWARFDie DIE = Worklist.back();
1019     Worklist.pop_back();
1020 
1021     if (!DIE.isValid())
1022       continue;
1023 
1024     if (DIE.getTag() == DW_TAG_lexical_block &&
1025         DIE.addressRangeContainsAddress(Address)) {
1026       Result.BlockDIE = DIE;
1027       break;
1028     }
1029 
1030     for (auto Child : DIE)
1031       Worklist.push_back(Child);
1032   }
1033 
1034   return Result;
1035 }
1036 
1037 /// TODO: change input parameter from "uint64_t Address"
1038 ///       into "SectionedAddress Address"
getFunctionNameAndStartLineForAddress(DWARFCompileUnit * CU,uint64_t Address,FunctionNameKind Kind,std::string & FunctionName,uint32_t & StartLine)1039 static bool getFunctionNameAndStartLineForAddress(DWARFCompileUnit *CU,
1040                                                   uint64_t Address,
1041                                                   FunctionNameKind Kind,
1042                                                   std::string &FunctionName,
1043                                                   uint32_t &StartLine) {
1044   // The address may correspond to instruction in some inlined function,
1045   // so we have to build the chain of inlined functions and take the
1046   // name of the topmost function in it.
1047   SmallVector<DWARFDie, 4> InlinedChain;
1048   CU->getInlinedChainForAddress(Address, InlinedChain);
1049   if (InlinedChain.empty())
1050     return false;
1051 
1052   const DWARFDie &DIE = InlinedChain[0];
1053   bool FoundResult = false;
1054   const char *Name = nullptr;
1055   if (Kind != FunctionNameKind::None && (Name = DIE.getSubroutineName(Kind))) {
1056     FunctionName = Name;
1057     FoundResult = true;
1058   }
1059   if (auto DeclLineResult = DIE.getDeclLine()) {
1060     StartLine = DeclLineResult;
1061     FoundResult = true;
1062   }
1063 
1064   return FoundResult;
1065 }
1066 
getTypeSize(DWARFDie Type,uint64_t PointerSize)1067 static Optional<uint64_t> getTypeSize(DWARFDie Type, uint64_t PointerSize) {
1068   if (auto SizeAttr = Type.find(DW_AT_byte_size))
1069     if (Optional<uint64_t> Size = SizeAttr->getAsUnsignedConstant())
1070       return Size;
1071 
1072   switch (Type.getTag()) {
1073   case DW_TAG_pointer_type:
1074   case DW_TAG_reference_type:
1075   case DW_TAG_rvalue_reference_type:
1076     return PointerSize;
1077   case DW_TAG_ptr_to_member_type: {
1078     if (DWARFDie BaseType = Type.getAttributeValueAsReferencedDie(DW_AT_type))
1079       if (BaseType.getTag() == DW_TAG_subroutine_type)
1080         return 2 * PointerSize;
1081     return PointerSize;
1082   }
1083   case DW_TAG_const_type:
1084   case DW_TAG_volatile_type:
1085   case DW_TAG_restrict_type:
1086   case DW_TAG_typedef: {
1087     if (DWARFDie BaseType = Type.getAttributeValueAsReferencedDie(DW_AT_type))
1088       return getTypeSize(BaseType, PointerSize);
1089     break;
1090   }
1091   case DW_TAG_array_type: {
1092     DWARFDie BaseType = Type.getAttributeValueAsReferencedDie(DW_AT_type);
1093     if (!BaseType)
1094       return Optional<uint64_t>();
1095     Optional<uint64_t> BaseSize = getTypeSize(BaseType, PointerSize);
1096     if (!BaseSize)
1097       return Optional<uint64_t>();
1098     uint64_t Size = *BaseSize;
1099     for (DWARFDie Child : Type) {
1100       if (Child.getTag() != DW_TAG_subrange_type)
1101         continue;
1102 
1103       if (auto ElemCountAttr = Child.find(DW_AT_count))
1104         if (Optional<uint64_t> ElemCount =
1105                 ElemCountAttr->getAsUnsignedConstant())
1106           Size *= *ElemCount;
1107       if (auto UpperBoundAttr = Child.find(DW_AT_upper_bound))
1108         if (Optional<int64_t> UpperBound =
1109                 UpperBoundAttr->getAsSignedConstant()) {
1110           int64_t LowerBound = 0;
1111           if (auto LowerBoundAttr = Child.find(DW_AT_lower_bound))
1112             LowerBound = LowerBoundAttr->getAsSignedConstant().getValueOr(0);
1113           Size *= *UpperBound - LowerBound + 1;
1114         }
1115     }
1116     return Size;
1117   }
1118   default:
1119     break;
1120   }
1121   return Optional<uint64_t>();
1122 }
1123 
1124 static Optional<int64_t>
getExpressionFrameOffset(ArrayRef<uint8_t> Expr,Optional<unsigned> FrameBaseReg)1125 getExpressionFrameOffset(ArrayRef<uint8_t> Expr,
1126                          Optional<unsigned> FrameBaseReg) {
1127   if (!Expr.empty() &&
1128       (Expr[0] == DW_OP_fbreg ||
1129        (FrameBaseReg && Expr[0] == DW_OP_breg0 + *FrameBaseReg))) {
1130     unsigned Count;
1131     int64_t Offset = decodeSLEB128(Expr.data() + 1, &Count, Expr.end());
1132     // A single DW_OP_fbreg or DW_OP_breg.
1133     if (Expr.size() == Count + 1)
1134       return Offset;
1135     // Same + DW_OP_deref (Fortran arrays look like this).
1136     if (Expr.size() == Count + 2 && Expr[Count + 1] == DW_OP_deref)
1137       return Offset;
1138     // Fallthrough. Do not accept ex. (DW_OP_breg W29, DW_OP_stack_value)
1139   }
1140   return None;
1141 }
1142 
addLocalsForDie(DWARFCompileUnit * CU,DWARFDie Subprogram,DWARFDie Die,std::vector<DILocal> & Result)1143 void DWARFContext::addLocalsForDie(DWARFCompileUnit *CU, DWARFDie Subprogram,
1144                                    DWARFDie Die, std::vector<DILocal> &Result) {
1145   if (Die.getTag() == DW_TAG_variable ||
1146       Die.getTag() == DW_TAG_formal_parameter) {
1147     DILocal Local;
1148     if (const char *Name = Subprogram.getSubroutineName(DINameKind::ShortName))
1149       Local.FunctionName = Name;
1150 
1151     Optional<unsigned> FrameBaseReg;
1152     if (auto FrameBase = Subprogram.find(DW_AT_frame_base))
1153       if (Optional<ArrayRef<uint8_t>> Expr = FrameBase->getAsBlock())
1154         if (!Expr->empty() && (*Expr)[0] >= DW_OP_reg0 &&
1155             (*Expr)[0] <= DW_OP_reg31) {
1156           FrameBaseReg = (*Expr)[0] - DW_OP_reg0;
1157         }
1158 
1159     if (Expected<std::vector<DWARFLocationExpression>> Loc =
1160             Die.getLocations(DW_AT_location)) {
1161       for (const auto &Entry : *Loc) {
1162         if (Optional<int64_t> FrameOffset =
1163                 getExpressionFrameOffset(Entry.Expr, FrameBaseReg)) {
1164           Local.FrameOffset = *FrameOffset;
1165           break;
1166         }
1167       }
1168     } else {
1169       // FIXME: missing DW_AT_location is OK here, but other errors should be
1170       // reported to the user.
1171       consumeError(Loc.takeError());
1172     }
1173 
1174     if (auto TagOffsetAttr = Die.find(DW_AT_LLVM_tag_offset))
1175       Local.TagOffset = TagOffsetAttr->getAsUnsignedConstant();
1176 
1177     if (auto Origin =
1178             Die.getAttributeValueAsReferencedDie(DW_AT_abstract_origin))
1179       Die = Origin;
1180     if (auto NameAttr = Die.find(DW_AT_name))
1181       if (Optional<const char *> Name = NameAttr->getAsCString())
1182         Local.Name = *Name;
1183     if (auto Type = Die.getAttributeValueAsReferencedDie(DW_AT_type))
1184       Local.Size = getTypeSize(Type, getCUAddrSize());
1185     if (auto DeclFileAttr = Die.find(DW_AT_decl_file)) {
1186       if (const auto *LT = CU->getContext().getLineTableForUnit(CU))
1187         LT->getFileNameByIndex(
1188             DeclFileAttr->getAsUnsignedConstant().getValue(),
1189             CU->getCompilationDir(),
1190             DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath,
1191             Local.DeclFile);
1192     }
1193     if (auto DeclLineAttr = Die.find(DW_AT_decl_line))
1194       Local.DeclLine = DeclLineAttr->getAsUnsignedConstant().getValue();
1195 
1196     Result.push_back(Local);
1197     return;
1198   }
1199 
1200   if (Die.getTag() == DW_TAG_inlined_subroutine)
1201     if (auto Origin =
1202             Die.getAttributeValueAsReferencedDie(DW_AT_abstract_origin))
1203       Subprogram = Origin;
1204 
1205   for (auto Child : Die)
1206     addLocalsForDie(CU, Subprogram, Child, Result);
1207 }
1208 
1209 std::vector<DILocal>
getLocalsForAddress(object::SectionedAddress Address)1210 DWARFContext::getLocalsForAddress(object::SectionedAddress Address) {
1211   std::vector<DILocal> Result;
1212   DWARFCompileUnit *CU = getCompileUnitForAddress(Address.Address);
1213   if (!CU)
1214     return Result;
1215 
1216   DWARFDie Subprogram = CU->getSubroutineForAddress(Address.Address);
1217   if (Subprogram.isValid())
1218     addLocalsForDie(CU, Subprogram, Subprogram, Result);
1219   return Result;
1220 }
1221 
getLineInfoForAddress(object::SectionedAddress Address,DILineInfoSpecifier Spec)1222 DILineInfo DWARFContext::getLineInfoForAddress(object::SectionedAddress Address,
1223                                                DILineInfoSpecifier Spec) {
1224   DILineInfo Result;
1225 
1226   DWARFCompileUnit *CU = getCompileUnitForAddress(Address.Address);
1227   if (!CU)
1228     return Result;
1229 
1230   getFunctionNameAndStartLineForAddress(CU, Address.Address, Spec.FNKind,
1231                                         Result.FunctionName, Result.StartLine);
1232   if (Spec.FLIKind != FileLineInfoKind::None) {
1233     if (const DWARFLineTable *LineTable = getLineTableForUnit(CU)) {
1234       LineTable->getFileLineInfoForAddress(
1235           {Address.Address, Address.SectionIndex}, CU->getCompilationDir(),
1236           Spec.FLIKind, Result);
1237     }
1238   }
1239   return Result;
1240 }
1241 
getLineInfoForAddressRange(object::SectionedAddress Address,uint64_t Size,DILineInfoSpecifier Spec)1242 DILineInfoTable DWARFContext::getLineInfoForAddressRange(
1243     object::SectionedAddress Address, uint64_t Size, DILineInfoSpecifier Spec) {
1244   DILineInfoTable  Lines;
1245   DWARFCompileUnit *CU = getCompileUnitForAddress(Address.Address);
1246   if (!CU)
1247     return Lines;
1248 
1249   uint32_t StartLine = 0;
1250   std::string FunctionName(DILineInfo::BadString);
1251   getFunctionNameAndStartLineForAddress(CU, Address.Address, Spec.FNKind,
1252                                         FunctionName, StartLine);
1253 
1254   // If the Specifier says we don't need FileLineInfo, just
1255   // return the top-most function at the starting address.
1256   if (Spec.FLIKind == FileLineInfoKind::None) {
1257     DILineInfo Result;
1258     Result.FunctionName = FunctionName;
1259     Result.StartLine = StartLine;
1260     Lines.push_back(std::make_pair(Address.Address, Result));
1261     return Lines;
1262   }
1263 
1264   const DWARFLineTable *LineTable = getLineTableForUnit(CU);
1265 
1266   // Get the index of row we're looking for in the line table.
1267   std::vector<uint32_t> RowVector;
1268   if (!LineTable->lookupAddressRange({Address.Address, Address.SectionIndex},
1269                                      Size, RowVector)) {
1270     return Lines;
1271   }
1272 
1273   for (uint32_t RowIndex : RowVector) {
1274     // Take file number and line/column from the row.
1275     const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
1276     DILineInfo Result;
1277     LineTable->getFileNameByIndex(Row.File, CU->getCompilationDir(),
1278                                   Spec.FLIKind, Result.FileName);
1279     Result.FunctionName = FunctionName;
1280     Result.Line = Row.Line;
1281     Result.Column = Row.Column;
1282     Result.StartLine = StartLine;
1283     Lines.push_back(std::make_pair(Row.Address.Address, Result));
1284   }
1285 
1286   return Lines;
1287 }
1288 
1289 DIInliningInfo
getInliningInfoForAddress(object::SectionedAddress Address,DILineInfoSpecifier Spec)1290 DWARFContext::getInliningInfoForAddress(object::SectionedAddress Address,
1291                                         DILineInfoSpecifier Spec) {
1292   DIInliningInfo InliningInfo;
1293 
1294   DWARFCompileUnit *CU = getCompileUnitForAddress(Address.Address);
1295   if (!CU)
1296     return InliningInfo;
1297 
1298   const DWARFLineTable *LineTable = nullptr;
1299   SmallVector<DWARFDie, 4> InlinedChain;
1300   CU->getInlinedChainForAddress(Address.Address, InlinedChain);
1301   if (InlinedChain.size() == 0) {
1302     // If there is no DIE for address (e.g. it is in unavailable .dwo file),
1303     // try to at least get file/line info from symbol table.
1304     if (Spec.FLIKind != FileLineInfoKind::None) {
1305       DILineInfo Frame;
1306       LineTable = getLineTableForUnit(CU);
1307       if (LineTable && LineTable->getFileLineInfoForAddress(
1308                            {Address.Address, Address.SectionIndex},
1309                            CU->getCompilationDir(), Spec.FLIKind, Frame))
1310         InliningInfo.addFrame(Frame);
1311     }
1312     return InliningInfo;
1313   }
1314 
1315   uint32_t CallFile = 0, CallLine = 0, CallColumn = 0, CallDiscriminator = 0;
1316   for (uint32_t i = 0, n = InlinedChain.size(); i != n; i++) {
1317     DWARFDie &FunctionDIE = InlinedChain[i];
1318     DILineInfo Frame;
1319     // Get function name if necessary.
1320     if (const char *Name = FunctionDIE.getSubroutineName(Spec.FNKind))
1321       Frame.FunctionName = Name;
1322     if (auto DeclLineResult = FunctionDIE.getDeclLine())
1323       Frame.StartLine = DeclLineResult;
1324     if (Spec.FLIKind != FileLineInfoKind::None) {
1325       if (i == 0) {
1326         // For the topmost frame, initialize the line table of this
1327         // compile unit and fetch file/line info from it.
1328         LineTable = getLineTableForUnit(CU);
1329         // For the topmost routine, get file/line info from line table.
1330         if (LineTable)
1331           LineTable->getFileLineInfoForAddress(
1332               {Address.Address, Address.SectionIndex}, CU->getCompilationDir(),
1333               Spec.FLIKind, Frame);
1334       } else {
1335         // Otherwise, use call file, call line and call column from
1336         // previous DIE in inlined chain.
1337         if (LineTable)
1338           LineTable->getFileNameByIndex(CallFile, CU->getCompilationDir(),
1339                                         Spec.FLIKind, Frame.FileName);
1340         Frame.Line = CallLine;
1341         Frame.Column = CallColumn;
1342         Frame.Discriminator = CallDiscriminator;
1343       }
1344       // Get call file/line/column of a current DIE.
1345       if (i + 1 < n) {
1346         FunctionDIE.getCallerFrame(CallFile, CallLine, CallColumn,
1347                                    CallDiscriminator);
1348       }
1349     }
1350     InliningInfo.addFrame(Frame);
1351   }
1352   return InliningInfo;
1353 }
1354 
1355 std::shared_ptr<DWARFContext>
getDWOContext(StringRef AbsolutePath)1356 DWARFContext::getDWOContext(StringRef AbsolutePath) {
1357   if (auto S = DWP.lock()) {
1358     DWARFContext *Ctxt = S->Context.get();
1359     return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
1360   }
1361 
1362   std::weak_ptr<DWOFile> *Entry = &DWOFiles[AbsolutePath];
1363 
1364   if (auto S = Entry->lock()) {
1365     DWARFContext *Ctxt = S->Context.get();
1366     return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
1367   }
1368 
1369   Expected<OwningBinary<ObjectFile>> Obj = [&] {
1370     if (!CheckedForDWP) {
1371       SmallString<128> DWPName;
1372       auto Obj = object::ObjectFile::createObjectFile(
1373           this->DWPName.empty()
1374               ? (DObj->getFileName() + ".dwp").toStringRef(DWPName)
1375               : StringRef(this->DWPName));
1376       if (Obj) {
1377         Entry = &DWP;
1378         return Obj;
1379       } else {
1380         CheckedForDWP = true;
1381         // TODO: Should this error be handled (maybe in a high verbosity mode)
1382         // before falling back to .dwo files?
1383         consumeError(Obj.takeError());
1384       }
1385     }
1386 
1387     return object::ObjectFile::createObjectFile(AbsolutePath);
1388   }();
1389 
1390   if (!Obj) {
1391     // TODO: Actually report errors helpfully.
1392     consumeError(Obj.takeError());
1393     return nullptr;
1394   }
1395 
1396   auto S = std::make_shared<DWOFile>();
1397   S->File = std::move(Obj.get());
1398   S->Context = DWARFContext::create(*S->File.getBinary());
1399   *Entry = S;
1400   auto *Ctxt = S->Context.get();
1401   return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
1402 }
1403 
createError(const Twine & Reason,llvm::Error E)1404 static Error createError(const Twine &Reason, llvm::Error E) {
1405   return make_error<StringError>(Reason + toString(std::move(E)),
1406                                  inconvertibleErrorCode());
1407 }
1408 
1409 /// SymInfo contains information about symbol: it's address
1410 /// and section index which is -1LL for absolute symbols.
1411 struct SymInfo {
1412   uint64_t Address;
1413   uint64_t SectionIndex;
1414 };
1415 
1416 /// Returns the address of symbol relocation used against and a section index.
1417 /// Used for futher relocations computation. Symbol's section load address is
getSymbolInfo(const object::ObjectFile & Obj,const RelocationRef & Reloc,const LoadedObjectInfo * L,std::map<SymbolRef,SymInfo> & Cache)1418 static Expected<SymInfo> getSymbolInfo(const object::ObjectFile &Obj,
1419                                        const RelocationRef &Reloc,
1420                                        const LoadedObjectInfo *L,
1421                                        std::map<SymbolRef, SymInfo> &Cache) {
1422   SymInfo Ret = {0, (uint64_t)-1LL};
1423   object::section_iterator RSec = Obj.section_end();
1424   object::symbol_iterator Sym = Reloc.getSymbol();
1425 
1426   std::map<SymbolRef, SymInfo>::iterator CacheIt = Cache.end();
1427   // First calculate the address of the symbol or section as it appears
1428   // in the object file
1429   if (Sym != Obj.symbol_end()) {
1430     bool New;
1431     std::tie(CacheIt, New) = Cache.insert({*Sym, {0, 0}});
1432     if (!New)
1433       return CacheIt->second;
1434 
1435     Expected<uint64_t> SymAddrOrErr = Sym->getAddress();
1436     if (!SymAddrOrErr)
1437       return createError("failed to compute symbol address: ",
1438                          SymAddrOrErr.takeError());
1439 
1440     // Also remember what section this symbol is in for later
1441     auto SectOrErr = Sym->getSection();
1442     if (!SectOrErr)
1443       return createError("failed to get symbol section: ",
1444                          SectOrErr.takeError());
1445 
1446     RSec = *SectOrErr;
1447     Ret.Address = *SymAddrOrErr;
1448   } else if (auto *MObj = dyn_cast<MachOObjectFile>(&Obj)) {
1449     RSec = MObj->getRelocationSection(Reloc.getRawDataRefImpl());
1450     Ret.Address = RSec->getAddress();
1451   }
1452 
1453   if (RSec != Obj.section_end())
1454     Ret.SectionIndex = RSec->getIndex();
1455 
1456   // If we are given load addresses for the sections, we need to adjust:
1457   // SymAddr = (Address of Symbol Or Section in File) -
1458   //           (Address of Section in File) +
1459   //           (Load Address of Section)
1460   // RSec is now either the section being targeted or the section
1461   // containing the symbol being targeted. In either case,
1462   // we need to perform the same computation.
1463   if (L && RSec != Obj.section_end())
1464     if (uint64_t SectionLoadAddress = L->getSectionLoadAddress(*RSec))
1465       Ret.Address += SectionLoadAddress - RSec->getAddress();
1466 
1467   if (CacheIt != Cache.end())
1468     CacheIt->second = Ret;
1469 
1470   return Ret;
1471 }
1472 
getSymbolName(const object::ObjectFile & Obj,const RelocationRef & Reloc)1473 static StringRef getSymbolName(const object::ObjectFile &Obj,
1474                                const RelocationRef &Reloc) {
1475   object::symbol_iterator Sym = Reloc.getSymbol();
1476   if (Sym != Obj.symbol_end()) {
1477     Expected<StringRef> SymNameOrErr = Sym->getName();
1478     if (SymNameOrErr && !SymNameOrErr->empty())
1479       return *SymNameOrErr;
1480   }
1481   return "<unknown symbol>";
1482 }
1483 
isRelocScattered(const object::ObjectFile & Obj,const RelocationRef & Reloc)1484 static bool isRelocScattered(const object::ObjectFile &Obj,
1485                              const RelocationRef &Reloc) {
1486   const MachOObjectFile *MachObj = dyn_cast<MachOObjectFile>(&Obj);
1487   if (!MachObj)
1488     return false;
1489   // MachO also has relocations that point to sections and
1490   // scattered relocations.
1491   auto RelocInfo = MachObj->getRelocation(Reloc.getRawDataRefImpl());
1492   return MachObj->isRelocationScattered(RelocInfo);
1493 }
1494 
1495 namespace {
1496 struct DWARFSectionMap final : public DWARFSection {
1497   RelocAddrMap Relocs;
1498 };
1499 
1500 class DWARFObjInMemory final : public DWARFObject {
1501   bool IsLittleEndian;
1502   uint8_t AddressSize;
1503   StringRef FileName;
1504   const object::ObjectFile *Obj = nullptr;
1505   std::vector<SectionName> SectionNames;
1506 
1507   using InfoSectionMap = MapVector<object::SectionRef, DWARFSectionMap,
1508                                    std::map<object::SectionRef, unsigned>>;
1509 
1510   InfoSectionMap InfoSections;
1511   InfoSectionMap TypesSections;
1512   InfoSectionMap InfoDWOSections;
1513   InfoSectionMap TypesDWOSections;
1514 
1515   DWARFSectionMap LocSection;
1516   DWARFSectionMap LoclistsSection;
1517   DWARFSectionMap LoclistsDWOSection;
1518   DWARFSectionMap LineSection;
1519   DWARFSectionMap RangesSection;
1520   DWARFSectionMap RnglistsSection;
1521   DWARFSectionMap StrOffsetsSection;
1522   DWARFSectionMap LineDWOSection;
1523   DWARFSectionMap FrameSection;
1524   DWARFSectionMap EHFrameSection;
1525   DWARFSectionMap LocDWOSection;
1526   DWARFSectionMap StrOffsetsDWOSection;
1527   DWARFSectionMap RangesDWOSection;
1528   DWARFSectionMap RnglistsDWOSection;
1529   DWARFSectionMap AddrSection;
1530   DWARFSectionMap AppleNamesSection;
1531   DWARFSectionMap AppleTypesSection;
1532   DWARFSectionMap AppleNamespacesSection;
1533   DWARFSectionMap AppleObjCSection;
1534   DWARFSectionMap NamesSection;
1535   DWARFSectionMap PubnamesSection;
1536   DWARFSectionMap PubtypesSection;
1537   DWARFSectionMap GnuPubnamesSection;
1538   DWARFSectionMap GnuPubtypesSection;
1539   DWARFSectionMap MacroSection;
1540 
mapNameToDWARFSection(StringRef Name)1541   DWARFSectionMap *mapNameToDWARFSection(StringRef Name) {
1542     return StringSwitch<DWARFSectionMap *>(Name)
1543         .Case("debug_loc", &LocSection)
1544         .Case("debug_loclists", &LoclistsSection)
1545         .Case("debug_loclists.dwo", &LoclistsDWOSection)
1546         .Case("debug_line", &LineSection)
1547         .Case("debug_frame", &FrameSection)
1548         .Case("eh_frame", &EHFrameSection)
1549         .Case("debug_str_offsets", &StrOffsetsSection)
1550         .Case("debug_ranges", &RangesSection)
1551         .Case("debug_rnglists", &RnglistsSection)
1552         .Case("debug_loc.dwo", &LocDWOSection)
1553         .Case("debug_line.dwo", &LineDWOSection)
1554         .Case("debug_names", &NamesSection)
1555         .Case("debug_rnglists.dwo", &RnglistsDWOSection)
1556         .Case("debug_str_offsets.dwo", &StrOffsetsDWOSection)
1557         .Case("debug_addr", &AddrSection)
1558         .Case("apple_names", &AppleNamesSection)
1559         .Case("debug_pubnames", &PubnamesSection)
1560         .Case("debug_pubtypes", &PubtypesSection)
1561         .Case("debug_gnu_pubnames", &GnuPubnamesSection)
1562         .Case("debug_gnu_pubtypes", &GnuPubtypesSection)
1563         .Case("apple_types", &AppleTypesSection)
1564         .Case("apple_namespaces", &AppleNamespacesSection)
1565         .Case("apple_namespac", &AppleNamespacesSection)
1566         .Case("apple_objc", &AppleObjCSection)
1567         .Case("debug_macro", &MacroSection)
1568         .Default(nullptr);
1569   }
1570 
1571   StringRef AbbrevSection;
1572   StringRef ArangesSection;
1573   StringRef StrSection;
1574   StringRef MacinfoSection;
1575   StringRef MacinfoDWOSection;
1576   StringRef MacroDWOSection;
1577   StringRef AbbrevDWOSection;
1578   StringRef StrDWOSection;
1579   StringRef CUIndexSection;
1580   StringRef GdbIndexSection;
1581   StringRef TUIndexSection;
1582   StringRef LineStrSection;
1583 
1584   // A deque holding section data whose iterators are not invalidated when
1585   // new decompressed sections are inserted at the end.
1586   std::deque<SmallString<0>> UncompressedSections;
1587 
mapSectionToMember(StringRef Name)1588   StringRef *mapSectionToMember(StringRef Name) {
1589     if (DWARFSection *Sec = mapNameToDWARFSection(Name))
1590       return &Sec->Data;
1591     return StringSwitch<StringRef *>(Name)
1592         .Case("debug_abbrev", &AbbrevSection)
1593         .Case("debug_aranges", &ArangesSection)
1594         .Case("debug_str", &StrSection)
1595         .Case("debug_macinfo", &MacinfoSection)
1596         .Case("debug_macinfo.dwo", &MacinfoDWOSection)
1597         .Case("debug_macro.dwo", &MacroDWOSection)
1598         .Case("debug_abbrev.dwo", &AbbrevDWOSection)
1599         .Case("debug_str.dwo", &StrDWOSection)
1600         .Case("debug_cu_index", &CUIndexSection)
1601         .Case("debug_tu_index", &TUIndexSection)
1602         .Case("gdb_index", &GdbIndexSection)
1603         .Case("debug_line_str", &LineStrSection)
1604         // Any more debug info sections go here.
1605         .Default(nullptr);
1606   }
1607 
1608   /// If Sec is compressed section, decompresses and updates its contents
1609   /// provided by Data. Otherwise leaves it unchanged.
maybeDecompress(const object::SectionRef & Sec,StringRef Name,StringRef & Data)1610   Error maybeDecompress(const object::SectionRef &Sec, StringRef Name,
1611                         StringRef &Data) {
1612     if (!Decompressor::isCompressed(Sec))
1613       return Error::success();
1614 
1615     Expected<Decompressor> Decompressor =
1616         Decompressor::create(Name, Data, IsLittleEndian, AddressSize == 8);
1617     if (!Decompressor)
1618       return Decompressor.takeError();
1619 
1620     SmallString<0> Out;
1621     if (auto Err = Decompressor->resizeAndDecompress(Out))
1622       return Err;
1623 
1624     UncompressedSections.push_back(std::move(Out));
1625     Data = UncompressedSections.back();
1626 
1627     return Error::success();
1628   }
1629 
1630 public:
DWARFObjInMemory(const StringMap<std::unique_ptr<MemoryBuffer>> & Sections,uint8_t AddrSize,bool IsLittleEndian)1631   DWARFObjInMemory(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections,
1632                    uint8_t AddrSize, bool IsLittleEndian)
1633       : IsLittleEndian(IsLittleEndian) {
1634     for (const auto &SecIt : Sections) {
1635       if (StringRef *SectionData = mapSectionToMember(SecIt.first()))
1636         *SectionData = SecIt.second->getBuffer();
1637       else if (SecIt.first() == "debug_info")
1638         // Find debug_info and debug_types data by section rather than name as
1639         // there are multiple, comdat grouped, of these sections.
1640         InfoSections[SectionRef()].Data = SecIt.second->getBuffer();
1641       else if (SecIt.first() == "debug_info.dwo")
1642         InfoDWOSections[SectionRef()].Data = SecIt.second->getBuffer();
1643       else if (SecIt.first() == "debug_types")
1644         TypesSections[SectionRef()].Data = SecIt.second->getBuffer();
1645       else if (SecIt.first() == "debug_types.dwo")
1646         TypesDWOSections[SectionRef()].Data = SecIt.second->getBuffer();
1647     }
1648   }
DWARFObjInMemory(const object::ObjectFile & Obj,const LoadedObjectInfo * L,function_ref<void (Error)> HandleError,function_ref<void (Error)> HandleWarning)1649   DWARFObjInMemory(const object::ObjectFile &Obj, const LoadedObjectInfo *L,
1650                    function_ref<void(Error)> HandleError, function_ref<void(Error)> HandleWarning )
1651       : IsLittleEndian(Obj.isLittleEndian()),
1652         AddressSize(Obj.getBytesInAddress()), FileName(Obj.getFileName()),
1653         Obj(&Obj) {
1654 
1655     StringMap<unsigned> SectionAmountMap;
1656     for (const SectionRef &Section : Obj.sections()) {
1657       StringRef Name;
1658       if (auto NameOrErr = Section.getName())
1659         Name = *NameOrErr;
1660       else
1661         consumeError(NameOrErr.takeError());
1662 
1663       ++SectionAmountMap[Name];
1664       SectionNames.push_back({ Name, true });
1665 
1666       // Skip BSS and Virtual sections, they aren't interesting.
1667       if (Section.isBSS() || Section.isVirtual())
1668         continue;
1669 
1670       // Skip sections stripped by dsymutil.
1671       if (Section.isStripped())
1672         continue;
1673 
1674       StringRef Data;
1675       Expected<section_iterator> SecOrErr = Section.getRelocatedSection();
1676       if (!SecOrErr) {
1677         HandleError(createError("failed to get relocated section: ",
1678                                 SecOrErr.takeError()));
1679         continue;
1680       }
1681 
1682       // Try to obtain an already relocated version of this section.
1683       // Else use the unrelocated section from the object file. We'll have to
1684       // apply relocations ourselves later.
1685       section_iterator RelocatedSection = *SecOrErr;
1686       if (!L || !L->getLoadedSectionContents(*RelocatedSection, Data)) {
1687         Expected<StringRef> E = Section.getContents();
1688         if (E)
1689           Data = *E;
1690         else
1691           // maybeDecompress below will error.
1692           consumeError(E.takeError());
1693       }
1694 
1695       if (auto Err = maybeDecompress(Section, Name, Data)) {
1696         HandleError(createError("failed to decompress '" + Name + "', ",
1697                                 std::move(Err)));
1698         continue;
1699       }
1700 
1701       // Compressed sections names in GNU style starts from ".z",
1702       // at this point section is decompressed and we drop compression prefix.
1703       Name = Name.substr(
1704           Name.find_first_not_of("._z")); // Skip ".", "z" and "_" prefixes.
1705 
1706       // Map platform specific debug section names to DWARF standard section
1707       // names.
1708       Name = Obj.mapDebugSectionName(Name);
1709 
1710       if (StringRef *SectionData = mapSectionToMember(Name)) {
1711         *SectionData = Data;
1712         if (Name == "debug_ranges") {
1713           // FIXME: Use the other dwo range section when we emit it.
1714           RangesDWOSection.Data = Data;
1715         }
1716       } else if (Name == "debug_info") {
1717         // Find debug_info and debug_types data by section rather than name as
1718         // there are multiple, comdat grouped, of these sections.
1719         InfoSections[Section].Data = Data;
1720       } else if (Name == "debug_info.dwo") {
1721         InfoDWOSections[Section].Data = Data;
1722       } else if (Name == "debug_types") {
1723         TypesSections[Section].Data = Data;
1724       } else if (Name == "debug_types.dwo") {
1725         TypesDWOSections[Section].Data = Data;
1726       }
1727 
1728       if (RelocatedSection == Obj.section_end())
1729         continue;
1730 
1731       StringRef RelSecName;
1732       if (auto NameOrErr = RelocatedSection->getName())
1733         RelSecName = *NameOrErr;
1734       else
1735         consumeError(NameOrErr.takeError());
1736 
1737       // If the section we're relocating was relocated already by the JIT,
1738       // then we used the relocated version above, so we do not need to process
1739       // relocations for it now.
1740       StringRef RelSecData;
1741       if (L && L->getLoadedSectionContents(*RelocatedSection, RelSecData))
1742         continue;
1743 
1744       // In Mach-o files, the relocations do not need to be applied if
1745       // there is no load offset to apply. The value read at the
1746       // relocation point already factors in the section address
1747       // (actually applying the relocations will produce wrong results
1748       // as the section address will be added twice).
1749       if (!L && isa<MachOObjectFile>(&Obj))
1750         continue;
1751 
1752       RelSecName = RelSecName.substr(
1753           RelSecName.find_first_not_of("._z")); // Skip . and _ prefixes.
1754 
1755       // TODO: Add support for relocations in other sections as needed.
1756       // Record relocations for the debug_info and debug_line sections.
1757       DWARFSectionMap *Sec = mapNameToDWARFSection(RelSecName);
1758       RelocAddrMap *Map = Sec ? &Sec->Relocs : nullptr;
1759       if (!Map) {
1760         // Find debug_info and debug_types relocs by section rather than name
1761         // as there are multiple, comdat grouped, of these sections.
1762         if (RelSecName == "debug_info")
1763           Map = &static_cast<DWARFSectionMap &>(InfoSections[*RelocatedSection])
1764                      .Relocs;
1765         else if (RelSecName == "debug_info.dwo")
1766           Map = &static_cast<DWARFSectionMap &>(
1767                      InfoDWOSections[*RelocatedSection])
1768                      .Relocs;
1769         else if (RelSecName == "debug_types")
1770           Map =
1771               &static_cast<DWARFSectionMap &>(TypesSections[*RelocatedSection])
1772                    .Relocs;
1773         else if (RelSecName == "debug_types.dwo")
1774           Map = &static_cast<DWARFSectionMap &>(
1775                      TypesDWOSections[*RelocatedSection])
1776                      .Relocs;
1777         else
1778           continue;
1779       }
1780 
1781       if (Section.relocation_begin() == Section.relocation_end())
1782         continue;
1783 
1784       // Symbol to [address, section index] cache mapping.
1785       std::map<SymbolRef, SymInfo> AddrCache;
1786       bool (*Supports)(uint64_t);
1787       RelocationResolver Resolver;
1788       std::tie(Supports, Resolver) = getRelocationResolver(Obj);
1789       for (const RelocationRef &Reloc : Section.relocations()) {
1790         // FIXME: it's not clear how to correctly handle scattered
1791         // relocations.
1792         if (isRelocScattered(Obj, Reloc))
1793           continue;
1794 
1795         Expected<SymInfo> SymInfoOrErr =
1796             getSymbolInfo(Obj, Reloc, L, AddrCache);
1797         if (!SymInfoOrErr) {
1798           HandleError(SymInfoOrErr.takeError());
1799           continue;
1800         }
1801 
1802         // Check if Resolver can handle this relocation type early so as not to
1803         // handle invalid cases in DWARFDataExtractor.
1804         //
1805         // TODO Don't store Resolver in every RelocAddrEntry.
1806         if (Supports && Supports(Reloc.getType())) {
1807           auto I = Map->try_emplace(
1808               Reloc.getOffset(),
1809               RelocAddrEntry{SymInfoOrErr->SectionIndex, Reloc,
1810                              SymInfoOrErr->Address,
1811                              Optional<object::RelocationRef>(), 0, Resolver});
1812           // If we didn't successfully insert that's because we already had a
1813           // relocation for that offset. Store it as a second relocation in the
1814           // same RelocAddrEntry instead.
1815           if (!I.second) {
1816             RelocAddrEntry &entry = I.first->getSecond();
1817             if (entry.Reloc2) {
1818               HandleError(createError(
1819                   "At most two relocations per offset are supported"));
1820             }
1821             entry.Reloc2 = Reloc;
1822             entry.SymbolValue2 = SymInfoOrErr->Address;
1823           }
1824         } else {
1825           SmallString<32> Type;
1826           Reloc.getTypeName(Type);
1827           // FIXME: Support more relocations & change this to an error
1828           HandleWarning(
1829               createError("failed to compute relocation: " + Type + " in " + Name +
1830                               " against " + getSymbolName(Obj, Reloc) + ", ",
1831                           errorCodeToError(object_error::parse_failed)));
1832         }
1833       }
1834     }
1835 
1836     for (SectionName &S : SectionNames)
1837       if (SectionAmountMap[S.Name] > 1)
1838         S.IsNameUnique = false;
1839   }
1840 
find(const DWARFSection & S,uint64_t Pos) const1841   Optional<RelocAddrEntry> find(const DWARFSection &S,
1842                                 uint64_t Pos) const override {
1843     auto &Sec = static_cast<const DWARFSectionMap &>(S);
1844     RelocAddrMap::const_iterator AI = Sec.Relocs.find(Pos);
1845     if (AI == Sec.Relocs.end())
1846       return None;
1847     return AI->second;
1848   }
1849 
getFile() const1850   const object::ObjectFile *getFile() const override { return Obj; }
1851 
getSectionNames() const1852   ArrayRef<SectionName> getSectionNames() const override {
1853     return SectionNames;
1854   }
1855 
isLittleEndian() const1856   bool isLittleEndian() const override { return IsLittleEndian; }
getAbbrevDWOSection() const1857   StringRef getAbbrevDWOSection() const override { return AbbrevDWOSection; }
getLineDWOSection() const1858   const DWARFSection &getLineDWOSection() const override {
1859     return LineDWOSection;
1860   }
getLocDWOSection() const1861   const DWARFSection &getLocDWOSection() const override {
1862     return LocDWOSection;
1863   }
getStrDWOSection() const1864   StringRef getStrDWOSection() const override { return StrDWOSection; }
getStrOffsetsDWOSection() const1865   const DWARFSection &getStrOffsetsDWOSection() const override {
1866     return StrOffsetsDWOSection;
1867   }
getRangesDWOSection() const1868   const DWARFSection &getRangesDWOSection() const override {
1869     return RangesDWOSection;
1870   }
getRnglistsDWOSection() const1871   const DWARFSection &getRnglistsDWOSection() const override {
1872     return RnglistsDWOSection;
1873   }
getLoclistsDWOSection() const1874   const DWARFSection &getLoclistsDWOSection() const override {
1875     return LoclistsDWOSection;
1876   }
getAddrSection() const1877   const DWARFSection &getAddrSection() const override { return AddrSection; }
getCUIndexSection() const1878   StringRef getCUIndexSection() const override { return CUIndexSection; }
getGdbIndexSection() const1879   StringRef getGdbIndexSection() const override { return GdbIndexSection; }
getTUIndexSection() const1880   StringRef getTUIndexSection() const override { return TUIndexSection; }
1881 
1882   // DWARF v5
getStrOffsetsSection() const1883   const DWARFSection &getStrOffsetsSection() const override {
1884     return StrOffsetsSection;
1885   }
getLineStrSection() const1886   StringRef getLineStrSection() const override { return LineStrSection; }
1887 
1888   // Sections for DWARF5 split dwarf proposal.
forEachInfoDWOSections(function_ref<void (const DWARFSection &)> F) const1889   void forEachInfoDWOSections(
1890       function_ref<void(const DWARFSection &)> F) const override {
1891     for (auto &P : InfoDWOSections)
1892       F(P.second);
1893   }
forEachTypesDWOSections(function_ref<void (const DWARFSection &)> F) const1894   void forEachTypesDWOSections(
1895       function_ref<void(const DWARFSection &)> F) const override {
1896     for (auto &P : TypesDWOSections)
1897       F(P.second);
1898   }
1899 
getAbbrevSection() const1900   StringRef getAbbrevSection() const override { return AbbrevSection; }
getLocSection() const1901   const DWARFSection &getLocSection() const override { return LocSection; }
getLoclistsSection() const1902   const DWARFSection &getLoclistsSection() const override { return LoclistsSection; }
getArangesSection() const1903   StringRef getArangesSection() const override { return ArangesSection; }
getFrameSection() const1904   const DWARFSection &getFrameSection() const override {
1905     return FrameSection;
1906   }
getEHFrameSection() const1907   const DWARFSection &getEHFrameSection() const override {
1908     return EHFrameSection;
1909   }
getLineSection() const1910   const DWARFSection &getLineSection() const override { return LineSection; }
getStrSection() const1911   StringRef getStrSection() const override { return StrSection; }
getRangesSection() const1912   const DWARFSection &getRangesSection() const override { return RangesSection; }
getRnglistsSection() const1913   const DWARFSection &getRnglistsSection() const override {
1914     return RnglistsSection;
1915   }
getMacroSection() const1916   const DWARFSection &getMacroSection() const override { return MacroSection; }
getMacroDWOSection() const1917   StringRef getMacroDWOSection() const override { return MacroDWOSection; }
getMacinfoSection() const1918   StringRef getMacinfoSection() const override { return MacinfoSection; }
getMacinfoDWOSection() const1919   StringRef getMacinfoDWOSection() const override { return MacinfoDWOSection; }
getPubnamesSection() const1920   const DWARFSection &getPubnamesSection() const override { return PubnamesSection; }
getPubtypesSection() const1921   const DWARFSection &getPubtypesSection() const override { return PubtypesSection; }
getGnuPubnamesSection() const1922   const DWARFSection &getGnuPubnamesSection() const override {
1923     return GnuPubnamesSection;
1924   }
getGnuPubtypesSection() const1925   const DWARFSection &getGnuPubtypesSection() const override {
1926     return GnuPubtypesSection;
1927   }
getAppleNamesSection() const1928   const DWARFSection &getAppleNamesSection() const override {
1929     return AppleNamesSection;
1930   }
getAppleTypesSection() const1931   const DWARFSection &getAppleTypesSection() const override {
1932     return AppleTypesSection;
1933   }
getAppleNamespacesSection() const1934   const DWARFSection &getAppleNamespacesSection() const override {
1935     return AppleNamespacesSection;
1936   }
getAppleObjCSection() const1937   const DWARFSection &getAppleObjCSection() const override {
1938     return AppleObjCSection;
1939   }
getNamesSection() const1940   const DWARFSection &getNamesSection() const override {
1941     return NamesSection;
1942   }
1943 
getFileName() const1944   StringRef getFileName() const override { return FileName; }
getAddressSize() const1945   uint8_t getAddressSize() const override { return AddressSize; }
forEachInfoSections(function_ref<void (const DWARFSection &)> F) const1946   void forEachInfoSections(
1947       function_ref<void(const DWARFSection &)> F) const override {
1948     for (auto &P : InfoSections)
1949       F(P.second);
1950   }
forEachTypesSections(function_ref<void (const DWARFSection &)> F) const1951   void forEachTypesSections(
1952       function_ref<void(const DWARFSection &)> F) const override {
1953     for (auto &P : TypesSections)
1954       F(P.second);
1955   }
1956 };
1957 } // namespace
1958 
1959 std::unique_ptr<DWARFContext>
create(const object::ObjectFile & Obj,const LoadedObjectInfo * L,std::string DWPName,std::function<void (Error)> RecoverableErrorHandler,std::function<void (Error)> WarningHandler)1960 DWARFContext::create(const object::ObjectFile &Obj, const LoadedObjectInfo *L,
1961                      std::string DWPName,
1962                      std::function<void(Error)> RecoverableErrorHandler,
1963                      std::function<void(Error)> WarningHandler) {
1964   auto DObj =
1965       std::make_unique<DWARFObjInMemory>(Obj, L, RecoverableErrorHandler, WarningHandler);
1966   return std::make_unique<DWARFContext>(std::move(DObj), std::move(DWPName),
1967                                         RecoverableErrorHandler,
1968                                         WarningHandler);
1969 }
1970 
1971 std::unique_ptr<DWARFContext>
create(const StringMap<std::unique_ptr<MemoryBuffer>> & Sections,uint8_t AddrSize,bool isLittleEndian,std::function<void (Error)> RecoverableErrorHandler,std::function<void (Error)> WarningHandler)1972 DWARFContext::create(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections,
1973                      uint8_t AddrSize, bool isLittleEndian,
1974                      std::function<void(Error)> RecoverableErrorHandler,
1975                      std::function<void(Error)> WarningHandler) {
1976   auto DObj =
1977       std::make_unique<DWARFObjInMemory>(Sections, AddrSize, isLittleEndian);
1978   return std::make_unique<DWARFContext>(
1979       std::move(DObj), "", RecoverableErrorHandler, WarningHandler);
1980 }
1981 
loadRegisterInfo(const object::ObjectFile & Obj)1982 Error DWARFContext::loadRegisterInfo(const object::ObjectFile &Obj) {
1983   // Detect the architecture from the object file. We usually don't need OS
1984   // info to lookup a target and create register info.
1985   Triple TT;
1986   TT.setArch(Triple::ArchType(Obj.getArch()));
1987   TT.setVendor(Triple::UnknownVendor);
1988   TT.setOS(Triple::UnknownOS);
1989   std::string TargetLookupError;
1990   const Target *TheTarget =
1991       TargetRegistry::lookupTarget(TT.str(), TargetLookupError);
1992   if (!TargetLookupError.empty())
1993     return createStringError(errc::invalid_argument,
1994                              TargetLookupError.c_str());
1995   MCTargetOptions MCOptions;
1996   RegInfo.reset(TheTarget->createMCRegInfo(TT.str(), MCOptions));
1997   return Error::success();
1998 }
1999 
getCUAddrSize()2000 uint8_t DWARFContext::getCUAddrSize() {
2001   // In theory, different compile units may have different address byte
2002   // sizes, but for simplicity we just use the address byte size of the
2003   // first compile unit. In practice the address size field is repeated across
2004   // various DWARF headers (at least in version 5) to make it easier to dump
2005   // them independently, not to enable varying the address size.
2006   unit_iterator_range CUs = compile_units();
2007   return CUs.empty() ? 0 : (*CUs.begin())->getAddressByteSize();
2008 }
2009