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/Archive.h"
21#include "llvm/Bitcode/ReaderWriter.h"
22#include "llvm/IR/Module.h"
23#include "llvm/Object/Archive.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>
40using namespace llvm;
41using namespace object;
42
43namespace {
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  std::string ToolName;
125}
126
127
128static void error(Twine message, Twine path = Twine()) {
129  errs() << ToolName << ": " << path << ": " << message << ".\n";
130}
131
132static bool error(error_code ec, Twine path = Twine()) {
133  if (ec) {
134    error(ec.message(), path);
135    return true;
136  }
137  return false;
138}
139
140namespace {
141  struct NMSymbol {
142    uint64_t  Address;
143    uint64_t  Size;
144    char      TypeChar;
145    StringRef Name;
146  };
147
148  static bool CompareSymbolAddress(const NMSymbol &a, const NMSymbol &b) {
149    if (a.Address < b.Address)
150      return true;
151    else if (a.Address == b.Address && a.Name < b.Name)
152      return true;
153    else if (a.Address == b.Address && a.Name == b.Name && a.Size < b.Size)
154      return true;
155    else
156      return false;
157
158  }
159
160  static bool CompareSymbolSize(const NMSymbol &a, const NMSymbol &b) {
161    if (a.Size < b.Size)
162      return true;
163    else if (a.Size == b.Size && a.Name < b.Name)
164      return true;
165    else if (a.Size == b.Size && a.Name == b.Name && a.Address < b.Address)
166      return true;
167    else
168      return false;
169  }
170
171  static bool CompareSymbolName(const NMSymbol &a, const NMSymbol &b) {
172    if (a.Name < b.Name)
173      return true;
174    else if (a.Name == b.Name && a.Size < b.Size)
175      return true;
176    else if (a.Name == b.Name && a.Size == b.Size && a.Address < b.Address)
177      return true;
178    else
179      return false;
180  }
181
182  StringRef CurrentFilename;
183  typedef std::vector<NMSymbol> SymbolListT;
184  SymbolListT SymbolList;
185}
186
187static 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(),
208                             e = SymbolList.end(); 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 == object::UnknownAddressOrSize)
220      strcpy(SymbolAddrStr, "        ");
221    if (OutputFormat == sysv)
222      strcpy(SymbolSizeStr, "        ");
223
224    if (i->Address != object::UnknownAddressOrSize)
225      format("%08" PRIx64, i->Address).print(SymbolAddrStr,
226                                             sizeof(SymbolAddrStr));
227    if (i->Size != object::UnknownAddressOrSize)
228      format("%08" PRIx64, i->Size).print(SymbolSizeStr, sizeof(SymbolSizeStr));
229
230    if (OutputFormat == posix) {
231      outs() << i->Name << " " << i->TypeChar << " "
232             << SymbolAddrStr << SymbolSizeStr << "\n";
233    } else if (OutputFormat == bsd) {
234      if (PrintAddress)
235        outs() << SymbolAddrStr << ' ';
236      if (PrintSize) {
237        outs() << SymbolSizeStr;
238        if (i->Size != object::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 << "|   "
247             << i->TypeChar
248             << "  |                  |" << SymbolSizeStr << "|     |\n";
249    }
250  }
251
252  SymbolList.clear();
253}
254
255static char TypeCharForSymbol(GlobalValue &GV) {
256  if (GV.isDeclaration())                                  return 'U';
257  if (GV.hasLinkOnceLinkage())                             return 'C';
258  if (GV.hasCommonLinkage())                               return 'C';
259  if (GV.hasWeakLinkage())                                 return 'W';
260  if (isa<Function>(GV) && GV.hasInternalLinkage())        return 't';
261  if (isa<Function>(GV))                                   return 'T';
262  if (isa<GlobalVariable>(GV) && GV.hasInternalLinkage())  return 'd';
263  if (isa<GlobalVariable>(GV))                             return 'D';
264  if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(&GV)) {
265    const GlobalValue *AliasedGV = GA->getAliasedGlobal();
266    if (isa<Function>(AliasedGV))                          return 'T';
267    if (isa<GlobalVariable>(AliasedGV))                    return 'D';
268  }
269                                                           return '?';
270}
271
272static void DumpSymbolNameForGlobalValue(GlobalValue &GV) {
273  // Private linkage and available_externally linkage don't exist in symtab.
274  if (GV.hasPrivateLinkage() ||
275      GV.hasLinkerPrivateLinkage() ||
276      GV.hasLinkerPrivateWeakLinkage() ||
277      GV.hasAvailableExternallyLinkage())
278    return;
279  char TypeChar = TypeCharForSymbol(GV);
280  if (GV.hasLocalLinkage () && ExternalOnly)
281    return;
282
283  NMSymbol s;
284  s.Address = object::UnknownAddressOrSize;
285  s.Size = object::UnknownAddressOrSize;
286  s.TypeChar = TypeChar;
287  s.Name     = GV.getName();
288  SymbolList.push_back(s);
289}
290
291static void DumpSymbolNamesFromModule(Module *M) {
292  CurrentFilename = M->getModuleIdentifier();
293  std::for_each (M->begin(), M->end(), DumpSymbolNameForGlobalValue);
294  std::for_each (M->global_begin(), M->global_end(),
295                 DumpSymbolNameForGlobalValue);
296  if (!WithoutAliases)
297    std::for_each (M->alias_begin(), M->alias_end(),
298		   DumpSymbolNameForGlobalValue);
299
300  SortAndPrintSymbolList();
301}
302
303static void DumpSymbolNamesFromObject(ObjectFile *obj) {
304  error_code ec;
305  symbol_iterator ibegin = obj->begin_symbols();
306  symbol_iterator iend = obj->end_symbols();
307  if (DynamicSyms) {
308    ibegin = obj->begin_dynamic_symbols();
309    iend = obj->end_dynamic_symbols();
310  }
311  for (symbol_iterator i = ibegin; i != iend; i.increment(ec)) {
312    if (error(ec)) break;
313    uint32_t symflags;
314    if (error(i->getFlags(symflags))) break;
315    if (!DebugSyms && (symflags & SymbolRef::SF_FormatSpecific))
316      continue;
317    NMSymbol s;
318    s.Size = object::UnknownAddressOrSize;
319    s.Address = object::UnknownAddressOrSize;
320    if (PrintSize || SizeSort) {
321      if (error(i->getSize(s.Size))) break;
322    }
323    if (PrintAddress)
324      if (error(i->getAddress(s.Address))) break;
325    if (error(i->getNMTypeChar(s.TypeChar))) break;
326    if (error(i->getName(s.Name))) break;
327    SymbolList.push_back(s);
328  }
329
330  CurrentFilename = obj->getFileName();
331  SortAndPrintSymbolList();
332}
333
334static void DumpSymbolNamesFromFile(std::string &Filename) {
335  if (Filename != "-" && !sys::fs::exists(Filename)) {
336    errs() << ToolName << ": '" << Filename << "': " << "No such file\n";
337    return;
338  }
339
340  OwningPtr<MemoryBuffer> Buffer;
341  if (error(MemoryBuffer::getFileOrSTDIN(Filename, Buffer), Filename))
342    return;
343
344  sys::fs::file_magic magic = sys::fs::identify_magic(Buffer->getBuffer());
345
346  LLVMContext &Context = getGlobalContext();
347  std::string ErrorMessage;
348  if (magic == sys::fs::file_magic::bitcode) {
349    Module *Result = 0;
350    Result = ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage);
351    if (Result) {
352      DumpSymbolNamesFromModule(Result);
353      delete Result;
354    } else {
355      error(ErrorMessage, Filename);
356      return;
357    }
358  } else if (magic == sys::fs::file_magic::archive) {
359    OwningPtr<Binary> arch;
360    if (error(object::createBinary(Buffer.take(), arch), Filename))
361      return;
362
363    if (object::Archive *a = dyn_cast<object::Archive>(arch.get())) {
364      if (ArchiveMap) {
365        outs() << "Archive map" << "\n";
366        for (object::Archive::symbol_iterator i = a->begin_symbols(),
367             e = a->end_symbols(); i != e; ++i) {
368          object::Archive::child_iterator c;
369          StringRef symname;
370          StringRef filename;
371          if (error(i->getMember(c)))
372              return;
373          if (error(i->getName(symname)))
374              return;
375          if (error(c->getName(filename)))
376              return;
377          outs() << symname << " in " << filename << "\n";
378        }
379        outs() << "\n";
380      }
381
382      for (object::Archive::child_iterator i = a->begin_children(),
383                                           e = a->end_children(); i != e; ++i) {
384        OwningPtr<Binary> child;
385        if (i->getAsBinary(child)) {
386          // Try opening it as a bitcode file.
387          OwningPtr<MemoryBuffer> buff;
388          if (error(i->getMemoryBuffer(buff)))
389            return;
390          Module *Result = 0;
391          if (buff)
392            Result = ParseBitcodeFile(buff.get(), Context, &ErrorMessage);
393
394          if (Result) {
395            DumpSymbolNamesFromModule(Result);
396            delete Result;
397          }
398          continue;
399        }
400        if (object::ObjectFile *o = dyn_cast<ObjectFile>(child.get())) {
401          outs() << o->getFileName() << ":\n";
402          DumpSymbolNamesFromObject(o);
403        }
404      }
405    }
406  } else if (magic.is_object()) {
407    OwningPtr<Binary> obj;
408    if (error(object::createBinary(Buffer.take(), obj), Filename))
409      return;
410    if (object::ObjectFile *o = dyn_cast<ObjectFile>(obj.get()))
411      DumpSymbolNamesFromObject(o);
412  } else {
413    errs() << ToolName << ": " << Filename << ": "
414           << "unrecognizable file type\n";
415    return;
416  }
417}
418
419int main(int argc, char **argv) {
420  // Print a stack trace if we signal out.
421  sys::PrintStackTraceOnErrorSignal();
422  PrettyStackTraceProgram X(argc, argv);
423
424  llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
425  cl::ParseCommandLineOptions(argc, argv, "llvm symbol table dumper\n");
426
427  // llvm-nm only reads binary files.
428  if (error(sys::Program::ChangeStdinToBinary()))
429    return 1;
430
431  ToolName = argv[0];
432  if (BSDFormat) OutputFormat = bsd;
433  if (POSIXFormat) OutputFormat = posix;
434
435  // The relative order of these is important. If you pass --size-sort it should
436  // only print out the size. However, if you pass -S --size-sort, it should
437  // print out both the size and address.
438  if (SizeSort && !PrintSize) PrintAddress = false;
439  if (OutputFormat == sysv || SizeSort) PrintSize = true;
440
441  switch (InputFilenames.size()) {
442  case 0: InputFilenames.push_back("-");
443  case 1: break;
444  default: MultipleFiles = true;
445  }
446
447  std::for_each(InputFilenames.begin(), InputFilenames.end(),
448                DumpSymbolNamesFromFile);
449  return 0;
450}
451