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 //===----------------------------------------------------------------------===//
13 #include "SymbolizableObjectFile.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/ADT/Triple.h"
17 #include "llvm/BinaryFormat/COFF.h"
18 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
19 #include "llvm/DebugInfo/Symbolize/SymbolizableModule.h"
20 #include "llvm/Object/COFF.h"
21 #include "llvm/Object/ObjectFile.h"
22 #include "llvm/Object/SymbolSize.h"
23 #include "llvm/Support/Casting.h"
24 #include "llvm/Support/DataExtractor.h"
25 #include "llvm/Support/Error.h"
30 #include <system_error>
35 using namespace object;
36 using namespace symbolize;
38 static DILineInfoSpecifier
39 getDILineInfoSpecifier(FunctionNameKind FNKind) {
40 return DILineInfoSpecifier(
41 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, FNKind);
44 ErrorOr<std::unique_ptr<SymbolizableObjectFile>>
45 SymbolizableObjectFile::create(const object::ObjectFile *Obj,
46 std::unique_ptr<DIContext> DICtx) {
48 std::unique_ptr<SymbolizableObjectFile> res(
49 new SymbolizableObjectFile(Obj, std::move(DICtx)));
50 std::unique_ptr<DataExtractor> OpdExtractor;
51 uint64_t OpdAddress = 0;
52 // Find the .opd (function descriptor) section if any, for big-endian
54 if (Obj->getArch() == Triple::ppc64) {
55 for (section_iterator Section : Obj->sections()) {
57 if (auto EC = Section->getName(Name))
60 Expected<StringRef> E = Section->getContents();
62 return errorToErrorCode(E.takeError());
63 OpdExtractor.reset(new DataExtractor(*E, Obj->isLittleEndian(),
64 Obj->getBytesInAddress()));
65 OpdAddress = Section->getAddress();
70 std::vector<std::pair<SymbolRef, uint64_t>> Symbols =
71 computeSymbolSizes(*Obj);
72 for (auto &P : Symbols)
73 res->addSymbol(P.first, P.second, OpdExtractor.get(), OpdAddress);
75 // If this is a COFF object and we didn't find any symbols, try the export
77 if (Symbols.empty()) {
78 if (auto *CoffObj = dyn_cast<COFFObjectFile>(Obj))
79 if (auto EC = res->addCoffExportSymbols(CoffObj))
83 std::vector<std::pair<SymbolDesc, StringRef>> &Fs = res->Functions,
85 auto Uniquify = [](std::vector<std::pair<SymbolDesc, StringRef>> &S) {
86 // Sort by (Addr,Size,Name). If several SymbolDescs share the same Addr,
87 // pick the one with the largest Size. This helps us avoid symbols with no
88 // size information (Size=0).
90 auto I = S.begin(), E = S.end(), J = S.begin();
93 while (++I != E && OI->first.Addr == I->first.Addr) {
102 return std::move(res);
105 SymbolizableObjectFile::SymbolizableObjectFile(const ObjectFile *Obj,
106 std::unique_ptr<DIContext> DICtx)
107 : Module(Obj), DebugInfoContext(std::move(DICtx)) {}
111 struct OffsetNamePair {
115 bool operator<(const OffsetNamePair &R) const {
116 return Offset < R.Offset;
120 } // end anonymous namespace
122 std::error_code SymbolizableObjectFile::addCoffExportSymbols(
123 const COFFObjectFile *CoffObj) {
124 // Get all export names and offsets.
125 std::vector<OffsetNamePair> ExportSyms;
126 for (const ExportDirectoryEntryRef &Ref : CoffObj->export_directories()) {
129 if (auto EC = Ref.getSymbolName(Name))
131 if (auto EC = Ref.getExportRVA(Offset))
133 ExportSyms.push_back(OffsetNamePair{Offset, Name});
135 if (ExportSyms.empty())
136 return std::error_code();
138 // Sort by ascending offset.
139 array_pod_sort(ExportSyms.begin(), ExportSyms.end());
141 // Approximate the symbol sizes by assuming they run to the next symbol.
142 // FIXME: This assumes all exports are functions.
143 uint64_t ImageBase = CoffObj->getImageBase();
144 for (auto I = ExportSyms.begin(), E = ExportSyms.end(); I != E; ++I) {
145 OffsetNamePair &Export = *I;
146 // FIXME: The last export has a one byte size now.
147 uint32_t NextOffset = I != E ? I->Offset : Export.Offset + 1;
148 uint64_t SymbolStart = ImageBase + Export.Offset;
149 uint64_t SymbolSize = NextOffset - Export.Offset;
150 SymbolDesc SD = {SymbolStart, SymbolSize};
151 Functions.emplace_back(SD, Export.Name);
153 return std::error_code();
156 std::error_code SymbolizableObjectFile::addSymbol(const SymbolRef &Symbol,
158 DataExtractor *OpdExtractor,
159 uint64_t OpdAddress) {
160 // Avoid adding symbols from an unknown/undefined section.
161 const ObjectFile *Obj = Symbol.getObject();
162 Expected<section_iterator> Sec = Symbol.getSection();
163 if (!Sec || (Obj && Obj->section_end() == *Sec))
164 return std::error_code();
165 Expected<SymbolRef::Type> SymbolTypeOrErr = Symbol.getType();
166 if (!SymbolTypeOrErr)
167 return errorToErrorCode(SymbolTypeOrErr.takeError());
168 SymbolRef::Type SymbolType = *SymbolTypeOrErr;
169 if (SymbolType != SymbolRef::ST_Function && SymbolType != SymbolRef::ST_Data)
170 return std::error_code();
171 Expected<uint64_t> SymbolAddressOrErr = Symbol.getAddress();
172 if (!SymbolAddressOrErr)
173 return errorToErrorCode(SymbolAddressOrErr.takeError());
174 uint64_t SymbolAddress = *SymbolAddressOrErr;
176 // For big-endian PowerPC64 ELF, symbols in the .opd section refer to
177 // function descriptors. The first word of the descriptor is a pointer to
178 // the function's code.
179 // For the purposes of symbolization, pretend the symbol's address is that
180 // of the function's code, not the descriptor.
181 uint64_t OpdOffset = SymbolAddress - OpdAddress;
182 uint32_t OpdOffset32 = OpdOffset;
183 if (OpdOffset == OpdOffset32 &&
184 OpdExtractor->isValidOffsetForAddress(OpdOffset32))
185 SymbolAddress = OpdExtractor->getAddress(&OpdOffset32);
187 Expected<StringRef> SymbolNameOrErr = Symbol.getName();
188 if (!SymbolNameOrErr)
189 return errorToErrorCode(SymbolNameOrErr.takeError());
190 StringRef SymbolName = *SymbolNameOrErr;
191 // Mach-O symbol table names have leading underscore, skip it.
192 if (Module->isMachO() && !SymbolName.empty() && SymbolName[0] == '_')
193 SymbolName = SymbolName.drop_front();
194 // FIXME: If a function has alias, there are two entries in symbol table
195 // with same address size. Make sure we choose the correct one.
196 auto &M = SymbolType == SymbolRef::ST_Function ? Functions : Objects;
197 SymbolDesc SD = { SymbolAddress, SymbolSize };
198 M.emplace_back(SD, SymbolName);
199 return std::error_code();
202 // Return true if this is a 32-bit x86 PE COFF module.
203 bool SymbolizableObjectFile::isWin32Module() const {
204 auto *CoffObject = dyn_cast<COFFObjectFile>(Module);
205 return CoffObject && CoffObject->getMachine() == COFF::IMAGE_FILE_MACHINE_I386;
208 uint64_t SymbolizableObjectFile::getModulePreferredBase() const {
209 if (auto *CoffObject = dyn_cast<COFFObjectFile>(Module))
210 return CoffObject->getImageBase();
214 bool SymbolizableObjectFile::getNameFromSymbolTable(SymbolRef::Type Type,
218 uint64_t &Size) const {
219 const auto &Symbols = Type == SymbolRef::ST_Function ? Functions : Objects;
220 std::pair<SymbolDesc, StringRef> SD{{Address, UINT64_C(-1)}, StringRef()};
221 auto SymbolIterator = llvm::upper_bound(Symbols, SD);
222 if (SymbolIterator == Symbols.begin())
225 if (SymbolIterator->first.Size != 0 &&
226 SymbolIterator->first.Addr + SymbolIterator->first.Size <= Address)
228 Name = SymbolIterator->second.str();
229 Addr = SymbolIterator->first.Addr;
230 Size = SymbolIterator->first.Size;
234 bool SymbolizableObjectFile::shouldOverrideWithSymbolTable(
235 FunctionNameKind FNKind, bool UseSymbolTable) const {
236 // When DWARF is used with -gline-tables-only / -gmlt, the symbol table gives
237 // better answers for linkage names than the DIContext. Otherwise, we are
238 // probably using PEs and PDBs, and we shouldn't do the override. PE files
239 // generally only contain the names of exported symbols.
240 return FNKind == FunctionNameKind::LinkageName && UseSymbolTable &&
241 isa<DWARFContext>(DebugInfoContext.get());
245 SymbolizableObjectFile::symbolizeCode(object::SectionedAddress ModuleOffset,
246 FunctionNameKind FNKind,
247 bool UseSymbolTable) const {
248 if (ModuleOffset.SectionIndex == object::SectionedAddress::UndefSection)
249 ModuleOffset.SectionIndex =
250 getModuleSectionIndexForAddress(ModuleOffset.Address);
251 DILineInfo LineInfo = DebugInfoContext->getLineInfoForAddress(
252 ModuleOffset, getDILineInfoSpecifier(FNKind));
254 // Override function name from symbol table if necessary.
255 if (shouldOverrideWithSymbolTable(FNKind, UseSymbolTable)) {
256 std::string FunctionName;
257 uint64_t Start, Size;
258 if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset.Address,
259 FunctionName, Start, Size)) {
260 LineInfo.FunctionName = FunctionName;
266 DIInliningInfo SymbolizableObjectFile::symbolizeInlinedCode(
267 object::SectionedAddress ModuleOffset, FunctionNameKind FNKind,
268 bool UseSymbolTable) const {
269 if (ModuleOffset.SectionIndex == object::SectionedAddress::UndefSection)
270 ModuleOffset.SectionIndex =
271 getModuleSectionIndexForAddress(ModuleOffset.Address);
272 DIInliningInfo InlinedContext = DebugInfoContext->getInliningInfoForAddress(
273 ModuleOffset, getDILineInfoSpecifier(FNKind));
275 // Make sure there is at least one frame in context.
276 if (InlinedContext.getNumberOfFrames() == 0)
277 InlinedContext.addFrame(DILineInfo());
279 // Override the function name in lower frame with name from symbol table.
280 if (shouldOverrideWithSymbolTable(FNKind, UseSymbolTable)) {
281 std::string FunctionName;
282 uint64_t Start, Size;
283 if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset.Address,
284 FunctionName, Start, Size)) {
285 InlinedContext.getMutableFrame(InlinedContext.getNumberOfFrames() - 1)
286 ->FunctionName = FunctionName;
290 return InlinedContext;
293 DIGlobal SymbolizableObjectFile::symbolizeData(
294 object::SectionedAddress ModuleOffset) const {
296 getNameFromSymbolTable(SymbolRef::ST_Data, ModuleOffset.Address, Res.Name,
297 Res.Start, Res.Size);
301 std::vector<DILocal> SymbolizableObjectFile::symbolizeFrame(
302 object::SectionedAddress ModuleOffset) const {
303 if (ModuleOffset.SectionIndex == object::SectionedAddress::UndefSection)
304 ModuleOffset.SectionIndex =
305 getModuleSectionIndexForAddress(ModuleOffset.Address);
306 return DebugInfoContext->getLocalsForAddress(ModuleOffset);
309 /// Search for the first occurence of specified Address in ObjectFile.
310 uint64_t SymbolizableObjectFile::getModuleSectionIndexForAddress(
311 uint64_t Address) const {
313 for (SectionRef Sec : Module->sections()) {
314 if (!Sec.isText() || Sec.isVirtual())
317 if (Address >= Sec.getAddress() &&
318 Address < Sec.getAddress() + Sec.getSize())
319 return Sec.getIndex();
322 return object::SectionedAddress::UndefSection;