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 Expected<DILineInfo>
symbolizeCodeCommon(SymbolizableModule * Info,object::SectionedAddress ModuleOffset)43 LLVMSymbolizer::symbolizeCodeCommon(SymbolizableModule *Info,
44 object::SectionedAddress ModuleOffset) {
45 // A null module means an error has already been reported. Return an empty
46 // result.
47 if (!Info)
48 return DILineInfo();
49
50 // If the user is giving us relative addresses, add the preferred base of the
51 // object to the offset before we do the query. It's what DIContext expects.
52 if (Opts.RelativeAddresses)
53 ModuleOffset.Address += Info->getModulePreferredBase();
54
55 DILineInfo LineInfo = Info->symbolizeCode(
56 ModuleOffset, DILineInfoSpecifier(Opts.PathStyle, Opts.PrintFunctions),
57 Opts.UseSymbolTable);
58 if (Opts.Demangle)
59 LineInfo.FunctionName = DemangleName(LineInfo.FunctionName, Info);
60 return LineInfo;
61 }
62
63 Expected<DILineInfo>
symbolizeCode(const ObjectFile & Obj,object::SectionedAddress ModuleOffset)64 LLVMSymbolizer::symbolizeCode(const ObjectFile &Obj,
65 object::SectionedAddress ModuleOffset) {
66 StringRef ModuleName = Obj.getFileName();
67 auto I = Modules.find(ModuleName);
68 if (I != Modules.end())
69 return symbolizeCodeCommon(I->second.get(), ModuleOffset);
70
71 std::unique_ptr<DIContext> Context = DWARFContext::create(Obj);
72 Expected<SymbolizableModule *> InfoOrErr =
73 createModuleInfo(&Obj, std::move(Context), ModuleName);
74 if (!InfoOrErr)
75 return InfoOrErr.takeError();
76 return symbolizeCodeCommon(*InfoOrErr, ModuleOffset);
77 }
78
79 Expected<DILineInfo>
symbolizeCode(const std::string & ModuleName,object::SectionedAddress ModuleOffset)80 LLVMSymbolizer::symbolizeCode(const std::string &ModuleName,
81 object::SectionedAddress ModuleOffset) {
82 Expected<SymbolizableModule *> InfoOrErr = getOrCreateModuleInfo(ModuleName);
83 if (!InfoOrErr)
84 return InfoOrErr.takeError();
85 return symbolizeCodeCommon(*InfoOrErr, ModuleOffset);
86 }
87
88 Expected<DIInliningInfo>
symbolizeInlinedCode(const std::string & ModuleName,object::SectionedAddress ModuleOffset)89 LLVMSymbolizer::symbolizeInlinedCode(const std::string &ModuleName,
90 object::SectionedAddress ModuleOffset) {
91 SymbolizableModule *Info;
92 if (auto InfoOrErr = getOrCreateModuleInfo(ModuleName))
93 Info = InfoOrErr.get();
94 else
95 return InfoOrErr.takeError();
96
97 // A null module means an error has already been reported. Return an empty
98 // result.
99 if (!Info)
100 return DIInliningInfo();
101
102 // If the user is giving us relative addresses, add the preferred base of the
103 // object to the offset before we do the query. It's what DIContext expects.
104 if (Opts.RelativeAddresses)
105 ModuleOffset.Address += Info->getModulePreferredBase();
106
107 DIInliningInfo InlinedContext = Info->symbolizeInlinedCode(
108 ModuleOffset, DILineInfoSpecifier(Opts.PathStyle, Opts.PrintFunctions),
109 Opts.UseSymbolTable);
110 if (Opts.Demangle) {
111 for (int i = 0, n = InlinedContext.getNumberOfFrames(); i < n; i++) {
112 auto *Frame = InlinedContext.getMutableFrame(i);
113 Frame->FunctionName = DemangleName(Frame->FunctionName, Info);
114 }
115 }
116 return InlinedContext;
117 }
118
119 Expected<DIGlobal>
symbolizeData(const std::string & ModuleName,object::SectionedAddress ModuleOffset)120 LLVMSymbolizer::symbolizeData(const std::string &ModuleName,
121 object::SectionedAddress ModuleOffset) {
122 SymbolizableModule *Info;
123 if (auto InfoOrErr = getOrCreateModuleInfo(ModuleName))
124 Info = InfoOrErr.get();
125 else
126 return InfoOrErr.takeError();
127
128 // A null module means an error has already been reported. Return an empty
129 // result.
130 if (!Info)
131 return DIGlobal();
132
133 // If the user is giving us relative addresses, add the preferred base of
134 // the object to the offset before we do the query. It's what DIContext
135 // expects.
136 if (Opts.RelativeAddresses)
137 ModuleOffset.Address += Info->getModulePreferredBase();
138
139 DIGlobal Global = Info->symbolizeData(ModuleOffset);
140 if (Opts.Demangle)
141 Global.Name = DemangleName(Global.Name, Info);
142 return Global;
143 }
144
145 Expected<std::vector<DILocal>>
symbolizeFrame(const std::string & ModuleName,object::SectionedAddress ModuleOffset)146 LLVMSymbolizer::symbolizeFrame(const std::string &ModuleName,
147 object::SectionedAddress ModuleOffset) {
148 SymbolizableModule *Info;
149 if (auto InfoOrErr = getOrCreateModuleInfo(ModuleName))
150 Info = InfoOrErr.get();
151 else
152 return InfoOrErr.takeError();
153
154 // A null module means an error has already been reported. Return an empty
155 // result.
156 if (!Info)
157 return std::vector<DILocal>();
158
159 // If the user is giving us relative addresses, add the preferred base of
160 // the object to the offset before we do the query. It's what DIContext
161 // expects.
162 if (Opts.RelativeAddresses)
163 ModuleOffset.Address += Info->getModulePreferredBase();
164
165 return Info->symbolizeFrame(ModuleOffset);
166 }
167
flush()168 void LLVMSymbolizer::flush() {
169 ObjectForUBPathAndArch.clear();
170 BinaryForPath.clear();
171 ObjectPairForPathArch.clear();
172 Modules.clear();
173 }
174
175 namespace {
176
177 // For Path="/path/to/foo" and Basename="foo" assume that debug info is in
178 // /path/to/foo.dSYM/Contents/Resources/DWARF/foo.
179 // For Path="/path/to/bar.dSYM" and Basename="foo" assume that debug info is in
180 // /path/to/bar.dSYM/Contents/Resources/DWARF/foo.
getDarwinDWARFResourceForPath(const std::string & Path,const std::string & Basename)181 std::string getDarwinDWARFResourceForPath(
182 const std::string &Path, const std::string &Basename) {
183 SmallString<16> ResourceName = StringRef(Path);
184 if (sys::path::extension(Path) != ".dSYM") {
185 ResourceName += ".dSYM";
186 }
187 sys::path::append(ResourceName, "Contents", "Resources", "DWARF");
188 sys::path::append(ResourceName, Basename);
189 return std::string(ResourceName.str());
190 }
191
checkFileCRC(StringRef Path,uint32_t CRCHash)192 bool checkFileCRC(StringRef Path, uint32_t CRCHash) {
193 ErrorOr<std::unique_ptr<MemoryBuffer>> MB =
194 MemoryBuffer::getFileOrSTDIN(Path);
195 if (!MB)
196 return false;
197 return CRCHash == llvm::crc32(arrayRefFromStringRef(MB.get()->getBuffer()));
198 }
199
findDebugBinary(const std::string & OrigPath,const std::string & DebuglinkName,uint32_t CRCHash,const std::string & FallbackDebugPath,std::string & Result)200 bool findDebugBinary(const std::string &OrigPath,
201 const std::string &DebuglinkName, uint32_t CRCHash,
202 const std::string &FallbackDebugPath,
203 std::string &Result) {
204 SmallString<16> OrigDir(OrigPath);
205 llvm::sys::path::remove_filename(OrigDir);
206 SmallString<16> DebugPath = OrigDir;
207 // Try relative/path/to/original_binary/debuglink_name
208 llvm::sys::path::append(DebugPath, DebuglinkName);
209 if (checkFileCRC(DebugPath, CRCHash)) {
210 Result = std::string(DebugPath.str());
211 return true;
212 }
213 // Try relative/path/to/original_binary/.debug/debuglink_name
214 DebugPath = OrigDir;
215 llvm::sys::path::append(DebugPath, ".debug", DebuglinkName);
216 if (checkFileCRC(DebugPath, CRCHash)) {
217 Result = std::string(DebugPath.str());
218 return true;
219 }
220 // Make the path absolute so that lookups will go to
221 // "/usr/lib/debug/full/path/to/debug", not
222 // "/usr/lib/debug/to/debug"
223 llvm::sys::fs::make_absolute(OrigDir);
224 if (!FallbackDebugPath.empty()) {
225 // Try <FallbackDebugPath>/absolute/path/to/original_binary/debuglink_name
226 DebugPath = FallbackDebugPath;
227 } else {
228 #if defined(__NetBSD__)
229 // Try /usr/libdata/debug/absolute/path/to/original_binary/debuglink_name
230 DebugPath = "/usr/libdata/debug";
231 #else
232 // Try /usr/lib/debug/absolute/path/to/original_binary/debuglink_name
233 DebugPath = "/usr/lib/debug";
234 #endif
235 }
236 llvm::sys::path::append(DebugPath, llvm::sys::path::relative_path(OrigDir),
237 DebuglinkName);
238 if (checkFileCRC(DebugPath, CRCHash)) {
239 Result = std::string(DebugPath.str());
240 return true;
241 }
242 return false;
243 }
244
getGNUDebuglinkContents(const ObjectFile * Obj,std::string & DebugName,uint32_t & CRCHash)245 bool getGNUDebuglinkContents(const ObjectFile *Obj, std::string &DebugName,
246 uint32_t &CRCHash) {
247 if (!Obj)
248 return false;
249 for (const SectionRef &Section : Obj->sections()) {
250 StringRef Name;
251 if (Expected<StringRef> NameOrErr = Section.getName())
252 Name = *NameOrErr;
253 else
254 consumeError(NameOrErr.takeError());
255
256 Name = Name.substr(Name.find_first_not_of("._"));
257 if (Name == "gnu_debuglink") {
258 Expected<StringRef> ContentsOrErr = Section.getContents();
259 if (!ContentsOrErr) {
260 consumeError(ContentsOrErr.takeError());
261 return false;
262 }
263 DataExtractor DE(*ContentsOrErr, Obj->isLittleEndian(), 0);
264 uint64_t Offset = 0;
265 if (const char *DebugNameStr = DE.getCStr(&Offset)) {
266 // 4-byte align the offset.
267 Offset = (Offset + 3) & ~0x3;
268 if (DE.isValidOffsetForDataOfSize(Offset, 4)) {
269 DebugName = DebugNameStr;
270 CRCHash = DE.getU32(&Offset);
271 return true;
272 }
273 }
274 break;
275 }
276 }
277 return false;
278 }
279
darwinDsymMatchesBinary(const MachOObjectFile * DbgObj,const MachOObjectFile * Obj)280 bool darwinDsymMatchesBinary(const MachOObjectFile *DbgObj,
281 const MachOObjectFile *Obj) {
282 ArrayRef<uint8_t> dbg_uuid = DbgObj->getUuid();
283 ArrayRef<uint8_t> bin_uuid = Obj->getUuid();
284 if (dbg_uuid.empty() || bin_uuid.empty())
285 return false;
286 return !memcmp(dbg_uuid.data(), bin_uuid.data(), dbg_uuid.size());
287 }
288
289 template <typename ELFT>
getBuildID(const ELFFile<ELFT> * Obj)290 Optional<ArrayRef<uint8_t>> getBuildID(const ELFFile<ELFT> *Obj) {
291 if (!Obj)
292 return {};
293 auto PhdrsOrErr = Obj->program_headers();
294 if (!PhdrsOrErr) {
295 consumeError(PhdrsOrErr.takeError());
296 return {};
297 }
298 for (const auto &P : *PhdrsOrErr) {
299 if (P.p_type != ELF::PT_NOTE)
300 continue;
301 Error Err = Error::success();
302 for (auto N : Obj->notes(P, Err))
303 if (N.getType() == ELF::NT_GNU_BUILD_ID && N.getName() == ELF::ELF_NOTE_GNU)
304 return N.getDesc();
305 consumeError(std::move(Err));
306 }
307 return {};
308 }
309
getBuildID(const ELFObjectFileBase * Obj)310 Optional<ArrayRef<uint8_t>> getBuildID(const ELFObjectFileBase *Obj) {
311 Optional<ArrayRef<uint8_t>> BuildID;
312 if (auto *O = dyn_cast<ELFObjectFile<ELF32LE>>(Obj))
313 BuildID = getBuildID(O->getELFFile());
314 else if (auto *O = dyn_cast<ELFObjectFile<ELF32BE>>(Obj))
315 BuildID = getBuildID(O->getELFFile());
316 else if (auto *O = dyn_cast<ELFObjectFile<ELF64LE>>(Obj))
317 BuildID = getBuildID(O->getELFFile());
318 else if (auto *O = dyn_cast<ELFObjectFile<ELF64BE>>(Obj))
319 BuildID = getBuildID(O->getELFFile());
320 else
321 llvm_unreachable("unsupported file format");
322 return BuildID;
323 }
324
findDebugBinary(const std::vector<std::string> & DebugFileDirectory,const ArrayRef<uint8_t> BuildID,std::string & Result)325 bool findDebugBinary(const std::vector<std::string> &DebugFileDirectory,
326 const ArrayRef<uint8_t> BuildID,
327 std::string &Result) {
328 auto getDebugPath = [&](StringRef Directory) {
329 SmallString<128> Path{Directory};
330 sys::path::append(Path, ".build-id",
331 llvm::toHex(BuildID[0], /*LowerCase=*/true),
332 llvm::toHex(BuildID.slice(1), /*LowerCase=*/true));
333 Path += ".debug";
334 return Path;
335 };
336 if (DebugFileDirectory.empty()) {
337 SmallString<128> Path = getDebugPath(
338 #if defined(__NetBSD__)
339 // Try /usr/libdata/debug/.build-id/../...
340 "/usr/libdata/debug"
341 #else
342 // Try /usr/lib/debug/.build-id/../...
343 "/usr/lib/debug"
344 #endif
345 );
346 if (llvm::sys::fs::exists(Path)) {
347 Result = std::string(Path.str());
348 return true;
349 }
350 } else {
351 for (const auto &Directory : DebugFileDirectory) {
352 // Try <debug-file-directory>/.build-id/../...
353 SmallString<128> Path = getDebugPath(Directory);
354 if (llvm::sys::fs::exists(Path)) {
355 Result = std::string(Path.str());
356 return true;
357 }
358 }
359 }
360 return false;
361 }
362
363 } // end anonymous namespace
364
lookUpDsymFile(const std::string & ExePath,const MachOObjectFile * MachExeObj,const std::string & ArchName)365 ObjectFile *LLVMSymbolizer::lookUpDsymFile(const std::string &ExePath,
366 const MachOObjectFile *MachExeObj, const std::string &ArchName) {
367 // On Darwin we may find DWARF in separate object file in
368 // resource directory.
369 std::vector<std::string> DsymPaths;
370 StringRef Filename = sys::path::filename(ExePath);
371 DsymPaths.push_back(
372 getDarwinDWARFResourceForPath(ExePath, std::string(Filename)));
373 for (const auto &Path : Opts.DsymHints) {
374 DsymPaths.push_back(
375 getDarwinDWARFResourceForPath(Path, std::string(Filename)));
376 }
377 for (const auto &Path : DsymPaths) {
378 auto DbgObjOrErr = getOrCreateObject(Path, ArchName);
379 if (!DbgObjOrErr) {
380 // Ignore errors, the file might not exist.
381 consumeError(DbgObjOrErr.takeError());
382 continue;
383 }
384 ObjectFile *DbgObj = DbgObjOrErr.get();
385 if (!DbgObj)
386 continue;
387 const MachOObjectFile *MachDbgObj = dyn_cast<const MachOObjectFile>(DbgObj);
388 if (!MachDbgObj)
389 continue;
390 if (darwinDsymMatchesBinary(MachDbgObj, MachExeObj))
391 return DbgObj;
392 }
393 return nullptr;
394 }
395
lookUpDebuglinkObject(const std::string & Path,const ObjectFile * Obj,const std::string & ArchName)396 ObjectFile *LLVMSymbolizer::lookUpDebuglinkObject(const std::string &Path,
397 const ObjectFile *Obj,
398 const std::string &ArchName) {
399 std::string DebuglinkName;
400 uint32_t CRCHash;
401 std::string DebugBinaryPath;
402 if (!getGNUDebuglinkContents(Obj, DebuglinkName, CRCHash))
403 return nullptr;
404 if (!findDebugBinary(Path, DebuglinkName, CRCHash, Opts.FallbackDebugPath,
405 DebugBinaryPath))
406 return nullptr;
407 auto DbgObjOrErr = getOrCreateObject(DebugBinaryPath, ArchName);
408 if (!DbgObjOrErr) {
409 // Ignore errors, the file might not exist.
410 consumeError(DbgObjOrErr.takeError());
411 return nullptr;
412 }
413 return DbgObjOrErr.get();
414 }
415
lookUpBuildIDObject(const std::string & Path,const ELFObjectFileBase * Obj,const std::string & ArchName)416 ObjectFile *LLVMSymbolizer::lookUpBuildIDObject(const std::string &Path,
417 const ELFObjectFileBase *Obj,
418 const std::string &ArchName) {
419 auto BuildID = getBuildID(Obj);
420 if (!BuildID)
421 return nullptr;
422 if (BuildID->size() < 2)
423 return nullptr;
424 std::string DebugBinaryPath;
425 if (!findDebugBinary(Opts.DebugFileDirectory, *BuildID, DebugBinaryPath))
426 return nullptr;
427 auto DbgObjOrErr = getOrCreateObject(DebugBinaryPath, ArchName);
428 if (!DbgObjOrErr) {
429 consumeError(DbgObjOrErr.takeError());
430 return nullptr;
431 }
432 return DbgObjOrErr.get();
433 }
434
435 Expected<LLVMSymbolizer::ObjectPair>
getOrCreateObjectPair(const std::string & Path,const std::string & ArchName)436 LLVMSymbolizer::getOrCreateObjectPair(const std::string &Path,
437 const std::string &ArchName) {
438 auto I = ObjectPairForPathArch.find(std::make_pair(Path, ArchName));
439 if (I != ObjectPairForPathArch.end())
440 return I->second;
441
442 auto ObjOrErr = getOrCreateObject(Path, ArchName);
443 if (!ObjOrErr) {
444 ObjectPairForPathArch.emplace(std::make_pair(Path, ArchName),
445 ObjectPair(nullptr, nullptr));
446 return ObjOrErr.takeError();
447 }
448
449 ObjectFile *Obj = ObjOrErr.get();
450 assert(Obj != nullptr);
451 ObjectFile *DbgObj = nullptr;
452
453 if (auto MachObj = dyn_cast<const MachOObjectFile>(Obj))
454 DbgObj = lookUpDsymFile(Path, MachObj, ArchName);
455 else if (auto ELFObj = dyn_cast<const ELFObjectFileBase>(Obj))
456 DbgObj = lookUpBuildIDObject(Path, ELFObj, ArchName);
457 if (!DbgObj)
458 DbgObj = lookUpDebuglinkObject(Path, Obj, ArchName);
459 if (!DbgObj)
460 DbgObj = Obj;
461 ObjectPair Res = std::make_pair(Obj, DbgObj);
462 ObjectPairForPathArch.emplace(std::make_pair(Path, ArchName), Res);
463 return Res;
464 }
465
466 Expected<ObjectFile *>
getOrCreateObject(const std::string & Path,const std::string & ArchName)467 LLVMSymbolizer::getOrCreateObject(const std::string &Path,
468 const std::string &ArchName) {
469 Binary *Bin;
470 auto Pair = BinaryForPath.emplace(Path, OwningBinary<Binary>());
471 if (!Pair.second) {
472 Bin = Pair.first->second.getBinary();
473 } else {
474 Expected<OwningBinary<Binary>> BinOrErr = createBinary(Path);
475 if (!BinOrErr)
476 return BinOrErr.takeError();
477 Pair.first->second = std::move(BinOrErr.get());
478 Bin = Pair.first->second.getBinary();
479 }
480
481 if (!Bin)
482 return static_cast<ObjectFile *>(nullptr);
483
484 if (MachOUniversalBinary *UB = dyn_cast_or_null<MachOUniversalBinary>(Bin)) {
485 auto I = ObjectForUBPathAndArch.find(std::make_pair(Path, ArchName));
486 if (I != ObjectForUBPathAndArch.end())
487 return I->second.get();
488
489 Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
490 UB->getMachOObjectForArch(ArchName);
491 if (!ObjOrErr) {
492 ObjectForUBPathAndArch.emplace(std::make_pair(Path, ArchName),
493 std::unique_ptr<ObjectFile>());
494 return ObjOrErr.takeError();
495 }
496 ObjectFile *Res = ObjOrErr->get();
497 ObjectForUBPathAndArch.emplace(std::make_pair(Path, ArchName),
498 std::move(ObjOrErr.get()));
499 return Res;
500 }
501 if (Bin->isObject()) {
502 return cast<ObjectFile>(Bin);
503 }
504 return errorCodeToError(object_error::arch_not_found);
505 }
506
507 Expected<SymbolizableModule *>
createModuleInfo(const ObjectFile * Obj,std::unique_ptr<DIContext> Context,StringRef ModuleName)508 LLVMSymbolizer::createModuleInfo(const ObjectFile *Obj,
509 std::unique_ptr<DIContext> Context,
510 StringRef ModuleName) {
511 auto InfoOrErr = SymbolizableObjectFile::create(Obj, std::move(Context),
512 Opts.UntagAddresses);
513 std::unique_ptr<SymbolizableModule> SymMod;
514 if (InfoOrErr)
515 SymMod = std::move(*InfoOrErr);
516 auto InsertResult = Modules.insert(
517 std::make_pair(std::string(ModuleName), std::move(SymMod)));
518 assert(InsertResult.second);
519 if (!InfoOrErr)
520 return InfoOrErr.takeError();
521 return InsertResult.first->second.get();
522 }
523
524 Expected<SymbolizableModule *>
getOrCreateModuleInfo(const std::string & ModuleName)525 LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) {
526 auto I = Modules.find(ModuleName);
527 if (I != Modules.end())
528 return I->second.get();
529
530 std::string BinaryName = ModuleName;
531 std::string ArchName = Opts.DefaultArch;
532 size_t ColonPos = ModuleName.find_last_of(':');
533 // Verify that substring after colon form a valid arch name.
534 if (ColonPos != std::string::npos) {
535 std::string ArchStr = ModuleName.substr(ColonPos + 1);
536 if (Triple(ArchStr).getArch() != Triple::UnknownArch) {
537 BinaryName = ModuleName.substr(0, ColonPos);
538 ArchName = ArchStr;
539 }
540 }
541 auto ObjectsOrErr = getOrCreateObjectPair(BinaryName, ArchName);
542 if (!ObjectsOrErr) {
543 // Failed to find valid object file.
544 Modules.emplace(ModuleName, std::unique_ptr<SymbolizableModule>());
545 return ObjectsOrErr.takeError();
546 }
547 ObjectPair Objects = ObjectsOrErr.get();
548
549 std::unique_ptr<DIContext> Context;
550 // If this is a COFF object containing PDB info, use a PDBContext to
551 // symbolize. Otherwise, use DWARF.
552 if (auto CoffObject = dyn_cast<COFFObjectFile>(Objects.first)) {
553 const codeview::DebugInfo *DebugInfo;
554 StringRef PDBFileName;
555 auto EC = CoffObject->getDebugPDBInfo(DebugInfo, PDBFileName);
556 if (!EC && DebugInfo != nullptr && !PDBFileName.empty()) {
557 using namespace pdb;
558 std::unique_ptr<IPDBSession> Session;
559
560 PDB_ReaderType ReaderType = PDB_ReaderType::Native;
561 #if LLVM_ENABLE_DIA_SDK
562 if (!Opts.UseNativePDBReader)
563 ReaderType = PDB_ReaderType::DIA;
564 #endif
565 if (auto Err = loadDataForEXE(ReaderType, Objects.first->getFileName(),
566 Session)) {
567 Modules.emplace(ModuleName, std::unique_ptr<SymbolizableModule>());
568 // Return along the PDB filename to provide more context
569 return createFileError(PDBFileName, std::move(Err));
570 }
571 Context.reset(new PDBContext(*CoffObject, std::move(Session)));
572 }
573 }
574 if (!Context)
575 Context = DWARFContext::create(*Objects.second, nullptr, Opts.DWPName);
576 return createModuleInfo(Objects.first, std::move(Context), ModuleName);
577 }
578
579 namespace {
580
581 // Undo these various manglings for Win32 extern "C" functions:
582 // cdecl - _foo
583 // stdcall - _foo@12
584 // fastcall - @foo@12
585 // vectorcall - foo@@12
586 // These are all different linkage names for 'foo'.
demanglePE32ExternCFunc(StringRef SymbolName)587 StringRef demanglePE32ExternCFunc(StringRef SymbolName) {
588 // Remove any '_' or '@' prefix.
589 char Front = SymbolName.empty() ? '\0' : SymbolName[0];
590 if (Front == '_' || Front == '@')
591 SymbolName = SymbolName.drop_front();
592
593 // Remove any '@[0-9]+' suffix.
594 if (Front != '?') {
595 size_t AtPos = SymbolName.rfind('@');
596 if (AtPos != StringRef::npos &&
597 std::all_of(SymbolName.begin() + AtPos + 1, SymbolName.end(),
598 [](char C) { return C >= '0' && C <= '9'; })) {
599 SymbolName = SymbolName.substr(0, AtPos);
600 }
601 }
602
603 // Remove any ending '@' for vectorcall.
604 if (SymbolName.endswith("@"))
605 SymbolName = SymbolName.drop_back();
606
607 return SymbolName;
608 }
609
610 } // end anonymous namespace
611
612 std::string
DemangleName(const std::string & Name,const SymbolizableModule * DbiModuleDescriptor)613 LLVMSymbolizer::DemangleName(const std::string &Name,
614 const SymbolizableModule *DbiModuleDescriptor) {
615 // We can spoil names of symbols with C linkage, so use an heuristic
616 // approach to check if the name should be demangled.
617 if (Name.substr(0, 2) == "_Z") {
618 int status = 0;
619 char *DemangledName = itaniumDemangle(Name.c_str(), nullptr, nullptr, &status);
620 if (status != 0)
621 return Name;
622 std::string Result = DemangledName;
623 free(DemangledName);
624 return Result;
625 }
626
627 if (!Name.empty() && Name.front() == '?') {
628 // Only do MSVC C++ demangling on symbols starting with '?'.
629 int status = 0;
630 char *DemangledName = microsoftDemangle(
631 Name.c_str(), nullptr, nullptr, nullptr, &status,
632 MSDemangleFlags(MSDF_NoAccessSpecifier | MSDF_NoCallingConvention |
633 MSDF_NoMemberType | MSDF_NoReturnType));
634 if (status != 0)
635 return Name;
636 std::string Result = DemangledName;
637 free(DemangledName);
638 return Result;
639 }
640
641 if (DbiModuleDescriptor && DbiModuleDescriptor->isWin32Module())
642 return std::string(demanglePE32ExternCFunc(Name));
643 return Name;
644 }
645
646 } // namespace symbolize
647 } // namespace llvm
648