blob: 6aaf7d2f74bf3bdaa2135044d84c6dd632b8f0f5 [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"
21#include "Support/CommandLine.h"
Brian Gaeke1c0b6982003-11-16 23:34:13 +000022#include "Support/FileUtilities.h"
Chris Lattnerf73b4ca2004-02-19 20:32:12 +000023#include "Support/Signals.h"
Brian Gaeke972d3d72003-10-16 04:43:15 +000024#include <cctype>
Alkis Evlogimenos09233fb2004-04-21 16:11:40 +000025#include <cerrno>
Brian Gaeke08d03c72003-11-19 21:52:09 +000026#include <cstring>
Brian Gaeke972d3d72003-10-16 04:43:15 +000027
Brian Gaeked0fde302003-11-11 22:41:34 +000028using namespace llvm;
29
Brian Gaeke972d3d72003-10-16 04:43:15 +000030namespace {
31 enum OutputFormatTy { bsd, sysv, posix };
32 cl::opt<OutputFormatTy>
33 OutputFormat("format",
34 cl::desc("Specify output format"),
35 cl::values(clEnumVal(bsd, "BSD format"),
36 clEnumVal(sysv, "System V format"),
37 clEnumVal(posix, "POSIX.2 format"), 0), cl::init(bsd));
38 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
39 cl::aliasopt(OutputFormat));
40
41 cl::list<std::string>
42 InputFilenames(cl::Positional, cl::desc("<input bytecode files>"),
Chris Lattnerfc046d52003-10-16 18:45:23 +000043 cl::ZeroOrMore);
Brian Gaeke972d3d72003-10-16 04:43:15 +000044
45 cl::opt<bool> UndefinedOnly("undefined-only",
46 cl::desc("Show only undefined symbols"));
47 cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
48 cl::aliasopt(UndefinedOnly));
49
50 cl::opt<bool> DefinedOnly("defined-only",
51 cl::desc("Show only defined symbols"));
52
53 cl::opt<bool> ExternalOnly("extern-only",
54 cl::desc("Show only external symbols"));
55 cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
56 cl::aliasopt(ExternalOnly));
57
58 cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
59 cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
60
61 bool MultipleFiles = false;
62
63 std::string ToolName;
64};
65
66char TypeCharForSymbol (GlobalValue &GV) {
67 if (GV.isExternal ()) return 'U';
68 if (GV.hasLinkOnceLinkage ()) return 'C';
Brian Gaeke972d3d72003-10-16 04:43:15 +000069 if (GV.hasWeakLinkage ()) return 'W';
Brian Gaeke972d3d72003-10-16 04:43:15 +000070 if (isa<Function> (GV) && GV.hasInternalLinkage ()) return 't';
71 if (isa<Function> (GV)) return 'T';
72 if (isa<GlobalVariable> (GV) && GV.hasInternalLinkage ()) return 'd';
73 if (isa<GlobalVariable> (GV)) return 'D';
74 return '?';
75}
76
77void DumpSymbolNameForGlobalValue (GlobalValue &GV) {
78 const std::string SymbolAddrStr = " "; // Not used yet...
79 char TypeChar = TypeCharForSymbol (GV);
80 if ((TypeChar != 'U') && UndefinedOnly)
81 return;
82 if ((TypeChar == 'U') && DefinedOnly)
83 return;
84 if (GV.hasInternalLinkage () && ExternalOnly)
85 return;
86 if (OutputFormat == posix) {
87 std::cout << GV.getName () << " " << TypeCharForSymbol (GV) << " "
88 << SymbolAddrStr << "\n";
89 } else if (OutputFormat == bsd) {
90 std::cout << SymbolAddrStr << " " << TypeCharForSymbol (GV) << " "
91 << GV.getName () << "\n";
92 } else if (OutputFormat == sysv) {
93 std::string PaddedName (GV.getName ());
94 while (PaddedName.length () < 20)
95 PaddedName += " ";
96 std::cout << PaddedName << "|" << SymbolAddrStr << "| "
97 << TypeCharForSymbol (GV)
98 << " | | | |\n";
99 }
100}
101
102void DumpSymbolNamesFromModule (Module *M) {
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000103 const std::string &Filename = M->getModuleIdentifier ();
104 if (OutputFormat == posix && MultipleFiles) {
105 std::cout << Filename << ":\n";
106 } else if (OutputFormat == bsd && MultipleFiles) {
107 std::cout << "\n" << Filename << ":\n";
108 } else if (OutputFormat == sysv) {
109 std::cout << "\n\nSymbols from " << Filename << ":\n\n"
110 << "Name Value Class Type"
111 << " Size Line Section\n";
112 }
Brian Gaeke972d3d72003-10-16 04:43:15 +0000113 std::for_each (M->begin (), M->end (), DumpSymbolNameForGlobalValue);
114 std::for_each (M->gbegin (), M->gend (), DumpSymbolNameForGlobalValue);
115}
116
117void DumpSymbolNamesFromFile (std::string &Filename) {
118 std::string ErrorMessage;
Brian Gaeke8b1daa32003-11-19 22:15:00 +0000119 if (Filename != "-" && !FileOpenable (Filename)) {
Brian Gaeke08d03c72003-11-19 21:52:09 +0000120 std::cerr << ToolName << ": " << Filename << ": " << strerror (errno)
121 << "\n";
122 return;
123 }
Brian Gaeke8b1daa32003-11-19 22:15:00 +0000124 // Note: Currently we do not support reading an archive from stdin.
125 if (Filename == "-" || IsBytecode (Filename)) {
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000126 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
127 if (Result) {
128 DumpSymbolNamesFromModule (Result);
129 } else {
130 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
Brian Gaeke08d03c72003-11-19 21:52:09 +0000131 return;
Brian Gaeke972d3d72003-10-16 04:43:15 +0000132 }
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000133 } else if (IsArchive (Filename)) {
134 std::vector<Module *> Modules;
Brian Gaeke08d03c72003-11-19 21:52:09 +0000135 if (ReadArchiveFile (Filename, Modules, &ErrorMessage)) {
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000136 std::cerr << ToolName << ": " << Filename << ": "
137 << ErrorMessage << "\n";
Brian Gaeke08d03c72003-11-19 21:52:09 +0000138 return;
139 }
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000140 MultipleFiles = true;
141 std::for_each (Modules.begin (), Modules.end (), DumpSymbolNamesFromModule);
Brian Gaeke82042872003-11-19 21:57:30 +0000142 } else {
143 std::cerr << ToolName << ": " << Filename << ": "
144 << "unrecognizable file type\n";
145 return;
Brian Gaeke972d3d72003-10-16 04:43:15 +0000146 }
147}
148
149int main(int argc, char **argv) {
150 cl::ParseCommandLineOptions(argc, argv, " llvm symbol table dumper\n");
Chris Lattnerf73b4ca2004-02-19 20:32:12 +0000151 PrintStackTraceOnErrorSignal();
152
Brian Gaeke972d3d72003-10-16 04:43:15 +0000153 ToolName = argv[0];
154 if (BSDFormat) OutputFormat = bsd;
155 if (POSIXFormat) OutputFormat = posix;
Chris Lattnerfc046d52003-10-16 18:45:23 +0000156
157 switch (InputFilenames.size()) {
158 case 0: InputFilenames.push_back("-");
159 case 1: break;
160 default: MultipleFiles = true;
161 }
Brian Gaeke972d3d72003-10-16 04:43:15 +0000162
163 std::for_each (InputFilenames.begin (), InputFilenames.end (),
164 DumpSymbolNamesFromFile);
165 return 0;
166}