blob: 014cb296500003249b55cd07d0efa5b6d475481f [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//
Chris Lattner21c62da2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// 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
Owen Anderson8b477ed2009-07-01 16:58:40 +000019#include "llvm/LLVMContext.h"
Brian Gaeke972d3d72003-10-16 04:43:15 +000020#include "llvm/Module.h"
Chris Lattner4d5aad22007-05-06 05:36:18 +000021#include "llvm/Bitcode/ReaderWriter.h"
Chris Lattner44dadff2007-05-06 09:29:57 +000022#include "llvm/Bitcode/Archive.h"
Michael J. Spencer20d335a2011-01-20 06:38:57 +000023#include "llvm/Object/ObjectFile.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/Support/CommandLine.h"
Michael J. Spencer20d335a2011-01-20 06:38:57 +000025#include "llvm/Support/FileSystem.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000026#include "llvm/Support/ManagedStatic.h"
Chris Lattner4d5aad22007-05-06 05:36:18 +000027#include "llvm/Support/MemoryBuffer.h"
Chris Lattnercc14d252009-03-06 05:34:10 +000028#include "llvm/Support/PrettyStackTrace.h"
Dan Gohman65f57c22009-07-15 16:35:29 +000029#include "llvm/Support/raw_ostream.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000030#include "llvm/Support/Signals.h"
Michael J. Spencer20d335a2011-01-20 06:38:57 +000031#include "llvm/Support/Format.h"
Michael J. Spencer333fb042010-12-09 17:36:48 +000032#include "llvm/Support/system_error.h"
Jeff Cohenca5183d2007-03-05 00:00:42 +000033#include <algorithm>
Brian Gaeke972d3d72003-10-16 04:43:15 +000034#include <cctype>
Alkis Evlogimenos09233fb2004-04-21 16:11:40 +000035#include <cerrno>
Brian Gaeke08d03c72003-11-19 21:52:09 +000036#include <cstring>
Michael J. Spencer20d335a2011-01-20 06:38:57 +000037#include <vector>
Brian Gaeked0fde302003-11-11 22:41:34 +000038using namespace llvm;
Michael J. Spencer20d335a2011-01-20 06:38:57 +000039using namespace object;
Brian Gaeked0fde302003-11-11 22:41:34 +000040
Brian Gaeke972d3d72003-10-16 04:43:15 +000041namespace {
42 enum OutputFormatTy { bsd, sysv, posix };
43 cl::opt<OutputFormatTy>
44 OutputFormat("format",
45 cl::desc("Specify output format"),
46 cl::values(clEnumVal(bsd, "BSD format"),
47 clEnumVal(sysv, "System V format"),
Misha Brukman3da94ae2005-04-22 00:00:37 +000048 clEnumVal(posix, "POSIX.2 format"),
Chris Lattner4d143ee2004-07-16 00:08:28 +000049 clEnumValEnd), cl::init(bsd));
Brian Gaeke972d3d72003-10-16 04:43:15 +000050 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
51 cl::aliasopt(OutputFormat));
52
Misha Brukman3da94ae2005-04-22 00:00:37 +000053 cl::list<std::string>
Gabor Greifa99be512007-07-05 17:07:56 +000054 InputFilenames(cl::Positional, cl::desc("<input bitcode files>"),
Chris Lattnerfc046d52003-10-16 18:45:23 +000055 cl::ZeroOrMore);
Brian Gaeke972d3d72003-10-16 04:43:15 +000056
57 cl::opt<bool> UndefinedOnly("undefined-only",
58 cl::desc("Show only undefined symbols"));
59 cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
60 cl::aliasopt(UndefinedOnly));
61
62 cl::opt<bool> DefinedOnly("defined-only",
63 cl::desc("Show only defined symbols"));
64
65 cl::opt<bool> ExternalOnly("extern-only",
66 cl::desc("Show only external symbols"));
67 cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
68 cl::aliasopt(ExternalOnly));
69
70 cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
71 cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
72
Michael J. Spencer20d335a2011-01-20 06:38:57 +000073 cl::opt<bool> PrintFileName("print-file-name",
74 cl::desc("Precede each symbol with the object file it came from"));
75
76 cl::alias PrintFileNameA("A", cl::desc("Alias for --print-file-name"),
77 cl::aliasopt(PrintFileName));
78 cl::alias PrintFileNameo("o", cl::desc("Alias for --print-file-name"),
79 cl::aliasopt(PrintFileName));
80
81 cl::opt<bool> DebugSyms("debug-syms",
82 cl::desc("Show all symbols, even debugger only"));
83 cl::alias DebugSymsa("a", cl::desc("Alias for --debug-syms"),
84 cl::aliasopt(DebugSyms));
85
86 cl::opt<bool> NumericSort("numeric-sort",
87 cl::desc("Sort symbols by address"));
88 cl::alias NumericSortn("n", cl::desc("Alias for --numeric-sort"),
89 cl::aliasopt(NumericSort));
90 cl::alias NumericSortv("v", cl::desc("Alias for --numeric-sort"),
91 cl::aliasopt(NumericSort));
92
93 cl::opt<bool> NoSort("no-sort",
94 cl::desc("Show symbols in order encountered"));
95 cl::alias NoSortp("p", cl::desc("Alias for --no-sort"),
96 cl::aliasopt(NoSort));
97
98 cl::opt<bool> PrintSize("print-size",
99 cl::desc("Show symbol size instead of address"));
100 cl::alias PrintSizeS("S", cl::desc("Alias for --print-size"),
101 cl::aliasopt(PrintSize));
102
103 cl::opt<bool> SizeSort("size-sort", cl::desc("Sort symbols by size"));
104
105 bool PrintAddress = true;
106
Brian Gaeke972d3d72003-10-16 04:43:15 +0000107 bool MultipleFiles = false;
108
109 std::string ToolName;
Chris Lattnerd74ea2b2006-05-24 17:04:05 +0000110}
Brian Gaeke972d3d72003-10-16 04:43:15 +0000111
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000112namespace {
113 struct NMSymbol {
114 uint64_t Address;
115 uint64_t Size;
116 char TypeChar;
117 StringRef Name;
118 };
119
120 static bool CompareSymbolAddress(const NMSymbol &a, const NMSymbol &b) {
121 if (a.Address < b.Address)
122 return true;
123 else if (a.Address == b.Address && a.Name < b.Name)
124 return true;
125 else
126 return false;
127
128 }
129
130 static bool CompareSymbolSize(const NMSymbol &a, const NMSymbol &b) {
131 if (a.Size < b.Size)
132 return true;
133 else if (a.Size == b.Size && a.Name < b.Name)
134 return true;
135 else
136 return false;
137 }
138
139 static bool CompareSymbolName(const NMSymbol &a, const NMSymbol &b) {
140 return a.Name < b.Name;
141 }
142
143 StringRef CurrentFilename;
144 typedef std::vector<NMSymbol> SymbolListT;
145 SymbolListT SymbolList;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000146
147 bool error(error_code ec) {
148 if (!ec) return false;
149
150 outs() << ToolName << ": error reading file: " << ec.message() << ".\n";
151 outs().flush();
152 return true;
153 }
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000154}
155
156static void SortAndPrintSymbolList() {
157 if (!NoSort) {
158 if (NumericSort)
159 std::sort(SymbolList.begin(), SymbolList.end(), CompareSymbolAddress);
160 else if (SizeSort)
161 std::sort(SymbolList.begin(), SymbolList.end(), CompareSymbolSize);
162 else
163 std::sort(SymbolList.begin(), SymbolList.end(), CompareSymbolName);
164 }
165
166 if (OutputFormat == posix && MultipleFiles) {
167 outs() << '\n' << CurrentFilename << ":\n";
168 } else if (OutputFormat == bsd && MultipleFiles) {
169 outs() << "\n" << CurrentFilename << ":\n";
170 } else if (OutputFormat == sysv) {
171 outs() << "\n\nSymbols from " << CurrentFilename << ":\n\n"
172 << "Name Value Class Type"
173 << " Size Line Section\n";
174 }
175
176 for (SymbolListT::iterator i = SymbolList.begin(),
177 e = SymbolList.end(); i != e; ++i) {
178 if ((i->TypeChar != 'U') && UndefinedOnly)
179 continue;
180 if ((i->TypeChar == 'U') && DefinedOnly)
181 continue;
182 if (SizeSort && !PrintAddress && i->Size == UnknownAddressOrSize)
183 continue;
184
185 char SymbolAddrStr[10] = "";
186 char SymbolSizeStr[10] = "";
187
188 if (OutputFormat == sysv || i->Address == object::UnknownAddressOrSize)
189 strcpy(SymbolAddrStr, " ");
190 if (OutputFormat == sysv)
191 strcpy(SymbolSizeStr, " ");
192
193 if (i->Address != object::UnknownAddressOrSize)
194 format("%08x", i->Address).print(SymbolAddrStr, sizeof(SymbolAddrStr));
195 if (i->Size != object::UnknownAddressOrSize)
196 format("%08x", i->Size).print(SymbolSizeStr, sizeof(SymbolSizeStr));
197
198 if (OutputFormat == posix) {
199 outs() << i->Name << " " << i->TypeChar << " "
200 << SymbolAddrStr << SymbolSizeStr << "\n";
201 } else if (OutputFormat == bsd) {
202 if (PrintAddress)
203 outs() << SymbolAddrStr << ' ';
204 if (PrintSize) {
205 outs() << SymbolSizeStr;
206 if (i->Size != object::UnknownAddressOrSize)
207 outs() << ' ';
208 }
209 outs() << i->TypeChar << " " << i->Name << "\n";
210 } else if (OutputFormat == sysv) {
211 std::string PaddedName (i->Name);
212 while (PaddedName.length () < 20)
213 PaddedName += " ";
214 outs() << PaddedName << "|" << SymbolAddrStr << "| "
215 << i->TypeChar
216 << " | |" << SymbolSizeStr << "| |\n";
217 }
218 }
219
220 SymbolList.clear();
221}
222
Chris Lattnerc30598b2006-12-06 01:18:01 +0000223static char TypeCharForSymbol(GlobalValue &GV) {
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +0000224 if (GV.isDeclaration()) return 'U';
Chris Lattnerc30598b2006-12-06 01:18:01 +0000225 if (GV.hasLinkOnceLinkage()) return 'C';
Dale Johannesen7d5633e2008-05-16 22:44:18 +0000226 if (GV.hasCommonLinkage()) return 'C';
Chris Lattnerc30598b2006-12-06 01:18:01 +0000227 if (GV.hasWeakLinkage()) return 'W';
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +0000228 if (isa<Function>(GV) && GV.hasInternalLinkage()) return 't';
Chris Lattnerc30598b2006-12-06 01:18:01 +0000229 if (isa<Function>(GV)) return 'T';
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +0000230 if (isa<GlobalVariable>(GV) && GV.hasInternalLinkage()) return 'd';
Chris Lattnerc30598b2006-12-06 01:18:01 +0000231 if (isa<GlobalVariable>(GV)) return 'D';
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +0000232 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(&GV)) {
233 const GlobalValue *AliasedGV = GA->getAliasedGlobal();
234 if (isa<Function>(AliasedGV)) return 'T';
235 if (isa<GlobalVariable>(AliasedGV)) return 'D';
236 }
237 return '?';
Brian Gaeke972d3d72003-10-16 04:43:15 +0000238}
239
Chris Lattnerc30598b2006-12-06 01:18:01 +0000240static void DumpSymbolNameForGlobalValue(GlobalValue &GV) {
Chris Lattner266c7bb2009-04-13 05:44:34 +0000241 // Private linkage and available_externally linkage don't exist in symtab.
Bill Wendling4e34d502010-08-24 20:00:52 +0000242 if (GV.hasPrivateLinkage() ||
243 GV.hasLinkerPrivateLinkage() ||
244 GV.hasLinkerPrivateWeakLinkage() ||
245 GV.hasLinkerPrivateWeakDefAutoLinkage() ||
246 GV.hasAvailableExternallyLinkage())
Bill Wendling5e721d72010-07-01 21:55:59 +0000247 return;
Chris Lattner266c7bb2009-04-13 05:44:34 +0000248 char TypeChar = TypeCharForSymbol(GV);
Rafael Espindolabb46f522009-01-15 20:18:42 +0000249 if (GV.hasLocalLinkage () && ExternalOnly)
Brian Gaeke972d3d72003-10-16 04:43:15 +0000250 return;
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000251
252 NMSymbol s;
253 s.Address = object::UnknownAddressOrSize;
254 s.Size = object::UnknownAddressOrSize;
255 s.TypeChar = TypeChar;
256 s.Name = GV.getName();
257 SymbolList.push_back(s);
Brian Gaeke972d3d72003-10-16 04:43:15 +0000258}
259
Chris Lattnerc30598b2006-12-06 01:18:01 +0000260static void DumpSymbolNamesFromModule(Module *M) {
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000261 CurrentFilename = M->getModuleIdentifier();
Chris Lattner266c7bb2009-04-13 05:44:34 +0000262 std::for_each (M->begin(), M->end(), DumpSymbolNameForGlobalValue);
263 std::for_each (M->global_begin(), M->global_end(),
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +0000264 DumpSymbolNameForGlobalValue);
Chris Lattner266c7bb2009-04-13 05:44:34 +0000265 std::for_each (M->alias_begin(), M->alias_end(),
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +0000266 DumpSymbolNameForGlobalValue);
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000267
268 SortAndPrintSymbolList();
269}
270
271static void DumpSymbolNamesFromObject(ObjectFile *obj) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000272 error_code ec;
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000273 for (ObjectFile::symbol_iterator i = obj->begin_symbols(),
Michael J. Spencer25b15772011-06-25 17:55:23 +0000274 e = obj->end_symbols();
275 i != e; i.increment(ec)) {
276 if (error(ec)) break;
277 bool internal;
278 if (error(i->isInternal(internal))) break;
279 if (!DebugSyms && internal)
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000280 continue;
281 NMSymbol s;
282 s.Size = object::UnknownAddressOrSize;
283 s.Address = object::UnknownAddressOrSize;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000284 if (PrintSize || SizeSort) {
285 if (error(i->getSize(s.Size))) break;
286 }
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000287 if (PrintAddress)
Michael J. Spencer25b15772011-06-25 17:55:23 +0000288 if (error(i->getAddress(s.Address))) break;
289 if (error(i->getNMTypeChar(s.TypeChar))) break;
290 if (error(i->getName(s.Name))) break;
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000291 SymbolList.push_back(s);
292 }
293
Michael J. Spencer001c9202011-06-25 17:54:50 +0000294 CurrentFilename = obj->getFileName();
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000295 SortAndPrintSymbolList();
Brian Gaeke972d3d72003-10-16 04:43:15 +0000296}
297
Chris Lattnerc30598b2006-12-06 01:18:01 +0000298static void DumpSymbolNamesFromFile(std::string &Filename) {
Owen Anderson0d7c6952009-07-15 22:16:10 +0000299 LLVMContext &Context = getGlobalContext();
Brian Gaeke972d3d72003-10-16 04:43:15 +0000300 std::string ErrorMessage;
Reid Spencer11db4b82004-12-13 03:01:26 +0000301 sys::Path aPath(Filename);
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000302 bool exists;
303 if (sys::fs::exists(aPath.str(), exists) || !exists)
304 errs() << ToolName << ": '" << Filename << "': " << "No such file\n";
Brian Gaeke8b1daa32003-11-19 22:15:00 +0000305 // Note: Currently we do not support reading an archive from stdin.
Chris Lattner44dadff2007-05-06 09:29:57 +0000306 if (Filename == "-" || aPath.isBitcodeFile()) {
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000307 OwningPtr<MemoryBuffer> Buffer;
308 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buffer))
Michael J. Spencer333fb042010-12-09 17:36:48 +0000309 ErrorMessage = ec.message();
Chris Lattner4d5aad22007-05-06 05:36:18 +0000310 Module *Result = 0;
311 if (Buffer.get())
Owen Anderson31895e72009-07-01 21:22:36 +0000312 Result = ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage);
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000313
Nuno Lopes8018f5d2009-09-10 14:56:31 +0000314 if (Result) {
Chris Lattner4d5aad22007-05-06 05:36:18 +0000315 DumpSymbolNamesFromModule(Result);
Nuno Lopes8018f5d2009-09-10 14:56:31 +0000316 delete Result;
317 } else
Dan Gohman65f57c22009-07-15 16:35:29 +0000318 errs() << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000319
Reid Spencer11db4b82004-12-13 03:01:26 +0000320 } else if (aPath.isArchive()) {
Reid Spencer8d8a7ff2006-07-07 20:56:50 +0000321 std::string ErrMsg;
Owen Anderson31895e72009-07-01 21:22:36 +0000322 Archive* archive = Archive::OpenAndLoad(sys::Path(Filename), Context,
Owen Anderson8b477ed2009-07-01 16:58:40 +0000323 &ErrorMessage);
Reid Spencer63efb772004-11-14 22:27:46 +0000324 if (!archive)
Dan Gohman65f57c22009-07-15 16:35:29 +0000325 errs() << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000326 std::vector<Module *> Modules;
Chris Lattnerc30598b2006-12-06 01:18:01 +0000327 if (archive->getAllModules(Modules, &ErrorMessage)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000328 errs() << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
Brian Gaeke08d03c72003-11-19 21:52:09 +0000329 return;
330 }
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000331 MultipleFiles = true;
Chris Lattnerc30598b2006-12-06 01:18:01 +0000332 std::for_each (Modules.begin(), Modules.end(), DumpSymbolNamesFromModule);
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000333 } else if (aPath.isObjectFile()) {
Michael J. Spencer76fb9b02011-06-25 17:54:59 +0000334 OwningPtr<Binary> obj;
335 if (error_code ec = object::createBinary(aPath.str(), obj)) {
336 errs() << ToolName << ": " << Filename << ": " << ec.message() << ".\n";
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000337 return;
338 }
Michael J. Spencer76fb9b02011-06-25 17:54:59 +0000339 if (object::ObjectFile *o = dyn_cast<ObjectFile>(obj.get()))
340 DumpSymbolNamesFromObject(o);
Brian Gaeke82042872003-11-19 21:57:30 +0000341 } else {
Dan Gohman65f57c22009-07-15 16:35:29 +0000342 errs() << ToolName << ": " << Filename << ": "
343 << "unrecognizable file type\n";
Brian Gaeke82042872003-11-19 21:57:30 +0000344 return;
Brian Gaeke972d3d72003-10-16 04:43:15 +0000345 }
346}
347
348int main(int argc, char **argv) {
Chris Lattnercc14d252009-03-06 05:34:10 +0000349 // Print a stack trace if we signal out.
Chris Lattner4d5aad22007-05-06 05:36:18 +0000350 sys::PrintStackTraceOnErrorSignal();
Chris Lattnercc14d252009-03-06 05:34:10 +0000351 PrettyStackTraceProgram X(argc, argv);
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000352
Chris Lattnercc14d252009-03-06 05:34:10 +0000353 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
354 cl::ParseCommandLineOptions(argc, argv, "llvm symbol table dumper\n");
Chris Lattnerf73b4ca2004-02-19 20:32:12 +0000355
Chris Lattner4d5aad22007-05-06 05:36:18 +0000356 ToolName = argv[0];
357 if (BSDFormat) OutputFormat = bsd;
358 if (POSIXFormat) OutputFormat = posix;
Chris Lattnerfc046d52003-10-16 18:45:23 +0000359
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000360 // The relative order of these is important. If you pass --size-sort it should
361 // only print out the size. However, if you pass -S --size-sort, it should
362 // print out both the size and address.
363 if (SizeSort && !PrintSize) PrintAddress = false;
364 if (OutputFormat == sysv || SizeSort) PrintSize = true;
365
Chris Lattner4d5aad22007-05-06 05:36:18 +0000366 switch (InputFilenames.size()) {
367 case 0: InputFilenames.push_back("-");
368 case 1: break;
369 default: MultipleFiles = true;
Chris Lattnerfc046d52003-10-16 18:45:23 +0000370 }
Chris Lattner4d5aad22007-05-06 05:36:18 +0000371
372 std::for_each(InputFilenames.begin(), InputFilenames.end(),
373 DumpSymbolNamesFromFile);
374 return 0;
Brian Gaeke972d3d72003-10-16 04:43:15 +0000375}