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/SymbolizableObjectFile.h"
22 #include "llvm/Debuginfod/Debuginfod.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 findDebugBinary(const std::string &OrigPath,
233                      const std::string &DebuglinkName, uint32_t CRCHash,
234                      const std::string &FallbackDebugPath,
235                      std::string &Result) {
236   SmallString<16> OrigDir(OrigPath);
237   llvm::sys::path::remove_filename(OrigDir);
238   SmallString<16> DebugPath = OrigDir;
239   // Try relative/path/to/original_binary/debuglink_name
240   llvm::sys::path::append(DebugPath, DebuglinkName);
241   if (checkFileCRC(DebugPath, CRCHash)) {
242     Result = std::string(DebugPath.str());
243     return true;
244   }
245   // Try relative/path/to/original_binary/.debug/debuglink_name
246   DebugPath = OrigDir;
247   llvm::sys::path::append(DebugPath, ".debug", DebuglinkName);
248   if (checkFileCRC(DebugPath, CRCHash)) {
249     Result = std::string(DebugPath.str());
250     return true;
251   }
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 {
260 #if defined(__NetBSD__)
261     // Try /usr/libdata/debug/absolute/path/to/original_binary/debuglink_name
262     DebugPath = "/usr/libdata/debug";
263 #else
264     // Try /usr/lib/debug/absolute/path/to/original_binary/debuglink_name
265     DebugPath = "/usr/lib/debug";
266 #endif
267   }
268   llvm::sys::path::append(DebugPath, llvm::sys::path::relative_path(OrigDir),
269                           DebuglinkName);
270   if (checkFileCRC(DebugPath, CRCHash)) {
271     Result = std::string(DebugPath.str());
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;
283     consumeError(Section.getName().moveInto(Name));
284 
285     Name = Name.substr(Name.find_first_not_of("._"));
286     if (Name == "gnu_debuglink") {
287       Expected<StringRef> ContentsOrErr = Section.getContents();
288       if (!ContentsOrErr) {
289         consumeError(ContentsOrErr.takeError());
290         return false;
291       }
292       DataExtractor DE(*ContentsOrErr, Obj->isLittleEndian(), 0);
293       uint64_t Offset = 0;
294       if (const char *DebugNameStr = DE.getCStr(&Offset)) {
295         // 4-byte align the offset.
296         Offset = (Offset + 3) & ~0x3;
297         if (DE.isValidOffsetForDataOfSize(Offset, 4)) {
298           DebugName = DebugNameStr;
299           CRCHash = DE.getU32(&Offset);
300           return true;
301         }
302       }
303       break;
304     }
305   }
306   return false;
307 }
308 
309 bool darwinDsymMatchesBinary(const MachOObjectFile *DbgObj,
310                              const MachOObjectFile *Obj) {
311   ArrayRef<uint8_t> dbg_uuid = DbgObj->getUuid();
312   ArrayRef<uint8_t> bin_uuid = Obj->getUuid();
313   if (dbg_uuid.empty() || bin_uuid.empty())
314     return false;
315   return !memcmp(dbg_uuid.data(), bin_uuid.data(), dbg_uuid.size());
316 }
317 
318 template <typename ELFT>
319 Optional<ArrayRef<uint8_t>> getBuildID(const ELFFile<ELFT> &Obj) {
320   auto PhdrsOrErr = Obj.program_headers();
321   if (!PhdrsOrErr) {
322     consumeError(PhdrsOrErr.takeError());
323     return {};
324   }
325   for (const auto &P : *PhdrsOrErr) {
326     if (P.p_type != ELF::PT_NOTE)
327       continue;
328     Error Err = Error::success();
329     for (auto N : Obj.notes(P, Err))
330       if (N.getType() == ELF::NT_GNU_BUILD_ID &&
331           N.getName() == ELF::ELF_NOTE_GNU)
332         return N.getDesc();
333     consumeError(std::move(Err));
334   }
335   return {};
336 }
337 
338 Optional<ArrayRef<uint8_t>> getBuildID(const ELFObjectFileBase *Obj) {
339   Optional<ArrayRef<uint8_t>> BuildID;
340   if (auto *O = dyn_cast<ELFObjectFile<ELF32LE>>(Obj))
341     BuildID = getBuildID(O->getELFFile());
342   else if (auto *O = dyn_cast<ELFObjectFile<ELF32BE>>(Obj))
343     BuildID = getBuildID(O->getELFFile());
344   else if (auto *O = dyn_cast<ELFObjectFile<ELF64LE>>(Obj))
345     BuildID = getBuildID(O->getELFFile());
346   else if (auto *O = dyn_cast<ELFObjectFile<ELF64BE>>(Obj))
347     BuildID = getBuildID(O->getELFFile());
348   else
349     llvm_unreachable("unsupported file format");
350   return BuildID;
351 }
352 
353 bool findDebugBinary(const std::vector<std::string> &DebugFileDirectory,
354                      const ArrayRef<uint8_t> BuildID, std::string &Result) {
355   auto getDebugPath = [&](StringRef Directory) {
356     SmallString<128> Path{Directory};
357     sys::path::append(Path, ".build-id",
358                       llvm::toHex(BuildID[0], /*LowerCase=*/true),
359                       llvm::toHex(BuildID.slice(1), /*LowerCase=*/true));
360     Path += ".debug";
361     return Path;
362   };
363   if (DebugFileDirectory.empty()) {
364     SmallString<128> Path = getDebugPath(
365 #if defined(__NetBSD__)
366         // Try /usr/libdata/debug/.build-id/../...
367         "/usr/libdata/debug"
368 #else
369         // Try /usr/lib/debug/.build-id/../...
370         "/usr/lib/debug"
371 #endif
372     );
373     if (llvm::sys::fs::exists(Path)) {
374       Result = std::string(Path.str());
375       return true;
376     }
377   } else {
378     for (const auto &Directory : DebugFileDirectory) {
379       // Try <debug-file-directory>/.build-id/../...
380       SmallString<128> Path = getDebugPath(Directory);
381       if (llvm::sys::fs::exists(Path)) {
382         Result = std::string(Path.str());
383         return true;
384       }
385     }
386   }
387   // Try debuginfod client cache and known servers.
388   Expected<std::string> PathOrErr = getCachedOrDownloadDebuginfo(BuildID);
389   if (!PathOrErr) {
390     consumeError(PathOrErr.takeError());
391     return false;
392   }
393   Result = *PathOrErr;
394   return true;
395 }
396 
397 } // end anonymous namespace
398 
399 ObjectFile *LLVMSymbolizer::lookUpDsymFile(const std::string &ExePath,
400                                            const MachOObjectFile *MachExeObj,
401                                            const std::string &ArchName) {
402   // On Darwin we may find DWARF in separate object file in
403   // resource directory.
404   std::vector<std::string> DsymPaths;
405   StringRef Filename = sys::path::filename(ExePath);
406   DsymPaths.push_back(
407       getDarwinDWARFResourceForPath(ExePath, std::string(Filename)));
408   for (const auto &Path : Opts.DsymHints) {
409     DsymPaths.push_back(
410         getDarwinDWARFResourceForPath(Path, std::string(Filename)));
411   }
412   for (const auto &Path : DsymPaths) {
413     auto DbgObjOrErr = getOrCreateObject(Path, ArchName);
414     if (!DbgObjOrErr) {
415       // Ignore errors, the file might not exist.
416       consumeError(DbgObjOrErr.takeError());
417       continue;
418     }
419     ObjectFile *DbgObj = DbgObjOrErr.get();
420     if (!DbgObj)
421       continue;
422     const MachOObjectFile *MachDbgObj = dyn_cast<const MachOObjectFile>(DbgObj);
423     if (!MachDbgObj)
424       continue;
425     if (darwinDsymMatchesBinary(MachDbgObj, MachExeObj))
426       return DbgObj;
427   }
428   return nullptr;
429 }
430 
431 ObjectFile *LLVMSymbolizer::lookUpDebuglinkObject(const std::string &Path,
432                                                   const ObjectFile *Obj,
433                                                   const std::string &ArchName) {
434   std::string DebuglinkName;
435   uint32_t CRCHash;
436   std::string DebugBinaryPath;
437   if (!getGNUDebuglinkContents(Obj, DebuglinkName, CRCHash))
438     return nullptr;
439   if (!findDebugBinary(Path, DebuglinkName, CRCHash, Opts.FallbackDebugPath,
440                        DebugBinaryPath))
441     return nullptr;
442   auto DbgObjOrErr = getOrCreateObject(DebugBinaryPath, ArchName);
443   if (!DbgObjOrErr) {
444     // Ignore errors, the file might not exist.
445     consumeError(DbgObjOrErr.takeError());
446     return nullptr;
447   }
448   return DbgObjOrErr.get();
449 }
450 
451 ObjectFile *LLVMSymbolizer::lookUpBuildIDObject(const std::string &Path,
452                                                 const ELFObjectFileBase *Obj,
453                                                 const std::string &ArchName) {
454   auto BuildID = getBuildID(Obj);
455   if (!BuildID)
456     return nullptr;
457   if (BuildID->size() < 2)
458     return nullptr;
459   std::string DebugBinaryPath;
460   if (!findDebugBinary(Opts.DebugFileDirectory, *BuildID, DebugBinaryPath))
461     return nullptr;
462   auto DbgObjOrErr = getOrCreateObject(DebugBinaryPath, ArchName);
463   if (!DbgObjOrErr) {
464     consumeError(DbgObjOrErr.takeError());
465     return nullptr;
466   }
467   return DbgObjOrErr.get();
468 }
469 
470 Expected<LLVMSymbolizer::ObjectPair>
471 LLVMSymbolizer::getOrCreateObjectPair(const std::string &Path,
472                                       const std::string &ArchName) {
473   auto I = ObjectPairForPathArch.find(std::make_pair(Path, ArchName));
474   if (I != ObjectPairForPathArch.end())
475     return I->second;
476 
477   auto ObjOrErr = getOrCreateObject(Path, ArchName);
478   if (!ObjOrErr) {
479     ObjectPairForPathArch.emplace(std::make_pair(Path, ArchName),
480                                   ObjectPair(nullptr, nullptr));
481     return ObjOrErr.takeError();
482   }
483 
484   ObjectFile *Obj = ObjOrErr.get();
485   assert(Obj != nullptr);
486   ObjectFile *DbgObj = nullptr;
487 
488   if (auto MachObj = dyn_cast<const MachOObjectFile>(Obj))
489     DbgObj = lookUpDsymFile(Path, MachObj, ArchName);
490   else if (auto ELFObj = dyn_cast<const ELFObjectFileBase>(Obj))
491     DbgObj = lookUpBuildIDObject(Path, ELFObj, ArchName);
492   if (!DbgObj)
493     DbgObj = lookUpDebuglinkObject(Path, Obj, ArchName);
494   if (!DbgObj)
495     DbgObj = Obj;
496   ObjectPair Res = std::make_pair(Obj, DbgObj);
497   ObjectPairForPathArch.emplace(std::make_pair(Path, ArchName), Res);
498   return Res;
499 }
500 
501 Expected<ObjectFile *>
502 LLVMSymbolizer::getOrCreateObject(const std::string &Path,
503                                   const std::string &ArchName) {
504   Binary *Bin;
505   auto Pair = BinaryForPath.emplace(Path, OwningBinary<Binary>());
506   if (!Pair.second) {
507     Bin = Pair.first->second.getBinary();
508   } else {
509     Expected<OwningBinary<Binary>> BinOrErr = createBinary(Path);
510     if (!BinOrErr)
511       return BinOrErr.takeError();
512     Pair.first->second = std::move(BinOrErr.get());
513     Bin = Pair.first->second.getBinary();
514   }
515 
516   if (!Bin)
517     return static_cast<ObjectFile *>(nullptr);
518 
519   if (MachOUniversalBinary *UB = dyn_cast_or_null<MachOUniversalBinary>(Bin)) {
520     auto I = ObjectForUBPathAndArch.find(std::make_pair(Path, ArchName));
521     if (I != ObjectForUBPathAndArch.end())
522       return I->second.get();
523 
524     Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
525         UB->getMachOObjectForArch(ArchName);
526     if (!ObjOrErr) {
527       ObjectForUBPathAndArch.emplace(std::make_pair(Path, ArchName),
528                                      std::unique_ptr<ObjectFile>());
529       return ObjOrErr.takeError();
530     }
531     ObjectFile *Res = ObjOrErr->get();
532     ObjectForUBPathAndArch.emplace(std::make_pair(Path, ArchName),
533                                    std::move(ObjOrErr.get()));
534     return Res;
535   }
536   if (Bin->isObject()) {
537     return cast<ObjectFile>(Bin);
538   }
539   return errorCodeToError(object_error::arch_not_found);
540 }
541 
542 Expected<SymbolizableModule *>
543 LLVMSymbolizer::createModuleInfo(const ObjectFile *Obj,
544                                  std::unique_ptr<DIContext> Context,
545                                  StringRef ModuleName) {
546   auto InfoOrErr = SymbolizableObjectFile::create(Obj, std::move(Context),
547                                                   Opts.UntagAddresses);
548   std::unique_ptr<SymbolizableModule> SymMod;
549   if (InfoOrErr)
550     SymMod = std::move(*InfoOrErr);
551   auto InsertResult = Modules.insert(
552       std::make_pair(std::string(ModuleName), std::move(SymMod)));
553   assert(InsertResult.second);
554   if (!InfoOrErr)
555     return InfoOrErr.takeError();
556   return InsertResult.first->second.get();
557 }
558 
559 Expected<SymbolizableModule *>
560 LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) {
561   auto I = Modules.find(ModuleName);
562   if (I != Modules.end())
563     return I->second.get();
564 
565   std::string BinaryName = ModuleName;
566   std::string ArchName = Opts.DefaultArch;
567   size_t ColonPos = ModuleName.find_last_of(':');
568   // Verify that substring after colon form a valid arch name.
569   if (ColonPos != std::string::npos) {
570     std::string ArchStr = ModuleName.substr(ColonPos + 1);
571     if (Triple(ArchStr).getArch() != Triple::UnknownArch) {
572       BinaryName = ModuleName.substr(0, ColonPos);
573       ArchName = ArchStr;
574     }
575   }
576   auto ObjectsOrErr = getOrCreateObjectPair(BinaryName, ArchName);
577   if (!ObjectsOrErr) {
578     // Failed to find valid object file.
579     Modules.emplace(ModuleName, std::unique_ptr<SymbolizableModule>());
580     return ObjectsOrErr.takeError();
581   }
582   ObjectPair Objects = ObjectsOrErr.get();
583 
584   std::unique_ptr<DIContext> Context;
585   // If this is a COFF object containing PDB info, use a PDBContext to
586   // symbolize. Otherwise, use DWARF.
587   if (auto CoffObject = dyn_cast<COFFObjectFile>(Objects.first)) {
588     const codeview::DebugInfo *DebugInfo;
589     StringRef PDBFileName;
590     auto EC = CoffObject->getDebugPDBInfo(DebugInfo, PDBFileName);
591     if (!EC && DebugInfo != nullptr && !PDBFileName.empty()) {
592       using namespace pdb;
593       std::unique_ptr<IPDBSession> Session;
594 
595       PDB_ReaderType ReaderType =
596           Opts.UseDIA ? PDB_ReaderType::DIA : PDB_ReaderType::Native;
597       if (auto Err = loadDataForEXE(ReaderType, Objects.first->getFileName(),
598                                     Session)) {
599         Modules.emplace(ModuleName, std::unique_ptr<SymbolizableModule>());
600         // Return along the PDB filename to provide more context
601         return createFileError(PDBFileName, std::move(Err));
602       }
603       Context.reset(new PDBContext(*CoffObject, std::move(Session)));
604     }
605   }
606   if (!Context)
607     Context = DWARFContext::create(
608         *Objects.second, DWARFContext::ProcessDebugRelocations::Process,
609         nullptr, Opts.DWPName);
610   return createModuleInfo(Objects.first, std::move(Context), ModuleName);
611 }
612 
613 Expected<SymbolizableModule *>
614 LLVMSymbolizer::getOrCreateModuleInfo(const ObjectFile &Obj) {
615   StringRef ObjName = Obj.getFileName();
616   auto I = Modules.find(ObjName);
617   if (I != Modules.end())
618     return I->second.get();
619 
620   std::unique_ptr<DIContext> Context = DWARFContext::create(Obj);
621   // FIXME: handle COFF object with PDB info to use PDBContext
622   return createModuleInfo(&Obj, std::move(Context), ObjName);
623 }
624 
625 namespace {
626 
627 // Undo these various manglings for Win32 extern "C" functions:
628 // cdecl       - _foo
629 // stdcall     - _foo@12
630 // fastcall    - @foo@12
631 // vectorcall  - foo@@12
632 // These are all different linkage names for 'foo'.
633 StringRef demanglePE32ExternCFunc(StringRef SymbolName) {
634   // Remove any '_' or '@' prefix.
635   char Front = SymbolName.empty() ? '\0' : SymbolName[0];
636   if (Front == '_' || Front == '@')
637     SymbolName = SymbolName.drop_front();
638 
639   // Remove any '@[0-9]+' suffix.
640   if (Front != '?') {
641     size_t AtPos = SymbolName.rfind('@');
642     if (AtPos != StringRef::npos &&
643         all_of(drop_begin(SymbolName, AtPos + 1), isDigit))
644       SymbolName = SymbolName.substr(0, AtPos);
645   }
646 
647   // Remove any ending '@' for vectorcall.
648   if (SymbolName.endswith("@"))
649     SymbolName = SymbolName.drop_back();
650 
651   return SymbolName;
652 }
653 
654 } // end anonymous namespace
655 
656 std::string
657 LLVMSymbolizer::DemangleName(const std::string &Name,
658                              const SymbolizableModule *DbiModuleDescriptor) {
659   std::string Result;
660   if (nonMicrosoftDemangle(Name.c_str(), Result))
661     return Result;
662 
663   if (!Name.empty() && Name.front() == '?') {
664     // Only do MSVC C++ demangling on symbols starting with '?'.
665     int status = 0;
666     char *DemangledName = microsoftDemangle(
667         Name.c_str(), nullptr, nullptr, nullptr, &status,
668         MSDemangleFlags(MSDF_NoAccessSpecifier | MSDF_NoCallingConvention |
669                         MSDF_NoMemberType | MSDF_NoReturnType));
670     if (status != 0)
671       return Name;
672     Result = DemangledName;
673     free(DemangledName);
674     return Result;
675   }
676 
677   if (DbiModuleDescriptor && DbiModuleDescriptor->isWin32Module())
678     return std::string(demanglePE32ExternCFunc(Name));
679   return Name;
680 }
681 
682 } // namespace symbolize
683 } // namespace llvm
684