llvm-nm.cpp revision aba65b05fc5dd8649725431e38359b7fa1ab59e8
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",
11// that is, it prints out the names of symbols in a bitcode file,
12// along with some information about each symbol.
13//
14// This "nm" does not print symbols' addresses. It supports many of
15// the features of GNU "nm", including its different output formats.
16//
17//===----------------------------------------------------------------------===//
18
19#include "llvm/LLVMContext.h"
20#include "llvm/Module.h"
21#include "llvm/Bitcode/ReaderWriter.h"
22#include "llvm/Bitcode/Archive.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/ManagedStatic.h"
28#include "llvm/Support/MemoryBuffer.h"
29#include "llvm/Support/PrettyStackTrace.h"
30#include "llvm/Support/Program.h"
31#include "llvm/Support/raw_ostream.h"
32#include "llvm/Support/Signals.h"
33#include "llvm/Support/Format.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> DefinedOnly("defined-only",
65                            cl::desc("Show only defined symbols"));
66
67  cl::opt<bool> ExternalOnly("extern-only",
68                             cl::desc("Show only external symbols"));
69  cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
70                          cl::aliasopt(ExternalOnly));
71
72  cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
73  cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
74
75  cl::opt<bool> PrintFileName("print-file-name",
76    cl::desc("Precede each symbol with the object file it came from"));
77
78  cl::alias PrintFileNameA("A", cl::desc("Alias for --print-file-name"),
79                                cl::aliasopt(PrintFileName));
80  cl::alias PrintFileNameo("o", cl::desc("Alias for --print-file-name"),
81                                cl::aliasopt(PrintFileName));
82
83  cl::opt<bool> DebugSyms("debug-syms",
84    cl::desc("Show all symbols, even debugger only"));
85  cl::alias DebugSymsa("a", cl::desc("Alias for --debug-syms"),
86                            cl::aliasopt(DebugSyms));
87
88  cl::opt<bool> NumericSort("numeric-sort",
89    cl::desc("Sort symbols by address"));
90  cl::alias NumericSortn("n", cl::desc("Alias for --numeric-sort"),
91                              cl::aliasopt(NumericSort));
92  cl::alias NumericSortv("v", cl::desc("Alias for --numeric-sort"),
93                              cl::aliasopt(NumericSort));
94
95  cl::opt<bool> NoSort("no-sort",
96    cl::desc("Show symbols in order encountered"));
97  cl::alias NoSortp("p", cl::desc("Alias for --no-sort"),
98                         cl::aliasopt(NoSort));
99
100  cl::opt<bool> PrintSize("print-size",
101    cl::desc("Show symbol size instead of address"));
102  cl::alias PrintSizeS("S", cl::desc("Alias for --print-size"),
103                            cl::aliasopt(PrintSize));
104
105  cl::opt<bool> SizeSort("size-sort", cl::desc("Sort symbols by size"));
106
107  bool PrintAddress = true;
108
109  bool MultipleFiles = false;
110
111  std::string ToolName;
112}
113
114
115static void error(Twine message, Twine path = Twine()) {
116  errs() << ToolName << ": " << path << ": " << message << ".\n";
117}
118
119static bool error(error_code ec, Twine path = Twine()) {
120  if (ec) {
121    error(ec.message(), path);
122    return true;
123  }
124  return false;
125}
126
127namespace {
128  struct NMSymbol {
129    uint64_t  Address;
130    uint64_t  Size;
131    char      TypeChar;
132    StringRef Name;
133  };
134
135  static bool CompareSymbolAddress(const NMSymbol &a, const NMSymbol &b) {
136    if (a.Address < b.Address)
137      return true;
138    else if (a.Address == b.Address && a.Name < b.Name)
139      return true;
140    else
141      return false;
142
143  }
144
145  static bool CompareSymbolSize(const NMSymbol &a, const NMSymbol &b) {
146    if (a.Size < b.Size)
147      return true;
148    else if (a.Size == b.Size && a.Name < b.Name)
149      return true;
150    else
151      return false;
152  }
153
154  static bool CompareSymbolName(const NMSymbol &a, const NMSymbol &b) {
155    return a.Name < b.Name;
156  }
157
158  StringRef CurrentFilename;
159  typedef std::vector<NMSymbol> SymbolListT;
160  SymbolListT SymbolList;
161}
162
163static void SortAndPrintSymbolList() {
164  if (!NoSort) {
165    if (NumericSort)
166      std::sort(SymbolList.begin(), SymbolList.end(), CompareSymbolAddress);
167    else if (SizeSort)
168      std::sort(SymbolList.begin(), SymbolList.end(), CompareSymbolSize);
169    else
170      std::sort(SymbolList.begin(), SymbolList.end(), CompareSymbolName);
171  }
172
173  if (OutputFormat == posix && MultipleFiles) {
174    outs() << '\n' << CurrentFilename << ":\n";
175  } else if (OutputFormat == bsd && MultipleFiles) {
176    outs() << "\n" << CurrentFilename << ":\n";
177  } else if (OutputFormat == sysv) {
178    outs() << "\n\nSymbols from " << CurrentFilename << ":\n\n"
179           << "Name                  Value   Class        Type"
180           << "         Size   Line  Section\n";
181  }
182
183  for (SymbolListT::iterator i = SymbolList.begin(),
184                             e = SymbolList.end(); i != e; ++i) {
185    if ((i->TypeChar != 'U') && UndefinedOnly)
186      continue;
187    if ((i->TypeChar == 'U') && DefinedOnly)
188      continue;
189    if (SizeSort && !PrintAddress && i->Size == UnknownAddressOrSize)
190      continue;
191
192    char SymbolAddrStr[10] = "";
193    char SymbolSizeStr[10] = "";
194
195    if (OutputFormat == sysv || i->Address == object::UnknownAddressOrSize)
196      strcpy(SymbolAddrStr, "        ");
197    if (OutputFormat == sysv)
198      strcpy(SymbolSizeStr, "        ");
199
200    if (i->Address != object::UnknownAddressOrSize)
201      format("%08"PRIx64, i->Address).print(SymbolAddrStr, sizeof(SymbolAddrStr));
202    if (i->Size != object::UnknownAddressOrSize)
203      format("%08"PRIx64, i->Size).print(SymbolSizeStr, sizeof(SymbolSizeStr));
204
205    if (OutputFormat == posix) {
206      outs() << i->Name << " " << i->TypeChar << " "
207             << SymbolAddrStr << SymbolSizeStr << "\n";
208    } else if (OutputFormat == bsd) {
209      if (PrintAddress)
210        outs() << SymbolAddrStr << ' ';
211      if (PrintSize) {
212        outs() << SymbolSizeStr;
213        if (i->Size != object::UnknownAddressOrSize)
214          outs() << ' ';
215      }
216      outs() << i->TypeChar << " " << i->Name  << "\n";
217    } else if (OutputFormat == sysv) {
218      std::string PaddedName (i->Name);
219      while (PaddedName.length () < 20)
220        PaddedName += " ";
221      outs() << PaddedName << "|" << SymbolAddrStr << "|   "
222             << i->TypeChar
223             << "  |                  |" << SymbolSizeStr << "|     |\n";
224    }
225  }
226
227  SymbolList.clear();
228}
229
230static char TypeCharForSymbol(GlobalValue &GV) {
231  if (GV.isDeclaration())                                  return 'U';
232  if (GV.hasLinkOnceLinkage())                             return 'C';
233  if (GV.hasCommonLinkage())                               return 'C';
234  if (GV.hasWeakLinkage())                                 return 'W';
235  if (isa<Function>(GV) && GV.hasInternalLinkage())        return 't';
236  if (isa<Function>(GV))                                   return 'T';
237  if (isa<GlobalVariable>(GV) && GV.hasInternalLinkage())  return 'd';
238  if (isa<GlobalVariable>(GV))                             return 'D';
239  if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(&GV)) {
240    const GlobalValue *AliasedGV = GA->getAliasedGlobal();
241    if (isa<Function>(AliasedGV))                          return 'T';
242    if (isa<GlobalVariable>(AliasedGV))                    return 'D';
243  }
244                                                           return '?';
245}
246
247static void DumpSymbolNameForGlobalValue(GlobalValue &GV) {
248  // Private linkage and available_externally linkage don't exist in symtab.
249  if (GV.hasPrivateLinkage() ||
250      GV.hasLinkerPrivateLinkage() ||
251      GV.hasLinkerPrivateWeakLinkage() ||
252      GV.hasLinkerPrivateWeakDefAutoLinkage() ||
253      GV.hasAvailableExternallyLinkage())
254    return;
255  char TypeChar = TypeCharForSymbol(GV);
256  if (GV.hasLocalLinkage () && ExternalOnly)
257    return;
258
259  NMSymbol s;
260  s.Address = object::UnknownAddressOrSize;
261  s.Size = object::UnknownAddressOrSize;
262  s.TypeChar = TypeChar;
263  s.Name     = GV.getName();
264  SymbolList.push_back(s);
265}
266
267static void DumpSymbolNamesFromModule(Module *M) {
268  CurrentFilename = M->getModuleIdentifier();
269  std::for_each (M->begin(), M->end(), DumpSymbolNameForGlobalValue);
270  std::for_each (M->global_begin(), M->global_end(),
271                 DumpSymbolNameForGlobalValue);
272  std::for_each (M->alias_begin(), M->alias_end(),
273                 DumpSymbolNameForGlobalValue);
274
275  SortAndPrintSymbolList();
276}
277
278static void DumpSymbolNamesFromObject(ObjectFile *obj) {
279  error_code ec;
280  for (symbol_iterator i = obj->begin_symbols(),
281                       e = obj->end_symbols();
282                       i != e; i.increment(ec)) {
283    if (error(ec)) break;
284    bool internal;
285    if (error(i->isInternal(internal))) break;
286    if (!DebugSyms && internal)
287      continue;
288    NMSymbol s;
289    s.Size = object::UnknownAddressOrSize;
290    s.Address = object::UnknownAddressOrSize;
291    if (PrintSize || SizeSort) {
292      if (error(i->getSize(s.Size))) break;
293    }
294    if (PrintAddress)
295      if (error(i->getAddress(s.Address))) break;
296    if (error(i->getNMTypeChar(s.TypeChar))) break;
297    if (error(i->getName(s.Name))) break;
298    SymbolList.push_back(s);
299  }
300
301  CurrentFilename = obj->getFileName();
302  SortAndPrintSymbolList();
303}
304
305static void DumpSymbolNamesFromFile(std::string &Filename) {
306  if (Filename != "-" && !sys::fs::exists(Filename)) {
307    errs() << ToolName << ": '" << Filename << "': " << "No such file\n";
308    return;
309  }
310
311  OwningPtr<MemoryBuffer> Buffer;
312  if (error(MemoryBuffer::getFileOrSTDIN(Filename, Buffer), Filename))
313    return;
314
315  sys::fs::file_magic magic = sys::fs::identify_magic(Buffer->getBuffer());
316
317  LLVMContext &Context = getGlobalContext();
318  std::string ErrorMessage;
319  if (magic == sys::fs::file_magic::bitcode) {
320    Module *Result = 0;
321    Result = ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage);
322    if (Result) {
323      DumpSymbolNamesFromModule(Result);
324      delete Result;
325    } else {
326      error(ErrorMessage, Filename);
327      return;
328    }
329  } else if (magic == sys::fs::file_magic::archive) {
330    OwningPtr<Binary> arch;
331    if (error(object::createBinary(Buffer.take(), arch), Filename))
332      return;
333
334    if (object::Archive *a = dyn_cast<object::Archive>(arch.get())) {
335      for (object::Archive::child_iterator i = a->begin_children(),
336                                           e = a->end_children(); i != e; ++i) {
337        OwningPtr<Binary> child;
338        if (i->getAsBinary(child)) {
339          // Try opening it as a bitcode file.
340          OwningPtr<MemoryBuffer> buff(i->getBuffer());
341          Module *Result = 0;
342          if (buff)
343            Result = ParseBitcodeFile(buff.get(), Context, &ErrorMessage);
344
345          if (Result) {
346            DumpSymbolNamesFromModule(Result);
347            delete Result;
348          }
349          continue;
350        }
351        if (object::ObjectFile *o = dyn_cast<ObjectFile>(child.get())) {
352          outs() << o->getFileName() << ":\n";
353          DumpSymbolNamesFromObject(o);
354        }
355      }
356    }
357  } else if (magic.is_object()) {
358    OwningPtr<Binary> obj;
359    if (error(object::createBinary(Buffer.take(), obj), Filename))
360      return;
361    if (object::ObjectFile *o = dyn_cast<ObjectFile>(obj.get()))
362      DumpSymbolNamesFromObject(o);
363  } else {
364    errs() << ToolName << ": " << Filename << ": "
365           << "unrecognizable file type\n";
366    return;
367  }
368}
369
370int main(int argc, char **argv) {
371  // Print a stack trace if we signal out.
372  sys::PrintStackTraceOnErrorSignal();
373  PrettyStackTraceProgram X(argc, argv);
374
375  llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
376  cl::ParseCommandLineOptions(argc, argv, "llvm symbol table dumper\n");
377
378  // llvm-nm only reads binary files.
379  if (error(sys::Program::ChangeStdinToBinary()))
380    return 1;
381
382  ToolName = argv[0];
383  if (BSDFormat) OutputFormat = bsd;
384  if (POSIXFormat) OutputFormat = posix;
385
386  // The relative order of these is important. If you pass --size-sort it should
387  // only print out the size. However, if you pass -S --size-sort, it should
388  // print out both the size and address.
389  if (SizeSort && !PrintSize) PrintAddress = false;
390  if (OutputFormat == sysv || SizeSort) PrintSize = true;
391
392  switch (InputFilenames.size()) {
393  case 0: InputFilenames.push_back("-");
394  case 1: break;
395  default: MultipleFiles = true;
396  }
397
398  std::for_each(InputFilenames.begin(), InputFilenames.end(),
399                DumpSymbolNamesFromFile);
400  return 0;
401}
402