fb2873e8159d03bc6ff50a62f9b4510fe30c6432
[oota-llvm.git] / lib / DebugInfo / Symbolize / SymbolizableObjectFile.cpp
1 //===-- SymbolizableObjectFile.cpp ----------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Implementation of SymbolizableObjectFile class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "SymbolizableObjectFile.h"
15 #include "llvm/Object/SymbolSize.h"
16 #include "llvm/Support/DataExtractor.h"
17
18 namespace llvm {
19 namespace symbolize {
20
21 using namespace object;
22
23 static DILineInfoSpecifier
24 getDILineInfoSpecifier(FunctionNameKind FNKind) {
25   return DILineInfoSpecifier(
26       DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, FNKind);
27 }
28
29 ErrorOr<std::unique_ptr<SymbolizableObjectFile>>
30 SymbolizableObjectFile::create(object::ObjectFile *Obj,
31                                std::unique_ptr<DIContext> DICtx) {
32   std::unique_ptr<SymbolizableObjectFile> res(
33       new SymbolizableObjectFile(Obj, std::move(DICtx)));
34   std::unique_ptr<DataExtractor> OpdExtractor;
35   uint64_t OpdAddress = 0;
36   // Find the .opd (function descriptor) section if any, for big-endian
37   // PowerPC64 ELF.
38   if (Obj->getArch() == Triple::ppc64) {
39     for (section_iterator Section : Obj->sections()) {
40       StringRef Name;
41       StringRef Data;
42       if (auto EC = Section->getName(Name))
43         return EC;
44       if (Name == ".opd") {
45         if (auto EC = Section->getContents(Data))
46           return EC;
47         OpdExtractor.reset(new DataExtractor(Data, Obj->isLittleEndian(),
48                                              Obj->getBytesInAddress()));
49         OpdAddress = Section->getAddress();
50         break;
51       }
52     }
53   }
54   std::vector<std::pair<SymbolRef, uint64_t>> Symbols =
55       computeSymbolSizes(*Obj);
56   for (auto &P : Symbols)
57     res->addSymbol(P.first, P.second, OpdExtractor.get(), OpdAddress);
58
59   // If this is a COFF object and we didn't find any symbols, try the export
60   // table.
61   if (Symbols.empty()) {
62     if (auto *CoffObj = dyn_cast<COFFObjectFile>(Obj))
63       if (auto EC = res->addCoffExportSymbols(CoffObj))
64         return EC;
65   }
66   return std::move(res);
67 }
68
69 SymbolizableObjectFile::SymbolizableObjectFile(ObjectFile *Obj,
70                                                std::unique_ptr<DIContext> DICtx)
71     : Module(Obj), DebugInfoContext(std::move(DICtx)) {}
72
73 namespace {
74 struct OffsetNamePair {
75   uint32_t Offset;
76   StringRef Name;
77   bool operator<(const OffsetNamePair &R) const {
78     return Offset < R.Offset;
79   }
80 };
81 }
82
83 std::error_code SymbolizableObjectFile::addCoffExportSymbols(
84     const COFFObjectFile *CoffObj) {
85   // Get all export names and offsets.
86   std::vector<OffsetNamePair> ExportSyms;
87   for (const ExportDirectoryEntryRef &Ref : CoffObj->export_directories()) {
88     StringRef Name;
89     uint32_t Offset;
90     if (auto EC = Ref.getSymbolName(Name))
91       return EC;
92     if (auto EC = Ref.getExportRVA(Offset))
93       return EC;
94     ExportSyms.push_back(OffsetNamePair{Offset, Name});
95   }
96   if (ExportSyms.empty())
97     return std::error_code();
98
99   // Sort by ascending offset.
100   array_pod_sort(ExportSyms.begin(), ExportSyms.end());
101
102   // Approximate the symbol sizes by assuming they run to the next symbol.
103   // FIXME: This assumes all exports are functions.
104   uint64_t ImageBase = CoffObj->getImageBase();
105   for (auto I = ExportSyms.begin(), E = ExportSyms.end(); I != E; ++I) {
106     OffsetNamePair &Export = *I;
107     // FIXME: The last export has a one byte size now.
108     uint32_t NextOffset = I != E ? I->Offset : Export.Offset + 1;
109     uint64_t SymbolStart = ImageBase + Export.Offset;
110     uint64_t SymbolSize = NextOffset - Export.Offset;
111     SymbolDesc SD = {SymbolStart, SymbolSize};
112     Functions.insert(std::make_pair(SD, Export.Name));
113   }
114   return std::error_code();
115 }
116
117 std::error_code SymbolizableObjectFile::addSymbol(const SymbolRef &Symbol,
118                                                   uint64_t SymbolSize,
119                                                   DataExtractor *OpdExtractor,
120                                                   uint64_t OpdAddress) {
121   SymbolRef::Type SymbolType = Symbol.getType();
122   if (SymbolType != SymbolRef::ST_Function && SymbolType != SymbolRef::ST_Data)
123     return std::error_code();
124   ErrorOr<uint64_t> SymbolAddressOrErr = Symbol.getAddress();
125   if (auto EC = SymbolAddressOrErr.getError())
126     return EC;
127   uint64_t SymbolAddress = *SymbolAddressOrErr;
128   if (OpdExtractor) {
129     // For big-endian PowerPC64 ELF, symbols in the .opd section refer to
130     // function descriptors. The first word of the descriptor is a pointer to
131     // the function's code.
132     // For the purposes of symbolization, pretend the symbol's address is that
133     // of the function's code, not the descriptor.
134     uint64_t OpdOffset = SymbolAddress - OpdAddress;
135     uint32_t OpdOffset32 = OpdOffset;
136     if (OpdOffset == OpdOffset32 && 
137         OpdExtractor->isValidOffsetForAddress(OpdOffset32))
138       SymbolAddress = OpdExtractor->getAddress(&OpdOffset32);
139   }
140   ErrorOr<StringRef> SymbolNameOrErr = Symbol.getName();
141   if (auto EC = SymbolNameOrErr.getError())
142     return EC;
143   StringRef SymbolName = *SymbolNameOrErr;
144   // Mach-O symbol table names have leading underscore, skip it.
145   if (Module->isMachO() && SymbolName.size() > 0 && SymbolName[0] == '_')
146     SymbolName = SymbolName.drop_front();
147   // FIXME: If a function has alias, there are two entries in symbol table
148   // with same address size. Make sure we choose the correct one.
149   auto &M = SymbolType == SymbolRef::ST_Function ? Functions : Objects;
150   SymbolDesc SD = { SymbolAddress, SymbolSize };
151   M.insert(std::make_pair(SD, SymbolName));
152   return std::error_code();
153 }
154
155 // Return true if this is a 32-bit x86 PE COFF module.
156 bool SymbolizableObjectFile::isWin32Module() const {
157   auto *CoffObject = dyn_cast<COFFObjectFile>(Module);
158   return CoffObject && CoffObject->getMachine() == COFF::IMAGE_FILE_MACHINE_I386;
159 }
160
161 uint64_t SymbolizableObjectFile::getModulePreferredBase() const {
162   if (auto *CoffObject = dyn_cast<COFFObjectFile>(Module))
163     return CoffObject->getImageBase();
164   return 0;
165 }
166
167 bool SymbolizableObjectFile::getNameFromSymbolTable(SymbolRef::Type Type,
168                                                     uint64_t Address,
169                                                     std::string &Name,
170                                                     uint64_t &Addr,
171                                                     uint64_t &Size) const {
172   const auto &SymbolMap = Type == SymbolRef::ST_Function ? Functions : Objects;
173   if (SymbolMap.empty())
174     return false;
175   SymbolDesc SD = { Address, Address };
176   auto SymbolIterator = SymbolMap.upper_bound(SD);
177   if (SymbolIterator == SymbolMap.begin())
178     return false;
179   --SymbolIterator;
180   if (SymbolIterator->first.Size != 0 &&
181       SymbolIterator->first.Addr + SymbolIterator->first.Size <= Address)
182     return false;
183   Name = SymbolIterator->second.str();
184   Addr = SymbolIterator->first.Addr;
185   Size = SymbolIterator->first.Size;
186   return true;
187 }
188
189 DILineInfo SymbolizableObjectFile::symbolizeCode(uint64_t ModuleOffset,
190                                                  FunctionNameKind FNKind,
191                                                  bool UseSymbolTable) const {
192   DILineInfo LineInfo;
193   if (DebugInfoContext) {
194     LineInfo = DebugInfoContext->getLineInfoForAddress(
195         ModuleOffset, getDILineInfoSpecifier(FNKind));
196   }
197   // Override function name from symbol table if necessary.
198   if (FNKind == FunctionNameKind::LinkageName && UseSymbolTable) {
199     std::string FunctionName;
200     uint64_t Start, Size;
201     if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset,
202                                FunctionName, Start, Size)) {
203       LineInfo.FunctionName = FunctionName;
204     }
205   }
206   return LineInfo;
207 }
208
209 DIInliningInfo SymbolizableObjectFile::symbolizeInlinedCode(
210     uint64_t ModuleOffset, FunctionNameKind FNKind, bool UseSymbolTable) const {
211   DIInliningInfo InlinedContext;
212
213   if (DebugInfoContext)
214     InlinedContext = DebugInfoContext->getInliningInfoForAddress(
215         ModuleOffset, getDILineInfoSpecifier(FNKind));
216   // Make sure there is at least one frame in context.
217   if (InlinedContext.getNumberOfFrames() == 0)
218     InlinedContext.addFrame(DILineInfo());
219
220   if (FNKind != FunctionNameKind::LinkageName || !UseSymbolTable)
221     return InlinedContext;
222
223   // Override the function name in lower frame with name from symbol table.
224   // We can't directly change the last element of DIInliningInfo, so copy
225   // all frames into new context, replacing function name in the last one.
226   DIInliningInfo PatchedInlinedContext;
227   for (uint32_t i = 0, n = InlinedContext.getNumberOfFrames(); i < n; i++) {
228     DILineInfo LineInfo = InlinedContext.getFrame(i);
229     if (i == n - 1) {
230       std::string FunctionName;
231       uint64_t Start, Size;
232       if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset,
233                                  FunctionName, Start, Size)) {
234         LineInfo.FunctionName = FunctionName;
235       }
236     }
237     PatchedInlinedContext.addFrame(LineInfo);
238   }
239   return PatchedInlinedContext;
240 }
241
242 DIGlobal SymbolizableObjectFile::symbolizeData(uint64_t ModuleOffset) const {
243   DIGlobal Res;
244   getNameFromSymbolTable(SymbolRef::ST_Data, ModuleOffset, Res.Name, Res.Start,
245                          Res.Size);
246   return Res;
247 }
248
249 }  // namespace symbolize
250 }  // namespace llvm
251