blob: ba8b5d7f0d5fead44a34e18eef59a26005eec068 [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"),
Chris Lattner4d143ee2004-07-16 00:08:28 +000038 clEnumVal(posix, "POSIX.2 format"),
39 clEnumValEnd), cl::init(bsd));
Brian Gaeke972d3d72003-10-16 04:43:15 +000040 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
41 cl::aliasopt(OutputFormat));
42
43 cl::list<std::string>
44 InputFilenames(cl::Positional, cl::desc("<input bytecode files>"),
Chris Lattnerfc046d52003-10-16 18:45:23 +000045 cl::ZeroOrMore);
Brian Gaeke972d3d72003-10-16 04:43:15 +000046
47 cl::opt<bool> UndefinedOnly("undefined-only",
48 cl::desc("Show only undefined symbols"));
49 cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
50 cl::aliasopt(UndefinedOnly));
51
52 cl::opt<bool> DefinedOnly("defined-only",
53 cl::desc("Show only defined symbols"));
54
55 cl::opt<bool> ExternalOnly("extern-only",
56 cl::desc("Show only external symbols"));
57 cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
58 cl::aliasopt(ExternalOnly));
59
60 cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
61 cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
62
63 bool MultipleFiles = false;
64
65 std::string ToolName;
66};
67
68char TypeCharForSymbol (GlobalValue &GV) {
69 if (GV.isExternal ()) return 'U';
70 if (GV.hasLinkOnceLinkage ()) return 'C';
Brian Gaeke972d3d72003-10-16 04:43:15 +000071 if (GV.hasWeakLinkage ()) return 'W';
Brian Gaeke972d3d72003-10-16 04:43:15 +000072 if (isa<Function> (GV) && GV.hasInternalLinkage ()) return 't';
73 if (isa<Function> (GV)) return 'T';
74 if (isa<GlobalVariable> (GV) && GV.hasInternalLinkage ()) return 'd';
75 if (isa<GlobalVariable> (GV)) return 'D';
76 return '?';
77}
78
79void DumpSymbolNameForGlobalValue (GlobalValue &GV) {
80 const std::string SymbolAddrStr = " "; // Not used yet...
81 char TypeChar = TypeCharForSymbol (GV);
82 if ((TypeChar != 'U') && UndefinedOnly)
83 return;
84 if ((TypeChar == 'U') && DefinedOnly)
85 return;
86 if (GV.hasInternalLinkage () && ExternalOnly)
87 return;
88 if (OutputFormat == posix) {
89 std::cout << GV.getName () << " " << TypeCharForSymbol (GV) << " "
90 << SymbolAddrStr << "\n";
91 } else if (OutputFormat == bsd) {
92 std::cout << SymbolAddrStr << " " << TypeCharForSymbol (GV) << " "
93 << GV.getName () << "\n";
94 } else if (OutputFormat == sysv) {
95 std::string PaddedName (GV.getName ());
96 while (PaddedName.length () < 20)
97 PaddedName += " ";
98 std::cout << PaddedName << "|" << SymbolAddrStr << "| "
99 << TypeCharForSymbol (GV)
100 << " | | | |\n";
101 }
102}
103
104void DumpSymbolNamesFromModule (Module *M) {
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000105 const std::string &Filename = M->getModuleIdentifier ();
106 if (OutputFormat == posix && MultipleFiles) {
107 std::cout << Filename << ":\n";
108 } else if (OutputFormat == bsd && MultipleFiles) {
109 std::cout << "\n" << Filename << ":\n";
110 } else if (OutputFormat == sysv) {
111 std::cout << "\n\nSymbols from " << Filename << ":\n\n"
112 << "Name Value Class Type"
113 << " Size Line Section\n";
114 }
Brian Gaeke972d3d72003-10-16 04:43:15 +0000115 std::for_each (M->begin (), M->end (), DumpSymbolNameForGlobalValue);
116 std::for_each (M->gbegin (), M->gend (), DumpSymbolNameForGlobalValue);
117}
118
119void DumpSymbolNamesFromFile (std::string &Filename) {
120 std::string ErrorMessage;
Brian Gaeke8b1daa32003-11-19 22:15:00 +0000121 if (Filename != "-" && !FileOpenable (Filename)) {
Brian Gaeke08d03c72003-11-19 21:52:09 +0000122 std::cerr << ToolName << ": " << Filename << ": " << strerror (errno)
123 << "\n";
124 return;
125 }
Brian Gaeke8b1daa32003-11-19 22:15:00 +0000126 // Note: Currently we do not support reading an archive from stdin.
127 if (Filename == "-" || IsBytecode (Filename)) {
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000128 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
129 if (Result) {
130 DumpSymbolNamesFromModule (Result);
131 } else {
132 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
Brian Gaeke08d03c72003-11-19 21:52:09 +0000133 return;
Brian Gaeke972d3d72003-10-16 04:43:15 +0000134 }
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000135 } else if (IsArchive (Filename)) {
136 std::vector<Module *> Modules;
Brian Gaeke08d03c72003-11-19 21:52:09 +0000137 if (ReadArchiveFile (Filename, Modules, &ErrorMessage)) {
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000138 std::cerr << ToolName << ": " << Filename << ": "
139 << ErrorMessage << "\n";
Brian Gaeke08d03c72003-11-19 21:52:09 +0000140 return;
141 }
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000142 MultipleFiles = true;
143 std::for_each (Modules.begin (), Modules.end (), DumpSymbolNamesFromModule);
Brian Gaeke82042872003-11-19 21:57:30 +0000144 } else {
145 std::cerr << ToolName << ": " << Filename << ": "
146 << "unrecognizable file type\n";
147 return;
Brian Gaeke972d3d72003-10-16 04:43:15 +0000148 }
149}
150
151int main(int argc, char **argv) {
152 cl::ParseCommandLineOptions(argc, argv, " llvm symbol table dumper\n");
Reid Spencer9de7b332004-08-29 19:28:55 +0000153 sys::PrintStackTraceOnErrorSignal();
Chris Lattnerf73b4ca2004-02-19 20:32:12 +0000154
Brian Gaeke972d3d72003-10-16 04:43:15 +0000155 ToolName = argv[0];
156 if (BSDFormat) OutputFormat = bsd;
157 if (POSIXFormat) OutputFormat = posix;
Chris Lattnerfc046d52003-10-16 18:45:23 +0000158
159 switch (InputFilenames.size()) {
160 case 0: InputFilenames.push_back("-");
161 case 1: break;
162 default: MultipleFiles = true;
163 }
Brian Gaeke972d3d72003-10-16 04:43:15 +0000164
165 std::for_each (InputFilenames.begin (), InputFilenames.end (),
166 DumpSymbolNamesFromFile);
167 return 0;
168}