1 //===-- LLVMSymbolize.cpp -------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Implementation for LLVM symbolization library.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/DebugInfo/Symbolize/Symbolize.h"
14 
15 #include "SymbolizableObjectFile.h"
16 
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/BinaryFormat/COFF.h"
19 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
20 #include "llvm/DebugInfo/PDB/PDB.h"
21 #include "llvm/DebugInfo/PDB/PDBContext.h"
22 #include "llvm/Demangle/Demangle.h"
23 #include "llvm/Object/COFF.h"
24 #include "llvm/Object/MachO.h"
25 #include "llvm/Object/MachOUniversal.h"
26 #include "llvm/Support/CRC.h"
27 #include "llvm/Support/Casting.h"
28 #include "llvm/Support/Compression.h"
29 #include "llvm/Support/DataExtractor.h"
30 #include "llvm/Support/Errc.h"
31 #include "llvm/Support/FileSystem.h"
32 #include "llvm/Support/MemoryBuffer.h"
33 #include "llvm/Support/Path.h"
34 #include <algorithm>
35 #include <cassert>
36 #include <cstring>
37 
38 #if defined(_MSC_VER)
39 #include <Windows.h>
40 
41 // This must be included after windows.h.
42 #include <DbgHelp.h>
43 #pragma comment(lib, "dbghelp.lib")
44 
45 // Windows.h conflicts with our COFF header definitions.
46 #ifdef IMAGE_FILE_MACHINE_I386
47 #undef IMAGE_FILE_MACHINE_I386
48 #endif
49 #endif
50 
51 namespace llvm {
52 namespace symbolize {
53 
54 Expected<DILineInfo>
55 LLVMSymbolizer::symbolizeCode(const std::string &ModuleName,
56                               object::SectionedAddress ModuleOffset,
57                               StringRef DWPName) {
58   SymbolizableModule *Info;
59   if (auto InfoOrErr = getOrCreateModuleInfo(ModuleName, DWPName))
60     Info = InfoOrErr.get();
61   else
62     return InfoOrErr.takeError();
63 
64   // A null module means an error has already been reported. Return an empty
65   // result.
66   if (!Info)
67     return DILineInfo();
68 
69   // If the user is giving us relative addresses, add the preferred base of the
70   // object to the offset before we do the query. It's what DIContext expects.
71   if (Opts.RelativeAddresses)
72     ModuleOffset.Address += Info->getModulePreferredBase();
73 
74   DILineInfo LineInfo = Info->symbolizeCode(ModuleOffset, Opts.PrintFunctions,
75                                             Opts.UseSymbolTable);
76   if (Opts.Demangle)
77     LineInfo.FunctionName = DemangleName(LineInfo.FunctionName, Info);
78   return LineInfo;
79 }
80 
81 Expected<DIInliningInfo>
82 LLVMSymbolizer::symbolizeInlinedCode(const std::string &ModuleName,
83                                      object::SectionedAddress ModuleOffset,
84                                      StringRef DWPName) {
85   SymbolizableModule *Info;
86   if (auto InfoOrErr = getOrCreateModuleInfo(ModuleName, DWPName))
87     Info = InfoOrErr.get();
88   else
89     return InfoOrErr.takeError();
90 
91   // A null module means an error has already been reported. Return an empty
92   // result.
93   if (!Info)
94     return DIInliningInfo();
95 
96   // If the user is giving us relative addresses, add the preferred base of the
97   // object to the offset before we do the query. It's what DIContext expects.
98   if (Opts.RelativeAddresses)
99     ModuleOffset.Address += Info->getModulePreferredBase();
100 
101   DIInliningInfo InlinedContext = Info->symbolizeInlinedCode(
102       ModuleOffset, Opts.PrintFunctions, Opts.UseSymbolTable);
103   if (Opts.Demangle) {
104     for (int i = 0, n = InlinedContext.getNumberOfFrames(); i < n; i++) {
105       auto *Frame = InlinedContext.getMutableFrame(i);
106       Frame->FunctionName = DemangleName(Frame->FunctionName, Info);
107     }
108   }
109   return InlinedContext;
110 }
111 
112 Expected<DIGlobal>
113 LLVMSymbolizer::symbolizeData(const std::string &ModuleName,
114                               object::SectionedAddress ModuleOffset) {
115   SymbolizableModule *Info;
116   if (auto InfoOrErr = getOrCreateModuleInfo(ModuleName))
117     Info = InfoOrErr.get();
118   else
119     return InfoOrErr.takeError();
120 
121   // A null module means an error has already been reported. Return an empty
122   // result.
123   if (!Info)
124     return DIGlobal();
125 
126   // If the user is giving us relative addresses, add the preferred base of
127   // the object to the offset before we do the query. It's what DIContext
128   // expects.
129   if (Opts.RelativeAddresses)
130     ModuleOffset.Address += Info->getModulePreferredBase();
131 
132   DIGlobal Global = Info->symbolizeData(ModuleOffset);
133   if (Opts.Demangle)
134     Global.Name = DemangleName(Global.Name, Info);
135   return Global;
136 }
137 
138 void LLVMSymbolizer::flush() {
139   ObjectForUBPathAndArch.clear();
140   BinaryForPath.clear();
141   ObjectPairForPathArch.clear();
142   Modules.clear();
143 }
144 
145 namespace {
146 
147 // For Path="/path/to/foo" and Basename="foo" assume that debug info is in
148 // /path/to/foo.dSYM/Contents/Resources/DWARF/foo.
149 // For Path="/path/to/bar.dSYM" and Basename="foo" assume that debug info is in
150 // /path/to/bar.dSYM/Contents/Resources/DWARF/foo.
151 std::string getDarwinDWARFResourceForPath(
152     const std::string &Path, const std::string &Basename) {
153   SmallString<16> ResourceName = StringRef(Path);
154   if (sys::path::extension(Path) != ".dSYM") {
155     ResourceName += ".dSYM";
156   }
157   sys::path::append(ResourceName, "Contents", "Resources", "DWARF");
158   sys::path::append(ResourceName, Basename);
159   return ResourceName.str();
160 }
161 
162 bool checkFileCRC(StringRef Path, uint32_t CRCHash) {
163   ErrorOr<std::unique_ptr<MemoryBuffer>> MB =
164       MemoryBuffer::getFileOrSTDIN(Path);
165   if (!MB)
166     return false;
167   return CRCHash == llvm::crc32(0, MB.get()->getBuffer());
168 }
169 
170 bool findDebugBinary(const std::string &OrigPath,
171                      const std::string &DebuglinkName, uint32_t CRCHash,
172                      const std::string &FallbackDebugPath,
173                      std::string &Result) {
174   SmallString<16> OrigDir(OrigPath);
175   llvm::sys::path::remove_filename(OrigDir);
176   SmallString<16> DebugPath = OrigDir;
177   // Try relative/path/to/original_binary/debuglink_name
178   llvm::sys::path::append(DebugPath, DebuglinkName);
179   if (checkFileCRC(DebugPath, CRCHash)) {
180     Result = DebugPath.str();
181     return true;
182   }
183   // Try relative/path/to/original_binary/.debug/debuglink_name
184   DebugPath = OrigDir;
185   llvm::sys::path::append(DebugPath, ".debug", DebuglinkName);
186   if (checkFileCRC(DebugPath, CRCHash)) {
187     Result = DebugPath.str();
188     return true;
189   }
190   // Make the path absolute so that lookups will go to
191   // "/usr/lib/debug/full/path/to/debug", not
192   // "/usr/lib/debug/to/debug"
193   llvm::sys::fs::make_absolute(OrigDir);
194   if (!FallbackDebugPath.empty()) {
195     // Try <FallbackDebugPath>/absolute/path/to/original_binary/debuglink_name
196     DebugPath = FallbackDebugPath;
197   } else {
198 #if defined(__NetBSD__)
199     // Try /usr/libdata/debug/absolute/path/to/original_binary/debuglink_name
200     DebugPath = "/usr/libdata/debug";
201 #else
202     // Try /usr/lib/debug/absolute/path/to/original_binary/debuglink_name
203     DebugPath = "/usr/lib/debug";
204 #endif
205   }
206   llvm::sys::path::append(DebugPath, llvm::sys::path::relative_path(OrigDir),
207                           DebuglinkName);
208   if (checkFileCRC(DebugPath, CRCHash)) {
209     Result = DebugPath.str();
210     return true;
211   }
212   return false;
213 }
214 
215 bool getGNUDebuglinkContents(const ObjectFile *Obj, std::string &DebugName,
216                              uint32_t &CRCHash) {
217   if (!Obj)
218     return false;
219   for (const SectionRef &Section : Obj->sections()) {
220     StringRef Name;
221     Section.getName(Name);
222     Name = Name.substr(Name.find_first_not_of("._"));
223     if (Name == "gnu_debuglink") {
224       Expected<StringRef> ContentsOrErr = Section.getContents();
225       if (!ContentsOrErr) {
226         consumeError(ContentsOrErr.takeError());
227         return false;
228       }
229       DataExtractor DE(*ContentsOrErr, Obj->isLittleEndian(), 0);
230       uint32_t Offset = 0;
231       if (const char *DebugNameStr = DE.getCStr(&Offset)) {
232         // 4-byte align the offset.
233         Offset = (Offset + 3) & ~0x3;
234         if (DE.isValidOffsetForDataOfSize(Offset, 4)) {
235           DebugName = DebugNameStr;
236           CRCHash = DE.getU32(&Offset);
237           return true;
238         }
239       }
240       break;
241     }
242   }
243   return false;
244 }
245 
246 bool darwinDsymMatchesBinary(const MachOObjectFile *DbgObj,
247                              const MachOObjectFile *Obj) {
248   ArrayRef<uint8_t> dbg_uuid = DbgObj->getUuid();
249   ArrayRef<uint8_t> bin_uuid = Obj->getUuid();
250   if (dbg_uuid.empty() || bin_uuid.empty())
251     return false;
252   return !memcmp(dbg_uuid.data(), bin_uuid.data(), dbg_uuid.size());
253 }
254 
255 } // end anonymous namespace
256 
257 ObjectFile *LLVMSymbolizer::lookUpDsymFile(const std::string &ExePath,
258     const MachOObjectFile *MachExeObj, const std::string &ArchName) {
259   // On Darwin we may find DWARF in separate object file in
260   // resource directory.
261   std::vector<std::string> DsymPaths;
262   StringRef Filename = sys::path::filename(ExePath);
263   DsymPaths.push_back(getDarwinDWARFResourceForPath(ExePath, Filename));
264   for (const auto &Path : Opts.DsymHints) {
265     DsymPaths.push_back(getDarwinDWARFResourceForPath(Path, Filename));
266   }
267   for (const auto &Path : DsymPaths) {
268     auto DbgObjOrErr = getOrCreateObject(Path, ArchName);
269     if (!DbgObjOrErr) {
270       // Ignore errors, the file might not exist.
271       consumeError(DbgObjOrErr.takeError());
272       continue;
273     }
274     ObjectFile *DbgObj = DbgObjOrErr.get();
275     if (!DbgObj)
276       continue;
277     const MachOObjectFile *MachDbgObj = dyn_cast<const MachOObjectFile>(DbgObj);
278     if (!MachDbgObj)
279       continue;
280     if (darwinDsymMatchesBinary(MachDbgObj, MachExeObj))
281       return DbgObj;
282   }
283   return nullptr;
284 }
285 
286 ObjectFile *LLVMSymbolizer::lookUpDebuglinkObject(const std::string &Path,
287                                                   const ObjectFile *Obj,
288                                                   const std::string &ArchName) {
289   std::string DebuglinkName;
290   uint32_t CRCHash;
291   std::string DebugBinaryPath;
292   if (!getGNUDebuglinkContents(Obj, DebuglinkName, CRCHash))
293     return nullptr;
294   if (!findDebugBinary(Path, DebuglinkName, CRCHash, Opts.FallbackDebugPath,
295                        DebugBinaryPath))
296     return nullptr;
297   auto DbgObjOrErr = getOrCreateObject(DebugBinaryPath, ArchName);
298   if (!DbgObjOrErr) {
299     // Ignore errors, the file might not exist.
300     consumeError(DbgObjOrErr.takeError());
301     return nullptr;
302   }
303   return DbgObjOrErr.get();
304 }
305 
306 Expected<LLVMSymbolizer::ObjectPair>
307 LLVMSymbolizer::getOrCreateObjectPair(const std::string &Path,
308                                       const std::string &ArchName) {
309   const auto &I = ObjectPairForPathArch.find(std::make_pair(Path, ArchName));
310   if (I != ObjectPairForPathArch.end()) {
311     return I->second;
312   }
313 
314   auto ObjOrErr = getOrCreateObject(Path, ArchName);
315   if (!ObjOrErr) {
316     ObjectPairForPathArch.insert(std::make_pair(std::make_pair(Path, ArchName),
317                                                 ObjectPair(nullptr, nullptr)));
318     return ObjOrErr.takeError();
319   }
320 
321   ObjectFile *Obj = ObjOrErr.get();
322   assert(Obj != nullptr);
323   ObjectFile *DbgObj = nullptr;
324 
325   if (auto MachObj = dyn_cast<const MachOObjectFile>(Obj))
326     DbgObj = lookUpDsymFile(Path, MachObj, ArchName);
327   if (!DbgObj)
328     DbgObj = lookUpDebuglinkObject(Path, Obj, ArchName);
329   if (!DbgObj)
330     DbgObj = Obj;
331   ObjectPair Res = std::make_pair(Obj, DbgObj);
332   ObjectPairForPathArch.insert(
333       std::make_pair(std::make_pair(Path, ArchName), Res));
334   return Res;
335 }
336 
337 Expected<ObjectFile *>
338 LLVMSymbolizer::getOrCreateObject(const std::string &Path,
339                                   const std::string &ArchName) {
340   const auto &I = BinaryForPath.find(Path);
341   Binary *Bin = nullptr;
342   if (I == BinaryForPath.end()) {
343     Expected<OwningBinary<Binary>> BinOrErr = createBinary(Path);
344     if (!BinOrErr) {
345       BinaryForPath.insert(std::make_pair(Path, OwningBinary<Binary>()));
346       return BinOrErr.takeError();
347     }
348     Bin = BinOrErr->getBinary();
349     BinaryForPath.insert(std::make_pair(Path, std::move(BinOrErr.get())));
350   } else {
351     Bin = I->second.getBinary();
352   }
353 
354   if (!Bin)
355     return static_cast<ObjectFile *>(nullptr);
356 
357   if (MachOUniversalBinary *UB = dyn_cast_or_null<MachOUniversalBinary>(Bin)) {
358     const auto &I = ObjectForUBPathAndArch.find(std::make_pair(Path, ArchName));
359     if (I != ObjectForUBPathAndArch.end()) {
360       return I->second.get();
361     }
362     Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
363         UB->getObjectForArch(ArchName);
364     if (!ObjOrErr) {
365       ObjectForUBPathAndArch.insert(std::make_pair(
366           std::make_pair(Path, ArchName), std::unique_ptr<ObjectFile>()));
367       return ObjOrErr.takeError();
368     }
369     ObjectFile *Res = ObjOrErr->get();
370     ObjectForUBPathAndArch.insert(std::make_pair(std::make_pair(Path, ArchName),
371                                                  std::move(ObjOrErr.get())));
372     return Res;
373   }
374   if (Bin->isObject()) {
375     return cast<ObjectFile>(Bin);
376   }
377   return errorCodeToError(object_error::arch_not_found);
378 }
379 
380 Expected<SymbolizableModule *>
381 LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName,
382                                       StringRef DWPName) {
383   const auto &I = Modules.find(ModuleName);
384   if (I != Modules.end()) {
385     return I->second.get();
386   }
387   std::string BinaryName = ModuleName;
388   std::string ArchName = Opts.DefaultArch;
389   size_t ColonPos = ModuleName.find_last_of(':');
390   // Verify that substring after colon form a valid arch name.
391   if (ColonPos != std::string::npos) {
392     std::string ArchStr = ModuleName.substr(ColonPos + 1);
393     if (Triple(ArchStr).getArch() != Triple::UnknownArch) {
394       BinaryName = ModuleName.substr(0, ColonPos);
395       ArchName = ArchStr;
396     }
397   }
398   auto ObjectsOrErr = getOrCreateObjectPair(BinaryName, ArchName);
399   if (!ObjectsOrErr) {
400     // Failed to find valid object file.
401     Modules.insert(
402         std::make_pair(ModuleName, std::unique_ptr<SymbolizableModule>()));
403     return ObjectsOrErr.takeError();
404   }
405   ObjectPair Objects = ObjectsOrErr.get();
406 
407   std::unique_ptr<DIContext> Context;
408   // If this is a COFF object containing PDB info, use a PDBContext to
409   // symbolize. Otherwise, use DWARF.
410   if (auto CoffObject = dyn_cast<COFFObjectFile>(Objects.first)) {
411     const codeview::DebugInfo *DebugInfo;
412     StringRef PDBFileName;
413     auto EC = CoffObject->getDebugPDBInfo(DebugInfo, PDBFileName);
414     if (!EC && DebugInfo != nullptr && !PDBFileName.empty()) {
415       using namespace pdb;
416       std::unique_ptr<IPDBSession> Session;
417       if (auto Err = loadDataForEXE(PDB_ReaderType::DIA,
418                                     Objects.first->getFileName(), Session)) {
419         Modules.insert(
420             std::make_pair(ModuleName, std::unique_ptr<SymbolizableModule>()));
421         // Return along the PDB filename to provide more context
422         return createFileError(PDBFileName, std::move(Err));
423       }
424       Context.reset(new PDBContext(*CoffObject, std::move(Session)));
425     }
426   }
427   if (!Context)
428     Context = DWARFContext::create(*Objects.second, nullptr,
429                                    DWARFContext::defaultErrorHandler, DWPName);
430   assert(Context);
431   auto InfoOrErr =
432       SymbolizableObjectFile::create(Objects.first, std::move(Context));
433   std::unique_ptr<SymbolizableModule> SymMod;
434   if (InfoOrErr)
435     SymMod = std::move(InfoOrErr.get());
436   auto InsertResult =
437       Modules.insert(std::make_pair(ModuleName, std::move(SymMod)));
438   assert(InsertResult.second);
439   if (auto EC = InfoOrErr.getError())
440     return errorCodeToError(EC);
441   return InsertResult.first->second.get();
442 }
443 
444 namespace {
445 
446 // Undo these various manglings for Win32 extern "C" functions:
447 // cdecl       - _foo
448 // stdcall     - _foo@12
449 // fastcall    - @foo@12
450 // vectorcall  - foo@@12
451 // These are all different linkage names for 'foo'.
452 StringRef demanglePE32ExternCFunc(StringRef SymbolName) {
453   // Remove any '_' or '@' prefix.
454   char Front = SymbolName.empty() ? '\0' : SymbolName[0];
455   if (Front == '_' || Front == '@')
456     SymbolName = SymbolName.drop_front();
457 
458   // Remove any '@[0-9]+' suffix.
459   if (Front != '?') {
460     size_t AtPos = SymbolName.rfind('@');
461     if (AtPos != StringRef::npos &&
462         std::all_of(SymbolName.begin() + AtPos + 1, SymbolName.end(),
463                     [](char C) { return C >= '0' && C <= '9'; })) {
464       SymbolName = SymbolName.substr(0, AtPos);
465     }
466   }
467 
468   // Remove any ending '@' for vectorcall.
469   if (SymbolName.endswith("@"))
470     SymbolName = SymbolName.drop_back();
471 
472   return SymbolName;
473 }
474 
475 } // end anonymous namespace
476 
477 std::string
478 LLVMSymbolizer::DemangleName(const std::string &Name,
479                              const SymbolizableModule *DbiModuleDescriptor) {
480   // We can spoil names of symbols with C linkage, so use an heuristic
481   // approach to check if the name should be demangled.
482   if (Name.substr(0, 2) == "_Z") {
483     int status = 0;
484     char *DemangledName = itaniumDemangle(Name.c_str(), nullptr, nullptr, &status);
485     if (status != 0)
486       return Name;
487     std::string Result = DemangledName;
488     free(DemangledName);
489     return Result;
490   }
491 
492 #if defined(_MSC_VER)
493   if (!Name.empty() && Name.front() == '?') {
494     // Only do MSVC C++ demangling on symbols starting with '?'.
495     char DemangledName[1024] = {0};
496     DWORD result = ::UnDecorateSymbolName(
497         Name.c_str(), DemangledName, 1023,
498         UNDNAME_NO_ACCESS_SPECIFIERS |       // Strip public, private, protected
499             UNDNAME_NO_ALLOCATION_LANGUAGE | // Strip __thiscall, __stdcall, etc
500             UNDNAME_NO_THROW_SIGNATURES |    // Strip throw() specifications
501             UNDNAME_NO_MEMBER_TYPE | // Strip virtual, static, etc specifiers
502             UNDNAME_NO_MS_KEYWORDS | // Strip all MS extension keywords
503             UNDNAME_NO_FUNCTION_RETURNS); // Strip function return types
504     return (result == 0) ? Name : std::string(DemangledName);
505   }
506 #endif
507   if (DbiModuleDescriptor && DbiModuleDescriptor->isWin32Module())
508     return std::string(demanglePE32ExternCFunc(Name));
509   return Name;
510 }
511 
512 } // namespace symbolize
513 } // namespace llvm
514