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