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 instead of 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 // FIXME: This option takes exactly two strings and should be allowed anywhere
187 // on the command line. Such that "llvm-nm -s __TEXT __text foo.o" would work.
188 // But that does not as the CommandLine Library does not have a way to make
189 // this work. For now the "-s __TEXT __text" has to be last on the command
191 cl::list
<std::string
> SegSect("s", cl::Positional
, cl::ZeroOrMore
,
192 cl::value_desc("segment section"), cl::Hidden
,
193 cl::desc("Dump only symbols from this segment "
194 "and section name, Mach-O only"),
197 cl::opt
<bool> FormatMachOasHex("x",
198 cl::desc("Print symbol entry in hex, "
200 cl::Grouping
, cl::cat(NMCat
));
201 cl::opt
<bool> AddDyldInfo("add-dyldinfo",
202 cl::desc("Add symbols from the dyldinfo not already "
203 "in the symbol table, Mach-O only"),
205 cl::opt
<bool> NoDyldInfo("no-dyldinfo",
206 cl::desc("Don't add any symbols from the dyldinfo, "
209 cl::opt
<bool> DyldInfoOnly("dyldinfo-only",
210 cl::desc("Show only symbols from the dyldinfo, "
214 cl::opt
<bool> NoLLVMBitcode("no-llvm-bc",
215 cl::desc("Disable LLVM bitcode reader"),
218 cl::extrahelp
HelpResponse("\nPass @FILE as argument to read options from FILE.\n");
220 bool PrintAddress
= true;
222 bool MultipleFiles
= false;
224 bool HadError
= false;
226 std::string ToolName
;
227 } // anonymous namespace
229 static void error(Twine Message
, Twine Path
= Twine()) {
231 WithColor::error(errs(), ToolName
) << Path
<< ": " << Message
<< ".\n";
234 static bool error(std::error_code EC
, Twine Path
= Twine()) {
236 error(EC
.message(), Path
);
242 // This version of error() prints the archive name and member name, for example:
243 // "libx.a(foo.o)" after the ToolName before the error message. It sets
244 // HadError but returns allowing the code to move on to other archive members.
245 static void error(llvm::Error E
, StringRef FileName
, const Archive::Child
&C
,
246 StringRef ArchitectureName
= StringRef()) {
248 WithColor::error(errs(), ToolName
) << FileName
;
250 Expected
<StringRef
> NameOrErr
= C
.getName();
251 // TODO: if we have a error getting the name then it would be nice to print
252 // the index of which archive member this is and or its offset in the
253 // archive instead of "???" as the name.
255 consumeError(NameOrErr
.takeError());
256 errs() << "(" << "???" << ")";
258 errs() << "(" << NameOrErr
.get() << ")";
260 if (!ArchitectureName
.empty())
261 errs() << " (for architecture " << ArchitectureName
<< ") ";
264 raw_string_ostream
OS(Buf
);
265 logAllUnhandledErrors(std::move(E
), OS
);
267 errs() << " " << Buf
<< "\n";
270 // This version of error() prints the file name and which architecture slice it
271 // is from, for example: "foo.o (for architecture i386)" after the ToolName
272 // before the error message. It sets HadError but returns allowing the code to
273 // move on to other architecture slices.
274 static void error(llvm::Error E
, StringRef FileName
,
275 StringRef ArchitectureName
= StringRef()) {
277 WithColor::error(errs(), ToolName
) << FileName
;
279 if (!ArchitectureName
.empty())
280 errs() << " (for architecture " << ArchitectureName
<< ") ";
283 raw_string_ostream
OS(Buf
);
284 logAllUnhandledErrors(std::move(E
), OS
);
286 errs() << " " << Buf
<< "\n";
295 StringRef SectionName
;
298 // The Sym field above points to the native symbol in the object file,
299 // for Mach-O when we are creating symbols from the dyld info the above
300 // pointer is null as there is no native symbol. In these cases the fields
301 // below are filled in to represent what would have been a Mach-O nlist
308 StringRef IndirectName
;
310 } // anonymous namespace
312 static bool compareSymbolAddress(const NMSymbol
&A
, const NMSymbol
&B
) {
314 if (A
.Sym
.getRawDataRefImpl().p
)
315 ADefined
= !(A
.Sym
.getFlags() & SymbolRef::SF_Undefined
);
317 ADefined
= A
.TypeChar
!= 'U';
319 if (B
.Sym
.getRawDataRefImpl().p
)
320 BDefined
= !(B
.Sym
.getFlags() & SymbolRef::SF_Undefined
);
322 BDefined
= B
.TypeChar
!= 'U';
323 return std::make_tuple(ADefined
, A
.Address
, A
.Name
, A
.Size
) <
324 std::make_tuple(BDefined
, B
.Address
, B
.Name
, B
.Size
);
327 static bool compareSymbolSize(const NMSymbol
&A
, const NMSymbol
&B
) {
328 return std::make_tuple(A
.Size
, A
.Name
, A
.Address
) <
329 std::make_tuple(B
.Size
, B
.Name
, B
.Address
);
332 static bool compareSymbolName(const NMSymbol
&A
, const NMSymbol
&B
) {
333 return std::make_tuple(A
.Name
, A
.Size
, A
.Address
) <
334 std::make_tuple(B
.Name
, B
.Size
, B
.Address
);
337 static char isSymbolList64Bit(SymbolicFile
&Obj
) {
338 if (auto *IRObj
= dyn_cast
<IRObjectFile
>(&Obj
))
339 return Triple(IRObj
->getTargetTriple()).isArch64Bit();
340 if (isa
<COFFObjectFile
>(Obj
) || isa
<COFFImportFile
>(Obj
))
342 if (isa
<WasmObjectFile
>(Obj
))
344 if (MachOObjectFile
*MachO
= dyn_cast
<MachOObjectFile
>(&Obj
))
345 return MachO
->is64Bit();
346 return cast
<ELFObjectFileBase
>(Obj
).getBytesInAddress() == 8;
349 static StringRef CurrentFilename
;
350 static std::vector
<NMSymbol
> SymbolList
;
352 static char getSymbolNMTypeChar(IRObjectFile
&Obj
, basic_symbol_iterator I
);
354 // darwinPrintSymbol() is used to print a symbol from a Mach-O file when the
355 // the OutputFormat is darwin or we are printing Mach-O symbols in hex. For
356 // the darwin format it produces the same output as darwin's nm(1) -m output
357 // and when printing Mach-O symbols in hex it produces the same output as
358 // darwin's nm(1) -x format.
359 static void darwinPrintSymbol(SymbolicFile
&Obj
, const NMSymbol
&S
,
360 char *SymbolAddrStr
, const char *printBlanks
,
361 const char *printDashes
,
362 const char *printFormat
) {
363 MachO::mach_header H
;
364 MachO::mach_header_64 H_64
;
365 uint32_t Filetype
= MachO::MH_OBJECT
;
372 MachOObjectFile
*MachO
= dyn_cast
<MachOObjectFile
>(&Obj
);
374 uint32_t SymFlags
= S
.Sym
.getFlags();
375 if (SymFlags
& SymbolRef::SF_Global
)
376 NType
|= MachO::N_EXT
;
377 if (SymFlags
& SymbolRef::SF_Hidden
)
378 NType
|= MachO::N_PEXT
;
379 if (SymFlags
& SymbolRef::SF_Undefined
)
380 NType
|= MachO::N_EXT
| MachO::N_UNDF
;
382 // Here we have a symbol definition. So to fake out a section name we
383 // use 1, 2 and 3 for section numbers. See below where they are used to
384 // print out fake section names.
385 NType
|= MachO::N_SECT
;
386 if (SymFlags
& SymbolRef::SF_Const
)
388 else if (SymFlags
& SymbolRef::SF_Executable
)
393 if (SymFlags
& SymbolRef::SF_Weak
)
394 NDesc
|= MachO::N_WEAK_DEF
;
396 DataRefImpl SymDRI
= S
.Sym
.getRawDataRefImpl();
397 if (MachO
->is64Bit()) {
398 H_64
= MachO
->MachOObjectFile::getHeader64();
399 Filetype
= H_64
.filetype
;
402 MachO::nlist_64 STE_64
= MachO
->getSymbol64TableEntry(SymDRI
);
403 NType
= STE_64
.n_type
;
404 NSect
= STE_64
.n_sect
;
405 NDesc
= STE_64
.n_desc
;
406 NStrx
= STE_64
.n_strx
;
407 NValue
= STE_64
.n_value
;
416 H
= MachO
->MachOObjectFile::getHeader();
417 Filetype
= H
.filetype
;
420 MachO::nlist STE
= MachO
->getSymbolTableEntry(SymDRI
);
425 NValue
= STE
.n_value
;
436 // If we are printing Mach-O symbols in hex do that and return.
437 if (FormatMachOasHex
) {
438 outs() << format(printFormat
, NValue
) << ' '
439 << format("%02x %02x %04x %08x", NType
, NSect
, NDesc
, NStrx
) << ' '
441 if ((NType
& MachO::N_TYPE
) == MachO::N_INDR
) {
442 outs() << " (indirect for ";
443 outs() << format(printFormat
, NValue
) << ' ';
444 StringRef IndirectName
;
445 if (S
.Sym
.getRawDataRefImpl().p
) {
446 if (MachO
->getIndirectName(S
.Sym
.getRawDataRefImpl(), IndirectName
))
449 outs() << IndirectName
<< ")";
451 outs() << S
.IndirectName
<< ")";
458 if ((NType
& MachO::N_TYPE
) == MachO::N_INDR
)
459 strcpy(SymbolAddrStr
, printBlanks
);
460 if (Obj
.isIR() && (NType
& MachO::N_TYPE
) == MachO::N_TYPE
)
461 strcpy(SymbolAddrStr
, printDashes
);
462 outs() << SymbolAddrStr
<< ' ';
465 switch (NType
& MachO::N_TYPE
) {
468 outs() << "(common) ";
469 if (MachO::GET_COMM_ALIGN(NDesc
) != 0)
470 outs() << "(alignment 2^" << (int)MachO::GET_COMM_ALIGN(NDesc
) << ") ";
472 if ((NType
& MachO::N_TYPE
) == MachO::N_PBUD
)
473 outs() << "(prebound ";
476 if ((NDesc
& MachO::REFERENCE_TYPE
) ==
477 MachO::REFERENCE_FLAG_UNDEFINED_LAZY
)
478 outs() << "undefined [lazy bound]) ";
479 else if ((NDesc
& MachO::REFERENCE_TYPE
) ==
480 MachO::REFERENCE_FLAG_PRIVATE_UNDEFINED_LAZY
)
481 outs() << "undefined [private lazy bound]) ";
482 else if ((NDesc
& MachO::REFERENCE_TYPE
) ==
483 MachO::REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY
)
484 outs() << "undefined [private]) ";
486 outs() << "undefined) ";
490 outs() << "(absolute) ";
493 outs() << "(indirect) ";
495 case MachO::N_SECT
: {
497 // For llvm bitcode files print out a fake section name using the values
498 // use 1, 2 and 3 for section numbers as set above.
500 outs() << "(LTO,CODE) ";
502 outs() << "(LTO,DATA) ";
504 outs() << "(LTO,RODATA) ";
509 section_iterator Sec
= SectionRef();
510 if (S
.Sym
.getRawDataRefImpl().p
) {
511 Expected
<section_iterator
> SecOrErr
=
512 MachO
->getSymbolSection(S
.Sym
.getRawDataRefImpl());
514 consumeError(SecOrErr
.takeError());
519 if (Sec
== MachO
->section_end()) {
526 DataRefImpl Ref
= Sec
->getRawDataRefImpl();
527 StringRef SectionName
;
528 if (Expected
<StringRef
> NameOrErr
= MachO
->getSectionName(Ref
))
529 SectionName
= *NameOrErr
;
530 StringRef SegmentName
= MachO
->getSectionFinalSegmentName(Ref
);
531 outs() << "(" << SegmentName
<< "," << SectionName
<< ") ";
539 if (NType
& MachO::N_EXT
) {
540 if (NDesc
& MachO::REFERENCED_DYNAMICALLY
)
541 outs() << "[referenced dynamically] ";
542 if (NType
& MachO::N_PEXT
) {
543 if ((NDesc
& MachO::N_WEAK_DEF
) == MachO::N_WEAK_DEF
)
544 outs() << "weak private external ";
546 outs() << "private external ";
548 if ((NDesc
& MachO::N_WEAK_REF
) == MachO::N_WEAK_REF
||
549 (NDesc
& MachO::N_WEAK_DEF
) == MachO::N_WEAK_DEF
) {
550 if ((NDesc
& (MachO::N_WEAK_REF
| MachO::N_WEAK_DEF
)) ==
551 (MachO::N_WEAK_REF
| MachO::N_WEAK_DEF
))
552 outs() << "weak external automatically hidden ";
554 outs() << "weak external ";
556 outs() << "external ";
559 if (NType
& MachO::N_PEXT
)
560 outs() << "non-external (was a private external) ";
562 outs() << "non-external ";
565 if (Filetype
== MachO::MH_OBJECT
) {
566 if (NDesc
& MachO::N_NO_DEAD_STRIP
)
567 outs() << "[no dead strip] ";
568 if ((NType
& MachO::N_TYPE
) != MachO::N_UNDF
&&
569 NDesc
& MachO::N_SYMBOL_RESOLVER
)
570 outs() << "[symbol resolver] ";
571 if ((NType
& MachO::N_TYPE
) != MachO::N_UNDF
&& NDesc
& MachO::N_ALT_ENTRY
)
572 outs() << "[alt entry] ";
573 if ((NType
& MachO::N_TYPE
) != MachO::N_UNDF
&& NDesc
& MachO::N_COLD_FUNC
)
574 outs() << "[cold func] ";
577 if ((NDesc
& MachO::N_ARM_THUMB_DEF
) == MachO::N_ARM_THUMB_DEF
)
578 outs() << "[Thumb] ";
580 if ((NType
& MachO::N_TYPE
) == MachO::N_INDR
) {
581 outs() << S
.Name
<< " (for ";
582 StringRef IndirectName
;
584 if (S
.Sym
.getRawDataRefImpl().p
) {
585 if (MachO
->getIndirectName(S
.Sym
.getRawDataRefImpl(), IndirectName
))
588 outs() << IndirectName
<< ")";
590 outs() << S
.IndirectName
<< ")";
596 if ((Flags
& MachO::MH_TWOLEVEL
) == MachO::MH_TWOLEVEL
&&
597 (((NType
& MachO::N_TYPE
) == MachO::N_UNDF
&& NValue
== 0) ||
598 (NType
& MachO::N_TYPE
) == MachO::N_PBUD
)) {
599 uint32_t LibraryOrdinal
= MachO::GET_LIBRARY_ORDINAL(NDesc
);
600 if (LibraryOrdinal
!= 0) {
601 if (LibraryOrdinal
== MachO::EXECUTABLE_ORDINAL
)
602 outs() << " (from executable)";
603 else if (LibraryOrdinal
== MachO::DYNAMIC_LOOKUP_ORDINAL
)
604 outs() << " (dynamically looked up)";
606 StringRef LibraryName
;
608 MachO
->getLibraryShortNameByIndex(LibraryOrdinal
- 1, LibraryName
))
609 outs() << " (from bad library ordinal " << LibraryOrdinal
<< ")";
611 outs() << " (from " << LibraryName
<< ")";
619 // Table that maps Darwin's Mach-O stab constants to strings to allow printing.
620 struct DarwinStabName
{
624 static const struct DarwinStabName DarwinStabNames
[] = {
625 {MachO::N_GSYM
, "GSYM"},
626 {MachO::N_FNAME
, "FNAME"},
627 {MachO::N_FUN
, "FUN"},
628 {MachO::N_STSYM
, "STSYM"},
629 {MachO::N_LCSYM
, "LCSYM"},
630 {MachO::N_BNSYM
, "BNSYM"},
632 {MachO::N_AST
, "AST"},
633 {MachO::N_OPT
, "OPT"},
634 {MachO::N_RSYM
, "RSYM"},
635 {MachO::N_SLINE
, "SLINE"},
636 {MachO::N_ENSYM
, "ENSYM"},
637 {MachO::N_SSYM
, "SSYM"},
639 {MachO::N_OSO
, "OSO"},
640 {MachO::N_LSYM
, "LSYM"},
641 {MachO::N_BINCL
, "BINCL"},
642 {MachO::N_SOL
, "SOL"},
643 {MachO::N_PARAMS
, "PARAM"},
644 {MachO::N_VERSION
, "VERS"},
645 {MachO::N_OLEVEL
, "OLEV"},
646 {MachO::N_PSYM
, "PSYM"},
647 {MachO::N_EINCL
, "EINCL"},
648 {MachO::N_ENTRY
, "ENTRY"},
649 {MachO::N_LBRAC
, "LBRAC"},
650 {MachO::N_EXCL
, "EXCL"},
651 {MachO::N_RBRAC
, "RBRAC"},
652 {MachO::N_BCOMM
, "BCOMM"},
653 {MachO::N_ECOMM
, "ECOMM"},
654 {MachO::N_ECOML
, "ECOML"},
655 {MachO::N_LENG
, "LENG"},
658 static const char *getDarwinStabString(uint8_t NType
) {
659 for (auto I
: makeArrayRef(DarwinStabNames
))
660 if (I
.NType
== NType
)
665 // darwinPrintStab() prints the n_sect, n_desc along with a symbolic name of
666 // a stab n_type value in a Mach-O file.
667 static void darwinPrintStab(MachOObjectFile
*MachO
, const NMSymbol
&S
) {
668 MachO::nlist_64 STE_64
;
673 DataRefImpl SymDRI
= S
.Sym
.getRawDataRefImpl();
674 if (MachO
->is64Bit()) {
675 STE_64
= MachO
->getSymbol64TableEntry(SymDRI
);
676 NType
= STE_64
.n_type
;
677 NSect
= STE_64
.n_sect
;
678 NDesc
= STE_64
.n_desc
;
680 STE
= MachO
->getSymbolTableEntry(SymDRI
);
686 outs() << format(" %02x %04x ", NSect
, NDesc
);
687 if (const char *stabString
= getDarwinStabString(NType
))
688 outs() << format("%5.5s", stabString
);
690 outs() << format(" %02x", NType
);
693 static Optional
<std::string
> demangle(StringRef Name
, bool StripUnderscore
) {
694 if (StripUnderscore
&& !Name
.empty() && Name
[0] == '_')
695 Name
= Name
.substr(1);
697 if (!Name
.startswith("_Z"))
702 itaniumDemangle(Name
.str().c_str(), nullptr, nullptr, &Status
);
706 std::string
S(Undecorated
);
711 static bool symbolIsDefined(const NMSymbol
&Sym
) {
712 return Sym
.TypeChar
!= 'U' && Sym
.TypeChar
!= 'w' && Sym
.TypeChar
!= 'v';
715 static void sortAndPrintSymbolList(SymbolicFile
&Obj
, bool printName
,
716 const std::string
&ArchiveName
,
717 const std::string
&ArchitectureName
) {
719 std::function
<bool(const NMSymbol
&, const NMSymbol
&)> Cmp
;
721 Cmp
= compareSymbolAddress
;
723 Cmp
= compareSymbolSize
;
725 Cmp
= compareSymbolName
;
728 Cmp
= [=](const NMSymbol
&A
, const NMSymbol
&B
) { return Cmp(B
, A
); };
729 llvm::sort(SymbolList
, Cmp
);
732 if (!PrintFileName
) {
733 if (OutputFormat
== posix
&& MultipleFiles
&& printName
) {
734 outs() << '\n' << CurrentFilename
<< ":\n";
735 } else if (OutputFormat
== bsd
&& MultipleFiles
&& printName
) {
736 outs() << "\n" << CurrentFilename
<< ":\n";
737 } else if (OutputFormat
== sysv
) {
738 outs() << "\n\nSymbols from " << CurrentFilename
<< ":\n\n";
739 if (isSymbolList64Bit(Obj
))
740 outs() << "Name Value Class Type"
741 << " Size Line Section\n";
743 outs() << "Name Value Class Type"
744 << " Size Line Section\n";
748 const char *printBlanks
, *printDashes
, *printFormat
;
749 if (isSymbolList64Bit(Obj
)) {
751 printDashes
= "----------------";
752 switch (AddressRadix
) {
754 printFormat
= OutputFormat
== posix
? "%" PRIo64
: "%016" PRIo64
;
757 printFormat
= OutputFormat
== posix
? "%" PRIx64
: "%016" PRIx64
;
760 printFormat
= OutputFormat
== posix
? "%" PRId64
: "%016" PRId64
;
764 printDashes
= "--------";
765 switch (AddressRadix
) {
767 printFormat
= OutputFormat
== posix
? "%" PRIo64
: "%08" PRIo64
;
770 printFormat
= OutputFormat
== posix
? "%" PRIx64
: "%08" PRIx64
;
773 printFormat
= OutputFormat
== posix
? "%" PRId64
: "%08" PRId64
;
777 auto writeFileName
= [&](raw_ostream
&S
) {
778 if (!ArchitectureName
.empty())
779 S
<< "(for architecture " << ArchitectureName
<< "):";
780 if (OutputFormat
== posix
&& !ArchiveName
.empty())
781 S
<< ArchiveName
<< "[" << CurrentFilename
<< "]: ";
783 if (!ArchiveName
.empty())
784 S
<< ArchiveName
<< ":";
785 S
<< CurrentFilename
<< ": ";
789 if (SymbolList
.empty()) {
791 writeFileName(errs());
792 errs() << "no symbols\n";
795 for (const NMSymbol
&S
: SymbolList
) {
797 std::string Name
= S
.Name
.str();
798 MachOObjectFile
*MachO
= dyn_cast
<MachOObjectFile
>(&Obj
);
800 if (Optional
<std::string
> Opt
= demangle(S
.Name
, MachO
))
803 if (S
.Sym
.getRawDataRefImpl().p
)
804 SymFlags
= S
.Sym
.getFlags();
806 SymFlags
= S
.SymFlags
;
808 bool Undefined
= SymFlags
& SymbolRef::SF_Undefined
;
809 bool Global
= SymFlags
& SymbolRef::SF_Global
;
810 bool Weak
= SymFlags
& SymbolRef::SF_Weak
;
811 if ((!Undefined
&& UndefinedOnly
) || (Undefined
&& DefinedOnly
) ||
812 (!Global
&& ExternalOnly
) || (Weak
&& NoWeakSymbols
))
815 writeFileName(outs());
816 if ((JustSymbolName
||
817 (UndefinedOnly
&& MachO
&& OutputFormat
!= darwin
)) &&
818 OutputFormat
!= posix
) {
819 outs() << Name
<< "\n";
823 char SymbolAddrStr
[23], SymbolSizeStr
[23];
825 // If the format is SysV or the symbol isn't defined, then print spaces.
826 if (OutputFormat
== sysv
|| !symbolIsDefined(S
)) {
827 if (OutputFormat
== posix
) {
828 format(printFormat
, S
.Address
)
829 .print(SymbolAddrStr
, sizeof(SymbolAddrStr
));
830 format(printFormat
, S
.Size
).print(SymbolSizeStr
, sizeof(SymbolSizeStr
));
832 strcpy(SymbolAddrStr
, printBlanks
);
833 strcpy(SymbolSizeStr
, printBlanks
);
837 if (symbolIsDefined(S
)) {
838 // Otherwise, print the symbol address and size.
840 strcpy(SymbolAddrStr
, printDashes
);
841 else if (MachO
&& S
.TypeChar
== 'I')
842 strcpy(SymbolAddrStr
, printBlanks
);
844 format(printFormat
, S
.Address
)
845 .print(SymbolAddrStr
, sizeof(SymbolAddrStr
));
846 format(printFormat
, S
.Size
).print(SymbolSizeStr
, sizeof(SymbolSizeStr
));
849 // If OutputFormat is darwin or we are printing Mach-O symbols in hex and
850 // we have a MachOObjectFile, call darwinPrintSymbol to print as darwin's
851 // nm(1) -m output or hex, else if OutputFormat is darwin or we are
852 // printing Mach-O symbols in hex and not a Mach-O object fall back to
853 // OutputFormat bsd (see below).
854 if ((OutputFormat
== darwin
|| FormatMachOasHex
) && (MachO
|| Obj
.isIR())) {
855 darwinPrintSymbol(Obj
, S
, SymbolAddrStr
, printBlanks
, printDashes
,
857 } else if (OutputFormat
== posix
) {
858 outs() << Name
<< " " << S
.TypeChar
<< " " << SymbolAddrStr
<< " "
859 << (MachO
? "0" : SymbolSizeStr
) << "\n";
860 } else if (OutputFormat
== bsd
|| (OutputFormat
== darwin
&& !MachO
)) {
862 outs() << SymbolAddrStr
<< ' ';
864 outs() << SymbolSizeStr
<< ' ';
865 outs() << S
.TypeChar
;
866 if (S
.TypeChar
== '-' && MachO
)
867 darwinPrintStab(MachO
, S
);
868 outs() << " " << Name
;
869 if (S
.TypeChar
== 'I' && MachO
) {
870 outs() << " (indirect for ";
871 if (S
.Sym
.getRawDataRefImpl().p
) {
872 StringRef IndirectName
;
873 if (MachO
->getIndirectName(S
.Sym
.getRawDataRefImpl(), IndirectName
))
876 outs() << IndirectName
<< ")";
878 outs() << S
.IndirectName
<< ")";
881 } else if (OutputFormat
== sysv
) {
882 outs() << left_justify(Name
, 20) << "|" << SymbolAddrStr
<< "| "
883 << S
.TypeChar
<< " |" << right_justify(S
.TypeName
, 18) << "|"
884 << SymbolSizeStr
<< "| |" << S
.SectionName
<< "\n";
891 static char getSymbolNMTypeChar(ELFObjectFileBase
&Obj
,
892 basic_symbol_iterator I
) {
894 elf_symbol_iterator
SymI(I
);
896 Expected
<elf_section_iterator
> SecIOrErr
= SymI
->getSection();
898 consumeError(SecIOrErr
.takeError());
902 elf_section_iterator SecI
= *SecIOrErr
;
903 if (SecI
!= Obj
.section_end()) {
904 uint32_t Type
= SecI
->getType();
905 uint64_t Flags
= SecI
->getFlags();
906 if (Flags
& ELF::SHF_EXECINSTR
)
908 if (Type
== ELF::SHT_NOBITS
)
910 if (Flags
& ELF::SHF_ALLOC
)
911 return Flags
& ELF::SHF_WRITE
? 'd' : 'r';
914 if (SymI
->getELFType() == ELF::STT_SECTION
) {
915 Expected
<StringRef
> Name
= SymI
->getName();
917 consumeError(Name
.takeError());
920 return StringSwitch
<char>(*Name
)
921 .StartsWith(".debug", 'N')
922 .StartsWith(".note", 'n')
923 .StartsWith(".comment", 'n')
930 static char getSymbolNMTypeChar(COFFObjectFile
&Obj
, symbol_iterator I
) {
931 COFFSymbolRef Symb
= Obj
.getCOFFSymbol(*I
);
933 symbol_iterator
SymI(I
);
935 Expected
<StringRef
> Name
= SymI
->getName();
937 consumeError(Name
.takeError());
941 char Ret
= StringSwitch
<char>(*Name
)
942 .StartsWith(".debug", 'N')
943 .StartsWith(".sxdata", 'N')
949 uint32_t Characteristics
= 0;
950 if (!COFF::isReservedSectionNumber(Symb
.getSectionNumber())) {
951 Expected
<section_iterator
> SecIOrErr
= SymI
->getSection();
953 consumeError(SecIOrErr
.takeError());
956 section_iterator SecI
= *SecIOrErr
;
957 const coff_section
*Section
= Obj
.getCOFFSection(*SecI
);
958 Characteristics
= Section
->Characteristics
;
959 if (Expected
<StringRef
> NameOrErr
= Obj
.getSectionName(Section
))
960 if (NameOrErr
->startswith(".idata"))
964 switch (Symb
.getSectionNumber()) {
965 case COFF::IMAGE_SYM_DEBUG
:
968 // Check section type.
969 if (Characteristics
& COFF::IMAGE_SCN_CNT_CODE
)
971 if (Characteristics
& COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
)
972 return Characteristics
& COFF::IMAGE_SCN_MEM_WRITE
? 'd' : 'r';
973 if (Characteristics
& COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
)
975 if (Characteristics
& COFF::IMAGE_SCN_LNK_INFO
)
977 // Check for section symbol.
978 if (Symb
.isSectionDefinition())
985 static char getSymbolNMTypeChar(COFFImportFile
&Obj
) {
986 switch (Obj
.getCOFFImportHeader()->getType()) {
987 case COFF::IMPORT_CODE
:
989 case COFF::IMPORT_DATA
:
991 case COFF::IMPORT_CONST
:
997 static char getSymbolNMTypeChar(MachOObjectFile
&Obj
, basic_symbol_iterator I
) {
998 DataRefImpl Symb
= I
->getRawDataRefImpl();
999 uint8_t NType
= Obj
.is64Bit() ? Obj
.getSymbol64TableEntry(Symb
).n_type
1000 : Obj
.getSymbolTableEntry(Symb
).n_type
;
1002 if (NType
& MachO::N_STAB
)
1005 switch (NType
& MachO::N_TYPE
) {
1010 case MachO::N_SECT
: {
1011 Expected
<section_iterator
> SecOrErr
= Obj
.getSymbolSection(Symb
);
1013 consumeError(SecOrErr
.takeError());
1016 section_iterator Sec
= *SecOrErr
;
1017 if (Sec
== Obj
.section_end())
1019 DataRefImpl Ref
= Sec
->getRawDataRefImpl();
1020 StringRef SectionName
;
1021 if (Expected
<StringRef
> NameOrErr
= Obj
.getSectionName(Ref
))
1022 SectionName
= *NameOrErr
;
1023 StringRef SegmentName
= Obj
.getSectionFinalSegmentName(Ref
);
1024 if (Obj
.is64Bit() && Obj
.getHeader64().filetype
== MachO::MH_KEXT_BUNDLE
&&
1025 SegmentName
== "__TEXT_EXEC" && SectionName
== "__text")
1027 if (SegmentName
== "__TEXT" && SectionName
== "__text")
1029 if (SegmentName
== "__DATA" && SectionName
== "__data")
1031 if (SegmentName
== "__DATA" && SectionName
== "__bss")
1040 static char getSymbolNMTypeChar(WasmObjectFile
&Obj
, basic_symbol_iterator I
) {
1041 uint32_t Flags
= I
->getFlags();
1042 if (Flags
& SymbolRef::SF_Executable
)
1047 static char getSymbolNMTypeChar(IRObjectFile
&Obj
, basic_symbol_iterator I
) {
1048 uint32_t Flags
= I
->getFlags();
1049 // FIXME: should we print 'b'? At the IR level we cannot be sure if this
1050 // will be in bss or not, but we could approximate.
1051 if (Flags
& SymbolRef::SF_Executable
)
1053 else if (Triple(Obj
.getTargetTriple()).isOSDarwin() &&
1054 (Flags
& SymbolRef::SF_Const
))
1060 static bool isObject(SymbolicFile
&Obj
, basic_symbol_iterator I
) {
1061 return !dyn_cast
<ELFObjectFileBase
>(&Obj
)
1063 : elf_symbol_iterator(I
)->getELFType() == ELF::STT_OBJECT
;
1066 // For ELF object files, Set TypeName to the symbol typename, to be printed
1067 // in the 'Type' column of the SYSV format output.
1068 static StringRef
getNMTypeName(SymbolicFile
&Obj
, basic_symbol_iterator I
) {
1069 if (isa
<ELFObjectFileBase
>(&Obj
)) {
1070 elf_symbol_iterator
SymI(I
);
1071 return SymI
->getELFTypeName();
1076 // Return Posix nm class type tag (single letter), but also set SecName and
1077 // section and name, to be used in format=sysv output.
1078 static char getNMSectionTagAndName(SymbolicFile
&Obj
, basic_symbol_iterator I
,
1079 StringRef
&SecName
) {
1080 uint32_t Symflags
= I
->getFlags();
1081 if (isa
<ELFObjectFileBase
>(&Obj
)) {
1082 if (Symflags
& object::SymbolRef::SF_Absolute
)
1084 else if (Symflags
& object::SymbolRef::SF_Common
)
1086 else if (Symflags
& object::SymbolRef::SF_Undefined
)
1089 elf_symbol_iterator
SymI(I
);
1090 Expected
<elf_section_iterator
> SecIOrErr
= SymI
->getSection();
1092 consumeError(SecIOrErr
.takeError());
1095 elf_section_iterator secT
= *SecIOrErr
;
1096 secT
->getName(SecName
);
1100 if ((Symflags
& object::SymbolRef::SF_Weak
) && !isa
<MachOObjectFile
>(Obj
)) {
1101 char Ret
= isObject(Obj
, I
) ? 'v' : 'w';
1102 return (!(Symflags
& object::SymbolRef::SF_Undefined
)) ? toupper(Ret
) : Ret
;
1105 if (Symflags
& object::SymbolRef::SF_Undefined
)
1108 if (Symflags
& object::SymbolRef::SF_Common
)
1112 if (Symflags
& object::SymbolRef::SF_Absolute
)
1114 else if (IRObjectFile
*IR
= dyn_cast
<IRObjectFile
>(&Obj
))
1115 Ret
= getSymbolNMTypeChar(*IR
, I
);
1116 else if (COFFObjectFile
*COFF
= dyn_cast
<COFFObjectFile
>(&Obj
))
1117 Ret
= getSymbolNMTypeChar(*COFF
, I
);
1118 else if (COFFImportFile
*COFFImport
= dyn_cast
<COFFImportFile
>(&Obj
))
1119 Ret
= getSymbolNMTypeChar(*COFFImport
);
1120 else if (MachOObjectFile
*MachO
= dyn_cast
<MachOObjectFile
>(&Obj
))
1121 Ret
= getSymbolNMTypeChar(*MachO
, I
);
1122 else if (WasmObjectFile
*Wasm
= dyn_cast
<WasmObjectFile
>(&Obj
))
1123 Ret
= getSymbolNMTypeChar(*Wasm
, I
);
1125 Ret
= getSymbolNMTypeChar(cast
<ELFObjectFileBase
>(Obj
), I
);
1127 if (Symflags
& object::SymbolRef::SF_Global
)
1133 // getNsectForSegSect() is used to implement the Mach-O "-s segname sectname"
1134 // option to dump only those symbols from that section in a Mach-O file.
1135 // It is called once for each Mach-O file from dumpSymbolNamesFromObject()
1136 // to get the section number for that named section from the command line
1137 // arguments. It returns the section number for that section in the Mach-O
1138 // file or zero it is not present.
1139 static unsigned getNsectForSegSect(MachOObjectFile
*Obj
) {
1141 for (auto &S
: Obj
->sections()) {
1142 DataRefImpl Ref
= S
.getRawDataRefImpl();
1143 StringRef SectionName
;
1144 if (Expected
<StringRef
> NameOrErr
= Obj
->getSectionName(Ref
))
1145 SectionName
= *NameOrErr
;
1146 StringRef SegmentName
= Obj
->getSectionFinalSegmentName(Ref
);
1147 if (SegmentName
== SegSect
[0] && SectionName
== SegSect
[1])
1154 // getNsectInMachO() is used to implement the Mach-O "-s segname sectname"
1155 // option to dump only those symbols from that section in a Mach-O file.
1156 // It is called once for each symbol in a Mach-O file from
1157 // dumpSymbolNamesFromObject() and returns the section number for that symbol
1158 // if it is in a section, else it returns 0.
1159 static unsigned getNsectInMachO(MachOObjectFile
&Obj
, BasicSymbolRef Sym
) {
1160 DataRefImpl Symb
= Sym
.getRawDataRefImpl();
1161 if (Obj
.is64Bit()) {
1162 MachO::nlist_64 STE
= Obj
.getSymbol64TableEntry(Symb
);
1163 return (STE
.n_type
& MachO::N_TYPE
) == MachO::N_SECT
? STE
.n_sect
: 0;
1165 MachO::nlist STE
= Obj
.getSymbolTableEntry(Symb
);
1166 return (STE
.n_type
& MachO::N_TYPE
) == MachO::N_SECT
? STE
.n_sect
: 0;
1170 dumpSymbolNamesFromObject(SymbolicFile
&Obj
, bool printName
,
1171 const std::string
&ArchiveName
= std::string(),
1172 const std::string
&ArchitectureName
= std::string()) {
1173 auto Symbols
= Obj
.symbols();
1175 const auto *E
= dyn_cast
<ELFObjectFileBase
>(&Obj
);
1177 error("File format has no dynamic symbol table", Obj
.getFileName());
1180 Symbols
= E
->getDynamicSymbolIterators();
1182 std::string NameBuffer
;
1183 raw_string_ostream
OS(NameBuffer
);
1184 // If a "-s segname sectname" option was specified and this is a Mach-O
1185 // file get the section number for that section in this object file.
1186 unsigned int Nsect
= 0;
1187 MachOObjectFile
*MachO
= dyn_cast
<MachOObjectFile
>(&Obj
);
1188 if (!SegSect
.empty() && MachO
) {
1189 Nsect
= getNsectForSegSect(MachO
);
1190 // If this section is not in the object file no symbols are printed.
1194 if (!MachO
|| !DyldInfoOnly
) {
1195 for (BasicSymbolRef Sym
: Symbols
) {
1196 uint32_t SymFlags
= Sym
.getFlags();
1197 if (!DebugSyms
&& (SymFlags
& SymbolRef::SF_FormatSpecific
))
1199 if (WithoutAliases
&& (SymFlags
& SymbolRef::SF_Indirect
))
1201 // If a "-s segname sectname" option was specified and this is a Mach-O
1202 // file and this section appears in this file, Nsect will be non-zero then
1203 // see if this symbol is a symbol from that section and if not skip it.
1204 if (Nsect
&& Nsect
!= getNsectInMachO(*MachO
, Sym
))
1209 if (isa
<ELFObjectFileBase
>(&Obj
))
1210 S
.Size
= ELFSymbolRef(Sym
).getSize();
1211 if (PrintAddress
&& isa
<ObjectFile
>(Obj
)) {
1212 SymbolRef
SymRef(Sym
);
1213 Expected
<uint64_t> AddressOrErr
= SymRef
.getAddress();
1214 if (!AddressOrErr
) {
1215 consumeError(AddressOrErr
.takeError());
1218 S
.Address
= *AddressOrErr
;
1220 S
.TypeName
= getNMTypeName(Obj
, Sym
);
1221 S
.TypeChar
= getNMSectionTagAndName(Obj
, Sym
, S
.SectionName
);
1222 if (Error E
= Sym
.printName(OS
)) {
1224 OS
<< "bad string index";
1225 consumeError(std::move(E
));
1227 error(std::move(E
), Obj
.getFileName());
1231 SymbolList
.push_back(S
);
1236 const char *P
= NameBuffer
.c_str();
1238 for (I
= 0; I
< SymbolList
.size(); ++I
) {
1239 SymbolList
[I
].Name
= P
;
1243 // If this is a Mach-O file where the nlist symbol table is out of sync
1244 // with the dyld export trie then look through exports and fake up symbols
1245 // for the ones that are missing (also done with the -add-dyldinfo flag).
1246 // This is needed if strip(1) -T is run on a binary containing swift
1247 // language symbols for example. The option -only-dyldinfo will fake up
1248 // all symbols from the dyld export trie as well as the bind info.
1249 std::string ExportsNameBuffer
;
1250 raw_string_ostream
EOS(ExportsNameBuffer
);
1251 std::string BindsNameBuffer
;
1252 raw_string_ostream
BOS(BindsNameBuffer
);
1253 std::string LazysNameBuffer
;
1254 raw_string_ostream
LOS(LazysNameBuffer
);
1255 std::string WeaksNameBuffer
;
1256 raw_string_ostream
WOS(WeaksNameBuffer
);
1257 std::string FunctionStartsNameBuffer
;
1258 raw_string_ostream
FOS(FunctionStartsNameBuffer
);
1259 if (MachO
&& !NoDyldInfo
) {
1260 MachO::mach_header H
;
1261 MachO::mach_header_64 H_64
;
1262 uint32_t HFlags
= 0;
1263 if (MachO
->is64Bit()) {
1264 H_64
= MachO
->MachOObjectFile::getHeader64();
1265 HFlags
= H_64
.flags
;
1267 H
= MachO
->MachOObjectFile::getHeader();
1270 uint64_t BaseSegmentAddress
= 0;
1271 for (const auto &Command
: MachO
->load_commands()) {
1272 if (Command
.C
.cmd
== MachO::LC_SEGMENT
) {
1273 MachO::segment_command Seg
= MachO
->getSegmentLoadCommand(Command
);
1274 if (Seg
.fileoff
== 0 && Seg
.filesize
!= 0) {
1275 BaseSegmentAddress
= Seg
.vmaddr
;
1278 } else if (Command
.C
.cmd
== MachO::LC_SEGMENT_64
) {
1279 MachO::segment_command_64 Seg
= MachO
->getSegment64LoadCommand(Command
);
1280 if (Seg
.fileoff
== 0 && Seg
.filesize
!= 0) {
1281 BaseSegmentAddress
= Seg
.vmaddr
;
1286 if (DyldInfoOnly
|| AddDyldInfo
||
1287 HFlags
& MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO
) {
1288 unsigned ExportsAdded
= 0;
1289 Error Err
= Error::success();
1290 for (const llvm::object::ExportEntry
&Entry
: MachO
->exports(Err
)) {
1292 bool ReExport
= false;
1293 if (!DyldInfoOnly
) {
1294 for (const NMSymbol
&S
: SymbolList
)
1295 if (S
.Address
== Entry
.address() + BaseSegmentAddress
&&
1296 S
.Name
== Entry
.name()) {
1303 S
.Address
= Entry
.address() + BaseSegmentAddress
;
1306 S
.Name
= Entry
.name();
1307 // There is no symbol in the nlist symbol table for this so we set
1308 // Sym effectivly to null and the rest of code in here must test for
1309 // it and not do things like Sym.getFlags() for it.
1310 S
.Sym
= BasicSymbolRef();
1311 S
.SymFlags
= SymbolRef::SF_Global
;
1312 S
.Section
= SectionRef();
1316 S
.IndirectName
= StringRef();
1318 uint64_t EFlags
= Entry
.flags();
1319 bool Abs
= ((EFlags
& MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK
) ==
1320 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE
);
1321 bool Resolver
= (EFlags
&
1322 MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER
);
1323 ReExport
= (EFlags
& MachO::EXPORT_SYMBOL_FLAGS_REEXPORT
);
1324 bool WeakDef
= (EFlags
& MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION
);
1326 S
.NDesc
|= MachO::N_WEAK_DEF
;
1328 S
.NType
= MachO::N_EXT
| MachO::N_ABS
;
1330 } else if (ReExport
) {
1331 S
.NType
= MachO::N_EXT
| MachO::N_INDR
;
1334 S
.NType
= MachO::N_EXT
| MachO::N_SECT
;
1336 S
.Address
= Entry
.other() + BaseSegmentAddress
;
1337 if ((S
.Address
& 1) != 0 &&
1338 !MachO
->is64Bit() && H
.cputype
== MachO::CPU_TYPE_ARM
){
1340 S
.NDesc
|= MachO::N_ARM_THUMB_DEF
;
1343 S
.Address
= Entry
.address() + BaseSegmentAddress
;
1345 StringRef SegmentName
= StringRef();
1346 StringRef SectionName
= StringRef();
1347 for (const SectionRef
&Section
: MachO
->sections()) {
1349 Section
.getName(SectionName
);
1350 SegmentName
= MachO
->getSectionFinalSegmentName(
1351 Section
.getRawDataRefImpl());
1352 if (S
.Address
>= Section
.getAddress() &&
1353 S
.Address
< Section
.getAddress() + Section
.getSize()) {
1354 S
.Section
= Section
;
1356 } else if (Entry
.name() == "__mh_execute_header" &&
1357 SegmentName
== "__TEXT" && SectionName
== "__text") {
1358 S
.Section
= Section
;
1359 S
.NDesc
|= MachO::REFERENCED_DYNAMICALLY
;
1363 if (SegmentName
== "__TEXT" && SectionName
== "__text")
1365 else if (SegmentName
== "__DATA" && SectionName
== "__data")
1367 else if (SegmentName
== "__DATA" && SectionName
== "__bss")
1372 SymbolList
.push_back(S
);
1374 EOS
<< Entry
.name();
1378 // For ReExports there are a two more things to do, first add the
1379 // indirect name and second create the undefined symbol using the
1380 // referened dynamic library.
1383 // Add the indirect name.
1384 if (Entry
.otherName().empty())
1385 EOS
<< Entry
.name();
1387 EOS
<< Entry
.otherName();
1390 // Now create the undefined symbol using the referened dynamic
1396 if (Entry
.otherName().empty())
1397 U
.Name
= Entry
.name();
1399 U
.Name
= Entry
.otherName();
1400 // Again there is no symbol in the nlist symbol table for this so
1401 // we set Sym effectivly to null and the rest of code in here must
1402 // test for it and not do things like Sym.getFlags() for it.
1403 U
.Sym
= BasicSymbolRef();
1404 U
.SymFlags
= SymbolRef::SF_Global
| SymbolRef::SF_Undefined
;
1405 U
.Section
= SectionRef();
1406 U
.NType
= MachO::N_EXT
| MachO::N_UNDF
;
1409 // The library ordinal for this undefined symbol is in the export
1410 // trie Entry.other().
1411 MachO::SET_LIBRARY_ORDINAL(U
.NDesc
, Entry
.other());
1412 U
.IndirectName
= StringRef();
1413 SymbolList
.push_back(U
);
1415 // Finally add the undefined symbol's name.
1416 if (Entry
.otherName().empty())
1417 EOS
<< Entry
.name();
1419 EOS
<< Entry
.otherName();
1426 error(std::move(Err
), MachO
->getFileName());
1427 // Set the symbol names and indirect names for the added symbols.
1430 const char *Q
= ExportsNameBuffer
.c_str();
1431 for (unsigned K
= 0; K
< ExportsAdded
; K
++) {
1432 SymbolList
[I
].Name
= Q
;
1434 if (SymbolList
[I
].TypeChar
== 'I') {
1435 SymbolList
[I
].IndirectName
= Q
;
1442 // Add the undefined symbols from the bind entries.
1443 unsigned BindsAdded
= 0;
1444 Error BErr
= Error::success();
1445 StringRef LastSymbolName
= StringRef();
1446 for (const llvm::object::MachOBindEntry
&Entry
: MachO
->bindTable(BErr
)) {
1448 if (LastSymbolName
== Entry
.symbolName())
1450 else if(!DyldInfoOnly
) {
1451 for (unsigned J
= 0; J
< SymbolList
.size() && !found
; ++J
) {
1452 if (SymbolList
[J
].Name
== Entry
.symbolName())
1457 LastSymbolName
= Entry
.symbolName();
1462 // There is no symbol in the nlist symbol table for this so we set
1463 // Sym effectivly to null and the rest of code in here must test for
1464 // it and not do things like Sym.getFlags() for it.
1465 B
.Sym
= BasicSymbolRef();
1466 B
.SymFlags
= SymbolRef::SF_Global
| SymbolRef::SF_Undefined
;
1467 B
.NType
= MachO::N_EXT
| MachO::N_UNDF
;
1470 MachO::SET_LIBRARY_ORDINAL(B
.NDesc
, Entry
.ordinal());
1471 B
.IndirectName
= StringRef();
1472 B
.Name
= Entry
.symbolName();
1473 SymbolList
.push_back(B
);
1474 BOS
<< Entry
.symbolName();
1480 error(std::move(BErr
), MachO
->getFileName());
1481 // Set the symbol names and indirect names for the added symbols.
1484 const char *Q
= BindsNameBuffer
.c_str();
1485 for (unsigned K
= 0; K
< BindsAdded
; K
++) {
1486 SymbolList
[I
].Name
= Q
;
1488 if (SymbolList
[I
].TypeChar
== 'I') {
1489 SymbolList
[I
].IndirectName
= Q
;
1496 // Add the undefined symbols from the lazy bind entries.
1497 unsigned LazysAdded
= 0;
1498 Error LErr
= Error::success();
1499 LastSymbolName
= StringRef();
1500 for (const llvm::object::MachOBindEntry
&Entry
:
1501 MachO
->lazyBindTable(LErr
)) {
1503 if (LastSymbolName
== Entry
.symbolName())
1506 // Here we must check to see it this symbol is already in the
1507 // SymbolList as it might have already have been added above via a
1508 // non-lazy (bind) entry.
1509 for (unsigned J
= 0; J
< SymbolList
.size() && !found
; ++J
) {
1510 if (SymbolList
[J
].Name
== Entry
.symbolName())
1515 LastSymbolName
= Entry
.symbolName();
1517 L
.Name
= Entry
.symbolName();
1521 // There is no symbol in the nlist symbol table for this so we set
1522 // Sym effectivly to null and the rest of code in here must test for
1523 // it and not do things like Sym.getFlags() for it.
1524 L
.Sym
= BasicSymbolRef();
1525 L
.SymFlags
= SymbolRef::SF_Global
| SymbolRef::SF_Undefined
;
1526 L
.NType
= MachO::N_EXT
| MachO::N_UNDF
;
1528 // The REFERENCE_FLAG_UNDEFINED_LAZY is no longer used but here it
1529 // makes sence since we are creating this from a lazy bind entry.
1530 L
.NDesc
= MachO::REFERENCE_FLAG_UNDEFINED_LAZY
;
1531 MachO::SET_LIBRARY_ORDINAL(L
.NDesc
, Entry
.ordinal());
1532 L
.IndirectName
= StringRef();
1533 SymbolList
.push_back(L
);
1534 LOS
<< Entry
.symbolName();
1540 error(std::move(LErr
), MachO
->getFileName());
1541 // Set the symbol names and indirect names for the added symbols.
1544 const char *Q
= LazysNameBuffer
.c_str();
1545 for (unsigned K
= 0; K
< LazysAdded
; K
++) {
1546 SymbolList
[I
].Name
= Q
;
1548 if (SymbolList
[I
].TypeChar
== 'I') {
1549 SymbolList
[I
].IndirectName
= Q
;
1556 // Add the undefineds symbol from the weak bind entries which are not
1558 unsigned WeaksAdded
= 0;
1559 Error WErr
= Error::success();
1560 LastSymbolName
= StringRef();
1561 for (const llvm::object::MachOBindEntry
&Entry
:
1562 MachO
->weakBindTable(WErr
)) {
1565 if (LastSymbolName
== Entry
.symbolName() ||
1566 Entry
.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION
) {
1569 for (J
= 0; J
< SymbolList
.size() && !found
; ++J
) {
1570 if (SymbolList
[J
].Name
== Entry
.symbolName()) {
1577 LastSymbolName
= Entry
.symbolName();
1579 memset(&W
, '\0', sizeof(NMSymbol
));
1580 W
.Name
= Entry
.symbolName();
1584 // There is no symbol in the nlist symbol table for this so we set
1585 // Sym effectivly to null and the rest of code in here must test for
1586 // it and not do things like Sym.getFlags() for it.
1587 W
.Sym
= BasicSymbolRef();
1588 W
.SymFlags
= SymbolRef::SF_Global
| SymbolRef::SF_Undefined
;
1589 W
.NType
= MachO::N_EXT
| MachO::N_UNDF
;
1591 // Odd that we are using N_WEAK_DEF on an undefined symbol but that is
1592 // what is created in this case by the linker when there are real
1593 // symbols in the nlist structs.
1594 W
.NDesc
= MachO::N_WEAK_DEF
;
1595 W
.IndirectName
= StringRef();
1596 SymbolList
.push_back(W
);
1597 WOS
<< Entry
.symbolName();
1601 // This is the case the symbol was previously been found and it could
1602 // have been added from a bind or lazy bind symbol. If so and not
1603 // a definition also mark it as weak.
1604 if (SymbolList
[J
].TypeChar
== 'U')
1605 // See comment above about N_WEAK_DEF.
1606 SymbolList
[J
].NDesc
|= MachO::N_WEAK_DEF
;
1610 error(std::move(WErr
), MachO
->getFileName());
1611 // Set the symbol names and indirect names for the added symbols.
1614 const char *Q
= WeaksNameBuffer
.c_str();
1615 for (unsigned K
= 0; K
< WeaksAdded
; K
++) {
1616 SymbolList
[I
].Name
= Q
;
1618 if (SymbolList
[I
].TypeChar
== 'I') {
1619 SymbolList
[I
].IndirectName
= Q
;
1626 // Trying adding symbol from the function starts table and LC_MAIN entry
1628 SmallVector
<uint64_t, 8> FoundFns
;
1629 uint64_t lc_main_offset
= UINT64_MAX
;
1630 for (const auto &Command
: MachO
->load_commands()) {
1631 if (Command
.C
.cmd
== MachO::LC_FUNCTION_STARTS
) {
1632 // We found a function starts segment, parse the addresses for
1634 MachO::linkedit_data_command LLC
=
1635 MachO
->getLinkeditDataLoadCommand(Command
);
1637 MachO
->ReadULEB128s(LLC
.dataoff
, FoundFns
);
1638 } else if (Command
.C
.cmd
== MachO::LC_MAIN
) {
1639 MachO::entry_point_command LCmain
=
1640 MachO
->getEntryPointCommand(Command
);
1641 lc_main_offset
= LCmain
.entryoff
;
1644 // See if these addresses are already in the symbol table.
1645 unsigned FunctionStartsAdded
= 0;
1646 for (uint64_t f
= 0; f
< FoundFns
.size(); f
++) {
1648 for (unsigned J
= 0; J
< SymbolList
.size() && !found
; ++J
) {
1649 if (SymbolList
[J
].Address
== FoundFns
[f
] + BaseSegmentAddress
)
1652 // See this address is not already in the symbol table fake up an
1656 F
.Name
= "<redacted function X>";
1657 F
.Address
= FoundFns
[f
] + BaseSegmentAddress
;
1659 // There is no symbol in the nlist symbol table for this so we set
1660 // Sym effectivly to null and the rest of code in here must test for
1661 // it and not do things like Sym.getFlags() for it.
1662 F
.Sym
= BasicSymbolRef();
1664 F
.NType
= MachO::N_SECT
;
1666 StringRef SegmentName
= StringRef();
1667 StringRef SectionName
= StringRef();
1668 for (const SectionRef
&Section
: MachO
->sections()) {
1669 Section
.getName(SectionName
);
1670 SegmentName
= MachO
->getSectionFinalSegmentName(
1671 Section
.getRawDataRefImpl());
1673 if (F
.Address
>= Section
.getAddress() &&
1674 F
.Address
< Section
.getAddress() + Section
.getSize()) {
1675 F
.Section
= Section
;
1679 if (SegmentName
== "__TEXT" && SectionName
== "__text")
1681 else if (SegmentName
== "__DATA" && SectionName
== "__data")
1683 else if (SegmentName
== "__DATA" && SectionName
== "__bss")
1688 F
.IndirectName
= StringRef();
1689 SymbolList
.push_back(F
);
1690 if (FoundFns
[f
] == lc_main_offset
)
1691 FOS
<< "<redacted LC_MAIN>";
1693 FOS
<< "<redacted function " << f
<< ">";
1695 FunctionStartsAdded
++;
1698 if (FunctionStartsAdded
) {
1700 const char *Q
= FunctionStartsNameBuffer
.c_str();
1701 for (unsigned K
= 0; K
< FunctionStartsAdded
; K
++) {
1702 SymbolList
[I
].Name
= Q
;
1704 if (SymbolList
[I
].TypeChar
== 'I') {
1705 SymbolList
[I
].IndirectName
= Q
;
1714 CurrentFilename
= Obj
.getFileName();
1715 sortAndPrintSymbolList(Obj
, printName
, ArchiveName
, ArchitectureName
);
1718 // checkMachOAndArchFlags() checks to see if the SymbolicFile is a Mach-O file
1719 // and if it is and there is a list of architecture flags is specified then
1720 // check to make sure this Mach-O file is one of those architectures or all
1721 // architectures was specificed. If not then an error is generated and this
1722 // routine returns false. Else it returns true.
1723 static bool checkMachOAndArchFlags(SymbolicFile
*O
, std::string
&Filename
) {
1724 auto *MachO
= dyn_cast
<MachOObjectFile
>(O
);
1726 if (!MachO
|| ArchAll
|| ArchFlags
.empty())
1729 MachO::mach_header H
;
1730 MachO::mach_header_64 H_64
;
1732 const char *McpuDefault
, *ArchFlag
;
1733 if (MachO
->is64Bit()) {
1734 H_64
= MachO
->MachOObjectFile::getHeader64();
1735 T
= MachOObjectFile::getArchTriple(H_64
.cputype
, H_64
.cpusubtype
,
1736 &McpuDefault
, &ArchFlag
);
1738 H
= MachO
->MachOObjectFile::getHeader();
1739 T
= MachOObjectFile::getArchTriple(H
.cputype
, H
.cpusubtype
,
1740 &McpuDefault
, &ArchFlag
);
1742 const std::string
ArchFlagName(ArchFlag
);
1743 if (none_of(ArchFlags
, [&](const std::string
&Name
) {
1744 return Name
== ArchFlagName
;
1746 error("No architecture specified", Filename
);
1752 static void dumpSymbolNamesFromFile(std::string
&Filename
) {
1753 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> BufferOrErr
=
1754 MemoryBuffer::getFileOrSTDIN(Filename
);
1755 if (error(BufferOrErr
.getError(), Filename
))
1758 LLVMContext Context
;
1759 LLVMContext
*ContextPtr
= NoLLVMBitcode
? nullptr : &Context
;
1760 Expected
<std::unique_ptr
<Binary
>> BinaryOrErr
=
1761 createBinary(BufferOrErr
.get()->getMemBufferRef(), ContextPtr
);
1763 error(BinaryOrErr
.takeError(), Filename
);
1766 Binary
&Bin
= *BinaryOrErr
.get();
1768 if (Archive
*A
= dyn_cast
<Archive
>(&Bin
)) {
1770 Archive::symbol_iterator I
= A
->symbol_begin();
1771 Archive::symbol_iterator E
= A
->symbol_end();
1773 outs() << "Archive map\n";
1774 for (; I
!= E
; ++I
) {
1775 Expected
<Archive::Child
> C
= I
->getMember();
1777 error(C
.takeError(), Filename
);
1780 Expected
<StringRef
> FileNameOrErr
= C
->getName();
1781 if (!FileNameOrErr
) {
1782 error(FileNameOrErr
.takeError(), Filename
);
1785 StringRef SymName
= I
->getName();
1786 outs() << SymName
<< " in " << FileNameOrErr
.get() << "\n";
1793 Error Err
= Error::success();
1794 for (auto &C
: A
->children(Err
)) {
1795 Expected
<std::unique_ptr
<Binary
>> ChildOrErr
=
1796 C
.getAsBinary(ContextPtr
);
1798 if (auto E
= isNotObjectErrorInvalidFileType(ChildOrErr
.takeError()))
1799 error(std::move(E
), Filename
, C
);
1802 if (SymbolicFile
*O
= dyn_cast
<SymbolicFile
>(&*ChildOrErr
.get())) {
1803 if (!MachOPrintSizeWarning
&& PrintSize
&& isa
<MachOObjectFile
>(O
)) {
1804 WithColor::warning(errs(), ToolName
)
1805 << "sizes with -print-size for Mach-O files are always zero.\n";
1806 MachOPrintSizeWarning
= true;
1808 if (!checkMachOAndArchFlags(O
, Filename
))
1810 if (!PrintFileName
) {
1812 if (isa
<MachOObjectFile
>(O
)) {
1813 outs() << Filename
<< "(" << O
->getFileName() << ")";
1815 outs() << O
->getFileName();
1818 dumpSymbolNamesFromObject(*O
, false, Filename
);
1822 error(std::move(Err
), A
->getFileName());
1826 if (MachOUniversalBinary
*UB
= dyn_cast
<MachOUniversalBinary
>(&Bin
)) {
1827 // If we have a list of architecture flags specified dump only those.
1828 if (!ArchAll
&& !ArchFlags
.empty()) {
1829 // Look for a slice in the universal binary that matches each ArchFlag.
1831 for (unsigned i
= 0; i
< ArchFlags
.size(); ++i
) {
1833 for (MachOUniversalBinary::object_iterator I
= UB
->begin_objects(),
1834 E
= UB
->end_objects();
1836 if (ArchFlags
[i
] == I
->getArchFlagName()) {
1838 Expected
<std::unique_ptr
<ObjectFile
>> ObjOrErr
=
1839 I
->getAsObjectFile();
1840 std::string ArchiveName
;
1841 std::string ArchitectureName
;
1842 ArchiveName
.clear();
1843 ArchitectureName
.clear();
1845 ObjectFile
&Obj
= *ObjOrErr
.get();
1846 if (ArchFlags
.size() > 1) {
1848 ArchitectureName
= I
->getArchFlagName();
1850 outs() << "\n" << Obj
.getFileName() << " (for architecture "
1851 << I
->getArchFlagName() << ")"
1854 dumpSymbolNamesFromObject(Obj
, false, ArchiveName
,
1856 } else if (auto E
= isNotObjectErrorInvalidFileType(
1857 ObjOrErr
.takeError())) {
1858 error(std::move(E
), Filename
, ArchFlags
.size() > 1 ?
1859 StringRef(I
->getArchFlagName()) : StringRef());
1861 } else if (Expected
<std::unique_ptr
<Archive
>> AOrErr
=
1862 I
->getAsArchive()) {
1863 std::unique_ptr
<Archive
> &A
= *AOrErr
;
1864 Error Err
= Error::success();
1865 for (auto &C
: A
->children(Err
)) {
1866 Expected
<std::unique_ptr
<Binary
>> ChildOrErr
=
1867 C
.getAsBinary(ContextPtr
);
1869 if (auto E
= isNotObjectErrorInvalidFileType(
1870 ChildOrErr
.takeError())) {
1871 error(std::move(E
), Filename
, C
, ArchFlags
.size() > 1 ?
1872 StringRef(I
->getArchFlagName()) : StringRef());
1876 if (SymbolicFile
*O
=
1877 dyn_cast
<SymbolicFile
>(&*ChildOrErr
.get())) {
1878 if (PrintFileName
) {
1879 ArchiveName
= A
->getFileName();
1880 if (ArchFlags
.size() > 1)
1881 ArchitectureName
= I
->getArchFlagName();
1883 outs() << "\n" << A
->getFileName();
1884 outs() << "(" << O
->getFileName() << ")";
1885 if (ArchFlags
.size() > 1) {
1886 outs() << " (for architecture " << I
->getArchFlagName()
1891 dumpSymbolNamesFromObject(*O
, false, ArchiveName
,
1896 error(std::move(Err
), A
->getFileName());
1898 consumeError(AOrErr
.takeError());
1899 error(Filename
+ " for architecture " +
1900 StringRef(I
->getArchFlagName()) +
1901 " is not a Mach-O file or an archive file",
1902 "Mach-O universal file");
1908 "file: " + Filename
+ " does not contain architecture");
1914 // No architecture flags were specified so if this contains a slice that
1915 // matches the host architecture dump only that.
1917 Triple HostTriple
= MachOObjectFile::getHostArch();
1918 StringRef HostArchName
= HostTriple
.getArchName();
1919 for (MachOUniversalBinary::object_iterator I
= UB
->begin_objects(),
1920 E
= UB
->end_objects();
1922 if (HostArchName
== I
->getArchFlagName()) {
1923 Expected
<std::unique_ptr
<ObjectFile
>> ObjOrErr
= I
->getAsObjectFile();
1924 std::string ArchiveName
;
1926 ObjectFile
&Obj
= *ObjOrErr
.get();
1927 dumpSymbolNamesFromObject(Obj
, false);
1928 } else if (auto E
= isNotObjectErrorInvalidFileType(
1929 ObjOrErr
.takeError())) {
1930 error(std::move(E
), Filename
);
1932 } else if (Expected
<std::unique_ptr
<Archive
>> AOrErr
=
1933 I
->getAsArchive()) {
1934 std::unique_ptr
<Archive
> &A
= *AOrErr
;
1935 Error Err
= Error::success();
1936 for (auto &C
: A
->children(Err
)) {
1937 Expected
<std::unique_ptr
<Binary
>> ChildOrErr
=
1938 C
.getAsBinary(ContextPtr
);
1940 if (auto E
= isNotObjectErrorInvalidFileType(
1941 ChildOrErr
.takeError()))
1942 error(std::move(E
), Filename
, C
);
1945 if (SymbolicFile
*O
=
1946 dyn_cast
<SymbolicFile
>(&*ChildOrErr
.get())) {
1948 ArchiveName
= A
->getFileName();
1950 outs() << "\n" << A
->getFileName() << "(" << O
->getFileName()
1953 dumpSymbolNamesFromObject(*O
, false, ArchiveName
);
1957 error(std::move(Err
), A
->getFileName());
1959 consumeError(AOrErr
.takeError());
1960 error(Filename
+ " for architecture " +
1961 StringRef(I
->getArchFlagName()) +
1962 " is not a Mach-O file or an archive file",
1963 "Mach-O universal file");
1969 // Either all architectures have been specified or none have been specified
1970 // and this does not contain the host architecture so dump all the slices.
1971 bool moreThanOneArch
= UB
->getNumberOfObjects() > 1;
1972 for (const MachOUniversalBinary::ObjectForArch
&O
: UB
->objects()) {
1973 Expected
<std::unique_ptr
<ObjectFile
>> ObjOrErr
= O
.getAsObjectFile();
1974 std::string ArchiveName
;
1975 std::string ArchitectureName
;
1976 ArchiveName
.clear();
1977 ArchitectureName
.clear();
1979 ObjectFile
&Obj
= *ObjOrErr
.get();
1980 if (PrintFileName
) {
1981 if (isa
<MachOObjectFile
>(Obj
) && moreThanOneArch
)
1982 ArchitectureName
= O
.getArchFlagName();
1984 if (moreThanOneArch
)
1986 outs() << Obj
.getFileName();
1987 if (isa
<MachOObjectFile
>(Obj
) && moreThanOneArch
)
1988 outs() << " (for architecture " << O
.getArchFlagName() << ")";
1991 dumpSymbolNamesFromObject(Obj
, false, ArchiveName
, ArchitectureName
);
1992 } else if (auto E
= isNotObjectErrorInvalidFileType(
1993 ObjOrErr
.takeError())) {
1994 error(std::move(E
), Filename
, moreThanOneArch
?
1995 StringRef(O
.getArchFlagName()) : StringRef());
1997 } else if (Expected
<std::unique_ptr
<Archive
>> AOrErr
=
1999 std::unique_ptr
<Archive
> &A
= *AOrErr
;
2000 Error Err
= Error::success();
2001 for (auto &C
: A
->children(Err
)) {
2002 Expected
<std::unique_ptr
<Binary
>> ChildOrErr
=
2003 C
.getAsBinary(ContextPtr
);
2005 if (auto E
= isNotObjectErrorInvalidFileType(
2006 ChildOrErr
.takeError()))
2007 error(std::move(E
), Filename
, C
, moreThanOneArch
?
2008 StringRef(ArchitectureName
) : StringRef());
2011 if (SymbolicFile
*F
= dyn_cast
<SymbolicFile
>(&*ChildOrErr
.get())) {
2012 if (PrintFileName
) {
2013 ArchiveName
= A
->getFileName();
2014 if (isa
<MachOObjectFile
>(F
) && moreThanOneArch
)
2015 ArchitectureName
= O
.getArchFlagName();
2017 outs() << "\n" << A
->getFileName();
2018 if (isa
<MachOObjectFile
>(F
)) {
2019 outs() << "(" << F
->getFileName() << ")";
2020 if (moreThanOneArch
)
2021 outs() << " (for architecture " << O
.getArchFlagName()
2024 outs() << ":" << F
->getFileName();
2027 dumpSymbolNamesFromObject(*F
, false, ArchiveName
, ArchitectureName
);
2031 error(std::move(Err
), A
->getFileName());
2033 consumeError(AOrErr
.takeError());
2034 error(Filename
+ " for architecture " +
2035 StringRef(O
.getArchFlagName()) +
2036 " is not a Mach-O file or an archive file",
2037 "Mach-O universal file");
2042 if (SymbolicFile
*O
= dyn_cast
<SymbolicFile
>(&Bin
)) {
2043 if (!MachOPrintSizeWarning
&& PrintSize
&& isa
<MachOObjectFile
>(O
)) {
2044 WithColor::warning(errs(), ToolName
)
2045 << "sizes with --print-size for Mach-O files are always zero.\n";
2046 MachOPrintSizeWarning
= true;
2048 if (!checkMachOAndArchFlags(O
, Filename
))
2050 dumpSymbolNamesFromObject(*O
, true);
2054 int main(int argc
, char **argv
) {
2055 InitLLVM
X(argc
, argv
);
2056 cl::HideUnrelatedOptions(NMCat
);
2057 cl::ParseCommandLineOptions(argc
, argv
, "llvm symbol table dumper\n");
2059 // llvm-nm only reads binary files.
2060 if (error(sys::ChangeStdinToBinary()))
2063 // These calls are needed so that we can read bitcode correctly.
2064 llvm::InitializeAllTargetInfos();
2065 llvm::InitializeAllTargetMCs();
2066 llvm::InitializeAllAsmParsers();
2072 OutputFormat
= posix
;
2074 OutputFormat
= darwin
;
2076 // The relative order of these is important. If you pass --size-sort it should
2077 // only print out the size. However, if you pass -S --size-sort, it should
2078 // print out both the size and address.
2079 if (SizeSort
&& !PrintSize
)
2080 PrintAddress
= false;
2081 if (OutputFormat
== sysv
|| SizeSort
)
2083 if (InputFilenames
.empty())
2084 InputFilenames
.push_back("a.out");
2085 if (InputFilenames
.size() > 1)
2086 MultipleFiles
= true;
2088 // If both --demangle and --no-demangle are specified then pick the last one.
2089 if (NoDemangle
.getPosition() > Demangle
.getPosition())
2090 Demangle
= !NoDemangle
;
2092 for (unsigned i
= 0; i
< ArchFlags
.size(); ++i
) {
2093 if (ArchFlags
[i
] == "all") {
2096 if (!MachOObjectFile::isValidArch(ArchFlags
[i
]))
2097 error("Unknown architecture named '" + ArchFlags
[i
] + "'",
2098 "for the --arch option");
2102 if (!SegSect
.empty() && SegSect
.size() != 2)
2103 error("bad number of arguments (must be two arguments)",
2104 "for the -s option");
2106 if (NoDyldInfo
&& (AddDyldInfo
|| DyldInfoOnly
))
2107 error("--no-dyldinfo can't be used with --add-dyldinfo or --dyldinfo-only");
2109 llvm::for_each(InputFilenames
, dumpSymbolNamesFromFile
);