blob: c5930e93728f2ab0f32c3cd963378522eb1df59f [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>
Brian Gaeke08d03c72003-11-19 21:52:09 +000025#include <cstring>
Brian Gaeke972d3d72003-10-16 04:43:15 +000026
Brian Gaeked0fde302003-11-11 22:41:34 +000027using namespace llvm;
28
Brian Gaeke972d3d72003-10-16 04:43:15 +000029namespace {
30 enum OutputFormatTy { bsd, sysv, posix };
31 cl::opt<OutputFormatTy>
32 OutputFormat("format",
33 cl::desc("Specify output format"),
34 cl::values(clEnumVal(bsd, "BSD format"),
35 clEnumVal(sysv, "System V format"),
36 clEnumVal(posix, "POSIX.2 format"), 0), cl::init(bsd));
37 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
38 cl::aliasopt(OutputFormat));
39
40 cl::list<std::string>
41 InputFilenames(cl::Positional, cl::desc("<input bytecode files>"),
Chris Lattnerfc046d52003-10-16 18:45:23 +000042 cl::ZeroOrMore);
Brian Gaeke972d3d72003-10-16 04:43:15 +000043
44 cl::opt<bool> UndefinedOnly("undefined-only",
45 cl::desc("Show only undefined symbols"));
46 cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
47 cl::aliasopt(UndefinedOnly));
48
49 cl::opt<bool> DefinedOnly("defined-only",
50 cl::desc("Show only defined symbols"));
51
52 cl::opt<bool> ExternalOnly("extern-only",
53 cl::desc("Show only external symbols"));
54 cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
55 cl::aliasopt(ExternalOnly));
56
57 cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
58 cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
59
60 bool MultipleFiles = false;
61
62 std::string ToolName;
63};
64
65char TypeCharForSymbol (GlobalValue &GV) {
66 if (GV.isExternal ()) return 'U';
67 if (GV.hasLinkOnceLinkage ()) return 'C';
Brian Gaeke972d3d72003-10-16 04:43:15 +000068 if (GV.hasWeakLinkage ()) return 'W';
Brian Gaeke972d3d72003-10-16 04:43:15 +000069 if (isa<Function> (GV) && GV.hasInternalLinkage ()) return 't';
70 if (isa<Function> (GV)) return 'T';
71 if (isa<GlobalVariable> (GV) && GV.hasInternalLinkage ()) return 'd';
72 if (isa<GlobalVariable> (GV)) return 'D';
73 return '?';
74}
75
76void DumpSymbolNameForGlobalValue (GlobalValue &GV) {
77 const std::string SymbolAddrStr = " "; // Not used yet...
78 char TypeChar = TypeCharForSymbol (GV);
79 if ((TypeChar != 'U') && UndefinedOnly)
80 return;
81 if ((TypeChar == 'U') && DefinedOnly)
82 return;
83 if (GV.hasInternalLinkage () && ExternalOnly)
84 return;
85 if (OutputFormat == posix) {
86 std::cout << GV.getName () << " " << TypeCharForSymbol (GV) << " "
87 << SymbolAddrStr << "\n";
88 } else if (OutputFormat == bsd) {
89 std::cout << SymbolAddrStr << " " << TypeCharForSymbol (GV) << " "
90 << GV.getName () << "\n";
91 } else if (OutputFormat == sysv) {
92 std::string PaddedName (GV.getName ());
93 while (PaddedName.length () < 20)
94 PaddedName += " ";
95 std::cout << PaddedName << "|" << SymbolAddrStr << "| "
96 << TypeCharForSymbol (GV)
97 << " | | | |\n";
98 }
99}
100
101void DumpSymbolNamesFromModule (Module *M) {
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000102 const std::string &Filename = M->getModuleIdentifier ();
103 if (OutputFormat == posix && MultipleFiles) {
104 std::cout << Filename << ":\n";
105 } else if (OutputFormat == bsd && MultipleFiles) {
106 std::cout << "\n" << Filename << ":\n";
107 } else if (OutputFormat == sysv) {
108 std::cout << "\n\nSymbols from " << Filename << ":\n\n"
109 << "Name Value Class Type"
110 << " Size Line Section\n";
111 }
Brian Gaeke972d3d72003-10-16 04:43:15 +0000112 std::for_each (M->begin (), M->end (), DumpSymbolNameForGlobalValue);
113 std::for_each (M->gbegin (), M->gend (), DumpSymbolNameForGlobalValue);
114}
115
116void DumpSymbolNamesFromFile (std::string &Filename) {
117 std::string ErrorMessage;
Brian Gaeke8b1daa32003-11-19 22:15:00 +0000118 if (Filename != "-" && !FileOpenable (Filename)) {
Brian Gaeke08d03c72003-11-19 21:52:09 +0000119 std::cerr << ToolName << ": " << Filename << ": " << strerror (errno)
120 << "\n";
121 return;
122 }
Brian Gaeke8b1daa32003-11-19 22:15:00 +0000123 // Note: Currently we do not support reading an archive from stdin.
124 if (Filename == "-" || IsBytecode (Filename)) {
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000125 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
126 if (Result) {
127 DumpSymbolNamesFromModule (Result);
128 } else {
129 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
Brian Gaeke08d03c72003-11-19 21:52:09 +0000130 return;
Brian Gaeke972d3d72003-10-16 04:43:15 +0000131 }
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000132 } else if (IsArchive (Filename)) {
133 std::vector<Module *> Modules;
Brian Gaeke08d03c72003-11-19 21:52:09 +0000134 if (ReadArchiveFile (Filename, Modules, &ErrorMessage)) {
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000135 std::cerr << ToolName << ": " << Filename << ": "
136 << ErrorMessage << "\n";
Brian Gaeke08d03c72003-11-19 21:52:09 +0000137 return;
138 }
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000139 MultipleFiles = true;
140 std::for_each (Modules.begin (), Modules.end (), DumpSymbolNamesFromModule);
Brian Gaeke82042872003-11-19 21:57:30 +0000141 } else {
142 std::cerr << ToolName << ": " << Filename << ": "
143 << "unrecognizable file type\n";
144 return;
Brian Gaeke972d3d72003-10-16 04:43:15 +0000145 }
146}
147
148int main(int argc, char **argv) {
149 cl::ParseCommandLineOptions(argc, argv, " llvm symbol table dumper\n");
Chris Lattnerf73b4ca2004-02-19 20:32:12 +0000150 PrintStackTraceOnErrorSignal();
151
Brian Gaeke972d3d72003-10-16 04:43:15 +0000152 ToolName = argv[0];
153 if (BSDFormat) OutputFormat = bsd;
154 if (POSIXFormat) OutputFormat = posix;
Chris Lattnerfc046d52003-10-16 18:45:23 +0000155
156 switch (InputFilenames.size()) {
157 case 0: InputFilenames.push_back("-");
158 case 1: break;
159 default: MultipleFiles = true;
160 }
Brian Gaeke972d3d72003-10-16 04:43:15 +0000161
162 std::for_each (InputFilenames.begin (), InputFilenames.end (),
163 DumpSymbolNamesFromFile);
164 return 0;
165}