blob: ca4654d7931c21ba10ded69ea61e5b0f52b288ff [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- llvm-nm.cpp - Symbol table dumping utility for llvm ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5f5a5732007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This program is a utility that works like traditional Unix "nm",
11// that is, it prints out the names of symbols in a bitcode 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 "llvm/Module.h"
20#include "llvm/Bitcode/ReaderWriter.h"
21#include "llvm/Bitcode/Archive.h"
22#include "llvm/Support/CommandLine.h"
23#include "llvm/Support/ManagedStatic.h"
24#include "llvm/Support/MemoryBuffer.h"
25#include "llvm/System/Signals.h"
26#include <algorithm>
27#include <cctype>
28#include <cerrno>
29#include <cstring>
30#include <iostream>
31using namespace llvm;
32
33namespace {
34 enum OutputFormatTy { bsd, sysv, posix };
35 cl::opt<OutputFormatTy>
36 OutputFormat("format",
37 cl::desc("Specify output format"),
38 cl::values(clEnumVal(bsd, "BSD format"),
39 clEnumVal(sysv, "System V format"),
40 clEnumVal(posix, "POSIX.2 format"),
41 clEnumValEnd), cl::init(bsd));
42 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
43 cl::aliasopt(OutputFormat));
44
45 cl::list<std::string>
46 InputFilenames(cl::Positional, cl::desc("<input bitcode files>"),
47 cl::ZeroOrMore);
48
49 cl::opt<bool> UndefinedOnly("undefined-only",
50 cl::desc("Show only undefined symbols"));
51 cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
52 cl::aliasopt(UndefinedOnly));
53
54 cl::opt<bool> DefinedOnly("defined-only",
55 cl::desc("Show only defined symbols"));
56
57 cl::opt<bool> ExternalOnly("extern-only",
58 cl::desc("Show only external symbols"));
59 cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
60 cl::aliasopt(ExternalOnly));
61
62 cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
63 cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
64
65 bool MultipleFiles = false;
66
67 std::string ToolName;
68}
69
70static char TypeCharForSymbol(GlobalValue &GV) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +000071 /* FIXME: what to do with private linkage? */
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 if (GV.isDeclaration()) return 'U';
73 if (GV.hasLinkOnceLinkage()) return 'C';
Dale Johannesen415ec962008-05-16 22:44:18 +000074 if (GV.hasCommonLinkage()) return 'C';
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 if (GV.hasWeakLinkage()) return 'W';
76 if (isa<Function>(GV) && GV.hasInternalLinkage()) return 't';
77 if (isa<Function>(GV)) return 'T';
78 if (isa<GlobalVariable>(GV) && GV.hasInternalLinkage()) return 'd';
79 if (isa<GlobalVariable>(GV)) return 'D';
80 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(&GV)) {
81 const GlobalValue *AliasedGV = GA->getAliasedGlobal();
82 if (isa<Function>(AliasedGV)) return 'T';
83 if (isa<GlobalVariable>(AliasedGV)) return 'D';
84 }
85 return '?';
86}
87
88static void DumpSymbolNameForGlobalValue(GlobalValue &GV) {
89 const std::string SymbolAddrStr = " "; // Not used yet...
90 char TypeChar = TypeCharForSymbol (GV);
91 if ((TypeChar != 'U') && UndefinedOnly)
92 return;
93 if ((TypeChar == 'U') && DefinedOnly)
94 return;
Rafael Espindolaa168fc92009-01-15 20:18:42 +000095 if (GV.hasLocalLinkage () && ExternalOnly)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096 return;
97 if (OutputFormat == posix) {
98 std::cout << GV.getName () << " " << TypeCharForSymbol (GV) << " "
99 << SymbolAddrStr << "\n";
100 } else if (OutputFormat == bsd) {
101 std::cout << SymbolAddrStr << " " << TypeCharForSymbol (GV) << " "
102 << GV.getName () << "\n";
103 } else if (OutputFormat == sysv) {
104 std::string PaddedName (GV.getName ());
105 while (PaddedName.length () < 20)
106 PaddedName += " ";
107 std::cout << PaddedName << "|" << SymbolAddrStr << "| "
108 << TypeCharForSymbol (GV)
109 << " | | | |\n";
110 }
111}
112
113static void DumpSymbolNamesFromModule(Module *M) {
114 const std::string &Filename = M->getModuleIdentifier ();
115 if (OutputFormat == posix && MultipleFiles) {
116 std::cout << Filename << ":\n";
117 } else if (OutputFormat == bsd && MultipleFiles) {
118 std::cout << "\n" << Filename << ":\n";
119 } else if (OutputFormat == sysv) {
120 std::cout << "\n\nSymbols from " << Filename << ":\n\n"
121 << "Name Value Class Type"
122 << " Size Line Section\n";
123 }
124 std::for_each (M->begin (), M->end (), DumpSymbolNameForGlobalValue);
125 std::for_each (M->global_begin (), M->global_end (),
126 DumpSymbolNameForGlobalValue);
127 std::for_each (M->alias_begin (), M->alias_end (),
128 DumpSymbolNameForGlobalValue);
129}
130
131static void DumpSymbolNamesFromFile(std::string &Filename) {
132 std::string ErrorMessage;
133 sys::Path aPath(Filename);
134 // Note: Currently we do not support reading an archive from stdin.
135 if (Filename == "-" || aPath.isBitcodeFile()) {
136 std::auto_ptr<MemoryBuffer> Buffer(
137 MemoryBuffer::getFileOrSTDIN(Filename, &ErrorMessage));
138 Module *Result = 0;
139 if (Buffer.get())
140 Result = ParseBitcodeFile(Buffer.get(), &ErrorMessage);
141
142 if (Result)
143 DumpSymbolNamesFromModule(Result);
144 else {
145 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
146 return;
147 }
148
149 } else if (aPath.isArchive()) {
150 std::string ErrMsg;
151 Archive* archive = Archive::OpenAndLoad(sys::Path(Filename), &ErrorMessage);
152 if (!archive)
153 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
154 std::vector<Module *> Modules;
155 if (archive->getAllModules(Modules, &ErrorMessage)) {
156 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
157 return;
158 }
159 MultipleFiles = true;
160 std::for_each (Modules.begin(), Modules.end(), DumpSymbolNamesFromModule);
161 } else {
162 std::cerr << ToolName << ": " << Filename << ": "
163 << "unrecognizable file type\n";
164 return;
165 }
166}
167
168int main(int argc, char **argv) {
169 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Dan Gohman6099df82007-10-08 15:45:12 +0000170 cl::ParseCommandLineOptions(argc, argv, "llvm symbol table dumper\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 sys::PrintStackTraceOnErrorSignal();
172
173 ToolName = argv[0];
174 if (BSDFormat) OutputFormat = bsd;
175 if (POSIXFormat) OutputFormat = posix;
176
177 switch (InputFilenames.size()) {
178 case 0: InputFilenames.push_back("-");
179 case 1: break;
180 default: MultipleFiles = true;
181 }
182
183 std::for_each(InputFilenames.begin(), InputFilenames.end(),
184 DumpSymbolNamesFromFile);
185 return 0;
186}