blob: ffd8cf3324cad167981bae1bcf37d99d86431b56 [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//===----------------------------------------------------------------------===//
9//
Brian Gaeke972d3d72003-10-16 04:43:15 +000010//
11// This program is a utility that works like traditional Unix "nm",
12// that is, it prints out the names of symbols in a bytecode file,
13// along with some information about each symbol.
14//
15// This "nm" does not print symbols' addresses. It supports many of
16// the features of GNU "nm", including its different output formats.
17//
18//===----------------------------------------------------------------------===//
19
20#include "Support/CommandLine.h"
21#include "llvm/Bytecode/Reader.h"
22#include "llvm/GlobalValue.h"
23#include "llvm/Module.h"
24#include <cctype>
25
26namespace {
27 enum OutputFormatTy { bsd, sysv, posix };
28 cl::opt<OutputFormatTy>
29 OutputFormat("format",
30 cl::desc("Specify output format"),
31 cl::values(clEnumVal(bsd, "BSD format"),
32 clEnumVal(sysv, "System V format"),
33 clEnumVal(posix, "POSIX.2 format"), 0), cl::init(bsd));
34 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
35 cl::aliasopt(OutputFormat));
36
37 cl::list<std::string>
38 InputFilenames(cl::Positional, cl::desc("<input bytecode files>"),
Chris Lattnerfc046d52003-10-16 18:45:23 +000039 cl::ZeroOrMore);
Brian Gaeke972d3d72003-10-16 04:43:15 +000040
41 cl::opt<bool> UndefinedOnly("undefined-only",
42 cl::desc("Show only undefined symbols"));
43 cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
44 cl::aliasopt(UndefinedOnly));
45
46 cl::opt<bool> DefinedOnly("defined-only",
47 cl::desc("Show only defined symbols"));
48
49 cl::opt<bool> ExternalOnly("extern-only",
50 cl::desc("Show only external symbols"));
51 cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
52 cl::aliasopt(ExternalOnly));
53
54 cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
55 cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
56
57 bool MultipleFiles = false;
58
59 std::string ToolName;
60};
61
62char TypeCharForSymbol (GlobalValue &GV) {
63 if (GV.isExternal ()) return 'U';
64 if (GV.hasLinkOnceLinkage ()) return 'C';
Brian Gaeke972d3d72003-10-16 04:43:15 +000065 if (GV.hasWeakLinkage ()) return 'W';
Brian Gaeke972d3d72003-10-16 04:43:15 +000066 if (isa<Function> (GV) && GV.hasInternalLinkage ()) return 't';
67 if (isa<Function> (GV)) return 'T';
68 if (isa<GlobalVariable> (GV) && GV.hasInternalLinkage ()) return 'd';
69 if (isa<GlobalVariable> (GV)) return 'D';
70 return '?';
71}
72
73void DumpSymbolNameForGlobalValue (GlobalValue &GV) {
74 const std::string SymbolAddrStr = " "; // Not used yet...
75 char TypeChar = TypeCharForSymbol (GV);
76 if ((TypeChar != 'U') && UndefinedOnly)
77 return;
78 if ((TypeChar == 'U') && DefinedOnly)
79 return;
80 if (GV.hasInternalLinkage () && ExternalOnly)
81 return;
82 if (OutputFormat == posix) {
83 std::cout << GV.getName () << " " << TypeCharForSymbol (GV) << " "
84 << SymbolAddrStr << "\n";
85 } else if (OutputFormat == bsd) {
86 std::cout << SymbolAddrStr << " " << TypeCharForSymbol (GV) << " "
87 << GV.getName () << "\n";
88 } else if (OutputFormat == sysv) {
89 std::string PaddedName (GV.getName ());
90 while (PaddedName.length () < 20)
91 PaddedName += " ";
92 std::cout << PaddedName << "|" << SymbolAddrStr << "| "
93 << TypeCharForSymbol (GV)
94 << " | | | |\n";
95 }
96}
97
98void DumpSymbolNamesFromModule (Module *M) {
99 std::for_each (M->begin (), M->end (), DumpSymbolNameForGlobalValue);
100 std::for_each (M->gbegin (), M->gend (), DumpSymbolNameForGlobalValue);
101}
102
103void DumpSymbolNamesFromFile (std::string &Filename) {
104 std::string ErrorMessage;
105 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
106 if (Result) {
107 if (OutputFormat == posix && MultipleFiles) {
108 std::cout << Filename << ":\n";
109 } else if (OutputFormat == bsd && MultipleFiles) {
110 std::cout << "\n" << Filename << ":\n";
111 } else if (OutputFormat == sysv) {
112 std::cout << "\n\nSymbols from " << Filename << ":\n\n"
113 << "Name Value Class Type"
114 << " Size Line Section\n";
115 }
116 DumpSymbolNamesFromModule (Result);
117 } else {
118 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
119 }
120}
121
122int main(int argc, char **argv) {
123 cl::ParseCommandLineOptions(argc, argv, " llvm symbol table dumper\n");
124 ToolName = argv[0];
125 if (BSDFormat) OutputFormat = bsd;
126 if (POSIXFormat) OutputFormat = posix;
Chris Lattnerfc046d52003-10-16 18:45:23 +0000127
128 switch (InputFilenames.size()) {
129 case 0: InputFilenames.push_back("-");
130 case 1: break;
131 default: MultipleFiles = true;
132 }
Brian Gaeke972d3d72003-10-16 04:43:15 +0000133
134 std::for_each (InputFilenames.begin (), InputFilenames.end (),
135 DumpSymbolNamesFromFile);
136 return 0;
137}