blob: aa782aae40fa62a677c486af7e969f1a6c5f7f04 [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//
Michael J. Spencer4d616642012-06-06 23:34:10 +000010// This program is a utility that works like traditional Unix "nm", that is, it
11// prints out the names of symbols in a bitcode or object file, along with some
12// information about each symbol.
Misha Brukman3da94ae2005-04-22 00:00:37 +000013//
Michael J. Spencer4d616642012-06-06 23:34:10 +000014// This "nm" supports many of the features of GNU "nm", including its different
15// output formats.
Brian Gaeke972d3d72003-10-16 04:43:15 +000016//
17//===----------------------------------------------------------------------===//
18
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000019#include "llvm/IR/LLVMContext.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000020#include "llvm/Bitcode/ReaderWriter.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000021#include "llvm/IR/Module.h"
Michael J. Spencer9142ae22011-09-27 19:37:18 +000022#include "llvm/Object/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"
Chandler Carruthf010c462012-12-04 10:44:52 +000026#include "llvm/Support/Format.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000027#include "llvm/Support/ManagedStatic.h"
Chris Lattner4d5aad22007-05-06 05:36:18 +000028#include "llvm/Support/MemoryBuffer.h"
Chris Lattnercc14d252009-03-06 05:34:10 +000029#include "llvm/Support/PrettyStackTrace.h"
Michael J. Spencera740e192011-12-13 23:17:29 +000030#include "llvm/Support/Program.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000031#include "llvm/Support/Signals.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000032#include "llvm/Support/raw_ostream.h"
Michael J. Spencer333fb042010-12-09 17:36:48 +000033#include "llvm/Support/system_error.h"
Jeff Cohenca5183d2007-03-05 00:00:42 +000034#include <algorithm>
Brian Gaeke972d3d72003-10-16 04:43:15 +000035#include <cctype>
Alkis Evlogimenos09233fb2004-04-21 16:11:40 +000036#include <cerrno>
Brian Gaeke08d03c72003-11-19 21:52:09 +000037#include <cstring>
Michael J. Spencer20d335a2011-01-20 06:38:57 +000038#include <vector>
Brian Gaeked0fde302003-11-11 22:41:34 +000039using namespace llvm;
Michael J. Spencer20d335a2011-01-20 06:38:57 +000040using namespace object;
Brian Gaeked0fde302003-11-11 22:41:34 +000041
Brian Gaeke972d3d72003-10-16 04:43:15 +000042namespace {
43 enum OutputFormatTy { bsd, sysv, posix };
44 cl::opt<OutputFormatTy>
45 OutputFormat("format",
46 cl::desc("Specify output format"),
47 cl::values(clEnumVal(bsd, "BSD format"),
48 clEnumVal(sysv, "System V format"),
Misha Brukman3da94ae2005-04-22 00:00:37 +000049 clEnumVal(posix, "POSIX.2 format"),
Chris Lattner4d143ee2004-07-16 00:08:28 +000050 clEnumValEnd), cl::init(bsd));
Brian Gaeke972d3d72003-10-16 04:43:15 +000051 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
52 cl::aliasopt(OutputFormat));
53
Misha Brukman3da94ae2005-04-22 00:00:37 +000054 cl::list<std::string>
Gabor Greifa99be512007-07-05 17:07:56 +000055 InputFilenames(cl::Positional, cl::desc("<input bitcode files>"),
Chris Lattnerfc046d52003-10-16 18:45:23 +000056 cl::ZeroOrMore);
Brian Gaeke972d3d72003-10-16 04:43:15 +000057
58 cl::opt<bool> UndefinedOnly("undefined-only",
59 cl::desc("Show only undefined symbols"));
60 cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
61 cl::aliasopt(UndefinedOnly));
62
Michael J. Spencerdfa18962012-02-28 00:40:37 +000063 cl::opt<bool> DynamicSyms("dynamic",
64 cl::desc("Display the dynamic symbols instead "
65 "of normal symbols."));
66 cl::alias DynamicSyms2("D", cl::desc("Alias for --dynamic"),
67 cl::aliasopt(DynamicSyms));
68
Brian Gaeke972d3d72003-10-16 04:43:15 +000069 cl::opt<bool> DefinedOnly("defined-only",
70 cl::desc("Show only defined symbols"));
71
72 cl::opt<bool> ExternalOnly("extern-only",
73 cl::desc("Show only external symbols"));
74 cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
75 cl::aliasopt(ExternalOnly));
76
77 cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
78 cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
79
Michael J. Spencer20d335a2011-01-20 06:38:57 +000080 cl::opt<bool> PrintFileName("print-file-name",
81 cl::desc("Precede each symbol with the object file it came from"));
82
83 cl::alias PrintFileNameA("A", cl::desc("Alias for --print-file-name"),
84 cl::aliasopt(PrintFileName));
85 cl::alias PrintFileNameo("o", cl::desc("Alias for --print-file-name"),
86 cl::aliasopt(PrintFileName));
87
88 cl::opt<bool> DebugSyms("debug-syms",
89 cl::desc("Show all symbols, even debugger only"));
90 cl::alias DebugSymsa("a", cl::desc("Alias for --debug-syms"),
91 cl::aliasopt(DebugSyms));
92
93 cl::opt<bool> NumericSort("numeric-sort",
94 cl::desc("Sort symbols by address"));
95 cl::alias NumericSortn("n", cl::desc("Alias for --numeric-sort"),
96 cl::aliasopt(NumericSort));
97 cl::alias NumericSortv("v", cl::desc("Alias for --numeric-sort"),
98 cl::aliasopt(NumericSort));
99
100 cl::opt<bool> NoSort("no-sort",
101 cl::desc("Show symbols in order encountered"));
102 cl::alias NoSortp("p", cl::desc("Alias for --no-sort"),
103 cl::aliasopt(NoSort));
104
105 cl::opt<bool> PrintSize("print-size",
106 cl::desc("Show symbol size instead of address"));
107 cl::alias PrintSizeS("S", cl::desc("Alias for --print-size"),
108 cl::aliasopt(PrintSize));
109
110 cl::opt<bool> SizeSort("size-sort", cl::desc("Sort symbols by size"));
111
Jan Sjödin18505b32012-09-18 18:47:58 +0000112 cl::opt<bool> WithoutAliases("without-aliases", cl::Hidden,
113 cl::desc("Exclude aliases from output"));
114
Shankar Easwaran206252c2012-11-13 18:38:42 +0000115 cl::opt<bool> ArchiveMap("print-armap",
116 cl::desc("Print the archive map"));
117 cl::alias ArchiveMaps("s", cl::desc("Alias for --print-armap"),
118 cl::aliasopt(ArchiveMap));
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000119 bool PrintAddress = true;
120
Brian Gaeke972d3d72003-10-16 04:43:15 +0000121 bool MultipleFiles = false;
122
123 std::string ToolName;
Chris Lattnerd74ea2b2006-05-24 17:04:05 +0000124}
Brian Gaeke972d3d72003-10-16 04:43:15 +0000125
Michael J. Spencera740e192011-12-13 23:17:29 +0000126
127static void error(Twine message, Twine path = Twine()) {
128 errs() << ToolName << ": " << path << ": " << message << ".\n";
129}
130
131static bool error(error_code ec, Twine path = Twine()) {
132 if (ec) {
133 error(ec.message(), path);
134 return true;
135 }
136 return false;
137}
138
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000139namespace {
140 struct NMSymbol {
141 uint64_t Address;
142 uint64_t Size;
143 char TypeChar;
144 StringRef Name;
145 };
146
147 static bool CompareSymbolAddress(const NMSymbol &a, const NMSymbol &b) {
148 if (a.Address < b.Address)
149 return true;
150 else if (a.Address == b.Address && a.Name < b.Name)
151 return true;
Daniel Dunbared074e92012-11-13 19:39:55 +0000152 else if (a.Address == b.Address && a.Name == b.Name && a.Size < b.Size)
153 return true;
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000154 else
155 return false;
156
157 }
158
159 static bool CompareSymbolSize(const NMSymbol &a, const NMSymbol &b) {
160 if (a.Size < b.Size)
161 return true;
162 else if (a.Size == b.Size && a.Name < b.Name)
163 return true;
Daniel Dunbared074e92012-11-13 19:39:55 +0000164 else if (a.Size == b.Size && a.Name == b.Name && a.Address < b.Address)
165 return true;
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000166 else
167 return false;
168 }
169
170 static bool CompareSymbolName(const NMSymbol &a, const NMSymbol &b) {
Daniel Dunbared074e92012-11-13 19:39:55 +0000171 if (a.Name < b.Name)
172 return true;
173 else if (a.Name == b.Name && a.Size < b.Size)
174 return true;
175 else if (a.Name == b.Name && a.Size == b.Size && a.Address < b.Address)
176 return true;
177 else
178 return false;
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000179 }
180
181 StringRef CurrentFilename;
182 typedef std::vector<NMSymbol> SymbolListT;
183 SymbolListT SymbolList;
184}
185
186static void SortAndPrintSymbolList() {
187 if (!NoSort) {
188 if (NumericSort)
189 std::sort(SymbolList.begin(), SymbolList.end(), CompareSymbolAddress);
190 else if (SizeSort)
191 std::sort(SymbolList.begin(), SymbolList.end(), CompareSymbolSize);
192 else
193 std::sort(SymbolList.begin(), SymbolList.end(), CompareSymbolName);
194 }
195
196 if (OutputFormat == posix && MultipleFiles) {
197 outs() << '\n' << CurrentFilename << ":\n";
198 } else if (OutputFormat == bsd && MultipleFiles) {
199 outs() << "\n" << CurrentFilename << ":\n";
200 } else if (OutputFormat == sysv) {
201 outs() << "\n\nSymbols from " << CurrentFilename << ":\n\n"
202 << "Name Value Class Type"
203 << " Size Line Section\n";
204 }
205
206 for (SymbolListT::iterator i = SymbolList.begin(),
207 e = SymbolList.end(); i != e; ++i) {
208 if ((i->TypeChar != 'U') && UndefinedOnly)
209 continue;
210 if ((i->TypeChar == 'U') && DefinedOnly)
211 continue;
212 if (SizeSort && !PrintAddress && i->Size == UnknownAddressOrSize)
213 continue;
214
215 char SymbolAddrStr[10] = "";
216 char SymbolSizeStr[10] = "";
217
218 if (OutputFormat == sysv || i->Address == object::UnknownAddressOrSize)
219 strcpy(SymbolAddrStr, " ");
220 if (OutputFormat == sysv)
221 strcpy(SymbolSizeStr, " ");
222
223 if (i->Address != object::UnknownAddressOrSize)
Benjamin Kramer51cf8662012-03-10 02:04:38 +0000224 format("%08" PRIx64, i->Address).print(SymbolAddrStr,
225 sizeof(SymbolAddrStr));
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000226 if (i->Size != object::UnknownAddressOrSize)
Benjamin Kramer51cf8662012-03-10 02:04:38 +0000227 format("%08" PRIx64, i->Size).print(SymbolSizeStr, sizeof(SymbolSizeStr));
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000228
229 if (OutputFormat == posix) {
230 outs() << i->Name << " " << i->TypeChar << " "
231 << SymbolAddrStr << SymbolSizeStr << "\n";
232 } else if (OutputFormat == bsd) {
233 if (PrintAddress)
234 outs() << SymbolAddrStr << ' ';
235 if (PrintSize) {
236 outs() << SymbolSizeStr;
237 if (i->Size != object::UnknownAddressOrSize)
238 outs() << ' ';
239 }
240 outs() << i->TypeChar << " " << i->Name << "\n";
241 } else if (OutputFormat == sysv) {
242 std::string PaddedName (i->Name);
243 while (PaddedName.length () < 20)
244 PaddedName += " ";
245 outs() << PaddedName << "|" << SymbolAddrStr << "| "
246 << i->TypeChar
247 << " | |" << SymbolSizeStr << "| |\n";
248 }
249 }
250
251 SymbolList.clear();
252}
253
Chris Lattnerc30598b2006-12-06 01:18:01 +0000254static char TypeCharForSymbol(GlobalValue &GV) {
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +0000255 if (GV.isDeclaration()) return 'U';
Chris Lattnerc30598b2006-12-06 01:18:01 +0000256 if (GV.hasLinkOnceLinkage()) return 'C';
Dale Johannesen7d5633e2008-05-16 22:44:18 +0000257 if (GV.hasCommonLinkage()) return 'C';
Chris Lattnerc30598b2006-12-06 01:18:01 +0000258 if (GV.hasWeakLinkage()) return 'W';
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +0000259 if (isa<Function>(GV) && GV.hasInternalLinkage()) return 't';
Chris Lattnerc30598b2006-12-06 01:18:01 +0000260 if (isa<Function>(GV)) return 'T';
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +0000261 if (isa<GlobalVariable>(GV) && GV.hasInternalLinkage()) return 'd';
Chris Lattnerc30598b2006-12-06 01:18:01 +0000262 if (isa<GlobalVariable>(GV)) return 'D';
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +0000263 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(&GV)) {
264 const GlobalValue *AliasedGV = GA->getAliasedGlobal();
265 if (isa<Function>(AliasedGV)) return 'T';
266 if (isa<GlobalVariable>(AliasedGV)) return 'D';
267 }
268 return '?';
Brian Gaeke972d3d72003-10-16 04:43:15 +0000269}
270
Chris Lattnerc30598b2006-12-06 01:18:01 +0000271static void DumpSymbolNameForGlobalValue(GlobalValue &GV) {
Chris Lattner266c7bb2009-04-13 05:44:34 +0000272 // Private linkage and available_externally linkage don't exist in symtab.
Bill Wendling4e34d502010-08-24 20:00:52 +0000273 if (GV.hasPrivateLinkage() ||
274 GV.hasLinkerPrivateLinkage() ||
275 GV.hasLinkerPrivateWeakLinkage() ||
Bill Wendling4e34d502010-08-24 20:00:52 +0000276 GV.hasAvailableExternallyLinkage())
Bill Wendling5e721d72010-07-01 21:55:59 +0000277 return;
Chris Lattner266c7bb2009-04-13 05:44:34 +0000278 char TypeChar = TypeCharForSymbol(GV);
Rafael Espindolabb46f522009-01-15 20:18:42 +0000279 if (GV.hasLocalLinkage () && ExternalOnly)
Brian Gaeke972d3d72003-10-16 04:43:15 +0000280 return;
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000281
282 NMSymbol s;
283 s.Address = object::UnknownAddressOrSize;
284 s.Size = object::UnknownAddressOrSize;
285 s.TypeChar = TypeChar;
286 s.Name = GV.getName();
287 SymbolList.push_back(s);
Brian Gaeke972d3d72003-10-16 04:43:15 +0000288}
289
Chris Lattnerc30598b2006-12-06 01:18:01 +0000290static void DumpSymbolNamesFromModule(Module *M) {
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000291 CurrentFilename = M->getModuleIdentifier();
Chris Lattner266c7bb2009-04-13 05:44:34 +0000292 std::for_each (M->begin(), M->end(), DumpSymbolNameForGlobalValue);
293 std::for_each (M->global_begin(), M->global_end(),
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +0000294 DumpSymbolNameForGlobalValue);
Jan Sjödin18505b32012-09-18 18:47:58 +0000295 if (!WithoutAliases)
296 std::for_each (M->alias_begin(), M->alias_end(),
297 DumpSymbolNameForGlobalValue);
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000298
299 SortAndPrintSymbolList();
300}
301
302static void DumpSymbolNamesFromObject(ObjectFile *obj) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000303 error_code ec;
Michael J. Spencerdfa18962012-02-28 00:40:37 +0000304 symbol_iterator ibegin = obj->begin_symbols();
305 symbol_iterator iend = obj->end_symbols();
306 if (DynamicSyms) {
307 ibegin = obj->begin_dynamic_symbols();
308 iend = obj->end_dynamic_symbols();
309 }
310 for (symbol_iterator i = ibegin; i != iend; i.increment(ec)) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000311 if (error(ec)) break;
David Meyerc46255a2012-02-28 23:47:53 +0000312 uint32_t symflags;
313 if (error(i->getFlags(symflags))) break;
314 if (!DebugSyms && (symflags & SymbolRef::SF_FormatSpecific))
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000315 continue;
316 NMSymbol s;
317 s.Size = object::UnknownAddressOrSize;
318 s.Address = object::UnknownAddressOrSize;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000319 if (PrintSize || SizeSort) {
320 if (error(i->getSize(s.Size))) break;
321 }
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000322 if (PrintAddress)
Danil Malyshevb0436a72011-11-29 17:40:10 +0000323 if (error(i->getAddress(s.Address))) break;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000324 if (error(i->getNMTypeChar(s.TypeChar))) break;
325 if (error(i->getName(s.Name))) break;
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000326 SymbolList.push_back(s);
327 }
328
Michael J. Spencer001c9202011-06-25 17:54:50 +0000329 CurrentFilename = obj->getFileName();
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000330 SortAndPrintSymbolList();
Brian Gaeke972d3d72003-10-16 04:43:15 +0000331}
332
Chris Lattnerc30598b2006-12-06 01:18:01 +0000333static void DumpSymbolNamesFromFile(std::string &Filename) {
Michael J. Spencera740e192011-12-13 23:17:29 +0000334 if (Filename != "-" && !sys::fs::exists(Filename)) {
335 errs() << ToolName << ": '" << Filename << "': " << "No such file\n";
336 return;
337 }
338
339 OwningPtr<MemoryBuffer> Buffer;
340 if (error(MemoryBuffer::getFileOrSTDIN(Filename, Buffer), Filename))
341 return;
342
343 sys::fs::file_magic magic = sys::fs::identify_magic(Buffer->getBuffer());
344
Owen Anderson0d7c6952009-07-15 22:16:10 +0000345 LLVMContext &Context = getGlobalContext();
Brian Gaeke972d3d72003-10-16 04:43:15 +0000346 std::string ErrorMessage;
Michael J. Spencera740e192011-12-13 23:17:29 +0000347 if (magic == sys::fs::file_magic::bitcode) {
Chris Lattner4d5aad22007-05-06 05:36:18 +0000348 Module *Result = 0;
Michael J. Spencera740e192011-12-13 23:17:29 +0000349 Result = ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage);
Nuno Lopes8018f5d2009-09-10 14:56:31 +0000350 if (Result) {
Chris Lattner4d5aad22007-05-06 05:36:18 +0000351 DumpSymbolNamesFromModule(Result);
Nuno Lopes8018f5d2009-09-10 14:56:31 +0000352 delete Result;
Michael J. Spencera740e192011-12-13 23:17:29 +0000353 } else {
354 error(ErrorMessage, Filename);
Brian Gaeke08d03c72003-11-19 21:52:09 +0000355 return;
356 }
Michael J. Spencera740e192011-12-13 23:17:29 +0000357 } else if (magic == sys::fs::file_magic::archive) {
358 OwningPtr<Binary> arch;
359 if (error(object::createBinary(Buffer.take(), arch), Filename))
360 return;
361
Michael J. Spencer9142ae22011-09-27 19:37:18 +0000362 if (object::Archive *a = dyn_cast<object::Archive>(arch.get())) {
Shankar Easwaran206252c2012-11-13 18:38:42 +0000363 if (ArchiveMap) {
364 outs() << "Archive map" << "\n";
365 for (object::Archive::symbol_iterator i = a->begin_symbols(),
366 e = a->end_symbols(); i != e; ++i) {
367 object::Archive::child_iterator c;
368 StringRef symname;
369 StringRef filename;
370 if (error(i->getMember(c)))
371 return;
372 if (error(i->getName(symname)))
373 return;
374 if (error(c->getName(filename)))
375 return;
376 outs() << symname << " in " << filename << "\n";
377 }
378 outs() << "\n";
379 }
380
Michael J. Spencer9142ae22011-09-27 19:37:18 +0000381 for (object::Archive::child_iterator i = a->begin_children(),
382 e = a->end_children(); i != e; ++i) {
383 OwningPtr<Binary> child;
Rafael Espindolaaba65b02011-12-25 01:20:19 +0000384 if (i->getAsBinary(child)) {
Michael J. Spencer9142ae22011-09-27 19:37:18 +0000385 // Try opening it as a bitcode file.
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000386 OwningPtr<MemoryBuffer> buff;
387 if (error(i->getMemoryBuffer(buff)))
388 return;
Michael J. Spencer9142ae22011-09-27 19:37:18 +0000389 Module *Result = 0;
390 if (buff)
Benjamin Kramer1a9908d2011-10-10 13:10:04 +0000391 Result = ParseBitcodeFile(buff.get(), Context, &ErrorMessage);
Michael J. Spencer9142ae22011-09-27 19:37:18 +0000392
393 if (Result) {
394 DumpSymbolNamesFromModule(Result);
395 delete Result;
396 }
397 continue;
398 }
399 if (object::ObjectFile *o = dyn_cast<ObjectFile>(child.get())) {
400 outs() << o->getFileName() << ":\n";
401 DumpSymbolNamesFromObject(o);
402 }
403 }
404 }
Michael J. Spencera740e192011-12-13 23:17:29 +0000405 } else if (magic.is_object()) {
Michael J. Spencer76fb9b02011-06-25 17:54:59 +0000406 OwningPtr<Binary> obj;
Michael J. Spencera740e192011-12-13 23:17:29 +0000407 if (error(object::createBinary(Buffer.take(), obj), Filename))
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000408 return;
Michael J. Spencer76fb9b02011-06-25 17:54:59 +0000409 if (object::ObjectFile *o = dyn_cast<ObjectFile>(obj.get()))
410 DumpSymbolNamesFromObject(o);
Brian Gaeke82042872003-11-19 21:57:30 +0000411 } else {
Dan Gohman65f57c22009-07-15 16:35:29 +0000412 errs() << ToolName << ": " << Filename << ": "
413 << "unrecognizable file type\n";
Brian Gaeke82042872003-11-19 21:57:30 +0000414 return;
Brian Gaeke972d3d72003-10-16 04:43:15 +0000415 }
416}
417
418int main(int argc, char **argv) {
Chris Lattnercc14d252009-03-06 05:34:10 +0000419 // Print a stack trace if we signal out.
Chris Lattner4d5aad22007-05-06 05:36:18 +0000420 sys::PrintStackTraceOnErrorSignal();
Chris Lattnercc14d252009-03-06 05:34:10 +0000421 PrettyStackTraceProgram X(argc, argv);
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000422
Chris Lattnercc14d252009-03-06 05:34:10 +0000423 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
424 cl::ParseCommandLineOptions(argc, argv, "llvm symbol table dumper\n");
Chris Lattnerf73b4ca2004-02-19 20:32:12 +0000425
Michael J. Spencera740e192011-12-13 23:17:29 +0000426 // llvm-nm only reads binary files.
Rafael Espindola9f1d9fd2013-06-12 20:58:35 +0000427 if (error(sys::ChangeStdinToBinary()))
Michael J. Spencera740e192011-12-13 23:17:29 +0000428 return 1;
429
Chris Lattner4d5aad22007-05-06 05:36:18 +0000430 ToolName = argv[0];
431 if (BSDFormat) OutputFormat = bsd;
432 if (POSIXFormat) OutputFormat = posix;
Chris Lattnerfc046d52003-10-16 18:45:23 +0000433
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000434 // The relative order of these is important. If you pass --size-sort it should
435 // only print out the size. However, if you pass -S --size-sort, it should
436 // print out both the size and address.
437 if (SizeSort && !PrintSize) PrintAddress = false;
438 if (OutputFormat == sysv || SizeSort) PrintSize = true;
439
Chris Lattner4d5aad22007-05-06 05:36:18 +0000440 switch (InputFilenames.size()) {
441 case 0: InputFilenames.push_back("-");
442 case 1: break;
443 default: MultipleFiles = true;
Chris Lattnerfc046d52003-10-16 18:45:23 +0000444 }
Chris Lattner4d5aad22007-05-06 05:36:18 +0000445
446 std::for_each(InputFilenames.begin(), InputFilenames.end(),
447 DumpSymbolNamesFromFile);
448 return 0;
Brian Gaeke972d3d72003-10-16 04:43:15 +0000449}