Eugene Zelenko | 44d9512 | 2017-02-09 01:09:54 +0000 | [diff] [blame] | 1 | //===- SymbolizableObjectFile.cpp -----------------------------------------===// |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Implementation of SymbolizableObjectFile class. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "SymbolizableObjectFile.h" |
Eugene Zelenko | 44d9512 | 2017-02-09 01:09:54 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/STLExtras.h" |
| 15 | #include "llvm/ADT/StringRef.h" |
| 16 | #include "llvm/ADT/Triple.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 17 | #include "llvm/BinaryFormat/COFF.h" |
Reid Kleckner | c038e2d | 2015-11-13 17:00:36 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
Eugene Zelenko | 44d9512 | 2017-02-09 01:09:54 +0000 | [diff] [blame] | 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" |
Eugene Zelenko | 44d9512 | 2017-02-09 01:09:54 +0000 | [diff] [blame] | 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> |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 33 | |
Eugene Zelenko | 44d9512 | 2017-02-09 01:09:54 +0000 | [diff] [blame] | 34 | using namespace llvm; |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 35 | using namespace object; |
Eugene Zelenko | 44d9512 | 2017-02-09 01:09:54 +0000 | [diff] [blame] | 36 | using namespace symbolize; |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 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 { |
Eugene Zelenko | 44d9512 | 2017-02-09 01:09:54 +0000 | [diff] [blame] | 89 | |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 90 | struct OffsetNamePair { |
| 91 | uint32_t Offset; |
| 92 | StringRef Name; |
Eugene Zelenko | 44d9512 | 2017-02-09 01:09:54 +0000 | [diff] [blame] | 93 | |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 94 | bool operator<(const OffsetNamePair &R) const { |
| 95 | return Offset < R.Offset; |
| 96 | } |
| 97 | }; |
Eugene Zelenko | 44d9512 | 2017-02-09 01:09:54 +0000 | [diff] [blame] | 98 | |
| 99 | } // end anonymous namespace |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 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) { |
Matt Davis | 123be5d | 2019-02-14 23:50:35 +0000 | [diff] [blame] | 139 | // Avoid adding symbols from an unknown/undefined section. |
| 140 | const ObjectFile *Obj = Symbol.getObject(); |
| 141 | Expected<section_iterator> Sec = Symbol.getSection(); |
| 142 | if (!Sec || (Obj && Obj->section_end() == *Sec)) |
| 143 | return std::error_code(); |
Kevin Enderby | 7bd8d99 | 2016-05-02 20:28:12 +0000 | [diff] [blame] | 144 | Expected<SymbolRef::Type> SymbolTypeOrErr = Symbol.getType(); |
| 145 | if (!SymbolTypeOrErr) |
| 146 | return errorToErrorCode(SymbolTypeOrErr.takeError()); |
Kevin Enderby | 5afbc1c | 2016-03-23 20:27:00 +0000 | [diff] [blame] | 147 | SymbolRef::Type SymbolType = *SymbolTypeOrErr; |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 148 | if (SymbolType != SymbolRef::ST_Function && SymbolType != SymbolRef::ST_Data) |
| 149 | return std::error_code(); |
Kevin Enderby | 931cb65 | 2016-06-24 18:24:42 +0000 | [diff] [blame] | 150 | Expected<uint64_t> SymbolAddressOrErr = Symbol.getAddress(); |
| 151 | if (!SymbolAddressOrErr) |
| 152 | return errorToErrorCode(SymbolAddressOrErr.takeError()); |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 153 | uint64_t SymbolAddress = *SymbolAddressOrErr; |
| 154 | if (OpdExtractor) { |
| 155 | // For big-endian PowerPC64 ELF, symbols in the .opd section refer to |
| 156 | // function descriptors. The first word of the descriptor is a pointer to |
| 157 | // the function's code. |
| 158 | // For the purposes of symbolization, pretend the symbol's address is that |
| 159 | // of the function's code, not the descriptor. |
| 160 | uint64_t OpdOffset = SymbolAddress - OpdAddress; |
| 161 | uint32_t OpdOffset32 = OpdOffset; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 162 | if (OpdOffset == OpdOffset32 && |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 163 | OpdExtractor->isValidOffsetForAddress(OpdOffset32)) |
| 164 | SymbolAddress = OpdExtractor->getAddress(&OpdOffset32); |
| 165 | } |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 166 | Expected<StringRef> SymbolNameOrErr = Symbol.getName(); |
| 167 | if (!SymbolNameOrErr) |
| 168 | return errorToErrorCode(SymbolNameOrErr.takeError()); |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 169 | StringRef SymbolName = *SymbolNameOrErr; |
| 170 | // Mach-O symbol table names have leading underscore, skip it. |
Eugene Zelenko | 44d9512 | 2017-02-09 01:09:54 +0000 | [diff] [blame] | 171 | if (Module->isMachO() && !SymbolName.empty() && SymbolName[0] == '_') |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 172 | SymbolName = SymbolName.drop_front(); |
| 173 | // FIXME: If a function has alias, there are two entries in symbol table |
| 174 | // with same address size. Make sure we choose the correct one. |
| 175 | auto &M = SymbolType == SymbolRef::ST_Function ? Functions : Objects; |
| 176 | SymbolDesc SD = { SymbolAddress, SymbolSize }; |
| 177 | M.insert(std::make_pair(SD, SymbolName)); |
| 178 | return std::error_code(); |
| 179 | } |
| 180 | |
| 181 | // Return true if this is a 32-bit x86 PE COFF module. |
| 182 | bool SymbolizableObjectFile::isWin32Module() const { |
| 183 | auto *CoffObject = dyn_cast<COFFObjectFile>(Module); |
| 184 | return CoffObject && CoffObject->getMachine() == COFF::IMAGE_FILE_MACHINE_I386; |
| 185 | } |
| 186 | |
| 187 | uint64_t SymbolizableObjectFile::getModulePreferredBase() const { |
| 188 | if (auto *CoffObject = dyn_cast<COFFObjectFile>(Module)) |
| 189 | return CoffObject->getImageBase(); |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | bool SymbolizableObjectFile::getNameFromSymbolTable(SymbolRef::Type Type, |
| 194 | uint64_t Address, |
| 195 | std::string &Name, |
| 196 | uint64_t &Addr, |
| 197 | uint64_t &Size) const { |
| 198 | const auto &SymbolMap = Type == SymbolRef::ST_Function ? Functions : Objects; |
| 199 | if (SymbolMap.empty()) |
| 200 | return false; |
| 201 | SymbolDesc SD = { Address, Address }; |
| 202 | auto SymbolIterator = SymbolMap.upper_bound(SD); |
| 203 | if (SymbolIterator == SymbolMap.begin()) |
| 204 | return false; |
| 205 | --SymbolIterator; |
| 206 | if (SymbolIterator->first.Size != 0 && |
| 207 | SymbolIterator->first.Addr + SymbolIterator->first.Size <= Address) |
| 208 | return false; |
| 209 | Name = SymbolIterator->second.str(); |
| 210 | Addr = SymbolIterator->first.Addr; |
| 211 | Size = SymbolIterator->first.Size; |
| 212 | return true; |
| 213 | } |
| 214 | |
Reid Kleckner | c038e2d | 2015-11-13 17:00:36 +0000 | [diff] [blame] | 215 | bool SymbolizableObjectFile::shouldOverrideWithSymbolTable( |
| 216 | FunctionNameKind FNKind, bool UseSymbolTable) const { |
| 217 | // When DWARF is used with -gline-tables-only / -gmlt, the symbol table gives |
| 218 | // better answers for linkage names than the DIContext. Otherwise, we are |
| 219 | // probably using PEs and PDBs, and we shouldn't do the override. PE files |
| 220 | // generally only contain the names of exported symbols. |
| 221 | return FNKind == FunctionNameKind::LinkageName && UseSymbolTable && |
| 222 | isa<DWARFContext>(DebugInfoContext.get()); |
| 223 | } |
| 224 | |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame^] | 225 | DILineInfo |
| 226 | SymbolizableObjectFile::symbolizeCode(object::SectionedAddress ModuleOffset, |
| 227 | FunctionNameKind FNKind, |
| 228 | bool UseSymbolTable) const { |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 229 | DILineInfo LineInfo; |
| 230 | if (DebugInfoContext) { |
| 231 | LineInfo = DebugInfoContext->getLineInfoForAddress( |
| 232 | ModuleOffset, getDILineInfoSpecifier(FNKind)); |
| 233 | } |
| 234 | // Override function name from symbol table if necessary. |
Reid Kleckner | c038e2d | 2015-11-13 17:00:36 +0000 | [diff] [blame] | 235 | if (shouldOverrideWithSymbolTable(FNKind, UseSymbolTable)) { |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 236 | std::string FunctionName; |
| 237 | uint64_t Start, Size; |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame^] | 238 | if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset.Address, |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 239 | FunctionName, Start, Size)) { |
| 240 | LineInfo.FunctionName = FunctionName; |
| 241 | } |
| 242 | } |
| 243 | return LineInfo; |
| 244 | } |
| 245 | |
| 246 | DIInliningInfo SymbolizableObjectFile::symbolizeInlinedCode( |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame^] | 247 | object::SectionedAddress ModuleOffset, FunctionNameKind FNKind, |
| 248 | bool UseSymbolTable) const { |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 249 | DIInliningInfo InlinedContext; |
| 250 | |
| 251 | if (DebugInfoContext) |
| 252 | InlinedContext = DebugInfoContext->getInliningInfoForAddress( |
| 253 | ModuleOffset, getDILineInfoSpecifier(FNKind)); |
| 254 | // Make sure there is at least one frame in context. |
| 255 | if (InlinedContext.getNumberOfFrames() == 0) |
| 256 | InlinedContext.addFrame(DILineInfo()); |
| 257 | |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 258 | // Override the function name in lower frame with name from symbol table. |
Reid Kleckner | c038e2d | 2015-11-13 17:00:36 +0000 | [diff] [blame] | 259 | if (shouldOverrideWithSymbolTable(FNKind, UseSymbolTable)) { |
Alexey Samsonov | e46bd74 | 2015-10-30 00:02:55 +0000 | [diff] [blame] | 260 | std::string FunctionName; |
| 261 | uint64_t Start, Size; |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame^] | 262 | if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset.Address, |
Alexey Samsonov | e46bd74 | 2015-10-30 00:02:55 +0000 | [diff] [blame] | 263 | FunctionName, Start, Size)) { |
| 264 | InlinedContext.getMutableFrame(InlinedContext.getNumberOfFrames() - 1) |
| 265 | ->FunctionName = FunctionName; |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 266 | } |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 267 | } |
Alexey Samsonov | e46bd74 | 2015-10-30 00:02:55 +0000 | [diff] [blame] | 268 | |
| 269 | return InlinedContext; |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame^] | 272 | DIGlobal SymbolizableObjectFile::symbolizeData( |
| 273 | object::SectionedAddress ModuleOffset) const { |
Alexey Samsonov | 76f7ecb | 2015-10-29 23:49:19 +0000 | [diff] [blame] | 274 | DIGlobal Res; |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame^] | 275 | getNameFromSymbolTable(SymbolRef::ST_Data, ModuleOffset.Address, Res.Name, |
| 276 | Res.Start, Res.Size); |
Alexey Samsonov | 76f7ecb | 2015-10-29 23:49:19 +0000 | [diff] [blame] | 277 | return Res; |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 278 | } |