Home | History | Annotate | Download | only in DWARF
      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/SmallString.h"
     12 #include "llvm/ADT/StringSwitch.h"
     13 #include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
     14 #include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h"
     15 #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
     16 #include "llvm/Object/MachO.h"
     17 #include "llvm/Object/RelocVisitor.h"
     18 #include "llvm/Support/Compression.h"
     19 #include "llvm/Support/Dwarf.h"
     20 #include "llvm/Support/ELF.h"
     21 #include "llvm/Support/Format.h"
     22 #include "llvm/Support/Path.h"
     23 #include "llvm/Support/raw_ostream.h"
     24 #include <algorithm>
     25 using namespace llvm;
     26 using namespace dwarf;
     27 using namespace object;
     28 
     29 #define DEBUG_TYPE "dwarf"
     30 
     31 typedef DWARFDebugLine::LineTable DWARFLineTable;
     32 typedef DILineInfoSpecifier::FileLineInfoKind FileLineInfoKind;
     33 typedef DILineInfoSpecifier::FunctionNameKind FunctionNameKind;
     34 
     35 static void dumpPubSection(raw_ostream &OS, StringRef Name, StringRef Data,
     36                            bool LittleEndian, bool GnuStyle) {
     37   OS << "\n." << Name << " contents:\n";
     38   DataExtractor pubNames(Data, LittleEndian, 0);
     39   uint32_t offset = 0;
     40   while (pubNames.isValidOffset(offset)) {
     41     OS << "length = " << format("0x%08x", pubNames.getU32(&offset));
     42     OS << " version = " << format("0x%04x", pubNames.getU16(&offset));
     43     OS << " unit_offset = " << format("0x%08x", pubNames.getU32(&offset));
     44     OS << " unit_size = " << format("0x%08x", pubNames.getU32(&offset)) << '\n';
     45     if (GnuStyle)
     46       OS << "Offset     Linkage  Kind     Name\n";
     47     else
     48       OS << "Offset     Name\n";
     49 
     50     while (offset < Data.size()) {
     51       uint32_t dieRef = pubNames.getU32(&offset);
     52       if (dieRef == 0)
     53         break;
     54       OS << format("0x%8.8x ", dieRef);
     55       if (GnuStyle) {
     56         PubIndexEntryDescriptor desc(pubNames.getU8(&offset));
     57         OS << format("%-8s", dwarf::GDBIndexEntryLinkageString(desc.Linkage))
     58            << ' ' << format("%-8s", dwarf::GDBIndexEntryKindString(desc.Kind))
     59            << ' ';
     60       }
     61       OS << '\"' << pubNames.getCStr(&offset) << "\"\n";
     62     }
     63   }
     64 }
     65 
     66 static void dumpAccelSection(raw_ostream &OS, StringRef Name,
     67                              const DWARFSection& Section, StringRef StringSection,
     68                              bool LittleEndian) {
     69   DataExtractor AccelSection(Section.Data, LittleEndian, 0);
     70   DataExtractor StrData(StringSection, LittleEndian, 0);
     71   OS << "\n." << Name << " contents:\n";
     72   DWARFAcceleratorTable Accel(AccelSection, StrData, Section.Relocs);
     73   if (!Accel.extract())
     74     return;
     75   Accel.dump(OS);
     76 }
     77 
     78 void DWARFContext::dump(raw_ostream &OS, DIDumpType DumpType, bool DumpEH) {
     79   if (DumpType == DIDT_All || DumpType == DIDT_Abbrev) {
     80     OS << ".debug_abbrev contents:\n";
     81     getDebugAbbrev()->dump(OS);
     82   }
     83 
     84   if (DumpType == DIDT_All || DumpType == DIDT_AbbrevDwo)
     85     if (const DWARFDebugAbbrev *D = getDebugAbbrevDWO()) {
     86       OS << "\n.debug_abbrev.dwo contents:\n";
     87       D->dump(OS);
     88     }
     89 
     90   if (DumpType == DIDT_All || DumpType == DIDT_Info) {
     91     OS << "\n.debug_info contents:\n";
     92     for (const auto &CU : compile_units())
     93       CU->dump(OS);
     94   }
     95 
     96   if ((DumpType == DIDT_All || DumpType == DIDT_InfoDwo) &&
     97       getNumDWOCompileUnits()) {
     98     OS << "\n.debug_info.dwo contents:\n";
     99     for (const auto &DWOCU : dwo_compile_units())
    100       DWOCU->dump(OS);
    101   }
    102 
    103   if ((DumpType == DIDT_All || DumpType == DIDT_Types) && getNumTypeUnits()) {
    104     OS << "\n.debug_types contents:\n";
    105     for (const auto &TUS : type_unit_sections())
    106       for (const auto &TU : TUS)
    107         TU->dump(OS);
    108   }
    109 
    110   if ((DumpType == DIDT_All || DumpType == DIDT_TypesDwo) &&
    111       getNumDWOTypeUnits()) {
    112     OS << "\n.debug_types.dwo contents:\n";
    113     for (const auto &DWOTUS : dwo_type_unit_sections())
    114       for (const auto &DWOTU : DWOTUS)
    115         DWOTU->dump(OS);
    116   }
    117 
    118   if (DumpType == DIDT_All || DumpType == DIDT_Loc) {
    119     OS << "\n.debug_loc contents:\n";
    120     getDebugLoc()->dump(OS);
    121   }
    122 
    123   if (DumpType == DIDT_All || DumpType == DIDT_LocDwo) {
    124     OS << "\n.debug_loc.dwo contents:\n";
    125     getDebugLocDWO()->dump(OS);
    126   }
    127 
    128   if (DumpType == DIDT_All || DumpType == DIDT_Frames) {
    129     OS << "\n.debug_frame contents:\n";
    130     getDebugFrame()->dump(OS);
    131     if (DumpEH) {
    132       OS << "\n.eh_frame contents:\n";
    133       getEHFrame()->dump(OS);
    134     }
    135   }
    136 
    137   if (DumpType == DIDT_All || DumpType == DIDT_Macro) {
    138     OS << "\n.debug_macinfo contents:\n";
    139     getDebugMacro()->dump(OS);
    140   }
    141 
    142   uint32_t offset = 0;
    143   if (DumpType == DIDT_All || DumpType == DIDT_Aranges) {
    144     OS << "\n.debug_aranges contents:\n";
    145     DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
    146     DWARFDebugArangeSet set;
    147     while (set.extract(arangesData, &offset))
    148       set.dump(OS);
    149   }
    150 
    151   uint8_t savedAddressByteSize = 0;
    152   if (DumpType == DIDT_All || DumpType == DIDT_Line) {
    153     OS << "\n.debug_line contents:\n";
    154     for (const auto &CU : compile_units()) {
    155       savedAddressByteSize = CU->getAddressByteSize();
    156       const auto *CUDIE = CU->getUnitDIE();
    157       if (CUDIE == nullptr)
    158         continue;
    159       unsigned stmtOffset = CUDIE->getAttributeValueAsSectionOffset(
    160           CU.get(), DW_AT_stmt_list, -1U);
    161       if (stmtOffset != -1U) {
    162         DataExtractor lineData(getLineSection().Data, isLittleEndian(),
    163                                savedAddressByteSize);
    164         DWARFDebugLine::LineTable LineTable;
    165         LineTable.parse(lineData, &getLineSection().Relocs, &stmtOffset);
    166         LineTable.dump(OS);
    167       }
    168     }
    169   }
    170 
    171   if (DumpType == DIDT_All || DumpType == DIDT_CUIndex) {
    172     OS << "\n.debug_cu_index contents:\n";
    173     getCUIndex().dump(OS);
    174   }
    175 
    176   if (DumpType == DIDT_All || DumpType == DIDT_TUIndex) {
    177     OS << "\n.debug_tu_index contents:\n";
    178     getTUIndex().dump(OS);
    179   }
    180 
    181   if (DumpType == DIDT_All || DumpType == DIDT_LineDwo) {
    182     OS << "\n.debug_line.dwo contents:\n";
    183     unsigned stmtOffset = 0;
    184     DataExtractor lineData(getLineDWOSection().Data, isLittleEndian(),
    185                            savedAddressByteSize);
    186     DWARFDebugLine::LineTable LineTable;
    187     while (LineTable.Prologue.parse(lineData, &stmtOffset)) {
    188       LineTable.dump(OS);
    189       LineTable.clear();
    190     }
    191   }
    192 
    193   if (DumpType == DIDT_All || DumpType == DIDT_Str) {
    194     OS << "\n.debug_str contents:\n";
    195     DataExtractor strData(getStringSection(), isLittleEndian(), 0);
    196     offset = 0;
    197     uint32_t strOffset = 0;
    198     while (const char *s = strData.getCStr(&offset)) {
    199       OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
    200       strOffset = offset;
    201     }
    202   }
    203 
    204   if ((DumpType == DIDT_All || DumpType == DIDT_StrDwo) &&
    205       !getStringDWOSection().empty()) {
    206     OS << "\n.debug_str.dwo contents:\n";
    207     DataExtractor strDWOData(getStringDWOSection(), isLittleEndian(), 0);
    208     offset = 0;
    209     uint32_t strDWOOffset = 0;
    210     while (const char *s = strDWOData.getCStr(&offset)) {
    211       OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
    212       strDWOOffset = offset;
    213     }
    214   }
    215 
    216   if (DumpType == DIDT_All || DumpType == DIDT_Ranges) {
    217     OS << "\n.debug_ranges contents:\n";
    218     // In fact, different compile units may have different address byte
    219     // sizes, but for simplicity we just use the address byte size of the last
    220     // compile unit (there is no easy and fast way to associate address range
    221     // list and the compile unit it describes).
    222     DataExtractor rangesData(getRangeSection(), isLittleEndian(),
    223                              savedAddressByteSize);
    224     offset = 0;
    225     DWARFDebugRangeList rangeList;
    226     while (rangeList.extract(rangesData, &offset))
    227       rangeList.dump(OS);
    228   }
    229 
    230   if (DumpType == DIDT_All || DumpType == DIDT_Pubnames)
    231     dumpPubSection(OS, "debug_pubnames", getPubNamesSection(),
    232                    isLittleEndian(), false);
    233 
    234   if (DumpType == DIDT_All || DumpType == DIDT_Pubtypes)
    235     dumpPubSection(OS, "debug_pubtypes", getPubTypesSection(),
    236                    isLittleEndian(), false);
    237 
    238   if (DumpType == DIDT_All || DumpType == DIDT_GnuPubnames)
    239     dumpPubSection(OS, "debug_gnu_pubnames", getGnuPubNamesSection(),
    240                    isLittleEndian(), true /* GnuStyle */);
    241 
    242   if (DumpType == DIDT_All || DumpType == DIDT_GnuPubtypes)
    243     dumpPubSection(OS, "debug_gnu_pubtypes", getGnuPubTypesSection(),
    244                    isLittleEndian(), true /* GnuStyle */);
    245 
    246   if ((DumpType == DIDT_All || DumpType == DIDT_StrOffsetsDwo) &&
    247       !getStringOffsetDWOSection().empty()) {
    248     OS << "\n.debug_str_offsets.dwo contents:\n";
    249     DataExtractor strOffsetExt(getStringOffsetDWOSection(), isLittleEndian(),
    250                                0);
    251     offset = 0;
    252     uint64_t size = getStringOffsetDWOSection().size();
    253     while (offset < size) {
    254       OS << format("0x%8.8x: ", offset);
    255       OS << format("%8.8x\n", strOffsetExt.getU32(&offset));
    256     }
    257   }
    258 
    259   if (DumpType == DIDT_All || DumpType == DIDT_AppleNames)
    260     dumpAccelSection(OS, "apple_names", getAppleNamesSection(),
    261                      getStringSection(), isLittleEndian());
    262 
    263   if (DumpType == DIDT_All || DumpType == DIDT_AppleTypes)
    264     dumpAccelSection(OS, "apple_types", getAppleTypesSection(),
    265                      getStringSection(), isLittleEndian());
    266 
    267   if (DumpType == DIDT_All || DumpType == DIDT_AppleNamespaces)
    268     dumpAccelSection(OS, "apple_namespaces", getAppleNamespacesSection(),
    269                      getStringSection(), isLittleEndian());
    270 
    271   if (DumpType == DIDT_All || DumpType == DIDT_AppleObjC)
    272     dumpAccelSection(OS, "apple_objc", getAppleObjCSection(),
    273                      getStringSection(), isLittleEndian());
    274 }
    275 
    276 const DWARFUnitIndex &DWARFContext::getCUIndex() {
    277   if (CUIndex)
    278     return *CUIndex;
    279 
    280   DataExtractor CUIndexData(getCUIndexSection(), isLittleEndian(), 0);
    281 
    282   CUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_INFO);
    283   CUIndex->parse(CUIndexData);
    284   return *CUIndex;
    285 }
    286 
    287 const DWARFUnitIndex &DWARFContext::getTUIndex() {
    288   if (TUIndex)
    289     return *TUIndex;
    290 
    291   DataExtractor TUIndexData(getTUIndexSection(), isLittleEndian(), 0);
    292 
    293   TUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_TYPES);
    294   TUIndex->parse(TUIndexData);
    295   return *TUIndex;
    296 }
    297 
    298 const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
    299   if (Abbrev)
    300     return Abbrev.get();
    301 
    302   DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
    303 
    304   Abbrev.reset(new DWARFDebugAbbrev());
    305   Abbrev->extract(abbrData);
    306   return Abbrev.get();
    307 }
    308 
    309 const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
    310   if (AbbrevDWO)
    311     return AbbrevDWO.get();
    312 
    313   DataExtractor abbrData(getAbbrevDWOSection(), isLittleEndian(), 0);
    314   AbbrevDWO.reset(new DWARFDebugAbbrev());
    315   AbbrevDWO->extract(abbrData);
    316   return AbbrevDWO.get();
    317 }
    318 
    319 const DWARFDebugLoc *DWARFContext::getDebugLoc() {
    320   if (Loc)
    321     return Loc.get();
    322 
    323   DataExtractor LocData(getLocSection().Data, isLittleEndian(), 0);
    324   Loc.reset(new DWARFDebugLoc(getLocSection().Relocs));
    325   // assume all compile units have the same address byte size
    326   if (getNumCompileUnits())
    327     Loc->parse(LocData, getCompileUnitAtIndex(0)->getAddressByteSize());
    328   return Loc.get();
    329 }
    330 
    331 const DWARFDebugLocDWO *DWARFContext::getDebugLocDWO() {
    332   if (LocDWO)
    333     return LocDWO.get();
    334 
    335   DataExtractor LocData(getLocDWOSection().Data, isLittleEndian(), 0);
    336   LocDWO.reset(new DWARFDebugLocDWO());
    337   LocDWO->parse(LocData);
    338   return LocDWO.get();
    339 }
    340 
    341 const DWARFDebugAranges *DWARFContext::getDebugAranges() {
    342   if (Aranges)
    343     return Aranges.get();
    344 
    345   Aranges.reset(new DWARFDebugAranges());
    346   Aranges->generate(this);
    347   return Aranges.get();
    348 }
    349 
    350 const DWARFDebugFrame *DWARFContext::getDebugFrame() {
    351   if (DebugFrame)
    352     return DebugFrame.get();
    353 
    354   // There's a "bug" in the DWARFv3 standard with respect to the target address
    355   // size within debug frame sections. While DWARF is supposed to be independent
    356   // of its container, FDEs have fields with size being "target address size",
    357   // which isn't specified in DWARF in general. It's only specified for CUs, but
    358   // .eh_frame can appear without a .debug_info section. Follow the example of
    359   // other tools (libdwarf) and extract this from the container (ObjectFile
    360   // provides this information). This problem is fixed in DWARFv4
    361   // See this dwarf-discuss discussion for more details:
    362   // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
    363   DataExtractor debugFrameData(getDebugFrameSection(), isLittleEndian(),
    364                                getAddressSize());
    365   DebugFrame.reset(new DWARFDebugFrame(false /* IsEH */));
    366   DebugFrame->parse(debugFrameData);
    367   return DebugFrame.get();
    368 }
    369 
    370 const DWARFDebugFrame *DWARFContext::getEHFrame() {
    371   if (EHFrame)
    372     return EHFrame.get();
    373 
    374   DataExtractor debugFrameData(getEHFrameSection(), isLittleEndian(),
    375                                getAddressSize());
    376   DebugFrame.reset(new DWARFDebugFrame(true /* IsEH */));
    377   DebugFrame->parse(debugFrameData);
    378   return DebugFrame.get();
    379 }
    380 
    381 const DWARFDebugMacro *DWARFContext::getDebugMacro() {
    382   if (Macro)
    383     return Macro.get();
    384 
    385   DataExtractor MacinfoData(getMacinfoSection(), isLittleEndian(), 0);
    386   Macro.reset(new DWARFDebugMacro());
    387   Macro->parse(MacinfoData);
    388   return Macro.get();
    389 }
    390 
    391 const DWARFLineTable *
    392 DWARFContext::getLineTableForUnit(DWARFUnit *U) {
    393   if (!Line)
    394     Line.reset(new DWARFDebugLine(&getLineSection().Relocs));
    395 
    396   const auto *UnitDIE = U->getUnitDIE();
    397   if (UnitDIE == nullptr)
    398     return nullptr;
    399 
    400   unsigned stmtOffset =
    401       UnitDIE->getAttributeValueAsSectionOffset(U, DW_AT_stmt_list, -1U);
    402   if (stmtOffset == -1U)
    403     return nullptr; // No line table for this compile unit.
    404 
    405   stmtOffset += U->getLineTableOffset();
    406   // See if the line table is cached.
    407   if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
    408     return lt;
    409 
    410   // We have to parse it first.
    411   DataExtractor lineData(U->getLineSection(), isLittleEndian(),
    412                          U->getAddressByteSize());
    413   return Line->getOrParseLineTable(lineData, stmtOffset);
    414 }
    415 
    416 void DWARFContext::parseCompileUnits() {
    417   CUs.parse(*this, getInfoSection());
    418 }
    419 
    420 void DWARFContext::parseTypeUnits() {
    421   if (!TUs.empty())
    422     return;
    423   for (const auto &I : getTypesSections()) {
    424     TUs.emplace_back();
    425     TUs.back().parse(*this, I.second);
    426   }
    427 }
    428 
    429 void DWARFContext::parseDWOCompileUnits() {
    430   DWOCUs.parseDWO(*this, getInfoDWOSection());
    431 }
    432 
    433 void DWARFContext::parseDWOTypeUnits() {
    434   if (!DWOTUs.empty())
    435     return;
    436   for (const auto &I : getTypesDWOSections()) {
    437     DWOTUs.emplace_back();
    438     DWOTUs.back().parseDWO(*this, I.second);
    439   }
    440 }
    441 
    442 DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
    443   parseCompileUnits();
    444   return CUs.getUnitForOffset(Offset);
    445 }
    446 
    447 DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
    448   // First, get the offset of the compile unit.
    449   uint32_t CUOffset = getDebugAranges()->findAddress(Address);
    450   // Retrieve the compile unit.
    451   return getCompileUnitForOffset(CUOffset);
    452 }
    453 
    454 static bool getFunctionNameForAddress(DWARFCompileUnit *CU, uint64_t Address,
    455                                       FunctionNameKind Kind,
    456                                       std::string &FunctionName) {
    457   if (Kind == FunctionNameKind::None)
    458     return false;
    459   // The address may correspond to instruction in some inlined function,
    460   // so we have to build the chain of inlined functions and take the
    461   // name of the topmost function in it.
    462   const DWARFDebugInfoEntryInlinedChain &InlinedChain =
    463       CU->getInlinedChainForAddress(Address);
    464   if (InlinedChain.DIEs.size() == 0)
    465     return false;
    466   const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain.DIEs[0];
    467   if (const char *Name =
    468           TopFunctionDIE.getSubroutineName(InlinedChain.U, Kind)) {
    469     FunctionName = Name;
    470     return true;
    471   }
    472   return false;
    473 }
    474 
    475 DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
    476                                                DILineInfoSpecifier Spec) {
    477   DILineInfo Result;
    478 
    479   DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
    480   if (!CU)
    481     return Result;
    482   getFunctionNameForAddress(CU, Address, Spec.FNKind, Result.FunctionName);
    483   if (Spec.FLIKind != FileLineInfoKind::None) {
    484     if (const DWARFLineTable *LineTable = getLineTableForUnit(CU))
    485       LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
    486                                            Spec.FLIKind, Result);
    487   }
    488   return Result;
    489 }
    490 
    491 DILineInfoTable
    492 DWARFContext::getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
    493                                          DILineInfoSpecifier Spec) {
    494   DILineInfoTable  Lines;
    495   DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
    496   if (!CU)
    497     return Lines;
    498 
    499   std::string FunctionName = "<invalid>";
    500   getFunctionNameForAddress(CU, Address, Spec.FNKind, FunctionName);
    501 
    502   // If the Specifier says we don't need FileLineInfo, just
    503   // return the top-most function at the starting address.
    504   if (Spec.FLIKind == FileLineInfoKind::None) {
    505     DILineInfo Result;
    506     Result.FunctionName = FunctionName;
    507     Lines.push_back(std::make_pair(Address, Result));
    508     return Lines;
    509   }
    510 
    511   const DWARFLineTable *LineTable = getLineTableForUnit(CU);
    512 
    513   // Get the index of row we're looking for in the line table.
    514   std::vector<uint32_t> RowVector;
    515   if (!LineTable->lookupAddressRange(Address, Size, RowVector))
    516     return Lines;
    517 
    518   for (uint32_t RowIndex : RowVector) {
    519     // Take file number and line/column from the row.
    520     const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
    521     DILineInfo Result;
    522     LineTable->getFileNameByIndex(Row.File, CU->getCompilationDir(),
    523                                   Spec.FLIKind, Result.FileName);
    524     Result.FunctionName = FunctionName;
    525     Result.Line = Row.Line;
    526     Result.Column = Row.Column;
    527     Lines.push_back(std::make_pair(Row.Address, Result));
    528   }
    529 
    530   return Lines;
    531 }
    532 
    533 DIInliningInfo
    534 DWARFContext::getInliningInfoForAddress(uint64_t Address,
    535                                         DILineInfoSpecifier Spec) {
    536   DIInliningInfo InliningInfo;
    537 
    538   DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
    539   if (!CU)
    540     return InliningInfo;
    541 
    542   const DWARFLineTable *LineTable = nullptr;
    543   const DWARFDebugInfoEntryInlinedChain &InlinedChain =
    544       CU->getInlinedChainForAddress(Address);
    545   if (InlinedChain.DIEs.size() == 0) {
    546     // If there is no DIE for address (e.g. it is in unavailable .dwo file),
    547     // try to at least get file/line info from symbol table.
    548     if (Spec.FLIKind != FileLineInfoKind::None) {
    549       DILineInfo Frame;
    550       LineTable = getLineTableForUnit(CU);
    551       if (LineTable &&
    552           LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
    553                                                Spec.FLIKind, Frame))
    554         InliningInfo.addFrame(Frame);
    555     }
    556     return InliningInfo;
    557   }
    558 
    559   uint32_t CallFile = 0, CallLine = 0, CallColumn = 0;
    560   for (uint32_t i = 0, n = InlinedChain.DIEs.size(); i != n; i++) {
    561     const DWARFDebugInfoEntryMinimal &FunctionDIE = InlinedChain.DIEs[i];
    562     DILineInfo Frame;
    563     // Get function name if necessary.
    564     if (const char *Name =
    565             FunctionDIE.getSubroutineName(InlinedChain.U, Spec.FNKind))
    566       Frame.FunctionName = Name;
    567     if (Spec.FLIKind != FileLineInfoKind::None) {
    568       if (i == 0) {
    569         // For the topmost frame, initialize the line table of this
    570         // compile unit and fetch file/line info from it.
    571         LineTable = getLineTableForUnit(CU);
    572         // For the topmost routine, get file/line info from line table.
    573         if (LineTable)
    574           LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
    575                                                Spec.FLIKind, Frame);
    576       } else {
    577         // Otherwise, use call file, call line and call column from
    578         // previous DIE in inlined chain.
    579         if (LineTable)
    580           LineTable->getFileNameByIndex(CallFile, CU->getCompilationDir(),
    581                                         Spec.FLIKind, Frame.FileName);
    582         Frame.Line = CallLine;
    583         Frame.Column = CallColumn;
    584       }
    585       // Get call file/line/column of a current DIE.
    586       if (i + 1 < n) {
    587         FunctionDIE.getCallerFrame(InlinedChain.U, CallFile, CallLine,
    588                                    CallColumn);
    589       }
    590     }
    591     InliningInfo.addFrame(Frame);
    592   }
    593   return InliningInfo;
    594 }
    595 
    596 static bool consumeCompressedGnuHeader(StringRef &data,
    597                                        uint64_t &OriginalSize) {
    598   // Consume "ZLIB" prefix.
    599   if (!data.startswith("ZLIB"))
    600     return false;
    601   data = data.substr(4);
    602   // Consume uncompressed section size (big-endian 8 bytes).
    603   DataExtractor extractor(data, false, 8);
    604   uint32_t Offset = 0;
    605   OriginalSize = extractor.getU64(&Offset);
    606   if (Offset == 0)
    607     return false;
    608   data = data.substr(Offset);
    609   return true;
    610 }
    611 
    612 static bool consumeCompressedZLibHeader(StringRef &Data, uint64_t &OriginalSize,
    613                                         bool IsLE, bool Is64Bit) {
    614   using namespace ELF;
    615   uint64_t HdrSize = Is64Bit ? sizeof(Elf64_Chdr) : sizeof(Elf32_Chdr);
    616   if (Data.size() < HdrSize)
    617     return false;
    618 
    619   DataExtractor Extractor(Data, IsLE, 0);
    620   uint32_t Offset = 0;
    621   if (Extractor.getUnsigned(&Offset, Is64Bit ? sizeof(Elf64_Word)
    622                                              : sizeof(Elf32_Word)) !=
    623       ELFCOMPRESS_ZLIB)
    624     return false;
    625 
    626   // Skip Elf64_Chdr::ch_reserved field.
    627   if (Is64Bit)
    628     Offset += sizeof(Elf64_Word);
    629 
    630   OriginalSize = Extractor.getUnsigned(&Offset, Is64Bit ? sizeof(Elf64_Xword)
    631                                                         : sizeof(Elf32_Word));
    632   Data = Data.substr(HdrSize);
    633   return true;
    634 }
    635 
    636 static bool tryDecompress(StringRef &Name, StringRef &Data,
    637                           SmallString<32> &Out, bool ZLibStyle, bool IsLE,
    638                           bool Is64Bit) {
    639   if (!zlib::isAvailable())
    640     return false;
    641 
    642   uint64_t OriginalSize;
    643   bool Result =
    644       ZLibStyle ? consumeCompressedZLibHeader(Data, OriginalSize, IsLE, Is64Bit)
    645                 : consumeCompressedGnuHeader(Data, OriginalSize);
    646 
    647   if (!Result || zlib::uncompress(Data, Out, OriginalSize) != zlib::StatusOK)
    648     return false;
    649 
    650   // gnu-style names are started from "z", consume that.
    651   if (!ZLibStyle)
    652     Name = Name.substr(1);
    653   return true;
    654 }
    655 
    656 DWARFContextInMemory::DWARFContextInMemory(const object::ObjectFile &Obj,
    657     const LoadedObjectInfo *L)
    658     : IsLittleEndian(Obj.isLittleEndian()),
    659       AddressSize(Obj.getBytesInAddress()) {
    660   for (const SectionRef &Section : Obj.sections()) {
    661     StringRef name;
    662     Section.getName(name);
    663     // Skip BSS and Virtual sections, they aren't interesting.
    664     bool IsBSS = Section.isBSS();
    665     if (IsBSS)
    666       continue;
    667     bool IsVirtual = Section.isVirtual();
    668     if (IsVirtual)
    669       continue;
    670     StringRef data;
    671 
    672     section_iterator RelocatedSection = Section.getRelocatedSection();
    673     // Try to obtain an already relocated version of this section.
    674     // Else use the unrelocated section from the object file. We'll have to
    675     // apply relocations ourselves later.
    676     if (!L || !L->getLoadedSectionContents(*RelocatedSection,data))
    677       Section.getContents(data);
    678 
    679     name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes.
    680 
    681     bool ZLibStyleCompressed = Section.isCompressed();
    682     if (ZLibStyleCompressed || name.startswith("zdebug_")) {
    683       SmallString<32> Out;
    684       if (!tryDecompress(name, data, Out, ZLibStyleCompressed, IsLittleEndian,
    685                          AddressSize == 8))
    686         continue;
    687       UncompressedSections.emplace_back(std::move(Out));
    688       data = UncompressedSections.back();
    689     }
    690 
    691     StringRef *SectionData =
    692         StringSwitch<StringRef *>(name)
    693             .Case("debug_info", &InfoSection.Data)
    694             .Case("debug_abbrev", &AbbrevSection)
    695             .Case("debug_loc", &LocSection.Data)
    696             .Case("debug_line", &LineSection.Data)
    697             .Case("debug_aranges", &ARangeSection)
    698             .Case("debug_frame", &DebugFrameSection)
    699             .Case("eh_frame", &EHFrameSection)
    700             .Case("debug_str", &StringSection)
    701             .Case("debug_ranges", &RangeSection)
    702             .Case("debug_macinfo", &MacinfoSection)
    703             .Case("debug_pubnames", &PubNamesSection)
    704             .Case("debug_pubtypes", &PubTypesSection)
    705             .Case("debug_gnu_pubnames", &GnuPubNamesSection)
    706             .Case("debug_gnu_pubtypes", &GnuPubTypesSection)
    707             .Case("debug_info.dwo", &InfoDWOSection.Data)
    708             .Case("debug_abbrev.dwo", &AbbrevDWOSection)
    709             .Case("debug_loc.dwo", &LocDWOSection.Data)
    710             .Case("debug_line.dwo", &LineDWOSection.Data)
    711             .Case("debug_str.dwo", &StringDWOSection)
    712             .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
    713             .Case("debug_addr", &AddrSection)
    714             .Case("apple_names", &AppleNamesSection.Data)
    715             .Case("apple_types", &AppleTypesSection.Data)
    716             .Case("apple_namespaces", &AppleNamespacesSection.Data)
    717             .Case("apple_namespac", &AppleNamespacesSection.Data)
    718             .Case("apple_objc", &AppleObjCSection.Data)
    719             .Case("debug_cu_index", &CUIndexSection)
    720             .Case("debug_tu_index", &TUIndexSection)
    721             // Any more debug info sections go here.
    722             .Default(nullptr);
    723     if (SectionData) {
    724       *SectionData = data;
    725       if (name == "debug_ranges") {
    726         // FIXME: Use the other dwo range section when we emit it.
    727         RangeDWOSection = data;
    728       }
    729     } else if (name == "debug_types") {
    730       // Find debug_types data by section rather than name as there are
    731       // multiple, comdat grouped, debug_types sections.
    732       TypesSections[Section].Data = data;
    733     } else if (name == "debug_types.dwo") {
    734       TypesDWOSections[Section].Data = data;
    735     }
    736 
    737     if (RelocatedSection == Obj.section_end())
    738       continue;
    739 
    740     StringRef RelSecName;
    741     StringRef RelSecData;
    742     RelocatedSection->getName(RelSecName);
    743 
    744     // If the section we're relocating was relocated already by the JIT,
    745     // then we used the relocated version above, so we do not need to process
    746     // relocations for it now.
    747     if (L && L->getLoadedSectionContents(*RelocatedSection,RelSecData))
    748       continue;
    749 
    750     // In Mach-o files, the relocations do not need to be applied if
    751     // there is no load offset to apply. The value read at the
    752     // relocation point already factors in the section address
    753     // (actually applying the relocations will produce wrong results
    754     // as the section address will be added twice).
    755     if (!L && isa<MachOObjectFile>(&Obj))
    756       continue;
    757 
    758     RelSecName = RelSecName.substr(
    759         RelSecName.find_first_not_of("._")); // Skip . and _ prefixes.
    760 
    761     // TODO: Add support for relocations in other sections as needed.
    762     // Record relocations for the debug_info and debug_line sections.
    763     RelocAddrMap *Map = StringSwitch<RelocAddrMap*>(RelSecName)
    764         .Case("debug_info", &InfoSection.Relocs)
    765         .Case("debug_loc", &LocSection.Relocs)
    766         .Case("debug_info.dwo", &InfoDWOSection.Relocs)
    767         .Case("debug_line", &LineSection.Relocs)
    768         .Case("apple_names", &AppleNamesSection.Relocs)
    769         .Case("apple_types", &AppleTypesSection.Relocs)
    770         .Case("apple_namespaces", &AppleNamespacesSection.Relocs)
    771         .Case("apple_namespac", &AppleNamespacesSection.Relocs)
    772         .Case("apple_objc", &AppleObjCSection.Relocs)
    773         .Default(nullptr);
    774     if (!Map) {
    775       // Find debug_types relocs by section rather than name as there are
    776       // multiple, comdat grouped, debug_types sections.
    777       if (RelSecName == "debug_types")
    778         Map = &TypesSections[*RelocatedSection].Relocs;
    779       else if (RelSecName == "debug_types.dwo")
    780         Map = &TypesDWOSections[*RelocatedSection].Relocs;
    781       else
    782         continue;
    783     }
    784 
    785     if (Section.relocation_begin() != Section.relocation_end()) {
    786       uint64_t SectionSize = RelocatedSection->getSize();
    787       for (const RelocationRef &Reloc : Section.relocations()) {
    788         uint64_t Address = Reloc.getOffset();
    789         uint64_t Type = Reloc.getType();
    790         uint64_t SymAddr = 0;
    791         uint64_t SectionLoadAddress = 0;
    792         object::symbol_iterator Sym = Reloc.getSymbol();
    793         object::section_iterator RSec = Obj.section_end();
    794 
    795         // First calculate the address of the symbol or section as it appears
    796         // in the objct file
    797         if (Sym != Obj.symbol_end()) {
    798           Expected<uint64_t> SymAddrOrErr = Sym->getAddress();
    799           if (!SymAddrOrErr) {
    800             std::string Buf;
    801             raw_string_ostream OS(Buf);
    802             logAllUnhandledErrors(SymAddrOrErr.takeError(), OS, "");
    803             OS.flush();
    804             errs() << "error: failed to compute symbol address: "
    805                    << Buf << '\n';
    806             continue;
    807           }
    808           SymAddr = *SymAddrOrErr;
    809           // Also remember what section this symbol is in for later
    810           auto SectOrErr = Sym->getSection();
    811           if (!SectOrErr) {
    812             std::string Buf;
    813             raw_string_ostream OS(Buf);
    814             logAllUnhandledErrors(SectOrErr.takeError(), OS, "");
    815             OS.flush();
    816             errs() << "error: failed to get symbol section: "
    817                    << Buf << '\n';
    818             continue;
    819           }
    820           RSec = *SectOrErr;
    821         } else if (auto *MObj = dyn_cast<MachOObjectFile>(&Obj)) {
    822           // MachO also has relocations that point to sections and
    823           // scattered relocations.
    824           auto RelocInfo = MObj->getRelocation(Reloc.getRawDataRefImpl());
    825           if (MObj->isRelocationScattered(RelocInfo)) {
    826             // FIXME: it's not clear how to correctly handle scattered
    827             // relocations.
    828             continue;
    829           } else {
    830             RSec = MObj->getRelocationSection(Reloc.getRawDataRefImpl());
    831             SymAddr = RSec->getAddress();
    832           }
    833         }
    834 
    835         // If we are given load addresses for the sections, we need to adjust:
    836         // SymAddr = (Address of Symbol Or Section in File) -
    837         //           (Address of Section in File) +
    838         //           (Load Address of Section)
    839         if (L != nullptr && RSec != Obj.section_end()) {
    840           // RSec is now either the section being targeted or the section
    841           // containing the symbol being targeted. In either case,
    842           // we need to perform the same computation.
    843           StringRef SecName;
    844           RSec->getName(SecName);
    845 //           llvm::dbgs() << "Name: '" << SecName
    846 //                        << "', RSec: " << RSec->getRawDataRefImpl()
    847 //                        << ", Section: " << Section.getRawDataRefImpl() << "\n";
    848           SectionLoadAddress = L->getSectionLoadAddress(*RSec);
    849           if (SectionLoadAddress != 0)
    850             SymAddr += SectionLoadAddress - RSec->getAddress();
    851         }
    852 
    853         object::RelocVisitor V(Obj);
    854         object::RelocToApply R(V.visit(Type, Reloc, SymAddr));
    855         if (V.error()) {
    856           SmallString<32> Name;
    857           Reloc.getTypeName(Name);
    858           errs() << "error: failed to compute relocation: "
    859                  << Name << "\n";
    860           continue;
    861         }
    862 
    863         if (Address + R.Width > SectionSize) {
    864           errs() << "error: " << R.Width << "-byte relocation starting "
    865                  << Address << " bytes into section " << name << " which is "
    866                  << SectionSize << " bytes long.\n";
    867           continue;
    868         }
    869         if (R.Width > 8) {
    870           errs() << "error: can't handle a relocation of more than 8 bytes at "
    871                     "a time.\n";
    872           continue;
    873         }
    874         DEBUG(dbgs() << "Writing " << format("%p", R.Value)
    875                      << " at " << format("%p", Address)
    876                      << " with width " << format("%d", R.Width)
    877                      << "\n");
    878         Map->insert(std::make_pair(Address, std::make_pair(R.Width, R.Value)));
    879       }
    880     }
    881   }
    882 }
    883 
    884 void DWARFContextInMemory::anchor() { }
    885