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