Geeze, what is with Brian and these ifdef's?
[oota-llvm.git] / tools / llvm-nm / llvm-nm.cpp
1 //===-- llvm-nm.cpp - Symbol table dumping utility for llvm ---------------===//
2 //
3 // This program is a utility that works like traditional Unix "nm",
4 // that is, it prints out the names of symbols in a bytecode file,
5 // along with some information about each symbol.
6 // 
7 // This "nm" does not print symbols' addresses. It supports many of
8 // the features of GNU "nm", including its different output formats.
9 //
10 //===----------------------------------------------------------------------===//
11
12 #include "Support/CommandLine.h"
13 #include "llvm/Bytecode/Reader.h"
14 #include "llvm/GlobalValue.h"
15 #include "llvm/Module.h"
16 #include <cctype>
17
18 namespace {
19   enum OutputFormatTy { bsd, sysv, posix };
20   cl::opt<OutputFormatTy>
21   OutputFormat("format",
22        cl::desc("Specify output format"),
23          cl::values(clEnumVal(bsd,   "BSD format"),
24                     clEnumVal(sysv,  "System V format"),
25                     clEnumVal(posix, "POSIX.2 format"), 0), cl::init(bsd));
26   cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
27                           cl::aliasopt(OutputFormat));
28
29   cl::list<std::string> 
30   InputFilenames(cl::Positional, cl::desc("<input bytecode files>"),
31                  cl::OneOrMore);
32
33   cl::opt<bool> UndefinedOnly("undefined-only",
34                               cl::desc("Show only undefined symbols"));
35   cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
36                            cl::aliasopt(UndefinedOnly));
37
38   cl::opt<bool> DefinedOnly("defined-only",
39                             cl::desc("Show only defined symbols"));
40
41   cl::opt<bool> ExternalOnly("extern-only",
42                              cl::desc("Show only external symbols"));
43   cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
44                           cl::aliasopt(ExternalOnly));
45
46   cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
47   cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
48
49   bool MultipleFiles = false;
50
51   std::string ToolName;
52 };
53
54 char TypeCharForSymbol (GlobalValue &GV) {
55   if (GV.isExternal ())                                     return 'U';
56   if (GV.hasLinkOnceLinkage ())                             return 'C';
57   if (GV.hasWeakLinkage ())                                 return 'W';
58   if (isa<Function> (GV) && GV.hasInternalLinkage ())       return 't';
59   if (isa<Function> (GV))                                   return 'T';
60   if (isa<GlobalVariable> (GV) && GV.hasInternalLinkage ()) return 'd';
61   if (isa<GlobalVariable> (GV))                             return 'D';
62                                                             return '?';
63 }
64
65 void DumpSymbolNameForGlobalValue (GlobalValue &GV) {
66   const std::string SymbolAddrStr = "        "; // Not used yet...
67   char TypeChar = TypeCharForSymbol (GV);
68   if ((TypeChar != 'U') && UndefinedOnly)
69     return;
70   if ((TypeChar == 'U') && DefinedOnly)
71     return;
72   if (GV.hasInternalLinkage () && ExternalOnly)
73     return;
74   if (OutputFormat == posix) {
75     std::cout << GV.getName () << " " << TypeCharForSymbol (GV) << " "
76               << SymbolAddrStr << "\n";
77   } else if (OutputFormat == bsd) {
78     std::cout << SymbolAddrStr << " " << TypeCharForSymbol (GV) << " "
79               << GV.getName () << "\n";
80   } else if (OutputFormat == sysv) {
81     std::string PaddedName (GV.getName ());
82     while (PaddedName.length () < 20)
83       PaddedName += " ";
84     std::cout << PaddedName << "|" << SymbolAddrStr << "|   "
85               << TypeCharForSymbol (GV)
86               << "  |                  |      |     |\n";
87   }
88 }
89
90 void DumpSymbolNamesFromModule (Module *M) {
91   std::for_each (M->begin (), M->end (), DumpSymbolNameForGlobalValue);
92   std::for_each (M->gbegin (), M->gend (), DumpSymbolNameForGlobalValue);
93 }
94
95 void DumpSymbolNamesFromFile (std::string &Filename) {
96   std::string ErrorMessage;
97   Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
98   if (Result) {
99     if (OutputFormat == posix && MultipleFiles) {
100       std::cout << Filename << ":\n";
101     } else if (OutputFormat == bsd && MultipleFiles) {
102       std::cout << "\n" << Filename << ":\n";
103     } else if (OutputFormat == sysv) {
104       std::cout << "\n\nSymbols from " << Filename << ":\n\n"
105                 << "Name                  Value   Class        Type"
106                 << "         Size   Line  Section\n";
107     }
108     DumpSymbolNamesFromModule (Result);
109   } else {
110     std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
111   }
112 }
113
114 int main(int argc, char **argv) {
115   cl::ParseCommandLineOptions(argc, argv, " llvm symbol table dumper\n");
116   ToolName = argv[0];
117   if (BSDFormat) OutputFormat = bsd;
118   if (POSIXFormat) OutputFormat = posix;
119   if (InputFilenames.size () != 1)
120     MultipleFiles = true;
121
122   std::for_each (InputFilenames.begin (), InputFilenames.end (),
123                  DumpSymbolNamesFromFile);
124   return 0;
125 }