144d95122SEugene Zelenko //===- SymbolizableObjectFile.cpp -----------------------------------------===//
28df3a07aSAlexey Samsonov //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
68df3a07aSAlexey Samsonov //
78df3a07aSAlexey Samsonov //===----------------------------------------------------------------------===//
88df3a07aSAlexey Samsonov //
98df3a07aSAlexey Samsonov // Implementation of SymbolizableObjectFile class.
108df3a07aSAlexey Samsonov //
118df3a07aSAlexey Samsonov //===----------------------------------------------------------------------===//
128df3a07aSAlexey Samsonov 
133c34ef40SSnehasish Kumar #include "llvm/DebugInfo/Symbolize/SymbolizableObjectFile.h"
1444d95122SEugene Zelenko #include "llvm/ADT/STLExtras.h"
1544d95122SEugene Zelenko #include "llvm/ADT/Triple.h"
16264b5d9eSZachary Turner #include "llvm/BinaryFormat/COFF.h"
17c038e2dbSReid Kleckner #include "llvm/DebugInfo/DWARF/DWARFContext.h"
1844d95122SEugene Zelenko #include "llvm/Object/COFF.h"
1904a2e126SFangrui Song #include "llvm/Object/ELFObjectFile.h"
2044d95122SEugene Zelenko #include "llvm/Object/ObjectFile.h"
2144d95122SEugene Zelenko #include "llvm/Object/SymbolSize.h"
2244d95122SEugene Zelenko #include "llvm/Support/Casting.h"
2344d95122SEugene Zelenko #include "llvm/Support/DataExtractor.h"
2444d95122SEugene Zelenko #include <algorithm>
258df3a07aSAlexey Samsonov 
2644d95122SEugene Zelenko using namespace llvm;
278df3a07aSAlexey Samsonov using namespace object;
2844d95122SEugene Zelenko using namespace symbolize;
298df3a07aSAlexey Samsonov 
301c03389cSReid Kleckner Expected<std::unique_ptr<SymbolizableObjectFile>>
create(const object::ObjectFile * Obj,std::unique_ptr<DIContext> DICtx,bool UntagAddresses)315de4692cSYuanfang Chen SymbolizableObjectFile::create(const object::ObjectFile *Obj,
32a56d81f4SPeter Collingbourne                                std::unique_ptr<DIContext> DICtx,
33a56d81f4SPeter Collingbourne                                bool UntagAddresses) {
340feb6e52SPeter Collingbourne   assert(DICtx);
358df3a07aSAlexey Samsonov   std::unique_ptr<SymbolizableObjectFile> res(
36a56d81f4SPeter Collingbourne       new SymbolizableObjectFile(Obj, std::move(DICtx), UntagAddresses));
378df3a07aSAlexey Samsonov   std::unique_ptr<DataExtractor> OpdExtractor;
388df3a07aSAlexey Samsonov   uint64_t OpdAddress = 0;
398df3a07aSAlexey Samsonov   // Find the .opd (function descriptor) section if any, for big-endian
408df3a07aSAlexey Samsonov   // PowerPC64 ELF.
418df3a07aSAlexey Samsonov   if (Obj->getArch() == Triple::ppc64) {
428df3a07aSAlexey Samsonov     for (section_iterator Section : Obj->sections()) {
43bcc00e1aSGeorge Rimar       Expected<StringRef> NameOrErr = Section->getName();
44bcc00e1aSGeorge Rimar       if (!NameOrErr)
451c03389cSReid Kleckner         return NameOrErr.takeError();
46bcc00e1aSGeorge Rimar 
47bcc00e1aSGeorge Rimar       if (*NameOrErr == ".opd") {
48e183340cSFangrui Song         Expected<StringRef> E = Section->getContents();
49e183340cSFangrui Song         if (!E)
501c03389cSReid Kleckner           return E.takeError();
51e183340cSFangrui Song         OpdExtractor.reset(new DataExtractor(*E, Obj->isLittleEndian(),
528df3a07aSAlexey Samsonov                                              Obj->getBytesInAddress()));
538df3a07aSAlexey Samsonov         OpdAddress = Section->getAddress();
548df3a07aSAlexey Samsonov         break;
558df3a07aSAlexey Samsonov       }
568df3a07aSAlexey Samsonov     }
578df3a07aSAlexey Samsonov   }
588df3a07aSAlexey Samsonov   std::vector<std::pair<SymbolRef, uint64_t>> Symbols =
598df3a07aSAlexey Samsonov       computeSymbolSizes(*Obj);
608df3a07aSAlexey Samsonov   for (auto &P : Symbols)
611c03389cSReid Kleckner     if (Error E =
621c03389cSReid Kleckner             res->addSymbol(P.first, P.second, OpdExtractor.get(), OpdAddress))
631c03389cSReid Kleckner       return std::move(E);
648df3a07aSAlexey Samsonov 
658df3a07aSAlexey Samsonov   // If this is a COFF object and we didn't find any symbols, try the export
668df3a07aSAlexey Samsonov   // table.
678df3a07aSAlexey Samsonov   if (Symbols.empty()) {
688df3a07aSAlexey Samsonov     if (auto *CoffObj = dyn_cast<COFFObjectFile>(Obj))
691c03389cSReid Kleckner       if (Error E = res->addCoffExportSymbols(CoffObj))
701c03389cSReid Kleckner         return std::move(E);
718df3a07aSAlexey Samsonov   }
72afb54fd6SFangrui Song 
7392ee3dd9SFangrui Song   std::vector<SymbolDesc> &SS = res->Symbols;
74cb300f12SFangrui Song   // Sort by (Addr,Size,Name). If several SymbolDescs share the same Addr,
75cb300f12SFangrui Song   // pick the one with the largest Size. This helps us avoid symbols with no
76cb300f12SFangrui Song   // size information (Size=0).
770fd7c31aSFangrui Song   llvm::stable_sort(SS);
7892ee3dd9SFangrui Song   auto I = SS.begin(), E = SS.end(), J = SS.begin();
79cb300f12SFangrui Song   while (I != E) {
80cb300f12SFangrui Song     auto OI = I;
8104a2e126SFangrui Song     while (++I != E && OI->Addr == I->Addr) {
82cb300f12SFangrui Song     }
83cb300f12SFangrui Song     *J++ = I[-1];
84cb300f12SFangrui Song   }
8592ee3dd9SFangrui Song   SS.erase(J, SS.end());
86afb54fd6SFangrui Song 
87c55cf4afSBill Wendling   return std::move(res);
888df3a07aSAlexey Samsonov }
898df3a07aSAlexey Samsonov 
SymbolizableObjectFile(const ObjectFile * Obj,std::unique_ptr<DIContext> DICtx,bool UntagAddresses)905de4692cSYuanfang Chen SymbolizableObjectFile::SymbolizableObjectFile(const ObjectFile *Obj,
91a56d81f4SPeter Collingbourne                                                std::unique_ptr<DIContext> DICtx,
92a56d81f4SPeter Collingbourne                                                bool UntagAddresses)
93a56d81f4SPeter Collingbourne     : Module(Obj), DebugInfoContext(std::move(DICtx)),
94a56d81f4SPeter Collingbourne       UntagAddresses(UntagAddresses) {}
958df3a07aSAlexey Samsonov 
968df3a07aSAlexey Samsonov namespace {
9744d95122SEugene Zelenko 
988df3a07aSAlexey Samsonov struct OffsetNamePair {
998df3a07aSAlexey Samsonov   uint32_t Offset;
1008df3a07aSAlexey Samsonov   StringRef Name;
10144d95122SEugene Zelenko 
operator <__anona54ac7b30111::OffsetNamePair1028df3a07aSAlexey Samsonov   bool operator<(const OffsetNamePair &R) const {
1038df3a07aSAlexey Samsonov     return Offset < R.Offset;
1048df3a07aSAlexey Samsonov   }
1058df3a07aSAlexey Samsonov };
10644d95122SEugene Zelenko 
10744d95122SEugene Zelenko } // end anonymous namespace
1088df3a07aSAlexey Samsonov 
addCoffExportSymbols(const COFFObjectFile * CoffObj)1091c03389cSReid Kleckner Error SymbolizableObjectFile::addCoffExportSymbols(
1108df3a07aSAlexey Samsonov     const COFFObjectFile *CoffObj) {
1118df3a07aSAlexey Samsonov   // Get all export names and offsets.
1128df3a07aSAlexey Samsonov   std::vector<OffsetNamePair> ExportSyms;
1138df3a07aSAlexey Samsonov   for (const ExportDirectoryEntryRef &Ref : CoffObj->export_directories()) {
1148df3a07aSAlexey Samsonov     StringRef Name;
1158df3a07aSAlexey Samsonov     uint32_t Offset;
1168df3a07aSAlexey Samsonov     if (auto EC = Ref.getSymbolName(Name))
1178df3a07aSAlexey Samsonov       return EC;
1188df3a07aSAlexey Samsonov     if (auto EC = Ref.getExportRVA(Offset))
1198df3a07aSAlexey Samsonov       return EC;
1208df3a07aSAlexey Samsonov     ExportSyms.push_back(OffsetNamePair{Offset, Name});
1218df3a07aSAlexey Samsonov   }
1228df3a07aSAlexey Samsonov   if (ExportSyms.empty())
1231c03389cSReid Kleckner     return Error::success();
1248df3a07aSAlexey Samsonov 
1258df3a07aSAlexey Samsonov   // Sort by ascending offset.
1268df3a07aSAlexey Samsonov   array_pod_sort(ExportSyms.begin(), ExportSyms.end());
1278df3a07aSAlexey Samsonov 
1288df3a07aSAlexey Samsonov   // Approximate the symbol sizes by assuming they run to the next symbol.
1298df3a07aSAlexey Samsonov   // FIXME: This assumes all exports are functions.
1308df3a07aSAlexey Samsonov   uint64_t ImageBase = CoffObj->getImageBase();
1318df3a07aSAlexey Samsonov   for (auto I = ExportSyms.begin(), E = ExportSyms.end(); I != E; ++I) {
1328df3a07aSAlexey Samsonov     OffsetNamePair &Export = *I;
1338df3a07aSAlexey Samsonov     // FIXME: The last export has a one byte size now.
1348df3a07aSAlexey Samsonov     uint32_t NextOffset = I != E ? I->Offset : Export.Offset + 1;
1358df3a07aSAlexey Samsonov     uint64_t SymbolStart = ImageBase + Export.Offset;
1368df3a07aSAlexey Samsonov     uint64_t SymbolSize = NextOffset - Export.Offset;
13792ee3dd9SFangrui Song     Symbols.push_back({SymbolStart, SymbolSize, Export.Name, 0});
1388df3a07aSAlexey Samsonov   }
1391c03389cSReid Kleckner   return Error::success();
1408df3a07aSAlexey Samsonov }
1418df3a07aSAlexey Samsonov 
addSymbol(const SymbolRef & Symbol,uint64_t SymbolSize,DataExtractor * OpdExtractor,uint64_t OpdAddress)1421c03389cSReid Kleckner Error SymbolizableObjectFile::addSymbol(const SymbolRef &Symbol,
1438df3a07aSAlexey Samsonov                                         uint64_t SymbolSize,
1448df3a07aSAlexey Samsonov                                         DataExtractor *OpdExtractor,
1458df3a07aSAlexey Samsonov                                         uint64_t OpdAddress) {
146123be5d4SMatt Davis   // Avoid adding symbols from an unknown/undefined section.
1476d766c8bSFangrui Song   const ObjectFile &Obj = *Symbol.getObject();
14804a2e126SFangrui Song   Expected<StringRef> SymbolNameOrErr = Symbol.getName();
14904a2e126SFangrui Song   if (!SymbolNameOrErr)
15004a2e126SFangrui Song     return SymbolNameOrErr.takeError();
15104a2e126SFangrui Song   StringRef SymbolName = *SymbolNameOrErr;
15204a2e126SFangrui Song 
15304a2e126SFangrui Song   uint32_t ELFSymIdx =
15404a2e126SFangrui Song       Obj.isELF() ? ELFSymbolRef(Symbol).getRawDataRefImpl().d.b : 0;
155123be5d4SMatt Davis   Expected<section_iterator> Sec = Symbol.getSection();
15604a2e126SFangrui Song   if (!Sec || Obj.section_end() == *Sec) {
15704a2e126SFangrui Song     if (Obj.isELF()) {
15804a2e126SFangrui Song       // Store the (index, filename) pair for a file symbol.
15904a2e126SFangrui Song       ELFSymbolRef ESym(Symbol);
16004a2e126SFangrui Song       if (ESym.getELFType() == ELF::STT_FILE)
16104a2e126SFangrui Song         FileSymbols.emplace_back(ELFSymIdx, SymbolName);
16204a2e126SFangrui Song     }
1631c03389cSReid Kleckner     return Error::success();
16404a2e126SFangrui Song   }
1656d766c8bSFangrui Song 
1667bd8d994SKevin Enderby   Expected<SymbolRef::Type> SymbolTypeOrErr = Symbol.getType();
1677bd8d994SKevin Enderby   if (!SymbolTypeOrErr)
1681c03389cSReid Kleckner     return SymbolTypeOrErr.takeError();
1695afbc1cdSKevin Enderby   SymbolRef::Type SymbolType = *SymbolTypeOrErr;
1706d766c8bSFangrui Song   if (Obj.isELF()) {
1716d766c8bSFangrui Song     // Allow function and data symbols. Additionally allow STT_NONE, which are
1726d766c8bSFangrui Song     // common for functions defined in assembly.
1736d766c8bSFangrui Song     uint8_t Type = ELFSymbolRef(Symbol).getELFType();
1746d766c8bSFangrui Song     if (Type != ELF::STT_NOTYPE && Type != ELF::STT_FUNC &&
1756d766c8bSFangrui Song         Type != ELF::STT_OBJECT && Type != ELF::STT_GNU_IFUNC)
1761c03389cSReid Kleckner       return Error::success();
177a7ceef92SFangrui Song     // Some STT_NOTYPE symbols are not desired. This excludes STT_SECTION and
178a7ceef92SFangrui Song     // ARM mapping symbols.
179a7ceef92SFangrui Song     uint32_t Flags = cantFail(Symbol.getFlags());
180a7ceef92SFangrui Song     if (Flags & SymbolRef::SF_FormatSpecific)
181a7ceef92SFangrui Song       return Error::success();
1826d766c8bSFangrui Song   } else if (SymbolType != SymbolRef::ST_Function &&
1836d766c8bSFangrui Song              SymbolType != SymbolRef::ST_Data) {
1846d766c8bSFangrui Song     return Error::success();
1856d766c8bSFangrui Song   }
1866d766c8bSFangrui Song 
187931cb65dSKevin Enderby   Expected<uint64_t> SymbolAddressOrErr = Symbol.getAddress();
188931cb65dSKevin Enderby   if (!SymbolAddressOrErr)
1891c03389cSReid Kleckner     return SymbolAddressOrErr.takeError();
1908df3a07aSAlexey Samsonov   uint64_t SymbolAddress = *SymbolAddressOrErr;
191a56d81f4SPeter Collingbourne   if (UntagAddresses) {
192a56d81f4SPeter Collingbourne     // For kernel addresses, bits 56-63 need to be set, so we sign extend bit 55
193a56d81f4SPeter Collingbourne     // into bits 56-63 instead of masking them out.
194f0380bacSPeter Collingbourne     SymbolAddress &= (1ull << 56) - 1;
195a56d81f4SPeter Collingbourne     SymbolAddress = (int64_t(SymbolAddress) << 8) >> 8;
196a56d81f4SPeter Collingbourne   }
1978df3a07aSAlexey Samsonov   if (OpdExtractor) {
1988df3a07aSAlexey Samsonov     // For big-endian PowerPC64 ELF, symbols in the .opd section refer to
1998df3a07aSAlexey Samsonov     // function descriptors. The first word of the descriptor is a pointer to
2008df3a07aSAlexey Samsonov     // the function's code.
2018df3a07aSAlexey Samsonov     // For the purposes of symbolization, pretend the symbol's address is that
2028df3a07aSAlexey Samsonov     // of the function's code, not the descriptor.
2038df3a07aSAlexey Samsonov     uint64_t OpdOffset = SymbolAddress - OpdAddress;
204f26a70a5SIgor Kudrin     if (OpdExtractor->isValidOffsetForAddress(OpdOffset))
205f26a70a5SIgor Kudrin       SymbolAddress = OpdExtractor->getAddress(&OpdOffset);
2068df3a07aSAlexey Samsonov   }
2078df3a07aSAlexey Samsonov   // Mach-O symbol table names have leading underscore, skip it.
20844d95122SEugene Zelenko   if (Module->isMachO() && !SymbolName.empty() && SymbolName[0] == '_')
2098df3a07aSAlexey Samsonov     SymbolName = SymbolName.drop_front();
2106d766c8bSFangrui Song 
21104a2e126SFangrui Song   if (Obj.isELF() && ELFSymbolRef(Symbol).getBinding() != ELF::STB_LOCAL)
21204a2e126SFangrui Song     ELFSymIdx = 0;
21392ee3dd9SFangrui Song   Symbols.push_back({SymbolAddress, SymbolSize, SymbolName, ELFSymIdx});
2141c03389cSReid Kleckner   return Error::success();
2158df3a07aSAlexey Samsonov }
2168df3a07aSAlexey Samsonov 
2178df3a07aSAlexey Samsonov // Return true if this is a 32-bit x86 PE COFF module.
isWin32Module() const2188df3a07aSAlexey Samsonov bool SymbolizableObjectFile::isWin32Module() const {
2198df3a07aSAlexey Samsonov   auto *CoffObject = dyn_cast<COFFObjectFile>(Module);
2208df3a07aSAlexey Samsonov   return CoffObject && CoffObject->getMachine() == COFF::IMAGE_FILE_MACHINE_I386;
2218df3a07aSAlexey Samsonov }
2228df3a07aSAlexey Samsonov 
getModulePreferredBase() const2238df3a07aSAlexey Samsonov uint64_t SymbolizableObjectFile::getModulePreferredBase() const {
2248df3a07aSAlexey Samsonov   if (auto *CoffObject = dyn_cast<COFFObjectFile>(Module))
2258df3a07aSAlexey Samsonov     return CoffObject->getImageBase();
2268df3a07aSAlexey Samsonov   return 0;
2278df3a07aSAlexey Samsonov }
2288df3a07aSAlexey Samsonov 
getNameFromSymbolTable(uint64_t Address,std::string & Name,uint64_t & Addr,uint64_t & Size,std::string & FileName) const22904a2e126SFangrui Song bool SymbolizableObjectFile::getNameFromSymbolTable(
23092ee3dd9SFangrui Song     uint64_t Address, std::string &Name, uint64_t &Addr, uint64_t &Size,
23192ee3dd9SFangrui Song     std::string &FileName) const {
23204a2e126SFangrui Song   SymbolDesc SD{Address, UINT64_C(-1), StringRef(), 0};
233afb54fd6SFangrui Song   auto SymbolIterator = llvm::upper_bound(Symbols, SD);
234afb54fd6SFangrui Song   if (SymbolIterator == Symbols.begin())
2358df3a07aSAlexey Samsonov     return false;
2368df3a07aSAlexey Samsonov   --SymbolIterator;
23704a2e126SFangrui Song   if (SymbolIterator->Size != 0 &&
23804a2e126SFangrui Song       SymbolIterator->Addr + SymbolIterator->Size <= Address)
2398df3a07aSAlexey Samsonov     return false;
24004a2e126SFangrui Song   Name = SymbolIterator->Name.str();
24104a2e126SFangrui Song   Addr = SymbolIterator->Addr;
24204a2e126SFangrui Song   Size = SymbolIterator->Size;
24304a2e126SFangrui Song 
24404a2e126SFangrui Song   if (SymbolIterator->ELFLocalSymIdx != 0) {
24504a2e126SFangrui Song     // If this is an ELF local symbol, find the STT_FILE symbol preceding
24604a2e126SFangrui Song     // SymbolIterator to get the filename. The ELF spec requires the STT_FILE
24704a2e126SFangrui Song     // symbol (if present) precedes the other STB_LOCAL symbols for the file.
24804a2e126SFangrui Song     assert(Module->isELF());
24904a2e126SFangrui Song     auto It = llvm::upper_bound(
25004a2e126SFangrui Song         FileSymbols,
25104a2e126SFangrui Song         std::make_pair(SymbolIterator->ELFLocalSymIdx, StringRef()));
25204a2e126SFangrui Song     if (It != FileSymbols.begin())
25304a2e126SFangrui Song       FileName = It[-1].second.str();
25404a2e126SFangrui Song   }
2558df3a07aSAlexey Samsonov   return true;
2568df3a07aSAlexey Samsonov }
2578df3a07aSAlexey Samsonov 
shouldOverrideWithSymbolTable(FunctionNameKind FNKind,bool UseSymbolTable) const258c038e2dbSReid Kleckner bool SymbolizableObjectFile::shouldOverrideWithSymbolTable(
259c038e2dbSReid Kleckner     FunctionNameKind FNKind, bool UseSymbolTable) const {
260c038e2dbSReid Kleckner   // When DWARF is used with -gline-tables-only / -gmlt, the symbol table gives
261c038e2dbSReid Kleckner   // better answers for linkage names than the DIContext. Otherwise, we are
262c038e2dbSReid Kleckner   // probably using PEs and PDBs, and we shouldn't do the override. PE files
263c038e2dbSReid Kleckner   // generally only contain the names of exported symbols.
264c038e2dbSReid Kleckner   return FNKind == FunctionNameKind::LinkageName && UseSymbolTable &&
265c038e2dbSReid Kleckner          isa<DWARFContext>(DebugInfoContext.get());
266c038e2dbSReid Kleckner }
267c038e2dbSReid Kleckner 
26877fc1f60SAlexey Lapshin DILineInfo
symbolizeCode(object::SectionedAddress ModuleOffset,DILineInfoSpecifier LineInfoSpecifier,bool UseSymbolTable) const26977fc1f60SAlexey Lapshin SymbolizableObjectFile::symbolizeCode(object::SectionedAddress ModuleOffset,
2705de4ba17SSterling Augustine                                       DILineInfoSpecifier LineInfoSpecifier,
2718df3a07aSAlexey Samsonov                                       bool UseSymbolTable) const {
272b2c4b8bdSAlexey Lapshin   if (ModuleOffset.SectionIndex == object::SectionedAddress::UndefSection)
273b2c4b8bdSAlexey Lapshin     ModuleOffset.SectionIndex =
274b2c4b8bdSAlexey Lapshin         getModuleSectionIndexForAddress(ModuleOffset.Address);
2755de4ba17SSterling Augustine   DILineInfo LineInfo =
2765de4ba17SSterling Augustine       DebugInfoContext->getLineInfoForAddress(ModuleOffset, LineInfoSpecifier);
2770feb6e52SPeter Collingbourne 
2788df3a07aSAlexey Samsonov   // Override function name from symbol table if necessary.
2795de4ba17SSterling Augustine   if (shouldOverrideWithSymbolTable(LineInfoSpecifier.FNKind, UseSymbolTable)) {
28004a2e126SFangrui Song     std::string FunctionName, FileName;
2818df3a07aSAlexey Samsonov     uint64_t Start, Size;
28292ee3dd9SFangrui Song     if (getNameFromSymbolTable(ModuleOffset.Address, FunctionName, Start, Size,
28392ee3dd9SFangrui Song                                FileName)) {
2848df3a07aSAlexey Samsonov       LineInfo.FunctionName = FunctionName;
2854fedb3a6SAlex Orlov       LineInfo.StartAddress = Start;
28604a2e126SFangrui Song       if (LineInfo.FileName == DILineInfo::BadString && !FileName.empty())
28704a2e126SFangrui Song         LineInfo.FileName = FileName;
2888df3a07aSAlexey Samsonov     }
2898df3a07aSAlexey Samsonov   }
2908df3a07aSAlexey Samsonov   return LineInfo;
2918df3a07aSAlexey Samsonov }
2928df3a07aSAlexey Samsonov 
symbolizeInlinedCode(object::SectionedAddress ModuleOffset,DILineInfoSpecifier LineInfoSpecifier,bool UseSymbolTable) const2938df3a07aSAlexey Samsonov DIInliningInfo SymbolizableObjectFile::symbolizeInlinedCode(
2945de4ba17SSterling Augustine     object::SectionedAddress ModuleOffset,
2955de4ba17SSterling Augustine     DILineInfoSpecifier LineInfoSpecifier, bool UseSymbolTable) const {
296b2c4b8bdSAlexey Lapshin   if (ModuleOffset.SectionIndex == object::SectionedAddress::UndefSection)
297b2c4b8bdSAlexey Lapshin     ModuleOffset.SectionIndex =
298b2c4b8bdSAlexey Lapshin         getModuleSectionIndexForAddress(ModuleOffset.Address);
2990feb6e52SPeter Collingbourne   DIInliningInfo InlinedContext = DebugInfoContext->getInliningInfoForAddress(
3005de4ba17SSterling Augustine       ModuleOffset, LineInfoSpecifier);
3010feb6e52SPeter Collingbourne 
3028df3a07aSAlexey Samsonov   // Make sure there is at least one frame in context.
3038df3a07aSAlexey Samsonov   if (InlinedContext.getNumberOfFrames() == 0)
3048df3a07aSAlexey Samsonov     InlinedContext.addFrame(DILineInfo());
3058df3a07aSAlexey Samsonov 
3068df3a07aSAlexey Samsonov   // Override the function name in lower frame with name from symbol table.
3075de4ba17SSterling Augustine   if (shouldOverrideWithSymbolTable(LineInfoSpecifier.FNKind, UseSymbolTable)) {
30804a2e126SFangrui Song     std::string FunctionName, FileName;
3098df3a07aSAlexey Samsonov     uint64_t Start, Size;
31092ee3dd9SFangrui Song     if (getNameFromSymbolTable(ModuleOffset.Address, FunctionName, Start, Size,
31192ee3dd9SFangrui Song                                FileName)) {
31204a2e126SFangrui Song       DILineInfo *LI = InlinedContext.getMutableFrame(
31304a2e126SFangrui Song           InlinedContext.getNumberOfFrames() - 1);
31404a2e126SFangrui Song       LI->FunctionName = FunctionName;
3154fedb3a6SAlex Orlov       LI->StartAddress = Start;
31604a2e126SFangrui Song       if (LI->FileName == DILineInfo::BadString && !FileName.empty())
31704a2e126SFangrui Song         LI->FileName = FileName;
3188df3a07aSAlexey Samsonov     }
3198df3a07aSAlexey Samsonov   }
320e46bd741SAlexey Samsonov 
321e46bd741SAlexey Samsonov   return InlinedContext;
3228df3a07aSAlexey Samsonov }
3238df3a07aSAlexey Samsonov 
symbolizeData(object::SectionedAddress ModuleOffset) const32477fc1f60SAlexey Lapshin DIGlobal SymbolizableObjectFile::symbolizeData(
32577fc1f60SAlexey Lapshin     object::SectionedAddress ModuleOffset) const {
32676f7ecb8SAlexey Samsonov   DIGlobal Res;
32704a2e126SFangrui Song   std::string FileName;
32892ee3dd9SFangrui Song   getNameFromSymbolTable(ModuleOffset.Address, Res.Name, Res.Start, Res.Size,
32992ee3dd9SFangrui Song                          FileName);
330*cead4eceSMitch Phillips   Res.DeclFile = FileName;
331*cead4eceSMitch Phillips 
332*cead4eceSMitch Phillips   // Try and get a better filename:lineno pair from the debuginfo, if present.
333*cead4eceSMitch Phillips   DILineInfo DL = DebugInfoContext->getLineInfoForDataAddress(ModuleOffset);
334*cead4eceSMitch Phillips   if (DL.Line != 0) {
335*cead4eceSMitch Phillips     Res.DeclFile = DL.FileName;
336*cead4eceSMitch Phillips     Res.DeclLine = DL.Line;
337*cead4eceSMitch Phillips   }
33876f7ecb8SAlexey Samsonov   return Res;
3398df3a07aSAlexey Samsonov }
340b2c4b8bdSAlexey Lapshin 
symbolizeFrame(object::SectionedAddress ModuleOffset) const3419c8282a9SPeter Collingbourne std::vector<DILocal> SymbolizableObjectFile::symbolizeFrame(
3429c8282a9SPeter Collingbourne     object::SectionedAddress ModuleOffset) const {
3439c8282a9SPeter Collingbourne   if (ModuleOffset.SectionIndex == object::SectionedAddress::UndefSection)
3449c8282a9SPeter Collingbourne     ModuleOffset.SectionIndex =
3459c8282a9SPeter Collingbourne         getModuleSectionIndexForAddress(ModuleOffset.Address);
3469c8282a9SPeter Collingbourne   return DebugInfoContext->getLocalsForAddress(ModuleOffset);
3479c8282a9SPeter Collingbourne }
3489c8282a9SPeter Collingbourne 
349b2c4b8bdSAlexey Lapshin /// Search for the first occurence of specified Address in ObjectFile.
getModuleSectionIndexForAddress(uint64_t Address) const350b2c4b8bdSAlexey Lapshin uint64_t SymbolizableObjectFile::getModuleSectionIndexForAddress(
351b2c4b8bdSAlexey Lapshin     uint64_t Address) const {
352b2c4b8bdSAlexey Lapshin 
353b2c4b8bdSAlexey Lapshin   for (SectionRef Sec : Module->sections()) {
354b2c4b8bdSAlexey Lapshin     if (!Sec.isText() || Sec.isVirtual())
355b2c4b8bdSAlexey Lapshin       continue;
356b2c4b8bdSAlexey Lapshin 
357b2c4b8bdSAlexey Lapshin     if (Address >= Sec.getAddress() &&
358dd0e8335SFangrui Song         Address < Sec.getAddress() + Sec.getSize())
359b2c4b8bdSAlexey Lapshin       return Sec.getIndex();
360b2c4b8bdSAlexey Lapshin   }
361b2c4b8bdSAlexey Lapshin 
362b2c4b8bdSAlexey Lapshin   return object::SectionedAddress::UndefSection;
363b2c4b8bdSAlexey Lapshin }
364