Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 1 | //===-- LLVMSymbolize.cpp -------------------------------------------------===// |
| 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 | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Implementation for LLVM symbolization library. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Alexey Samsonov | 57f8837 | 2015-10-26 17:56:12 +0000 | [diff] [blame] | 13 | #include "llvm/DebugInfo/Symbolize/Symbolize.h" |
| 14 | |
Alexey Samsonov | 8df3a07 | 2015-10-29 22:21:37 +0000 | [diff] [blame] | 15 | #include "SymbolizableObjectFile.h" |
| 16 | |
Alexey Samsonov | dd71c5b | 2013-03-19 15:33:18 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 18 | #include "llvm/BinaryFormat/COFF.h" |
Zachary Turner | 6489d7b | 2015-04-23 17:37:47 +0000 | [diff] [blame] | 19 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
Zachary Turner | 20dbd0d | 2015-04-27 17:19:51 +0000 | [diff] [blame] | 20 | #include "llvm/DebugInfo/PDB/PDB.h" |
| 21 | #include "llvm/DebugInfo/PDB/PDBContext.h" |
Eugene Zemtsov | cd72cbc | 2018-03-07 23:07:34 +0000 | [diff] [blame] | 22 | #include "llvm/Demangle/Demangle.h" |
Reid Kleckner | dafc5d7 | 2016-07-06 16:56:42 +0000 | [diff] [blame] | 23 | #include "llvm/Object/COFF.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" |
Eugene Leviant | 18873b2 | 2019-04-08 12:31:12 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CRC.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> |
Eugene Zelenko | 35623fb | 2016-03-28 17:40:08 +0000 | [diff] [blame] | 36 | #include <cstring> |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 37 | |
Martin Storsjo | b8f7902 | 2019-10-04 07:22:37 +0000 | [diff] [blame^] | 38 | #if defined(_MSC_VER) |
| 39 | #include <Windows.h> |
| 40 | |
| 41 | // This must be included after windows.h. |
| 42 | #include <DbgHelp.h> |
| 43 | #pragma comment(lib, "dbghelp.lib") |
| 44 | |
| 45 | // Windows.h conflicts with our COFF header definitions. |
| 46 | #ifdef IMAGE_FILE_MACHINE_I386 |
| 47 | #undef IMAGE_FILE_MACHINE_I386 |
| 48 | #endif |
| 49 | #endif |
| 50 | |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 51 | namespace llvm { |
| 52 | namespace symbolize { |
| 53 | |
David Blaikie | e5adb68 | 2017-07-30 01:34:08 +0000 | [diff] [blame] | 54 | Expected<DILineInfo> |
Yuanfang Chen | 5de4692 | 2019-07-08 19:28:57 +0000 | [diff] [blame] | 55 | LLVMSymbolizer::symbolizeCodeCommon(SymbolizableModule *Info, |
| 56 | object::SectionedAddress ModuleOffset) { |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 57 | // A null module means an error has already been reported. Return an empty |
| 58 | // result. |
| 59 | if (!Info) |
| 60 | return DILineInfo(); |
Reid Kleckner | e94fef7 | 2015-10-09 00:15:01 +0000 | [diff] [blame] | 61 | |
| 62 | // If the user is giving us relative addresses, add the preferred base of the |
| 63 | // object to the offset before we do the query. It's what DIContext expects. |
| 64 | if (Opts.RelativeAddresses) |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame] | 65 | ModuleOffset.Address += Info->getModulePreferredBase(); |
Reid Kleckner | e94fef7 | 2015-10-09 00:15:01 +0000 | [diff] [blame] | 66 | |
Alexey Samsonov | 0fb6451 | 2015-10-26 22:34:56 +0000 | [diff] [blame] | 67 | DILineInfo LineInfo = Info->symbolizeCode(ModuleOffset, Opts.PrintFunctions, |
| 68 | Opts.UseSymbolTable); |
Alexey Samsonov | 6881249 | 2015-11-03 21:36:13 +0000 | [diff] [blame] | 69 | if (Opts.Demangle) |
| 70 | LineInfo.FunctionName = DemangleName(LineInfo.FunctionName, Info); |
Alexey Samsonov | d6aa820 | 2015-11-03 22:20:52 +0000 | [diff] [blame] | 71 | return LineInfo; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Yuanfang Chen | 5de4692 | 2019-07-08 19:28:57 +0000 | [diff] [blame] | 74 | Expected<DILineInfo> |
| 75 | LLVMSymbolizer::symbolizeCode(const ObjectFile &Obj, |
| 76 | object::SectionedAddress ModuleOffset) { |
| 77 | StringRef ModuleName = Obj.getFileName(); |
| 78 | auto I = Modules.find(ModuleName); |
| 79 | if (I != Modules.end()) |
| 80 | return symbolizeCodeCommon(I->second.get(), ModuleOffset); |
| 81 | |
| 82 | std::unique_ptr<DIContext> Context = |
| 83 | DWARFContext::create(Obj, nullptr, DWARFContext::defaultErrorHandler); |
| 84 | Expected<SymbolizableModule *> InfoOrErr = |
| 85 | createModuleInfo(&Obj, std::move(Context), ModuleName); |
| 86 | if (!InfoOrErr) |
| 87 | return InfoOrErr.takeError(); |
| 88 | return symbolizeCodeCommon(*InfoOrErr, ModuleOffset); |
| 89 | } |
| 90 | |
| 91 | Expected<DILineInfo> |
| 92 | LLVMSymbolizer::symbolizeCode(const std::string &ModuleName, |
| 93 | object::SectionedAddress ModuleOffset) { |
| 94 | Expected<SymbolizableModule *> InfoOrErr = getOrCreateModuleInfo(ModuleName); |
| 95 | if (!InfoOrErr) |
| 96 | return InfoOrErr.takeError(); |
| 97 | return symbolizeCodeCommon(*InfoOrErr, ModuleOffset); |
| 98 | } |
| 99 | |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 100 | Expected<DIInliningInfo> |
Alexey Samsonov | d6aa820 | 2015-11-03 22:20:52 +0000 | [diff] [blame] | 101 | LLVMSymbolizer::symbolizeInlinedCode(const std::string &ModuleName, |
Peter Collingbourne | e5bdeda | 2019-06-11 02:32:27 +0000 | [diff] [blame] | 102 | object::SectionedAddress ModuleOffset) { |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 103 | SymbolizableModule *Info; |
Peter Collingbourne | e5bdeda | 2019-06-11 02:32:27 +0000 | [diff] [blame] | 104 | if (auto InfoOrErr = getOrCreateModuleInfo(ModuleName)) |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 105 | Info = InfoOrErr.get(); |
| 106 | else |
| 107 | return InfoOrErr.takeError(); |
| 108 | |
| 109 | // A null module means an error has already been reported. Return an empty |
| 110 | // result. |
| 111 | if (!Info) |
| 112 | return DIInliningInfo(); |
Alexey Samsonov | 46c1ce6 | 2015-10-30 00:40:20 +0000 | [diff] [blame] | 113 | |
| 114 | // If the user is giving us relative addresses, add the preferred base of the |
| 115 | // object to the offset before we do the query. It's what DIContext expects. |
| 116 | if (Opts.RelativeAddresses) |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame] | 117 | ModuleOffset.Address += Info->getModulePreferredBase(); |
Alexey Samsonov | 46c1ce6 | 2015-10-30 00:40:20 +0000 | [diff] [blame] | 118 | |
| 119 | DIInliningInfo InlinedContext = Info->symbolizeInlinedCode( |
| 120 | ModuleOffset, Opts.PrintFunctions, Opts.UseSymbolTable); |
Alexey Samsonov | 6881249 | 2015-11-03 21:36:13 +0000 | [diff] [blame] | 121 | if (Opts.Demangle) { |
| 122 | for (int i = 0, n = InlinedContext.getNumberOfFrames(); i < n; i++) { |
| 123 | auto *Frame = InlinedContext.getMutableFrame(i); |
| 124 | Frame->FunctionName = DemangleName(Frame->FunctionName, Info); |
| 125 | } |
| 126 | } |
Alexey Samsonov | d6aa820 | 2015-11-03 22:20:52 +0000 | [diff] [blame] | 127 | return InlinedContext; |
Alexey Samsonov | 46c1ce6 | 2015-10-30 00:40:20 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame] | 130 | Expected<DIGlobal> |
| 131 | LLVMSymbolizer::symbolizeData(const std::string &ModuleName, |
| 132 | object::SectionedAddress ModuleOffset) { |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 133 | SymbolizableModule *Info; |
| 134 | if (auto InfoOrErr = getOrCreateModuleInfo(ModuleName)) |
| 135 | Info = InfoOrErr.get(); |
| 136 | else |
| 137 | return InfoOrErr.takeError(); |
| 138 | |
| 139 | // A null module means an error has already been reported. Return an empty |
| 140 | // result. |
| 141 | if (!Info) |
| 142 | return DIGlobal(); |
Alexey Samsonov | 6881249 | 2015-11-03 21:36:13 +0000 | [diff] [blame] | 143 | |
| 144 | // If the user is giving us relative addresses, add the preferred base of |
| 145 | // the object to the offset before we do the query. It's what DIContext |
| 146 | // expects. |
| 147 | if (Opts.RelativeAddresses) |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame] | 148 | ModuleOffset.Address += Info->getModulePreferredBase(); |
Alexey Samsonov | 6881249 | 2015-11-03 21:36:13 +0000 | [diff] [blame] | 149 | |
| 150 | DIGlobal Global = Info->symbolizeData(ModuleOffset); |
| 151 | if (Opts.Demangle) |
| 152 | Global.Name = DemangleName(Global.Name, Info); |
Alexey Samsonov | d6aa820 | 2015-11-03 22:20:52 +0000 | [diff] [blame] | 153 | return Global; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Peter Collingbourne | 9c8282a | 2019-06-24 20:03:23 +0000 | [diff] [blame] | 156 | Expected<std::vector<DILocal>> |
| 157 | LLVMSymbolizer::symbolizeFrame(const std::string &ModuleName, |
| 158 | object::SectionedAddress ModuleOffset) { |
| 159 | SymbolizableModule *Info; |
| 160 | if (auto InfoOrErr = getOrCreateModuleInfo(ModuleName)) |
| 161 | Info = InfoOrErr.get(); |
| 162 | else |
| 163 | return InfoOrErr.takeError(); |
| 164 | |
| 165 | // A null module means an error has already been reported. Return an empty |
| 166 | // result. |
| 167 | if (!Info) |
| 168 | return std::vector<DILocal>(); |
| 169 | |
| 170 | // If the user is giving us relative addresses, add the preferred base of |
| 171 | // the object to the offset before we do the query. It's what DIContext |
| 172 | // expects. |
| 173 | if (Opts.RelativeAddresses) |
| 174 | ModuleOffset.Address += Info->getModulePreferredBase(); |
| 175 | |
| 176 | return Info->symbolizeFrame(ModuleOffset); |
| 177 | } |
| 178 | |
Dmitry Vyukov | e8504e2 | 2013-03-19 10:24:42 +0000 | [diff] [blame] | 179 | void LLVMSymbolizer::flush() { |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame] | 180 | ObjectForUBPathAndArch.clear(); |
| 181 | BinaryForPath.clear(); |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 182 | ObjectPairForPathArch.clear(); |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame] | 183 | Modules.clear(); |
Dmitry Vyukov | e8504e2 | 2013-03-19 10:24:42 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Eugene Zelenko | 35623fb | 2016-03-28 17:40:08 +0000 | [diff] [blame] | 186 | namespace { |
| 187 | |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 188 | // For Path="/path/to/foo" and Basename="foo" assume that debug info is in |
| 189 | // /path/to/foo.dSYM/Contents/Resources/DWARF/foo. |
| 190 | // For Path="/path/to/bar.dSYM" and Basename="foo" assume that debug info is in |
| 191 | // /path/to/bar.dSYM/Contents/Resources/DWARF/foo. |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 192 | std::string getDarwinDWARFResourceForPath( |
| 193 | const std::string &Path, const std::string &Basename) { |
| 194 | SmallString<16> ResourceName = StringRef(Path); |
| 195 | if (sys::path::extension(Path) != ".dSYM") { |
| 196 | ResourceName += ".dSYM"; |
| 197 | } |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 198 | sys::path::append(ResourceName, "Contents", "Resources", "DWARF"); |
| 199 | sys::path::append(ResourceName, Basename); |
| 200 | return ResourceName.str(); |
| 201 | } |
| 202 | |
Eugene Zelenko | 35623fb | 2016-03-28 17:40:08 +0000 | [diff] [blame] | 203 | bool checkFileCRC(StringRef Path, uint32_t CRCHash) { |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 204 | ErrorOr<std::unique_ptr<MemoryBuffer>> MB = |
| 205 | MemoryBuffer::getFileOrSTDIN(Path); |
| 206 | if (!MB) |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 207 | return false; |
Eugene Leviant | 7671a1d | 2019-04-08 13:40:58 +0000 | [diff] [blame] | 208 | return CRCHash == llvm::crc32(0, MB.get()->getBuffer()); |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Eugene Zelenko | 35623fb | 2016-03-28 17:40:08 +0000 | [diff] [blame] | 211 | bool findDebugBinary(const std::string &OrigPath, |
| 212 | const std::string &DebuglinkName, uint32_t CRCHash, |
Jordan Rupprecht | 5b7ad42 | 2019-02-11 18:05:48 +0000 | [diff] [blame] | 213 | const std::string &FallbackDebugPath, |
Eugene Zelenko | 35623fb | 2016-03-28 17:40:08 +0000 | [diff] [blame] | 214 | std::string &Result) { |
Jordan Rupprecht | 835df27 | 2019-02-01 21:04:16 +0000 | [diff] [blame] | 215 | SmallString<16> OrigDir(OrigPath); |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 216 | llvm::sys::path::remove_filename(OrigDir); |
| 217 | SmallString<16> DebugPath = OrigDir; |
Jordan Rupprecht | 5b7ad42 | 2019-02-11 18:05:48 +0000 | [diff] [blame] | 218 | // Try relative/path/to/original_binary/debuglink_name |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 219 | llvm::sys::path::append(DebugPath, DebuglinkName); |
| 220 | if (checkFileCRC(DebugPath, CRCHash)) { |
| 221 | Result = DebugPath.str(); |
| 222 | return true; |
| 223 | } |
Jordan Rupprecht | 5b7ad42 | 2019-02-11 18:05:48 +0000 | [diff] [blame] | 224 | // Try relative/path/to/original_binary/.debug/debuglink_name |
Francis Ricci | fe6cbce | 2018-03-02 22:56:45 +0000 | [diff] [blame] | 225 | DebugPath = OrigDir; |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 226 | llvm::sys::path::append(DebugPath, ".debug", DebuglinkName); |
| 227 | if (checkFileCRC(DebugPath, CRCHash)) { |
| 228 | Result = DebugPath.str(); |
| 229 | return true; |
| 230 | } |
Jordan Rupprecht | 5b7ad42 | 2019-02-11 18:05:48 +0000 | [diff] [blame] | 231 | // Make the path absolute so that lookups will go to |
| 232 | // "/usr/lib/debug/full/path/to/debug", not |
| 233 | // "/usr/lib/debug/to/debug" |
| 234 | llvm::sys::fs::make_absolute(OrigDir); |
| 235 | if (!FallbackDebugPath.empty()) { |
| 236 | // Try <FallbackDebugPath>/absolute/path/to/original_binary/debuglink_name |
| 237 | DebugPath = FallbackDebugPath; |
| 238 | } else { |
Kamil Rytarowski | a8448ad | 2018-06-25 18:49:13 +0000 | [diff] [blame] | 239 | #if defined(__NetBSD__) |
Jordan Rupprecht | 5b7ad42 | 2019-02-11 18:05:48 +0000 | [diff] [blame] | 240 | // Try /usr/libdata/debug/absolute/path/to/original_binary/debuglink_name |
| 241 | DebugPath = "/usr/libdata/debug"; |
Kamil Rytarowski | a8448ad | 2018-06-25 18:49:13 +0000 | [diff] [blame] | 242 | #else |
Jordan Rupprecht | 5b7ad42 | 2019-02-11 18:05:48 +0000 | [diff] [blame] | 243 | // Try /usr/lib/debug/absolute/path/to/original_binary/debuglink_name |
| 244 | DebugPath = "/usr/lib/debug"; |
Kamil Rytarowski | a8448ad | 2018-06-25 18:49:13 +0000 | [diff] [blame] | 245 | #endif |
Jordan Rupprecht | 5b7ad42 | 2019-02-11 18:05:48 +0000 | [diff] [blame] | 246 | } |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 247 | llvm::sys::path::append(DebugPath, llvm::sys::path::relative_path(OrigDir), |
| 248 | DebuglinkName); |
| 249 | if (checkFileCRC(DebugPath, CRCHash)) { |
| 250 | Result = DebugPath.str(); |
| 251 | return true; |
| 252 | } |
| 253 | return false; |
| 254 | } |
| 255 | |
Eugene Zelenko | 35623fb | 2016-03-28 17:40:08 +0000 | [diff] [blame] | 256 | bool getGNUDebuglinkContents(const ObjectFile *Obj, std::string &DebugName, |
| 257 | uint32_t &CRCHash) { |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 258 | if (!Obj) |
| 259 | return false; |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 260 | for (const SectionRef &Section : Obj->sections()) { |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 261 | StringRef Name; |
George Rimar | bcc00e1 | 2019-08-14 11:10:11 +0000 | [diff] [blame] | 262 | if (Expected<StringRef> NameOrErr = Section.getName()) |
| 263 | Name = *NameOrErr; |
| 264 | else |
| 265 | consumeError(NameOrErr.takeError()); |
| 266 | |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 267 | Name = Name.substr(Name.find_first_not_of("._")); |
| 268 | if (Name == "gnu_debuglink") { |
Fangrui Song | e183340 | 2019-05-16 13:24:04 +0000 | [diff] [blame] | 269 | Expected<StringRef> ContentsOrErr = Section.getContents(); |
| 270 | if (!ContentsOrErr) { |
| 271 | consumeError(ContentsOrErr.takeError()); |
| 272 | return false; |
| 273 | } |
| 274 | DataExtractor DE(*ContentsOrErr, Obj->isLittleEndian(), 0); |
Igor Kudrin | f26a70a | 2019-08-06 10:49:40 +0000 | [diff] [blame] | 275 | uint64_t Offset = 0; |
Alexey Samsonov | 3e9997f | 2013-08-14 17:09:30 +0000 | [diff] [blame] | 276 | if (const char *DebugNameStr = DE.getCStr(&Offset)) { |
| 277 | // 4-byte align the offset. |
| 278 | Offset = (Offset + 3) & ~0x3; |
| 279 | if (DE.isValidOffsetForDataOfSize(Offset, 4)) { |
| 280 | DebugName = DebugNameStr; |
| 281 | CRCHash = DE.getU32(&Offset); |
| 282 | return true; |
| 283 | } |
| 284 | } |
| 285 | break; |
| 286 | } |
| 287 | } |
| 288 | return false; |
| 289 | } |
| 290 | |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 291 | bool darwinDsymMatchesBinary(const MachOObjectFile *DbgObj, |
| 292 | const MachOObjectFile *Obj) { |
| 293 | ArrayRef<uint8_t> dbg_uuid = DbgObj->getUuid(); |
| 294 | ArrayRef<uint8_t> bin_uuid = Obj->getUuid(); |
| 295 | if (dbg_uuid.empty() || bin_uuid.empty()) |
| 296 | return false; |
| 297 | return !memcmp(dbg_uuid.data(), bin_uuid.data(), dbg_uuid.size()); |
| 298 | } |
| 299 | |
Eugene Zelenko | 35623fb | 2016-03-28 17:40:08 +0000 | [diff] [blame] | 300 | } // end anonymous namespace |
| 301 | |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 302 | ObjectFile *LLVMSymbolizer::lookUpDsymFile(const std::string &ExePath, |
| 303 | const MachOObjectFile *MachExeObj, const std::string &ArchName) { |
| 304 | // On Darwin we may find DWARF in separate object file in |
| 305 | // resource directory. |
| 306 | std::vector<std::string> DsymPaths; |
| 307 | StringRef Filename = sys::path::filename(ExePath); |
| 308 | DsymPaths.push_back(getDarwinDWARFResourceForPath(ExePath, Filename)); |
| 309 | for (const auto &Path : Opts.DsymHints) { |
| 310 | DsymPaths.push_back(getDarwinDWARFResourceForPath(Path, Filename)); |
| 311 | } |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame] | 312 | for (const auto &Path : DsymPaths) { |
| 313 | auto DbgObjOrErr = getOrCreateObject(Path, ArchName); |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 314 | if (!DbgObjOrErr) { |
| 315 | // Ignore errors, the file might not exist. |
| 316 | consumeError(DbgObjOrErr.takeError()); |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 317 | continue; |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 318 | } |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 319 | ObjectFile *DbgObj = DbgObjOrErr.get(); |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 320 | if (!DbgObj) |
| 321 | continue; |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame] | 322 | const MachOObjectFile *MachDbgObj = dyn_cast<const MachOObjectFile>(DbgObj); |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 323 | if (!MachDbgObj) |
| 324 | continue; |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame] | 325 | if (darwinDsymMatchesBinary(MachDbgObj, MachExeObj)) |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 326 | return DbgObj; |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 327 | } |
| 328 | return nullptr; |
| 329 | } |
| 330 | |
Alexey Samsonov | 5365a01 | 2015-11-04 00:30:26 +0000 | [diff] [blame] | 331 | ObjectFile *LLVMSymbolizer::lookUpDebuglinkObject(const std::string &Path, |
| 332 | const ObjectFile *Obj, |
| 333 | const std::string &ArchName) { |
| 334 | std::string DebuglinkName; |
| 335 | uint32_t CRCHash; |
| 336 | std::string DebugBinaryPath; |
| 337 | if (!getGNUDebuglinkContents(Obj, DebuglinkName, CRCHash)) |
| 338 | return nullptr; |
Jordan Rupprecht | 5b7ad42 | 2019-02-11 18:05:48 +0000 | [diff] [blame] | 339 | if (!findDebugBinary(Path, DebuglinkName, CRCHash, Opts.FallbackDebugPath, |
| 340 | DebugBinaryPath)) |
Alexey Samsonov | 5365a01 | 2015-11-04 00:30:26 +0000 | [diff] [blame] | 341 | return nullptr; |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame] | 342 | auto DbgObjOrErr = getOrCreateObject(DebugBinaryPath, ArchName); |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 343 | if (!DbgObjOrErr) { |
| 344 | // Ignore errors, the file might not exist. |
| 345 | consumeError(DbgObjOrErr.takeError()); |
Alexey Samsonov | 5365a01 | 2015-11-04 00:30:26 +0000 | [diff] [blame] | 346 | return nullptr; |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 347 | } |
Alexey Samsonov | 5365a01 | 2015-11-04 00:30:26 +0000 | [diff] [blame] | 348 | return DbgObjOrErr.get(); |
| 349 | } |
| 350 | |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 351 | Expected<LLVMSymbolizer::ObjectPair> |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame] | 352 | LLVMSymbolizer::getOrCreateObjectPair(const std::string &Path, |
| 353 | const std::string &ArchName) { |
Fangrui Song | 22e478f | 2019-06-21 11:05:26 +0000 | [diff] [blame] | 354 | auto I = ObjectPairForPathArch.find(std::make_pair(Path, ArchName)); |
| 355 | if (I != ObjectPairForPathArch.end()) |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 356 | return I->second; |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 357 | |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame] | 358 | auto ObjOrErr = getOrCreateObject(Path, ArchName); |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 359 | if (!ObjOrErr) { |
Fangrui Song | 22e478f | 2019-06-21 11:05:26 +0000 | [diff] [blame] | 360 | ObjectPairForPathArch.emplace(std::make_pair(Path, ArchName), |
| 361 | ObjectPair(nullptr, nullptr)); |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 362 | return ObjOrErr.takeError(); |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 363 | } |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 364 | |
| 365 | ObjectFile *Obj = ObjOrErr.get(); |
| 366 | assert(Obj != nullptr); |
| 367 | ObjectFile *DbgObj = nullptr; |
| 368 | |
| 369 | if (auto MachObj = dyn_cast<const MachOObjectFile>(Obj)) |
| 370 | DbgObj = lookUpDsymFile(Path, MachObj, ArchName); |
Alexey Samsonov | 5365a01 | 2015-11-04 00:30:26 +0000 | [diff] [blame] | 371 | if (!DbgObj) |
| 372 | DbgObj = lookUpDebuglinkObject(Path, Obj, ArchName); |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 373 | if (!DbgObj) |
| 374 | DbgObj = Obj; |
| 375 | ObjectPair Res = std::make_pair(Obj, DbgObj); |
Fangrui Song | 22e478f | 2019-06-21 11:05:26 +0000 | [diff] [blame] | 376 | ObjectPairForPathArch.emplace(std::make_pair(Path, ArchName), Res); |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 377 | return Res; |
| 378 | } |
| 379 | |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 380 | Expected<ObjectFile *> |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame] | 381 | LLVMSymbolizer::getOrCreateObject(const std::string &Path, |
| 382 | const std::string &ArchName) { |
Fangrui Song | 22e478f | 2019-06-21 11:05:26 +0000 | [diff] [blame] | 383 | Binary *Bin; |
| 384 | auto Pair = BinaryForPath.emplace(Path, OwningBinary<Binary>()); |
| 385 | if (!Pair.second) { |
| 386 | Bin = Pair.first->second.getBinary(); |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame] | 387 | } else { |
Fangrui Song | 22e478f | 2019-06-21 11:05:26 +0000 | [diff] [blame] | 388 | Expected<OwningBinary<Binary>> BinOrErr = createBinary(Path); |
| 389 | if (!BinOrErr) |
| 390 | return BinOrErr.takeError(); |
| 391 | Pair.first->second = std::move(BinOrErr.get()); |
| 392 | Bin = Pair.first->second.getBinary(); |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 395 | if (!Bin) |
| 396 | return static_cast<ObjectFile *>(nullptr); |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame] | 397 | |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 398 | if (MachOUniversalBinary *UB = dyn_cast_or_null<MachOUniversalBinary>(Bin)) { |
Fangrui Song | 22e478f | 2019-06-21 11:05:26 +0000 | [diff] [blame] | 399 | auto I = ObjectForUBPathAndArch.find(std::make_pair(Path, ArchName)); |
| 400 | if (I != ObjectForUBPathAndArch.end()) |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 401 | return I->second.get(); |
Fangrui Song | 22e478f | 2019-06-21 11:05:26 +0000 | [diff] [blame] | 402 | |
Kevin Enderby | 9acb109 | 2016-05-31 20:35:34 +0000 | [diff] [blame] | 403 | Expected<std::unique_ptr<ObjectFile>> ObjOrErr = |
Alexander Shaposhnikov | 4fd11c1 | 2019-09-19 00:02:12 +0000 | [diff] [blame] | 404 | UB->getMachOObjectForArch(ArchName); |
Kevin Enderby | 9acb109 | 2016-05-31 20:35:34 +0000 | [diff] [blame] | 405 | if (!ObjOrErr) { |
Fangrui Song | 22e478f | 2019-06-21 11:05:26 +0000 | [diff] [blame] | 406 | ObjectForUBPathAndArch.emplace(std::make_pair(Path, ArchName), |
| 407 | std::unique_ptr<ObjectFile>()); |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 408 | return ObjOrErr.takeError(); |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame] | 409 | } |
| 410 | ObjectFile *Res = ObjOrErr->get(); |
Fangrui Song | 22e478f | 2019-06-21 11:05:26 +0000 | [diff] [blame] | 411 | ObjectForUBPathAndArch.emplace(std::make_pair(Path, ArchName), |
| 412 | std::move(ObjOrErr.get())); |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 413 | return Res; |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 414 | } |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 415 | if (Bin->isObject()) { |
| 416 | return cast<ObjectFile>(Bin); |
| 417 | } |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 418 | return errorCodeToError(object_error::arch_not_found); |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 421 | Expected<SymbolizableModule *> |
Yuanfang Chen | 5de4692 | 2019-07-08 19:28:57 +0000 | [diff] [blame] | 422 | LLVMSymbolizer::createModuleInfo(const ObjectFile *Obj, |
| 423 | std::unique_ptr<DIContext> Context, |
| 424 | StringRef ModuleName) { |
Peter Collingbourne | a56d81f | 2019-08-05 20:59:25 +0000 | [diff] [blame] | 425 | auto InfoOrErr = SymbolizableObjectFile::create(Obj, std::move(Context), |
| 426 | Opts.UntagAddresses); |
Yuanfang Chen | 5de4692 | 2019-07-08 19:28:57 +0000 | [diff] [blame] | 427 | std::unique_ptr<SymbolizableModule> SymMod; |
| 428 | if (InfoOrErr) |
| 429 | SymMod = std::move(*InfoOrErr); |
| 430 | auto InsertResult = |
| 431 | Modules.insert(std::make_pair(ModuleName, std::move(SymMod))); |
| 432 | assert(InsertResult.second); |
| 433 | if (std::error_code EC = InfoOrErr.getError()) |
| 434 | return errorCodeToError(EC); |
| 435 | return InsertResult.first->second.get(); |
| 436 | } |
| 437 | |
| 438 | Expected<SymbolizableModule *> |
Peter Collingbourne | e5bdeda | 2019-06-11 02:32:27 +0000 | [diff] [blame] | 439 | LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) { |
Fangrui Song | 22e478f | 2019-06-21 11:05:26 +0000 | [diff] [blame] | 440 | auto I = Modules.find(ModuleName); |
| 441 | if (I != Modules.end()) |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 442 | return I->second.get(); |
Fangrui Song | 22e478f | 2019-06-21 11:05:26 +0000 | [diff] [blame] | 443 | |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 444 | std::string BinaryName = ModuleName; |
| 445 | std::string ArchName = Opts.DefaultArch; |
Alexey Samsonov | b119b46 | 2013-07-17 06:45:36 +0000 | [diff] [blame] | 446 | size_t ColonPos = ModuleName.find_last_of(':'); |
| 447 | // Verify that substring after colon form a valid arch name. |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 448 | if (ColonPos != std::string::npos) { |
Alexey Samsonov | b119b46 | 2013-07-17 06:45:36 +0000 | [diff] [blame] | 449 | std::string ArchStr = ModuleName.substr(ColonPos + 1); |
NAKAMURA Takumi | 8ee89c6 | 2013-07-17 06:53:51 +0000 | [diff] [blame] | 450 | if (Triple(ArchStr).getArch() != Triple::UnknownArch) { |
Alexey Samsonov | b119b46 | 2013-07-17 06:45:36 +0000 | [diff] [blame] | 451 | BinaryName = ModuleName.substr(0, ColonPos); |
| 452 | ArchName = ArchStr; |
| 453 | } |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 454 | } |
Alexey Samsonov | 1eaae4c | 2015-12-18 22:02:14 +0000 | [diff] [blame] | 455 | auto ObjectsOrErr = getOrCreateObjectPair(BinaryName, ArchName); |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 456 | if (!ObjectsOrErr) { |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 457 | // Failed to find valid object file. |
Fangrui Song | 22e478f | 2019-06-21 11:05:26 +0000 | [diff] [blame] | 458 | Modules.emplace(ModuleName, std::unique_ptr<SymbolizableModule>()); |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 459 | return ObjectsOrErr.takeError(); |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 460 | } |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 461 | ObjectPair Objects = ObjectsOrErr.get(); |
| 462 | |
Alexey Samsonov | 7a952e5 | 2015-10-26 19:41:23 +0000 | [diff] [blame] | 463 | std::unique_ptr<DIContext> Context; |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 464 | // If this is a COFF object containing PDB info, use a PDBContext to |
| 465 | // symbolize. Otherwise, use DWARF. |
Zachary Turner | 20dbd0d | 2015-04-27 17:19:51 +0000 | [diff] [blame] | 466 | if (auto CoffObject = dyn_cast<COFFObjectFile>(Objects.first)) { |
Saleem Abdulrasool | 0152802 | 2016-08-09 00:25:12 +0000 | [diff] [blame] | 467 | const codeview::DebugInfo *DebugInfo; |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 468 | StringRef PDBFileName; |
Saleem Abdulrasool | 0152802 | 2016-08-09 00:25:12 +0000 | [diff] [blame] | 469 | auto EC = CoffObject->getDebugPDBInfo(DebugInfo, PDBFileName); |
Reid Kleckner | d1882f2 | 2016-09-01 20:28:59 +0000 | [diff] [blame] | 470 | if (!EC && DebugInfo != nullptr && !PDBFileName.empty()) { |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 471 | using namespace pdb; |
| 472 | std::unique_ptr<IPDBSession> Session; |
| 473 | if (auto Err = loadDataForEXE(PDB_ReaderType::DIA, |
| 474 | Objects.first->getFileName(), Session)) { |
Fangrui Song | 22e478f | 2019-06-21 11:05:26 +0000 | [diff] [blame] | 475 | Modules.emplace(ModuleName, std::unique_ptr<SymbolizableModule>()); |
Alexandre Ganea | 6a7efef | 2018-08-31 17:41:58 +0000 | [diff] [blame] | 476 | // Return along the PDB filename to provide more context |
| 477 | return createFileError(PDBFileName, std::move(Err)); |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 478 | } |
Alexey Samsonov | 7a952e5 | 2015-10-26 19:41:23 +0000 | [diff] [blame] | 479 | Context.reset(new PDBContext(*CoffObject, std::move(Session))); |
Zachary Turner | c007aa4 | 2015-05-06 22:26:30 +0000 | [diff] [blame] | 480 | } |
Zachary Turner | 20dbd0d | 2015-04-27 17:19:51 +0000 | [diff] [blame] | 481 | } |
| 482 | if (!Context) |
Peter Collingbourne | e5bdeda | 2019-06-11 02:32:27 +0000 | [diff] [blame] | 483 | Context = |
| 484 | DWARFContext::create(*Objects.second, nullptr, |
| 485 | DWARFContext::defaultErrorHandler, Opts.DWPName); |
Yuanfang Chen | 5de4692 | 2019-07-08 19:28:57 +0000 | [diff] [blame] | 486 | return createModuleInfo(Objects.first, std::move(Context), ModuleName); |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Eugene Zelenko | 35623fb | 2016-03-28 17:40:08 +0000 | [diff] [blame] | 489 | namespace { |
| 490 | |
Reid Kleckner | c25c794 | 2015-08-10 21:47:11 +0000 | [diff] [blame] | 491 | // Undo these various manglings for Win32 extern "C" functions: |
| 492 | // cdecl - _foo |
| 493 | // stdcall - _foo@12 |
| 494 | // fastcall - @foo@12 |
| 495 | // vectorcall - foo@@12 |
| 496 | // These are all different linkage names for 'foo'. |
Eugene Zelenko | 35623fb | 2016-03-28 17:40:08 +0000 | [diff] [blame] | 497 | StringRef demanglePE32ExternCFunc(StringRef SymbolName) { |
Reid Kleckner | c25c794 | 2015-08-10 21:47:11 +0000 | [diff] [blame] | 498 | // Remove any '_' or '@' prefix. |
| 499 | char Front = SymbolName.empty() ? '\0' : SymbolName[0]; |
| 500 | if (Front == '_' || Front == '@') |
| 501 | SymbolName = SymbolName.drop_front(); |
| 502 | |
| 503 | // Remove any '@[0-9]+' suffix. |
| 504 | if (Front != '?') { |
| 505 | size_t AtPos = SymbolName.rfind('@'); |
| 506 | if (AtPos != StringRef::npos && |
| 507 | std::all_of(SymbolName.begin() + AtPos + 1, SymbolName.end(), |
| 508 | [](char C) { return C >= '0' && C <= '9'; })) { |
| 509 | SymbolName = SymbolName.substr(0, AtPos); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | // Remove any ending '@' for vectorcall. |
| 514 | if (SymbolName.endswith("@")) |
| 515 | SymbolName = SymbolName.drop_back(); |
| 516 | |
| 517 | return SymbolName; |
| 518 | } |
| 519 | |
Eugene Zelenko | 35623fb | 2016-03-28 17:40:08 +0000 | [diff] [blame] | 520 | } // end anonymous namespace |
| 521 | |
Zachary Turner | 67c5601 | 2017-04-27 16:11:19 +0000 | [diff] [blame] | 522 | std::string |
| 523 | LLVMSymbolizer::DemangleName(const std::string &Name, |
| 524 | const SymbolizableModule *DbiModuleDescriptor) { |
Ed Maste | ef6fed7 | 2014-01-16 17:25:12 +0000 | [diff] [blame] | 525 | // We can spoil names of symbols with C linkage, so use an heuristic |
| 526 | // approach to check if the name should be demangled. |
Martin Storsjo | b8f7902 | 2019-10-04 07:22:37 +0000 | [diff] [blame^] | 527 | if (Name.substr(0, 2) == "_Z") { |
| 528 | int status = 0; |
| 529 | char *DemangledName = itaniumDemangle(Name.c_str(), nullptr, nullptr, &status); |
| 530 | if (status != 0) |
| 531 | return Name; |
| 532 | std::string Result = DemangledName; |
| 533 | free(DemangledName); |
| 534 | return Result; |
| 535 | } |
Eugene Zemtsov | cd72cbc | 2018-03-07 23:07:34 +0000 | [diff] [blame] | 536 | |
Martin Storsjo | b8f7902 | 2019-10-04 07:22:37 +0000 | [diff] [blame^] | 537 | #if defined(_MSC_VER) |
| 538 | if (!Name.empty() && Name.front() == '?') { |
| 539 | // Only do MSVC C++ demangling on symbols starting with '?'. |
| 540 | char DemangledName[1024] = {0}; |
| 541 | DWORD result = ::UnDecorateSymbolName( |
| 542 | Name.c_str(), DemangledName, 1023, |
| 543 | UNDNAME_NO_ACCESS_SPECIFIERS | // Strip public, private, protected |
| 544 | UNDNAME_NO_ALLOCATION_LANGUAGE | // Strip __thiscall, __stdcall, etc |
| 545 | UNDNAME_NO_THROW_SIGNATURES | // Strip throw() specifications |
| 546 | UNDNAME_NO_MEMBER_TYPE | // Strip virtual, static, etc specifiers |
| 547 | UNDNAME_NO_MS_KEYWORDS | // Strip all MS extension keywords |
| 548 | UNDNAME_NO_FUNCTION_RETURNS); // Strip function return types |
| 549 | return (result == 0) ? Name : std::string(DemangledName); |
| 550 | } |
| 551 | #endif |
Zachary Turner | 67c5601 | 2017-04-27 16:11:19 +0000 | [diff] [blame] | 552 | if (DbiModuleDescriptor && DbiModuleDescriptor->isWin32Module()) |
Reid Kleckner | c25c794 | 2015-08-10 21:47:11 +0000 | [diff] [blame] | 553 | return std::string(demanglePE32ExternCFunc(Name)); |
| 554 | return Name; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 557 | } // namespace symbolize |
| 558 | } // namespace llvm |