blob: 344951051e83767f6fa207553b1c5f681cc4aefd [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
Brian Gaeke972d3d72003-10-16 04:43:15 +000019#include "llvm/Module.h"
Chris Lattner4d5aad22007-05-06 05:36:18 +000020#include "llvm/Bitcode/ReaderWriter.h"
Chris Lattner44dadff2007-05-06 09:29:57 +000021#include "llvm/Bitcode/Archive.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000022#include "llvm/Support/CommandLine.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000023#include "llvm/Support/ManagedStatic.h"
Chris Lattner4d5aad22007-05-06 05:36:18 +000024#include "llvm/Support/MemoryBuffer.h"
Chris Lattnercc14d252009-03-06 05:34:10 +000025#include "llvm/Support/PrettyStackTrace.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000026#include "llvm/System/Signals.h"
Jeff Cohenca5183d2007-03-05 00:00:42 +000027#include <algorithm>
Brian Gaeke972d3d72003-10-16 04:43:15 +000028#include <cctype>
Alkis Evlogimenos09233fb2004-04-21 16:11:40 +000029#include <cerrno>
Brian Gaeke08d03c72003-11-19 21:52:09 +000030#include <cstring>
Reid Spencer86f42bd2004-07-04 12:20:55 +000031#include <iostream>
Brian Gaeked0fde302003-11-11 22:41:34 +000032using namespace llvm;
33
Brian Gaeke972d3d72003-10-16 04:43:15 +000034namespace {
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"),
Misha Brukman3da94ae2005-04-22 00:00:37 +000041 clEnumVal(posix, "POSIX.2 format"),
Chris Lattner4d143ee2004-07-16 00:08:28 +000042 clEnumValEnd), cl::init(bsd));
Brian Gaeke972d3d72003-10-16 04:43:15 +000043 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
44 cl::aliasopt(OutputFormat));
45
Misha Brukman3da94ae2005-04-22 00:00:37 +000046 cl::list<std::string>
Gabor Greifa99be512007-07-05 17:07:56 +000047 InputFilenames(cl::Positional, cl::desc("<input bitcode files>"),
Chris Lattnerfc046d52003-10-16 18:45:23 +000048 cl::ZeroOrMore);
Brian Gaeke972d3d72003-10-16 04:43:15 +000049
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;
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000069}
Brian Gaeke972d3d72003-10-16 04:43:15 +000070
Chris Lattnerc30598b2006-12-06 01:18:01 +000071static char TypeCharForSymbol(GlobalValue &GV) {
Rafael Espindolabb46f522009-01-15 20:18:42 +000072 /* FIXME: what to do with private linkage? */
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +000073 if (GV.isDeclaration()) return 'U';
Chris Lattnerc30598b2006-12-06 01:18:01 +000074 if (GV.hasLinkOnceLinkage()) return 'C';
Dale Johannesen7d5633e2008-05-16 22:44:18 +000075 if (GV.hasCommonLinkage()) return 'C';
Chris Lattnerc30598b2006-12-06 01:18:01 +000076 if (GV.hasWeakLinkage()) return 'W';
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +000077 if (isa<Function>(GV) && GV.hasInternalLinkage()) return 't';
Chris Lattnerc30598b2006-12-06 01:18:01 +000078 if (isa<Function>(GV)) return 'T';
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +000079 if (isa<GlobalVariable>(GV) && GV.hasInternalLinkage()) return 'd';
Chris Lattnerc30598b2006-12-06 01:18:01 +000080 if (isa<GlobalVariable>(GV)) return 'D';
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +000081 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 '?';
Brian Gaeke972d3d72003-10-16 04:43:15 +000087}
88
Chris Lattnerc30598b2006-12-06 01:18:01 +000089static void DumpSymbolNameForGlobalValue(GlobalValue &GV) {
Brian Gaeke972d3d72003-10-16 04:43:15 +000090 const std::string SymbolAddrStr = " "; // Not used yet...
91 char TypeChar = TypeCharForSymbol (GV);
92 if ((TypeChar != 'U') && UndefinedOnly)
93 return;
94 if ((TypeChar == 'U') && DefinedOnly)
95 return;
Rafael Espindolabb46f522009-01-15 20:18:42 +000096 if (GV.hasLocalLinkage () && ExternalOnly)
Brian Gaeke972d3d72003-10-16 04:43:15 +000097 return;
98 if (OutputFormat == posix) {
99 std::cout << GV.getName () << " " << TypeCharForSymbol (GV) << " "
100 << SymbolAddrStr << "\n";
101 } else if (OutputFormat == bsd) {
102 std::cout << SymbolAddrStr << " " << TypeCharForSymbol (GV) << " "
103 << GV.getName () << "\n";
104 } else if (OutputFormat == sysv) {
105 std::string PaddedName (GV.getName ());
106 while (PaddedName.length () < 20)
107 PaddedName += " ";
108 std::cout << PaddedName << "|" << SymbolAddrStr << "| "
109 << TypeCharForSymbol (GV)
110 << " | | | |\n";
111 }
112}
113
Chris Lattnerc30598b2006-12-06 01:18:01 +0000114static void DumpSymbolNamesFromModule(Module *M) {
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000115 const std::string &Filename = M->getModuleIdentifier ();
116 if (OutputFormat == posix && MultipleFiles) {
117 std::cout << Filename << ":\n";
118 } else if (OutputFormat == bsd && MultipleFiles) {
119 std::cout << "\n" << Filename << ":\n";
120 } else if (OutputFormat == sysv) {
121 std::cout << "\n\nSymbols from " << Filename << ":\n\n"
122 << "Name Value Class Type"
123 << " Size Line Section\n";
124 }
Brian Gaeke972d3d72003-10-16 04:43:15 +0000125 std::for_each (M->begin (), M->end (), DumpSymbolNameForGlobalValue);
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +0000126 std::for_each (M->global_begin (), M->global_end (),
127 DumpSymbolNameForGlobalValue);
128 std::for_each (M->alias_begin (), M->alias_end (),
129 DumpSymbolNameForGlobalValue);
Brian Gaeke972d3d72003-10-16 04:43:15 +0000130}
131
Chris Lattnerc30598b2006-12-06 01:18:01 +0000132static void DumpSymbolNamesFromFile(std::string &Filename) {
Brian Gaeke972d3d72003-10-16 04:43:15 +0000133 std::string ErrorMessage;
Reid Spencer11db4b82004-12-13 03:01:26 +0000134 sys::Path aPath(Filename);
Brian Gaeke8b1daa32003-11-19 22:15:00 +0000135 // Note: Currently we do not support reading an archive from stdin.
Chris Lattner44dadff2007-05-06 09:29:57 +0000136 if (Filename == "-" || aPath.isBitcodeFile()) {
Chris Lattner4d5aad22007-05-06 05:36:18 +0000137 std::auto_ptr<MemoryBuffer> Buffer(
Chris Lattner065344d2007-05-06 23:45:49 +0000138 MemoryBuffer::getFileOrSTDIN(Filename, &ErrorMessage));
Chris Lattner4d5aad22007-05-06 05:36:18 +0000139 Module *Result = 0;
140 if (Buffer.get())
141 Result = ParseBitcodeFile(Buffer.get(), &ErrorMessage);
142
143 if (Result)
144 DumpSymbolNamesFromModule(Result);
145 else {
146 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
147 return;
148 }
149
Reid Spencer11db4b82004-12-13 03:01:26 +0000150 } else if (aPath.isArchive()) {
Reid Spencer8d8a7ff2006-07-07 20:56:50 +0000151 std::string ErrMsg;
Chris Lattnerc30598b2006-12-06 01:18:01 +0000152 Archive* archive = Archive::OpenAndLoad(sys::Path(Filename), &ErrorMessage);
Reid Spencer63efb772004-11-14 22:27:46 +0000153 if (!archive)
154 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000155 std::vector<Module *> Modules;
Chris Lattnerc30598b2006-12-06 01:18:01 +0000156 if (archive->getAllModules(Modules, &ErrorMessage)) {
Reid Spencer63efb772004-11-14 22:27:46 +0000157 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
Brian Gaeke08d03c72003-11-19 21:52:09 +0000158 return;
159 }
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000160 MultipleFiles = true;
Chris Lattnerc30598b2006-12-06 01:18:01 +0000161 std::for_each (Modules.begin(), Modules.end(), DumpSymbolNamesFromModule);
Brian Gaeke82042872003-11-19 21:57:30 +0000162 } else {
163 std::cerr << ToolName << ": " << Filename << ": "
164 << "unrecognizable file type\n";
165 return;
Brian Gaeke972d3d72003-10-16 04:43:15 +0000166 }
167}
168
169int main(int argc, char **argv) {
Chris Lattnercc14d252009-03-06 05:34:10 +0000170 // Print a stack trace if we signal out.
Chris Lattner4d5aad22007-05-06 05:36:18 +0000171 sys::PrintStackTraceOnErrorSignal();
Chris Lattnercc14d252009-03-06 05:34:10 +0000172 PrettyStackTraceProgram X(argc, argv);
173
174 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
175 cl::ParseCommandLineOptions(argc, argv, "llvm symbol table dumper\n");
Chris Lattnerf73b4ca2004-02-19 20:32:12 +0000176
Chris Lattner4d5aad22007-05-06 05:36:18 +0000177 ToolName = argv[0];
178 if (BSDFormat) OutputFormat = bsd;
179 if (POSIXFormat) OutputFormat = posix;
Chris Lattnerfc046d52003-10-16 18:45:23 +0000180
Chris Lattner4d5aad22007-05-06 05:36:18 +0000181 switch (InputFilenames.size()) {
182 case 0: InputFilenames.push_back("-");
183 case 1: break;
184 default: MultipleFiles = true;
Chris Lattnerfc046d52003-10-16 18:45:23 +0000185 }
Chris Lattner4d5aad22007-05-06 05:36:18 +0000186
187 std::for_each(InputFilenames.begin(), InputFilenames.end(),
188 DumpSymbolNamesFromFile);
189 return 0;
Brian Gaeke972d3d72003-10-16 04:43:15 +0000190}