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