1 //===-- llvm-nm.cpp - Symbol table dumping utility for llvm ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This program is a utility that works like traditional Unix "nm", that is, it
11 // prints out the names of symbols in a bitcode or object file, along with some
12 // information about each symbol.
14 // This "nm" supports many of the features of GNU "nm", including its different
17 //===----------------------------------------------------------------------===//
19 #include "llvm/ADT/StringSwitch.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/IR/GlobalAlias.h"
22 #include "llvm/IR/GlobalVariable.h"
23 #include "llvm/IR/LLVMContext.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/Object/Archive.h"
26 #include "llvm/Object/COFF.h"
27 #include "llvm/Object/ELFObjectFile.h"
28 #include "llvm/Object/IRObjectFile.h"
29 #include "llvm/Object/MachO.h"
30 #include "llvm/Object/MachOUniversal.h"
31 #include "llvm/Object/ObjectFile.h"
32 #include "llvm/Object/Wasm.h"
33 #include "llvm/Support/COFF.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/Support/FileSystem.h"
36 #include "llvm/Support/Format.h"
37 #include "llvm/Support/ManagedStatic.h"
38 #include "llvm/Support/MemoryBuffer.h"
39 #include "llvm/Support/PrettyStackTrace.h"
40 #include "llvm/Support/Program.h"
41 #include "llvm/Support/Signals.h"
42 #include "llvm/Support/TargetSelect.h"
43 #include "llvm/Support/raw_ostream.h"
48 #include <system_error>
52 using namespace object;
55 enum OutputFormatTy { bsd, sysv, posix, darwin };
56 cl::opt<OutputFormatTy> OutputFormat(
57 "format", cl::desc("Specify output format"),
58 cl::values(clEnumVal(bsd, "BSD format"), clEnumVal(sysv, "System V format"),
59 clEnumVal(posix, "POSIX.2 format"),
60 clEnumVal(darwin, "Darwin -m format")),
62 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
63 cl::aliasopt(OutputFormat));
65 cl::list<std::string> InputFilenames(cl::Positional, cl::desc("<input files>"),
68 cl::opt<bool> UndefinedOnly("undefined-only",
69 cl::desc("Show only undefined symbols"));
70 cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
71 cl::aliasopt(UndefinedOnly), cl::Grouping);
73 cl::opt<bool> DynamicSyms("dynamic",
74 cl::desc("Display the dynamic symbols instead "
75 "of normal symbols."));
76 cl::alias DynamicSyms2("D", cl::desc("Alias for --dynamic"),
77 cl::aliasopt(DynamicSyms), cl::Grouping);
79 cl::opt<bool> DefinedOnly("defined-only",
80 cl::desc("Show only defined symbols"));
81 cl::alias DefinedOnly2("U", cl::desc("Alias for --defined-only"),
82 cl::aliasopt(DefinedOnly), cl::Grouping);
84 cl::opt<bool> ExternalOnly("extern-only",
85 cl::desc("Show only external symbols"));
86 cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
87 cl::aliasopt(ExternalOnly), cl::Grouping);
89 cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"),
91 cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"),
93 cl::opt<bool> DarwinFormat("m", cl::desc("Alias for --format=darwin"),
96 static cl::list<std::string>
97 ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
101 cl::opt<bool> PrintFileName(
103 cl::desc("Precede each symbol with the object file it came from"));
105 cl::alias PrintFileNameA("A", cl::desc("Alias for --print-file-name"),
106 cl::aliasopt(PrintFileName), cl::Grouping);
107 cl::alias PrintFileNameo("o", cl::desc("Alias for --print-file-name"),
108 cl::aliasopt(PrintFileName), cl::Grouping);
110 cl::opt<bool> DebugSyms("debug-syms",
111 cl::desc("Show all symbols, even debugger only"));
112 cl::alias DebugSymsa("a", cl::desc("Alias for --debug-syms"),
113 cl::aliasopt(DebugSyms), cl::Grouping);
115 cl::opt<bool> NumericSort("numeric-sort", cl::desc("Sort symbols by address"));
116 cl::alias NumericSortn("n", cl::desc("Alias for --numeric-sort"),
117 cl::aliasopt(NumericSort), cl::Grouping);
118 cl::alias NumericSortv("v", cl::desc("Alias for --numeric-sort"),
119 cl::aliasopt(NumericSort), cl::Grouping);
121 cl::opt<bool> NoSort("no-sort", cl::desc("Show symbols in order encountered"));
122 cl::alias NoSortp("p", cl::desc("Alias for --no-sort"), cl::aliasopt(NoSort),
125 cl::opt<bool> ReverseSort("reverse-sort", cl::desc("Sort in reverse order"));
126 cl::alias ReverseSortr("r", cl::desc("Alias for --reverse-sort"),
127 cl::aliasopt(ReverseSort), cl::Grouping);
129 cl::opt<bool> PrintSize("print-size",
130 cl::desc("Show symbol size instead of address"));
131 cl::alias PrintSizeS("S", cl::desc("Alias for --print-size"),
132 cl::aliasopt(PrintSize), cl::Grouping);
133 bool MachOPrintSizeWarning = false;
135 cl::opt<bool> SizeSort("size-sort", cl::desc("Sort symbols by size"));
137 cl::opt<bool> WithoutAliases("without-aliases", cl::Hidden,
138 cl::desc("Exclude aliases from output"));
140 cl::opt<bool> ArchiveMap("print-armap", cl::desc("Print the archive map"));
141 cl::alias ArchiveMaps("M", cl::desc("Alias for --print-armap"),
142 cl::aliasopt(ArchiveMap), cl::Grouping);
144 enum Radix { d, o, x };
146 AddressRadix("radix", cl::desc("Radix (o/d/x) for printing symbol Values"),
147 cl::values(clEnumVal(d, "decimal"), clEnumVal(o, "octal"),
148 clEnumVal(x, "hexadecimal")),
150 cl::alias RadixAlias("t", cl::desc("Alias for --radix"),
151 cl::aliasopt(AddressRadix));
153 cl::opt<bool> JustSymbolName("just-symbol-name",
154 cl::desc("Print just the symbol's name"));
155 cl::alias JustSymbolNames("j", cl::desc("Alias for --just-symbol-name"),
156 cl::aliasopt(JustSymbolName), cl::Grouping);
158 // FIXME: This option takes exactly two strings and should be allowed anywhere
159 // on the command line. Such that "llvm-nm -s __TEXT __text foo.o" would work.
160 // But that does not as the CommandLine Library does not have a way to make
161 // this work. For now the "-s __TEXT __text" has to be last on the command
163 cl::list<std::string> SegSect("s", cl::Positional, cl::ZeroOrMore,
164 cl::desc("Dump only symbols from this segment "
165 "and section name, Mach-O only"));
167 cl::opt<bool> FormatMachOasHex("x", cl::desc("Print symbol entry in hex, "
168 "Mach-O only"), cl::Grouping);
170 cl::opt<bool> NoLLVMBitcode("no-llvm-bc",
171 cl::desc("Disable LLVM bitcode reader"));
173 bool PrintAddress = true;
175 bool MultipleFiles = false;
177 bool HadError = false;
179 std::string ToolName;
180 } // anonymous namespace
182 static void error(Twine Message, Twine Path = Twine()) {
184 errs() << ToolName << ": " << Path << ": " << Message << ".\n";
187 static bool error(std::error_code EC, Twine Path = Twine()) {
189 error(EC.message(), Path);
195 // This version of error() prints the archive name and member name, for example:
196 // "libx.a(foo.o)" after the ToolName before the error message. It sets
197 // HadError but returns allowing the code to move on to other archive members.
198 static void error(llvm::Error E, StringRef FileName, const Archive::Child &C,
199 StringRef ArchitectureName = StringRef()) {
201 errs() << ToolName << ": " << FileName;
203 Expected<StringRef> NameOrErr = C.getName();
204 // TODO: if we have a error getting the name then it would be nice to print
205 // the index of which archive member this is and or its offset in the
206 // archive instead of "???" as the name.
208 consumeError(NameOrErr.takeError());
209 errs() << "(" << "???" << ")";
211 errs() << "(" << NameOrErr.get() << ")";
213 if (!ArchitectureName.empty())
214 errs() << " (for architecture " << ArchitectureName << ") ";
217 raw_string_ostream OS(Buf);
218 logAllUnhandledErrors(std::move(E), OS, "");
220 errs() << " " << Buf << "\n";
223 // This version of error() prints the file name and which architecture slice it
224 // is from, for example: "foo.o (for architecture i386)" after the ToolName
225 // before the error message. It sets HadError but returns allowing the code to
226 // move on to other architecture slices.
227 static void error(llvm::Error E, StringRef FileName,
228 StringRef ArchitectureName = StringRef()) {
230 errs() << ToolName << ": " << FileName;
232 if (!ArchitectureName.empty())
233 errs() << " (for architecture " << ArchitectureName << ") ";
236 raw_string_ostream OS(Buf);
237 logAllUnhandledErrors(std::move(E), OS, "");
239 errs() << " " << Buf << "\n";
250 } // anonymous namespace
252 static bool compareSymbolAddress(const NMSymbol &A, const NMSymbol &B) {
253 bool ADefined = !(A.Sym.getFlags() & SymbolRef::SF_Undefined);
254 bool BDefined = !(B.Sym.getFlags() & SymbolRef::SF_Undefined);
255 return std::make_tuple(ADefined, A.Address, A.Name, A.Size) <
256 std::make_tuple(BDefined, B.Address, B.Name, B.Size);
259 static bool compareSymbolSize(const NMSymbol &A, const NMSymbol &B) {
260 return std::make_tuple(A.Size, A.Name, A.Address) <
261 std::make_tuple(B.Size, B.Name, B.Address);
264 static bool compareSymbolName(const NMSymbol &A, const NMSymbol &B) {
265 return std::make_tuple(A.Name, A.Size, A.Address) <
266 std::make_tuple(B.Name, B.Size, B.Address);
269 static char isSymbolList64Bit(SymbolicFile &Obj) {
270 if (auto *IRObj = dyn_cast<IRObjectFile>(&Obj))
271 return Triple(IRObj->getTargetTriple()).isArch64Bit();
272 if (isa<COFFObjectFile>(Obj))
274 if (isa<WasmObjectFile>(Obj))
276 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj))
277 return MachO->is64Bit();
278 return cast<ELFObjectFileBase>(Obj).getBytesInAddress() == 8;
281 static StringRef CurrentFilename;
282 typedef std::vector<NMSymbol> SymbolListT;
283 static SymbolListT SymbolList;
285 static char getSymbolNMTypeChar(IRObjectFile &Obj, basic_symbol_iterator I);
287 // darwinPrintSymbol() is used to print a symbol from a Mach-O file when the
288 // the OutputFormat is darwin or we are printing Mach-O symbols in hex. For
289 // the darwin format it produces the same output as darwin's nm(1) -m output
290 // and when printing Mach-O symbols in hex it produces the same output as
291 // darwin's nm(1) -x format.
292 static void darwinPrintSymbol(SymbolicFile &Obj, SymbolListT::iterator I,
293 char *SymbolAddrStr, const char *printBlanks,
294 const char *printDashes, const char *printFormat) {
295 MachO::mach_header H;
296 MachO::mach_header_64 H_64;
297 uint32_t Filetype = MachO::MH_OBJECT;
304 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj);
306 uint32_t SymFlags = I->Sym.getFlags();
307 if (SymFlags & SymbolRef::SF_Global)
308 NType |= MachO::N_EXT;
309 if (SymFlags & SymbolRef::SF_Hidden)
310 NType |= MachO::N_PEXT;
311 if (SymFlags & SymbolRef::SF_Undefined)
312 NType |= MachO::N_EXT | MachO::N_UNDF;
314 // Here we have a symbol definition. So to fake out a section name we
315 // use 1, 2 and 3 for section numbers. See below where they are used to
316 // print out fake section names.
317 NType |= MachO::N_SECT;
318 if (SymFlags & SymbolRef::SF_Const)
320 else if (SymFlags & SymbolRef::SF_Executable)
325 if (SymFlags & SymbolRef::SF_Weak)
326 NDesc |= MachO::N_WEAK_DEF;
328 DataRefImpl SymDRI = I->Sym.getRawDataRefImpl();
329 if (MachO->is64Bit()) {
330 H_64 = MachO->MachOObjectFile::getHeader64();
331 Filetype = H_64.filetype;
333 MachO::nlist_64 STE_64 = MachO->getSymbol64TableEntry(SymDRI);
334 NType = STE_64.n_type;
335 NSect = STE_64.n_sect;
336 NDesc = STE_64.n_desc;
337 NStrx = STE_64.n_strx;
338 NValue = STE_64.n_value;
340 H = MachO->MachOObjectFile::getHeader();
341 Filetype = H.filetype;
343 MachO::nlist STE = MachO->getSymbolTableEntry(SymDRI);
348 NValue = STE.n_value;
352 // If we are printing Mach-O symbols in hex do that and return.
353 if (FormatMachOasHex) {
355 format(printFormat, NValue).print(Str, sizeof(Str));
356 outs() << Str << ' ';
357 format("%02x", NType).print(Str, sizeof(Str));
358 outs() << Str << ' ';
359 format("%02x", NSect).print(Str, sizeof(Str));
360 outs() << Str << ' ';
361 format("%04x", NDesc).print(Str, sizeof(Str));
362 outs() << Str << ' ';
363 format("%08x", NStrx).print(Str, sizeof(Str));
364 outs() << Str << ' ';
365 outs() << I->Name << "\n";
370 if ((NType & MachO::N_TYPE) == MachO::N_INDR)
371 strcpy(SymbolAddrStr, printBlanks);
372 if (Obj.isIR() && (NType & MachO::N_TYPE) == MachO::N_TYPE)
373 strcpy(SymbolAddrStr, printDashes);
374 outs() << SymbolAddrStr << ' ';
377 switch (NType & MachO::N_TYPE) {
380 outs() << "(common) ";
381 if (MachO::GET_COMM_ALIGN(NDesc) != 0)
382 outs() << "(alignment 2^" << (int)MachO::GET_COMM_ALIGN(NDesc) << ") ";
384 if ((NType & MachO::N_TYPE) == MachO::N_PBUD)
385 outs() << "(prebound ";
388 if ((NDesc & MachO::REFERENCE_TYPE) ==
389 MachO::REFERENCE_FLAG_UNDEFINED_LAZY)
390 outs() << "undefined [lazy bound]) ";
391 else if ((NDesc & MachO::REFERENCE_TYPE) ==
392 MachO::REFERENCE_FLAG_PRIVATE_UNDEFINED_LAZY)
393 outs() << "undefined [private lazy bound]) ";
394 else if ((NDesc & MachO::REFERENCE_TYPE) ==
395 MachO::REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY)
396 outs() << "undefined [private]) ";
398 outs() << "undefined) ";
402 outs() << "(absolute) ";
405 outs() << "(indirect) ";
407 case MachO::N_SECT: {
409 // For llvm bitcode files print out a fake section name using the values
410 // use 1, 2 and 3 for section numbers as set above.
412 outs() << "(LTO,CODE) ";
414 outs() << "(LTO,DATA) ";
416 outs() << "(LTO,RODATA) ";
421 Expected<section_iterator> SecOrErr =
422 MachO->getSymbolSection(I->Sym.getRawDataRefImpl());
424 consumeError(SecOrErr.takeError());
428 section_iterator Sec = *SecOrErr;
429 DataRefImpl Ref = Sec->getRawDataRefImpl();
430 StringRef SectionName;
431 MachO->getSectionName(Ref, SectionName);
432 StringRef SegmentName = MachO->getSectionFinalSegmentName(Ref);
433 outs() << "(" << SegmentName << "," << SectionName << ") ";
441 if (NType & MachO::N_EXT) {
442 if (NDesc & MachO::REFERENCED_DYNAMICALLY)
443 outs() << "[referenced dynamically] ";
444 if (NType & MachO::N_PEXT) {
445 if ((NDesc & MachO::N_WEAK_DEF) == MachO::N_WEAK_DEF)
446 outs() << "weak private external ";
448 outs() << "private external ";
450 if ((NDesc & MachO::N_WEAK_REF) == MachO::N_WEAK_REF ||
451 (NDesc & MachO::N_WEAK_DEF) == MachO::N_WEAK_DEF) {
452 if ((NDesc & (MachO::N_WEAK_REF | MachO::N_WEAK_DEF)) ==
453 (MachO::N_WEAK_REF | MachO::N_WEAK_DEF))
454 outs() << "weak external automatically hidden ";
456 outs() << "weak external ";
458 outs() << "external ";
461 if (NType & MachO::N_PEXT)
462 outs() << "non-external (was a private external) ";
464 outs() << "non-external ";
467 if (Filetype == MachO::MH_OBJECT &&
468 (NDesc & MachO::N_NO_DEAD_STRIP) == MachO::N_NO_DEAD_STRIP)
469 outs() << "[no dead strip] ";
471 if (Filetype == MachO::MH_OBJECT &&
472 ((NType & MachO::N_TYPE) != MachO::N_UNDF) &&
473 (NDesc & MachO::N_SYMBOL_RESOLVER) == MachO::N_SYMBOL_RESOLVER)
474 outs() << "[symbol resolver] ";
476 if (Filetype == MachO::MH_OBJECT &&
477 ((NType & MachO::N_TYPE) != MachO::N_UNDF) &&
478 (NDesc & MachO::N_ALT_ENTRY) == MachO::N_ALT_ENTRY)
479 outs() << "[alt entry] ";
481 if ((NDesc & MachO::N_ARM_THUMB_DEF) == MachO::N_ARM_THUMB_DEF)
482 outs() << "[Thumb] ";
484 if ((NType & MachO::N_TYPE) == MachO::N_INDR) {
485 outs() << I->Name << " (for ";
486 StringRef IndirectName;
488 MachO->getIndirectName(I->Sym.getRawDataRefImpl(), IndirectName))
491 outs() << IndirectName << ")";
495 if ((Flags & MachO::MH_TWOLEVEL) == MachO::MH_TWOLEVEL &&
496 (((NType & MachO::N_TYPE) == MachO::N_UNDF && NValue == 0) ||
497 (NType & MachO::N_TYPE) == MachO::N_PBUD)) {
498 uint32_t LibraryOrdinal = MachO::GET_LIBRARY_ORDINAL(NDesc);
499 if (LibraryOrdinal != 0) {
500 if (LibraryOrdinal == MachO::EXECUTABLE_ORDINAL)
501 outs() << " (from executable)";
502 else if (LibraryOrdinal == MachO::DYNAMIC_LOOKUP_ORDINAL)
503 outs() << " (dynamically looked up)";
505 StringRef LibraryName;
507 MachO->getLibraryShortNameByIndex(LibraryOrdinal - 1, LibraryName))
508 outs() << " (from bad library ordinal " << LibraryOrdinal << ")";
510 outs() << " (from " << LibraryName << ")";
518 // Table that maps Darwin's Mach-O stab constants to strings to allow printing.
519 struct DarwinStabName {
523 static const struct DarwinStabName DarwinStabNames[] = {
524 {MachO::N_GSYM, "GSYM"},
525 {MachO::N_FNAME, "FNAME"},
526 {MachO::N_FUN, "FUN"},
527 {MachO::N_STSYM, "STSYM"},
528 {MachO::N_LCSYM, "LCSYM"},
529 {MachO::N_BNSYM, "BNSYM"},
531 {MachO::N_AST, "AST"},
532 {MachO::N_OPT, "OPT"},
533 {MachO::N_RSYM, "RSYM"},
534 {MachO::N_SLINE, "SLINE"},
535 {MachO::N_ENSYM, "ENSYM"},
536 {MachO::N_SSYM, "SSYM"},
538 {MachO::N_OSO, "OSO"},
539 {MachO::N_LSYM, "LSYM"},
540 {MachO::N_BINCL, "BINCL"},
541 {MachO::N_SOL, "SOL"},
542 {MachO::N_PARAMS, "PARAM"},
543 {MachO::N_VERSION, "VERS"},
544 {MachO::N_OLEVEL, "OLEV"},
545 {MachO::N_PSYM, "PSYM"},
546 {MachO::N_EINCL, "EINCL"},
547 {MachO::N_ENTRY, "ENTRY"},
548 {MachO::N_LBRAC, "LBRAC"},
549 {MachO::N_EXCL, "EXCL"},
550 {MachO::N_RBRAC, "RBRAC"},
551 {MachO::N_BCOMM, "BCOMM"},
552 {MachO::N_ECOMM, "ECOMM"},
553 {MachO::N_ECOML, "ECOML"},
554 {MachO::N_LENG, "LENG"},
557 static const char *getDarwinStabString(uint8_t NType) {
558 for (unsigned i = 0; DarwinStabNames[i].Name; i++) {
559 if (DarwinStabNames[i].NType == NType)
560 return DarwinStabNames[i].Name;
565 // darwinPrintStab() prints the n_sect, n_desc along with a symbolic name of
566 // a stab n_type value in a Mach-O file.
567 static void darwinPrintStab(MachOObjectFile *MachO, SymbolListT::iterator I) {
568 MachO::nlist_64 STE_64;
573 DataRefImpl SymDRI = I->Sym.getRawDataRefImpl();
574 if (MachO->is64Bit()) {
575 STE_64 = MachO->getSymbol64TableEntry(SymDRI);
576 NType = STE_64.n_type;
577 NSect = STE_64.n_sect;
578 NDesc = STE_64.n_desc;
580 STE = MachO->getSymbolTableEntry(SymDRI);
587 format("%02x", NSect).print(Str, sizeof(Str));
588 outs() << ' ' << Str << ' ';
589 format("%04x", NDesc).print(Str, sizeof(Str));
590 outs() << Str << ' ';
591 if (const char *stabString = getDarwinStabString(NType))
592 format("%5.5s", stabString).print(Str, sizeof(Str));
594 format(" %02x", NType).print(Str, sizeof(Str));
598 static bool symbolIsDefined(const NMSymbol &Sym) {
599 return Sym.TypeChar != 'U' && Sym.TypeChar != 'w' && Sym.TypeChar != 'v';
602 static void sortAndPrintSymbolList(SymbolicFile &Obj, bool printName,
603 const std::string &ArchiveName,
604 const std::string &ArchitectureName) {
606 std::function<bool(const NMSymbol &, const NMSymbol &)> Cmp;
608 Cmp = compareSymbolAddress;
610 Cmp = compareSymbolSize;
612 Cmp = compareSymbolName;
615 Cmp = [=](const NMSymbol &A, const NMSymbol &B) { return Cmp(B, A); };
616 std::sort(SymbolList.begin(), SymbolList.end(), Cmp);
619 if (!PrintFileName) {
620 if (OutputFormat == posix && MultipleFiles && printName) {
621 outs() << '\n' << CurrentFilename << ":\n";
622 } else if (OutputFormat == bsd && MultipleFiles && printName) {
623 outs() << "\n" << CurrentFilename << ":\n";
624 } else if (OutputFormat == sysv) {
625 outs() << "\n\nSymbols from " << CurrentFilename << ":\n\n"
626 << "Name Value Class Type"
627 << " Size Line Section\n";
631 const char *printBlanks, *printDashes, *printFormat;
632 if (isSymbolList64Bit(Obj)) {
634 printDashes = "----------------";
635 switch (AddressRadix) {
637 printFormat = OutputFormat == posix ? "%" PRIo64 : "%016" PRIo64;
640 printFormat = OutputFormat == posix ? "%" PRIx64 : "%016" PRIx64;
643 printFormat = OutputFormat == posix ? "%" PRId64 : "%016" PRId64;
647 printDashes = "--------";
648 switch (AddressRadix) {
650 printFormat = OutputFormat == posix ? "%" PRIo64 : "%08" PRIo64;
653 printFormat = OutputFormat == posix ? "%" PRIx64 : "%08" PRIx64;
656 printFormat = OutputFormat == posix ? "%" PRId64 : "%08" PRId64;
660 for (SymbolListT::iterator I = SymbolList.begin(), E = SymbolList.end();
662 uint32_t SymFlags = I->Sym.getFlags();
663 bool Undefined = SymFlags & SymbolRef::SF_Undefined;
664 bool Global = SymFlags & SymbolRef::SF_Global;
665 if ((!Undefined && UndefinedOnly) || (Undefined && DefinedOnly) ||
666 (!Global && ExternalOnly) || (SizeSort && !PrintAddress))
669 if (!ArchitectureName.empty())
670 outs() << "(for architecture " << ArchitectureName << "):";
671 if (OutputFormat == posix && !ArchiveName.empty())
672 outs() << ArchiveName << "[" << CurrentFilename << "]: ";
674 if (!ArchiveName.empty())
675 outs() << ArchiveName << ":";
676 outs() << CurrentFilename << ": ";
679 if ((JustSymbolName || (UndefinedOnly && isa<MachOObjectFile>(Obj) &&
680 OutputFormat != darwin)) && OutputFormat != posix) {
681 outs() << I->Name << "\n";
685 char SymbolAddrStr[18] = "";
686 char SymbolSizeStr[18] = "";
688 // If the format is SysV or the symbol isn't defined, then print spaces.
689 if (OutputFormat == sysv || !symbolIsDefined(*I)) {
690 if (OutputFormat == posix) {
691 format(printFormat, I->Address)
692 .print(SymbolAddrStr, sizeof(SymbolAddrStr));
693 format(printFormat, I->Size)
694 .print(SymbolSizeStr, sizeof(SymbolSizeStr));
696 strcpy(SymbolAddrStr, printBlanks);
697 strcpy(SymbolSizeStr, printBlanks);
701 // Otherwise, print the symbol address and size.
702 if (symbolIsDefined(*I)) {
704 strcpy(SymbolAddrStr, printDashes);
706 format(printFormat, I->Address)
707 .print(SymbolAddrStr, sizeof(SymbolAddrStr));
708 format(printFormat, I->Size).print(SymbolSizeStr, sizeof(SymbolSizeStr));
711 // If OutputFormat is darwin or we are printing Mach-O symbols in hex and
712 // we have a MachOObjectFile, call darwinPrintSymbol to print as darwin's
713 // nm(1) -m output or hex, else if OutputFormat is darwin or we are
714 // printing Mach-O symbols in hex and not a Mach-O object fall back to
715 // OutputFormat bsd (see below).
716 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj);
717 if ((OutputFormat == darwin || FormatMachOasHex) && (MachO || Obj.isIR())) {
718 darwinPrintSymbol(Obj, I, SymbolAddrStr, printBlanks, printDashes,
720 } else if (OutputFormat == posix) {
721 outs() << I->Name << " " << I->TypeChar << " ";
723 outs() << SymbolAddrStr << " " << "0" /* SymbolSizeStr */ << "\n";
725 outs() << SymbolAddrStr << " " << SymbolSizeStr << "\n";
726 } else if (OutputFormat == bsd || (OutputFormat == darwin && !MachO)) {
728 outs() << SymbolAddrStr << ' ';
730 outs() << SymbolSizeStr;
733 outs() << I->TypeChar;
734 if (I->TypeChar == '-' && MachO)
735 darwinPrintStab(MachO, I);
736 outs() << " " << I->Name << "\n";
737 } else if (OutputFormat == sysv) {
738 std::string PaddedName(I->Name);
739 while (PaddedName.length() < 20)
741 outs() << PaddedName << "|" << SymbolAddrStr << "| " << I->TypeChar
742 << " | |" << SymbolSizeStr << "| |\n";
749 static char getSymbolNMTypeChar(ELFObjectFileBase &Obj,
750 basic_symbol_iterator I) {
752 elf_symbol_iterator SymI(I);
754 Expected<elf_section_iterator> SecIOrErr = SymI->getSection();
756 consumeError(SecIOrErr.takeError());
760 elf_section_iterator SecI = *SecIOrErr;
761 if (SecI != Obj.section_end()) {
762 switch (SecI->getType()) {
763 case ELF::SHT_PROGBITS:
764 case ELF::SHT_DYNAMIC:
765 switch (SecI->getFlags()) {
766 case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR):
768 case (ELF::SHF_TLS | ELF::SHF_ALLOC | ELF::SHF_WRITE):
769 case (ELF::SHF_ALLOC | ELF::SHF_WRITE):
772 case (ELF::SHF_ALLOC | ELF::SHF_MERGE):
773 case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS):
777 case ELF::SHT_NOBITS:
779 case ELF::SHT_INIT_ARRAY:
780 case ELF::SHT_FINI_ARRAY:
785 if (SymI->getELFType() == ELF::STT_SECTION) {
786 Expected<StringRef> Name = SymI->getName();
788 consumeError(Name.takeError());
791 return StringSwitch<char>(*Name)
792 .StartsWith(".debug", 'N')
793 .StartsWith(".note", 'n')
800 static char getSymbolNMTypeChar(COFFObjectFile &Obj, symbol_iterator I) {
801 COFFSymbolRef Symb = Obj.getCOFFSymbol(*I);
803 symbol_iterator SymI(I);
805 Expected<StringRef> Name = SymI->getName();
807 consumeError(Name.takeError());
811 char Ret = StringSwitch<char>(*Name)
812 .StartsWith(".debug", 'N')
813 .StartsWith(".sxdata", 'N')
819 uint32_t Characteristics = 0;
820 if (!COFF::isReservedSectionNumber(Symb.getSectionNumber())) {
821 Expected<section_iterator> SecIOrErr = SymI->getSection();
823 consumeError(SecIOrErr.takeError());
826 section_iterator SecI = *SecIOrErr;
827 const coff_section *Section = Obj.getCOFFSection(*SecI);
828 Characteristics = Section->Characteristics;
831 switch (Symb.getSectionNumber()) {
832 case COFF::IMAGE_SYM_DEBUG:
835 // Check section type.
836 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
838 if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
839 return Characteristics & COFF::IMAGE_SCN_MEM_WRITE ? 'd' : 'r';
840 if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
842 if (Characteristics & COFF::IMAGE_SCN_LNK_INFO)
844 // Check for section symbol.
845 if (Symb.isSectionDefinition())
852 static char getSymbolNMTypeChar(MachOObjectFile &Obj, basic_symbol_iterator I) {
853 DataRefImpl Symb = I->getRawDataRefImpl();
854 uint8_t NType = Obj.is64Bit() ? Obj.getSymbol64TableEntry(Symb).n_type
855 : Obj.getSymbolTableEntry(Symb).n_type;
857 if (NType & MachO::N_STAB)
860 switch (NType & MachO::N_TYPE) {
865 case MachO::N_SECT: {
866 Expected<section_iterator> SecOrErr = Obj.getSymbolSection(Symb);
868 consumeError(SecOrErr.takeError());
871 section_iterator Sec = *SecOrErr;
872 DataRefImpl Ref = Sec->getRawDataRefImpl();
873 StringRef SectionName;
874 Obj.getSectionName(Ref, SectionName);
875 StringRef SegmentName = Obj.getSectionFinalSegmentName(Ref);
876 if (SegmentName == "__TEXT" && SectionName == "__text")
878 if (SegmentName == "__DATA" && SectionName == "__data")
880 if (SegmentName == "__DATA" && SectionName == "__bss")
889 static char getSymbolNMTypeChar(WasmObjectFile &Obj, basic_symbol_iterator I) {
890 uint32_t Flags = I->getFlags();
891 if (Flags & SymbolRef::SF_Executable)
896 static char getSymbolNMTypeChar(IRObjectFile &Obj, basic_symbol_iterator I) {
897 uint32_t Flags = I->getFlags();
898 // FIXME: should we print 'b'? At the IR level we cannot be sure if this
899 // will be in bss or not, but we could approximate.
900 if (Flags & SymbolRef::SF_Executable)
902 else if (Triple(Obj.getTargetTriple()).isOSDarwin() &&
903 (Flags & SymbolRef::SF_Const))
909 static bool isObject(SymbolicFile &Obj, basic_symbol_iterator I) {
910 return !dyn_cast<ELFObjectFileBase>(&Obj)
912 : elf_symbol_iterator(I)->getELFType() == ELF::STT_OBJECT;
915 static char getNMTypeChar(SymbolicFile &Obj, basic_symbol_iterator I) {
916 uint32_t Symflags = I->getFlags();
917 if ((Symflags & object::SymbolRef::SF_Weak) && !isa<MachOObjectFile>(Obj)) {
918 char Ret = isObject(Obj, I) ? 'v' : 'w';
919 return (!(Symflags & object::SymbolRef::SF_Undefined)) ? toupper(Ret) : Ret;
922 if (Symflags & object::SymbolRef::SF_Undefined)
925 if (Symflags & object::SymbolRef::SF_Common)
929 if (Symflags & object::SymbolRef::SF_Absolute)
931 else if (IRObjectFile *IR = dyn_cast<IRObjectFile>(&Obj))
932 Ret = getSymbolNMTypeChar(*IR, I);
933 else if (COFFObjectFile *COFF = dyn_cast<COFFObjectFile>(&Obj))
934 Ret = getSymbolNMTypeChar(*COFF, I);
935 else if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj))
936 Ret = getSymbolNMTypeChar(*MachO, I);
937 else if (WasmObjectFile *Wasm = dyn_cast<WasmObjectFile>(&Obj))
938 Ret = getSymbolNMTypeChar(*Wasm, I);
940 Ret = getSymbolNMTypeChar(cast<ELFObjectFileBase>(Obj), I);
942 if (Symflags & object::SymbolRef::SF_Global)
948 // getNsectForSegSect() is used to implement the Mach-O "-s segname sectname"
949 // option to dump only those symbols from that section in a Mach-O file.
950 // It is called once for each Mach-O file from dumpSymbolNamesFromObject()
951 // to get the section number for that named section from the command line
952 // arguments. It returns the section number for that section in the Mach-O
953 // file or zero it is not present.
954 static unsigned getNsectForSegSect(MachOObjectFile *Obj) {
956 for (auto &S : Obj->sections()) {
957 DataRefImpl Ref = S.getRawDataRefImpl();
958 StringRef SectionName;
959 Obj->getSectionName(Ref, SectionName);
960 StringRef SegmentName = Obj->getSectionFinalSegmentName(Ref);
961 if (SegmentName == SegSect[0] && SectionName == SegSect[1])
968 // getNsectInMachO() is used to implement the Mach-O "-s segname sectname"
969 // option to dump only those symbols from that section in a Mach-O file.
970 // It is called once for each symbol in a Mach-O file from
971 // dumpSymbolNamesFromObject() and returns the section number for that symbol
972 // if it is in a section, else it returns 0.
973 static unsigned getNsectInMachO(MachOObjectFile &Obj, BasicSymbolRef Sym) {
974 DataRefImpl Symb = Sym.getRawDataRefImpl();
976 MachO::nlist_64 STE = Obj.getSymbol64TableEntry(Symb);
977 return (STE.n_type & MachO::N_TYPE) == MachO::N_SECT ? STE.n_sect : 0;
979 MachO::nlist STE = Obj.getSymbolTableEntry(Symb);
980 return (STE.n_type & MachO::N_TYPE) == MachO::N_SECT ? STE.n_sect : 0;
984 dumpSymbolNamesFromObject(SymbolicFile &Obj, bool printName,
985 const std::string &ArchiveName = std::string(),
986 const std::string &ArchitectureName = std::string()) {
987 auto Symbols = Obj.symbols();
989 const auto *E = dyn_cast<ELFObjectFileBase>(&Obj);
991 error("File format has no dynamic symbol table", Obj.getFileName());
994 auto DynSymbols = E->getDynamicSymbolIterators();
996 make_range<basic_symbol_iterator>(DynSymbols.begin(), DynSymbols.end());
998 std::string NameBuffer;
999 raw_string_ostream OS(NameBuffer);
1000 // If a "-s segname sectname" option was specified and this is a Mach-O
1001 // file get the section number for that section in this object file.
1002 unsigned int Nsect = 0;
1003 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj);
1004 if (SegSect.size() != 0 && MachO) {
1005 Nsect = getNsectForSegSect(MachO);
1006 // If this section is not in the object file no symbols are printed.
1010 for (BasicSymbolRef Sym : Symbols) {
1011 uint32_t SymFlags = Sym.getFlags();
1012 if (!DebugSyms && (SymFlags & SymbolRef::SF_FormatSpecific))
1014 if (WithoutAliases && (SymFlags & SymbolRef::SF_Indirect))
1016 // If a "-s segname sectname" option was specified and this is a Mach-O
1017 // file and this section appears in this file, Nsect will be non-zero then
1018 // see if this symbol is a symbol from that section and if not skip it.
1019 if (Nsect && Nsect != getNsectInMachO(*MachO, Sym))
1025 if (isa<ELFObjectFileBase>(&Obj))
1026 S.Size = ELFSymbolRef(Sym).getSize();
1028 if (PrintAddress && isa<ObjectFile>(Obj)) {
1029 SymbolRef SymRef(Sym);
1030 Expected<uint64_t> AddressOrErr = SymRef.getAddress();
1031 if (!AddressOrErr) {
1032 consumeError(AddressOrErr.takeError());
1035 S.Address = *AddressOrErr;
1037 S.TypeChar = getNMTypeChar(Obj, Sym);
1038 std::error_code EC = Sym.printName(OS);
1040 OS << "bad string index";
1045 SymbolList.push_back(S);
1049 const char *P = NameBuffer.c_str();
1050 for (unsigned I = 0; I < SymbolList.size(); ++I) {
1051 SymbolList[I].Name = P;
1055 CurrentFilename = Obj.getFileName();
1056 sortAndPrintSymbolList(Obj, printName, ArchiveName, ArchitectureName);
1059 // checkMachOAndArchFlags() checks to see if the SymbolicFile is a Mach-O file
1060 // and if it is and there is a list of architecture flags is specified then
1061 // check to make sure this Mach-O file is one of those architectures or all
1062 // architectures was specificed. If not then an error is generated and this
1063 // routine returns false. Else it returns true.
1064 static bool checkMachOAndArchFlags(SymbolicFile *O, std::string &Filename) {
1065 auto *MachO = dyn_cast<MachOObjectFile>(O);
1067 if (!MachO || ArchAll || ArchFlags.empty())
1070 MachO::mach_header H;
1071 MachO::mach_header_64 H_64;
1073 const char *McpuDefault, *ArchFlag;
1074 if (MachO->is64Bit()) {
1075 H_64 = MachO->MachOObjectFile::getHeader64();
1076 T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype,
1077 &McpuDefault, &ArchFlag);
1079 H = MachO->MachOObjectFile::getHeader();
1080 T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype,
1081 &McpuDefault, &ArchFlag);
1083 const std::string ArchFlagName(ArchFlag);
1084 if (none_of(ArchFlags, [&](const std::string &Name) {
1085 return Name == ArchFlagName;
1087 error("No architecture specified", Filename);
1093 static void dumpSymbolNamesFromFile(std::string &Filename) {
1094 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
1095 MemoryBuffer::getFileOrSTDIN(Filename);
1096 if (error(BufferOrErr.getError(), Filename))
1099 LLVMContext Context;
1100 Expected<std::unique_ptr<Binary>> BinaryOrErr = createBinary(
1101 BufferOrErr.get()->getMemBufferRef(), NoLLVMBitcode ? nullptr : &Context);
1103 error(BinaryOrErr.takeError(), Filename);
1106 Binary &Bin = *BinaryOrErr.get();
1108 if (Archive *A = dyn_cast<Archive>(&Bin)) {
1110 Archive::symbol_iterator I = A->symbol_begin();
1111 Archive::symbol_iterator E = A->symbol_end();
1113 outs() << "Archive map\n";
1114 for (; I != E; ++I) {
1115 Expected<Archive::Child> C = I->getMember();
1117 error(C.takeError(), Filename);
1118 Expected<StringRef> FileNameOrErr = C->getName();
1119 if (!FileNameOrErr) {
1120 error(FileNameOrErr.takeError(), Filename);
1123 StringRef SymName = I->getName();
1124 outs() << SymName << " in " << FileNameOrErr.get() << "\n";
1131 Error Err = Error::success();
1132 for (auto &C : A->children(Err)) {
1133 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary(&Context);
1135 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
1136 error(std::move(E), Filename, C);
1139 if (SymbolicFile *O = dyn_cast<SymbolicFile>(&*ChildOrErr.get())) {
1140 if (!MachOPrintSizeWarning && PrintSize && isa<MachOObjectFile>(O)) {
1141 errs() << ToolName << ": warning sizes with -print-size for Mach-O "
1142 "files are always zero.\n";
1143 MachOPrintSizeWarning = true;
1145 if (!checkMachOAndArchFlags(O, Filename))
1147 if (!PrintFileName) {
1149 if (isa<MachOObjectFile>(O)) {
1150 outs() << Filename << "(" << O->getFileName() << ")";
1152 outs() << O->getFileName();
1155 dumpSymbolNamesFromObject(*O, false, Filename);
1159 error(std::move(Err), A->getFileName());
1163 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
1164 // If we have a list of architecture flags specified dump only those.
1165 if (!ArchAll && ArchFlags.size() != 0) {
1166 // Look for a slice in the universal binary that matches each ArchFlag.
1168 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1170 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1171 E = UB->end_objects();
1173 if (ArchFlags[i] == I->getArchFlagName()) {
1175 Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
1176 I->getAsObjectFile();
1177 std::string ArchiveName;
1178 std::string ArchitectureName;
1179 ArchiveName.clear();
1180 ArchitectureName.clear();
1182 ObjectFile &Obj = *ObjOrErr.get();
1183 if (ArchFlags.size() > 1) {
1185 ArchitectureName = I->getArchFlagName();
1187 outs() << "\n" << Obj.getFileName() << " (for architecture "
1188 << I->getArchFlagName() << ")"
1191 dumpSymbolNamesFromObject(Obj, false, ArchiveName,
1193 } else if (auto E = isNotObjectErrorInvalidFileType(
1194 ObjOrErr.takeError())) {
1195 error(std::move(E), Filename, ArchFlags.size() > 1 ?
1196 StringRef(I->getArchFlagName()) : StringRef());
1198 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
1199 I->getAsArchive()) {
1200 std::unique_ptr<Archive> &A = *AOrErr;
1201 Error Err = Error::success();
1202 for (auto &C : A->children(Err)) {
1203 Expected<std::unique_ptr<Binary>> ChildOrErr =
1204 C.getAsBinary(&Context);
1206 if (auto E = isNotObjectErrorInvalidFileType(
1207 ChildOrErr.takeError())) {
1208 error(std::move(E), Filename, C, ArchFlags.size() > 1 ?
1209 StringRef(I->getArchFlagName()) : StringRef());
1213 if (SymbolicFile *O =
1214 dyn_cast<SymbolicFile>(&*ChildOrErr.get())) {
1215 if (PrintFileName) {
1216 ArchiveName = A->getFileName();
1217 if (ArchFlags.size() > 1)
1218 ArchitectureName = I->getArchFlagName();
1220 outs() << "\n" << A->getFileName();
1221 outs() << "(" << O->getFileName() << ")";
1222 if (ArchFlags.size() > 1) {
1223 outs() << " (for architecture " << I->getArchFlagName()
1228 dumpSymbolNamesFromObject(*O, false, ArchiveName,
1233 error(std::move(Err), A->getFileName());
1235 consumeError(AOrErr.takeError());
1236 error(Filename + " for architecture " +
1237 StringRef(I->getArchFlagName()) +
1238 " is not a Mach-O file or an archive file",
1239 "Mach-O universal file");
1245 "file: " + Filename + " does not contain architecture");
1251 // No architecture flags were specified so if this contains a slice that
1252 // matches the host architecture dump only that.
1254 StringRef HostArchName = MachOObjectFile::getHostArch().getArchName();
1255 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1256 E = UB->end_objects();
1258 if (HostArchName == I->getArchFlagName()) {
1259 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
1260 std::string ArchiveName;
1261 ArchiveName.clear();
1263 ObjectFile &Obj = *ObjOrErr.get();
1264 dumpSymbolNamesFromObject(Obj, false);
1265 } else if (auto E = isNotObjectErrorInvalidFileType(
1266 ObjOrErr.takeError())) {
1267 error(std::move(E), Filename);
1269 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
1270 I->getAsArchive()) {
1271 std::unique_ptr<Archive> &A = *AOrErr;
1272 Error Err = Error::success();
1273 for (auto &C : A->children(Err)) {
1274 Expected<std::unique_ptr<Binary>> ChildOrErr =
1275 C.getAsBinary(&Context);
1277 if (auto E = isNotObjectErrorInvalidFileType(
1278 ChildOrErr.takeError()))
1279 error(std::move(E), Filename, C);
1282 if (SymbolicFile *O =
1283 dyn_cast<SymbolicFile>(&*ChildOrErr.get())) {
1285 ArchiveName = A->getFileName();
1287 outs() << "\n" << A->getFileName() << "(" << O->getFileName()
1290 dumpSymbolNamesFromObject(*O, false, ArchiveName);
1294 error(std::move(Err), A->getFileName());
1296 consumeError(AOrErr.takeError());
1297 error(Filename + " for architecture " +
1298 StringRef(I->getArchFlagName()) +
1299 " is not a Mach-O file or an archive file",
1300 "Mach-O universal file");
1306 // Either all architectures have been specified or none have been specified
1307 // and this does not contain the host architecture so dump all the slices.
1308 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
1309 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1310 E = UB->end_objects();
1312 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
1313 std::string ArchiveName;
1314 std::string ArchitectureName;
1315 ArchiveName.clear();
1316 ArchitectureName.clear();
1318 ObjectFile &Obj = *ObjOrErr.get();
1319 if (PrintFileName) {
1320 if (isa<MachOObjectFile>(Obj) && moreThanOneArch)
1321 ArchitectureName = I->getArchFlagName();
1323 if (moreThanOneArch)
1325 outs() << Obj.getFileName();
1326 if (isa<MachOObjectFile>(Obj) && moreThanOneArch)
1327 outs() << " (for architecture " << I->getArchFlagName() << ")";
1330 dumpSymbolNamesFromObject(Obj, false, ArchiveName, ArchitectureName);
1331 } else if (auto E = isNotObjectErrorInvalidFileType(
1332 ObjOrErr.takeError())) {
1333 error(std::move(E), Filename, moreThanOneArch ?
1334 StringRef(I->getArchFlagName()) : StringRef());
1336 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
1337 I->getAsArchive()) {
1338 std::unique_ptr<Archive> &A = *AOrErr;
1339 Error Err = Error::success();
1340 for (auto &C : A->children(Err)) {
1341 Expected<std::unique_ptr<Binary>> ChildOrErr =
1342 C.getAsBinary(&Context);
1344 if (auto E = isNotObjectErrorInvalidFileType(
1345 ChildOrErr.takeError()))
1346 error(std::move(E), Filename, C, moreThanOneArch ?
1347 StringRef(ArchitectureName) : StringRef());
1350 if (SymbolicFile *O = dyn_cast<SymbolicFile>(&*ChildOrErr.get())) {
1351 if (PrintFileName) {
1352 ArchiveName = A->getFileName();
1353 if (isa<MachOObjectFile>(O) && moreThanOneArch)
1354 ArchitectureName = I->getArchFlagName();
1356 outs() << "\n" << A->getFileName();
1357 if (isa<MachOObjectFile>(O)) {
1358 outs() << "(" << O->getFileName() << ")";
1359 if (moreThanOneArch)
1360 outs() << " (for architecture " << I->getArchFlagName()
1363 outs() << ":" << O->getFileName();
1366 dumpSymbolNamesFromObject(*O, false, ArchiveName, ArchitectureName);
1370 error(std::move(Err), A->getFileName());
1372 consumeError(AOrErr.takeError());
1373 error(Filename + " for architecture " +
1374 StringRef(I->getArchFlagName()) +
1375 " is not a Mach-O file or an archive file",
1376 "Mach-O universal file");
1381 if (SymbolicFile *O = dyn_cast<SymbolicFile>(&Bin)) {
1382 if (!MachOPrintSizeWarning && PrintSize && isa<MachOObjectFile>(O)) {
1383 errs() << ToolName << ": warning sizes with -print-size for Mach-O files "
1384 "are always zero.\n";
1385 MachOPrintSizeWarning = true;
1387 if (!checkMachOAndArchFlags(O, Filename))
1389 dumpSymbolNamesFromObject(*O, true);
1393 int main(int argc, char **argv) {
1394 // Print a stack trace if we signal out.
1395 sys::PrintStackTraceOnErrorSignal(argv[0]);
1396 PrettyStackTraceProgram X(argc, argv);
1398 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
1399 cl::ParseCommandLineOptions(argc, argv, "llvm symbol table dumper\n");
1401 // llvm-nm only reads binary files.
1402 if (error(sys::ChangeStdinToBinary()))
1405 // These calls are needed so that we can read bitcode correctly.
1406 llvm::InitializeAllTargetInfos();
1407 llvm::InitializeAllTargetMCs();
1408 llvm::InitializeAllAsmParsers();
1414 OutputFormat = posix;
1416 OutputFormat = darwin;
1418 // The relative order of these is important. If you pass --size-sort it should
1419 // only print out the size. However, if you pass -S --size-sort, it should
1420 // print out both the size and address.
1421 if (SizeSort && !PrintSize)
1422 PrintAddress = false;
1423 if (OutputFormat == sysv || SizeSort)
1425 if (InputFilenames.empty())
1426 InputFilenames.push_back("a.out");
1427 if (InputFilenames.size() > 1)
1428 MultipleFiles = true;
1430 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1431 if (ArchFlags[i] == "all") {
1434 if (!MachOObjectFile::isValidArch(ArchFlags[i]))
1435 error("Unknown architecture named '" + ArchFlags[i] + "'",
1436 "for the -arch option");
1440 if (SegSect.size() != 0 && SegSect.size() != 2)
1441 error("bad number of arguments (must be two arguments)",
1442 "for the -s option");
1444 std::for_each(InputFilenames.begin(), InputFilenames.end(),
1445 dumpSymbolNamesFromFile);