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