LCOV - code coverage report
Current view: top level - lib/DebugInfo/DWARF - DWARFContext.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 725 751 96.5 %
Date: 2018-09-23 13:06:45 Functions: 97 97 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13