blob: f7a0ef6746504752724398dbf7240c4e340c54c2 [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 Gaeke972d3d72003-10-16 04:43:15 +000022#include <cctype>
23
24namespace {
25 enum OutputFormatTy { bsd, sysv, posix };
26 cl::opt<OutputFormatTy>
27 OutputFormat("format",
28 cl::desc("Specify output format"),
29 cl::values(clEnumVal(bsd, "BSD format"),
30 clEnumVal(sysv, "System V format"),
31 clEnumVal(posix, "POSIX.2 format"), 0), cl::init(bsd));
32 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
33 cl::aliasopt(OutputFormat));
34
35 cl::list<std::string>
36 InputFilenames(cl::Positional, cl::desc("<input bytecode files>"),
Chris Lattnerfc046d52003-10-16 18:45:23 +000037 cl::ZeroOrMore);
Brian Gaeke972d3d72003-10-16 04:43:15 +000038
39 cl::opt<bool> UndefinedOnly("undefined-only",
40 cl::desc("Show only undefined symbols"));
41 cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
42 cl::aliasopt(UndefinedOnly));
43
44 cl::opt<bool> DefinedOnly("defined-only",
45 cl::desc("Show only defined symbols"));
46
47 cl::opt<bool> ExternalOnly("extern-only",
48 cl::desc("Show only external symbols"));
49 cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
50 cl::aliasopt(ExternalOnly));
51
52 cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
53 cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
54
55 bool MultipleFiles = false;
56
57 std::string ToolName;
58};
59
60char TypeCharForSymbol (GlobalValue &GV) {
61 if (GV.isExternal ()) return 'U';
62 if (GV.hasLinkOnceLinkage ()) return 'C';
Brian Gaeke972d3d72003-10-16 04:43:15 +000063 if (GV.hasWeakLinkage ()) return 'W';
Brian Gaeke972d3d72003-10-16 04:43:15 +000064 if (isa<Function> (GV) && GV.hasInternalLinkage ()) return 't';
65 if (isa<Function> (GV)) return 'T';
66 if (isa<GlobalVariable> (GV) && GV.hasInternalLinkage ()) return 'd';
67 if (isa<GlobalVariable> (GV)) return 'D';
68 return '?';
69}
70
71void DumpSymbolNameForGlobalValue (GlobalValue &GV) {
72 const std::string SymbolAddrStr = " "; // Not used yet...
73 char TypeChar = TypeCharForSymbol (GV);
74 if ((TypeChar != 'U') && UndefinedOnly)
75 return;
76 if ((TypeChar == 'U') && DefinedOnly)
77 return;
78 if (GV.hasInternalLinkage () && ExternalOnly)
79 return;
80 if (OutputFormat == posix) {
81 std::cout << GV.getName () << " " << TypeCharForSymbol (GV) << " "
82 << SymbolAddrStr << "\n";
83 } else if (OutputFormat == bsd) {
84 std::cout << SymbolAddrStr << " " << TypeCharForSymbol (GV) << " "
85 << GV.getName () << "\n";
86 } else if (OutputFormat == sysv) {
87 std::string PaddedName (GV.getName ());
88 while (PaddedName.length () < 20)
89 PaddedName += " ";
90 std::cout << PaddedName << "|" << SymbolAddrStr << "| "
91 << TypeCharForSymbol (GV)
92 << " | | | |\n";
93 }
94}
95
96void DumpSymbolNamesFromModule (Module *M) {
97 std::for_each (M->begin (), M->end (), DumpSymbolNameForGlobalValue);
98 std::for_each (M->gbegin (), M->gend (), DumpSymbolNameForGlobalValue);
99}
100
101void DumpSymbolNamesFromFile (std::string &Filename) {
102 std::string ErrorMessage;
103 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
104 if (Result) {
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 }
114 DumpSymbolNamesFromModule (Result);
115 } else {
116 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
117 }
118}
119
120int main(int argc, char **argv) {
121 cl::ParseCommandLineOptions(argc, argv, " llvm symbol table dumper\n");
122 ToolName = argv[0];
123 if (BSDFormat) OutputFormat = bsd;
124 if (POSIXFormat) OutputFormat = posix;
Chris Lattnerfc046d52003-10-16 18:45:23 +0000125
126 switch (InputFilenames.size()) {
127 case 0: InputFilenames.push_back("-");
128 case 1: break;
129 default: MultipleFiles = true;
130 }
Brian Gaeke972d3d72003-10-16 04:43:15 +0000131
132 std::for_each (InputFilenames.begin (), InputFilenames.end (),
133 DumpSymbolNamesFromFile);
134 return 0;
135}