blob: 49148a9e830a376f05896b8c7273137766cf72ea [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 Gaeke08d03c72003-11-19 21:52:09 +0000117 if (!FileOpenable (Filename)) {
118 std::cerr << ToolName << ": " << Filename << ": " << strerror (errno)
119 << "\n";
120 return;
121 }
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000122 if (IsBytecode (Filename)) {
123 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
124 if (Result) {
125 DumpSymbolNamesFromModule (Result);
126 } else {
127 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
Brian Gaeke08d03c72003-11-19 21:52:09 +0000128 return;
Brian Gaeke972d3d72003-10-16 04:43:15 +0000129 }
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000130 } else if (IsArchive (Filename)) {
131 std::vector<Module *> Modules;
Brian Gaeke08d03c72003-11-19 21:52:09 +0000132 if (ReadArchiveFile (Filename, Modules, &ErrorMessage)) {
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000133 std::cerr << ToolName << ": " << Filename << ": "
134 << ErrorMessage << "\n";
Brian Gaeke08d03c72003-11-19 21:52:09 +0000135 return;
136 }
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000137 MultipleFiles = true;
138 std::for_each (Modules.begin (), Modules.end (), DumpSymbolNamesFromModule);
Brian Gaeke972d3d72003-10-16 04:43:15 +0000139 }
140}
141
142int main(int argc, char **argv) {
143 cl::ParseCommandLineOptions(argc, argv, " llvm symbol table dumper\n");
144 ToolName = argv[0];
145 if (BSDFormat) OutputFormat = bsd;
146 if (POSIXFormat) OutputFormat = posix;
Chris Lattnerfc046d52003-10-16 18:45:23 +0000147
148 switch (InputFilenames.size()) {
149 case 0: InputFilenames.push_back("-");
150 case 1: break;
151 default: MultipleFiles = true;
152 }
Brian Gaeke972d3d72003-10-16 04:43:15 +0000153
154 std::for_each (InputFilenames.begin (), InputFilenames.end (),
155 DumpSymbolNamesFromFile);
156 return 0;
157}