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