blob: 38c945cb94f2eb8e612254aca990a948bf35b8b2 [file] [log] [blame]
Brian Gaeke0af759d2003-10-16 04:43:15 +00001//===-- llvm-nm.cpp - Symbol table dumping utility for llvm ---------------===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
John Criswell09344dc2003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner345353d2007-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 Brukman650ba8e2005-04-22 00:00:37 +00007//
John Criswell09344dc2003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Brian Gaeke0af759d2003-10-16 04:43:15 +00009//
Michael J. Spencer838e5ad2012-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 Brukman650ba8e2005-04-22 00:00:37 +000013//
Michael J. Spencer838e5ad2012-06-06 23:34:10 +000014// This "nm" supports many of the features of GNU "nm", including its different
15// output formats.
Brian Gaeke0af759d2003-10-16 04:43:15 +000016//
17//===----------------------------------------------------------------------===//
18
Rafael Espindolaf12b8282014-02-21 20:10:59 +000019#include "llvm/IR/Function.h"
20#include "llvm/IR/GlobalAlias.h"
21#include "llvm/IR/GlobalVariable.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/LLVMContext.h"
Michael J. Spencer2bc774a2011-09-27 19:37:18 +000023#include "llvm/Object/Archive.h"
Rafael Espindola586af97a2013-11-02 21:16:09 +000024#include "llvm/Object/COFF.h"
25#include "llvm/Object/ELFObjectFile.h"
Chandler Carruth442f7842014-03-04 10:07:28 +000026#include "llvm/Object/IRObjectFile.h"
Rafael Espindola586af97a2013-11-02 21:16:09 +000027#include "llvm/Object/MachO.h"
Alexey Samsonove6388e62013-06-18 15:03:28 +000028#include "llvm/Object/MachOUniversal.h"
Michael J. Spencerb8672a52011-01-20 06:38:57 +000029#include "llvm/Object/ObjectFile.h"
Rui Ueyamaf078eff2014-03-18 23:37:53 +000030#include "llvm/Support/COFF.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000031#include "llvm/Support/CommandLine.h"
Michael J. Spencerb8672a52011-01-20 06:38:57 +000032#include "llvm/Support/FileSystem.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000033#include "llvm/Support/Format.h"
Chris Lattner76d46322006-12-06 01:18:01 +000034#include "llvm/Support/ManagedStatic.h"
Chris Lattneref8f3892007-05-06 05:36:18 +000035#include "llvm/Support/MemoryBuffer.h"
Chris Lattnere3fc2d12009-03-06 05:34:10 +000036#include "llvm/Support/PrettyStackTrace.h"
Michael J. Spencerbc96f372011-12-13 23:17:29 +000037#include "llvm/Support/Program.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000038#include "llvm/Support/Signals.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000039#include "llvm/Support/raw_ostream.h"
Rafael Espindola13b69d62014-07-03 18:59:23 +000040#include "llvm/Support/TargetSelect.h"
Jeff Cohenb622c112007-03-05 00:00:42 +000041#include <algorithm>
Brian Gaeke0af759d2003-10-16 04:43:15 +000042#include <cctype>
Alkis Evlogimenosf68f40e2004-04-21 16:11:40 +000043#include <cerrno>
Brian Gaeke55447b42003-11-19 21:52:09 +000044#include <cstring>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000045#include <system_error>
Michael J. Spencerb8672a52011-01-20 06:38:57 +000046#include <vector>
Brian Gaeke960707c2003-11-11 22:41:34 +000047using namespace llvm;
Michael J. Spencerb8672a52011-01-20 06:38:57 +000048using namespace object;
Brian Gaeke960707c2003-11-11 22:41:34 +000049
Brian Gaeke0af759d2003-10-16 04:43:15 +000050namespace {
Kevin Enderby980b2582014-06-05 21:21:57 +000051enum OutputFormatTy { bsd, sysv, posix, darwin };
Rafael Espindola619581c2014-01-29 04:56:19 +000052cl::opt<OutputFormatTy> OutputFormat(
53 "format", cl::desc("Specify output format"),
54 cl::values(clEnumVal(bsd, "BSD format"), clEnumVal(sysv, "System V format"),
Kevin Enderby980b2582014-06-05 21:21:57 +000055 clEnumVal(posix, "POSIX.2 format"),
56 clEnumVal(darwin, "Darwin -m format"), clEnumValEnd),
Rafael Espindola619581c2014-01-29 04:56:19 +000057 cl::init(bsd));
58cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
59 cl::aliasopt(OutputFormat));
Brian Gaeke0af759d2003-10-16 04:43:15 +000060
Rafael Espindolaf12b8282014-02-21 20:10:59 +000061cl::list<std::string> InputFilenames(cl::Positional, cl::desc("<input files>"),
Rafael Espindola619581c2014-01-29 04:56:19 +000062 cl::ZeroOrMore);
Brian Gaeke0af759d2003-10-16 04:43:15 +000063
Rafael Espindola619581c2014-01-29 04:56:19 +000064cl::opt<bool> UndefinedOnly("undefined-only",
65 cl::desc("Show only undefined symbols"));
66cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
67 cl::aliasopt(UndefinedOnly));
Brian Gaeke0af759d2003-10-16 04:43:15 +000068
Rafael Espindola619581c2014-01-29 04:56:19 +000069cl::opt<bool> DynamicSyms("dynamic",
70 cl::desc("Display the dynamic symbols instead "
71 "of normal symbols."));
72cl::alias DynamicSyms2("D", cl::desc("Alias for --dynamic"),
73 cl::aliasopt(DynamicSyms));
Michael J. Spencer8c4729f2012-02-28 00:40:37 +000074
Rafael Espindola619581c2014-01-29 04:56:19 +000075cl::opt<bool> DefinedOnly("defined-only",
76 cl::desc("Show only defined symbols"));
Kevin Enderbyacaaf902014-07-03 18:18:50 +000077cl::alias DefinedOnly2("U", cl::desc("Alias for --defined-only"),
78 cl::aliasopt(DefinedOnly));
Brian Gaeke0af759d2003-10-16 04:43:15 +000079
Rafael Espindola619581c2014-01-29 04:56:19 +000080cl::opt<bool> ExternalOnly("extern-only",
81 cl::desc("Show only external symbols"));
82cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
83 cl::aliasopt(ExternalOnly));
Brian Gaeke0af759d2003-10-16 04:43:15 +000084
Rafael Espindola619581c2014-01-29 04:56:19 +000085cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
86cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
Kevin Enderby14a96ac2014-06-20 00:04:16 +000087cl::opt<bool> DarwinFormat("m", cl::desc("Alias for --format=darwin"));
Brian Gaeke0af759d2003-10-16 04:43:15 +000088
Kevin Enderby8f6dcf52014-07-01 22:44:51 +000089static cl::list<std::string>
Kevin Enderbybe84b292014-07-17 22:56:27 +000090 ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
91 cl::ZeroOrMore);
Kevin Enderby4c8dfe42014-06-30 18:45:23 +000092bool ArchAll = false;
93
Rafael Espindola619581c2014-01-29 04:56:19 +000094cl::opt<bool> PrintFileName(
95 "print-file-name",
Michael J. Spencerb8672a52011-01-20 06:38:57 +000096 cl::desc("Precede each symbol with the object file it came from"));
97
Rafael Espindola619581c2014-01-29 04:56:19 +000098cl::alias PrintFileNameA("A", cl::desc("Alias for --print-file-name"),
99 cl::aliasopt(PrintFileName));
100cl::alias PrintFileNameo("o", cl::desc("Alias for --print-file-name"),
101 cl::aliasopt(PrintFileName));
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000102
Rafael Espindola619581c2014-01-29 04:56:19 +0000103cl::opt<bool> DebugSyms("debug-syms",
104 cl::desc("Show all symbols, even debugger only"));
105cl::alias DebugSymsa("a", cl::desc("Alias for --debug-syms"),
106 cl::aliasopt(DebugSyms));
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000107
Rafael Espindolab4865d62014-04-03 00:19:35 +0000108cl::opt<bool> NumericSort("numeric-sort", cl::desc("Sort symbols by address"));
Rafael Espindola619581c2014-01-29 04:56:19 +0000109cl::alias NumericSortn("n", cl::desc("Alias for --numeric-sort"),
110 cl::aliasopt(NumericSort));
111cl::alias NumericSortv("v", cl::desc("Alias for --numeric-sort"),
112 cl::aliasopt(NumericSort));
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000113
Rafael Espindola619581c2014-01-29 04:56:19 +0000114cl::opt<bool> NoSort("no-sort", cl::desc("Show symbols in order encountered"));
115cl::alias NoSortp("p", cl::desc("Alias for --no-sort"), cl::aliasopt(NoSort));
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000116
Kevin Enderby25a614b2014-07-02 23:23:58 +0000117cl::opt<bool> ReverseSort("reverse-sort", cl::desc("Sort in reverse order"));
118cl::alias ReverseSortr("r", cl::desc("Alias for --reverse-sort"),
119 cl::aliasopt(ReverseSort));
120
Rafael Espindola619581c2014-01-29 04:56:19 +0000121cl::opt<bool> PrintSize("print-size",
Rafael Espindolab4865d62014-04-03 00:19:35 +0000122 cl::desc("Show symbol size instead of address"));
Rafael Espindola619581c2014-01-29 04:56:19 +0000123cl::alias PrintSizeS("S", cl::desc("Alias for --print-size"),
124 cl::aliasopt(PrintSize));
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000125
Rafael Espindola619581c2014-01-29 04:56:19 +0000126cl::opt<bool> SizeSort("size-sort", cl::desc("Sort symbols by size"));
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000127
Rafael Espindola619581c2014-01-29 04:56:19 +0000128cl::opt<bool> WithoutAliases("without-aliases", cl::Hidden,
129 cl::desc("Exclude aliases from output"));
Jan Sjödin4d0c2992012-09-18 18:47:58 +0000130
Rafael Espindola619581c2014-01-29 04:56:19 +0000131cl::opt<bool> ArchiveMap("print-armap", cl::desc("Print the archive map"));
Kevin Enderby8da4bd62014-07-08 23:47:31 +0000132cl::alias ArchiveMaps("M", cl::desc("Alias for --print-armap"),
Rafael Espindola619581c2014-01-29 04:56:19 +0000133 cl::aliasopt(ArchiveMap));
Kevin Enderby0fd8aac2014-07-03 21:51:07 +0000134
135cl::opt<bool> JustSymbolName("just-symbol-name",
136 cl::desc("Print just the symbol's name"));
137cl::alias JustSymbolNames("j", cl::desc("Alias for --just-symbol-name"),
138 cl::aliasopt(JustSymbolName));
Kevin Enderbyfe6ad972014-07-11 20:30:00 +0000139
140// FIXME: This option takes exactly two strings and should be allowed anywhere
141// on the command line. Such that "llvm-nm -s __TEXT __text foo.o" would work.
142// But that does not as the CommandLine Library does not have a way to make
143// this work. For now the "-s __TEXT __text" has to be last on the command
144// line.
145cl::list<std::string> SegSect("s", cl::Positional, cl::ZeroOrMore,
146 cl::desc("Dump only symbols from this segment "
147 "and section name, Mach-O only"));
148
Kevin Enderby77b968e2014-07-16 17:38:26 +0000149cl::opt<bool> FormatMachOasHex("x", cl::desc("Print symbol entry in hex, "
150 "Mach-O only"));
151
Rafael Espindolab4865d62014-04-03 00:19:35 +0000152bool PrintAddress = true;
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000153
Rafael Espindola619581c2014-01-29 04:56:19 +0000154bool MultipleFiles = false;
Brian Gaeke0af759d2003-10-16 04:43:15 +0000155
Rafael Espindola619581c2014-01-29 04:56:19 +0000156bool HadError = false;
Rafael Espindola8b82a4d2013-07-03 15:46:03 +0000157
Rafael Espindola619581c2014-01-29 04:56:19 +0000158std::string ToolName;
Chris Lattneraa2372562006-05-24 17:04:05 +0000159}
Brian Gaeke0af759d2003-10-16 04:43:15 +0000160
Rafael Espindola619581c2014-01-29 04:56:19 +0000161static void error(Twine Message, Twine Path = Twine()) {
162 HadError = true;
163 errs() << ToolName << ": " << Path << ": " << Message << ".\n";
Michael J. Spencerbc96f372011-12-13 23:17:29 +0000164}
165
Rafael Espindola4453e42942014-06-13 03:07:50 +0000166static bool error(std::error_code EC, Twine Path = Twine()) {
Rafael Espindola619581c2014-01-29 04:56:19 +0000167 if (EC) {
168 error(EC.message(), Path);
Michael J. Spencerbc96f372011-12-13 23:17:29 +0000169 return true;
170 }
171 return false;
172}
173
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000174namespace {
Rafael Espindola619581c2014-01-29 04:56:19 +0000175struct NMSymbol {
Rafael Espindolab4865d62014-04-03 00:19:35 +0000176 uint64_t Address;
Rafael Espindola619581c2014-01-29 04:56:19 +0000177 uint64_t Size;
178 char TypeChar;
179 StringRef Name;
Kevin Enderby980b2582014-06-05 21:21:57 +0000180 DataRefImpl Symb;
Rafael Espindola619581c2014-01-29 04:56:19 +0000181};
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000182}
183
Rafael Espindolab4865d62014-04-03 00:19:35 +0000184static bool compareSymbolAddress(const NMSymbol &A, const NMSymbol &B) {
Kevin Enderby25a614b2014-07-02 23:23:58 +0000185 if (!ReverseSort) {
186 if (A.Address < B.Address)
187 return true;
188 else if (A.Address == B.Address && A.Name < B.Name)
189 return true;
190 else if (A.Address == B.Address && A.Name == B.Name && A.Size < B.Size)
191 return true;
192 else
193 return false;
194 } else {
195 if (A.Address > B.Address)
196 return true;
197 else if (A.Address == B.Address && A.Name > B.Name)
198 return true;
199 else if (A.Address == B.Address && A.Name == B.Name && A.Size > B.Size)
200 return true;
201 else
202 return false;
203 }
Rafael Espindola619581c2014-01-29 04:56:19 +0000204}
205
206static bool compareSymbolSize(const NMSymbol &A, const NMSymbol &B) {
Kevin Enderby25a614b2014-07-02 23:23:58 +0000207 if (!ReverseSort) {
208 if (A.Size < B.Size)
209 return true;
210 else if (A.Size == B.Size && A.Name < B.Name)
211 return true;
212 else if (A.Size == B.Size && A.Name == B.Name && A.Address < B.Address)
213 return true;
214 else
215 return false;
216 } else {
217 if (A.Size > B.Size)
218 return true;
219 else if (A.Size == B.Size && A.Name > B.Name)
220 return true;
221 else if (A.Size == B.Size && A.Name == B.Name && A.Address > B.Address)
222 return true;
223 else
224 return false;
225 }
Rafael Espindola619581c2014-01-29 04:56:19 +0000226}
227
228static bool compareSymbolName(const NMSymbol &A, const NMSymbol &B) {
Kevin Enderby25a614b2014-07-02 23:23:58 +0000229 if (!ReverseSort) {
230 if (A.Name < B.Name)
231 return true;
232 else if (A.Name == B.Name && A.Size < B.Size)
233 return true;
234 else if (A.Name == B.Name && A.Size == B.Size && A.Address < B.Address)
235 return true;
236 else
237 return false;
238 } else {
239 if (A.Name > B.Name)
240 return true;
241 else if (A.Name == B.Name && A.Size > B.Size)
242 return true;
243 else if (A.Name == B.Name && A.Size == B.Size && A.Address > B.Address)
244 return true;
245 else
246 return false;
247 }
Rafael Espindola619581c2014-01-29 04:56:19 +0000248}
249
Kevin Enderby6abc2e52014-05-09 23:57:49 +0000250static char isSymbolList64Bit(SymbolicFile *Obj) {
Kevin Enderby15e24712014-05-12 20:45:00 +0000251 if (isa<IRObjectFile>(Obj))
Kevin Enderby6abc2e52014-05-09 23:57:49 +0000252 return false;
Kevin Enderby15e24712014-05-12 20:45:00 +0000253 else if (isa<COFFObjectFile>(Obj))
Kevin Enderby6abc2e52014-05-09 23:57:49 +0000254 return false;
255 else if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(Obj))
256 return MachO->is64Bit();
Kevin Enderby15e24712014-05-12 20:45:00 +0000257 else if (isa<ELF32LEObjectFile>(Obj))
Kevin Enderby6abc2e52014-05-09 23:57:49 +0000258 return false;
Kevin Enderby15e24712014-05-12 20:45:00 +0000259 else if (isa<ELF64LEObjectFile>(Obj))
Kevin Enderby6abc2e52014-05-09 23:57:49 +0000260 return true;
Kevin Enderby15e24712014-05-12 20:45:00 +0000261 else if (isa<ELF32BEObjectFile>(Obj))
Kevin Enderby6abc2e52014-05-09 23:57:49 +0000262 return false;
Kevin Enderby8f6dcf52014-07-01 22:44:51 +0000263 else if (isa<ELF64BEObjectFile>(Obj))
Kevin Enderby6abc2e52014-05-09 23:57:49 +0000264 return true;
265 else
266 return false;
267}
268
Rafael Espindola619581c2014-01-29 04:56:19 +0000269static StringRef CurrentFilename;
270typedef std::vector<NMSymbol> SymbolListT;
271static SymbolListT SymbolList;
272
Kevin Enderby980b2582014-06-05 21:21:57 +0000273// darwinPrintSymbol() is used to print a symbol from a Mach-O file when the
Kevin Enderby77b968e2014-07-16 17:38:26 +0000274// the OutputFormat is darwin or we are printing Mach-O symbols in hex. For
275// the darwin format it produces the same output as darwin's nm(1) -m output
276// and when printing Mach-O symbols in hex it produces the same output as
277// darwin's nm(1) -x format.
Kevin Enderby980b2582014-06-05 21:21:57 +0000278static void darwinPrintSymbol(MachOObjectFile *MachO, SymbolListT::iterator I,
279 char *SymbolAddrStr, const char *printBlanks) {
280 MachO::mach_header H;
281 MachO::mach_header_64 H_64;
282 uint32_t Filetype, Flags;
283 MachO::nlist_64 STE_64;
284 MachO::nlist STE;
285 uint8_t NType;
Kevin Enderby77b968e2014-07-16 17:38:26 +0000286 uint8_t NSect;
Kevin Enderby980b2582014-06-05 21:21:57 +0000287 uint16_t NDesc;
Kevin Enderby77b968e2014-07-16 17:38:26 +0000288 uint32_t NStrx;
Kevin Enderby980b2582014-06-05 21:21:57 +0000289 uint64_t NValue;
290 if (MachO->is64Bit()) {
291 H_64 = MachO->MachOObjectFile::getHeader64();
292 Filetype = H_64.filetype;
293 Flags = H_64.flags;
294 STE_64 = MachO->getSymbol64TableEntry(I->Symb);
295 NType = STE_64.n_type;
Kevin Enderby77b968e2014-07-16 17:38:26 +0000296 NSect = STE_64.n_sect;
Kevin Enderby980b2582014-06-05 21:21:57 +0000297 NDesc = STE_64.n_desc;
Kevin Enderby77b968e2014-07-16 17:38:26 +0000298 NStrx = STE_64.n_strx;
Kevin Enderby980b2582014-06-05 21:21:57 +0000299 NValue = STE_64.n_value;
300 } else {
301 H = MachO->MachOObjectFile::getHeader();
302 Filetype = H.filetype;
303 Flags = H.flags;
304 STE = MachO->getSymbolTableEntry(I->Symb);
305 NType = STE.n_type;
Kevin Enderby77b968e2014-07-16 17:38:26 +0000306 NSect = STE.n_sect;
Kevin Enderby980b2582014-06-05 21:21:57 +0000307 NDesc = STE.n_desc;
Kevin Enderby77b968e2014-07-16 17:38:26 +0000308 NStrx = STE.n_strx;
Kevin Enderby980b2582014-06-05 21:21:57 +0000309 NValue = STE.n_value;
310 }
311
Kevin Enderby77b968e2014-07-16 17:38:26 +0000312 // If we are printing Mach-O symbols in hex do that and return.
313 if (FormatMachOasHex) {
314 char Str[18] = "";
315 const char *printFormat;
316 if (MachO->is64Bit())
317 printFormat = "%016" PRIx64;
318 else
319 printFormat = "%08" PRIx64;
320 format(printFormat, NValue).print(Str, sizeof(Str));
321 outs() << Str << ' ';
322 format("%02x", NType).print(Str, sizeof(Str));
323 outs() << Str << ' ';
324 format("%02x", NSect).print(Str, sizeof(Str));
325 outs() << Str << ' ';
326 format("%04x", NDesc).print(Str, sizeof(Str));
327 outs() << Str << ' ';
328 format("%08x", NStrx).print(Str, sizeof(Str));
329 outs() << Str << ' ';
330 outs() << I->Name << "\n";
331 return;
332 }
333
Kevin Enderby980b2582014-06-05 21:21:57 +0000334 if (PrintAddress) {
335 if ((NType & MachO::N_TYPE) == MachO::N_INDR)
336 strcpy(SymbolAddrStr, printBlanks);
337 outs() << SymbolAddrStr << ' ';
338 }
339
340 switch (NType & MachO::N_TYPE) {
341 case MachO::N_UNDF:
342 if (NValue != 0) {
343 outs() << "(common) ";
344 if (MachO::GET_COMM_ALIGN(NDesc) != 0)
Kevin Enderby8f6dcf52014-07-01 22:44:51 +0000345 outs() << "(alignment 2^" << (int)MachO::GET_COMM_ALIGN(NDesc) << ") ";
Kevin Enderby980b2582014-06-05 21:21:57 +0000346 } else {
347 if ((NType & MachO::N_TYPE) == MachO::N_PBUD)
348 outs() << "(prebound ";
349 else
350 outs() << "(";
351 if ((NDesc & MachO::REFERENCE_TYPE) ==
352 MachO::REFERENCE_FLAG_UNDEFINED_LAZY)
353 outs() << "undefined [lazy bound]) ";
354 else if ((NDesc & MachO::REFERENCE_TYPE) ==
355 MachO::REFERENCE_FLAG_UNDEFINED_LAZY)
356 outs() << "undefined [private lazy bound]) ";
357 else if ((NDesc & MachO::REFERENCE_TYPE) ==
358 MachO::REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY)
359 outs() << "undefined [private]) ";
360 else
361 outs() << "undefined) ";
362 }
363 break;
364 case MachO::N_ABS:
365 outs() << "(absolute) ";
366 break;
367 case MachO::N_INDR:
368 outs() << "(indirect) ";
369 break;
370 case MachO::N_SECT: {
371 section_iterator Sec = MachO->section_end();
372 MachO->getSymbolSection(I->Symb, Sec);
373 DataRefImpl Ref = Sec->getRawDataRefImpl();
374 StringRef SectionName;
375 MachO->getSectionName(Ref, SectionName);
376 StringRef SegmentName = MachO->getSectionFinalSegmentName(Ref);
377 outs() << "(" << SegmentName << "," << SectionName << ") ";
378 break;
379 }
380 default:
381 outs() << "(?) ";
382 break;
383 }
384
385 if (NType & MachO::N_EXT) {
386 if (NDesc & MachO::REFERENCED_DYNAMICALLY)
387 outs() << "[referenced dynamically] ";
388 if (NType & MachO::N_PEXT) {
389 if ((NDesc & MachO::N_WEAK_DEF) == MachO::N_WEAK_DEF)
Kevin Enderby8f6dcf52014-07-01 22:44:51 +0000390 outs() << "weak private external ";
Kevin Enderby980b2582014-06-05 21:21:57 +0000391 else
Kevin Enderby8f6dcf52014-07-01 22:44:51 +0000392 outs() << "private external ";
Kevin Enderby980b2582014-06-05 21:21:57 +0000393 } else {
394 if ((NDesc & MachO::N_WEAK_REF) == MachO::N_WEAK_REF ||
Kevin Enderby8f6dcf52014-07-01 22:44:51 +0000395 (NDesc & MachO::N_WEAK_DEF) == MachO::N_WEAK_DEF) {
Kevin Enderby980b2582014-06-05 21:21:57 +0000396 if ((NDesc & (MachO::N_WEAK_REF | MachO::N_WEAK_DEF)) ==
397 (MachO::N_WEAK_REF | MachO::N_WEAK_DEF))
398 outs() << "weak external automatically hidden ";
399 else
400 outs() << "weak external ";
Kevin Enderby8f6dcf52014-07-01 22:44:51 +0000401 } else
Kevin Enderby980b2582014-06-05 21:21:57 +0000402 outs() << "external ";
403 }
404 } else {
405 if (NType & MachO::N_PEXT)
406 outs() << "non-external (was a private external) ";
407 else
408 outs() << "non-external ";
409 }
410
411 if (Filetype == MachO::MH_OBJECT &&
412 (NDesc & MachO::N_NO_DEAD_STRIP) == MachO::N_NO_DEAD_STRIP)
413 outs() << "[no dead strip] ";
414
415 if (Filetype == MachO::MH_OBJECT &&
416 ((NType & MachO::N_TYPE) != MachO::N_UNDF) &&
417 (NDesc & MachO::N_SYMBOL_RESOLVER) == MachO::N_SYMBOL_RESOLVER)
418 outs() << "[symbol resolver] ";
419
420 if (Filetype == MachO::MH_OBJECT &&
421 ((NType & MachO::N_TYPE) != MachO::N_UNDF) &&
422 (NDesc & MachO::N_ALT_ENTRY) == MachO::N_ALT_ENTRY)
423 outs() << "[alt entry] ";
424
425 if ((NDesc & MachO::N_ARM_THUMB_DEF) == MachO::N_ARM_THUMB_DEF)
426 outs() << "[Thumb] ";
427
428 if ((NType & MachO::N_TYPE) == MachO::N_INDR) {
429 outs() << I->Name << " (for ";
430 StringRef IndirectName;
431 if (MachO->getIndirectName(I->Symb, IndirectName))
432 outs() << "?)";
433 else
434 outs() << IndirectName << ")";
Kevin Enderby8f6dcf52014-07-01 22:44:51 +0000435 } else
Kevin Enderby980b2582014-06-05 21:21:57 +0000436 outs() << I->Name;
437
438 if ((Flags & MachO::MH_TWOLEVEL) == MachO::MH_TWOLEVEL &&
Kevin Enderby8f6dcf52014-07-01 22:44:51 +0000439 (((NType & MachO::N_TYPE) == MachO::N_UNDF && NValue == 0) ||
Kevin Enderby980b2582014-06-05 21:21:57 +0000440 (NType & MachO::N_TYPE) == MachO::N_PBUD)) {
441 uint32_t LibraryOrdinal = MachO::GET_LIBRARY_ORDINAL(NDesc);
442 if (LibraryOrdinal != 0) {
443 if (LibraryOrdinal == MachO::EXECUTABLE_ORDINAL)
444 outs() << " (from executable)";
445 else if (LibraryOrdinal == MachO::DYNAMIC_LOOKUP_ORDINAL)
446 outs() << " (dynamically looked up)";
447 else {
448 StringRef LibraryName;
Kevin Enderby8f6dcf52014-07-01 22:44:51 +0000449 if (MachO->getLibraryShortNameByIndex(LibraryOrdinal - 1, LibraryName))
450 outs() << " (from bad library ordinal " << LibraryOrdinal << ")";
Kevin Enderby980b2582014-06-05 21:21:57 +0000451 else
452 outs() << " (from " << LibraryName << ")";
453 }
454 }
455 }
456
457 outs() << "\n";
458}
459
Kevin Enderby407cc212014-07-17 22:47:16 +0000460// Table that maps Darwin's Mach-O stab constants to strings to allow printing.
461struct DarwinStabName {
462 uint8_t NType;
463 const char *Name;
464};
465static const struct DarwinStabName DarwinStabNames[] = {
466 {MachO::N_GSYM, "GSYM"},
467 {MachO::N_FNAME, "FNAME"},
468 {MachO::N_FUN, "FUN"},
469 {MachO::N_STSYM, "STSYM"},
470 {MachO::N_LCSYM, "LCSYM"},
471 {MachO::N_BNSYM, "BNSYM"},
472 {MachO::N_PC, "PC"},
473 {MachO::N_AST, "AST"},
474 {MachO::N_OPT, "OPT"},
475 {MachO::N_RSYM, "RSYM"},
476 {MachO::N_SLINE, "SLINE"},
477 {MachO::N_ENSYM, "ENSYM"},
478 {MachO::N_SSYM, "SSYM"},
479 {MachO::N_SO, "SO"},
480 {MachO::N_OSO, "OSO"},
481 {MachO::N_LSYM, "LSYM"},
482 {MachO::N_BINCL, "BINCL"},
483 {MachO::N_SOL, "SOL"},
484 {MachO::N_PARAMS, "PARAM"},
485 {MachO::N_VERSION, "VERS"},
486 {MachO::N_OLEVEL, "OLEV"},
487 {MachO::N_PSYM, "PSYM"},
488 {MachO::N_EINCL, "EINCL"},
489 {MachO::N_ENTRY, "ENTRY"},
490 {MachO::N_LBRAC, "LBRAC"},
491 {MachO::N_EXCL, "EXCL"},
492 {MachO::N_RBRAC, "RBRAC"},
493 {MachO::N_BCOMM, "BCOMM"},
494 {MachO::N_ECOMM, "ECOMM"},
495 {MachO::N_ECOML, "ECOML"},
496 {MachO::N_LENG, "LENG"},
497 {0, 0}};
498static const char *getDarwinStabString(uint8_t NType) {
499 for (unsigned i = 0; DarwinStabNames[i].Name; i++) {
500 if (DarwinStabNames[i].NType == NType)
501 return DarwinStabNames[i].Name;
502 }
503 return 0;
504}
505
506// darwinPrintStab() prints the n_sect, n_desc along with a symbolic name of
507// a stab n_type value in a Mach-O file.
508static void darwinPrintStab(MachOObjectFile *MachO, SymbolListT::iterator I) {
509 MachO::nlist_64 STE_64;
510 MachO::nlist STE;
511 uint8_t NType;
512 uint8_t NSect;
513 uint16_t NDesc;
514 if (MachO->is64Bit()) {
515 STE_64 = MachO->getSymbol64TableEntry(I->Symb);
516 NType = STE_64.n_type;
517 NSect = STE_64.n_sect;
518 NDesc = STE_64.n_desc;
519 } else {
520 STE = MachO->getSymbolTableEntry(I->Symb);
521 NType = STE.n_type;
522 NSect = STE.n_sect;
523 NDesc = STE.n_desc;
524 }
525
526 char Str[18] = "";
527 format("%02x", NSect).print(Str, sizeof(Str));
528 outs() << ' ' << Str << ' ';
529 format("%04x", NDesc).print(Str, sizeof(Str));
530 outs() << Str << ' ';
531 if (const char *stabString = getDarwinStabString(NType))
532 format("%5.5s", stabString).print(Str, sizeof(Str));
533 else
534 format(" %02x", NType).print(Str, sizeof(Str));
535 outs() << Str;
536}
537
Kevin Enderby08e1bbd2014-07-24 23:31:52 +0000538static void sortAndPrintSymbolList(SymbolicFile *Obj, bool printName,
539 std::string ArchiveName,
540 std::string ArchitectureName) {
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000541 if (!NoSort) {
542 if (NumericSort)
Rafael Espindolab4865d62014-04-03 00:19:35 +0000543 std::sort(SymbolList.begin(), SymbolList.end(), compareSymbolAddress);
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000544 else if (SizeSort)
Rafael Espindola619581c2014-01-29 04:56:19 +0000545 std::sort(SymbolList.begin(), SymbolList.end(), compareSymbolSize);
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000546 else
Rafael Espindola619581c2014-01-29 04:56:19 +0000547 std::sort(SymbolList.begin(), SymbolList.end(), compareSymbolName);
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000548 }
549
Kevin Enderby08e1bbd2014-07-24 23:31:52 +0000550 if (!PrintFileName) {
551 if (OutputFormat == posix && MultipleFiles && printName) {
552 outs() << '\n' << CurrentFilename << ":\n";
553 } else if (OutputFormat == bsd && MultipleFiles && printName) {
554 outs() << "\n" << CurrentFilename << ":\n";
555 } else if (OutputFormat == sysv) {
556 outs() << "\n\nSymbols from " << CurrentFilename << ":\n\n"
557 << "Name Value Class Type"
558 << " Size Line Section\n";
559 }
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000560 }
561
Kevin Enderby6abc2e52014-05-09 23:57:49 +0000562 const char *printBlanks, *printFormat;
563 if (isSymbolList64Bit(Obj)) {
564 printBlanks = " ";
565 printFormat = "%016" PRIx64;
566 } else {
567 printBlanks = " ";
568 printFormat = "%08" PRIx64;
569 }
570
Rafael Espindola619581c2014-01-29 04:56:19 +0000571 for (SymbolListT::iterator I = SymbolList.begin(), E = SymbolList.end();
572 I != E; ++I) {
573 if ((I->TypeChar != 'U') && UndefinedOnly)
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000574 continue;
Rafael Espindola619581c2014-01-29 04:56:19 +0000575 if ((I->TypeChar == 'U') && DefinedOnly)
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000576 continue;
Rafael Espindolab4865d62014-04-03 00:19:35 +0000577 if (SizeSort && !PrintAddress && I->Size == UnknownAddressOrSize)
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000578 continue;
Kevin Enderby08e1bbd2014-07-24 23:31:52 +0000579 if (PrintFileName) {
580 if (!ArchitectureName.empty())
581 outs() << "(for architecture " << ArchitectureName << "):";
582 if (!ArchiveName.empty())
583 outs() << ArchiveName << ":";
584 outs() << CurrentFilename << ": ";
585 }
Kevin Enderby0fd8aac2014-07-03 21:51:07 +0000586 if (JustSymbolName) {
587 outs() << I->Name << "\n";
588 continue;
589 }
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000590
Kevin Enderby6abc2e52014-05-09 23:57:49 +0000591 char SymbolAddrStr[18] = "";
592 char SymbolSizeStr[18] = "";
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000593
Rafael Espindolab4865d62014-04-03 00:19:35 +0000594 if (OutputFormat == sysv || I->Address == UnknownAddressOrSize)
Kevin Enderby6abc2e52014-05-09 23:57:49 +0000595 strcpy(SymbolAddrStr, printBlanks);
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000596 if (OutputFormat == sysv)
Kevin Enderby6abc2e52014-05-09 23:57:49 +0000597 strcpy(SymbolSizeStr, printBlanks);
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000598
Rafael Espindolab4865d62014-04-03 00:19:35 +0000599 if (I->Address != UnknownAddressOrSize)
Kevin Enderby6abc2e52014-05-09 23:57:49 +0000600 format(printFormat, I->Address)
Rafael Espindolab4865d62014-04-03 00:19:35 +0000601 .print(SymbolAddrStr, sizeof(SymbolAddrStr));
Rafael Espindola0115b082014-01-30 21:51:42 +0000602 if (I->Size != UnknownAddressOrSize)
Kevin Enderby6abc2e52014-05-09 23:57:49 +0000603 format(printFormat, I->Size).print(SymbolSizeStr, sizeof(SymbolSizeStr));
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000604
Kevin Enderby77b968e2014-07-16 17:38:26 +0000605 // If OutputFormat is darwin or we are printing Mach-O symbols in hex and
606 // we have a MachOObjectFile, call darwinPrintSymbol to print as darwin's
607 // nm(1) -m output or hex, else if OutputFormat is darwin or we are
608 // printing Mach-O symbols in hex and not a Mach-O object fall back to
609 // OutputFormat bsd (see below).
Kevin Enderby980b2582014-06-05 21:21:57 +0000610 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(Obj);
Kevin Enderby77b968e2014-07-16 17:38:26 +0000611 if ((OutputFormat == darwin || FormatMachOasHex) && MachO) {
Kevin Enderby980b2582014-06-05 21:21:57 +0000612 darwinPrintSymbol(MachO, I, SymbolAddrStr, printBlanks);
613 } else if (OutputFormat == posix) {
Rafael Espindolab4865d62014-04-03 00:19:35 +0000614 outs() << I->Name << " " << I->TypeChar << " " << SymbolAddrStr
Rafael Espindola619581c2014-01-29 04:56:19 +0000615 << SymbolSizeStr << "\n";
Kevin Enderby980b2582014-06-05 21:21:57 +0000616 } else if (OutputFormat == bsd || (OutputFormat == darwin && !MachO)) {
Rafael Espindolab4865d62014-04-03 00:19:35 +0000617 if (PrintAddress)
618 outs() << SymbolAddrStr << ' ';
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000619 if (PrintSize) {
620 outs() << SymbolSizeStr;
Rafael Espindola0115b082014-01-30 21:51:42 +0000621 if (I->Size != UnknownAddressOrSize)
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000622 outs() << ' ';
623 }
Kevin Enderby407cc212014-07-17 22:47:16 +0000624 outs() << I->TypeChar;
625 if (I->TypeChar == '-' && MachO)
626 darwinPrintStab(MachO, I);
627 outs() << " " << I->Name << "\n";
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000628 } else if (OutputFormat == sysv) {
Rafael Espindola619581c2014-01-29 04:56:19 +0000629 std::string PaddedName(I->Name);
630 while (PaddedName.length() < 20)
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000631 PaddedName += " ";
Rafael Espindolab4865d62014-04-03 00:19:35 +0000632 outs() << PaddedName << "|" << SymbolAddrStr << "| " << I->TypeChar
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000633 << " | |" << SymbolSizeStr << "| |\n";
634 }
635 }
636
637 SymbolList.clear();
638}
639
Rafael Espindola586af97a2013-11-02 21:16:09 +0000640template <class ELFT>
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000641static char getSymbolNMTypeChar(ELFObjectFile<ELFT> &Obj,
642 basic_symbol_iterator I) {
Rafael Espindola586af97a2013-11-02 21:16:09 +0000643 typedef typename ELFObjectFile<ELFT>::Elf_Sym Elf_Sym;
644 typedef typename ELFObjectFile<ELFT>::Elf_Shdr Elf_Shdr;
645
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000646 // OK, this is ELF
647 symbol_iterator SymI(I);
648
Rafael Espindola586af97a2013-11-02 21:16:09 +0000649 DataRefImpl Symb = I->getRawDataRefImpl();
650 const Elf_Sym *ESym = Obj.getSymbol(Symb);
651 const ELFFile<ELFT> &EF = *Obj.getELFFile();
652 const Elf_Shdr *ESec = EF.getSection(ESym);
653
Rafael Espindola586af97a2013-11-02 21:16:09 +0000654 if (ESec) {
655 switch (ESec->sh_type) {
656 case ELF::SHT_PROGBITS:
657 case ELF::SHT_DYNAMIC:
658 switch (ESec->sh_flags) {
Kevin Enderby8f6dcf52014-07-01 22:44:51 +0000659 case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR):
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000660 return 't';
Kevin Enderby8f6dcf52014-07-01 22:44:51 +0000661 case (ELF::SHF_TLS | ELF::SHF_ALLOC | ELF::SHF_WRITE):
662 case (ELF::SHF_ALLOC | ELF::SHF_WRITE):
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000663 return 'd';
Rafael Espindola586af97a2013-11-02 21:16:09 +0000664 case ELF::SHF_ALLOC:
Kevin Enderby8f6dcf52014-07-01 22:44:51 +0000665 case (ELF::SHF_ALLOC | ELF::SHF_MERGE):
666 case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS):
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000667 return 'r';
Rafael Espindola586af97a2013-11-02 21:16:09 +0000668 }
669 break;
670 case ELF::SHT_NOBITS:
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000671 return 'b';
Rafael Espindola586af97a2013-11-02 21:16:09 +0000672 }
673 }
674
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000675 if (ESym->getType() == ELF::STT_SECTION) {
Rafael Espindola586af97a2013-11-02 21:16:09 +0000676 StringRef Name;
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000677 if (error(SymI->getName(Name)))
Rafael Espindola74375892014-02-04 00:21:18 +0000678 return '?';
679 return StringSwitch<char>(Name)
680 .StartsWith(".debug", 'N')
681 .StartsWith(".note", 'n')
682 .Default('?');
Rafael Espindola586af97a2013-11-02 21:16:09 +0000683 }
684
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000685 return '?';
Rafael Espindola586af97a2013-11-02 21:16:09 +0000686}
687
Rafael Espindola74375892014-02-04 00:21:18 +0000688static char getSymbolNMTypeChar(COFFObjectFile &Obj, symbol_iterator I) {
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000689 const coff_symbol *Symb = Obj.getCOFFSymbol(*I);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000690 // OK, this is COFF.
691 symbol_iterator SymI(I);
692
Rafael Espindola619581c2014-01-29 04:56:19 +0000693 StringRef Name;
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000694 if (error(SymI->getName(Name)))
Rafael Espindola74375892014-02-04 00:21:18 +0000695 return '?';
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000696
Rafael Espindola619581c2014-01-29 04:56:19 +0000697 char Ret = StringSwitch<char>(Name)
Rafael Espindola586af97a2013-11-02 21:16:09 +0000698 .StartsWith(".debug", 'N')
699 .StartsWith(".sxdata", 'N')
700 .Default('?');
701
Rafael Espindola74375892014-02-04 00:21:18 +0000702 if (Ret != '?')
703 return Ret;
Rafael Espindola586af97a2013-11-02 21:16:09 +0000704
705 uint32_t Characteristics = 0;
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000706 if (!COFF::isReservedSectionNumber(Symb->SectionNumber)) {
Rafael Espindolab5155a52014-02-10 20:24:04 +0000707 section_iterator SecI = Obj.section_end();
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000708 if (error(SymI->getSection(SecI)))
Rafael Espindola74375892014-02-04 00:21:18 +0000709 return '?';
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000710 const coff_section *Section = Obj.getCOFFSection(*SecI);
Rafael Espindola586af97a2013-11-02 21:16:09 +0000711 Characteristics = Section->Characteristics;
712 }
713
Rafael Espindola619581c2014-01-29 04:56:19 +0000714 switch (Symb->SectionNumber) {
Rafael Espindola586af97a2013-11-02 21:16:09 +0000715 case COFF::IMAGE_SYM_DEBUG:
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000716 return 'n';
Rafael Espindola586af97a2013-11-02 21:16:09 +0000717 default:
718 // Check section type.
719 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000720 return 't';
Rafael Espindola586af97a2013-11-02 21:16:09 +0000721 else if (Characteristics & COFF::IMAGE_SCN_MEM_READ &&
722 ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000723 return 'r';
Rafael Espindola586af97a2013-11-02 21:16:09 +0000724 else if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000725 return 'd';
Rafael Espindola586af97a2013-11-02 21:16:09 +0000726 else if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000727 return 'b';
Rafael Espindola586af97a2013-11-02 21:16:09 +0000728 else if (Characteristics & COFF::IMAGE_SCN_LNK_INFO)
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000729 return 'i';
Rafael Espindola586af97a2013-11-02 21:16:09 +0000730
731 // Check for section symbol.
David Majnemerddf28f22014-03-19 04:47:47 +0000732 else if (Symb->isSectionDefinition())
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000733 return 's';
Rafael Espindola586af97a2013-11-02 21:16:09 +0000734 }
735
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000736 return '?';
Rafael Espindola586af97a2013-11-02 21:16:09 +0000737}
738
739static uint8_t getNType(MachOObjectFile &Obj, DataRefImpl Symb) {
740 if (Obj.is64Bit()) {
741 MachO::nlist_64 STE = Obj.getSymbol64TableEntry(Symb);
742 return STE.n_type;
743 }
744 MachO::nlist STE = Obj.getSymbolTableEntry(Symb);
745 return STE.n_type;
746}
747
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000748static char getSymbolNMTypeChar(MachOObjectFile &Obj, basic_symbol_iterator I) {
Rafael Espindola586af97a2013-11-02 21:16:09 +0000749 DataRefImpl Symb = I->getRawDataRefImpl();
750 uint8_t NType = getNType(Obj, Symb);
751
Kevin Enderby407cc212014-07-17 22:47:16 +0000752 if (NType & MachO::N_STAB)
753 return '-';
754
Rafael Espindola586af97a2013-11-02 21:16:09 +0000755 switch (NType & MachO::N_TYPE) {
Rafael Espindola586af97a2013-11-02 21:16:09 +0000756 case MachO::N_ABS:
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000757 return 's';
Tim Northovereaef0742014-05-30 13:22:59 +0000758 case MachO::N_INDR:
759 return 'i';
Rafael Espindola586af97a2013-11-02 21:16:09 +0000760 case MachO::N_SECT: {
Rafael Espindolab5155a52014-02-10 20:24:04 +0000761 section_iterator Sec = Obj.section_end();
Rafael Espindola586af97a2013-11-02 21:16:09 +0000762 Obj.getSymbolSection(Symb, Sec);
763 DataRefImpl Ref = Sec->getRawDataRefImpl();
764 StringRef SectionName;
765 Obj.getSectionName(Ref, SectionName);
766 StringRef SegmentName = Obj.getSectionFinalSegmentName(Ref);
767 if (SegmentName == "__TEXT" && SectionName == "__text")
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000768 return 't';
Kevin Enderby1e1b9922014-06-19 22:49:21 +0000769 else if (SegmentName == "__DATA" && SectionName == "__data")
770 return 'd';
771 else if (SegmentName == "__DATA" && SectionName == "__bss")
772 return 'b';
Rafael Espindola586af97a2013-11-02 21:16:09 +0000773 else
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000774 return 's';
775 }
Rafael Espindola586af97a2013-11-02 21:16:09 +0000776 }
777
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000778 return '?';
779}
780
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000781static char getSymbolNMTypeChar(const GlobalValue &GV) {
Rafael Espindola64c1e182014-06-03 02:41:57 +0000782 if (GV.getType()->getElementType()->isFunctionTy())
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000783 return 't';
784 // FIXME: should we print 'b'? At the IR level we cannot be sure if this
785 // will be in bss or not, but we could approximate.
Rafael Espindola64c1e182014-06-03 02:41:57 +0000786 return 'd';
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000787}
788
789static char getSymbolNMTypeChar(IRObjectFile &Obj, basic_symbol_iterator I) {
Rafael Espindola13b69d62014-07-03 18:59:23 +0000790 const GlobalValue *GV = Obj.getSymbolGV(I->getRawDataRefImpl());
791 if (!GV)
792 return 't';
793 return getSymbolNMTypeChar(*GV);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000794}
795
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000796template <class ELFT>
797static bool isObject(ELFObjectFile<ELFT> &Obj, symbol_iterator I) {
798 typedef typename ELFObjectFile<ELFT>::Elf_Sym Elf_Sym;
799
800 DataRefImpl Symb = I->getRawDataRefImpl();
801 const Elf_Sym *ESym = Obj.getSymbol(Symb);
802
803 return ESym->getType() == ELF::STT_OBJECT;
804}
805
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000806static bool isObject(SymbolicFile *Obj, basic_symbol_iterator I) {
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000807 if (ELF32LEObjectFile *ELF = dyn_cast<ELF32LEObjectFile>(Obj))
808 return isObject(*ELF, I);
809 if (ELF64LEObjectFile *ELF = dyn_cast<ELF64LEObjectFile>(Obj))
810 return isObject(*ELF, I);
811 if (ELF32BEObjectFile *ELF = dyn_cast<ELF32BEObjectFile>(Obj))
812 return isObject(*ELF, I);
813 if (ELF64BEObjectFile *ELF = dyn_cast<ELF64BEObjectFile>(Obj))
814 return isObject(*ELF, I);
815 return false;
Rafael Espindola586af97a2013-11-02 21:16:09 +0000816}
817
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000818static char getNMTypeChar(SymbolicFile *Obj, basic_symbol_iterator I) {
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000819 uint32_t Symflags = I->getFlags();
820 if ((Symflags & object::SymbolRef::SF_Weak) && !isa<MachOObjectFile>(Obj)) {
821 char Ret = isObject(Obj, I) ? 'v' : 'w';
822 if (!(Symflags & object::SymbolRef::SF_Undefined))
823 Ret = toupper(Ret);
824 return Ret;
825 }
826
827 if (Symflags & object::SymbolRef::SF_Undefined)
828 return 'U';
829
830 if (Symflags & object::SymbolRef::SF_Common)
831 return 'C';
832
833 char Ret = '?';
834 if (Symflags & object::SymbolRef::SF_Absolute)
835 Ret = 'a';
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000836 else if (IRObjectFile *IR = dyn_cast<IRObjectFile>(Obj))
837 Ret = getSymbolNMTypeChar(*IR, I);
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000838 else if (COFFObjectFile *COFF = dyn_cast<COFFObjectFile>(Obj))
839 Ret = getSymbolNMTypeChar(*COFF, I);
840 else if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(Obj))
841 Ret = getSymbolNMTypeChar(*MachO, I);
842 else if (ELF32LEObjectFile *ELF = dyn_cast<ELF32LEObjectFile>(Obj))
843 Ret = getSymbolNMTypeChar(*ELF, I);
844 else if (ELF64LEObjectFile *ELF = dyn_cast<ELF64LEObjectFile>(Obj))
845 Ret = getSymbolNMTypeChar(*ELF, I);
846 else if (ELF32BEObjectFile *ELF = dyn_cast<ELF32BEObjectFile>(Obj))
847 Ret = getSymbolNMTypeChar(*ELF, I);
848 else
849 Ret = getSymbolNMTypeChar(*cast<ELF64BEObjectFile>(Obj), I);
850
851 if (Symflags & object::SymbolRef::SF_Global)
852 Ret = toupper(Ret);
853
854 return Ret;
Rafael Espindola586af97a2013-11-02 21:16:09 +0000855}
856
Kevin Enderbyfe6ad972014-07-11 20:30:00 +0000857// getNsectForSegSect() is used to implement the Mach-O "-s segname sectname"
858// option to dump only those symbols from that section in a Mach-O file.
859// It is called once for each Mach-O file from dumpSymbolNamesFromObject()
860// to get the section number for that named section from the command line
861// arguments. It returns the section number for that section in the Mach-O
862// file or zero it is not present.
863static unsigned getNsectForSegSect(MachOObjectFile *Obj) {
864 unsigned Nsect = 1;
865 for (section_iterator I = Obj->section_begin(), E = Obj->section_end();
866 I != E; ++I) {
867 DataRefImpl Ref = I->getRawDataRefImpl();
868 StringRef SectionName;
869 Obj->getSectionName(Ref, SectionName);
870 StringRef SegmentName = Obj->getSectionFinalSegmentName(Ref);
871 if (SegmentName == SegSect[0] && SectionName == SegSect[1])
872 return Nsect;
873 Nsect++;
874 }
875 return 0;
876}
877
878// getNsectInMachO() is used to implement the Mach-O "-s segname sectname"
879// option to dump only those symbols from that section in a Mach-O file.
880// It is called once for each symbol in a Mach-O file from
881// dumpSymbolNamesFromObject() and returns the section number for that symbol
882// if it is in a section, else it returns 0.
883static unsigned getNsectInMachO(MachOObjectFile &Obj, basic_symbol_iterator I) {
884 DataRefImpl Symb = I->getRawDataRefImpl();
885 if (Obj.is64Bit()) {
886 MachO::nlist_64 STE = Obj.getSymbol64TableEntry(Symb);
887 if ((STE.n_type & MachO::N_TYPE) == MachO::N_SECT)
888 return STE.n_sect;
889 return 0;
890 }
891 MachO::nlist STE = Obj.getSymbolTableEntry(Symb);
892 if ((STE.n_type & MachO::N_TYPE) == MachO::N_SECT)
893 return STE.n_sect;
894 return 0;
895}
896
Kevin Enderby08e1bbd2014-07-24 23:31:52 +0000897static void dumpSymbolNamesFromObject(SymbolicFile *Obj, bool printName,
898 std::string ArchiveName = std::string(),
899 std::string ArchitectureName =
900 std::string()) {
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000901 basic_symbol_iterator IBegin = Obj->symbol_begin();
902 basic_symbol_iterator IEnd = Obj->symbol_end();
Michael J. Spencer8c4729f2012-02-28 00:40:37 +0000903 if (DynamicSyms) {
Rafael Espindola196666c2014-01-30 20:45:33 +0000904 if (!Obj->isELF()) {
905 error("File format has no dynamic symbol table", Obj->getFileName());
906 return;
907 }
Alexey Samsonov65056a32014-02-26 12:51:19 +0000908 std::pair<symbol_iterator, symbol_iterator> IDyn =
909 getELFDynamicSymbolIterators(Obj);
910 IBegin = IDyn.first;
911 IEnd = IDyn.second;
Michael J. Spencer8c4729f2012-02-28 00:40:37 +0000912 }
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000913 std::string NameBuffer;
914 raw_string_ostream OS(NameBuffer);
Kevin Enderbyfe6ad972014-07-11 20:30:00 +0000915 // If a "-s segname sectname" option was specified and this is a Mach-O
916 // file get the section number for that section in this object file.
917 unsigned int Nsect = 0;
918 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(Obj);
919 if (SegSect.size() != 0 && MachO) {
920 Nsect = getNsectForSegSect(MachO);
921 // If this section is not in the object file no symbols are printed.
922 if (Nsect == 0)
923 return;
924 }
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000925 for (basic_symbol_iterator I = IBegin; I != IEnd; ++I) {
Rafael Espindola20122a42014-01-31 20:57:12 +0000926 uint32_t SymFlags = I->getFlags();
Rafael Espindola619581c2014-01-29 04:56:19 +0000927 if (!DebugSyms && (SymFlags & SymbolRef::SF_FormatSpecific))
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000928 continue;
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000929 if (WithoutAliases) {
930 if (IRObjectFile *IR = dyn_cast<IRObjectFile>(Obj)) {
Rafael Espindola13b69d62014-07-03 18:59:23 +0000931 const GlobalValue *GV = IR->getSymbolGV(I->getRawDataRefImpl());
932 if (GV && isa<GlobalAlias>(GV))
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000933 continue;
934 }
935 }
Kevin Enderbyfe6ad972014-07-11 20:30:00 +0000936 // If a "-s segname sectname" option was specified and this is a Mach-O
937 // file and this section appears in this file, Nsect will be non-zero then
938 // see if this symbol is a symbol from that section and if not skip it.
939 if (Nsect && Nsect != getNsectInMachO(*MachO, I))
940 continue;
Rafael Espindola619581c2014-01-29 04:56:19 +0000941 NMSymbol S;
Rafael Espindola0115b082014-01-30 21:51:42 +0000942 S.Size = UnknownAddressOrSize;
Rafael Espindolab4865d62014-04-03 00:19:35 +0000943 S.Address = UnknownAddressOrSize;
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000944 if ((PrintSize || SizeSort) && isa<ObjectFile>(Obj)) {
945 symbol_iterator SymI = I;
946 if (error(SymI->getSize(S.Size)))
Rafael Espindola619581c2014-01-29 04:56:19 +0000947 break;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000948 }
Rafael Espindolab4865d62014-04-03 00:19:35 +0000949 if (PrintAddress && isa<ObjectFile>(Obj))
950 if (error(symbol_iterator(I)->getAddress(S.Address)))
Rafael Espindola619581c2014-01-29 04:56:19 +0000951 break;
952 S.TypeChar = getNMTypeChar(Obj, I);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000953 if (error(I->printName(OS)))
Rafael Espindola619581c2014-01-29 04:56:19 +0000954 break;
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000955 OS << '\0';
Kevin Enderby980b2582014-06-05 21:21:57 +0000956 S.Symb = I->getRawDataRefImpl();
Rafael Espindola619581c2014-01-29 04:56:19 +0000957 SymbolList.push_back(S);
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000958 }
959
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000960 OS.flush();
961 const char *P = NameBuffer.c_str();
962 for (unsigned I = 0; I < SymbolList.size(); ++I) {
963 SymbolList[I].Name = P;
964 P += strlen(P) + 1;
965 }
966
Rafael Espindola619581c2014-01-29 04:56:19 +0000967 CurrentFilename = Obj->getFileName();
Kevin Enderby08e1bbd2014-07-24 23:31:52 +0000968 sortAndPrintSymbolList(Obj, printName, ArchiveName, ArchitectureName);
Brian Gaeke0af759d2003-10-16 04:43:15 +0000969}
970
Kevin Enderby4c8dfe42014-06-30 18:45:23 +0000971// checkMachOAndArchFlags() checks to see if the SymbolicFile is a Mach-O file
972// and if it is and there is a list of architecture flags is specified then
973// check to make sure this Mach-O file is one of those architectures or all
974// architectures was specificed. If not then an error is generated and this
975// routine returns false. Else it returns true.
976static bool checkMachOAndArchFlags(SymbolicFile *O, std::string &Filename) {
977 if (isa<MachOObjectFile>(O) && !ArchAll && ArchFlags.size() != 0) {
978 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(O);
979 bool ArchFound = false;
980 MachO::mach_header H;
981 MachO::mach_header_64 H_64;
982 Triple T;
983 if (MachO->is64Bit()) {
984 H_64 = MachO->MachOObjectFile::getHeader64();
985 T = MachOObjectFile::getArch(H_64.cputype, H_64.cpusubtype);
986 } else {
987 H = MachO->MachOObjectFile::getHeader();
988 T = MachOObjectFile::getArch(H.cputype, H.cpusubtype);
989 }
990 unsigned i;
Kevin Enderby8f6dcf52014-07-01 22:44:51 +0000991 for (i = 0; i < ArchFlags.size(); ++i) {
Kevin Enderby4c8dfe42014-06-30 18:45:23 +0000992 if (ArchFlags[i] == T.getArchName())
993 ArchFound = true;
994 break;
995 }
996 if (!ArchFound) {
997 error(ArchFlags[i],
998 "file: " + Filename + " does not contain architecture");
999 return false;
1000 }
1001 }
1002 return true;
1003}
1004
Rafael Espindola619581c2014-01-29 04:56:19 +00001005static void dumpSymbolNamesFromFile(std::string &Filename) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +00001006 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
1007 MemoryBuffer::getFileOrSTDIN(Filename);
1008 if (error(BufferOrErr.getError(), Filename))
Michael J. Spencerbc96f372011-12-13 23:17:29 +00001009 return;
1010
Owen Anderson19251ec2009-07-15 22:16:10 +00001011 LLVMContext &Context = getGlobalContext();
David Blaikie4b9ae522014-07-21 16:26:24 +00001012 ErrorOr<Binary *> BinaryOrErr =
1013 createBinary(std::move(*BufferOrErr), &Context);
Rafael Espindolace82a072014-01-30 19:24:00 +00001014 if (error(BinaryOrErr.getError(), Filename))
1015 return;
Ahmed Charles56440fd2014-03-06 05:51:42 +00001016 std::unique_ptr<Binary> Bin(BinaryOrErr.get());
Shankar Easwaran15b28be2012-11-13 18:38:42 +00001017
Rafael Espindola0115b082014-01-30 21:51:42 +00001018 if (Archive *A = dyn_cast<Archive>(Bin.get())) {
Rafael Espindolace82a072014-01-30 19:24:00 +00001019 if (ArchiveMap) {
Rafael Espindola0115b082014-01-30 21:51:42 +00001020 Archive::symbol_iterator I = A->symbol_begin();
1021 Archive::symbol_iterator E = A->symbol_end();
Rafael Espindolace82a072014-01-30 19:24:00 +00001022 if (I != E) {
Rafael Espindola0115b082014-01-30 21:51:42 +00001023 outs() << "Archive map\n";
Rafael Espindolace82a072014-01-30 19:24:00 +00001024 for (; I != E; ++I) {
Rafael Espindolaae460022014-06-16 16:08:36 +00001025 ErrorOr<Archive::child_iterator> C = I->getMember();
1026 if (error(C.getError()))
Michael J. Spencer9718f452013-02-03 10:48:50 +00001027 return;
Rafael Espindolaae460022014-06-16 16:08:36 +00001028 ErrorOr<StringRef> FileNameOrErr = C.get()->getName();
1029 if (error(FileNameOrErr.getError()))
Rafael Espindolace82a072014-01-30 19:24:00 +00001030 return;
Rafael Espindolaae460022014-06-16 16:08:36 +00001031 StringRef SymName = I->getName();
1032 outs() << SymName << " in " << FileNameOrErr.get() << "\n";
Michael J. Spencer2bc774a2011-09-27 19:37:18 +00001033 }
Rafael Espindolace82a072014-01-30 19:24:00 +00001034 outs() << "\n";
Michael J. Spencer2bc774a2011-09-27 19:37:18 +00001035 }
1036 }
Alexey Samsonove6388e62013-06-18 15:03:28 +00001037
Rafael Espindola0115b082014-01-30 21:51:42 +00001038 for (Archive::child_iterator I = A->child_begin(), E = A->child_end();
Rafael Espindolace82a072014-01-30 19:24:00 +00001039 I != E; ++I) {
Rafael Espindolaae460022014-06-16 16:08:36 +00001040 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = I->getAsBinary(&Context);
1041 if (ChildOrErr.getError())
Rafael Espindolace82a072014-01-30 19:24:00 +00001042 continue;
Rafael Espindolaae460022014-06-16 16:08:36 +00001043 if (SymbolicFile *O = dyn_cast<SymbolicFile>(&*ChildOrErr.get())) {
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001044 if (!checkMachOAndArchFlags(O, Filename))
1045 return;
Kevin Enderby08e1bbd2014-07-24 23:31:52 +00001046 if (!PrintFileName) {
1047 outs() << "\n";
1048 if (isa<MachOObjectFile>(O)) {
1049 outs() << Filename << "(" << O->getFileName() << ")";
1050 } else
1051 outs() << O->getFileName();
1052 outs() << ":\n";
1053 }
1054 dumpSymbolNamesFromObject(O, false, Filename);
Rafael Espindolace82a072014-01-30 19:24:00 +00001055 }
1056 }
1057 return;
1058 }
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001059 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(Bin.get())) {
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001060 // If we have a list of architecture flags specified dump only those.
1061 if (!ArchAll && ArchFlags.size() != 0) {
1062 // Look for a slice in the universal binary that matches each ArchFlag.
1063 bool ArchFound;
Kevin Enderby8f6dcf52014-07-01 22:44:51 +00001064 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001065 ArchFound = false;
1066 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1067 E = UB->end_objects();
1068 I != E; ++I) {
Kevin Enderby8f6dcf52014-07-01 22:44:51 +00001069 if (ArchFlags[i] == I->getArchTypeName()) {
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001070 ArchFound = true;
1071 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr =
Kevin Enderby8f6dcf52014-07-01 22:44:51 +00001072 I->getAsObjectFile();
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001073 std::unique_ptr<Archive> A;
Kevin Enderby08e1bbd2014-07-24 23:31:52 +00001074 std::string ArchiveName;
1075 std::string ArchitectureName;
1076 ArchiveName.clear();
1077 ArchitectureName.clear();
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001078 if (ObjOrErr) {
1079 std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get());
1080 if (ArchFlags.size() > 1) {
Kevin Enderby08e1bbd2014-07-24 23:31:52 +00001081 if (PrintFileName)
1082 ArchitectureName = I->getArchTypeName();
1083 else
1084 outs() << "\n" << Obj->getFileName() << " (for architecture "
1085 << I->getArchTypeName() << ")"
1086 << ":\n";
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001087 }
Kevin Enderby08e1bbd2014-07-24 23:31:52 +00001088 dumpSymbolNamesFromObject(Obj.get(), false, ArchiveName,
1089 ArchitectureName);
Kevin Enderby8f6dcf52014-07-01 22:44:51 +00001090 } else if (!I->getAsArchive(A)) {
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001091 for (Archive::child_iterator AI = A->child_begin(),
1092 AE = A->child_end();
1093 AI != AE; ++AI) {
1094 ErrorOr<std::unique_ptr<Binary>> ChildOrErr =
1095 AI->getAsBinary(&Context);
1096 if (ChildOrErr.getError())
1097 continue;
Kevin Enderby8f6dcf52014-07-01 22:44:51 +00001098 if (SymbolicFile *O =
1099 dyn_cast<SymbolicFile>(&*ChildOrErr.get())) {
Kevin Enderby08e1bbd2014-07-24 23:31:52 +00001100 if (PrintFileName) {
1101 ArchiveName = A->getFileName();
1102 if (ArchFlags.size() > 1)
1103 ArchitectureName = I->getArchTypeName();
1104 } else {
1105 outs() << "\n" << A->getFileName();
1106 outs() << "(" << O->getFileName() << ")";
1107 if (ArchFlags.size() > 1) {
1108 outs() << " (for architecture " << I->getArchTypeName()
1109 << ")";
1110 }
1111 outs() << ":\n";
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001112 }
Kevin Enderby08e1bbd2014-07-24 23:31:52 +00001113 dumpSymbolNamesFromObject(O, false, ArchiveName,
1114 ArchitectureName);
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001115 }
1116 }
1117 }
1118 }
1119 }
1120 if (!ArchFound) {
1121 error(ArchFlags[i],
1122 "file: " + Filename + " does not contain architecture");
1123 return;
1124 }
1125 }
1126 return;
1127 }
1128 // No architecture flags were specified so if this contains a slice that
1129 // matches the host architecture dump only that.
1130 if (!ArchAll) {
Kevin Enderby8f6dcf52014-07-01 22:44:51 +00001131 StringRef HostArchName = MachOObjectFile::getHostArch().getArchName();
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001132 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1133 E = UB->end_objects();
1134 I != E; ++I) {
Kevin Enderby8f6dcf52014-07-01 22:44:51 +00001135 if (HostArchName == I->getArchTypeName()) {
1136 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001137 std::unique_ptr<Archive> A;
Kevin Enderby08e1bbd2014-07-24 23:31:52 +00001138 std::string ArchiveName;
1139 ArchiveName.clear();
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001140 if (ObjOrErr) {
1141 std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get());
1142 dumpSymbolNamesFromObject(Obj.get(), false);
Kevin Enderby8f6dcf52014-07-01 22:44:51 +00001143 } else if (!I->getAsArchive(A)) {
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001144 for (Archive::child_iterator AI = A->child_begin(),
1145 AE = A->child_end();
1146 AI != AE; ++AI) {
1147 ErrorOr<std::unique_ptr<Binary>> ChildOrErr =
1148 AI->getAsBinary(&Context);
1149 if (ChildOrErr.getError())
1150 continue;
Kevin Enderby8f6dcf52014-07-01 22:44:51 +00001151 if (SymbolicFile *O =
1152 dyn_cast<SymbolicFile>(&*ChildOrErr.get())) {
Kevin Enderby08e1bbd2014-07-24 23:31:52 +00001153 if (PrintFileName)
1154 ArchiveName = A->getFileName();
1155 else
1156 outs() << "\n" << A->getFileName() << "(" << O->getFileName()
1157 << ")"
1158 << ":\n";
1159 dumpSymbolNamesFromObject(O, false, ArchiveName);
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001160 }
1161 }
1162 }
1163 return;
1164 }
1165 }
1166 }
1167 // Either all architectures have been specified or none have been specified
1168 // and this does not contain the host architecture so dump all the slices.
Kevin Enderby1983fcf2014-06-19 22:03:18 +00001169 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
Rafael Espindola0115b082014-01-30 21:51:42 +00001170 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1171 E = UB->end_objects();
Alexey Samsonove6388e62013-06-18 15:03:28 +00001172 I != E; ++I) {
Rafael Espindola4f7932b2014-06-23 20:41:02 +00001173 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
Kevin Enderbye858a652014-05-14 21:18:50 +00001174 std::unique_ptr<Archive> A;
Kevin Enderby08e1bbd2014-07-24 23:31:52 +00001175 std::string ArchiveName;
1176 std::string ArchitectureName;
1177 ArchiveName.clear();
1178 ArchitectureName.clear();
Rafael Espindola4f7932b2014-06-23 20:41:02 +00001179 if (ObjOrErr) {
1180 std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get());
Kevin Enderby08e1bbd2014-07-24 23:31:52 +00001181 if (PrintFileName) {
1182 if (isa<MachOObjectFile>(Obj.get()) && moreThanOneArch)
1183 ArchitectureName = I->getArchTypeName();
1184 } else {
1185 if (moreThanOneArch)
1186 outs() << "\n";
1187 outs() << Obj->getFileName();
1188 if (isa<MachOObjectFile>(Obj.get()) && moreThanOneArch)
1189 outs() << " (for architecture " << I->getArchTypeName() << ")";
1190 outs() << ":\n";
1191 }
1192 dumpSymbolNamesFromObject(Obj.get(), false, ArchiveName,
1193 ArchitectureName);
Kevin Enderby8f6dcf52014-07-01 22:44:51 +00001194 } else if (!I->getAsArchive(A)) {
Kevin Enderbye858a652014-05-14 21:18:50 +00001195 for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end();
1196 AI != AE; ++AI) {
Rafael Espindolaae460022014-06-16 16:08:36 +00001197 ErrorOr<std::unique_ptr<Binary>> ChildOrErr =
1198 AI->getAsBinary(&Context);
1199 if (ChildOrErr.getError())
Kevin Enderbye858a652014-05-14 21:18:50 +00001200 continue;
Rafael Espindolaae460022014-06-16 16:08:36 +00001201 if (SymbolicFile *O = dyn_cast<SymbolicFile>(&*ChildOrErr.get())) {
Kevin Enderby08e1bbd2014-07-24 23:31:52 +00001202 if (PrintFileName) {
1203 ArchiveName = A->getFileName();
1204 if (isa<MachOObjectFile>(O) && moreThanOneArch)
1205 ArchitectureName = I->getArchTypeName();
1206 } else {
1207 outs() << "\n" << A->getFileName();
1208 if (isa<MachOObjectFile>(O)) {
1209 outs() << "(" << O->getFileName() << ")";
1210 if (moreThanOneArch)
1211 outs() << " (for architecture " << I->getArchTypeName()
1212 << ")";
1213 } else
1214 outs() << ":" << O->getFileName();
1215 outs() << ":\n";
1216 }
1217 dumpSymbolNamesFromObject(O, false, ArchiveName, ArchitectureName);
Kevin Enderbye858a652014-05-14 21:18:50 +00001218 }
1219 }
1220 }
Alexey Samsonove6388e62013-06-18 15:03:28 +00001221 }
Brian Gaeke618026a2003-11-19 21:57:30 +00001222 return;
Brian Gaeke0af759d2003-10-16 04:43:15 +00001223 }
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001224 if (SymbolicFile *O = dyn_cast<SymbolicFile>(Bin.get())) {
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001225 if (!checkMachOAndArchFlags(O, Filename))
1226 return;
Kevin Enderby26646102014-06-20 21:29:27 +00001227 dumpSymbolNamesFromObject(O, true);
Rafael Espindolace82a072014-01-30 19:24:00 +00001228 return;
1229 }
1230 error("unrecognizable file type", Filename);
1231 return;
Brian Gaeke0af759d2003-10-16 04:43:15 +00001232}
1233
1234int main(int argc, char **argv) {
Chris Lattnere3fc2d12009-03-06 05:34:10 +00001235 // Print a stack trace if we signal out.
Chris Lattneref8f3892007-05-06 05:36:18 +00001236 sys::PrintStackTraceOnErrorSignal();
Chris Lattnere3fc2d12009-03-06 05:34:10 +00001237 PrettyStackTraceProgram X(argc, argv);
Michael J. Spencer618d2192010-08-31 06:36:46 +00001238
Rafael Espindola619581c2014-01-29 04:56:19 +00001239 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Chris Lattnere3fc2d12009-03-06 05:34:10 +00001240 cl::ParseCommandLineOptions(argc, argv, "llvm symbol table dumper\n");
Chris Lattner12439ff2004-02-19 20:32:12 +00001241
Michael J. Spencerbc96f372011-12-13 23:17:29 +00001242 // llvm-nm only reads binary files.
Rafael Espindolacb2eca02013-06-12 20:58:35 +00001243 if (error(sys::ChangeStdinToBinary()))
Michael J. Spencerbc96f372011-12-13 23:17:29 +00001244 return 1;
1245
Rafael Espindola13b69d62014-07-03 18:59:23 +00001246 llvm::InitializeAllTargetInfos();
1247 llvm::InitializeAllTargetMCs();
1248 llvm::InitializeAllAsmParsers();
1249
Chris Lattneref8f3892007-05-06 05:36:18 +00001250 ToolName = argv[0];
Rafael Espindola619581c2014-01-29 04:56:19 +00001251 if (BSDFormat)
1252 OutputFormat = bsd;
1253 if (POSIXFormat)
1254 OutputFormat = posix;
Kevin Enderby14a96ac2014-06-20 00:04:16 +00001255 if (DarwinFormat)
1256 OutputFormat = darwin;
Chris Lattner4aae1f42003-10-16 18:45:23 +00001257
Michael J. Spencerb8672a52011-01-20 06:38:57 +00001258 // The relative order of these is important. If you pass --size-sort it should
1259 // only print out the size. However, if you pass -S --size-sort, it should
Rafael Espindolab4865d62014-04-03 00:19:35 +00001260 // print out both the size and address.
Rafael Espindola619581c2014-01-29 04:56:19 +00001261 if (SizeSort && !PrintSize)
Rafael Espindolab4865d62014-04-03 00:19:35 +00001262 PrintAddress = false;
Rafael Espindola619581c2014-01-29 04:56:19 +00001263 if (OutputFormat == sysv || SizeSort)
1264 PrintSize = true;
Michael J. Spencerb8672a52011-01-20 06:38:57 +00001265
Chris Lattneref8f3892007-05-06 05:36:18 +00001266 switch (InputFilenames.size()) {
Rafael Espindola619581c2014-01-29 04:56:19 +00001267 case 0:
Kevin Enderby4fc2edb2014-06-23 20:27:53 +00001268 InputFilenames.push_back("a.out");
Rafael Espindola619581c2014-01-29 04:56:19 +00001269 case 1:
1270 break;
1271 default:
1272 MultipleFiles = true;
Chris Lattner4aae1f42003-10-16 18:45:23 +00001273 }
Chris Lattneref8f3892007-05-06 05:36:18 +00001274
Kevin Enderby8f6dcf52014-07-01 22:44:51 +00001275 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001276 if (ArchFlags[i] == "all") {
1277 ArchAll = true;
Kevin Enderby8f6dcf52014-07-01 22:44:51 +00001278 } else {
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001279 Triple T = MachOObjectFile::getArch(ArchFlags[i]);
1280 if (T.getArch() == Triple::UnknownArch)
1281 error("Unknown architecture named '" + ArchFlags[i] + "'",
1282 "for the -arch option");
1283 }
1284 }
1285
Kevin Enderbyfe6ad972014-07-11 20:30:00 +00001286 if (SegSect.size() != 0 && SegSect.size() != 2)
1287 error("bad number of arguments (must be two arguments)",
1288 "for the -s option");
1289
Chris Lattneref8f3892007-05-06 05:36:18 +00001290 std::for_each(InputFilenames.begin(), InputFilenames.end(),
Rafael Espindola619581c2014-01-29 04:56:19 +00001291 dumpSymbolNamesFromFile);
Rafael Espindola8b82a4d2013-07-03 15:46:03 +00001292
1293 if (HadError)
1294 return 1;
1295
Chris Lattneref8f3892007-05-06 05:36:18 +00001296 return 0;
Brian Gaeke0af759d2003-10-16 04:43:15 +00001297}