Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 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" |
Reid Kleckner | c038e2d | 2015-11-13 17:00:36 +0000 | [diff] [blame] | 17 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 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 | SymbolRef::Type SymbolType = Symbol.getType(); |
| 123 | if (SymbolType != SymbolRef::ST_Function && SymbolType != SymbolRef::ST_Data) |
| 124 | return std::error_code(); |
| 125 | ErrorOr<uint64_t> SymbolAddressOrErr = Symbol.getAddress(); |
| 126 | if (auto EC = SymbolAddressOrErr.getError()) |
| 127 | return EC; |
| 128 | uint64_t SymbolAddress = *SymbolAddressOrErr; |
| 129 | if (OpdExtractor) { |
| 130 | // For big-endian PowerPC64 ELF, symbols in the .opd section refer to |
| 131 | // function descriptors. The first word of the descriptor is a pointer to |
| 132 | // the function's code. |
| 133 | // For the purposes of symbolization, pretend the symbol's address is that |
| 134 | // of the function's code, not the descriptor. |
| 135 | uint64_t OpdOffset = SymbolAddress - OpdAddress; |
| 136 | uint32_t OpdOffset32 = OpdOffset; |
| 137 | if (OpdOffset == OpdOffset32 && |
| 138 | OpdExtractor->isValidOffsetForAddress(OpdOffset32)) |
| 139 | SymbolAddress = OpdExtractor->getAddress(&OpdOffset32); |
| 140 | } |
| 141 | ErrorOr<StringRef> SymbolNameOrErr = Symbol.getName(); |
| 142 | if (auto EC = SymbolNameOrErr.getError()) |
| 143 | return EC; |
| 144 | StringRef SymbolName = *SymbolNameOrErr; |
| 145 | // Mach-O symbol table names have leading underscore, skip it. |
| 146 | if (Module->isMachO() && SymbolName.size() > 0 && SymbolName[0] == '_') |
| 147 | SymbolName = SymbolName.drop_front(); |
| 148 | // FIXME: If a function has alias, there are two entries in symbol table |
| 149 | // with same address size. Make sure we choose the correct one. |
| 150 | auto &M = SymbolType == SymbolRef::ST_Function ? Functions : Objects; |
| 151 | SymbolDesc SD = { SymbolAddress, SymbolSize }; |
| 152 | M.insert(std::make_pair(SD, SymbolName)); |
| 153 | return std::error_code(); |
| 154 | } |
| 155 | |
| 156 | // Return true if this is a 32-bit x86 PE COFF module. |
| 157 | bool SymbolizableObjectFile::isWin32Module() const { |
| 158 | auto *CoffObject = dyn_cast<COFFObjectFile>(Module); |
| 159 | return CoffObject && CoffObject->getMachine() == COFF::IMAGE_FILE_MACHINE_I386; |
| 160 | } |
| 161 | |
| 162 | uint64_t SymbolizableObjectFile::getModulePreferredBase() const { |
| 163 | if (auto *CoffObject = dyn_cast<COFFObjectFile>(Module)) |
| 164 | return CoffObject->getImageBase(); |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | bool SymbolizableObjectFile::getNameFromSymbolTable(SymbolRef::Type Type, |
| 169 | uint64_t Address, |
| 170 | std::string &Name, |
| 171 | uint64_t &Addr, |
| 172 | uint64_t &Size) const { |
| 173 | const auto &SymbolMap = Type == SymbolRef::ST_Function ? Functions : Objects; |
| 174 | if (SymbolMap.empty()) |
| 175 | return false; |
| 176 | SymbolDesc SD = { Address, Address }; |
| 177 | auto SymbolIterator = SymbolMap.upper_bound(SD); |
| 178 | if (SymbolIterator == SymbolMap.begin()) |
| 179 | return false; |
| 180 | --SymbolIterator; |
| 181 | if (SymbolIterator->first.Size != 0 && |
| 182 | SymbolIterator->first.Addr + SymbolIterator->first.Size <= Address) |
| 183 | return false; |
| 184 | Name = SymbolIterator->second.str(); |
| 185 | Addr = SymbolIterator->first.Addr; |
| 186 | Size = SymbolIterator->first.Size; |
| 187 | return true; |
| 188 | } |
| 189 | |
Reid Kleckner | c038e2d | 2015-11-13 17:00:36 +0000 | [diff] [blame] | 190 | bool SymbolizableObjectFile::shouldOverrideWithSymbolTable( |
| 191 | FunctionNameKind FNKind, bool UseSymbolTable) const { |
| 192 | // When DWARF is used with -gline-tables-only / -gmlt, the symbol table gives |
| 193 | // better answers for linkage names than the DIContext. Otherwise, we are |
| 194 | // probably using PEs and PDBs, and we shouldn't do the override. PE files |
| 195 | // generally only contain the names of exported symbols. |
| 196 | return FNKind == FunctionNameKind::LinkageName && UseSymbolTable && |
| 197 | isa<DWARFContext>(DebugInfoContext.get()); |
| 198 | } |
| 199 | |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 200 | DILineInfo SymbolizableObjectFile::symbolizeCode(uint64_t ModuleOffset, |
| 201 | FunctionNameKind FNKind, |
| 202 | bool UseSymbolTable) const { |
| 203 | DILineInfo LineInfo; |
| 204 | if (DebugInfoContext) { |
| 205 | LineInfo = DebugInfoContext->getLineInfoForAddress( |
| 206 | ModuleOffset, getDILineInfoSpecifier(FNKind)); |
| 207 | } |
| 208 | // Override function name from symbol table if necessary. |
Reid Kleckner | c038e2d | 2015-11-13 17:00:36 +0000 | [diff] [blame] | 209 | if (shouldOverrideWithSymbolTable(FNKind, UseSymbolTable)) { |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 210 | std::string FunctionName; |
| 211 | uint64_t Start, Size; |
| 212 | if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset, |
| 213 | FunctionName, Start, Size)) { |
| 214 | LineInfo.FunctionName = FunctionName; |
| 215 | } |
| 216 | } |
| 217 | return LineInfo; |
| 218 | } |
| 219 | |
| 220 | DIInliningInfo SymbolizableObjectFile::symbolizeInlinedCode( |
| 221 | uint64_t ModuleOffset, FunctionNameKind FNKind, bool UseSymbolTable) const { |
| 222 | DIInliningInfo InlinedContext; |
| 223 | |
| 224 | if (DebugInfoContext) |
| 225 | InlinedContext = DebugInfoContext->getInliningInfoForAddress( |
| 226 | ModuleOffset, getDILineInfoSpecifier(FNKind)); |
| 227 | // Make sure there is at least one frame in context. |
| 228 | if (InlinedContext.getNumberOfFrames() == 0) |
| 229 | InlinedContext.addFrame(DILineInfo()); |
| 230 | |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 231 | // Override the function name in lower frame with name from symbol table. |
Reid Kleckner | c038e2d | 2015-11-13 17:00:36 +0000 | [diff] [blame] | 232 | if (shouldOverrideWithSymbolTable(FNKind, UseSymbolTable)) { |
Alexey Samsonov | e46bd74 | 2015-10-30 00:02:55 +0000 | [diff] [blame] | 233 | std::string FunctionName; |
| 234 | uint64_t Start, Size; |
| 235 | if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset, |
| 236 | FunctionName, Start, Size)) { |
| 237 | InlinedContext.getMutableFrame(InlinedContext.getNumberOfFrames() - 1) |
| 238 | ->FunctionName = FunctionName; |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 239 | } |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 240 | } |
Alexey Samsonov | e46bd74 | 2015-10-30 00:02:55 +0000 | [diff] [blame] | 241 | |
| 242 | return InlinedContext; |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Alexey Samsonov | 76f7ecb | 2015-10-29 23:49:19 +0000 | [diff] [blame] | 245 | DIGlobal SymbolizableObjectFile::symbolizeData(uint64_t ModuleOffset) const { |
| 246 | DIGlobal Res; |
| 247 | getNameFromSymbolTable(SymbolRef::ST_Data, ModuleOffset, Res.Name, Res.Start, |
| 248 | Res.Size); |
| 249 | return Res; |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | } // namespace symbolize |
| 253 | } // namespace llvm |
| 254 | |