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