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 "llvm/ADT/STLExtras.h" 16 #include "llvm/BinaryFormat/COFF.h" 17 #include "llvm/Config/config.h" 18 #include "llvm/DebugInfo/DWARF/DWARFContext.h" 19 #include "llvm/DebugInfo/PDB/PDB.h" 20 #include "llvm/DebugInfo/PDB/PDBContext.h" 21 #include "llvm/DebugInfo/Symbolize/DIFetcher.h" 22 #include "llvm/DebugInfo/Symbolize/SymbolizableObjectFile.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" 27 #include "llvm/Support/CRC.h" 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> 37 #include <cstring> 38 39 namespace llvm { 40 namespace symbolize { 41 42 template <typename T> 43 Expected<DILineInfo> 44 LLVMSymbolizer::symbolizeCodeCommon(const T &ModuleSpecifier, 45 object::SectionedAddress ModuleOffset) { 46 47 auto InfoOrErr = getOrCreateModuleInfo(ModuleSpecifier); 48 if (!InfoOrErr) 49 return InfoOrErr.takeError(); 50 51 SymbolizableModule *Info = *InfoOrErr; 52 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) 61 ModuleOffset.Address += Info->getModulePreferredBase(); 62 63 DILineInfo LineInfo = Info->symbolizeCode( 64 ModuleOffset, DILineInfoSpecifier(Opts.PathStyle, Opts.PrintFunctions), 65 Opts.UseSymbolTable); 66 if (Opts.Demangle) 67 LineInfo.FunctionName = DemangleName(LineInfo.FunctionName, Info); 68 return LineInfo; 69 } 70 71 Expected<DILineInfo> 72 LLVMSymbolizer::symbolizeCode(const ObjectFile &Obj, 73 object::SectionedAddress ModuleOffset) { 74 return symbolizeCodeCommon(Obj, ModuleOffset); 75 } 76 77 Expected<DILineInfo> 78 LLVMSymbolizer::symbolizeCode(const std::string &ModuleName, 79 object::SectionedAddress ModuleOffset) { 80 return symbolizeCodeCommon(ModuleName, ModuleOffset); 81 } 82 83 template <typename T> 84 Expected<DIInliningInfo> LLVMSymbolizer::symbolizeInlinedCodeCommon( 85 const T &ModuleSpecifier, object::SectionedAddress ModuleOffset) { 86 auto InfoOrErr = getOrCreateModuleInfo(ModuleSpecifier); 87 if (!InfoOrErr) 88 return InfoOrErr.takeError(); 89 90 SymbolizableModule *Info = *InfoOrErr; 91 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) 100 ModuleOffset.Address += Info->getModulePreferredBase(); 101 102 DIInliningInfo InlinedContext = Info->symbolizeInlinedCode( 103 ModuleOffset, DILineInfoSpecifier(Opts.PathStyle, Opts.PrintFunctions), 104 Opts.UseSymbolTable); 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 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> 127 Expected<DIGlobal> 128 LLVMSymbolizer::symbolizeDataCommon(const T &ModuleSpecifier, 129 object::SectionedAddress ModuleOffset) { 130 131 auto InfoOrErr = getOrCreateModuleInfo(ModuleSpecifier); 132 if (!InfoOrErr) 133 return InfoOrErr.takeError(); 134 135 SymbolizableModule *Info = *InfoOrErr; 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) 145 ModuleOffset.Address += Info->getModulePreferredBase(); 146 147 DIGlobal Global = Info->symbolizeData(ModuleOffset); 148 if (Opts.Demangle) 149 Global.Name = DemangleName(Global.Name, Info); 150 return Global; 151 } 152 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> 166 Expected<std::vector<DILocal>> 167 LLVMSymbolizer::symbolizeFrameCommon(const T &ModuleSpecifier, 168 object::SectionedAddress ModuleOffset) { 169 auto InfoOrErr = getOrCreateModuleInfo(ModuleSpecifier); 170 if (!InfoOrErr) 171 return InfoOrErr.takeError(); 172 173 SymbolizableModule *Info = *InfoOrErr; 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 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 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. 213 std::string getDarwinDWARFResourceForPath(const std::string &Path, 214 const std::string &Basename) { 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); 221 return std::string(ResourceName.str()); 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; 229 return CRCHash == llvm::crc32(arrayRefFromStringRef(MB.get()->getBuffer())); 230 } 231 232 bool getGNUDebuglinkContents(const ObjectFile *Obj, std::string &DebugName, 233 uint32_t &CRCHash) { 234 if (!Obj) 235 return false; 236 for (const SectionRef &Section : Obj->sections()) { 237 StringRef Name; 238 consumeError(Section.getName().moveInto(Name)); 239 240 Name = Name.substr(Name.find_first_not_of("._")); 241 if (Name == "gnu_debuglink") { 242 Expected<StringRef> ContentsOrErr = Section.getContents(); 243 if (!ContentsOrErr) { 244 consumeError(ContentsOrErr.takeError()); 245 return false; 246 } 247 DataExtractor DE(*ContentsOrErr, Obj->isLittleEndian(), 0); 248 uint64_t Offset = 0; 249 if (const char *DebugNameStr = DE.getCStr(&Offset)) { 250 // 4-byte align the offset. 251 Offset = (Offset + 3) & ~0x3; 252 if (DE.isValidOffsetForDataOfSize(Offset, 4)) { 253 DebugName = DebugNameStr; 254 CRCHash = DE.getU32(&Offset); 255 return true; 256 } 257 } 258 break; 259 } 260 } 261 return false; 262 } 263 264 bool darwinDsymMatchesBinary(const MachOObjectFile *DbgObj, 265 const MachOObjectFile *Obj) { 266 ArrayRef<uint8_t> dbg_uuid = DbgObj->getUuid(); 267 ArrayRef<uint8_t> bin_uuid = Obj->getUuid(); 268 if (dbg_uuid.empty() || bin_uuid.empty()) 269 return false; 270 return !memcmp(dbg_uuid.data(), bin_uuid.data(), dbg_uuid.size()); 271 } 272 273 template <typename ELFT> 274 Optional<ArrayRef<uint8_t>> getBuildID(const ELFFile<ELFT> &Obj) { 275 auto PhdrsOrErr = Obj.program_headers(); 276 if (!PhdrsOrErr) { 277 consumeError(PhdrsOrErr.takeError()); 278 return {}; 279 } 280 for (const auto &P : *PhdrsOrErr) { 281 if (P.p_type != ELF::PT_NOTE) 282 continue; 283 Error Err = Error::success(); 284 for (auto N : Obj.notes(P, Err)) 285 if (N.getType() == ELF::NT_GNU_BUILD_ID && 286 N.getName() == ELF::ELF_NOTE_GNU) 287 return N.getDesc(); 288 consumeError(std::move(Err)); 289 } 290 return {}; 291 } 292 293 Optional<ArrayRef<uint8_t>> getBuildID(const ELFObjectFileBase *Obj) { 294 Optional<ArrayRef<uint8_t>> BuildID; 295 if (auto *O = dyn_cast<ELFObjectFile<ELF32LE>>(Obj)) 296 BuildID = getBuildID(O->getELFFile()); 297 else if (auto *O = dyn_cast<ELFObjectFile<ELF32BE>>(Obj)) 298 BuildID = getBuildID(O->getELFFile()); 299 else if (auto *O = dyn_cast<ELFObjectFile<ELF64LE>>(Obj)) 300 BuildID = getBuildID(O->getELFFile()); 301 else if (auto *O = dyn_cast<ELFObjectFile<ELF64BE>>(Obj)) 302 BuildID = getBuildID(O->getELFFile()); 303 else 304 llvm_unreachable("unsupported file format"); 305 return BuildID; 306 } 307 308 } // end anonymous namespace 309 310 ObjectFile *LLVMSymbolizer::lookUpDsymFile(const std::string &ExePath, 311 const MachOObjectFile *MachExeObj, 312 const std::string &ArchName) { 313 // On Darwin we may find DWARF in separate object file in 314 // resource directory. 315 std::vector<std::string> DsymPaths; 316 StringRef Filename = sys::path::filename(ExePath); 317 DsymPaths.push_back( 318 getDarwinDWARFResourceForPath(ExePath, std::string(Filename))); 319 for (const auto &Path : Opts.DsymHints) { 320 DsymPaths.push_back( 321 getDarwinDWARFResourceForPath(Path, std::string(Filename))); 322 } 323 for (const auto &Path : DsymPaths) { 324 auto DbgObjOrErr = getOrCreateObject(Path, ArchName); 325 if (!DbgObjOrErr) { 326 // Ignore errors, the file might not exist. 327 consumeError(DbgObjOrErr.takeError()); 328 continue; 329 } 330 ObjectFile *DbgObj = DbgObjOrErr.get(); 331 if (!DbgObj) 332 continue; 333 const MachOObjectFile *MachDbgObj = dyn_cast<const MachOObjectFile>(DbgObj); 334 if (!MachDbgObj) 335 continue; 336 if (darwinDsymMatchesBinary(MachDbgObj, MachExeObj)) 337 return DbgObj; 338 } 339 return nullptr; 340 } 341 342 ObjectFile *LLVMSymbolizer::lookUpDebuglinkObject(const std::string &Path, 343 const ObjectFile *Obj, 344 const std::string &ArchName) { 345 std::string DebuglinkName; 346 uint32_t CRCHash; 347 std::string DebugBinaryPath; 348 if (!getGNUDebuglinkContents(Obj, DebuglinkName, CRCHash)) 349 return nullptr; 350 if (!findDebugBinary(Path, DebuglinkName, CRCHash, DebugBinaryPath)) 351 return nullptr; 352 auto DbgObjOrErr = getOrCreateObject(DebugBinaryPath, ArchName); 353 if (!DbgObjOrErr) { 354 // Ignore errors, the file might not exist. 355 consumeError(DbgObjOrErr.takeError()); 356 return nullptr; 357 } 358 return DbgObjOrErr.get(); 359 } 360 361 ObjectFile *LLVMSymbolizer::lookUpBuildIDObject(const std::string &Path, 362 const ELFObjectFileBase *Obj, 363 const std::string &ArchName) { 364 auto BuildID = getBuildID(Obj); 365 if (!BuildID) 366 return nullptr; 367 if (BuildID->size() < 2) 368 return nullptr; 369 std::string DebugBinaryPath; 370 if (!findDebugBinary(*BuildID, DebugBinaryPath)) 371 return nullptr; 372 auto DbgObjOrErr = getOrCreateObject(DebugBinaryPath, ArchName); 373 if (!DbgObjOrErr) { 374 consumeError(DbgObjOrErr.takeError()); 375 return nullptr; 376 } 377 return DbgObjOrErr.get(); 378 } 379 380 bool LLVMSymbolizer::findDebugBinary(const std::string &OrigPath, 381 const std::string &DebuglinkName, 382 uint32_t CRCHash, std::string &Result) { 383 SmallString<16> OrigDir(OrigPath); 384 llvm::sys::path::remove_filename(OrigDir); 385 SmallString<16> DebugPath = OrigDir; 386 // Try relative/path/to/original_binary/debuglink_name 387 llvm::sys::path::append(DebugPath, DebuglinkName); 388 if (checkFileCRC(DebugPath, CRCHash)) { 389 Result = std::string(DebugPath.str()); 390 return true; 391 } 392 // Try relative/path/to/original_binary/.debug/debuglink_name 393 DebugPath = OrigDir; 394 llvm::sys::path::append(DebugPath, ".debug", DebuglinkName); 395 if (checkFileCRC(DebugPath, CRCHash)) { 396 Result = std::string(DebugPath.str()); 397 return true; 398 } 399 // Make the path absolute so that lookups will go to 400 // "/usr/lib/debug/full/path/to/debug", not 401 // "/usr/lib/debug/to/debug" 402 llvm::sys::fs::make_absolute(OrigDir); 403 if (!Opts.FallbackDebugPath.empty()) { 404 // Try <FallbackDebugPath>/absolute/path/to/original_binary/debuglink_name 405 DebugPath = Opts.FallbackDebugPath; 406 } else { 407 #if defined(__NetBSD__) 408 // Try /usr/libdata/debug/absolute/path/to/original_binary/debuglink_name 409 DebugPath = "/usr/libdata/debug"; 410 #else 411 // Try /usr/lib/debug/absolute/path/to/original_binary/debuglink_name 412 DebugPath = "/usr/lib/debug"; 413 #endif 414 } 415 llvm::sys::path::append(DebugPath, llvm::sys::path::relative_path(OrigDir), 416 DebuglinkName); 417 if (checkFileCRC(DebugPath, CRCHash)) { 418 Result = std::string(DebugPath.str()); 419 return true; 420 } 421 return false; 422 } 423 424 bool LLVMSymbolizer::findDebugBinary(const ArrayRef<uint8_t> BuildID, 425 std::string &Result) { 426 Optional<std::string> Path; 427 Path = LocalDIFetcher(Opts.DebugFileDirectory).fetchBuildID(BuildID); 428 if (Path) { 429 Result = std::move(*Path); 430 return true; 431 } 432 433 // Try caller-provided debug info fetchers. 434 for (const std::unique_ptr<DIFetcher> &Fetcher : DIFetchers) { 435 Path = Fetcher->fetchBuildID(BuildID); 436 if (Path) { 437 Result = std::move(*Path); 438 return true; 439 } 440 } 441 442 return false; 443 } 444 445 Expected<LLVMSymbolizer::ObjectPair> 446 LLVMSymbolizer::getOrCreateObjectPair(const std::string &Path, 447 const std::string &ArchName) { 448 auto I = ObjectPairForPathArch.find(std::make_pair(Path, ArchName)); 449 if (I != ObjectPairForPathArch.end()) 450 return I->second; 451 452 auto ObjOrErr = getOrCreateObject(Path, ArchName); 453 if (!ObjOrErr) { 454 ObjectPairForPathArch.emplace(std::make_pair(Path, ArchName), 455 ObjectPair(nullptr, nullptr)); 456 return ObjOrErr.takeError(); 457 } 458 459 ObjectFile *Obj = ObjOrErr.get(); 460 assert(Obj != nullptr); 461 ObjectFile *DbgObj = nullptr; 462 463 if (auto MachObj = dyn_cast<const MachOObjectFile>(Obj)) 464 DbgObj = lookUpDsymFile(Path, MachObj, ArchName); 465 else if (auto ELFObj = dyn_cast<const ELFObjectFileBase>(Obj)) 466 DbgObj = lookUpBuildIDObject(Path, ELFObj, ArchName); 467 if (!DbgObj) 468 DbgObj = lookUpDebuglinkObject(Path, Obj, ArchName); 469 if (!DbgObj) 470 DbgObj = Obj; 471 ObjectPair Res = std::make_pair(Obj, DbgObj); 472 ObjectPairForPathArch.emplace(std::make_pair(Path, ArchName), Res); 473 return Res; 474 } 475 476 Expected<ObjectFile *> 477 LLVMSymbolizer::getOrCreateObject(const std::string &Path, 478 const std::string &ArchName) { 479 Binary *Bin; 480 auto Pair = BinaryForPath.emplace(Path, OwningBinary<Binary>()); 481 if (!Pair.second) { 482 Bin = Pair.first->second.getBinary(); 483 } else { 484 Expected<OwningBinary<Binary>> BinOrErr = createBinary(Path); 485 if (!BinOrErr) 486 return BinOrErr.takeError(); 487 Pair.first->second = std::move(BinOrErr.get()); 488 Bin = Pair.first->second.getBinary(); 489 } 490 491 if (!Bin) 492 return static_cast<ObjectFile *>(nullptr); 493 494 if (MachOUniversalBinary *UB = dyn_cast_or_null<MachOUniversalBinary>(Bin)) { 495 auto I = ObjectForUBPathAndArch.find(std::make_pair(Path, ArchName)); 496 if (I != ObjectForUBPathAndArch.end()) 497 return I->second.get(); 498 499 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = 500 UB->getMachOObjectForArch(ArchName); 501 if (!ObjOrErr) { 502 ObjectForUBPathAndArch.emplace(std::make_pair(Path, ArchName), 503 std::unique_ptr<ObjectFile>()); 504 return ObjOrErr.takeError(); 505 } 506 ObjectFile *Res = ObjOrErr->get(); 507 ObjectForUBPathAndArch.emplace(std::make_pair(Path, ArchName), 508 std::move(ObjOrErr.get())); 509 return Res; 510 } 511 if (Bin->isObject()) { 512 return cast<ObjectFile>(Bin); 513 } 514 return errorCodeToError(object_error::arch_not_found); 515 } 516 517 Expected<SymbolizableModule *> 518 LLVMSymbolizer::createModuleInfo(const ObjectFile *Obj, 519 std::unique_ptr<DIContext> Context, 520 StringRef ModuleName) { 521 auto InfoOrErr = SymbolizableObjectFile::create(Obj, std::move(Context), 522 Opts.UntagAddresses); 523 std::unique_ptr<SymbolizableModule> SymMod; 524 if (InfoOrErr) 525 SymMod = std::move(*InfoOrErr); 526 auto InsertResult = Modules.insert( 527 std::make_pair(std::string(ModuleName), std::move(SymMod))); 528 assert(InsertResult.second); 529 if (!InfoOrErr) 530 return InfoOrErr.takeError(); 531 return InsertResult.first->second.get(); 532 } 533 534 Expected<SymbolizableModule *> 535 LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) { 536 auto I = Modules.find(ModuleName); 537 if (I != Modules.end()) 538 return I->second.get(); 539 540 std::string BinaryName = ModuleName; 541 std::string ArchName = Opts.DefaultArch; 542 size_t ColonPos = ModuleName.find_last_of(':'); 543 // Verify that substring after colon form a valid arch name. 544 if (ColonPos != std::string::npos) { 545 std::string ArchStr = ModuleName.substr(ColonPos + 1); 546 if (Triple(ArchStr).getArch() != Triple::UnknownArch) { 547 BinaryName = ModuleName.substr(0, ColonPos); 548 ArchName = ArchStr; 549 } 550 } 551 auto ObjectsOrErr = getOrCreateObjectPair(BinaryName, ArchName); 552 if (!ObjectsOrErr) { 553 // Failed to find valid object file. 554 Modules.emplace(ModuleName, std::unique_ptr<SymbolizableModule>()); 555 return ObjectsOrErr.takeError(); 556 } 557 ObjectPair Objects = ObjectsOrErr.get(); 558 559 std::unique_ptr<DIContext> Context; 560 // If this is a COFF object containing PDB info, use a PDBContext to 561 // symbolize. Otherwise, use DWARF. 562 if (auto CoffObject = dyn_cast<COFFObjectFile>(Objects.first)) { 563 const codeview::DebugInfo *DebugInfo; 564 StringRef PDBFileName; 565 auto EC = CoffObject->getDebugPDBInfo(DebugInfo, PDBFileName); 566 if (!EC && DebugInfo != nullptr && !PDBFileName.empty()) { 567 using namespace pdb; 568 std::unique_ptr<IPDBSession> Session; 569 570 PDB_ReaderType ReaderType = 571 Opts.UseDIA ? PDB_ReaderType::DIA : PDB_ReaderType::Native; 572 if (auto Err = loadDataForEXE(ReaderType, Objects.first->getFileName(), 573 Session)) { 574 Modules.emplace(ModuleName, std::unique_ptr<SymbolizableModule>()); 575 // Return along the PDB filename to provide more context 576 return createFileError(PDBFileName, std::move(Err)); 577 } 578 Context.reset(new PDBContext(*CoffObject, std::move(Session))); 579 } 580 } 581 if (!Context) 582 Context = DWARFContext::create( 583 *Objects.second, DWARFContext::ProcessDebugRelocations::Process, 584 nullptr, Opts.DWPName); 585 return createModuleInfo(Objects.first, std::move(Context), ModuleName); 586 } 587 588 Expected<SymbolizableModule *> 589 LLVMSymbolizer::getOrCreateModuleInfo(const ObjectFile &Obj) { 590 StringRef ObjName = Obj.getFileName(); 591 auto I = Modules.find(ObjName); 592 if (I != Modules.end()) 593 return I->second.get(); 594 595 std::unique_ptr<DIContext> Context = DWARFContext::create(Obj); 596 // FIXME: handle COFF object with PDB info to use PDBContext 597 return createModuleInfo(&Obj, std::move(Context), ObjName); 598 } 599 600 namespace { 601 602 // Undo these various manglings for Win32 extern "C" functions: 603 // cdecl - _foo 604 // stdcall - _foo@12 605 // fastcall - @foo@12 606 // vectorcall - foo@@12 607 // These are all different linkage names for 'foo'. 608 StringRef demanglePE32ExternCFunc(StringRef SymbolName) { 609 // Remove any '_' or '@' prefix. 610 char Front = SymbolName.empty() ? '\0' : SymbolName[0]; 611 if (Front == '_' || Front == '@') 612 SymbolName = SymbolName.drop_front(); 613 614 // Remove any '@[0-9]+' suffix. 615 if (Front != '?') { 616 size_t AtPos = SymbolName.rfind('@'); 617 if (AtPos != StringRef::npos && 618 all_of(drop_begin(SymbolName, AtPos + 1), isDigit)) 619 SymbolName = SymbolName.substr(0, AtPos); 620 } 621 622 // Remove any ending '@' for vectorcall. 623 if (SymbolName.endswith("@")) 624 SymbolName = SymbolName.drop_back(); 625 626 return SymbolName; 627 } 628 629 } // end anonymous namespace 630 631 std::string 632 LLVMSymbolizer::DemangleName(const std::string &Name, 633 const SymbolizableModule *DbiModuleDescriptor) { 634 std::string Result; 635 if (nonMicrosoftDemangle(Name.c_str(), Result)) 636 return Result; 637 638 if (!Name.empty() && Name.front() == '?') { 639 // Only do MSVC C++ demangling on symbols starting with '?'. 640 int status = 0; 641 char *DemangledName = microsoftDemangle( 642 Name.c_str(), nullptr, nullptr, nullptr, &status, 643 MSDemangleFlags(MSDF_NoAccessSpecifier | MSDF_NoCallingConvention | 644 MSDF_NoMemberType | MSDF_NoReturnType)); 645 if (status != 0) 646 return Name; 647 Result = DemangledName; 648 free(DemangledName); 649 return Result; 650 } 651 652 if (DbiModuleDescriptor && DbiModuleDescriptor->isWin32Module()) 653 return std::string(demanglePE32ExternCFunc(Name)); 654 return Name; 655 } 656 657 } // namespace symbolize 658 } // namespace llvm 659