blob: 78d23d8c5209728b74f0b8375982727cd115fa5a [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 Lattnerbed85ff2004-05-27 05:41:36 +000023#include "llvm/System/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>
Reid Spencer86f42bd2004-07-04 12:20:55 +000027#include <iostream>
Brian Gaeke972d3d72003-10-16 04:43:15 +000028
Brian Gaeked0fde302003-11-11 22:41:34 +000029using namespace llvm;
30
Brian Gaeke972d3d72003-10-16 04:43:15 +000031namespace {
32 enum OutputFormatTy { bsd, sysv, posix };
33 cl::opt<OutputFormatTy>
34 OutputFormat("format",
35 cl::desc("Specify output format"),
36 cl::values(clEnumVal(bsd, "BSD format"),
37 clEnumVal(sysv, "System V format"),
38 clEnumVal(posix, "POSIX.2 format"), 0), cl::init(bsd));
39 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
40 cl::aliasopt(OutputFormat));
41
42 cl::list<std::string>
43 InputFilenames(cl::Positional, cl::desc("<input bytecode files>"),
Chris Lattnerfc046d52003-10-16 18:45:23 +000044 cl::ZeroOrMore);
Brian Gaeke972d3d72003-10-16 04:43:15 +000045
46 cl::opt<bool> UndefinedOnly("undefined-only",
47 cl::desc("Show only undefined symbols"));
48 cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
49 cl::aliasopt(UndefinedOnly));
50
51 cl::opt<bool> DefinedOnly("defined-only",
52 cl::desc("Show only defined symbols"));
53
54 cl::opt<bool> ExternalOnly("extern-only",
55 cl::desc("Show only external symbols"));
56 cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
57 cl::aliasopt(ExternalOnly));
58
59 cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
60 cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
61
62 bool MultipleFiles = false;
63
64 std::string ToolName;
65};
66
67char TypeCharForSymbol (GlobalValue &GV) {
68 if (GV.isExternal ()) return 'U';
69 if (GV.hasLinkOnceLinkage ()) return 'C';
Brian Gaeke972d3d72003-10-16 04:43:15 +000070 if (GV.hasWeakLinkage ()) return 'W';
Brian Gaeke972d3d72003-10-16 04:43:15 +000071 if (isa<Function> (GV) && GV.hasInternalLinkage ()) return 't';
72 if (isa<Function> (GV)) return 'T';
73 if (isa<GlobalVariable> (GV) && GV.hasInternalLinkage ()) return 'd';
74 if (isa<GlobalVariable> (GV)) return 'D';
75 return '?';
76}
77
78void DumpSymbolNameForGlobalValue (GlobalValue &GV) {
79 const std::string SymbolAddrStr = " "; // Not used yet...
80 char TypeChar = TypeCharForSymbol (GV);
81 if ((TypeChar != 'U') && UndefinedOnly)
82 return;
83 if ((TypeChar == 'U') && DefinedOnly)
84 return;
85 if (GV.hasInternalLinkage () && ExternalOnly)
86 return;
87 if (OutputFormat == posix) {
88 std::cout << GV.getName () << " " << TypeCharForSymbol (GV) << " "
89 << SymbolAddrStr << "\n";
90 } else if (OutputFormat == bsd) {
91 std::cout << SymbolAddrStr << " " << TypeCharForSymbol (GV) << " "
92 << GV.getName () << "\n";
93 } else if (OutputFormat == sysv) {
94 std::string PaddedName (GV.getName ());
95 while (PaddedName.length () < 20)
96 PaddedName += " ";
97 std::cout << PaddedName << "|" << SymbolAddrStr << "| "
98 << TypeCharForSymbol (GV)
99 << " | | | |\n";
100 }
101}
102
103void DumpSymbolNamesFromModule (Module *M) {
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000104 const std::string &Filename = M->getModuleIdentifier ();
105 if (OutputFormat == posix && MultipleFiles) {
106 std::cout << Filename << ":\n";
107 } else if (OutputFormat == bsd && MultipleFiles) {
108 std::cout << "\n" << Filename << ":\n";
109 } else if (OutputFormat == sysv) {
110 std::cout << "\n\nSymbols from " << Filename << ":\n\n"
111 << "Name Value Class Type"
112 << " Size Line Section\n";
113 }
Brian Gaeke972d3d72003-10-16 04:43:15 +0000114 std::for_each (M->begin (), M->end (), DumpSymbolNameForGlobalValue);
115 std::for_each (M->gbegin (), M->gend (), DumpSymbolNameForGlobalValue);
116}
117
118void DumpSymbolNamesFromFile (std::string &Filename) {
119 std::string ErrorMessage;
Brian Gaeke8b1daa32003-11-19 22:15:00 +0000120 if (Filename != "-" && !FileOpenable (Filename)) {
Brian Gaeke08d03c72003-11-19 21:52:09 +0000121 std::cerr << ToolName << ": " << Filename << ": " << strerror (errno)
122 << "\n";
123 return;
124 }
Brian Gaeke8b1daa32003-11-19 22:15:00 +0000125 // Note: Currently we do not support reading an archive from stdin.
126 if (Filename == "-" || IsBytecode (Filename)) {
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000127 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
128 if (Result) {
129 DumpSymbolNamesFromModule (Result);
130 } else {
131 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
Brian Gaeke08d03c72003-11-19 21:52:09 +0000132 return;
Brian Gaeke972d3d72003-10-16 04:43:15 +0000133 }
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000134 } else if (IsArchive (Filename)) {
135 std::vector<Module *> Modules;
Brian Gaeke08d03c72003-11-19 21:52:09 +0000136 if (ReadArchiveFile (Filename, Modules, &ErrorMessage)) {
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000137 std::cerr << ToolName << ": " << Filename << ": "
138 << ErrorMessage << "\n";
Brian Gaeke08d03c72003-11-19 21:52:09 +0000139 return;
140 }
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000141 MultipleFiles = true;
142 std::for_each (Modules.begin (), Modules.end (), DumpSymbolNamesFromModule);
Brian Gaeke82042872003-11-19 21:57:30 +0000143 } else {
144 std::cerr << ToolName << ": " << Filename << ": "
145 << "unrecognizable file type\n";
146 return;
Brian Gaeke972d3d72003-10-16 04:43:15 +0000147 }
148}
149
150int main(int argc, char **argv) {
151 cl::ParseCommandLineOptions(argc, argv, " llvm symbol table dumper\n");
Chris Lattnerf73b4ca2004-02-19 20:32:12 +0000152 PrintStackTraceOnErrorSignal();
153
Brian Gaeke972d3d72003-10-16 04:43:15 +0000154 ToolName = argv[0];
155 if (BSDFormat) OutputFormat = bsd;
156 if (POSIXFormat) OutputFormat = posix;
Chris Lattnerfc046d52003-10-16 18:45:23 +0000157
158 switch (InputFilenames.size()) {
159 case 0: InputFilenames.push_back("-");
160 case 1: break;
161 default: MultipleFiles = true;
162 }
Brian Gaeke972d3d72003-10-16 04:43:15 +0000163
164 std::for_each (InputFilenames.begin (), InputFilenames.end (),
165 DumpSymbolNamesFromFile);
166 return 0;
167}