Simplify isSymbolList64Bit. NFC.
[oota-llvm.git] / tools / llvm-nm / llvm-nm.cpp
1 //===-- llvm-nm.cpp - Symbol table dumping utility for llvm ---------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
13 //
14 // This "nm" supports many of the features of GNU "nm", including its different
15 // output formats.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/GlobalAlias.h"
21 #include "llvm/IR/GlobalVariable.h"
22 #include "llvm/IR/LLVMContext.h"
23 #include "llvm/Object/Archive.h"
24 #include "llvm/Object/COFF.h"
25 #include "llvm/Object/ELFObjectFile.h"
26 #include "llvm/Object/IRObjectFile.h"
27 #include "llvm/Object/MachO.h"
28 #include "llvm/Object/MachOUniversal.h"
29 #include "llvm/Object/ObjectFile.h"
30 #include "llvm/Support/COFF.h"
31 #include "llvm/Support/CommandLine.h"
32 #include "llvm/Support/FileSystem.h"
33 #include "llvm/Support/Format.h"
34 #include "llvm/Support/ManagedStatic.h"
35 #include "llvm/Support/MemoryBuffer.h"
36 #include "llvm/Support/PrettyStackTrace.h"
37 #include "llvm/Support/Program.h"
38 #include "llvm/Support/Signals.h"
39 #include "llvm/Support/TargetSelect.h"
40 #include "llvm/Support/raw_ostream.h"
41 #include <algorithm>
42 #include <cctype>
43 #include <cerrno>
44 #include <cstring>
45 #include <system_error>
46 #include <vector>
47 using namespace llvm;
48 using namespace object;
49
50 namespace {
51 enum OutputFormatTy { bsd, sysv, posix, darwin };
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"), clEnumValEnd),
57     cl::init(bsd));
58 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
59                         cl::aliasopt(OutputFormat));
60
61 cl::list<std::string> InputFilenames(cl::Positional, cl::desc("<input files>"),
62                                      cl::ZeroOrMore);
63
64 cl::opt<bool> UndefinedOnly("undefined-only",
65                             cl::desc("Show only undefined symbols"));
66 cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
67                          cl::aliasopt(UndefinedOnly));
68
69 cl::opt<bool> DynamicSyms("dynamic",
70                           cl::desc("Display the dynamic symbols instead "
71                                    "of normal symbols."));
72 cl::alias DynamicSyms2("D", cl::desc("Alias for --dynamic"),
73                        cl::aliasopt(DynamicSyms));
74
75 cl::opt<bool> DefinedOnly("defined-only",
76                           cl::desc("Show only defined symbols"));
77 cl::alias DefinedOnly2("U", cl::desc("Alias for --defined-only"),
78                        cl::aliasopt(DefinedOnly));
79
80 cl::opt<bool> ExternalOnly("extern-only",
81                            cl::desc("Show only external symbols"));
82 cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
83                         cl::aliasopt(ExternalOnly));
84
85 cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
86 cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
87 cl::opt<bool> DarwinFormat("m", cl::desc("Alias for --format=darwin"));
88
89 static cl::list<std::string>
90     ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
91               cl::ZeroOrMore);
92 bool ArchAll = false;
93
94 cl::opt<bool> PrintFileName(
95     "print-file-name",
96     cl::desc("Precede each symbol with the object file it came from"));
97
98 cl::alias PrintFileNameA("A", cl::desc("Alias for --print-file-name"),
99                          cl::aliasopt(PrintFileName));
100 cl::alias PrintFileNameo("o", cl::desc("Alias for --print-file-name"),
101                          cl::aliasopt(PrintFileName));
102
103 cl::opt<bool> DebugSyms("debug-syms",
104                         cl::desc("Show all symbols, even debugger only"));
105 cl::alias DebugSymsa("a", cl::desc("Alias for --debug-syms"),
106                      cl::aliasopt(DebugSyms));
107
108 cl::opt<bool> NumericSort("numeric-sort", cl::desc("Sort symbols by address"));
109 cl::alias NumericSortn("n", cl::desc("Alias for --numeric-sort"),
110                        cl::aliasopt(NumericSort));
111 cl::alias NumericSortv("v", cl::desc("Alias for --numeric-sort"),
112                        cl::aliasopt(NumericSort));
113
114 cl::opt<bool> NoSort("no-sort", cl::desc("Show symbols in order encountered"));
115 cl::alias NoSortp("p", cl::desc("Alias for --no-sort"), cl::aliasopt(NoSort));
116
117 cl::opt<bool> ReverseSort("reverse-sort", cl::desc("Sort in reverse order"));
118 cl::alias ReverseSortr("r", cl::desc("Alias for --reverse-sort"),
119                        cl::aliasopt(ReverseSort));
120
121 cl::opt<bool> PrintSize("print-size",
122                         cl::desc("Show symbol size instead of address"));
123 cl::alias PrintSizeS("S", cl::desc("Alias for --print-size"),
124                      cl::aliasopt(PrintSize));
125
126 cl::opt<bool> SizeSort("size-sort", cl::desc("Sort symbols by size"));
127
128 cl::opt<bool> WithoutAliases("without-aliases", cl::Hidden,
129                              cl::desc("Exclude aliases from output"));
130
131 cl::opt<bool> ArchiveMap("print-armap", cl::desc("Print the archive map"));
132 cl::alias ArchiveMaps("M", cl::desc("Alias for --print-armap"),
133                       cl::aliasopt(ArchiveMap));
134
135 cl::opt<bool> JustSymbolName("just-symbol-name",
136                              cl::desc("Print just the symbol's name"));
137 cl::alias JustSymbolNames("j", cl::desc("Alias for --just-symbol-name"),
138                           cl::aliasopt(JustSymbolName));
139
140 // FIXME: This option takes exactly two strings and should be allowed anywhere
141 // on the command line.  Such that "llvm-nm -s __TEXT __text foo.o" would work.
142 // But that does not as the CommandLine Library does not have a way to make
143 // this work.  For now the "-s __TEXT __text" has to be last on the command
144 // line.
145 cl::list<std::string> SegSect("s", cl::Positional, cl::ZeroOrMore,
146                               cl::desc("Dump only symbols from this segment "
147                                        "and section name, Mach-O only"));
148
149 cl::opt<bool> FormatMachOasHex("x", cl::desc("Print symbol entry in hex, "
150                                              "Mach-O only"));
151
152 cl::opt<bool> NoLLVMBitcode("no-llvm-bc",
153                             cl::desc("Disable LLVM bitcode reader"));
154
155 bool PrintAddress = true;
156
157 bool MultipleFiles = false;
158
159 bool HadError = false;
160
161 std::string ToolName;
162 }
163
164 static void error(Twine Message, Twine Path = Twine()) {
165   HadError = true;
166   errs() << ToolName << ": " << Path << ": " << Message << ".\n";
167 }
168
169 static bool error(std::error_code EC, Twine Path = Twine()) {
170   if (EC) {
171     error(EC.message(), Path);
172     return true;
173   }
174   return false;
175 }
176
177 namespace {
178 struct NMSymbol {
179   uint64_t Address;
180   uint64_t Size;
181   char TypeChar;
182   StringRef Name;
183   DataRefImpl Symb;
184 };
185 }
186
187 static bool compareSymbolAddress(const NMSymbol &A, const NMSymbol &B) {
188   if (!ReverseSort) {
189     if (A.Address < B.Address)
190       return true;
191     if (A.Address == B.Address && A.Name < B.Name)
192       return true;
193     if (A.Address == B.Address && A.Name == B.Name && A.Size < B.Size)
194       return true;
195     return false;
196   }
197
198   if (A.Address > B.Address)
199     return true;
200   if (A.Address == B.Address && A.Name > B.Name)
201     return true;
202   if (A.Address == B.Address && A.Name == B.Name && A.Size > B.Size)
203     return true;
204   return false;
205 }
206
207 static bool compareSymbolSize(const NMSymbol &A, const NMSymbol &B) {
208   if (!ReverseSort) {
209     if (A.Size < B.Size)
210       return true;
211     if (A.Size == B.Size && A.Name < B.Name)
212       return true;
213     if (A.Size == B.Size && A.Name == B.Name && A.Address < B.Address)
214       return true;
215     return false;
216   }
217
218   if (A.Size > B.Size)
219     return true;
220   if (A.Size == B.Size && A.Name > B.Name)
221     return true;
222   if (A.Size == B.Size && A.Name == B.Name && A.Address > B.Address)
223     return true;
224   return false;
225 }
226
227 static bool compareSymbolName(const NMSymbol &A, const NMSymbol &B) {
228   if (!ReverseSort) {
229     if (A.Name < B.Name)
230       return true;
231     if (A.Name == B.Name && A.Size < B.Size)
232       return true;
233     if (A.Name == B.Name && A.Size == B.Size && A.Address < B.Address)
234       return true;
235     return false;
236   }
237   if (A.Name > B.Name)
238     return true;
239   if (A.Name == B.Name && A.Size > B.Size)
240     return true;
241   if (A.Name == B.Name && A.Size == B.Size && A.Address > B.Address)
242     return true;
243   return false;
244 }
245
246 static char isSymbolList64Bit(SymbolicFile &Obj) {
247   if (isa<IRObjectFile>(Obj))
248     return false;
249   if (isa<COFFObjectFile>(Obj))
250     return false;
251   if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj))
252     return MachO->is64Bit();
253   return cast<ELFObjectFileBase>(Obj).getBytesInAddress() == 8;
254 }
255
256 static StringRef CurrentFilename;
257 typedef std::vector<NMSymbol> SymbolListT;
258 static SymbolListT SymbolList;
259
260 // darwinPrintSymbol() is used to print a symbol from a Mach-O file when the
261 // the OutputFormat is darwin or we are printing Mach-O symbols in hex.  For
262 // the darwin format it produces the same output as darwin's nm(1) -m output
263 // and when printing Mach-O symbols in hex it produces the same output as
264 // darwin's nm(1) -x format.
265 static void darwinPrintSymbol(MachOObjectFile *MachO, SymbolListT::iterator I,
266                               char *SymbolAddrStr, const char *printBlanks) {
267   MachO::mach_header H;
268   MachO::mach_header_64 H_64;
269   uint32_t Filetype, Flags;
270   MachO::nlist_64 STE_64;
271   MachO::nlist STE;
272   uint8_t NType;
273   uint8_t NSect;
274   uint16_t NDesc;
275   uint32_t NStrx;
276   uint64_t NValue;
277   if (MachO->is64Bit()) {
278     H_64 = MachO->MachOObjectFile::getHeader64();
279     Filetype = H_64.filetype;
280     Flags = H_64.flags;
281     STE_64 = MachO->getSymbol64TableEntry(I->Symb);
282     NType = STE_64.n_type;
283     NSect = STE_64.n_sect;
284     NDesc = STE_64.n_desc;
285     NStrx = STE_64.n_strx;
286     NValue = STE_64.n_value;
287   } else {
288     H = MachO->MachOObjectFile::getHeader();
289     Filetype = H.filetype;
290     Flags = H.flags;
291     STE = MachO->getSymbolTableEntry(I->Symb);
292     NType = STE.n_type;
293     NSect = STE.n_sect;
294     NDesc = STE.n_desc;
295     NStrx = STE.n_strx;
296     NValue = STE.n_value;
297   }
298
299   // If we are printing Mach-O symbols in hex do that and return.
300   if (FormatMachOasHex) {
301     char Str[18] = "";
302     const char *printFormat;
303     if (MachO->is64Bit())
304       printFormat = "%016" PRIx64;
305     else
306       printFormat = "%08" PRIx64;
307     format(printFormat, NValue).print(Str, sizeof(Str));
308     outs() << Str << ' ';
309     format("%02x", NType).print(Str, sizeof(Str));
310     outs() << Str << ' ';
311     format("%02x", NSect).print(Str, sizeof(Str));
312     outs() << Str << ' ';
313     format("%04x", NDesc).print(Str, sizeof(Str));
314     outs() << Str << ' ';
315     format("%08x", NStrx).print(Str, sizeof(Str));
316     outs() << Str << ' ';
317     outs() << I->Name << "\n";
318     return;
319   }
320
321   if (PrintAddress) {
322     if ((NType & MachO::N_TYPE) == MachO::N_INDR)
323       strcpy(SymbolAddrStr, printBlanks);
324     outs() << SymbolAddrStr << ' ';
325   }
326
327   switch (NType & MachO::N_TYPE) {
328   case MachO::N_UNDF:
329     if (NValue != 0) {
330       outs() << "(common) ";
331       if (MachO::GET_COMM_ALIGN(NDesc) != 0)
332         outs() << "(alignment 2^" << (int)MachO::GET_COMM_ALIGN(NDesc) << ") ";
333     } else {
334       if ((NType & MachO::N_TYPE) == MachO::N_PBUD)
335         outs() << "(prebound ";
336       else
337         outs() << "(";
338       if ((NDesc & MachO::REFERENCE_TYPE) ==
339           MachO::REFERENCE_FLAG_UNDEFINED_LAZY)
340         outs() << "undefined [lazy bound]) ";
341       else if ((NDesc & MachO::REFERENCE_TYPE) ==
342                MachO::REFERENCE_FLAG_UNDEFINED_LAZY)
343         outs() << "undefined [private lazy bound]) ";
344       else if ((NDesc & MachO::REFERENCE_TYPE) ==
345                MachO::REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY)
346         outs() << "undefined [private]) ";
347       else
348         outs() << "undefined) ";
349     }
350     break;
351   case MachO::N_ABS:
352     outs() << "(absolute) ";
353     break;
354   case MachO::N_INDR:
355     outs() << "(indirect) ";
356     break;
357   case MachO::N_SECT: {
358     section_iterator Sec = MachO->section_end();
359     MachO->getSymbolSection(I->Symb, Sec);
360     DataRefImpl Ref = Sec->getRawDataRefImpl();
361     StringRef SectionName;
362     MachO->getSectionName(Ref, SectionName);
363     StringRef SegmentName = MachO->getSectionFinalSegmentName(Ref);
364     outs() << "(" << SegmentName << "," << SectionName << ") ";
365     break;
366   }
367   default:
368     outs() << "(?) ";
369     break;
370   }
371
372   if (NType & MachO::N_EXT) {
373     if (NDesc & MachO::REFERENCED_DYNAMICALLY)
374       outs() << "[referenced dynamically] ";
375     if (NType & MachO::N_PEXT) {
376       if ((NDesc & MachO::N_WEAK_DEF) == MachO::N_WEAK_DEF)
377         outs() << "weak private external ";
378       else
379         outs() << "private external ";
380     } else {
381       if ((NDesc & MachO::N_WEAK_REF) == MachO::N_WEAK_REF ||
382           (NDesc & MachO::N_WEAK_DEF) == MachO::N_WEAK_DEF) {
383         if ((NDesc & (MachO::N_WEAK_REF | MachO::N_WEAK_DEF)) ==
384             (MachO::N_WEAK_REF | MachO::N_WEAK_DEF))
385           outs() << "weak external automatically hidden ";
386         else
387           outs() << "weak external ";
388       } else
389         outs() << "external ";
390     }
391   } else {
392     if (NType & MachO::N_PEXT)
393       outs() << "non-external (was a private external) ";
394     else
395       outs() << "non-external ";
396   }
397
398   if (Filetype == MachO::MH_OBJECT &&
399       (NDesc & MachO::N_NO_DEAD_STRIP) == MachO::N_NO_DEAD_STRIP)
400     outs() << "[no dead strip] ";
401
402   if (Filetype == MachO::MH_OBJECT &&
403       ((NType & MachO::N_TYPE) != MachO::N_UNDF) &&
404       (NDesc & MachO::N_SYMBOL_RESOLVER) == MachO::N_SYMBOL_RESOLVER)
405     outs() << "[symbol resolver] ";
406
407   if (Filetype == MachO::MH_OBJECT &&
408       ((NType & MachO::N_TYPE) != MachO::N_UNDF) &&
409       (NDesc & MachO::N_ALT_ENTRY) == MachO::N_ALT_ENTRY)
410     outs() << "[alt entry] ";
411
412   if ((NDesc & MachO::N_ARM_THUMB_DEF) == MachO::N_ARM_THUMB_DEF)
413     outs() << "[Thumb] ";
414
415   if ((NType & MachO::N_TYPE) == MachO::N_INDR) {
416     outs() << I->Name << " (for ";
417     StringRef IndirectName;
418     if (MachO->getIndirectName(I->Symb, IndirectName))
419       outs() << "?)";
420     else
421       outs() << IndirectName << ")";
422   } else
423     outs() << I->Name;
424
425   if ((Flags & MachO::MH_TWOLEVEL) == MachO::MH_TWOLEVEL &&
426       (((NType & MachO::N_TYPE) == MachO::N_UNDF && NValue == 0) ||
427        (NType & MachO::N_TYPE) == MachO::N_PBUD)) {
428     uint32_t LibraryOrdinal = MachO::GET_LIBRARY_ORDINAL(NDesc);
429     if (LibraryOrdinal != 0) {
430       if (LibraryOrdinal == MachO::EXECUTABLE_ORDINAL)
431         outs() << " (from executable)";
432       else if (LibraryOrdinal == MachO::DYNAMIC_LOOKUP_ORDINAL)
433         outs() << " (dynamically looked up)";
434       else {
435         StringRef LibraryName;
436         if (MachO->getLibraryShortNameByIndex(LibraryOrdinal - 1, LibraryName))
437           outs() << " (from bad library ordinal " << LibraryOrdinal << ")";
438         else
439           outs() << " (from " << LibraryName << ")";
440       }
441     }
442   }
443
444   outs() << "\n";
445 }
446
447 // Table that maps Darwin's Mach-O stab constants to strings to allow printing.
448 struct DarwinStabName {
449   uint8_t NType;
450   const char *Name;
451 };
452 static const struct DarwinStabName DarwinStabNames[] = {
453     {MachO::N_GSYM, "GSYM"},
454     {MachO::N_FNAME, "FNAME"},
455     {MachO::N_FUN, "FUN"},
456     {MachO::N_STSYM, "STSYM"},
457     {MachO::N_LCSYM, "LCSYM"},
458     {MachO::N_BNSYM, "BNSYM"},
459     {MachO::N_PC, "PC"},
460     {MachO::N_AST, "AST"},
461     {MachO::N_OPT, "OPT"},
462     {MachO::N_RSYM, "RSYM"},
463     {MachO::N_SLINE, "SLINE"},
464     {MachO::N_ENSYM, "ENSYM"},
465     {MachO::N_SSYM, "SSYM"},
466     {MachO::N_SO, "SO"},
467     {MachO::N_OSO, "OSO"},
468     {MachO::N_LSYM, "LSYM"},
469     {MachO::N_BINCL, "BINCL"},
470     {MachO::N_SOL, "SOL"},
471     {MachO::N_PARAMS, "PARAM"},
472     {MachO::N_VERSION, "VERS"},
473     {MachO::N_OLEVEL, "OLEV"},
474     {MachO::N_PSYM, "PSYM"},
475     {MachO::N_EINCL, "EINCL"},
476     {MachO::N_ENTRY, "ENTRY"},
477     {MachO::N_LBRAC, "LBRAC"},
478     {MachO::N_EXCL, "EXCL"},
479     {MachO::N_RBRAC, "RBRAC"},
480     {MachO::N_BCOMM, "BCOMM"},
481     {MachO::N_ECOMM, "ECOMM"},
482     {MachO::N_ECOML, "ECOML"},
483     {MachO::N_LENG, "LENG"},
484     {0, 0}};
485 static const char *getDarwinStabString(uint8_t NType) {
486   for (unsigned i = 0; DarwinStabNames[i].Name; i++) {
487     if (DarwinStabNames[i].NType == NType)
488       return DarwinStabNames[i].Name;
489   }
490   return 0;
491 }
492
493 // darwinPrintStab() prints the n_sect, n_desc along with a symbolic name of
494 // a stab n_type value in a Mach-O file.
495 static void darwinPrintStab(MachOObjectFile *MachO, SymbolListT::iterator I) {
496   MachO::nlist_64 STE_64;
497   MachO::nlist STE;
498   uint8_t NType;
499   uint8_t NSect;
500   uint16_t NDesc;
501   if (MachO->is64Bit()) {
502     STE_64 = MachO->getSymbol64TableEntry(I->Symb);
503     NType = STE_64.n_type;
504     NSect = STE_64.n_sect;
505     NDesc = STE_64.n_desc;
506   } else {
507     STE = MachO->getSymbolTableEntry(I->Symb);
508     NType = STE.n_type;
509     NSect = STE.n_sect;
510     NDesc = STE.n_desc;
511   }
512
513   char Str[18] = "";
514   format("%02x", NSect).print(Str, sizeof(Str));
515   outs() << ' ' << Str << ' ';
516   format("%04x", NDesc).print(Str, sizeof(Str));
517   outs() << Str << ' ';
518   if (const char *stabString = getDarwinStabString(NType))
519     format("%5.5s", stabString).print(Str, sizeof(Str));
520   else
521     format("   %02x", NType).print(Str, sizeof(Str));
522   outs() << Str;
523 }
524
525 static void sortAndPrintSymbolList(SymbolicFile &Obj, bool printName,
526                                    std::string ArchiveName,
527                                    std::string ArchitectureName) {
528   if (!NoSort) {
529     if (NumericSort)
530       std::sort(SymbolList.begin(), SymbolList.end(), compareSymbolAddress);
531     else if (SizeSort)
532       std::sort(SymbolList.begin(), SymbolList.end(), compareSymbolSize);
533     else
534       std::sort(SymbolList.begin(), SymbolList.end(), compareSymbolName);
535   }
536
537   if (!PrintFileName) {
538     if (OutputFormat == posix && MultipleFiles && printName) {
539       outs() << '\n' << CurrentFilename << ":\n";
540     } else if (OutputFormat == bsd && MultipleFiles && printName) {
541       outs() << "\n" << CurrentFilename << ":\n";
542     } else if (OutputFormat == sysv) {
543       outs() << "\n\nSymbols from " << CurrentFilename << ":\n\n"
544              << "Name                  Value   Class        Type"
545              << "         Size   Line  Section\n";
546     }
547   }
548
549   const char *printBlanks, *printFormat;
550   if (isSymbolList64Bit(Obj)) {
551     printBlanks = "                ";
552     printFormat = "%016" PRIx64;
553   } else {
554     printBlanks = "        ";
555     printFormat = "%08" PRIx64;
556   }
557
558   for (SymbolListT::iterator I = SymbolList.begin(), E = SymbolList.end();
559        I != E; ++I) {
560     if ((I->TypeChar != 'U') && UndefinedOnly)
561       continue;
562     if ((I->TypeChar == 'U') && DefinedOnly)
563       continue;
564     if (SizeSort && !PrintAddress)
565       continue;
566     if (PrintFileName) {
567       if (!ArchitectureName.empty())
568         outs() << "(for architecture " << ArchitectureName << "):";
569       if (!ArchiveName.empty())
570         outs() << ArchiveName << ":";
571       outs() << CurrentFilename << ": ";
572     }
573     if (JustSymbolName || (UndefinedOnly && isa<MachOObjectFile>(Obj))) {
574       outs() << I->Name << "\n";
575       continue;
576     }
577
578     char SymbolAddrStr[18] = "";
579     char SymbolSizeStr[18] = "";
580
581     if (OutputFormat == sysv || I->Address == UnknownAddress)
582       strcpy(SymbolAddrStr, printBlanks);
583     if (OutputFormat == sysv)
584       strcpy(SymbolSizeStr, printBlanks);
585
586     if (I->Address != UnknownAddress)
587       format(printFormat, I->Address)
588           .print(SymbolAddrStr, sizeof(SymbolAddrStr));
589     format(printFormat, I->Size).print(SymbolSizeStr, sizeof(SymbolSizeStr));
590
591     // If OutputFormat is darwin or we are printing Mach-O symbols in hex and
592     // we have a MachOObjectFile, call darwinPrintSymbol to print as darwin's
593     // nm(1) -m output or hex, else if OutputFormat is darwin or we are
594     // printing Mach-O symbols in hex and not a Mach-O object fall back to
595     // OutputFormat bsd (see below).
596     MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj);
597     if ((OutputFormat == darwin || FormatMachOasHex) && MachO) {
598       darwinPrintSymbol(MachO, I, SymbolAddrStr, printBlanks);
599     } else if (OutputFormat == posix) {
600       outs() << I->Name << " " << I->TypeChar << " " << SymbolAddrStr
601              << SymbolSizeStr << "\n";
602     } else if (OutputFormat == bsd || (OutputFormat == darwin && !MachO)) {
603       if (PrintAddress)
604         outs() << SymbolAddrStr << ' ';
605       if (PrintSize) {
606         outs() << SymbolSizeStr;
607         outs() << ' ';
608       }
609       outs() << I->TypeChar;
610       if (I->TypeChar == '-' && MachO)
611         darwinPrintStab(MachO, I);
612       outs() << " " << I->Name << "\n";
613     } else if (OutputFormat == sysv) {
614       std::string PaddedName(I->Name);
615       while (PaddedName.length() < 20)
616         PaddedName += " ";
617       outs() << PaddedName << "|" << SymbolAddrStr << "|   " << I->TypeChar
618              << "  |                  |" << SymbolSizeStr << "|     |\n";
619     }
620   }
621
622   SymbolList.clear();
623 }
624
625 static char getSymbolNMTypeChar(ELFObjectFileBase &Obj,
626                                 basic_symbol_iterator I) {
627   // OK, this is ELF
628   elf_symbol_iterator SymI(I);
629
630   elf_section_iterator SecI = Obj.section_end();
631   if (error(SymI->getSection(SecI)))
632     return '?';
633
634   if (SecI != Obj.section_end()) {
635     switch (SecI->getType()) {
636     case ELF::SHT_PROGBITS:
637     case ELF::SHT_DYNAMIC:
638       switch (SecI->getFlags()) {
639       case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR):
640         return 't';
641       case (ELF::SHF_TLS | ELF::SHF_ALLOC | ELF::SHF_WRITE):
642       case (ELF::SHF_ALLOC | ELF::SHF_WRITE):
643         return 'd';
644       case ELF::SHF_ALLOC:
645       case (ELF::SHF_ALLOC | ELF::SHF_MERGE):
646       case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS):
647         return 'r';
648       }
649       break;
650     case ELF::SHT_NOBITS:
651       return 'b';
652     }
653   }
654
655   if (SymI->getELFType() == ELF::STT_SECTION) {
656     StringRef Name;
657     if (error(SymI->getName(Name)))
658       return '?';
659     return StringSwitch<char>(Name)
660         .StartsWith(".debug", 'N')
661         .StartsWith(".note", 'n')
662         .Default('?');
663   }
664
665   return 'n';
666 }
667
668 static char getSymbolNMTypeChar(COFFObjectFile &Obj, symbol_iterator I) {
669   COFFSymbolRef Symb = Obj.getCOFFSymbol(*I);
670   // OK, this is COFF.
671   symbol_iterator SymI(I);
672
673   StringRef Name;
674   if (error(SymI->getName(Name)))
675     return '?';
676
677   char Ret = StringSwitch<char>(Name)
678                  .StartsWith(".debug", 'N')
679                  .StartsWith(".sxdata", 'N')
680                  .Default('?');
681
682   if (Ret != '?')
683     return Ret;
684
685   uint32_t Characteristics = 0;
686   if (!COFF::isReservedSectionNumber(Symb.getSectionNumber())) {
687     section_iterator SecI = Obj.section_end();
688     if (error(SymI->getSection(SecI)))
689       return '?';
690     const coff_section *Section = Obj.getCOFFSection(*SecI);
691     Characteristics = Section->Characteristics;
692   }
693
694   switch (Symb.getSectionNumber()) {
695   case COFF::IMAGE_SYM_DEBUG:
696     return 'n';
697   default:
698     // Check section type.
699     if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
700       return 't';
701     if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
702       return Characteristics & COFF::IMAGE_SCN_MEM_WRITE ? 'd' : 'r';
703     if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
704       return 'b';
705     if (Characteristics & COFF::IMAGE_SCN_LNK_INFO)
706       return 'i';
707     // Check for section symbol.
708     if (Symb.isSectionDefinition())
709       return 's';
710   }
711
712   return '?';
713 }
714
715 static uint8_t getNType(MachOObjectFile &Obj, DataRefImpl Symb) {
716   if (Obj.is64Bit()) {
717     MachO::nlist_64 STE = Obj.getSymbol64TableEntry(Symb);
718     return STE.n_type;
719   }
720   MachO::nlist STE = Obj.getSymbolTableEntry(Symb);
721   return STE.n_type;
722 }
723
724 static char getSymbolNMTypeChar(MachOObjectFile &Obj, basic_symbol_iterator I) {
725   DataRefImpl Symb = I->getRawDataRefImpl();
726   uint8_t NType = getNType(Obj, Symb);
727
728   if (NType & MachO::N_STAB)
729     return '-';
730
731   switch (NType & MachO::N_TYPE) {
732   case MachO::N_ABS:
733     return 's';
734   case MachO::N_INDR:
735     return 'i';
736   case MachO::N_SECT: {
737     section_iterator Sec = Obj.section_end();
738     Obj.getSymbolSection(Symb, Sec);
739     DataRefImpl Ref = Sec->getRawDataRefImpl();
740     StringRef SectionName;
741     Obj.getSectionName(Ref, SectionName);
742     StringRef SegmentName = Obj.getSectionFinalSegmentName(Ref);
743     if (SegmentName == "__TEXT" && SectionName == "__text")
744       return 't';
745     else if (SegmentName == "__DATA" && SectionName == "__data")
746       return 'd';
747     else if (SegmentName == "__DATA" && SectionName == "__bss")
748       return 'b';
749     else
750       return 's';
751   }
752   }
753
754   return '?';
755 }
756
757 static char getSymbolNMTypeChar(const GlobalValue &GV) {
758   if (GV.getType()->getElementType()->isFunctionTy())
759     return 't';
760   // FIXME: should we print 'b'? At the IR level we cannot be sure if this
761   // will be in bss or not, but we could approximate.
762   return 'd';
763 }
764
765 static char getSymbolNMTypeChar(IRObjectFile &Obj, basic_symbol_iterator I) {
766   const GlobalValue *GV = Obj.getSymbolGV(I->getRawDataRefImpl());
767   if (!GV)
768     return 't';
769   return getSymbolNMTypeChar(*GV);
770 }
771
772 static bool isObject(SymbolicFile &Obj, basic_symbol_iterator I) {
773   auto *ELF = dyn_cast<ELFObjectFileBase>(&Obj);
774   if (!ELF)
775     return false;
776
777   return elf_symbol_iterator(I)->getELFType() == ELF::STT_OBJECT;
778 }
779
780 static char getNMTypeChar(SymbolicFile &Obj, basic_symbol_iterator I) {
781   uint32_t Symflags = I->getFlags();
782   if ((Symflags & object::SymbolRef::SF_Weak) && !isa<MachOObjectFile>(Obj)) {
783     char Ret = isObject(Obj, I) ? 'v' : 'w';
784     if (!(Symflags & object::SymbolRef::SF_Undefined))
785       Ret = toupper(Ret);
786     return Ret;
787   }
788
789   if (Symflags & object::SymbolRef::SF_Undefined)
790     return 'U';
791
792   if (Symflags & object::SymbolRef::SF_Common)
793     return 'C';
794
795   char Ret = '?';
796   if (Symflags & object::SymbolRef::SF_Absolute)
797     Ret = 'a';
798   else if (IRObjectFile *IR = dyn_cast<IRObjectFile>(&Obj))
799     Ret = getSymbolNMTypeChar(*IR, I);
800   else if (COFFObjectFile *COFF = dyn_cast<COFFObjectFile>(&Obj))
801     Ret = getSymbolNMTypeChar(*COFF, I);
802   else if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj))
803     Ret = getSymbolNMTypeChar(*MachO, I);
804   else
805     Ret = getSymbolNMTypeChar(cast<ELFObjectFileBase>(Obj), I);
806
807   if (Symflags & object::SymbolRef::SF_Global)
808     Ret = toupper(Ret);
809
810   return Ret;
811 }
812
813 // getNsectForSegSect() is used to implement the Mach-O "-s segname sectname"
814 // option to dump only those symbols from that section in a Mach-O file.
815 // It is called once for each Mach-O file from dumpSymbolNamesFromObject()
816 // to get the section number for that named section from the command line
817 // arguments. It returns the section number for that section in the Mach-O
818 // file or zero it is not present.
819 static unsigned getNsectForSegSect(MachOObjectFile *Obj) {
820   unsigned Nsect = 1;
821   for (section_iterator I = Obj->section_begin(), E = Obj->section_end();
822        I != E; ++I) {
823     DataRefImpl Ref = I->getRawDataRefImpl();
824     StringRef SectionName;
825     Obj->getSectionName(Ref, SectionName);
826     StringRef SegmentName = Obj->getSectionFinalSegmentName(Ref);
827     if (SegmentName == SegSect[0] && SectionName == SegSect[1])
828       return Nsect;
829     Nsect++;
830   }
831   return 0;
832 }
833
834 // getNsectInMachO() is used to implement the Mach-O "-s segname sectname"
835 // option to dump only those symbols from that section in a Mach-O file.
836 // It is called once for each symbol in a Mach-O file from
837 // dumpSymbolNamesFromObject() and returns the section number for that symbol
838 // if it is in a section, else it returns 0.
839 static unsigned getNsectInMachO(MachOObjectFile &Obj, BasicSymbolRef Sym) {
840   DataRefImpl Symb = Sym.getRawDataRefImpl();
841   if (Obj.is64Bit()) {
842     MachO::nlist_64 STE = Obj.getSymbol64TableEntry(Symb);
843     if ((STE.n_type & MachO::N_TYPE) == MachO::N_SECT)
844       return STE.n_sect;
845     return 0;
846   }
847   MachO::nlist STE = Obj.getSymbolTableEntry(Symb);
848   if ((STE.n_type & MachO::N_TYPE) == MachO::N_SECT)
849     return STE.n_sect;
850   return 0;
851 }
852
853 static void dumpSymbolNamesFromObject(SymbolicFile &Obj, bool printName,
854                                       std::string ArchiveName = std::string(),
855                                       std::string ArchitectureName =
856                                         std::string()) {
857   auto Symbols = Obj.symbols();
858   if (DynamicSyms) {
859     const auto *E = dyn_cast<ELFObjectFileBase>(&Obj);
860     if (!E) {
861       error("File format has no dynamic symbol table", Obj.getFileName());
862       return;
863     }
864     auto DynSymbols = E->getDynamicSymbolIterators();
865     Symbols =
866         make_range<basic_symbol_iterator>(DynSymbols.begin(), DynSymbols.end());
867   }
868   std::string NameBuffer;
869   raw_string_ostream OS(NameBuffer);
870   // If a "-s segname sectname" option was specified and this is a Mach-O
871   // file get the section number for that section in this object file.
872   unsigned int Nsect = 0;
873   MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj);
874   if (SegSect.size() != 0 && MachO) {
875     Nsect = getNsectForSegSect(MachO);
876     // If this section is not in the object file no symbols are printed.
877     if (Nsect == 0)
878       return;
879   }
880   for (BasicSymbolRef Sym : Symbols) {
881     uint32_t SymFlags = Sym.getFlags();
882     if (!DebugSyms && (SymFlags & SymbolRef::SF_FormatSpecific))
883       continue;
884     if (WithoutAliases) {
885       if (IRObjectFile *IR = dyn_cast<IRObjectFile>(&Obj)) {
886         const GlobalValue *GV = IR->getSymbolGV(Sym.getRawDataRefImpl());
887         if (GV && isa<GlobalAlias>(GV))
888           continue;
889       }
890     }
891     // If a "-s segname sectname" option was specified and this is a Mach-O
892     // file and this section appears in this file, Nsect will be non-zero then
893     // see if this symbol is a symbol from that section and if not skip it.
894     if (Nsect && Nsect != getNsectInMachO(*MachO, Sym))
895       continue;
896     NMSymbol S;
897     S.Size = 0;
898     S.Address = UnknownAddress;
899     if (PrintSize) {
900       if (isa<ELFObjectFileBase>(&Obj))
901         S.Size = ELFSymbolRef(Sym).getSize();
902     }
903     if (PrintAddress && isa<ObjectFile>(Obj)) {
904       if (error(SymbolRef(Sym).getAddress(S.Address)))
905         break;
906     }
907     S.TypeChar = getNMTypeChar(Obj, Sym);
908     if (error(Sym.printName(OS)))
909       break;
910     OS << '\0';
911     S.Symb = Sym.getRawDataRefImpl();
912     SymbolList.push_back(S);
913   }
914
915   OS.flush();
916   const char *P = NameBuffer.c_str();
917   for (unsigned I = 0; I < SymbolList.size(); ++I) {
918     SymbolList[I].Name = P;
919     P += strlen(P) + 1;
920   }
921
922   CurrentFilename = Obj.getFileName();
923   sortAndPrintSymbolList(Obj, printName, ArchiveName, ArchitectureName);
924 }
925
926 // checkMachOAndArchFlags() checks to see if the SymbolicFile is a Mach-O file
927 // and if it is and there is a list of architecture flags is specified then
928 // check to make sure this Mach-O file is one of those architectures or all
929 // architectures was specificed.  If not then an error is generated and this
930 // routine returns false.  Else it returns true.
931 static bool checkMachOAndArchFlags(SymbolicFile *O, std::string &Filename) {
932   MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(O);
933
934   if (!MachO || ArchAll || ArchFlags.size() == 0)
935     return true;
936
937   MachO::mach_header H;
938   MachO::mach_header_64 H_64;
939   Triple T;
940   if (MachO->is64Bit()) {
941     H_64 = MachO->MachOObjectFile::getHeader64();
942     T = MachOObjectFile::getArch(H_64.cputype, H_64.cpusubtype);
943   } else {
944     H = MachO->MachOObjectFile::getHeader();
945     T = MachOObjectFile::getArch(H.cputype, H.cpusubtype);
946   }
947   if (std::none_of(
948           ArchFlags.begin(), ArchFlags.end(),
949           [&](const std::string &Name) { return Name == T.getArchName(); })) {
950     error("No architecture specified", Filename);
951     return false;
952   }
953   return true;
954 }
955
956 static void dumpSymbolNamesFromFile(std::string &Filename) {
957   ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
958       MemoryBuffer::getFileOrSTDIN(Filename);
959   if (error(BufferOrErr.getError(), Filename))
960     return;
961
962   LLVMContext &Context = getGlobalContext();
963   ErrorOr<std::unique_ptr<Binary>> BinaryOrErr = createBinary(
964       BufferOrErr.get()->getMemBufferRef(), NoLLVMBitcode ? nullptr : &Context);
965   if (error(BinaryOrErr.getError(), Filename))
966     return;
967   Binary &Bin = *BinaryOrErr.get();
968
969   if (Archive *A = dyn_cast<Archive>(&Bin)) {
970     if (ArchiveMap) {
971       Archive::symbol_iterator I = A->symbol_begin();
972       Archive::symbol_iterator E = A->symbol_end();
973       if (I != E) {
974         outs() << "Archive map\n";
975         for (; I != E; ++I) {
976           ErrorOr<Archive::child_iterator> C = I->getMember();
977           if (error(C.getError()))
978             return;
979           ErrorOr<StringRef> FileNameOrErr = C.get()->getName();
980           if (error(FileNameOrErr.getError()))
981             return;
982           StringRef SymName = I->getName();
983           outs() << SymName << " in " << FileNameOrErr.get() << "\n";
984         }
985         outs() << "\n";
986       }
987     }
988
989     for (Archive::child_iterator I = A->child_begin(), E = A->child_end();
990          I != E; ++I) {
991       ErrorOr<std::unique_ptr<Binary>> ChildOrErr = I->getAsBinary(&Context);
992       if (ChildOrErr.getError())
993         continue;
994       if (SymbolicFile *O = dyn_cast<SymbolicFile>(&*ChildOrErr.get())) {
995         if (!checkMachOAndArchFlags(O, Filename))
996           return;
997         if (!PrintFileName) {
998           outs() << "\n";
999           if (isa<MachOObjectFile>(O)) {
1000             outs() << Filename << "(" << O->getFileName() << ")";
1001           } else
1002             outs() << O->getFileName();
1003           outs() << ":\n";
1004         }
1005         dumpSymbolNamesFromObject(*O, false, Filename);
1006       }
1007     }
1008     return;
1009   }
1010   if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
1011     // If we have a list of architecture flags specified dump only those.
1012     if (!ArchAll && ArchFlags.size() != 0) {
1013       // Look for a slice in the universal binary that matches each ArchFlag.
1014       bool ArchFound;
1015       for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1016         ArchFound = false;
1017         for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1018                                                    E = UB->end_objects();
1019              I != E; ++I) {
1020           if (ArchFlags[i] == I->getArchTypeName()) {
1021             ArchFound = true;
1022             ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr =
1023                 I->getAsObjectFile();
1024             std::string ArchiveName;
1025             std::string ArchitectureName;
1026             ArchiveName.clear();
1027             ArchitectureName.clear();
1028             if (ObjOrErr) {
1029               ObjectFile &Obj = *ObjOrErr.get();
1030               if (ArchFlags.size() > 1) {
1031                 if (PrintFileName)
1032                   ArchitectureName = I->getArchTypeName();
1033                 else
1034                   outs() << "\n" << Obj.getFileName() << " (for architecture "
1035                          << I->getArchTypeName() << ")"
1036                          << ":\n";
1037               }
1038               dumpSymbolNamesFromObject(Obj, false, ArchiveName,
1039                                         ArchitectureName);
1040             } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
1041                            I->getAsArchive()) {
1042               std::unique_ptr<Archive> &A = *AOrErr;
1043               for (Archive::child_iterator AI = A->child_begin(),
1044                                            AE = A->child_end();
1045                    AI != AE; ++AI) {
1046                 ErrorOr<std::unique_ptr<Binary>> ChildOrErr =
1047                     AI->getAsBinary(&Context);
1048                 if (ChildOrErr.getError())
1049                   continue;
1050                 if (SymbolicFile *O =
1051                         dyn_cast<SymbolicFile>(&*ChildOrErr.get())) {
1052                   if (PrintFileName) {
1053                     ArchiveName = A->getFileName();
1054                     if (ArchFlags.size() > 1)
1055                       ArchitectureName = I->getArchTypeName();
1056                   } else {
1057                     outs() << "\n" << A->getFileName();
1058                     outs() << "(" << O->getFileName() << ")";
1059                     if (ArchFlags.size() > 1) {
1060                       outs() << " (for architecture " << I->getArchTypeName()
1061                              << ")";
1062                     }
1063                     outs() << ":\n";
1064                   }
1065                   dumpSymbolNamesFromObject(*O, false, ArchiveName,
1066                                             ArchitectureName);
1067                 }
1068               }
1069             }
1070           }
1071         }
1072         if (!ArchFound) {
1073           error(ArchFlags[i],
1074                 "file: " + Filename + " does not contain architecture");
1075           return;
1076         }
1077       }
1078       return;
1079     }
1080     // No architecture flags were specified so if this contains a slice that
1081     // matches the host architecture dump only that.
1082     if (!ArchAll) {
1083       StringRef HostArchName = MachOObjectFile::getHostArch().getArchName();
1084       for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1085                                                  E = UB->end_objects();
1086            I != E; ++I) {
1087         if (HostArchName == I->getArchTypeName()) {
1088           ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
1089           std::string ArchiveName;
1090           ArchiveName.clear();
1091           if (ObjOrErr) {
1092             ObjectFile &Obj = *ObjOrErr.get();
1093             dumpSymbolNamesFromObject(Obj, false);
1094           } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
1095                          I->getAsArchive()) {
1096             std::unique_ptr<Archive> &A = *AOrErr;
1097             for (Archive::child_iterator AI = A->child_begin(),
1098                                          AE = A->child_end();
1099                  AI != AE; ++AI) {
1100               ErrorOr<std::unique_ptr<Binary>> ChildOrErr =
1101                   AI->getAsBinary(&Context);
1102               if (ChildOrErr.getError())
1103                 continue;
1104               if (SymbolicFile *O =
1105                       dyn_cast<SymbolicFile>(&*ChildOrErr.get())) {
1106                 if (PrintFileName)
1107                   ArchiveName = A->getFileName();
1108                 else
1109                   outs() << "\n" << A->getFileName() << "(" << O->getFileName()
1110                          << ")"
1111                          << ":\n";
1112                 dumpSymbolNamesFromObject(*O, false, ArchiveName);
1113               }
1114             }
1115           }
1116           return;
1117         }
1118       }
1119     }
1120     // Either all architectures have been specified or none have been specified
1121     // and this does not contain the host architecture so dump all the slices.
1122     bool moreThanOneArch = UB->getNumberOfObjects() > 1;
1123     for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1124                                                E = UB->end_objects();
1125          I != E; ++I) {
1126       ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
1127       std::string ArchiveName;
1128       std::string ArchitectureName;
1129       ArchiveName.clear();
1130       ArchitectureName.clear();
1131       if (ObjOrErr) {
1132         ObjectFile &Obj = *ObjOrErr.get();
1133         if (PrintFileName) {
1134           if (isa<MachOObjectFile>(Obj) && moreThanOneArch)
1135             ArchitectureName = I->getArchTypeName();
1136         } else {
1137           if (moreThanOneArch)
1138             outs() << "\n";
1139           outs() << Obj.getFileName();
1140           if (isa<MachOObjectFile>(Obj) && moreThanOneArch)
1141             outs() << " (for architecture " << I->getArchTypeName() << ")";
1142           outs() << ":\n";
1143         }
1144         dumpSymbolNamesFromObject(Obj, false, ArchiveName, ArchitectureName);
1145       } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = I->getAsArchive()) {
1146         std::unique_ptr<Archive> &A = *AOrErr;
1147         for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end();
1148              AI != AE; ++AI) {
1149           ErrorOr<std::unique_ptr<Binary>> ChildOrErr =
1150               AI->getAsBinary(&Context);
1151           if (ChildOrErr.getError())
1152             continue;
1153           if (SymbolicFile *O = dyn_cast<SymbolicFile>(&*ChildOrErr.get())) {
1154             if (PrintFileName) {
1155               ArchiveName = A->getFileName();
1156               if (isa<MachOObjectFile>(O) && moreThanOneArch)
1157                 ArchitectureName = I->getArchTypeName();
1158             } else {
1159               outs() << "\n" << A->getFileName();
1160               if (isa<MachOObjectFile>(O)) {
1161                 outs() << "(" << O->getFileName() << ")";
1162                 if (moreThanOneArch)
1163                   outs() << " (for architecture " << I->getArchTypeName()
1164                          << ")";
1165               } else
1166                 outs() << ":" << O->getFileName();
1167               outs() << ":\n";
1168             }
1169             dumpSymbolNamesFromObject(*O, false, ArchiveName, ArchitectureName);
1170           }
1171         }
1172       }
1173     }
1174     return;
1175   }
1176   if (SymbolicFile *O = dyn_cast<SymbolicFile>(&Bin)) {
1177     if (!checkMachOAndArchFlags(O, Filename))
1178       return;
1179     dumpSymbolNamesFromObject(*O, true);
1180     return;
1181   }
1182   error("unrecognizable file type", Filename);
1183   return;
1184 }
1185
1186 int main(int argc, char **argv) {
1187   // Print a stack trace if we signal out.
1188   sys::PrintStackTraceOnErrorSignal();
1189   PrettyStackTraceProgram X(argc, argv);
1190
1191   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
1192   cl::ParseCommandLineOptions(argc, argv, "llvm symbol table dumper\n");
1193
1194   // llvm-nm only reads binary files.
1195   if (error(sys::ChangeStdinToBinary()))
1196     return 1;
1197
1198   llvm::InitializeAllTargetInfos();
1199   llvm::InitializeAllTargetMCs();
1200   llvm::InitializeAllAsmParsers();
1201
1202   ToolName = argv[0];
1203   if (BSDFormat)
1204     OutputFormat = bsd;
1205   if (POSIXFormat)
1206     OutputFormat = posix;
1207   if (DarwinFormat)
1208     OutputFormat = darwin;
1209
1210   // The relative order of these is important. If you pass --size-sort it should
1211   // only print out the size. However, if you pass -S --size-sort, it should
1212   // print out both the size and address.
1213   if (SizeSort && !PrintSize)
1214     PrintAddress = false;
1215   if (OutputFormat == sysv || SizeSort)
1216     PrintSize = true;
1217
1218   switch (InputFilenames.size()) {
1219   case 0:
1220     InputFilenames.push_back("a.out");
1221   case 1:
1222     break;
1223   default:
1224     MultipleFiles = true;
1225   }
1226
1227   for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1228     if (ArchFlags[i] == "all") {
1229       ArchAll = true;
1230     } else {
1231       if (!MachOObjectFile::isValidArch(ArchFlags[i]))
1232         error("Unknown architecture named '" + ArchFlags[i] + "'",
1233               "for the -arch option");
1234     }
1235   }
1236
1237   if (SegSect.size() != 0 && SegSect.size() != 2)
1238     error("bad number of arguments (must be two arguments)",
1239           "for the -s option");
1240
1241   std::for_each(InputFilenames.begin(), InputFilenames.end(),
1242                 dumpSymbolNamesFromFile);
1243
1244   if (HadError)
1245     return 1;
1246
1247   return 0;
1248 }