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 "llvm/ADT/Optional.h"
13 #include "llvm/Object/MachO.h"
14 #include "llvm/Support/Path.h"
15 #include "llvm/Support/WithColor.h"
16 #include "llvm/Support/raw_ostream.h"
21 using namespace llvm::dsymutil
;
22 using namespace llvm::object
;
24 class MachODebugMapParser
{
26 MachODebugMapParser(llvm::IntrusiveRefCntPtr
<llvm::vfs::FileSystem
> VFS
,
27 StringRef BinaryPath
, ArrayRef
<std::string
> Archs
,
28 StringRef PathPrefix
= "",
29 bool PaperTrailWarnings
= false, bool Verbose
= false)
30 : BinaryPath(std::string(BinaryPath
)), Archs(Archs
.begin(), Archs
.end()),
31 PathPrefix(std::string(PathPrefix
)),
32 PaperTrailWarnings(PaperTrailWarnings
), BinHolder(VFS
, Verbose
),
33 CurrentDebugMapObject(nullptr) {}
35 /// Parses and returns the DebugMaps of the input binary. The binary contains
36 /// multiple maps in case it is a universal binary.
37 /// \returns an error in case the provided BinaryPath doesn't exist
38 /// or isn't of a supported type.
39 ErrorOr
<std::vector
<std::unique_ptr
<DebugMap
>>> parse();
41 /// Walk the symbol table and dump it.
45 std::string BinaryPath
;
46 SmallVector
<StringRef
, 1> Archs
;
47 std::string PathPrefix
;
48 bool PaperTrailWarnings
;
50 /// Owns the MemoryBuffer for the main binary.
51 BinaryHolder BinHolder
;
52 /// Map of the binary symbol addresses.
53 StringMap
<uint64_t> MainBinarySymbolAddresses
;
54 StringRef MainBinaryStrings
;
55 /// The constructed DebugMap.
56 std::unique_ptr
<DebugMap
> Result
;
57 /// List of common symbols that need to be added to the debug map.
58 std::vector
<std::string
> CommonSymbols
;
60 /// Map of the currently processed object file symbol addresses.
61 StringMap
<Optional
<uint64_t>> CurrentObjectAddresses
;
62 /// Element of the debug map corresponding to the current object file.
63 DebugMapObject
*CurrentDebugMapObject
;
65 /// Holds function info while function scope processing.
66 const char *CurrentFunctionName
;
67 uint64_t CurrentFunctionAddress
;
69 std::unique_ptr
<DebugMap
> parseOneBinary(const MachOObjectFile
&MainBinary
,
70 StringRef BinaryPath
);
73 switchToNewDebugMapObject(StringRef Filename
,
74 sys::TimePoint
<std::chrono::seconds
> Timestamp
);
75 void resetParserState();
76 uint64_t getMainBinarySymbolAddress(StringRef Name
);
77 std::vector
<StringRef
> getMainBinarySymbolNames(uint64_t Value
);
78 void loadMainBinarySymbols(const MachOObjectFile
&MainBinary
);
79 void loadCurrentObjectFileSymbols(const object::MachOObjectFile
&Obj
);
80 void handleStabSymbolTableEntry(uint32_t StringIndex
, uint8_t Type
,
81 uint8_t SectionIndex
, uint16_t Flags
,
84 template <typename STEType
> void handleStabDebugMapEntry(const STEType
&STE
) {
85 handleStabSymbolTableEntry(STE
.n_strx
, STE
.n_type
, STE
.n_sect
, STE
.n_desc
,
89 void addCommonSymbols();
91 /// Dump the symbol table output header.
92 void dumpSymTabHeader(raw_ostream
&OS
, StringRef Arch
);
94 /// Dump the contents of nlist entries.
95 void dumpSymTabEntry(raw_ostream
&OS
, uint64_t Index
, uint32_t StringIndex
,
96 uint8_t Type
, uint8_t SectionIndex
, uint16_t Flags
,
99 template <typename STEType
>
100 void dumpSymTabEntry(raw_ostream
&OS
, uint64_t Index
, const STEType
&STE
) {
101 dumpSymTabEntry(OS
, Index
, STE
.n_strx
, STE
.n_type
, STE
.n_sect
, STE
.n_desc
,
104 void dumpOneBinaryStab(const MachOObjectFile
&MainBinary
,
105 StringRef BinaryPath
);
107 void Warning(const Twine
&Msg
, StringRef File
= StringRef()) {
108 WithColor::warning() << "("
109 << MachOUtils::getArchName(
110 Result
->getTriple().getArchName())
111 << ") " << File
<< " " << Msg
<< "\n";
113 if (PaperTrailWarnings
) {
115 Result
->addDebugMapObject(File
, sys::TimePoint
<std::chrono::seconds
>());
116 if (Result
->end() != Result
->begin())
117 (*--Result
->end())->addWarning(Msg
.str());
122 } // anonymous namespace
124 /// Reset the parser state corresponding to the current object
125 /// file. This is to be called after an object file is finished
127 void MachODebugMapParser::resetParserState() {
128 CommonSymbols
.clear();
129 CurrentObjectAddresses
.clear();
130 CurrentDebugMapObject
= nullptr;
133 /// Commons symbols won't show up in the symbol map but might need to be
134 /// relocated. We can add them to the symbol table ourselves by combining the
135 /// information in the object file (the symbol name) and the main binary (the
137 void MachODebugMapParser::addCommonSymbols() {
138 for (auto &CommonSymbol
: CommonSymbols
) {
139 uint64_t CommonAddr
= getMainBinarySymbolAddress(CommonSymbol
);
140 if (CommonAddr
== 0) {
141 // The main binary doesn't have an address for the given symbol.
144 if (!CurrentDebugMapObject
->addSymbol(CommonSymbol
, None
/*ObjectAddress*/,
145 CommonAddr
, 0 /*size*/)) {
146 // The symbol is already present.
152 /// Create a new DebugMapObject. This function resets the state of the
153 /// parser that was referring to the last object file and sets
154 /// everything up to add symbols to the new one.
155 void MachODebugMapParser::switchToNewDebugMapObject(
156 StringRef Filename
, sys::TimePoint
<std::chrono::seconds
> Timestamp
) {
160 SmallString
<80> Path(PathPrefix
);
161 sys::path::append(Path
, Filename
);
163 auto ObjectEntry
= BinHolder
.getObjectEntry(Path
, Timestamp
);
165 auto Err
= ObjectEntry
.takeError();
166 Warning("unable to open object file: " + toString(std::move(Err
)),
171 auto Object
= ObjectEntry
->getObjectAs
<MachOObjectFile
>(Result
->getTriple());
173 auto Err
= Object
.takeError();
174 Warning("unable to open object file: " + toString(std::move(Err
)),
179 CurrentDebugMapObject
=
180 &Result
->addDebugMapObject(Path
, Timestamp
, MachO::N_OSO
);
181 loadCurrentObjectFileSymbols(*Object
);
184 static std::string
getArchName(const object::MachOObjectFile
&Obj
) {
185 Triple T
= Obj
.getArchTriple();
186 return std::string(T
.getArchName());
189 std::unique_ptr
<DebugMap
>
190 MachODebugMapParser::parseOneBinary(const MachOObjectFile
&MainBinary
,
191 StringRef BinaryPath
) {
192 loadMainBinarySymbols(MainBinary
);
193 ArrayRef
<uint8_t> UUID
= MainBinary
.getUuid();
195 std::make_unique
<DebugMap
>(MainBinary
.getArchTriple(), BinaryPath
, UUID
);
196 MainBinaryStrings
= MainBinary
.getStringTableData();
197 for (const SymbolRef
&Symbol
: MainBinary
.symbols()) {
198 const DataRefImpl
&DRI
= Symbol
.getRawDataRefImpl();
199 if (MainBinary
.is64Bit())
200 handleStabDebugMapEntry(MainBinary
.getSymbol64TableEntry(DRI
));
202 handleStabDebugMapEntry(MainBinary
.getSymbolTableEntry(DRI
));
206 return std::move(Result
);
209 // Table that maps Darwin's Mach-O stab constants to strings to allow printing.
210 // llvm-nm has very similar code, the strings used here are however slightly
211 // different and part of the interface of dsymutil (some project's build-systems
212 // parse the ouptut of dsymutil -s), thus they shouldn't be changed.
213 struct DarwinStabName
{
218 const struct DarwinStabName DarwinStabNames
[] = {
219 {MachO::N_GSYM
, "N_GSYM"}, {MachO::N_FNAME
, "N_FNAME"},
220 {MachO::N_FUN
, "N_FUN"}, {MachO::N_STSYM
, "N_STSYM"},
221 {MachO::N_LCSYM
, "N_LCSYM"}, {MachO::N_BNSYM
, "N_BNSYM"},
222 {MachO::N_PC
, "N_PC"}, {MachO::N_AST
, "N_AST"},
223 {MachO::N_OPT
, "N_OPT"}, {MachO::N_RSYM
, "N_RSYM"},
224 {MachO::N_SLINE
, "N_SLINE"}, {MachO::N_ENSYM
, "N_ENSYM"},
225 {MachO::N_SSYM
, "N_SSYM"}, {MachO::N_SO
, "N_SO"},
226 {MachO::N_OSO
, "N_OSO"}, {MachO::N_LSYM
, "N_LSYM"},
227 {MachO::N_BINCL
, "N_BINCL"}, {MachO::N_SOL
, "N_SOL"},
228 {MachO::N_PARAMS
, "N_PARAM"}, {MachO::N_VERSION
, "N_VERS"},
229 {MachO::N_OLEVEL
, "N_OLEV"}, {MachO::N_PSYM
, "N_PSYM"},
230 {MachO::N_EINCL
, "N_EINCL"}, {MachO::N_ENTRY
, "N_ENTRY"},
231 {MachO::N_LBRAC
, "N_LBRAC"}, {MachO::N_EXCL
, "N_EXCL"},
232 {MachO::N_RBRAC
, "N_RBRAC"}, {MachO::N_BCOMM
, "N_BCOMM"},
233 {MachO::N_ECOMM
, "N_ECOMM"}, {MachO::N_ECOML
, "N_ECOML"},
234 {MachO::N_LENG
, "N_LENG"}, {0, nullptr}};
236 static const char *getDarwinStabString(uint8_t NType
) {
237 for (unsigned i
= 0; DarwinStabNames
[i
].Name
; i
++) {
238 if (DarwinStabNames
[i
].NType
== NType
)
239 return DarwinStabNames
[i
].Name
;
244 void MachODebugMapParser::dumpSymTabHeader(raw_ostream
&OS
, StringRef Arch
) {
245 OS
<< "-----------------------------------"
246 "-----------------------------------\n";
247 OS
<< "Symbol table for: '" << BinaryPath
<< "' (" << Arch
.data() << ")\n";
248 OS
<< "-----------------------------------"
249 "-----------------------------------\n";
250 OS
<< "Index n_strx n_type n_sect n_desc n_value\n";
251 OS
<< "======== -------- ------------------ ------ ------ ----------------\n";
254 void MachODebugMapParser::dumpSymTabEntry(raw_ostream
&OS
, uint64_t Index
,
255 uint32_t StringIndex
, uint8_t Type
,
256 uint8_t SectionIndex
, uint16_t Flags
,
259 OS
<< '[' << format_decimal(Index
, 6)
262 << format_hex_no_prefix(StringIndex
, 8)
265 << format_hex_no_prefix(Type
, 2) << " (";
267 if (Type
& MachO::N_STAB
)
268 OS
<< left_justify(getDarwinStabString(Type
), 13);
270 if (Type
& MachO::N_PEXT
)
274 switch (Type
& MachO::N_TYPE
) {
275 case MachO::N_UNDF
: // 0x0 undefined, n_sect == NO_SECT
278 case MachO::N_ABS
: // 0x2 absolute, n_sect == NO_SECT
281 case MachO::N_SECT
: // 0xe defined in section number n_sect
284 case MachO::N_PBUD
: // 0xc prebound undefined (defined in a dylib)
287 case MachO::N_INDR
: // 0xa indirect
291 OS
<< format_hex_no_prefix(Type
, 2) << " ";
294 if (Type
& MachO::N_EXT
)
302 << format_hex_no_prefix(SectionIndex
, 2)
305 << format_hex_no_prefix(Flags
, 4)
308 << format_hex_no_prefix(Value
, 16);
310 const char *Name
= &MainBinaryStrings
.data()[StringIndex
];
312 OS
<< " '" << Name
<< "'";
317 void MachODebugMapParser::dumpOneBinaryStab(const MachOObjectFile
&MainBinary
,
318 StringRef BinaryPath
) {
319 loadMainBinarySymbols(MainBinary
);
320 MainBinaryStrings
= MainBinary
.getStringTableData();
321 raw_ostream
&OS(llvm::outs());
323 dumpSymTabHeader(OS
, getArchName(MainBinary
));
325 for (const SymbolRef
&Symbol
: MainBinary
.symbols()) {
326 const DataRefImpl
&DRI
= Symbol
.getRawDataRefImpl();
327 if (MainBinary
.is64Bit())
328 dumpSymTabEntry(OS
, Idx
, MainBinary
.getSymbol64TableEntry(DRI
));
330 dumpSymTabEntry(OS
, Idx
, MainBinary
.getSymbolTableEntry(DRI
));
338 static bool shouldLinkArch(SmallVectorImpl
<StringRef
> &Archs
, StringRef Arch
) {
339 if (Archs
.empty() || is_contained(Archs
, "all") || is_contained(Archs
, "*"))
342 if (Arch
.startswith("arm") && Arch
!= "arm64" && is_contained(Archs
, "arm"))
345 SmallString
<16> ArchName
= Arch
;
346 if (Arch
.startswith("thumb"))
347 ArchName
= ("arm" + Arch
.substr(5)).str();
349 return is_contained(Archs
, ArchName
);
352 bool MachODebugMapParser::dumpStab() {
353 auto ObjectEntry
= BinHolder
.getObjectEntry(BinaryPath
);
355 auto Err
= ObjectEntry
.takeError();
356 WithColor::error() << "cannot load '" << BinaryPath
357 << "': " << toString(std::move(Err
)) << '\n';
361 auto Objects
= ObjectEntry
->getObjectsAs
<MachOObjectFile
>();
363 auto Err
= Objects
.takeError();
364 WithColor::error() << "cannot get '" << BinaryPath
365 << "' as MachO file: " << toString(std::move(Err
))
370 for (const auto *Object
: *Objects
)
371 if (shouldLinkArch(Archs
, Object
->getArchTriple().getArchName()))
372 dumpOneBinaryStab(*Object
, BinaryPath
);
377 /// This main parsing routine tries to open the main binary and if
378 /// successful iterates over the STAB entries. The real parsing is
379 /// done in handleStabSymbolTableEntry.
380 ErrorOr
<std::vector
<std::unique_ptr
<DebugMap
>>> MachODebugMapParser::parse() {
381 auto ObjectEntry
= BinHolder
.getObjectEntry(BinaryPath
);
383 return errorToErrorCode(ObjectEntry
.takeError());
386 auto Objects
= ObjectEntry
->getObjectsAs
<MachOObjectFile
>();
388 return errorToErrorCode(Objects
.takeError());
391 std::vector
<std::unique_ptr
<DebugMap
>> Results
;
392 for (const auto *Object
: *Objects
)
393 if (shouldLinkArch(Archs
, Object
->getArchTriple().getArchName()))
394 Results
.push_back(parseOneBinary(*Object
, BinaryPath
));
396 return std::move(Results
);
399 /// Interpret the STAB entries to fill the DebugMap.
400 void MachODebugMapParser::handleStabSymbolTableEntry(uint32_t StringIndex
,
402 uint8_t SectionIndex
,
405 if (!(Type
& MachO::N_STAB
))
408 const char *Name
= &MainBinaryStrings
.data()[StringIndex
];
410 // An N_OSO entry represents the start of a new object file description.
411 if (Type
== MachO::N_OSO
)
412 return switchToNewDebugMapObject(Name
, sys::toTimePoint(Value
));
414 if (Type
== MachO::N_AST
) {
415 SmallString
<80> Path(PathPrefix
);
416 sys::path::append(Path
, Name
);
417 Result
->addDebugMapObject(Path
, sys::toTimePoint(Value
), Type
);
421 // If the last N_OSO object file wasn't found, CurrentDebugMapObject will be
422 // null. Do not update anything until we find the next valid N_OSO entry.
423 if (!CurrentDebugMapObject
)
429 // This is a global variable. We need to query the main binary
430 // symbol table to find its address as it might not be in the
431 // debug map (for common symbols).
432 Value
= getMainBinarySymbolAddress(Name
);
435 // Functions are scopes in STABS. They have an end marker that
436 // contains the function size.
437 if (Name
[0] == '\0') {
439 Value
= CurrentFunctionAddress
;
440 Name
= CurrentFunctionName
;
443 CurrentFunctionName
= Name
;
444 CurrentFunctionAddress
= Value
;
453 auto ObjectSymIt
= CurrentObjectAddresses
.find(Name
);
455 // If the name of a (non-static) symbol is not in the current object, we
456 // check all its aliases from the main binary.
457 if (ObjectSymIt
== CurrentObjectAddresses
.end() && Type
!= MachO::N_STSYM
) {
458 for (const auto &Alias
: getMainBinarySymbolNames(Value
)) {
459 ObjectSymIt
= CurrentObjectAddresses
.find(Alias
);
460 if (ObjectSymIt
!= CurrentObjectAddresses
.end())
465 // ThinLTO adds a unique suffix to exported private symbols.
466 if (ObjectSymIt
== CurrentObjectAddresses
.end()) {
467 for (auto Iter
= CurrentObjectAddresses
.begin();
468 Iter
!= CurrentObjectAddresses
.end(); ++Iter
) {
469 llvm::StringRef SymbolName
= Iter
->getKey();
470 auto Pos
= SymbolName
.rfind(".llvm.");
471 if (Pos
!= llvm::StringRef::npos
&& SymbolName
.substr(0, Pos
) == Name
) {
478 if (ObjectSymIt
== CurrentObjectAddresses
.end()) {
479 Warning("could not find object file symbol for symbol " + Twine(Name
));
483 if (!CurrentDebugMapObject
->addSymbol(Name
, ObjectSymIt
->getValue(), Value
,
485 Warning(Twine("failed to insert symbol '") + Name
+ "' in the debug map.");
490 /// Load the current object file symbols into CurrentObjectAddresses.
491 void MachODebugMapParser::loadCurrentObjectFileSymbols(
492 const object::MachOObjectFile
&Obj
) {
493 CurrentObjectAddresses
.clear();
495 for (auto Sym
: Obj
.symbols()) {
496 uint64_t Addr
= cantFail(Sym
.getValue());
497 Expected
<StringRef
> Name
= Sym
.getName();
499 // TODO: Actually report errors helpfully.
500 consumeError(Name
.takeError());
503 // The value of some categories of symbols isn't meaningful. For
504 // example common symbols store their size in the value field, not
505 // their address. Absolute symbols have a fixed address that can
506 // conflict with standard symbols. These symbols (especially the
507 // common ones), might still be referenced by relocations. These
508 // relocations will use the symbol itself, and won't need an
509 // object file address. The object file address field is optional
510 // in the DebugMap, leave it unassigned for these symbols.
511 uint32_t Flags
= cantFail(Sym
.getFlags());
512 if (Flags
& SymbolRef::SF_Absolute
) {
513 CurrentObjectAddresses
[*Name
] = None
;
514 } else if (Flags
& SymbolRef::SF_Common
) {
515 CurrentObjectAddresses
[*Name
] = None
;
516 CommonSymbols
.push_back(std::string(*Name
));
518 CurrentObjectAddresses
[*Name
] = Addr
;
523 /// Lookup a symbol address in the main binary symbol table. The
524 /// parser only needs to query common symbols, thus not every symbol's
525 /// address is available through this function.
526 uint64_t MachODebugMapParser::getMainBinarySymbolAddress(StringRef Name
) {
527 auto Sym
= MainBinarySymbolAddresses
.find(Name
);
528 if (Sym
== MainBinarySymbolAddresses
.end())
533 /// Get all symbol names in the main binary for the given value.
534 std::vector
<StringRef
>
535 MachODebugMapParser::getMainBinarySymbolNames(uint64_t Value
) {
536 std::vector
<StringRef
> Names
;
537 for (const auto &Entry
: MainBinarySymbolAddresses
) {
538 if (Entry
.second
== Value
)
539 Names
.push_back(Entry
.first());
544 /// Load the interesting main binary symbols' addresses into
545 /// MainBinarySymbolAddresses.
546 void MachODebugMapParser::loadMainBinarySymbols(
547 const MachOObjectFile
&MainBinary
) {
548 section_iterator Section
= MainBinary
.section_end();
549 MainBinarySymbolAddresses
.clear();
550 for (const auto &Sym
: MainBinary
.symbols()) {
551 Expected
<SymbolRef::Type
> TypeOrErr
= Sym
.getType();
553 // TODO: Actually report errors helpfully.
554 consumeError(TypeOrErr
.takeError());
557 SymbolRef::Type Type
= *TypeOrErr
;
558 // Skip undefined and STAB entries.
559 if ((Type
== SymbolRef::ST_Debug
) || (Type
== SymbolRef::ST_Unknown
))
561 // In theory, the only symbols of interest are the global variables. These
562 // are the only ones that need to be queried because the address of common
563 // data won't be described in the debug map. All other addresses should be
564 // fetched for the debug map. In reality, by playing with 'ld -r' and
565 // export lists, you can get symbols described as N_GSYM in the debug map,
566 // but associated with a local symbol. Gather all the symbols, but prefer
569 MainBinary
.getSymbolTableEntry(Sym
.getRawDataRefImpl()).n_type
;
570 bool Extern
= SymType
& (MachO::N_EXT
| MachO::N_PEXT
);
571 Expected
<section_iterator
> SectionOrErr
= Sym
.getSection();
573 // TODO: Actually report errors helpfully.
574 consumeError(SectionOrErr
.takeError());
577 Section
= *SectionOrErr
;
578 if ((Section
== MainBinary
.section_end() || Section
->isText()) && !Extern
)
580 uint64_t Addr
= cantFail(Sym
.getValue());
581 Expected
<StringRef
> NameOrErr
= Sym
.getName();
583 // TODO: Actually report errors helpfully.
584 consumeError(NameOrErr
.takeError());
587 StringRef Name
= *NameOrErr
;
588 if (Name
.size() == 0 || Name
[0] == '\0')
590 // Override only if the new key is global.
592 MainBinarySymbolAddresses
[Name
] = Addr
;
594 MainBinarySymbolAddresses
.try_emplace(Name
, Addr
);
600 llvm::ErrorOr
<std::vector
<std::unique_ptr
<DebugMap
>>>
601 parseDebugMap(llvm::IntrusiveRefCntPtr
<llvm::vfs::FileSystem
> VFS
,
602 StringRef InputFile
, ArrayRef
<std::string
> Archs
,
603 StringRef PrependPath
, bool PaperTrailWarnings
, bool Verbose
,
606 return DebugMap::parseYAMLDebugMap(InputFile
, PrependPath
, Verbose
);
608 MachODebugMapParser
Parser(VFS
, InputFile
, Archs
, PrependPath
,
609 PaperTrailWarnings
, Verbose
);
610 return Parser
.parse();
613 bool dumpStab(llvm::IntrusiveRefCntPtr
<llvm::vfs::FileSystem
> VFS
,
614 StringRef InputFile
, ArrayRef
<std::string
> Archs
,
615 StringRef PrependPath
) {
616 MachODebugMapParser
Parser(VFS
, InputFile
, Archs
, PrependPath
, false);
617 return Parser
.dumpStab();
619 } // namespace dsymutil