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 
138df3a07aSAlexey Samsonov #include "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"
1944d95122SEugene Zelenko #include "llvm/Object/ObjectFile.h"
2044d95122SEugene Zelenko #include "llvm/Object/SymbolSize.h"
2144d95122SEugene Zelenko #include "llvm/Support/Casting.h"
2244d95122SEugene Zelenko #include "llvm/Support/DataExtractor.h"
2344d95122SEugene Zelenko #include <algorithm>
248df3a07aSAlexey Samsonov 
2544d95122SEugene Zelenko using namespace llvm;
268df3a07aSAlexey Samsonov using namespace object;
2744d95122SEugene Zelenko using namespace symbolize;
288df3a07aSAlexey Samsonov 
291c03389cSReid Kleckner Expected<std::unique_ptr<SymbolizableObjectFile>>
305de4692cSYuanfang Chen SymbolizableObjectFile::create(const object::ObjectFile *Obj,
31a56d81f4SPeter Collingbourne                                std::unique_ptr<DIContext> DICtx,
32a56d81f4SPeter Collingbourne                                bool UntagAddresses) {
330feb6e52SPeter Collingbourne   assert(DICtx);
348df3a07aSAlexey Samsonov   std::unique_ptr<SymbolizableObjectFile> res(
35a56d81f4SPeter Collingbourne       new SymbolizableObjectFile(Obj, std::move(DICtx), UntagAddresses));
368df3a07aSAlexey Samsonov   std::unique_ptr<DataExtractor> OpdExtractor;
378df3a07aSAlexey Samsonov   uint64_t OpdAddress = 0;
388df3a07aSAlexey Samsonov   // Find the .opd (function descriptor) section if any, for big-endian
398df3a07aSAlexey Samsonov   // PowerPC64 ELF.
408df3a07aSAlexey Samsonov   if (Obj->getArch() == Triple::ppc64) {
418df3a07aSAlexey Samsonov     for (section_iterator Section : Obj->sections()) {
42bcc00e1aSGeorge Rimar       Expected<StringRef> NameOrErr = Section->getName();
43bcc00e1aSGeorge Rimar       if (!NameOrErr)
441c03389cSReid Kleckner         return NameOrErr.takeError();
45bcc00e1aSGeorge Rimar 
46bcc00e1aSGeorge Rimar       if (*NameOrErr == ".opd") {
47e183340cSFangrui Song         Expected<StringRef> E = Section->getContents();
48e183340cSFangrui Song         if (!E)
491c03389cSReid Kleckner           return E.takeError();
50e183340cSFangrui Song         OpdExtractor.reset(new DataExtractor(*E, Obj->isLittleEndian(),
518df3a07aSAlexey Samsonov                                              Obj->getBytesInAddress()));
528df3a07aSAlexey Samsonov         OpdAddress = Section->getAddress();
538df3a07aSAlexey Samsonov         break;
548df3a07aSAlexey Samsonov       }
558df3a07aSAlexey Samsonov     }
568df3a07aSAlexey Samsonov   }
578df3a07aSAlexey Samsonov   std::vector<std::pair<SymbolRef, uint64_t>> Symbols =
588df3a07aSAlexey Samsonov       computeSymbolSizes(*Obj);
598df3a07aSAlexey Samsonov   for (auto &P : Symbols)
601c03389cSReid Kleckner     if (Error E =
611c03389cSReid Kleckner             res->addSymbol(P.first, P.second, OpdExtractor.get(), OpdAddress))
621c03389cSReid Kleckner       return std::move(E);
638df3a07aSAlexey Samsonov 
648df3a07aSAlexey Samsonov   // If this is a COFF object and we didn't find any symbols, try the export
658df3a07aSAlexey Samsonov   // table.
668df3a07aSAlexey Samsonov   if (Symbols.empty()) {
678df3a07aSAlexey Samsonov     if (auto *CoffObj = dyn_cast<COFFObjectFile>(Obj))
681c03389cSReid Kleckner       if (Error E = res->addCoffExportSymbols(CoffObj))
691c03389cSReid Kleckner         return std::move(E);
708df3a07aSAlexey Samsonov   }
71afb54fd6SFangrui Song 
72afb54fd6SFangrui Song   std::vector<std::pair<SymbolDesc, StringRef>> &Fs = res->Functions,
73afb54fd6SFangrui Song                                                 &Os = res->Objects;
74cb300f12SFangrui Song   auto Uniquify = [](std::vector<std::pair<SymbolDesc, StringRef>> &S) {
75cb300f12SFangrui Song     // Sort by (Addr,Size,Name). If several SymbolDescs share the same Addr,
76cb300f12SFangrui Song     // pick the one with the largest Size. This helps us avoid symbols with no
77cb300f12SFangrui Song     // size information (Size=0).
78cb300f12SFangrui Song     llvm::sort(S);
79cb300f12SFangrui Song     auto I = S.begin(), E = S.end(), J = S.begin();
80cb300f12SFangrui Song     while (I != E) {
81cb300f12SFangrui Song       auto OI = I;
82cb300f12SFangrui Song       while (++I != E && OI->first.Addr == I->first.Addr) {
83cb300f12SFangrui Song       }
84cb300f12SFangrui Song       *J++ = I[-1];
85cb300f12SFangrui Song     }
86cb300f12SFangrui Song     S.erase(J, S.end());
87cb300f12SFangrui Song   };
88cb300f12SFangrui Song   Uniquify(Fs);
89cb300f12SFangrui Song   Uniquify(Os);
90afb54fd6SFangrui Song 
91c55cf4afSBill Wendling   return std::move(res);
928df3a07aSAlexey Samsonov }
938df3a07aSAlexey Samsonov 
945de4692cSYuanfang Chen SymbolizableObjectFile::SymbolizableObjectFile(const ObjectFile *Obj,
95a56d81f4SPeter Collingbourne                                                std::unique_ptr<DIContext> DICtx,
96a56d81f4SPeter Collingbourne                                                bool UntagAddresses)
97a56d81f4SPeter Collingbourne     : Module(Obj), DebugInfoContext(std::move(DICtx)),
98a56d81f4SPeter Collingbourne       UntagAddresses(UntagAddresses) {}
998df3a07aSAlexey Samsonov 
1008df3a07aSAlexey Samsonov namespace {
10144d95122SEugene Zelenko 
1028df3a07aSAlexey Samsonov struct OffsetNamePair {
1038df3a07aSAlexey Samsonov   uint32_t Offset;
1048df3a07aSAlexey Samsonov   StringRef Name;
10544d95122SEugene Zelenko 
1068df3a07aSAlexey Samsonov   bool operator<(const OffsetNamePair &R) const {
1078df3a07aSAlexey Samsonov     return Offset < R.Offset;
1088df3a07aSAlexey Samsonov   }
1098df3a07aSAlexey Samsonov };
11044d95122SEugene Zelenko 
11144d95122SEugene Zelenko } // end anonymous namespace
1128df3a07aSAlexey Samsonov 
1131c03389cSReid Kleckner Error SymbolizableObjectFile::addCoffExportSymbols(
1148df3a07aSAlexey Samsonov     const COFFObjectFile *CoffObj) {
1158df3a07aSAlexey Samsonov   // Get all export names and offsets.
1168df3a07aSAlexey Samsonov   std::vector<OffsetNamePair> ExportSyms;
1178df3a07aSAlexey Samsonov   for (const ExportDirectoryEntryRef &Ref : CoffObj->export_directories()) {
1188df3a07aSAlexey Samsonov     StringRef Name;
1198df3a07aSAlexey Samsonov     uint32_t Offset;
1208df3a07aSAlexey Samsonov     if (auto EC = Ref.getSymbolName(Name))
1218df3a07aSAlexey Samsonov       return EC;
1228df3a07aSAlexey Samsonov     if (auto EC = Ref.getExportRVA(Offset))
1238df3a07aSAlexey Samsonov       return EC;
1248df3a07aSAlexey Samsonov     ExportSyms.push_back(OffsetNamePair{Offset, Name});
1258df3a07aSAlexey Samsonov   }
1268df3a07aSAlexey Samsonov   if (ExportSyms.empty())
1271c03389cSReid Kleckner     return Error::success();
1288df3a07aSAlexey Samsonov 
1298df3a07aSAlexey Samsonov   // Sort by ascending offset.
1308df3a07aSAlexey Samsonov   array_pod_sort(ExportSyms.begin(), ExportSyms.end());
1318df3a07aSAlexey Samsonov 
1328df3a07aSAlexey Samsonov   // Approximate the symbol sizes by assuming they run to the next symbol.
1338df3a07aSAlexey Samsonov   // FIXME: This assumes all exports are functions.
1348df3a07aSAlexey Samsonov   uint64_t ImageBase = CoffObj->getImageBase();
1358df3a07aSAlexey Samsonov   for (auto I = ExportSyms.begin(), E = ExportSyms.end(); I != E; ++I) {
1368df3a07aSAlexey Samsonov     OffsetNamePair &Export = *I;
1378df3a07aSAlexey Samsonov     // FIXME: The last export has a one byte size now.
1388df3a07aSAlexey Samsonov     uint32_t NextOffset = I != E ? I->Offset : Export.Offset + 1;
1398df3a07aSAlexey Samsonov     uint64_t SymbolStart = ImageBase + Export.Offset;
1408df3a07aSAlexey Samsonov     uint64_t SymbolSize = NextOffset - Export.Offset;
1418df3a07aSAlexey Samsonov     SymbolDesc SD = {SymbolStart, SymbolSize};
142afb54fd6SFangrui Song     Functions.emplace_back(SD, Export.Name);
1438df3a07aSAlexey Samsonov   }
1441c03389cSReid Kleckner   return Error::success();
1458df3a07aSAlexey Samsonov }
1468df3a07aSAlexey Samsonov 
1471c03389cSReid Kleckner Error SymbolizableObjectFile::addSymbol(const SymbolRef &Symbol,
1488df3a07aSAlexey Samsonov                                         uint64_t SymbolSize,
1498df3a07aSAlexey Samsonov                                         DataExtractor *OpdExtractor,
1508df3a07aSAlexey Samsonov                                         uint64_t OpdAddress) {
151123be5d4SMatt Davis   // Avoid adding symbols from an unknown/undefined section.
152*6d766c8bSFangrui Song   const ObjectFile &Obj = *Symbol.getObject();
153123be5d4SMatt Davis   Expected<section_iterator> Sec = Symbol.getSection();
154*6d766c8bSFangrui Song   if (!Sec || Obj.section_end() == *Sec)
1551c03389cSReid Kleckner     return Error::success();
156*6d766c8bSFangrui Song 
1577bd8d994SKevin Enderby   Expected<SymbolRef::Type> SymbolTypeOrErr = Symbol.getType();
1587bd8d994SKevin Enderby   if (!SymbolTypeOrErr)
1591c03389cSReid Kleckner     return SymbolTypeOrErr.takeError();
1605afbc1cdSKevin Enderby   SymbolRef::Type SymbolType = *SymbolTypeOrErr;
161*6d766c8bSFangrui Song   if (Obj.isELF()) {
162*6d766c8bSFangrui Song     // Allow function and data symbols. Additionally allow STT_NONE, which are
163*6d766c8bSFangrui Song     // common for functions defined in assembly.
164*6d766c8bSFangrui Song     uint8_t Type = ELFSymbolRef(Symbol).getELFType();
165*6d766c8bSFangrui Song     if (Type != ELF::STT_NOTYPE && Type != ELF::STT_FUNC &&
166*6d766c8bSFangrui Song         Type != ELF::STT_OBJECT && Type != ELF::STT_GNU_IFUNC)
1671c03389cSReid Kleckner       return Error::success();
168*6d766c8bSFangrui Song   } else if (SymbolType != SymbolRef::ST_Function &&
169*6d766c8bSFangrui Song              SymbolType != SymbolRef::ST_Data) {
170*6d766c8bSFangrui Song     return Error::success();
171*6d766c8bSFangrui Song   }
172*6d766c8bSFangrui Song 
173931cb65dSKevin Enderby   Expected<uint64_t> SymbolAddressOrErr = Symbol.getAddress();
174931cb65dSKevin Enderby   if (!SymbolAddressOrErr)
1751c03389cSReid Kleckner     return SymbolAddressOrErr.takeError();
1768df3a07aSAlexey Samsonov   uint64_t SymbolAddress = *SymbolAddressOrErr;
177a56d81f4SPeter Collingbourne   if (UntagAddresses) {
178a56d81f4SPeter Collingbourne     // For kernel addresses, bits 56-63 need to be set, so we sign extend bit 55
179a56d81f4SPeter Collingbourne     // into bits 56-63 instead of masking them out.
180f0380bacSPeter Collingbourne     SymbolAddress &= (1ull << 56) - 1;
181a56d81f4SPeter Collingbourne     SymbolAddress = (int64_t(SymbolAddress) << 8) >> 8;
182a56d81f4SPeter Collingbourne   }
1838df3a07aSAlexey Samsonov   if (OpdExtractor) {
1848df3a07aSAlexey Samsonov     // For big-endian PowerPC64 ELF, symbols in the .opd section refer to
1858df3a07aSAlexey Samsonov     // function descriptors. The first word of the descriptor is a pointer to
1868df3a07aSAlexey Samsonov     // the function's code.
1878df3a07aSAlexey Samsonov     // For the purposes of symbolization, pretend the symbol's address is that
1888df3a07aSAlexey Samsonov     // of the function's code, not the descriptor.
1898df3a07aSAlexey Samsonov     uint64_t OpdOffset = SymbolAddress - OpdAddress;
190f26a70a5SIgor Kudrin     if (OpdExtractor->isValidOffsetForAddress(OpdOffset))
191f26a70a5SIgor Kudrin       SymbolAddress = OpdExtractor->getAddress(&OpdOffset);
1928df3a07aSAlexey Samsonov   }
19381e8b7d9SKevin Enderby   Expected<StringRef> SymbolNameOrErr = Symbol.getName();
19481e8b7d9SKevin Enderby   if (!SymbolNameOrErr)
1951c03389cSReid Kleckner     return SymbolNameOrErr.takeError();
1968df3a07aSAlexey Samsonov   StringRef SymbolName = *SymbolNameOrErr;
1978df3a07aSAlexey Samsonov   // Mach-O symbol table names have leading underscore, skip it.
19844d95122SEugene Zelenko   if (Module->isMachO() && !SymbolName.empty() && SymbolName[0] == '_')
1998df3a07aSAlexey Samsonov     SymbolName = SymbolName.drop_front();
200*6d766c8bSFangrui Song 
2018df3a07aSAlexey Samsonov   SymbolDesc SD = {SymbolAddress, SymbolSize};
202*6d766c8bSFangrui Song 
203*6d766c8bSFangrui Song   // DATA command symbolizes just ST_Data (ELF STT_OBJECT) symbols as an
204*6d766c8bSFangrui Song   // optimization. Treat everything else (e.g. ELF STT_NOTYPE, STT_FUNC and
205*6d766c8bSFangrui Song   // STT_GNU_IFUNC) as function symbols which can be used to symbolize
206*6d766c8bSFangrui Song   // addresses.
207*6d766c8bSFangrui Song   if (SymbolType == SymbolRef::ST_Data)
208*6d766c8bSFangrui Song     Objects.emplace_back(SD, SymbolName);
209*6d766c8bSFangrui Song   else
210*6d766c8bSFangrui Song     Functions.emplace_back(SD, SymbolName);
2111c03389cSReid Kleckner   return Error::success();
2128df3a07aSAlexey Samsonov }
2138df3a07aSAlexey Samsonov 
2148df3a07aSAlexey Samsonov // Return true if this is a 32-bit x86 PE COFF module.
2158df3a07aSAlexey Samsonov bool SymbolizableObjectFile::isWin32Module() const {
2168df3a07aSAlexey Samsonov   auto *CoffObject = dyn_cast<COFFObjectFile>(Module);
2178df3a07aSAlexey Samsonov   return CoffObject && CoffObject->getMachine() == COFF::IMAGE_FILE_MACHINE_I386;
2188df3a07aSAlexey Samsonov }
2198df3a07aSAlexey Samsonov 
2208df3a07aSAlexey Samsonov uint64_t SymbolizableObjectFile::getModulePreferredBase() const {
2218df3a07aSAlexey Samsonov   if (auto *CoffObject = dyn_cast<COFFObjectFile>(Module))
2228df3a07aSAlexey Samsonov     return CoffObject->getImageBase();
2238df3a07aSAlexey Samsonov   return 0;
2248df3a07aSAlexey Samsonov }
2258df3a07aSAlexey Samsonov 
2268df3a07aSAlexey Samsonov bool SymbolizableObjectFile::getNameFromSymbolTable(SymbolRef::Type Type,
2278df3a07aSAlexey Samsonov                                                     uint64_t Address,
2288df3a07aSAlexey Samsonov                                                     std::string &Name,
2298df3a07aSAlexey Samsonov                                                     uint64_t &Addr,
2308df3a07aSAlexey Samsonov                                                     uint64_t &Size) const {
231afb54fd6SFangrui Song   const auto &Symbols = Type == SymbolRef::ST_Function ? Functions : Objects;
232afb54fd6SFangrui Song   std::pair<SymbolDesc, StringRef> SD{{Address, UINT64_C(-1)}, StringRef()};
233afb54fd6SFangrui Song   auto SymbolIterator = llvm::upper_bound(Symbols, SD);
234afb54fd6SFangrui Song   if (SymbolIterator == Symbols.begin())
2358df3a07aSAlexey Samsonov     return false;
2368df3a07aSAlexey Samsonov   --SymbolIterator;
2378df3a07aSAlexey Samsonov   if (SymbolIterator->first.Size != 0 &&
2388df3a07aSAlexey Samsonov       SymbolIterator->first.Addr + SymbolIterator->first.Size <= Address)
2398df3a07aSAlexey Samsonov     return false;
2408df3a07aSAlexey Samsonov   Name = SymbolIterator->second.str();
2418df3a07aSAlexey Samsonov   Addr = SymbolIterator->first.Addr;
2428df3a07aSAlexey Samsonov   Size = SymbolIterator->first.Size;
2438df3a07aSAlexey Samsonov   return true;
2448df3a07aSAlexey Samsonov }
2458df3a07aSAlexey Samsonov 
246c038e2dbSReid Kleckner bool SymbolizableObjectFile::shouldOverrideWithSymbolTable(
247c038e2dbSReid Kleckner     FunctionNameKind FNKind, bool UseSymbolTable) const {
248c038e2dbSReid Kleckner   // When DWARF is used with -gline-tables-only / -gmlt, the symbol table gives
249c038e2dbSReid Kleckner   // better answers for linkage names than the DIContext. Otherwise, we are
250c038e2dbSReid Kleckner   // probably using PEs and PDBs, and we shouldn't do the override. PE files
251c038e2dbSReid Kleckner   // generally only contain the names of exported symbols.
252c038e2dbSReid Kleckner   return FNKind == FunctionNameKind::LinkageName && UseSymbolTable &&
253c038e2dbSReid Kleckner          isa<DWARFContext>(DebugInfoContext.get());
254c038e2dbSReid Kleckner }
255c038e2dbSReid Kleckner 
25677fc1f60SAlexey Lapshin DILineInfo
25777fc1f60SAlexey Lapshin SymbolizableObjectFile::symbolizeCode(object::SectionedAddress ModuleOffset,
2585de4ba17SSterling Augustine                                       DILineInfoSpecifier LineInfoSpecifier,
2598df3a07aSAlexey Samsonov                                       bool UseSymbolTable) const {
260b2c4b8bdSAlexey Lapshin   if (ModuleOffset.SectionIndex == object::SectionedAddress::UndefSection)
261b2c4b8bdSAlexey Lapshin     ModuleOffset.SectionIndex =
262b2c4b8bdSAlexey Lapshin         getModuleSectionIndexForAddress(ModuleOffset.Address);
2635de4ba17SSterling Augustine   DILineInfo LineInfo =
2645de4ba17SSterling Augustine       DebugInfoContext->getLineInfoForAddress(ModuleOffset, LineInfoSpecifier);
2650feb6e52SPeter Collingbourne 
2668df3a07aSAlexey Samsonov   // Override function name from symbol table if necessary.
2675de4ba17SSterling Augustine   if (shouldOverrideWithSymbolTable(LineInfoSpecifier.FNKind, UseSymbolTable)) {
2688df3a07aSAlexey Samsonov     std::string FunctionName;
2698df3a07aSAlexey Samsonov     uint64_t Start, Size;
27077fc1f60SAlexey Lapshin     if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset.Address,
2718df3a07aSAlexey Samsonov                                FunctionName, Start, Size)) {
2728df3a07aSAlexey Samsonov       LineInfo.FunctionName = FunctionName;
2738df3a07aSAlexey Samsonov     }
2748df3a07aSAlexey Samsonov   }
2758df3a07aSAlexey Samsonov   return LineInfo;
2768df3a07aSAlexey Samsonov }
2778df3a07aSAlexey Samsonov 
2788df3a07aSAlexey Samsonov DIInliningInfo SymbolizableObjectFile::symbolizeInlinedCode(
2795de4ba17SSterling Augustine     object::SectionedAddress ModuleOffset,
2805de4ba17SSterling Augustine     DILineInfoSpecifier LineInfoSpecifier, bool UseSymbolTable) const {
281b2c4b8bdSAlexey Lapshin   if (ModuleOffset.SectionIndex == object::SectionedAddress::UndefSection)
282b2c4b8bdSAlexey Lapshin     ModuleOffset.SectionIndex =
283b2c4b8bdSAlexey Lapshin         getModuleSectionIndexForAddress(ModuleOffset.Address);
2840feb6e52SPeter Collingbourne   DIInliningInfo InlinedContext = DebugInfoContext->getInliningInfoForAddress(
2855de4ba17SSterling Augustine       ModuleOffset, LineInfoSpecifier);
2860feb6e52SPeter Collingbourne 
2878df3a07aSAlexey Samsonov   // Make sure there is at least one frame in context.
2888df3a07aSAlexey Samsonov   if (InlinedContext.getNumberOfFrames() == 0)
2898df3a07aSAlexey Samsonov     InlinedContext.addFrame(DILineInfo());
2908df3a07aSAlexey Samsonov 
2918df3a07aSAlexey Samsonov   // Override the function name in lower frame with name from symbol table.
2925de4ba17SSterling Augustine   if (shouldOverrideWithSymbolTable(LineInfoSpecifier.FNKind, UseSymbolTable)) {
2938df3a07aSAlexey Samsonov     std::string FunctionName;
2948df3a07aSAlexey Samsonov     uint64_t Start, Size;
29577fc1f60SAlexey Lapshin     if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset.Address,
2968df3a07aSAlexey Samsonov                                FunctionName, Start, Size)) {
297e46bd741SAlexey Samsonov       InlinedContext.getMutableFrame(InlinedContext.getNumberOfFrames() - 1)
298e46bd741SAlexey Samsonov           ->FunctionName = FunctionName;
2998df3a07aSAlexey Samsonov     }
3008df3a07aSAlexey Samsonov   }
301e46bd741SAlexey Samsonov 
302e46bd741SAlexey Samsonov   return InlinedContext;
3038df3a07aSAlexey Samsonov }
3048df3a07aSAlexey Samsonov 
30577fc1f60SAlexey Lapshin DIGlobal SymbolizableObjectFile::symbolizeData(
30677fc1f60SAlexey Lapshin     object::SectionedAddress ModuleOffset) const {
30776f7ecb8SAlexey Samsonov   DIGlobal Res;
30877fc1f60SAlexey Lapshin   getNameFromSymbolTable(SymbolRef::ST_Data, ModuleOffset.Address, Res.Name,
30977fc1f60SAlexey Lapshin                          Res.Start, Res.Size);
31076f7ecb8SAlexey Samsonov   return Res;
3118df3a07aSAlexey Samsonov }
312b2c4b8bdSAlexey Lapshin 
3139c8282a9SPeter Collingbourne std::vector<DILocal> SymbolizableObjectFile::symbolizeFrame(
3149c8282a9SPeter Collingbourne     object::SectionedAddress ModuleOffset) const {
3159c8282a9SPeter Collingbourne   if (ModuleOffset.SectionIndex == object::SectionedAddress::UndefSection)
3169c8282a9SPeter Collingbourne     ModuleOffset.SectionIndex =
3179c8282a9SPeter Collingbourne         getModuleSectionIndexForAddress(ModuleOffset.Address);
3189c8282a9SPeter Collingbourne   return DebugInfoContext->getLocalsForAddress(ModuleOffset);
3199c8282a9SPeter Collingbourne }
3209c8282a9SPeter Collingbourne 
321b2c4b8bdSAlexey Lapshin /// Search for the first occurence of specified Address in ObjectFile.
322b2c4b8bdSAlexey Lapshin uint64_t SymbolizableObjectFile::getModuleSectionIndexForAddress(
323b2c4b8bdSAlexey Lapshin     uint64_t Address) const {
324b2c4b8bdSAlexey Lapshin 
325b2c4b8bdSAlexey Lapshin   for (SectionRef Sec : Module->sections()) {
326b2c4b8bdSAlexey Lapshin     if (!Sec.isText() || Sec.isVirtual())
327b2c4b8bdSAlexey Lapshin       continue;
328b2c4b8bdSAlexey Lapshin 
329b2c4b8bdSAlexey Lapshin     if (Address >= Sec.getAddress() &&
330dd0e8335SFangrui Song         Address < Sec.getAddress() + Sec.getSize())
331b2c4b8bdSAlexey Lapshin       return Sec.getIndex();
332b2c4b8bdSAlexey Lapshin   }
333b2c4b8bdSAlexey Lapshin 
334b2c4b8bdSAlexey Lapshin   return object::SectionedAddress::UndefSection;
335b2c4b8bdSAlexey Lapshin }
336