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
1381ad6265SDimitry Andric #include "llvm/DebugInfo/Symbolize/SymbolizableObjectFile.h"
140b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
150b57cec5SDimitry Andric #include "llvm/BinaryFormat/COFF.h"
160b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFContext.h"
170b57cec5SDimitry Andric #include "llvm/Object/COFF.h"
18fe6060f1SDimitry Andric #include "llvm/Object/ELFObjectFile.h"
190b57cec5SDimitry Andric #include "llvm/Object/ObjectFile.h"
200b57cec5SDimitry Andric #include "llvm/Object/SymbolSize.h"
210b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
220b57cec5SDimitry Andric #include "llvm/Support/DataExtractor.h"
2306c3fb27SDimitry Andric #include "llvm/TargetParser/Triple.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>>
create(const object::ObjectFile * Obj,std::unique_ptr<DIContext> DICtx,bool UntagAddresses)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
73fe6060f1SDimitry 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).
77fe6060f1SDimitry Andric llvm::stable_sort(SS);
78fe6060f1SDimitry Andric auto I = SS.begin(), E = SS.end(), J = SS.begin();
790b57cec5SDimitry Andric while (I != E) {
800b57cec5SDimitry Andric auto OI = I;
81fe6060f1SDimitry Andric while (++I != E && OI->Addr == I->Addr) {
820b57cec5SDimitry Andric }
830b57cec5SDimitry Andric *J++ = I[-1];
840b57cec5SDimitry Andric }
85fe6060f1SDimitry Andric SS.erase(J, SS.end());
860b57cec5SDimitry Andric
870b57cec5SDimitry Andric return std::move(res);
880b57cec5SDimitry Andric }
890b57cec5SDimitry Andric
SymbolizableObjectFile(const ObjectFile * Obj,std::unique_ptr<DIContext> DICtx,bool UntagAddresses)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
operator <__anon03ee34830111::OffsetNamePair1020b57cec5SDimitry 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
addCoffExportSymbols(const COFFObjectFile * CoffObj)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;
137fe6060f1SDimitry Andric Symbols.push_back({SymbolStart, SymbolSize, Export.Name, 0});
1380b57cec5SDimitry Andric }
1395ffd83dbSDimitry Andric return Error::success();
1400b57cec5SDimitry Andric }
1410b57cec5SDimitry Andric
addSymbol(const SymbolRef & Symbol,uint64_t SymbolSize,DataExtractor * OpdExtractor,uint64_t OpdAddress)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.
147fe6060f1SDimitry Andric const ObjectFile &Obj = *Symbol.getObject();
148fe6060f1SDimitry Andric Expected<StringRef> SymbolNameOrErr = Symbol.getName();
149fe6060f1SDimitry Andric if (!SymbolNameOrErr)
150fe6060f1SDimitry Andric return SymbolNameOrErr.takeError();
151fe6060f1SDimitry Andric StringRef SymbolName = *SymbolNameOrErr;
152fe6060f1SDimitry Andric
153fe6060f1SDimitry Andric uint32_t ELFSymIdx =
154fe6060f1SDimitry Andric Obj.isELF() ? ELFSymbolRef(Symbol).getRawDataRefImpl().d.b : 0;
1550b57cec5SDimitry Andric Expected<section_iterator> Sec = Symbol.getSection();
156fe6060f1SDimitry Andric if (!Sec || Obj.section_end() == *Sec) {
157fe6060f1SDimitry Andric if (Obj.isELF()) {
158fe6060f1SDimitry Andric // Store the (index, filename) pair for a file symbol.
159fe6060f1SDimitry Andric ELFSymbolRef ESym(Symbol);
160fe6060f1SDimitry Andric if (ESym.getELFType() == ELF::STT_FILE)
161fe6060f1SDimitry Andric FileSymbols.emplace_back(ELFSymIdx, SymbolName);
162fe6060f1SDimitry Andric }
1635ffd83dbSDimitry Andric return Error::success();
164fe6060f1SDimitry Andric }
165fe6060f1SDimitry Andric
1660b57cec5SDimitry Andric Expected<SymbolRef::Type> SymbolTypeOrErr = Symbol.getType();
1670b57cec5SDimitry Andric if (!SymbolTypeOrErr)
1685ffd83dbSDimitry Andric return SymbolTypeOrErr.takeError();
1690b57cec5SDimitry Andric SymbolRef::Type SymbolType = *SymbolTypeOrErr;
170fe6060f1SDimitry Andric if (Obj.isELF()) {
171bdd1243dSDimitry Andric // Ignore any symbols coming from sections that don't have runtime
172bdd1243dSDimitry Andric // allocated memory.
173bdd1243dSDimitry Andric if ((elf_section_iterator(*Sec)->getFlags() & ELF::SHF_ALLOC) == 0)
174bdd1243dSDimitry Andric return Error::success();
175bdd1243dSDimitry Andric
176fe6060f1SDimitry Andric // Allow function and data symbols. Additionally allow STT_NONE, which are
177fe6060f1SDimitry Andric // common for functions defined in assembly.
178fe6060f1SDimitry Andric uint8_t Type = ELFSymbolRef(Symbol).getELFType();
179fe6060f1SDimitry Andric if (Type != ELF::STT_NOTYPE && Type != ELF::STT_FUNC &&
180fe6060f1SDimitry Andric Type != ELF::STT_OBJECT && Type != ELF::STT_GNU_IFUNC)
1815ffd83dbSDimitry Andric return Error::success();
182fe6060f1SDimitry Andric // Some STT_NOTYPE symbols are not desired. This excludes STT_SECTION and
183fe6060f1SDimitry Andric // ARM mapping symbols.
184fe6060f1SDimitry Andric uint32_t Flags = cantFail(Symbol.getFlags());
185fe6060f1SDimitry Andric if (Flags & SymbolRef::SF_FormatSpecific)
186fe6060f1SDimitry Andric return Error::success();
187fe6060f1SDimitry Andric } else if (SymbolType != SymbolRef::ST_Function &&
188fe6060f1SDimitry Andric SymbolType != SymbolRef::ST_Data) {
189fe6060f1SDimitry Andric return Error::success();
190fe6060f1SDimitry Andric }
191fe6060f1SDimitry Andric
1920b57cec5SDimitry Andric Expected<uint64_t> SymbolAddressOrErr = Symbol.getAddress();
1930b57cec5SDimitry Andric if (!SymbolAddressOrErr)
1945ffd83dbSDimitry Andric return SymbolAddressOrErr.takeError();
1950b57cec5SDimitry Andric uint64_t SymbolAddress = *SymbolAddressOrErr;
1968bcb0991SDimitry Andric if (UntagAddresses) {
1978bcb0991SDimitry Andric // For kernel addresses, bits 56-63 need to be set, so we sign extend bit 55
1988bcb0991SDimitry Andric // into bits 56-63 instead of masking them out.
1998bcb0991SDimitry Andric SymbolAddress &= (1ull << 56) - 1;
2008bcb0991SDimitry Andric SymbolAddress = (int64_t(SymbolAddress) << 8) >> 8;
2018bcb0991SDimitry Andric }
2020b57cec5SDimitry Andric if (OpdExtractor) {
2030b57cec5SDimitry Andric // For big-endian PowerPC64 ELF, symbols in the .opd section refer to
2040b57cec5SDimitry Andric // function descriptors. The first word of the descriptor is a pointer to
2050b57cec5SDimitry Andric // the function's code.
2060b57cec5SDimitry Andric // For the purposes of symbolization, pretend the symbol's address is that
2070b57cec5SDimitry Andric // of the function's code, not the descriptor.
2080b57cec5SDimitry Andric uint64_t OpdOffset = SymbolAddress - OpdAddress;
2098bcb0991SDimitry Andric if (OpdExtractor->isValidOffsetForAddress(OpdOffset))
2108bcb0991SDimitry Andric SymbolAddress = OpdExtractor->getAddress(&OpdOffset);
2110b57cec5SDimitry Andric }
2120b57cec5SDimitry Andric // Mach-O symbol table names have leading underscore, skip it.
2137a6dacacSDimitry Andric if (Module->isMachO())
2147a6dacacSDimitry Andric SymbolName.consume_front("_");
215fe6060f1SDimitry Andric
216fe6060f1SDimitry Andric if (Obj.isELF() && ELFSymbolRef(Symbol).getBinding() != ELF::STB_LOCAL)
217fe6060f1SDimitry Andric ELFSymIdx = 0;
218fe6060f1SDimitry Andric Symbols.push_back({SymbolAddress, SymbolSize, SymbolName, ELFSymIdx});
2195ffd83dbSDimitry Andric return Error::success();
2200b57cec5SDimitry Andric }
2210b57cec5SDimitry Andric
2220b57cec5SDimitry Andric // Return true if this is a 32-bit x86 PE COFF module.
isWin32Module() const2230b57cec5SDimitry Andric bool SymbolizableObjectFile::isWin32Module() const {
2240b57cec5SDimitry Andric auto *CoffObject = dyn_cast<COFFObjectFile>(Module);
2250b57cec5SDimitry Andric return CoffObject && CoffObject->getMachine() == COFF::IMAGE_FILE_MACHINE_I386;
2260b57cec5SDimitry Andric }
2270b57cec5SDimitry Andric
getModulePreferredBase() const2280b57cec5SDimitry Andric uint64_t SymbolizableObjectFile::getModulePreferredBase() const {
2290b57cec5SDimitry Andric if (auto *CoffObject = dyn_cast<COFFObjectFile>(Module))
2300b57cec5SDimitry Andric return CoffObject->getImageBase();
2310b57cec5SDimitry Andric return 0;
2320b57cec5SDimitry Andric }
2330b57cec5SDimitry Andric
getNameFromSymbolTable(uint64_t Address,std::string & Name,uint64_t & Addr,uint64_t & Size,std::string & FileName) const234fe6060f1SDimitry Andric bool SymbolizableObjectFile::getNameFromSymbolTable(
235fe6060f1SDimitry Andric uint64_t Address, std::string &Name, uint64_t &Addr, uint64_t &Size,
236fe6060f1SDimitry Andric std::string &FileName) const {
237fe6060f1SDimitry Andric SymbolDesc SD{Address, UINT64_C(-1), StringRef(), 0};
2380b57cec5SDimitry Andric auto SymbolIterator = llvm::upper_bound(Symbols, SD);
2390b57cec5SDimitry Andric if (SymbolIterator == Symbols.begin())
2400b57cec5SDimitry Andric return false;
2410b57cec5SDimitry Andric --SymbolIterator;
242fe6060f1SDimitry Andric if (SymbolIterator->Size != 0 &&
243fe6060f1SDimitry Andric SymbolIterator->Addr + SymbolIterator->Size <= Address)
2440b57cec5SDimitry Andric return false;
245fe6060f1SDimitry Andric Name = SymbolIterator->Name.str();
246fe6060f1SDimitry Andric Addr = SymbolIterator->Addr;
247fe6060f1SDimitry Andric Size = SymbolIterator->Size;
248fe6060f1SDimitry Andric
249fe6060f1SDimitry Andric if (SymbolIterator->ELFLocalSymIdx != 0) {
250fe6060f1SDimitry Andric // If this is an ELF local symbol, find the STT_FILE symbol preceding
251fe6060f1SDimitry Andric // SymbolIterator to get the filename. The ELF spec requires the STT_FILE
252fe6060f1SDimitry Andric // symbol (if present) precedes the other STB_LOCAL symbols for the file.
253fe6060f1SDimitry Andric assert(Module->isELF());
254fe6060f1SDimitry Andric auto It = llvm::upper_bound(
255fe6060f1SDimitry Andric FileSymbols,
256fe6060f1SDimitry Andric std::make_pair(SymbolIterator->ELFLocalSymIdx, StringRef()));
257fe6060f1SDimitry Andric if (It != FileSymbols.begin())
258fe6060f1SDimitry Andric FileName = It[-1].second.str();
259fe6060f1SDimitry Andric }
2600b57cec5SDimitry Andric return true;
2610b57cec5SDimitry Andric }
2620b57cec5SDimitry Andric
shouldOverrideWithSymbolTable(FunctionNameKind FNKind,bool UseSymbolTable) const2630b57cec5SDimitry Andric bool SymbolizableObjectFile::shouldOverrideWithSymbolTable(
2640b57cec5SDimitry Andric FunctionNameKind FNKind, bool UseSymbolTable) const {
2650b57cec5SDimitry Andric // When DWARF is used with -gline-tables-only / -gmlt, the symbol table gives
2660b57cec5SDimitry Andric // better answers for linkage names than the DIContext. Otherwise, we are
2670b57cec5SDimitry Andric // probably using PEs and PDBs, and we shouldn't do the override. PE files
2680b57cec5SDimitry Andric // generally only contain the names of exported symbols.
2690b57cec5SDimitry Andric return FNKind == FunctionNameKind::LinkageName && UseSymbolTable &&
2700b57cec5SDimitry Andric isa<DWARFContext>(DebugInfoContext.get());
2710b57cec5SDimitry Andric }
2720b57cec5SDimitry Andric
2730b57cec5SDimitry Andric DILineInfo
symbolizeCode(object::SectionedAddress ModuleOffset,DILineInfoSpecifier LineInfoSpecifier,bool UseSymbolTable) const2740b57cec5SDimitry Andric SymbolizableObjectFile::symbolizeCode(object::SectionedAddress ModuleOffset,
2755ffd83dbSDimitry Andric DILineInfoSpecifier LineInfoSpecifier,
2760b57cec5SDimitry Andric bool UseSymbolTable) const {
2770b57cec5SDimitry Andric if (ModuleOffset.SectionIndex == object::SectionedAddress::UndefSection)
2780b57cec5SDimitry Andric ModuleOffset.SectionIndex =
2790b57cec5SDimitry Andric getModuleSectionIndexForAddress(ModuleOffset.Address);
2805ffd83dbSDimitry Andric DILineInfo LineInfo =
2815ffd83dbSDimitry Andric DebugInfoContext->getLineInfoForAddress(ModuleOffset, LineInfoSpecifier);
2820b57cec5SDimitry Andric
2830b57cec5SDimitry Andric // Override function name from symbol table if necessary.
2845ffd83dbSDimitry Andric if (shouldOverrideWithSymbolTable(LineInfoSpecifier.FNKind, UseSymbolTable)) {
285fe6060f1SDimitry Andric std::string FunctionName, FileName;
2860b57cec5SDimitry Andric uint64_t Start, Size;
287fe6060f1SDimitry Andric if (getNameFromSymbolTable(ModuleOffset.Address, FunctionName, Start, Size,
288fe6060f1SDimitry Andric FileName)) {
2890b57cec5SDimitry Andric LineInfo.FunctionName = FunctionName;
290fe6060f1SDimitry Andric LineInfo.StartAddress = Start;
291fe6060f1SDimitry Andric if (LineInfo.FileName == DILineInfo::BadString && !FileName.empty())
292fe6060f1SDimitry Andric LineInfo.FileName = FileName;
2930b57cec5SDimitry Andric }
2940b57cec5SDimitry Andric }
2950b57cec5SDimitry Andric return LineInfo;
2960b57cec5SDimitry Andric }
2970b57cec5SDimitry Andric
symbolizeInlinedCode(object::SectionedAddress ModuleOffset,DILineInfoSpecifier LineInfoSpecifier,bool UseSymbolTable) const2980b57cec5SDimitry Andric DIInliningInfo SymbolizableObjectFile::symbolizeInlinedCode(
2995ffd83dbSDimitry Andric object::SectionedAddress ModuleOffset,
3005ffd83dbSDimitry Andric DILineInfoSpecifier LineInfoSpecifier, bool UseSymbolTable) const {
3010b57cec5SDimitry Andric if (ModuleOffset.SectionIndex == object::SectionedAddress::UndefSection)
3020b57cec5SDimitry Andric ModuleOffset.SectionIndex =
3030b57cec5SDimitry Andric getModuleSectionIndexForAddress(ModuleOffset.Address);
3040b57cec5SDimitry Andric DIInliningInfo InlinedContext = DebugInfoContext->getInliningInfoForAddress(
3055ffd83dbSDimitry Andric ModuleOffset, LineInfoSpecifier);
3060b57cec5SDimitry Andric
3070b57cec5SDimitry Andric // Make sure there is at least one frame in context.
3080b57cec5SDimitry Andric if (InlinedContext.getNumberOfFrames() == 0)
3090b57cec5SDimitry Andric InlinedContext.addFrame(DILineInfo());
3100b57cec5SDimitry Andric
3110b57cec5SDimitry Andric // Override the function name in lower frame with name from symbol table.
3125ffd83dbSDimitry Andric if (shouldOverrideWithSymbolTable(LineInfoSpecifier.FNKind, UseSymbolTable)) {
313fe6060f1SDimitry Andric std::string FunctionName, FileName;
3140b57cec5SDimitry Andric uint64_t Start, Size;
315fe6060f1SDimitry Andric if (getNameFromSymbolTable(ModuleOffset.Address, FunctionName, Start, Size,
316fe6060f1SDimitry Andric FileName)) {
317fe6060f1SDimitry Andric DILineInfo *LI = InlinedContext.getMutableFrame(
318fe6060f1SDimitry Andric InlinedContext.getNumberOfFrames() - 1);
319fe6060f1SDimitry Andric LI->FunctionName = FunctionName;
320fe6060f1SDimitry Andric LI->StartAddress = Start;
321fe6060f1SDimitry Andric if (LI->FileName == DILineInfo::BadString && !FileName.empty())
322fe6060f1SDimitry Andric LI->FileName = FileName;
3230b57cec5SDimitry Andric }
3240b57cec5SDimitry Andric }
3250b57cec5SDimitry Andric
3260b57cec5SDimitry Andric return InlinedContext;
3270b57cec5SDimitry Andric }
3280b57cec5SDimitry Andric
symbolizeData(object::SectionedAddress ModuleOffset) const3290b57cec5SDimitry Andric DIGlobal SymbolizableObjectFile::symbolizeData(
3300b57cec5SDimitry Andric object::SectionedAddress ModuleOffset) const {
3310b57cec5SDimitry Andric DIGlobal Res;
332fe6060f1SDimitry Andric std::string FileName;
333fe6060f1SDimitry Andric getNameFromSymbolTable(ModuleOffset.Address, Res.Name, Res.Start, Res.Size,
334fe6060f1SDimitry Andric FileName);
33581ad6265SDimitry Andric Res.DeclFile = FileName;
33681ad6265SDimitry Andric
33781ad6265SDimitry Andric // Try and get a better filename:lineno pair from the debuginfo, if present.
33881ad6265SDimitry Andric DILineInfo DL = DebugInfoContext->getLineInfoForDataAddress(ModuleOffset);
33981ad6265SDimitry Andric if (DL.Line != 0) {
34081ad6265SDimitry Andric Res.DeclFile = DL.FileName;
34181ad6265SDimitry Andric Res.DeclLine = DL.Line;
34281ad6265SDimitry Andric }
3430b57cec5SDimitry Andric return Res;
3440b57cec5SDimitry Andric }
3450b57cec5SDimitry Andric
symbolizeFrame(object::SectionedAddress ModuleOffset) const3460b57cec5SDimitry Andric std::vector<DILocal> SymbolizableObjectFile::symbolizeFrame(
3470b57cec5SDimitry Andric object::SectionedAddress ModuleOffset) const {
3480b57cec5SDimitry Andric if (ModuleOffset.SectionIndex == object::SectionedAddress::UndefSection)
3490b57cec5SDimitry Andric ModuleOffset.SectionIndex =
3500b57cec5SDimitry Andric getModuleSectionIndexForAddress(ModuleOffset.Address);
3510b57cec5SDimitry Andric return DebugInfoContext->getLocalsForAddress(ModuleOffset);
3520b57cec5SDimitry Andric }
3530b57cec5SDimitry Andric
3545f757f3fSDimitry Andric std::vector<object::SectionedAddress>
findSymbol(StringRef Symbol,uint64_t Offset) const3555f757f3fSDimitry Andric SymbolizableObjectFile::findSymbol(StringRef Symbol, uint64_t Offset) const {
3565f757f3fSDimitry Andric std::vector<object::SectionedAddress> Result;
3575f757f3fSDimitry Andric for (const SymbolDesc &Sym : Symbols) {
358*0fca6ea1SDimitry Andric if (Sym.Name == Symbol) {
3595f757f3fSDimitry Andric uint64_t Addr = Sym.Addr;
3605f757f3fSDimitry Andric if (Offset < Sym.Size)
3615f757f3fSDimitry Andric Addr += Offset;
3625f757f3fSDimitry Andric object::SectionedAddress A{Addr, getModuleSectionIndexForAddress(Addr)};
3635f757f3fSDimitry Andric Result.push_back(A);
3645f757f3fSDimitry Andric }
3655f757f3fSDimitry Andric }
3665f757f3fSDimitry Andric return Result;
3675f757f3fSDimitry Andric }
3685f757f3fSDimitry Andric
3690b57cec5SDimitry Andric /// Search for the first occurence of specified Address in ObjectFile.
getModuleSectionIndexForAddress(uint64_t Address) const3700b57cec5SDimitry Andric uint64_t SymbolizableObjectFile::getModuleSectionIndexForAddress(
3710b57cec5SDimitry Andric uint64_t Address) const {
3720b57cec5SDimitry Andric
3730b57cec5SDimitry Andric for (SectionRef Sec : Module->sections()) {
3740b57cec5SDimitry Andric if (!Sec.isText() || Sec.isVirtual())
3750b57cec5SDimitry Andric continue;
3760b57cec5SDimitry Andric
3770b57cec5SDimitry Andric if (Address >= Sec.getAddress() &&
3780b57cec5SDimitry Andric Address < Sec.getAddress() + Sec.getSize())
3790b57cec5SDimitry Andric return Sec.getIndex();
3800b57cec5SDimitry Andric }
3810b57cec5SDimitry Andric
3820b57cec5SDimitry Andric return object::SectionedAddress::UndefSection;
3830b57cec5SDimitry Andric }
384