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