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/StringRef.h"
1644d95122SEugene Zelenko #include "llvm/ADT/Triple.h"
17264b5d9eSZachary Turner #include "llvm/BinaryFormat/COFF.h"
18c038e2dbSReid Kleckner #include "llvm/DebugInfo/DWARF/DWARFContext.h"
1944d95122SEugene Zelenko #include "llvm/DebugInfo/Symbolize/SymbolizableModule.h"
2044d95122SEugene Zelenko #include "llvm/Object/COFF.h"
2144d95122SEugene Zelenko #include "llvm/Object/ObjectFile.h"
2244d95122SEugene Zelenko #include "llvm/Object/SymbolSize.h"
2344d95122SEugene Zelenko #include "llvm/Support/Casting.h"
2444d95122SEugene Zelenko #include "llvm/Support/DataExtractor.h"
2544d95122SEugene Zelenko #include "llvm/Support/Error.h"
2644d95122SEugene Zelenko #include <algorithm>
2744d95122SEugene Zelenko #include <cstdint>
2844d95122SEugene Zelenko #include <memory>
2944d95122SEugene Zelenko #include <string>
3044d95122SEugene Zelenko #include <system_error>
3144d95122SEugene Zelenko #include <utility>
3244d95122SEugene Zelenko #include <vector>
338df3a07aSAlexey Samsonov 
3444d95122SEugene Zelenko using namespace llvm;
358df3a07aSAlexey Samsonov using namespace object;
3644d95122SEugene Zelenko using namespace symbolize;
378df3a07aSAlexey Samsonov 
38*1c03389cSReid Kleckner Expected<std::unique_ptr<SymbolizableObjectFile>>
395de4692cSYuanfang Chen SymbolizableObjectFile::create(const object::ObjectFile *Obj,
40a56d81f4SPeter Collingbourne                                std::unique_ptr<DIContext> DICtx,
41a56d81f4SPeter Collingbourne                                bool UntagAddresses) {
420feb6e52SPeter Collingbourne   assert(DICtx);
438df3a07aSAlexey Samsonov   std::unique_ptr<SymbolizableObjectFile> res(
44a56d81f4SPeter Collingbourne       new SymbolizableObjectFile(Obj, std::move(DICtx), UntagAddresses));
458df3a07aSAlexey Samsonov   std::unique_ptr<DataExtractor> OpdExtractor;
468df3a07aSAlexey Samsonov   uint64_t OpdAddress = 0;
478df3a07aSAlexey Samsonov   // Find the .opd (function descriptor) section if any, for big-endian
488df3a07aSAlexey Samsonov   // PowerPC64 ELF.
498df3a07aSAlexey Samsonov   if (Obj->getArch() == Triple::ppc64) {
508df3a07aSAlexey Samsonov     for (section_iterator Section : Obj->sections()) {
51bcc00e1aSGeorge Rimar       Expected<StringRef> NameOrErr = Section->getName();
52bcc00e1aSGeorge Rimar       if (!NameOrErr)
53*1c03389cSReid Kleckner         return NameOrErr.takeError();
54bcc00e1aSGeorge Rimar 
55bcc00e1aSGeorge Rimar       if (*NameOrErr == ".opd") {
56e183340cSFangrui Song         Expected<StringRef> E = Section->getContents();
57e183340cSFangrui Song         if (!E)
58*1c03389cSReid Kleckner           return E.takeError();
59e183340cSFangrui Song         OpdExtractor.reset(new DataExtractor(*E, Obj->isLittleEndian(),
608df3a07aSAlexey Samsonov                                              Obj->getBytesInAddress()));
618df3a07aSAlexey Samsonov         OpdAddress = Section->getAddress();
628df3a07aSAlexey Samsonov         break;
638df3a07aSAlexey Samsonov       }
648df3a07aSAlexey Samsonov     }
658df3a07aSAlexey Samsonov   }
668df3a07aSAlexey Samsonov   std::vector<std::pair<SymbolRef, uint64_t>> Symbols =
678df3a07aSAlexey Samsonov       computeSymbolSizes(*Obj);
688df3a07aSAlexey Samsonov   for (auto &P : Symbols)
69*1c03389cSReid Kleckner     if (Error E =
70*1c03389cSReid Kleckner             res->addSymbol(P.first, P.second, OpdExtractor.get(), OpdAddress))
71*1c03389cSReid Kleckner       return std::move(E);
728df3a07aSAlexey Samsonov 
738df3a07aSAlexey Samsonov   // If this is a COFF object and we didn't find any symbols, try the export
748df3a07aSAlexey Samsonov   // table.
758df3a07aSAlexey Samsonov   if (Symbols.empty()) {
768df3a07aSAlexey Samsonov     if (auto *CoffObj = dyn_cast<COFFObjectFile>(Obj))
77*1c03389cSReid Kleckner       if (Error E = res->addCoffExportSymbols(CoffObj))
78*1c03389cSReid Kleckner         return std::move(E);
798df3a07aSAlexey Samsonov   }
80afb54fd6SFangrui Song 
81afb54fd6SFangrui Song   std::vector<std::pair<SymbolDesc, StringRef>> &Fs = res->Functions,
82afb54fd6SFangrui Song                                                 &Os = res->Objects;
83cb300f12SFangrui Song   auto Uniquify = [](std::vector<std::pair<SymbolDesc, StringRef>> &S) {
84cb300f12SFangrui Song     // Sort by (Addr,Size,Name). If several SymbolDescs share the same Addr,
85cb300f12SFangrui Song     // pick the one with the largest Size. This helps us avoid symbols with no
86cb300f12SFangrui Song     // size information (Size=0).
87cb300f12SFangrui Song     llvm::sort(S);
88cb300f12SFangrui Song     auto I = S.begin(), E = S.end(), J = S.begin();
89cb300f12SFangrui Song     while (I != E) {
90cb300f12SFangrui Song       auto OI = I;
91cb300f12SFangrui Song       while (++I != E && OI->first.Addr == I->first.Addr) {
92cb300f12SFangrui Song       }
93cb300f12SFangrui Song       *J++ = I[-1];
94cb300f12SFangrui Song     }
95cb300f12SFangrui Song     S.erase(J, S.end());
96cb300f12SFangrui Song   };
97cb300f12SFangrui Song   Uniquify(Fs);
98cb300f12SFangrui Song   Uniquify(Os);
99afb54fd6SFangrui Song 
100c55cf4afSBill Wendling   return std::move(res);
1018df3a07aSAlexey Samsonov }
1028df3a07aSAlexey Samsonov 
1035de4692cSYuanfang Chen SymbolizableObjectFile::SymbolizableObjectFile(const ObjectFile *Obj,
104a56d81f4SPeter Collingbourne                                                std::unique_ptr<DIContext> DICtx,
105a56d81f4SPeter Collingbourne                                                bool UntagAddresses)
106a56d81f4SPeter Collingbourne     : Module(Obj), DebugInfoContext(std::move(DICtx)),
107a56d81f4SPeter Collingbourne       UntagAddresses(UntagAddresses) {}
1088df3a07aSAlexey Samsonov 
1098df3a07aSAlexey Samsonov namespace {
11044d95122SEugene Zelenko 
1118df3a07aSAlexey Samsonov struct OffsetNamePair {
1128df3a07aSAlexey Samsonov   uint32_t Offset;
1138df3a07aSAlexey Samsonov   StringRef Name;
11444d95122SEugene Zelenko 
1158df3a07aSAlexey Samsonov   bool operator<(const OffsetNamePair &R) const {
1168df3a07aSAlexey Samsonov     return Offset < R.Offset;
1178df3a07aSAlexey Samsonov   }
1188df3a07aSAlexey Samsonov };
11944d95122SEugene Zelenko 
12044d95122SEugene Zelenko } // end anonymous namespace
1218df3a07aSAlexey Samsonov 
122*1c03389cSReid Kleckner Error SymbolizableObjectFile::addCoffExportSymbols(
1238df3a07aSAlexey Samsonov     const COFFObjectFile *CoffObj) {
1248df3a07aSAlexey Samsonov   // Get all export names and offsets.
1258df3a07aSAlexey Samsonov   std::vector<OffsetNamePair> ExportSyms;
1268df3a07aSAlexey Samsonov   for (const ExportDirectoryEntryRef &Ref : CoffObj->export_directories()) {
1278df3a07aSAlexey Samsonov     StringRef Name;
1288df3a07aSAlexey Samsonov     uint32_t Offset;
1298df3a07aSAlexey Samsonov     if (auto EC = Ref.getSymbolName(Name))
1308df3a07aSAlexey Samsonov       return EC;
1318df3a07aSAlexey Samsonov     if (auto EC = Ref.getExportRVA(Offset))
1328df3a07aSAlexey Samsonov       return EC;
1338df3a07aSAlexey Samsonov     ExportSyms.push_back(OffsetNamePair{Offset, Name});
1348df3a07aSAlexey Samsonov   }
1358df3a07aSAlexey Samsonov   if (ExportSyms.empty())
136*1c03389cSReid Kleckner     return Error::success();
1378df3a07aSAlexey Samsonov 
1388df3a07aSAlexey Samsonov   // Sort by ascending offset.
1398df3a07aSAlexey Samsonov   array_pod_sort(ExportSyms.begin(), ExportSyms.end());
1408df3a07aSAlexey Samsonov 
1418df3a07aSAlexey Samsonov   // Approximate the symbol sizes by assuming they run to the next symbol.
1428df3a07aSAlexey Samsonov   // FIXME: This assumes all exports are functions.
1438df3a07aSAlexey Samsonov   uint64_t ImageBase = CoffObj->getImageBase();
1448df3a07aSAlexey Samsonov   for (auto I = ExportSyms.begin(), E = ExportSyms.end(); I != E; ++I) {
1458df3a07aSAlexey Samsonov     OffsetNamePair &Export = *I;
1468df3a07aSAlexey Samsonov     // FIXME: The last export has a one byte size now.
1478df3a07aSAlexey Samsonov     uint32_t NextOffset = I != E ? I->Offset : Export.Offset + 1;
1488df3a07aSAlexey Samsonov     uint64_t SymbolStart = ImageBase + Export.Offset;
1498df3a07aSAlexey Samsonov     uint64_t SymbolSize = NextOffset - Export.Offset;
1508df3a07aSAlexey Samsonov     SymbolDesc SD = {SymbolStart, SymbolSize};
151afb54fd6SFangrui Song     Functions.emplace_back(SD, Export.Name);
1528df3a07aSAlexey Samsonov   }
153*1c03389cSReid Kleckner   return Error::success();
1548df3a07aSAlexey Samsonov }
1558df3a07aSAlexey Samsonov 
156*1c03389cSReid Kleckner Error SymbolizableObjectFile::addSymbol(const SymbolRef &Symbol,
1578df3a07aSAlexey Samsonov                                         uint64_t SymbolSize,
1588df3a07aSAlexey Samsonov                                         DataExtractor *OpdExtractor,
1598df3a07aSAlexey Samsonov                                         uint64_t OpdAddress) {
160123be5d4SMatt Davis   // Avoid adding symbols from an unknown/undefined section.
161123be5d4SMatt Davis   const ObjectFile *Obj = Symbol.getObject();
162123be5d4SMatt Davis   Expected<section_iterator> Sec = Symbol.getSection();
163123be5d4SMatt Davis   if (!Sec || (Obj && Obj->section_end() == *Sec))
164*1c03389cSReid Kleckner     return Error::success();
1657bd8d994SKevin Enderby   Expected<SymbolRef::Type> SymbolTypeOrErr = Symbol.getType();
1667bd8d994SKevin Enderby   if (!SymbolTypeOrErr)
167*1c03389cSReid Kleckner     return SymbolTypeOrErr.takeError();
1685afbc1cdSKevin Enderby   SymbolRef::Type SymbolType = *SymbolTypeOrErr;
1698df3a07aSAlexey Samsonov   if (SymbolType != SymbolRef::ST_Function && SymbolType != SymbolRef::ST_Data)
170*1c03389cSReid Kleckner     return Error::success();
171931cb65dSKevin Enderby   Expected<uint64_t> SymbolAddressOrErr = Symbol.getAddress();
172931cb65dSKevin Enderby   if (!SymbolAddressOrErr)
173*1c03389cSReid Kleckner     return SymbolAddressOrErr.takeError();
1748df3a07aSAlexey Samsonov   uint64_t SymbolAddress = *SymbolAddressOrErr;
175a56d81f4SPeter Collingbourne   if (UntagAddresses) {
176a56d81f4SPeter Collingbourne     // For kernel addresses, bits 56-63 need to be set, so we sign extend bit 55
177a56d81f4SPeter Collingbourne     // into bits 56-63 instead of masking them out.
178f0380bacSPeter Collingbourne     SymbolAddress &= (1ull << 56) - 1;
179a56d81f4SPeter Collingbourne     SymbolAddress = (int64_t(SymbolAddress) << 8) >> 8;
180a56d81f4SPeter Collingbourne   }
1818df3a07aSAlexey Samsonov   if (OpdExtractor) {
1828df3a07aSAlexey Samsonov     // For big-endian PowerPC64 ELF, symbols in the .opd section refer to
1838df3a07aSAlexey Samsonov     // function descriptors. The first word of the descriptor is a pointer to
1848df3a07aSAlexey Samsonov     // the function's code.
1858df3a07aSAlexey Samsonov     // For the purposes of symbolization, pretend the symbol's address is that
1868df3a07aSAlexey Samsonov     // of the function's code, not the descriptor.
1878df3a07aSAlexey Samsonov     uint64_t OpdOffset = SymbolAddress - OpdAddress;
188f26a70a5SIgor Kudrin     if (OpdExtractor->isValidOffsetForAddress(OpdOffset))
189f26a70a5SIgor Kudrin       SymbolAddress = OpdExtractor->getAddress(&OpdOffset);
1908df3a07aSAlexey Samsonov   }
19181e8b7d9SKevin Enderby   Expected<StringRef> SymbolNameOrErr = Symbol.getName();
19281e8b7d9SKevin Enderby   if (!SymbolNameOrErr)
193*1c03389cSReid Kleckner     return SymbolNameOrErr.takeError();
1948df3a07aSAlexey Samsonov   StringRef SymbolName = *SymbolNameOrErr;
1958df3a07aSAlexey Samsonov   // Mach-O symbol table names have leading underscore, skip it.
19644d95122SEugene Zelenko   if (Module->isMachO() && !SymbolName.empty() && SymbolName[0] == '_')
1978df3a07aSAlexey Samsonov     SymbolName = SymbolName.drop_front();
1988df3a07aSAlexey Samsonov   // FIXME: If a function has alias, there are two entries in symbol table
1998df3a07aSAlexey Samsonov   // with same address size. Make sure we choose the correct one.
2008df3a07aSAlexey Samsonov   auto &M = SymbolType == SymbolRef::ST_Function ? Functions : Objects;
2018df3a07aSAlexey Samsonov   SymbolDesc SD = { SymbolAddress, SymbolSize };
202afb54fd6SFangrui Song   M.emplace_back(SD, SymbolName);
203*1c03389cSReid Kleckner   return Error::success();
2048df3a07aSAlexey Samsonov }
2058df3a07aSAlexey Samsonov 
2068df3a07aSAlexey Samsonov // Return true if this is a 32-bit x86 PE COFF module.
2078df3a07aSAlexey Samsonov bool SymbolizableObjectFile::isWin32Module() const {
2088df3a07aSAlexey Samsonov   auto *CoffObject = dyn_cast<COFFObjectFile>(Module);
2098df3a07aSAlexey Samsonov   return CoffObject && CoffObject->getMachine() == COFF::IMAGE_FILE_MACHINE_I386;
2108df3a07aSAlexey Samsonov }
2118df3a07aSAlexey Samsonov 
2128df3a07aSAlexey Samsonov uint64_t SymbolizableObjectFile::getModulePreferredBase() const {
2138df3a07aSAlexey Samsonov   if (auto *CoffObject = dyn_cast<COFFObjectFile>(Module))
2148df3a07aSAlexey Samsonov     return CoffObject->getImageBase();
2158df3a07aSAlexey Samsonov   return 0;
2168df3a07aSAlexey Samsonov }
2178df3a07aSAlexey Samsonov 
2188df3a07aSAlexey Samsonov bool SymbolizableObjectFile::getNameFromSymbolTable(SymbolRef::Type Type,
2198df3a07aSAlexey Samsonov                                                     uint64_t Address,
2208df3a07aSAlexey Samsonov                                                     std::string &Name,
2218df3a07aSAlexey Samsonov                                                     uint64_t &Addr,
2228df3a07aSAlexey Samsonov                                                     uint64_t &Size) const {
223afb54fd6SFangrui Song   const auto &Symbols = Type == SymbolRef::ST_Function ? Functions : Objects;
224afb54fd6SFangrui Song   std::pair<SymbolDesc, StringRef> SD{{Address, UINT64_C(-1)}, StringRef()};
225afb54fd6SFangrui Song   auto SymbolIterator = llvm::upper_bound(Symbols, SD);
226afb54fd6SFangrui Song   if (SymbolIterator == Symbols.begin())
2278df3a07aSAlexey Samsonov     return false;
2288df3a07aSAlexey Samsonov   --SymbolIterator;
2298df3a07aSAlexey Samsonov   if (SymbolIterator->first.Size != 0 &&
2308df3a07aSAlexey Samsonov       SymbolIterator->first.Addr + SymbolIterator->first.Size <= Address)
2318df3a07aSAlexey Samsonov     return false;
2328df3a07aSAlexey Samsonov   Name = SymbolIterator->second.str();
2338df3a07aSAlexey Samsonov   Addr = SymbolIterator->first.Addr;
2348df3a07aSAlexey Samsonov   Size = SymbolIterator->first.Size;
2358df3a07aSAlexey Samsonov   return true;
2368df3a07aSAlexey Samsonov }
2378df3a07aSAlexey Samsonov 
238c038e2dbSReid Kleckner bool SymbolizableObjectFile::shouldOverrideWithSymbolTable(
239c038e2dbSReid Kleckner     FunctionNameKind FNKind, bool UseSymbolTable) const {
240c038e2dbSReid Kleckner   // When DWARF is used with -gline-tables-only / -gmlt, the symbol table gives
241c038e2dbSReid Kleckner   // better answers for linkage names than the DIContext. Otherwise, we are
242c038e2dbSReid Kleckner   // probably using PEs and PDBs, and we shouldn't do the override. PE files
243c038e2dbSReid Kleckner   // generally only contain the names of exported symbols.
244c038e2dbSReid Kleckner   return FNKind == FunctionNameKind::LinkageName && UseSymbolTable &&
245c038e2dbSReid Kleckner          isa<DWARFContext>(DebugInfoContext.get());
246c038e2dbSReid Kleckner }
247c038e2dbSReid Kleckner 
24877fc1f60SAlexey Lapshin DILineInfo
24977fc1f60SAlexey Lapshin SymbolizableObjectFile::symbolizeCode(object::SectionedAddress ModuleOffset,
2505de4ba17SSterling Augustine                                       DILineInfoSpecifier LineInfoSpecifier,
2518df3a07aSAlexey Samsonov                                       bool UseSymbolTable) const {
252b2c4b8bdSAlexey Lapshin   if (ModuleOffset.SectionIndex == object::SectionedAddress::UndefSection)
253b2c4b8bdSAlexey Lapshin     ModuleOffset.SectionIndex =
254b2c4b8bdSAlexey Lapshin         getModuleSectionIndexForAddress(ModuleOffset.Address);
2555de4ba17SSterling Augustine   DILineInfo LineInfo =
2565de4ba17SSterling Augustine       DebugInfoContext->getLineInfoForAddress(ModuleOffset, LineInfoSpecifier);
2570feb6e52SPeter Collingbourne 
2588df3a07aSAlexey Samsonov   // Override function name from symbol table if necessary.
2595de4ba17SSterling Augustine   if (shouldOverrideWithSymbolTable(LineInfoSpecifier.FNKind, UseSymbolTable)) {
2608df3a07aSAlexey Samsonov     std::string FunctionName;
2618df3a07aSAlexey Samsonov     uint64_t Start, Size;
26277fc1f60SAlexey Lapshin     if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset.Address,
2638df3a07aSAlexey Samsonov                                FunctionName, Start, Size)) {
2648df3a07aSAlexey Samsonov       LineInfo.FunctionName = FunctionName;
2658df3a07aSAlexey Samsonov     }
2668df3a07aSAlexey Samsonov   }
2678df3a07aSAlexey Samsonov   return LineInfo;
2688df3a07aSAlexey Samsonov }
2698df3a07aSAlexey Samsonov 
2708df3a07aSAlexey Samsonov DIInliningInfo SymbolizableObjectFile::symbolizeInlinedCode(
2715de4ba17SSterling Augustine     object::SectionedAddress ModuleOffset,
2725de4ba17SSterling Augustine     DILineInfoSpecifier LineInfoSpecifier, bool UseSymbolTable) const {
273b2c4b8bdSAlexey Lapshin   if (ModuleOffset.SectionIndex == object::SectionedAddress::UndefSection)
274b2c4b8bdSAlexey Lapshin     ModuleOffset.SectionIndex =
275b2c4b8bdSAlexey Lapshin         getModuleSectionIndexForAddress(ModuleOffset.Address);
2760feb6e52SPeter Collingbourne   DIInliningInfo InlinedContext = DebugInfoContext->getInliningInfoForAddress(
2775de4ba17SSterling Augustine       ModuleOffset, LineInfoSpecifier);
2780feb6e52SPeter Collingbourne 
2798df3a07aSAlexey Samsonov   // Make sure there is at least one frame in context.
2808df3a07aSAlexey Samsonov   if (InlinedContext.getNumberOfFrames() == 0)
2818df3a07aSAlexey Samsonov     InlinedContext.addFrame(DILineInfo());
2828df3a07aSAlexey Samsonov 
2838df3a07aSAlexey Samsonov   // Override the function name in lower frame with name from symbol table.
2845de4ba17SSterling Augustine   if (shouldOverrideWithSymbolTable(LineInfoSpecifier.FNKind, UseSymbolTable)) {
2858df3a07aSAlexey Samsonov     std::string FunctionName;
2868df3a07aSAlexey Samsonov     uint64_t Start, Size;
28777fc1f60SAlexey Lapshin     if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset.Address,
2888df3a07aSAlexey Samsonov                                FunctionName, Start, Size)) {
289e46bd741SAlexey Samsonov       InlinedContext.getMutableFrame(InlinedContext.getNumberOfFrames() - 1)
290e46bd741SAlexey Samsonov           ->FunctionName = FunctionName;
2918df3a07aSAlexey Samsonov     }
2928df3a07aSAlexey Samsonov   }
293e46bd741SAlexey Samsonov 
294e46bd741SAlexey Samsonov   return InlinedContext;
2958df3a07aSAlexey Samsonov }
2968df3a07aSAlexey Samsonov 
29777fc1f60SAlexey Lapshin DIGlobal SymbolizableObjectFile::symbolizeData(
29877fc1f60SAlexey Lapshin     object::SectionedAddress ModuleOffset) const {
29976f7ecb8SAlexey Samsonov   DIGlobal Res;
30077fc1f60SAlexey Lapshin   getNameFromSymbolTable(SymbolRef::ST_Data, ModuleOffset.Address, Res.Name,
30177fc1f60SAlexey Lapshin                          Res.Start, Res.Size);
30276f7ecb8SAlexey Samsonov   return Res;
3038df3a07aSAlexey Samsonov }
304b2c4b8bdSAlexey Lapshin 
3059c8282a9SPeter Collingbourne std::vector<DILocal> SymbolizableObjectFile::symbolizeFrame(
3069c8282a9SPeter Collingbourne     object::SectionedAddress ModuleOffset) const {
3079c8282a9SPeter Collingbourne   if (ModuleOffset.SectionIndex == object::SectionedAddress::UndefSection)
3089c8282a9SPeter Collingbourne     ModuleOffset.SectionIndex =
3099c8282a9SPeter Collingbourne         getModuleSectionIndexForAddress(ModuleOffset.Address);
3109c8282a9SPeter Collingbourne   return DebugInfoContext->getLocalsForAddress(ModuleOffset);
3119c8282a9SPeter Collingbourne }
3129c8282a9SPeter Collingbourne 
313b2c4b8bdSAlexey Lapshin /// Search for the first occurence of specified Address in ObjectFile.
314b2c4b8bdSAlexey Lapshin uint64_t SymbolizableObjectFile::getModuleSectionIndexForAddress(
315b2c4b8bdSAlexey Lapshin     uint64_t Address) const {
316b2c4b8bdSAlexey Lapshin 
317b2c4b8bdSAlexey Lapshin   for (SectionRef Sec : Module->sections()) {
318b2c4b8bdSAlexey Lapshin     if (!Sec.isText() || Sec.isVirtual())
319b2c4b8bdSAlexey Lapshin       continue;
320b2c4b8bdSAlexey Lapshin 
321b2c4b8bdSAlexey Lapshin     if (Address >= Sec.getAddress() &&
322dd0e8335SFangrui Song         Address < Sec.getAddress() + Sec.getSize())
323b2c4b8bdSAlexey Lapshin       return Sec.getIndex();
324b2c4b8bdSAlexey Lapshin   }
325b2c4b8bdSAlexey Lapshin 
326b2c4b8bdSAlexey Lapshin   return object::SectionedAddress::UndefSection;
327b2c4b8bdSAlexey Lapshin }
328