Allow alias to point to an arbitrary ConstantExpr.
[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_INDR:
399     return 'i';
400   case MachO::N_SECT: {
401     section_iterator Sec = Obj.section_end();
402     Obj.getSymbolSection(Symb, Sec);
403     DataRefImpl Ref = Sec->getRawDataRefImpl();
404     StringRef SectionName;
405     Obj.getSectionName(Ref, SectionName);
406     StringRef SegmentName = Obj.getSectionFinalSegmentName(Ref);
407     if (SegmentName == "__TEXT" && SectionName == "__text")
408       return 't';
409     else
410       return 's';
411   }
412   }
413
414   return '?';
415 }
416
417 static char getSymbolNMTypeChar(const GlobalValue &GV) {
418   if (GV.getType()->getElementType()->isFunctionTy())
419     return 't';
420   // FIXME: should we print 'b'? At the IR level we cannot be sure if this
421   // will be in bss or not, but we could approximate.
422   return 'd';
423 }
424
425 static char getSymbolNMTypeChar(IRObjectFile &Obj, basic_symbol_iterator I) {
426   const GlobalValue &GV = Obj.getSymbolGV(I->getRawDataRefImpl());
427   return getSymbolNMTypeChar(GV);
428 }
429
430 template <class ELFT>
431 static bool isObject(ELFObjectFile<ELFT> &Obj, symbol_iterator I) {
432   typedef typename ELFObjectFile<ELFT>::Elf_Sym Elf_Sym;
433
434   DataRefImpl Symb = I->getRawDataRefImpl();
435   const Elf_Sym *ESym = Obj.getSymbol(Symb);
436
437   return ESym->getType() == ELF::STT_OBJECT;
438 }
439
440 static bool isObject(SymbolicFile *Obj, basic_symbol_iterator I) {
441   if (ELF32LEObjectFile *ELF = dyn_cast<ELF32LEObjectFile>(Obj))
442     return isObject(*ELF, I);
443   if (ELF64LEObjectFile *ELF = dyn_cast<ELF64LEObjectFile>(Obj))
444     return isObject(*ELF, I);
445   if (ELF32BEObjectFile *ELF = dyn_cast<ELF32BEObjectFile>(Obj))
446     return isObject(*ELF, I);
447   if (ELF64BEObjectFile *ELF = dyn_cast<ELF64BEObjectFile>(Obj))
448     return isObject(*ELF, I);
449   return false;
450 }
451
452 static char getNMTypeChar(SymbolicFile *Obj, basic_symbol_iterator I) {
453   uint32_t Symflags = I->getFlags();
454   if ((Symflags & object::SymbolRef::SF_Weak) && !isa<MachOObjectFile>(Obj)) {
455     char Ret = isObject(Obj, I) ? 'v' : 'w';
456     if (!(Symflags & object::SymbolRef::SF_Undefined))
457       Ret = toupper(Ret);
458     return Ret;
459   }
460
461   if (Symflags & object::SymbolRef::SF_Undefined)
462     return 'U';
463
464   if (Symflags & object::SymbolRef::SF_Common)
465     return 'C';
466
467   char Ret = '?';
468   if (Symflags & object::SymbolRef::SF_Absolute)
469     Ret = 'a';
470   else if (IRObjectFile *IR = dyn_cast<IRObjectFile>(Obj))
471     Ret = getSymbolNMTypeChar(*IR, I);
472   else if (COFFObjectFile *COFF = dyn_cast<COFFObjectFile>(Obj))
473     Ret = getSymbolNMTypeChar(*COFF, I);
474   else if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(Obj))
475     Ret = getSymbolNMTypeChar(*MachO, I);
476   else if (ELF32LEObjectFile *ELF = dyn_cast<ELF32LEObjectFile>(Obj))
477     Ret = getSymbolNMTypeChar(*ELF, I);
478   else if (ELF64LEObjectFile *ELF = dyn_cast<ELF64LEObjectFile>(Obj))
479     Ret = getSymbolNMTypeChar(*ELF, I);
480   else if (ELF32BEObjectFile *ELF = dyn_cast<ELF32BEObjectFile>(Obj))
481     Ret = getSymbolNMTypeChar(*ELF, I);
482   else
483     Ret = getSymbolNMTypeChar(*cast<ELF64BEObjectFile>(Obj), I);
484
485   if (Symflags & object::SymbolRef::SF_Global)
486     Ret = toupper(Ret);
487
488   return Ret;
489 }
490
491 static void dumpSymbolNamesFromObject(SymbolicFile *Obj) {
492   basic_symbol_iterator IBegin = Obj->symbol_begin();
493   basic_symbol_iterator IEnd = Obj->symbol_end();
494   if (DynamicSyms) {
495     if (!Obj->isELF()) {
496       error("File format has no dynamic symbol table", Obj->getFileName());
497       return;
498     }
499     std::pair<symbol_iterator, symbol_iterator> IDyn =
500         getELFDynamicSymbolIterators(Obj);
501     IBegin = IDyn.first;
502     IEnd = IDyn.second;
503   }
504   std::string NameBuffer;
505   raw_string_ostream OS(NameBuffer);
506   for (basic_symbol_iterator I = IBegin; I != IEnd; ++I) {
507     uint32_t SymFlags = I->getFlags();
508     if (!DebugSyms && (SymFlags & SymbolRef::SF_FormatSpecific))
509       continue;
510     if (WithoutAliases) {
511       if (IRObjectFile *IR = dyn_cast<IRObjectFile>(Obj)) {
512         const GlobalValue &GV = IR->getSymbolGV(I->getRawDataRefImpl());
513         if(isa<GlobalAlias>(GV))
514           continue;
515       }
516     }
517     NMSymbol S;
518     S.Size = UnknownAddressOrSize;
519     S.Address = UnknownAddressOrSize;
520     if ((PrintSize || SizeSort) && isa<ObjectFile>(Obj)) {
521       symbol_iterator SymI = I;
522       if (error(SymI->getSize(S.Size)))
523         break;
524     }
525     if (PrintAddress && isa<ObjectFile>(Obj))
526       if (error(symbol_iterator(I)->getAddress(S.Address)))
527         break;
528     S.TypeChar = getNMTypeChar(Obj, I);
529     if (error(I->printName(OS)))
530       break;
531     OS << '\0';
532     SymbolList.push_back(S);
533   }
534
535   OS.flush();
536   const char *P = NameBuffer.c_str();
537   for (unsigned I = 0; I < SymbolList.size(); ++I) {
538     SymbolList[I].Name = P;
539     P += strlen(P) + 1;
540   }
541
542   CurrentFilename = Obj->getFileName();
543   sortAndPrintSymbolList(Obj);
544 }
545
546 static void dumpSymbolNamesFromFile(std::string &Filename) {
547   std::unique_ptr<MemoryBuffer> Buffer;
548   if (error(MemoryBuffer::getFileOrSTDIN(Filename, Buffer), Filename))
549     return;
550
551   LLVMContext &Context = getGlobalContext();
552   ErrorOr<Binary *> BinaryOrErr = createBinary(Buffer.release(), &Context);
553   if (error(BinaryOrErr.getError(), Filename))
554     return;
555   std::unique_ptr<Binary> Bin(BinaryOrErr.get());
556
557   if (Archive *A = dyn_cast<Archive>(Bin.get())) {
558     if (ArchiveMap) {
559       Archive::symbol_iterator I = A->symbol_begin();
560       Archive::symbol_iterator E = A->symbol_end();
561       if (I != E) {
562         outs() << "Archive map\n";
563         for (; I != E; ++I) {
564           Archive::child_iterator C;
565           StringRef SymName;
566           StringRef FileName;
567           if (error(I->getMember(C)))
568             return;
569           if (error(I->getName(SymName)))
570             return;
571           if (error(C->getName(FileName)))
572             return;
573           outs() << SymName << " in " << FileName << "\n";
574         }
575         outs() << "\n";
576       }
577     }
578
579     for (Archive::child_iterator I = A->child_begin(), E = A->child_end();
580          I != E; ++I) {
581       std::unique_ptr<Binary> Child;
582       if (I->getAsBinary(Child, &Context))
583         continue;
584       if (SymbolicFile *O = dyn_cast<SymbolicFile>(Child.get())) {
585         outs() << O->getFileName() << ":\n";
586         dumpSymbolNamesFromObject(O);
587       }
588     }
589     return;
590   }
591   if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(Bin.get())) {
592     for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
593                                                E = UB->end_objects();
594          I != E; ++I) {
595       std::unique_ptr<ObjectFile> Obj;
596       std::unique_ptr<Archive> A;
597       if (!I->getAsObjectFile(Obj)) {
598         outs() << Obj->getFileName() << ":\n";
599         dumpSymbolNamesFromObject(Obj.get());
600       }
601       else if (!I->getAsArchive(A)) {
602         for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end();
603              AI != AE; ++AI) {
604           std::unique_ptr<Binary> Child;
605           if (AI->getAsBinary(Child, &Context))
606             continue;
607           if (SymbolicFile *O = dyn_cast<SymbolicFile>(Child.get())) {
608             outs() << A->getFileName() << ":";
609             outs() << O->getFileName() << ":\n";
610             dumpSymbolNamesFromObject(O);
611           }
612         }
613       }
614     }
615     return;
616   }
617   if (SymbolicFile *O = dyn_cast<SymbolicFile>(Bin.get())) {
618     dumpSymbolNamesFromObject(O);
619     return;
620   }
621   error("unrecognizable file type", Filename);
622   return;
623 }
624
625 int main(int argc, char **argv) {
626   // Print a stack trace if we signal out.
627   sys::PrintStackTraceOnErrorSignal();
628   PrettyStackTraceProgram X(argc, argv);
629
630   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
631   cl::ParseCommandLineOptions(argc, argv, "llvm symbol table dumper\n");
632
633   // llvm-nm only reads binary files.
634   if (error(sys::ChangeStdinToBinary()))
635     return 1;
636
637   ToolName = argv[0];
638   if (BSDFormat)
639     OutputFormat = bsd;
640   if (POSIXFormat)
641     OutputFormat = posix;
642
643   // The relative order of these is important. If you pass --size-sort it should
644   // only print out the size. However, if you pass -S --size-sort, it should
645   // print out both the size and address.
646   if (SizeSort && !PrintSize)
647     PrintAddress = false;
648   if (OutputFormat == sysv || SizeSort)
649     PrintSize = true;
650
651   switch (InputFilenames.size()) {
652   case 0:
653     InputFilenames.push_back("-");
654   case 1:
655     break;
656   default:
657     MultipleFiles = true;
658   }
659
660   std::for_each(InputFilenames.begin(), InputFilenames.end(),
661                 dumpSymbolNamesFromFile);
662
663   if (HadError)
664     return 1;
665
666   return 0;
667 }