18df3a07aSAlexey Samsonov //===-- SymbolizableObjectFile.cpp ----------------------------------------===//
28df3a07aSAlexey Samsonov //
38df3a07aSAlexey Samsonov //                     The LLVM Compiler Infrastructure
48df3a07aSAlexey Samsonov //
58df3a07aSAlexey Samsonov // This file is distributed under the University of Illinois Open Source
68df3a07aSAlexey Samsonov // License. See LICENSE.TXT for details.
78df3a07aSAlexey Samsonov //
88df3a07aSAlexey Samsonov //===----------------------------------------------------------------------===//
98df3a07aSAlexey Samsonov //
108df3a07aSAlexey Samsonov // Implementation of SymbolizableObjectFile class.
118df3a07aSAlexey Samsonov //
128df3a07aSAlexey Samsonov //===----------------------------------------------------------------------===//
138df3a07aSAlexey Samsonov 
148df3a07aSAlexey Samsonov #include "SymbolizableObjectFile.h"
158df3a07aSAlexey Samsonov #include "llvm/Object/SymbolSize.h"
168df3a07aSAlexey Samsonov #include "llvm/Support/DataExtractor.h"
178df3a07aSAlexey Samsonov 
188df3a07aSAlexey Samsonov namespace llvm {
198df3a07aSAlexey Samsonov namespace symbolize {
208df3a07aSAlexey Samsonov 
218df3a07aSAlexey Samsonov using namespace object;
228df3a07aSAlexey Samsonov 
238df3a07aSAlexey Samsonov static DILineInfoSpecifier
248df3a07aSAlexey Samsonov getDILineInfoSpecifier(FunctionNameKind FNKind) {
258df3a07aSAlexey Samsonov   return DILineInfoSpecifier(
268df3a07aSAlexey Samsonov       DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, FNKind);
278df3a07aSAlexey Samsonov }
288df3a07aSAlexey Samsonov 
298df3a07aSAlexey Samsonov ErrorOr<std::unique_ptr<SymbolizableObjectFile>>
308df3a07aSAlexey Samsonov SymbolizableObjectFile::create(object::ObjectFile *Obj,
318df3a07aSAlexey Samsonov                                std::unique_ptr<DIContext> DICtx) {
328df3a07aSAlexey Samsonov   std::unique_ptr<SymbolizableObjectFile> res(
338df3a07aSAlexey Samsonov       new SymbolizableObjectFile(Obj, std::move(DICtx)));
348df3a07aSAlexey Samsonov   std::unique_ptr<DataExtractor> OpdExtractor;
358df3a07aSAlexey Samsonov   uint64_t OpdAddress = 0;
368df3a07aSAlexey Samsonov   // Find the .opd (function descriptor) section if any, for big-endian
378df3a07aSAlexey Samsonov   // PowerPC64 ELF.
388df3a07aSAlexey Samsonov   if (Obj->getArch() == Triple::ppc64) {
398df3a07aSAlexey Samsonov     for (section_iterator Section : Obj->sections()) {
408df3a07aSAlexey Samsonov       StringRef Name;
418df3a07aSAlexey Samsonov       StringRef Data;
428df3a07aSAlexey Samsonov       if (auto EC = Section->getName(Name))
438df3a07aSAlexey Samsonov         return EC;
448df3a07aSAlexey Samsonov       if (Name == ".opd") {
458df3a07aSAlexey Samsonov         if (auto EC = Section->getContents(Data))
468df3a07aSAlexey Samsonov           return EC;
478df3a07aSAlexey Samsonov         OpdExtractor.reset(new DataExtractor(Data, Obj->isLittleEndian(),
488df3a07aSAlexey Samsonov                                              Obj->getBytesInAddress()));
498df3a07aSAlexey Samsonov         OpdAddress = Section->getAddress();
508df3a07aSAlexey Samsonov         break;
518df3a07aSAlexey Samsonov       }
528df3a07aSAlexey Samsonov     }
538df3a07aSAlexey Samsonov   }
548df3a07aSAlexey Samsonov   std::vector<std::pair<SymbolRef, uint64_t>> Symbols =
558df3a07aSAlexey Samsonov       computeSymbolSizes(*Obj);
568df3a07aSAlexey Samsonov   for (auto &P : Symbols)
578df3a07aSAlexey Samsonov     res->addSymbol(P.first, P.second, OpdExtractor.get(), OpdAddress);
588df3a07aSAlexey Samsonov 
598df3a07aSAlexey Samsonov   // If this is a COFF object and we didn't find any symbols, try the export
608df3a07aSAlexey Samsonov   // table.
618df3a07aSAlexey Samsonov   if (Symbols.empty()) {
628df3a07aSAlexey Samsonov     if (auto *CoffObj = dyn_cast<COFFObjectFile>(Obj))
638df3a07aSAlexey Samsonov       if (auto EC = res->addCoffExportSymbols(CoffObj))
648df3a07aSAlexey Samsonov         return EC;
658df3a07aSAlexey Samsonov   }
668df3a07aSAlexey Samsonov   return std::move(res);
678df3a07aSAlexey Samsonov }
688df3a07aSAlexey Samsonov 
698df3a07aSAlexey Samsonov SymbolizableObjectFile::SymbolizableObjectFile(ObjectFile *Obj,
708df3a07aSAlexey Samsonov                                                std::unique_ptr<DIContext> DICtx)
718df3a07aSAlexey Samsonov     : Module(Obj), DebugInfoContext(std::move(DICtx)) {}
728df3a07aSAlexey Samsonov 
738df3a07aSAlexey Samsonov namespace {
748df3a07aSAlexey Samsonov struct OffsetNamePair {
758df3a07aSAlexey Samsonov   uint32_t Offset;
768df3a07aSAlexey Samsonov   StringRef Name;
778df3a07aSAlexey Samsonov   bool operator<(const OffsetNamePair &R) const {
788df3a07aSAlexey Samsonov     return Offset < R.Offset;
798df3a07aSAlexey Samsonov   }
808df3a07aSAlexey Samsonov };
818df3a07aSAlexey Samsonov }
828df3a07aSAlexey Samsonov 
838df3a07aSAlexey Samsonov std::error_code SymbolizableObjectFile::addCoffExportSymbols(
848df3a07aSAlexey Samsonov     const COFFObjectFile *CoffObj) {
858df3a07aSAlexey Samsonov   // Get all export names and offsets.
868df3a07aSAlexey Samsonov   std::vector<OffsetNamePair> ExportSyms;
878df3a07aSAlexey Samsonov   for (const ExportDirectoryEntryRef &Ref : CoffObj->export_directories()) {
888df3a07aSAlexey Samsonov     StringRef Name;
898df3a07aSAlexey Samsonov     uint32_t Offset;
908df3a07aSAlexey Samsonov     if (auto EC = Ref.getSymbolName(Name))
918df3a07aSAlexey Samsonov       return EC;
928df3a07aSAlexey Samsonov     if (auto EC = Ref.getExportRVA(Offset))
938df3a07aSAlexey Samsonov       return EC;
948df3a07aSAlexey Samsonov     ExportSyms.push_back(OffsetNamePair{Offset, Name});
958df3a07aSAlexey Samsonov   }
968df3a07aSAlexey Samsonov   if (ExportSyms.empty())
978df3a07aSAlexey Samsonov     return std::error_code();
988df3a07aSAlexey Samsonov 
998df3a07aSAlexey Samsonov   // Sort by ascending offset.
1008df3a07aSAlexey Samsonov   array_pod_sort(ExportSyms.begin(), ExportSyms.end());
1018df3a07aSAlexey Samsonov 
1028df3a07aSAlexey Samsonov   // Approximate the symbol sizes by assuming they run to the next symbol.
1038df3a07aSAlexey Samsonov   // FIXME: This assumes all exports are functions.
1048df3a07aSAlexey Samsonov   uint64_t ImageBase = CoffObj->getImageBase();
1058df3a07aSAlexey Samsonov   for (auto I = ExportSyms.begin(), E = ExportSyms.end(); I != E; ++I) {
1068df3a07aSAlexey Samsonov     OffsetNamePair &Export = *I;
1078df3a07aSAlexey Samsonov     // FIXME: The last export has a one byte size now.
1088df3a07aSAlexey Samsonov     uint32_t NextOffset = I != E ? I->Offset : Export.Offset + 1;
1098df3a07aSAlexey Samsonov     uint64_t SymbolStart = ImageBase + Export.Offset;
1108df3a07aSAlexey Samsonov     uint64_t SymbolSize = NextOffset - Export.Offset;
1118df3a07aSAlexey Samsonov     SymbolDesc SD = {SymbolStart, SymbolSize};
1128df3a07aSAlexey Samsonov     Functions.insert(std::make_pair(SD, Export.Name));
1138df3a07aSAlexey Samsonov   }
1148df3a07aSAlexey Samsonov   return std::error_code();
1158df3a07aSAlexey Samsonov }
1168df3a07aSAlexey Samsonov 
1178df3a07aSAlexey Samsonov std::error_code SymbolizableObjectFile::addSymbol(const SymbolRef &Symbol,
1188df3a07aSAlexey Samsonov                                                   uint64_t SymbolSize,
1198df3a07aSAlexey Samsonov                                                   DataExtractor *OpdExtractor,
1208df3a07aSAlexey Samsonov                                                   uint64_t OpdAddress) {
1218df3a07aSAlexey Samsonov   SymbolRef::Type SymbolType = Symbol.getType();
1228df3a07aSAlexey Samsonov   if (SymbolType != SymbolRef::ST_Function && SymbolType != SymbolRef::ST_Data)
1238df3a07aSAlexey Samsonov     return std::error_code();
1248df3a07aSAlexey Samsonov   ErrorOr<uint64_t> SymbolAddressOrErr = Symbol.getAddress();
1258df3a07aSAlexey Samsonov   if (auto EC = SymbolAddressOrErr.getError())
1268df3a07aSAlexey Samsonov     return EC;
1278df3a07aSAlexey Samsonov   uint64_t SymbolAddress = *SymbolAddressOrErr;
1288df3a07aSAlexey Samsonov   if (OpdExtractor) {
1298df3a07aSAlexey Samsonov     // For big-endian PowerPC64 ELF, symbols in the .opd section refer to
1308df3a07aSAlexey Samsonov     // function descriptors. The first word of the descriptor is a pointer to
1318df3a07aSAlexey Samsonov     // the function's code.
1328df3a07aSAlexey Samsonov     // For the purposes of symbolization, pretend the symbol's address is that
1338df3a07aSAlexey Samsonov     // of the function's code, not the descriptor.
1348df3a07aSAlexey Samsonov     uint64_t OpdOffset = SymbolAddress - OpdAddress;
1358df3a07aSAlexey Samsonov     uint32_t OpdOffset32 = OpdOffset;
1368df3a07aSAlexey Samsonov     if (OpdOffset == OpdOffset32 &&
1378df3a07aSAlexey Samsonov         OpdExtractor->isValidOffsetForAddress(OpdOffset32))
1388df3a07aSAlexey Samsonov       SymbolAddress = OpdExtractor->getAddress(&OpdOffset32);
1398df3a07aSAlexey Samsonov   }
1408df3a07aSAlexey Samsonov   ErrorOr<StringRef> SymbolNameOrErr = Symbol.getName();
1418df3a07aSAlexey Samsonov   if (auto EC = SymbolNameOrErr.getError())
1428df3a07aSAlexey Samsonov     return EC;
1438df3a07aSAlexey Samsonov   StringRef SymbolName = *SymbolNameOrErr;
1448df3a07aSAlexey Samsonov   // Mach-O symbol table names have leading underscore, skip it.
1458df3a07aSAlexey Samsonov   if (Module->isMachO() && SymbolName.size() > 0 && SymbolName[0] == '_')
1468df3a07aSAlexey Samsonov     SymbolName = SymbolName.drop_front();
1478df3a07aSAlexey Samsonov   // FIXME: If a function has alias, there are two entries in symbol table
1488df3a07aSAlexey Samsonov   // with same address size. Make sure we choose the correct one.
1498df3a07aSAlexey Samsonov   auto &M = SymbolType == SymbolRef::ST_Function ? Functions : Objects;
1508df3a07aSAlexey Samsonov   SymbolDesc SD = { SymbolAddress, SymbolSize };
1518df3a07aSAlexey Samsonov   M.insert(std::make_pair(SD, SymbolName));
1528df3a07aSAlexey Samsonov   return std::error_code();
1538df3a07aSAlexey Samsonov }
1548df3a07aSAlexey Samsonov 
1558df3a07aSAlexey Samsonov // Return true if this is a 32-bit x86 PE COFF module.
1568df3a07aSAlexey Samsonov bool SymbolizableObjectFile::isWin32Module() const {
1578df3a07aSAlexey Samsonov   auto *CoffObject = dyn_cast<COFFObjectFile>(Module);
1588df3a07aSAlexey Samsonov   return CoffObject && CoffObject->getMachine() == COFF::IMAGE_FILE_MACHINE_I386;
1598df3a07aSAlexey Samsonov }
1608df3a07aSAlexey Samsonov 
1618df3a07aSAlexey Samsonov uint64_t SymbolizableObjectFile::getModulePreferredBase() const {
1628df3a07aSAlexey Samsonov   if (auto *CoffObject = dyn_cast<COFFObjectFile>(Module))
1638df3a07aSAlexey Samsonov     return CoffObject->getImageBase();
1648df3a07aSAlexey Samsonov   return 0;
1658df3a07aSAlexey Samsonov }
1668df3a07aSAlexey Samsonov 
1678df3a07aSAlexey Samsonov bool SymbolizableObjectFile::getNameFromSymbolTable(SymbolRef::Type Type,
1688df3a07aSAlexey Samsonov                                                     uint64_t Address,
1698df3a07aSAlexey Samsonov                                                     std::string &Name,
1708df3a07aSAlexey Samsonov                                                     uint64_t &Addr,
1718df3a07aSAlexey Samsonov                                                     uint64_t &Size) const {
1728df3a07aSAlexey Samsonov   const auto &SymbolMap = Type == SymbolRef::ST_Function ? Functions : Objects;
1738df3a07aSAlexey Samsonov   if (SymbolMap.empty())
1748df3a07aSAlexey Samsonov     return false;
1758df3a07aSAlexey Samsonov   SymbolDesc SD = { Address, Address };
1768df3a07aSAlexey Samsonov   auto SymbolIterator = SymbolMap.upper_bound(SD);
1778df3a07aSAlexey Samsonov   if (SymbolIterator == SymbolMap.begin())
1788df3a07aSAlexey Samsonov     return false;
1798df3a07aSAlexey Samsonov   --SymbolIterator;
1808df3a07aSAlexey Samsonov   if (SymbolIterator->first.Size != 0 &&
1818df3a07aSAlexey Samsonov       SymbolIterator->first.Addr + SymbolIterator->first.Size <= Address)
1828df3a07aSAlexey Samsonov     return false;
1838df3a07aSAlexey Samsonov   Name = SymbolIterator->second.str();
1848df3a07aSAlexey Samsonov   Addr = SymbolIterator->first.Addr;
1858df3a07aSAlexey Samsonov   Size = SymbolIterator->first.Size;
1868df3a07aSAlexey Samsonov   return true;
1878df3a07aSAlexey Samsonov }
1888df3a07aSAlexey Samsonov 
1898df3a07aSAlexey Samsonov DILineInfo SymbolizableObjectFile::symbolizeCode(uint64_t ModuleOffset,
1908df3a07aSAlexey Samsonov                                                  FunctionNameKind FNKind,
1918df3a07aSAlexey Samsonov                                                  bool UseSymbolTable) const {
1928df3a07aSAlexey Samsonov   DILineInfo LineInfo;
1938df3a07aSAlexey Samsonov   if (DebugInfoContext) {
1948df3a07aSAlexey Samsonov     LineInfo = DebugInfoContext->getLineInfoForAddress(
1958df3a07aSAlexey Samsonov         ModuleOffset, getDILineInfoSpecifier(FNKind));
1968df3a07aSAlexey Samsonov   }
1978df3a07aSAlexey Samsonov   // Override function name from symbol table if necessary.
1988df3a07aSAlexey Samsonov   if (FNKind == FunctionNameKind::LinkageName && UseSymbolTable) {
1998df3a07aSAlexey Samsonov     std::string FunctionName;
2008df3a07aSAlexey Samsonov     uint64_t Start, Size;
2018df3a07aSAlexey Samsonov     if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset,
2028df3a07aSAlexey Samsonov                                FunctionName, Start, Size)) {
2038df3a07aSAlexey Samsonov       LineInfo.FunctionName = FunctionName;
2048df3a07aSAlexey Samsonov     }
2058df3a07aSAlexey Samsonov   }
2068df3a07aSAlexey Samsonov   return LineInfo;
2078df3a07aSAlexey Samsonov }
2088df3a07aSAlexey Samsonov 
2098df3a07aSAlexey Samsonov DIInliningInfo SymbolizableObjectFile::symbolizeInlinedCode(
2108df3a07aSAlexey Samsonov     uint64_t ModuleOffset, FunctionNameKind FNKind, bool UseSymbolTable) const {
2118df3a07aSAlexey Samsonov   DIInliningInfo InlinedContext;
2128df3a07aSAlexey Samsonov 
2138df3a07aSAlexey Samsonov   if (DebugInfoContext)
2148df3a07aSAlexey Samsonov     InlinedContext = DebugInfoContext->getInliningInfoForAddress(
2158df3a07aSAlexey Samsonov         ModuleOffset, getDILineInfoSpecifier(FNKind));
2168df3a07aSAlexey Samsonov   // Make sure there is at least one frame in context.
2178df3a07aSAlexey Samsonov   if (InlinedContext.getNumberOfFrames() == 0)
2188df3a07aSAlexey Samsonov     InlinedContext.addFrame(DILineInfo());
2198df3a07aSAlexey Samsonov 
2208df3a07aSAlexey Samsonov   if (FNKind != FunctionNameKind::LinkageName || !UseSymbolTable)
2218df3a07aSAlexey Samsonov     return InlinedContext;
2228df3a07aSAlexey Samsonov 
2238df3a07aSAlexey Samsonov   // Override the function name in lower frame with name from symbol table.
2248df3a07aSAlexey Samsonov   // We can't directly change the last element of DIInliningInfo, so copy
2258df3a07aSAlexey Samsonov   // all frames into new context, replacing function name in the last one.
2268df3a07aSAlexey Samsonov   DIInliningInfo PatchedInlinedContext;
2278df3a07aSAlexey Samsonov   for (uint32_t i = 0, n = InlinedContext.getNumberOfFrames(); i < n; i++) {
2288df3a07aSAlexey Samsonov     DILineInfo LineInfo = InlinedContext.getFrame(i);
2298df3a07aSAlexey Samsonov     if (i == n - 1) {
2308df3a07aSAlexey Samsonov       std::string FunctionName;
2318df3a07aSAlexey Samsonov       uint64_t Start, Size;
2328df3a07aSAlexey Samsonov       if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset,
2338df3a07aSAlexey Samsonov                                  FunctionName, Start, Size)) {
2348df3a07aSAlexey Samsonov         LineInfo.FunctionName = FunctionName;
2358df3a07aSAlexey Samsonov       }
2368df3a07aSAlexey Samsonov     }
2378df3a07aSAlexey Samsonov     PatchedInlinedContext.addFrame(LineInfo);
2388df3a07aSAlexey Samsonov   }
2398df3a07aSAlexey Samsonov   return PatchedInlinedContext;
2408df3a07aSAlexey Samsonov }
2418df3a07aSAlexey Samsonov 
242*76f7ecb8SAlexey Samsonov DIGlobal SymbolizableObjectFile::symbolizeData(uint64_t ModuleOffset) const {
243*76f7ecb8SAlexey Samsonov   DIGlobal Res;
244*76f7ecb8SAlexey Samsonov   getNameFromSymbolTable(SymbolRef::ST_Data, ModuleOffset, Res.Name, Res.Start,
245*76f7ecb8SAlexey Samsonov                          Res.Size);
246*76f7ecb8SAlexey Samsonov   return Res;
2478df3a07aSAlexey Samsonov }
2488df3a07aSAlexey Samsonov 
2498df3a07aSAlexey Samsonov }  // namespace symbolize
2508df3a07aSAlexey Samsonov }  // namespace llvm
2518df3a07aSAlexey Samsonov 
252