blob: 3f19940660e93b0828f4760880ad6a1df8623654 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- llvm-nm.cpp - Symbol table dumping utility for llvm ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5f5a5732007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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.
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
Owen Anderson25209b42009-07-01 16:58:40 +000019#include "llvm/LLVMContext.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#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"
Chris Lattnere6012df2009-03-06 05:34:10 +000026#include "llvm/Support/PrettyStackTrace.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include "llvm/System/Signals.h"
28#include <algorithm>
29#include <cctype>
30#include <cerrno>
31#include <cstring>
32#include <iostream>
33using namespace llvm;
34
35namespace {
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));
46
47 cl::list<std::string>
48 InputFilenames(cl::Positional, cl::desc("<input bitcode files>"),
49 cl::ZeroOrMore);
50
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));
55
56 cl::opt<bool> DefinedOnly("defined-only",
57 cl::desc("Show only defined symbols"));
58
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));
63
64 cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
65 cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
66
67 bool MultipleFiles = false;
68
69 std::string ToolName;
70}
71
72static char TypeCharForSymbol(GlobalValue &GV) {
73 if (GV.isDeclaration()) return 'U';
74 if (GV.hasLinkOnceLinkage()) return 'C';
Dale Johannesen415ec962008-05-16 22:44:18 +000075 if (GV.hasCommonLinkage()) return 'C';
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076 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';
85 }
86 return '?';
87}
88
89static void DumpSymbolNameForGlobalValue(GlobalValue &GV) {
Chris Lattner68433442009-04-13 05:44:34 +000090 // Private linkage and available_externally linkage don't exist in symtab.
91 if (GV.hasPrivateLinkage() || GV.hasAvailableExternallyLinkage()) return;
92
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093 const std::string SymbolAddrStr = " "; // Not used yet...
Chris Lattner68433442009-04-13 05:44:34 +000094 char TypeChar = TypeCharForSymbol(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095 if ((TypeChar != 'U') && UndefinedOnly)
96 return;
97 if ((TypeChar == 'U') && DefinedOnly)
98 return;
Rafael Espindolaa168fc92009-01-15 20:18:42 +000099 if (GV.hasLocalLinkage () && ExternalOnly)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100 return;
101 if (OutputFormat == posix) {
Chris Lattner68433442009-04-13 05:44:34 +0000102 std::cout << GV.getName () << " " << TypeCharForSymbol(GV) << " "
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103 << SymbolAddrStr << "\n";
104 } else if (OutputFormat == bsd) {
Chris Lattner68433442009-04-13 05:44:34 +0000105 std::cout << SymbolAddrStr << " " << TypeCharForSymbol(GV) << " "
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106 << GV.getName () << "\n";
107 } else if (OutputFormat == sysv) {
108 std::string PaddedName (GV.getName ());
109 while (PaddedName.length () < 20)
110 PaddedName += " ";
111 std::cout << PaddedName << "|" << SymbolAddrStr << "| "
Chris Lattner68433442009-04-13 05:44:34 +0000112 << TypeCharForSymbol(GV)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113 << " | | | |\n";
114 }
115}
116
117static void DumpSymbolNamesFromModule(Module *M) {
118 const std::string &Filename = M->getModuleIdentifier ();
119 if (OutputFormat == posix && MultipleFiles) {
120 std::cout << Filename << ":\n";
121 } else if (OutputFormat == bsd && MultipleFiles) {
122 std::cout << "\n" << Filename << ":\n";
123 } else if (OutputFormat == sysv) {
124 std::cout << "\n\nSymbols from " << Filename << ":\n\n"
125 << "Name Value Class Type"
126 << " Size Line Section\n";
127 }
Chris Lattner68433442009-04-13 05:44:34 +0000128 std::for_each (M->begin(), M->end(), DumpSymbolNameForGlobalValue);
129 std::for_each (M->global_begin(), M->global_end(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130 DumpSymbolNameForGlobalValue);
Chris Lattner68433442009-04-13 05:44:34 +0000131 std::for_each (M->alias_begin(), M->alias_end(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132 DumpSymbolNameForGlobalValue);
133}
134
135static void DumpSymbolNamesFromFile(std::string &Filename) {
Owen Anderson25209b42009-07-01 16:58:40 +0000136 LLVMContext Context;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137 std::string ErrorMessage;
138 sys::Path aPath(Filename);
139 // Note: Currently we do not support reading an archive from stdin.
140 if (Filename == "-" || aPath.isBitcodeFile()) {
141 std::auto_ptr<MemoryBuffer> Buffer(
142 MemoryBuffer::getFileOrSTDIN(Filename, &ErrorMessage));
143 Module *Result = 0;
144 if (Buffer.get())
Owen Anderson25209b42009-07-01 16:58:40 +0000145 Result = ParseBitcodeFile(Buffer.get(), &Context, &ErrorMessage);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146
147 if (Result)
148 DumpSymbolNamesFromModule(Result);
149 else {
150 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
151 return;
152 }
153
154 } else if (aPath.isArchive()) {
155 std::string ErrMsg;
Owen Anderson25209b42009-07-01 16:58:40 +0000156 Archive* archive = Archive::OpenAndLoad(sys::Path(Filename), &Context,
157 &ErrorMessage);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 if (!archive)
159 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
160 std::vector<Module *> Modules;
161 if (archive->getAllModules(Modules, &ErrorMessage)) {
162 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
163 return;
164 }
165 MultipleFiles = true;
166 std::for_each (Modules.begin(), Modules.end(), DumpSymbolNamesFromModule);
167 } else {
168 std::cerr << ToolName << ": " << Filename << ": "
169 << "unrecognizable file type\n";
170 return;
171 }
172}
173
174int main(int argc, char **argv) {
Chris Lattnere6012df2009-03-06 05:34:10 +0000175 // Print a stack trace if we signal out.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 sys::PrintStackTraceOnErrorSignal();
Chris Lattnere6012df2009-03-06 05:34:10 +0000177 PrettyStackTraceProgram X(argc, argv);
178
179 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
180 cl::ParseCommandLineOptions(argc, argv, "llvm symbol table dumper\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181
182 ToolName = argv[0];
183 if (BSDFormat) OutputFormat = bsd;
184 if (POSIXFormat) OutputFormat = posix;
185
186 switch (InputFilenames.size()) {
187 case 0: InputFilenames.push_back("-");
188 case 1: break;
189 default: MultipleFiles = true;
190 }
191
192 std::for_each(InputFilenames.begin(), InputFilenames.end(),
193 DumpSymbolNamesFromFile);
194 return 0;
195}