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