1//===-- LLVMSymbolize.cpp -------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Implementation for LLVM symbolization library.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/DebugInfo/Symbolize/Symbolize.h"
14
15#include "llvm/ADT/STLExtras.h"
16#include "llvm/DebugInfo/BTF/BTFContext.h"
17#include "llvm/DebugInfo/DWARF/DWARFContext.h"
18#include "llvm/DebugInfo/GSYM/GsymContext.h"
19#include "llvm/DebugInfo/GSYM/GsymReader.h"
20#include "llvm/DebugInfo/PDB/PDB.h"
21#include "llvm/DebugInfo/PDB/PDBContext.h"
22#include "llvm/DebugInfo/Symbolize/SymbolizableObjectFile.h"
23#include "llvm/Demangle/Demangle.h"
24#include "llvm/Object/Archive.h"
25#include "llvm/Object/BuildID.h"
26#include "llvm/Object/COFF.h"
27#include "llvm/Object/ELFObjectFile.h"
28#include "llvm/Object/MachO.h"
29#include "llvm/Object/MachOUniversal.h"
30#include "llvm/Support/CRC.h"
31#include "llvm/Support/Casting.h"
32#include "llvm/Support/DataExtractor.h"
33#include "llvm/Support/Errc.h"
34#include "llvm/Support/FileSystem.h"
35#include "llvm/Support/MemoryBuffer.h"
36#include "llvm/Support/Path.h"
37#include <cassert>
38#include <cstring>
39
40namespace llvm {
41namespace codeview {
42union DebugInfo;
43}
44namespace symbolize {
45
46LLVMSymbolizer::LLVMSymbolizer() = default;
47
48LLVMSymbolizer::LLVMSymbolizer(const Options &Opts)
49 : Opts(Opts),
50 BIDFetcher(std::make_unique<BuildIDFetcher>(args: Opts.DebugFileDirectory)) {}
51
52LLVMSymbolizer::~LLVMSymbolizer() = default;
53
54template <typename T>
55Expected<DILineInfo>
56LLVMSymbolizer::symbolizeCodeCommon(const T &ModuleSpecifier,
57 object::SectionedAddress ModuleOffset) {
58
59 auto InfoOrErr = getOrCreateModuleInfo(ModuleSpecifier);
60 if (!InfoOrErr)
61 return InfoOrErr.takeError();
62
63 SymbolizableModule *Info = *InfoOrErr;
64
65 // A null module means an error has already been reported. Return an empty
66 // result.
67 if (!Info)
68 return DILineInfo();
69
70 // If the user is giving us relative addresses, add the preferred base of the
71 // object to the offset before we do the query. It's what DIContext expects.
72 if (Opts.RelativeAddresses)
73 ModuleOffset.Address += Info->getModulePreferredBase();
74
75 DILineInfo LineInfo = Info->symbolizeCode(
76 ModuleOffset,
77 LineInfoSpecifier: DILineInfoSpecifier(Opts.PathStyle, Opts.PrintFunctions,
78 Opts.SkipLineZero),
79 UseSymbolTable: Opts.UseSymbolTable);
80 if (Opts.Demangle)
81 LineInfo.FunctionName = DemangleName(Name: LineInfo.FunctionName, DbiModuleDescriptor: Info);
82 return LineInfo;
83}
84
85Expected<DILineInfo>
86LLVMSymbolizer::symbolizeCode(const ObjectFile &Obj,
87 object::SectionedAddress ModuleOffset) {
88 return symbolizeCodeCommon(ModuleSpecifier: Obj, ModuleOffset);
89}
90
91Expected<DILineInfo>
92LLVMSymbolizer::symbolizeCode(StringRef ModuleName,
93 object::SectionedAddress ModuleOffset) {
94 return symbolizeCodeCommon(ModuleSpecifier: ModuleName, ModuleOffset);
95}
96
97Expected<DILineInfo>
98LLVMSymbolizer::symbolizeCode(ArrayRef<uint8_t> BuildID,
99 object::SectionedAddress ModuleOffset) {
100 return symbolizeCodeCommon(ModuleSpecifier: BuildID, ModuleOffset);
101}
102
103template <typename T>
104Expected<DIInliningInfo> LLVMSymbolizer::symbolizeInlinedCodeCommon(
105 const T &ModuleSpecifier, object::SectionedAddress ModuleOffset) {
106 auto InfoOrErr = getOrCreateModuleInfo(ModuleSpecifier);
107 if (!InfoOrErr)
108 return InfoOrErr.takeError();
109
110 SymbolizableModule *Info = *InfoOrErr;
111
112 // A null module means an error has already been reported. Return an empty
113 // result.
114 if (!Info)
115 return DIInliningInfo();
116
117 // If the user is giving us relative addresses, add the preferred base of the
118 // object to the offset before we do the query. It's what DIContext expects.
119 if (Opts.RelativeAddresses)
120 ModuleOffset.Address += Info->getModulePreferredBase();
121
122 DIInliningInfo InlinedContext = Info->symbolizeInlinedCode(
123 ModuleOffset,
124 LineInfoSpecifier: DILineInfoSpecifier(Opts.PathStyle, Opts.PrintFunctions,
125 Opts.SkipLineZero),
126 UseSymbolTable: Opts.UseSymbolTable);
127 if (Opts.Demangle) {
128 for (int i = 0, n = InlinedContext.getNumberOfFrames(); i < n; i++) {
129 auto *Frame = InlinedContext.getMutableFrame(Index: i);
130 Frame->FunctionName = DemangleName(Name: Frame->FunctionName, DbiModuleDescriptor: Info);
131 }
132 }
133 return InlinedContext;
134}
135
136Expected<DIInliningInfo>
137LLVMSymbolizer::symbolizeInlinedCode(const ObjectFile &Obj,
138 object::SectionedAddress ModuleOffset) {
139 return symbolizeInlinedCodeCommon(ModuleSpecifier: Obj, ModuleOffset);
140}
141
142Expected<DIInliningInfo>
143LLVMSymbolizer::symbolizeInlinedCode(StringRef ModuleName,
144 object::SectionedAddress ModuleOffset) {
145 return symbolizeInlinedCodeCommon(ModuleSpecifier: ModuleName, ModuleOffset);
146}
147
148Expected<DIInliningInfo>
149LLVMSymbolizer::symbolizeInlinedCode(ArrayRef<uint8_t> BuildID,
150 object::SectionedAddress ModuleOffset) {
151 return symbolizeInlinedCodeCommon(ModuleSpecifier: BuildID, ModuleOffset);
152}
153
154template <typename T>
155Expected<DIGlobal>
156LLVMSymbolizer::symbolizeDataCommon(const T &ModuleSpecifier,
157 object::SectionedAddress ModuleOffset) {
158
159 auto InfoOrErr = getOrCreateModuleInfo(ModuleSpecifier);
160 if (!InfoOrErr)
161 return InfoOrErr.takeError();
162
163 SymbolizableModule *Info = *InfoOrErr;
164 // A null module means an error has already been reported. Return an empty
165 // result.
166 if (!Info)
167 return DIGlobal();
168
169 // If the user is giving us relative addresses, add the preferred base of
170 // the object to the offset before we do the query. It's what DIContext
171 // expects.
172 if (Opts.RelativeAddresses)
173 ModuleOffset.Address += Info->getModulePreferredBase();
174
175 DIGlobal Global = Info->symbolizeData(ModuleOffset);
176 if (Opts.Demangle)
177 Global.Name = DemangleName(Name: Global.Name, DbiModuleDescriptor: Info);
178 return Global;
179}
180
181Expected<DIGlobal>
182LLVMSymbolizer::symbolizeData(const ObjectFile &Obj,
183 object::SectionedAddress ModuleOffset) {
184 return symbolizeDataCommon(ModuleSpecifier: Obj, ModuleOffset);
185}
186
187Expected<DIGlobal>
188LLVMSymbolizer::symbolizeData(StringRef ModuleName,
189 object::SectionedAddress ModuleOffset) {
190 return symbolizeDataCommon(ModuleSpecifier: ModuleName, ModuleOffset);
191}
192
193Expected<DIGlobal>
194LLVMSymbolizer::symbolizeData(ArrayRef<uint8_t> BuildID,
195 object::SectionedAddress ModuleOffset) {
196 return symbolizeDataCommon(ModuleSpecifier: BuildID, ModuleOffset);
197}
198
199template <typename T>
200Expected<std::vector<DILocal>>
201LLVMSymbolizer::symbolizeFrameCommon(const T &ModuleSpecifier,
202 object::SectionedAddress ModuleOffset) {
203 auto InfoOrErr = getOrCreateModuleInfo(ModuleSpecifier);
204 if (!InfoOrErr)
205 return InfoOrErr.takeError();
206
207 SymbolizableModule *Info = *InfoOrErr;
208 // A null module means an error has already been reported. Return an empty
209 // result.
210 if (!Info)
211 return std::vector<DILocal>();
212
213 // If the user is giving us relative addresses, add the preferred base of
214 // the object to the offset before we do the query. It's what DIContext
215 // expects.
216 if (Opts.RelativeAddresses)
217 ModuleOffset.Address += Info->getModulePreferredBase();
218
219 return Info->symbolizeFrame(ModuleOffset);
220}
221
222Expected<std::vector<DILocal>>
223LLVMSymbolizer::symbolizeFrame(const ObjectFile &Obj,
224 object::SectionedAddress ModuleOffset) {
225 return symbolizeFrameCommon(ModuleSpecifier: Obj, ModuleOffset);
226}
227
228Expected<std::vector<DILocal>>
229LLVMSymbolizer::symbolizeFrame(StringRef ModuleName,
230 object::SectionedAddress ModuleOffset) {
231 return symbolizeFrameCommon(ModuleSpecifier: ModuleName, ModuleOffset);
232}
233
234Expected<std::vector<DILocal>>
235LLVMSymbolizer::symbolizeFrame(ArrayRef<uint8_t> BuildID,
236 object::SectionedAddress ModuleOffset) {
237 return symbolizeFrameCommon(ModuleSpecifier: BuildID, ModuleOffset);
238}
239
240template <typename T>
241Expected<std::vector<DILineInfo>>
242LLVMSymbolizer::findSymbolCommon(const T &ModuleSpecifier, StringRef Symbol,
243 uint64_t Offset) {
244 auto InfoOrErr = getOrCreateModuleInfo(ModuleSpecifier);
245 if (!InfoOrErr)
246 return InfoOrErr.takeError();
247
248 SymbolizableModule *Info = *InfoOrErr;
249 std::vector<DILineInfo> Result;
250
251 // A null module means an error has already been reported. Return an empty
252 // result.
253 if (!Info)
254 return Result;
255
256 for (object::SectionedAddress A : Info->findSymbol(Symbol, Offset)) {
257 DILineInfo LineInfo = Info->symbolizeCode(
258 ModuleOffset: A, LineInfoSpecifier: DILineInfoSpecifier(Opts.PathStyle, Opts.PrintFunctions),
259 UseSymbolTable: Opts.UseSymbolTable);
260 if (LineInfo.FileName != DILineInfo::BadString) {
261 if (Opts.Demangle)
262 LineInfo.FunctionName = DemangleName(Name: LineInfo.FunctionName, DbiModuleDescriptor: Info);
263 Result.push_back(x: std::move(LineInfo));
264 }
265 }
266
267 return Result;
268}
269
270Expected<std::vector<DILineInfo>>
271LLVMSymbolizer::findSymbol(const ObjectFile &Obj, StringRef Symbol,
272 uint64_t Offset) {
273 return findSymbolCommon(ModuleSpecifier: Obj, Symbol, Offset);
274}
275
276Expected<std::vector<DILineInfo>>
277LLVMSymbolizer::findSymbol(StringRef ModuleName, StringRef Symbol,
278 uint64_t Offset) {
279 return findSymbolCommon(ModuleSpecifier: ModuleName, Symbol, Offset);
280}
281
282Expected<std::vector<DILineInfo>>
283LLVMSymbolizer::findSymbol(ArrayRef<uint8_t> BuildID, StringRef Symbol,
284 uint64_t Offset) {
285 return findSymbolCommon(ModuleSpecifier: BuildID, Symbol, Offset);
286}
287
288void LLVMSymbolizer::flush() {
289 ObjectFileCache.clear();
290 LRUBinaries.clear();
291 CacheSize = 0;
292 BinaryForPath.clear();
293 ObjectPairForPathArch.clear();
294 Modules.clear();
295 BuildIDPaths.clear();
296}
297
298namespace {
299
300// For Path="/path/to/foo" and Basename="foo" assume that debug info is in
301// /path/to/foo.dSYM/Contents/Resources/DWARF/foo.
302// For Path="/path/to/bar.dSYM" and Basename="foo" assume that debug info is in
303// /path/to/bar.dSYM/Contents/Resources/DWARF/foo.
304std::string getDarwinDWARFResourceForPath(const std::string &Path,
305 const std::string &Basename) {
306 SmallString<16> ResourceName = StringRef(Path);
307 if (sys::path::extension(path: Path) != ".dSYM") {
308 ResourceName += ".dSYM";
309 }
310 sys::path::append(path&: ResourceName, a: "Contents", b: "Resources", c: "DWARF");
311 sys::path::append(path&: ResourceName, a: Basename);
312 return std::string(ResourceName);
313}
314
315bool checkFileCRC(StringRef Path, uint32_t CRCHash) {
316 ErrorOr<std::unique_ptr<MemoryBuffer>> MB =
317 MemoryBuffer::getFileOrSTDIN(Filename: Path);
318 if (!MB)
319 return false;
320 return CRCHash == llvm::crc32(Data: arrayRefFromStringRef(Input: MB.get()->getBuffer()));
321}
322
323bool getGNUDebuglinkContents(const ObjectFile *Obj, std::string &DebugName,
324 uint32_t &CRCHash) {
325 if (!Obj)
326 return false;
327 for (const SectionRef &Section : Obj->sections()) {
328 StringRef Name;
329 consumeError(Err: Section.getName().moveInto(Value&: Name));
330
331 Name = Name.substr(Start: Name.find_first_not_of(Chars: "._"));
332 if (Name == "gnu_debuglink") {
333 Expected<StringRef> ContentsOrErr = Section.getContents();
334 if (!ContentsOrErr) {
335 consumeError(Err: ContentsOrErr.takeError());
336 return false;
337 }
338 DataExtractor DE(*ContentsOrErr, Obj->isLittleEndian());
339 uint64_t Offset = 0;
340 if (const char *DebugNameStr = DE.getCStr(OffsetPtr: &Offset)) {
341 // 4-byte align the offset.
342 Offset = (Offset + 3) & ~0x3;
343 if (DE.isValidOffsetForDataOfSize(offset: Offset, length: 4)) {
344 DebugName = DebugNameStr;
345 CRCHash = DE.getU32(offset_ptr: &Offset);
346 return true;
347 }
348 }
349 break;
350 }
351 }
352 return false;
353}
354
355bool darwinDsymMatchesBinary(const MachOObjectFile *DbgObj,
356 const MachOObjectFile *Obj) {
357 ArrayRef<uint8_t> dbg_uuid = DbgObj->getUuid();
358 ArrayRef<uint8_t> bin_uuid = Obj->getUuid();
359 if (dbg_uuid.empty() || bin_uuid.empty())
360 return false;
361 return !memcmp(s1: dbg_uuid.data(), s2: bin_uuid.data(), n: dbg_uuid.size());
362}
363
364} // end anonymous namespace
365
366ObjectFile *LLVMSymbolizer::lookUpDsymFile(const std::string &ExePath,
367 const MachOObjectFile *MachExeObj,
368 const std::string &ArchName) {
369 // On Darwin we may find DWARF in separate object file in
370 // resource directory.
371 std::vector<std::string> DsymPaths;
372 StringRef Filename = sys::path::filename(path: ExePath);
373 DsymPaths.push_back(
374 x: getDarwinDWARFResourceForPath(Path: ExePath, Basename: std::string(Filename)));
375 for (const auto &Path : Opts.DsymHints) {
376 DsymPaths.push_back(
377 x: getDarwinDWARFResourceForPath(Path, Basename: std::string(Filename)));
378 }
379 for (const auto &Path : DsymPaths) {
380 auto DbgObjOrErr = getOrCreateObject(InputPath: Path, DefaultArchName: ArchName);
381 if (!DbgObjOrErr) {
382 // Ignore errors, the file might not exist.
383 consumeError(Err: DbgObjOrErr.takeError());
384 continue;
385 }
386 ObjectFile *DbgObj = DbgObjOrErr.get();
387 if (!DbgObj)
388 continue;
389 const MachOObjectFile *MachDbgObj = dyn_cast<const MachOObjectFile>(Val: DbgObj);
390 if (!MachDbgObj)
391 continue;
392 if (darwinDsymMatchesBinary(DbgObj: MachDbgObj, Obj: MachExeObj))
393 return DbgObj;
394 }
395 return nullptr;
396}
397
398ObjectFile *LLVMSymbolizer::lookUpDebuglinkObject(const std::string &Path,
399 const ObjectFile *Obj,
400 const std::string &ArchName) {
401 std::string DebuglinkName;
402 uint32_t CRCHash;
403 std::string DebugBinaryPath;
404 if (!getGNUDebuglinkContents(Obj, DebugName&: DebuglinkName, CRCHash))
405 return nullptr;
406 if (!findDebugBinary(OrigPath: Path, DebuglinkName, CRCHash, Result&: DebugBinaryPath))
407 return nullptr;
408 auto DbgObjOrErr = getOrCreateObject(InputPath: DebugBinaryPath, DefaultArchName: ArchName);
409 if (!DbgObjOrErr) {
410 // Ignore errors, the file might not exist.
411 consumeError(Err: DbgObjOrErr.takeError());
412 return nullptr;
413 }
414 return DbgObjOrErr.get();
415}
416
417ObjectFile *LLVMSymbolizer::lookUpBuildIDObject(const std::string &Path,
418 const ELFObjectFileBase *Obj,
419 const std::string &ArchName) {
420 auto BuildID = getBuildID(Obj);
421 if (BuildID.size() < 2)
422 return nullptr;
423 std::string DebugBinaryPath;
424 if (!getOrFindDebugBinary(BuildID, Result&: DebugBinaryPath))
425 return nullptr;
426 auto DbgObjOrErr = getOrCreateObject(InputPath: DebugBinaryPath, DefaultArchName: ArchName);
427 if (!DbgObjOrErr) {
428 consumeError(Err: DbgObjOrErr.takeError());
429 return nullptr;
430 }
431 return DbgObjOrErr.get();
432}
433
434bool LLVMSymbolizer::findDebugBinary(const std::string &OrigPath,
435 const std::string &DebuglinkName,
436 uint32_t CRCHash, std::string &Result) {
437 SmallString<16> OrigDir(OrigPath);
438 llvm::sys::path::remove_filename(path&: OrigDir);
439 SmallString<16> DebugPath = OrigDir;
440 // Try relative/path/to/original_binary/debuglink_name
441 llvm::sys::path::append(path&: DebugPath, a: DebuglinkName);
442 if (checkFileCRC(Path: DebugPath, CRCHash)) {
443 Result = std::string(DebugPath);
444 return true;
445 }
446 // Try relative/path/to/original_binary/.debug/debuglink_name
447 DebugPath = OrigDir;
448 llvm::sys::path::append(path&: DebugPath, a: ".debug", b: DebuglinkName);
449 if (checkFileCRC(Path: DebugPath, CRCHash)) {
450 Result = std::string(DebugPath);
451 return true;
452 }
453 // Make the path absolute so that lookups will go to
454 // "/usr/lib/debug/full/path/to/debug", not
455 // "/usr/lib/debug/to/debug"
456 llvm::sys::fs::make_absolute(path&: OrigDir);
457 if (!Opts.FallbackDebugPath.empty()) {
458 // Try <FallbackDebugPath>/absolute/path/to/original_binary/debuglink_name
459 DebugPath = Opts.FallbackDebugPath;
460 } else {
461#if defined(__NetBSD__)
462 // Try /usr/libdata/debug/absolute/path/to/original_binary/debuglink_name
463 DebugPath = "/usr/libdata/debug";
464#else
465 // Try /usr/lib/debug/absolute/path/to/original_binary/debuglink_name
466 DebugPath = "/usr/lib/debug";
467#endif
468 }
469 llvm::sys::path::append(path&: DebugPath, a: llvm::sys::path::relative_path(path: OrigDir),
470 b: DebuglinkName);
471 if (checkFileCRC(Path: DebugPath, CRCHash)) {
472 Result = std::string(DebugPath);
473 return true;
474 }
475 return false;
476}
477
478static StringRef getBuildIDStr(ArrayRef<uint8_t> BuildID) {
479 return StringRef(reinterpret_cast<const char *>(BuildID.data()),
480 BuildID.size());
481}
482
483bool LLVMSymbolizer::getOrFindDebugBinary(const ArrayRef<uint8_t> BuildID,
484 std::string &Result) {
485 StringRef BuildIDStr = getBuildIDStr(BuildID);
486 auto I = BuildIDPaths.find(Key: BuildIDStr);
487 if (I != BuildIDPaths.end()) {
488 Result = I->second;
489 return true;
490 }
491 if (!BIDFetcher)
492 return false;
493 Expected<std::string> Path = BIDFetcher->fetch(BuildID);
494 if (Path) {
495 Result = *Path;
496 auto InsertResult = BuildIDPaths.insert(KV: {BuildIDStr, Result});
497 assert(InsertResult.second);
498 (void)InsertResult;
499 return true;
500 }
501 // Failure to fetch debuginfod is rarely an error and most users will not care
502 // why this failed.
503 consumeError(Err: Path.takeError());
504 return false;
505}
506
507std::string LLVMSymbolizer::lookUpGsymFile(const std::string &Path) {
508 if (Opts.DisableGsym)
509 return {};
510
511 auto CheckGsymFile = [](const llvm::StringRef &GsymPath) {
512 sys::fs::file_status Status;
513 std::error_code EC = llvm::sys::fs::status(path: GsymPath, result&: Status);
514 return !EC && !llvm::sys::fs::is_directory(status: Status);
515 };
516
517 // First, look beside the binary file
518 if (const auto GsymPath = Path + ".gsym"; CheckGsymFile(GsymPath))
519 return GsymPath;
520
521 // Then, look in the directories specified by GsymFileDirectory
522
523 for (const auto &Directory : Opts.GsymFileDirectory) {
524 SmallString<16> GsymPath = llvm::StringRef{Directory};
525 llvm::sys::path::append(path&: GsymPath,
526 a: llvm::sys::path::filename(path: Path) + ".gsym");
527
528 if (CheckGsymFile(GsymPath))
529 return static_cast<std::string>(GsymPath);
530 }
531
532 return {};
533}
534
535Expected<LLVMSymbolizer::ObjectPair>
536LLVMSymbolizer::getOrCreateObjectPair(const std::string &Path,
537 const std::string &ArchName) {
538 auto I = ObjectPairForPathArch.find(x: std::make_pair(x: Path, y: ArchName));
539 if (I != ObjectPairForPathArch.end()) {
540 recordAccess(Bin&: BinaryForPath.find(x: Path)->second);
541 return I->second;
542 }
543
544 auto ObjOrErr = getOrCreateObject(InputPath: Path, DefaultArchName: ArchName);
545 if (!ObjOrErr) {
546 ObjectPairForPathArch.emplace(args: std::make_pair(x: Path, y: ArchName),
547 args: ObjectPair(nullptr, nullptr));
548 return ObjOrErr.takeError();
549 }
550
551 ObjectFile *Obj = ObjOrErr.get();
552 assert(Obj != nullptr);
553 ObjectFile *DbgObj = nullptr;
554
555 if (auto MachObj = dyn_cast<const MachOObjectFile>(Val: Obj))
556 DbgObj = lookUpDsymFile(ExePath: Path, MachExeObj: MachObj, ArchName);
557 else if (auto ELFObj = dyn_cast<const ELFObjectFileBase>(Val: Obj))
558 DbgObj = lookUpBuildIDObject(Path, Obj: ELFObj, ArchName);
559 if (!DbgObj)
560 DbgObj = lookUpDebuglinkObject(Path, Obj, ArchName);
561 if (!DbgObj)
562 DbgObj = Obj;
563 ObjectPair Res = std::make_pair(x&: Obj, y&: DbgObj);
564 auto Pair =
565 ObjectPairForPathArch.emplace(args: std::make_pair(x: Path, y: ArchName), args&: Res);
566 std::string FullDbgObjKey;
567 auto It = ObjectToArchivePath.find(Val: DbgObj);
568 if (It != ObjectToArchivePath.end()) {
569 StringRef ArchivePath = It->second;
570 StringRef MemberName = sys::path::filename(path: DbgObj->getFileName());
571 FullDbgObjKey = (ArchivePath + "(" + MemberName + ")").str();
572 } else {
573 FullDbgObjKey = DbgObj->getFileName().str();
574 }
575 BinaryForPath.find(x: FullDbgObjKey)
576 ->second.pushEvictor(
577 Evictor: [this, I = Pair.first]() { ObjectPairForPathArch.erase(position: I); });
578 return Res;
579}
580
581Expected<object::Binary *>
582LLVMSymbolizer::loadOrGetBinary(const std::string &ArchivePathKey,
583 std::optional<StringRef> FullPathKey) {
584 // If no separate cache key is provided, use the archive path itself.
585 std::string FullPathKeyStr =
586 FullPathKey ? FullPathKey->str() : ArchivePathKey;
587 auto Pair = BinaryForPath.emplace(args&: FullPathKeyStr, args: OwningBinary<Binary>());
588 if (!Pair.second) {
589 recordAccess(Bin&: Pair.first->second);
590 return Pair.first->second->getBinary();
591 }
592
593 Expected<OwningBinary<Binary>> BinOrErr = createBinary(Path: ArchivePathKey);
594 if (!BinOrErr)
595 return BinOrErr.takeError();
596
597 CachedBinary &CachedBin = Pair.first->second;
598 CachedBin = std::move(*BinOrErr);
599 CachedBin.pushEvictor(Evictor: [this, I = Pair.first]() { BinaryForPath.erase(position: I); });
600 LRUBinaries.push_back(Node&: CachedBin);
601 CacheSize += CachedBin.size();
602 return CachedBin->getBinary();
603}
604
605Expected<ObjectFile *> LLVMSymbolizer::findOrCacheObject(
606 const ContainerCacheKey &Key,
607 llvm::function_ref<Expected<std::unique_ptr<ObjectFile>>()> Loader,
608 const std::string &PathForBinaryCache) {
609 auto It = ObjectFileCache.find(x: Key);
610 if (It != ObjectFileCache.end())
611 return It->second.get();
612
613 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = Loader();
614 if (!ObjOrErr) {
615 ObjectFileCache.emplace(args: Key, args: std::unique_ptr<ObjectFile>());
616 return ObjOrErr.takeError();
617 }
618
619 ObjectFile *Res = ObjOrErr->get();
620 auto NewEntry = ObjectFileCache.emplace(args: Key, args: std::move(*ObjOrErr));
621 auto CacheIter = BinaryForPath.find(x: PathForBinaryCache);
622 if (CacheIter != BinaryForPath.end())
623 CacheIter->second.pushEvictor(
624 Evictor: [this, Iter = NewEntry.first]() { ObjectFileCache.erase(position: Iter); });
625 return Res;
626}
627
628Expected<ObjectFile *> LLVMSymbolizer::getOrCreateObjectFromArchive(
629 StringRef ArchivePath, StringRef MemberName, StringRef ArchName,
630 StringRef FullPath) {
631 Expected<object::Binary *> BinOrErr =
632 loadOrGetBinary(ArchivePathKey: ArchivePath.str(), FullPathKey: FullPath);
633 if (!BinOrErr)
634 return BinOrErr.takeError();
635 object::Binary *Bin = *BinOrErr;
636
637 object::Archive *Archive = dyn_cast_if_present<object::Archive>(Val: Bin);
638 if (!Archive)
639 return createStringError(EC: std::errc::invalid_argument,
640 Fmt: "'%s' is not a valid archive",
641 Vals: ArchivePath.str().c_str());
642
643 Error Err = Error::success();
644 for (auto &Child : Archive->children(Err, /*SkipInternal=*/true)) {
645 Expected<StringRef> NameOrErr = Child.getName();
646 if (!NameOrErr) {
647 // TODO: Report this as a warning to the client. Consider adding a
648 // callback mechanism to report warning-level issues.
649 consumeError(Err: NameOrErr.takeError());
650 continue;
651 }
652 if (*NameOrErr == MemberName) {
653 Expected<std::unique_ptr<object::Binary>> MemberOrErr =
654 Child.getAsBinary();
655 if (!MemberOrErr) {
656 // TODO: Report this as a warning to the client. Consider adding a
657 // callback mechanism to report warning-level issues.
658 consumeError(Err: MemberOrErr.takeError());
659 continue;
660 }
661
662 std::unique_ptr<object::Binary> Binary = std::move(*MemberOrErr);
663 if (auto *Obj = dyn_cast<object::ObjectFile>(Val: Binary.get())) {
664 ObjectToArchivePath[Obj] = ArchivePath.str();
665 Triple::ArchType ObjArch = Obj->makeTriple().getArch();
666 Triple RequestedTriple;
667 RequestedTriple.setArch(Kind: Triple::getArchTypeForLLVMName(Str: ArchName));
668 if (ObjArch != RequestedTriple.getArch())
669 continue;
670
671 ContainerCacheKey CacheKey{.Path: ArchivePath.str(), .MemberName: MemberName.str(),
672 .ArchName: ArchName.str()};
673 Expected<ObjectFile *> Res = findOrCacheObject(
674 Key: CacheKey,
675 Loader: [O = std::unique_ptr<ObjectFile>(
676 Obj)]() mutable -> Expected<std::unique_ptr<ObjectFile>> {
677 return std::move(O);
678 },
679 PathForBinaryCache: ArchivePath.str());
680 Binary.release();
681 return Res;
682 }
683 }
684 }
685 if (Err)
686 return std::move(Err);
687 return createStringError(EC: std::errc::invalid_argument,
688 Fmt: "no matching member '%s' with arch '%s' in '%s'",
689 Vals: MemberName.str().c_str(), Vals: ArchName.str().c_str(),
690 Vals: ArchivePath.str().c_str());
691}
692
693Expected<ObjectFile *>
694LLVMSymbolizer::getOrCreateObject(const std::string &Path,
695 const std::string &ArchName) {
696 // First check for archive(member) format - more efficient to check closing
697 // paren first.
698 if (!Path.empty() && Path.back() == ')') {
699 size_t OpenParen = Path.rfind(c: '(', pos: Path.size() - 1);
700 if (OpenParen != std::string::npos) {
701 StringRef ArchivePath = StringRef(Path).substr(Start: 0, N: OpenParen);
702 StringRef MemberName =
703 StringRef(Path).substr(Start: OpenParen + 1, N: Path.size() - OpenParen - 2);
704 return getOrCreateObjectFromArchive(ArchivePath, MemberName, ArchName,
705 FullPath: Path);
706 }
707 }
708
709 Expected<object::Binary *> BinOrErr = loadOrGetBinary(ArchivePathKey: Path);
710 if (!BinOrErr)
711 return BinOrErr.takeError();
712 object::Binary *Bin = *BinOrErr;
713
714 if (MachOUniversalBinary *UB = dyn_cast_or_null<MachOUniversalBinary>(Val: Bin)) {
715 ContainerCacheKey CacheKey{.Path: Path, .MemberName: "", .ArchName: ArchName};
716 return findOrCacheObject(
717 Key: CacheKey,
718 Loader: [UB, ArchName]() -> Expected<std::unique_ptr<ObjectFile>> {
719 return UB->getMachOObjectForArch(ArchName);
720 },
721 PathForBinaryCache: Path);
722 }
723 if (Bin->isObject()) {
724 return cast<ObjectFile>(Val: Bin);
725 }
726 return errorCodeToError(EC: object_error::arch_not_found);
727}
728
729Expected<SymbolizableModule *>
730LLVMSymbolizer::createModuleInfo(const ObjectFile *Obj,
731 std::unique_ptr<DIContext> Context,
732 StringRef ModuleName) {
733 auto InfoOrErr = SymbolizableObjectFile::create(Obj, DICtx: std::move(Context),
734 UntagAddresses: Opts.UntagAddresses);
735 std::unique_ptr<SymbolizableModule> SymMod;
736 if (InfoOrErr)
737 SymMod = std::move(*InfoOrErr);
738 auto InsertResult = Modules.insert(
739 x: std::make_pair(x: std::string(ModuleName), y: std::move(SymMod)));
740 assert(InsertResult.second);
741 if (!InfoOrErr)
742 return InfoOrErr.takeError();
743 return InsertResult.first->second.get();
744}
745
746Expected<SymbolizableModule *>
747LLVMSymbolizer::getOrCreateModuleInfo(StringRef ModuleName) {
748 StringRef BinaryName = ModuleName;
749 StringRef ArchName = Opts.DefaultArch;
750 size_t ColonPos = ModuleName.find_last_of(C: ':');
751 // Verify that substring after colon form a valid arch name.
752 if (ColonPos != std::string::npos) {
753 StringRef ArchStr = ModuleName.substr(Start: ColonPos + 1);
754 if (Triple(ArchStr).getArch() != Triple::UnknownArch) {
755 BinaryName = ModuleName.substr(Start: 0, N: ColonPos);
756 ArchName = ArchStr;
757 }
758 }
759
760 auto I = Modules.find(x: ModuleName);
761 if (I != Modules.end()) {
762 recordAccess(Bin&: BinaryForPath.find(x: BinaryName)->second);
763 return I->second.get();
764 }
765
766 auto ObjectsOrErr =
767 getOrCreateObjectPair(Path: std::string{BinaryName}, ArchName: std::string{ArchName});
768 if (!ObjectsOrErr) {
769 // Failed to find valid object file.
770 Modules.emplace(args&: ModuleName, args: std::unique_ptr<SymbolizableModule>());
771 return ObjectsOrErr.takeError();
772 }
773 ObjectPair Objects = ObjectsOrErr.get();
774
775 std::unique_ptr<DIContext> Context;
776 pdb::PDB_ReaderType ReaderType =
777 Opts.UseDIA ? pdb::PDB_ReaderType::DIA : pdb::PDB_ReaderType::Native;
778 const auto *CoffObject = dyn_cast<COFFObjectFile>(Val: Objects.first);
779
780 // First, if the user specified a pdb file on the command line, use that.
781 if (CoffObject && !Opts.PDBName.empty()) {
782 using namespace pdb;
783 std::unique_ptr<IPDBSession> Session;
784 if (auto Err = loadDataForPDB(Type: ReaderType, Path: Opts.PDBName, Session)) {
785 Modules.emplace(args&: ModuleName, args: std::unique_ptr<SymbolizableModule>());
786 return createFileError(F: Opts.PDBName, E: std::move(Err));
787 }
788 Context.reset(p: new PDBContext(*CoffObject, std::move(Session)));
789 }
790
791 if (!Context) {
792 // If this is a COFF object containing PDB info and not containing DWARF
793 // section, use a PDBContext to symbolize. Otherwise, use DWARF.
794 // Create a DIContext to symbolize as follows:
795 // - If there is a GSYM file, create a GsymContext.
796 // - Otherwise, if this is a COFF object containing PDB info, create a
797 // PDBContext.
798 // - Otherwise, create a DWARFContext.
799 const auto GsymFile = lookUpGsymFile(Path: BinaryName.str());
800 if (!GsymFile.empty()) {
801 auto ReaderOrErr = gsym::GsymReader::openFile(Path: GsymFile);
802
803 if (ReaderOrErr)
804 Context = std::make_unique<gsym::GsymContext>(args: std::move(*ReaderOrErr));
805 }
806 }
807
808 if (!Context && CoffObject) {
809 const codeview::DebugInfo *DebugInfo;
810 StringRef PDBFileName;
811 auto EC = CoffObject->getDebugPDBInfo(Info&: DebugInfo, PDBFileName);
812 // Use DWARF if there're DWARF sections.
813 bool HasDwarf =
814 llvm::any_of(Range: Objects.first->sections(), P: [](SectionRef Section) -> bool {
815 if (Expected<StringRef> SectionName = Section.getName())
816 return SectionName.get() == ".debug_info";
817 return false;
818 });
819 if (!EC && !HasDwarf && DebugInfo != nullptr && !PDBFileName.empty()) {
820 using namespace pdb;
821 std::unique_ptr<IPDBSession> Session;
822
823 if (auto Err = loadDataForEXE(Type: ReaderType, Path: Objects.first->getFileName(),
824 Session)) {
825 Modules.emplace(args&: ModuleName, args: std::unique_ptr<SymbolizableModule>());
826 // Return along the PDB filename to provide more context
827 return createFileError(F: PDBFileName, E: std::move(Err));
828 }
829 Context.reset(p: new PDBContext(*CoffObject, std::move(Session)));
830 }
831 }
832 if (!Context)
833 Context = DWARFContext::create(
834 Obj: *Objects.second, RelocAction: DWARFContext::ProcessDebugRelocations::Process,
835 L: nullptr, DWPName: Opts.DWPName);
836 auto ModuleOrErr =
837 createModuleInfo(Obj: Objects.first, Context: std::move(Context), ModuleName);
838 if (ModuleOrErr) {
839 auto I = Modules.find(x: ModuleName);
840 BinaryForPath.find(x: BinaryName)->second.pushEvictor(Evictor: [this, I]() {
841 Modules.erase(position: I);
842 });
843 }
844 return ModuleOrErr;
845}
846
847// For BPF programs .BTF.ext section contains line numbers information,
848// use it if regular DWARF is not available (e.g. for stripped binary).
849static bool useBTFContext(const ObjectFile &Obj) {
850 return Obj.makeTriple().isBPF() && !Obj.hasDebugInfo() &&
851 BTFParser::hasBTFSections(Obj);
852}
853
854Expected<SymbolizableModule *>
855LLVMSymbolizer::getOrCreateModuleInfo(const ObjectFile &Obj) {
856 StringRef ObjName = Obj.getFileName();
857 auto I = Modules.find(x: ObjName);
858 if (I != Modules.end())
859 return I->second.get();
860
861 std::unique_ptr<DIContext> Context;
862 if (useBTFContext(Obj))
863 Context = BTFContext::create(Obj);
864 else
865 Context = DWARFContext::create(Obj);
866 // FIXME: handle COFF object with PDB info to use PDBContext
867 return createModuleInfo(Obj: &Obj, Context: std::move(Context), ModuleName: ObjName);
868}
869
870Expected<SymbolizableModule *>
871LLVMSymbolizer::getOrCreateModuleInfo(ArrayRef<uint8_t> BuildID) {
872 std::string Path;
873 if (!getOrFindDebugBinary(BuildID, Result&: Path)) {
874 return createStringError(EC: errc::no_such_file_or_directory,
875 S: "could not find build ID");
876 }
877 return getOrCreateModuleInfo(ModuleName: Path);
878}
879
880namespace {
881
882// Undo these various manglings for Win32 extern "C" functions:
883// cdecl - _foo
884// stdcall - _foo@12
885// fastcall - @foo@12
886// vectorcall - foo@@12
887// These are all different linkage names for 'foo'.
888StringRef demanglePE32ExternCFunc(StringRef SymbolName) {
889 char Front = SymbolName.empty() ? '\0' : SymbolName[0];
890
891 // Remove any '@[0-9]+' suffix.
892 bool HasAtNumSuffix = false;
893 if (Front != '?') {
894 size_t AtPos = SymbolName.rfind(C: '@');
895 if (AtPos != StringRef::npos &&
896 all_of(Range: drop_begin(RangeOrContainer&: SymbolName, N: AtPos + 1), P: isDigit)) {
897 SymbolName = SymbolName.substr(Start: 0, N: AtPos);
898 HasAtNumSuffix = true;
899 }
900 }
901
902 // Remove any ending '@' for vectorcall.
903 bool IsVectorCall = false;
904 if (HasAtNumSuffix && SymbolName.ends_with(Suffix: "@")) {
905 SymbolName = SymbolName.drop_back();
906 IsVectorCall = true;
907 }
908
909 // If not vectorcall, remove any '_' or '@' prefix.
910 if (!IsVectorCall && (Front == '_' || Front == '@'))
911 SymbolName = SymbolName.drop_front();
912
913 return SymbolName;
914}
915
916} // end anonymous namespace
917
918std::string
919LLVMSymbolizer::DemangleName(StringRef Name,
920 const SymbolizableModule *DbiModuleDescriptor) {
921 std::string Result;
922 if (nonMicrosoftDemangle(MangledName: Name, Result))
923 return Result;
924
925 if (Name.starts_with(Prefix: '?')) {
926 // Only do MSVC C++ demangling on symbols starting with '?'.
927 int status = 0;
928 char *DemangledName = microsoftDemangle(
929 mangled_name: Name, n_read: nullptr, status: &status,
930 Flags: MSDemangleFlags(MSDF_NoAccessSpecifier | MSDF_NoCallingConvention |
931 MSDF_NoMemberType | MSDF_NoReturnType));
932 if (status != 0)
933 return std::string{Name};
934 Result = DemangledName;
935 free(ptr: DemangledName);
936 return Result;
937 }
938
939 if (DbiModuleDescriptor && DbiModuleDescriptor->isWin32Module()) {
940 std::string DemangledCName(demanglePE32ExternCFunc(SymbolName: Name));
941 // On i386 Windows, the C name mangling for different calling conventions
942 // may also be applied on top of the Itanium or Rust name mangling.
943 if (nonMicrosoftDemangle(MangledName: DemangledCName, Result))
944 return Result;
945 return DemangledCName;
946 }
947 return std::string{Name};
948}
949
950void LLVMSymbolizer::recordAccess(CachedBinary &Bin) {
951 if (Bin->getBinary())
952 LRUBinaries.splice(I: LRUBinaries.end(), L2&: LRUBinaries, Node: Bin.getIterator());
953}
954
955void LLVMSymbolizer::pruneCache() {
956 // Evict the LRU binary until the max cache size is reached or there's <= 1
957 // item in the cache. The MRU binary is always kept to avoid thrashing if it's
958 // larger than the cache size.
959 while (CacheSize > Opts.MaxCacheSize && !LRUBinaries.empty() &&
960 std::next(x: LRUBinaries.begin()) != LRUBinaries.end()) {
961 CachedBinary &Bin = LRUBinaries.front();
962 CacheSize -= Bin.size();
963 LRUBinaries.pop_front();
964 Bin.evict();
965 }
966}
967
968void CachedBinary::pushEvictor(std::function<void()> NewEvictor) {
969 if (Evictor) {
970 this->Evictor = [OldEvictor = std::move(this->Evictor),
971 NewEvictor = std::move(NewEvictor)]() {
972 NewEvictor();
973 OldEvictor();
974 };
975 } else {
976 this->Evictor = std::move(NewEvictor);
977 }
978}
979
980} // namespace symbolize
981} // namespace llvm
982