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