blob: a0b92cc9fb7ce2a106890b9e8c94af21c9ead158 [file] [log] [blame]
Brian Gaeke972d3d72003-10-16 04:43:15 +00001//===-- llvm-nm.cpp - Symbol table dumping utility for llvm ---------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// 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.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Brian Gaeke972d3d72003-10-16 04:43:15 +00009//
10// This program is a utility that works like traditional Unix "nm",
Gabor Greifa99be512007-07-05 17:07:56 +000011// that is, it prints out the names of symbols in a bitcode file,
Brian Gaeke972d3d72003-10-16 04:43:15 +000012// along with some information about each symbol.
Misha Brukman3da94ae2005-04-22 00:00:37 +000013//
Brian Gaeke972d3d72003-10-16 04:43:15 +000014// 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 Lattner4d5aad22007-05-06 05:36:18 +000020#include "llvm/Bitcode/ReaderWriter.h"
Chris Lattner44dadff2007-05-06 09:29:57 +000021#include "llvm/Bitcode/Archive.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000022#include "llvm/Support/CommandLine.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000023#include "llvm/Support/ManagedStatic.h"
Chris Lattner4d5aad22007-05-06 05:36:18 +000024#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000025#include "llvm/System/Signals.h"
Jeff Cohenca5183d2007-03-05 00:00:42 +000026#include <algorithm>
Brian Gaeke972d3d72003-10-16 04:43:15 +000027#include <cctype>
Alkis Evlogimenos09233fb2004-04-21 16:11:40 +000028#include <cerrno>
Brian Gaeke08d03c72003-11-19 21:52:09 +000029#include <cstring>
Reid Spencer86f42bd2004-07-04 12:20:55 +000030#include <iostream>
Brian Gaeked0fde302003-11-11 22:41:34 +000031using namespace llvm;
32
Brian Gaeke972d3d72003-10-16 04:43:15 +000033namespace {
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"),
Misha Brukman3da94ae2005-04-22 00:00:37 +000040 clEnumVal(posix, "POSIX.2 format"),
Chris Lattner4d143ee2004-07-16 00:08:28 +000041 clEnumValEnd), cl::init(bsd));
Brian Gaeke972d3d72003-10-16 04:43:15 +000042 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
43 cl::aliasopt(OutputFormat));
44
Misha Brukman3da94ae2005-04-22 00:00:37 +000045 cl::list<std::string>
Gabor Greifa99be512007-07-05 17:07:56 +000046 InputFilenames(cl::Positional, cl::desc("<input bitcode files>"),
Chris Lattnerfc046d52003-10-16 18:45:23 +000047 cl::ZeroOrMore);
Brian Gaeke972d3d72003-10-16 04:43:15 +000048
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;
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000068}
Brian Gaeke972d3d72003-10-16 04:43:15 +000069
Chris Lattnerc30598b2006-12-06 01:18:01 +000070static char TypeCharForSymbol(GlobalValue &GV) {
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +000071 if (GV.isDeclaration()) return 'U';
Chris Lattnerc30598b2006-12-06 01:18:01 +000072 if (GV.hasLinkOnceLinkage()) return 'C';
73 if (GV.hasWeakLinkage()) return 'W';
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +000074 if (isa<Function>(GV) && GV.hasInternalLinkage()) return 't';
Chris Lattnerc30598b2006-12-06 01:18:01 +000075 if (isa<Function>(GV)) return 'T';
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +000076 if (isa<GlobalVariable>(GV) && GV.hasInternalLinkage()) return 'd';
Chris Lattnerc30598b2006-12-06 01:18:01 +000077 if (isa<GlobalVariable>(GV)) return 'D';
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +000078 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(&GV)) {
79 const GlobalValue *AliasedGV = GA->getAliasedGlobal();
80 if (isa<Function>(AliasedGV)) return 'T';
81 if (isa<GlobalVariable>(AliasedGV)) return 'D';
82 }
83 return '?';
Brian Gaeke972d3d72003-10-16 04:43:15 +000084}
85
Chris Lattnerc30598b2006-12-06 01:18:01 +000086static void DumpSymbolNameForGlobalValue(GlobalValue &GV) {
Brian Gaeke972d3d72003-10-16 04:43:15 +000087 const std::string SymbolAddrStr = " "; // Not used yet...
88 char TypeChar = TypeCharForSymbol (GV);
89 if ((TypeChar != 'U') && UndefinedOnly)
90 return;
91 if ((TypeChar == 'U') && DefinedOnly)
92 return;
93 if (GV.hasInternalLinkage () && ExternalOnly)
94 return;
95 if (OutputFormat == posix) {
96 std::cout << GV.getName () << " " << TypeCharForSymbol (GV) << " "
97 << SymbolAddrStr << "\n";
98 } else if (OutputFormat == bsd) {
99 std::cout << SymbolAddrStr << " " << TypeCharForSymbol (GV) << " "
100 << GV.getName () << "\n";
101 } else if (OutputFormat == sysv) {
102 std::string PaddedName (GV.getName ());
103 while (PaddedName.length () < 20)
104 PaddedName += " ";
105 std::cout << PaddedName << "|" << SymbolAddrStr << "| "
106 << TypeCharForSymbol (GV)
107 << " | | | |\n";
108 }
109}
110
Chris Lattnerc30598b2006-12-06 01:18:01 +0000111static void DumpSymbolNamesFromModule(Module *M) {
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000112 const std::string &Filename = M->getModuleIdentifier ();
113 if (OutputFormat == posix && MultipleFiles) {
114 std::cout << Filename << ":\n";
115 } else if (OutputFormat == bsd && MultipleFiles) {
116 std::cout << "\n" << Filename << ":\n";
117 } else if (OutputFormat == sysv) {
118 std::cout << "\n\nSymbols from " << Filename << ":\n\n"
119 << "Name Value Class Type"
120 << " Size Line Section\n";
121 }
Brian Gaeke972d3d72003-10-16 04:43:15 +0000122 std::for_each (M->begin (), M->end (), DumpSymbolNameForGlobalValue);
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +0000123 std::for_each (M->global_begin (), M->global_end (),
124 DumpSymbolNameForGlobalValue);
125 std::for_each (M->alias_begin (), M->alias_end (),
126 DumpSymbolNameForGlobalValue);
Brian Gaeke972d3d72003-10-16 04:43:15 +0000127}
128
Chris Lattnerc30598b2006-12-06 01:18:01 +0000129static void DumpSymbolNamesFromFile(std::string &Filename) {
Brian Gaeke972d3d72003-10-16 04:43:15 +0000130 std::string ErrorMessage;
Reid Spencer11db4b82004-12-13 03:01:26 +0000131 sys::Path aPath(Filename);
Brian Gaeke8b1daa32003-11-19 22:15:00 +0000132 // Note: Currently we do not support reading an archive from stdin.
Chris Lattner44dadff2007-05-06 09:29:57 +0000133 if (Filename == "-" || aPath.isBitcodeFile()) {
Chris Lattner4d5aad22007-05-06 05:36:18 +0000134 std::auto_ptr<MemoryBuffer> Buffer(
Chris Lattner065344d2007-05-06 23:45:49 +0000135 MemoryBuffer::getFileOrSTDIN(Filename, &ErrorMessage));
Chris Lattner4d5aad22007-05-06 05:36:18 +0000136 Module *Result = 0;
137 if (Buffer.get())
138 Result = ParseBitcodeFile(Buffer.get(), &ErrorMessage);
139
140 if (Result)
141 DumpSymbolNamesFromModule(Result);
142 else {
143 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
144 return;
145 }
146
Reid Spencer11db4b82004-12-13 03:01:26 +0000147 } else if (aPath.isArchive()) {
Reid Spencer8d8a7ff2006-07-07 20:56:50 +0000148 std::string ErrMsg;
Chris Lattnerc30598b2006-12-06 01:18:01 +0000149 Archive* archive = Archive::OpenAndLoad(sys::Path(Filename), &ErrorMessage);
Reid Spencer63efb772004-11-14 22:27:46 +0000150 if (!archive)
151 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000152 std::vector<Module *> Modules;
Chris Lattnerc30598b2006-12-06 01:18:01 +0000153 if (archive->getAllModules(Modules, &ErrorMessage)) {
Reid Spencer63efb772004-11-14 22:27:46 +0000154 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
Brian Gaeke08d03c72003-11-19 21:52:09 +0000155 return;
156 }
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000157 MultipleFiles = true;
Chris Lattnerc30598b2006-12-06 01:18:01 +0000158 std::for_each (Modules.begin(), Modules.end(), DumpSymbolNamesFromModule);
Brian Gaeke82042872003-11-19 21:57:30 +0000159 } else {
160 std::cerr << ToolName << ": " << Filename << ": "
161 << "unrecognizable file type\n";
162 return;
Brian Gaeke972d3d72003-10-16 04:43:15 +0000163 }
164}
165
166int main(int argc, char **argv) {
Chris Lattnerc30598b2006-12-06 01:18:01 +0000167 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Dan Gohman82a13c92007-10-08 15:45:12 +0000168 cl::ParseCommandLineOptions(argc, argv, "llvm symbol table dumper\n");
Chris Lattner4d5aad22007-05-06 05:36:18 +0000169 sys::PrintStackTraceOnErrorSignal();
Chris Lattnerf73b4ca2004-02-19 20:32:12 +0000170
Chris Lattner4d5aad22007-05-06 05:36:18 +0000171 ToolName = argv[0];
172 if (BSDFormat) OutputFormat = bsd;
173 if (POSIXFormat) OutputFormat = posix;
Chris Lattnerfc046d52003-10-16 18:45:23 +0000174
Chris Lattner4d5aad22007-05-06 05:36:18 +0000175 switch (InputFilenames.size()) {
176 case 0: InputFilenames.push_back("-");
177 case 1: break;
178 default: MultipleFiles = true;
Chris Lattnerfc046d52003-10-16 18:45:23 +0000179 }
Chris Lattner4d5aad22007-05-06 05:36:18 +0000180
181 std::for_each(InputFilenames.begin(), InputFilenames.end(),
182 DumpSymbolNamesFromFile);
183 return 0;
Brian Gaeke972d3d72003-10-16 04:43:15 +0000184}