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