blob: 324e0f67035c7021686ae7425a6746d6b9e52802 [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
19#include "llvm/Module.h"
20#include "llvm/Bitcode/ReaderWriter.h"
21#include "llvm/Bitcode/Archive.h"
22#include "llvm/Support/CommandLine.h"
23#include "llvm/Support/ManagedStatic.h"
24#include "llvm/Support/MemoryBuffer.h"
Chris Lattnere6012df2009-03-06 05:34:10 +000025#include "llvm/Support/PrettyStackTrace.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026#include "llvm/System/Signals.h"
27#include <algorithm>
28#include <cctype>
29#include <cerrno>
30#include <cstring>
31#include <iostream>
32using namespace llvm;
33
34namespace {
35 enum OutputFormatTy { bsd, sysv, posix };
36 cl::opt<OutputFormatTy>
37 OutputFormat("format",
38 cl::desc("Specify output format"),
39 cl::values(clEnumVal(bsd, "BSD format"),
40 clEnumVal(sysv, "System V format"),
41 clEnumVal(posix, "POSIX.2 format"),
42 clEnumValEnd), cl::init(bsd));
43 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
44 cl::aliasopt(OutputFormat));
45
46 cl::list<std::string>
47 InputFilenames(cl::Positional, cl::desc("<input bitcode files>"),
48 cl::ZeroOrMore);
49
50 cl::opt<bool> UndefinedOnly("undefined-only",
51 cl::desc("Show only undefined symbols"));
52 cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
53 cl::aliasopt(UndefinedOnly));
54
55 cl::opt<bool> DefinedOnly("defined-only",
56 cl::desc("Show only defined symbols"));
57
58 cl::opt<bool> ExternalOnly("extern-only",
59 cl::desc("Show only external symbols"));
60 cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
61 cl::aliasopt(ExternalOnly));
62
63 cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
64 cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
65
66 bool MultipleFiles = false;
67
68 std::string ToolName;
69}
70
71static char TypeCharForSymbol(GlobalValue &GV) {
72 if (GV.isDeclaration()) return 'U';
73 if (GV.hasLinkOnceLinkage()) return 'C';
Dale Johannesen415ec962008-05-16 22:44:18 +000074 if (GV.hasCommonLinkage()) return 'C';
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 if (GV.hasWeakLinkage()) return 'W';
76 if (isa<Function>(GV) && GV.hasInternalLinkage()) return 't';
77 if (isa<Function>(GV)) return 'T';
78 if (isa<GlobalVariable>(GV) && GV.hasInternalLinkage()) return 'd';
79 if (isa<GlobalVariable>(GV)) return 'D';
80 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(&GV)) {
81 const GlobalValue *AliasedGV = GA->getAliasedGlobal();
82 if (isa<Function>(AliasedGV)) return 'T';
83 if (isa<GlobalVariable>(AliasedGV)) return 'D';
84 }
85 return '?';
86}
87
88static void DumpSymbolNameForGlobalValue(GlobalValue &GV) {
Chris Lattner68433442009-04-13 05:44:34 +000089 // Private linkage and available_externally linkage don't exist in symtab.
90 if (GV.hasPrivateLinkage() || GV.hasAvailableExternallyLinkage()) return;
91
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092 const std::string SymbolAddrStr = " "; // Not used yet...
Chris Lattner68433442009-04-13 05:44:34 +000093 char TypeChar = TypeCharForSymbol(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 if ((TypeChar != 'U') && UndefinedOnly)
95 return;
96 if ((TypeChar == 'U') && DefinedOnly)
97 return;
Rafael Espindolaa168fc92009-01-15 20:18:42 +000098 if (GV.hasLocalLinkage () && ExternalOnly)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 return;
100 if (OutputFormat == posix) {
Chris Lattner68433442009-04-13 05:44:34 +0000101 std::cout << GV.getName () << " " << TypeCharForSymbol(GV) << " "
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102 << SymbolAddrStr << "\n";
103 } else if (OutputFormat == bsd) {
Chris Lattner68433442009-04-13 05:44:34 +0000104 std::cout << SymbolAddrStr << " " << TypeCharForSymbol(GV) << " "
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105 << GV.getName () << "\n";
106 } else if (OutputFormat == sysv) {
107 std::string PaddedName (GV.getName ());
108 while (PaddedName.length () < 20)
109 PaddedName += " ";
110 std::cout << PaddedName << "|" << SymbolAddrStr << "| "
Chris Lattner68433442009-04-13 05:44:34 +0000111 << TypeCharForSymbol(GV)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 << " | | | |\n";
113 }
114}
115
116static void DumpSymbolNamesFromModule(Module *M) {
117 const std::string &Filename = M->getModuleIdentifier ();
118 if (OutputFormat == posix && MultipleFiles) {
119 std::cout << Filename << ":\n";
120 } else if (OutputFormat == bsd && MultipleFiles) {
121 std::cout << "\n" << Filename << ":\n";
122 } else if (OutputFormat == sysv) {
123 std::cout << "\n\nSymbols from " << Filename << ":\n\n"
124 << "Name Value Class Type"
125 << " Size Line Section\n";
126 }
Chris Lattner68433442009-04-13 05:44:34 +0000127 std::for_each (M->begin(), M->end(), DumpSymbolNameForGlobalValue);
128 std::for_each (M->global_begin(), M->global_end(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 DumpSymbolNameForGlobalValue);
Chris Lattner68433442009-04-13 05:44:34 +0000130 std::for_each (M->alias_begin(), M->alias_end(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 DumpSymbolNameForGlobalValue);
132}
133
134static void DumpSymbolNamesFromFile(std::string &Filename) {
135 std::string ErrorMessage;
136 sys::Path aPath(Filename);
137 // Note: Currently we do not support reading an archive from stdin.
138 if (Filename == "-" || aPath.isBitcodeFile()) {
139 std::auto_ptr<MemoryBuffer> Buffer(
140 MemoryBuffer::getFileOrSTDIN(Filename, &ErrorMessage));
141 Module *Result = 0;
142 if (Buffer.get())
143 Result = ParseBitcodeFile(Buffer.get(), &ErrorMessage);
144
145 if (Result)
146 DumpSymbolNamesFromModule(Result);
147 else {
148 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
149 return;
150 }
151
152 } else if (aPath.isArchive()) {
153 std::string ErrMsg;
154 Archive* archive = Archive::OpenAndLoad(sys::Path(Filename), &ErrorMessage);
155 if (!archive)
156 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
157 std::vector<Module *> Modules;
158 if (archive->getAllModules(Modules, &ErrorMessage)) {
159 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
160 return;
161 }
162 MultipleFiles = true;
163 std::for_each (Modules.begin(), Modules.end(), DumpSymbolNamesFromModule);
164 } else {
165 std::cerr << ToolName << ": " << Filename << ": "
166 << "unrecognizable file type\n";
167 return;
168 }
169}
170
171int main(int argc, char **argv) {
Chris Lattnere6012df2009-03-06 05:34:10 +0000172 // Print a stack trace if we signal out.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 sys::PrintStackTraceOnErrorSignal();
Chris Lattnere6012df2009-03-06 05:34:10 +0000174 PrettyStackTraceProgram X(argc, argv);
175
176 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
177 cl::ParseCommandLineOptions(argc, argv, "llvm symbol table dumper\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178
179 ToolName = argv[0];
180 if (BSDFormat) OutputFormat = bsd;
181 if (POSIXFormat) OutputFormat = posix;
182
183 switch (InputFilenames.size()) {
184 case 0: InputFilenames.push_back("-");
185 case 1: break;
186 default: MultipleFiles = true;
187 }
188
189 std::for_each(InputFilenames.begin(), InputFilenames.end(),
190 DumpSymbolNamesFromFile);
191 return 0;
192}