1 //===- SymbolizableObjectFile.cpp -----------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Implementation of SymbolizableObjectFile class.
10 //
11 //===----------------------------------------------------------------------===//
12 
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"
26 #include <algorithm>
27 #include <cstdint>
28 #include <memory>
29 #include <string>
30 #include <system_error>
31 #include <utility>
32 #include <vector>
33 
34 using namespace llvm;
35 using namespace object;
36 using namespace symbolize;
37 
38 static DILineInfoSpecifier
39 getDILineInfoSpecifier(FunctionNameKind FNKind) {
40   return DILineInfoSpecifier(
41       DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, FNKind);
42 }
43 
44 ErrorOr<std::unique_ptr<SymbolizableObjectFile>>
45 SymbolizableObjectFile::create(object::ObjectFile *Obj,
46                                std::unique_ptr<DIContext> DICtx) {
47   std::unique_ptr<SymbolizableObjectFile> res(
48       new SymbolizableObjectFile(Obj, std::move(DICtx)));
49   std::unique_ptr<DataExtractor> OpdExtractor;
50   uint64_t OpdAddress = 0;
51   // Find the .opd (function descriptor) section if any, for big-endian
52   // PowerPC64 ELF.
53   if (Obj->getArch() == Triple::ppc64) {
54     for (section_iterator Section : Obj->sections()) {
55       StringRef Name;
56       StringRef Data;
57       if (auto EC = Section->getName(Name))
58         return EC;
59       if (Name == ".opd") {
60         if (auto EC = Section->getContents(Data))
61           return EC;
62         OpdExtractor.reset(new DataExtractor(Data, Obj->isLittleEndian(),
63                                              Obj->getBytesInAddress()));
64         OpdAddress = Section->getAddress();
65         break;
66       }
67     }
68   }
69   std::vector<std::pair<SymbolRef, uint64_t>> Symbols =
70       computeSymbolSizes(*Obj);
71   for (auto &P : Symbols)
72     res->addSymbol(P.first, P.second, OpdExtractor.get(), OpdAddress);
73 
74   // If this is a COFF object and we didn't find any symbols, try the export
75   // table.
76   if (Symbols.empty()) {
77     if (auto *CoffObj = dyn_cast<COFFObjectFile>(Obj))
78       if (auto EC = res->addCoffExportSymbols(CoffObj))
79         return EC;
80   }
81   return std::move(res);
82 }
83 
84 SymbolizableObjectFile::SymbolizableObjectFile(ObjectFile *Obj,
85                                                std::unique_ptr<DIContext> DICtx)
86     : Module(Obj), DebugInfoContext(std::move(DICtx)) {}
87 
88 namespace {
89 
90 struct OffsetNamePair {
91   uint32_t Offset;
92   StringRef Name;
93 
94   bool operator<(const OffsetNamePair &R) const {
95     return Offset < R.Offset;
96   }
97 };
98 
99 } // end anonymous namespace
100 
101 std::error_code SymbolizableObjectFile::addCoffExportSymbols(
102     const COFFObjectFile *CoffObj) {
103   // Get all export names and offsets.
104   std::vector<OffsetNamePair> ExportSyms;
105   for (const ExportDirectoryEntryRef &Ref : CoffObj->export_directories()) {
106     StringRef Name;
107     uint32_t Offset;
108     if (auto EC = Ref.getSymbolName(Name))
109       return EC;
110     if (auto EC = Ref.getExportRVA(Offset))
111       return EC;
112     ExportSyms.push_back(OffsetNamePair{Offset, Name});
113   }
114   if (ExportSyms.empty())
115     return std::error_code();
116 
117   // Sort by ascending offset.
118   array_pod_sort(ExportSyms.begin(), ExportSyms.end());
119 
120   // Approximate the symbol sizes by assuming they run to the next symbol.
121   // FIXME: This assumes all exports are functions.
122   uint64_t ImageBase = CoffObj->getImageBase();
123   for (auto I = ExportSyms.begin(), E = ExportSyms.end(); I != E; ++I) {
124     OffsetNamePair &Export = *I;
125     // FIXME: The last export has a one byte size now.
126     uint32_t NextOffset = I != E ? I->Offset : Export.Offset + 1;
127     uint64_t SymbolStart = ImageBase + Export.Offset;
128     uint64_t SymbolSize = NextOffset - Export.Offset;
129     SymbolDesc SD = {SymbolStart, SymbolSize};
130     Functions.insert(std::make_pair(SD, Export.Name));
131   }
132   return std::error_code();
133 }
134 
135 std::error_code SymbolizableObjectFile::addSymbol(const SymbolRef &Symbol,
136                                                   uint64_t SymbolSize,
137                                                   DataExtractor *OpdExtractor,
138                                                   uint64_t OpdAddress) {
139   // Avoid adding symbols from an unknown/undefined section.
140   const ObjectFile *Obj = Symbol.getObject();
141   Expected<section_iterator> Sec = Symbol.getSection();
142   if (!Sec || (Obj && Obj->section_end() == *Sec))
143     return std::error_code();
144   Expected<SymbolRef::Type> SymbolTypeOrErr = Symbol.getType();
145   if (!SymbolTypeOrErr)
146     return errorToErrorCode(SymbolTypeOrErr.takeError());
147   SymbolRef::Type SymbolType = *SymbolTypeOrErr;
148   if (SymbolType != SymbolRef::ST_Function && SymbolType != SymbolRef::ST_Data)
149     return std::error_code();
150   Expected<uint64_t> SymbolAddressOrErr = Symbol.getAddress();
151   if (!SymbolAddressOrErr)
152     return errorToErrorCode(SymbolAddressOrErr.takeError());
153   uint64_t SymbolAddress = *SymbolAddressOrErr;
154   if (OpdExtractor) {
155     // For big-endian PowerPC64 ELF, symbols in the .opd section refer to
156     // function descriptors. The first word of the descriptor is a pointer to
157     // the function's code.
158     // For the purposes of symbolization, pretend the symbol's address is that
159     // of the function's code, not the descriptor.
160     uint64_t OpdOffset = SymbolAddress - OpdAddress;
161     uint32_t OpdOffset32 = OpdOffset;
162     if (OpdOffset == OpdOffset32 &&
163         OpdExtractor->isValidOffsetForAddress(OpdOffset32))
164       SymbolAddress = OpdExtractor->getAddress(&OpdOffset32);
165   }
166   Expected<StringRef> SymbolNameOrErr = Symbol.getName();
167   if (!SymbolNameOrErr)
168     return errorToErrorCode(SymbolNameOrErr.takeError());
169   StringRef SymbolName = *SymbolNameOrErr;
170   // Mach-O symbol table names have leading underscore, skip it.
171   if (Module->isMachO() && !SymbolName.empty() && SymbolName[0] == '_')
172     SymbolName = SymbolName.drop_front();
173   // FIXME: If a function has alias, there are two entries in symbol table
174   // with same address size. Make sure we choose the correct one.
175   auto &M = SymbolType == SymbolRef::ST_Function ? Functions : Objects;
176   SymbolDesc SD = { SymbolAddress, SymbolSize };
177   M.insert(std::make_pair(SD, SymbolName));
178   return std::error_code();
179 }
180 
181 // Return true if this is a 32-bit x86 PE COFF module.
182 bool SymbolizableObjectFile::isWin32Module() const {
183   auto *CoffObject = dyn_cast<COFFObjectFile>(Module);
184   return CoffObject && CoffObject->getMachine() == COFF::IMAGE_FILE_MACHINE_I386;
185 }
186 
187 uint64_t SymbolizableObjectFile::getModulePreferredBase() const {
188   if (auto *CoffObject = dyn_cast<COFFObjectFile>(Module))
189     return CoffObject->getImageBase();
190   return 0;
191 }
192 
193 bool SymbolizableObjectFile::getNameFromSymbolTable(SymbolRef::Type Type,
194                                                     uint64_t Address,
195                                                     std::string &Name,
196                                                     uint64_t &Addr,
197                                                     uint64_t &Size) const {
198   const auto &SymbolMap = Type == SymbolRef::ST_Function ? Functions : Objects;
199   if (SymbolMap.empty())
200     return false;
201   SymbolDesc SD = { Address, Address };
202   auto SymbolIterator = SymbolMap.upper_bound(SD);
203   if (SymbolIterator == SymbolMap.begin())
204     return false;
205   --SymbolIterator;
206   if (SymbolIterator->first.Size != 0 &&
207       SymbolIterator->first.Addr + SymbolIterator->first.Size <= Address)
208     return false;
209   Name = SymbolIterator->second.str();
210   Addr = SymbolIterator->first.Addr;
211   Size = SymbolIterator->first.Size;
212   return true;
213 }
214 
215 bool SymbolizableObjectFile::shouldOverrideWithSymbolTable(
216     FunctionNameKind FNKind, bool UseSymbolTable) const {
217   // When DWARF is used with -gline-tables-only / -gmlt, the symbol table gives
218   // better answers for linkage names than the DIContext. Otherwise, we are
219   // probably using PEs and PDBs, and we shouldn't do the override. PE files
220   // generally only contain the names of exported symbols.
221   return FNKind == FunctionNameKind::LinkageName && UseSymbolTable &&
222          isa<DWARFContext>(DebugInfoContext.get());
223 }
224 
225 DILineInfo
226 SymbolizableObjectFile::symbolizeCode(object::SectionedAddress ModuleOffset,
227                                       FunctionNameKind FNKind,
228                                       bool UseSymbolTable) const {
229   DILineInfo LineInfo;
230 
231   if (ModuleOffset.SectionIndex == object::SectionedAddress::UndefSection)
232     ModuleOffset.SectionIndex =
233         getModuleSectionIndexForAddress(ModuleOffset.Address);
234 
235   if (DebugInfoContext) {
236     LineInfo = DebugInfoContext->getLineInfoForAddress(
237         ModuleOffset, getDILineInfoSpecifier(FNKind));
238   }
239   // Override function name from symbol table if necessary.
240   if (shouldOverrideWithSymbolTable(FNKind, UseSymbolTable)) {
241     std::string FunctionName;
242     uint64_t Start, Size;
243     if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset.Address,
244                                FunctionName, Start, Size)) {
245       LineInfo.FunctionName = FunctionName;
246     }
247   }
248   return LineInfo;
249 }
250 
251 DIInliningInfo SymbolizableObjectFile::symbolizeInlinedCode(
252     object::SectionedAddress ModuleOffset, FunctionNameKind FNKind,
253     bool UseSymbolTable) const {
254   DIInliningInfo InlinedContext;
255 
256   if (ModuleOffset.SectionIndex == object::SectionedAddress::UndefSection)
257     ModuleOffset.SectionIndex =
258         getModuleSectionIndexForAddress(ModuleOffset.Address);
259 
260   if (DebugInfoContext)
261     InlinedContext = DebugInfoContext->getInliningInfoForAddress(
262         ModuleOffset, getDILineInfoSpecifier(FNKind));
263   // Make sure there is at least one frame in context.
264   if (InlinedContext.getNumberOfFrames() == 0)
265     InlinedContext.addFrame(DILineInfo());
266 
267   // Override the function name in lower frame with name from symbol table.
268   if (shouldOverrideWithSymbolTable(FNKind, UseSymbolTable)) {
269     std::string FunctionName;
270     uint64_t Start, Size;
271     if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset.Address,
272                                FunctionName, Start, Size)) {
273       InlinedContext.getMutableFrame(InlinedContext.getNumberOfFrames() - 1)
274           ->FunctionName = FunctionName;
275     }
276   }
277 
278   return InlinedContext;
279 }
280 
281 DIGlobal SymbolizableObjectFile::symbolizeData(
282     object::SectionedAddress ModuleOffset) const {
283   DIGlobal Res;
284   getNameFromSymbolTable(SymbolRef::ST_Data, ModuleOffset.Address, Res.Name,
285                          Res.Start, Res.Size);
286   return Res;
287 }
288 
289 /// Search for the first occurence of specified Address in ObjectFile.
290 uint64_t SymbolizableObjectFile::getModuleSectionIndexForAddress(
291     uint64_t Address) const {
292 
293   for (SectionRef Sec : Module->sections()) {
294     if (!Sec.isText() || Sec.isVirtual())
295       continue;
296 
297     if (Address >= Sec.getAddress() &&
298         Address <= Sec.getAddress() + Sec.getSize()) {
299       return Sec.getIndex();
300     }
301   }
302 
303   return object::SectionedAddress::UndefSection;
304 }
305