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