blob: c33289bd875c76727b131cf3f8beb730c92253d9 [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"
Alexey Samsonov9c22f872013-06-18 15:03:28 +000023#include "llvm/Object/MachOUniversal.h"
Michael J. Spencer20d335a2011-01-20 06:38:57 +000024#include "llvm/Object/ObjectFile.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000025#include "llvm/Support/CommandLine.h"
Michael J. Spencer20d335a2011-01-20 06:38:57 +000026#include "llvm/Support/FileSystem.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000027#include "llvm/Support/Format.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000028#include "llvm/Support/ManagedStatic.h"
Chris Lattner4d5aad22007-05-06 05:36:18 +000029#include "llvm/Support/MemoryBuffer.h"
Chris Lattnercc14d252009-03-06 05:34:10 +000030#include "llvm/Support/PrettyStackTrace.h"
Michael J. Spencera740e192011-12-13 23:17:29 +000031#include "llvm/Support/Program.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000032#include "llvm/Support/Signals.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000033#include "llvm/Support/raw_ostream.h"
Michael J. Spencer333fb042010-12-09 17:36:48 +000034#include "llvm/Support/system_error.h"
Jeff Cohenca5183d2007-03-05 00:00:42 +000035#include <algorithm>
Brian Gaeke972d3d72003-10-16 04:43:15 +000036#include <cctype>
Alkis Evlogimenos09233fb2004-04-21 16:11:40 +000037#include <cerrno>
Brian Gaeke08d03c72003-11-19 21:52:09 +000038#include <cstring>
Michael J. Spencer20d335a2011-01-20 06:38:57 +000039#include <vector>
Brian Gaeked0fde302003-11-11 22:41:34 +000040using namespace llvm;
Michael J. Spencer20d335a2011-01-20 06:38:57 +000041using namespace object;
Brian Gaeked0fde302003-11-11 22:41:34 +000042
Brian Gaeke972d3d72003-10-16 04:43:15 +000043namespace {
44 enum OutputFormatTy { bsd, sysv, posix };
45 cl::opt<OutputFormatTy>
46 OutputFormat("format",
47 cl::desc("Specify output format"),
48 cl::values(clEnumVal(bsd, "BSD format"),
49 clEnumVal(sysv, "System V format"),
Misha Brukman3da94ae2005-04-22 00:00:37 +000050 clEnumVal(posix, "POSIX.2 format"),
Chris Lattner4d143ee2004-07-16 00:08:28 +000051 clEnumValEnd), cl::init(bsd));
Brian Gaeke972d3d72003-10-16 04:43:15 +000052 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
53 cl::aliasopt(OutputFormat));
54
Misha Brukman3da94ae2005-04-22 00:00:37 +000055 cl::list<std::string>
Gabor Greifa99be512007-07-05 17:07:56 +000056 InputFilenames(cl::Positional, cl::desc("<input bitcode files>"),
Chris Lattnerfc046d52003-10-16 18:45:23 +000057 cl::ZeroOrMore);
Brian Gaeke972d3d72003-10-16 04:43:15 +000058
59 cl::opt<bool> UndefinedOnly("undefined-only",
60 cl::desc("Show only undefined symbols"));
61 cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
62 cl::aliasopt(UndefinedOnly));
63
Michael J. Spencerdfa18962012-02-28 00:40:37 +000064 cl::opt<bool> DynamicSyms("dynamic",
65 cl::desc("Display the dynamic symbols instead "
66 "of normal symbols."));
67 cl::alias DynamicSyms2("D", cl::desc("Alias for --dynamic"),
68 cl::aliasopt(DynamicSyms));
69
Brian Gaeke972d3d72003-10-16 04:43:15 +000070 cl::opt<bool> DefinedOnly("defined-only",
71 cl::desc("Show only defined symbols"));
72
73 cl::opt<bool> ExternalOnly("extern-only",
74 cl::desc("Show only external symbols"));
75 cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
76 cl::aliasopt(ExternalOnly));
77
78 cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
79 cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
80
Michael J. Spencer20d335a2011-01-20 06:38:57 +000081 cl::opt<bool> PrintFileName("print-file-name",
82 cl::desc("Precede each symbol with the object file it came from"));
83
84 cl::alias PrintFileNameA("A", cl::desc("Alias for --print-file-name"),
85 cl::aliasopt(PrintFileName));
86 cl::alias PrintFileNameo("o", cl::desc("Alias for --print-file-name"),
87 cl::aliasopt(PrintFileName));
88
89 cl::opt<bool> DebugSyms("debug-syms",
90 cl::desc("Show all symbols, even debugger only"));
91 cl::alias DebugSymsa("a", cl::desc("Alias for --debug-syms"),
92 cl::aliasopt(DebugSyms));
93
94 cl::opt<bool> NumericSort("numeric-sort",
95 cl::desc("Sort symbols by address"));
96 cl::alias NumericSortn("n", cl::desc("Alias for --numeric-sort"),
97 cl::aliasopt(NumericSort));
98 cl::alias NumericSortv("v", cl::desc("Alias for --numeric-sort"),
99 cl::aliasopt(NumericSort));
100
101 cl::opt<bool> NoSort("no-sort",
102 cl::desc("Show symbols in order encountered"));
103 cl::alias NoSortp("p", cl::desc("Alias for --no-sort"),
104 cl::aliasopt(NoSort));
105
106 cl::opt<bool> PrintSize("print-size",
107 cl::desc("Show symbol size instead of address"));
108 cl::alias PrintSizeS("S", cl::desc("Alias for --print-size"),
109 cl::aliasopt(PrintSize));
110
111 cl::opt<bool> SizeSort("size-sort", cl::desc("Sort symbols by size"));
112
Jan Sjödin18505b32012-09-18 18:47:58 +0000113 cl::opt<bool> WithoutAliases("without-aliases", cl::Hidden,
114 cl::desc("Exclude aliases from output"));
115
Shankar Easwaran206252c2012-11-13 18:38:42 +0000116 cl::opt<bool> ArchiveMap("print-armap",
117 cl::desc("Print the archive map"));
118 cl::alias ArchiveMaps("s", cl::desc("Alias for --print-armap"),
119 cl::aliasopt(ArchiveMap));
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000120 bool PrintAddress = true;
121
Brian Gaeke972d3d72003-10-16 04:43:15 +0000122 bool MultipleFiles = false;
123
Rafael Espindola40d40dd2013-07-03 15:46:03 +0000124 bool HadError = false;
125
Brian Gaeke972d3d72003-10-16 04:43:15 +0000126 std::string ToolName;
Chris Lattnerd74ea2b2006-05-24 17:04:05 +0000127}
Brian Gaeke972d3d72003-10-16 04:43:15 +0000128
Michael J. Spencera740e192011-12-13 23:17:29 +0000129
130static void error(Twine message, Twine path = Twine()) {
131 errs() << ToolName << ": " << path << ": " << message << ".\n";
132}
133
134static bool error(error_code ec, Twine path = Twine()) {
135 if (ec) {
136 error(ec.message(), path);
Rafael Espindola40d40dd2013-07-03 15:46:03 +0000137 HadError = true;
Michael J. Spencera740e192011-12-13 23:17:29 +0000138 return true;
139 }
140 return false;
141}
142
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000143namespace {
144 struct NMSymbol {
145 uint64_t Address;
146 uint64_t Size;
147 char TypeChar;
148 StringRef Name;
149 };
150
151 static bool CompareSymbolAddress(const NMSymbol &a, const NMSymbol &b) {
152 if (a.Address < b.Address)
153 return true;
154 else if (a.Address == b.Address && a.Name < b.Name)
155 return true;
Daniel Dunbared074e92012-11-13 19:39:55 +0000156 else if (a.Address == b.Address && a.Name == b.Name && a.Size < b.Size)
157 return true;
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000158 else
159 return false;
160
161 }
162
163 static bool CompareSymbolSize(const NMSymbol &a, const NMSymbol &b) {
164 if (a.Size < b.Size)
165 return true;
166 else if (a.Size == b.Size && a.Name < b.Name)
167 return true;
Daniel Dunbared074e92012-11-13 19:39:55 +0000168 else if (a.Size == b.Size && a.Name == b.Name && a.Address < b.Address)
169 return true;
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000170 else
171 return false;
172 }
173
174 static bool CompareSymbolName(const NMSymbol &a, const NMSymbol &b) {
Daniel Dunbared074e92012-11-13 19:39:55 +0000175 if (a.Name < b.Name)
176 return true;
177 else if (a.Name == b.Name && a.Size < b.Size)
178 return true;
179 else if (a.Name == b.Name && a.Size == b.Size && a.Address < b.Address)
180 return true;
181 else
182 return false;
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000183 }
184
185 StringRef CurrentFilename;
186 typedef std::vector<NMSymbol> SymbolListT;
187 SymbolListT SymbolList;
188}
189
190static void SortAndPrintSymbolList() {
191 if (!NoSort) {
192 if (NumericSort)
193 std::sort(SymbolList.begin(), SymbolList.end(), CompareSymbolAddress);
194 else if (SizeSort)
195 std::sort(SymbolList.begin(), SymbolList.end(), CompareSymbolSize);
196 else
197 std::sort(SymbolList.begin(), SymbolList.end(), CompareSymbolName);
198 }
199
200 if (OutputFormat == posix && MultipleFiles) {
201 outs() << '\n' << CurrentFilename << ":\n";
202 } else if (OutputFormat == bsd && MultipleFiles) {
203 outs() << "\n" << CurrentFilename << ":\n";
204 } else if (OutputFormat == sysv) {
205 outs() << "\n\nSymbols from " << CurrentFilename << ":\n\n"
206 << "Name Value Class Type"
207 << " Size Line Section\n";
208 }
209
210 for (SymbolListT::iterator i = SymbolList.begin(),
211 e = SymbolList.end(); i != e; ++i) {
212 if ((i->TypeChar != 'U') && UndefinedOnly)
213 continue;
214 if ((i->TypeChar == 'U') && DefinedOnly)
215 continue;
216 if (SizeSort && !PrintAddress && i->Size == UnknownAddressOrSize)
217 continue;
218
219 char SymbolAddrStr[10] = "";
220 char SymbolSizeStr[10] = "";
221
222 if (OutputFormat == sysv || i->Address == object::UnknownAddressOrSize)
223 strcpy(SymbolAddrStr, " ");
224 if (OutputFormat == sysv)
225 strcpy(SymbolSizeStr, " ");
226
227 if (i->Address != object::UnknownAddressOrSize)
Benjamin Kramer51cf8662012-03-10 02:04:38 +0000228 format("%08" PRIx64, i->Address).print(SymbolAddrStr,
229 sizeof(SymbolAddrStr));
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000230 if (i->Size != object::UnknownAddressOrSize)
Benjamin Kramer51cf8662012-03-10 02:04:38 +0000231 format("%08" PRIx64, i->Size).print(SymbolSizeStr, sizeof(SymbolSizeStr));
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000232
233 if (OutputFormat == posix) {
234 outs() << i->Name << " " << i->TypeChar << " "
235 << SymbolAddrStr << SymbolSizeStr << "\n";
236 } else if (OutputFormat == bsd) {
237 if (PrintAddress)
238 outs() << SymbolAddrStr << ' ';
239 if (PrintSize) {
240 outs() << SymbolSizeStr;
241 if (i->Size != object::UnknownAddressOrSize)
242 outs() << ' ';
243 }
244 outs() << i->TypeChar << " " << i->Name << "\n";
245 } else if (OutputFormat == sysv) {
246 std::string PaddedName (i->Name);
247 while (PaddedName.length () < 20)
248 PaddedName += " ";
249 outs() << PaddedName << "|" << SymbolAddrStr << "| "
250 << i->TypeChar
251 << " | |" << SymbolSizeStr << "| |\n";
252 }
253 }
254
255 SymbolList.clear();
256}
257
Chris Lattnerc30598b2006-12-06 01:18:01 +0000258static char TypeCharForSymbol(GlobalValue &GV) {
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +0000259 if (GV.isDeclaration()) return 'U';
Chris Lattnerc30598b2006-12-06 01:18:01 +0000260 if (GV.hasLinkOnceLinkage()) return 'C';
Dale Johannesen7d5633e2008-05-16 22:44:18 +0000261 if (GV.hasCommonLinkage()) return 'C';
Chris Lattnerc30598b2006-12-06 01:18:01 +0000262 if (GV.hasWeakLinkage()) return 'W';
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +0000263 if (isa<Function>(GV) && GV.hasInternalLinkage()) return 't';
Chris Lattnerc30598b2006-12-06 01:18:01 +0000264 if (isa<Function>(GV)) return 'T';
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +0000265 if (isa<GlobalVariable>(GV) && GV.hasInternalLinkage()) return 'd';
Chris Lattnerc30598b2006-12-06 01:18:01 +0000266 if (isa<GlobalVariable>(GV)) return 'D';
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +0000267 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(&GV)) {
268 const GlobalValue *AliasedGV = GA->getAliasedGlobal();
269 if (isa<Function>(AliasedGV)) return 'T';
270 if (isa<GlobalVariable>(AliasedGV)) return 'D';
271 }
272 return '?';
Brian Gaeke972d3d72003-10-16 04:43:15 +0000273}
274
Chris Lattnerc30598b2006-12-06 01:18:01 +0000275static void DumpSymbolNameForGlobalValue(GlobalValue &GV) {
Chris Lattner266c7bb2009-04-13 05:44:34 +0000276 // Private linkage and available_externally linkage don't exist in symtab.
Bill Wendling4e34d502010-08-24 20:00:52 +0000277 if (GV.hasPrivateLinkage() ||
278 GV.hasLinkerPrivateLinkage() ||
279 GV.hasLinkerPrivateWeakLinkage() ||
Bill Wendling4e34d502010-08-24 20:00:52 +0000280 GV.hasAvailableExternallyLinkage())
Bill Wendling5e721d72010-07-01 21:55:59 +0000281 return;
Chris Lattner266c7bb2009-04-13 05:44:34 +0000282 char TypeChar = TypeCharForSymbol(GV);
Rafael Espindolabb46f522009-01-15 20:18:42 +0000283 if (GV.hasLocalLinkage () && ExternalOnly)
Brian Gaeke972d3d72003-10-16 04:43:15 +0000284 return;
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000285
286 NMSymbol s;
287 s.Address = object::UnknownAddressOrSize;
288 s.Size = object::UnknownAddressOrSize;
289 s.TypeChar = TypeChar;
290 s.Name = GV.getName();
291 SymbolList.push_back(s);
Brian Gaeke972d3d72003-10-16 04:43:15 +0000292}
293
Chris Lattnerc30598b2006-12-06 01:18:01 +0000294static void DumpSymbolNamesFromModule(Module *M) {
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000295 CurrentFilename = M->getModuleIdentifier();
Chris Lattner266c7bb2009-04-13 05:44:34 +0000296 std::for_each (M->begin(), M->end(), DumpSymbolNameForGlobalValue);
297 std::for_each (M->global_begin(), M->global_end(),
Lauro Ramos Venancio4a828ee2007-06-27 22:08:09 +0000298 DumpSymbolNameForGlobalValue);
Jan Sjödin18505b32012-09-18 18:47:58 +0000299 if (!WithoutAliases)
300 std::for_each (M->alias_begin(), M->alias_end(),
301 DumpSymbolNameForGlobalValue);
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000302
303 SortAndPrintSymbolList();
304}
305
306static void DumpSymbolNamesFromObject(ObjectFile *obj) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000307 error_code ec;
Michael J. Spencerdfa18962012-02-28 00:40:37 +0000308 symbol_iterator ibegin = obj->begin_symbols();
309 symbol_iterator iend = obj->end_symbols();
310 if (DynamicSyms) {
311 ibegin = obj->begin_dynamic_symbols();
312 iend = obj->end_dynamic_symbols();
313 }
314 for (symbol_iterator i = ibegin; i != iend; i.increment(ec)) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000315 if (error(ec)) break;
David Meyerc46255a2012-02-28 23:47:53 +0000316 uint32_t symflags;
317 if (error(i->getFlags(symflags))) break;
318 if (!DebugSyms && (symflags & SymbolRef::SF_FormatSpecific))
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000319 continue;
320 NMSymbol s;
321 s.Size = object::UnknownAddressOrSize;
322 s.Address = object::UnknownAddressOrSize;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000323 if (PrintSize || SizeSort) {
324 if (error(i->getSize(s.Size))) break;
325 }
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000326 if (PrintAddress)
Danil Malyshevb0436a72011-11-29 17:40:10 +0000327 if (error(i->getAddress(s.Address))) break;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000328 if (error(i->getNMTypeChar(s.TypeChar))) break;
329 if (error(i->getName(s.Name))) break;
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000330 SymbolList.push_back(s);
331 }
332
Michael J. Spencer001c9202011-06-25 17:54:50 +0000333 CurrentFilename = obj->getFileName();
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000334 SortAndPrintSymbolList();
Brian Gaeke972d3d72003-10-16 04:43:15 +0000335}
336
Chris Lattnerc30598b2006-12-06 01:18:01 +0000337static void DumpSymbolNamesFromFile(std::string &Filename) {
Michael J. Spencera740e192011-12-13 23:17:29 +0000338 if (Filename != "-" && !sys::fs::exists(Filename)) {
339 errs() << ToolName << ": '" << Filename << "': " << "No such file\n";
340 return;
341 }
342
343 OwningPtr<MemoryBuffer> Buffer;
344 if (error(MemoryBuffer::getFileOrSTDIN(Filename, Buffer), Filename))
345 return;
346
347 sys::fs::file_magic magic = sys::fs::identify_magic(Buffer->getBuffer());
348
Owen Anderson0d7c6952009-07-15 22:16:10 +0000349 LLVMContext &Context = getGlobalContext();
Brian Gaeke972d3d72003-10-16 04:43:15 +0000350 std::string ErrorMessage;
Michael J. Spencera740e192011-12-13 23:17:29 +0000351 if (magic == sys::fs::file_magic::bitcode) {
Chris Lattner4d5aad22007-05-06 05:36:18 +0000352 Module *Result = 0;
Michael J. Spencera740e192011-12-13 23:17:29 +0000353 Result = ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage);
Nuno Lopes8018f5d2009-09-10 14:56:31 +0000354 if (Result) {
Chris Lattner4d5aad22007-05-06 05:36:18 +0000355 DumpSymbolNamesFromModule(Result);
Nuno Lopes8018f5d2009-09-10 14:56:31 +0000356 delete Result;
Michael J. Spencera740e192011-12-13 23:17:29 +0000357 } else {
358 error(ErrorMessage, Filename);
Brian Gaeke08d03c72003-11-19 21:52:09 +0000359 return;
360 }
Michael J. Spencera740e192011-12-13 23:17:29 +0000361 } else if (magic == sys::fs::file_magic::archive) {
362 OwningPtr<Binary> arch;
363 if (error(object::createBinary(Buffer.take(), arch), Filename))
364 return;
365
Michael J. Spencer9142ae22011-09-27 19:37:18 +0000366 if (object::Archive *a = dyn_cast<object::Archive>(arch.get())) {
Shankar Easwaran206252c2012-11-13 18:38:42 +0000367 if (ArchiveMap) {
368 outs() << "Archive map" << "\n";
369 for (object::Archive::symbol_iterator i = a->begin_symbols(),
370 e = a->end_symbols(); i != e; ++i) {
371 object::Archive::child_iterator c;
372 StringRef symname;
373 StringRef filename;
374 if (error(i->getMember(c)))
375 return;
376 if (error(i->getName(symname)))
377 return;
378 if (error(c->getName(filename)))
379 return;
380 outs() << symname << " in " << filename << "\n";
381 }
382 outs() << "\n";
383 }
384
Michael J. Spencer9142ae22011-09-27 19:37:18 +0000385 for (object::Archive::child_iterator i = a->begin_children(),
386 e = a->end_children(); i != e; ++i) {
387 OwningPtr<Binary> child;
Rafael Espindolaaba65b02011-12-25 01:20:19 +0000388 if (i->getAsBinary(child)) {
Michael J. Spencer9142ae22011-09-27 19:37:18 +0000389 // Try opening it as a bitcode file.
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000390 OwningPtr<MemoryBuffer> buff;
391 if (error(i->getMemoryBuffer(buff)))
392 return;
Michael J. Spencer9142ae22011-09-27 19:37:18 +0000393 Module *Result = 0;
394 if (buff)
Benjamin Kramer1a9908d2011-10-10 13:10:04 +0000395 Result = ParseBitcodeFile(buff.get(), Context, &ErrorMessage);
Michael J. Spencer9142ae22011-09-27 19:37:18 +0000396
397 if (Result) {
398 DumpSymbolNamesFromModule(Result);
399 delete Result;
400 }
401 continue;
402 }
403 if (object::ObjectFile *o = dyn_cast<ObjectFile>(child.get())) {
404 outs() << o->getFileName() << ":\n";
405 DumpSymbolNamesFromObject(o);
406 }
407 }
408 }
Alexey Samsonov9c22f872013-06-18 15:03:28 +0000409 } else if (magic == sys::fs::file_magic::macho_universal_binary) {
410 OwningPtr<Binary> Bin;
411 if (error(object::createBinary(Buffer.take(), Bin), Filename))
412 return;
413
414 object::MachOUniversalBinary *UB =
415 cast<object::MachOUniversalBinary>(Bin.get());
416 for (object::MachOUniversalBinary::object_iterator
417 I = UB->begin_objects(),
418 E = UB->end_objects();
419 I != E; ++I) {
420 OwningPtr<ObjectFile> Obj;
421 if (!I->getAsObjectFile(Obj)) {
422 outs() << Obj->getFileName() << ":\n";
423 DumpSymbolNamesFromObject(Obj.get());
424 }
425 }
Michael J. Spencera740e192011-12-13 23:17:29 +0000426 } else if (magic.is_object()) {
Michael J. Spencer76fb9b02011-06-25 17:54:59 +0000427 OwningPtr<Binary> obj;
Michael J. Spencera740e192011-12-13 23:17:29 +0000428 if (error(object::createBinary(Buffer.take(), obj), Filename))
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000429 return;
Michael J. Spencer76fb9b02011-06-25 17:54:59 +0000430 if (object::ObjectFile *o = dyn_cast<ObjectFile>(obj.get()))
431 DumpSymbolNamesFromObject(o);
Brian Gaeke82042872003-11-19 21:57:30 +0000432 } else {
Dan Gohman65f57c22009-07-15 16:35:29 +0000433 errs() << ToolName << ": " << Filename << ": "
434 << "unrecognizable file type\n";
Rafael Espindola40d40dd2013-07-03 15:46:03 +0000435 HadError = true;
Brian Gaeke82042872003-11-19 21:57:30 +0000436 return;
Brian Gaeke972d3d72003-10-16 04:43:15 +0000437 }
438}
439
440int main(int argc, char **argv) {
Chris Lattnercc14d252009-03-06 05:34:10 +0000441 // Print a stack trace if we signal out.
Chris Lattner4d5aad22007-05-06 05:36:18 +0000442 sys::PrintStackTraceOnErrorSignal();
Chris Lattnercc14d252009-03-06 05:34:10 +0000443 PrettyStackTraceProgram X(argc, argv);
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000444
Chris Lattnercc14d252009-03-06 05:34:10 +0000445 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
446 cl::ParseCommandLineOptions(argc, argv, "llvm symbol table dumper\n");
Chris Lattnerf73b4ca2004-02-19 20:32:12 +0000447
Michael J. Spencera740e192011-12-13 23:17:29 +0000448 // llvm-nm only reads binary files.
Rafael Espindola9f1d9fd2013-06-12 20:58:35 +0000449 if (error(sys::ChangeStdinToBinary()))
Michael J. Spencera740e192011-12-13 23:17:29 +0000450 return 1;
451
Chris Lattner4d5aad22007-05-06 05:36:18 +0000452 ToolName = argv[0];
453 if (BSDFormat) OutputFormat = bsd;
454 if (POSIXFormat) OutputFormat = posix;
Chris Lattnerfc046d52003-10-16 18:45:23 +0000455
Michael J. Spencer20d335a2011-01-20 06:38:57 +0000456 // The relative order of these is important. If you pass --size-sort it should
457 // only print out the size. However, if you pass -S --size-sort, it should
458 // print out both the size and address.
459 if (SizeSort && !PrintSize) PrintAddress = false;
460 if (OutputFormat == sysv || SizeSort) PrintSize = true;
461
Chris Lattner4d5aad22007-05-06 05:36:18 +0000462 switch (InputFilenames.size()) {
463 case 0: InputFilenames.push_back("-");
464 case 1: break;
465 default: MultipleFiles = true;
Chris Lattnerfc046d52003-10-16 18:45:23 +0000466 }
Chris Lattner4d5aad22007-05-06 05:36:18 +0000467
468 std::for_each(InputFilenames.begin(), InputFilenames.end(),
469 DumpSymbolNamesFromFile);
Rafael Espindola40d40dd2013-07-03 15:46:03 +0000470
471 if (HadError)
472 return 1;
473
Chris Lattner4d5aad22007-05-06 05:36:18 +0000474 return 0;
Brian Gaeke972d3d72003-10-16 04:43:15 +0000475}