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