Okay, this is a little hack that "scratches an itch" of mine.
[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 #ifdef WEAK_LINKAGE_EVENTUALLY_IMPLEMENTED
58   if (GV.hasWeakLinkage ())                                 return 'W';
59 #endif
60   if (isa<Function> (GV) && GV.hasInternalLinkage ())       return 't';
61   if (isa<Function> (GV))                                   return 'T';
62   if (isa<GlobalVariable> (GV) && GV.hasInternalLinkage ()) return 'd';
63   if (isa<GlobalVariable> (GV))                             return 'D';
64                                                             return '?';
65 }
66
67 void DumpSymbolNameForGlobalValue (GlobalValue &GV) {
68   const std::string SymbolAddrStr = "        "; // Not used yet...
69   char TypeChar = TypeCharForSymbol (GV);
70   if ((TypeChar != 'U') && UndefinedOnly)
71     return;
72   if ((TypeChar == 'U') && DefinedOnly)
73     return;
74   if (GV.hasInternalLinkage () && ExternalOnly)
75     return;
76   if (OutputFormat == posix) {
77     std::cout << GV.getName () << " " << TypeCharForSymbol (GV) << " "
78               << SymbolAddrStr << "\n";
79   } else if (OutputFormat == bsd) {
80     std::cout << SymbolAddrStr << " " << TypeCharForSymbol (GV) << " "
81               << GV.getName () << "\n";
82   } else if (OutputFormat == sysv) {
83     std::string PaddedName (GV.getName ());
84     while (PaddedName.length () < 20)
85       PaddedName += " ";
86     std::cout << PaddedName << "|" << SymbolAddrStr << "|   "
87               << TypeCharForSymbol (GV)
88               << "  |                  |      |     |\n";
89   }
90 }
91
92 void DumpSymbolNamesFromModule (Module *M) {
93   std::for_each (M->begin (), M->end (), DumpSymbolNameForGlobalValue);
94   std::for_each (M->gbegin (), M->gend (), DumpSymbolNameForGlobalValue);
95 }
96
97 void DumpSymbolNamesFromFile (std::string &Filename) {
98   std::string ErrorMessage;
99   Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
100   if (Result) {
101     if (OutputFormat == posix && MultipleFiles) {
102       std::cout << Filename << ":\n";
103     } else if (OutputFormat == bsd && MultipleFiles) {
104       std::cout << "\n" << Filename << ":\n";
105     } else if (OutputFormat == sysv) {
106       std::cout << "\n\nSymbols from " << Filename << ":\n\n"
107                 << "Name                  Value   Class        Type"
108                 << "         Size   Line  Section\n";
109     }
110     DumpSymbolNamesFromModule (Result);
111   } else {
112     std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
113   }
114 }
115
116 int main(int argc, char **argv) {
117   cl::ParseCommandLineOptions(argc, argv, " llvm symbol table dumper\n");
118   ToolName = argv[0];
119   if (BSDFormat) OutputFormat = bsd;
120   if (POSIXFormat) OutputFormat = posix;
121   if (InputFilenames.size () != 1)
122     MultipleFiles = true;
123
124   std::for_each (InputFilenames.begin (), InputFilenames.end (),
125                  DumpSymbolNamesFromFile);
126   return 0;
127 }