Don't crash in 'llvm -s' when an archive has no symtab.
[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/LLVMContext.h"
20 #include "llvm/Bitcode/ReaderWriter.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/Object/Archive.h"
23 #include "llvm/Object/MachOUniversal.h"
24 #include "llvm/Object/ObjectFile.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/FileSystem.h"
27 #include "llvm/Support/Format.h"
28 #include "llvm/Support/ManagedStatic.h"
29 #include "llvm/Support/MemoryBuffer.h"
30 #include "llvm/Support/PrettyStackTrace.h"
31 #include "llvm/Support/Program.h"
32 #include "llvm/Support/Signals.h"
33 #include "llvm/Support/raw_ostream.h"
34 #include "llvm/Support/system_error.h"
35 #include <algorithm>
36 #include <cctype>
37 #include <cerrno>
38 #include <cstring>
39 #include <vector>
40 using namespace llvm;
41 using namespace object;
42
43 namespace {
44   enum OutputFormatTy { bsd, sysv, posix };
45   cl::opt<OutputFormatTy>
46   OutputFormat("format",
47        cl::desc("Specify output format"),
48          cl::values(clEnumVal(bsd,   "BSD format"),
49                     clEnumVal(sysv,  "System V format"),
50                     clEnumVal(posix, "POSIX.2 format"),
51                     clEnumValEnd), cl::init(bsd));
52   cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
53                           cl::aliasopt(OutputFormat));
54
55   cl::list<std::string>
56   InputFilenames(cl::Positional, cl::desc("<input bitcode files>"),
57                  cl::ZeroOrMore);
58
59   cl::opt<bool> UndefinedOnly("undefined-only",
60                               cl::desc("Show only undefined symbols"));
61   cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
62                            cl::aliasopt(UndefinedOnly));
63
64   cl::opt<bool> DynamicSyms("dynamic",
65                              cl::desc("Display the dynamic symbols instead "
66                                       "of normal symbols."));
67   cl::alias DynamicSyms2("D", cl::desc("Alias for --dynamic"),
68                          cl::aliasopt(DynamicSyms));
69
70   cl::opt<bool> DefinedOnly("defined-only",
71                             cl::desc("Show only defined symbols"));
72
73   cl::opt<bool> ExternalOnly("extern-only",
74                              cl::desc("Show only external symbols"));
75   cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
76                           cl::aliasopt(ExternalOnly));
77
78   cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
79   cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
80
81   cl::opt<bool> PrintFileName("print-file-name",
82     cl::desc("Precede each symbol with the object file it came from"));
83
84   cl::alias PrintFileNameA("A", cl::desc("Alias for --print-file-name"),
85                                 cl::aliasopt(PrintFileName));
86   cl::alias PrintFileNameo("o", cl::desc("Alias for --print-file-name"),
87                                 cl::aliasopt(PrintFileName));
88
89   cl::opt<bool> DebugSyms("debug-syms",
90     cl::desc("Show all symbols, even debugger only"));
91   cl::alias DebugSymsa("a", cl::desc("Alias for --debug-syms"),
92                             cl::aliasopt(DebugSyms));
93
94   cl::opt<bool> NumericSort("numeric-sort",
95     cl::desc("Sort symbols by address"));
96   cl::alias NumericSortn("n", cl::desc("Alias for --numeric-sort"),
97                               cl::aliasopt(NumericSort));
98   cl::alias NumericSortv("v", cl::desc("Alias for --numeric-sort"),
99                               cl::aliasopt(NumericSort));
100
101   cl::opt<bool> NoSort("no-sort",
102     cl::desc("Show symbols in order encountered"));
103   cl::alias NoSortp("p", cl::desc("Alias for --no-sort"),
104                          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",
117     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
130 static void error(Twine message, Twine path = Twine()) {
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     HadError = true;
138     return true;
139   }
140   return false;
141 }
142
143 namespace {
144   struct NMSymbol {
145     uint64_t  Address;
146     uint64_t  Size;
147     char      TypeChar;
148     StringRef Name;
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
163   static bool CompareSymbolSize(const NMSymbol &a, const NMSymbol &b) {
164     if (a.Size < b.Size)
165       return true;
166     else if (a.Size == b.Size && a.Name < b.Name)
167       return true;
168     else if (a.Size == b.Size && a.Name == b.Name && a.Address < b.Address)
169       return true;
170     else
171       return false;
172   }
173
174   static bool CompareSymbolName(const NMSymbol &a, const NMSymbol &b) {
175     if (a.Name < b.Name)
176       return true;
177     else if (a.Name == b.Name && a.Size < b.Size)
178       return true;
179     else if (a.Name == b.Name && a.Size == b.Size && a.Address < b.Address)
180       return true;
181     else
182       return false;
183   }
184
185   StringRef CurrentFilename;
186   typedef std::vector<NMSymbol> SymbolListT;
187   SymbolListT SymbolList;
188 }
189
190 static void SortAndPrintSymbolList() {
191   if (!NoSort) {
192     if (NumericSort)
193       std::sort(SymbolList.begin(), SymbolList.end(), CompareSymbolAddress);
194     else if (SizeSort)
195       std::sort(SymbolList.begin(), SymbolList.end(), CompareSymbolSize);
196     else
197       std::sort(SymbolList.begin(), SymbolList.end(), CompareSymbolName);
198   }
199
200   if (OutputFormat == posix && MultipleFiles) {
201     outs() << '\n' << CurrentFilename << ":\n";
202   } else if (OutputFormat == bsd && MultipleFiles) {
203     outs() << "\n" << CurrentFilename << ":\n";
204   } else if (OutputFormat == sysv) {
205     outs() << "\n\nSymbols from " << CurrentFilename << ":\n\n"
206            << "Name                  Value   Class        Type"
207            << "         Size   Line  Section\n";
208   }
209
210   for (SymbolListT::iterator i = SymbolList.begin(),
211                              e = SymbolList.end(); i != e; ++i) {
212     if ((i->TypeChar != 'U') && UndefinedOnly)
213       continue;
214     if ((i->TypeChar == 'U') && DefinedOnly)
215       continue;
216     if (SizeSort && !PrintAddress && i->Size == UnknownAddressOrSize)
217       continue;
218
219     char SymbolAddrStr[10] = "";
220     char SymbolSizeStr[10] = "";
221
222     if (OutputFormat == sysv || i->Address == object::UnknownAddressOrSize)
223       strcpy(SymbolAddrStr, "        ");
224     if (OutputFormat == sysv)
225       strcpy(SymbolSizeStr, "        ");
226
227     if (i->Address != object::UnknownAddressOrSize)
228       format("%08" PRIx64, i->Address).print(SymbolAddrStr,
229                                              sizeof(SymbolAddrStr));
230     if (i->Size != object::UnknownAddressOrSize)
231       format("%08" PRIx64, i->Size).print(SymbolSizeStr, sizeof(SymbolSizeStr));
232
233     if (OutputFormat == posix) {
234       outs() << i->Name << " " << i->TypeChar << " "
235              << SymbolAddrStr << SymbolSizeStr << "\n";
236     } else if (OutputFormat == bsd) {
237       if (PrintAddress)
238         outs() << SymbolAddrStr << ' ';
239       if (PrintSize) {
240         outs() << SymbolSizeStr;
241         if (i->Size != object::UnknownAddressOrSize)
242           outs() << ' ';
243       }
244       outs() << i->TypeChar << " " << i->Name  << "\n";
245     } else if (OutputFormat == sysv) {
246       std::string PaddedName (i->Name);
247       while (PaddedName.length () < 20)
248         PaddedName += " ";
249       outs() << PaddedName << "|" << SymbolAddrStr << "|   "
250              << i->TypeChar
251              << "  |                  |" << SymbolSizeStr << "|     |\n";
252     }
253   }
254
255   SymbolList.clear();
256 }
257
258 static char TypeCharForSymbol(GlobalValue &GV) {
259   if (GV.isDeclaration())                                  return 'U';
260   if (GV.hasLinkOnceLinkage())                             return 'C';
261   if (GV.hasCommonLinkage())                               return 'C';
262   if (GV.hasWeakLinkage())                                 return 'W';
263   if (isa<Function>(GV) && GV.hasInternalLinkage())        return 't';
264   if (isa<Function>(GV))                                   return 'T';
265   if (isa<GlobalVariable>(GV) && GV.hasInternalLinkage())  return 'd';
266   if (isa<GlobalVariable>(GV))                             return 'D';
267   if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(&GV)) {
268     const GlobalValue *AliasedGV = GA->getAliasedGlobal();
269     if (isa<Function>(AliasedGV))                          return 'T';
270     if (isa<GlobalVariable>(AliasedGV))                    return 'D';
271   }
272                                                            return '?';
273 }
274
275 static void DumpSymbolNameForGlobalValue(GlobalValue &GV) {
276   // Private linkage and available_externally linkage don't exist in symtab.
277   if (GV.hasPrivateLinkage() ||
278       GV.hasLinkerPrivateLinkage() ||
279       GV.hasLinkerPrivateWeakLinkage() ||
280       GV.hasAvailableExternallyLinkage())
281     return;
282   char TypeChar = TypeCharForSymbol(GV);
283   if (GV.hasLocalLinkage () && ExternalOnly)
284     return;
285
286   NMSymbol s;
287   s.Address = object::UnknownAddressOrSize;
288   s.Size = object::UnknownAddressOrSize;
289   s.TypeChar = TypeChar;
290   s.Name     = GV.getName();
291   SymbolList.push_back(s);
292 }
293
294 static void DumpSymbolNamesFromModule(Module *M) {
295   CurrentFilename = M->getModuleIdentifier();
296   std::for_each (M->begin(), M->end(), DumpSymbolNameForGlobalValue);
297   std::for_each (M->global_begin(), M->global_end(),
298                  DumpSymbolNameForGlobalValue);
299   if (!WithoutAliases)
300     std::for_each (M->alias_begin(), M->alias_end(),
301                    DumpSymbolNameForGlobalValue);
302
303   SortAndPrintSymbolList();
304 }
305
306 static void DumpSymbolNamesFromObject(ObjectFile *obj) {
307   error_code ec;
308   symbol_iterator ibegin = obj->begin_symbols();
309   symbol_iterator iend = obj->end_symbols();
310   if (DynamicSyms) {
311     ibegin = obj->begin_dynamic_symbols();
312     iend = obj->end_dynamic_symbols();
313   }
314   for (symbol_iterator i = ibegin; i != iend; i.increment(ec)) {
315     if (error(ec)) break;
316     uint32_t symflags;
317     if (error(i->getFlags(symflags))) break;
318     if (!DebugSyms && (symflags & SymbolRef::SF_FormatSpecific))
319       continue;
320     NMSymbol s;
321     s.Size = object::UnknownAddressOrSize;
322     s.Address = object::UnknownAddressOrSize;
323     if (PrintSize || SizeSort) {
324       if (error(i->getSize(s.Size))) break;
325     }
326     if (PrintAddress)
327       if (error(i->getAddress(s.Address))) break;
328     if (error(i->getNMTypeChar(s.TypeChar))) break;
329     if (error(i->getName(s.Name))) break;
330     SymbolList.push_back(s);
331   }
332
333   CurrentFilename = obj->getFileName();
334   SortAndPrintSymbolList();
335 }
336
337 static void DumpSymbolNamesFromFile(std::string &Filename) {
338   if (Filename != "-" && !sys::fs::exists(Filename)) {
339     errs() << ToolName << ": '" << Filename << "': " << "No such file\n";
340     return;
341   }
342
343   OwningPtr<MemoryBuffer> Buffer;
344   if (error(MemoryBuffer::getFileOrSTDIN(Filename, Buffer), Filename))
345     return;
346
347   sys::fs::file_magic magic = sys::fs::identify_magic(Buffer->getBuffer());
348
349   LLVMContext &Context = getGlobalContext();
350   std::string ErrorMessage;
351   if (magic == sys::fs::file_magic::bitcode) {
352     Module *Result = 0;
353     Result = ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage);
354     if (Result) {
355       DumpSymbolNamesFromModule(Result);
356       delete Result;
357     } else {
358       error(ErrorMessage, Filename);
359       return;
360     }
361   } else if (magic == sys::fs::file_magic::archive) {
362     OwningPtr<Binary> arch;
363     if (error(object::createBinary(Buffer.take(), arch), Filename))
364       return;
365
366     if (object::Archive *a = dyn_cast<object::Archive>(arch.get())) {
367       if (ArchiveMap) {
368         object::Archive::symbol_iterator I = a->begin_symbols();
369         object::Archive::symbol_iterator E = a->end_symbols();
370         if (I !=E) {
371           outs() << "Archive map" << "\n";
372           for (; I != E; ++I) {
373             object::Archive::child_iterator c;
374             StringRef symname;
375             StringRef filename;
376             if (error(I->getMember(c)))
377               return;
378             if (error(I->getName(symname)))
379               return;
380             if (error(c->getName(filename)))
381               return;
382             outs() << symname << " in " << filename << "\n";
383           }
384           outs() << "\n";
385         }
386       }
387
388       for (object::Archive::child_iterator i = a->begin_children(),
389                                            e = a->end_children(); i != e; ++i) {
390         OwningPtr<Binary> child;
391         if (i->getAsBinary(child)) {
392           // Try opening it as a bitcode file.
393           OwningPtr<MemoryBuffer> buff;
394           if (error(i->getMemoryBuffer(buff)))
395             return;
396           Module *Result = 0;
397           if (buff)
398             Result = ParseBitcodeFile(buff.get(), Context, &ErrorMessage);
399
400           if (Result) {
401             DumpSymbolNamesFromModule(Result);
402             delete Result;
403           }
404           continue;
405         }
406         if (object::ObjectFile *o = dyn_cast<ObjectFile>(child.get())) {
407           outs() << o->getFileName() << ":\n";
408           DumpSymbolNamesFromObject(o);
409         }
410       }
411     }
412   } else if (magic == sys::fs::file_magic::macho_universal_binary) {
413     OwningPtr<Binary> Bin;
414     if (error(object::createBinary(Buffer.take(), Bin), Filename))
415       return;
416
417     object::MachOUniversalBinary *UB =
418         cast<object::MachOUniversalBinary>(Bin.get());
419     for (object::MachOUniversalBinary::object_iterator
420              I = UB->begin_objects(),
421              E = UB->end_objects();
422          I != E; ++I) {
423       OwningPtr<ObjectFile> Obj;
424       if (!I->getAsObjectFile(Obj)) {
425         outs() << Obj->getFileName() << ":\n";
426         DumpSymbolNamesFromObject(Obj.get());
427       }
428     }
429   } else if (magic.is_object()) {
430     OwningPtr<Binary> obj;
431     if (error(object::createBinary(Buffer.take(), obj), Filename))
432       return;
433     if (object::ObjectFile *o = dyn_cast<ObjectFile>(obj.get()))
434       DumpSymbolNamesFromObject(o);
435   } else {
436     errs() << ToolName << ": " << Filename << ": "
437            << "unrecognizable file type\n";
438     HadError = true;
439     return;
440   }
441 }
442
443 int main(int argc, char **argv) {
444   // Print a stack trace if we signal out.
445   sys::PrintStackTraceOnErrorSignal();
446   PrettyStackTraceProgram X(argc, argv);
447
448   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
449   cl::ParseCommandLineOptions(argc, argv, "llvm symbol table dumper\n");
450
451   // llvm-nm only reads binary files.
452   if (error(sys::ChangeStdinToBinary()))
453     return 1;
454
455   ToolName = argv[0];
456   if (BSDFormat) OutputFormat = bsd;
457   if (POSIXFormat) OutputFormat = posix;
458
459   // The relative order of these is important. If you pass --size-sort it should
460   // only print out the size. However, if you pass -S --size-sort, it should
461   // print out both the size and address.
462   if (SizeSort && !PrintSize) PrintAddress = false;
463   if (OutputFormat == sysv || SizeSort) PrintSize = true;
464
465   switch (InputFilenames.size()) {
466   case 0: InputFilenames.push_back("-");
467   case 1: break;
468   default: MultipleFiles = true;
469   }
470
471   std::for_each(InputFilenames.begin(), InputFilenames.end(),
472                 DumpSymbolNamesFromFile);
473
474   if (HadError)
475     return 1;
476
477   return 0;
478 }