Suggested improvement by Rafael Espindola to use isa<> in a few places
[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/raw_ostream.h"
40 #include "llvm/Support/system_error.h"
41 #include <algorithm>
42 #include <cctype>
43 #include <cerrno>
44 #include <cstring>
45 #include <vector>
46 using namespace llvm;
47 using namespace object;
48
49 namespace {
50 enum OutputFormatTy { bsd, sysv, posix };
51 cl::opt<OutputFormatTy> OutputFormat(
52     "format", cl::desc("Specify output format"),
53     cl::values(clEnumVal(bsd, "BSD format"), clEnumVal(sysv, "System V format"),
54                clEnumVal(posix, "POSIX.2 format"), clEnumValEnd),
55     cl::init(bsd));
56 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
57                         cl::aliasopt(OutputFormat));
58
59 cl::list<std::string> InputFilenames(cl::Positional, cl::desc("<input files>"),
60                                      cl::ZeroOrMore);
61
62 cl::opt<bool> UndefinedOnly("undefined-only",
63                             cl::desc("Show only undefined symbols"));
64 cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
65                          cl::aliasopt(UndefinedOnly));
66
67 cl::opt<bool> DynamicSyms("dynamic",
68                           cl::desc("Display the dynamic symbols instead "
69                                    "of normal symbols."));
70 cl::alias DynamicSyms2("D", cl::desc("Alias for --dynamic"),
71                        cl::aliasopt(DynamicSyms));
72
73 cl::opt<bool> DefinedOnly("defined-only",
74                           cl::desc("Show only defined symbols"));
75
76 cl::opt<bool> ExternalOnly("extern-only",
77                            cl::desc("Show only external symbols"));
78 cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
79                         cl::aliasopt(ExternalOnly));
80
81 cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
82 cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
83
84 cl::opt<bool> PrintFileName(
85     "print-file-name",
86     cl::desc("Precede each symbol with the object file it came from"));
87
88 cl::alias PrintFileNameA("A", cl::desc("Alias for --print-file-name"),
89                          cl::aliasopt(PrintFileName));
90 cl::alias PrintFileNameo("o", cl::desc("Alias for --print-file-name"),
91                          cl::aliasopt(PrintFileName));
92
93 cl::opt<bool> DebugSyms("debug-syms",
94                         cl::desc("Show all symbols, even debugger only"));
95 cl::alias DebugSymsa("a", cl::desc("Alias for --debug-syms"),
96                      cl::aliasopt(DebugSyms));
97
98 cl::opt<bool> NumericSort("numeric-sort", cl::desc("Sort symbols by address"));
99 cl::alias NumericSortn("n", cl::desc("Alias for --numeric-sort"),
100                        cl::aliasopt(NumericSort));
101 cl::alias NumericSortv("v", cl::desc("Alias for --numeric-sort"),
102                        cl::aliasopt(NumericSort));
103
104 cl::opt<bool> NoSort("no-sort", cl::desc("Show symbols in order encountered"));
105 cl::alias NoSortp("p", cl::desc("Alias for --no-sort"), cl::aliasopt(NoSort));
106
107 cl::opt<bool> PrintSize("print-size",
108                         cl::desc("Show symbol size instead of address"));
109 cl::alias PrintSizeS("S", cl::desc("Alias for --print-size"),
110                      cl::aliasopt(PrintSize));
111
112 cl::opt<bool> SizeSort("size-sort", cl::desc("Sort symbols by size"));
113
114 cl::opt<bool> WithoutAliases("without-aliases", cl::Hidden,
115                              cl::desc("Exclude aliases from output"));
116
117 cl::opt<bool> ArchiveMap("print-armap", cl::desc("Print the archive map"));
118 cl::alias ArchiveMaps("s", cl::desc("Alias for --print-armap"),
119                       cl::aliasopt(ArchiveMap));
120 bool PrintAddress = true;
121
122 bool MultipleFiles = false;
123
124 bool HadError = false;
125
126 std::string ToolName;
127 }
128
129 static void error(Twine Message, Twine Path = Twine()) {
130   HadError = true;
131   errs() << ToolName << ": " << Path << ": " << Message << ".\n";
132 }
133
134 static bool error(error_code EC, Twine Path = Twine()) {
135   if (EC) {
136     error(EC.message(), Path);
137     return true;
138   }
139   return false;
140 }
141
142 namespace {
143 struct NMSymbol {
144   uint64_t Address;
145   uint64_t Size;
146   char TypeChar;
147   StringRef Name;
148 };
149 }
150
151 static bool compareSymbolAddress(const NMSymbol &A, const NMSymbol &B) {
152   if (A.Address < B.Address)
153     return true;
154   else if (A.Address == B.Address && A.Name < B.Name)
155     return true;
156   else if (A.Address == B.Address && A.Name == B.Name && A.Size < B.Size)
157     return true;
158   else
159     return false;
160 }
161
162 static bool compareSymbolSize(const NMSymbol &A, const NMSymbol &B) {
163   if (A.Size < B.Size)
164     return true;
165   else if (A.Size == B.Size && A.Name < B.Name)
166     return true;
167   else if (A.Size == B.Size && A.Name == B.Name && A.Address < B.Address)
168     return true;
169   else
170     return false;
171 }
172
173 static bool compareSymbolName(const NMSymbol &A, const NMSymbol &B) {
174   if (A.Name < B.Name)
175     return true;
176   else if (A.Name == B.Name && A.Size < B.Size)
177     return true;
178   else if (A.Name == B.Name && A.Size == B.Size && A.Address < B.Address)
179     return true;
180   else
181     return false;
182 }
183
184 static char isSymbolList64Bit(SymbolicFile *Obj) {
185   if (isa<IRObjectFile>(Obj))
186     return false;
187   else if (isa<COFFObjectFile>(Obj))
188     return false;
189   else if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(Obj))
190     return MachO->is64Bit();
191   else if (isa<ELF32LEObjectFile>(Obj))
192     return false;
193   else if (isa<ELF64LEObjectFile>(Obj))
194     return true;
195   else if (isa<ELF32BEObjectFile>(Obj))
196     return false;
197   else if(isa<ELF64BEObjectFile>(Obj))
198     return true;
199   else
200     return false;
201 }
202
203 static StringRef CurrentFilename;
204 typedef std::vector<NMSymbol> SymbolListT;
205 static SymbolListT SymbolList;
206
207 static void sortAndPrintSymbolList(SymbolicFile *Obj) {
208   if (!NoSort) {
209     if (NumericSort)
210       std::sort(SymbolList.begin(), SymbolList.end(), compareSymbolAddress);
211     else if (SizeSort)
212       std::sort(SymbolList.begin(), SymbolList.end(), compareSymbolSize);
213     else
214       std::sort(SymbolList.begin(), SymbolList.end(), compareSymbolName);
215   }
216
217   if (OutputFormat == posix && MultipleFiles) {
218     outs() << '\n' << CurrentFilename << ":\n";
219   } else if (OutputFormat == bsd && MultipleFiles) {
220     outs() << "\n" << CurrentFilename << ":\n";
221   } else if (OutputFormat == sysv) {
222     outs() << "\n\nSymbols from " << CurrentFilename << ":\n\n"
223            << "Name                  Value   Class        Type"
224            << "         Size   Line  Section\n";
225   }
226
227   const char *printBlanks, *printFormat;
228   if (isSymbolList64Bit(Obj)) {
229     printBlanks = "                ";
230     printFormat = "%016" PRIx64;
231   } else {
232     printBlanks = "        ";
233     printFormat = "%08" PRIx64;
234   }
235
236   for (SymbolListT::iterator I = SymbolList.begin(), E = SymbolList.end();
237        I != E; ++I) {
238     if ((I->TypeChar != 'U') && UndefinedOnly)
239       continue;
240     if ((I->TypeChar == 'U') && DefinedOnly)
241       continue;
242     if (SizeSort && !PrintAddress && I->Size == UnknownAddressOrSize)
243       continue;
244
245     char SymbolAddrStr[18] = "";
246     char SymbolSizeStr[18] = "";
247
248     if (OutputFormat == sysv || I->Address == UnknownAddressOrSize)
249       strcpy(SymbolAddrStr, printBlanks);
250     if (OutputFormat == sysv)
251       strcpy(SymbolSizeStr, printBlanks);
252
253     if (I->Address != UnknownAddressOrSize)
254       format(printFormat, I->Address)
255           .print(SymbolAddrStr, sizeof(SymbolAddrStr));
256     if (I->Size != UnknownAddressOrSize)
257       format(printFormat, I->Size).print(SymbolSizeStr, sizeof(SymbolSizeStr));
258
259     if (OutputFormat == posix) {
260       outs() << I->Name << " " << I->TypeChar << " " << SymbolAddrStr
261              << SymbolSizeStr << "\n";
262     } else if (OutputFormat == bsd) {
263       if (PrintAddress)
264         outs() << SymbolAddrStr << ' ';
265       if (PrintSize) {
266         outs() << SymbolSizeStr;
267         if (I->Size != UnknownAddressOrSize)
268           outs() << ' ';
269       }
270       outs() << I->TypeChar << " " << I->Name << "\n";
271     } else if (OutputFormat == sysv) {
272       std::string PaddedName(I->Name);
273       while (PaddedName.length() < 20)
274         PaddedName += " ";
275       outs() << PaddedName << "|" << SymbolAddrStr << "|   " << I->TypeChar
276              << "  |                  |" << SymbolSizeStr << "|     |\n";
277     }
278   }
279
280   SymbolList.clear();
281 }
282
283 template <class ELFT>
284 static char getSymbolNMTypeChar(ELFObjectFile<ELFT> &Obj,
285                                 basic_symbol_iterator I) {
286   typedef typename ELFObjectFile<ELFT>::Elf_Sym Elf_Sym;
287   typedef typename ELFObjectFile<ELFT>::Elf_Shdr Elf_Shdr;
288
289   // OK, this is ELF
290   symbol_iterator SymI(I);
291
292   DataRefImpl Symb = I->getRawDataRefImpl();
293   const Elf_Sym *ESym = Obj.getSymbol(Symb);
294   const ELFFile<ELFT> &EF = *Obj.getELFFile();
295   const Elf_Shdr *ESec = EF.getSection(ESym);
296
297   if (ESec) {
298     switch (ESec->sh_type) {
299     case ELF::SHT_PROGBITS:
300     case ELF::SHT_DYNAMIC:
301       switch (ESec->sh_flags) {
302       case(ELF::SHF_ALLOC | ELF::SHF_EXECINSTR) :
303         return 't';
304       case(ELF::SHF_TLS | ELF::SHF_ALLOC | ELF::SHF_WRITE) :
305       case(ELF::SHF_ALLOC | ELF::SHF_WRITE) :
306         return 'd';
307       case ELF::SHF_ALLOC:
308       case(ELF::SHF_ALLOC | ELF::SHF_MERGE) :
309       case(ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS) :
310         return 'r';
311       }
312       break;
313     case ELF::SHT_NOBITS:
314       return 'b';
315     }
316   }
317
318   if (ESym->getType() == ELF::STT_SECTION) {
319     StringRef Name;
320     if (error(SymI->getName(Name)))
321       return '?';
322     return StringSwitch<char>(Name)
323         .StartsWith(".debug", 'N')
324         .StartsWith(".note", 'n')
325         .Default('?');
326   }
327
328   return '?';
329 }
330
331 static char getSymbolNMTypeChar(COFFObjectFile &Obj, symbol_iterator I) {
332   const coff_symbol *Symb = Obj.getCOFFSymbol(*I);
333   // OK, this is COFF.
334   symbol_iterator SymI(I);
335
336   StringRef Name;
337   if (error(SymI->getName(Name)))
338     return '?';
339
340   char Ret = StringSwitch<char>(Name)
341                  .StartsWith(".debug", 'N')
342                  .StartsWith(".sxdata", 'N')
343                  .Default('?');
344
345   if (Ret != '?')
346     return Ret;
347
348   uint32_t Characteristics = 0;
349   if (!COFF::isReservedSectionNumber(Symb->SectionNumber)) {
350     section_iterator SecI = Obj.section_end();
351     if (error(SymI->getSection(SecI)))
352       return '?';
353     const coff_section *Section = Obj.getCOFFSection(*SecI);
354     Characteristics = Section->Characteristics;
355   }
356
357   switch (Symb->SectionNumber) {
358   case COFF::IMAGE_SYM_DEBUG:
359     return 'n';
360   default:
361     // Check section type.
362     if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
363       return 't';
364     else if (Characteristics & COFF::IMAGE_SCN_MEM_READ &&
365              ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
366       return 'r';
367     else if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
368       return 'd';
369     else if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
370       return 'b';
371     else if (Characteristics & COFF::IMAGE_SCN_LNK_INFO)
372       return 'i';
373
374     // Check for section symbol.
375     else if (Symb->isSectionDefinition())
376       return 's';
377   }
378
379   return '?';
380 }
381
382 static uint8_t getNType(MachOObjectFile &Obj, DataRefImpl Symb) {
383   if (Obj.is64Bit()) {
384     MachO::nlist_64 STE = Obj.getSymbol64TableEntry(Symb);
385     return STE.n_type;
386   }
387   MachO::nlist STE = Obj.getSymbolTableEntry(Symb);
388   return STE.n_type;
389 }
390
391 static char getSymbolNMTypeChar(MachOObjectFile &Obj, basic_symbol_iterator I) {
392   DataRefImpl Symb = I->getRawDataRefImpl();
393   uint8_t NType = getNType(Obj, Symb);
394
395   switch (NType & MachO::N_TYPE) {
396   case MachO::N_ABS:
397     return 's';
398   case MachO::N_SECT: {
399     section_iterator Sec = Obj.section_end();
400     Obj.getSymbolSection(Symb, Sec);
401     DataRefImpl Ref = Sec->getRawDataRefImpl();
402     StringRef SectionName;
403     Obj.getSectionName(Ref, SectionName);
404     StringRef SegmentName = Obj.getSectionFinalSegmentName(Ref);
405     if (SegmentName == "__TEXT" && SectionName == "__text")
406       return 't';
407     else
408       return 's';
409   }
410   }
411
412   return '?';
413 }
414
415 static char getSymbolNMTypeChar(const GlobalValue &GV) {
416   if (isa<Function>(GV))
417     return 't';
418   // FIXME: should we print 'b'? At the IR level we cannot be sure if this
419   // will be in bss or not, but we could approximate.
420   if (isa<GlobalVariable>(GV))
421     return 'd';
422   const GlobalAlias *GA = cast<GlobalAlias>(&GV);
423   const GlobalValue *AliasedGV = GA->getAliasedGlobal();
424   return getSymbolNMTypeChar(*AliasedGV);
425 }
426
427 static char getSymbolNMTypeChar(IRObjectFile &Obj, basic_symbol_iterator I) {
428   const GlobalValue &GV = Obj.getSymbolGV(I->getRawDataRefImpl());
429   return getSymbolNMTypeChar(GV);
430 }
431
432 template <class ELFT>
433 static bool isObject(ELFObjectFile<ELFT> &Obj, symbol_iterator I) {
434   typedef typename ELFObjectFile<ELFT>::Elf_Sym Elf_Sym;
435
436   DataRefImpl Symb = I->getRawDataRefImpl();
437   const Elf_Sym *ESym = Obj.getSymbol(Symb);
438
439   return ESym->getType() == ELF::STT_OBJECT;
440 }
441
442 static bool isObject(SymbolicFile *Obj, basic_symbol_iterator I) {
443   if (ELF32LEObjectFile *ELF = dyn_cast<ELF32LEObjectFile>(Obj))
444     return isObject(*ELF, I);
445   if (ELF64LEObjectFile *ELF = dyn_cast<ELF64LEObjectFile>(Obj))
446     return isObject(*ELF, I);
447   if (ELF32BEObjectFile *ELF = dyn_cast<ELF32BEObjectFile>(Obj))
448     return isObject(*ELF, I);
449   if (ELF64BEObjectFile *ELF = dyn_cast<ELF64BEObjectFile>(Obj))
450     return isObject(*ELF, I);
451   return false;
452 }
453
454 static char getNMTypeChar(SymbolicFile *Obj, basic_symbol_iterator I) {
455   uint32_t Symflags = I->getFlags();
456   if ((Symflags & object::SymbolRef::SF_Weak) && !isa<MachOObjectFile>(Obj)) {
457     char Ret = isObject(Obj, I) ? 'v' : 'w';
458     if (!(Symflags & object::SymbolRef::SF_Undefined))
459       Ret = toupper(Ret);
460     return Ret;
461   }
462
463   if (Symflags & object::SymbolRef::SF_Undefined)
464     return 'U';
465
466   if (Symflags & object::SymbolRef::SF_Common)
467     return 'C';
468
469   char Ret = '?';
470   if (Symflags & object::SymbolRef::SF_Absolute)
471     Ret = 'a';
472   else if (IRObjectFile *IR = dyn_cast<IRObjectFile>(Obj))
473     Ret = getSymbolNMTypeChar(*IR, I);
474   else if (COFFObjectFile *COFF = dyn_cast<COFFObjectFile>(Obj))
475     Ret = getSymbolNMTypeChar(*COFF, I);
476   else if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(Obj))
477     Ret = getSymbolNMTypeChar(*MachO, I);
478   else if (ELF32LEObjectFile *ELF = dyn_cast<ELF32LEObjectFile>(Obj))
479     Ret = getSymbolNMTypeChar(*ELF, I);
480   else if (ELF64LEObjectFile *ELF = dyn_cast<ELF64LEObjectFile>(Obj))
481     Ret = getSymbolNMTypeChar(*ELF, I);
482   else if (ELF32BEObjectFile *ELF = dyn_cast<ELF32BEObjectFile>(Obj))
483     Ret = getSymbolNMTypeChar(*ELF, I);
484   else
485     Ret = getSymbolNMTypeChar(*cast<ELF64BEObjectFile>(Obj), I);
486
487   if (Symflags & object::SymbolRef::SF_Global)
488     Ret = toupper(Ret);
489
490   return Ret;
491 }
492
493 static void dumpSymbolNamesFromObject(SymbolicFile *Obj) {
494   basic_symbol_iterator IBegin = Obj->symbol_begin();
495   basic_symbol_iterator IEnd = Obj->symbol_end();
496   if (DynamicSyms) {
497     if (!Obj->isELF()) {
498       error("File format has no dynamic symbol table", Obj->getFileName());
499       return;
500     }
501     std::pair<symbol_iterator, symbol_iterator> IDyn =
502         getELFDynamicSymbolIterators(Obj);
503     IBegin = IDyn.first;
504     IEnd = IDyn.second;
505   }
506   std::string NameBuffer;
507   raw_string_ostream OS(NameBuffer);
508   for (basic_symbol_iterator I = IBegin; I != IEnd; ++I) {
509     uint32_t SymFlags = I->getFlags();
510     if (!DebugSyms && (SymFlags & SymbolRef::SF_FormatSpecific))
511       continue;
512     if (WithoutAliases) {
513       if (IRObjectFile *IR = dyn_cast<IRObjectFile>(Obj)) {
514         const GlobalValue &GV = IR->getSymbolGV(I->getRawDataRefImpl());
515         if(isa<GlobalAlias>(GV))
516           continue;
517       }
518     }
519     NMSymbol S;
520     S.Size = UnknownAddressOrSize;
521     S.Address = UnknownAddressOrSize;
522     if ((PrintSize || SizeSort) && isa<ObjectFile>(Obj)) {
523       symbol_iterator SymI = I;
524       if (error(SymI->getSize(S.Size)))
525         break;
526     }
527     if (PrintAddress && isa<ObjectFile>(Obj))
528       if (error(symbol_iterator(I)->getAddress(S.Address)))
529         break;
530     S.TypeChar = getNMTypeChar(Obj, I);
531     if (error(I->printName(OS)))
532       break;
533     OS << '\0';
534     SymbolList.push_back(S);
535   }
536
537   OS.flush();
538   const char *P = NameBuffer.c_str();
539   for (unsigned I = 0; I < SymbolList.size(); ++I) {
540     SymbolList[I].Name = P;
541     P += strlen(P) + 1;
542   }
543
544   CurrentFilename = Obj->getFileName();
545   sortAndPrintSymbolList(Obj);
546 }
547
548 static void dumpSymbolNamesFromFile(std::string &Filename) {
549   std::unique_ptr<MemoryBuffer> Buffer;
550   if (error(MemoryBuffer::getFileOrSTDIN(Filename, Buffer), Filename))
551     return;
552
553   LLVMContext &Context = getGlobalContext();
554   ErrorOr<Binary *> BinaryOrErr = createBinary(Buffer.release(), &Context);
555   if (error(BinaryOrErr.getError(), Filename))
556     return;
557   std::unique_ptr<Binary> Bin(BinaryOrErr.get());
558
559   if (Archive *A = dyn_cast<Archive>(Bin.get())) {
560     if (ArchiveMap) {
561       Archive::symbol_iterator I = A->symbol_begin();
562       Archive::symbol_iterator E = A->symbol_end();
563       if (I != E) {
564         outs() << "Archive map\n";
565         for (; I != E; ++I) {
566           Archive::child_iterator C;
567           StringRef SymName;
568           StringRef FileName;
569           if (error(I->getMember(C)))
570             return;
571           if (error(I->getName(SymName)))
572             return;
573           if (error(C->getName(FileName)))
574             return;
575           outs() << SymName << " in " << FileName << "\n";
576         }
577         outs() << "\n";
578       }
579     }
580
581     for (Archive::child_iterator I = A->child_begin(), E = A->child_end();
582          I != E; ++I) {
583       std::unique_ptr<Binary> Child;
584       if (I->getAsBinary(Child, &Context))
585         continue;
586       if (SymbolicFile *O = dyn_cast<SymbolicFile>(Child.get())) {
587         outs() << O->getFileName() << ":\n";
588         dumpSymbolNamesFromObject(O);
589       }
590     }
591     return;
592   }
593   if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(Bin.get())) {
594     for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
595                                                E = UB->end_objects();
596          I != E; ++I) {
597       std::unique_ptr<ObjectFile> Obj;
598       if (!I->getAsObjectFile(Obj)) {
599         outs() << Obj->getFileName() << ":\n";
600         dumpSymbolNamesFromObject(Obj.get());
601       }
602     }
603     return;
604   }
605   if (SymbolicFile *O = dyn_cast<SymbolicFile>(Bin.get())) {
606     dumpSymbolNamesFromObject(O);
607     return;
608   }
609   error("unrecognizable file type", Filename);
610   return;
611 }
612
613 int main(int argc, char **argv) {
614   // Print a stack trace if we signal out.
615   sys::PrintStackTraceOnErrorSignal();
616   PrettyStackTraceProgram X(argc, argv);
617
618   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
619   cl::ParseCommandLineOptions(argc, argv, "llvm symbol table dumper\n");
620
621   // llvm-nm only reads binary files.
622   if (error(sys::ChangeStdinToBinary()))
623     return 1;
624
625   ToolName = argv[0];
626   if (BSDFormat)
627     OutputFormat = bsd;
628   if (POSIXFormat)
629     OutputFormat = posix;
630
631   // The relative order of these is important. If you pass --size-sort it should
632   // only print out the size. However, if you pass -S --size-sort, it should
633   // print out both the size and address.
634   if (SizeSort && !PrintSize)
635     PrintAddress = false;
636   if (OutputFormat == sysv || SizeSort)
637     PrintSize = true;
638
639   switch (InputFilenames.size()) {
640   case 0:
641     InputFilenames.push_back("-");
642   case 1:
643     break;
644   default:
645     MultipleFiles = true;
646   }
647
648   std::for_each(InputFilenames.begin(), InputFilenames.end(),
649                 dumpSymbolNamesFromFile);
650
651   if (HadError)
652     return 1;
653
654   return 0;
655 }