1 //===- tools/dsymutil/MachODebugMapParser.cpp - Parse STABS debug maps ----===//
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
7 //===----------------------------------------------------------------------===//
9 #include "BinaryHolder.h"
11 #include "MachOUtils.h"
12 #include "RelocationMap.h"
13 #include "llvm/ADT/DenseSet.h"
14 #include "llvm/ADT/SmallSet.h"
15 #include "llvm/Object/MachO.h"
16 #include "llvm/Support/Chrono.h"
17 #include "llvm/Support/Path.h"
18 #include "llvm/Support/WithColor.h"
19 #include "llvm/Support/raw_ostream.h"
25 using namespace llvm::dsymutil
;
26 using namespace llvm::object
;
28 class MachODebugMapParser
{
30 MachODebugMapParser(BinaryHolder
&BinHolder
, StringRef BinaryPath
,
31 ArrayRef
<std::string
> Archs
,
32 ArrayRef
<std::string
> DSYMSearchPaths
,
33 StringRef PathPrefix
= "", StringRef VariantSuffix
= "",
35 : BinaryPath(std::string(BinaryPath
)), Archs(Archs
),
36 DSYMSearchPaths(DSYMSearchPaths
), PathPrefix(std::string(PathPrefix
)),
37 VariantSuffix(std::string(VariantSuffix
)), BinHolder(BinHolder
),
38 CurrentDebugMapObject(nullptr), SkipDebugMapObject(false) {}
40 /// Parses and returns the DebugMaps of the input binary. The binary contains
41 /// multiple maps in case it is a universal binary.
42 /// \returns an error in case the provided BinaryPath doesn't exist
43 /// or isn't of a supported type.
44 ErrorOr
<std::vector
<std::unique_ptr
<DebugMap
>>> parse();
46 /// Walk the symbol table and dump it.
49 using OSO
= std::pair
<llvm::StringRef
, uint64_t>;
52 std::string BinaryPath
;
53 SmallVector
<StringRef
, 1> Archs
;
54 SmallVector
<StringRef
, 1> DSYMSearchPaths
;
55 std::string PathPrefix
;
56 std::string VariantSuffix
;
58 /// Owns the MemoryBuffer for the main binary.
59 BinaryHolder
&BinHolder
;
60 /// Map of the binary symbol addresses.
61 StringMap
<uint64_t> MainBinarySymbolAddresses
;
62 StringRef MainBinaryStrings
;
63 /// The constructed DebugMap.
64 std::unique_ptr
<DebugMap
> Result
;
65 /// List of common symbols that need to be added to the debug map.
66 std::vector
<std::string
> CommonSymbols
;
68 /// Map of the currently processed object file symbol addresses.
69 StringMap
<std::optional
<uint64_t>> CurrentObjectAddresses
;
71 /// Lazily computed map of symbols aliased to the processed object file.
72 StringMap
<std::optional
<uint64_t>> CurrentObjectAliasMap
;
74 /// If CurrentObjectAliasMap has been computed for a given address.
75 SmallSet
<uint64_t, 4> SeenAliasValues
;
77 /// Element of the debug map corresponding to the current object file.
78 DebugMapObject
*CurrentDebugMapObject
;
80 /// Whether we need to skip the current debug map object.
81 bool SkipDebugMapObject
;
83 /// Holds function info while function scope processing.
84 const char *CurrentFunctionName
;
85 uint64_t CurrentFunctionAddress
;
87 std::unique_ptr
<DebugMap
> parseOneBinary(const MachOObjectFile
&MainBinary
,
88 StringRef BinaryPath
);
89 void handleStabDebugMap(
90 const MachOObjectFile
&MainBinary
,
91 std::function
<void(uint32_t, uint8_t, uint8_t, uint16_t, uint64_t)> F
);
94 switchToNewDebugMapObject(StringRef Filename
,
95 sys::TimePoint
<std::chrono::seconds
> Timestamp
);
97 switchToNewLibDebugMapObject(StringRef Filename
,
98 sys::TimePoint
<std::chrono::seconds
> Timestamp
);
99 void resetParserState();
100 uint64_t getMainBinarySymbolAddress(StringRef Name
);
101 std::vector
<StringRef
> getMainBinarySymbolNames(uint64_t Value
);
102 void loadMainBinarySymbols(const MachOObjectFile
&MainBinary
);
103 void loadCurrentObjectFileSymbols(const object::MachOObjectFile
&Obj
);
105 void handleStabOSOEntry(uint32_t StringIndex
, uint8_t Type
,
106 uint8_t SectionIndex
, uint16_t Flags
, uint64_t Value
,
107 llvm::DenseSet
<OSO
> &OSOs
,
108 llvm::SmallSet
<OSO
, 4> &Duplicates
);
109 void handleStabSymbolTableEntry(uint32_t StringIndex
, uint8_t Type
,
110 uint8_t SectionIndex
, uint16_t Flags
,
112 const llvm::SmallSet
<OSO
, 4> &Duplicates
);
114 template <typename STEType
>
115 void handleStabDebugMapEntry(
117 std::function
<void(uint32_t, uint8_t, uint8_t, uint16_t, uint64_t)> F
) {
118 F(STE
.n_strx
, STE
.n_type
, STE
.n_sect
, STE
.n_desc
, STE
.n_value
);
121 void addCommonSymbols();
123 /// Dump the symbol table output header.
124 void dumpSymTabHeader(raw_ostream
&OS
, StringRef Arch
);
126 /// Dump the contents of nlist entries.
127 void dumpSymTabEntry(raw_ostream
&OS
, uint64_t Index
, uint32_t StringIndex
,
128 uint8_t Type
, uint8_t SectionIndex
, uint16_t Flags
,
131 template <typename STEType
>
132 void dumpSymTabEntry(raw_ostream
&OS
, uint64_t Index
, const STEType
&STE
) {
133 dumpSymTabEntry(OS
, Index
, STE
.n_strx
, STE
.n_type
, STE
.n_sect
, STE
.n_desc
,
136 void dumpOneBinaryStab(const MachOObjectFile
&MainBinary
,
137 StringRef BinaryPath
);
139 void Warning(const Twine
&Msg
, StringRef File
= StringRef()) {
141 "The debug map must be initialized before calling this function");
142 WithColor::warning() << "("
143 << MachOUtils::getArchName(
144 Result
->getTriple().getArchName())
145 << ") " << File
<< " " << Msg
<< "\n";
149 } // anonymous namespace
151 /// Reset the parser state corresponding to the current object
152 /// file. This is to be called after an object file is finished
154 void MachODebugMapParser::resetParserState() {
155 CommonSymbols
.clear();
156 CurrentObjectAddresses
.clear();
157 CurrentObjectAliasMap
.clear();
158 SeenAliasValues
.clear();
159 CurrentDebugMapObject
= nullptr;
160 SkipDebugMapObject
= false;
163 /// Commons symbols won't show up in the symbol map but might need to be
164 /// relocated. We can add them to the symbol table ourselves by combining the
165 /// information in the object file (the symbol name) and the main binary (the
167 void MachODebugMapParser::addCommonSymbols() {
168 for (auto &CommonSymbol
: CommonSymbols
) {
169 uint64_t CommonAddr
= getMainBinarySymbolAddress(CommonSymbol
);
170 if (CommonAddr
== 0) {
171 // The main binary doesn't have an address for the given symbol.
174 if (!CurrentDebugMapObject
->addSymbol(CommonSymbol
,
175 std::nullopt
/*ObjectAddress*/,
176 CommonAddr
, 0 /*size*/)) {
177 // The symbol is already present.
183 /// Create a new DebugMapObject. This function resets the state of the
184 /// parser that was referring to the last object file and sets
185 /// everything up to add symbols to the new one.
186 void MachODebugMapParser::switchToNewDebugMapObject(
187 StringRef Filename
, sys::TimePoint
<std::chrono::seconds
> Timestamp
) {
191 SmallString
<80> Path(PathPrefix
);
192 sys::path::append(Path
, Filename
);
194 auto ObjectEntry
= BinHolder
.getObjectEntry(Path
, Timestamp
);
196 auto Err
= ObjectEntry
.takeError();
197 Warning("unable to open object file: " + toString(std::move(Err
)),
202 auto Object
= ObjectEntry
->getObjectAs
<MachOObjectFile
>(Result
->getTriple());
204 auto Err
= Object
.takeError();
205 Warning("unable to open object file: " + toString(std::move(Err
)),
210 CurrentDebugMapObject
=
211 &Result
->addDebugMapObject(Path
, Timestamp
, MachO::N_OSO
);
213 loadCurrentObjectFileSymbols(*Object
);
216 /// Create a new DebugMapObject of type MachO::N_LIB.
217 /// This function resets the state of the parser that was
218 /// referring to the last object file and sets everything
219 /// up to add symbols to the new one.
220 void MachODebugMapParser::switchToNewLibDebugMapObject(
221 StringRef Filename
, sys::TimePoint
<std::chrono::seconds
> Timestamp
) {
223 if (DSYMSearchPaths
.empty()) {
224 Warning("no dSYM search path was specified");
228 StringRef LeafName
= sys::path::filename(Filename
);
229 SmallString
<128> VariantLeafName
;
230 SmallString
<128> ProductName(LeafName
);
232 // For Framework.framework/Framework and -build-variant-suffix=_debug,
233 // look in the following order:
234 // 1) Framework.framework.dSYM/Contents/Resources/DWARF/Framework_debug
235 // 2) Framework.framework.dSYM/Contents/Resources/DWARF/Framework
237 // For libName.dylib and -build-variant-suffix=_debug,
238 // look in the following order:
239 // 1) libName.dylib.dSYM/Contents/Resources/DWARF/libName_debug.dylib
240 // 2) libName.dylib.dSYM/Contents/Resources/DWARF/libName.dylib
242 size_t libExt
= LeafName
.rfind(".dylib");
243 if (libExt
!= StringRef::npos
) {
244 if (!VariantSuffix
.empty()) {
245 VariantLeafName
.append(LeafName
.substr(0, libExt
));
246 VariantLeafName
.append(VariantSuffix
);
247 VariantLeafName
.append(".dylib");
250 // Expected to be a framework
251 ProductName
.append(".framework");
252 if (!VariantSuffix
.empty()) {
253 VariantLeafName
.append(LeafName
);
254 VariantLeafName
.append(VariantSuffix
);
258 for (auto DSYMSearchPath
: DSYMSearchPaths
) {
259 SmallString
<256> Path(DSYMSearchPath
);
260 SmallString
<256> FallbackPath(Path
);
262 SmallString
<256> DSYMPath(ProductName
);
263 DSYMPath
.append(".dSYM");
264 sys::path::append(DSYMPath
, "Contents", "Resources", "DWARF");
266 if (!VariantSuffix
.empty()) {
267 sys::path::append(Path
, DSYMPath
, VariantLeafName
);
268 sys::path::append(FallbackPath
, DSYMPath
, LeafName
);
270 sys::path::append(Path
, DSYMPath
, LeafName
);
273 auto ObjectEntry
= BinHolder
.getObjectEntry(Path
, Timestamp
);
275 auto Err
= ObjectEntry
.takeError();
276 Warning("unable to open object file: " + toString(std::move(Err
)),
278 if (!VariantSuffix
.empty()) {
279 ObjectEntry
= BinHolder
.getObjectEntry(FallbackPath
, Timestamp
);
281 auto Err
= ObjectEntry
.takeError();
282 Warning("unable to open object file: " + toString(std::move(Err
)),
286 Path
.assign(FallbackPath
);
293 ObjectEntry
->getObjectAs
<MachOObjectFile
>(Result
->getTriple());
295 auto Err
= Object
.takeError();
296 Warning("unable to open object file: " + toString(std::move(Err
)),
301 if (CurrentDebugMapObject
&&
302 CurrentDebugMapObject
->getType() == MachO::N_LIB
&&
303 CurrentDebugMapObject
->getObjectFilename() == Path
) {
310 CurrentDebugMapObject
=
311 &Result
->addDebugMapObject(Path
, Timestamp
, MachO::N_LIB
);
313 CurrentDebugMapObject
->setInstallName(Filename
);
315 SmallString
<256> RMPath(DSYMSearchPath
);
316 sys::path::append(RMPath
, ProductName
);
317 RMPath
.append(".dSYM");
318 StringRef ArchName
= Triple::getArchName(Result
->getTriple().getArch(),
319 Result
->getTriple().getSubArch());
320 sys::path::append(RMPath
, "Contents", "Resources", "Relocations", ArchName
);
321 sys::path::append(RMPath
, LeafName
);
322 RMPath
.append(".yml");
323 const auto &RelocMapPtrOrErr
=
324 RelocationMap::parseYAMLRelocationMap(RMPath
, PathPrefix
);
325 if (auto EC
= RelocMapPtrOrErr
.getError()) {
326 Warning("cannot parse relocation map file: " + EC
.message(),
330 CurrentDebugMapObject
->setRelocationMap(*RelocMapPtrOrErr
->get());
332 loadCurrentObjectFileSymbols(*Object
);
334 // Found and loaded new dSYM file
339 static std::string
getArchName(const object::MachOObjectFile
&Obj
) {
340 Triple T
= Obj
.getArchTriple();
341 return std::string(T
.getArchName());
344 void MachODebugMapParser::handleStabDebugMap(
345 const MachOObjectFile
&MainBinary
,
346 std::function
<void(uint32_t, uint8_t, uint8_t, uint16_t, uint64_t)> F
) {
347 for (const SymbolRef
&Symbol
: MainBinary
.symbols()) {
348 const DataRefImpl
&DRI
= Symbol
.getRawDataRefImpl();
349 if (MainBinary
.is64Bit())
350 handleStabDebugMapEntry(MainBinary
.getSymbol64TableEntry(DRI
), F
);
352 handleStabDebugMapEntry(MainBinary
.getSymbolTableEntry(DRI
), F
);
356 std::unique_ptr
<DebugMap
>
357 MachODebugMapParser::parseOneBinary(const MachOObjectFile
&MainBinary
,
358 StringRef BinaryPath
) {
359 Result
= std::make_unique
<DebugMap
>(MainBinary
.getArchTriple(), BinaryPath
,
360 MainBinary
.getUuid());
361 loadMainBinarySymbols(MainBinary
);
362 MainBinaryStrings
= MainBinary
.getStringTableData();
364 // Static archives can contain multiple object files with identical names, in
365 // which case the timestamp is used to disambiguate. However, if both are
366 // identical, there's no way to tell them apart. Detect this and skip
367 // duplicate debug map objects.
368 llvm::DenseSet
<OSO
> OSOs
;
369 llvm::SmallSet
<OSO
, 4> Duplicates
;
371 // Iterate over all the STABS to find duplicate OSO entries.
372 handleStabDebugMap(MainBinary
,
373 [&](uint32_t StringIndex
, uint8_t Type
,
374 uint8_t SectionIndex
, uint16_t Flags
, uint64_t Value
) {
375 handleStabOSOEntry(StringIndex
, Type
, SectionIndex
,
376 Flags
, Value
, OSOs
, Duplicates
);
379 // Print an informative warning with the duplicate object file name and time
381 for (const auto &OSO
: Duplicates
) {
383 llvm::raw_string_ostream
OS(Buffer
);
384 OS
<< sys::TimePoint
<std::chrono::seconds
>(sys::toTimePoint(OSO
.second
));
385 Warning("skipping debug map object with duplicate name and timestamp: " +
386 Buffer
+ Twine(" ") + Twine(OSO
.first
));
389 // Build the debug map by iterating over the STABS again but ignore the
390 // duplicate debug objects.
391 handleStabDebugMap(MainBinary
, [&](uint32_t StringIndex
, uint8_t Type
,
392 uint8_t SectionIndex
, uint16_t Flags
,
394 handleStabSymbolTableEntry(StringIndex
, Type
, SectionIndex
, Flags
, Value
,
399 return std::move(Result
);
402 // Table that maps Darwin's Mach-O stab constants to strings to allow printing.
403 // llvm-nm has very similar code, the strings used here are however slightly
404 // different and part of the interface of dsymutil (some project's build-systems
405 // parse the ouptut of dsymutil -s), thus they shouldn't be changed.
406 struct DarwinStabName
{
411 const struct DarwinStabName DarwinStabNames
[] = {{MachO::N_GSYM
, "N_GSYM"},
412 {MachO::N_FNAME
, "N_FNAME"},
413 {MachO::N_FUN
, "N_FUN"},
414 {MachO::N_STSYM
, "N_STSYM"},
415 {MachO::N_LCSYM
, "N_LCSYM"},
416 {MachO::N_BNSYM
, "N_BNSYM"},
417 {MachO::N_PC
, "N_PC"},
418 {MachO::N_AST
, "N_AST"},
419 {MachO::N_OPT
, "N_OPT"},
420 {MachO::N_RSYM
, "N_RSYM"},
421 {MachO::N_SLINE
, "N_SLINE"},
422 {MachO::N_ENSYM
, "N_ENSYM"},
423 {MachO::N_SSYM
, "N_SSYM"},
424 {MachO::N_SO
, "N_SO"},
425 {MachO::N_OSO
, "N_OSO"},
426 {MachO::N_LIB
, "N_LIB"},
427 {MachO::N_LSYM
, "N_LSYM"},
428 {MachO::N_BINCL
, "N_BINCL"},
429 {MachO::N_SOL
, "N_SOL"},
430 {MachO::N_PARAMS
, "N_PARAM"},
431 {MachO::N_VERSION
, "N_VERS"},
432 {MachO::N_OLEVEL
, "N_OLEV"},
433 {MachO::N_PSYM
, "N_PSYM"},
434 {MachO::N_EINCL
, "N_EINCL"},
435 {MachO::N_ENTRY
, "N_ENTRY"},
436 {MachO::N_LBRAC
, "N_LBRAC"},
437 {MachO::N_EXCL
, "N_EXCL"},
438 {MachO::N_RBRAC
, "N_RBRAC"},
439 {MachO::N_BCOMM
, "N_BCOMM"},
440 {MachO::N_ECOMM
, "N_ECOMM"},
441 {MachO::N_ECOML
, "N_ECOML"},
442 {MachO::N_LENG
, "N_LENG"},
445 static const char *getDarwinStabString(uint8_t NType
) {
446 for (unsigned i
= 0; DarwinStabNames
[i
].Name
; i
++) {
447 if (DarwinStabNames
[i
].NType
== NType
)
448 return DarwinStabNames
[i
].Name
;
453 void MachODebugMapParser::dumpSymTabHeader(raw_ostream
&OS
, StringRef Arch
) {
454 OS
<< "-----------------------------------"
455 "-----------------------------------\n";
456 OS
<< "Symbol table for: '" << BinaryPath
<< "' (" << Arch
.data() << ")\n";
457 OS
<< "-----------------------------------"
458 "-----------------------------------\n";
459 OS
<< "Index n_strx n_type n_sect n_desc n_value\n";
460 OS
<< "======== -------- ------------------ ------ ------ ----------------\n";
463 void MachODebugMapParser::dumpSymTabEntry(raw_ostream
&OS
, uint64_t Index
,
464 uint32_t StringIndex
, uint8_t Type
,
465 uint8_t SectionIndex
, uint16_t Flags
,
468 OS
<< '[' << format_decimal(Index
, 6)
471 << format_hex_no_prefix(StringIndex
, 8)
474 << format_hex_no_prefix(Type
, 2) << " (";
476 if (Type
& MachO::N_STAB
)
477 OS
<< left_justify(getDarwinStabString(Type
), 13);
479 if (Type
& MachO::N_PEXT
)
483 switch (Type
& MachO::N_TYPE
) {
484 case MachO::N_UNDF
: // 0x0 undefined, n_sect == NO_SECT
487 case MachO::N_ABS
: // 0x2 absolute, n_sect == NO_SECT
490 case MachO::N_SECT
: // 0xe defined in section number n_sect
493 case MachO::N_PBUD
: // 0xc prebound undefined (defined in a dylib)
496 case MachO::N_INDR
: // 0xa indirect
500 OS
<< format_hex_no_prefix(Type
, 2) << " ";
503 if (Type
& MachO::N_EXT
)
511 << format_hex_no_prefix(SectionIndex
, 2)
514 << format_hex_no_prefix(Flags
, 4)
517 << format_hex_no_prefix(Value
, 16);
519 const char *Name
= &MainBinaryStrings
.data()[StringIndex
];
521 OS
<< " '" << Name
<< "'";
526 void MachODebugMapParser::dumpOneBinaryStab(const MachOObjectFile
&MainBinary
,
527 StringRef BinaryPath
) {
528 loadMainBinarySymbols(MainBinary
);
529 MainBinaryStrings
= MainBinary
.getStringTableData();
530 raw_ostream
&OS(llvm::outs());
532 dumpSymTabHeader(OS
, getArchName(MainBinary
));
534 for (const SymbolRef
&Symbol
: MainBinary
.symbols()) {
535 const DataRefImpl
&DRI
= Symbol
.getRawDataRefImpl();
536 if (MainBinary
.is64Bit())
537 dumpSymTabEntry(OS
, Idx
, MainBinary
.getSymbol64TableEntry(DRI
));
539 dumpSymTabEntry(OS
, Idx
, MainBinary
.getSymbolTableEntry(DRI
));
547 static bool shouldLinkArch(SmallVectorImpl
<StringRef
> &Archs
, StringRef Arch
) {
548 if (Archs
.empty() || is_contained(Archs
, "all") || is_contained(Archs
, "*"))
551 if (Arch
.starts_with("arm") && Arch
!= "arm64" && is_contained(Archs
, "arm"))
554 SmallString
<16> ArchName
= Arch
;
555 if (Arch
.starts_with("thumb"))
556 ArchName
= ("arm" + Arch
.substr(5)).str();
558 return is_contained(Archs
, ArchName
);
561 bool MachODebugMapParser::dumpStab() {
562 auto ObjectEntry
= BinHolder
.getObjectEntry(BinaryPath
);
564 auto Err
= ObjectEntry
.takeError();
565 WithColor::error() << "cannot load '" << BinaryPath
566 << "': " << toString(std::move(Err
)) << '\n';
570 auto Objects
= ObjectEntry
->getObjectsAs
<MachOObjectFile
>();
572 auto Err
= Objects
.takeError();
573 WithColor::error() << "cannot get '" << BinaryPath
574 << "' as MachO file: " << toString(std::move(Err
))
579 for (const auto *Object
: *Objects
)
580 if (shouldLinkArch(Archs
, Object
->getArchTriple().getArchName()))
581 dumpOneBinaryStab(*Object
, BinaryPath
);
586 /// This main parsing routine tries to open the main binary and if
587 /// successful iterates over the STAB entries. The real parsing is
588 /// done in handleStabSymbolTableEntry.
589 ErrorOr
<std::vector
<std::unique_ptr
<DebugMap
>>> MachODebugMapParser::parse() {
590 auto ObjectEntry
= BinHolder
.getObjectEntry(BinaryPath
);
592 return errorToErrorCode(ObjectEntry
.takeError());
595 auto Objects
= ObjectEntry
->getObjectsAs
<MachOObjectFile
>();
597 return errorToErrorCode(Objects
.takeError());
600 std::vector
<std::unique_ptr
<DebugMap
>> Results
;
601 for (const auto *Object
: *Objects
)
602 if (shouldLinkArch(Archs
, Object
->getArchTriple().getArchName()))
603 Results
.push_back(parseOneBinary(*Object
, BinaryPath
));
605 return std::move(Results
);
608 void MachODebugMapParser::handleStabOSOEntry(
609 uint32_t StringIndex
, uint8_t Type
, uint8_t SectionIndex
, uint16_t Flags
,
610 uint64_t Value
, llvm::DenseSet
<OSO
> &OSOs
,
611 llvm::SmallSet
<OSO
, 4> &Duplicates
) {
612 if (Type
!= MachO::N_OSO
)
615 OSO
O(&MainBinaryStrings
.data()[StringIndex
], Value
);
616 if (!OSOs
.insert(O
).second
)
617 Duplicates
.insert(O
);
620 /// Interpret the STAB entries to fill the DebugMap.
621 void MachODebugMapParser::handleStabSymbolTableEntry(
622 uint32_t StringIndex
, uint8_t Type
, uint8_t SectionIndex
, uint16_t Flags
,
623 uint64_t Value
, const llvm::SmallSet
<OSO
, 4> &Duplicates
) {
624 if (!(Type
& MachO::N_STAB
))
627 const char *Name
= &MainBinaryStrings
.data()[StringIndex
];
629 // An N_LIB entry represents the start of a new library file description.
630 if (Type
== MachO::N_LIB
) {
631 switchToNewLibDebugMapObject(Name
, sys::toTimePoint(Value
));
635 // An N_OSO entry represents the start of a new object file description.
636 // If an N_LIB entry was present, this is parsed only if the library
637 // dSYM file could not be found.
638 if (Type
== MachO::N_OSO
) {
639 if (!CurrentDebugMapObject
||
640 CurrentDebugMapObject
->getType() != MachO::N_LIB
) {
641 if (Duplicates
.count(OSO(Name
, Value
))) {
642 SkipDebugMapObject
= true;
645 switchToNewDebugMapObject(Name
, sys::toTimePoint(Value
));
650 if (SkipDebugMapObject
)
653 if (Type
== MachO::N_AST
) {
654 SmallString
<80> Path(PathPrefix
);
655 sys::path::append(Path
, Name
);
656 Result
->addDebugMapObject(Path
, sys::toTimePoint(Value
), Type
);
660 // If the last N_OSO object file wasn't found, CurrentDebugMapObject will be
661 // null. Do not update anything until we find the next valid N_OSO entry.
662 if (!CurrentDebugMapObject
)
668 // This is a global variable. We need to query the main binary
669 // symbol table to find its address as it might not be in the
670 // debug map (for common symbols).
671 Value
= getMainBinarySymbolAddress(Name
);
674 // Functions are scopes in STABS. They have an end marker that
675 // contains the function size.
676 if (Name
[0] == '\0') {
678 Value
= CurrentFunctionAddress
;
679 Name
= CurrentFunctionName
;
682 CurrentFunctionName
= Name
;
683 CurrentFunctionAddress
= Value
;
692 auto ObjectSymIt
= CurrentObjectAddresses
.find(Name
);
694 // If the name of a (non-static) symbol is not in the current object, we
695 // check all its aliases from the main binary.
696 if (ObjectSymIt
== CurrentObjectAddresses
.end() && Type
!= MachO::N_STSYM
) {
697 if (SeenAliasValues
.count(Value
) == 0) {
698 auto Aliases
= getMainBinarySymbolNames(Value
);
699 for (const auto &Alias
: Aliases
) {
700 auto It
= CurrentObjectAddresses
.find(Alias
);
701 if (It
!= CurrentObjectAddresses
.end()) {
702 auto AliasValue
= It
->getValue();
703 for (const auto &Alias
: Aliases
)
704 CurrentObjectAliasMap
[Alias
] = AliasValue
;
708 SeenAliasValues
.insert(Value
);
711 auto AliasIt
= CurrentObjectAliasMap
.find(Name
);
712 if (AliasIt
!= CurrentObjectAliasMap
.end())
713 ObjectSymIt
= AliasIt
;
716 // ThinLTO adds a unique suffix to exported private symbols.
717 if (ObjectSymIt
== CurrentObjectAddresses
.end()) {
718 for (auto Iter
= CurrentObjectAddresses
.begin();
719 Iter
!= CurrentObjectAddresses
.end(); ++Iter
) {
720 llvm::StringRef SymbolName
= Iter
->getKey();
721 auto Pos
= SymbolName
.rfind(".llvm.");
722 if (Pos
!= llvm::StringRef::npos
&& SymbolName
.substr(0, Pos
) == Name
) {
729 if (ObjectSymIt
== CurrentObjectAddresses
.end()) {
730 Warning("could not find symbol '" + Twine(Name
) + "' in object file '" +
731 CurrentDebugMapObject
->getObjectFilename() + "'");
735 if (!CurrentDebugMapObject
->addSymbol(Name
, ObjectSymIt
->getValue(), Value
,
737 Warning(Twine("failed to insert symbol '") + Name
+ "' in the debug map.");
742 /// Load the current object file symbols into CurrentObjectAddresses.
743 void MachODebugMapParser::loadCurrentObjectFileSymbols(
744 const object::MachOObjectFile
&Obj
) {
745 CurrentObjectAddresses
.clear();
747 for (auto Sym
: Obj
.symbols()) {
748 uint64_t Addr
= cantFail(Sym
.getValue());
749 Expected
<StringRef
> Name
= Sym
.getName();
751 auto Err
= Name
.takeError();
752 Warning("failed to get symbol name: " + toString(std::move(Err
)),
756 // The value of some categories of symbols isn't meaningful. For
757 // example common symbols store their size in the value field, not
758 // their address. Absolute symbols have a fixed address that can
759 // conflict with standard symbols. These symbols (especially the
760 // common ones), might still be referenced by relocations. These
761 // relocations will use the symbol itself, and won't need an
762 // object file address. The object file address field is optional
763 // in the DebugMap, leave it unassigned for these symbols.
764 uint32_t Flags
= cantFail(Sym
.getFlags());
765 if (Flags
& SymbolRef::SF_Absolute
) {
766 CurrentObjectAddresses
[*Name
] = std::nullopt
;
767 } else if (Flags
& SymbolRef::SF_Common
) {
768 CurrentObjectAddresses
[*Name
] = std::nullopt
;
769 CommonSymbols
.push_back(std::string(*Name
));
771 CurrentObjectAddresses
[*Name
] = Addr
;
776 /// Lookup a symbol address in the main binary symbol table. The
777 /// parser only needs to query common symbols, thus not every symbol's
778 /// address is available through this function.
779 uint64_t MachODebugMapParser::getMainBinarySymbolAddress(StringRef Name
) {
780 auto Sym
= MainBinarySymbolAddresses
.find(Name
);
781 if (Sym
== MainBinarySymbolAddresses
.end())
786 /// Get all symbol names in the main binary for the given value.
787 std::vector
<StringRef
>
788 MachODebugMapParser::getMainBinarySymbolNames(uint64_t Value
) {
789 std::vector
<StringRef
> Names
;
790 for (const auto &Entry
: MainBinarySymbolAddresses
) {
791 if (Entry
.second
== Value
)
792 Names
.push_back(Entry
.first());
797 /// Load the interesting main binary symbols' addresses into
798 /// MainBinarySymbolAddresses.
799 void MachODebugMapParser::loadMainBinarySymbols(
800 const MachOObjectFile
&MainBinary
) {
801 section_iterator Section
= MainBinary
.section_end();
802 MainBinarySymbolAddresses
.clear();
803 for (const auto &Sym
: MainBinary
.symbols()) {
804 Expected
<SymbolRef::Type
> TypeOrErr
= Sym
.getType();
806 auto Err
= TypeOrErr
.takeError();
807 Warning("failed to get symbol type: " + toString(std::move(Err
)),
808 MainBinary
.getFileName());
811 SymbolRef::Type Type
= *TypeOrErr
;
812 // Skip undefined and STAB entries.
813 if ((Type
== SymbolRef::ST_Debug
) || (Type
== SymbolRef::ST_Unknown
))
815 // In theory, the only symbols of interest are the global variables. These
816 // are the only ones that need to be queried because the address of common
817 // data won't be described in the debug map. All other addresses should be
818 // fetched for the debug map. In reality, by playing with 'ld -r' and
819 // export lists, you can get symbols described as N_GSYM in the debug map,
820 // but associated with a local symbol. Gather all the symbols, but prefer
823 MainBinary
.getSymbolTableEntry(Sym
.getRawDataRefImpl()).n_type
;
824 bool Extern
= SymType
& (MachO::N_EXT
| MachO::N_PEXT
);
825 Expected
<section_iterator
> SectionOrErr
= Sym
.getSection();
827 auto Err
= TypeOrErr
.takeError();
828 Warning("failed to get symbol section: " + toString(std::move(Err
)),
829 MainBinary
.getFileName());
832 Section
= *SectionOrErr
;
833 if ((Section
== MainBinary
.section_end() || Section
->isText()) && !Extern
)
835 uint64_t Addr
= cantFail(Sym
.getValue());
836 Expected
<StringRef
> NameOrErr
= Sym
.getName();
838 auto Err
= NameOrErr
.takeError();
839 Warning("failed to get symbol name: " + toString(std::move(Err
)),
840 MainBinary
.getFileName());
843 StringRef Name
= *NameOrErr
;
844 if (Name
.size() == 0 || Name
[0] == '\0')
846 // Override only if the new key is global.
848 MainBinarySymbolAddresses
[Name
] = Addr
;
850 MainBinarySymbolAddresses
.try_emplace(Name
, Addr
);
856 llvm::ErrorOr
<std::vector
<std::unique_ptr
<DebugMap
>>>
857 parseDebugMap(BinaryHolder
&BinHolder
, StringRef InputFile
,
858 ArrayRef
<std::string
> Archs
,
859 ArrayRef
<std::string
> DSYMSearchPaths
, StringRef PrependPath
,
860 StringRef VariantSuffix
, bool Verbose
, bool InputIsYAML
) {
862 return DebugMap::parseYAMLDebugMap(BinHolder
, InputFile
, PrependPath
,
865 MachODebugMapParser
Parser(BinHolder
, InputFile
, Archs
, DSYMSearchPaths
,
866 PrependPath
, VariantSuffix
, Verbose
);
868 return Parser
.parse();
871 bool dumpStab(BinaryHolder
&BinHolder
, StringRef InputFile
,
872 ArrayRef
<std::string
> Archs
,
873 ArrayRef
<std::string
> DSYMSearchPaths
, StringRef PrependPath
,
874 StringRef VariantSuffix
) {
875 MachODebugMapParser
Parser(BinHolder
, InputFile
, Archs
, DSYMSearchPaths
,
876 PrependPath
, VariantSuffix
, false);
877 return Parser
.dumpStab();
879 } // namespace dsymutil