SymbolizableObjectFile.cpp 10 KB

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