1 //===-- llvm-nm.cpp - Symbol table dumping utility for llvm ---------------===//
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 // This program is a utility that works like traditional Unix "nm", that is, it
10 // prints out the names of symbols in a bitcode or object file, along with some
11 // information about each symbol.
13 // This "nm" supports many of the features of GNU "nm", including its different
16 //===----------------------------------------------------------------------===//
18 #include "llvm/ADT/StringSwitch.h"
19 #include "llvm/BinaryFormat/COFF.h"
20 #include "llvm/Demangle/Demangle.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/LLVMContext.h"
23 #include "llvm/Object/Archive.h"
24 #include "llvm/Object/COFF.h"
25 #include "llvm/Object/COFFImportFile.h"
26 #include "llvm/Object/ELFObjectFile.h"
27 #include "llvm/Object/IRObjectFile.h"
28 #include "llvm/Object/MachO.h"
29 #include "llvm/Object/MachOUniversal.h"
30 #include "llvm/Object/ObjectFile.h"
31 #include "llvm/Object/Wasm.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Support/FileSystem.h"
34 #include "llvm/Support/Format.h"
35 #include "llvm/Support/InitLLVM.h"
36 #include "llvm/Support/MemoryBuffer.h"
37 #include "llvm/Support/Program.h"
38 #include "llvm/Support/Signals.h"
39 #include "llvm/Support/TargetSelect.h"
40 #include "llvm/Support/WithColor.h"
41 #include "llvm/Support/raw_ostream.h"
45 using namespace object
;
48 enum OutputFormatTy
{ bsd
, sysv
, posix
, darwin
};
50 cl::OptionCategory
NMCat("llvm-nm Options");
52 cl::opt
<OutputFormatTy
> OutputFormat(
53 "format", cl::desc("Specify output format"),
54 cl::values(clEnumVal(bsd
, "BSD format"), clEnumVal(sysv
, "System V format"),
55 clEnumVal(posix
, "POSIX.2 format"),
56 clEnumVal(darwin
, "Darwin -m format")),
57 cl::init(bsd
), cl::cat(NMCat
));
58 cl::alias
OutputFormat2("f", cl::desc("Alias for --format"),
59 cl::aliasopt(OutputFormat
));
61 cl::list
<std::string
> InputFilenames(cl::Positional
, cl::desc("<input files>"),
64 cl::opt
<bool> UndefinedOnly("undefined-only",
65 cl::desc("Show only undefined symbols"),
67 cl::alias
UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
68 cl::aliasopt(UndefinedOnly
), cl::Grouping
);
70 cl::opt
<bool> DynamicSyms("dynamic",
71 cl::desc("Display the dynamic symbols instead "
72 "of normal symbols."),
74 cl::alias
DynamicSyms2("D", cl::desc("Alias for --dynamic"),
75 cl::aliasopt(DynamicSyms
), cl::Grouping
);
77 cl::opt
<bool> DefinedOnly("defined-only", cl::desc("Show only defined symbols"),
79 cl::alias
DefinedOnly2("U", cl::desc("Alias for --defined-only"),
80 cl::aliasopt(DefinedOnly
), cl::Grouping
);
82 cl::opt
<bool> ExternalOnly("extern-only",
83 cl::desc("Show only external symbols"),
84 cl::ZeroOrMore
, cl::cat(NMCat
));
85 cl::alias
ExternalOnly2("g", cl::desc("Alias for --extern-only"),
86 cl::aliasopt(ExternalOnly
), cl::Grouping
,
89 cl::opt
<bool> NoWeakSymbols("no-weak", cl::desc("Show only non-weak symbols"),
91 cl::alias
NoWeakSymbols2("W", cl::desc("Alias for --no-weak"),
92 cl::aliasopt(NoWeakSymbols
), cl::Grouping
);
94 cl::opt
<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"), cl::Grouping
,
96 cl::opt
<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"),
97 cl::Grouping
, cl::cat(NMCat
));
98 cl::alias
Portability("portability", cl::desc("Alias for --format=posix"),
99 cl::aliasopt(POSIXFormat
), cl::NotHidden
);
100 cl::opt
<bool> DarwinFormat("m", cl::desc("Alias for --format=darwin"),
101 cl::Grouping
, cl::cat(NMCat
));
103 static cl::list
<std::string
>
104 ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
105 cl::ZeroOrMore
, cl::cat(NMCat
));
106 bool ArchAll
= false;
108 cl::opt
<bool> PrintFileName(
110 cl::desc("Precede each symbol with the object file it came from"),
113 cl::alias
PrintFileNameA("A", cl::desc("Alias for --print-file-name"),
114 cl::aliasopt(PrintFileName
), cl::Grouping
);
115 cl::alias
PrintFileNameo("o", cl::desc("Alias for --print-file-name"),
116 cl::aliasopt(PrintFileName
), cl::Grouping
);
118 cl::opt
<bool> DebugSyms("debug-syms",
119 cl::desc("Show all symbols, even debugger only"),
121 cl::alias
DebugSymsa("a", cl::desc("Alias for --debug-syms"),
122 cl::aliasopt(DebugSyms
), cl::Grouping
);
124 cl::opt
<bool> NumericSort("numeric-sort", cl::desc("Sort symbols by address"),
126 cl::alias
NumericSortn("n", cl::desc("Alias for --numeric-sort"),
127 cl::aliasopt(NumericSort
), cl::Grouping
);
128 cl::alias
NumericSortv("v", cl::desc("Alias for --numeric-sort"),
129 cl::aliasopt(NumericSort
), cl::Grouping
);
131 cl::opt
<bool> NoSort("no-sort", cl::desc("Show symbols in order encountered"),
133 cl::alias
NoSortp("p", cl::desc("Alias for --no-sort"), cl::aliasopt(NoSort
),
136 cl::opt
<bool> Demangle("demangle", cl::ZeroOrMore
,
137 cl::desc("Demangle C++ symbol names"), cl::cat(NMCat
));
138 cl::alias
DemangleC("C", cl::desc("Alias for --demangle"),
139 cl::aliasopt(Demangle
), cl::Grouping
);
140 cl::opt
<bool> NoDemangle("no-demangle", cl::init(false), cl::ZeroOrMore
,
141 cl::desc("Don't demangle symbol names"),
144 cl::opt
<bool> ReverseSort("reverse-sort", cl::desc("Sort in reverse order"),
146 cl::alias
ReverseSortr("r", cl::desc("Alias for --reverse-sort"),
147 cl::aliasopt(ReverseSort
), cl::Grouping
);
149 cl::opt
<bool> PrintSize("print-size",
150 cl::desc("Show symbol size as well as address"),
152 cl::alias
PrintSizeS("S", cl::desc("Alias for --print-size"),
153 cl::aliasopt(PrintSize
), cl::Grouping
);
154 bool MachOPrintSizeWarning
= false;
156 cl::opt
<bool> SizeSort("size-sort", cl::desc("Sort symbols by size"),
159 cl::opt
<bool> WithoutAliases("without-aliases", cl::Hidden
,
160 cl::desc("Exclude aliases from output"),
163 cl::opt
<bool> ArchiveMap("print-armap", cl::desc("Print the archive map"),
165 cl::alias
ArchiveMaps("M", cl::desc("Alias for --print-armap"),
166 cl::aliasopt(ArchiveMap
), cl::Grouping
);
168 enum Radix
{ d
, o
, x
};
170 AddressRadix("radix", cl::desc("Radix (o/d/x) for printing symbol Values"),
171 cl::values(clEnumVal(d
, "decimal"), clEnumVal(o
, "octal"),
172 clEnumVal(x
, "hexadecimal")),
173 cl::init(x
), cl::cat(NMCat
));
174 cl::alias
RadixAlias("t", cl::desc("Alias for --radix"),
175 cl::aliasopt(AddressRadix
));
177 cl::opt
<bool> JustSymbolName("just-symbol-name",
178 cl::desc("Print just the symbol's name"),
180 cl::alias
JustSymbolNames("j", cl::desc("Alias for --just-symbol-name"),
181 cl::aliasopt(JustSymbolName
), cl::Grouping
);
183 cl::opt
<bool> SpecialSyms("special-syms",
184 cl::desc("No-op. Used for GNU compatibility only"));
186 cl::list
<std::string
> SegSect("s", cl::multi_val(2), cl::ZeroOrMore
,
187 cl::value_desc("segment section"), cl::Hidden
,
188 cl::desc("Dump only symbols from this segment "
189 "and section name, Mach-O only"),
192 cl::opt
<bool> FormatMachOasHex("x",
193 cl::desc("Print symbol entry in hex, "
195 cl::Grouping
, cl::cat(NMCat
));
196 cl::opt
<bool> AddDyldInfo("add-dyldinfo",
197 cl::desc("Add symbols from the dyldinfo not already "
198 "in the symbol table, Mach-O only"),
200 cl::opt
<bool> NoDyldInfo("no-dyldinfo",
201 cl::desc("Don't add any symbols from the dyldinfo, "
204 cl::opt
<bool> DyldInfoOnly("dyldinfo-only",
205 cl::desc("Show only symbols from the dyldinfo, "
209 cl::opt
<bool> NoLLVMBitcode("no-llvm-bc",
210 cl::desc("Disable LLVM bitcode reader"),
213 cl::extrahelp
HelpResponse("\nPass @FILE as argument to read options from FILE.\n");
215 bool PrintAddress
= true;
217 bool MultipleFiles
= false;
219 bool HadError
= false;
221 std::string ToolName
;
222 } // anonymous namespace
224 static void error(Twine Message
, Twine Path
= Twine()) {
226 WithColor::error(errs(), ToolName
) << Path
<< ": " << Message
<< ".\n";
229 static bool error(std::error_code EC
, Twine Path
= Twine()) {
231 error(EC
.message(), Path
);
237 // This version of error() prints the archive name and member name, for example:
238 // "libx.a(foo.o)" after the ToolName before the error message. It sets
239 // HadError but returns allowing the code to move on to other archive members.
240 static void error(llvm::Error E
, StringRef FileName
, const Archive::Child
&C
,
241 StringRef ArchitectureName
= StringRef()) {
243 WithColor::error(errs(), ToolName
) << FileName
;
245 Expected
<StringRef
> NameOrErr
= C
.getName();
246 // TODO: if we have a error getting the name then it would be nice to print
247 // the index of which archive member this is and or its offset in the
248 // archive instead of "???" as the name.
250 consumeError(NameOrErr
.takeError());
251 errs() << "(" << "???" << ")";
253 errs() << "(" << NameOrErr
.get() << ")";
255 if (!ArchitectureName
.empty())
256 errs() << " (for architecture " << ArchitectureName
<< ") ";
259 raw_string_ostream
OS(Buf
);
260 logAllUnhandledErrors(std::move(E
), OS
);
262 errs() << " " << Buf
<< "\n";
265 // This version of error() prints the file name and which architecture slice it
266 // is from, for example: "foo.o (for architecture i386)" after the ToolName
267 // before the error message. It sets HadError but returns allowing the code to
268 // move on to other architecture slices.
269 static void error(llvm::Error E
, StringRef FileName
,
270 StringRef ArchitectureName
= StringRef()) {
272 WithColor::error(errs(), ToolName
) << FileName
;
274 if (!ArchitectureName
.empty())
275 errs() << " (for architecture " << ArchitectureName
<< ") ";
278 raw_string_ostream
OS(Buf
);
279 logAllUnhandledErrors(std::move(E
), OS
);
281 errs() << " " << Buf
<< "\n";
290 StringRef SectionName
;
293 // The Sym field above points to the native symbol in the object file,
294 // for Mach-O when we are creating symbols from the dyld info the above
295 // pointer is null as there is no native symbol. In these cases the fields
296 // below are filled in to represent what would have been a Mach-O nlist
303 StringRef IndirectName
;
305 } // anonymous namespace
307 static bool compareSymbolAddress(const NMSymbol
&A
, const NMSymbol
&B
) {
309 if (A
.Sym
.getRawDataRefImpl().p
)
310 ADefined
= !(A
.Sym
.getFlags() & SymbolRef::SF_Undefined
);
312 ADefined
= A
.TypeChar
!= 'U';
314 if (B
.Sym
.getRawDataRefImpl().p
)
315 BDefined
= !(B
.Sym
.getFlags() & SymbolRef::SF_Undefined
);
317 BDefined
= B
.TypeChar
!= 'U';
318 return std::make_tuple(ADefined
, A
.Address
, A
.Name
, A
.Size
) <
319 std::make_tuple(BDefined
, B
.Address
, B
.Name
, B
.Size
);
322 static bool compareSymbolSize(const NMSymbol
&A
, const NMSymbol
&B
) {
323 return std::make_tuple(A
.Size
, A
.Name
, A
.Address
) <
324 std::make_tuple(B
.Size
, B
.Name
, B
.Address
);
327 static bool compareSymbolName(const NMSymbol
&A
, const NMSymbol
&B
) {
328 return std::make_tuple(A
.Name
, A
.Size
, A
.Address
) <
329 std::make_tuple(B
.Name
, B
.Size
, B
.Address
);
332 static char isSymbolList64Bit(SymbolicFile
&Obj
) {
333 if (auto *IRObj
= dyn_cast
<IRObjectFile
>(&Obj
))
334 return Triple(IRObj
->getTargetTriple()).isArch64Bit();
335 if (isa
<COFFObjectFile
>(Obj
) || isa
<COFFImportFile
>(Obj
))
337 if (isa
<WasmObjectFile
>(Obj
))
339 if (MachOObjectFile
*MachO
= dyn_cast
<MachOObjectFile
>(&Obj
))
340 return MachO
->is64Bit();
341 return cast
<ELFObjectFileBase
>(Obj
).getBytesInAddress() == 8;
344 static StringRef CurrentFilename
;
345 static std::vector
<NMSymbol
> SymbolList
;
347 static char getSymbolNMTypeChar(IRObjectFile
&Obj
, basic_symbol_iterator I
);
349 // darwinPrintSymbol() is used to print a symbol from a Mach-O file when the
350 // the OutputFormat is darwin or we are printing Mach-O symbols in hex. For
351 // the darwin format it produces the same output as darwin's nm(1) -m output
352 // and when printing Mach-O symbols in hex it produces the same output as
353 // darwin's nm(1) -x format.
354 static void darwinPrintSymbol(SymbolicFile
&Obj
, const NMSymbol
&S
,
355 char *SymbolAddrStr
, const char *printBlanks
,
356 const char *printDashes
,
357 const char *printFormat
) {
358 MachO::mach_header H
;
359 MachO::mach_header_64 H_64
;
360 uint32_t Filetype
= MachO::MH_OBJECT
;
367 MachOObjectFile
*MachO
= dyn_cast
<MachOObjectFile
>(&Obj
);
369 uint32_t SymFlags
= S
.Sym
.getFlags();
370 if (SymFlags
& SymbolRef::SF_Global
)
371 NType
|= MachO::N_EXT
;
372 if (SymFlags
& SymbolRef::SF_Hidden
)
373 NType
|= MachO::N_PEXT
;
374 if (SymFlags
& SymbolRef::SF_Undefined
)
375 NType
|= MachO::N_EXT
| MachO::N_UNDF
;
377 // Here we have a symbol definition. So to fake out a section name we
378 // use 1, 2 and 3 for section numbers. See below where they are used to
379 // print out fake section names.
380 NType
|= MachO::N_SECT
;
381 if (SymFlags
& SymbolRef::SF_Const
)
383 else if (SymFlags
& SymbolRef::SF_Executable
)
388 if (SymFlags
& SymbolRef::SF_Weak
)
389 NDesc
|= MachO::N_WEAK_DEF
;
391 DataRefImpl SymDRI
= S
.Sym
.getRawDataRefImpl();
392 if (MachO
->is64Bit()) {
393 H_64
= MachO
->MachOObjectFile::getHeader64();
394 Filetype
= H_64
.filetype
;
397 MachO::nlist_64 STE_64
= MachO
->getSymbol64TableEntry(SymDRI
);
398 NType
= STE_64
.n_type
;
399 NSect
= STE_64
.n_sect
;
400 NDesc
= STE_64
.n_desc
;
401 NStrx
= STE_64
.n_strx
;
402 NValue
= STE_64
.n_value
;
411 H
= MachO
->MachOObjectFile::getHeader();
412 Filetype
= H
.filetype
;
415 MachO::nlist STE
= MachO
->getSymbolTableEntry(SymDRI
);
420 NValue
= STE
.n_value
;
431 // If we are printing Mach-O symbols in hex do that and return.
432 if (FormatMachOasHex
) {
433 outs() << format(printFormat
, NValue
) << ' '
434 << format("%02x %02x %04x %08x", NType
, NSect
, NDesc
, NStrx
) << ' '
436 if ((NType
& MachO::N_TYPE
) == MachO::N_INDR
) {
437 outs() << " (indirect for ";
438 outs() << format(printFormat
, NValue
) << ' ';
439 StringRef IndirectName
;
440 if (S
.Sym
.getRawDataRefImpl().p
) {
441 if (MachO
->getIndirectName(S
.Sym
.getRawDataRefImpl(), IndirectName
))
444 outs() << IndirectName
<< ")";
446 outs() << S
.IndirectName
<< ")";
453 if ((NType
& MachO::N_TYPE
) == MachO::N_INDR
)
454 strcpy(SymbolAddrStr
, printBlanks
);
455 if (Obj
.isIR() && (NType
& MachO::N_TYPE
) == MachO::N_TYPE
)
456 strcpy(SymbolAddrStr
, printDashes
);
457 outs() << SymbolAddrStr
<< ' ';
460 switch (NType
& MachO::N_TYPE
) {
463 outs() << "(common) ";
464 if (MachO::GET_COMM_ALIGN(NDesc
) != 0)
465 outs() << "(alignment 2^" << (int)MachO::GET_COMM_ALIGN(NDesc
) << ") ";
467 if ((NType
& MachO::N_TYPE
) == MachO::N_PBUD
)
468 outs() << "(prebound ";
471 if ((NDesc
& MachO::REFERENCE_TYPE
) ==
472 MachO::REFERENCE_FLAG_UNDEFINED_LAZY
)
473 outs() << "undefined [lazy bound]) ";
474 else if ((NDesc
& MachO::REFERENCE_TYPE
) ==
475 MachO::REFERENCE_FLAG_PRIVATE_UNDEFINED_LAZY
)
476 outs() << "undefined [private lazy bound]) ";
477 else if ((NDesc
& MachO::REFERENCE_TYPE
) ==
478 MachO::REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY
)
479 outs() << "undefined [private]) ";
481 outs() << "undefined) ";
485 outs() << "(absolute) ";
488 outs() << "(indirect) ";
490 case MachO::N_SECT
: {
492 // For llvm bitcode files print out a fake section name using the values
493 // use 1, 2 and 3 for section numbers as set above.
495 outs() << "(LTO,CODE) ";
497 outs() << "(LTO,DATA) ";
499 outs() << "(LTO,RODATA) ";
504 section_iterator Sec
= SectionRef();
505 if (S
.Sym
.getRawDataRefImpl().p
) {
506 Expected
<section_iterator
> SecOrErr
=
507 MachO
->getSymbolSection(S
.Sym
.getRawDataRefImpl());
509 consumeError(SecOrErr
.takeError());
514 if (Sec
== MachO
->section_end()) {
521 DataRefImpl Ref
= Sec
->getRawDataRefImpl();
522 StringRef SectionName
;
523 if (Expected
<StringRef
> NameOrErr
= MachO
->getSectionName(Ref
))
524 SectionName
= *NameOrErr
;
525 StringRef SegmentName
= MachO
->getSectionFinalSegmentName(Ref
);
526 outs() << "(" << SegmentName
<< "," << SectionName
<< ") ";
534 if (NType
& MachO::N_EXT
) {
535 if (NDesc
& MachO::REFERENCED_DYNAMICALLY
)
536 outs() << "[referenced dynamically] ";
537 if (NType
& MachO::N_PEXT
) {
538 if ((NDesc
& MachO::N_WEAK_DEF
) == MachO::N_WEAK_DEF
)
539 outs() << "weak private external ";
541 outs() << "private external ";
543 if ((NDesc
& MachO::N_WEAK_REF
) == MachO::N_WEAK_REF
||
544 (NDesc
& MachO::N_WEAK_DEF
) == MachO::N_WEAK_DEF
) {
545 if ((NDesc
& (MachO::N_WEAK_REF
| MachO::N_WEAK_DEF
)) ==
546 (MachO::N_WEAK_REF
| MachO::N_WEAK_DEF
))
547 outs() << "weak external automatically hidden ";
549 outs() << "weak external ";
551 outs() << "external ";
554 if (NType
& MachO::N_PEXT
)
555 outs() << "non-external (was a private external) ";
557 outs() << "non-external ";
560 if (Filetype
== MachO::MH_OBJECT
) {
561 if (NDesc
& MachO::N_NO_DEAD_STRIP
)
562 outs() << "[no dead strip] ";
563 if ((NType
& MachO::N_TYPE
) != MachO::N_UNDF
&&
564 NDesc
& MachO::N_SYMBOL_RESOLVER
)
565 outs() << "[symbol resolver] ";
566 if ((NType
& MachO::N_TYPE
) != MachO::N_UNDF
&& NDesc
& MachO::N_ALT_ENTRY
)
567 outs() << "[alt entry] ";
568 if ((NType
& MachO::N_TYPE
) != MachO::N_UNDF
&& NDesc
& MachO::N_COLD_FUNC
)
569 outs() << "[cold func] ";
572 if ((NDesc
& MachO::N_ARM_THUMB_DEF
) == MachO::N_ARM_THUMB_DEF
)
573 outs() << "[Thumb] ";
575 if ((NType
& MachO::N_TYPE
) == MachO::N_INDR
) {
576 outs() << S
.Name
<< " (for ";
577 StringRef IndirectName
;
579 if (S
.Sym
.getRawDataRefImpl().p
) {
580 if (MachO
->getIndirectName(S
.Sym
.getRawDataRefImpl(), IndirectName
))
583 outs() << IndirectName
<< ")";
585 outs() << S
.IndirectName
<< ")";
591 if ((Flags
& MachO::MH_TWOLEVEL
) == MachO::MH_TWOLEVEL
&&
592 (((NType
& MachO::N_TYPE
) == MachO::N_UNDF
&& NValue
== 0) ||
593 (NType
& MachO::N_TYPE
) == MachO::N_PBUD
)) {
594 uint32_t LibraryOrdinal
= MachO::GET_LIBRARY_ORDINAL(NDesc
);
595 if (LibraryOrdinal
!= 0) {
596 if (LibraryOrdinal
== MachO::EXECUTABLE_ORDINAL
)
597 outs() << " (from executable)";
598 else if (LibraryOrdinal
== MachO::DYNAMIC_LOOKUP_ORDINAL
)
599 outs() << " (dynamically looked up)";
601 StringRef LibraryName
;
603 MachO
->getLibraryShortNameByIndex(LibraryOrdinal
- 1, LibraryName
))
604 outs() << " (from bad library ordinal " << LibraryOrdinal
<< ")";
606 outs() << " (from " << LibraryName
<< ")";
614 // Table that maps Darwin's Mach-O stab constants to strings to allow printing.
615 struct DarwinStabName
{
619 static const struct DarwinStabName DarwinStabNames
[] = {
620 {MachO::N_GSYM
, "GSYM"},
621 {MachO::N_FNAME
, "FNAME"},
622 {MachO::N_FUN
, "FUN"},
623 {MachO::N_STSYM
, "STSYM"},
624 {MachO::N_LCSYM
, "LCSYM"},
625 {MachO::N_BNSYM
, "BNSYM"},
627 {MachO::N_AST
, "AST"},
628 {MachO::N_OPT
, "OPT"},
629 {MachO::N_RSYM
, "RSYM"},
630 {MachO::N_SLINE
, "SLINE"},
631 {MachO::N_ENSYM
, "ENSYM"},
632 {MachO::N_SSYM
, "SSYM"},
634 {MachO::N_OSO
, "OSO"},
635 {MachO::N_LSYM
, "LSYM"},
636 {MachO::N_BINCL
, "BINCL"},
637 {MachO::N_SOL
, "SOL"},
638 {MachO::N_PARAMS
, "PARAM"},
639 {MachO::N_VERSION
, "VERS"},
640 {MachO::N_OLEVEL
, "OLEV"},
641 {MachO::N_PSYM
, "PSYM"},
642 {MachO::N_EINCL
, "EINCL"},
643 {MachO::N_ENTRY
, "ENTRY"},
644 {MachO::N_LBRAC
, "LBRAC"},
645 {MachO::N_EXCL
, "EXCL"},
646 {MachO::N_RBRAC
, "RBRAC"},
647 {MachO::N_BCOMM
, "BCOMM"},
648 {MachO::N_ECOMM
, "ECOMM"},
649 {MachO::N_ECOML
, "ECOML"},
650 {MachO::N_LENG
, "LENG"},
653 static const char *getDarwinStabString(uint8_t NType
) {
654 for (auto I
: makeArrayRef(DarwinStabNames
))
655 if (I
.NType
== NType
)
660 // darwinPrintStab() prints the n_sect, n_desc along with a symbolic name of
661 // a stab n_type value in a Mach-O file.
662 static void darwinPrintStab(MachOObjectFile
*MachO
, const NMSymbol
&S
) {
663 MachO::nlist_64 STE_64
;
668 DataRefImpl SymDRI
= S
.Sym
.getRawDataRefImpl();
669 if (MachO
->is64Bit()) {
670 STE_64
= MachO
->getSymbol64TableEntry(SymDRI
);
671 NType
= STE_64
.n_type
;
672 NSect
= STE_64
.n_sect
;
673 NDesc
= STE_64
.n_desc
;
675 STE
= MachO
->getSymbolTableEntry(SymDRI
);
681 outs() << format(" %02x %04x ", NSect
, NDesc
);
682 if (const char *stabString
= getDarwinStabString(NType
))
683 outs() << format("%5.5s", stabString
);
685 outs() << format(" %02x", NType
);
688 static Optional
<std::string
> demangle(StringRef Name
, bool StripUnderscore
) {
689 if (StripUnderscore
&& !Name
.empty() && Name
[0] == '_')
690 Name
= Name
.substr(1);
692 if (!Name
.startswith("_Z"))
697 itaniumDemangle(Name
.str().c_str(), nullptr, nullptr, &Status
);
701 std::string
S(Undecorated
);
706 static bool symbolIsDefined(const NMSymbol
&Sym
) {
707 return Sym
.TypeChar
!= 'U' && Sym
.TypeChar
!= 'w' && Sym
.TypeChar
!= 'v';
710 static void sortAndPrintSymbolList(SymbolicFile
&Obj
, bool printName
,
711 const std::string
&ArchiveName
,
712 const std::string
&ArchitectureName
) {
714 using Comparator
= bool (*)(const NMSymbol
&, const NMSymbol
&);
717 Cmp
= &compareSymbolAddress
;
719 Cmp
= &compareSymbolSize
;
721 Cmp
= &compareSymbolName
;
724 llvm::sort(SymbolList
, [=](const NMSymbol
&A
, const NMSymbol
&B
) -> bool {
728 llvm::sort(SymbolList
, Cmp
);
731 if (!PrintFileName
) {
732 if (OutputFormat
== posix
&& MultipleFiles
&& printName
) {
733 outs() << '\n' << CurrentFilename
<< ":\n";
734 } else if (OutputFormat
== bsd
&& MultipleFiles
&& printName
) {
735 outs() << "\n" << CurrentFilename
<< ":\n";
736 } else if (OutputFormat
== sysv
) {
737 outs() << "\n\nSymbols from " << CurrentFilename
<< ":\n\n";
738 if (isSymbolList64Bit(Obj
))
739 outs() << "Name Value Class Type"
740 << " Size Line Section\n";
742 outs() << "Name Value Class Type"
743 << " Size Line Section\n";
747 const char *printBlanks
, *printDashes
, *printFormat
;
748 if (isSymbolList64Bit(Obj
)) {
750 printDashes
= "----------------";
751 switch (AddressRadix
) {
753 printFormat
= OutputFormat
== posix
? "%" PRIo64
: "%016" PRIo64
;
756 printFormat
= OutputFormat
== posix
? "%" PRIx64
: "%016" PRIx64
;
759 printFormat
= OutputFormat
== posix
? "%" PRId64
: "%016" PRId64
;
763 printDashes
= "--------";
764 switch (AddressRadix
) {
766 printFormat
= OutputFormat
== posix
? "%" PRIo64
: "%08" PRIo64
;
769 printFormat
= OutputFormat
== posix
? "%" PRIx64
: "%08" PRIx64
;
772 printFormat
= OutputFormat
== posix
? "%" PRId64
: "%08" PRId64
;
776 auto writeFileName
= [&](raw_ostream
&S
) {
777 if (!ArchitectureName
.empty())
778 S
<< "(for architecture " << ArchitectureName
<< "):";
779 if (OutputFormat
== posix
&& !ArchiveName
.empty())
780 S
<< ArchiveName
<< "[" << CurrentFilename
<< "]: ";
782 if (!ArchiveName
.empty())
783 S
<< ArchiveName
<< ":";
784 S
<< CurrentFilename
<< ": ";
788 if (SymbolList
.empty()) {
790 writeFileName(errs());
791 errs() << "no symbols\n";
794 for (const NMSymbol
&S
: SymbolList
) {
796 std::string Name
= S
.Name
.str();
797 MachOObjectFile
*MachO
= dyn_cast
<MachOObjectFile
>(&Obj
);
799 if (Optional
<std::string
> Opt
= demangle(S
.Name
, MachO
))
802 if (S
.Sym
.getRawDataRefImpl().p
)
803 SymFlags
= S
.Sym
.getFlags();
805 SymFlags
= S
.SymFlags
;
807 bool Undefined
= SymFlags
& SymbolRef::SF_Undefined
;
808 bool Global
= SymFlags
& SymbolRef::SF_Global
;
809 bool Weak
= SymFlags
& SymbolRef::SF_Weak
;
810 if ((!Undefined
&& UndefinedOnly
) || (Undefined
&& DefinedOnly
) ||
811 (!Global
&& ExternalOnly
) || (Weak
&& NoWeakSymbols
))
814 writeFileName(outs());
815 if ((JustSymbolName
||
816 (UndefinedOnly
&& MachO
&& OutputFormat
!= darwin
)) &&
817 OutputFormat
!= posix
) {
818 outs() << Name
<< "\n";
822 char SymbolAddrStr
[23], SymbolSizeStr
[23];
824 // If the format is SysV or the symbol isn't defined, then print spaces.
825 if (OutputFormat
== sysv
|| !symbolIsDefined(S
)) {
826 if (OutputFormat
== posix
) {
827 format(printFormat
, S
.Address
)
828 .print(SymbolAddrStr
, sizeof(SymbolAddrStr
));
829 format(printFormat
, S
.Size
).print(SymbolSizeStr
, sizeof(SymbolSizeStr
));
831 strcpy(SymbolAddrStr
, printBlanks
);
832 strcpy(SymbolSizeStr
, printBlanks
);
836 if (symbolIsDefined(S
)) {
837 // Otherwise, print the symbol address and size.
839 strcpy(SymbolAddrStr
, printDashes
);
840 else if (MachO
&& S
.TypeChar
== 'I')
841 strcpy(SymbolAddrStr
, printBlanks
);
843 format(printFormat
, S
.Address
)
844 .print(SymbolAddrStr
, sizeof(SymbolAddrStr
));
845 format(printFormat
, S
.Size
).print(SymbolSizeStr
, sizeof(SymbolSizeStr
));
848 // If OutputFormat is darwin or we are printing Mach-O symbols in hex and
849 // we have a MachOObjectFile, call darwinPrintSymbol to print as darwin's
850 // nm(1) -m output or hex, else if OutputFormat is darwin or we are
851 // printing Mach-O symbols in hex and not a Mach-O object fall back to
852 // OutputFormat bsd (see below).
853 if ((OutputFormat
== darwin
|| FormatMachOasHex
) && (MachO
|| Obj
.isIR())) {
854 darwinPrintSymbol(Obj
, S
, SymbolAddrStr
, printBlanks
, printDashes
,
856 } else if (OutputFormat
== posix
) {
857 outs() << Name
<< " " << S
.TypeChar
<< " " << SymbolAddrStr
<< " "
858 << (MachO
? "0" : SymbolSizeStr
) << "\n";
859 } else if (OutputFormat
== bsd
|| (OutputFormat
== darwin
&& !MachO
)) {
861 outs() << SymbolAddrStr
<< ' ';
863 outs() << SymbolSizeStr
<< ' ';
864 outs() << S
.TypeChar
;
865 if (S
.TypeChar
== '-' && MachO
)
866 darwinPrintStab(MachO
, S
);
867 outs() << " " << Name
;
868 if (S
.TypeChar
== 'I' && MachO
) {
869 outs() << " (indirect for ";
870 if (S
.Sym
.getRawDataRefImpl().p
) {
871 StringRef IndirectName
;
872 if (MachO
->getIndirectName(S
.Sym
.getRawDataRefImpl(), IndirectName
))
875 outs() << IndirectName
<< ")";
877 outs() << S
.IndirectName
<< ")";
880 } else if (OutputFormat
== sysv
) {
881 outs() << left_justify(Name
, 20) << "|" << SymbolAddrStr
<< "| "
882 << S
.TypeChar
<< " |" << right_justify(S
.TypeName
, 18) << "|"
883 << SymbolSizeStr
<< "| |" << S
.SectionName
<< "\n";
890 static char getSymbolNMTypeChar(ELFObjectFileBase
&Obj
,
891 basic_symbol_iterator I
) {
893 elf_symbol_iterator
SymI(I
);
895 Expected
<elf_section_iterator
> SecIOrErr
= SymI
->getSection();
897 consumeError(SecIOrErr
.takeError());
901 uint8_t Binding
= SymI
->getBinding();
902 if (Binding
== ELF::STB_GNU_UNIQUE
)
905 assert(Binding
!= ELF::STB_WEAK
&& "STB_WEAK not tested in calling function");
906 if (Binding
!= ELF::STB_GLOBAL
&& Binding
!= ELF::STB_LOCAL
)
909 elf_section_iterator SecI
= *SecIOrErr
;
910 if (SecI
!= Obj
.section_end()) {
911 uint32_t Type
= SecI
->getType();
912 uint64_t Flags
= SecI
->getFlags();
913 if (Flags
& ELF::SHF_EXECINSTR
)
915 if (Type
== ELF::SHT_NOBITS
)
917 if (Flags
& ELF::SHF_ALLOC
)
918 return Flags
& ELF::SHF_WRITE
? 'd' : 'r';
920 auto NameOrErr
= SecI
->getName();
922 consumeError(NameOrErr
.takeError());
925 if ((*NameOrErr
).startswith(".debug"))
927 if (!(Flags
& ELF::SHF_WRITE
))
934 static char getSymbolNMTypeChar(COFFObjectFile
&Obj
, symbol_iterator I
) {
935 COFFSymbolRef Symb
= Obj
.getCOFFSymbol(*I
);
937 symbol_iterator
SymI(I
);
939 Expected
<StringRef
> Name
= SymI
->getName();
941 consumeError(Name
.takeError());
945 char Ret
= StringSwitch
<char>(*Name
)
946 .StartsWith(".debug", 'N')
947 .StartsWith(".sxdata", 'N')
953 uint32_t Characteristics
= 0;
954 if (!COFF::isReservedSectionNumber(Symb
.getSectionNumber())) {
955 Expected
<section_iterator
> SecIOrErr
= SymI
->getSection();
957 consumeError(SecIOrErr
.takeError());
960 section_iterator SecI
= *SecIOrErr
;
961 const coff_section
*Section
= Obj
.getCOFFSection(*SecI
);
962 Characteristics
= Section
->Characteristics
;
963 if (Expected
<StringRef
> NameOrErr
= Obj
.getSectionName(Section
))
964 if (NameOrErr
->startswith(".idata"))
968 switch (Symb
.getSectionNumber()) {
969 case COFF::IMAGE_SYM_DEBUG
:
972 // Check section type.
973 if (Characteristics
& COFF::IMAGE_SCN_CNT_CODE
)
975 if (Characteristics
& COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
)
976 return Characteristics
& COFF::IMAGE_SCN_MEM_WRITE
? 'd' : 'r';
977 if (Characteristics
& COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
)
979 if (Characteristics
& COFF::IMAGE_SCN_LNK_INFO
)
981 // Check for section symbol.
982 if (Symb
.isSectionDefinition())
989 static char getSymbolNMTypeChar(COFFImportFile
&Obj
) {
990 switch (Obj
.getCOFFImportHeader()->getType()) {
991 case COFF::IMPORT_CODE
:
993 case COFF::IMPORT_DATA
:
995 case COFF::IMPORT_CONST
:
1001 static char getSymbolNMTypeChar(MachOObjectFile
&Obj
, basic_symbol_iterator I
) {
1002 DataRefImpl Symb
= I
->getRawDataRefImpl();
1003 uint8_t NType
= Obj
.is64Bit() ? Obj
.getSymbol64TableEntry(Symb
).n_type
1004 : Obj
.getSymbolTableEntry(Symb
).n_type
;
1006 if (NType
& MachO::N_STAB
)
1009 switch (NType
& MachO::N_TYPE
) {
1014 case MachO::N_SECT
: {
1015 Expected
<section_iterator
> SecOrErr
= Obj
.getSymbolSection(Symb
);
1017 consumeError(SecOrErr
.takeError());
1020 section_iterator Sec
= *SecOrErr
;
1021 if (Sec
== Obj
.section_end())
1023 DataRefImpl Ref
= Sec
->getRawDataRefImpl();
1024 StringRef SectionName
;
1025 if (Expected
<StringRef
> NameOrErr
= Obj
.getSectionName(Ref
))
1026 SectionName
= *NameOrErr
;
1027 StringRef SegmentName
= Obj
.getSectionFinalSegmentName(Ref
);
1028 if (Obj
.is64Bit() && Obj
.getHeader64().filetype
== MachO::MH_KEXT_BUNDLE
&&
1029 SegmentName
== "__TEXT_EXEC" && SectionName
== "__text")
1031 if (SegmentName
== "__TEXT" && SectionName
== "__text")
1033 if (SegmentName
== "__DATA" && SectionName
== "__data")
1035 if (SegmentName
== "__DATA" && SectionName
== "__bss")
1044 static char getSymbolNMTypeChar(WasmObjectFile
&Obj
, basic_symbol_iterator I
) {
1045 uint32_t Flags
= I
->getFlags();
1046 if (Flags
& SymbolRef::SF_Executable
)
1051 static char getSymbolNMTypeChar(IRObjectFile
&Obj
, basic_symbol_iterator I
) {
1052 uint32_t Flags
= I
->getFlags();
1053 // FIXME: should we print 'b'? At the IR level we cannot be sure if this
1054 // will be in bss or not, but we could approximate.
1055 if (Flags
& SymbolRef::SF_Executable
)
1057 else if (Triple(Obj
.getTargetTriple()).isOSDarwin() &&
1058 (Flags
& SymbolRef::SF_Const
))
1064 static bool isObject(SymbolicFile
&Obj
, basic_symbol_iterator I
) {
1065 return !dyn_cast
<ELFObjectFileBase
>(&Obj
)
1067 : elf_symbol_iterator(I
)->getELFType() == ELF::STT_OBJECT
;
1070 // For ELF object files, Set TypeName to the symbol typename, to be printed
1071 // in the 'Type' column of the SYSV format output.
1072 static StringRef
getNMTypeName(SymbolicFile
&Obj
, basic_symbol_iterator I
) {
1073 if (isa
<ELFObjectFileBase
>(&Obj
)) {
1074 elf_symbol_iterator
SymI(I
);
1075 return SymI
->getELFTypeName();
1080 // Return Posix nm class type tag (single letter), but also set SecName and
1081 // section and name, to be used in format=sysv output.
1082 static char getNMSectionTagAndName(SymbolicFile
&Obj
, basic_symbol_iterator I
,
1083 StringRef
&SecName
) {
1084 uint32_t Symflags
= I
->getFlags();
1085 if (ELFObjectFileBase
*ELFObj
= dyn_cast
<ELFObjectFileBase
>(&Obj
)) {
1086 if (Symflags
& object::SymbolRef::SF_Absolute
)
1088 else if (Symflags
& object::SymbolRef::SF_Common
)
1090 else if (Symflags
& object::SymbolRef::SF_Undefined
)
1093 elf_symbol_iterator
SymI(I
);
1094 Expected
<elf_section_iterator
> SecIOrErr
= SymI
->getSection();
1096 consumeError(SecIOrErr
.takeError());
1100 if (*SecIOrErr
== ELFObj
->section_end())
1103 Expected
<StringRef
> NameOrErr
= (*SecIOrErr
)->getName();
1105 consumeError(NameOrErr
.takeError());
1108 SecName
= *NameOrErr
;
1112 if ((Symflags
& object::SymbolRef::SF_Weak
) && !isa
<MachOObjectFile
>(Obj
)) {
1113 char Ret
= isObject(Obj
, I
) ? 'v' : 'w';
1114 return (!(Symflags
& object::SymbolRef::SF_Undefined
)) ? toupper(Ret
) : Ret
;
1117 if (Symflags
& object::SymbolRef::SF_Undefined
)
1120 if (Symflags
& object::SymbolRef::SF_Common
)
1124 if (Symflags
& object::SymbolRef::SF_Absolute
)
1126 else if (IRObjectFile
*IR
= dyn_cast
<IRObjectFile
>(&Obj
))
1127 Ret
= getSymbolNMTypeChar(*IR
, I
);
1128 else if (COFFObjectFile
*COFF
= dyn_cast
<COFFObjectFile
>(&Obj
))
1129 Ret
= getSymbolNMTypeChar(*COFF
, I
);
1130 else if (COFFImportFile
*COFFImport
= dyn_cast
<COFFImportFile
>(&Obj
))
1131 Ret
= getSymbolNMTypeChar(*COFFImport
);
1132 else if (MachOObjectFile
*MachO
= dyn_cast
<MachOObjectFile
>(&Obj
))
1133 Ret
= getSymbolNMTypeChar(*MachO
, I
);
1134 else if (WasmObjectFile
*Wasm
= dyn_cast
<WasmObjectFile
>(&Obj
))
1135 Ret
= getSymbolNMTypeChar(*Wasm
, I
);
1137 Ret
= getSymbolNMTypeChar(cast
<ELFObjectFileBase
>(Obj
), I
);
1139 if (!(Symflags
& object::SymbolRef::SF_Global
))
1142 if (Obj
.isELF() && ELFSymbolRef(*I
).getBinding() == ELF::STB_GNU_UNIQUE
)
1145 return toupper(Ret
);
1148 // getNsectForSegSect() is used to implement the Mach-O "-s segname sectname"
1149 // option to dump only those symbols from that section in a Mach-O file.
1150 // It is called once for each Mach-O file from dumpSymbolNamesFromObject()
1151 // to get the section number for that named section from the command line
1152 // arguments. It returns the section number for that section in the Mach-O
1153 // file or zero it is not present.
1154 static unsigned getNsectForSegSect(MachOObjectFile
*Obj
) {
1156 for (auto &S
: Obj
->sections()) {
1157 DataRefImpl Ref
= S
.getRawDataRefImpl();
1158 StringRef SectionName
;
1159 if (Expected
<StringRef
> NameOrErr
= Obj
->getSectionName(Ref
))
1160 SectionName
= *NameOrErr
;
1161 StringRef SegmentName
= Obj
->getSectionFinalSegmentName(Ref
);
1162 if (SegmentName
== SegSect
[0] && SectionName
== SegSect
[1])
1169 // getNsectInMachO() is used to implement the Mach-O "-s segname sectname"
1170 // option to dump only those symbols from that section in a Mach-O file.
1171 // It is called once for each symbol in a Mach-O file from
1172 // dumpSymbolNamesFromObject() and returns the section number for that symbol
1173 // if it is in a section, else it returns 0.
1174 static unsigned getNsectInMachO(MachOObjectFile
&Obj
, BasicSymbolRef Sym
) {
1175 DataRefImpl Symb
= Sym
.getRawDataRefImpl();
1176 if (Obj
.is64Bit()) {
1177 MachO::nlist_64 STE
= Obj
.getSymbol64TableEntry(Symb
);
1178 return (STE
.n_type
& MachO::N_TYPE
) == MachO::N_SECT
? STE
.n_sect
: 0;
1180 MachO::nlist STE
= Obj
.getSymbolTableEntry(Symb
);
1181 return (STE
.n_type
& MachO::N_TYPE
) == MachO::N_SECT
? STE
.n_sect
: 0;
1185 dumpSymbolNamesFromObject(SymbolicFile
&Obj
, bool printName
,
1186 const std::string
&ArchiveName
= std::string(),
1187 const std::string
&ArchitectureName
= std::string()) {
1188 auto Symbols
= Obj
.symbols();
1190 const auto *E
= dyn_cast
<ELFObjectFileBase
>(&Obj
);
1192 error("File format has no dynamic symbol table", Obj
.getFileName());
1195 Symbols
= E
->getDynamicSymbolIterators();
1197 std::string NameBuffer
;
1198 raw_string_ostream
OS(NameBuffer
);
1199 // If a "-s segname sectname" option was specified and this is a Mach-O
1200 // file get the section number for that section in this object file.
1201 unsigned int Nsect
= 0;
1202 MachOObjectFile
*MachO
= dyn_cast
<MachOObjectFile
>(&Obj
);
1203 if (!SegSect
.empty() && MachO
) {
1204 Nsect
= getNsectForSegSect(MachO
);
1205 // If this section is not in the object file no symbols are printed.
1209 if (!MachO
|| !DyldInfoOnly
) {
1210 for (BasicSymbolRef Sym
: Symbols
) {
1211 uint32_t SymFlags
= Sym
.getFlags();
1212 if (!DebugSyms
&& (SymFlags
& SymbolRef::SF_FormatSpecific
))
1214 if (WithoutAliases
&& (SymFlags
& SymbolRef::SF_Indirect
))
1216 // If a "-s segname sectname" option was specified and this is a Mach-O
1217 // file and this section appears in this file, Nsect will be non-zero then
1218 // see if this symbol is a symbol from that section and if not skip it.
1219 if (Nsect
&& Nsect
!= getNsectInMachO(*MachO
, Sym
))
1224 if (isa
<ELFObjectFileBase
>(&Obj
))
1225 S
.Size
= ELFSymbolRef(Sym
).getSize();
1226 if (PrintAddress
&& isa
<ObjectFile
>(Obj
)) {
1227 SymbolRef
SymRef(Sym
);
1228 Expected
<uint64_t> AddressOrErr
= SymRef
.getAddress();
1229 if (!AddressOrErr
) {
1230 consumeError(AddressOrErr
.takeError());
1233 S
.Address
= *AddressOrErr
;
1235 S
.TypeName
= getNMTypeName(Obj
, Sym
);
1236 S
.TypeChar
= getNMSectionTagAndName(Obj
, Sym
, S
.SectionName
);
1237 if (Error E
= Sym
.printName(OS
)) {
1239 OS
<< "bad string index";
1240 consumeError(std::move(E
));
1242 error(std::move(E
), Obj
.getFileName());
1246 SymbolList
.push_back(S
);
1251 const char *P
= NameBuffer
.c_str();
1253 for (I
= 0; I
< SymbolList
.size(); ++I
) {
1254 SymbolList
[I
].Name
= P
;
1258 // If this is a Mach-O file where the nlist symbol table is out of sync
1259 // with the dyld export trie then look through exports and fake up symbols
1260 // for the ones that are missing (also done with the -add-dyldinfo flag).
1261 // This is needed if strip(1) -T is run on a binary containing swift
1262 // language symbols for example. The option -only-dyldinfo will fake up
1263 // all symbols from the dyld export trie as well as the bind info.
1264 std::string ExportsNameBuffer
;
1265 raw_string_ostream
EOS(ExportsNameBuffer
);
1266 std::string BindsNameBuffer
;
1267 raw_string_ostream
BOS(BindsNameBuffer
);
1268 std::string LazysNameBuffer
;
1269 raw_string_ostream
LOS(LazysNameBuffer
);
1270 std::string WeaksNameBuffer
;
1271 raw_string_ostream
WOS(WeaksNameBuffer
);
1272 std::string FunctionStartsNameBuffer
;
1273 raw_string_ostream
FOS(FunctionStartsNameBuffer
);
1274 if (MachO
&& !NoDyldInfo
) {
1275 MachO::mach_header H
;
1276 MachO::mach_header_64 H_64
;
1277 uint32_t HFlags
= 0;
1278 if (MachO
->is64Bit()) {
1279 H_64
= MachO
->MachOObjectFile::getHeader64();
1280 HFlags
= H_64
.flags
;
1282 H
= MachO
->MachOObjectFile::getHeader();
1285 uint64_t BaseSegmentAddress
= 0;
1286 for (const auto &Command
: MachO
->load_commands()) {
1287 if (Command
.C
.cmd
== MachO::LC_SEGMENT
) {
1288 MachO::segment_command Seg
= MachO
->getSegmentLoadCommand(Command
);
1289 if (Seg
.fileoff
== 0 && Seg
.filesize
!= 0) {
1290 BaseSegmentAddress
= Seg
.vmaddr
;
1293 } else if (Command
.C
.cmd
== MachO::LC_SEGMENT_64
) {
1294 MachO::segment_command_64 Seg
= MachO
->getSegment64LoadCommand(Command
);
1295 if (Seg
.fileoff
== 0 && Seg
.filesize
!= 0) {
1296 BaseSegmentAddress
= Seg
.vmaddr
;
1301 if (DyldInfoOnly
|| AddDyldInfo
||
1302 HFlags
& MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO
) {
1303 unsigned ExportsAdded
= 0;
1304 Error Err
= Error::success();
1305 for (const llvm::object::ExportEntry
&Entry
: MachO
->exports(Err
)) {
1307 bool ReExport
= false;
1308 if (!DyldInfoOnly
) {
1309 for (const NMSymbol
&S
: SymbolList
)
1310 if (S
.Address
== Entry
.address() + BaseSegmentAddress
&&
1311 S
.Name
== Entry
.name()) {
1318 S
.Address
= Entry
.address() + BaseSegmentAddress
;
1321 S
.Name
= Entry
.name();
1322 // There is no symbol in the nlist symbol table for this so we set
1323 // Sym effectivly to null and the rest of code in here must test for
1324 // it and not do things like Sym.getFlags() for it.
1325 S
.Sym
= BasicSymbolRef();
1326 S
.SymFlags
= SymbolRef::SF_Global
;
1327 S
.Section
= SectionRef();
1331 S
.IndirectName
= StringRef();
1333 uint64_t EFlags
= Entry
.flags();
1334 bool Abs
= ((EFlags
& MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK
) ==
1335 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE
);
1336 bool Resolver
= (EFlags
&
1337 MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER
);
1338 ReExport
= (EFlags
& MachO::EXPORT_SYMBOL_FLAGS_REEXPORT
);
1339 bool WeakDef
= (EFlags
& MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION
);
1341 S
.NDesc
|= MachO::N_WEAK_DEF
;
1343 S
.NType
= MachO::N_EXT
| MachO::N_ABS
;
1345 } else if (ReExport
) {
1346 S
.NType
= MachO::N_EXT
| MachO::N_INDR
;
1349 S
.NType
= MachO::N_EXT
| MachO::N_SECT
;
1351 S
.Address
= Entry
.other() + BaseSegmentAddress
;
1352 if ((S
.Address
& 1) != 0 &&
1353 !MachO
->is64Bit() && H
.cputype
== MachO::CPU_TYPE_ARM
){
1355 S
.NDesc
|= MachO::N_ARM_THUMB_DEF
;
1358 S
.Address
= Entry
.address() + BaseSegmentAddress
;
1360 StringRef SegmentName
= StringRef();
1361 StringRef SectionName
= StringRef();
1362 for (const SectionRef
&Section
: MachO
->sections()) {
1365 if (Expected
<StringRef
> NameOrErr
= Section
.getName())
1366 SectionName
= *NameOrErr
;
1368 consumeError(NameOrErr
.takeError());
1370 SegmentName
= MachO
->getSectionFinalSegmentName(
1371 Section
.getRawDataRefImpl());
1372 if (S
.Address
>= Section
.getAddress() &&
1373 S
.Address
< Section
.getAddress() + Section
.getSize()) {
1374 S
.Section
= Section
;
1376 } else if (Entry
.name() == "__mh_execute_header" &&
1377 SegmentName
== "__TEXT" && SectionName
== "__text") {
1378 S
.Section
= Section
;
1379 S
.NDesc
|= MachO::REFERENCED_DYNAMICALLY
;
1383 if (SegmentName
== "__TEXT" && SectionName
== "__text")
1385 else if (SegmentName
== "__DATA" && SectionName
== "__data")
1387 else if (SegmentName
== "__DATA" && SectionName
== "__bss")
1392 SymbolList
.push_back(S
);
1394 EOS
<< Entry
.name();
1398 // For ReExports there are a two more things to do, first add the
1399 // indirect name and second create the undefined symbol using the
1400 // referened dynamic library.
1403 // Add the indirect name.
1404 if (Entry
.otherName().empty())
1405 EOS
<< Entry
.name();
1407 EOS
<< Entry
.otherName();
1410 // Now create the undefined symbol using the referened dynamic
1416 if (Entry
.otherName().empty())
1417 U
.Name
= Entry
.name();
1419 U
.Name
= Entry
.otherName();
1420 // Again there is no symbol in the nlist symbol table for this so
1421 // we set Sym effectivly to null and the rest of code in here must
1422 // test for it and not do things like Sym.getFlags() for it.
1423 U
.Sym
= BasicSymbolRef();
1424 U
.SymFlags
= SymbolRef::SF_Global
| SymbolRef::SF_Undefined
;
1425 U
.Section
= SectionRef();
1426 U
.NType
= MachO::N_EXT
| MachO::N_UNDF
;
1429 // The library ordinal for this undefined symbol is in the export
1430 // trie Entry.other().
1431 MachO::SET_LIBRARY_ORDINAL(U
.NDesc
, Entry
.other());
1432 U
.IndirectName
= StringRef();
1433 SymbolList
.push_back(U
);
1435 // Finally add the undefined symbol's name.
1436 if (Entry
.otherName().empty())
1437 EOS
<< Entry
.name();
1439 EOS
<< Entry
.otherName();
1446 error(std::move(Err
), MachO
->getFileName());
1447 // Set the symbol names and indirect names for the added symbols.
1450 const char *Q
= ExportsNameBuffer
.c_str();
1451 for (unsigned K
= 0; K
< ExportsAdded
; K
++) {
1452 SymbolList
[I
].Name
= Q
;
1454 if (SymbolList
[I
].TypeChar
== 'I') {
1455 SymbolList
[I
].IndirectName
= Q
;
1462 // Add the undefined symbols from the bind entries.
1463 unsigned BindsAdded
= 0;
1464 Error BErr
= Error::success();
1465 StringRef LastSymbolName
= StringRef();
1466 for (const llvm::object::MachOBindEntry
&Entry
: MachO
->bindTable(BErr
)) {
1468 if (LastSymbolName
== Entry
.symbolName())
1470 else if(!DyldInfoOnly
) {
1471 for (unsigned J
= 0; J
< SymbolList
.size() && !found
; ++J
) {
1472 if (SymbolList
[J
].Name
== Entry
.symbolName())
1477 LastSymbolName
= Entry
.symbolName();
1482 // There is no symbol in the nlist symbol table for this so we set
1483 // Sym effectivly to null and the rest of code in here must test for
1484 // it and not do things like Sym.getFlags() for it.
1485 B
.Sym
= BasicSymbolRef();
1486 B
.SymFlags
= SymbolRef::SF_Global
| SymbolRef::SF_Undefined
;
1487 B
.NType
= MachO::N_EXT
| MachO::N_UNDF
;
1490 MachO::SET_LIBRARY_ORDINAL(B
.NDesc
, Entry
.ordinal());
1491 B
.IndirectName
= StringRef();
1492 B
.Name
= Entry
.symbolName();
1493 SymbolList
.push_back(B
);
1494 BOS
<< Entry
.symbolName();
1500 error(std::move(BErr
), MachO
->getFileName());
1501 // Set the symbol names and indirect names for the added symbols.
1504 const char *Q
= BindsNameBuffer
.c_str();
1505 for (unsigned K
= 0; K
< BindsAdded
; K
++) {
1506 SymbolList
[I
].Name
= Q
;
1508 if (SymbolList
[I
].TypeChar
== 'I') {
1509 SymbolList
[I
].IndirectName
= Q
;
1516 // Add the undefined symbols from the lazy bind entries.
1517 unsigned LazysAdded
= 0;
1518 Error LErr
= Error::success();
1519 LastSymbolName
= StringRef();
1520 for (const llvm::object::MachOBindEntry
&Entry
:
1521 MachO
->lazyBindTable(LErr
)) {
1523 if (LastSymbolName
== Entry
.symbolName())
1526 // Here we must check to see it this symbol is already in the
1527 // SymbolList as it might have already have been added above via a
1528 // non-lazy (bind) entry.
1529 for (unsigned J
= 0; J
< SymbolList
.size() && !found
; ++J
) {
1530 if (SymbolList
[J
].Name
== Entry
.symbolName())
1535 LastSymbolName
= Entry
.symbolName();
1537 L
.Name
= Entry
.symbolName();
1541 // There is no symbol in the nlist symbol table for this so we set
1542 // Sym effectivly to null and the rest of code in here must test for
1543 // it and not do things like Sym.getFlags() for it.
1544 L
.Sym
= BasicSymbolRef();
1545 L
.SymFlags
= SymbolRef::SF_Global
| SymbolRef::SF_Undefined
;
1546 L
.NType
= MachO::N_EXT
| MachO::N_UNDF
;
1548 // The REFERENCE_FLAG_UNDEFINED_LAZY is no longer used but here it
1549 // makes sence since we are creating this from a lazy bind entry.
1550 L
.NDesc
= MachO::REFERENCE_FLAG_UNDEFINED_LAZY
;
1551 MachO::SET_LIBRARY_ORDINAL(L
.NDesc
, Entry
.ordinal());
1552 L
.IndirectName
= StringRef();
1553 SymbolList
.push_back(L
);
1554 LOS
<< Entry
.symbolName();
1560 error(std::move(LErr
), MachO
->getFileName());
1561 // Set the symbol names and indirect names for the added symbols.
1564 const char *Q
= LazysNameBuffer
.c_str();
1565 for (unsigned K
= 0; K
< LazysAdded
; K
++) {
1566 SymbolList
[I
].Name
= Q
;
1568 if (SymbolList
[I
].TypeChar
== 'I') {
1569 SymbolList
[I
].IndirectName
= Q
;
1576 // Add the undefineds symbol from the weak bind entries which are not
1578 unsigned WeaksAdded
= 0;
1579 Error WErr
= Error::success();
1580 LastSymbolName
= StringRef();
1581 for (const llvm::object::MachOBindEntry
&Entry
:
1582 MachO
->weakBindTable(WErr
)) {
1585 if (LastSymbolName
== Entry
.symbolName() ||
1586 Entry
.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION
) {
1589 for (J
= 0; J
< SymbolList
.size() && !found
; ++J
) {
1590 if (SymbolList
[J
].Name
== Entry
.symbolName()) {
1597 LastSymbolName
= Entry
.symbolName();
1599 memset(&W
, '\0', sizeof(NMSymbol
));
1600 W
.Name
= Entry
.symbolName();
1604 // There is no symbol in the nlist symbol table for this so we set
1605 // Sym effectivly to null and the rest of code in here must test for
1606 // it and not do things like Sym.getFlags() for it.
1607 W
.Sym
= BasicSymbolRef();
1608 W
.SymFlags
= SymbolRef::SF_Global
| SymbolRef::SF_Undefined
;
1609 W
.NType
= MachO::N_EXT
| MachO::N_UNDF
;
1611 // Odd that we are using N_WEAK_DEF on an undefined symbol but that is
1612 // what is created in this case by the linker when there are real
1613 // symbols in the nlist structs.
1614 W
.NDesc
= MachO::N_WEAK_DEF
;
1615 W
.IndirectName
= StringRef();
1616 SymbolList
.push_back(W
);
1617 WOS
<< Entry
.symbolName();
1621 // This is the case the symbol was previously been found and it could
1622 // have been added from a bind or lazy bind symbol. If so and not
1623 // a definition also mark it as weak.
1624 if (SymbolList
[J
].TypeChar
== 'U')
1625 // See comment above about N_WEAK_DEF.
1626 SymbolList
[J
].NDesc
|= MachO::N_WEAK_DEF
;
1630 error(std::move(WErr
), MachO
->getFileName());
1631 // Set the symbol names and indirect names for the added symbols.
1634 const char *Q
= WeaksNameBuffer
.c_str();
1635 for (unsigned K
= 0; K
< WeaksAdded
; K
++) {
1636 SymbolList
[I
].Name
= Q
;
1638 if (SymbolList
[I
].TypeChar
== 'I') {
1639 SymbolList
[I
].IndirectName
= Q
;
1646 // Trying adding symbol from the function starts table and LC_MAIN entry
1648 SmallVector
<uint64_t, 8> FoundFns
;
1649 uint64_t lc_main_offset
= UINT64_MAX
;
1650 for (const auto &Command
: MachO
->load_commands()) {
1651 if (Command
.C
.cmd
== MachO::LC_FUNCTION_STARTS
) {
1652 // We found a function starts segment, parse the addresses for
1654 MachO::linkedit_data_command LLC
=
1655 MachO
->getLinkeditDataLoadCommand(Command
);
1657 MachO
->ReadULEB128s(LLC
.dataoff
, FoundFns
);
1658 } else if (Command
.C
.cmd
== MachO::LC_MAIN
) {
1659 MachO::entry_point_command LCmain
=
1660 MachO
->getEntryPointCommand(Command
);
1661 lc_main_offset
= LCmain
.entryoff
;
1664 // See if these addresses are already in the symbol table.
1665 unsigned FunctionStartsAdded
= 0;
1666 for (uint64_t f
= 0; f
< FoundFns
.size(); f
++) {
1668 for (unsigned J
= 0; J
< SymbolList
.size() && !found
; ++J
) {
1669 if (SymbolList
[J
].Address
== FoundFns
[f
] + BaseSegmentAddress
)
1672 // See this address is not already in the symbol table fake up an
1676 F
.Name
= "<redacted function X>";
1677 F
.Address
= FoundFns
[f
] + BaseSegmentAddress
;
1679 // There is no symbol in the nlist symbol table for this so we set
1680 // Sym effectivly to null and the rest of code in here must test for
1681 // it and not do things like Sym.getFlags() for it.
1682 F
.Sym
= BasicSymbolRef();
1684 F
.NType
= MachO::N_SECT
;
1686 StringRef SegmentName
= StringRef();
1687 StringRef SectionName
= StringRef();
1688 for (const SectionRef
&Section
: MachO
->sections()) {
1689 if (Expected
<StringRef
> NameOrErr
= Section
.getName())
1690 SectionName
= *NameOrErr
;
1692 consumeError(NameOrErr
.takeError());
1694 SegmentName
= MachO
->getSectionFinalSegmentName(
1695 Section
.getRawDataRefImpl());
1697 if (F
.Address
>= Section
.getAddress() &&
1698 F
.Address
< Section
.getAddress() + Section
.getSize()) {
1699 F
.Section
= Section
;
1703 if (SegmentName
== "__TEXT" && SectionName
== "__text")
1705 else if (SegmentName
== "__DATA" && SectionName
== "__data")
1707 else if (SegmentName
== "__DATA" && SectionName
== "__bss")
1712 F
.IndirectName
= StringRef();
1713 SymbolList
.push_back(F
);
1714 if (FoundFns
[f
] == lc_main_offset
)
1715 FOS
<< "<redacted LC_MAIN>";
1717 FOS
<< "<redacted function " << f
<< ">";
1719 FunctionStartsAdded
++;
1722 if (FunctionStartsAdded
) {
1724 const char *Q
= FunctionStartsNameBuffer
.c_str();
1725 for (unsigned K
= 0; K
< FunctionStartsAdded
; K
++) {
1726 SymbolList
[I
].Name
= Q
;
1728 if (SymbolList
[I
].TypeChar
== 'I') {
1729 SymbolList
[I
].IndirectName
= Q
;
1738 CurrentFilename
= Obj
.getFileName();
1739 sortAndPrintSymbolList(Obj
, printName
, ArchiveName
, ArchitectureName
);
1742 // checkMachOAndArchFlags() checks to see if the SymbolicFile is a Mach-O file
1743 // and if it is and there is a list of architecture flags is specified then
1744 // check to make sure this Mach-O file is one of those architectures or all
1745 // architectures was specificed. If not then an error is generated and this
1746 // routine returns false. Else it returns true.
1747 static bool checkMachOAndArchFlags(SymbolicFile
*O
, std::string
&Filename
) {
1748 auto *MachO
= dyn_cast
<MachOObjectFile
>(O
);
1750 if (!MachO
|| ArchAll
|| ArchFlags
.empty())
1753 MachO::mach_header H
;
1754 MachO::mach_header_64 H_64
;
1756 const char *McpuDefault
, *ArchFlag
;
1757 if (MachO
->is64Bit()) {
1758 H_64
= MachO
->MachOObjectFile::getHeader64();
1759 T
= MachOObjectFile::getArchTriple(H_64
.cputype
, H_64
.cpusubtype
,
1760 &McpuDefault
, &ArchFlag
);
1762 H
= MachO
->MachOObjectFile::getHeader();
1763 T
= MachOObjectFile::getArchTriple(H
.cputype
, H
.cpusubtype
,
1764 &McpuDefault
, &ArchFlag
);
1766 const std::string
ArchFlagName(ArchFlag
);
1767 if (none_of(ArchFlags
, [&](const std::string
&Name
) {
1768 return Name
== ArchFlagName
;
1770 error("No architecture specified", Filename
);
1776 static void dumpSymbolNamesFromFile(std::string
&Filename
) {
1777 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> BufferOrErr
=
1778 MemoryBuffer::getFileOrSTDIN(Filename
);
1779 if (error(BufferOrErr
.getError(), Filename
))
1782 LLVMContext Context
;
1783 LLVMContext
*ContextPtr
= NoLLVMBitcode
? nullptr : &Context
;
1784 Expected
<std::unique_ptr
<Binary
>> BinaryOrErr
=
1785 createBinary(BufferOrErr
.get()->getMemBufferRef(), ContextPtr
);
1787 error(BinaryOrErr
.takeError(), Filename
);
1790 Binary
&Bin
= *BinaryOrErr
.get();
1792 if (Archive
*A
= dyn_cast
<Archive
>(&Bin
)) {
1794 Archive::symbol_iterator I
= A
->symbol_begin();
1795 Archive::symbol_iterator E
= A
->symbol_end();
1797 outs() << "Archive map\n";
1798 for (; I
!= E
; ++I
) {
1799 Expected
<Archive::Child
> C
= I
->getMember();
1801 error(C
.takeError(), Filename
);
1804 Expected
<StringRef
> FileNameOrErr
= C
->getName();
1805 if (!FileNameOrErr
) {
1806 error(FileNameOrErr
.takeError(), Filename
);
1809 StringRef SymName
= I
->getName();
1810 outs() << SymName
<< " in " << FileNameOrErr
.get() << "\n";
1817 Error Err
= Error::success();
1818 for (auto &C
: A
->children(Err
)) {
1819 Expected
<std::unique_ptr
<Binary
>> ChildOrErr
=
1820 C
.getAsBinary(ContextPtr
);
1822 if (auto E
= isNotObjectErrorInvalidFileType(ChildOrErr
.takeError()))
1823 error(std::move(E
), Filename
, C
);
1826 if (SymbolicFile
*O
= dyn_cast
<SymbolicFile
>(&*ChildOrErr
.get())) {
1827 if (!MachOPrintSizeWarning
&& PrintSize
&& isa
<MachOObjectFile
>(O
)) {
1828 WithColor::warning(errs(), ToolName
)
1829 << "sizes with -print-size for Mach-O files are always zero.\n";
1830 MachOPrintSizeWarning
= true;
1832 if (!checkMachOAndArchFlags(O
, Filename
))
1834 if (!PrintFileName
) {
1836 if (isa
<MachOObjectFile
>(O
)) {
1837 outs() << Filename
<< "(" << O
->getFileName() << ")";
1839 outs() << O
->getFileName();
1842 dumpSymbolNamesFromObject(*O
, false, Filename
);
1846 error(std::move(Err
), A
->getFileName());
1850 if (MachOUniversalBinary
*UB
= dyn_cast
<MachOUniversalBinary
>(&Bin
)) {
1851 // If we have a list of architecture flags specified dump only those.
1852 if (!ArchAll
&& !ArchFlags
.empty()) {
1853 // Look for a slice in the universal binary that matches each ArchFlag.
1855 for (unsigned i
= 0; i
< ArchFlags
.size(); ++i
) {
1857 for (MachOUniversalBinary::object_iterator I
= UB
->begin_objects(),
1858 E
= UB
->end_objects();
1860 if (ArchFlags
[i
] == I
->getArchFlagName()) {
1862 Expected
<std::unique_ptr
<ObjectFile
>> ObjOrErr
=
1863 I
->getAsObjectFile();
1864 std::string ArchiveName
;
1865 std::string ArchitectureName
;
1866 ArchiveName
.clear();
1867 ArchitectureName
.clear();
1869 ObjectFile
&Obj
= *ObjOrErr
.get();
1870 if (ArchFlags
.size() > 1) {
1872 ArchitectureName
= I
->getArchFlagName();
1874 outs() << "\n" << Obj
.getFileName() << " (for architecture "
1875 << I
->getArchFlagName() << ")"
1878 dumpSymbolNamesFromObject(Obj
, false, ArchiveName
,
1880 } else if (auto E
= isNotObjectErrorInvalidFileType(
1881 ObjOrErr
.takeError())) {
1882 error(std::move(E
), Filename
, ArchFlags
.size() > 1 ?
1883 StringRef(I
->getArchFlagName()) : StringRef());
1885 } else if (Expected
<std::unique_ptr
<Archive
>> AOrErr
=
1886 I
->getAsArchive()) {
1887 std::unique_ptr
<Archive
> &A
= *AOrErr
;
1888 Error Err
= Error::success();
1889 for (auto &C
: A
->children(Err
)) {
1890 Expected
<std::unique_ptr
<Binary
>> ChildOrErr
=
1891 C
.getAsBinary(ContextPtr
);
1893 if (auto E
= isNotObjectErrorInvalidFileType(
1894 ChildOrErr
.takeError())) {
1895 error(std::move(E
), Filename
, C
, ArchFlags
.size() > 1 ?
1896 StringRef(I
->getArchFlagName()) : StringRef());
1900 if (SymbolicFile
*O
=
1901 dyn_cast
<SymbolicFile
>(&*ChildOrErr
.get())) {
1902 if (PrintFileName
) {
1903 ArchiveName
= A
->getFileName();
1904 if (ArchFlags
.size() > 1)
1905 ArchitectureName
= I
->getArchFlagName();
1907 outs() << "\n" << A
->getFileName();
1908 outs() << "(" << O
->getFileName() << ")";
1909 if (ArchFlags
.size() > 1) {
1910 outs() << " (for architecture " << I
->getArchFlagName()
1915 dumpSymbolNamesFromObject(*O
, false, ArchiveName
,
1920 error(std::move(Err
), A
->getFileName());
1922 consumeError(AOrErr
.takeError());
1923 error(Filename
+ " for architecture " +
1924 StringRef(I
->getArchFlagName()) +
1925 " is not a Mach-O file or an archive file",
1926 "Mach-O universal file");
1932 "file: " + Filename
+ " does not contain architecture");
1938 // No architecture flags were specified so if this contains a slice that
1939 // matches the host architecture dump only that.
1941 Triple HostTriple
= MachOObjectFile::getHostArch();
1942 StringRef HostArchName
= HostTriple
.getArchName();
1943 for (MachOUniversalBinary::object_iterator I
= UB
->begin_objects(),
1944 E
= UB
->end_objects();
1946 if (HostArchName
== I
->getArchFlagName()) {
1947 Expected
<std::unique_ptr
<ObjectFile
>> ObjOrErr
= I
->getAsObjectFile();
1948 std::string ArchiveName
;
1950 ObjectFile
&Obj
= *ObjOrErr
.get();
1951 dumpSymbolNamesFromObject(Obj
, false);
1952 } else if (auto E
= isNotObjectErrorInvalidFileType(
1953 ObjOrErr
.takeError())) {
1954 error(std::move(E
), Filename
);
1956 } else if (Expected
<std::unique_ptr
<Archive
>> AOrErr
=
1957 I
->getAsArchive()) {
1958 std::unique_ptr
<Archive
> &A
= *AOrErr
;
1959 Error Err
= Error::success();
1960 for (auto &C
: A
->children(Err
)) {
1961 Expected
<std::unique_ptr
<Binary
>> ChildOrErr
=
1962 C
.getAsBinary(ContextPtr
);
1964 if (auto E
= isNotObjectErrorInvalidFileType(
1965 ChildOrErr
.takeError()))
1966 error(std::move(E
), Filename
, C
);
1969 if (SymbolicFile
*O
=
1970 dyn_cast
<SymbolicFile
>(&*ChildOrErr
.get())) {
1972 ArchiveName
= A
->getFileName();
1974 outs() << "\n" << A
->getFileName() << "(" << O
->getFileName()
1977 dumpSymbolNamesFromObject(*O
, false, ArchiveName
);
1981 error(std::move(Err
), A
->getFileName());
1983 consumeError(AOrErr
.takeError());
1984 error(Filename
+ " for architecture " +
1985 StringRef(I
->getArchFlagName()) +
1986 " is not a Mach-O file or an archive file",
1987 "Mach-O universal file");
1993 // Either all architectures have been specified or none have been specified
1994 // and this does not contain the host architecture so dump all the slices.
1995 bool moreThanOneArch
= UB
->getNumberOfObjects() > 1;
1996 for (const MachOUniversalBinary::ObjectForArch
&O
: UB
->objects()) {
1997 Expected
<std::unique_ptr
<ObjectFile
>> ObjOrErr
= O
.getAsObjectFile();
1998 std::string ArchiveName
;
1999 std::string ArchitectureName
;
2000 ArchiveName
.clear();
2001 ArchitectureName
.clear();
2003 ObjectFile
&Obj
= *ObjOrErr
.get();
2004 if (PrintFileName
) {
2005 if (isa
<MachOObjectFile
>(Obj
) && moreThanOneArch
)
2006 ArchitectureName
= O
.getArchFlagName();
2008 if (moreThanOneArch
)
2010 outs() << Obj
.getFileName();
2011 if (isa
<MachOObjectFile
>(Obj
) && moreThanOneArch
)
2012 outs() << " (for architecture " << O
.getArchFlagName() << ")";
2015 dumpSymbolNamesFromObject(Obj
, false, ArchiveName
, ArchitectureName
);
2016 } else if (auto E
= isNotObjectErrorInvalidFileType(
2017 ObjOrErr
.takeError())) {
2018 error(std::move(E
), Filename
, moreThanOneArch
?
2019 StringRef(O
.getArchFlagName()) : StringRef());
2021 } else if (Expected
<std::unique_ptr
<Archive
>> AOrErr
=
2023 std::unique_ptr
<Archive
> &A
= *AOrErr
;
2024 Error Err
= Error::success();
2025 for (auto &C
: A
->children(Err
)) {
2026 Expected
<std::unique_ptr
<Binary
>> ChildOrErr
=
2027 C
.getAsBinary(ContextPtr
);
2029 if (auto E
= isNotObjectErrorInvalidFileType(
2030 ChildOrErr
.takeError()))
2031 error(std::move(E
), Filename
, C
, moreThanOneArch
?
2032 StringRef(ArchitectureName
) : StringRef());
2035 if (SymbolicFile
*F
= dyn_cast
<SymbolicFile
>(&*ChildOrErr
.get())) {
2036 if (PrintFileName
) {
2037 ArchiveName
= A
->getFileName();
2038 if (isa
<MachOObjectFile
>(F
) && moreThanOneArch
)
2039 ArchitectureName
= O
.getArchFlagName();
2041 outs() << "\n" << A
->getFileName();
2042 if (isa
<MachOObjectFile
>(F
)) {
2043 outs() << "(" << F
->getFileName() << ")";
2044 if (moreThanOneArch
)
2045 outs() << " (for architecture " << O
.getArchFlagName()
2048 outs() << ":" << F
->getFileName();
2051 dumpSymbolNamesFromObject(*F
, false, ArchiveName
, ArchitectureName
);
2055 error(std::move(Err
), A
->getFileName());
2057 consumeError(AOrErr
.takeError());
2058 error(Filename
+ " for architecture " +
2059 StringRef(O
.getArchFlagName()) +
2060 " is not a Mach-O file or an archive file",
2061 "Mach-O universal file");
2066 if (SymbolicFile
*O
= dyn_cast
<SymbolicFile
>(&Bin
)) {
2067 if (!MachOPrintSizeWarning
&& PrintSize
&& isa
<MachOObjectFile
>(O
)) {
2068 WithColor::warning(errs(), ToolName
)
2069 << "sizes with --print-size for Mach-O files are always zero.\n";
2070 MachOPrintSizeWarning
= true;
2072 if (!checkMachOAndArchFlags(O
, Filename
))
2074 dumpSymbolNamesFromObject(*O
, true);
2078 int main(int argc
, char **argv
) {
2079 InitLLVM
X(argc
, argv
);
2080 cl::HideUnrelatedOptions(NMCat
);
2081 cl::ParseCommandLineOptions(argc
, argv
, "llvm symbol table dumper\n");
2083 // llvm-nm only reads binary files.
2084 if (error(sys::ChangeStdinToBinary()))
2087 // These calls are needed so that we can read bitcode correctly.
2088 llvm::InitializeAllTargetInfos();
2089 llvm::InitializeAllTargetMCs();
2090 llvm::InitializeAllAsmParsers();
2096 OutputFormat
= posix
;
2098 OutputFormat
= darwin
;
2100 // The relative order of these is important. If you pass --size-sort it should
2101 // only print out the size. However, if you pass -S --size-sort, it should
2102 // print out both the size and address.
2103 if (SizeSort
&& !PrintSize
)
2104 PrintAddress
= false;
2105 if (OutputFormat
== sysv
|| SizeSort
)
2107 if (InputFilenames
.empty())
2108 InputFilenames
.push_back("a.out");
2109 if (InputFilenames
.size() > 1)
2110 MultipleFiles
= true;
2112 // If both --demangle and --no-demangle are specified then pick the last one.
2113 if (NoDemangle
.getPosition() > Demangle
.getPosition())
2114 Demangle
= !NoDemangle
;
2116 for (unsigned i
= 0; i
< ArchFlags
.size(); ++i
) {
2117 if (ArchFlags
[i
] == "all") {
2120 if (!MachOObjectFile::isValidArch(ArchFlags
[i
]))
2121 error("Unknown architecture named '" + ArchFlags
[i
] + "'",
2122 "for the --arch option");
2126 if (!SegSect
.empty() && SegSect
.size() != 2)
2127 error("bad number of arguments (must be two arguments)",
2128 "for the -s option");
2130 if (NoDyldInfo
&& (AddDyldInfo
|| DyldInfoOnly
))
2131 error("--no-dyldinfo can't be used with --add-dyldinfo or --dyldinfo-only");
2133 llvm::for_each(InputFilenames
, dumpSymbolNamesFromFile
);