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