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