blob: daac355c0fe9c897c44bf53de853d0971311e70a [file] [log] [blame]
Brian Gaeke972d3d72003-10-16 04:43:15 +00001//===-- llvm-nm.cpp - Symbol table dumping utility for llvm ---------------===//
John Criswell7c0e0222003-10-20 17:47:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Brian Gaeke972d3d72003-10-16 04:43:15 +00009//
10// This program is a utility that works like traditional Unix "nm",
11// that is, it prints out the names of symbols in a bytecode 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
Brian Gaeke972d3d72003-10-16 04:43:15 +000019#include "llvm/Module.h"
Chris Lattner08020c12003-10-28 19:08:15 +000020#include "llvm/Bytecode/Reader.h"
Reid Spencer63efb772004-11-14 22:27:46 +000021#include "llvm/Bytecode/Archive.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000022#include "llvm/Support/CommandLine.h"
23#include "llvm/Support/FileUtilities.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000024#include "llvm/System/Signals.h"
Brian Gaeke972d3d72003-10-16 04:43:15 +000025#include <cctype>
Alkis Evlogimenos09233fb2004-04-21 16:11:40 +000026#include <cerrno>
Brian Gaeke08d03c72003-11-19 21:52:09 +000027#include <cstring>
Reid Spencer86f42bd2004-07-04 12:20:55 +000028#include <iostream>
Brian Gaeke972d3d72003-10-16 04:43:15 +000029
Brian Gaeked0fde302003-11-11 22:41:34 +000030using namespace llvm;
31
Brian Gaeke972d3d72003-10-16 04:43:15 +000032namespace {
33 enum OutputFormatTy { bsd, sysv, posix };
34 cl::opt<OutputFormatTy>
35 OutputFormat("format",
36 cl::desc("Specify output format"),
37 cl::values(clEnumVal(bsd, "BSD format"),
38 clEnumVal(sysv, "System V format"),
Chris Lattner4d143ee2004-07-16 00:08:28 +000039 clEnumVal(posix, "POSIX.2 format"),
40 clEnumValEnd), cl::init(bsd));
Brian Gaeke972d3d72003-10-16 04:43:15 +000041 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
42 cl::aliasopt(OutputFormat));
43
44 cl::list<std::string>
45 InputFilenames(cl::Positional, cl::desc("<input bytecode files>"),
Chris Lattnerfc046d52003-10-16 18:45:23 +000046 cl::ZeroOrMore);
Brian Gaeke972d3d72003-10-16 04:43:15 +000047
48 cl::opt<bool> UndefinedOnly("undefined-only",
49 cl::desc("Show only undefined symbols"));
50 cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
51 cl::aliasopt(UndefinedOnly));
52
53 cl::opt<bool> DefinedOnly("defined-only",
54 cl::desc("Show only defined symbols"));
55
56 cl::opt<bool> ExternalOnly("extern-only",
57 cl::desc("Show only external symbols"));
58 cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
59 cl::aliasopt(ExternalOnly));
60
61 cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
62 cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
63
64 bool MultipleFiles = false;
65
66 std::string ToolName;
67};
68
69char TypeCharForSymbol (GlobalValue &GV) {
70 if (GV.isExternal ()) return 'U';
71 if (GV.hasLinkOnceLinkage ()) return 'C';
Brian Gaeke972d3d72003-10-16 04:43:15 +000072 if (GV.hasWeakLinkage ()) return 'W';
Brian Gaeke972d3d72003-10-16 04:43:15 +000073 if (isa<Function> (GV) && GV.hasInternalLinkage ()) return 't';
74 if (isa<Function> (GV)) return 'T';
75 if (isa<GlobalVariable> (GV) && GV.hasInternalLinkage ()) return 'd';
76 if (isa<GlobalVariable> (GV)) return 'D';
77 return '?';
78}
79
80void DumpSymbolNameForGlobalValue (GlobalValue &GV) {
81 const std::string SymbolAddrStr = " "; // Not used yet...
82 char TypeChar = TypeCharForSymbol (GV);
83 if ((TypeChar != 'U') && UndefinedOnly)
84 return;
85 if ((TypeChar == 'U') && DefinedOnly)
86 return;
87 if (GV.hasInternalLinkage () && ExternalOnly)
88 return;
89 if (OutputFormat == posix) {
90 std::cout << GV.getName () << " " << TypeCharForSymbol (GV) << " "
91 << SymbolAddrStr << "\n";
92 } else if (OutputFormat == bsd) {
93 std::cout << SymbolAddrStr << " " << TypeCharForSymbol (GV) << " "
94 << GV.getName () << "\n";
95 } else if (OutputFormat == sysv) {
96 std::string PaddedName (GV.getName ());
97 while (PaddedName.length () < 20)
98 PaddedName += " ";
99 std::cout << PaddedName << "|" << SymbolAddrStr << "| "
100 << TypeCharForSymbol (GV)
101 << " | | | |\n";
102 }
103}
104
105void DumpSymbolNamesFromModule (Module *M) {
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000106 const std::string &Filename = M->getModuleIdentifier ();
107 if (OutputFormat == posix && MultipleFiles) {
108 std::cout << Filename << ":\n";
109 } else if (OutputFormat == bsd && MultipleFiles) {
110 std::cout << "\n" << Filename << ":\n";
111 } else if (OutputFormat == sysv) {
112 std::cout << "\n\nSymbols from " << Filename << ":\n\n"
113 << "Name Value Class Type"
114 << " Size Line Section\n";
115 }
Brian Gaeke972d3d72003-10-16 04:43:15 +0000116 std::for_each (M->begin (), M->end (), DumpSymbolNameForGlobalValue);
117 std::for_each (M->gbegin (), M->gend (), DumpSymbolNameForGlobalValue);
118}
119
120void DumpSymbolNamesFromFile (std::string &Filename) {
121 std::string ErrorMessage;
Brian Gaeke8b1daa32003-11-19 22:15:00 +0000122 if (Filename != "-" && !FileOpenable (Filename)) {
Brian Gaeke08d03c72003-11-19 21:52:09 +0000123 std::cerr << ToolName << ": " << Filename << ": " << strerror (errno)
124 << "\n";
125 return;
126 }
Brian Gaeke8b1daa32003-11-19 22:15:00 +0000127 // Note: Currently we do not support reading an archive from stdin.
128 if (Filename == "-" || IsBytecode (Filename)) {
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000129 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
130 if (Result) {
131 DumpSymbolNamesFromModule (Result);
132 } else {
133 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
Brian Gaeke08d03c72003-11-19 21:52:09 +0000134 return;
Brian Gaeke972d3d72003-10-16 04:43:15 +0000135 }
Reid Spencer63efb772004-11-14 22:27:46 +0000136 } else if (IsArchive(Filename)) {
137 Archive* archive = Archive::OpenAndLoad(sys::Path(Filename));
138 if (!archive)
139 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000140 std::vector<Module *> Modules;
Reid Spencer63efb772004-11-14 22:27:46 +0000141 if (archive->getAllModules(Modules,&ErrorMessage)) {
142 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
Brian Gaeke08d03c72003-11-19 21:52:09 +0000143 return;
144 }
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000145 MultipleFiles = true;
146 std::for_each (Modules.begin (), Modules.end (), DumpSymbolNamesFromModule);
Brian Gaeke82042872003-11-19 21:57:30 +0000147 } else {
148 std::cerr << ToolName << ": " << Filename << ": "
149 << "unrecognizable file type\n";
150 return;
Brian Gaeke972d3d72003-10-16 04:43:15 +0000151 }
152}
153
154int main(int argc, char **argv) {
155 cl::ParseCommandLineOptions(argc, argv, " llvm symbol table dumper\n");
Reid Spencer9de7b332004-08-29 19:28:55 +0000156 sys::PrintStackTraceOnErrorSignal();
Chris Lattnerf73b4ca2004-02-19 20:32:12 +0000157
Brian Gaeke972d3d72003-10-16 04:43:15 +0000158 ToolName = argv[0];
159 if (BSDFormat) OutputFormat = bsd;
160 if (POSIXFormat) OutputFormat = posix;
Chris Lattnerfc046d52003-10-16 18:45:23 +0000161
162 switch (InputFilenames.size()) {
163 case 0: InputFilenames.push_back("-");
164 case 1: break;
165 default: MultipleFiles = true;
166 }
Brian Gaeke972d3d72003-10-16 04:43:15 +0000167
168 std::for_each (InputFilenames.begin (), InputFilenames.end (),
169 DumpSymbolNamesFromFile);
170 return 0;
171}