blob: 7daf792c718bcb4ea115815be94a5170e593a82c [file] [log] [blame]
Brian Gaeke972d3d72003-10-16 04:43:15 +00001//===-- llvm-nm.cpp - Symbol table dumping utility for llvm ---------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Brian Gaeke972d3d72003-10-16 04:43:15 +00009//
10// This program is a utility that works like traditional Unix "nm",
Gabor Greifa99be512007-07-05 17:07:56 +000011// that is, it prints out the names of symbols in a bitcode file,
Brian Gaeke972d3d72003-10-16 04:43:15 +000012// along with some information about each symbol.
Misha Brukman3da94ae2005-04-22 00:00:37 +000013//
Brian Gaeke972d3d72003-10-16 04:43:15 +000014// 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 Anderson8b477ed2009-07-01 16:58:40 +000019#include "llvm/LLVMContext.h"
Brian Gaeke972d3d72003-10-16 04:43:15 +000020#include "llvm/Module.h"
Chris Lattner4d5aad22007-05-06 05:36:18 +000021#include "llvm/Bitcode/ReaderWriter.h"
Chris Lattner44dadff2007-05-06 09:29:57 +000022#include "llvm/Bitcode/Archive.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000023#include "llvm/Support/CommandLine.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000024#include "llvm/Support/ManagedStatic.h"
Chris Lattner4d5aad22007-05-06 05:36:18 +000025#include "llvm/Support/MemoryBuffer.h"
Chris Lattnercc14d252009-03-06 05:34:10 +000026#include "llvm/Support/PrettyStackTrace.h"
Dan Gohman65f57c22009-07-15 16:35:29 +000027#include "llvm/Support/raw_ostream.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000028#include "llvm/Support/Signals.h"
Michael J. Spencer333fb042010-12-09 17:36:48 +000029#include "llvm/Support/system_error.h"
Jeff Cohenca5183d2007-03-05 00:00:42 +000030#include <algorithm>
Brian Gaeke972d3d72003-10-16 04:43:15 +000031#include <cctype>
Alkis Evlogimenos09233fb2004-04-21 16:11:40 +000032#include <cerrno>
Brian Gaeke08d03c72003-11-19 21:52:09 +000033#include <cstring>
Brian Gaeked0fde302003-11-11 22:41:34 +000034using namespace llvm;
35
Brian Gaeke972d3d72003-10-16 04:43:15 +000036namespace {
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"),
Misha Brukman3da94ae2005-04-22 00:00:37 +000043 clEnumVal(posix, "POSIX.2 format"),
Chris Lattner4d143ee2004-07-16 00:08:28 +000044 clEnumValEnd), cl::init(bsd));
Brian Gaeke972d3d72003-10-16 04:43:15 +000045 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
46 cl::aliasopt(OutputFormat));
47
Misha Brukman3da94ae2005-04-22 00:00:37 +000048 cl::list<std::string>
Gabor Greifa99be512007-07-05 17:07:56 +000049 InputFilenames(cl::Positional, cl::desc("<input bitcode files>"),
Chris Lattnerfc046d52003-10-16 18:45:23 +000050 cl::ZeroOrMore);
Brian Gaeke972d3d72003-10-16 04:43:15 +000051
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;
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000071}
Brian Gaeke972d3d72003-10-16 04:43:15 +000072
Chris Lattnerc30598b2006-12-06 01:18:01 +000073static char TypeCharForSymbol(GlobalValue &GV) {
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +000074 if (GV.isDeclaration()) return 'U';
Chris Lattnerc30598b2006-12-06 01:18:01 +000075 if (GV.hasLinkOnceLinkage()) return 'C';
Dale Johannesen7d5633e2008-05-16 22:44:18 +000076 if (GV.hasCommonLinkage()) return 'C';
Chris Lattnerc30598b2006-12-06 01:18:01 +000077 if (GV.hasWeakLinkage()) return 'W';
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +000078 if (isa<Function>(GV) && GV.hasInternalLinkage()) return 't';
Chris Lattnerc30598b2006-12-06 01:18:01 +000079 if (isa<Function>(GV)) return 'T';
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +000080 if (isa<GlobalVariable>(GV) && GV.hasInternalLinkage()) return 'd';
Chris Lattnerc30598b2006-12-06 01:18:01 +000081 if (isa<GlobalVariable>(GV)) return 'D';
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +000082 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 '?';
Brian Gaeke972d3d72003-10-16 04:43:15 +000088}
89
Chris Lattnerc30598b2006-12-06 01:18:01 +000090static void DumpSymbolNameForGlobalValue(GlobalValue &GV) {
Chris Lattner266c7bb2009-04-13 05:44:34 +000091 // Private linkage and available_externally linkage don't exist in symtab.
Bill Wendling4e34d502010-08-24 20:00:52 +000092 if (GV.hasPrivateLinkage() ||
93 GV.hasLinkerPrivateLinkage() ||
94 GV.hasLinkerPrivateWeakLinkage() ||
95 GV.hasLinkerPrivateWeakDefAutoLinkage() ||
96 GV.hasAvailableExternallyLinkage())
Bill Wendling5e721d72010-07-01 21:55:59 +000097 return;
Michael J. Spencer4a295d32010-08-31 06:36:46 +000098
Brian Gaeke972d3d72003-10-16 04:43:15 +000099 const std::string SymbolAddrStr = " "; // Not used yet...
Chris Lattner266c7bb2009-04-13 05:44:34 +0000100 char TypeChar = TypeCharForSymbol(GV);
Brian Gaeke972d3d72003-10-16 04:43:15 +0000101 if ((TypeChar != 'U') && UndefinedOnly)
102 return;
103 if ((TypeChar == 'U') && DefinedOnly)
104 return;
Rafael Espindolabb46f522009-01-15 20:18:42 +0000105 if (GV.hasLocalLinkage () && ExternalOnly)
Brian Gaeke972d3d72003-10-16 04:43:15 +0000106 return;
107 if (OutputFormat == posix) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000108 outs() << GV.getName () << " " << TypeCharForSymbol(GV) << " "
109 << SymbolAddrStr << "\n";
Brian Gaeke972d3d72003-10-16 04:43:15 +0000110 } else if (OutputFormat == bsd) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000111 outs() << SymbolAddrStr << " " << TypeCharForSymbol(GV) << " "
112 << GV.getName () << "\n";
Brian Gaeke972d3d72003-10-16 04:43:15 +0000113 } else if (OutputFormat == sysv) {
114 std::string PaddedName (GV.getName ());
115 while (PaddedName.length () < 20)
116 PaddedName += " ";
Dan Gohmanac95cc72009-07-16 15:30:09 +0000117 outs() << PaddedName << "|" << SymbolAddrStr << "| "
118 << TypeCharForSymbol(GV)
119 << " | | | |\n";
Brian Gaeke972d3d72003-10-16 04:43:15 +0000120 }
121}
122
Chris Lattnerc30598b2006-12-06 01:18:01 +0000123static void DumpSymbolNamesFromModule(Module *M) {
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000124 const std::string &Filename = M->getModuleIdentifier ();
125 if (OutputFormat == posix && MultipleFiles) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000126 outs() << Filename << ":\n";
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000127 } else if (OutputFormat == bsd && MultipleFiles) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000128 outs() << "\n" << Filename << ":\n";
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000129 } else if (OutputFormat == sysv) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000130 outs() << "\n\nSymbols from " << Filename << ":\n\n"
131 << "Name Value Class Type"
132 << " Size Line Section\n";
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000133 }
Chris Lattner266c7bb2009-04-13 05:44:34 +0000134 std::for_each (M->begin(), M->end(), DumpSymbolNameForGlobalValue);
135 std::for_each (M->global_begin(), M->global_end(),
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +0000136 DumpSymbolNameForGlobalValue);
Chris Lattner266c7bb2009-04-13 05:44:34 +0000137 std::for_each (M->alias_begin(), M->alias_end(),
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +0000138 DumpSymbolNameForGlobalValue);
Brian Gaeke972d3d72003-10-16 04:43:15 +0000139}
140
Chris Lattnerc30598b2006-12-06 01:18:01 +0000141static void DumpSymbolNamesFromFile(std::string &Filename) {
Owen Anderson0d7c6952009-07-15 22:16:10 +0000142 LLVMContext &Context = getGlobalContext();
Brian Gaeke972d3d72003-10-16 04:43:15 +0000143 std::string ErrorMessage;
Reid Spencer11db4b82004-12-13 03:01:26 +0000144 sys::Path aPath(Filename);
Brian Gaeke8b1daa32003-11-19 22:15:00 +0000145 // Note: Currently we do not support reading an archive from stdin.
Chris Lattner44dadff2007-05-06 09:29:57 +0000146 if (Filename == "-" || aPath.isBitcodeFile()) {
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000147 OwningPtr<MemoryBuffer> Buffer;
148 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buffer))
Michael J. Spencer333fb042010-12-09 17:36:48 +0000149 ErrorMessage = ec.message();
Chris Lattner4d5aad22007-05-06 05:36:18 +0000150 Module *Result = 0;
151 if (Buffer.get())
Owen Anderson31895e72009-07-01 21:22:36 +0000152 Result = ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage);
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000153
Nuno Lopes8018f5d2009-09-10 14:56:31 +0000154 if (Result) {
Chris Lattner4d5aad22007-05-06 05:36:18 +0000155 DumpSymbolNamesFromModule(Result);
Nuno Lopes8018f5d2009-09-10 14:56:31 +0000156 delete Result;
157 } else
Dan Gohman65f57c22009-07-15 16:35:29 +0000158 errs() << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000159
Reid Spencer11db4b82004-12-13 03:01:26 +0000160 } else if (aPath.isArchive()) {
Reid Spencer8d8a7ff2006-07-07 20:56:50 +0000161 std::string ErrMsg;
Owen Anderson31895e72009-07-01 21:22:36 +0000162 Archive* archive = Archive::OpenAndLoad(sys::Path(Filename), Context,
Owen Anderson8b477ed2009-07-01 16:58:40 +0000163 &ErrorMessage);
Reid Spencer63efb772004-11-14 22:27:46 +0000164 if (!archive)
Dan Gohman65f57c22009-07-15 16:35:29 +0000165 errs() << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000166 std::vector<Module *> Modules;
Chris Lattnerc30598b2006-12-06 01:18:01 +0000167 if (archive->getAllModules(Modules, &ErrorMessage)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000168 errs() << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
Brian Gaeke08d03c72003-11-19 21:52:09 +0000169 return;
170 }
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000171 MultipleFiles = true;
Chris Lattnerc30598b2006-12-06 01:18:01 +0000172 std::for_each (Modules.begin(), Modules.end(), DumpSymbolNamesFromModule);
Brian Gaeke82042872003-11-19 21:57:30 +0000173 } else {
Dan Gohman65f57c22009-07-15 16:35:29 +0000174 errs() << ToolName << ": " << Filename << ": "
175 << "unrecognizable file type\n";
Brian Gaeke82042872003-11-19 21:57:30 +0000176 return;
Brian Gaeke972d3d72003-10-16 04:43:15 +0000177 }
178}
179
180int main(int argc, char **argv) {
Chris Lattnercc14d252009-03-06 05:34:10 +0000181 // Print a stack trace if we signal out.
Chris Lattner4d5aad22007-05-06 05:36:18 +0000182 sys::PrintStackTraceOnErrorSignal();
Chris Lattnercc14d252009-03-06 05:34:10 +0000183 PrettyStackTraceProgram X(argc, argv);
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000184
Chris Lattnercc14d252009-03-06 05:34:10 +0000185 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
186 cl::ParseCommandLineOptions(argc, argv, "llvm symbol table dumper\n");
Chris Lattnerf73b4ca2004-02-19 20:32:12 +0000187
Chris Lattner4d5aad22007-05-06 05:36:18 +0000188 ToolName = argv[0];
189 if (BSDFormat) OutputFormat = bsd;
190 if (POSIXFormat) OutputFormat = posix;
Chris Lattnerfc046d52003-10-16 18:45:23 +0000191
Chris Lattner4d5aad22007-05-06 05:36:18 +0000192 switch (InputFilenames.size()) {
193 case 0: InputFilenames.push_back("-");
194 case 1: break;
195 default: MultipleFiles = true;
Chris Lattnerfc046d52003-10-16 18:45:23 +0000196 }
Chris Lattner4d5aad22007-05-06 05:36:18 +0000197
198 std::for_each(InputFilenames.begin(), InputFilenames.end(),
199 DumpSymbolNamesFromFile);
200 return 0;
Brian Gaeke972d3d72003-10-16 04:43:15 +0000201}