6aaf7d2f74bf3bdaa2135044d84c6dd632b8f0f5
[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 was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 bytecode 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/Module.h"
20 #include "llvm/Bytecode/Reader.h"
21 #include "Support/CommandLine.h"
22 #include "Support/FileUtilities.h"
23 #include "Support/Signals.h"
24 #include <cctype>
25 #include <cerrno>
26 #include <cstring>
27
28 using namespace llvm;
29
30 namespace {
31   enum OutputFormatTy { bsd, sysv, posix };
32   cl::opt<OutputFormatTy>
33   OutputFormat("format",
34        cl::desc("Specify output format"),
35          cl::values(clEnumVal(bsd,   "BSD format"),
36                     clEnumVal(sysv,  "System V format"),
37                     clEnumVal(posix, "POSIX.2 format"), 0), cl::init(bsd));
38   cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
39                           cl::aliasopt(OutputFormat));
40
41   cl::list<std::string> 
42   InputFilenames(cl::Positional, cl::desc("<input bytecode files>"),
43                  cl::ZeroOrMore);
44
45   cl::opt<bool> UndefinedOnly("undefined-only",
46                               cl::desc("Show only undefined symbols"));
47   cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
48                            cl::aliasopt(UndefinedOnly));
49
50   cl::opt<bool> DefinedOnly("defined-only",
51                             cl::desc("Show only defined symbols"));
52
53   cl::opt<bool> ExternalOnly("extern-only",
54                              cl::desc("Show only external symbols"));
55   cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
56                           cl::aliasopt(ExternalOnly));
57
58   cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
59   cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
60
61   bool MultipleFiles = false;
62
63   std::string ToolName;
64 };
65
66 char TypeCharForSymbol (GlobalValue &GV) {
67   if (GV.isExternal ())                                     return 'U';
68   if (GV.hasLinkOnceLinkage ())                             return 'C';
69   if (GV.hasWeakLinkage ())                                 return 'W';
70   if (isa<Function> (GV) && GV.hasInternalLinkage ())       return 't';
71   if (isa<Function> (GV))                                   return 'T';
72   if (isa<GlobalVariable> (GV) && GV.hasInternalLinkage ()) return 'd';
73   if (isa<GlobalVariable> (GV))                             return 'D';
74                                                             return '?';
75 }
76
77 void DumpSymbolNameForGlobalValue (GlobalValue &GV) {
78   const std::string SymbolAddrStr = "        "; // Not used yet...
79   char TypeChar = TypeCharForSymbol (GV);
80   if ((TypeChar != 'U') && UndefinedOnly)
81     return;
82   if ((TypeChar == 'U') && DefinedOnly)
83     return;
84   if (GV.hasInternalLinkage () && ExternalOnly)
85     return;
86   if (OutputFormat == posix) {
87     std::cout << GV.getName () << " " << TypeCharForSymbol (GV) << " "
88               << SymbolAddrStr << "\n";
89   } else if (OutputFormat == bsd) {
90     std::cout << SymbolAddrStr << " " << TypeCharForSymbol (GV) << " "
91               << GV.getName () << "\n";
92   } else if (OutputFormat == sysv) {
93     std::string PaddedName (GV.getName ());
94     while (PaddedName.length () < 20)
95       PaddedName += " ";
96     std::cout << PaddedName << "|" << SymbolAddrStr << "|   "
97               << TypeCharForSymbol (GV)
98               << "  |                  |      |     |\n";
99   }
100 }
101
102 void DumpSymbolNamesFromModule (Module *M) {
103   const std::string &Filename = M->getModuleIdentifier ();
104   if (OutputFormat == posix && MultipleFiles) {
105     std::cout << Filename << ":\n";
106   } else if (OutputFormat == bsd && MultipleFiles) {
107     std::cout << "\n" << Filename << ":\n";
108   } else if (OutputFormat == sysv) {
109     std::cout << "\n\nSymbols from " << Filename << ":\n\n"
110               << "Name                  Value   Class        Type"
111               << "         Size   Line  Section\n";
112   }
113   std::for_each (M->begin (), M->end (), DumpSymbolNameForGlobalValue);
114   std::for_each (M->gbegin (), M->gend (), DumpSymbolNameForGlobalValue);
115 }
116
117 void DumpSymbolNamesFromFile (std::string &Filename) {
118   std::string ErrorMessage;
119   if (Filename != "-" && !FileOpenable (Filename)) {
120     std::cerr << ToolName << ": " << Filename << ": " << strerror (errno)
121               << "\n";
122     return;
123   }
124   // Note: Currently we do not support reading an archive from stdin.
125   if (Filename == "-" || IsBytecode (Filename)) {
126     Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
127     if (Result) {
128       DumpSymbolNamesFromModule (Result);
129     } else {
130       std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
131       return;
132     }
133   } else if (IsArchive (Filename)) {
134     std::vector<Module *> Modules;
135     if (ReadArchiveFile (Filename, Modules, &ErrorMessage)) {
136       std::cerr << ToolName << ": " << Filename << ": "
137                 << ErrorMessage << "\n";
138       return;
139     }
140     MultipleFiles = true;
141     std::for_each (Modules.begin (), Modules.end (), DumpSymbolNamesFromModule);
142   } else {
143     std::cerr << ToolName << ": " << Filename << ": "
144               << "unrecognizable file type\n";
145     return;
146   }
147 }
148
149 int main(int argc, char **argv) {
150   cl::ParseCommandLineOptions(argc, argv, " llvm symbol table dumper\n");
151   PrintStackTraceOnErrorSignal();
152
153   ToolName = argv[0];
154   if (BSDFormat) OutputFormat = bsd;
155   if (POSIXFormat) OutputFormat = posix;
156
157   switch (InputFilenames.size()) {
158   case 0: InputFilenames.push_back("-");
159   case 1: break;
160   default: MultipleFiles = true;
161   }
162
163   std::for_each (InputFilenames.begin (), InputFilenames.end (),
164                  DumpSymbolNamesFromFile);
165   return 0;
166 }