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/Support/COFF.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/FileSystem.h"
35 #include "llvm/Support/Format.h"
36 #include "llvm/Support/ManagedStatic.h"
37 #include "llvm/Support/MemoryBuffer.h"
38 #include "llvm/Support/PrettyStackTrace.h"
39 #include "llvm/Support/Program.h"
40 #include "llvm/Support/Signals.h"
41 #include "llvm/Support/TargetSelect.h"
42 #include "llvm/Support/raw_ostream.h"
47 #include <system_error>
51 using namespace object;
54 enum OutputFormatTy { bsd, sysv, posix, darwin };
55 cl::opt<OutputFormatTy> OutputFormat(
56 "format", cl::desc("Specify output format"),
57 cl::values(clEnumVal(bsd, "BSD format"), clEnumVal(sysv, "System V format"),
58 clEnumVal(posix, "POSIX.2 format"),
59 clEnumVal(darwin, "Darwin -m format"), clEnumValEnd),
61 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
62 cl::aliasopt(OutputFormat));
64 cl::list<std::string> InputFilenames(cl::Positional, cl::desc("<input files>"),
67 cl::opt<bool> UndefinedOnly("undefined-only",
68 cl::desc("Show only undefined symbols"));
69 cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
70 cl::aliasopt(UndefinedOnly), cl::Grouping);
72 cl::opt<bool> DynamicSyms("dynamic",
73 cl::desc("Display the dynamic symbols instead "
74 "of normal symbols."));
75 cl::alias DynamicSyms2("D", cl::desc("Alias for --dynamic"),
76 cl::aliasopt(DynamicSyms), cl::Grouping);
78 cl::opt<bool> DefinedOnly("defined-only",
79 cl::desc("Show only defined symbols"));
80 cl::alias DefinedOnly2("U", cl::desc("Alias for --defined-only"),
81 cl::aliasopt(DefinedOnly), cl::Grouping);
83 cl::opt<bool> ExternalOnly("extern-only",
84 cl::desc("Show only external symbols"));
85 cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
86 cl::aliasopt(ExternalOnly), cl::Grouping);
88 cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"),
90 cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"),
92 cl::opt<bool> DarwinFormat("m", cl::desc("Alias for --format=darwin"),
95 static cl::list<std::string>
96 ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
100 cl::opt<bool> PrintFileName(
102 cl::desc("Precede each symbol with the object file it came from"));
104 cl::alias PrintFileNameA("A", cl::desc("Alias for --print-file-name"),
105 cl::aliasopt(PrintFileName), cl::Grouping);
106 cl::alias PrintFileNameo("o", cl::desc("Alias for --print-file-name"),
107 cl::aliasopt(PrintFileName), cl::Grouping);
109 cl::opt<bool> DebugSyms("debug-syms",
110 cl::desc("Show all symbols, even debugger only"));
111 cl::alias DebugSymsa("a", cl::desc("Alias for --debug-syms"),
112 cl::aliasopt(DebugSyms), cl::Grouping);
114 cl::opt<bool> NumericSort("numeric-sort", cl::desc("Sort symbols by address"));
115 cl::alias NumericSortn("n", cl::desc("Alias for --numeric-sort"),
116 cl::aliasopt(NumericSort), cl::Grouping);
117 cl::alias NumericSortv("v", cl::desc("Alias for --numeric-sort"),
118 cl::aliasopt(NumericSort), cl::Grouping);
120 cl::opt<bool> NoSort("no-sort", cl::desc("Show symbols in order encountered"));
121 cl::alias NoSortp("p", cl::desc("Alias for --no-sort"), cl::aliasopt(NoSort),
124 cl::opt<bool> ReverseSort("reverse-sort", cl::desc("Sort in reverse order"));
125 cl::alias ReverseSortr("r", cl::desc("Alias for --reverse-sort"),
126 cl::aliasopt(ReverseSort), cl::Grouping);
128 cl::opt<bool> PrintSize("print-size",
129 cl::desc("Show symbol size instead of address"));
130 cl::alias PrintSizeS("S", cl::desc("Alias for --print-size"),
131 cl::aliasopt(PrintSize), cl::Grouping);
133 cl::opt<bool> SizeSort("size-sort", cl::desc("Sort symbols by size"));
135 cl::opt<bool> WithoutAliases("without-aliases", cl::Hidden,
136 cl::desc("Exclude aliases from output"));
138 cl::opt<bool> ArchiveMap("print-armap", cl::desc("Print the archive map"));
139 cl::alias ArchiveMaps("M", cl::desc("Alias for --print-armap"),
140 cl::aliasopt(ArchiveMap), cl::Grouping);
142 enum Radix { d, o, x };
144 AddressRadix("radix", cl::desc("Radix (o/d/x) for printing symbol Values"),
145 cl::values(clEnumVal(d, "decimal"), clEnumVal(o, "octal"),
146 clEnumVal(x, "hexadecimal"), clEnumValEnd),
148 cl::alias RadixAlias("t", cl::desc("Alias for --radix"),
149 cl::aliasopt(AddressRadix));
151 cl::opt<bool> JustSymbolName("just-symbol-name",
152 cl::desc("Print just the symbol's name"));
153 cl::alias JustSymbolNames("j", cl::desc("Alias for --just-symbol-name"),
154 cl::aliasopt(JustSymbolName), cl::Grouping);
156 // FIXME: This option takes exactly two strings and should be allowed anywhere
157 // on the command line. Such that "llvm-nm -s __TEXT __text foo.o" would work.
158 // But that does not as the CommandLine Library does not have a way to make
159 // this work. For now the "-s __TEXT __text" has to be last on the command
161 cl::list<std::string> SegSect("s", cl::Positional, cl::ZeroOrMore,
162 cl::desc("Dump only symbols from this segment "
163 "and section name, Mach-O only"));
165 cl::opt<bool> FormatMachOasHex("x", cl::desc("Print symbol entry in hex, "
166 "Mach-O only"), cl::Grouping);
168 cl::opt<bool> NoLLVMBitcode("no-llvm-bc",
169 cl::desc("Disable LLVM bitcode reader"));
171 bool PrintAddress = true;
173 bool MultipleFiles = false;
175 bool HadError = false;
177 std::string ToolName;
178 } // anonymous namespace
180 static void error(Twine Message, Twine Path = Twine()) {
182 errs() << ToolName << ": " << Path << ": " << Message << ".\n";
185 static bool error(std::error_code EC, Twine Path = Twine()) {
187 error(EC.message(), Path);
193 // This version of error() prints the archive name and member name, for example:
194 // "libx.a(foo.o)" after the ToolName before the error message. It sets
195 // HadError but returns allowing the code to move on to other archive members.
196 static void error(llvm::Error E, StringRef FileName, const Archive::Child &C,
197 StringRef ArchitectureName = StringRef()) {
199 errs() << ToolName << ": " << FileName;
201 ErrorOr<StringRef> NameOrErr = C.getName();
202 // TODO: if we have a error getting the name then it would be nice to print
203 // the index of which archive member this is and or its offset in the
204 // archive instead of "???" as the name.
205 if (NameOrErr.getError())
206 errs() << "(" << "???" << ")";
208 errs() << "(" << NameOrErr.get() << ")";
210 if (!ArchitectureName.empty())
211 errs() << " (for architecture " << ArchitectureName << ") ";
214 raw_string_ostream OS(Buf);
215 logAllUnhandledErrors(std::move(E), OS, "");
217 errs() << " " << Buf << "\n";
220 // This version of error() prints the file name and which architecture slice it
221 // is from, for example: "foo.o (for architecture i386)" after the ToolName
222 // before the error message. It sets HadError but returns allowing the code to
223 // move on to other architecture slices.
224 static void error(llvm::Error E, StringRef FileName,
225 StringRef ArchitectureName = StringRef()) {
227 errs() << ToolName << ": " << FileName;
229 if (!ArchitectureName.empty())
230 errs() << " (for architecture " << ArchitectureName << ") ";
233 raw_string_ostream OS(Buf);
234 logAllUnhandledErrors(std::move(E), OS, "");
236 errs() << " " << Buf << "\n";
247 } // anonymous namespace
249 static bool compareSymbolAddress(const NMSymbol &A, const NMSymbol &B) {
250 bool ADefined = !(A.Sym.getFlags() & SymbolRef::SF_Undefined);
251 bool BDefined = !(B.Sym.getFlags() & SymbolRef::SF_Undefined);
252 return std::make_tuple(ADefined, A.Address, A.Name, A.Size) <
253 std::make_tuple(BDefined, B.Address, B.Name, B.Size);
256 static bool compareSymbolSize(const NMSymbol &A, const NMSymbol &B) {
257 return std::make_tuple(A.Size, A.Name, A.Address) <
258 std::make_tuple(B.Size, B.Name, B.Address);
261 static bool compareSymbolName(const NMSymbol &A, const NMSymbol &B) {
262 return std::make_tuple(A.Name, A.Size, A.Address) <
263 std::make_tuple(B.Name, B.Size, B.Address);
266 static char isSymbolList64Bit(SymbolicFile &Obj) {
267 if (isa<IRObjectFile>(Obj)) {
268 IRObjectFile *IRobj = dyn_cast<IRObjectFile>(&Obj);
269 Module &M = IRobj->getModule();
270 if (M.getTargetTriple().empty())
272 Triple T(M.getTargetTriple());
273 return T.isArch64Bit();
275 if (isa<COFFObjectFile>(Obj))
277 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj))
278 return MachO->is64Bit();
279 return cast<ELFObjectFileBase>(Obj).getBytesInAddress() == 8;
282 static StringRef CurrentFilename;
283 typedef std::vector<NMSymbol> SymbolListT;
284 static SymbolListT SymbolList;
286 static char getSymbolNMTypeChar(IRObjectFile &Obj, basic_symbol_iterator I);
288 // darwinPrintSymbol() is used to print a symbol from a Mach-O file when the
289 // the OutputFormat is darwin or we are printing Mach-O symbols in hex. For
290 // the darwin format it produces the same output as darwin's nm(1) -m output
291 // and when printing Mach-O symbols in hex it produces the same output as
292 // darwin's nm(1) -x format.
293 static void darwinPrintSymbol(SymbolicFile &Obj, SymbolListT::iterator I,
294 char *SymbolAddrStr, const char *printBlanks,
295 const char *printDashes, const char *printFormat) {
296 MachO::mach_header H;
297 MachO::mach_header_64 H_64;
298 uint32_t Filetype = MachO::MH_OBJECT;
305 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj);
307 uint32_t SymFlags = I->Sym.getFlags();
308 if (SymFlags & SymbolRef::SF_Global)
309 NType |= MachO::N_EXT;
310 if (SymFlags & SymbolRef::SF_Hidden)
311 NType |= MachO::N_PEXT;
312 if (SymFlags & SymbolRef::SF_Undefined)
313 NType |= MachO::N_EXT | MachO::N_UNDF;
315 // Here we have a symbol definition. So to fake out a section name we
316 // use 1, 2 and 3 for section numbers. See below where they are used to
317 // print out fake section names.
318 NType |= MachO::N_SECT;
319 if (SymFlags & SymbolRef::SF_Const)
322 IRObjectFile *IRobj = dyn_cast<IRObjectFile>(&Obj);
323 NSect = (getSymbolNMTypeChar(*IRobj, I->Sym) == 't') ? 1 : 2;
326 if (SymFlags & SymbolRef::SF_Weak)
327 NDesc |= MachO::N_WEAK_DEF;
329 DataRefImpl SymDRI = I->Sym.getRawDataRefImpl();
330 if (MachO->is64Bit()) {
331 H_64 = MachO->MachOObjectFile::getHeader64();
332 Filetype = H_64.filetype;
334 MachO::nlist_64 STE_64 = MachO->getSymbol64TableEntry(SymDRI);
335 NType = STE_64.n_type;
336 NSect = STE_64.n_sect;
337 NDesc = STE_64.n_desc;
338 NStrx = STE_64.n_strx;
339 NValue = STE_64.n_value;
341 H = MachO->MachOObjectFile::getHeader();
342 Filetype = H.filetype;
344 MachO::nlist STE = MachO->getSymbolTableEntry(SymDRI);
349 NValue = STE.n_value;
353 // If we are printing Mach-O symbols in hex do that and return.
354 if (FormatMachOasHex) {
356 format(printFormat, NValue).print(Str, sizeof(Str));
357 outs() << Str << ' ';
358 format("%02x", NType).print(Str, sizeof(Str));
359 outs() << Str << ' ';
360 format("%02x", NSect).print(Str, sizeof(Str));
361 outs() << Str << ' ';
362 format("%04x", NDesc).print(Str, sizeof(Str));
363 outs() << Str << ' ';
364 format("%08x", NStrx).print(Str, sizeof(Str));
365 outs() << Str << ' ';
366 outs() << I->Name << "\n";
371 if ((NType & MachO::N_TYPE) == MachO::N_INDR)
372 strcpy(SymbolAddrStr, printBlanks);
373 if (Obj.isIR() && (NType & MachO::N_TYPE) == MachO::N_TYPE)
374 strcpy(SymbolAddrStr, printDashes);
375 outs() << SymbolAddrStr << ' ';
378 switch (NType & MachO::N_TYPE) {
381 outs() << "(common) ";
382 if (MachO::GET_COMM_ALIGN(NDesc) != 0)
383 outs() << "(alignment 2^" << (int)MachO::GET_COMM_ALIGN(NDesc) << ") ";
385 if ((NType & MachO::N_TYPE) == MachO::N_PBUD)
386 outs() << "(prebound ";
389 if ((NDesc & MachO::REFERENCE_TYPE) ==
390 MachO::REFERENCE_FLAG_UNDEFINED_LAZY)
391 outs() << "undefined [lazy bound]) ";
392 else if ((NDesc & MachO::REFERENCE_TYPE) ==
393 MachO::REFERENCE_FLAG_PRIVATE_UNDEFINED_LAZY)
394 outs() << "undefined [private lazy bound]) ";
395 else if ((NDesc & MachO::REFERENCE_TYPE) ==
396 MachO::REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY)
397 outs() << "undefined [private]) ";
399 outs() << "undefined) ";
403 outs() << "(absolute) ";
406 outs() << "(indirect) ";
408 case MachO::N_SECT: {
410 // For llvm bitcode files print out a fake section name using the values
411 // use 1, 2 and 3 for section numbers as set above.
413 outs() << "(LTO,CODE) ";
415 outs() << "(LTO,DATA) ";
417 outs() << "(LTO,RODATA) ";
422 Expected<section_iterator> SecOrErr =
423 MachO->getSymbolSection(I->Sym.getRawDataRefImpl());
425 consumeError(SecOrErr.takeError());
429 section_iterator Sec = *SecOrErr;
430 DataRefImpl Ref = Sec->getRawDataRefImpl();
431 StringRef SectionName;
432 MachO->getSectionName(Ref, SectionName);
433 StringRef SegmentName = MachO->getSectionFinalSegmentName(Ref);
434 outs() << "(" << SegmentName << "," << SectionName << ") ";
442 if (NType & MachO::N_EXT) {
443 if (NDesc & MachO::REFERENCED_DYNAMICALLY)
444 outs() << "[referenced dynamically] ";
445 if (NType & MachO::N_PEXT) {
446 if ((NDesc & MachO::N_WEAK_DEF) == MachO::N_WEAK_DEF)
447 outs() << "weak private external ";
449 outs() << "private external ";
451 if ((NDesc & MachO::N_WEAK_REF) == MachO::N_WEAK_REF ||
452 (NDesc & MachO::N_WEAK_DEF) == MachO::N_WEAK_DEF) {
453 if ((NDesc & (MachO::N_WEAK_REF | MachO::N_WEAK_DEF)) ==
454 (MachO::N_WEAK_REF | MachO::N_WEAK_DEF))
455 outs() << "weak external automatically hidden ";
457 outs() << "weak external ";
459 outs() << "external ";
462 if (NType & MachO::N_PEXT)
463 outs() << "non-external (was a private external) ";
465 outs() << "non-external ";
468 if (Filetype == MachO::MH_OBJECT &&
469 (NDesc & MachO::N_NO_DEAD_STRIP) == MachO::N_NO_DEAD_STRIP)
470 outs() << "[no dead strip] ";
472 if (Filetype == MachO::MH_OBJECT &&
473 ((NType & MachO::N_TYPE) != MachO::N_UNDF) &&
474 (NDesc & MachO::N_SYMBOL_RESOLVER) == MachO::N_SYMBOL_RESOLVER)
475 outs() << "[symbol resolver] ";
477 if (Filetype == MachO::MH_OBJECT &&
478 ((NType & MachO::N_TYPE) != MachO::N_UNDF) &&
479 (NDesc & MachO::N_ALT_ENTRY) == MachO::N_ALT_ENTRY)
480 outs() << "[alt entry] ";
482 if ((NDesc & MachO::N_ARM_THUMB_DEF) == MachO::N_ARM_THUMB_DEF)
483 outs() << "[Thumb] ";
485 if ((NType & MachO::N_TYPE) == MachO::N_INDR) {
486 outs() << I->Name << " (for ";
487 StringRef IndirectName;
489 MachO->getIndirectName(I->Sym.getRawDataRefImpl(), IndirectName))
492 outs() << IndirectName << ")";
496 if ((Flags & MachO::MH_TWOLEVEL) == MachO::MH_TWOLEVEL &&
497 (((NType & MachO::N_TYPE) == MachO::N_UNDF && NValue == 0) ||
498 (NType & MachO::N_TYPE) == MachO::N_PBUD)) {
499 uint32_t LibraryOrdinal = MachO::GET_LIBRARY_ORDINAL(NDesc);
500 if (LibraryOrdinal != 0) {
501 if (LibraryOrdinal == MachO::EXECUTABLE_ORDINAL)
502 outs() << " (from executable)";
503 else if (LibraryOrdinal == MachO::DYNAMIC_LOOKUP_ORDINAL)
504 outs() << " (dynamically looked up)";
506 StringRef LibraryName;
508 MachO->getLibraryShortNameByIndex(LibraryOrdinal - 1, LibraryName))
509 outs() << " (from bad library ordinal " << LibraryOrdinal << ")";
511 outs() << " (from " << LibraryName << ")";
519 // Table that maps Darwin's Mach-O stab constants to strings to allow printing.
520 struct DarwinStabName {
524 static const struct DarwinStabName DarwinStabNames[] = {
525 {MachO::N_GSYM, "GSYM"},
526 {MachO::N_FNAME, "FNAME"},
527 {MachO::N_FUN, "FUN"},
528 {MachO::N_STSYM, "STSYM"},
529 {MachO::N_LCSYM, "LCSYM"},
530 {MachO::N_BNSYM, "BNSYM"},
532 {MachO::N_AST, "AST"},
533 {MachO::N_OPT, "OPT"},
534 {MachO::N_RSYM, "RSYM"},
535 {MachO::N_SLINE, "SLINE"},
536 {MachO::N_ENSYM, "ENSYM"},
537 {MachO::N_SSYM, "SSYM"},
539 {MachO::N_OSO, "OSO"},
540 {MachO::N_LSYM, "LSYM"},
541 {MachO::N_BINCL, "BINCL"},
542 {MachO::N_SOL, "SOL"},
543 {MachO::N_PARAMS, "PARAM"},
544 {MachO::N_VERSION, "VERS"},
545 {MachO::N_OLEVEL, "OLEV"},
546 {MachO::N_PSYM, "PSYM"},
547 {MachO::N_EINCL, "EINCL"},
548 {MachO::N_ENTRY, "ENTRY"},
549 {MachO::N_LBRAC, "LBRAC"},
550 {MachO::N_EXCL, "EXCL"},
551 {MachO::N_RBRAC, "RBRAC"},
552 {MachO::N_BCOMM, "BCOMM"},
553 {MachO::N_ECOMM, "ECOMM"},
554 {MachO::N_ECOML, "ECOML"},
555 {MachO::N_LENG, "LENG"},
558 static const char *getDarwinStabString(uint8_t NType) {
559 for (unsigned i = 0; DarwinStabNames[i].Name; i++) {
560 if (DarwinStabNames[i].NType == NType)
561 return DarwinStabNames[i].Name;
566 // darwinPrintStab() prints the n_sect, n_desc along with a symbolic name of
567 // a stab n_type value in a Mach-O file.
568 static void darwinPrintStab(MachOObjectFile *MachO, SymbolListT::iterator I) {
569 MachO::nlist_64 STE_64;
574 DataRefImpl SymDRI = I->Sym.getRawDataRefImpl();
575 if (MachO->is64Bit()) {
576 STE_64 = MachO->getSymbol64TableEntry(SymDRI);
577 NType = STE_64.n_type;
578 NSect = STE_64.n_sect;
579 NDesc = STE_64.n_desc;
581 STE = MachO->getSymbolTableEntry(SymDRI);
588 format("%02x", NSect).print(Str, sizeof(Str));
589 outs() << ' ' << Str << ' ';
590 format("%04x", NDesc).print(Str, sizeof(Str));
591 outs() << Str << ' ';
592 if (const char *stabString = getDarwinStabString(NType))
593 format("%5.5s", stabString).print(Str, sizeof(Str));
595 format(" %02x", NType).print(Str, sizeof(Str));
599 static void sortAndPrintSymbolList(SymbolicFile &Obj, bool printName,
600 const std::string &ArchiveName,
601 const std::string &ArchitectureName) {
603 std::function<bool(const NMSymbol &, const NMSymbol &)> Cmp;
605 Cmp = compareSymbolAddress;
607 Cmp = compareSymbolSize;
609 Cmp = compareSymbolName;
612 Cmp = [=](const NMSymbol &A, const NMSymbol &B) { return Cmp(B, A); };
613 std::sort(SymbolList.begin(), SymbolList.end(), Cmp);
616 if (!PrintFileName) {
617 if (OutputFormat == posix && MultipleFiles && printName) {
618 outs() << '\n' << CurrentFilename << ":\n";
619 } else if (OutputFormat == bsd && MultipleFiles && printName) {
620 outs() << "\n" << CurrentFilename << ":\n";
621 } else if (OutputFormat == sysv) {
622 outs() << "\n\nSymbols from " << CurrentFilename << ":\n\n"
623 << "Name Value Class Type"
624 << " Size Line Section\n";
628 const char *printBlanks, *printDashes, *printFormat;
629 if (isSymbolList64Bit(Obj)) {
631 printDashes = "----------------";
632 switch (AddressRadix) {
634 printFormat = OutputFormat == posix ? "%" PRIo64 : "%016" PRIo64;
637 printFormat = OutputFormat == posix ? "%" PRIx64 : "%016" PRIx64;
640 printFormat = OutputFormat == posix ? "%" PRId64 : "%016" PRId64;
644 printDashes = "--------";
645 switch (AddressRadix) {
647 printFormat = OutputFormat == posix ? "%" PRIo64 : "%08" PRIo64;
650 printFormat = OutputFormat == posix ? "%" PRIx64 : "%08" PRIx64;
653 printFormat = OutputFormat == posix ? "%" PRId64 : "%08" PRId64;
657 for (SymbolListT::iterator I = SymbolList.begin(), E = SymbolList.end();
659 uint32_t SymFlags = I->Sym.getFlags();
660 bool Undefined = SymFlags & SymbolRef::SF_Undefined;
661 bool Global = SymFlags & SymbolRef::SF_Global;
662 if ((!Undefined && UndefinedOnly) || (Undefined && DefinedOnly) ||
663 (!Global && ExternalOnly) || (SizeSort && !PrintAddress))
666 if (!ArchitectureName.empty())
667 outs() << "(for architecture " << ArchitectureName << "):";
668 if (OutputFormat == posix && !ArchiveName.empty())
669 outs() << ArchiveName << "[" << CurrentFilename << "]: ";
671 if (!ArchiveName.empty())
672 outs() << ArchiveName << ":";
673 outs() << CurrentFilename << ": ";
676 if ((JustSymbolName || (UndefinedOnly && isa<MachOObjectFile>(Obj) &&
677 OutputFormat != darwin)) && OutputFormat != posix) {
678 outs() << I->Name << "\n";
682 char SymbolAddrStr[18] = "";
683 char SymbolSizeStr[18] = "";
685 if (OutputFormat == sysv || I->TypeChar == 'U') {
686 if (OutputFormat == posix)
687 format(printFormat, I->Address)
688 .print(SymbolAddrStr, sizeof(SymbolAddrStr));
690 strcpy(SymbolAddrStr, printBlanks);
692 if (OutputFormat == sysv)
693 strcpy(SymbolSizeStr, printBlanks);
695 if (I->TypeChar != 'U') {
697 strcpy(SymbolAddrStr, printDashes);
699 format(printFormat, I->Address)
700 .print(SymbolAddrStr, sizeof(SymbolAddrStr));
702 format(printFormat, I->Size).print(SymbolSizeStr, sizeof(SymbolSizeStr));
704 // If OutputFormat is darwin or we are printing Mach-O symbols in hex and
705 // we have a MachOObjectFile, call darwinPrintSymbol to print as darwin's
706 // nm(1) -m output or hex, else if OutputFormat is darwin or we are
707 // printing Mach-O symbols in hex and not a Mach-O object fall back to
708 // OutputFormat bsd (see below).
709 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj);
710 if ((OutputFormat == darwin || FormatMachOasHex) && (MachO || Obj.isIR())) {
711 darwinPrintSymbol(Obj, I, SymbolAddrStr, printBlanks, printDashes,
713 } else if (OutputFormat == posix) {
714 outs() << I->Name << " " << I->TypeChar << " ";
716 outs() << SymbolAddrStr << " " << "0" /* SymbolSizeStr */ << "\n";
718 outs() << SymbolAddrStr << " " << SymbolSizeStr << "\n";
719 } else if (OutputFormat == bsd || (OutputFormat == darwin && !MachO)) {
721 outs() << SymbolAddrStr << ' ';
723 outs() << SymbolSizeStr;
726 outs() << I->TypeChar;
727 if (I->TypeChar == '-' && MachO)
728 darwinPrintStab(MachO, I);
729 outs() << " " << I->Name << "\n";
730 } else if (OutputFormat == sysv) {
731 std::string PaddedName(I->Name);
732 while (PaddedName.length() < 20)
734 outs() << PaddedName << "|" << SymbolAddrStr << "| " << I->TypeChar
735 << " | |" << SymbolSizeStr << "| |\n";
742 static char getSymbolNMTypeChar(ELFObjectFileBase &Obj,
743 basic_symbol_iterator I) {
745 elf_symbol_iterator SymI(I);
747 Expected<elf_section_iterator> SecIOrErr = SymI->getSection();
749 consumeError(SecIOrErr.takeError());
753 elf_section_iterator SecI = *SecIOrErr;
754 if (SecI != Obj.section_end()) {
755 switch (SecI->getType()) {
756 case ELF::SHT_PROGBITS:
757 case ELF::SHT_DYNAMIC:
758 switch (SecI->getFlags()) {
759 case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR):
761 case (ELF::SHF_TLS | ELF::SHF_ALLOC | ELF::SHF_WRITE):
762 case (ELF::SHF_ALLOC | ELF::SHF_WRITE):
765 case (ELF::SHF_ALLOC | ELF::SHF_MERGE):
766 case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS):
770 case ELF::SHT_NOBITS:
775 if (SymI->getELFType() == ELF::STT_SECTION) {
776 Expected<StringRef> Name = SymI->getName();
778 consumeError(Name.takeError());
781 return StringSwitch<char>(*Name)
782 .StartsWith(".debug", 'N')
783 .StartsWith(".note", 'n')
790 static char getSymbolNMTypeChar(COFFObjectFile &Obj, symbol_iterator I) {
791 COFFSymbolRef Symb = Obj.getCOFFSymbol(*I);
793 symbol_iterator SymI(I);
795 Expected<StringRef> Name = SymI->getName();
797 consumeError(Name.takeError());
801 char Ret = StringSwitch<char>(*Name)
802 .StartsWith(".debug", 'N')
803 .StartsWith(".sxdata", 'N')
809 uint32_t Characteristics = 0;
810 if (!COFF::isReservedSectionNumber(Symb.getSectionNumber())) {
811 Expected<section_iterator> SecIOrErr = SymI->getSection();
813 consumeError(SecIOrErr.takeError());
816 section_iterator SecI = *SecIOrErr;
817 const coff_section *Section = Obj.getCOFFSection(*SecI);
818 Characteristics = Section->Characteristics;
821 switch (Symb.getSectionNumber()) {
822 case COFF::IMAGE_SYM_DEBUG:
825 // Check section type.
826 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
828 if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
829 return Characteristics & COFF::IMAGE_SCN_MEM_WRITE ? 'd' : 'r';
830 if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
832 if (Characteristics & COFF::IMAGE_SCN_LNK_INFO)
834 // Check for section symbol.
835 if (Symb.isSectionDefinition())
842 static char getSymbolNMTypeChar(MachOObjectFile &Obj, basic_symbol_iterator I) {
843 DataRefImpl Symb = I->getRawDataRefImpl();
844 uint8_t NType = Obj.is64Bit() ? Obj.getSymbol64TableEntry(Symb).n_type
845 : Obj.getSymbolTableEntry(Symb).n_type;
847 if (NType & MachO::N_STAB)
850 switch (NType & MachO::N_TYPE) {
855 case MachO::N_SECT: {
856 Expected<section_iterator> SecOrErr = Obj.getSymbolSection(Symb);
858 consumeError(SecOrErr.takeError());
861 section_iterator Sec = *SecOrErr;
862 DataRefImpl Ref = Sec->getRawDataRefImpl();
863 StringRef SectionName;
864 Obj.getSectionName(Ref, SectionName);
865 StringRef SegmentName = Obj.getSectionFinalSegmentName(Ref);
866 if (SegmentName == "__TEXT" && SectionName == "__text")
868 if (SegmentName == "__DATA" && SectionName == "__data")
870 if (SegmentName == "__DATA" && SectionName == "__bss")
879 static char getSymbolNMTypeChar(const GlobalValue &GV) {
880 // FIXME: should we print 'b'? At the IR level we cannot be sure if this
881 // will be in bss or not, but we could approximate.
882 return GV.getValueType()->isFunctionTy() ? 't' : 'd';
885 static char getSymbolNMTypeChar(IRObjectFile &Obj, basic_symbol_iterator I) {
886 const GlobalValue *GV = Obj.getSymbolGV(I->getRawDataRefImpl());
887 return !GV ? 't' : getSymbolNMTypeChar(*GV);
890 static bool isObject(SymbolicFile &Obj, basic_symbol_iterator I) {
891 return !dyn_cast<ELFObjectFileBase>(&Obj)
893 : elf_symbol_iterator(I)->getELFType() == ELF::STT_OBJECT;
896 static char getNMTypeChar(SymbolicFile &Obj, basic_symbol_iterator I) {
897 uint32_t Symflags = I->getFlags();
898 if ((Symflags & object::SymbolRef::SF_Weak) && !isa<MachOObjectFile>(Obj)) {
899 char Ret = isObject(Obj, I) ? 'v' : 'w';
900 return (!(Symflags & object::SymbolRef::SF_Undefined)) ? toupper(Ret) : Ret;
903 if (Symflags & object::SymbolRef::SF_Undefined)
906 if (Symflags & object::SymbolRef::SF_Common)
910 if (Symflags & object::SymbolRef::SF_Absolute)
912 else if (IRObjectFile *IR = dyn_cast<IRObjectFile>(&Obj)) {
913 Ret = getSymbolNMTypeChar(*IR, I);
914 Triple Host(sys::getDefaultTargetTriple());
915 if (Ret == 'd' && Host.isOSDarwin() && Symflags & SymbolRef::SF_Const)
918 else if (COFFObjectFile *COFF = dyn_cast<COFFObjectFile>(&Obj))
919 Ret = getSymbolNMTypeChar(*COFF, I);
920 else if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj))
921 Ret = getSymbolNMTypeChar(*MachO, I);
923 Ret = getSymbolNMTypeChar(cast<ELFObjectFileBase>(Obj), I);
925 if (Symflags & object::SymbolRef::SF_Global)
931 // getNsectForSegSect() is used to implement the Mach-O "-s segname sectname"
932 // option to dump only those symbols from that section in a Mach-O file.
933 // It is called once for each Mach-O file from dumpSymbolNamesFromObject()
934 // to get the section number for that named section from the command line
935 // arguments. It returns the section number for that section in the Mach-O
936 // file or zero it is not present.
937 static unsigned getNsectForSegSect(MachOObjectFile *Obj) {
939 for (auto &S : Obj->sections()) {
940 DataRefImpl Ref = S.getRawDataRefImpl();
941 StringRef SectionName;
942 Obj->getSectionName(Ref, SectionName);
943 StringRef SegmentName = Obj->getSectionFinalSegmentName(Ref);
944 if (SegmentName == SegSect[0] && SectionName == SegSect[1])
951 // getNsectInMachO() is used to implement the Mach-O "-s segname sectname"
952 // option to dump only those symbols from that section in a Mach-O file.
953 // It is called once for each symbol in a Mach-O file from
954 // dumpSymbolNamesFromObject() and returns the section number for that symbol
955 // if it is in a section, else it returns 0.
956 static unsigned getNsectInMachO(MachOObjectFile &Obj, BasicSymbolRef Sym) {
957 DataRefImpl Symb = Sym.getRawDataRefImpl();
959 MachO::nlist_64 STE = Obj.getSymbol64TableEntry(Symb);
960 return (STE.n_type & MachO::N_TYPE) == MachO::N_SECT ? STE.n_sect : 0;
962 MachO::nlist STE = Obj.getSymbolTableEntry(Symb);
963 return (STE.n_type & MachO::N_TYPE) == MachO::N_SECT ? STE.n_sect : 0;
967 dumpSymbolNamesFromObject(SymbolicFile &Obj, bool printName,
968 const std::string &ArchiveName = std::string(),
969 const std::string &ArchitectureName = std::string()) {
970 auto Symbols = Obj.symbols();
972 const auto *E = dyn_cast<ELFObjectFileBase>(&Obj);
974 error("File format has no dynamic symbol table", Obj.getFileName());
977 auto DynSymbols = E->getDynamicSymbolIterators();
979 make_range<basic_symbol_iterator>(DynSymbols.begin(), DynSymbols.end());
981 std::string NameBuffer;
982 raw_string_ostream OS(NameBuffer);
983 // If a "-s segname sectname" option was specified and this is a Mach-O
984 // file get the section number for that section in this object file.
985 unsigned int Nsect = 0;
986 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj);
987 if (SegSect.size() != 0 && MachO) {
988 Nsect = getNsectForSegSect(MachO);
989 // If this section is not in the object file no symbols are printed.
993 for (BasicSymbolRef Sym : Symbols) {
994 uint32_t SymFlags = Sym.getFlags();
995 if (!DebugSyms && (SymFlags & SymbolRef::SF_FormatSpecific))
997 if (WithoutAliases) {
998 if (IRObjectFile *IR = dyn_cast<IRObjectFile>(&Obj)) {
999 const GlobalValue *GV = IR->getSymbolGV(Sym.getRawDataRefImpl());
1000 if (GV && isa<GlobalAlias>(GV))
1004 // If a "-s segname sectname" option was specified and this is a Mach-O
1005 // file and this section appears in this file, Nsect will be non-zero then
1006 // see if this symbol is a symbol from that section and if not skip it.
1007 if (Nsect && Nsect != getNsectInMachO(*MachO, Sym))
1013 if (isa<ELFObjectFileBase>(&Obj))
1014 S.Size = ELFSymbolRef(Sym).getSize();
1016 if (PrintAddress && isa<ObjectFile>(Obj)) {
1017 SymbolRef SymRef(Sym);
1018 Expected<uint64_t> AddressOrErr = SymRef.getAddress();
1019 if (!AddressOrErr) {
1020 consumeError(AddressOrErr.takeError());
1023 S.Address = *AddressOrErr;
1025 S.TypeChar = getNMTypeChar(Obj, Sym);
1026 std::error_code EC = Sym.printName(OS);
1028 OS << "bad string index";
1033 SymbolList.push_back(S);
1037 const char *P = NameBuffer.c_str();
1038 for (unsigned I = 0; I < SymbolList.size(); ++I) {
1039 SymbolList[I].Name = P;
1043 CurrentFilename = Obj.getFileName();
1044 sortAndPrintSymbolList(Obj, printName, ArchiveName, ArchitectureName);
1047 // checkMachOAndArchFlags() checks to see if the SymbolicFile is a Mach-O file
1048 // and if it is and there is a list of architecture flags is specified then
1049 // check to make sure this Mach-O file is one of those architectures or all
1050 // architectures was specificed. If not then an error is generated and this
1051 // routine returns false. Else it returns true.
1052 static bool checkMachOAndArchFlags(SymbolicFile *O, std::string &Filename) {
1053 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(O);
1055 if (!MachO || ArchAll || ArchFlags.size() == 0)
1058 MachO::mach_header H;
1059 MachO::mach_header_64 H_64;
1061 if (MachO->is64Bit()) {
1062 H_64 = MachO->MachOObjectFile::getHeader64();
1063 T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype);
1065 H = MachO->MachOObjectFile::getHeader();
1066 T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype);
1069 ArchFlags.begin(), ArchFlags.end(),
1070 [&](const std::string &Name) { return Name == T.getArchName(); })) {
1071 error("No architecture specified", Filename);
1077 static void dumpSymbolNamesFromFile(std::string &Filename) {
1078 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
1079 MemoryBuffer::getFileOrSTDIN(Filename);
1080 if (error(BufferOrErr.getError(), Filename))
1083 LLVMContext Context;
1084 Expected<std::unique_ptr<Binary>> BinaryOrErr = createBinary(
1085 BufferOrErr.get()->getMemBufferRef(), NoLLVMBitcode ? nullptr : &Context);
1087 error(errorToErrorCode(BinaryOrErr.takeError()), Filename);
1090 Binary &Bin = *BinaryOrErr.get();
1092 if (Archive *A = dyn_cast<Archive>(&Bin)) {
1094 Archive::symbol_iterator I = A->symbol_begin();
1095 Archive::symbol_iterator E = A->symbol_end();
1097 outs() << "Archive map\n";
1098 for (; I != E; ++I) {
1099 ErrorOr<Archive::Child> C = I->getMember();
1100 if (error(C.getError()))
1102 ErrorOr<StringRef> FileNameOrErr = C->getName();
1103 if (error(FileNameOrErr.getError()))
1105 StringRef SymName = I->getName();
1106 outs() << SymName << " in " << FileNameOrErr.get() << "\n";
1114 for (auto &C : A->children(Err)) {
1115 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary(&Context);
1117 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
1118 error(std::move(E), Filename, C);
1121 if (SymbolicFile *O = dyn_cast<SymbolicFile>(&*ChildOrErr.get())) {
1122 if (!checkMachOAndArchFlags(O, Filename))
1124 if (!PrintFileName) {
1126 if (isa<MachOObjectFile>(O)) {
1127 outs() << Filename << "(" << O->getFileName() << ")";
1129 outs() << O->getFileName();
1132 dumpSymbolNamesFromObject(*O, false, Filename);
1136 error(std::move(Err), A->getFileName());
1140 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
1141 // If we have a list of architecture flags specified dump only those.
1142 if (!ArchAll && ArchFlags.size() != 0) {
1143 // Look for a slice in the universal binary that matches each ArchFlag.
1145 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1147 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1148 E = UB->end_objects();
1150 if (ArchFlags[i] == I->getArchTypeName()) {
1152 Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
1153 I->getAsObjectFile();
1154 std::string ArchiveName;
1155 std::string ArchitectureName;
1156 ArchiveName.clear();
1157 ArchitectureName.clear();
1159 ObjectFile &Obj = *ObjOrErr.get();
1160 if (ArchFlags.size() > 1) {
1162 ArchitectureName = I->getArchTypeName();
1164 outs() << "\n" << Obj.getFileName() << " (for architecture "
1165 << I->getArchTypeName() << ")"
1168 dumpSymbolNamesFromObject(Obj, false, ArchiveName,
1170 } else if (auto E = isNotObjectErrorInvalidFileType(
1171 ObjOrErr.takeError())) {
1172 error(std::move(E), Filename, ArchFlags.size() > 1 ?
1173 StringRef(I->getArchTypeName()) : StringRef());
1175 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
1176 I->getAsArchive()) {
1177 std::unique_ptr<Archive> &A = *AOrErr;
1179 for (auto &C : A->children(Err)) {
1180 Expected<std::unique_ptr<Binary>> ChildOrErr =
1181 C.getAsBinary(&Context);
1183 if (auto E = isNotObjectErrorInvalidFileType(
1184 ChildOrErr.takeError())) {
1185 error(std::move(E), Filename, C, ArchFlags.size() > 1 ?
1186 StringRef(I->getArchTypeName()) : StringRef());
1190 if (SymbolicFile *O =
1191 dyn_cast<SymbolicFile>(&*ChildOrErr.get())) {
1192 if (PrintFileName) {
1193 ArchiveName = A->getFileName();
1194 if (ArchFlags.size() > 1)
1195 ArchitectureName = I->getArchTypeName();
1197 outs() << "\n" << A->getFileName();
1198 outs() << "(" << O->getFileName() << ")";
1199 if (ArchFlags.size() > 1) {
1200 outs() << " (for architecture " << I->getArchTypeName()
1205 dumpSymbolNamesFromObject(*O, false, ArchiveName,
1210 error(std::move(Err), A->getFileName());
1212 consumeError(AOrErr.takeError());
1213 error(Filename + " for architecture " +
1214 StringRef(I->getArchTypeName()) +
1215 " is not a Mach-O file or an archive file",
1216 "Mach-O universal file");
1222 "file: " + Filename + " does not contain architecture");
1228 // No architecture flags were specified so if this contains a slice that
1229 // matches the host architecture dump only that.
1231 StringRef HostArchName = MachOObjectFile::getHostArch().getArchName();
1232 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1233 E = UB->end_objects();
1235 if (HostArchName == I->getArchTypeName()) {
1236 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
1237 std::string ArchiveName;
1238 ArchiveName.clear();
1240 ObjectFile &Obj = *ObjOrErr.get();
1241 dumpSymbolNamesFromObject(Obj, false);
1242 } else if (auto E = isNotObjectErrorInvalidFileType(
1243 ObjOrErr.takeError())) {
1244 error(std::move(E), Filename);
1246 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
1247 I->getAsArchive()) {
1248 std::unique_ptr<Archive> &A = *AOrErr;
1250 for (auto &C : A->children(Err)) {
1251 Expected<std::unique_ptr<Binary>> ChildOrErr =
1252 C.getAsBinary(&Context);
1254 if (auto E = isNotObjectErrorInvalidFileType(
1255 ChildOrErr.takeError()))
1256 error(std::move(E), Filename, C);
1259 if (SymbolicFile *O =
1260 dyn_cast<SymbolicFile>(&*ChildOrErr.get())) {
1262 ArchiveName = A->getFileName();
1264 outs() << "\n" << A->getFileName() << "(" << O->getFileName()
1267 dumpSymbolNamesFromObject(*O, false, ArchiveName);
1271 error(std::move(Err), A->getFileName());
1273 consumeError(AOrErr.takeError());
1274 error(Filename + " for architecture " +
1275 StringRef(I->getArchTypeName()) +
1276 " is not a Mach-O file or an archive file",
1277 "Mach-O universal file");
1283 // Either all architectures have been specified or none have been specified
1284 // and this does not contain the host architecture so dump all the slices.
1285 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
1286 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1287 E = UB->end_objects();
1289 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
1290 std::string ArchiveName;
1291 std::string ArchitectureName;
1292 ArchiveName.clear();
1293 ArchitectureName.clear();
1295 ObjectFile &Obj = *ObjOrErr.get();
1296 if (PrintFileName) {
1297 if (isa<MachOObjectFile>(Obj) && moreThanOneArch)
1298 ArchitectureName = I->getArchTypeName();
1300 if (moreThanOneArch)
1302 outs() << Obj.getFileName();
1303 if (isa<MachOObjectFile>(Obj) && moreThanOneArch)
1304 outs() << " (for architecture " << I->getArchTypeName() << ")";
1307 dumpSymbolNamesFromObject(Obj, false, ArchiveName, ArchitectureName);
1308 } else if (auto E = isNotObjectErrorInvalidFileType(
1309 ObjOrErr.takeError())) {
1310 error(std::move(E), Filename, moreThanOneArch ?
1311 StringRef(I->getArchTypeName()) : StringRef());
1313 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
1314 I->getAsArchive()) {
1315 std::unique_ptr<Archive> &A = *AOrErr;
1317 for (auto &C : A->children(Err)) {
1318 Expected<std::unique_ptr<Binary>> ChildOrErr =
1319 C.getAsBinary(&Context);
1321 if (auto E = isNotObjectErrorInvalidFileType(
1322 ChildOrErr.takeError()))
1323 error(std::move(E), Filename, C, moreThanOneArch ?
1324 StringRef(ArchitectureName) : StringRef());
1327 if (SymbolicFile *O = dyn_cast<SymbolicFile>(&*ChildOrErr.get())) {
1328 if (PrintFileName) {
1329 ArchiveName = A->getFileName();
1330 if (isa<MachOObjectFile>(O) && moreThanOneArch)
1331 ArchitectureName = I->getArchTypeName();
1333 outs() << "\n" << A->getFileName();
1334 if (isa<MachOObjectFile>(O)) {
1335 outs() << "(" << O->getFileName() << ")";
1336 if (moreThanOneArch)
1337 outs() << " (for architecture " << I->getArchTypeName()
1340 outs() << ":" << O->getFileName();
1343 dumpSymbolNamesFromObject(*O, false, ArchiveName, ArchitectureName);
1347 error(std::move(Err), A->getFileName());
1349 consumeError(AOrErr.takeError());
1350 error(Filename + " for architecture " +
1351 StringRef(I->getArchTypeName()) +
1352 " is not a Mach-O file or an archive file",
1353 "Mach-O universal file");
1358 if (SymbolicFile *O = dyn_cast<SymbolicFile>(&Bin)) {
1359 if (!checkMachOAndArchFlags(O, Filename))
1361 dumpSymbolNamesFromObject(*O, true);
1365 int main(int argc, char **argv) {
1366 // Print a stack trace if we signal out.
1367 sys::PrintStackTraceOnErrorSignal(argv[0]);
1368 PrettyStackTraceProgram X(argc, argv);
1370 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
1371 cl::ParseCommandLineOptions(argc, argv, "llvm symbol table dumper\n");
1373 // llvm-nm only reads binary files.
1374 if (error(sys::ChangeStdinToBinary()))
1377 // These calls are needed so that we can read bitcode correctly.
1378 llvm::InitializeAllTargetInfos();
1379 llvm::InitializeAllTargetMCs();
1380 llvm::InitializeAllAsmParsers();
1386 OutputFormat = posix;
1388 OutputFormat = darwin;
1390 // The relative order of these is important. If you pass --size-sort it should
1391 // only print out the size. However, if you pass -S --size-sort, it should
1392 // print out both the size and address.
1393 if (SizeSort && !PrintSize)
1394 PrintAddress = false;
1395 if (OutputFormat == sysv || SizeSort)
1397 if (InputFilenames.empty())
1398 InputFilenames.push_back("a.out");
1399 if (InputFilenames.size() > 1)
1400 MultipleFiles = true;
1402 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1403 if (ArchFlags[i] == "all") {
1406 if (!MachOObjectFile::isValidArch(ArchFlags[i]))
1407 error("Unknown architecture named '" + ArchFlags[i] + "'",
1408 "for the -arch option");
1412 if (SegSect.size() != 0 && SegSect.size() != 2)
1413 error("bad number of arguments (must be two arguments)",
1414 "for the -s option");
1416 std::for_each(InputFilenames.begin(), InputFilenames.end(),
1417 dumpSymbolNamesFromFile);