blob: e288e5a144d5f7d1c68742c3038fc8fdfcb7b274 [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"
Kevin Enderbydc0dbe12015-11-10 00:31:08 +000023#include "llvm/IR/Module.h"
Michael J. Spencer2bc774a2011-09-27 19:37:18 +000024#include "llvm/Object/Archive.h"
Rafael Espindola586af97a2013-11-02 21:16:09 +000025#include "llvm/Object/COFF.h"
26#include "llvm/Object/ELFObjectFile.h"
Chandler Carruth442f7842014-03-04 10:07:28 +000027#include "llvm/Object/IRObjectFile.h"
Rafael Espindola586af97a2013-11-02 21:16:09 +000028#include "llvm/Object/MachO.h"
Alexey Samsonove6388e62013-06-18 15:03:28 +000029#include "llvm/Object/MachOUniversal.h"
Michael J. Spencerb8672a52011-01-20 06:38:57 +000030#include "llvm/Object/ObjectFile.h"
Rui Ueyamaf078eff2014-03-18 23:37:53 +000031#include "llvm/Support/COFF.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000032#include "llvm/Support/CommandLine.h"
Michael J. Spencerb8672a52011-01-20 06:38:57 +000033#include "llvm/Support/FileSystem.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000034#include "llvm/Support/Format.h"
Chris Lattner76d46322006-12-06 01:18:01 +000035#include "llvm/Support/ManagedStatic.h"
Chris Lattneref8f3892007-05-06 05:36:18 +000036#include "llvm/Support/MemoryBuffer.h"
Chris Lattnere3fc2d12009-03-06 05:34:10 +000037#include "llvm/Support/PrettyStackTrace.h"
Michael J. Spencerbc96f372011-12-13 23:17:29 +000038#include "llvm/Support/Program.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000039#include "llvm/Support/Signals.h"
Rafael Espindola13b69d62014-07-03 18:59:23 +000040#include "llvm/Support/TargetSelect.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000041#include "llvm/Support/raw_ostream.h"
Jeff Cohenb622c112007-03-05 00:00:42 +000042#include <algorithm>
Brian Gaeke0af759d2003-10-16 04:43:15 +000043#include <cctype>
Alkis Evlogimenosf68f40e2004-04-21 16:11:40 +000044#include <cerrno>
Brian Gaeke55447b42003-11-19 21:52:09 +000045#include <cstring>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000046#include <system_error>
Michael J. Spencerb8672a52011-01-20 06:38:57 +000047#include <vector>
Hans Wennborg083ca9b2015-10-06 23:24:35 +000048
Brian Gaeke960707c2003-11-11 22:41:34 +000049using namespace llvm;
Michael J. Spencerb8672a52011-01-20 06:38:57 +000050using namespace object;
Brian Gaeke960707c2003-11-11 22:41:34 +000051
Brian Gaeke0af759d2003-10-16 04:43:15 +000052namespace {
Kevin Enderby980b2582014-06-05 21:21:57 +000053enum OutputFormatTy { bsd, sysv, posix, darwin };
Rafael Espindola619581c2014-01-29 04:56:19 +000054cl::opt<OutputFormatTy> OutputFormat(
55 "format", cl::desc("Specify output format"),
56 cl::values(clEnumVal(bsd, "BSD format"), clEnumVal(sysv, "System V format"),
Kevin Enderby980b2582014-06-05 21:21:57 +000057 clEnumVal(posix, "POSIX.2 format"),
58 clEnumVal(darwin, "Darwin -m format"), clEnumValEnd),
Rafael Espindola619581c2014-01-29 04:56:19 +000059 cl::init(bsd));
60cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
61 cl::aliasopt(OutputFormat));
Brian Gaeke0af759d2003-10-16 04:43:15 +000062
Rafael Espindolaf12b8282014-02-21 20:10:59 +000063cl::list<std::string> InputFilenames(cl::Positional, cl::desc("<input files>"),
Rafael Espindola619581c2014-01-29 04:56:19 +000064 cl::ZeroOrMore);
Brian Gaeke0af759d2003-10-16 04:43:15 +000065
Rafael Espindola619581c2014-01-29 04:56:19 +000066cl::opt<bool> UndefinedOnly("undefined-only",
67 cl::desc("Show only undefined symbols"));
68cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
Kevin Enderby9c8905c2015-11-02 23:42:05 +000069 cl::aliasopt(UndefinedOnly), cl::Grouping);
Brian Gaeke0af759d2003-10-16 04:43:15 +000070
Rafael Espindola619581c2014-01-29 04:56:19 +000071cl::opt<bool> DynamicSyms("dynamic",
72 cl::desc("Display the dynamic symbols instead "
73 "of normal symbols."));
74cl::alias DynamicSyms2("D", cl::desc("Alias for --dynamic"),
Kevin Enderby9c8905c2015-11-02 23:42:05 +000075 cl::aliasopt(DynamicSyms), cl::Grouping);
Michael J. Spencer8c4729f2012-02-28 00:40:37 +000076
Rafael Espindola619581c2014-01-29 04:56:19 +000077cl::opt<bool> DefinedOnly("defined-only",
78 cl::desc("Show only defined symbols"));
Kevin Enderbyacaaf902014-07-03 18:18:50 +000079cl::alias DefinedOnly2("U", cl::desc("Alias for --defined-only"),
Kevin Enderby9c8905c2015-11-02 23:42:05 +000080 cl::aliasopt(DefinedOnly), cl::Grouping);
Brian Gaeke0af759d2003-10-16 04:43:15 +000081
Rafael Espindola619581c2014-01-29 04:56:19 +000082cl::opt<bool> ExternalOnly("extern-only",
83 cl::desc("Show only external symbols"));
84cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
Kevin Enderby9c8905c2015-11-02 23:42:05 +000085 cl::aliasopt(ExternalOnly), cl::Grouping);
Brian Gaeke0af759d2003-10-16 04:43:15 +000086
Kevin Enderby9c8905c2015-11-02 23:42:05 +000087cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"),
88 cl::Grouping);
89cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"),
90 cl::Grouping);
91cl::opt<bool> DarwinFormat("m", cl::desc("Alias for --format=darwin"),
92 cl::Grouping);
Brian Gaeke0af759d2003-10-16 04:43:15 +000093
Kevin Enderby8f6dcf52014-07-01 22:44:51 +000094static cl::list<std::string>
Kevin Enderbybe84b292014-07-17 22:56:27 +000095 ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
96 cl::ZeroOrMore);
Kevin Enderby4c8dfe42014-06-30 18:45:23 +000097bool ArchAll = false;
98
Rafael Espindola619581c2014-01-29 04:56:19 +000099cl::opt<bool> PrintFileName(
100 "print-file-name",
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000101 cl::desc("Precede each symbol with the object file it came from"));
102
Rafael Espindola619581c2014-01-29 04:56:19 +0000103cl::alias PrintFileNameA("A", cl::desc("Alias for --print-file-name"),
Kevin Enderby9c8905c2015-11-02 23:42:05 +0000104 cl::aliasopt(PrintFileName), cl::Grouping);
Rafael Espindola619581c2014-01-29 04:56:19 +0000105cl::alias PrintFileNameo("o", cl::desc("Alias for --print-file-name"),
Kevin Enderby9c8905c2015-11-02 23:42:05 +0000106 cl::aliasopt(PrintFileName), cl::Grouping);
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000107
Rafael Espindola619581c2014-01-29 04:56:19 +0000108cl::opt<bool> DebugSyms("debug-syms",
109 cl::desc("Show all symbols, even debugger only"));
110cl::alias DebugSymsa("a", cl::desc("Alias for --debug-syms"),
Kevin Enderby9c8905c2015-11-02 23:42:05 +0000111 cl::aliasopt(DebugSyms), cl::Grouping);
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000112
Rafael Espindolab4865d62014-04-03 00:19:35 +0000113cl::opt<bool> NumericSort("numeric-sort", cl::desc("Sort symbols by address"));
Rafael Espindola619581c2014-01-29 04:56:19 +0000114cl::alias NumericSortn("n", cl::desc("Alias for --numeric-sort"),
Kevin Enderby9c8905c2015-11-02 23:42:05 +0000115 cl::aliasopt(NumericSort), cl::Grouping);
Rafael Espindola619581c2014-01-29 04:56:19 +0000116cl::alias NumericSortv("v", cl::desc("Alias for --numeric-sort"),
Kevin Enderby9c8905c2015-11-02 23:42:05 +0000117 cl::aliasopt(NumericSort), cl::Grouping);
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000118
Rafael Espindola619581c2014-01-29 04:56:19 +0000119cl::opt<bool> NoSort("no-sort", cl::desc("Show symbols in order encountered"));
Kevin Enderby9c8905c2015-11-02 23:42:05 +0000120cl::alias NoSortp("p", cl::desc("Alias for --no-sort"), cl::aliasopt(NoSort),
121 cl::Grouping);
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000122
Kevin Enderby25a614b2014-07-02 23:23:58 +0000123cl::opt<bool> ReverseSort("reverse-sort", cl::desc("Sort in reverse order"));
124cl::alias ReverseSortr("r", cl::desc("Alias for --reverse-sort"),
Kevin Enderby9c8905c2015-11-02 23:42:05 +0000125 cl::aliasopt(ReverseSort), cl::Grouping);
Kevin Enderby25a614b2014-07-02 23:23:58 +0000126
Rafael Espindola619581c2014-01-29 04:56:19 +0000127cl::opt<bool> PrintSize("print-size",
Rafael Espindolab4865d62014-04-03 00:19:35 +0000128 cl::desc("Show symbol size instead of address"));
Rafael Espindola619581c2014-01-29 04:56:19 +0000129cl::alias PrintSizeS("S", cl::desc("Alias for --print-size"),
Kevin Enderby9c8905c2015-11-02 23:42:05 +0000130 cl::aliasopt(PrintSize), cl::Grouping);
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000131
Rafael Espindola619581c2014-01-29 04:56:19 +0000132cl::opt<bool> SizeSort("size-sort", cl::desc("Sort symbols by size"));
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000133
Rafael Espindola619581c2014-01-29 04:56:19 +0000134cl::opt<bool> WithoutAliases("without-aliases", cl::Hidden,
135 cl::desc("Exclude aliases from output"));
Jan Sjödin4d0c2992012-09-18 18:47:58 +0000136
Rafael Espindola619581c2014-01-29 04:56:19 +0000137cl::opt<bool> ArchiveMap("print-armap", cl::desc("Print the archive map"));
Kevin Enderby8da4bd62014-07-08 23:47:31 +0000138cl::alias ArchiveMaps("M", cl::desc("Alias for --print-armap"),
Kevin Enderby9c8905c2015-11-02 23:42:05 +0000139 cl::aliasopt(ArchiveMap), cl::Grouping);
Kevin Enderby0fd8aac2014-07-03 21:51:07 +0000140
Hemant Kulkarni5e005a12016-02-10 17:51:39 +0000141enum Radix { d, o, x };
142cl::opt<Radix>
143 AddressRadix("radix", cl::desc("Radix (o/d/x) for printing symbol Values"),
144 cl::values(clEnumVal(d, "decimal"), clEnumVal(o, "octal"),
145 clEnumVal(x, "hexadecimal"), clEnumValEnd),
146 cl::init(x));
147cl::alias RadixAlias("t", cl::desc("Alias for --radix"),
148 cl::aliasopt(AddressRadix));
149
Kevin Enderby0fd8aac2014-07-03 21:51:07 +0000150cl::opt<bool> JustSymbolName("just-symbol-name",
151 cl::desc("Print just the symbol's name"));
152cl::alias JustSymbolNames("j", cl::desc("Alias for --just-symbol-name"),
Kevin Enderby9c8905c2015-11-02 23:42:05 +0000153 cl::aliasopt(JustSymbolName), cl::Grouping);
Kevin Enderbyfe6ad972014-07-11 20:30:00 +0000154
155// FIXME: This option takes exactly two strings and should be allowed anywhere
156// on the command line. Such that "llvm-nm -s __TEXT __text foo.o" would work.
157// But that does not as the CommandLine Library does not have a way to make
158// this work. For now the "-s __TEXT __text" has to be last on the command
159// line.
160cl::list<std::string> SegSect("s", cl::Positional, cl::ZeroOrMore,
161 cl::desc("Dump only symbols from this segment "
162 "and section name, Mach-O only"));
163
Kevin Enderby77b968e2014-07-16 17:38:26 +0000164cl::opt<bool> FormatMachOasHex("x", cl::desc("Print symbol entry in hex, "
Kevin Enderby9c8905c2015-11-02 23:42:05 +0000165 "Mach-O only"), cl::Grouping);
Kevin Enderby77b968e2014-07-16 17:38:26 +0000166
Peter Collingbourne10039c02014-09-18 21:28:49 +0000167cl::opt<bool> NoLLVMBitcode("no-llvm-bc",
168 cl::desc("Disable LLVM bitcode reader"));
169
Rafael Espindolab4865d62014-04-03 00:19:35 +0000170bool PrintAddress = true;
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000171
Rafael Espindola619581c2014-01-29 04:56:19 +0000172bool MultipleFiles = false;
Brian Gaeke0af759d2003-10-16 04:43:15 +0000173
Rafael Espindola619581c2014-01-29 04:56:19 +0000174bool HadError = false;
Rafael Espindola8b82a4d2013-07-03 15:46:03 +0000175
Rafael Espindola619581c2014-01-29 04:56:19 +0000176std::string ToolName;
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000177} // anonymous namespace
Brian Gaeke0af759d2003-10-16 04:43:15 +0000178
Rafael Espindola619581c2014-01-29 04:56:19 +0000179static void error(Twine Message, Twine Path = Twine()) {
180 HadError = true;
181 errs() << ToolName << ": " << Path << ": " << Message << ".\n";
Michael J. Spencerbc96f372011-12-13 23:17:29 +0000182}
183
Rafael Espindola4453e42942014-06-13 03:07:50 +0000184static bool error(std::error_code EC, Twine Path = Twine()) {
Rafael Espindola619581c2014-01-29 04:56:19 +0000185 if (EC) {
186 error(EC.message(), Path);
Michael J. Spencerbc96f372011-12-13 23:17:29 +0000187 return true;
188 }
189 return false;
190}
191
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000192namespace {
Rafael Espindola619581c2014-01-29 04:56:19 +0000193struct NMSymbol {
Rafael Espindolab4865d62014-04-03 00:19:35 +0000194 uint64_t Address;
Rafael Espindola619581c2014-01-29 04:56:19 +0000195 uint64_t Size;
196 char TypeChar;
197 StringRef Name;
Rafael Espindola2d5d23d2015-07-06 21:36:23 +0000198 BasicSymbolRef Sym;
Rafael Espindola619581c2014-01-29 04:56:19 +0000199};
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000200} // anonymous namespace
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000201
Rafael Espindolab4865d62014-04-03 00:19:35 +0000202static bool compareSymbolAddress(const NMSymbol &A, const NMSymbol &B) {
Rafael Espindola45592c62015-07-13 22:01:02 +0000203 bool ADefined = !(A.Sym.getFlags() & SymbolRef::SF_Undefined);
204 bool BDefined = !(B.Sym.getFlags() & SymbolRef::SF_Undefined);
205 return std::make_tuple(ADefined, A.Address, A.Name, A.Size) <
206 std::make_tuple(BDefined, B.Address, B.Name, B.Size);
Rafael Espindola619581c2014-01-29 04:56:19 +0000207}
208
209static bool compareSymbolSize(const NMSymbol &A, const NMSymbol &B) {
Rafael Espindola45592c62015-07-13 22:01:02 +0000210 return std::make_tuple(A.Size, A.Name, A.Address) <
211 std::make_tuple(B.Size, B.Name, B.Address);
Rafael Espindola619581c2014-01-29 04:56:19 +0000212}
213
214static bool compareSymbolName(const NMSymbol &A, const NMSymbol &B) {
Rafael Espindola45592c62015-07-13 22:01:02 +0000215 return std::make_tuple(A.Name, A.Size, A.Address) <
216 std::make_tuple(B.Name, B.Size, B.Address);
Rafael Espindola619581c2014-01-29 04:56:19 +0000217}
218
Rafael Espindolaceb23382014-07-31 21:00:10 +0000219static char isSymbolList64Bit(SymbolicFile &Obj) {
Kevin Enderbydc0dbe12015-11-10 00:31:08 +0000220 if (isa<IRObjectFile>(Obj)) {
221 IRObjectFile *IRobj = dyn_cast<IRObjectFile>(&Obj);
222 Module &M = IRobj->getModule();
223 if (M.getTargetTriple().empty())
224 return false;
225 Triple T(M.getTargetTriple());
226 return T.isArch64Bit();
227 }
David Blaikie328c0c12015-03-23 21:17:43 +0000228 if (isa<COFFObjectFile>(Obj))
Kevin Enderby6abc2e52014-05-09 23:57:49 +0000229 return false;
David Blaikie328c0c12015-03-23 21:17:43 +0000230 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj))
Kevin Enderby6abc2e52014-05-09 23:57:49 +0000231 return MachO->is64Bit();
Rafael Espindola034a4c382015-06-26 14:11:54 +0000232 return cast<ELFObjectFileBase>(Obj).getBytesInAddress() == 8;
Kevin Enderby6abc2e52014-05-09 23:57:49 +0000233}
234
Rafael Espindola619581c2014-01-29 04:56:19 +0000235static StringRef CurrentFilename;
236typedef std::vector<NMSymbol> SymbolListT;
237static SymbolListT SymbolList;
238
Kevin Enderbydc0dbe12015-11-10 00:31:08 +0000239static char getSymbolNMTypeChar(IRObjectFile &Obj, basic_symbol_iterator I);
240
Kevin Enderby980b2582014-06-05 21:21:57 +0000241// darwinPrintSymbol() is used to print a symbol from a Mach-O file when the
Kevin Enderby77b968e2014-07-16 17:38:26 +0000242// the OutputFormat is darwin or we are printing Mach-O symbols in hex. For
243// the darwin format it produces the same output as darwin's nm(1) -m output
244// and when printing Mach-O symbols in hex it produces the same output as
245// darwin's nm(1) -x format.
Kevin Enderbydc0dbe12015-11-10 00:31:08 +0000246static void darwinPrintSymbol(SymbolicFile &Obj, SymbolListT::iterator I,
247 char *SymbolAddrStr, const char *printBlanks,
248 const char *printDashes, const char *printFormat) {
Kevin Enderby980b2582014-06-05 21:21:57 +0000249 MachO::mach_header H;
250 MachO::mach_header_64 H_64;
Reid Kleckner3afb80e2015-11-19 00:51:50 +0000251 uint32_t Filetype = MachO::MH_OBJECT;
252 uint32_t Flags = 0;
Kevin Enderbydc0dbe12015-11-10 00:31:08 +0000253 uint8_t NType = 0;
254 uint8_t NSect = 0;
255 uint16_t NDesc = 0;
256 uint32_t NStrx = 0;
257 uint64_t NValue = 0;
258 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj);
259 if (Obj.isIR()) {
260 uint32_t SymFlags = I->Sym.getFlags();
261 if (SymFlags & SymbolRef::SF_Global)
262 NType |= MachO::N_EXT;
263 if (SymFlags & SymbolRef::SF_Hidden)
264 NType |= MachO::N_PEXT;
265 if (SymFlags & SymbolRef::SF_Undefined)
266 NType |= MachO::N_EXT | MachO::N_UNDF;
267 else {
268 // Here we have a symbol definition. So to fake out a section name we
269 // use 1, 2 and 3 for section numbers. See below where they are used to
270 // print out fake section names.
271 NType |= MachO::N_SECT;
Davide Italianod4a48532016-02-01 19:22:16 +0000272 if (SymFlags & SymbolRef::SF_Const)
Kevin Enderbydc0dbe12015-11-10 00:31:08 +0000273 NSect = 3;
274 else {
275 IRObjectFile *IRobj = dyn_cast<IRObjectFile>(&Obj);
Davide Italianod4a48532016-02-01 19:22:16 +0000276 NSect = (getSymbolNMTypeChar(*IRobj, I->Sym) == 't') ? 1 : 2;
Kevin Enderbydc0dbe12015-11-10 00:31:08 +0000277 }
278 }
279 if (SymFlags & SymbolRef::SF_Weak)
280 NDesc |= MachO::N_WEAK_DEF;
Kevin Enderby980b2582014-06-05 21:21:57 +0000281 } else {
Kevin Enderbydc0dbe12015-11-10 00:31:08 +0000282 DataRefImpl SymDRI = I->Sym.getRawDataRefImpl();
283 if (MachO->is64Bit()) {
284 H_64 = MachO->MachOObjectFile::getHeader64();
285 Filetype = H_64.filetype;
286 Flags = H_64.flags;
Reid Kleckner3afb80e2015-11-19 00:51:50 +0000287 MachO::nlist_64 STE_64 = MachO->getSymbol64TableEntry(SymDRI);
Kevin Enderbydc0dbe12015-11-10 00:31:08 +0000288 NType = STE_64.n_type;
289 NSect = STE_64.n_sect;
290 NDesc = STE_64.n_desc;
291 NStrx = STE_64.n_strx;
292 NValue = STE_64.n_value;
293 } else {
294 H = MachO->MachOObjectFile::getHeader();
295 Filetype = H.filetype;
296 Flags = H.flags;
Reid Kleckner3afb80e2015-11-19 00:51:50 +0000297 MachO::nlist STE = MachO->getSymbolTableEntry(SymDRI);
Kevin Enderbydc0dbe12015-11-10 00:31:08 +0000298 NType = STE.n_type;
299 NSect = STE.n_sect;
300 NDesc = STE.n_desc;
301 NStrx = STE.n_strx;
302 NValue = STE.n_value;
303 }
Kevin Enderby980b2582014-06-05 21:21:57 +0000304 }
305
Kevin Enderby77b968e2014-07-16 17:38:26 +0000306 // If we are printing Mach-O symbols in hex do that and return.
307 if (FormatMachOasHex) {
308 char Str[18] = "";
Kevin Enderby77b968e2014-07-16 17:38:26 +0000309 format(printFormat, NValue).print(Str, sizeof(Str));
310 outs() << Str << ' ';
311 format("%02x", NType).print(Str, sizeof(Str));
312 outs() << Str << ' ';
313 format("%02x", NSect).print(Str, sizeof(Str));
314 outs() << Str << ' ';
315 format("%04x", NDesc).print(Str, sizeof(Str));
316 outs() << Str << ' ';
317 format("%08x", NStrx).print(Str, sizeof(Str));
318 outs() << Str << ' ';
319 outs() << I->Name << "\n";
320 return;
321 }
322
Kevin Enderby980b2582014-06-05 21:21:57 +0000323 if (PrintAddress) {
324 if ((NType & MachO::N_TYPE) == MachO::N_INDR)
325 strcpy(SymbolAddrStr, printBlanks);
Kevin Enderbydc0dbe12015-11-10 00:31:08 +0000326 if (Obj.isIR() && (NType & MachO::N_TYPE) == MachO::N_TYPE)
327 strcpy(SymbolAddrStr, printDashes);
Kevin Enderby980b2582014-06-05 21:21:57 +0000328 outs() << SymbolAddrStr << ' ';
329 }
330
331 switch (NType & MachO::N_TYPE) {
332 case MachO::N_UNDF:
333 if (NValue != 0) {
334 outs() << "(common) ";
335 if (MachO::GET_COMM_ALIGN(NDesc) != 0)
Kevin Enderby8f6dcf52014-07-01 22:44:51 +0000336 outs() << "(alignment 2^" << (int)MachO::GET_COMM_ALIGN(NDesc) << ") ";
Kevin Enderby980b2582014-06-05 21:21:57 +0000337 } else {
338 if ((NType & MachO::N_TYPE) == MachO::N_PBUD)
339 outs() << "(prebound ";
340 else
341 outs() << "(";
342 if ((NDesc & MachO::REFERENCE_TYPE) ==
343 MachO::REFERENCE_FLAG_UNDEFINED_LAZY)
344 outs() << "undefined [lazy bound]) ";
345 else if ((NDesc & MachO::REFERENCE_TYPE) ==
346 MachO::REFERENCE_FLAG_UNDEFINED_LAZY)
347 outs() << "undefined [private lazy bound]) ";
348 else if ((NDesc & MachO::REFERENCE_TYPE) ==
349 MachO::REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY)
350 outs() << "undefined [private]) ";
351 else
352 outs() << "undefined) ";
353 }
354 break;
355 case MachO::N_ABS:
356 outs() << "(absolute) ";
357 break;
358 case MachO::N_INDR:
359 outs() << "(indirect) ";
360 break;
361 case MachO::N_SECT: {
Kevin Enderbydc0dbe12015-11-10 00:31:08 +0000362 if (Obj.isIR()) {
363 // For llvm bitcode files print out a fake section name using the values
364 // use 1, 2 and 3 for section numbers as set above.
365 if (NSect == 1)
366 outs() << "(LTO,CODE) ";
367 else if (NSect == 2)
368 outs() << "(LTO,DATA) ";
369 else if (NSect == 3)
370 outs() << "(LTO,RODATA) ";
371 else
372 outs() << "(?,?) ";
373 break;
374 }
Kevin Enderby1f472ea2016-01-21 21:13:27 +0000375 ErrorOr<section_iterator> SecOrErr =
376 MachO->getSymbolSection(I->Sym.getRawDataRefImpl());
377 if (SecOrErr.getError()) {
378 outs() << "(?,?) ";
379 break;
380 }
381 section_iterator Sec = *SecOrErr;
Kevin Enderby980b2582014-06-05 21:21:57 +0000382 DataRefImpl Ref = Sec->getRawDataRefImpl();
383 StringRef SectionName;
384 MachO->getSectionName(Ref, SectionName);
385 StringRef SegmentName = MachO->getSectionFinalSegmentName(Ref);
386 outs() << "(" << SegmentName << "," << SectionName << ") ";
387 break;
388 }
389 default:
390 outs() << "(?) ";
391 break;
392 }
393
394 if (NType & MachO::N_EXT) {
395 if (NDesc & MachO::REFERENCED_DYNAMICALLY)
396 outs() << "[referenced dynamically] ";
397 if (NType & MachO::N_PEXT) {
398 if ((NDesc & MachO::N_WEAK_DEF) == MachO::N_WEAK_DEF)
Kevin Enderby8f6dcf52014-07-01 22:44:51 +0000399 outs() << "weak private external ";
Kevin Enderby980b2582014-06-05 21:21:57 +0000400 else
Kevin Enderby8f6dcf52014-07-01 22:44:51 +0000401 outs() << "private external ";
Kevin Enderby980b2582014-06-05 21:21:57 +0000402 } else {
403 if ((NDesc & MachO::N_WEAK_REF) == MachO::N_WEAK_REF ||
Kevin Enderby8f6dcf52014-07-01 22:44:51 +0000404 (NDesc & MachO::N_WEAK_DEF) == MachO::N_WEAK_DEF) {
Kevin Enderby980b2582014-06-05 21:21:57 +0000405 if ((NDesc & (MachO::N_WEAK_REF | MachO::N_WEAK_DEF)) ==
406 (MachO::N_WEAK_REF | MachO::N_WEAK_DEF))
407 outs() << "weak external automatically hidden ";
408 else
409 outs() << "weak external ";
Kevin Enderby8f6dcf52014-07-01 22:44:51 +0000410 } else
Kevin Enderby980b2582014-06-05 21:21:57 +0000411 outs() << "external ";
412 }
413 } else {
414 if (NType & MachO::N_PEXT)
415 outs() << "non-external (was a private external) ";
416 else
417 outs() << "non-external ";
418 }
419
420 if (Filetype == MachO::MH_OBJECT &&
421 (NDesc & MachO::N_NO_DEAD_STRIP) == MachO::N_NO_DEAD_STRIP)
422 outs() << "[no dead strip] ";
423
424 if (Filetype == MachO::MH_OBJECT &&
425 ((NType & MachO::N_TYPE) != MachO::N_UNDF) &&
426 (NDesc & MachO::N_SYMBOL_RESOLVER) == MachO::N_SYMBOL_RESOLVER)
427 outs() << "[symbol resolver] ";
428
429 if (Filetype == MachO::MH_OBJECT &&
430 ((NType & MachO::N_TYPE) != MachO::N_UNDF) &&
431 (NDesc & MachO::N_ALT_ENTRY) == MachO::N_ALT_ENTRY)
432 outs() << "[alt entry] ";
433
434 if ((NDesc & MachO::N_ARM_THUMB_DEF) == MachO::N_ARM_THUMB_DEF)
435 outs() << "[Thumb] ";
436
437 if ((NType & MachO::N_TYPE) == MachO::N_INDR) {
438 outs() << I->Name << " (for ";
439 StringRef IndirectName;
Kevin Enderbydc0dbe12015-11-10 00:31:08 +0000440 if (!MachO ||
441 MachO->getIndirectName(I->Sym.getRawDataRefImpl(), IndirectName))
Kevin Enderby980b2582014-06-05 21:21:57 +0000442 outs() << "?)";
443 else
444 outs() << IndirectName << ")";
Kevin Enderby8f6dcf52014-07-01 22:44:51 +0000445 } else
Kevin Enderby980b2582014-06-05 21:21:57 +0000446 outs() << I->Name;
447
448 if ((Flags & MachO::MH_TWOLEVEL) == MachO::MH_TWOLEVEL &&
Kevin Enderby8f6dcf52014-07-01 22:44:51 +0000449 (((NType & MachO::N_TYPE) == MachO::N_UNDF && NValue == 0) ||
Kevin Enderby980b2582014-06-05 21:21:57 +0000450 (NType & MachO::N_TYPE) == MachO::N_PBUD)) {
451 uint32_t LibraryOrdinal = MachO::GET_LIBRARY_ORDINAL(NDesc);
452 if (LibraryOrdinal != 0) {
453 if (LibraryOrdinal == MachO::EXECUTABLE_ORDINAL)
454 outs() << " (from executable)";
455 else if (LibraryOrdinal == MachO::DYNAMIC_LOOKUP_ORDINAL)
456 outs() << " (dynamically looked up)";
457 else {
458 StringRef LibraryName;
Kevin Enderbydc0dbe12015-11-10 00:31:08 +0000459 if (!MachO ||
460 MachO->getLibraryShortNameByIndex(LibraryOrdinal - 1, LibraryName))
Kevin Enderby8f6dcf52014-07-01 22:44:51 +0000461 outs() << " (from bad library ordinal " << LibraryOrdinal << ")";
Kevin Enderby980b2582014-06-05 21:21:57 +0000462 else
463 outs() << " (from " << LibraryName << ")";
464 }
465 }
466 }
467
468 outs() << "\n";
469}
470
Kevin Enderby407cc212014-07-17 22:47:16 +0000471// Table that maps Darwin's Mach-O stab constants to strings to allow printing.
472struct DarwinStabName {
473 uint8_t NType;
474 const char *Name;
475};
476static const struct DarwinStabName DarwinStabNames[] = {
477 {MachO::N_GSYM, "GSYM"},
478 {MachO::N_FNAME, "FNAME"},
479 {MachO::N_FUN, "FUN"},
480 {MachO::N_STSYM, "STSYM"},
481 {MachO::N_LCSYM, "LCSYM"},
482 {MachO::N_BNSYM, "BNSYM"},
483 {MachO::N_PC, "PC"},
484 {MachO::N_AST, "AST"},
485 {MachO::N_OPT, "OPT"},
486 {MachO::N_RSYM, "RSYM"},
487 {MachO::N_SLINE, "SLINE"},
488 {MachO::N_ENSYM, "ENSYM"},
489 {MachO::N_SSYM, "SSYM"},
490 {MachO::N_SO, "SO"},
491 {MachO::N_OSO, "OSO"},
492 {MachO::N_LSYM, "LSYM"},
493 {MachO::N_BINCL, "BINCL"},
494 {MachO::N_SOL, "SOL"},
495 {MachO::N_PARAMS, "PARAM"},
496 {MachO::N_VERSION, "VERS"},
497 {MachO::N_OLEVEL, "OLEV"},
498 {MachO::N_PSYM, "PSYM"},
499 {MachO::N_EINCL, "EINCL"},
500 {MachO::N_ENTRY, "ENTRY"},
501 {MachO::N_LBRAC, "LBRAC"},
502 {MachO::N_EXCL, "EXCL"},
503 {MachO::N_RBRAC, "RBRAC"},
504 {MachO::N_BCOMM, "BCOMM"},
505 {MachO::N_ECOMM, "ECOMM"},
506 {MachO::N_ECOML, "ECOML"},
507 {MachO::N_LENG, "LENG"},
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000508 {0, nullptr}};
509
Kevin Enderby407cc212014-07-17 22:47:16 +0000510static const char *getDarwinStabString(uint8_t NType) {
511 for (unsigned i = 0; DarwinStabNames[i].Name; i++) {
512 if (DarwinStabNames[i].NType == NType)
513 return DarwinStabNames[i].Name;
514 }
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000515 return nullptr;
Kevin Enderby407cc212014-07-17 22:47:16 +0000516}
517
518// darwinPrintStab() prints the n_sect, n_desc along with a symbolic name of
519// a stab n_type value in a Mach-O file.
520static void darwinPrintStab(MachOObjectFile *MachO, SymbolListT::iterator I) {
521 MachO::nlist_64 STE_64;
522 MachO::nlist STE;
523 uint8_t NType;
524 uint8_t NSect;
525 uint16_t NDesc;
Rafael Espindola2d5d23d2015-07-06 21:36:23 +0000526 DataRefImpl SymDRI = I->Sym.getRawDataRefImpl();
Kevin Enderby407cc212014-07-17 22:47:16 +0000527 if (MachO->is64Bit()) {
Rafael Espindola2d5d23d2015-07-06 21:36:23 +0000528 STE_64 = MachO->getSymbol64TableEntry(SymDRI);
Kevin Enderby407cc212014-07-17 22:47:16 +0000529 NType = STE_64.n_type;
530 NSect = STE_64.n_sect;
531 NDesc = STE_64.n_desc;
532 } else {
Rafael Espindola2d5d23d2015-07-06 21:36:23 +0000533 STE = MachO->getSymbolTableEntry(SymDRI);
Kevin Enderby407cc212014-07-17 22:47:16 +0000534 NType = STE.n_type;
535 NSect = STE.n_sect;
536 NDesc = STE.n_desc;
537 }
538
539 char Str[18] = "";
540 format("%02x", NSect).print(Str, sizeof(Str));
541 outs() << ' ' << Str << ' ';
542 format("%04x", NDesc).print(Str, sizeof(Str));
543 outs() << Str << ' ';
544 if (const char *stabString = getDarwinStabString(NType))
545 format("%5.5s", stabString).print(Str, sizeof(Str));
546 else
547 format(" %02x", NType).print(Str, sizeof(Str));
548 outs() << Str;
549}
550
Rafael Espindolaceb23382014-07-31 21:00:10 +0000551static void sortAndPrintSymbolList(SymbolicFile &Obj, bool printName,
Kevin Enderby08e1bbd2014-07-24 23:31:52 +0000552 std::string ArchiveName,
553 std::string ArchitectureName) {
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000554 if (!NoSort) {
Rafael Espindola63d10d62015-07-06 18:48:47 +0000555 std::function<bool(const NMSymbol &, const NMSymbol &)> Cmp;
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000556 if (NumericSort)
Rafael Espindola63d10d62015-07-06 18:48:47 +0000557 Cmp = compareSymbolAddress;
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000558 else if (SizeSort)
Rafael Espindola63d10d62015-07-06 18:48:47 +0000559 Cmp = compareSymbolSize;
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000560 else
Rafael Espindola63d10d62015-07-06 18:48:47 +0000561 Cmp = compareSymbolName;
562
563 if (ReverseSort)
Rafael Espindolacab82752015-07-06 19:24:40 +0000564 Cmp = [=](const NMSymbol &A, const NMSymbol &B) { return Cmp(B, A); };
Rafael Espindola63d10d62015-07-06 18:48:47 +0000565 std::sort(SymbolList.begin(), SymbolList.end(), Cmp);
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000566 }
567
Kevin Enderby08e1bbd2014-07-24 23:31:52 +0000568 if (!PrintFileName) {
569 if (OutputFormat == posix && MultipleFiles && printName) {
570 outs() << '\n' << CurrentFilename << ":\n";
571 } else if (OutputFormat == bsd && MultipleFiles && printName) {
572 outs() << "\n" << CurrentFilename << ":\n";
573 } else if (OutputFormat == sysv) {
574 outs() << "\n\nSymbols from " << CurrentFilename << ":\n\n"
575 << "Name Value Class Type"
576 << " Size Line Section\n";
577 }
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000578 }
579
Kevin Enderbydc0dbe12015-11-10 00:31:08 +0000580 const char *printBlanks, *printDashes, *printFormat;
Kevin Enderby6abc2e52014-05-09 23:57:49 +0000581 if (isSymbolList64Bit(Obj)) {
582 printBlanks = " ";
Kevin Enderbydc0dbe12015-11-10 00:31:08 +0000583 printDashes = "----------------";
Hemant Kulkarni5e005a12016-02-10 17:51:39 +0000584 switch (AddressRadix) {
585 case Radix::o:
586 printFormat = "%016" PRIo64;
587 break;
588 case Radix::x:
589 printFormat = "%016" PRIx64;
590 break;
591 default:
592 printFormat = "%016" PRId64;
593 }
Kevin Enderby6abc2e52014-05-09 23:57:49 +0000594 } else {
595 printBlanks = " ";
Kevin Enderbydc0dbe12015-11-10 00:31:08 +0000596 printDashes = "--------";
Hemant Kulkarni5e005a12016-02-10 17:51:39 +0000597 switch (AddressRadix) {
598 case Radix::o:
599 printFormat = "%08" PRIo64;
600 break;
601 case Radix::x:
602 printFormat = "%08" PRIx64;
603 break;
604 default:
605 printFormat = "%08" PRId64;
606 }
Kevin Enderby6abc2e52014-05-09 23:57:49 +0000607 }
608
Rafael Espindola619581c2014-01-29 04:56:19 +0000609 for (SymbolListT::iterator I = SymbolList.begin(), E = SymbolList.end();
610 I != E; ++I) {
Rafael Espindola2d5d23d2015-07-06 21:36:23 +0000611 uint32_t SymFlags = I->Sym.getFlags();
612 bool Undefined = SymFlags & SymbolRef::SF_Undefined;
Kevin Enderby1543bdf2015-10-30 19:55:32 +0000613 bool Global = SymFlags & SymbolRef::SF_Global;
Davide Italiano74237152016-01-26 19:57:42 +0000614 if ((!Undefined && UndefinedOnly) || (Undefined && DefinedOnly) ||
615 (!Global && ExternalOnly) || (SizeSort && !PrintAddress))
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000616 continue;
Kevin Enderby08e1bbd2014-07-24 23:31:52 +0000617 if (PrintFileName) {
618 if (!ArchitectureName.empty())
619 outs() << "(for architecture " << ArchitectureName << "):";
620 if (!ArchiveName.empty())
621 outs() << ArchiveName << ":";
622 outs() << CurrentFilename << ": ";
623 }
Kevin Enderbydc0dbe12015-11-10 00:31:08 +0000624 if ((JustSymbolName || (UndefinedOnly && isa<MachOObjectFile>(Obj) &&
625 OutputFormat != darwin)) && OutputFormat != posix) {
Kevin Enderby0fd8aac2014-07-03 21:51:07 +0000626 outs() << I->Name << "\n";
627 continue;
628 }
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000629
Kevin Enderby6abc2e52014-05-09 23:57:49 +0000630 char SymbolAddrStr[18] = "";
631 char SymbolSizeStr[18] = "";
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000632
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +0000633 if (OutputFormat == sysv || I->TypeChar == 'U')
Kevin Enderby6abc2e52014-05-09 23:57:49 +0000634 strcpy(SymbolAddrStr, printBlanks);
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000635 if (OutputFormat == sysv)
Kevin Enderby6abc2e52014-05-09 23:57:49 +0000636 strcpy(SymbolSizeStr, printBlanks);
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000637
Kevin Enderbydc0dbe12015-11-10 00:31:08 +0000638 if (I->TypeChar != 'U') {
639 if (Obj.isIR())
640 strcpy(SymbolAddrStr, printDashes);
641 else
642 format(printFormat, I->Address)
Rafael Espindolab4865d62014-04-03 00:19:35 +0000643 .print(SymbolAddrStr, sizeof(SymbolAddrStr));
Kevin Enderbydc0dbe12015-11-10 00:31:08 +0000644 }
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000645 format(printFormat, I->Size).print(SymbolSizeStr, sizeof(SymbolSizeStr));
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000646
Kevin Enderby77b968e2014-07-16 17:38:26 +0000647 // If OutputFormat is darwin or we are printing Mach-O symbols in hex and
648 // we have a MachOObjectFile, call darwinPrintSymbol to print as darwin's
649 // nm(1) -m output or hex, else if OutputFormat is darwin or we are
650 // printing Mach-O symbols in hex and not a Mach-O object fall back to
651 // OutputFormat bsd (see below).
Rafael Espindolaceb23382014-07-31 21:00:10 +0000652 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj);
Kevin Enderbydc0dbe12015-11-10 00:31:08 +0000653 if ((OutputFormat == darwin || FormatMachOasHex) && (MachO || Obj.isIR())) {
654 darwinPrintSymbol(Obj, I, SymbolAddrStr, printBlanks, printDashes,
655 printFormat);
Kevin Enderby980b2582014-06-05 21:21:57 +0000656 } else if (OutputFormat == posix) {
Kevin Enderbydc0dbe12015-11-10 00:31:08 +0000657 outs() << I->Name << " " << I->TypeChar << " ";
658 if (MachO)
659 outs() << I->Address << " " << "0" /* SymbolSizeStr */ << "\n";
660 else
661 outs() << SymbolAddrStr << SymbolSizeStr << "\n";
Kevin Enderby980b2582014-06-05 21:21:57 +0000662 } else if (OutputFormat == bsd || (OutputFormat == darwin && !MachO)) {
Rafael Espindolab4865d62014-04-03 00:19:35 +0000663 if (PrintAddress)
664 outs() << SymbolAddrStr << ' ';
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000665 if (PrintSize) {
666 outs() << SymbolSizeStr;
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000667 outs() << ' ';
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000668 }
Kevin Enderby407cc212014-07-17 22:47:16 +0000669 outs() << I->TypeChar;
670 if (I->TypeChar == '-' && MachO)
671 darwinPrintStab(MachO, I);
672 outs() << " " << I->Name << "\n";
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000673 } else if (OutputFormat == sysv) {
Rafael Espindola619581c2014-01-29 04:56:19 +0000674 std::string PaddedName(I->Name);
675 while (PaddedName.length() < 20)
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000676 PaddedName += " ";
Rafael Espindolab4865d62014-04-03 00:19:35 +0000677 outs() << PaddedName << "|" << SymbolAddrStr << "| " << I->TypeChar
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000678 << " | |" << SymbolSizeStr << "| |\n";
679 }
680 }
681
682 SymbolList.clear();
683}
684
Rafael Espindolab39953d2015-06-26 13:11:15 +0000685static char getSymbolNMTypeChar(ELFObjectFileBase &Obj,
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000686 basic_symbol_iterator I) {
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000687 // OK, this is ELF
Rafael Espindolab39953d2015-06-26 13:11:15 +0000688 elf_symbol_iterator SymI(I);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000689
Rafael Espindola8bab8892015-08-07 23:27:14 +0000690 ErrorOr<elf_section_iterator> SecIOrErr = SymI->getSection();
691 if (error(SecIOrErr.getError()))
Rafael Espindolab39953d2015-06-26 13:11:15 +0000692 return '?';
Rafael Espindola586af97a2013-11-02 21:16:09 +0000693
Rafael Espindola8bab8892015-08-07 23:27:14 +0000694 elf_section_iterator SecI = *SecIOrErr;
Rafael Espindolab39953d2015-06-26 13:11:15 +0000695 if (SecI != Obj.section_end()) {
696 switch (SecI->getType()) {
Rafael Espindola586af97a2013-11-02 21:16:09 +0000697 case ELF::SHT_PROGBITS:
698 case ELF::SHT_DYNAMIC:
Rafael Espindolab39953d2015-06-26 13:11:15 +0000699 switch (SecI->getFlags()) {
Kevin Enderby8f6dcf52014-07-01 22:44:51 +0000700 case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR):
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000701 return 't';
Kevin Enderby8f6dcf52014-07-01 22:44:51 +0000702 case (ELF::SHF_TLS | ELF::SHF_ALLOC | ELF::SHF_WRITE):
703 case (ELF::SHF_ALLOC | ELF::SHF_WRITE):
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000704 return 'd';
Rafael Espindola586af97a2013-11-02 21:16:09 +0000705 case ELF::SHF_ALLOC:
Kevin Enderby8f6dcf52014-07-01 22:44:51 +0000706 case (ELF::SHF_ALLOC | ELF::SHF_MERGE):
707 case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS):
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000708 return 'r';
Rafael Espindola586af97a2013-11-02 21:16:09 +0000709 }
710 break;
711 case ELF::SHT_NOBITS:
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000712 return 'b';
Rafael Espindola586af97a2013-11-02 21:16:09 +0000713 }
714 }
715
Rafael Espindolab39953d2015-06-26 13:11:15 +0000716 if (SymI->getELFType() == ELF::STT_SECTION) {
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000717 ErrorOr<StringRef> Name = SymI->getName();
718 if (error(Name.getError()))
Rafael Espindola74375892014-02-04 00:21:18 +0000719 return '?';
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000720 return StringSwitch<char>(*Name)
Rafael Espindola74375892014-02-04 00:21:18 +0000721 .StartsWith(".debug", 'N')
722 .StartsWith(".note", 'n')
723 .Default('?');
Rafael Espindola586af97a2013-11-02 21:16:09 +0000724 }
725
Rafael Espindola60c1a8c2015-06-25 16:01:53 +0000726 return 'n';
Rafael Espindola586af97a2013-11-02 21:16:09 +0000727}
728
Rafael Espindola74375892014-02-04 00:21:18 +0000729static char getSymbolNMTypeChar(COFFObjectFile &Obj, symbol_iterator I) {
David Majnemer44f51e52014-09-10 12:51:52 +0000730 COFFSymbolRef Symb = Obj.getCOFFSymbol(*I);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000731 // OK, this is COFF.
732 symbol_iterator SymI(I);
733
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000734 ErrorOr<StringRef> Name = SymI->getName();
735 if (error(Name.getError()))
Rafael Espindola74375892014-02-04 00:21:18 +0000736 return '?';
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000737
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000738 char Ret = StringSwitch<char>(*Name)
Rafael Espindola586af97a2013-11-02 21:16:09 +0000739 .StartsWith(".debug", 'N')
740 .StartsWith(".sxdata", 'N')
741 .Default('?');
742
Rafael Espindola74375892014-02-04 00:21:18 +0000743 if (Ret != '?')
744 return Ret;
Rafael Espindola586af97a2013-11-02 21:16:09 +0000745
746 uint32_t Characteristics = 0;
David Majnemer44f51e52014-09-10 12:51:52 +0000747 if (!COFF::isReservedSectionNumber(Symb.getSectionNumber())) {
Rafael Espindola8bab8892015-08-07 23:27:14 +0000748 ErrorOr<section_iterator> SecIOrErr = SymI->getSection();
749 if (error(SecIOrErr.getError()))
Rafael Espindola74375892014-02-04 00:21:18 +0000750 return '?';
Rafael Espindola8bab8892015-08-07 23:27:14 +0000751 section_iterator SecI = *SecIOrErr;
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000752 const coff_section *Section = Obj.getCOFFSection(*SecI);
Rafael Espindola586af97a2013-11-02 21:16:09 +0000753 Characteristics = Section->Characteristics;
754 }
755
David Majnemer44f51e52014-09-10 12:51:52 +0000756 switch (Symb.getSectionNumber()) {
Rafael Espindola586af97a2013-11-02 21:16:09 +0000757 case COFF::IMAGE_SYM_DEBUG:
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000758 return 'n';
Rafael Espindola586af97a2013-11-02 21:16:09 +0000759 default:
760 // Check section type.
761 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000762 return 't';
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000763 if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
764 return Characteristics & COFF::IMAGE_SCN_MEM_WRITE ? 'd' : 'r';
765 if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000766 return 'b';
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000767 if (Characteristics & COFF::IMAGE_SCN_LNK_INFO)
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000768 return 'i';
Rafael Espindola586af97a2013-11-02 21:16:09 +0000769 // Check for section symbol.
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000770 if (Symb.isSectionDefinition())
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000771 return 's';
Rafael Espindola586af97a2013-11-02 21:16:09 +0000772 }
773
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000774 return '?';
Rafael Espindola586af97a2013-11-02 21:16:09 +0000775}
776
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000777static char getSymbolNMTypeChar(MachOObjectFile &Obj, basic_symbol_iterator I) {
Rafael Espindola586af97a2013-11-02 21:16:09 +0000778 DataRefImpl Symb = I->getRawDataRefImpl();
Davide Italianoba284b62016-02-11 02:56:02 +0000779 uint8_t NType = Obj.is64Bit() ? Obj.getSymbol64TableEntry(Symb).n_type
780 : Obj.getSymbolTableEntry(Symb).n_type;
Rafael Espindola586af97a2013-11-02 21:16:09 +0000781
Kevin Enderby407cc212014-07-17 22:47:16 +0000782 if (NType & MachO::N_STAB)
783 return '-';
784
Rafael Espindola586af97a2013-11-02 21:16:09 +0000785 switch (NType & MachO::N_TYPE) {
Rafael Espindola586af97a2013-11-02 21:16:09 +0000786 case MachO::N_ABS:
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000787 return 's';
Tim Northovereaef0742014-05-30 13:22:59 +0000788 case MachO::N_INDR:
789 return 'i';
Rafael Espindola586af97a2013-11-02 21:16:09 +0000790 case MachO::N_SECT: {
Kevin Enderby1f472ea2016-01-21 21:13:27 +0000791 ErrorOr<section_iterator> SecOrErr = Obj.getSymbolSection(Symb);
792 if (SecOrErr.getError())
793 return 's';
794 section_iterator Sec = *SecOrErr;
Rafael Espindola586af97a2013-11-02 21:16:09 +0000795 DataRefImpl Ref = Sec->getRawDataRefImpl();
796 StringRef SectionName;
797 Obj.getSectionName(Ref, SectionName);
798 StringRef SegmentName = Obj.getSectionFinalSegmentName(Ref);
799 if (SegmentName == "__TEXT" && SectionName == "__text")
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000800 return 't';
Davide Italianoe4db1872016-01-26 19:28:51 +0000801 if (SegmentName == "__DATA" && SectionName == "__data")
Kevin Enderby1e1b9922014-06-19 22:49:21 +0000802 return 'd';
Davide Italianoe4db1872016-01-26 19:28:51 +0000803 if (SegmentName == "__DATA" && SectionName == "__bss")
Kevin Enderby1e1b9922014-06-19 22:49:21 +0000804 return 'b';
Davide Italianoe4db1872016-01-26 19:28:51 +0000805 return 's';
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000806 }
Rafael Espindola586af97a2013-11-02 21:16:09 +0000807 }
808
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000809 return '?';
810}
811
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000812static char getSymbolNMTypeChar(const GlobalValue &GV) {
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000813 // FIXME: should we print 'b'? At the IR level we cannot be sure if this
814 // will be in bss or not, but we could approximate.
Davide Italiano92e6c282016-02-08 23:50:23 +0000815 return GV.getValueType()->isFunctionTy() ? 't' : 'd';
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000816}
817
818static char getSymbolNMTypeChar(IRObjectFile &Obj, basic_symbol_iterator I) {
Rafael Espindola13b69d62014-07-03 18:59:23 +0000819 const GlobalValue *GV = Obj.getSymbolGV(I->getRawDataRefImpl());
Davide Italiano92e6c282016-02-08 23:50:23 +0000820 return !GV ? 't' : getSymbolNMTypeChar(*GV);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000821}
822
Rafael Espindolaceb23382014-07-31 21:00:10 +0000823static bool isObject(SymbolicFile &Obj, basic_symbol_iterator I) {
Davide Italiano92e6c282016-02-08 23:50:23 +0000824 return !dyn_cast<ELFObjectFileBase>(&Obj)
Davide Italiano71c85df2016-02-08 22:58:26 +0000825 ? false
826 : elf_symbol_iterator(I)->getELFType() == ELF::STT_OBJECT;
Rafael Espindola586af97a2013-11-02 21:16:09 +0000827}
828
Rafael Espindolaceb23382014-07-31 21:00:10 +0000829static char getNMTypeChar(SymbolicFile &Obj, basic_symbol_iterator I) {
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000830 uint32_t Symflags = I->getFlags();
831 if ((Symflags & object::SymbolRef::SF_Weak) && !isa<MachOObjectFile>(Obj)) {
832 char Ret = isObject(Obj, I) ? 'v' : 'w';
Davide Italianoa090a002016-02-05 21:10:48 +0000833 return (!(Symflags & object::SymbolRef::SF_Undefined)) ? toupper(Ret) : Ret;
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000834 }
835
836 if (Symflags & object::SymbolRef::SF_Undefined)
837 return 'U';
838
839 if (Symflags & object::SymbolRef::SF_Common)
840 return 'C';
841
842 char Ret = '?';
843 if (Symflags & object::SymbolRef::SF_Absolute)
844 Ret = 'a';
Kevin Enderbydc0dbe12015-11-10 00:31:08 +0000845 else if (IRObjectFile *IR = dyn_cast<IRObjectFile>(&Obj)) {
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000846 Ret = getSymbolNMTypeChar(*IR, I);
Kevin Enderbydc0dbe12015-11-10 00:31:08 +0000847 Triple Host(sys::getDefaultTargetTriple());
Davide Italianoa090a002016-02-05 21:10:48 +0000848 if (Ret == 'd' && Host.isOSDarwin() && Symflags & SymbolRef::SF_Const)
849 Ret = 's';
Kevin Enderbydc0dbe12015-11-10 00:31:08 +0000850 }
Rafael Espindolaceb23382014-07-31 21:00:10 +0000851 else if (COFFObjectFile *COFF = dyn_cast<COFFObjectFile>(&Obj))
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000852 Ret = getSymbolNMTypeChar(*COFF, I);
Rafael Espindolaceb23382014-07-31 21:00:10 +0000853 else if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj))
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000854 Ret = getSymbolNMTypeChar(*MachO, I);
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000855 else
Rafael Espindolab39953d2015-06-26 13:11:15 +0000856 Ret = getSymbolNMTypeChar(cast<ELFObjectFileBase>(Obj), I);
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000857
858 if (Symflags & object::SymbolRef::SF_Global)
859 Ret = toupper(Ret);
860
861 return Ret;
Rafael Espindola586af97a2013-11-02 21:16:09 +0000862}
863
Kevin Enderbyfe6ad972014-07-11 20:30:00 +0000864// getNsectForSegSect() is used to implement the Mach-O "-s segname sectname"
865// option to dump only those symbols from that section in a Mach-O file.
866// It is called once for each Mach-O file from dumpSymbolNamesFromObject()
867// to get the section number for that named section from the command line
868// arguments. It returns the section number for that section in the Mach-O
869// file or zero it is not present.
870static unsigned getNsectForSegSect(MachOObjectFile *Obj) {
871 unsigned Nsect = 1;
Davide Italiano9ee9fb62016-02-10 23:16:17 +0000872 for (auto &S : Obj->sections()) {
873 DataRefImpl Ref = S.getRawDataRefImpl();
Kevin Enderbyfe6ad972014-07-11 20:30:00 +0000874 StringRef SectionName;
875 Obj->getSectionName(Ref, SectionName);
876 StringRef SegmentName = Obj->getSectionFinalSegmentName(Ref);
877 if (SegmentName == SegSect[0] && SectionName == SegSect[1])
878 return Nsect;
879 Nsect++;
880 }
881 return 0;
882}
883
884// getNsectInMachO() is used to implement the Mach-O "-s segname sectname"
885// option to dump only those symbols from that section in a Mach-O file.
886// It is called once for each symbol in a Mach-O file from
887// dumpSymbolNamesFromObject() and returns the section number for that symbol
888// if it is in a section, else it returns 0.
Rafael Espindolab85e10c2015-06-25 15:00:38 +0000889static unsigned getNsectInMachO(MachOObjectFile &Obj, BasicSymbolRef Sym) {
890 DataRefImpl Symb = Sym.getRawDataRefImpl();
Kevin Enderbyfe6ad972014-07-11 20:30:00 +0000891 if (Obj.is64Bit()) {
892 MachO::nlist_64 STE = Obj.getSymbol64TableEntry(Symb);
Davide Italianoba284b62016-02-11 02:56:02 +0000893 return (STE.n_type & MachO::N_TYPE) == MachO::N_SECT ? STE.n_sect : 0;
Kevin Enderbyfe6ad972014-07-11 20:30:00 +0000894 }
895 MachO::nlist STE = Obj.getSymbolTableEntry(Symb);
Davide Italianoba284b62016-02-11 02:56:02 +0000896 return (STE.n_type & MachO::N_TYPE) == MachO::N_SECT ? STE.n_sect : 0;
Kevin Enderbyfe6ad972014-07-11 20:30:00 +0000897}
898
Rafael Espindolaceb23382014-07-31 21:00:10 +0000899static void dumpSymbolNamesFromObject(SymbolicFile &Obj, bool printName,
Kevin Enderby08e1bbd2014-07-24 23:31:52 +0000900 std::string ArchiveName = std::string(),
901 std::string ArchitectureName =
902 std::string()) {
Rafael Espindolab85e10c2015-06-25 15:00:38 +0000903 auto Symbols = Obj.symbols();
Michael J. Spencer8c4729f2012-02-28 00:40:37 +0000904 if (DynamicSyms) {
Rafael Espindola11afad02015-06-25 14:39:35 +0000905 const auto *E = dyn_cast<ELFObjectFileBase>(&Obj);
906 if (!E) {
Rafael Espindolaceb23382014-07-31 21:00:10 +0000907 error("File format has no dynamic symbol table", Obj.getFileName());
Rafael Espindola196666c2014-01-30 20:45:33 +0000908 return;
909 }
Rafael Espindolab85e10c2015-06-25 15:00:38 +0000910 auto DynSymbols = E->getDynamicSymbolIterators();
911 Symbols =
912 make_range<basic_symbol_iterator>(DynSymbols.begin(), DynSymbols.end());
Michael J. Spencer8c4729f2012-02-28 00:40:37 +0000913 }
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000914 std::string NameBuffer;
915 raw_string_ostream OS(NameBuffer);
Kevin Enderbyfe6ad972014-07-11 20:30:00 +0000916 // If a "-s segname sectname" option was specified and this is a Mach-O
917 // file get the section number for that section in this object file.
918 unsigned int Nsect = 0;
Rafael Espindolaceb23382014-07-31 21:00:10 +0000919 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj);
Kevin Enderbyfe6ad972014-07-11 20:30:00 +0000920 if (SegSect.size() != 0 && MachO) {
921 Nsect = getNsectForSegSect(MachO);
922 // If this section is not in the object file no symbols are printed.
923 if (Nsect == 0)
924 return;
925 }
Rafael Espindolab85e10c2015-06-25 15:00:38 +0000926 for (BasicSymbolRef Sym : Symbols) {
927 uint32_t SymFlags = Sym.getFlags();
Rafael Espindola619581c2014-01-29 04:56:19 +0000928 if (!DebugSyms && (SymFlags & SymbolRef::SF_FormatSpecific))
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000929 continue;
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000930 if (WithoutAliases) {
Rafael Espindolaceb23382014-07-31 21:00:10 +0000931 if (IRObjectFile *IR = dyn_cast<IRObjectFile>(&Obj)) {
Rafael Espindolab85e10c2015-06-25 15:00:38 +0000932 const GlobalValue *GV = IR->getSymbolGV(Sym.getRawDataRefImpl());
Rafael Espindola13b69d62014-07-03 18:59:23 +0000933 if (GV && isa<GlobalAlias>(GV))
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000934 continue;
935 }
936 }
Kevin Enderbyfe6ad972014-07-11 20:30:00 +0000937 // If a "-s segname sectname" option was specified and this is a Mach-O
938 // file and this section appears in this file, Nsect will be non-zero then
939 // see if this symbol is a symbol from that section and if not skip it.
Rafael Espindolab85e10c2015-06-25 15:00:38 +0000940 if (Nsect && Nsect != getNsectInMachO(*MachO, Sym))
Kevin Enderbyfe6ad972014-07-11 20:30:00 +0000941 continue;
Rafael Espindola619581c2014-01-29 04:56:19 +0000942 NMSymbol S;
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000943 S.Size = 0;
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +0000944 S.Address = 0;
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000945 if (PrintSize) {
Rafael Espindoladbb6bd32015-06-25 22:10:04 +0000946 if (isa<ELFObjectFileBase>(&Obj))
947 S.Size = ELFSymbolRef(Sym).getSize();
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000948 }
Rafael Espindolab85e10c2015-06-25 15:00:38 +0000949 if (PrintAddress && isa<ObjectFile>(Obj)) {
Rafael Espindola80c33542015-07-06 18:18:44 +0000950 SymbolRef SymRef(Sym);
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +0000951 ErrorOr<uint64_t> AddressOrErr = SymRef.getAddress();
952 if (error(AddressOrErr.getError()))
953 break;
954 S.Address = *AddressOrErr;
Rafael Espindolab85e10c2015-06-25 15:00:38 +0000955 }
956 S.TypeChar = getNMTypeChar(Obj, Sym);
Kevin Enderbyf681ec52016-01-22 18:47:14 +0000957 std::error_code EC = Sym.printName(OS);
958 if (EC && MachO)
959 OS << "bad string index";
Hemant Kulkarni5e005a12016-02-10 17:51:39 +0000960 else
Kevin Enderbyf681ec52016-01-22 18:47:14 +0000961 error(EC);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000962 OS << '\0';
Rafael Espindola2d5d23d2015-07-06 21:36:23 +0000963 S.Sym = Sym;
Rafael Espindola619581c2014-01-29 04:56:19 +0000964 SymbolList.push_back(S);
Michael J. Spencerb8672a52011-01-20 06:38:57 +0000965 }
966
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000967 OS.flush();
968 const char *P = NameBuffer.c_str();
969 for (unsigned I = 0; I < SymbolList.size(); ++I) {
970 SymbolList[I].Name = P;
971 P += strlen(P) + 1;
972 }
973
Rafael Espindolaceb23382014-07-31 21:00:10 +0000974 CurrentFilename = Obj.getFileName();
Kevin Enderby08e1bbd2014-07-24 23:31:52 +0000975 sortAndPrintSymbolList(Obj, printName, ArchiveName, ArchitectureName);
Brian Gaeke0af759d2003-10-16 04:43:15 +0000976}
977
Kevin Enderby4c8dfe42014-06-30 18:45:23 +0000978// checkMachOAndArchFlags() checks to see if the SymbolicFile is a Mach-O file
979// and if it is and there is a list of architecture flags is specified then
980// check to make sure this Mach-O file is one of those architectures or all
981// architectures was specificed. If not then an error is generated and this
982// routine returns false. Else it returns true.
983static bool checkMachOAndArchFlags(SymbolicFile *O, std::string &Filename) {
David Blaikie328c0c12015-03-23 21:17:43 +0000984 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(O);
985
986 if (!MachO || ArchAll || ArchFlags.size() == 0)
987 return true;
988
989 MachO::mach_header H;
990 MachO::mach_header_64 H_64;
991 Triple T;
992 if (MachO->is64Bit()) {
993 H_64 = MachO->MachOObjectFile::getHeader64();
994 T = MachOObjectFile::getArch(H_64.cputype, H_64.cpusubtype);
995 } else {
996 H = MachO->MachOObjectFile::getHeader();
997 T = MachOObjectFile::getArch(H.cputype, H.cpusubtype);
998 }
999 if (std::none_of(
1000 ArchFlags.begin(), ArchFlags.end(),
1001 [&](const std::string &Name) { return Name == T.getArchName(); })) {
1002 error("No architecture specified", Filename);
1003 return false;
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001004 }
1005 return true;
1006}
1007
Rafael Espindola619581c2014-01-29 04:56:19 +00001008static void dumpSymbolNamesFromFile(std::string &Filename) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +00001009 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
1010 MemoryBuffer::getFileOrSTDIN(Filename);
1011 if (error(BufferOrErr.getError(), Filename))
Michael J. Spencerbc96f372011-12-13 23:17:29 +00001012 return;
1013
Owen Anderson19251ec2009-07-15 22:16:10 +00001014 LLVMContext &Context = getGlobalContext();
Peter Collingbourne10039c02014-09-18 21:28:49 +00001015 ErrorOr<std::unique_ptr<Binary>> BinaryOrErr = createBinary(
1016 BufferOrErr.get()->getMemBufferRef(), NoLLVMBitcode ? nullptr : &Context);
Rafael Espindolace82a072014-01-30 19:24:00 +00001017 if (error(BinaryOrErr.getError(), Filename))
1018 return;
Rafael Espindola3f6481d2014-08-01 14:31:55 +00001019 Binary &Bin = *BinaryOrErr.get();
Shankar Easwaran15b28be2012-11-13 18:38:42 +00001020
Rafael Espindola3f6481d2014-08-01 14:31:55 +00001021 if (Archive *A = dyn_cast<Archive>(&Bin)) {
Rafael Espindolace82a072014-01-30 19:24:00 +00001022 if (ArchiveMap) {
Rafael Espindola0115b082014-01-30 21:51:42 +00001023 Archive::symbol_iterator I = A->symbol_begin();
1024 Archive::symbol_iterator E = A->symbol_end();
Rafael Espindolace82a072014-01-30 19:24:00 +00001025 if (I != E) {
Rafael Espindola0115b082014-01-30 21:51:42 +00001026 outs() << "Archive map\n";
Rafael Espindolace82a072014-01-30 19:24:00 +00001027 for (; I != E; ++I) {
Rafael Espindola52151b32015-11-03 01:32:40 +00001028 ErrorOr<Archive::Child> C = I->getMember();
Kevin Enderbyda9dd052015-10-21 17:13:20 +00001029 if (error(C.getError()))
Michael J. Spencer9718f452013-02-03 10:48:50 +00001030 return;
Rafael Espindola52151b32015-11-03 01:32:40 +00001031 ErrorOr<StringRef> FileNameOrErr = C->getName();
Rafael Espindolaae460022014-06-16 16:08:36 +00001032 if (error(FileNameOrErr.getError()))
Rafael Espindolace82a072014-01-30 19:24:00 +00001033 return;
Rafael Espindolaae460022014-06-16 16:08:36 +00001034 StringRef SymName = I->getName();
1035 outs() << SymName << " in " << FileNameOrErr.get() << "\n";
Michael J. Spencer2bc774a2011-09-27 19:37:18 +00001036 }
Rafael Espindolace82a072014-01-30 19:24:00 +00001037 outs() << "\n";
Michael J. Spencer2bc774a2011-09-27 19:37:18 +00001038 }
1039 }
Alexey Samsonove6388e62013-06-18 15:03:28 +00001040
Rafael Espindola0115b082014-01-30 21:51:42 +00001041 for (Archive::child_iterator I = A->child_begin(), E = A->child_end();
Rafael Espindolace82a072014-01-30 19:24:00 +00001042 I != E; ++I) {
Kevin Enderby7a969422015-11-05 19:24:56 +00001043 if (error(I->getError()))
1044 return;
1045 auto &C = I->get();
1046 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary(&Context);
Rafael Espindolaae460022014-06-16 16:08:36 +00001047 if (ChildOrErr.getError())
Rafael Espindolace82a072014-01-30 19:24:00 +00001048 continue;
Rafael Espindolaae460022014-06-16 16:08:36 +00001049 if (SymbolicFile *O = dyn_cast<SymbolicFile>(&*ChildOrErr.get())) {
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001050 if (!checkMachOAndArchFlags(O, Filename))
1051 return;
Kevin Enderby08e1bbd2014-07-24 23:31:52 +00001052 if (!PrintFileName) {
1053 outs() << "\n";
1054 if (isa<MachOObjectFile>(O)) {
1055 outs() << Filename << "(" << O->getFileName() << ")";
1056 } else
1057 outs() << O->getFileName();
1058 outs() << ":\n";
1059 }
Rafael Espindolaceb23382014-07-31 21:00:10 +00001060 dumpSymbolNamesFromObject(*O, false, Filename);
Rafael Espindolace82a072014-01-30 19:24:00 +00001061 }
1062 }
1063 return;
1064 }
Rafael Espindola3f6481d2014-08-01 14:31:55 +00001065 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001066 // If we have a list of architecture flags specified dump only those.
1067 if (!ArchAll && ArchFlags.size() != 0) {
1068 // Look for a slice in the universal binary that matches each ArchFlag.
1069 bool ArchFound;
Kevin Enderby8f6dcf52014-07-01 22:44:51 +00001070 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001071 ArchFound = false;
1072 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1073 E = UB->end_objects();
1074 I != E; ++I) {
Kevin Enderby8f6dcf52014-07-01 22:44:51 +00001075 if (ArchFlags[i] == I->getArchTypeName()) {
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001076 ArchFound = true;
1077 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr =
Kevin Enderby8f6dcf52014-07-01 22:44:51 +00001078 I->getAsObjectFile();
Kevin Enderby08e1bbd2014-07-24 23:31:52 +00001079 std::string ArchiveName;
1080 std::string ArchitectureName;
1081 ArchiveName.clear();
1082 ArchitectureName.clear();
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001083 if (ObjOrErr) {
Rafael Espindola3f6481d2014-08-01 14:31:55 +00001084 ObjectFile &Obj = *ObjOrErr.get();
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001085 if (ArchFlags.size() > 1) {
Kevin Enderby08e1bbd2014-07-24 23:31:52 +00001086 if (PrintFileName)
1087 ArchitectureName = I->getArchTypeName();
1088 else
Rafael Espindola3f6481d2014-08-01 14:31:55 +00001089 outs() << "\n" << Obj.getFileName() << " (for architecture "
Kevin Enderby08e1bbd2014-07-24 23:31:52 +00001090 << I->getArchTypeName() << ")"
1091 << ":\n";
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001092 }
Rafael Espindola3f6481d2014-08-01 14:31:55 +00001093 dumpSymbolNamesFromObject(Obj, false, ArchiveName,
Kevin Enderby08e1bbd2014-07-24 23:31:52 +00001094 ArchitectureName);
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001095 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
1096 I->getAsArchive()) {
1097 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001098 for (Archive::child_iterator AI = A->child_begin(),
1099 AE = A->child_end();
1100 AI != AE; ++AI) {
Kevin Enderby7a969422015-11-05 19:24:56 +00001101 if (error(AI->getError()))
1102 return;
1103 auto &C = AI->get();
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001104 ErrorOr<std::unique_ptr<Binary>> ChildOrErr =
Kevin Enderby7a969422015-11-05 19:24:56 +00001105 C.getAsBinary(&Context);
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001106 if (ChildOrErr.getError())
1107 continue;
Kevin Enderby8f6dcf52014-07-01 22:44:51 +00001108 if (SymbolicFile *O =
1109 dyn_cast<SymbolicFile>(&*ChildOrErr.get())) {
Kevin Enderby08e1bbd2014-07-24 23:31:52 +00001110 if (PrintFileName) {
1111 ArchiveName = A->getFileName();
1112 if (ArchFlags.size() > 1)
1113 ArchitectureName = I->getArchTypeName();
1114 } else {
1115 outs() << "\n" << A->getFileName();
1116 outs() << "(" << O->getFileName() << ")";
1117 if (ArchFlags.size() > 1) {
1118 outs() << " (for architecture " << I->getArchTypeName()
1119 << ")";
1120 }
1121 outs() << ":\n";
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001122 }
Rafael Espindolaceb23382014-07-31 21:00:10 +00001123 dumpSymbolNamesFromObject(*O, false, ArchiveName,
Kevin Enderby08e1bbd2014-07-24 23:31:52 +00001124 ArchitectureName);
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001125 }
1126 }
1127 }
1128 }
1129 }
1130 if (!ArchFound) {
1131 error(ArchFlags[i],
1132 "file: " + Filename + " does not contain architecture");
1133 return;
1134 }
1135 }
1136 return;
1137 }
1138 // No architecture flags were specified so if this contains a slice that
1139 // matches the host architecture dump only that.
1140 if (!ArchAll) {
Kevin Enderby8f6dcf52014-07-01 22:44:51 +00001141 StringRef HostArchName = MachOObjectFile::getHostArch().getArchName();
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001142 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1143 E = UB->end_objects();
1144 I != E; ++I) {
Kevin Enderby8f6dcf52014-07-01 22:44:51 +00001145 if (HostArchName == I->getArchTypeName()) {
1146 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
Kevin Enderby08e1bbd2014-07-24 23:31:52 +00001147 std::string ArchiveName;
1148 ArchiveName.clear();
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001149 if (ObjOrErr) {
Rafael Espindola3f6481d2014-08-01 14:31:55 +00001150 ObjectFile &Obj = *ObjOrErr.get();
1151 dumpSymbolNamesFromObject(Obj, false);
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001152 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
1153 I->getAsArchive()) {
1154 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001155 for (Archive::child_iterator AI = A->child_begin(),
1156 AE = A->child_end();
1157 AI != AE; ++AI) {
Kevin Enderby7a969422015-11-05 19:24:56 +00001158 if (error(AI->getError()))
1159 return;
1160 auto &C = AI->get();
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001161 ErrorOr<std::unique_ptr<Binary>> ChildOrErr =
Kevin Enderby7a969422015-11-05 19:24:56 +00001162 C.getAsBinary(&Context);
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001163 if (ChildOrErr.getError())
1164 continue;
Kevin Enderby8f6dcf52014-07-01 22:44:51 +00001165 if (SymbolicFile *O =
1166 dyn_cast<SymbolicFile>(&*ChildOrErr.get())) {
Kevin Enderby08e1bbd2014-07-24 23:31:52 +00001167 if (PrintFileName)
1168 ArchiveName = A->getFileName();
1169 else
1170 outs() << "\n" << A->getFileName() << "(" << O->getFileName()
1171 << ")"
1172 << ":\n";
Rafael Espindolaceb23382014-07-31 21:00:10 +00001173 dumpSymbolNamesFromObject(*O, false, ArchiveName);
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001174 }
1175 }
1176 }
1177 return;
1178 }
1179 }
1180 }
1181 // Either all architectures have been specified or none have been specified
1182 // and this does not contain the host architecture so dump all the slices.
Kevin Enderby1983fcf2014-06-19 22:03:18 +00001183 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
Rafael Espindola0115b082014-01-30 21:51:42 +00001184 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1185 E = UB->end_objects();
Alexey Samsonove6388e62013-06-18 15:03:28 +00001186 I != E; ++I) {
Rafael Espindola4f7932b2014-06-23 20:41:02 +00001187 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
Kevin Enderby08e1bbd2014-07-24 23:31:52 +00001188 std::string ArchiveName;
1189 std::string ArchitectureName;
1190 ArchiveName.clear();
1191 ArchitectureName.clear();
Rafael Espindola4f7932b2014-06-23 20:41:02 +00001192 if (ObjOrErr) {
Rafael Espindola3f6481d2014-08-01 14:31:55 +00001193 ObjectFile &Obj = *ObjOrErr.get();
Kevin Enderby08e1bbd2014-07-24 23:31:52 +00001194 if (PrintFileName) {
Rafael Espindola3f6481d2014-08-01 14:31:55 +00001195 if (isa<MachOObjectFile>(Obj) && moreThanOneArch)
Kevin Enderby08e1bbd2014-07-24 23:31:52 +00001196 ArchitectureName = I->getArchTypeName();
1197 } else {
1198 if (moreThanOneArch)
1199 outs() << "\n";
Rafael Espindola3f6481d2014-08-01 14:31:55 +00001200 outs() << Obj.getFileName();
1201 if (isa<MachOObjectFile>(Obj) && moreThanOneArch)
Kevin Enderby08e1bbd2014-07-24 23:31:52 +00001202 outs() << " (for architecture " << I->getArchTypeName() << ")";
1203 outs() << ":\n";
1204 }
Rafael Espindola3f6481d2014-08-01 14:31:55 +00001205 dumpSymbolNamesFromObject(Obj, false, ArchiveName, ArchitectureName);
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001206 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = I->getAsArchive()) {
1207 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbye858a652014-05-14 21:18:50 +00001208 for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end();
1209 AI != AE; ++AI) {
Kevin Enderby7a969422015-11-05 19:24:56 +00001210 if (error(AI->getError()))
1211 return;
1212 auto &C = AI->get();
1213 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary(&Context);
Rafael Espindolaae460022014-06-16 16:08:36 +00001214 if (ChildOrErr.getError())
Kevin Enderbye858a652014-05-14 21:18:50 +00001215 continue;
Rafael Espindolaae460022014-06-16 16:08:36 +00001216 if (SymbolicFile *O = dyn_cast<SymbolicFile>(&*ChildOrErr.get())) {
Kevin Enderby08e1bbd2014-07-24 23:31:52 +00001217 if (PrintFileName) {
1218 ArchiveName = A->getFileName();
1219 if (isa<MachOObjectFile>(O) && moreThanOneArch)
1220 ArchitectureName = I->getArchTypeName();
1221 } else {
1222 outs() << "\n" << A->getFileName();
1223 if (isa<MachOObjectFile>(O)) {
1224 outs() << "(" << O->getFileName() << ")";
1225 if (moreThanOneArch)
1226 outs() << " (for architecture " << I->getArchTypeName()
1227 << ")";
1228 } else
1229 outs() << ":" << O->getFileName();
1230 outs() << ":\n";
1231 }
Rafael Espindolaceb23382014-07-31 21:00:10 +00001232 dumpSymbolNamesFromObject(*O, false, ArchiveName, ArchitectureName);
Kevin Enderbye858a652014-05-14 21:18:50 +00001233 }
1234 }
1235 }
Alexey Samsonove6388e62013-06-18 15:03:28 +00001236 }
Brian Gaeke618026a2003-11-19 21:57:30 +00001237 return;
Brian Gaeke0af759d2003-10-16 04:43:15 +00001238 }
Rafael Espindola3f6481d2014-08-01 14:31:55 +00001239 if (SymbolicFile *O = dyn_cast<SymbolicFile>(&Bin)) {
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001240 if (!checkMachOAndArchFlags(O, Filename))
1241 return;
Rafael Espindolaceb23382014-07-31 21:00:10 +00001242 dumpSymbolNamesFromObject(*O, true);
Rafael Espindolace82a072014-01-30 19:24:00 +00001243 }
Brian Gaeke0af759d2003-10-16 04:43:15 +00001244}
1245
1246int main(int argc, char **argv) {
Chris Lattnere3fc2d12009-03-06 05:34:10 +00001247 // Print a stack trace if we signal out.
Chris Lattneref8f3892007-05-06 05:36:18 +00001248 sys::PrintStackTraceOnErrorSignal();
Chris Lattnere3fc2d12009-03-06 05:34:10 +00001249 PrettyStackTraceProgram X(argc, argv);
Michael J. Spencer618d2192010-08-31 06:36:46 +00001250
Rafael Espindola619581c2014-01-29 04:56:19 +00001251 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Chris Lattnere3fc2d12009-03-06 05:34:10 +00001252 cl::ParseCommandLineOptions(argc, argv, "llvm symbol table dumper\n");
Chris Lattner12439ff2004-02-19 20:32:12 +00001253
Michael J. Spencerbc96f372011-12-13 23:17:29 +00001254 // llvm-nm only reads binary files.
Rafael Espindolacb2eca02013-06-12 20:58:35 +00001255 if (error(sys::ChangeStdinToBinary()))
Michael J. Spencerbc96f372011-12-13 23:17:29 +00001256 return 1;
1257
Davide Italiano63634cb2016-01-29 23:38:05 +00001258 // These calls are needed so that we can read bitcode correctly.
Rafael Espindola13b69d62014-07-03 18:59:23 +00001259 llvm::InitializeAllTargetInfos();
1260 llvm::InitializeAllTargetMCs();
1261 llvm::InitializeAllAsmParsers();
1262
Chris Lattneref8f3892007-05-06 05:36:18 +00001263 ToolName = argv[0];
Rafael Espindola619581c2014-01-29 04:56:19 +00001264 if (BSDFormat)
1265 OutputFormat = bsd;
1266 if (POSIXFormat)
1267 OutputFormat = posix;
Kevin Enderby14a96ac2014-06-20 00:04:16 +00001268 if (DarwinFormat)
1269 OutputFormat = darwin;
Chris Lattner4aae1f42003-10-16 18:45:23 +00001270
Michael J. Spencerb8672a52011-01-20 06:38:57 +00001271 // The relative order of these is important. If you pass --size-sort it should
1272 // only print out the size. However, if you pass -S --size-sort, it should
Rafael Espindolab4865d62014-04-03 00:19:35 +00001273 // print out both the size and address.
Rafael Espindola619581c2014-01-29 04:56:19 +00001274 if (SizeSort && !PrintSize)
Rafael Espindolab4865d62014-04-03 00:19:35 +00001275 PrintAddress = false;
Rafael Espindola619581c2014-01-29 04:56:19 +00001276 if (OutputFormat == sysv || SizeSort)
1277 PrintSize = true;
Davide Italianoda570132016-02-05 22:10:42 +00001278 if (InputFilenames.empty())
Kevin Enderby4fc2edb2014-06-23 20:27:53 +00001279 InputFilenames.push_back("a.out");
Davide Italianod5353652016-02-05 22:07:09 +00001280 if (InputFilenames.size() > 1)
Rafael Espindola619581c2014-01-29 04:56:19 +00001281 MultipleFiles = true;
Chris Lattneref8f3892007-05-06 05:36:18 +00001282
Kevin Enderby8f6dcf52014-07-01 22:44:51 +00001283 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001284 if (ArchFlags[i] == "all") {
1285 ArchAll = true;
Kevin Enderby8f6dcf52014-07-01 22:44:51 +00001286 } else {
Rafael Espindola72318b42014-08-08 16:30:17 +00001287 if (!MachOObjectFile::isValidArch(ArchFlags[i]))
Kevin Enderby4c8dfe42014-06-30 18:45:23 +00001288 error("Unknown architecture named '" + ArchFlags[i] + "'",
1289 "for the -arch option");
1290 }
1291 }
1292
Kevin Enderbyfe6ad972014-07-11 20:30:00 +00001293 if (SegSect.size() != 0 && SegSect.size() != 2)
1294 error("bad number of arguments (must be two arguments)",
1295 "for the -s option");
1296
Chris Lattneref8f3892007-05-06 05:36:18 +00001297 std::for_each(InputFilenames.begin(), InputFilenames.end(),
Rafael Espindola619581c2014-01-29 04:56:19 +00001298 dumpSymbolNamesFromFile);
Rafael Espindola8b82a4d2013-07-03 15:46:03 +00001299
1300 if (HadError)
1301 return 1;
1302
Davide Italianoc442e922016-02-10 23:56:18 +00001303 return EXIT_SUCCESS;
Brian Gaeke0af759d2003-10-16 04:43:15 +00001304}