blob: 1afa5032957c50298c5cd581157114c6bd1bcd5b [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;
146}
147
148static void SortAndPrintSymbolList() {
149 if (!NoSort) {
150 if (NumericSort)
151 std::sort(SymbolList.begin(), SymbolList.end(), CompareSymbolAddress);
152 else if (SizeSort)
153 std::sort(SymbolList.begin(), SymbolList.end(), CompareSymbolSize);
154 else
155 std::sort(SymbolList.begin(), SymbolList.end(), CompareSymbolName);
156 }
157
158 if (OutputFormat == posix && MultipleFiles) {
159 outs() << '\n' << CurrentFilename << ":\n";
160 } else if (OutputFormat == bsd && MultipleFiles) {
161 outs() << "\n" << CurrentFilename << ":\n";
162 } else if (OutputFormat == sysv) {
163 outs() << "\n\nSymbols from " << CurrentFilename << ":\n\n"
164 << "Name Value Class Type"
165 << " Size Line Section\n";
166 }
167
168 for (SymbolListT::iterator i = SymbolList.begin(),
169 e = SymbolList.end(); i != e; ++i) {
170 if ((i->TypeChar != 'U') && UndefinedOnly)
171 continue;
172 if ((i->TypeChar == 'U') && DefinedOnly)
173 continue;
174 if (SizeSort && !PrintAddress && i->Size == UnknownAddressOrSize)
175 continue;
176
177 char SymbolAddrStr[10] = "";
178 char SymbolSizeStr[10] = "";
179
180 if (OutputFormat == sysv || i->Address == object::UnknownAddressOrSize)
181 strcpy(SymbolAddrStr, " ");
182 if (OutputFormat == sysv)
183 strcpy(SymbolSizeStr, " ");
184
185 if (i->Address != object::UnknownAddressOrSize)
186 format("%08x", i->Address).print(SymbolAddrStr, sizeof(SymbolAddrStr));
187 if (i->Size != object::UnknownAddressOrSize)
188 format("%08x", i->Size).print(SymbolSizeStr, sizeof(SymbolSizeStr));
189
190 if (OutputFormat == posix) {
191 outs() << i->Name << " " << i->TypeChar << " "
192 << SymbolAddrStr << SymbolSizeStr << "\n";
193 } else if (OutputFormat == bsd) {
194 if (PrintAddress)
195 outs() << SymbolAddrStr << ' ';
196 if (PrintSize) {
197 outs() << SymbolSizeStr;
198 if (i->Size != object::UnknownAddressOrSize)
199 outs() << ' ';
200 }
201 outs() << i->TypeChar << " " << i->Name << "\n";
202 } else if (OutputFormat == sysv) {
203 std::string PaddedName (i->Name);
204 while (PaddedName.length () < 20)
205 PaddedName += " ";
206 outs() << PaddedName << "|" << SymbolAddrStr << "| "
207 << i->TypeChar
208 << " | |" << SymbolSizeStr << "| |\n";
209 }
210 }
211
212 SymbolList.clear();
213}
214
Chris Lattnerc30598b2006-12-06 01:18:01 +0000215static char TypeCharForSymbol(GlobalValue &GV) {
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +0000216 if (GV.isDeclaration()) return 'U';
Chris Lattnerc30598b2006-12-06 01:18:01 +0000217 if (GV.hasLinkOnceLinkage()) return 'C';
Dale Johannesen7d5633e2008-05-16 22:44:18 +0000218 if (GV.hasCommonLinkage()) return 'C';
Chris Lattnerc30598b2006-12-06 01:18:01 +0000219 if (GV.hasWeakLinkage()) return 'W';
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +0000220 if (isa<Function>(GV) && GV.hasInternalLinkage()) return 't';
Chris Lattnerc30598b2006-12-06 01:18:01 +0000221 if (isa<Function>(GV)) return 'T';
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +0000222 if (isa<GlobalVariable>(GV) && GV.hasInternalLinkage()) return 'd';
Chris Lattnerc30598b2006-12-06 01:18:01 +0000223 if (isa<GlobalVariable>(GV)) return 'D';
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +0000224 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(&GV)) {
225 const GlobalValue *AliasedGV = GA->getAliasedGlobal();
226 if (isa<Function>(AliasedGV)) return 'T';
227 if (isa<GlobalVariable>(AliasedGV)) return 'D';
228 }
229 return '?';
Brian Gaeke972d3d72003-10-16 04:43:15 +0000230}
231
Chris Lattnerc30598b2006-12-06 01:18:01 +0000232static void DumpSymbolNameForGlobalValue(GlobalValue &GV) {
Chris Lattner266c7bb2009-04-13 05:44:34 +0000233 // Private linkage and available_externally linkage don't exist in symtab.
Bill Wendling4e34d502010-08-24 20:00:52 +0000234 if (GV.hasPrivateLinkage() ||
235 GV.hasLinkerPrivateLinkage() ||
236 GV.hasLinkerPrivateWeakLinkage() ||
237 GV.hasLinkerPrivateWeakDefAutoLinkage() ||
238 GV.hasAvailableExternallyLinkage())
Bill Wendling5e721d72010-07-01 21:55:59 +0000239 return;
Chris Lattner266c7bb2009-04-13 05:44:34 +0000240 char TypeChar = TypeCharForSymbol(GV);
Rafael Espindolabb46f522009-01-15 20:18:42 +0000241 if (GV.hasLocalLinkage () && ExternalOnly)
Brian Gaeke972d3d72003-10-16 04:43:15 +0000242 return;
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000243
244 NMSymbol s;
245 s.Address = object::UnknownAddressOrSize;
246 s.Size = object::UnknownAddressOrSize;
247 s.TypeChar = TypeChar;
248 s.Name = GV.getName();
249 SymbolList.push_back(s);
Brian Gaeke972d3d72003-10-16 04:43:15 +0000250}
251
Chris Lattnerc30598b2006-12-06 01:18:01 +0000252static void DumpSymbolNamesFromModule(Module *M) {
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000253 CurrentFilename = M->getModuleIdentifier();
Chris Lattner266c7bb2009-04-13 05:44:34 +0000254 std::for_each (M->begin(), M->end(), DumpSymbolNameForGlobalValue);
255 std::for_each (M->global_begin(), M->global_end(),
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +0000256 DumpSymbolNameForGlobalValue);
Chris Lattner266c7bb2009-04-13 05:44:34 +0000257 std::for_each (M->alias_begin(), M->alias_end(),
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +0000258 DumpSymbolNameForGlobalValue);
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000259
260 SortAndPrintSymbolList();
261}
262
263static void DumpSymbolNamesFromObject(ObjectFile *obj) {
264 for (ObjectFile::symbol_iterator i = obj->begin_symbols(),
265 e = obj->end_symbols(); i != e; ++i) {
266 if (!DebugSyms && i->isInternal())
267 continue;
268 NMSymbol s;
269 s.Size = object::UnknownAddressOrSize;
270 s.Address = object::UnknownAddressOrSize;
271 if (PrintSize || SizeSort)
272 s.Size = i->getSize();
273 if (PrintAddress)
274 s.Address = i->getAddress();
275 s.TypeChar = i->getNMTypeChar();
276 s.Name = i->getName();
277 SymbolList.push_back(s);
278 }
279
280 CurrentFilename = obj->getFilename();
281 SortAndPrintSymbolList();
Brian Gaeke972d3d72003-10-16 04:43:15 +0000282}
283
Chris Lattnerc30598b2006-12-06 01:18:01 +0000284static void DumpSymbolNamesFromFile(std::string &Filename) {
Owen Anderson0d7c6952009-07-15 22:16:10 +0000285 LLVMContext &Context = getGlobalContext();
Brian Gaeke972d3d72003-10-16 04:43:15 +0000286 std::string ErrorMessage;
Reid Spencer11db4b82004-12-13 03:01:26 +0000287 sys::Path aPath(Filename);
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000288 bool exists;
289 if (sys::fs::exists(aPath.str(), exists) || !exists)
290 errs() << ToolName << ": '" << Filename << "': " << "No such file\n";
Brian Gaeke8b1daa32003-11-19 22:15:00 +0000291 // Note: Currently we do not support reading an archive from stdin.
Chris Lattner44dadff2007-05-06 09:29:57 +0000292 if (Filename == "-" || aPath.isBitcodeFile()) {
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000293 OwningPtr<MemoryBuffer> Buffer;
294 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buffer))
Michael J. Spencer333fb042010-12-09 17:36:48 +0000295 ErrorMessage = ec.message();
Chris Lattner4d5aad22007-05-06 05:36:18 +0000296 Module *Result = 0;
297 if (Buffer.get())
Owen Anderson31895e72009-07-01 21:22:36 +0000298 Result = ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage);
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000299
Nuno Lopes8018f5d2009-09-10 14:56:31 +0000300 if (Result) {
Chris Lattner4d5aad22007-05-06 05:36:18 +0000301 DumpSymbolNamesFromModule(Result);
Nuno Lopes8018f5d2009-09-10 14:56:31 +0000302 delete Result;
303 } else
Dan Gohman65f57c22009-07-15 16:35:29 +0000304 errs() << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000305
Reid Spencer11db4b82004-12-13 03:01:26 +0000306 } else if (aPath.isArchive()) {
Reid Spencer8d8a7ff2006-07-07 20:56:50 +0000307 std::string ErrMsg;
Owen Anderson31895e72009-07-01 21:22:36 +0000308 Archive* archive = Archive::OpenAndLoad(sys::Path(Filename), Context,
Owen Anderson8b477ed2009-07-01 16:58:40 +0000309 &ErrorMessage);
Reid Spencer63efb772004-11-14 22:27:46 +0000310 if (!archive)
Dan Gohman65f57c22009-07-15 16:35:29 +0000311 errs() << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000312 std::vector<Module *> Modules;
Chris Lattnerc30598b2006-12-06 01:18:01 +0000313 if (archive->getAllModules(Modules, &ErrorMessage)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000314 errs() << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
Brian Gaeke08d03c72003-11-19 21:52:09 +0000315 return;
316 }
Brian Gaeke1c0b6982003-11-16 23:34:13 +0000317 MultipleFiles = true;
Chris Lattnerc30598b2006-12-06 01:18:01 +0000318 std::for_each (Modules.begin(), Modules.end(), DumpSymbolNamesFromModule);
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000319 } else if (aPath.isObjectFile()) {
320 std::auto_ptr<ObjectFile> obj(ObjectFile::createObjectFile(aPath.str()));
321 if (!obj.get()) {
322 errs() << ToolName << ": " << Filename << ": "
323 << "Failed to open object file\n";
324 return;
325 }
326 DumpSymbolNamesFromObject(obj.get());
Brian Gaeke82042872003-11-19 21:57:30 +0000327 } else {
Dan Gohman65f57c22009-07-15 16:35:29 +0000328 errs() << ToolName << ": " << Filename << ": "
329 << "unrecognizable file type\n";
Brian Gaeke82042872003-11-19 21:57:30 +0000330 return;
Brian Gaeke972d3d72003-10-16 04:43:15 +0000331 }
332}
333
334int main(int argc, char **argv) {
Chris Lattnercc14d252009-03-06 05:34:10 +0000335 // Print a stack trace if we signal out.
Chris Lattner4d5aad22007-05-06 05:36:18 +0000336 sys::PrintStackTraceOnErrorSignal();
Chris Lattnercc14d252009-03-06 05:34:10 +0000337 PrettyStackTraceProgram X(argc, argv);
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000338
Chris Lattnercc14d252009-03-06 05:34:10 +0000339 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
340 cl::ParseCommandLineOptions(argc, argv, "llvm symbol table dumper\n");
Chris Lattnerf73b4ca2004-02-19 20:32:12 +0000341
Chris Lattner4d5aad22007-05-06 05:36:18 +0000342 ToolName = argv[0];
343 if (BSDFormat) OutputFormat = bsd;
344 if (POSIXFormat) OutputFormat = posix;
Chris Lattnerfc046d52003-10-16 18:45:23 +0000345
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000346 // The relative order of these is important. If you pass --size-sort it should
347 // only print out the size. However, if you pass -S --size-sort, it should
348 // print out both the size and address.
349 if (SizeSort && !PrintSize) PrintAddress = false;
350 if (OutputFormat == sysv || SizeSort) PrintSize = true;
351
Chris Lattner4d5aad22007-05-06 05:36:18 +0000352 switch (InputFilenames.size()) {
353 case 0: InputFilenames.push_back("-");
354 case 1: break;
355 default: MultipleFiles = true;
Chris Lattnerfc046d52003-10-16 18:45:23 +0000356 }
Chris Lattner4d5aad22007-05-06 05:36:18 +0000357
358 std::for_each(InputFilenames.begin(), InputFilenames.end(),
359 DumpSymbolNamesFromFile);
360 return 0;
Brian Gaeke972d3d72003-10-16 04:43:15 +0000361}