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