xref: /freebsd/contrib/llvm-project/llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp (revision fe6060f10f634930ff71b7c50291ddc610da2475)
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/Triple.h"
160b57cec5SDimitry Andric #include "llvm/BinaryFormat/COFF.h"
170b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFContext.h"
180b57cec5SDimitry Andric #include "llvm/Object/COFF.h"
19*fe6060f1SDimitry Andric #include "llvm/Object/ELFObjectFile.h"
200b57cec5SDimitry Andric #include "llvm/Object/ObjectFile.h"
210b57cec5SDimitry Andric #include "llvm/Object/SymbolSize.h"
220b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
230b57cec5SDimitry Andric #include "llvm/Support/DataExtractor.h"
240b57cec5SDimitry Andric #include <algorithm>
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric using namespace llvm;
270b57cec5SDimitry Andric using namespace object;
280b57cec5SDimitry Andric using namespace symbolize;
290b57cec5SDimitry Andric 
305ffd83dbSDimitry Andric Expected<std::unique_ptr<SymbolizableObjectFile>>
310b57cec5SDimitry Andric SymbolizableObjectFile::create(const object::ObjectFile *Obj,
328bcb0991SDimitry Andric                                std::unique_ptr<DIContext> DICtx,
338bcb0991SDimitry Andric                                bool UntagAddresses) {
340b57cec5SDimitry Andric   assert(DICtx);
350b57cec5SDimitry Andric   std::unique_ptr<SymbolizableObjectFile> res(
368bcb0991SDimitry Andric       new SymbolizableObjectFile(Obj, std::move(DICtx), UntagAddresses));
370b57cec5SDimitry Andric   std::unique_ptr<DataExtractor> OpdExtractor;
380b57cec5SDimitry Andric   uint64_t OpdAddress = 0;
390b57cec5SDimitry Andric   // Find the .opd (function descriptor) section if any, for big-endian
400b57cec5SDimitry Andric   // PowerPC64 ELF.
410b57cec5SDimitry Andric   if (Obj->getArch() == Triple::ppc64) {
420b57cec5SDimitry Andric     for (section_iterator Section : Obj->sections()) {
438bcb0991SDimitry Andric       Expected<StringRef> NameOrErr = Section->getName();
448bcb0991SDimitry Andric       if (!NameOrErr)
455ffd83dbSDimitry Andric         return NameOrErr.takeError();
468bcb0991SDimitry Andric 
478bcb0991SDimitry Andric       if (*NameOrErr == ".opd") {
480b57cec5SDimitry Andric         Expected<StringRef> E = Section->getContents();
490b57cec5SDimitry Andric         if (!E)
505ffd83dbSDimitry Andric           return E.takeError();
510b57cec5SDimitry Andric         OpdExtractor.reset(new DataExtractor(*E, Obj->isLittleEndian(),
520b57cec5SDimitry Andric                                              Obj->getBytesInAddress()));
530b57cec5SDimitry Andric         OpdAddress = Section->getAddress();
540b57cec5SDimitry Andric         break;
550b57cec5SDimitry Andric       }
560b57cec5SDimitry Andric     }
570b57cec5SDimitry Andric   }
580b57cec5SDimitry Andric   std::vector<std::pair<SymbolRef, uint64_t>> Symbols =
590b57cec5SDimitry Andric       computeSymbolSizes(*Obj);
600b57cec5SDimitry Andric   for (auto &P : Symbols)
615ffd83dbSDimitry Andric     if (Error E =
625ffd83dbSDimitry Andric             res->addSymbol(P.first, P.second, OpdExtractor.get(), OpdAddress))
635ffd83dbSDimitry Andric       return std::move(E);
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric   // If this is a COFF object and we didn't find any symbols, try the export
660b57cec5SDimitry Andric   // table.
670b57cec5SDimitry Andric   if (Symbols.empty()) {
680b57cec5SDimitry Andric     if (auto *CoffObj = dyn_cast<COFFObjectFile>(Obj))
695ffd83dbSDimitry Andric       if (Error E = res->addCoffExportSymbols(CoffObj))
705ffd83dbSDimitry Andric         return std::move(E);
710b57cec5SDimitry Andric   }
720b57cec5SDimitry Andric 
73*fe6060f1SDimitry Andric   std::vector<SymbolDesc> &SS = res->Symbols;
740b57cec5SDimitry Andric   // Sort by (Addr,Size,Name). If several SymbolDescs share the same Addr,
750b57cec5SDimitry Andric   // pick the one with the largest Size. This helps us avoid symbols with no
760b57cec5SDimitry Andric   // size information (Size=0).
77*fe6060f1SDimitry Andric   llvm::stable_sort(SS);
78*fe6060f1SDimitry Andric   auto I = SS.begin(), E = SS.end(), J = SS.begin();
790b57cec5SDimitry Andric   while (I != E) {
800b57cec5SDimitry Andric     auto OI = I;
81*fe6060f1SDimitry Andric     while (++I != E && OI->Addr == I->Addr) {
820b57cec5SDimitry Andric     }
830b57cec5SDimitry Andric     *J++ = I[-1];
840b57cec5SDimitry Andric   }
85*fe6060f1SDimitry Andric   SS.erase(J, SS.end());
860b57cec5SDimitry Andric 
870b57cec5SDimitry Andric   return std::move(res);
880b57cec5SDimitry Andric }
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric SymbolizableObjectFile::SymbolizableObjectFile(const ObjectFile *Obj,
918bcb0991SDimitry Andric                                                std::unique_ptr<DIContext> DICtx,
928bcb0991SDimitry Andric                                                bool UntagAddresses)
938bcb0991SDimitry Andric     : Module(Obj), DebugInfoContext(std::move(DICtx)),
948bcb0991SDimitry Andric       UntagAddresses(UntagAddresses) {}
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric namespace {
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric struct OffsetNamePair {
990b57cec5SDimitry Andric   uint32_t Offset;
1000b57cec5SDimitry Andric   StringRef Name;
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric   bool operator<(const OffsetNamePair &R) const {
1030b57cec5SDimitry Andric     return Offset < R.Offset;
1040b57cec5SDimitry Andric   }
1050b57cec5SDimitry Andric };
1060b57cec5SDimitry Andric 
1070b57cec5SDimitry Andric } // end anonymous namespace
1080b57cec5SDimitry Andric 
1095ffd83dbSDimitry Andric Error SymbolizableObjectFile::addCoffExportSymbols(
1100b57cec5SDimitry Andric     const COFFObjectFile *CoffObj) {
1110b57cec5SDimitry Andric   // Get all export names and offsets.
1120b57cec5SDimitry Andric   std::vector<OffsetNamePair> ExportSyms;
1130b57cec5SDimitry Andric   for (const ExportDirectoryEntryRef &Ref : CoffObj->export_directories()) {
1140b57cec5SDimitry Andric     StringRef Name;
1150b57cec5SDimitry Andric     uint32_t Offset;
1160b57cec5SDimitry Andric     if (auto EC = Ref.getSymbolName(Name))
1170b57cec5SDimitry Andric       return EC;
1180b57cec5SDimitry Andric     if (auto EC = Ref.getExportRVA(Offset))
1190b57cec5SDimitry Andric       return EC;
1200b57cec5SDimitry Andric     ExportSyms.push_back(OffsetNamePair{Offset, Name});
1210b57cec5SDimitry Andric   }
1220b57cec5SDimitry Andric   if (ExportSyms.empty())
1235ffd83dbSDimitry Andric     return Error::success();
1240b57cec5SDimitry Andric 
1250b57cec5SDimitry Andric   // Sort by ascending offset.
1260b57cec5SDimitry Andric   array_pod_sort(ExportSyms.begin(), ExportSyms.end());
1270b57cec5SDimitry Andric 
1280b57cec5SDimitry Andric   // Approximate the symbol sizes by assuming they run to the next symbol.
1290b57cec5SDimitry Andric   // FIXME: This assumes all exports are functions.
1300b57cec5SDimitry Andric   uint64_t ImageBase = CoffObj->getImageBase();
1310b57cec5SDimitry Andric   for (auto I = ExportSyms.begin(), E = ExportSyms.end(); I != E; ++I) {
1320b57cec5SDimitry Andric     OffsetNamePair &Export = *I;
1330b57cec5SDimitry Andric     // FIXME: The last export has a one byte size now.
1340b57cec5SDimitry Andric     uint32_t NextOffset = I != E ? I->Offset : Export.Offset + 1;
1350b57cec5SDimitry Andric     uint64_t SymbolStart = ImageBase + Export.Offset;
1360b57cec5SDimitry Andric     uint64_t SymbolSize = NextOffset - Export.Offset;
137*fe6060f1SDimitry Andric     Symbols.push_back({SymbolStart, SymbolSize, Export.Name, 0});
1380b57cec5SDimitry Andric   }
1395ffd83dbSDimitry Andric   return Error::success();
1400b57cec5SDimitry Andric }
1410b57cec5SDimitry Andric 
1425ffd83dbSDimitry Andric Error SymbolizableObjectFile::addSymbol(const SymbolRef &Symbol,
1430b57cec5SDimitry Andric                                         uint64_t SymbolSize,
1440b57cec5SDimitry Andric                                         DataExtractor *OpdExtractor,
1450b57cec5SDimitry Andric                                         uint64_t OpdAddress) {
1460b57cec5SDimitry Andric   // Avoid adding symbols from an unknown/undefined section.
147*fe6060f1SDimitry Andric   const ObjectFile &Obj = *Symbol.getObject();
148*fe6060f1SDimitry Andric   Expected<StringRef> SymbolNameOrErr = Symbol.getName();
149*fe6060f1SDimitry Andric   if (!SymbolNameOrErr)
150*fe6060f1SDimitry Andric     return SymbolNameOrErr.takeError();
151*fe6060f1SDimitry Andric   StringRef SymbolName = *SymbolNameOrErr;
152*fe6060f1SDimitry Andric 
153*fe6060f1SDimitry Andric   uint32_t ELFSymIdx =
154*fe6060f1SDimitry Andric       Obj.isELF() ? ELFSymbolRef(Symbol).getRawDataRefImpl().d.b : 0;
1550b57cec5SDimitry Andric   Expected<section_iterator> Sec = Symbol.getSection();
156*fe6060f1SDimitry Andric   if (!Sec || Obj.section_end() == *Sec) {
157*fe6060f1SDimitry Andric     if (Obj.isELF()) {
158*fe6060f1SDimitry Andric       // Store the (index, filename) pair for a file symbol.
159*fe6060f1SDimitry Andric       ELFSymbolRef ESym(Symbol);
160*fe6060f1SDimitry Andric       if (ESym.getELFType() == ELF::STT_FILE)
161*fe6060f1SDimitry Andric         FileSymbols.emplace_back(ELFSymIdx, SymbolName);
162*fe6060f1SDimitry Andric     }
1635ffd83dbSDimitry Andric     return Error::success();
164*fe6060f1SDimitry Andric   }
165*fe6060f1SDimitry Andric 
1660b57cec5SDimitry Andric   Expected<SymbolRef::Type> SymbolTypeOrErr = Symbol.getType();
1670b57cec5SDimitry Andric   if (!SymbolTypeOrErr)
1685ffd83dbSDimitry Andric     return SymbolTypeOrErr.takeError();
1690b57cec5SDimitry Andric   SymbolRef::Type SymbolType = *SymbolTypeOrErr;
170*fe6060f1SDimitry Andric   if (Obj.isELF()) {
171*fe6060f1SDimitry Andric     // Allow function and data symbols. Additionally allow STT_NONE, which are
172*fe6060f1SDimitry Andric     // common for functions defined in assembly.
173*fe6060f1SDimitry Andric     uint8_t Type = ELFSymbolRef(Symbol).getELFType();
174*fe6060f1SDimitry Andric     if (Type != ELF::STT_NOTYPE && Type != ELF::STT_FUNC &&
175*fe6060f1SDimitry Andric         Type != ELF::STT_OBJECT && Type != ELF::STT_GNU_IFUNC)
1765ffd83dbSDimitry Andric       return Error::success();
177*fe6060f1SDimitry Andric     // Some STT_NOTYPE symbols are not desired. This excludes STT_SECTION and
178*fe6060f1SDimitry Andric     // ARM mapping symbols.
179*fe6060f1SDimitry Andric     uint32_t Flags = cantFail(Symbol.getFlags());
180*fe6060f1SDimitry Andric     if (Flags & SymbolRef::SF_FormatSpecific)
181*fe6060f1SDimitry Andric       return Error::success();
182*fe6060f1SDimitry Andric   } else if (SymbolType != SymbolRef::ST_Function &&
183*fe6060f1SDimitry Andric              SymbolType != SymbolRef::ST_Data) {
184*fe6060f1SDimitry Andric     return Error::success();
185*fe6060f1SDimitry Andric   }
186*fe6060f1SDimitry Andric 
1870b57cec5SDimitry Andric   Expected<uint64_t> SymbolAddressOrErr = Symbol.getAddress();
1880b57cec5SDimitry Andric   if (!SymbolAddressOrErr)
1895ffd83dbSDimitry Andric     return SymbolAddressOrErr.takeError();
1900b57cec5SDimitry Andric   uint64_t SymbolAddress = *SymbolAddressOrErr;
1918bcb0991SDimitry Andric   if (UntagAddresses) {
1928bcb0991SDimitry Andric     // For kernel addresses, bits 56-63 need to be set, so we sign extend bit 55
1938bcb0991SDimitry Andric     // into bits 56-63 instead of masking them out.
1948bcb0991SDimitry Andric     SymbolAddress &= (1ull << 56) - 1;
1958bcb0991SDimitry Andric     SymbolAddress = (int64_t(SymbolAddress) << 8) >> 8;
1968bcb0991SDimitry Andric   }
1970b57cec5SDimitry Andric   if (OpdExtractor) {
1980b57cec5SDimitry Andric     // For big-endian PowerPC64 ELF, symbols in the .opd section refer to
1990b57cec5SDimitry Andric     // function descriptors. The first word of the descriptor is a pointer to
2000b57cec5SDimitry Andric     // the function's code.
2010b57cec5SDimitry Andric     // For the purposes of symbolization, pretend the symbol's address is that
2020b57cec5SDimitry Andric     // of the function's code, not the descriptor.
2030b57cec5SDimitry Andric     uint64_t OpdOffset = SymbolAddress - OpdAddress;
2048bcb0991SDimitry Andric     if (OpdExtractor->isValidOffsetForAddress(OpdOffset))
2058bcb0991SDimitry Andric       SymbolAddress = OpdExtractor->getAddress(&OpdOffset);
2060b57cec5SDimitry Andric   }
2070b57cec5SDimitry Andric   // Mach-O symbol table names have leading underscore, skip it.
2080b57cec5SDimitry Andric   if (Module->isMachO() && !SymbolName.empty() && SymbolName[0] == '_')
2090b57cec5SDimitry Andric     SymbolName = SymbolName.drop_front();
210*fe6060f1SDimitry Andric 
211*fe6060f1SDimitry Andric   if (Obj.isELF() && ELFSymbolRef(Symbol).getBinding() != ELF::STB_LOCAL)
212*fe6060f1SDimitry Andric     ELFSymIdx = 0;
213*fe6060f1SDimitry Andric   Symbols.push_back({SymbolAddress, SymbolSize, SymbolName, ELFSymIdx});
2145ffd83dbSDimitry Andric   return Error::success();
2150b57cec5SDimitry Andric }
2160b57cec5SDimitry Andric 
2170b57cec5SDimitry Andric // Return true if this is a 32-bit x86 PE COFF module.
2180b57cec5SDimitry Andric bool SymbolizableObjectFile::isWin32Module() const {
2190b57cec5SDimitry Andric   auto *CoffObject = dyn_cast<COFFObjectFile>(Module);
2200b57cec5SDimitry Andric   return CoffObject && CoffObject->getMachine() == COFF::IMAGE_FILE_MACHINE_I386;
2210b57cec5SDimitry Andric }
2220b57cec5SDimitry Andric 
2230b57cec5SDimitry Andric uint64_t SymbolizableObjectFile::getModulePreferredBase() const {
2240b57cec5SDimitry Andric   if (auto *CoffObject = dyn_cast<COFFObjectFile>(Module))
2250b57cec5SDimitry Andric     return CoffObject->getImageBase();
2260b57cec5SDimitry Andric   return 0;
2270b57cec5SDimitry Andric }
2280b57cec5SDimitry Andric 
229*fe6060f1SDimitry Andric bool SymbolizableObjectFile::getNameFromSymbolTable(
230*fe6060f1SDimitry Andric     uint64_t Address, std::string &Name, uint64_t &Addr, uint64_t &Size,
231*fe6060f1SDimitry Andric     std::string &FileName) const {
232*fe6060f1SDimitry Andric   SymbolDesc SD{Address, UINT64_C(-1), StringRef(), 0};
2330b57cec5SDimitry Andric   auto SymbolIterator = llvm::upper_bound(Symbols, SD);
2340b57cec5SDimitry Andric   if (SymbolIterator == Symbols.begin())
2350b57cec5SDimitry Andric     return false;
2360b57cec5SDimitry Andric   --SymbolIterator;
237*fe6060f1SDimitry Andric   if (SymbolIterator->Size != 0 &&
238*fe6060f1SDimitry Andric       SymbolIterator->Addr + SymbolIterator->Size <= Address)
2390b57cec5SDimitry Andric     return false;
240*fe6060f1SDimitry Andric   Name = SymbolIterator->Name.str();
241*fe6060f1SDimitry Andric   Addr = SymbolIterator->Addr;
242*fe6060f1SDimitry Andric   Size = SymbolIterator->Size;
243*fe6060f1SDimitry Andric 
244*fe6060f1SDimitry Andric   if (SymbolIterator->ELFLocalSymIdx != 0) {
245*fe6060f1SDimitry Andric     // If this is an ELF local symbol, find the STT_FILE symbol preceding
246*fe6060f1SDimitry Andric     // SymbolIterator to get the filename. The ELF spec requires the STT_FILE
247*fe6060f1SDimitry Andric     // symbol (if present) precedes the other STB_LOCAL symbols for the file.
248*fe6060f1SDimitry Andric     assert(Module->isELF());
249*fe6060f1SDimitry Andric     auto It = llvm::upper_bound(
250*fe6060f1SDimitry Andric         FileSymbols,
251*fe6060f1SDimitry Andric         std::make_pair(SymbolIterator->ELFLocalSymIdx, StringRef()));
252*fe6060f1SDimitry Andric     if (It != FileSymbols.begin())
253*fe6060f1SDimitry Andric       FileName = It[-1].second.str();
254*fe6060f1SDimitry Andric   }
2550b57cec5SDimitry Andric   return true;
2560b57cec5SDimitry Andric }
2570b57cec5SDimitry Andric 
2580b57cec5SDimitry Andric bool SymbolizableObjectFile::shouldOverrideWithSymbolTable(
2590b57cec5SDimitry Andric     FunctionNameKind FNKind, bool UseSymbolTable) const {
2600b57cec5SDimitry Andric   // When DWARF is used with -gline-tables-only / -gmlt, the symbol table gives
2610b57cec5SDimitry Andric   // better answers for linkage names than the DIContext. Otherwise, we are
2620b57cec5SDimitry Andric   // probably using PEs and PDBs, and we shouldn't do the override. PE files
2630b57cec5SDimitry Andric   // generally only contain the names of exported symbols.
2640b57cec5SDimitry Andric   return FNKind == FunctionNameKind::LinkageName && UseSymbolTable &&
2650b57cec5SDimitry Andric          isa<DWARFContext>(DebugInfoContext.get());
2660b57cec5SDimitry Andric }
2670b57cec5SDimitry Andric 
2680b57cec5SDimitry Andric DILineInfo
2690b57cec5SDimitry Andric SymbolizableObjectFile::symbolizeCode(object::SectionedAddress ModuleOffset,
2705ffd83dbSDimitry Andric                                       DILineInfoSpecifier LineInfoSpecifier,
2710b57cec5SDimitry Andric                                       bool UseSymbolTable) const {
2720b57cec5SDimitry Andric   if (ModuleOffset.SectionIndex == object::SectionedAddress::UndefSection)
2730b57cec5SDimitry Andric     ModuleOffset.SectionIndex =
2740b57cec5SDimitry Andric         getModuleSectionIndexForAddress(ModuleOffset.Address);
2755ffd83dbSDimitry Andric   DILineInfo LineInfo =
2765ffd83dbSDimitry Andric       DebugInfoContext->getLineInfoForAddress(ModuleOffset, LineInfoSpecifier);
2770b57cec5SDimitry Andric 
2780b57cec5SDimitry Andric   // Override function name from symbol table if necessary.
2795ffd83dbSDimitry Andric   if (shouldOverrideWithSymbolTable(LineInfoSpecifier.FNKind, UseSymbolTable)) {
280*fe6060f1SDimitry Andric     std::string FunctionName, FileName;
2810b57cec5SDimitry Andric     uint64_t Start, Size;
282*fe6060f1SDimitry Andric     if (getNameFromSymbolTable(ModuleOffset.Address, FunctionName, Start, Size,
283*fe6060f1SDimitry Andric                                FileName)) {
2840b57cec5SDimitry Andric       LineInfo.FunctionName = FunctionName;
285*fe6060f1SDimitry Andric       LineInfo.StartAddress = Start;
286*fe6060f1SDimitry Andric       if (LineInfo.FileName == DILineInfo::BadString && !FileName.empty())
287*fe6060f1SDimitry Andric         LineInfo.FileName = FileName;
2880b57cec5SDimitry Andric     }
2890b57cec5SDimitry Andric   }
2900b57cec5SDimitry Andric   return LineInfo;
2910b57cec5SDimitry Andric }
2920b57cec5SDimitry Andric 
2930b57cec5SDimitry Andric DIInliningInfo SymbolizableObjectFile::symbolizeInlinedCode(
2945ffd83dbSDimitry Andric     object::SectionedAddress ModuleOffset,
2955ffd83dbSDimitry Andric     DILineInfoSpecifier LineInfoSpecifier, bool UseSymbolTable) const {
2960b57cec5SDimitry Andric   if (ModuleOffset.SectionIndex == object::SectionedAddress::UndefSection)
2970b57cec5SDimitry Andric     ModuleOffset.SectionIndex =
2980b57cec5SDimitry Andric         getModuleSectionIndexForAddress(ModuleOffset.Address);
2990b57cec5SDimitry Andric   DIInliningInfo InlinedContext = DebugInfoContext->getInliningInfoForAddress(
3005ffd83dbSDimitry Andric       ModuleOffset, LineInfoSpecifier);
3010b57cec5SDimitry Andric 
3020b57cec5SDimitry Andric   // Make sure there is at least one frame in context.
3030b57cec5SDimitry Andric   if (InlinedContext.getNumberOfFrames() == 0)
3040b57cec5SDimitry Andric     InlinedContext.addFrame(DILineInfo());
3050b57cec5SDimitry Andric 
3060b57cec5SDimitry Andric   // Override the function name in lower frame with name from symbol table.
3075ffd83dbSDimitry Andric   if (shouldOverrideWithSymbolTable(LineInfoSpecifier.FNKind, UseSymbolTable)) {
308*fe6060f1SDimitry Andric     std::string FunctionName, FileName;
3090b57cec5SDimitry Andric     uint64_t Start, Size;
310*fe6060f1SDimitry Andric     if (getNameFromSymbolTable(ModuleOffset.Address, FunctionName, Start, Size,
311*fe6060f1SDimitry Andric                                FileName)) {
312*fe6060f1SDimitry Andric       DILineInfo *LI = InlinedContext.getMutableFrame(
313*fe6060f1SDimitry Andric           InlinedContext.getNumberOfFrames() - 1);
314*fe6060f1SDimitry Andric       LI->FunctionName = FunctionName;
315*fe6060f1SDimitry Andric       LI->StartAddress = Start;
316*fe6060f1SDimitry Andric       if (LI->FileName == DILineInfo::BadString && !FileName.empty())
317*fe6060f1SDimitry Andric         LI->FileName = FileName;
3180b57cec5SDimitry Andric     }
3190b57cec5SDimitry Andric   }
3200b57cec5SDimitry Andric 
3210b57cec5SDimitry Andric   return InlinedContext;
3220b57cec5SDimitry Andric }
3230b57cec5SDimitry Andric 
3240b57cec5SDimitry Andric DIGlobal SymbolizableObjectFile::symbolizeData(
3250b57cec5SDimitry Andric     object::SectionedAddress ModuleOffset) const {
3260b57cec5SDimitry Andric   DIGlobal Res;
327*fe6060f1SDimitry Andric   std::string FileName;
328*fe6060f1SDimitry Andric   getNameFromSymbolTable(ModuleOffset.Address, Res.Name, Res.Start, Res.Size,
329*fe6060f1SDimitry Andric                          FileName);
3300b57cec5SDimitry Andric   return Res;
3310b57cec5SDimitry Andric }
3320b57cec5SDimitry Andric 
3330b57cec5SDimitry Andric std::vector<DILocal> SymbolizableObjectFile::symbolizeFrame(
3340b57cec5SDimitry Andric     object::SectionedAddress ModuleOffset) const {
3350b57cec5SDimitry Andric   if (ModuleOffset.SectionIndex == object::SectionedAddress::UndefSection)
3360b57cec5SDimitry Andric     ModuleOffset.SectionIndex =
3370b57cec5SDimitry Andric         getModuleSectionIndexForAddress(ModuleOffset.Address);
3380b57cec5SDimitry Andric   return DebugInfoContext->getLocalsForAddress(ModuleOffset);
3390b57cec5SDimitry Andric }
3400b57cec5SDimitry Andric 
3410b57cec5SDimitry Andric /// Search for the first occurence of specified Address in ObjectFile.
3420b57cec5SDimitry Andric uint64_t SymbolizableObjectFile::getModuleSectionIndexForAddress(
3430b57cec5SDimitry Andric     uint64_t Address) const {
3440b57cec5SDimitry Andric 
3450b57cec5SDimitry Andric   for (SectionRef Sec : Module->sections()) {
3460b57cec5SDimitry Andric     if (!Sec.isText() || Sec.isVirtual())
3470b57cec5SDimitry Andric       continue;
3480b57cec5SDimitry Andric 
3490b57cec5SDimitry Andric     if (Address >= Sec.getAddress() &&
3500b57cec5SDimitry Andric         Address < Sec.getAddress() + Sec.getSize())
3510b57cec5SDimitry Andric       return Sec.getIndex();
3520b57cec5SDimitry Andric   }
3530b57cec5SDimitry Andric 
3540b57cec5SDimitry Andric   return object::SectionedAddress::UndefSection;
3550b57cec5SDimitry Andric }
356