blob: ec024f9b02b0c1d6ac8aaf98806e795933072bec [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>
24
Brian Gaeked0fde302003-11-11 22:41:34 +000025using namespace llvm;
26
Brian Gaeke972d3d72003-10-16 04:43:15 +000027namespace {
28 enum OutputFormatTy { bsd, sysv, posix };
29 cl::opt<OutputFormatTy>
30 OutputFormat("format",
31 cl::desc("Specify output format"),
32 cl::values(clEnumVal(bsd, "BSD format"),
33 clEnumVal(sysv, "System V format"),
34 clEnumVal(posix, "POSIX.2 format"), 0), cl::init(bsd));
35 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
36 cl::aliasopt(OutputFormat));
37
38 cl::list<std::string>
39 InputFilenames(cl::Positional, cl::desc("<input bytecode files>"),
Chris Lattnerfc046d52003-10-16 18:45:23 +000040 cl::ZeroOrMore);
Brian Gaeke972d3d72003-10-16 04:43:15 +000041
42 cl::opt<bool> UndefinedOnly("undefined-only",
43 cl::desc("Show only undefined symbols"));
44 cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
45 cl::aliasopt(UndefinedOnly));
46
47 cl::opt<bool> DefinedOnly("defined-only",
48 cl::desc("Show only defined symbols"));
49
50 cl::opt<bool> ExternalOnly("extern-only",
51 cl::desc("Show only external symbols"));
52 cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
53 cl::aliasopt(ExternalOnly));
54
55 cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
56 cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
57
58 bool MultipleFiles = false;
59
60 std::string ToolName;
61};
62
63char TypeCharForSymbol (GlobalValue &GV) {
64 if (GV.isExternal ()) return 'U';
65 if (GV.hasLinkOnceLinkage ()) return 'C';
Brian Gaeke972d3d72003-10-16 04:43:15 +000066 if (GV.hasWeakLinkage ()) return 'W';
Brian Gaeke972d3d72003-10-16 04:43:15 +000067 if (isa<Function> (GV) && GV.hasInternalLinkage ()) return 't';
68 if (isa<Function> (GV)) return 'T';
69 if (isa<GlobalVariable> (GV) && GV.hasInternalLinkage ()) return 'd';
70 if (isa<GlobalVariable> (GV)) return 'D';
71 return '?';
72}
73
74void DumpSymbolNameForGlobalValue (GlobalValue &GV) {
75 const std::string SymbolAddrStr = " "; // Not used yet...
76 char TypeChar = TypeCharForSymbol (GV);
77 if ((TypeChar != 'U') && UndefinedOnly)
78 return;
79 if ((TypeChar == 'U') && DefinedOnly)
80 return;
81 if (GV.hasInternalLinkage () && ExternalOnly)
82 return;
83 if (OutputFormat == posix) {
84 std::cout << GV.getName () << " " << TypeCharForSymbol (GV) << " "
85 << SymbolAddrStr << "\n";
86 } else if (OutputFormat == bsd) {
87 std::cout << SymbolAddrStr << " " << TypeCharForSymbol (GV) << " "
88 << GV.getName () << "\n";
89 } else if (OutputFormat == sysv) {
90 std::string PaddedName (GV.getName ());
91 while (PaddedName.length () < 20)
92 PaddedName += " ";
93 std::cout << PaddedName << "|" << SymbolAddrStr << "| "
94 << TypeCharForSymbol (GV)
95 << " | | | |\n";
96 }
97}
98
99void DumpSymbolNamesFromModule (Module *M) {
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000100 const std::string &Filename = M->getModuleIdentifier ();
101 if (OutputFormat == posix && MultipleFiles) {
102 std::cout << Filename << ":\n";
103 } else if (OutputFormat == bsd && MultipleFiles) {
104 std::cout << "\n" << Filename << ":\n";
105 } else if (OutputFormat == sysv) {
106 std::cout << "\n\nSymbols from " << Filename << ":\n\n"
107 << "Name Value Class Type"
108 << " Size Line Section\n";
109 }
Brian Gaeke972d3d72003-10-16 04:43:15 +0000110 std::for_each (M->begin (), M->end (), DumpSymbolNameForGlobalValue);
111 std::for_each (M->gbegin (), M->gend (), DumpSymbolNameForGlobalValue);
112}
113
114void DumpSymbolNamesFromFile (std::string &Filename) {
115 std::string ErrorMessage;
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000116 if (IsBytecode (Filename)) {
117 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
118 if (Result) {
119 DumpSymbolNamesFromModule (Result);
120 } else {
121 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
Brian Gaeke972d3d72003-10-16 04:43:15 +0000122 }
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000123 } else if (IsArchive (Filename)) {
124 std::vector<Module *> Modules;
125 if (ReadArchiveFile (Filename, Modules, &ErrorMessage))
126 std::cerr << ToolName << ": " << Filename << ": "
127 << ErrorMessage << "\n";
128 MultipleFiles = true;
129 std::for_each (Modules.begin (), Modules.end (), DumpSymbolNamesFromModule);
Brian Gaeke972d3d72003-10-16 04:43:15 +0000130 }
131}
132
133int main(int argc, char **argv) {
134 cl::ParseCommandLineOptions(argc, argv, " llvm symbol table dumper\n");
135 ToolName = argv[0];
136 if (BSDFormat) OutputFormat = bsd;
137 if (POSIXFormat) OutputFormat = posix;
Chris Lattnerfc046d52003-10-16 18:45:23 +0000138
139 switch (InputFilenames.size()) {
140 case 0: InputFilenames.push_back("-");
141 case 1: break;
142 default: MultipleFiles = true;
143 }
Brian Gaeke972d3d72003-10-16 04:43:15 +0000144
145 std::for_each (InputFilenames.begin (), InputFilenames.end (),
146 DumpSymbolNamesFromFile);
147 return 0;
148}