blob: 5646835148152a533f244dd9660c60271878a0e6 [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 Gohmanf8b81bf2009-07-15 16:35:29 +000027#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/System/Signals.h"
29#include <algorithm>
30#include <cctype>
31#include <cerrno>
32#include <cstring>
33#include <iostream>
34using namespace llvm;
35
36namespace {
37 enum OutputFormatTy { bsd, sysv, posix };
38 cl::opt<OutputFormatTy>
39 OutputFormat("format",
40 cl::desc("Specify output format"),
41 cl::values(clEnumVal(bsd, "BSD format"),
42 clEnumVal(sysv, "System V format"),
43 clEnumVal(posix, "POSIX.2 format"),
44 clEnumValEnd), cl::init(bsd));
45 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
46 cl::aliasopt(OutputFormat));
47
48 cl::list<std::string>
49 InputFilenames(cl::Positional, cl::desc("<input bitcode files>"),
50 cl::ZeroOrMore);
51
52 cl::opt<bool> UndefinedOnly("undefined-only",
53 cl::desc("Show only undefined symbols"));
54 cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
55 cl::aliasopt(UndefinedOnly));
56
57 cl::opt<bool> DefinedOnly("defined-only",
58 cl::desc("Show only defined symbols"));
59
60 cl::opt<bool> ExternalOnly("extern-only",
61 cl::desc("Show only external symbols"));
62 cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
63 cl::aliasopt(ExternalOnly));
64
65 cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
66 cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
67
68 bool MultipleFiles = false;
69
70 std::string ToolName;
71}
72
73static char TypeCharForSymbol(GlobalValue &GV) {
74 if (GV.isDeclaration()) return 'U';
75 if (GV.hasLinkOnceLinkage()) return 'C';
Dale Johannesen415ec962008-05-16 22:44:18 +000076 if (GV.hasCommonLinkage()) return 'C';
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077 if (GV.hasWeakLinkage()) return 'W';
78 if (isa<Function>(GV) && GV.hasInternalLinkage()) return 't';
79 if (isa<Function>(GV)) return 'T';
80 if (isa<GlobalVariable>(GV) && GV.hasInternalLinkage()) return 'd';
81 if (isa<GlobalVariable>(GV)) return 'D';
82 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(&GV)) {
83 const GlobalValue *AliasedGV = GA->getAliasedGlobal();
84 if (isa<Function>(AliasedGV)) return 'T';
85 if (isa<GlobalVariable>(AliasedGV)) return 'D';
86 }
87 return '?';
88}
89
90static void DumpSymbolNameForGlobalValue(GlobalValue &GV) {
Chris Lattner68433442009-04-13 05:44:34 +000091 // Private linkage and available_externally linkage don't exist in symtab.
92 if (GV.hasPrivateLinkage() || GV.hasAvailableExternallyLinkage()) return;
93
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 const std::string SymbolAddrStr = " "; // Not used yet...
Chris Lattner68433442009-04-13 05:44:34 +000095 char TypeChar = TypeCharForSymbol(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096 if ((TypeChar != 'U') && UndefinedOnly)
97 return;
98 if ((TypeChar == 'U') && DefinedOnly)
99 return;
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000100 if (GV.hasLocalLinkage () && ExternalOnly)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 return;
102 if (OutputFormat == posix) {
Chris Lattner68433442009-04-13 05:44:34 +0000103 std::cout << GV.getName () << " " << TypeCharForSymbol(GV) << " "
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104 << SymbolAddrStr << "\n";
105 } else if (OutputFormat == bsd) {
Chris Lattner68433442009-04-13 05:44:34 +0000106 std::cout << SymbolAddrStr << " " << TypeCharForSymbol(GV) << " "
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 << GV.getName () << "\n";
108 } else if (OutputFormat == sysv) {
109 std::string PaddedName (GV.getName ());
110 while (PaddedName.length () < 20)
111 PaddedName += " ";
112 std::cout << PaddedName << "|" << SymbolAddrStr << "| "
Chris Lattner68433442009-04-13 05:44:34 +0000113 << TypeCharForSymbol(GV)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114 << " | | | |\n";
115 }
116}
117
118static void DumpSymbolNamesFromModule(Module *M) {
119 const std::string &Filename = M->getModuleIdentifier ();
120 if (OutputFormat == posix && MultipleFiles) {
121 std::cout << Filename << ":\n";
122 } else if (OutputFormat == bsd && MultipleFiles) {
123 std::cout << "\n" << Filename << ":\n";
124 } else if (OutputFormat == sysv) {
125 std::cout << "\n\nSymbols from " << Filename << ":\n\n"
126 << "Name Value Class Type"
127 << " Size Line Section\n";
128 }
Chris Lattner68433442009-04-13 05:44:34 +0000129 std::for_each (M->begin(), M->end(), DumpSymbolNameForGlobalValue);
130 std::for_each (M->global_begin(), M->global_end(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 DumpSymbolNameForGlobalValue);
Chris Lattner68433442009-04-13 05:44:34 +0000132 std::for_each (M->alias_begin(), M->alias_end(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133 DumpSymbolNameForGlobalValue);
134}
135
136static void DumpSymbolNamesFromFile(std::string &Filename) {
Owen Anderson25209b42009-07-01 16:58:40 +0000137 LLVMContext Context;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 std::string ErrorMessage;
139 sys::Path aPath(Filename);
140 // Note: Currently we do not support reading an archive from stdin.
141 if (Filename == "-" || aPath.isBitcodeFile()) {
142 std::auto_ptr<MemoryBuffer> Buffer(
143 MemoryBuffer::getFileOrSTDIN(Filename, &ErrorMessage));
144 Module *Result = 0;
145 if (Buffer.get())
Owen Andersona148fdd2009-07-01 21:22:36 +0000146 Result = ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147
148 if (Result)
149 DumpSymbolNamesFromModule(Result);
150 else {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000151 errs() << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152 return;
153 }
154
155 } else if (aPath.isArchive()) {
156 std::string ErrMsg;
Owen Andersona148fdd2009-07-01 21:22:36 +0000157 Archive* archive = Archive::OpenAndLoad(sys::Path(Filename), Context,
Owen Anderson25209b42009-07-01 16:58:40 +0000158 &ErrorMessage);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159 if (!archive)
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000160 errs() << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161 std::vector<Module *> Modules;
162 if (archive->getAllModules(Modules, &ErrorMessage)) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000163 errs() << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164 return;
165 }
166 MultipleFiles = true;
167 std::for_each (Modules.begin(), Modules.end(), DumpSymbolNamesFromModule);
168 } else {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000169 errs() << ToolName << ": " << Filename << ": "
170 << "unrecognizable file type\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 return;
172 }
173}
174
175int main(int argc, char **argv) {
Chris Lattnere6012df2009-03-06 05:34:10 +0000176 // Print a stack trace if we signal out.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 sys::PrintStackTraceOnErrorSignal();
Chris Lattnere6012df2009-03-06 05:34:10 +0000178 PrettyStackTraceProgram X(argc, argv);
179
180 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
181 cl::ParseCommandLineOptions(argc, argv, "llvm symbol table dumper\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182
183 ToolName = argv[0];
184 if (BSDFormat) OutputFormat = bsd;
185 if (POSIXFormat) OutputFormat = posix;
186
187 switch (InputFilenames.size()) {
188 case 0: InputFilenames.push_back("-");
189 case 1: break;
190 default: MultipleFiles = true;
191 }
192
193 std::for_each(InputFilenames.begin(), InputFilenames.end(),
194 DumpSymbolNamesFromFile);
195 return 0;
196}