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 
388df3a07aSAlexey Samsonov static DILineInfoSpecifier
398df3a07aSAlexey Samsonov getDILineInfoSpecifier(FunctionNameKind FNKind) {
408df3a07aSAlexey Samsonov   return DILineInfoSpecifier(
418df3a07aSAlexey Samsonov       DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, FNKind);
428df3a07aSAlexey Samsonov }
438df3a07aSAlexey Samsonov 
448df3a07aSAlexey Samsonov ErrorOr<std::unique_ptr<SymbolizableObjectFile>>
458df3a07aSAlexey Samsonov SymbolizableObjectFile::create(object::ObjectFile *Obj,
468df3a07aSAlexey Samsonov                                std::unique_ptr<DIContext> DICtx) {
478df3a07aSAlexey Samsonov   std::unique_ptr<SymbolizableObjectFile> res(
488df3a07aSAlexey Samsonov       new SymbolizableObjectFile(Obj, std::move(DICtx)));
498df3a07aSAlexey Samsonov   std::unique_ptr<DataExtractor> OpdExtractor;
508df3a07aSAlexey Samsonov   uint64_t OpdAddress = 0;
518df3a07aSAlexey Samsonov   // Find the .opd (function descriptor) section if any, for big-endian
528df3a07aSAlexey Samsonov   // PowerPC64 ELF.
538df3a07aSAlexey Samsonov   if (Obj->getArch() == Triple::ppc64) {
548df3a07aSAlexey Samsonov     for (section_iterator Section : Obj->sections()) {
558df3a07aSAlexey Samsonov       StringRef Name;
568df3a07aSAlexey Samsonov       StringRef Data;
578df3a07aSAlexey Samsonov       if (auto EC = Section->getName(Name))
588df3a07aSAlexey Samsonov         return EC;
598df3a07aSAlexey Samsonov       if (Name == ".opd") {
608df3a07aSAlexey Samsonov         if (auto EC = Section->getContents(Data))
618df3a07aSAlexey Samsonov           return EC;
628df3a07aSAlexey Samsonov         OpdExtractor.reset(new DataExtractor(Data, Obj->isLittleEndian(),
638df3a07aSAlexey Samsonov                                              Obj->getBytesInAddress()));
648df3a07aSAlexey Samsonov         OpdAddress = Section->getAddress();
658df3a07aSAlexey Samsonov         break;
668df3a07aSAlexey Samsonov       }
678df3a07aSAlexey Samsonov     }
688df3a07aSAlexey Samsonov   }
698df3a07aSAlexey Samsonov   std::vector<std::pair<SymbolRef, uint64_t>> Symbols =
708df3a07aSAlexey Samsonov       computeSymbolSizes(*Obj);
718df3a07aSAlexey Samsonov   for (auto &P : Symbols)
728df3a07aSAlexey Samsonov     res->addSymbol(P.first, P.second, OpdExtractor.get(), OpdAddress);
738df3a07aSAlexey Samsonov 
748df3a07aSAlexey Samsonov   // If this is a COFF object and we didn't find any symbols, try the export
758df3a07aSAlexey Samsonov   // table.
768df3a07aSAlexey Samsonov   if (Symbols.empty()) {
778df3a07aSAlexey Samsonov     if (auto *CoffObj = dyn_cast<COFFObjectFile>(Obj))
788df3a07aSAlexey Samsonov       if (auto EC = res->addCoffExportSymbols(CoffObj))
798df3a07aSAlexey Samsonov         return EC;
808df3a07aSAlexey Samsonov   }
81*afb54fd6SFangrui Song 
82*afb54fd6SFangrui Song   std::vector<std::pair<SymbolDesc, StringRef>> &Fs = res->Functions,
83*afb54fd6SFangrui Song                                                 &Os = res->Objects;
84*afb54fd6SFangrui Song   llvm::sort(Fs);
85*afb54fd6SFangrui Song   Fs.erase(std::unique(Fs.begin(), Fs.end()), Fs.end());
86*afb54fd6SFangrui Song   llvm::sort(Os);
87*afb54fd6SFangrui Song   Os.erase(std::unique(Os.begin(), Os.end()), Os.end());
88*afb54fd6SFangrui Song 
898df3a07aSAlexey Samsonov   return std::move(res);
908df3a07aSAlexey Samsonov }
918df3a07aSAlexey Samsonov 
928df3a07aSAlexey Samsonov SymbolizableObjectFile::SymbolizableObjectFile(ObjectFile *Obj,
938df3a07aSAlexey Samsonov                                                std::unique_ptr<DIContext> DICtx)
948df3a07aSAlexey Samsonov     : Module(Obj), DebugInfoContext(std::move(DICtx)) {}
958df3a07aSAlexey Samsonov 
968df3a07aSAlexey Samsonov namespace {
9744d95122SEugene Zelenko 
988df3a07aSAlexey Samsonov struct OffsetNamePair {
998df3a07aSAlexey Samsonov   uint32_t Offset;
1008df3a07aSAlexey Samsonov   StringRef Name;
10144d95122SEugene Zelenko 
1028df3a07aSAlexey 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 
1098df3a07aSAlexey Samsonov std::error_code 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())
1238df3a07aSAlexey Samsonov     return std::error_code();
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;
1378df3a07aSAlexey Samsonov     SymbolDesc SD = {SymbolStart, SymbolSize};
138*afb54fd6SFangrui Song     Functions.emplace_back(SD, Export.Name);
1398df3a07aSAlexey Samsonov   }
1408df3a07aSAlexey Samsonov   return std::error_code();
1418df3a07aSAlexey Samsonov }
1428df3a07aSAlexey Samsonov 
1438df3a07aSAlexey Samsonov std::error_code SymbolizableObjectFile::addSymbol(const SymbolRef &Symbol,
1448df3a07aSAlexey Samsonov                                                   uint64_t SymbolSize,
1458df3a07aSAlexey Samsonov                                                   DataExtractor *OpdExtractor,
1468df3a07aSAlexey Samsonov                                                   uint64_t OpdAddress) {
147123be5d4SMatt Davis   // Avoid adding symbols from an unknown/undefined section.
148123be5d4SMatt Davis   const ObjectFile *Obj = Symbol.getObject();
149123be5d4SMatt Davis   Expected<section_iterator> Sec = Symbol.getSection();
150123be5d4SMatt Davis   if (!Sec || (Obj && Obj->section_end() == *Sec))
151123be5d4SMatt Davis     return std::error_code();
1527bd8d994SKevin Enderby   Expected<SymbolRef::Type> SymbolTypeOrErr = Symbol.getType();
1537bd8d994SKevin Enderby   if (!SymbolTypeOrErr)
1547bd8d994SKevin Enderby     return errorToErrorCode(SymbolTypeOrErr.takeError());
1555afbc1cdSKevin Enderby   SymbolRef::Type SymbolType = *SymbolTypeOrErr;
1568df3a07aSAlexey Samsonov   if (SymbolType != SymbolRef::ST_Function && SymbolType != SymbolRef::ST_Data)
1578df3a07aSAlexey Samsonov     return std::error_code();
158931cb65dSKevin Enderby   Expected<uint64_t> SymbolAddressOrErr = Symbol.getAddress();
159931cb65dSKevin Enderby   if (!SymbolAddressOrErr)
160931cb65dSKevin Enderby     return errorToErrorCode(SymbolAddressOrErr.takeError());
1618df3a07aSAlexey Samsonov   uint64_t SymbolAddress = *SymbolAddressOrErr;
1628df3a07aSAlexey Samsonov   if (OpdExtractor) {
1638df3a07aSAlexey Samsonov     // For big-endian PowerPC64 ELF, symbols in the .opd section refer to
1648df3a07aSAlexey Samsonov     // function descriptors. The first word of the descriptor is a pointer to
1658df3a07aSAlexey Samsonov     // the function's code.
1668df3a07aSAlexey Samsonov     // For the purposes of symbolization, pretend the symbol's address is that
1678df3a07aSAlexey Samsonov     // of the function's code, not the descriptor.
1688df3a07aSAlexey Samsonov     uint64_t OpdOffset = SymbolAddress - OpdAddress;
1698df3a07aSAlexey Samsonov     uint32_t OpdOffset32 = OpdOffset;
1708df3a07aSAlexey Samsonov     if (OpdOffset == OpdOffset32 &&
1718df3a07aSAlexey Samsonov         OpdExtractor->isValidOffsetForAddress(OpdOffset32))
1728df3a07aSAlexey Samsonov       SymbolAddress = OpdExtractor->getAddress(&OpdOffset32);
1738df3a07aSAlexey Samsonov   }
17481e8b7d9SKevin Enderby   Expected<StringRef> SymbolNameOrErr = Symbol.getName();
17581e8b7d9SKevin Enderby   if (!SymbolNameOrErr)
17681e8b7d9SKevin Enderby     return errorToErrorCode(SymbolNameOrErr.takeError());
1778df3a07aSAlexey Samsonov   StringRef SymbolName = *SymbolNameOrErr;
1788df3a07aSAlexey Samsonov   // Mach-O symbol table names have leading underscore, skip it.
17944d95122SEugene Zelenko   if (Module->isMachO() && !SymbolName.empty() && SymbolName[0] == '_')
1808df3a07aSAlexey Samsonov     SymbolName = SymbolName.drop_front();
1818df3a07aSAlexey Samsonov   // FIXME: If a function has alias, there are two entries in symbol table
1828df3a07aSAlexey Samsonov   // with same address size. Make sure we choose the correct one.
1838df3a07aSAlexey Samsonov   auto &M = SymbolType == SymbolRef::ST_Function ? Functions : Objects;
1848df3a07aSAlexey Samsonov   SymbolDesc SD = { SymbolAddress, SymbolSize };
185*afb54fd6SFangrui Song   M.emplace_back(SD, SymbolName);
1868df3a07aSAlexey Samsonov   return std::error_code();
1878df3a07aSAlexey Samsonov }
1888df3a07aSAlexey Samsonov 
1898df3a07aSAlexey Samsonov // Return true if this is a 32-bit x86 PE COFF module.
1908df3a07aSAlexey Samsonov bool SymbolizableObjectFile::isWin32Module() const {
1918df3a07aSAlexey Samsonov   auto *CoffObject = dyn_cast<COFFObjectFile>(Module);
1928df3a07aSAlexey Samsonov   return CoffObject && CoffObject->getMachine() == COFF::IMAGE_FILE_MACHINE_I386;
1938df3a07aSAlexey Samsonov }
1948df3a07aSAlexey Samsonov 
1958df3a07aSAlexey Samsonov uint64_t SymbolizableObjectFile::getModulePreferredBase() const {
1968df3a07aSAlexey Samsonov   if (auto *CoffObject = dyn_cast<COFFObjectFile>(Module))
1978df3a07aSAlexey Samsonov     return CoffObject->getImageBase();
1988df3a07aSAlexey Samsonov   return 0;
1998df3a07aSAlexey Samsonov }
2008df3a07aSAlexey Samsonov 
2018df3a07aSAlexey Samsonov bool SymbolizableObjectFile::getNameFromSymbolTable(SymbolRef::Type Type,
2028df3a07aSAlexey Samsonov                                                     uint64_t Address,
2038df3a07aSAlexey Samsonov                                                     std::string &Name,
2048df3a07aSAlexey Samsonov                                                     uint64_t &Addr,
2058df3a07aSAlexey Samsonov                                                     uint64_t &Size) const {
206*afb54fd6SFangrui Song   const auto &Symbols = Type == SymbolRef::ST_Function ? Functions : Objects;
207*afb54fd6SFangrui Song   std::pair<SymbolDesc, StringRef> SD{{Address, UINT64_C(-1)}, StringRef()};
208e2622b3eSFangrui Song   // SymbolDescs are sorted by (Addr,Size), if several SymbolDescs share the
209e2622b3eSFangrui Song   // same Addr, pick the one with the largest Size. This helps us avoid symbols
210e2622b3eSFangrui Song   // with no size information (Size=0).
211*afb54fd6SFangrui Song   auto SymbolIterator = llvm::upper_bound(Symbols, SD);
212*afb54fd6SFangrui Song   if (SymbolIterator == Symbols.begin())
2138df3a07aSAlexey Samsonov     return false;
2148df3a07aSAlexey Samsonov   --SymbolIterator;
2158df3a07aSAlexey Samsonov   if (SymbolIterator->first.Size != 0 &&
2168df3a07aSAlexey Samsonov       SymbolIterator->first.Addr + SymbolIterator->first.Size <= Address)
2178df3a07aSAlexey Samsonov     return false;
2188df3a07aSAlexey Samsonov   Name = SymbolIterator->second.str();
2198df3a07aSAlexey Samsonov   Addr = SymbolIterator->first.Addr;
2208df3a07aSAlexey Samsonov   Size = SymbolIterator->first.Size;
2218df3a07aSAlexey Samsonov   return true;
2228df3a07aSAlexey Samsonov }
2238df3a07aSAlexey Samsonov 
224c038e2dbSReid Kleckner bool SymbolizableObjectFile::shouldOverrideWithSymbolTable(
225c038e2dbSReid Kleckner     FunctionNameKind FNKind, bool UseSymbolTable) const {
226c038e2dbSReid Kleckner   // When DWARF is used with -gline-tables-only / -gmlt, the symbol table gives
227c038e2dbSReid Kleckner   // better answers for linkage names than the DIContext. Otherwise, we are
228c038e2dbSReid Kleckner   // probably using PEs and PDBs, and we shouldn't do the override. PE files
229c038e2dbSReid Kleckner   // generally only contain the names of exported symbols.
230c038e2dbSReid Kleckner   return FNKind == FunctionNameKind::LinkageName && UseSymbolTable &&
231c038e2dbSReid Kleckner          isa<DWARFContext>(DebugInfoContext.get());
232c038e2dbSReid Kleckner }
233c038e2dbSReid Kleckner 
23477fc1f60SAlexey Lapshin DILineInfo
23577fc1f60SAlexey Lapshin SymbolizableObjectFile::symbolizeCode(object::SectionedAddress ModuleOffset,
2368df3a07aSAlexey Samsonov                                       FunctionNameKind FNKind,
2378df3a07aSAlexey Samsonov                                       bool UseSymbolTable) const {
2388df3a07aSAlexey Samsonov   DILineInfo LineInfo;
239b2c4b8bdSAlexey Lapshin 
240b2c4b8bdSAlexey Lapshin   if (ModuleOffset.SectionIndex == object::SectionedAddress::UndefSection)
241b2c4b8bdSAlexey Lapshin     ModuleOffset.SectionIndex =
242b2c4b8bdSAlexey Lapshin         getModuleSectionIndexForAddress(ModuleOffset.Address);
243b2c4b8bdSAlexey Lapshin 
2448df3a07aSAlexey Samsonov   if (DebugInfoContext) {
2458df3a07aSAlexey Samsonov     LineInfo = DebugInfoContext->getLineInfoForAddress(
2468df3a07aSAlexey Samsonov         ModuleOffset, getDILineInfoSpecifier(FNKind));
2478df3a07aSAlexey Samsonov   }
2488df3a07aSAlexey Samsonov   // Override function name from symbol table if necessary.
249c038e2dbSReid Kleckner   if (shouldOverrideWithSymbolTable(FNKind, UseSymbolTable)) {
2508df3a07aSAlexey Samsonov     std::string FunctionName;
2518df3a07aSAlexey Samsonov     uint64_t Start, Size;
25277fc1f60SAlexey Lapshin     if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset.Address,
2538df3a07aSAlexey Samsonov                                FunctionName, Start, Size)) {
2548df3a07aSAlexey Samsonov       LineInfo.FunctionName = FunctionName;
2558df3a07aSAlexey Samsonov     }
2568df3a07aSAlexey Samsonov   }
2578df3a07aSAlexey Samsonov   return LineInfo;
2588df3a07aSAlexey Samsonov }
2598df3a07aSAlexey Samsonov 
2608df3a07aSAlexey Samsonov DIInliningInfo SymbolizableObjectFile::symbolizeInlinedCode(
26177fc1f60SAlexey Lapshin     object::SectionedAddress ModuleOffset, FunctionNameKind FNKind,
26277fc1f60SAlexey Lapshin     bool UseSymbolTable) const {
2638df3a07aSAlexey Samsonov   DIInliningInfo InlinedContext;
2648df3a07aSAlexey Samsonov 
265b2c4b8bdSAlexey Lapshin   if (ModuleOffset.SectionIndex == object::SectionedAddress::UndefSection)
266b2c4b8bdSAlexey Lapshin     ModuleOffset.SectionIndex =
267b2c4b8bdSAlexey Lapshin         getModuleSectionIndexForAddress(ModuleOffset.Address);
268b2c4b8bdSAlexey Lapshin 
2698df3a07aSAlexey Samsonov   if (DebugInfoContext)
2708df3a07aSAlexey Samsonov     InlinedContext = DebugInfoContext->getInliningInfoForAddress(
2718df3a07aSAlexey Samsonov         ModuleOffset, getDILineInfoSpecifier(FNKind));
2728df3a07aSAlexey Samsonov   // Make sure there is at least one frame in context.
2738df3a07aSAlexey Samsonov   if (InlinedContext.getNumberOfFrames() == 0)
2748df3a07aSAlexey Samsonov     InlinedContext.addFrame(DILineInfo());
2758df3a07aSAlexey Samsonov 
2768df3a07aSAlexey Samsonov   // Override the function name in lower frame with name from symbol table.
277c038e2dbSReid Kleckner   if (shouldOverrideWithSymbolTable(FNKind, UseSymbolTable)) {
2788df3a07aSAlexey Samsonov     std::string FunctionName;
2798df3a07aSAlexey Samsonov     uint64_t Start, Size;
28077fc1f60SAlexey Lapshin     if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset.Address,
2818df3a07aSAlexey Samsonov                                FunctionName, Start, Size)) {
282e46bd741SAlexey Samsonov       InlinedContext.getMutableFrame(InlinedContext.getNumberOfFrames() - 1)
283e46bd741SAlexey Samsonov           ->FunctionName = FunctionName;
2848df3a07aSAlexey Samsonov     }
2858df3a07aSAlexey Samsonov   }
286e46bd741SAlexey Samsonov 
287e46bd741SAlexey Samsonov   return InlinedContext;
2888df3a07aSAlexey Samsonov }
2898df3a07aSAlexey Samsonov 
29077fc1f60SAlexey Lapshin DIGlobal SymbolizableObjectFile::symbolizeData(
29177fc1f60SAlexey Lapshin     object::SectionedAddress ModuleOffset) const {
29276f7ecb8SAlexey Samsonov   DIGlobal Res;
29377fc1f60SAlexey Lapshin   getNameFromSymbolTable(SymbolRef::ST_Data, ModuleOffset.Address, Res.Name,
29477fc1f60SAlexey Lapshin                          Res.Start, Res.Size);
29576f7ecb8SAlexey Samsonov   return Res;
2968df3a07aSAlexey Samsonov }
297b2c4b8bdSAlexey Lapshin 
298b2c4b8bdSAlexey Lapshin /// Search for the first occurence of specified Address in ObjectFile.
299b2c4b8bdSAlexey Lapshin uint64_t SymbolizableObjectFile::getModuleSectionIndexForAddress(
300b2c4b8bdSAlexey Lapshin     uint64_t Address) const {
301b2c4b8bdSAlexey Lapshin 
302b2c4b8bdSAlexey Lapshin   for (SectionRef Sec : Module->sections()) {
303b2c4b8bdSAlexey Lapshin     if (!Sec.isText() || Sec.isVirtual())
304b2c4b8bdSAlexey Lapshin       continue;
305b2c4b8bdSAlexey Lapshin 
306b2c4b8bdSAlexey Lapshin     if (Address >= Sec.getAddress() &&
307b2c4b8bdSAlexey Lapshin         Address <= Sec.getAddress() + Sec.getSize()) {
308b2c4b8bdSAlexey Lapshin       return Sec.getIndex();
309b2c4b8bdSAlexey Lapshin     }
310b2c4b8bdSAlexey Lapshin   }
311b2c4b8bdSAlexey Lapshin 
312b2c4b8bdSAlexey Lapshin   return object::SectionedAddress::UndefSection;
313b2c4b8bdSAlexey Lapshin }
314