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 std::function
<bool(const NMSymbol
&, const NMSymbol
&)> Cmp
;
716 Cmp
= compareSymbolAddress
;
718 Cmp
= compareSymbolSize
;
720 Cmp
= compareSymbolName
;
723 Cmp
= [=](const NMSymbol
&A
, const NMSymbol
&B
) { return Cmp(B
, A
); };
724 llvm::sort(SymbolList
, Cmp
);
727 if (!PrintFileName
) {
728 if (OutputFormat
== posix
&& MultipleFiles
&& printName
) {
729 outs() << '\n' << CurrentFilename
<< ":\n";
730 } else if (OutputFormat
== bsd
&& MultipleFiles
&& printName
) {
731 outs() << "\n" << CurrentFilename
<< ":\n";
732 } else if (OutputFormat
== sysv
) {
733 outs() << "\n\nSymbols from " << CurrentFilename
<< ":\n\n";
734 if (isSymbolList64Bit(Obj
))
735 outs() << "Name Value Class Type"
736 << " Size Line Section\n";
738 outs() << "Name Value Class Type"
739 << " Size Line Section\n";
743 const char *printBlanks
, *printDashes
, *printFormat
;
744 if (isSymbolList64Bit(Obj
)) {
746 printDashes
= "----------------";
747 switch (AddressRadix
) {
749 printFormat
= OutputFormat
== posix
? "%" PRIo64
: "%016" PRIo64
;
752 printFormat
= OutputFormat
== posix
? "%" PRIx64
: "%016" PRIx64
;
755 printFormat
= OutputFormat
== posix
? "%" PRId64
: "%016" PRId64
;
759 printDashes
= "--------";
760 switch (AddressRadix
) {
762 printFormat
= OutputFormat
== posix
? "%" PRIo64
: "%08" PRIo64
;
765 printFormat
= OutputFormat
== posix
? "%" PRIx64
: "%08" PRIx64
;
768 printFormat
= OutputFormat
== posix
? "%" PRId64
: "%08" PRId64
;
772 auto writeFileName
= [&](raw_ostream
&S
) {
773 if (!ArchitectureName
.empty())
774 S
<< "(for architecture " << ArchitectureName
<< "):";
775 if (OutputFormat
== posix
&& !ArchiveName
.empty())
776 S
<< ArchiveName
<< "[" << CurrentFilename
<< "]: ";
778 if (!ArchiveName
.empty())
779 S
<< ArchiveName
<< ":";
780 S
<< CurrentFilename
<< ": ";
784 if (SymbolList
.empty()) {
786 writeFileName(errs());
787 errs() << "no symbols\n";
790 for (const NMSymbol
&S
: SymbolList
) {
792 std::string Name
= S
.Name
.str();
793 MachOObjectFile
*MachO
= dyn_cast
<MachOObjectFile
>(&Obj
);
795 if (Optional
<std::string
> Opt
= demangle(S
.Name
, MachO
))
798 if (S
.Sym
.getRawDataRefImpl().p
)
799 SymFlags
= S
.Sym
.getFlags();
801 SymFlags
= S
.SymFlags
;
803 bool Undefined
= SymFlags
& SymbolRef::SF_Undefined
;
804 bool Global
= SymFlags
& SymbolRef::SF_Global
;
805 bool Weak
= SymFlags
& SymbolRef::SF_Weak
;
806 if ((!Undefined
&& UndefinedOnly
) || (Undefined
&& DefinedOnly
) ||
807 (!Global
&& ExternalOnly
) || (Weak
&& NoWeakSymbols
))
810 writeFileName(outs());
811 if ((JustSymbolName
||
812 (UndefinedOnly
&& MachO
&& OutputFormat
!= darwin
)) &&
813 OutputFormat
!= posix
) {
814 outs() << Name
<< "\n";
818 char SymbolAddrStr
[23], SymbolSizeStr
[23];
820 // If the format is SysV or the symbol isn't defined, then print spaces.
821 if (OutputFormat
== sysv
|| !symbolIsDefined(S
)) {
822 if (OutputFormat
== posix
) {
823 format(printFormat
, S
.Address
)
824 .print(SymbolAddrStr
, sizeof(SymbolAddrStr
));
825 format(printFormat
, S
.Size
).print(SymbolSizeStr
, sizeof(SymbolSizeStr
));
827 strcpy(SymbolAddrStr
, printBlanks
);
828 strcpy(SymbolSizeStr
, printBlanks
);
832 if (symbolIsDefined(S
)) {
833 // Otherwise, print the symbol address and size.
835 strcpy(SymbolAddrStr
, printDashes
);
836 else if (MachO
&& S
.TypeChar
== 'I')
837 strcpy(SymbolAddrStr
, printBlanks
);
839 format(printFormat
, S
.Address
)
840 .print(SymbolAddrStr
, sizeof(SymbolAddrStr
));
841 format(printFormat
, S
.Size
).print(SymbolSizeStr
, sizeof(SymbolSizeStr
));
844 // If OutputFormat is darwin or we are printing Mach-O symbols in hex and
845 // we have a MachOObjectFile, call darwinPrintSymbol to print as darwin's
846 // nm(1) -m output or hex, else if OutputFormat is darwin or we are
847 // printing Mach-O symbols in hex and not a Mach-O object fall back to
848 // OutputFormat bsd (see below).
849 if ((OutputFormat
== darwin
|| FormatMachOasHex
) && (MachO
|| Obj
.isIR())) {
850 darwinPrintSymbol(Obj
, S
, SymbolAddrStr
, printBlanks
, printDashes
,
852 } else if (OutputFormat
== posix
) {
853 outs() << Name
<< " " << S
.TypeChar
<< " " << SymbolAddrStr
<< " "
854 << (MachO
? "0" : SymbolSizeStr
) << "\n";
855 } else if (OutputFormat
== bsd
|| (OutputFormat
== darwin
&& !MachO
)) {
857 outs() << SymbolAddrStr
<< ' ';
859 outs() << SymbolSizeStr
<< ' ';
860 outs() << S
.TypeChar
;
861 if (S
.TypeChar
== '-' && MachO
)
862 darwinPrintStab(MachO
, S
);
863 outs() << " " << Name
;
864 if (S
.TypeChar
== 'I' && MachO
) {
865 outs() << " (indirect for ";
866 if (S
.Sym
.getRawDataRefImpl().p
) {
867 StringRef IndirectName
;
868 if (MachO
->getIndirectName(S
.Sym
.getRawDataRefImpl(), IndirectName
))
871 outs() << IndirectName
<< ")";
873 outs() << S
.IndirectName
<< ")";
876 } else if (OutputFormat
== sysv
) {
877 outs() << left_justify(Name
, 20) << "|" << SymbolAddrStr
<< "| "
878 << S
.TypeChar
<< " |" << right_justify(S
.TypeName
, 18) << "|"
879 << SymbolSizeStr
<< "| |" << S
.SectionName
<< "\n";
886 static char getSymbolNMTypeChar(ELFObjectFileBase
&Obj
,
887 basic_symbol_iterator I
) {
889 elf_symbol_iterator
SymI(I
);
891 Expected
<elf_section_iterator
> SecIOrErr
= SymI
->getSection();
893 consumeError(SecIOrErr
.takeError());
897 uint8_t Binding
= SymI
->getBinding();
898 if (Binding
== ELF::STB_GNU_UNIQUE
)
901 assert(Binding
!= ELF::STB_WEAK
&& "STB_WEAK not tested in calling function");
902 if (Binding
!= ELF::STB_GLOBAL
&& Binding
!= ELF::STB_LOCAL
)
905 elf_section_iterator SecI
= *SecIOrErr
;
906 if (SecI
!= Obj
.section_end()) {
907 uint32_t Type
= SecI
->getType();
908 uint64_t Flags
= SecI
->getFlags();
909 if (Flags
& ELF::SHF_EXECINSTR
)
911 if (Type
== ELF::SHT_NOBITS
)
913 if (Flags
& ELF::SHF_ALLOC
)
914 return Flags
& ELF::SHF_WRITE
? 'd' : 'r';
917 if (SecI
->getName(SecName
))
919 if (SecName
.startswith(".debug"))
921 if (!(Flags
& ELF::SHF_WRITE
))
928 static char getSymbolNMTypeChar(COFFObjectFile
&Obj
, symbol_iterator I
) {
929 COFFSymbolRef Symb
= Obj
.getCOFFSymbol(*I
);
931 symbol_iterator
SymI(I
);
933 Expected
<StringRef
> Name
= SymI
->getName();
935 consumeError(Name
.takeError());
939 char Ret
= StringSwitch
<char>(*Name
)
940 .StartsWith(".debug", 'N')
941 .StartsWith(".sxdata", 'N')
947 uint32_t Characteristics
= 0;
948 if (!COFF::isReservedSectionNumber(Symb
.getSectionNumber())) {
949 Expected
<section_iterator
> SecIOrErr
= SymI
->getSection();
951 consumeError(SecIOrErr
.takeError());
954 section_iterator SecI
= *SecIOrErr
;
955 const coff_section
*Section
= Obj
.getCOFFSection(*SecI
);
956 Characteristics
= Section
->Characteristics
;
957 if (Expected
<StringRef
> NameOrErr
= Obj
.getSectionName(Section
))
958 if (NameOrErr
->startswith(".idata"))
962 switch (Symb
.getSectionNumber()) {
963 case COFF::IMAGE_SYM_DEBUG
:
966 // Check section type.
967 if (Characteristics
& COFF::IMAGE_SCN_CNT_CODE
)
969 if (Characteristics
& COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
)
970 return Characteristics
& COFF::IMAGE_SCN_MEM_WRITE
? 'd' : 'r';
971 if (Characteristics
& COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
)
973 if (Characteristics
& COFF::IMAGE_SCN_LNK_INFO
)
975 // Check for section symbol.
976 if (Symb
.isSectionDefinition())
983 static char getSymbolNMTypeChar(COFFImportFile
&Obj
) {
984 switch (Obj
.getCOFFImportHeader()->getType()) {
985 case COFF::IMPORT_CODE
:
987 case COFF::IMPORT_DATA
:
989 case COFF::IMPORT_CONST
:
995 static char getSymbolNMTypeChar(MachOObjectFile
&Obj
, basic_symbol_iterator I
) {
996 DataRefImpl Symb
= I
->getRawDataRefImpl();
997 uint8_t NType
= Obj
.is64Bit() ? Obj
.getSymbol64TableEntry(Symb
).n_type
998 : Obj
.getSymbolTableEntry(Symb
).n_type
;
1000 if (NType
& MachO::N_STAB
)
1003 switch (NType
& MachO::N_TYPE
) {
1008 case MachO::N_SECT
: {
1009 Expected
<section_iterator
> SecOrErr
= Obj
.getSymbolSection(Symb
);
1011 consumeError(SecOrErr
.takeError());
1014 section_iterator Sec
= *SecOrErr
;
1015 if (Sec
== Obj
.section_end())
1017 DataRefImpl Ref
= Sec
->getRawDataRefImpl();
1018 StringRef SectionName
;
1019 if (Expected
<StringRef
> NameOrErr
= Obj
.getSectionName(Ref
))
1020 SectionName
= *NameOrErr
;
1021 StringRef SegmentName
= Obj
.getSectionFinalSegmentName(Ref
);
1022 if (Obj
.is64Bit() && Obj
.getHeader64().filetype
== MachO::MH_KEXT_BUNDLE
&&
1023 SegmentName
== "__TEXT_EXEC" && SectionName
== "__text")
1025 if (SegmentName
== "__TEXT" && SectionName
== "__text")
1027 if (SegmentName
== "__DATA" && SectionName
== "__data")
1029 if (SegmentName
== "__DATA" && SectionName
== "__bss")
1038 static char getSymbolNMTypeChar(WasmObjectFile
&Obj
, basic_symbol_iterator I
) {
1039 uint32_t Flags
= I
->getFlags();
1040 if (Flags
& SymbolRef::SF_Executable
)
1045 static char getSymbolNMTypeChar(IRObjectFile
&Obj
, basic_symbol_iterator I
) {
1046 uint32_t Flags
= I
->getFlags();
1047 // FIXME: should we print 'b'? At the IR level we cannot be sure if this
1048 // will be in bss or not, but we could approximate.
1049 if (Flags
& SymbolRef::SF_Executable
)
1051 else if (Triple(Obj
.getTargetTriple()).isOSDarwin() &&
1052 (Flags
& SymbolRef::SF_Const
))
1058 static bool isObject(SymbolicFile
&Obj
, basic_symbol_iterator I
) {
1059 return !dyn_cast
<ELFObjectFileBase
>(&Obj
)
1061 : elf_symbol_iterator(I
)->getELFType() == ELF::STT_OBJECT
;
1064 // For ELF object files, Set TypeName to the symbol typename, to be printed
1065 // in the 'Type' column of the SYSV format output.
1066 static StringRef
getNMTypeName(SymbolicFile
&Obj
, basic_symbol_iterator I
) {
1067 if (isa
<ELFObjectFileBase
>(&Obj
)) {
1068 elf_symbol_iterator
SymI(I
);
1069 return SymI
->getELFTypeName();
1074 // Return Posix nm class type tag (single letter), but also set SecName and
1075 // section and name, to be used in format=sysv output.
1076 static char getNMSectionTagAndName(SymbolicFile
&Obj
, basic_symbol_iterator I
,
1077 StringRef
&SecName
) {
1078 uint32_t Symflags
= I
->getFlags();
1079 if (isa
<ELFObjectFileBase
>(&Obj
)) {
1080 if (Symflags
& object::SymbolRef::SF_Absolute
)
1082 else if (Symflags
& object::SymbolRef::SF_Common
)
1084 else if (Symflags
& object::SymbolRef::SF_Undefined
)
1087 elf_symbol_iterator
SymI(I
);
1088 Expected
<elf_section_iterator
> SecIOrErr
= SymI
->getSection();
1090 consumeError(SecIOrErr
.takeError());
1093 elf_section_iterator secT
= *SecIOrErr
;
1094 secT
->getName(SecName
);
1098 if ((Symflags
& object::SymbolRef::SF_Weak
) && !isa
<MachOObjectFile
>(Obj
)) {
1099 char Ret
= isObject(Obj
, I
) ? 'v' : 'w';
1100 return (!(Symflags
& object::SymbolRef::SF_Undefined
)) ? toupper(Ret
) : Ret
;
1103 if (Symflags
& object::SymbolRef::SF_Undefined
)
1106 if (Symflags
& object::SymbolRef::SF_Common
)
1110 if (Symflags
& object::SymbolRef::SF_Absolute
)
1112 else if (IRObjectFile
*IR
= dyn_cast
<IRObjectFile
>(&Obj
))
1113 Ret
= getSymbolNMTypeChar(*IR
, I
);
1114 else if (COFFObjectFile
*COFF
= dyn_cast
<COFFObjectFile
>(&Obj
))
1115 Ret
= getSymbolNMTypeChar(*COFF
, I
);
1116 else if (COFFImportFile
*COFFImport
= dyn_cast
<COFFImportFile
>(&Obj
))
1117 Ret
= getSymbolNMTypeChar(*COFFImport
);
1118 else if (MachOObjectFile
*MachO
= dyn_cast
<MachOObjectFile
>(&Obj
))
1119 Ret
= getSymbolNMTypeChar(*MachO
, I
);
1120 else if (WasmObjectFile
*Wasm
= dyn_cast
<WasmObjectFile
>(&Obj
))
1121 Ret
= getSymbolNMTypeChar(*Wasm
, I
);
1123 Ret
= getSymbolNMTypeChar(cast
<ELFObjectFileBase
>(Obj
), I
);
1125 if (!(Symflags
& object::SymbolRef::SF_Global
))
1128 if (Obj
.isELF() && ELFSymbolRef(*I
).getBinding() == ELF::STB_GNU_UNIQUE
)
1131 return toupper(Ret
);
1134 // getNsectForSegSect() is used to implement the Mach-O "-s segname sectname"
1135 // option to dump only those symbols from that section in a Mach-O file.
1136 // It is called once for each Mach-O file from dumpSymbolNamesFromObject()
1137 // to get the section number for that named section from the command line
1138 // arguments. It returns the section number for that section in the Mach-O
1139 // file or zero it is not present.
1140 static unsigned getNsectForSegSect(MachOObjectFile
*Obj
) {
1142 for (auto &S
: Obj
->sections()) {
1143 DataRefImpl Ref
= S
.getRawDataRefImpl();
1144 StringRef SectionName
;
1145 if (Expected
<StringRef
> NameOrErr
= Obj
->getSectionName(Ref
))
1146 SectionName
= *NameOrErr
;
1147 StringRef SegmentName
= Obj
->getSectionFinalSegmentName(Ref
);
1148 if (SegmentName
== SegSect
[0] && SectionName
== SegSect
[1])
1155 // getNsectInMachO() is used to implement the Mach-O "-s segname sectname"
1156 // option to dump only those symbols from that section in a Mach-O file.
1157 // It is called once for each symbol in a Mach-O file from
1158 // dumpSymbolNamesFromObject() and returns the section number for that symbol
1159 // if it is in a section, else it returns 0.
1160 static unsigned getNsectInMachO(MachOObjectFile
&Obj
, BasicSymbolRef Sym
) {
1161 DataRefImpl Symb
= Sym
.getRawDataRefImpl();
1162 if (Obj
.is64Bit()) {
1163 MachO::nlist_64 STE
= Obj
.getSymbol64TableEntry(Symb
);
1164 return (STE
.n_type
& MachO::N_TYPE
) == MachO::N_SECT
? STE
.n_sect
: 0;
1166 MachO::nlist STE
= Obj
.getSymbolTableEntry(Symb
);
1167 return (STE
.n_type
& MachO::N_TYPE
) == MachO::N_SECT
? STE
.n_sect
: 0;
1171 dumpSymbolNamesFromObject(SymbolicFile
&Obj
, bool printName
,
1172 const std::string
&ArchiveName
= std::string(),
1173 const std::string
&ArchitectureName
= std::string()) {
1174 auto Symbols
= Obj
.symbols();
1176 const auto *E
= dyn_cast
<ELFObjectFileBase
>(&Obj
);
1178 error("File format has no dynamic symbol table", Obj
.getFileName());
1181 Symbols
= E
->getDynamicSymbolIterators();
1183 std::string NameBuffer
;
1184 raw_string_ostream
OS(NameBuffer
);
1185 // If a "-s segname sectname" option was specified and this is a Mach-O
1186 // file get the section number for that section in this object file.
1187 unsigned int Nsect
= 0;
1188 MachOObjectFile
*MachO
= dyn_cast
<MachOObjectFile
>(&Obj
);
1189 if (!SegSect
.empty() && MachO
) {
1190 Nsect
= getNsectForSegSect(MachO
);
1191 // If this section is not in the object file no symbols are printed.
1195 if (!MachO
|| !DyldInfoOnly
) {
1196 for (BasicSymbolRef Sym
: Symbols
) {
1197 uint32_t SymFlags
= Sym
.getFlags();
1198 if (!DebugSyms
&& (SymFlags
& SymbolRef::SF_FormatSpecific
))
1200 if (WithoutAliases
&& (SymFlags
& SymbolRef::SF_Indirect
))
1202 // If a "-s segname sectname" option was specified and this is a Mach-O
1203 // file and this section appears in this file, Nsect will be non-zero then
1204 // see if this symbol is a symbol from that section and if not skip it.
1205 if (Nsect
&& Nsect
!= getNsectInMachO(*MachO
, Sym
))
1210 if (isa
<ELFObjectFileBase
>(&Obj
))
1211 S
.Size
= ELFSymbolRef(Sym
).getSize();
1212 if (PrintAddress
&& isa
<ObjectFile
>(Obj
)) {
1213 SymbolRef
SymRef(Sym
);
1214 Expected
<uint64_t> AddressOrErr
= SymRef
.getAddress();
1215 if (!AddressOrErr
) {
1216 consumeError(AddressOrErr
.takeError());
1219 S
.Address
= *AddressOrErr
;
1221 S
.TypeName
= getNMTypeName(Obj
, Sym
);
1222 S
.TypeChar
= getNMSectionTagAndName(Obj
, Sym
, S
.SectionName
);
1223 if (Error E
= Sym
.printName(OS
)) {
1225 OS
<< "bad string index";
1226 consumeError(std::move(E
));
1228 error(std::move(E
), Obj
.getFileName());
1232 SymbolList
.push_back(S
);
1237 const char *P
= NameBuffer
.c_str();
1239 for (I
= 0; I
< SymbolList
.size(); ++I
) {
1240 SymbolList
[I
].Name
= P
;
1244 // If this is a Mach-O file where the nlist symbol table is out of sync
1245 // with the dyld export trie then look through exports and fake up symbols
1246 // for the ones that are missing (also done with the -add-dyldinfo flag).
1247 // This is needed if strip(1) -T is run on a binary containing swift
1248 // language symbols for example. The option -only-dyldinfo will fake up
1249 // all symbols from the dyld export trie as well as the bind info.
1250 std::string ExportsNameBuffer
;
1251 raw_string_ostream
EOS(ExportsNameBuffer
);
1252 std::string BindsNameBuffer
;
1253 raw_string_ostream
BOS(BindsNameBuffer
);
1254 std::string LazysNameBuffer
;
1255 raw_string_ostream
LOS(LazysNameBuffer
);
1256 std::string WeaksNameBuffer
;
1257 raw_string_ostream
WOS(WeaksNameBuffer
);
1258 std::string FunctionStartsNameBuffer
;
1259 raw_string_ostream
FOS(FunctionStartsNameBuffer
);
1260 if (MachO
&& !NoDyldInfo
) {
1261 MachO::mach_header H
;
1262 MachO::mach_header_64 H_64
;
1263 uint32_t HFlags
= 0;
1264 if (MachO
->is64Bit()) {
1265 H_64
= MachO
->MachOObjectFile::getHeader64();
1266 HFlags
= H_64
.flags
;
1268 H
= MachO
->MachOObjectFile::getHeader();
1271 uint64_t BaseSegmentAddress
= 0;
1272 for (const auto &Command
: MachO
->load_commands()) {
1273 if (Command
.C
.cmd
== MachO::LC_SEGMENT
) {
1274 MachO::segment_command Seg
= MachO
->getSegmentLoadCommand(Command
);
1275 if (Seg
.fileoff
== 0 && Seg
.filesize
!= 0) {
1276 BaseSegmentAddress
= Seg
.vmaddr
;
1279 } else if (Command
.C
.cmd
== MachO::LC_SEGMENT_64
) {
1280 MachO::segment_command_64 Seg
= MachO
->getSegment64LoadCommand(Command
);
1281 if (Seg
.fileoff
== 0 && Seg
.filesize
!= 0) {
1282 BaseSegmentAddress
= Seg
.vmaddr
;
1287 if (DyldInfoOnly
|| AddDyldInfo
||
1288 HFlags
& MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO
) {
1289 unsigned ExportsAdded
= 0;
1290 Error Err
= Error::success();
1291 for (const llvm::object::ExportEntry
&Entry
: MachO
->exports(Err
)) {
1293 bool ReExport
= false;
1294 if (!DyldInfoOnly
) {
1295 for (const NMSymbol
&S
: SymbolList
)
1296 if (S
.Address
== Entry
.address() + BaseSegmentAddress
&&
1297 S
.Name
== Entry
.name()) {
1304 S
.Address
= Entry
.address() + BaseSegmentAddress
;
1307 S
.Name
= Entry
.name();
1308 // There is no symbol in the nlist symbol table for this so we set
1309 // Sym effectivly to null and the rest of code in here must test for
1310 // it and not do things like Sym.getFlags() for it.
1311 S
.Sym
= BasicSymbolRef();
1312 S
.SymFlags
= SymbolRef::SF_Global
;
1313 S
.Section
= SectionRef();
1317 S
.IndirectName
= StringRef();
1319 uint64_t EFlags
= Entry
.flags();
1320 bool Abs
= ((EFlags
& MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK
) ==
1321 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE
);
1322 bool Resolver
= (EFlags
&
1323 MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER
);
1324 ReExport
= (EFlags
& MachO::EXPORT_SYMBOL_FLAGS_REEXPORT
);
1325 bool WeakDef
= (EFlags
& MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION
);
1327 S
.NDesc
|= MachO::N_WEAK_DEF
;
1329 S
.NType
= MachO::N_EXT
| MachO::N_ABS
;
1331 } else if (ReExport
) {
1332 S
.NType
= MachO::N_EXT
| MachO::N_INDR
;
1335 S
.NType
= MachO::N_EXT
| MachO::N_SECT
;
1337 S
.Address
= Entry
.other() + BaseSegmentAddress
;
1338 if ((S
.Address
& 1) != 0 &&
1339 !MachO
->is64Bit() && H
.cputype
== MachO::CPU_TYPE_ARM
){
1341 S
.NDesc
|= MachO::N_ARM_THUMB_DEF
;
1344 S
.Address
= Entry
.address() + BaseSegmentAddress
;
1346 StringRef SegmentName
= StringRef();
1347 StringRef SectionName
= StringRef();
1348 for (const SectionRef
&Section
: MachO
->sections()) {
1350 Section
.getName(SectionName
);
1351 SegmentName
= MachO
->getSectionFinalSegmentName(
1352 Section
.getRawDataRefImpl());
1353 if (S
.Address
>= Section
.getAddress() &&
1354 S
.Address
< Section
.getAddress() + Section
.getSize()) {
1355 S
.Section
= Section
;
1357 } else if (Entry
.name() == "__mh_execute_header" &&
1358 SegmentName
== "__TEXT" && SectionName
== "__text") {
1359 S
.Section
= Section
;
1360 S
.NDesc
|= MachO::REFERENCED_DYNAMICALLY
;
1364 if (SegmentName
== "__TEXT" && SectionName
== "__text")
1366 else if (SegmentName
== "__DATA" && SectionName
== "__data")
1368 else if (SegmentName
== "__DATA" && SectionName
== "__bss")
1373 SymbolList
.push_back(S
);
1375 EOS
<< Entry
.name();
1379 // For ReExports there are a two more things to do, first add the
1380 // indirect name and second create the undefined symbol using the
1381 // referened dynamic library.
1384 // Add the indirect name.
1385 if (Entry
.otherName().empty())
1386 EOS
<< Entry
.name();
1388 EOS
<< Entry
.otherName();
1391 // Now create the undefined symbol using the referened dynamic
1397 if (Entry
.otherName().empty())
1398 U
.Name
= Entry
.name();
1400 U
.Name
= Entry
.otherName();
1401 // Again there is no symbol in the nlist symbol table for this so
1402 // we set Sym effectivly to null and the rest of code in here must
1403 // test for it and not do things like Sym.getFlags() for it.
1404 U
.Sym
= BasicSymbolRef();
1405 U
.SymFlags
= SymbolRef::SF_Global
| SymbolRef::SF_Undefined
;
1406 U
.Section
= SectionRef();
1407 U
.NType
= MachO::N_EXT
| MachO::N_UNDF
;
1410 // The library ordinal for this undefined symbol is in the export
1411 // trie Entry.other().
1412 MachO::SET_LIBRARY_ORDINAL(U
.NDesc
, Entry
.other());
1413 U
.IndirectName
= StringRef();
1414 SymbolList
.push_back(U
);
1416 // Finally add the undefined symbol's name.
1417 if (Entry
.otherName().empty())
1418 EOS
<< Entry
.name();
1420 EOS
<< Entry
.otherName();
1427 error(std::move(Err
), MachO
->getFileName());
1428 // Set the symbol names and indirect names for the added symbols.
1431 const char *Q
= ExportsNameBuffer
.c_str();
1432 for (unsigned K
= 0; K
< ExportsAdded
; K
++) {
1433 SymbolList
[I
].Name
= Q
;
1435 if (SymbolList
[I
].TypeChar
== 'I') {
1436 SymbolList
[I
].IndirectName
= Q
;
1443 // Add the undefined symbols from the bind entries.
1444 unsigned BindsAdded
= 0;
1445 Error BErr
= Error::success();
1446 StringRef LastSymbolName
= StringRef();
1447 for (const llvm::object::MachOBindEntry
&Entry
: MachO
->bindTable(BErr
)) {
1449 if (LastSymbolName
== Entry
.symbolName())
1451 else if(!DyldInfoOnly
) {
1452 for (unsigned J
= 0; J
< SymbolList
.size() && !found
; ++J
) {
1453 if (SymbolList
[J
].Name
== Entry
.symbolName())
1458 LastSymbolName
= Entry
.symbolName();
1463 // There is no symbol in the nlist symbol table for this so we set
1464 // Sym effectivly to null and the rest of code in here must test for
1465 // it and not do things like Sym.getFlags() for it.
1466 B
.Sym
= BasicSymbolRef();
1467 B
.SymFlags
= SymbolRef::SF_Global
| SymbolRef::SF_Undefined
;
1468 B
.NType
= MachO::N_EXT
| MachO::N_UNDF
;
1471 MachO::SET_LIBRARY_ORDINAL(B
.NDesc
, Entry
.ordinal());
1472 B
.IndirectName
= StringRef();
1473 B
.Name
= Entry
.symbolName();
1474 SymbolList
.push_back(B
);
1475 BOS
<< Entry
.symbolName();
1481 error(std::move(BErr
), MachO
->getFileName());
1482 // Set the symbol names and indirect names for the added symbols.
1485 const char *Q
= BindsNameBuffer
.c_str();
1486 for (unsigned K
= 0; K
< BindsAdded
; K
++) {
1487 SymbolList
[I
].Name
= Q
;
1489 if (SymbolList
[I
].TypeChar
== 'I') {
1490 SymbolList
[I
].IndirectName
= Q
;
1497 // Add the undefined symbols from the lazy bind entries.
1498 unsigned LazysAdded
= 0;
1499 Error LErr
= Error::success();
1500 LastSymbolName
= StringRef();
1501 for (const llvm::object::MachOBindEntry
&Entry
:
1502 MachO
->lazyBindTable(LErr
)) {
1504 if (LastSymbolName
== Entry
.symbolName())
1507 // Here we must check to see it this symbol is already in the
1508 // SymbolList as it might have already have been added above via a
1509 // non-lazy (bind) entry.
1510 for (unsigned J
= 0; J
< SymbolList
.size() && !found
; ++J
) {
1511 if (SymbolList
[J
].Name
== Entry
.symbolName())
1516 LastSymbolName
= Entry
.symbolName();
1518 L
.Name
= Entry
.symbolName();
1522 // There is no symbol in the nlist symbol table for this so we set
1523 // Sym effectivly to null and the rest of code in here must test for
1524 // it and not do things like Sym.getFlags() for it.
1525 L
.Sym
= BasicSymbolRef();
1526 L
.SymFlags
= SymbolRef::SF_Global
| SymbolRef::SF_Undefined
;
1527 L
.NType
= MachO::N_EXT
| MachO::N_UNDF
;
1529 // The REFERENCE_FLAG_UNDEFINED_LAZY is no longer used but here it
1530 // makes sence since we are creating this from a lazy bind entry.
1531 L
.NDesc
= MachO::REFERENCE_FLAG_UNDEFINED_LAZY
;
1532 MachO::SET_LIBRARY_ORDINAL(L
.NDesc
, Entry
.ordinal());
1533 L
.IndirectName
= StringRef();
1534 SymbolList
.push_back(L
);
1535 LOS
<< Entry
.symbolName();
1541 error(std::move(LErr
), MachO
->getFileName());
1542 // Set the symbol names and indirect names for the added symbols.
1545 const char *Q
= LazysNameBuffer
.c_str();
1546 for (unsigned K
= 0; K
< LazysAdded
; K
++) {
1547 SymbolList
[I
].Name
= Q
;
1549 if (SymbolList
[I
].TypeChar
== 'I') {
1550 SymbolList
[I
].IndirectName
= Q
;
1557 // Add the undefineds symbol from the weak bind entries which are not
1559 unsigned WeaksAdded
= 0;
1560 Error WErr
= Error::success();
1561 LastSymbolName
= StringRef();
1562 for (const llvm::object::MachOBindEntry
&Entry
:
1563 MachO
->weakBindTable(WErr
)) {
1566 if (LastSymbolName
== Entry
.symbolName() ||
1567 Entry
.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION
) {
1570 for (J
= 0; J
< SymbolList
.size() && !found
; ++J
) {
1571 if (SymbolList
[J
].Name
== Entry
.symbolName()) {
1578 LastSymbolName
= Entry
.symbolName();
1580 memset(&W
, '\0', sizeof(NMSymbol
));
1581 W
.Name
= Entry
.symbolName();
1585 // There is no symbol in the nlist symbol table for this so we set
1586 // Sym effectivly to null and the rest of code in here must test for
1587 // it and not do things like Sym.getFlags() for it.
1588 W
.Sym
= BasicSymbolRef();
1589 W
.SymFlags
= SymbolRef::SF_Global
| SymbolRef::SF_Undefined
;
1590 W
.NType
= MachO::N_EXT
| MachO::N_UNDF
;
1592 // Odd that we are using N_WEAK_DEF on an undefined symbol but that is
1593 // what is created in this case by the linker when there are real
1594 // symbols in the nlist structs.
1595 W
.NDesc
= MachO::N_WEAK_DEF
;
1596 W
.IndirectName
= StringRef();
1597 SymbolList
.push_back(W
);
1598 WOS
<< Entry
.symbolName();
1602 // This is the case the symbol was previously been found and it could
1603 // have been added from a bind or lazy bind symbol. If so and not
1604 // a definition also mark it as weak.
1605 if (SymbolList
[J
].TypeChar
== 'U')
1606 // See comment above about N_WEAK_DEF.
1607 SymbolList
[J
].NDesc
|= MachO::N_WEAK_DEF
;
1611 error(std::move(WErr
), MachO
->getFileName());
1612 // Set the symbol names and indirect names for the added symbols.
1615 const char *Q
= WeaksNameBuffer
.c_str();
1616 for (unsigned K
= 0; K
< WeaksAdded
; K
++) {
1617 SymbolList
[I
].Name
= Q
;
1619 if (SymbolList
[I
].TypeChar
== 'I') {
1620 SymbolList
[I
].IndirectName
= Q
;
1627 // Trying adding symbol from the function starts table and LC_MAIN entry
1629 SmallVector
<uint64_t, 8> FoundFns
;
1630 uint64_t lc_main_offset
= UINT64_MAX
;
1631 for (const auto &Command
: MachO
->load_commands()) {
1632 if (Command
.C
.cmd
== MachO::LC_FUNCTION_STARTS
) {
1633 // We found a function starts segment, parse the addresses for
1635 MachO::linkedit_data_command LLC
=
1636 MachO
->getLinkeditDataLoadCommand(Command
);
1638 MachO
->ReadULEB128s(LLC
.dataoff
, FoundFns
);
1639 } else if (Command
.C
.cmd
== MachO::LC_MAIN
) {
1640 MachO::entry_point_command LCmain
=
1641 MachO
->getEntryPointCommand(Command
);
1642 lc_main_offset
= LCmain
.entryoff
;
1645 // See if these addresses are already in the symbol table.
1646 unsigned FunctionStartsAdded
= 0;
1647 for (uint64_t f
= 0; f
< FoundFns
.size(); f
++) {
1649 for (unsigned J
= 0; J
< SymbolList
.size() && !found
; ++J
) {
1650 if (SymbolList
[J
].Address
== FoundFns
[f
] + BaseSegmentAddress
)
1653 // See this address is not already in the symbol table fake up an
1657 F
.Name
= "<redacted function X>";
1658 F
.Address
= FoundFns
[f
] + BaseSegmentAddress
;
1660 // There is no symbol in the nlist symbol table for this so we set
1661 // Sym effectivly to null and the rest of code in here must test for
1662 // it and not do things like Sym.getFlags() for it.
1663 F
.Sym
= BasicSymbolRef();
1665 F
.NType
= MachO::N_SECT
;
1667 StringRef SegmentName
= StringRef();
1668 StringRef SectionName
= StringRef();
1669 for (const SectionRef
&Section
: MachO
->sections()) {
1670 Section
.getName(SectionName
);
1671 SegmentName
= MachO
->getSectionFinalSegmentName(
1672 Section
.getRawDataRefImpl());
1674 if (F
.Address
>= Section
.getAddress() &&
1675 F
.Address
< Section
.getAddress() + Section
.getSize()) {
1676 F
.Section
= Section
;
1680 if (SegmentName
== "__TEXT" && SectionName
== "__text")
1682 else if (SegmentName
== "__DATA" && SectionName
== "__data")
1684 else if (SegmentName
== "__DATA" && SectionName
== "__bss")
1689 F
.IndirectName
= StringRef();
1690 SymbolList
.push_back(F
);
1691 if (FoundFns
[f
] == lc_main_offset
)
1692 FOS
<< "<redacted LC_MAIN>";
1694 FOS
<< "<redacted function " << f
<< ">";
1696 FunctionStartsAdded
++;
1699 if (FunctionStartsAdded
) {
1701 const char *Q
= FunctionStartsNameBuffer
.c_str();
1702 for (unsigned K
= 0; K
< FunctionStartsAdded
; K
++) {
1703 SymbolList
[I
].Name
= Q
;
1705 if (SymbolList
[I
].TypeChar
== 'I') {
1706 SymbolList
[I
].IndirectName
= Q
;
1715 CurrentFilename
= Obj
.getFileName();
1716 sortAndPrintSymbolList(Obj
, printName
, ArchiveName
, ArchitectureName
);
1719 // checkMachOAndArchFlags() checks to see if the SymbolicFile is a Mach-O file
1720 // and if it is and there is a list of architecture flags is specified then
1721 // check to make sure this Mach-O file is one of those architectures or all
1722 // architectures was specificed. If not then an error is generated and this
1723 // routine returns false. Else it returns true.
1724 static bool checkMachOAndArchFlags(SymbolicFile
*O
, std::string
&Filename
) {
1725 auto *MachO
= dyn_cast
<MachOObjectFile
>(O
);
1727 if (!MachO
|| ArchAll
|| ArchFlags
.empty())
1730 MachO::mach_header H
;
1731 MachO::mach_header_64 H_64
;
1733 const char *McpuDefault
, *ArchFlag
;
1734 if (MachO
->is64Bit()) {
1735 H_64
= MachO
->MachOObjectFile::getHeader64();
1736 T
= MachOObjectFile::getArchTriple(H_64
.cputype
, H_64
.cpusubtype
,
1737 &McpuDefault
, &ArchFlag
);
1739 H
= MachO
->MachOObjectFile::getHeader();
1740 T
= MachOObjectFile::getArchTriple(H
.cputype
, H
.cpusubtype
,
1741 &McpuDefault
, &ArchFlag
);
1743 const std::string
ArchFlagName(ArchFlag
);
1744 if (none_of(ArchFlags
, [&](const std::string
&Name
) {
1745 return Name
== ArchFlagName
;
1747 error("No architecture specified", Filename
);
1753 static void dumpSymbolNamesFromFile(std::string
&Filename
) {
1754 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> BufferOrErr
=
1755 MemoryBuffer::getFileOrSTDIN(Filename
);
1756 if (error(BufferOrErr
.getError(), Filename
))
1759 LLVMContext Context
;
1760 LLVMContext
*ContextPtr
= NoLLVMBitcode
? nullptr : &Context
;
1761 Expected
<std::unique_ptr
<Binary
>> BinaryOrErr
=
1762 createBinary(BufferOrErr
.get()->getMemBufferRef(), ContextPtr
);
1764 error(BinaryOrErr
.takeError(), Filename
);
1767 Binary
&Bin
= *BinaryOrErr
.get();
1769 if (Archive
*A
= dyn_cast
<Archive
>(&Bin
)) {
1771 Archive::symbol_iterator I
= A
->symbol_begin();
1772 Archive::symbol_iterator E
= A
->symbol_end();
1774 outs() << "Archive map\n";
1775 for (; I
!= E
; ++I
) {
1776 Expected
<Archive::Child
> C
= I
->getMember();
1778 error(C
.takeError(), Filename
);
1781 Expected
<StringRef
> FileNameOrErr
= C
->getName();
1782 if (!FileNameOrErr
) {
1783 error(FileNameOrErr
.takeError(), Filename
);
1786 StringRef SymName
= I
->getName();
1787 outs() << SymName
<< " in " << FileNameOrErr
.get() << "\n";
1794 Error Err
= Error::success();
1795 for (auto &C
: A
->children(Err
)) {
1796 Expected
<std::unique_ptr
<Binary
>> ChildOrErr
=
1797 C
.getAsBinary(ContextPtr
);
1799 if (auto E
= isNotObjectErrorInvalidFileType(ChildOrErr
.takeError()))
1800 error(std::move(E
), Filename
, C
);
1803 if (SymbolicFile
*O
= dyn_cast
<SymbolicFile
>(&*ChildOrErr
.get())) {
1804 if (!MachOPrintSizeWarning
&& PrintSize
&& isa
<MachOObjectFile
>(O
)) {
1805 WithColor::warning(errs(), ToolName
)
1806 << "sizes with -print-size for Mach-O files are always zero.\n";
1807 MachOPrintSizeWarning
= true;
1809 if (!checkMachOAndArchFlags(O
, Filename
))
1811 if (!PrintFileName
) {
1813 if (isa
<MachOObjectFile
>(O
)) {
1814 outs() << Filename
<< "(" << O
->getFileName() << ")";
1816 outs() << O
->getFileName();
1819 dumpSymbolNamesFromObject(*O
, false, Filename
);
1823 error(std::move(Err
), A
->getFileName());
1827 if (MachOUniversalBinary
*UB
= dyn_cast
<MachOUniversalBinary
>(&Bin
)) {
1828 // If we have a list of architecture flags specified dump only those.
1829 if (!ArchAll
&& !ArchFlags
.empty()) {
1830 // Look for a slice in the universal binary that matches each ArchFlag.
1832 for (unsigned i
= 0; i
< ArchFlags
.size(); ++i
) {
1834 for (MachOUniversalBinary::object_iterator I
= UB
->begin_objects(),
1835 E
= UB
->end_objects();
1837 if (ArchFlags
[i
] == I
->getArchFlagName()) {
1839 Expected
<std::unique_ptr
<ObjectFile
>> ObjOrErr
=
1840 I
->getAsObjectFile();
1841 std::string ArchiveName
;
1842 std::string ArchitectureName
;
1843 ArchiveName
.clear();
1844 ArchitectureName
.clear();
1846 ObjectFile
&Obj
= *ObjOrErr
.get();
1847 if (ArchFlags
.size() > 1) {
1849 ArchitectureName
= I
->getArchFlagName();
1851 outs() << "\n" << Obj
.getFileName() << " (for architecture "
1852 << I
->getArchFlagName() << ")"
1855 dumpSymbolNamesFromObject(Obj
, false, ArchiveName
,
1857 } else if (auto E
= isNotObjectErrorInvalidFileType(
1858 ObjOrErr
.takeError())) {
1859 error(std::move(E
), Filename
, ArchFlags
.size() > 1 ?
1860 StringRef(I
->getArchFlagName()) : StringRef());
1862 } else if (Expected
<std::unique_ptr
<Archive
>> AOrErr
=
1863 I
->getAsArchive()) {
1864 std::unique_ptr
<Archive
> &A
= *AOrErr
;
1865 Error Err
= Error::success();
1866 for (auto &C
: A
->children(Err
)) {
1867 Expected
<std::unique_ptr
<Binary
>> ChildOrErr
=
1868 C
.getAsBinary(ContextPtr
);
1870 if (auto E
= isNotObjectErrorInvalidFileType(
1871 ChildOrErr
.takeError())) {
1872 error(std::move(E
), Filename
, C
, ArchFlags
.size() > 1 ?
1873 StringRef(I
->getArchFlagName()) : StringRef());
1877 if (SymbolicFile
*O
=
1878 dyn_cast
<SymbolicFile
>(&*ChildOrErr
.get())) {
1879 if (PrintFileName
) {
1880 ArchiveName
= A
->getFileName();
1881 if (ArchFlags
.size() > 1)
1882 ArchitectureName
= I
->getArchFlagName();
1884 outs() << "\n" << A
->getFileName();
1885 outs() << "(" << O
->getFileName() << ")";
1886 if (ArchFlags
.size() > 1) {
1887 outs() << " (for architecture " << I
->getArchFlagName()
1892 dumpSymbolNamesFromObject(*O
, false, ArchiveName
,
1897 error(std::move(Err
), A
->getFileName());
1899 consumeError(AOrErr
.takeError());
1900 error(Filename
+ " for architecture " +
1901 StringRef(I
->getArchFlagName()) +
1902 " is not a Mach-O file or an archive file",
1903 "Mach-O universal file");
1909 "file: " + Filename
+ " does not contain architecture");
1915 // No architecture flags were specified so if this contains a slice that
1916 // matches the host architecture dump only that.
1918 Triple HostTriple
= MachOObjectFile::getHostArch();
1919 StringRef HostArchName
= HostTriple
.getArchName();
1920 for (MachOUniversalBinary::object_iterator I
= UB
->begin_objects(),
1921 E
= UB
->end_objects();
1923 if (HostArchName
== I
->getArchFlagName()) {
1924 Expected
<std::unique_ptr
<ObjectFile
>> ObjOrErr
= I
->getAsObjectFile();
1925 std::string ArchiveName
;
1927 ObjectFile
&Obj
= *ObjOrErr
.get();
1928 dumpSymbolNamesFromObject(Obj
, false);
1929 } else if (auto E
= isNotObjectErrorInvalidFileType(
1930 ObjOrErr
.takeError())) {
1931 error(std::move(E
), Filename
);
1933 } else if (Expected
<std::unique_ptr
<Archive
>> AOrErr
=
1934 I
->getAsArchive()) {
1935 std::unique_ptr
<Archive
> &A
= *AOrErr
;
1936 Error Err
= Error::success();
1937 for (auto &C
: A
->children(Err
)) {
1938 Expected
<std::unique_ptr
<Binary
>> ChildOrErr
=
1939 C
.getAsBinary(ContextPtr
);
1941 if (auto E
= isNotObjectErrorInvalidFileType(
1942 ChildOrErr
.takeError()))
1943 error(std::move(E
), Filename
, C
);
1946 if (SymbolicFile
*O
=
1947 dyn_cast
<SymbolicFile
>(&*ChildOrErr
.get())) {
1949 ArchiveName
= A
->getFileName();
1951 outs() << "\n" << A
->getFileName() << "(" << O
->getFileName()
1954 dumpSymbolNamesFromObject(*O
, false, ArchiveName
);
1958 error(std::move(Err
), A
->getFileName());
1960 consumeError(AOrErr
.takeError());
1961 error(Filename
+ " for architecture " +
1962 StringRef(I
->getArchFlagName()) +
1963 " is not a Mach-O file or an archive file",
1964 "Mach-O universal file");
1970 // Either all architectures have been specified or none have been specified
1971 // and this does not contain the host architecture so dump all the slices.
1972 bool moreThanOneArch
= UB
->getNumberOfObjects() > 1;
1973 for (const MachOUniversalBinary::ObjectForArch
&O
: UB
->objects()) {
1974 Expected
<std::unique_ptr
<ObjectFile
>> ObjOrErr
= O
.getAsObjectFile();
1975 std::string ArchiveName
;
1976 std::string ArchitectureName
;
1977 ArchiveName
.clear();
1978 ArchitectureName
.clear();
1980 ObjectFile
&Obj
= *ObjOrErr
.get();
1981 if (PrintFileName
) {
1982 if (isa
<MachOObjectFile
>(Obj
) && moreThanOneArch
)
1983 ArchitectureName
= O
.getArchFlagName();
1985 if (moreThanOneArch
)
1987 outs() << Obj
.getFileName();
1988 if (isa
<MachOObjectFile
>(Obj
) && moreThanOneArch
)
1989 outs() << " (for architecture " << O
.getArchFlagName() << ")";
1992 dumpSymbolNamesFromObject(Obj
, false, ArchiveName
, ArchitectureName
);
1993 } else if (auto E
= isNotObjectErrorInvalidFileType(
1994 ObjOrErr
.takeError())) {
1995 error(std::move(E
), Filename
, moreThanOneArch
?
1996 StringRef(O
.getArchFlagName()) : StringRef());
1998 } else if (Expected
<std::unique_ptr
<Archive
>> AOrErr
=
2000 std::unique_ptr
<Archive
> &A
= *AOrErr
;
2001 Error Err
= Error::success();
2002 for (auto &C
: A
->children(Err
)) {
2003 Expected
<std::unique_ptr
<Binary
>> ChildOrErr
=
2004 C
.getAsBinary(ContextPtr
);
2006 if (auto E
= isNotObjectErrorInvalidFileType(
2007 ChildOrErr
.takeError()))
2008 error(std::move(E
), Filename
, C
, moreThanOneArch
?
2009 StringRef(ArchitectureName
) : StringRef());
2012 if (SymbolicFile
*F
= dyn_cast
<SymbolicFile
>(&*ChildOrErr
.get())) {
2013 if (PrintFileName
) {
2014 ArchiveName
= A
->getFileName();
2015 if (isa
<MachOObjectFile
>(F
) && moreThanOneArch
)
2016 ArchitectureName
= O
.getArchFlagName();
2018 outs() << "\n" << A
->getFileName();
2019 if (isa
<MachOObjectFile
>(F
)) {
2020 outs() << "(" << F
->getFileName() << ")";
2021 if (moreThanOneArch
)
2022 outs() << " (for architecture " << O
.getArchFlagName()
2025 outs() << ":" << F
->getFileName();
2028 dumpSymbolNamesFromObject(*F
, false, ArchiveName
, ArchitectureName
);
2032 error(std::move(Err
), A
->getFileName());
2034 consumeError(AOrErr
.takeError());
2035 error(Filename
+ " for architecture " +
2036 StringRef(O
.getArchFlagName()) +
2037 " is not a Mach-O file or an archive file",
2038 "Mach-O universal file");
2043 if (SymbolicFile
*O
= dyn_cast
<SymbolicFile
>(&Bin
)) {
2044 if (!MachOPrintSizeWarning
&& PrintSize
&& isa
<MachOObjectFile
>(O
)) {
2045 WithColor::warning(errs(), ToolName
)
2046 << "sizes with --print-size for Mach-O files are always zero.\n";
2047 MachOPrintSizeWarning
= true;
2049 if (!checkMachOAndArchFlags(O
, Filename
))
2051 dumpSymbolNamesFromObject(*O
, true);
2055 int main(int argc
, char **argv
) {
2056 InitLLVM
X(argc
, argv
);
2057 cl::HideUnrelatedOptions(NMCat
);
2058 cl::ParseCommandLineOptions(argc
, argv
, "llvm symbol table dumper\n");
2060 // llvm-nm only reads binary files.
2061 if (error(sys::ChangeStdinToBinary()))
2064 // These calls are needed so that we can read bitcode correctly.
2065 llvm::InitializeAllTargetInfos();
2066 llvm::InitializeAllTargetMCs();
2067 llvm::InitializeAllAsmParsers();
2073 OutputFormat
= posix
;
2075 OutputFormat
= darwin
;
2077 // The relative order of these is important. If you pass --size-sort it should
2078 // only print out the size. However, if you pass -S --size-sort, it should
2079 // print out both the size and address.
2080 if (SizeSort
&& !PrintSize
)
2081 PrintAddress
= false;
2082 if (OutputFormat
== sysv
|| SizeSort
)
2084 if (InputFilenames
.empty())
2085 InputFilenames
.push_back("a.out");
2086 if (InputFilenames
.size() > 1)
2087 MultipleFiles
= true;
2089 // If both --demangle and --no-demangle are specified then pick the last one.
2090 if (NoDemangle
.getPosition() > Demangle
.getPosition())
2091 Demangle
= !NoDemangle
;
2093 for (unsigned i
= 0; i
< ArchFlags
.size(); ++i
) {
2094 if (ArchFlags
[i
] == "all") {
2097 if (!MachOObjectFile::isValidArch(ArchFlags
[i
]))
2098 error("Unknown architecture named '" + ArchFlags
[i
] + "'",
2099 "for the --arch option");
2103 if (!SegSect
.empty() && SegSect
.size() != 2)
2104 error("bad number of arguments (must be two arguments)",
2105 "for the -s option");
2107 if (NoDyldInfo
&& (AddDyldInfo
|| DyldInfoOnly
))
2108 error("--no-dyldinfo can't be used with --add-dyldinfo or --dyldinfo-only");
2110 llvm::for_each(InputFilenames
, dumpSymbolNamesFromFile
);