1//===- SymbolizableObjectFile.cpp -----------------------------------------===// 3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4// See https://llvm.org/LICENSE.txt for license information. 5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7//===----------------------------------------------------------------------===// 9// Implementation of SymbolizableObjectFile class. 11//===----------------------------------------------------------------------===// 27using namespacesymbolize;
31 std::unique_ptr<DIContext> DICtx,
34 std::unique_ptr<SymbolizableObjectFile> res(
36 std::unique_ptr<DataExtractor> OpdExtractor;
38// Find the .opd (function descriptor) section if any, for big-endian 46if (*NameOrErr ==
".opd") {
52 OpdAddress = Section->getAddress();
57 std::vector<std::pair<SymbolRef, uint64_t>> Symbols =
59for (
auto &
P : Symbols)
61 res->addSymbol(
P.first,
P.second, OpdExtractor.get(), OpdAddress))
64// If this is a COFF object and we didn't find any symbols, try the export 67if (
auto *CoffObj = dyn_cast<COFFObjectFile>(Obj))
68if (
Error E = res->addCoffExportSymbols(CoffObj))
72 std::vector<SymbolDesc> &SS = res->Symbols;
73// Sort by (Addr,Size,Name). If several SymbolDescs share the same Addr, 74// pick the one with the largest Size. This helps us avoid symbols with no 75// size information (Size=0). 77autoI = SS.begin(), E = SS.end(), J = SS.begin();
80while (++
I != E && OI->Addr ==
I->Addr) {
84 SS.erase(J, SS.end());
89SymbolizableObjectFile::SymbolizableObjectFile(
constObjectFile *Obj,
90 std::unique_ptr<DIContext> DICtx,
93 UntagAddresses(UntagAddresses) {}
101booloperator<(
const OffsetNamePair &R)
const{
106}
// end anonymous namespace 108Error SymbolizableObjectFile::addCoffExportSymbols(
110// Get all export names and offsets. 111 std::vector<OffsetNamePair> ExportSyms;
115if (
auto EC =
Ref.getSymbolName(
Name))
119 ExportSyms.push_back(OffsetNamePair{
Offset,
Name});
121if (ExportSyms.empty())
124// Sort by ascending offset. 127// Approximate the symbol sizes by assuming they run to the next symbol. 128// FIXME: This assumes all exports are functions. 130for (
autoI = ExportSyms.begin(), E = ExportSyms.end();
I != E; ++
I) {
132// FIXME: The last export has a one byte size now. 136 Symbols.push_back({SymbolStart, SymbolSize,
Export.Name, 0});
145// Avoid adding symbols from an unknown/undefined section. 157// Store the (index, filename) pair for a file symbol. 160 FileSymbols.emplace_back(ELFSymIdx, SymbolName);
170// Ignore any symbols coming from sections that don't have runtime 175// Allow function and data symbols. Additionally allow STT_NONE, which are 176// common for functions defined in assembly. 181// Some STT_NOTYPE symbols are not desired. This excludes STT_SECTION and 182// ARM mapping symbols. 184if (Flags & SymbolRef::SF_FormatSpecific)
192if (!SymbolAddressOrErr)
194uint64_t SymbolAddress = *SymbolAddressOrErr;
196// For kernel addresses, bits 56-63 need to be set, so we sign extend bit 55 197// into bits 56-63 instead of masking them out. 198 SymbolAddress &= (1ull << 56) - 1;
199 SymbolAddress = (int64_t(SymbolAddress) << 8) >> 8;
202// For big-endian PowerPC64 ELF, symbols in the .opd section refer to 203// function descriptors. The first word of the descriptor is a pointer to 204// the function's code. 205// For the purposes of symbolization, pretend the symbol's address is that 206// of the function's code, not the descriptor. 207uint64_t OpdOffset = SymbolAddress - OpdAddress;
209 SymbolAddress = OpdExtractor->
getAddress(&OpdOffset);
211// Mach-O symbol table names have leading underscore, skip it. 217 Symbols.push_back({SymbolAddress, SymbolSize,
SymbolName, ELFSymIdx});
221// Return true if this is a 32-bit x86 PE COFF module. 223auto *CoffObject = dyn_cast<COFFObjectFile>(
Module);
228if (
auto *CoffObject = dyn_cast<COFFObjectFile>(
Module))
229return CoffObject->getImageBase();
233bool SymbolizableObjectFile::getNameFromSymbolTable(
235 std::string &FileName)
const{
238if (SymbolIterator == Symbols.begin())
241if (SymbolIterator->Size != 0 &&
242 SymbolIterator->Addr + SymbolIterator->Size <=
Address)
244Name = SymbolIterator->Name.str();
245Addr = SymbolIterator->Addr;
246Size = SymbolIterator->Size;
248if (SymbolIterator->ELFLocalSymIdx != 0) {
249// If this is an ELF local symbol, find the STT_FILE symbol preceding 250// SymbolIterator to get the filename. The ELF spec requires the STT_FILE 251// symbol (if present) precedes the other STB_LOCAL symbols for the file. 255 std::make_pair(SymbolIterator->ELFLocalSymIdx,
StringRef()));
256if (It != FileSymbols.begin())
257 FileName = It[-1].second.str();
262bool SymbolizableObjectFile::shouldOverrideWithSymbolTable(
264// When DWARF is used with -gline-tables-only / -gmlt, the symbol table gives 265// better answers for linkage names than the DIContext. Otherwise, we are 266// probably using PEs and PDBs, and we shouldn't do the override. PE files 267// generally only contain the names of exported symbols. 268return FNKind == FunctionNameKind::LinkageName && UseSymbolTable &&
269 isa<DWARFContext>(DebugInfoContext.get());
275bool UseSymbolTable)
const{
278 getModuleSectionIndexForAddress(ModuleOffset.
Address);
280 DebugInfoContext->getLineInfoForAddress(ModuleOffset, LineInfoSpecifier);
282// Override function name from symbol table if necessary. 283if (shouldOverrideWithSymbolTable(LineInfoSpecifier.
FNKind, UseSymbolTable)) {
284 std::string FunctionName, FileName;
286if (getNameFromSymbolTable(ModuleOffset.
Address, FunctionName, Start,
Size,
302 getModuleSectionIndexForAddress(ModuleOffset.
Address);
304 ModuleOffset, LineInfoSpecifier);
306// Make sure there is at least one frame in context. 310// Override the function name in lower frame with name from symbol table. 311if (shouldOverrideWithSymbolTable(LineInfoSpecifier.
FNKind, UseSymbolTable)) {
312 std::string FunctionName, FileName;
314if (getNameFromSymbolTable(ModuleOffset.
Address, FunctionName, Start,
Size,
331 std::string FileName;
336// Try and get a better filename:lineno pair from the debuginfo, if present. 337DILineInfoDL = DebugInfoContext->getLineInfoForDataAddress(ModuleOffset);
349 getModuleSectionIndexForAddress(ModuleOffset.
Address);
350return DebugInfoContext->getLocalsForAddress(ModuleOffset);
353std::vector<object::SectionedAddress>
355 std::vector<object::SectionedAddress> Result;
356for (
const SymbolDesc &
Sym : Symbols) {
357if (
Sym.Name == Symbol) {
368/// Search for the first occurence of specified Address in ObjectFile. 369uint64_t SymbolizableObjectFile::getModuleSectionIndexForAddress(
373if (!Sec.isText() || Sec.isVirtual())
376if (
Address >= Sec.getAddress() &&
377Address < Sec.getAddress() + Sec.getSize())
378return Sec.getIndex();
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
A format-neutral container for inlined code description.
uint64_t getAddress(uint64_t *offset_ptr) const
Extract an pointer from *offset_ptr.
bool isValidOffsetForAddress(uint64_t offset) const
Test the availability of enough bytes of data for a pointer from offset.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
A Module instance is used to store all the information related to an LLVM module.
StringRef - Represent a constant reference to a string, i.e.
The instances of the Type class are immutable: once they are created, they are never changed.
DataRefImpl getRawDataRefImpl() const
bool isLittleEndian() const
iterator_range< export_directory_iterator > export_directories() const
uint64_t getImageBase() const
uint64_t getFlags() const
uint8_t getELFType() const
uint8_t getBinding() const
This class is the base class for all object file types.
virtual section_iterator section_end() const =0
virtual uint8_t getBytesInAddress() const =0
The number of bytes used to represent an address in this object file format.
section_iterator_range sections() const
virtual Triple::ArchType getArch() const =0
This is a value type class that represents a single section in the list of sections in the object fil...
This is a value type class that represents a single symbol in the list of symbols in the object file.
DIGlobal symbolizeData(object::SectionedAddress ModuleOffset) const override
DILineInfo symbolizeCode(object::SectionedAddress ModuleOffset, DILineInfoSpecifier LineInfoSpecifier, bool UseSymbolTable) const override
static Expected< std::unique_ptr< SymbolizableObjectFile > > create(const object::ObjectFile *Obj, std::unique_ptr< DIContext > DICtx, bool UntagAddresses)
std::vector< object::SectionedAddress > findSymbol(StringRef Symbol, uint64_t Offset) const override
uint64_t getModulePreferredBase() const override
std::vector< DILocal > symbolizeFrame(object::SectionedAddress ModuleOffset) const override
bool isWin32Module() const override
DIInliningInfo symbolizeInlinedCode(object::SectionedAddress ModuleOffset, DILineInfoSpecifier LineInfoSpecifier, bool UseSymbolTable) const override
constexpr char SymbolName[]
Key for Kernel::Metadata::mSymbolName.
@ IMAGE_FILE_MACHINE_I386
std::vector< std::pair< SymbolRef, uint64_t > > computeSymbolSizes(const ObjectFile &O)
This is an optimization pass for GlobalISel generic memory operations.
bool operator<(int64_t V1, const APSInt &V2)
void stable_sort(R &&Range)
@ Export
Export information to summary.
auto upper_bound(R &&Range, T &&Value)
Provide wrappers to std::upper_bound which take ranges instead of having to pass begin/end explicitly...
@ Ref
The access may reference the value stored in memory.
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
DINameKind
A DINameKind is passed to name search methods to specify a preference regarding the type of name reso...
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
void array_pod_sort(IteratorTy Start, IteratorTy End)
array_pod_sort - This sorts an array with the specified start and end extent.
Implement std::hash so that hash_code can be used in STL containers.
Container for description of a global variable.
Controls which fields of DILineInfo container should be filled with data.
A format-neutral container for source line information.
static constexpr const char *const BadString
std::optional< uint64_t > StartAddress
static const uint64_t UndefSection
struct llvm::object::DataRefImpl::@370 d