Mark variable 'NoImplicitFloatOps' used only in an assert as used.
[llvm.git] / tools / llvm-nm / llvm-nm.cpp
blobdaa85712379a299f0359775dc616fc91a901cc7b
1 //===-- llvm-nm.cpp - Symbol table dumping utility for llvm ---------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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 bitcode file,
12 // along with some information about each symbol.
14 // This "nm" does not print symbols' addresses. It supports many of
15 // the features of GNU "nm", including its different output formats.
17 //===----------------------------------------------------------------------===//
19 #include "llvm/LLVMContext.h"
20 #include "llvm/Module.h"
21 #include "llvm/Bitcode/ReaderWriter.h"
22 #include "llvm/Bitcode/Archive.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/ManagedStatic.h"
25 #include "llvm/Support/MemoryBuffer.h"
26 #include "llvm/Support/PrettyStackTrace.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/System/Signals.h"
29 #include <algorithm>
30 #include <cctype>
31 #include <cerrno>
32 #include <cstring>
33 using namespace llvm;
35 namespace {
36 enum OutputFormatTy { bsd, sysv, posix };
37 cl::opt<OutputFormatTy>
38 OutputFormat("format",
39 cl::desc("Specify output format"),
40 cl::values(clEnumVal(bsd, "BSD format"),
41 clEnumVal(sysv, "System V format"),
42 clEnumVal(posix, "POSIX.2 format"),
43 clEnumValEnd), cl::init(bsd));
44 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
45 cl::aliasopt(OutputFormat));
47 cl::list<std::string>
48 InputFilenames(cl::Positional, cl::desc("<input bitcode files>"),
49 cl::ZeroOrMore);
51 cl::opt<bool> UndefinedOnly("undefined-only",
52 cl::desc("Show only undefined symbols"));
53 cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
54 cl::aliasopt(UndefinedOnly));
56 cl::opt<bool> DefinedOnly("defined-only",
57 cl::desc("Show only defined symbols"));
59 cl::opt<bool> ExternalOnly("extern-only",
60 cl::desc("Show only external symbols"));
61 cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
62 cl::aliasopt(ExternalOnly));
64 cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
65 cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
67 bool MultipleFiles = false;
69 std::string ToolName;
72 static char TypeCharForSymbol(GlobalValue &GV) {
73 if (GV.isDeclaration()) return 'U';
74 if (GV.hasLinkOnceLinkage()) return 'C';
75 if (GV.hasCommonLinkage()) return 'C';
76 if (GV.hasWeakLinkage()) return 'W';
77 if (isa<Function>(GV) && GV.hasInternalLinkage()) return 't';
78 if (isa<Function>(GV)) return 'T';
79 if (isa<GlobalVariable>(GV) && GV.hasInternalLinkage()) return 'd';
80 if (isa<GlobalVariable>(GV)) return 'D';
81 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(&GV)) {
82 const GlobalValue *AliasedGV = GA->getAliasedGlobal();
83 if (isa<Function>(AliasedGV)) return 'T';
84 if (isa<GlobalVariable>(AliasedGV)) return 'D';
86 return '?';
89 static void DumpSymbolNameForGlobalValue(GlobalValue &GV) {
90 // Private linkage and available_externally linkage don't exist in symtab.
91 if (GV.hasPrivateLinkage() ||
92 GV.hasLinkerPrivateLinkage() ||
93 GV.hasLinkerPrivateWeakLinkage() ||
94 GV.hasLinkerPrivateWeakDefAutoLinkage() ||
95 GV.hasAvailableExternallyLinkage())
96 return;
98 const std::string SymbolAddrStr = " "; // Not used yet...
99 char TypeChar = TypeCharForSymbol(GV);
100 if ((TypeChar != 'U') && UndefinedOnly)
101 return;
102 if ((TypeChar == 'U') && DefinedOnly)
103 return;
104 if (GV.hasLocalLinkage () && ExternalOnly)
105 return;
106 if (OutputFormat == posix) {
107 outs() << GV.getName () << " " << TypeCharForSymbol(GV) << " "
108 << SymbolAddrStr << "\n";
109 } else if (OutputFormat == bsd) {
110 outs() << SymbolAddrStr << " " << TypeCharForSymbol(GV) << " "
111 << GV.getName () << "\n";
112 } else if (OutputFormat == sysv) {
113 std::string PaddedName (GV.getName ());
114 while (PaddedName.length () < 20)
115 PaddedName += " ";
116 outs() << PaddedName << "|" << SymbolAddrStr << "| "
117 << TypeCharForSymbol(GV)
118 << " | | | |\n";
122 static void DumpSymbolNamesFromModule(Module *M) {
123 const std::string &Filename = M->getModuleIdentifier ();
124 if (OutputFormat == posix && MultipleFiles) {
125 outs() << Filename << ":\n";
126 } else if (OutputFormat == bsd && MultipleFiles) {
127 outs() << "\n" << Filename << ":\n";
128 } else if (OutputFormat == sysv) {
129 outs() << "\n\nSymbols from " << Filename << ":\n\n"
130 << "Name Value Class Type"
131 << " Size Line Section\n";
133 std::for_each (M->begin(), M->end(), DumpSymbolNameForGlobalValue);
134 std::for_each (M->global_begin(), M->global_end(),
135 DumpSymbolNameForGlobalValue);
136 std::for_each (M->alias_begin(), M->alias_end(),
137 DumpSymbolNameForGlobalValue);
140 static void DumpSymbolNamesFromFile(std::string &Filename) {
141 LLVMContext &Context = getGlobalContext();
142 std::string ErrorMessage;
143 sys::Path aPath(Filename);
144 // Note: Currently we do not support reading an archive from stdin.
145 if (Filename == "-" || aPath.isBitcodeFile()) {
146 std::auto_ptr<MemoryBuffer> Buffer(
147 MemoryBuffer::getFileOrSTDIN(Filename, &ErrorMessage));
148 Module *Result = 0;
149 if (Buffer.get())
150 Result = ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage);
152 if (Result) {
153 DumpSymbolNamesFromModule(Result);
154 delete Result;
155 } else
156 errs() << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
158 } else if (aPath.isArchive()) {
159 std::string ErrMsg;
160 Archive* archive = Archive::OpenAndLoad(sys::Path(Filename), Context,
161 &ErrorMessage);
162 if (!archive)
163 errs() << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
164 std::vector<Module *> Modules;
165 if (archive->getAllModules(Modules, &ErrorMessage)) {
166 errs() << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
167 return;
169 MultipleFiles = true;
170 std::for_each (Modules.begin(), Modules.end(), DumpSymbolNamesFromModule);
171 } else {
172 errs() << ToolName << ": " << Filename << ": "
173 << "unrecognizable file type\n";
174 return;
178 int main(int argc, char **argv) {
179 // Print a stack trace if we signal out.
180 sys::PrintStackTraceOnErrorSignal();
181 PrettyStackTraceProgram X(argc, argv);
183 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
184 cl::ParseCommandLineOptions(argc, argv, "llvm symbol table dumper\n");
186 ToolName = argv[0];
187 if (BSDFormat) OutputFormat = bsd;
188 if (POSIXFormat) OutputFormat = posix;
190 switch (InputFilenames.size()) {
191 case 0: InputFilenames.push_back("-");
192 case 1: break;
193 default: MultipleFiles = true;
196 std::for_each(InputFilenames.begin(), InputFilenames.end(),
197 DumpSymbolNamesFromFile);
198 return 0;