ec024f9b02b0c1d6ac8aaf98806e795933072bec
[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 <cctype>
24
25 using namespace llvm;
26
27 namespace {
28   enum OutputFormatTy { bsd, sysv, posix };
29   cl::opt<OutputFormatTy>
30   OutputFormat("format",
31        cl::desc("Specify output format"),
32          cl::values(clEnumVal(bsd,   "BSD format"),
33                     clEnumVal(sysv,  "System V format"),
34                     clEnumVal(posix, "POSIX.2 format"), 0), cl::init(bsd));
35   cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
36                           cl::aliasopt(OutputFormat));
37
38   cl::list<std::string> 
39   InputFilenames(cl::Positional, cl::desc("<input bytecode files>"),
40                  cl::ZeroOrMore);
41
42   cl::opt<bool> UndefinedOnly("undefined-only",
43                               cl::desc("Show only undefined symbols"));
44   cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
45                            cl::aliasopt(UndefinedOnly));
46
47   cl::opt<bool> DefinedOnly("defined-only",
48                             cl::desc("Show only defined symbols"));
49
50   cl::opt<bool> ExternalOnly("extern-only",
51                              cl::desc("Show only external symbols"));
52   cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
53                           cl::aliasopt(ExternalOnly));
54
55   cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
56   cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
57
58   bool MultipleFiles = false;
59
60   std::string ToolName;
61 };
62
63 char TypeCharForSymbol (GlobalValue &GV) {
64   if (GV.isExternal ())                                     return 'U';
65   if (GV.hasLinkOnceLinkage ())                             return 'C';
66   if (GV.hasWeakLinkage ())                                 return 'W';
67   if (isa<Function> (GV) && GV.hasInternalLinkage ())       return 't';
68   if (isa<Function> (GV))                                   return 'T';
69   if (isa<GlobalVariable> (GV) && GV.hasInternalLinkage ()) return 'd';
70   if (isa<GlobalVariable> (GV))                             return 'D';
71                                                             return '?';
72 }
73
74 void DumpSymbolNameForGlobalValue (GlobalValue &GV) {
75   const std::string SymbolAddrStr = "        "; // Not used yet...
76   char TypeChar = TypeCharForSymbol (GV);
77   if ((TypeChar != 'U') && UndefinedOnly)
78     return;
79   if ((TypeChar == 'U') && DefinedOnly)
80     return;
81   if (GV.hasInternalLinkage () && ExternalOnly)
82     return;
83   if (OutputFormat == posix) {
84     std::cout << GV.getName () << " " << TypeCharForSymbol (GV) << " "
85               << SymbolAddrStr << "\n";
86   } else if (OutputFormat == bsd) {
87     std::cout << SymbolAddrStr << " " << TypeCharForSymbol (GV) << " "
88               << GV.getName () << "\n";
89   } else if (OutputFormat == sysv) {
90     std::string PaddedName (GV.getName ());
91     while (PaddedName.length () < 20)
92       PaddedName += " ";
93     std::cout << PaddedName << "|" << SymbolAddrStr << "|   "
94               << TypeCharForSymbol (GV)
95               << "  |                  |      |     |\n";
96   }
97 }
98
99 void DumpSymbolNamesFromModule (Module *M) {
100   const std::string &Filename = M->getModuleIdentifier ();
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   std::for_each (M->begin (), M->end (), DumpSymbolNameForGlobalValue);
111   std::for_each (M->gbegin (), M->gend (), DumpSymbolNameForGlobalValue);
112 }
113
114 void DumpSymbolNamesFromFile (std::string &Filename) {
115   std::string ErrorMessage;
116   if (IsBytecode (Filename)) {
117     Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
118     if (Result) {
119       DumpSymbolNamesFromModule (Result);
120     } else {
121       std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
122     }
123   } else if (IsArchive (Filename)) {
124     std::vector<Module *> Modules;
125     if (ReadArchiveFile (Filename, Modules, &ErrorMessage))
126       std::cerr << ToolName << ": " << Filename << ": "
127                 << ErrorMessage << "\n";
128     MultipleFiles = true;
129     std::for_each (Modules.begin (), Modules.end (), DumpSymbolNamesFromModule);
130   }
131 }
132
133 int main(int argc, char **argv) {
134   cl::ParseCommandLineOptions(argc, argv, " llvm symbol table dumper\n");
135   ToolName = argv[0];
136   if (BSDFormat) OutputFormat = bsd;
137   if (POSIXFormat) OutputFormat = posix;
138
139   switch (InputFilenames.size()) {
140   case 0: InputFilenames.push_back("-");
141   case 1: break;
142   default: MultipleFiles = true;
143   }
144
145   std::for_each (InputFilenames.begin (), InputFilenames.end (),
146                  DumpSymbolNamesFromFile);
147   return 0;
148 }