Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 1 | //===-- LLVMSymbolize.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 for LLVM symbolization library. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Alexey Samsonov | 57f8837 | 2015-10-26 17:56:12 +0000 | [diff] [blame] | 14 | #include "llvm/DebugInfo/Symbolize/Symbolize.h" |
| 15 | |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 16 | #include "SymbolizableObjectFile.h" |
| 17 | |
Alexey Samsonov | dd71c5b | 2013-03-19 15:33:18 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h" |
Alexey Samsonov | a591ae6 | 2013-08-26 18:12:03 +0000 | [diff] [blame] | 19 | #include "llvm/Config/config.h" |
Zachary Turner | 6489d7b | 2015-04-23 17:37:47 +0000 | [diff] [blame] | 20 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
Zachary Turner | 20dbd0d | 2015-04-27 17:19:51 +0000 | [diff] [blame] | 21 | #include "llvm/DebugInfo/PDB/PDB.h" |
| 22 | #include "llvm/DebugInfo/PDB/PDBContext.h" |
Alexey Samsonov | a5f0768 | 2014-02-26 13:10:01 +0000 | [diff] [blame] | 23 | #include "llvm/Object/ELFObjectFile.h" |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 24 | #include "llvm/Object/MachO.h" |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame^] | 25 | #include "llvm/Object/MachOUniversal.h" |
Reid Kleckner | c25c794 | 2015-08-10 21:47:11 +0000 | [diff] [blame] | 26 | #include "llvm/Support/COFF.h" |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Casting.h" |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Compression.h" |
| 29 | #include "llvm/Support/DataExtractor.h" |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Errc.h" |
Alexey Samsonov | 5239d58 | 2013-06-04 07:57:38 +0000 | [diff] [blame] | 31 | #include "llvm/Support/FileSystem.h" |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 32 | #include "llvm/Support/MemoryBuffer.h" |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Path.h" |
Alexey Samsonov | a591ae6 | 2013-08-26 18:12:03 +0000 | [diff] [blame] | 34 | #include <stdlib.h> |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 35 | |
Zachary Turner | c007aa4 | 2015-05-06 22:26:30 +0000 | [diff] [blame] | 36 | #if defined(_MSC_VER) |
| 37 | #include <Windows.h> |
| 38 | #include <DbgHelp.h> |
Leny Kholodov | bebb27b | 2015-07-02 14:34:57 +0000 | [diff] [blame] | 39 | #pragma comment(lib, "dbghelp.lib") |
Reid Kleckner | c25c794 | 2015-08-10 21:47:11 +0000 | [diff] [blame] | 40 | |
| 41 | // Windows.h conflicts with our COFF header definitions. |
| 42 | #ifdef IMAGE_FILE_MACHINE_I386 |
| 43 | #undef IMAGE_FILE_MACHINE_I386 |
| 44 | #endif |
Zachary Turner | c007aa4 | 2015-05-06 22:26:30 +0000 | [diff] [blame] | 45 | #endif |
| 46 | |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 47 | namespace llvm { |
| 48 | namespace symbolize { |
| 49 | |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 50 | ErrorOr<DILineInfo> LLVMSymbolizer::symbolizeCode(const std::string &ModuleName, |
| 51 | uint64_t ModuleOffset) { |
| 52 | auto InfoOrErr = getOrCreateModuleInfo(ModuleName); |
| 53 | if (auto EC = InfoOrErr.getError()) |
| 54 | return EC; |
| 55 | SymbolizableModule *Info = InfoOrErr.get(); |
Reid Kleckner | e94fef7 | 2015-10-09 00:15:01 +0000 | [diff] [blame] | 56 | |
| 57 | // If the user is giving us relative addresses, add the preferred base of the |
| 58 | // object to the offset before we do the query. It's what DIContext expects. |
| 59 | if (Opts.RelativeAddresses) |
| 60 | ModuleOffset += Info->getModulePreferredBase(); |
| 61 | |
Alexey Samsonov | 0fb6451 | 2015-10-26 22:34:56 +0000 | [diff] [blame] | 62 | DILineInfo LineInfo = Info->symbolizeCode(ModuleOffset, Opts.PrintFunctions, |
| 63 | Opts.UseSymbolTable); |
Alexey Samsonov | 6881249 | 2015-11-03 21:36:13 +0000 | [diff] [blame] | 64 | if (Opts.Demangle) |
| 65 | LineInfo.FunctionName = DemangleName(LineInfo.FunctionName, Info); |
Alexey Samsonov | d6aa820 | 2015-11-03 22:20:52 +0000 | [diff] [blame] | 66 | return LineInfo; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 69 | ErrorOr<DIInliningInfo> |
Alexey Samsonov | d6aa820 | 2015-11-03 22:20:52 +0000 | [diff] [blame] | 70 | LLVMSymbolizer::symbolizeInlinedCode(const std::string &ModuleName, |
| 71 | uint64_t ModuleOffset) { |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 72 | auto InfoOrErr = getOrCreateModuleInfo(ModuleName); |
| 73 | if (auto EC = InfoOrErr.getError()) |
| 74 | return EC; |
| 75 | SymbolizableModule *Info = InfoOrErr.get(); |
Alexey Samsonov | 46c1ce6 | 2015-10-30 00:40:20 +0000 | [diff] [blame] | 76 | |
| 77 | // If the user is giving us relative addresses, add the preferred base of the |
| 78 | // object to the offset before we do the query. It's what DIContext expects. |
| 79 | if (Opts.RelativeAddresses) |
| 80 | ModuleOffset += Info->getModulePreferredBase(); |
| 81 | |
| 82 | DIInliningInfo InlinedContext = Info->symbolizeInlinedCode( |
| 83 | ModuleOffset, Opts.PrintFunctions, Opts.UseSymbolTable); |
Alexey Samsonov | 6881249 | 2015-11-03 21:36:13 +0000 | [diff] [blame] | 84 | if (Opts.Demangle) { |
| 85 | for (int i = 0, n = InlinedContext.getNumberOfFrames(); i < n; i++) { |
| 86 | auto *Frame = InlinedContext.getMutableFrame(i); |
| 87 | Frame->FunctionName = DemangleName(Frame->FunctionName, Info); |
| 88 | } |
| 89 | } |
Alexey Samsonov | d6aa820 | 2015-11-03 22:20:52 +0000 | [diff] [blame] | 90 | return InlinedContext; |
Alexey Samsonov | 46c1ce6 | 2015-10-30 00:40:20 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 93 | ErrorOr<DIGlobal> LLVMSymbolizer::symbolizeData(const std::string &ModuleName, |
| 94 | uint64_t ModuleOffset) { |
| 95 | auto InfoOrErr = getOrCreateModuleInfo(ModuleName); |
| 96 | if (auto EC = InfoOrErr.getError()) |
| 97 | return EC; |
| 98 | SymbolizableModule *Info = InfoOrErr.get(); |
Alexey Samsonov | 6881249 | 2015-11-03 21:36:13 +0000 | [diff] [blame] | 99 | |
| 100 | // If the user is giving us relative addresses, add the preferred base of |
| 101 | // the object to the offset before we do the query. It's what DIContext |
| 102 | // expects. |
| 103 | if (Opts.RelativeAddresses) |
| 104 | ModuleOffset += Info->getModulePreferredBase(); |
| 105 | |
| 106 | DIGlobal Global = Info->symbolizeData(ModuleOffset); |
| 107 | if (Opts.Demangle) |
| 108 | Global.Name = DemangleName(Global.Name, Info); |
Alexey Samsonov | d6aa820 | 2015-11-03 22:20:52 +0000 | [diff] [blame] | 109 | return Global; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Dmitry Vyukov | e8504e2 | 2013-03-19 10:24:42 +0000 | [diff] [blame] | 112 | void LLVMSymbolizer::flush() { |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame^] | 113 | ObjectForUBPathAndArch.clear(); |
| 114 | BinaryForPath.clear(); |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 115 | ObjectPairForPathArch.clear(); |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame^] | 116 | Modules.clear(); |
Dmitry Vyukov | e8504e2 | 2013-03-19 10:24:42 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 119 | // For Path="/path/to/foo" and Basename="foo" assume that debug info is in |
| 120 | // /path/to/foo.dSYM/Contents/Resources/DWARF/foo. |
| 121 | // For Path="/path/to/bar.dSYM" and Basename="foo" assume that debug info is in |
| 122 | // /path/to/bar.dSYM/Contents/Resources/DWARF/foo. |
| 123 | static |
| 124 | std::string getDarwinDWARFResourceForPath( |
| 125 | const std::string &Path, const std::string &Basename) { |
| 126 | SmallString<16> ResourceName = StringRef(Path); |
| 127 | if (sys::path::extension(Path) != ".dSYM") { |
| 128 | ResourceName += ".dSYM"; |
| 129 | } |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 130 | sys::path::append(ResourceName, "Contents", "Resources", "DWARF"); |
| 131 | sys::path::append(ResourceName, Basename); |
| 132 | return ResourceName.str(); |
| 133 | } |
| 134 | |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 135 | static bool checkFileCRC(StringRef Path, uint32_t CRCHash) { |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 136 | ErrorOr<std::unique_ptr<MemoryBuffer>> MB = |
| 137 | MemoryBuffer::getFileOrSTDIN(Path); |
| 138 | if (!MB) |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 139 | return false; |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 140 | return !zlib::isAvailable() || CRCHash == zlib::crc32(MB.get()->getBuffer()); |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static bool findDebugBinary(const std::string &OrigPath, |
| 144 | const std::string &DebuglinkName, uint32_t CRCHash, |
| 145 | std::string &Result) { |
Alexey Samsonov | a591ae6 | 2013-08-26 18:12:03 +0000 | [diff] [blame] | 146 | std::string OrigRealPath = OrigPath; |
| 147 | #if defined(HAVE_REALPATH) |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 148 | if (char *RP = realpath(OrigPath.c_str(), nullptr)) { |
Alexey Samsonov | a591ae6 | 2013-08-26 18:12:03 +0000 | [diff] [blame] | 149 | OrigRealPath = RP; |
| 150 | free(RP); |
| 151 | } |
| 152 | #endif |
| 153 | SmallString<16> OrigDir(OrigRealPath); |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 154 | llvm::sys::path::remove_filename(OrigDir); |
| 155 | SmallString<16> DebugPath = OrigDir; |
| 156 | // Try /path/to/original_binary/debuglink_name |
| 157 | llvm::sys::path::append(DebugPath, DebuglinkName); |
| 158 | if (checkFileCRC(DebugPath, CRCHash)) { |
| 159 | Result = DebugPath.str(); |
| 160 | return true; |
| 161 | } |
| 162 | // Try /path/to/original_binary/.debug/debuglink_name |
Alexey Samsonov | a591ae6 | 2013-08-26 18:12:03 +0000 | [diff] [blame] | 163 | DebugPath = OrigRealPath; |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 164 | llvm::sys::path::append(DebugPath, ".debug", DebuglinkName); |
| 165 | if (checkFileCRC(DebugPath, CRCHash)) { |
| 166 | Result = DebugPath.str(); |
| 167 | return true; |
| 168 | } |
| 169 | // Try /usr/lib/debug/path/to/original_binary/debuglink_name |
| 170 | DebugPath = "/usr/lib/debug"; |
| 171 | llvm::sys::path::append(DebugPath, llvm::sys::path::relative_path(OrigDir), |
| 172 | DebuglinkName); |
| 173 | if (checkFileCRC(DebugPath, CRCHash)) { |
| 174 | Result = DebugPath.str(); |
| 175 | return true; |
| 176 | } |
| 177 | return false; |
| 178 | } |
| 179 | |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 180 | static bool getGNUDebuglinkContents(const ObjectFile *Obj, std::string &DebugName, |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 181 | uint32_t &CRCHash) { |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 182 | if (!Obj) |
| 183 | return false; |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 184 | for (const SectionRef &Section : Obj->sections()) { |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 185 | StringRef Name; |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 186 | Section.getName(Name); |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 187 | Name = Name.substr(Name.find_first_not_of("._")); |
| 188 | if (Name == "gnu_debuglink") { |
| 189 | StringRef Data; |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 190 | Section.getContents(Data); |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 191 | DataExtractor DE(Data, Obj->isLittleEndian(), 0); |
| 192 | uint32_t Offset = 0; |
| 193 | if (const char *DebugNameStr = DE.getCStr(&Offset)) { |
| 194 | // 4-byte align the offset. |
| 195 | Offset = (Offset + 3) & ~0x3; |
| 196 | if (DE.isValidOffsetForDataOfSize(Offset, 4)) { |
| 197 | DebugName = DebugNameStr; |
| 198 | CRCHash = DE.getU32(&Offset); |
| 199 | return true; |
| 200 | } |
| 201 | } |
| 202 | break; |
| 203 | } |
| 204 | } |
| 205 | return false; |
| 206 | } |
| 207 | |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 208 | static |
| 209 | bool darwinDsymMatchesBinary(const MachOObjectFile *DbgObj, |
| 210 | const MachOObjectFile *Obj) { |
| 211 | ArrayRef<uint8_t> dbg_uuid = DbgObj->getUuid(); |
| 212 | ArrayRef<uint8_t> bin_uuid = Obj->getUuid(); |
| 213 | if (dbg_uuid.empty() || bin_uuid.empty()) |
| 214 | return false; |
| 215 | return !memcmp(dbg_uuid.data(), bin_uuid.data(), dbg_uuid.size()); |
| 216 | } |
| 217 | |
| 218 | ObjectFile *LLVMSymbolizer::lookUpDsymFile(const std::string &ExePath, |
| 219 | const MachOObjectFile *MachExeObj, const std::string &ArchName) { |
| 220 | // On Darwin we may find DWARF in separate object file in |
| 221 | // resource directory. |
| 222 | std::vector<std::string> DsymPaths; |
| 223 | StringRef Filename = sys::path::filename(ExePath); |
| 224 | DsymPaths.push_back(getDarwinDWARFResourceForPath(ExePath, Filename)); |
| 225 | for (const auto &Path : Opts.DsymHints) { |
| 226 | DsymPaths.push_back(getDarwinDWARFResourceForPath(Path, Filename)); |
| 227 | } |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame^] | 228 | for (const auto &Path : DsymPaths) { |
| 229 | auto DbgObjOrErr = getOrCreateObject(Path, ArchName); |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 230 | if (!DbgObjOrErr) |
| 231 | continue; |
| 232 | ObjectFile *DbgObj = DbgObjOrErr.get(); |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame^] | 233 | const MachOObjectFile *MachDbgObj = dyn_cast<const MachOObjectFile>(DbgObj); |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 234 | if (!MachDbgObj) |
| 235 | continue; |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame^] | 236 | if (darwinDsymMatchesBinary(MachDbgObj, MachExeObj)) |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 237 | return DbgObj; |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 238 | } |
| 239 | return nullptr; |
| 240 | } |
| 241 | |
Alexey Samsonov | 5365a01 | 2015-11-04 00:30:26 +0000 | [diff] [blame] | 242 | ObjectFile *LLVMSymbolizer::lookUpDebuglinkObject(const std::string &Path, |
| 243 | const ObjectFile *Obj, |
| 244 | const std::string &ArchName) { |
| 245 | std::string DebuglinkName; |
| 246 | uint32_t CRCHash; |
| 247 | std::string DebugBinaryPath; |
| 248 | if (!getGNUDebuglinkContents(Obj, DebuglinkName, CRCHash)) |
| 249 | return nullptr; |
| 250 | if (!findDebugBinary(Path, DebuglinkName, CRCHash, DebugBinaryPath)) |
| 251 | return nullptr; |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame^] | 252 | auto DbgObjOrErr = getOrCreateObject(DebugBinaryPath, ArchName); |
Alexey Samsonov | 5365a01 | 2015-11-04 00:30:26 +0000 | [diff] [blame] | 253 | if (!DbgObjOrErr) |
| 254 | return nullptr; |
Alexey Samsonov | 5365a01 | 2015-11-04 00:30:26 +0000 | [diff] [blame] | 255 | return DbgObjOrErr.get(); |
| 256 | } |
| 257 | |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 258 | ErrorOr<LLVMSymbolizer::ObjectPair> |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame^] | 259 | LLVMSymbolizer::getOrCreateObjectPair(const std::string &Path, |
| 260 | const std::string &ArchName) { |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 261 | const auto &I = ObjectPairForPathArch.find(std::make_pair(Path, ArchName)); |
| 262 | if (I != ObjectPairForPathArch.end()) |
| 263 | return I->second; |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 264 | |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame^] | 265 | auto ObjOrErr = getOrCreateObject(Path, ArchName); |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 266 | if (auto EC = ObjOrErr.getError()) { |
| 267 | ObjectPairForPathArch.insert( |
| 268 | std::make_pair(std::make_pair(Path, ArchName), EC)); |
| 269 | return EC; |
| 270 | } |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 271 | |
| 272 | ObjectFile *Obj = ObjOrErr.get(); |
| 273 | assert(Obj != nullptr); |
| 274 | ObjectFile *DbgObj = nullptr; |
| 275 | |
| 276 | if (auto MachObj = dyn_cast<const MachOObjectFile>(Obj)) |
| 277 | DbgObj = lookUpDsymFile(Path, MachObj, ArchName); |
Alexey Samsonov | 5365a01 | 2015-11-04 00:30:26 +0000 | [diff] [blame] | 278 | if (!DbgObj) |
| 279 | DbgObj = lookUpDebuglinkObject(Path, Obj, ArchName); |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 280 | if (!DbgObj) |
| 281 | DbgObj = Obj; |
| 282 | ObjectPair Res = std::make_pair(Obj, DbgObj); |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 283 | ObjectPairForPathArch.insert( |
| 284 | std::make_pair(std::make_pair(Path, ArchName), Res)); |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 285 | return Res; |
| 286 | } |
| 287 | |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 288 | ErrorOr<ObjectFile *> |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame^] | 289 | LLVMSymbolizer::getOrCreateObject(const std::string &Path, |
| 290 | const std::string &ArchName) { |
| 291 | const auto &I = BinaryForPath.find(Path); |
| 292 | Binary *Bin = nullptr; |
| 293 | if (I == BinaryForPath.end()) { |
| 294 | ErrorOr<OwningBinary<Binary>> BinOrErr = createBinary(Path); |
| 295 | if (auto EC = BinOrErr.getError()) { |
| 296 | BinaryForPath.insert(std::make_pair(Path, EC)); |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 297 | return EC; |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 298 | } |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame^] | 299 | Bin = BinOrErr->getBinary(); |
| 300 | BinaryForPath.insert(std::make_pair(Path, std::move(BinOrErr.get()))); |
| 301 | } else if (auto EC = I->second.getError()) { |
| 302 | return EC; |
| 303 | } else { |
| 304 | Bin = I->second->getBinary(); |
| 305 | } |
| 306 | |
| 307 | assert(Bin != nullptr); |
| 308 | |
| 309 | if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(Bin)) { |
| 310 | const auto &I = ObjectForUBPathAndArch.find(std::make_pair(Path, ArchName)); |
| 311 | if (I != ObjectForUBPathAndArch.end()) { |
| 312 | if (auto EC = I->second.getError()) |
| 313 | return EC; |
| 314 | return I->second->get(); |
| 315 | } |
| 316 | ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = |
| 317 | UB->getObjectForArch(ArchName); |
| 318 | if (auto EC = ObjOrErr.getError()) { |
| 319 | ObjectForUBPathAndArch.insert( |
| 320 | std::make_pair(std::make_pair(Path, ArchName), EC)); |
| 321 | return EC; |
| 322 | } |
| 323 | ObjectFile *Res = ObjOrErr->get(); |
| 324 | ObjectForUBPathAndArch.insert(std::make_pair(std::make_pair(Path, ArchName), |
| 325 | std::move(ObjOrErr.get()))); |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 326 | return Res; |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 327 | } |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 328 | if (Bin->isObject()) { |
| 329 | return cast<ObjectFile>(Bin); |
| 330 | } |
| 331 | return object_error::arch_not_found; |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 334 | ErrorOr<SymbolizableModule *> |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 335 | LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) { |
Alexander Potapenko | 45bfe37 | 2014-10-14 13:40:44 +0000 | [diff] [blame] | 336 | const auto &I = Modules.find(ModuleName); |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 337 | if (I != Modules.end()) { |
| 338 | auto &InfoOrErr = I->second; |
| 339 | if (auto EC = InfoOrErr.getError()) |
| 340 | return EC; |
| 341 | return InfoOrErr->get(); |
| 342 | } |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 343 | std::string BinaryName = ModuleName; |
| 344 | std::string ArchName = Opts.DefaultArch; |
Alexey Samsonov | b119b46 | 2013-07-17 06:45:36 +0000 | [diff] [blame] | 345 | size_t ColonPos = ModuleName.find_last_of(':'); |
| 346 | // Verify that substring after colon form a valid arch name. |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 347 | if (ColonPos != std::string::npos) { |
Alexey Samsonov | b119b46 | 2013-07-17 06:45:36 +0000 | [diff] [blame] | 348 | std::string ArchStr = ModuleName.substr(ColonPos + 1); |
NAKAMURA Takumi | 8ee89c6 | 2013-07-17 06:53:51 +0000 | [diff] [blame] | 349 | if (Triple(ArchStr).getArch() != Triple::UnknownArch) { |
Alexey Samsonov | b119b46 | 2013-07-17 06:45:36 +0000 | [diff] [blame] | 350 | BinaryName = ModuleName.substr(0, ColonPos); |
| 351 | ArchName = ArchStr; |
| 352 | } |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 353 | } |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame^] | 354 | auto ObjectsOrErr = getOrCreateObjectPair(BinaryName, ArchName); |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 355 | if (auto EC = ObjectsOrErr.getError()) { |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 356 | // Failed to find valid object file. |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 357 | Modules.insert(std::make_pair(ModuleName, EC)); |
| 358 | return EC; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 359 | } |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 360 | ObjectPair Objects = ObjectsOrErr.get(); |
| 361 | |
Alexey Samsonov | 7a952e5 | 2015-10-26 19:41:23 +0000 | [diff] [blame] | 362 | std::unique_ptr<DIContext> Context; |
Zachary Turner | 20dbd0d | 2015-04-27 17:19:51 +0000 | [diff] [blame] | 363 | if (auto CoffObject = dyn_cast<COFFObjectFile>(Objects.first)) { |
| 364 | // If this is a COFF object, assume it contains PDB debug information. If |
| 365 | // we don't find any we will fall back to the DWARF case. |
| 366 | std::unique_ptr<IPDBSession> Session; |
| 367 | PDB_ErrorCode Error = loadDataForEXE(PDB_ReaderType::DIA, |
| 368 | Objects.first->getFileName(), Session); |
Zachary Turner | c007aa4 | 2015-05-06 22:26:30 +0000 | [diff] [blame] | 369 | if (Error == PDB_ErrorCode::Success) { |
Alexey Samsonov | 7a952e5 | 2015-10-26 19:41:23 +0000 | [diff] [blame] | 370 | Context.reset(new PDBContext(*CoffObject, std::move(Session))); |
Zachary Turner | c007aa4 | 2015-05-06 22:26:30 +0000 | [diff] [blame] | 371 | } |
Zachary Turner | 20dbd0d | 2015-04-27 17:19:51 +0000 | [diff] [blame] | 372 | } |
| 373 | if (!Context) |
Alexey Samsonov | 7a952e5 | 2015-10-26 19:41:23 +0000 | [diff] [blame] | 374 | Context.reset(new DWARFContextInMemory(*Objects.second)); |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 375 | assert(Context); |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 376 | auto InfoOrErr = |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 377 | SymbolizableObjectFile::create(Objects.first, std::move(Context)); |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 378 | auto InsertResult = |
| 379 | Modules.insert(std::make_pair(ModuleName, std::move(InfoOrErr))); |
| 380 | assert(InsertResult.second); |
| 381 | if (auto EC = InsertResult.first->second.getError()) |
| 382 | return EC; |
| 383 | return InsertResult.first->second->get(); |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Reid Kleckner | c25c794 | 2015-08-10 21:47:11 +0000 | [diff] [blame] | 386 | // Undo these various manglings for Win32 extern "C" functions: |
| 387 | // cdecl - _foo |
| 388 | // stdcall - _foo@12 |
| 389 | // fastcall - @foo@12 |
| 390 | // vectorcall - foo@@12 |
| 391 | // These are all different linkage names for 'foo'. |
| 392 | static StringRef demanglePE32ExternCFunc(StringRef SymbolName) { |
| 393 | // Remove any '_' or '@' prefix. |
| 394 | char Front = SymbolName.empty() ? '\0' : SymbolName[0]; |
| 395 | if (Front == '_' || Front == '@') |
| 396 | SymbolName = SymbolName.drop_front(); |
| 397 | |
| 398 | // Remove any '@[0-9]+' suffix. |
| 399 | if (Front != '?') { |
| 400 | size_t AtPos = SymbolName.rfind('@'); |
| 401 | if (AtPos != StringRef::npos && |
| 402 | std::all_of(SymbolName.begin() + AtPos + 1, SymbolName.end(), |
| 403 | [](char C) { return C >= '0' && C <= '9'; })) { |
| 404 | SymbolName = SymbolName.substr(0, AtPos); |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | // Remove any ending '@' for vectorcall. |
| 409 | if (SymbolName.endswith("@")) |
| 410 | SymbolName = SymbolName.drop_back(); |
| 411 | |
| 412 | return SymbolName; |
| 413 | } |
| 414 | |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 415 | #if !defined(_MSC_VER) |
| 416 | // Assume that __cxa_demangle is provided by libcxxabi (except for Windows). |
| 417 | extern "C" char *__cxa_demangle(const char *mangled_name, char *output_buffer, |
| 418 | size_t *length, int *status); |
| 419 | #endif |
| 420 | |
Reid Kleckner | c25c794 | 2015-08-10 21:47:11 +0000 | [diff] [blame] | 421 | std::string LLVMSymbolizer::DemangleName(const std::string &Name, |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 422 | const SymbolizableModule *ModInfo) { |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 423 | #if !defined(_MSC_VER) |
Ed Maste | ef6fed7 | 2014-01-16 17:25:12 +0000 | [diff] [blame] | 424 | // We can spoil names of symbols with C linkage, so use an heuristic |
| 425 | // approach to check if the name should be demangled. |
Reid Kleckner | c25c794 | 2015-08-10 21:47:11 +0000 | [diff] [blame] | 426 | if (Name.substr(0, 2) == "_Z") { |
| 427 | int status = 0; |
| 428 | char *DemangledName = __cxa_demangle(Name.c_str(), nullptr, nullptr, &status); |
| 429 | if (status != 0) |
| 430 | return Name; |
| 431 | std::string Result = DemangledName; |
| 432 | free(DemangledName); |
| 433 | return Result; |
| 434 | } |
Alexey Samsonov | 601beb7 | 2013-06-28 12:06:25 +0000 | [diff] [blame] | 435 | #else |
Reid Kleckner | c25c794 | 2015-08-10 21:47:11 +0000 | [diff] [blame] | 436 | if (!Name.empty() && Name.front() == '?') { |
| 437 | // Only do MSVC C++ demangling on symbols starting with '?'. |
| 438 | char DemangledName[1024] = {0}; |
| 439 | DWORD result = ::UnDecorateSymbolName( |
| 440 | Name.c_str(), DemangledName, 1023, |
| 441 | UNDNAME_NO_ACCESS_SPECIFIERS | // Strip public, private, protected |
| 442 | UNDNAME_NO_ALLOCATION_LANGUAGE | // Strip __thiscall, __stdcall, etc |
| 443 | UNDNAME_NO_THROW_SIGNATURES | // Strip throw() specifications |
| 444 | UNDNAME_NO_MEMBER_TYPE | // Strip virtual, static, etc specifiers |
| 445 | UNDNAME_NO_MS_KEYWORDS | // Strip all MS extension keywords |
| 446 | UNDNAME_NO_FUNCTION_RETURNS); // Strip function return types |
| 447 | return (result == 0) ? Name : std::string(DemangledName); |
| 448 | } |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 449 | #endif |
Alexey Samsonov | 76f7ecb | 2015-10-29 23:49:19 +0000 | [diff] [blame] | 450 | if (ModInfo && ModInfo->isWin32Module()) |
Reid Kleckner | c25c794 | 2015-08-10 21:47:11 +0000 | [diff] [blame] | 451 | return std::string(demanglePE32ExternCFunc(Name)); |
| 452 | return Name; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 455 | } // namespace symbolize |
| 456 | } // namespace llvm |