xref: /freebsd/contrib/llvm-project/llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
10b57cec5SDimitry Andric //===- SymbolizableObjectFile.cpp -----------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // Implementation of SymbolizableObjectFile class.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "SymbolizableObjectFile.h"
140b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
150b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
160b57cec5SDimitry Andric #include "llvm/ADT/Triple.h"
170b57cec5SDimitry Andric #include "llvm/BinaryFormat/COFF.h"
180b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFContext.h"
190b57cec5SDimitry Andric #include "llvm/DebugInfo/Symbolize/SymbolizableModule.h"
200b57cec5SDimitry Andric #include "llvm/Object/COFF.h"
210b57cec5SDimitry Andric #include "llvm/Object/ObjectFile.h"
220b57cec5SDimitry Andric #include "llvm/Object/SymbolSize.h"
230b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
240b57cec5SDimitry Andric #include "llvm/Support/DataExtractor.h"
250b57cec5SDimitry Andric #include "llvm/Support/Error.h"
260b57cec5SDimitry Andric #include <algorithm>
270b57cec5SDimitry Andric #include <cstdint>
280b57cec5SDimitry Andric #include <memory>
290b57cec5SDimitry Andric #include <string>
300b57cec5SDimitry Andric #include <system_error>
310b57cec5SDimitry Andric #include <utility>
320b57cec5SDimitry Andric #include <vector>
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric using namespace llvm;
350b57cec5SDimitry Andric using namespace object;
360b57cec5SDimitry Andric using namespace symbolize;
370b57cec5SDimitry Andric 
38*5ffd83dbSDimitry Andric Expected<std::unique_ptr<SymbolizableObjectFile>>
390b57cec5SDimitry Andric SymbolizableObjectFile::create(const object::ObjectFile *Obj,
408bcb0991SDimitry Andric                                std::unique_ptr<DIContext> DICtx,
418bcb0991SDimitry Andric                                bool UntagAddresses) {
420b57cec5SDimitry Andric   assert(DICtx);
430b57cec5SDimitry Andric   std::unique_ptr<SymbolizableObjectFile> res(
448bcb0991SDimitry Andric       new SymbolizableObjectFile(Obj, std::move(DICtx), UntagAddresses));
450b57cec5SDimitry Andric   std::unique_ptr<DataExtractor> OpdExtractor;
460b57cec5SDimitry Andric   uint64_t OpdAddress = 0;
470b57cec5SDimitry Andric   // Find the .opd (function descriptor) section if any, for big-endian
480b57cec5SDimitry Andric   // PowerPC64 ELF.
490b57cec5SDimitry Andric   if (Obj->getArch() == Triple::ppc64) {
500b57cec5SDimitry Andric     for (section_iterator Section : Obj->sections()) {
518bcb0991SDimitry Andric       Expected<StringRef> NameOrErr = Section->getName();
528bcb0991SDimitry Andric       if (!NameOrErr)
53*5ffd83dbSDimitry Andric         return NameOrErr.takeError();
548bcb0991SDimitry Andric 
558bcb0991SDimitry Andric       if (*NameOrErr == ".opd") {
560b57cec5SDimitry Andric         Expected<StringRef> E = Section->getContents();
570b57cec5SDimitry Andric         if (!E)
58*5ffd83dbSDimitry Andric           return E.takeError();
590b57cec5SDimitry Andric         OpdExtractor.reset(new DataExtractor(*E, Obj->isLittleEndian(),
600b57cec5SDimitry Andric                                              Obj->getBytesInAddress()));
610b57cec5SDimitry Andric         OpdAddress = Section->getAddress();
620b57cec5SDimitry Andric         break;
630b57cec5SDimitry Andric       }
640b57cec5SDimitry Andric     }
650b57cec5SDimitry Andric   }
660b57cec5SDimitry Andric   std::vector<std::pair<SymbolRef, uint64_t>> Symbols =
670b57cec5SDimitry Andric       computeSymbolSizes(*Obj);
680b57cec5SDimitry Andric   for (auto &P : Symbols)
69*5ffd83dbSDimitry Andric     if (Error E =
70*5ffd83dbSDimitry Andric             res->addSymbol(P.first, P.second, OpdExtractor.get(), OpdAddress))
71*5ffd83dbSDimitry Andric       return std::move(E);
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric   // If this is a COFF object and we didn't find any symbols, try the export
740b57cec5SDimitry Andric   // table.
750b57cec5SDimitry Andric   if (Symbols.empty()) {
760b57cec5SDimitry Andric     if (auto *CoffObj = dyn_cast<COFFObjectFile>(Obj))
77*5ffd83dbSDimitry Andric       if (Error E = res->addCoffExportSymbols(CoffObj))
78*5ffd83dbSDimitry Andric         return std::move(E);
790b57cec5SDimitry Andric   }
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric   std::vector<std::pair<SymbolDesc, StringRef>> &Fs = res->Functions,
820b57cec5SDimitry Andric                                                 &Os = res->Objects;
830b57cec5SDimitry Andric   auto Uniquify = [](std::vector<std::pair<SymbolDesc, StringRef>> &S) {
840b57cec5SDimitry Andric     // Sort by (Addr,Size,Name). If several SymbolDescs share the same Addr,
850b57cec5SDimitry Andric     // pick the one with the largest Size. This helps us avoid symbols with no
860b57cec5SDimitry Andric     // size information (Size=0).
870b57cec5SDimitry Andric     llvm::sort(S);
880b57cec5SDimitry Andric     auto I = S.begin(), E = S.end(), J = S.begin();
890b57cec5SDimitry Andric     while (I != E) {
900b57cec5SDimitry Andric       auto OI = I;
910b57cec5SDimitry Andric       while (++I != E && OI->first.Addr == I->first.Addr) {
920b57cec5SDimitry Andric       }
930b57cec5SDimitry Andric       *J++ = I[-1];
940b57cec5SDimitry Andric     }
950b57cec5SDimitry Andric     S.erase(J, S.end());
960b57cec5SDimitry Andric   };
970b57cec5SDimitry Andric   Uniquify(Fs);
980b57cec5SDimitry Andric   Uniquify(Os);
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric   return std::move(res);
1010b57cec5SDimitry Andric }
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric SymbolizableObjectFile::SymbolizableObjectFile(const ObjectFile *Obj,
1048bcb0991SDimitry Andric                                                std::unique_ptr<DIContext> DICtx,
1058bcb0991SDimitry Andric                                                bool UntagAddresses)
1068bcb0991SDimitry Andric     : Module(Obj), DebugInfoContext(std::move(DICtx)),
1078bcb0991SDimitry Andric       UntagAddresses(UntagAddresses) {}
1080b57cec5SDimitry Andric 
1090b57cec5SDimitry Andric namespace {
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric struct OffsetNamePair {
1120b57cec5SDimitry Andric   uint32_t Offset;
1130b57cec5SDimitry Andric   StringRef Name;
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric   bool operator<(const OffsetNamePair &R) const {
1160b57cec5SDimitry Andric     return Offset < R.Offset;
1170b57cec5SDimitry Andric   }
1180b57cec5SDimitry Andric };
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric } // end anonymous namespace
1210b57cec5SDimitry Andric 
122*5ffd83dbSDimitry Andric Error SymbolizableObjectFile::addCoffExportSymbols(
1230b57cec5SDimitry Andric     const COFFObjectFile *CoffObj) {
1240b57cec5SDimitry Andric   // Get all export names and offsets.
1250b57cec5SDimitry Andric   std::vector<OffsetNamePair> ExportSyms;
1260b57cec5SDimitry Andric   for (const ExportDirectoryEntryRef &Ref : CoffObj->export_directories()) {
1270b57cec5SDimitry Andric     StringRef Name;
1280b57cec5SDimitry Andric     uint32_t Offset;
1290b57cec5SDimitry Andric     if (auto EC = Ref.getSymbolName(Name))
1300b57cec5SDimitry Andric       return EC;
1310b57cec5SDimitry Andric     if (auto EC = Ref.getExportRVA(Offset))
1320b57cec5SDimitry Andric       return EC;
1330b57cec5SDimitry Andric     ExportSyms.push_back(OffsetNamePair{Offset, Name});
1340b57cec5SDimitry Andric   }
1350b57cec5SDimitry Andric   if (ExportSyms.empty())
136*5ffd83dbSDimitry Andric     return Error::success();
1370b57cec5SDimitry Andric 
1380b57cec5SDimitry Andric   // Sort by ascending offset.
1390b57cec5SDimitry Andric   array_pod_sort(ExportSyms.begin(), ExportSyms.end());
1400b57cec5SDimitry Andric 
1410b57cec5SDimitry Andric   // Approximate the symbol sizes by assuming they run to the next symbol.
1420b57cec5SDimitry Andric   // FIXME: This assumes all exports are functions.
1430b57cec5SDimitry Andric   uint64_t ImageBase = CoffObj->getImageBase();
1440b57cec5SDimitry Andric   for (auto I = ExportSyms.begin(), E = ExportSyms.end(); I != E; ++I) {
1450b57cec5SDimitry Andric     OffsetNamePair &Export = *I;
1460b57cec5SDimitry Andric     // FIXME: The last export has a one byte size now.
1470b57cec5SDimitry Andric     uint32_t NextOffset = I != E ? I->Offset : Export.Offset + 1;
1480b57cec5SDimitry Andric     uint64_t SymbolStart = ImageBase + Export.Offset;
1490b57cec5SDimitry Andric     uint64_t SymbolSize = NextOffset - Export.Offset;
1500b57cec5SDimitry Andric     SymbolDesc SD = {SymbolStart, SymbolSize};
1510b57cec5SDimitry Andric     Functions.emplace_back(SD, Export.Name);
1520b57cec5SDimitry Andric   }
153*5ffd83dbSDimitry Andric   return Error::success();
1540b57cec5SDimitry Andric }
1550b57cec5SDimitry Andric 
156*5ffd83dbSDimitry Andric Error SymbolizableObjectFile::addSymbol(const SymbolRef &Symbol,
1570b57cec5SDimitry Andric                                         uint64_t SymbolSize,
1580b57cec5SDimitry Andric                                         DataExtractor *OpdExtractor,
1590b57cec5SDimitry Andric                                         uint64_t OpdAddress) {
1600b57cec5SDimitry Andric   // Avoid adding symbols from an unknown/undefined section.
1610b57cec5SDimitry Andric   const ObjectFile *Obj = Symbol.getObject();
1620b57cec5SDimitry Andric   Expected<section_iterator> Sec = Symbol.getSection();
1630b57cec5SDimitry Andric   if (!Sec || (Obj && Obj->section_end() == *Sec))
164*5ffd83dbSDimitry Andric     return Error::success();
1650b57cec5SDimitry Andric   Expected<SymbolRef::Type> SymbolTypeOrErr = Symbol.getType();
1660b57cec5SDimitry Andric   if (!SymbolTypeOrErr)
167*5ffd83dbSDimitry Andric     return SymbolTypeOrErr.takeError();
1680b57cec5SDimitry Andric   SymbolRef::Type SymbolType = *SymbolTypeOrErr;
1690b57cec5SDimitry Andric   if (SymbolType != SymbolRef::ST_Function && SymbolType != SymbolRef::ST_Data)
170*5ffd83dbSDimitry Andric     return Error::success();
1710b57cec5SDimitry Andric   Expected<uint64_t> SymbolAddressOrErr = Symbol.getAddress();
1720b57cec5SDimitry Andric   if (!SymbolAddressOrErr)
173*5ffd83dbSDimitry Andric     return SymbolAddressOrErr.takeError();
1740b57cec5SDimitry Andric   uint64_t SymbolAddress = *SymbolAddressOrErr;
1758bcb0991SDimitry Andric   if (UntagAddresses) {
1768bcb0991SDimitry Andric     // For kernel addresses, bits 56-63 need to be set, so we sign extend bit 55
1778bcb0991SDimitry Andric     // into bits 56-63 instead of masking them out.
1788bcb0991SDimitry Andric     SymbolAddress &= (1ull << 56) - 1;
1798bcb0991SDimitry Andric     SymbolAddress = (int64_t(SymbolAddress) << 8) >> 8;
1808bcb0991SDimitry Andric   }
1810b57cec5SDimitry Andric   if (OpdExtractor) {
1820b57cec5SDimitry Andric     // For big-endian PowerPC64 ELF, symbols in the .opd section refer to
1830b57cec5SDimitry Andric     // function descriptors. The first word of the descriptor is a pointer to
1840b57cec5SDimitry Andric     // the function's code.
1850b57cec5SDimitry Andric     // For the purposes of symbolization, pretend the symbol's address is that
1860b57cec5SDimitry Andric     // of the function's code, not the descriptor.
1870b57cec5SDimitry Andric     uint64_t OpdOffset = SymbolAddress - OpdAddress;
1888bcb0991SDimitry Andric     if (OpdExtractor->isValidOffsetForAddress(OpdOffset))
1898bcb0991SDimitry Andric       SymbolAddress = OpdExtractor->getAddress(&OpdOffset);
1900b57cec5SDimitry Andric   }
1910b57cec5SDimitry Andric   Expected<StringRef> SymbolNameOrErr = Symbol.getName();
1920b57cec5SDimitry Andric   if (!SymbolNameOrErr)
193*5ffd83dbSDimitry Andric     return SymbolNameOrErr.takeError();
1940b57cec5SDimitry Andric   StringRef SymbolName = *SymbolNameOrErr;
1950b57cec5SDimitry Andric   // Mach-O symbol table names have leading underscore, skip it.
1960b57cec5SDimitry Andric   if (Module->isMachO() && !SymbolName.empty() && SymbolName[0] == '_')
1970b57cec5SDimitry Andric     SymbolName = SymbolName.drop_front();
1980b57cec5SDimitry Andric   // FIXME: If a function has alias, there are two entries in symbol table
1990b57cec5SDimitry Andric   // with same address size. Make sure we choose the correct one.
2000b57cec5SDimitry Andric   auto &M = SymbolType == SymbolRef::ST_Function ? Functions : Objects;
2010b57cec5SDimitry Andric   SymbolDesc SD = { SymbolAddress, SymbolSize };
2020b57cec5SDimitry Andric   M.emplace_back(SD, SymbolName);
203*5ffd83dbSDimitry Andric   return Error::success();
2040b57cec5SDimitry Andric }
2050b57cec5SDimitry Andric 
2060b57cec5SDimitry Andric // Return true if this is a 32-bit x86 PE COFF module.
2070b57cec5SDimitry Andric bool SymbolizableObjectFile::isWin32Module() const {
2080b57cec5SDimitry Andric   auto *CoffObject = dyn_cast<COFFObjectFile>(Module);
2090b57cec5SDimitry Andric   return CoffObject && CoffObject->getMachine() == COFF::IMAGE_FILE_MACHINE_I386;
2100b57cec5SDimitry Andric }
2110b57cec5SDimitry Andric 
2120b57cec5SDimitry Andric uint64_t SymbolizableObjectFile::getModulePreferredBase() const {
2130b57cec5SDimitry Andric   if (auto *CoffObject = dyn_cast<COFFObjectFile>(Module))
2140b57cec5SDimitry Andric     return CoffObject->getImageBase();
2150b57cec5SDimitry Andric   return 0;
2160b57cec5SDimitry Andric }
2170b57cec5SDimitry Andric 
2180b57cec5SDimitry Andric bool SymbolizableObjectFile::getNameFromSymbolTable(SymbolRef::Type Type,
2190b57cec5SDimitry Andric                                                     uint64_t Address,
2200b57cec5SDimitry Andric                                                     std::string &Name,
2210b57cec5SDimitry Andric                                                     uint64_t &Addr,
2220b57cec5SDimitry Andric                                                     uint64_t &Size) const {
2230b57cec5SDimitry Andric   const auto &Symbols = Type == SymbolRef::ST_Function ? Functions : Objects;
2240b57cec5SDimitry Andric   std::pair<SymbolDesc, StringRef> SD{{Address, UINT64_C(-1)}, StringRef()};
2250b57cec5SDimitry Andric   auto SymbolIterator = llvm::upper_bound(Symbols, SD);
2260b57cec5SDimitry Andric   if (SymbolIterator == Symbols.begin())
2270b57cec5SDimitry Andric     return false;
2280b57cec5SDimitry Andric   --SymbolIterator;
2290b57cec5SDimitry Andric   if (SymbolIterator->first.Size != 0 &&
2300b57cec5SDimitry Andric       SymbolIterator->first.Addr + SymbolIterator->first.Size <= Address)
2310b57cec5SDimitry Andric     return false;
2320b57cec5SDimitry Andric   Name = SymbolIterator->second.str();
2330b57cec5SDimitry Andric   Addr = SymbolIterator->first.Addr;
2340b57cec5SDimitry Andric   Size = SymbolIterator->first.Size;
2350b57cec5SDimitry Andric   return true;
2360b57cec5SDimitry Andric }
2370b57cec5SDimitry Andric 
2380b57cec5SDimitry Andric bool SymbolizableObjectFile::shouldOverrideWithSymbolTable(
2390b57cec5SDimitry Andric     FunctionNameKind FNKind, bool UseSymbolTable) const {
2400b57cec5SDimitry Andric   // When DWARF is used with -gline-tables-only / -gmlt, the symbol table gives
2410b57cec5SDimitry Andric   // better answers for linkage names than the DIContext. Otherwise, we are
2420b57cec5SDimitry Andric   // probably using PEs and PDBs, and we shouldn't do the override. PE files
2430b57cec5SDimitry Andric   // generally only contain the names of exported symbols.
2440b57cec5SDimitry Andric   return FNKind == FunctionNameKind::LinkageName && UseSymbolTable &&
2450b57cec5SDimitry Andric          isa<DWARFContext>(DebugInfoContext.get());
2460b57cec5SDimitry Andric }
2470b57cec5SDimitry Andric 
2480b57cec5SDimitry Andric DILineInfo
2490b57cec5SDimitry Andric SymbolizableObjectFile::symbolizeCode(object::SectionedAddress ModuleOffset,
250*5ffd83dbSDimitry Andric                                       DILineInfoSpecifier LineInfoSpecifier,
2510b57cec5SDimitry Andric                                       bool UseSymbolTable) const {
2520b57cec5SDimitry Andric   if (ModuleOffset.SectionIndex == object::SectionedAddress::UndefSection)
2530b57cec5SDimitry Andric     ModuleOffset.SectionIndex =
2540b57cec5SDimitry Andric         getModuleSectionIndexForAddress(ModuleOffset.Address);
255*5ffd83dbSDimitry Andric   DILineInfo LineInfo =
256*5ffd83dbSDimitry Andric       DebugInfoContext->getLineInfoForAddress(ModuleOffset, LineInfoSpecifier);
2570b57cec5SDimitry Andric 
2580b57cec5SDimitry Andric   // Override function name from symbol table if necessary.
259*5ffd83dbSDimitry Andric   if (shouldOverrideWithSymbolTable(LineInfoSpecifier.FNKind, UseSymbolTable)) {
2600b57cec5SDimitry Andric     std::string FunctionName;
2610b57cec5SDimitry Andric     uint64_t Start, Size;
2620b57cec5SDimitry Andric     if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset.Address,
2630b57cec5SDimitry Andric                                FunctionName, Start, Size)) {
2640b57cec5SDimitry Andric       LineInfo.FunctionName = FunctionName;
2650b57cec5SDimitry Andric     }
2660b57cec5SDimitry Andric   }
2670b57cec5SDimitry Andric   return LineInfo;
2680b57cec5SDimitry Andric }
2690b57cec5SDimitry Andric 
2700b57cec5SDimitry Andric DIInliningInfo SymbolizableObjectFile::symbolizeInlinedCode(
271*5ffd83dbSDimitry Andric     object::SectionedAddress ModuleOffset,
272*5ffd83dbSDimitry Andric     DILineInfoSpecifier LineInfoSpecifier, bool UseSymbolTable) const {
2730b57cec5SDimitry Andric   if (ModuleOffset.SectionIndex == object::SectionedAddress::UndefSection)
2740b57cec5SDimitry Andric     ModuleOffset.SectionIndex =
2750b57cec5SDimitry Andric         getModuleSectionIndexForAddress(ModuleOffset.Address);
2760b57cec5SDimitry Andric   DIInliningInfo InlinedContext = DebugInfoContext->getInliningInfoForAddress(
277*5ffd83dbSDimitry Andric       ModuleOffset, LineInfoSpecifier);
2780b57cec5SDimitry Andric 
2790b57cec5SDimitry Andric   // Make sure there is at least one frame in context.
2800b57cec5SDimitry Andric   if (InlinedContext.getNumberOfFrames() == 0)
2810b57cec5SDimitry Andric     InlinedContext.addFrame(DILineInfo());
2820b57cec5SDimitry Andric 
2830b57cec5SDimitry Andric   // Override the function name in lower frame with name from symbol table.
284*5ffd83dbSDimitry Andric   if (shouldOverrideWithSymbolTable(LineInfoSpecifier.FNKind, UseSymbolTable)) {
2850b57cec5SDimitry Andric     std::string FunctionName;
2860b57cec5SDimitry Andric     uint64_t Start, Size;
2870b57cec5SDimitry Andric     if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset.Address,
2880b57cec5SDimitry Andric                                FunctionName, Start, Size)) {
2890b57cec5SDimitry Andric       InlinedContext.getMutableFrame(InlinedContext.getNumberOfFrames() - 1)
2900b57cec5SDimitry Andric           ->FunctionName = FunctionName;
2910b57cec5SDimitry Andric     }
2920b57cec5SDimitry Andric   }
2930b57cec5SDimitry Andric 
2940b57cec5SDimitry Andric   return InlinedContext;
2950b57cec5SDimitry Andric }
2960b57cec5SDimitry Andric 
2970b57cec5SDimitry Andric DIGlobal SymbolizableObjectFile::symbolizeData(
2980b57cec5SDimitry Andric     object::SectionedAddress ModuleOffset) const {
2990b57cec5SDimitry Andric   DIGlobal Res;
3000b57cec5SDimitry Andric   getNameFromSymbolTable(SymbolRef::ST_Data, ModuleOffset.Address, Res.Name,
3010b57cec5SDimitry Andric                          Res.Start, Res.Size);
3020b57cec5SDimitry Andric   return Res;
3030b57cec5SDimitry Andric }
3040b57cec5SDimitry Andric 
3050b57cec5SDimitry Andric std::vector<DILocal> SymbolizableObjectFile::symbolizeFrame(
3060b57cec5SDimitry Andric     object::SectionedAddress ModuleOffset) const {
3070b57cec5SDimitry Andric   if (ModuleOffset.SectionIndex == object::SectionedAddress::UndefSection)
3080b57cec5SDimitry Andric     ModuleOffset.SectionIndex =
3090b57cec5SDimitry Andric         getModuleSectionIndexForAddress(ModuleOffset.Address);
3100b57cec5SDimitry Andric   return DebugInfoContext->getLocalsForAddress(ModuleOffset);
3110b57cec5SDimitry Andric }
3120b57cec5SDimitry Andric 
3130b57cec5SDimitry Andric /// Search for the first occurence of specified Address in ObjectFile.
3140b57cec5SDimitry Andric uint64_t SymbolizableObjectFile::getModuleSectionIndexForAddress(
3150b57cec5SDimitry Andric     uint64_t Address) const {
3160b57cec5SDimitry Andric 
3170b57cec5SDimitry Andric   for (SectionRef Sec : Module->sections()) {
3180b57cec5SDimitry Andric     if (!Sec.isText() || Sec.isVirtual())
3190b57cec5SDimitry Andric       continue;
3200b57cec5SDimitry Andric 
3210b57cec5SDimitry Andric     if (Address >= Sec.getAddress() &&
3220b57cec5SDimitry Andric         Address < Sec.getAddress() + Sec.getSize())
3230b57cec5SDimitry Andric       return Sec.getIndex();
3240b57cec5SDimitry Andric   }
3250b57cec5SDimitry Andric 
3260b57cec5SDimitry Andric   return object::SectionedAddress::UndefSection;
3270b57cec5SDimitry Andric }
328