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