[X86] Various type corrections to the code that creates LOCK_OR32mi8/OR32mi8Locked...
[llvm-core.git] / tools / llvm-nm / llvm-nm.cpp
blobf427f3764972995ad43ca8ecdebeb0106b1aa668
1 //===-- llvm-nm.cpp - Symbol table dumping utility for llvm ---------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This program is a utility that works like traditional Unix "nm", that is, it
10 // prints out the names of symbols in a bitcode or object file, along with some
11 // information about each symbol.
13 // This "nm" supports many of the features of GNU "nm", including its different
14 // output formats.
16 //===----------------------------------------------------------------------===//
18 #include "llvm/ADT/StringSwitch.h"
19 #include "llvm/BinaryFormat/COFF.h"
20 #include "llvm/Demangle/Demangle.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/LLVMContext.h"
23 #include "llvm/Object/Archive.h"
24 #include "llvm/Object/COFF.h"
25 #include "llvm/Object/COFFImportFile.h"
26 #include "llvm/Object/ELFObjectFile.h"
27 #include "llvm/Object/IRObjectFile.h"
28 #include "llvm/Object/MachO.h"
29 #include "llvm/Object/MachOUniversal.h"
30 #include "llvm/Object/ObjectFile.h"
31 #include "llvm/Object/Wasm.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Support/FileSystem.h"
34 #include "llvm/Support/Format.h"
35 #include "llvm/Support/InitLLVM.h"
36 #include "llvm/Support/MemoryBuffer.h"
37 #include "llvm/Support/Program.h"
38 #include "llvm/Support/Signals.h"
39 #include "llvm/Support/TargetSelect.h"
40 #include "llvm/Support/WithColor.h"
41 #include "llvm/Support/raw_ostream.h"
42 #include <vector>
44 using namespace llvm;
45 using namespace object;
47 namespace {
48 enum OutputFormatTy { bsd, sysv, posix, darwin };
50 cl::OptionCategory NMCat("llvm-nm Options");
52 cl::opt<OutputFormatTy> OutputFormat(
53 "format", cl::desc("Specify output format"),
54 cl::values(clEnumVal(bsd, "BSD format"), clEnumVal(sysv, "System V format"),
55 clEnumVal(posix, "POSIX.2 format"),
56 clEnumVal(darwin, "Darwin -m format")),
57 cl::init(bsd), cl::cat(NMCat));
58 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
59 cl::aliasopt(OutputFormat));
61 cl::list<std::string> InputFilenames(cl::Positional, cl::desc("<input files>"),
62 cl::ZeroOrMore);
64 cl::opt<bool> UndefinedOnly("undefined-only",
65 cl::desc("Show only undefined symbols"),
66 cl::cat(NMCat));
67 cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
68 cl::aliasopt(UndefinedOnly), cl::Grouping);
70 cl::opt<bool> DynamicSyms("dynamic",
71 cl::desc("Display the dynamic symbols instead "
72 "of normal symbols."),
73 cl::cat(NMCat));
74 cl::alias DynamicSyms2("D", cl::desc("Alias for --dynamic"),
75 cl::aliasopt(DynamicSyms), cl::Grouping);
77 cl::opt<bool> DefinedOnly("defined-only", cl::desc("Show only defined symbols"),
78 cl::cat(NMCat));
79 cl::alias DefinedOnly2("U", cl::desc("Alias for --defined-only"),
80 cl::aliasopt(DefinedOnly), cl::Grouping);
82 cl::opt<bool> ExternalOnly("extern-only",
83 cl::desc("Show only external symbols"),
84 cl::ZeroOrMore, cl::cat(NMCat));
85 cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
86 cl::aliasopt(ExternalOnly), cl::Grouping,
87 cl::ZeroOrMore);
89 cl::opt<bool> NoWeakSymbols("no-weak", cl::desc("Show only non-weak symbols"),
90 cl::cat(NMCat));
91 cl::alias NoWeakSymbols2("W", cl::desc("Alias for --no-weak"),
92 cl::aliasopt(NoWeakSymbols), cl::Grouping);
94 cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"), cl::Grouping,
95 cl::cat(NMCat));
96 cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"),
97 cl::Grouping, cl::cat(NMCat));
98 cl::alias Portability("portability", cl::desc("Alias for --format=posix"),
99 cl::aliasopt(POSIXFormat), cl::NotHidden);
100 cl::opt<bool> DarwinFormat("m", cl::desc("Alias for --format=darwin"),
101 cl::Grouping, cl::cat(NMCat));
103 static cl::list<std::string>
104 ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
105 cl::ZeroOrMore, cl::cat(NMCat));
106 bool ArchAll = false;
108 cl::opt<bool> PrintFileName(
109 "print-file-name",
110 cl::desc("Precede each symbol with the object file it came from"),
111 cl::cat(NMCat));
113 cl::alias PrintFileNameA("A", cl::desc("Alias for --print-file-name"),
114 cl::aliasopt(PrintFileName), cl::Grouping);
115 cl::alias PrintFileNameo("o", cl::desc("Alias for --print-file-name"),
116 cl::aliasopt(PrintFileName), cl::Grouping);
118 cl::opt<bool> DebugSyms("debug-syms",
119 cl::desc("Show all symbols, even debugger only"),
120 cl::cat(NMCat));
121 cl::alias DebugSymsa("a", cl::desc("Alias for --debug-syms"),
122 cl::aliasopt(DebugSyms), cl::Grouping);
124 cl::opt<bool> NumericSort("numeric-sort", cl::desc("Sort symbols by address"),
125 cl::cat(NMCat));
126 cl::alias NumericSortn("n", cl::desc("Alias for --numeric-sort"),
127 cl::aliasopt(NumericSort), cl::Grouping);
128 cl::alias NumericSortv("v", cl::desc("Alias for --numeric-sort"),
129 cl::aliasopt(NumericSort), cl::Grouping);
131 cl::opt<bool> NoSort("no-sort", cl::desc("Show symbols in order encountered"),
132 cl::cat(NMCat));
133 cl::alias NoSortp("p", cl::desc("Alias for --no-sort"), cl::aliasopt(NoSort),
134 cl::Grouping);
136 cl::opt<bool> Demangle("demangle", cl::ZeroOrMore,
137 cl::desc("Demangle C++ symbol names"), cl::cat(NMCat));
138 cl::alias DemangleC("C", cl::desc("Alias for --demangle"),
139 cl::aliasopt(Demangle), cl::Grouping);
140 cl::opt<bool> NoDemangle("no-demangle", cl::init(false), cl::ZeroOrMore,
141 cl::desc("Don't demangle symbol names"),
142 cl::cat(NMCat));
144 cl::opt<bool> ReverseSort("reverse-sort", cl::desc("Sort in reverse order"),
145 cl::cat(NMCat));
146 cl::alias ReverseSortr("r", cl::desc("Alias for --reverse-sort"),
147 cl::aliasopt(ReverseSort), cl::Grouping);
149 cl::opt<bool> PrintSize("print-size",
150 cl::desc("Show symbol size instead of address"),
151 cl::cat(NMCat));
152 cl::alias PrintSizeS("S", cl::desc("Alias for --print-size"),
153 cl::aliasopt(PrintSize), cl::Grouping);
154 bool MachOPrintSizeWarning = false;
156 cl::opt<bool> SizeSort("size-sort", cl::desc("Sort symbols by size"),
157 cl::cat(NMCat));
159 cl::opt<bool> WithoutAliases("without-aliases", cl::Hidden,
160 cl::desc("Exclude aliases from output"),
161 cl::cat(NMCat));
163 cl::opt<bool> ArchiveMap("print-armap", cl::desc("Print the archive map"),
164 cl::cat(NMCat));
165 cl::alias ArchiveMaps("M", cl::desc("Alias for --print-armap"),
166 cl::aliasopt(ArchiveMap), cl::Grouping);
168 enum Radix { d, o, x };
169 cl::opt<Radix>
170 AddressRadix("radix", cl::desc("Radix (o/d/x) for printing symbol Values"),
171 cl::values(clEnumVal(d, "decimal"), clEnumVal(o, "octal"),
172 clEnumVal(x, "hexadecimal")),
173 cl::init(x), cl::cat(NMCat));
174 cl::alias RadixAlias("t", cl::desc("Alias for --radix"),
175 cl::aliasopt(AddressRadix));
177 cl::opt<bool> JustSymbolName("just-symbol-name",
178 cl::desc("Print just the symbol's name"),
179 cl::cat(NMCat));
180 cl::alias JustSymbolNames("j", cl::desc("Alias for --just-symbol-name"),
181 cl::aliasopt(JustSymbolName), cl::Grouping);
183 cl::opt<bool> SpecialSyms("special-syms",
184 cl::desc("No-op. Used for GNU compatibility only"));
186 // FIXME: This option takes exactly two strings and should be allowed anywhere
187 // on the command line. Such that "llvm-nm -s __TEXT __text foo.o" would work.
188 // But that does not as the CommandLine Library does not have a way to make
189 // this work. For now the "-s __TEXT __text" has to be last on the command
190 // line.
191 cl::list<std::string> SegSect("s", cl::Positional, cl::ZeroOrMore,
192 cl::value_desc("segment section"), cl::Hidden,
193 cl::desc("Dump only symbols from this segment "
194 "and section name, Mach-O only"),
195 cl::cat(NMCat));
197 cl::opt<bool> FormatMachOasHex("x",
198 cl::desc("Print symbol entry in hex, "
199 "Mach-O only"),
200 cl::Grouping, cl::cat(NMCat));
201 cl::opt<bool> AddDyldInfo("add-dyldinfo",
202 cl::desc("Add symbols from the dyldinfo not already "
203 "in the symbol table, Mach-O only"),
204 cl::cat(NMCat));
205 cl::opt<bool> NoDyldInfo("no-dyldinfo",
206 cl::desc("Don't add any symbols from the dyldinfo, "
207 "Mach-O only"),
208 cl::cat(NMCat));
209 cl::opt<bool> DyldInfoOnly("dyldinfo-only",
210 cl::desc("Show only symbols from the dyldinfo, "
211 "Mach-O only"),
212 cl::cat(NMCat));
214 cl::opt<bool> NoLLVMBitcode("no-llvm-bc",
215 cl::desc("Disable LLVM bitcode reader"),
216 cl::cat(NMCat));
218 cl::extrahelp HelpResponse("\nPass @FILE as argument to read options from FILE.\n");
220 bool PrintAddress = true;
222 bool MultipleFiles = false;
224 bool HadError = false;
226 std::string ToolName;
227 } // anonymous namespace
229 static void error(Twine Message, Twine Path = Twine()) {
230 HadError = true;
231 WithColor::error(errs(), ToolName) << Path << ": " << Message << ".\n";
234 static bool error(std::error_code EC, Twine Path = Twine()) {
235 if (EC) {
236 error(EC.message(), Path);
237 return true;
239 return false;
242 // This version of error() prints the archive name and member name, for example:
243 // "libx.a(foo.o)" after the ToolName before the error message. It sets
244 // HadError but returns allowing the code to move on to other archive members.
245 static void error(llvm::Error E, StringRef FileName, const Archive::Child &C,
246 StringRef ArchitectureName = StringRef()) {
247 HadError = true;
248 WithColor::error(errs(), ToolName) << FileName;
250 Expected<StringRef> NameOrErr = C.getName();
251 // TODO: if we have a error getting the name then it would be nice to print
252 // the index of which archive member this is and or its offset in the
253 // archive instead of "???" as the name.
254 if (!NameOrErr) {
255 consumeError(NameOrErr.takeError());
256 errs() << "(" << "???" << ")";
257 } else
258 errs() << "(" << NameOrErr.get() << ")";
260 if (!ArchitectureName.empty())
261 errs() << " (for architecture " << ArchitectureName << ") ";
263 std::string Buf;
264 raw_string_ostream OS(Buf);
265 logAllUnhandledErrors(std::move(E), OS);
266 OS.flush();
267 errs() << " " << Buf << "\n";
270 // This version of error() prints the file name and which architecture slice it
271 // is from, for example: "foo.o (for architecture i386)" after the ToolName
272 // before the error message. It sets HadError but returns allowing the code to
273 // move on to other architecture slices.
274 static void error(llvm::Error E, StringRef FileName,
275 StringRef ArchitectureName = StringRef()) {
276 HadError = true;
277 WithColor::error(errs(), ToolName) << FileName;
279 if (!ArchitectureName.empty())
280 errs() << " (for architecture " << ArchitectureName << ") ";
282 std::string Buf;
283 raw_string_ostream OS(Buf);
284 logAllUnhandledErrors(std::move(E), OS);
285 OS.flush();
286 errs() << " " << Buf << "\n";
289 namespace {
290 struct NMSymbol {
291 uint64_t Address;
292 uint64_t Size;
293 char TypeChar;
294 StringRef Name;
295 StringRef SectionName;
296 StringRef TypeName;
297 BasicSymbolRef Sym;
298 // The Sym field above points to the native symbol in the object file,
299 // for Mach-O when we are creating symbols from the dyld info the above
300 // pointer is null as there is no native symbol. In these cases the fields
301 // below are filled in to represent what would have been a Mach-O nlist
302 // native symbol.
303 uint32_t SymFlags;
304 SectionRef Section;
305 uint8_t NType;
306 uint8_t NSect;
307 uint16_t NDesc;
308 StringRef IndirectName;
310 } // anonymous namespace
312 static bool compareSymbolAddress(const NMSymbol &A, const NMSymbol &B) {
313 bool ADefined;
314 if (A.Sym.getRawDataRefImpl().p)
315 ADefined = !(A.Sym.getFlags() & SymbolRef::SF_Undefined);
316 else
317 ADefined = A.TypeChar != 'U';
318 bool BDefined;
319 if (B.Sym.getRawDataRefImpl().p)
320 BDefined = !(B.Sym.getFlags() & SymbolRef::SF_Undefined);
321 else
322 BDefined = B.TypeChar != 'U';
323 return std::make_tuple(ADefined, A.Address, A.Name, A.Size) <
324 std::make_tuple(BDefined, B.Address, B.Name, B.Size);
327 static bool compareSymbolSize(const NMSymbol &A, const NMSymbol &B) {
328 return std::make_tuple(A.Size, A.Name, A.Address) <
329 std::make_tuple(B.Size, B.Name, B.Address);
332 static bool compareSymbolName(const NMSymbol &A, const NMSymbol &B) {
333 return std::make_tuple(A.Name, A.Size, A.Address) <
334 std::make_tuple(B.Name, B.Size, B.Address);
337 static char isSymbolList64Bit(SymbolicFile &Obj) {
338 if (auto *IRObj = dyn_cast<IRObjectFile>(&Obj))
339 return Triple(IRObj->getTargetTriple()).isArch64Bit();
340 if (isa<COFFObjectFile>(Obj) || isa<COFFImportFile>(Obj))
341 return false;
342 if (isa<WasmObjectFile>(Obj))
343 return false;
344 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj))
345 return MachO->is64Bit();
346 return cast<ELFObjectFileBase>(Obj).getBytesInAddress() == 8;
349 static StringRef CurrentFilename;
350 static std::vector<NMSymbol> SymbolList;
352 static char getSymbolNMTypeChar(IRObjectFile &Obj, basic_symbol_iterator I);
354 // darwinPrintSymbol() is used to print a symbol from a Mach-O file when the
355 // the OutputFormat is darwin or we are printing Mach-O symbols in hex. For
356 // the darwin format it produces the same output as darwin's nm(1) -m output
357 // and when printing Mach-O symbols in hex it produces the same output as
358 // darwin's nm(1) -x format.
359 static void darwinPrintSymbol(SymbolicFile &Obj, const NMSymbol &S,
360 char *SymbolAddrStr, const char *printBlanks,
361 const char *printDashes,
362 const char *printFormat) {
363 MachO::mach_header H;
364 MachO::mach_header_64 H_64;
365 uint32_t Filetype = MachO::MH_OBJECT;
366 uint32_t Flags = 0;
367 uint8_t NType = 0;
368 uint8_t NSect = 0;
369 uint16_t NDesc = 0;
370 uint32_t NStrx = 0;
371 uint64_t NValue = 0;
372 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj);
373 if (Obj.isIR()) {
374 uint32_t SymFlags = S.Sym.getFlags();
375 if (SymFlags & SymbolRef::SF_Global)
376 NType |= MachO::N_EXT;
377 if (SymFlags & SymbolRef::SF_Hidden)
378 NType |= MachO::N_PEXT;
379 if (SymFlags & SymbolRef::SF_Undefined)
380 NType |= MachO::N_EXT | MachO::N_UNDF;
381 else {
382 // Here we have a symbol definition. So to fake out a section name we
383 // use 1, 2 and 3 for section numbers. See below where they are used to
384 // print out fake section names.
385 NType |= MachO::N_SECT;
386 if (SymFlags & SymbolRef::SF_Const)
387 NSect = 3;
388 else if (SymFlags & SymbolRef::SF_Executable)
389 NSect = 1;
390 else
391 NSect = 2;
393 if (SymFlags & SymbolRef::SF_Weak)
394 NDesc |= MachO::N_WEAK_DEF;
395 } else {
396 DataRefImpl SymDRI = S.Sym.getRawDataRefImpl();
397 if (MachO->is64Bit()) {
398 H_64 = MachO->MachOObjectFile::getHeader64();
399 Filetype = H_64.filetype;
400 Flags = H_64.flags;
401 if (SymDRI.p){
402 MachO::nlist_64 STE_64 = MachO->getSymbol64TableEntry(SymDRI);
403 NType = STE_64.n_type;
404 NSect = STE_64.n_sect;
405 NDesc = STE_64.n_desc;
406 NStrx = STE_64.n_strx;
407 NValue = STE_64.n_value;
408 } else {
409 NType = S.NType;
410 NSect = S.NSect;
411 NDesc = S.NDesc;
412 NStrx = 0;
413 NValue = S.Address;
415 } else {
416 H = MachO->MachOObjectFile::getHeader();
417 Filetype = H.filetype;
418 Flags = H.flags;
419 if (SymDRI.p){
420 MachO::nlist STE = MachO->getSymbolTableEntry(SymDRI);
421 NType = STE.n_type;
422 NSect = STE.n_sect;
423 NDesc = STE.n_desc;
424 NStrx = STE.n_strx;
425 NValue = STE.n_value;
426 } else {
427 NType = S.NType;
428 NSect = S.NSect;
429 NDesc = S.NDesc;
430 NStrx = 0;
431 NValue = S.Address;
436 // If we are printing Mach-O symbols in hex do that and return.
437 if (FormatMachOasHex) {
438 outs() << format(printFormat, NValue) << ' '
439 << format("%02x %02x %04x %08x", NType, NSect, NDesc, NStrx) << ' '
440 << S.Name;
441 if ((NType & MachO::N_TYPE) == MachO::N_INDR) {
442 outs() << " (indirect for ";
443 outs() << format(printFormat, NValue) << ' ';
444 StringRef IndirectName;
445 if (S.Sym.getRawDataRefImpl().p) {
446 if (MachO->getIndirectName(S.Sym.getRawDataRefImpl(), IndirectName))
447 outs() << "?)";
448 else
449 outs() << IndirectName << ")";
450 } else
451 outs() << S.IndirectName << ")";
453 outs() << "\n";
454 return;
457 if (PrintAddress) {
458 if ((NType & MachO::N_TYPE) == MachO::N_INDR)
459 strcpy(SymbolAddrStr, printBlanks);
460 if (Obj.isIR() && (NType & MachO::N_TYPE) == MachO::N_TYPE)
461 strcpy(SymbolAddrStr, printDashes);
462 outs() << SymbolAddrStr << ' ';
465 switch (NType & MachO::N_TYPE) {
466 case MachO::N_UNDF:
467 if (NValue != 0) {
468 outs() << "(common) ";
469 if (MachO::GET_COMM_ALIGN(NDesc) != 0)
470 outs() << "(alignment 2^" << (int)MachO::GET_COMM_ALIGN(NDesc) << ") ";
471 } else {
472 if ((NType & MachO::N_TYPE) == MachO::N_PBUD)
473 outs() << "(prebound ";
474 else
475 outs() << "(";
476 if ((NDesc & MachO::REFERENCE_TYPE) ==
477 MachO::REFERENCE_FLAG_UNDEFINED_LAZY)
478 outs() << "undefined [lazy bound]) ";
479 else if ((NDesc & MachO::REFERENCE_TYPE) ==
480 MachO::REFERENCE_FLAG_PRIVATE_UNDEFINED_LAZY)
481 outs() << "undefined [private lazy bound]) ";
482 else if ((NDesc & MachO::REFERENCE_TYPE) ==
483 MachO::REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY)
484 outs() << "undefined [private]) ";
485 else
486 outs() << "undefined) ";
488 break;
489 case MachO::N_ABS:
490 outs() << "(absolute) ";
491 break;
492 case MachO::N_INDR:
493 outs() << "(indirect) ";
494 break;
495 case MachO::N_SECT: {
496 if (Obj.isIR()) {
497 // For llvm bitcode files print out a fake section name using the values
498 // use 1, 2 and 3 for section numbers as set above.
499 if (NSect == 1)
500 outs() << "(LTO,CODE) ";
501 else if (NSect == 2)
502 outs() << "(LTO,DATA) ";
503 else if (NSect == 3)
504 outs() << "(LTO,RODATA) ";
505 else
506 outs() << "(?,?) ";
507 break;
509 section_iterator Sec = SectionRef();
510 if (S.Sym.getRawDataRefImpl().p) {
511 Expected<section_iterator> SecOrErr =
512 MachO->getSymbolSection(S.Sym.getRawDataRefImpl());
513 if (!SecOrErr) {
514 consumeError(SecOrErr.takeError());
515 outs() << "(?,?) ";
516 break;
518 Sec = *SecOrErr;
519 if (Sec == MachO->section_end()) {
520 outs() << "(?,?) ";
521 break;
523 } else {
524 Sec = S.Section;
526 DataRefImpl Ref = Sec->getRawDataRefImpl();
527 StringRef SectionName;
528 if (Expected<StringRef> NameOrErr = MachO->getSectionName(Ref))
529 SectionName = *NameOrErr;
530 StringRef SegmentName = MachO->getSectionFinalSegmentName(Ref);
531 outs() << "(" << SegmentName << "," << SectionName << ") ";
532 break;
534 default:
535 outs() << "(?) ";
536 break;
539 if (NType & MachO::N_EXT) {
540 if (NDesc & MachO::REFERENCED_DYNAMICALLY)
541 outs() << "[referenced dynamically] ";
542 if (NType & MachO::N_PEXT) {
543 if ((NDesc & MachO::N_WEAK_DEF) == MachO::N_WEAK_DEF)
544 outs() << "weak private external ";
545 else
546 outs() << "private external ";
547 } else {
548 if ((NDesc & MachO::N_WEAK_REF) == MachO::N_WEAK_REF ||
549 (NDesc & MachO::N_WEAK_DEF) == MachO::N_WEAK_DEF) {
550 if ((NDesc & (MachO::N_WEAK_REF | MachO::N_WEAK_DEF)) ==
551 (MachO::N_WEAK_REF | MachO::N_WEAK_DEF))
552 outs() << "weak external automatically hidden ";
553 else
554 outs() << "weak external ";
555 } else
556 outs() << "external ";
558 } else {
559 if (NType & MachO::N_PEXT)
560 outs() << "non-external (was a private external) ";
561 else
562 outs() << "non-external ";
565 if (Filetype == MachO::MH_OBJECT) {
566 if (NDesc & MachO::N_NO_DEAD_STRIP)
567 outs() << "[no dead strip] ";
568 if ((NType & MachO::N_TYPE) != MachO::N_UNDF &&
569 NDesc & MachO::N_SYMBOL_RESOLVER)
570 outs() << "[symbol resolver] ";
571 if ((NType & MachO::N_TYPE) != MachO::N_UNDF && NDesc & MachO::N_ALT_ENTRY)
572 outs() << "[alt entry] ";
573 if ((NType & MachO::N_TYPE) != MachO::N_UNDF && NDesc & MachO::N_COLD_FUNC)
574 outs() << "[cold func] ";
577 if ((NDesc & MachO::N_ARM_THUMB_DEF) == MachO::N_ARM_THUMB_DEF)
578 outs() << "[Thumb] ";
580 if ((NType & MachO::N_TYPE) == MachO::N_INDR) {
581 outs() << S.Name << " (for ";
582 StringRef IndirectName;
583 if (MachO) {
584 if (S.Sym.getRawDataRefImpl().p) {
585 if (MachO->getIndirectName(S.Sym.getRawDataRefImpl(), IndirectName))
586 outs() << "?)";
587 else
588 outs() << IndirectName << ")";
589 } else
590 outs() << S.IndirectName << ")";
591 } else
592 outs() << "?)";
593 } else
594 outs() << S.Name;
596 if ((Flags & MachO::MH_TWOLEVEL) == MachO::MH_TWOLEVEL &&
597 (((NType & MachO::N_TYPE) == MachO::N_UNDF && NValue == 0) ||
598 (NType & MachO::N_TYPE) == MachO::N_PBUD)) {
599 uint32_t LibraryOrdinal = MachO::GET_LIBRARY_ORDINAL(NDesc);
600 if (LibraryOrdinal != 0) {
601 if (LibraryOrdinal == MachO::EXECUTABLE_ORDINAL)
602 outs() << " (from executable)";
603 else if (LibraryOrdinal == MachO::DYNAMIC_LOOKUP_ORDINAL)
604 outs() << " (dynamically looked up)";
605 else {
606 StringRef LibraryName;
607 if (!MachO ||
608 MachO->getLibraryShortNameByIndex(LibraryOrdinal - 1, LibraryName))
609 outs() << " (from bad library ordinal " << LibraryOrdinal << ")";
610 else
611 outs() << " (from " << LibraryName << ")";
616 outs() << "\n";
619 // Table that maps Darwin's Mach-O stab constants to strings to allow printing.
620 struct DarwinStabName {
621 uint8_t NType;
622 const char *Name;
624 static const struct DarwinStabName DarwinStabNames[] = {
625 {MachO::N_GSYM, "GSYM"},
626 {MachO::N_FNAME, "FNAME"},
627 {MachO::N_FUN, "FUN"},
628 {MachO::N_STSYM, "STSYM"},
629 {MachO::N_LCSYM, "LCSYM"},
630 {MachO::N_BNSYM, "BNSYM"},
631 {MachO::N_PC, "PC"},
632 {MachO::N_AST, "AST"},
633 {MachO::N_OPT, "OPT"},
634 {MachO::N_RSYM, "RSYM"},
635 {MachO::N_SLINE, "SLINE"},
636 {MachO::N_ENSYM, "ENSYM"},
637 {MachO::N_SSYM, "SSYM"},
638 {MachO::N_SO, "SO"},
639 {MachO::N_OSO, "OSO"},
640 {MachO::N_LSYM, "LSYM"},
641 {MachO::N_BINCL, "BINCL"},
642 {MachO::N_SOL, "SOL"},
643 {MachO::N_PARAMS, "PARAM"},
644 {MachO::N_VERSION, "VERS"},
645 {MachO::N_OLEVEL, "OLEV"},
646 {MachO::N_PSYM, "PSYM"},
647 {MachO::N_EINCL, "EINCL"},
648 {MachO::N_ENTRY, "ENTRY"},
649 {MachO::N_LBRAC, "LBRAC"},
650 {MachO::N_EXCL, "EXCL"},
651 {MachO::N_RBRAC, "RBRAC"},
652 {MachO::N_BCOMM, "BCOMM"},
653 {MachO::N_ECOMM, "ECOMM"},
654 {MachO::N_ECOML, "ECOML"},
655 {MachO::N_LENG, "LENG"},
658 static const char *getDarwinStabString(uint8_t NType) {
659 for (auto I : makeArrayRef(DarwinStabNames))
660 if (I.NType == NType)
661 return I.Name;
662 return nullptr;
665 // darwinPrintStab() prints the n_sect, n_desc along with a symbolic name of
666 // a stab n_type value in a Mach-O file.
667 static void darwinPrintStab(MachOObjectFile *MachO, const NMSymbol &S) {
668 MachO::nlist_64 STE_64;
669 MachO::nlist STE;
670 uint8_t NType;
671 uint8_t NSect;
672 uint16_t NDesc;
673 DataRefImpl SymDRI = S.Sym.getRawDataRefImpl();
674 if (MachO->is64Bit()) {
675 STE_64 = MachO->getSymbol64TableEntry(SymDRI);
676 NType = STE_64.n_type;
677 NSect = STE_64.n_sect;
678 NDesc = STE_64.n_desc;
679 } else {
680 STE = MachO->getSymbolTableEntry(SymDRI);
681 NType = STE.n_type;
682 NSect = STE.n_sect;
683 NDesc = STE.n_desc;
686 outs() << format(" %02x %04x ", NSect, NDesc);
687 if (const char *stabString = getDarwinStabString(NType))
688 outs() << format("%5.5s", stabString);
689 else
690 outs() << format(" %02x", NType);
693 static Optional<std::string> demangle(StringRef Name, bool StripUnderscore) {
694 if (StripUnderscore && !Name.empty() && Name[0] == '_')
695 Name = Name.substr(1);
697 if (!Name.startswith("_Z"))
698 return None;
700 int Status;
701 char *Undecorated =
702 itaniumDemangle(Name.str().c_str(), nullptr, nullptr, &Status);
703 if (Status != 0)
704 return None;
706 std::string S(Undecorated);
707 free(Undecorated);
708 return S;
711 static bool symbolIsDefined(const NMSymbol &Sym) {
712 return Sym.TypeChar != 'U' && Sym.TypeChar != 'w' && Sym.TypeChar != 'v';
715 static void sortAndPrintSymbolList(SymbolicFile &Obj, bool printName,
716 const std::string &ArchiveName,
717 const std::string &ArchitectureName) {
718 if (!NoSort) {
719 std::function<bool(const NMSymbol &, const NMSymbol &)> Cmp;
720 if (NumericSort)
721 Cmp = compareSymbolAddress;
722 else if (SizeSort)
723 Cmp = compareSymbolSize;
724 else
725 Cmp = compareSymbolName;
727 if (ReverseSort)
728 Cmp = [=](const NMSymbol &A, const NMSymbol &B) { return Cmp(B, A); };
729 llvm::sort(SymbolList, Cmp);
732 if (!PrintFileName) {
733 if (OutputFormat == posix && MultipleFiles && printName) {
734 outs() << '\n' << CurrentFilename << ":\n";
735 } else if (OutputFormat == bsd && MultipleFiles && printName) {
736 outs() << "\n" << CurrentFilename << ":\n";
737 } else if (OutputFormat == sysv) {
738 outs() << "\n\nSymbols from " << CurrentFilename << ":\n\n";
739 if (isSymbolList64Bit(Obj))
740 outs() << "Name Value Class Type"
741 << " Size Line Section\n";
742 else
743 outs() << "Name Value Class Type"
744 << " Size Line Section\n";
748 const char *printBlanks, *printDashes, *printFormat;
749 if (isSymbolList64Bit(Obj)) {
750 printBlanks = " ";
751 printDashes = "----------------";
752 switch (AddressRadix) {
753 case Radix::o:
754 printFormat = OutputFormat == posix ? "%" PRIo64 : "%016" PRIo64;
755 break;
756 case Radix::x:
757 printFormat = OutputFormat == posix ? "%" PRIx64 : "%016" PRIx64;
758 break;
759 default:
760 printFormat = OutputFormat == posix ? "%" PRId64 : "%016" PRId64;
762 } else {
763 printBlanks = " ";
764 printDashes = "--------";
765 switch (AddressRadix) {
766 case Radix::o:
767 printFormat = OutputFormat == posix ? "%" PRIo64 : "%08" PRIo64;
768 break;
769 case Radix::x:
770 printFormat = OutputFormat == posix ? "%" PRIx64 : "%08" PRIx64;
771 break;
772 default:
773 printFormat = OutputFormat == posix ? "%" PRId64 : "%08" PRId64;
777 auto writeFileName = [&](raw_ostream &S) {
778 if (!ArchitectureName.empty())
779 S << "(for architecture " << ArchitectureName << "):";
780 if (OutputFormat == posix && !ArchiveName.empty())
781 S << ArchiveName << "[" << CurrentFilename << "]: ";
782 else {
783 if (!ArchiveName.empty())
784 S << ArchiveName << ":";
785 S << CurrentFilename << ": ";
789 if (SymbolList.empty()) {
790 if (PrintFileName)
791 writeFileName(errs());
792 errs() << "no symbols\n";
795 for (const NMSymbol &S : SymbolList) {
796 uint32_t SymFlags;
797 std::string Name = S.Name.str();
798 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj);
799 if (Demangle) {
800 if (Optional<std::string> Opt = demangle(S.Name, MachO))
801 Name = *Opt;
803 if (S.Sym.getRawDataRefImpl().p)
804 SymFlags = S.Sym.getFlags();
805 else
806 SymFlags = S.SymFlags;
808 bool Undefined = SymFlags & SymbolRef::SF_Undefined;
809 bool Global = SymFlags & SymbolRef::SF_Global;
810 bool Weak = SymFlags & SymbolRef::SF_Weak;
811 if ((!Undefined && UndefinedOnly) || (Undefined && DefinedOnly) ||
812 (!Global && ExternalOnly) || (Weak && NoWeakSymbols))
813 continue;
814 if (PrintFileName)
815 writeFileName(outs());
816 if ((JustSymbolName ||
817 (UndefinedOnly && MachO && OutputFormat != darwin)) &&
818 OutputFormat != posix) {
819 outs() << Name << "\n";
820 continue;
823 char SymbolAddrStr[23], SymbolSizeStr[23];
825 // If the format is SysV or the symbol isn't defined, then print spaces.
826 if (OutputFormat == sysv || !symbolIsDefined(S)) {
827 if (OutputFormat == posix) {
828 format(printFormat, S.Address)
829 .print(SymbolAddrStr, sizeof(SymbolAddrStr));
830 format(printFormat, S.Size).print(SymbolSizeStr, sizeof(SymbolSizeStr));
831 } else {
832 strcpy(SymbolAddrStr, printBlanks);
833 strcpy(SymbolSizeStr, printBlanks);
837 if (symbolIsDefined(S)) {
838 // Otherwise, print the symbol address and size.
839 if (Obj.isIR())
840 strcpy(SymbolAddrStr, printDashes);
841 else if (MachO && S.TypeChar == 'I')
842 strcpy(SymbolAddrStr, printBlanks);
843 else
844 format(printFormat, S.Address)
845 .print(SymbolAddrStr, sizeof(SymbolAddrStr));
846 format(printFormat, S.Size).print(SymbolSizeStr, sizeof(SymbolSizeStr));
849 // If OutputFormat is darwin or we are printing Mach-O symbols in hex and
850 // we have a MachOObjectFile, call darwinPrintSymbol to print as darwin's
851 // nm(1) -m output or hex, else if OutputFormat is darwin or we are
852 // printing Mach-O symbols in hex and not a Mach-O object fall back to
853 // OutputFormat bsd (see below).
854 if ((OutputFormat == darwin || FormatMachOasHex) && (MachO || Obj.isIR())) {
855 darwinPrintSymbol(Obj, S, SymbolAddrStr, printBlanks, printDashes,
856 printFormat);
857 } else if (OutputFormat == posix) {
858 outs() << Name << " " << S.TypeChar << " " << SymbolAddrStr << " "
859 << (MachO ? "0" : SymbolSizeStr) << "\n";
860 } else if (OutputFormat == bsd || (OutputFormat == darwin && !MachO)) {
861 if (PrintAddress)
862 outs() << SymbolAddrStr << ' ';
863 if (PrintSize)
864 outs() << SymbolSizeStr << ' ';
865 outs() << S.TypeChar;
866 if (S.TypeChar == '-' && MachO)
867 darwinPrintStab(MachO, S);
868 outs() << " " << Name;
869 if (S.TypeChar == 'I' && MachO) {
870 outs() << " (indirect for ";
871 if (S.Sym.getRawDataRefImpl().p) {
872 StringRef IndirectName;
873 if (MachO->getIndirectName(S.Sym.getRawDataRefImpl(), IndirectName))
874 outs() << "?)";
875 else
876 outs() << IndirectName << ")";
877 } else
878 outs() << S.IndirectName << ")";
880 outs() << "\n";
881 } else if (OutputFormat == sysv) {
882 outs() << left_justify(Name, 20) << "|" << SymbolAddrStr << "| "
883 << S.TypeChar << " |" << right_justify(S.TypeName, 18) << "|"
884 << SymbolSizeStr << "| |" << S.SectionName << "\n";
888 SymbolList.clear();
891 static char getSymbolNMTypeChar(ELFObjectFileBase &Obj,
892 basic_symbol_iterator I) {
893 // OK, this is ELF
894 elf_symbol_iterator SymI(I);
896 Expected<elf_section_iterator> SecIOrErr = SymI->getSection();
897 if (!SecIOrErr) {
898 consumeError(SecIOrErr.takeError());
899 return '?';
902 elf_section_iterator SecI = *SecIOrErr;
903 if (SecI != Obj.section_end()) {
904 uint32_t Type = SecI->getType();
905 uint64_t Flags = SecI->getFlags();
906 if (Flags & ELF::SHF_EXECINSTR)
907 return 't';
908 if (Type == ELF::SHT_NOBITS)
909 return 'b';
910 if (Flags & ELF::SHF_ALLOC)
911 return Flags & ELF::SHF_WRITE ? 'd' : 'r';
914 if (SymI->getELFType() == ELF::STT_SECTION) {
915 Expected<StringRef> Name = SymI->getName();
916 if (!Name) {
917 consumeError(Name.takeError());
918 return '?';
920 return StringSwitch<char>(*Name)
921 .StartsWith(".debug", 'N')
922 .StartsWith(".note", 'n')
923 .StartsWith(".comment", 'n')
924 .Default('?');
927 return 'n';
930 static char getSymbolNMTypeChar(COFFObjectFile &Obj, symbol_iterator I) {
931 COFFSymbolRef Symb = Obj.getCOFFSymbol(*I);
932 // OK, this is COFF.
933 symbol_iterator SymI(I);
935 Expected<StringRef> Name = SymI->getName();
936 if (!Name) {
937 consumeError(Name.takeError());
938 return '?';
941 char Ret = StringSwitch<char>(*Name)
942 .StartsWith(".debug", 'N')
943 .StartsWith(".sxdata", 'N')
944 .Default('?');
946 if (Ret != '?')
947 return Ret;
949 uint32_t Characteristics = 0;
950 if (!COFF::isReservedSectionNumber(Symb.getSectionNumber())) {
951 Expected<section_iterator> SecIOrErr = SymI->getSection();
952 if (!SecIOrErr) {
953 consumeError(SecIOrErr.takeError());
954 return '?';
956 section_iterator SecI = *SecIOrErr;
957 const coff_section *Section = Obj.getCOFFSection(*SecI);
958 Characteristics = Section->Characteristics;
959 if (Expected<StringRef> NameOrErr = Obj.getSectionName(Section))
960 if (NameOrErr->startswith(".idata"))
961 return 'i';
964 switch (Symb.getSectionNumber()) {
965 case COFF::IMAGE_SYM_DEBUG:
966 return 'n';
967 default:
968 // Check section type.
969 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
970 return 't';
971 if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
972 return Characteristics & COFF::IMAGE_SCN_MEM_WRITE ? 'd' : 'r';
973 if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
974 return 'b';
975 if (Characteristics & COFF::IMAGE_SCN_LNK_INFO)
976 return 'i';
977 // Check for section symbol.
978 if (Symb.isSectionDefinition())
979 return 's';
982 return '?';
985 static char getSymbolNMTypeChar(COFFImportFile &Obj) {
986 switch (Obj.getCOFFImportHeader()->getType()) {
987 case COFF::IMPORT_CODE:
988 return 't';
989 case COFF::IMPORT_DATA:
990 return 'd';
991 case COFF::IMPORT_CONST:
992 return 'r';
994 return '?';
997 static char getSymbolNMTypeChar(MachOObjectFile &Obj, basic_symbol_iterator I) {
998 DataRefImpl Symb = I->getRawDataRefImpl();
999 uint8_t NType = Obj.is64Bit() ? Obj.getSymbol64TableEntry(Symb).n_type
1000 : Obj.getSymbolTableEntry(Symb).n_type;
1002 if (NType & MachO::N_STAB)
1003 return '-';
1005 switch (NType & MachO::N_TYPE) {
1006 case MachO::N_ABS:
1007 return 's';
1008 case MachO::N_INDR:
1009 return 'i';
1010 case MachO::N_SECT: {
1011 Expected<section_iterator> SecOrErr = Obj.getSymbolSection(Symb);
1012 if (!SecOrErr) {
1013 consumeError(SecOrErr.takeError());
1014 return 's';
1016 section_iterator Sec = *SecOrErr;
1017 if (Sec == Obj.section_end())
1018 return 's';
1019 DataRefImpl Ref = Sec->getRawDataRefImpl();
1020 StringRef SectionName;
1021 if (Expected<StringRef> NameOrErr = Obj.getSectionName(Ref))
1022 SectionName = *NameOrErr;
1023 StringRef SegmentName = Obj.getSectionFinalSegmentName(Ref);
1024 if (Obj.is64Bit() && Obj.getHeader64().filetype == MachO::MH_KEXT_BUNDLE &&
1025 SegmentName == "__TEXT_EXEC" && SectionName == "__text")
1026 return 't';
1027 if (SegmentName == "__TEXT" && SectionName == "__text")
1028 return 't';
1029 if (SegmentName == "__DATA" && SectionName == "__data")
1030 return 'd';
1031 if (SegmentName == "__DATA" && SectionName == "__bss")
1032 return 'b';
1033 return 's';
1037 return '?';
1040 static char getSymbolNMTypeChar(WasmObjectFile &Obj, basic_symbol_iterator I) {
1041 uint32_t Flags = I->getFlags();
1042 if (Flags & SymbolRef::SF_Executable)
1043 return 't';
1044 return 'd';
1047 static char getSymbolNMTypeChar(IRObjectFile &Obj, basic_symbol_iterator I) {
1048 uint32_t Flags = I->getFlags();
1049 // FIXME: should we print 'b'? At the IR level we cannot be sure if this
1050 // will be in bss or not, but we could approximate.
1051 if (Flags & SymbolRef::SF_Executable)
1052 return 't';
1053 else if (Triple(Obj.getTargetTriple()).isOSDarwin() &&
1054 (Flags & SymbolRef::SF_Const))
1055 return 's';
1056 else
1057 return 'd';
1060 static bool isObject(SymbolicFile &Obj, basic_symbol_iterator I) {
1061 return !dyn_cast<ELFObjectFileBase>(&Obj)
1062 ? false
1063 : elf_symbol_iterator(I)->getELFType() == ELF::STT_OBJECT;
1066 // For ELF object files, Set TypeName to the symbol typename, to be printed
1067 // in the 'Type' column of the SYSV format output.
1068 static StringRef getNMTypeName(SymbolicFile &Obj, basic_symbol_iterator I) {
1069 if (isa<ELFObjectFileBase>(&Obj)) {
1070 elf_symbol_iterator SymI(I);
1071 return SymI->getELFTypeName();
1073 return "";
1076 // Return Posix nm class type tag (single letter), but also set SecName and
1077 // section and name, to be used in format=sysv output.
1078 static char getNMSectionTagAndName(SymbolicFile &Obj, basic_symbol_iterator I,
1079 StringRef &SecName) {
1080 uint32_t Symflags = I->getFlags();
1081 if (isa<ELFObjectFileBase>(&Obj)) {
1082 if (Symflags & object::SymbolRef::SF_Absolute)
1083 SecName = "*ABS*";
1084 else if (Symflags & object::SymbolRef::SF_Common)
1085 SecName = "*COM*";
1086 else if (Symflags & object::SymbolRef::SF_Undefined)
1087 SecName = "*UND*";
1088 else {
1089 elf_symbol_iterator SymI(I);
1090 Expected<elf_section_iterator> SecIOrErr = SymI->getSection();
1091 if (!SecIOrErr) {
1092 consumeError(SecIOrErr.takeError());
1093 return '?';
1095 elf_section_iterator secT = *SecIOrErr;
1096 secT->getName(SecName);
1100 if ((Symflags & object::SymbolRef::SF_Weak) && !isa<MachOObjectFile>(Obj)) {
1101 char Ret = isObject(Obj, I) ? 'v' : 'w';
1102 return (!(Symflags & object::SymbolRef::SF_Undefined)) ? toupper(Ret) : Ret;
1105 if (Symflags & object::SymbolRef::SF_Undefined)
1106 return 'U';
1108 if (Symflags & object::SymbolRef::SF_Common)
1109 return 'C';
1111 char Ret = '?';
1112 if (Symflags & object::SymbolRef::SF_Absolute)
1113 Ret = 'a';
1114 else if (IRObjectFile *IR = dyn_cast<IRObjectFile>(&Obj))
1115 Ret = getSymbolNMTypeChar(*IR, I);
1116 else if (COFFObjectFile *COFF = dyn_cast<COFFObjectFile>(&Obj))
1117 Ret = getSymbolNMTypeChar(*COFF, I);
1118 else if (COFFImportFile *COFFImport = dyn_cast<COFFImportFile>(&Obj))
1119 Ret = getSymbolNMTypeChar(*COFFImport);
1120 else if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj))
1121 Ret = getSymbolNMTypeChar(*MachO, I);
1122 else if (WasmObjectFile *Wasm = dyn_cast<WasmObjectFile>(&Obj))
1123 Ret = getSymbolNMTypeChar(*Wasm, I);
1124 else
1125 Ret = getSymbolNMTypeChar(cast<ELFObjectFileBase>(Obj), I);
1127 if (Symflags & object::SymbolRef::SF_Global)
1128 Ret = toupper(Ret);
1130 return Ret;
1133 // getNsectForSegSect() is used to implement the Mach-O "-s segname sectname"
1134 // option to dump only those symbols from that section in a Mach-O file.
1135 // It is called once for each Mach-O file from dumpSymbolNamesFromObject()
1136 // to get the section number for that named section from the command line
1137 // arguments. It returns the section number for that section in the Mach-O
1138 // file or zero it is not present.
1139 static unsigned getNsectForSegSect(MachOObjectFile *Obj) {
1140 unsigned Nsect = 1;
1141 for (auto &S : Obj->sections()) {
1142 DataRefImpl Ref = S.getRawDataRefImpl();
1143 StringRef SectionName;
1144 if (Expected<StringRef> NameOrErr = Obj->getSectionName(Ref))
1145 SectionName = *NameOrErr;
1146 StringRef SegmentName = Obj->getSectionFinalSegmentName(Ref);
1147 if (SegmentName == SegSect[0] && SectionName == SegSect[1])
1148 return Nsect;
1149 Nsect++;
1151 return 0;
1154 // getNsectInMachO() is used to implement the Mach-O "-s segname sectname"
1155 // option to dump only those symbols from that section in a Mach-O file.
1156 // It is called once for each symbol in a Mach-O file from
1157 // dumpSymbolNamesFromObject() and returns the section number for that symbol
1158 // if it is in a section, else it returns 0.
1159 static unsigned getNsectInMachO(MachOObjectFile &Obj, BasicSymbolRef Sym) {
1160 DataRefImpl Symb = Sym.getRawDataRefImpl();
1161 if (Obj.is64Bit()) {
1162 MachO::nlist_64 STE = Obj.getSymbol64TableEntry(Symb);
1163 return (STE.n_type & MachO::N_TYPE) == MachO::N_SECT ? STE.n_sect : 0;
1165 MachO::nlist STE = Obj.getSymbolTableEntry(Symb);
1166 return (STE.n_type & MachO::N_TYPE) == MachO::N_SECT ? STE.n_sect : 0;
1169 static void
1170 dumpSymbolNamesFromObject(SymbolicFile &Obj, bool printName,
1171 const std::string &ArchiveName = std::string(),
1172 const std::string &ArchitectureName = std::string()) {
1173 auto Symbols = Obj.symbols();
1174 if (DynamicSyms) {
1175 const auto *E = dyn_cast<ELFObjectFileBase>(&Obj);
1176 if (!E) {
1177 error("File format has no dynamic symbol table", Obj.getFileName());
1178 return;
1180 Symbols = E->getDynamicSymbolIterators();
1182 std::string NameBuffer;
1183 raw_string_ostream OS(NameBuffer);
1184 // If a "-s segname sectname" option was specified and this is a Mach-O
1185 // file get the section number for that section in this object file.
1186 unsigned int Nsect = 0;
1187 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj);
1188 if (!SegSect.empty() && MachO) {
1189 Nsect = getNsectForSegSect(MachO);
1190 // If this section is not in the object file no symbols are printed.
1191 if (Nsect == 0)
1192 return;
1194 if (!MachO || !DyldInfoOnly) {
1195 for (BasicSymbolRef Sym : Symbols) {
1196 uint32_t SymFlags = Sym.getFlags();
1197 if (!DebugSyms && (SymFlags & SymbolRef::SF_FormatSpecific))
1198 continue;
1199 if (WithoutAliases && (SymFlags & SymbolRef::SF_Indirect))
1200 continue;
1201 // If a "-s segname sectname" option was specified and this is a Mach-O
1202 // file and this section appears in this file, Nsect will be non-zero then
1203 // see if this symbol is a symbol from that section and if not skip it.
1204 if (Nsect && Nsect != getNsectInMachO(*MachO, Sym))
1205 continue;
1206 NMSymbol S = {};
1207 S.Size = 0;
1208 S.Address = 0;
1209 if (isa<ELFObjectFileBase>(&Obj))
1210 S.Size = ELFSymbolRef(Sym).getSize();
1211 if (PrintAddress && isa<ObjectFile>(Obj)) {
1212 SymbolRef SymRef(Sym);
1213 Expected<uint64_t> AddressOrErr = SymRef.getAddress();
1214 if (!AddressOrErr) {
1215 consumeError(AddressOrErr.takeError());
1216 break;
1218 S.Address = *AddressOrErr;
1220 S.TypeName = getNMTypeName(Obj, Sym);
1221 S.TypeChar = getNMSectionTagAndName(Obj, Sym, S.SectionName);
1222 if (Error E = Sym.printName(OS)) {
1223 if (MachO) {
1224 OS << "bad string index";
1225 consumeError(std::move(E));
1226 } else
1227 error(std::move(E), Obj.getFileName());
1229 OS << '\0';
1230 S.Sym = Sym;
1231 SymbolList.push_back(S);
1235 OS.flush();
1236 const char *P = NameBuffer.c_str();
1237 unsigned I;
1238 for (I = 0; I < SymbolList.size(); ++I) {
1239 SymbolList[I].Name = P;
1240 P += strlen(P) + 1;
1243 // If this is a Mach-O file where the nlist symbol table is out of sync
1244 // with the dyld export trie then look through exports and fake up symbols
1245 // for the ones that are missing (also done with the -add-dyldinfo flag).
1246 // This is needed if strip(1) -T is run on a binary containing swift
1247 // language symbols for example. The option -only-dyldinfo will fake up
1248 // all symbols from the dyld export trie as well as the bind info.
1249 std::string ExportsNameBuffer;
1250 raw_string_ostream EOS(ExportsNameBuffer);
1251 std::string BindsNameBuffer;
1252 raw_string_ostream BOS(BindsNameBuffer);
1253 std::string LazysNameBuffer;
1254 raw_string_ostream LOS(LazysNameBuffer);
1255 std::string WeaksNameBuffer;
1256 raw_string_ostream WOS(WeaksNameBuffer);
1257 std::string FunctionStartsNameBuffer;
1258 raw_string_ostream FOS(FunctionStartsNameBuffer);
1259 if (MachO && !NoDyldInfo) {
1260 MachO::mach_header H;
1261 MachO::mach_header_64 H_64;
1262 uint32_t HFlags = 0;
1263 if (MachO->is64Bit()) {
1264 H_64 = MachO->MachOObjectFile::getHeader64();
1265 HFlags = H_64.flags;
1266 } else {
1267 H = MachO->MachOObjectFile::getHeader();
1268 HFlags = H.flags;
1270 uint64_t BaseSegmentAddress = 0;
1271 for (const auto &Command : MachO->load_commands()) {
1272 if (Command.C.cmd == MachO::LC_SEGMENT) {
1273 MachO::segment_command Seg = MachO->getSegmentLoadCommand(Command);
1274 if (Seg.fileoff == 0 && Seg.filesize != 0) {
1275 BaseSegmentAddress = Seg.vmaddr;
1276 break;
1278 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
1279 MachO::segment_command_64 Seg = MachO->getSegment64LoadCommand(Command);
1280 if (Seg.fileoff == 0 && Seg.filesize != 0) {
1281 BaseSegmentAddress = Seg.vmaddr;
1282 break;
1286 if (DyldInfoOnly || AddDyldInfo ||
1287 HFlags & MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO) {
1288 unsigned ExportsAdded = 0;
1289 Error Err = Error::success();
1290 for (const llvm::object::ExportEntry &Entry : MachO->exports(Err)) {
1291 bool found = false;
1292 bool ReExport = false;
1293 if (!DyldInfoOnly) {
1294 for (const NMSymbol &S : SymbolList)
1295 if (S.Address == Entry.address() + BaseSegmentAddress &&
1296 S.Name == Entry.name()) {
1297 found = true;
1298 break;
1301 if (!found) {
1302 NMSymbol S = {};
1303 S.Address = Entry.address() + BaseSegmentAddress;
1304 S.Size = 0;
1305 S.TypeChar = '\0';
1306 S.Name = Entry.name();
1307 // There is no symbol in the nlist symbol table for this so we set
1308 // Sym effectivly to null and the rest of code in here must test for
1309 // it and not do things like Sym.getFlags() for it.
1310 S.Sym = BasicSymbolRef();
1311 S.SymFlags = SymbolRef::SF_Global;
1312 S.Section = SectionRef();
1313 S.NType = 0;
1314 S.NSect = 0;
1315 S.NDesc = 0;
1316 S.IndirectName = StringRef();
1318 uint64_t EFlags = Entry.flags();
1319 bool Abs = ((EFlags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
1320 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
1321 bool Resolver = (EFlags &
1322 MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
1323 ReExport = (EFlags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
1324 bool WeakDef = (EFlags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
1325 if (WeakDef)
1326 S.NDesc |= MachO::N_WEAK_DEF;
1327 if (Abs) {
1328 S.NType = MachO::N_EXT | MachO::N_ABS;
1329 S.TypeChar = 'A';
1330 } else if (ReExport) {
1331 S.NType = MachO::N_EXT | MachO::N_INDR;
1332 S.TypeChar = 'I';
1333 } else {
1334 S.NType = MachO::N_EXT | MachO::N_SECT;
1335 if (Resolver) {
1336 S.Address = Entry.other() + BaseSegmentAddress;
1337 if ((S.Address & 1) != 0 &&
1338 !MachO->is64Bit() && H.cputype == MachO::CPU_TYPE_ARM){
1339 S.Address &= ~1LL;
1340 S.NDesc |= MachO::N_ARM_THUMB_DEF;
1342 } else {
1343 S.Address = Entry.address() + BaseSegmentAddress;
1345 StringRef SegmentName = StringRef();
1346 StringRef SectionName = StringRef();
1347 for (const SectionRef &Section : MachO->sections()) {
1348 S.NSect++;
1349 Section.getName(SectionName);
1350 SegmentName = MachO->getSectionFinalSegmentName(
1351 Section.getRawDataRefImpl());
1352 if (S.Address >= Section.getAddress() &&
1353 S.Address < Section.getAddress() + Section.getSize()) {
1354 S.Section = Section;
1355 break;
1356 } else if (Entry.name() == "__mh_execute_header" &&
1357 SegmentName == "__TEXT" && SectionName == "__text") {
1358 S.Section = Section;
1359 S.NDesc |= MachO::REFERENCED_DYNAMICALLY;
1360 break;
1363 if (SegmentName == "__TEXT" && SectionName == "__text")
1364 S.TypeChar = 'T';
1365 else if (SegmentName == "__DATA" && SectionName == "__data")
1366 S.TypeChar = 'D';
1367 else if (SegmentName == "__DATA" && SectionName == "__bss")
1368 S.TypeChar = 'B';
1369 else
1370 S.TypeChar = 'S';
1372 SymbolList.push_back(S);
1374 EOS << Entry.name();
1375 EOS << '\0';
1376 ExportsAdded++;
1378 // For ReExports there are a two more things to do, first add the
1379 // indirect name and second create the undefined symbol using the
1380 // referened dynamic library.
1381 if (ReExport) {
1383 // Add the indirect name.
1384 if (Entry.otherName().empty())
1385 EOS << Entry.name();
1386 else
1387 EOS << Entry.otherName();
1388 EOS << '\0';
1390 // Now create the undefined symbol using the referened dynamic
1391 // library.
1392 NMSymbol U = {};
1393 U.Address = 0;
1394 U.Size = 0;
1395 U.TypeChar = 'U';
1396 if (Entry.otherName().empty())
1397 U.Name = Entry.name();
1398 else
1399 U.Name = Entry.otherName();
1400 // Again there is no symbol in the nlist symbol table for this so
1401 // we set Sym effectivly to null and the rest of code in here must
1402 // test for it and not do things like Sym.getFlags() for it.
1403 U.Sym = BasicSymbolRef();
1404 U.SymFlags = SymbolRef::SF_Global | SymbolRef::SF_Undefined;
1405 U.Section = SectionRef();
1406 U.NType = MachO::N_EXT | MachO::N_UNDF;
1407 U.NSect = 0;
1408 U.NDesc = 0;
1409 // The library ordinal for this undefined symbol is in the export
1410 // trie Entry.other().
1411 MachO::SET_LIBRARY_ORDINAL(U.NDesc, Entry.other());
1412 U.IndirectName = StringRef();
1413 SymbolList.push_back(U);
1415 // Finally add the undefined symbol's name.
1416 if (Entry.otherName().empty())
1417 EOS << Entry.name();
1418 else
1419 EOS << Entry.otherName();
1420 EOS << '\0';
1421 ExportsAdded++;
1425 if (Err)
1426 error(std::move(Err), MachO->getFileName());
1427 // Set the symbol names and indirect names for the added symbols.
1428 if (ExportsAdded) {
1429 EOS.flush();
1430 const char *Q = ExportsNameBuffer.c_str();
1431 for (unsigned K = 0; K < ExportsAdded; K++) {
1432 SymbolList[I].Name = Q;
1433 Q += strlen(Q) + 1;
1434 if (SymbolList[I].TypeChar == 'I') {
1435 SymbolList[I].IndirectName = Q;
1436 Q += strlen(Q) + 1;
1438 I++;
1442 // Add the undefined symbols from the bind entries.
1443 unsigned BindsAdded = 0;
1444 Error BErr = Error::success();
1445 StringRef LastSymbolName = StringRef();
1446 for (const llvm::object::MachOBindEntry &Entry : MachO->bindTable(BErr)) {
1447 bool found = false;
1448 if (LastSymbolName == Entry.symbolName())
1449 found = true;
1450 else if(!DyldInfoOnly) {
1451 for (unsigned J = 0; J < SymbolList.size() && !found; ++J) {
1452 if (SymbolList[J].Name == Entry.symbolName())
1453 found = true;
1456 if (!found) {
1457 LastSymbolName = Entry.symbolName();
1458 NMSymbol B = {};
1459 B.Address = 0;
1460 B.Size = 0;
1461 B.TypeChar = 'U';
1462 // There is no symbol in the nlist symbol table for this so we set
1463 // Sym effectivly to null and the rest of code in here must test for
1464 // it and not do things like Sym.getFlags() for it.
1465 B.Sym = BasicSymbolRef();
1466 B.SymFlags = SymbolRef::SF_Global | SymbolRef::SF_Undefined;
1467 B.NType = MachO::N_EXT | MachO::N_UNDF;
1468 B.NSect = 0;
1469 B.NDesc = 0;
1470 MachO::SET_LIBRARY_ORDINAL(B.NDesc, Entry.ordinal());
1471 B.IndirectName = StringRef();
1472 B.Name = Entry.symbolName();
1473 SymbolList.push_back(B);
1474 BOS << Entry.symbolName();
1475 BOS << '\0';
1476 BindsAdded++;
1479 if (BErr)
1480 error(std::move(BErr), MachO->getFileName());
1481 // Set the symbol names and indirect names for the added symbols.
1482 if (BindsAdded) {
1483 BOS.flush();
1484 const char *Q = BindsNameBuffer.c_str();
1485 for (unsigned K = 0; K < BindsAdded; K++) {
1486 SymbolList[I].Name = Q;
1487 Q += strlen(Q) + 1;
1488 if (SymbolList[I].TypeChar == 'I') {
1489 SymbolList[I].IndirectName = Q;
1490 Q += strlen(Q) + 1;
1492 I++;
1496 // Add the undefined symbols from the lazy bind entries.
1497 unsigned LazysAdded = 0;
1498 Error LErr = Error::success();
1499 LastSymbolName = StringRef();
1500 for (const llvm::object::MachOBindEntry &Entry :
1501 MachO->lazyBindTable(LErr)) {
1502 bool found = false;
1503 if (LastSymbolName == Entry.symbolName())
1504 found = true;
1505 else {
1506 // Here we must check to see it this symbol is already in the
1507 // SymbolList as it might have already have been added above via a
1508 // non-lazy (bind) entry.
1509 for (unsigned J = 0; J < SymbolList.size() && !found; ++J) {
1510 if (SymbolList[J].Name == Entry.symbolName())
1511 found = true;
1514 if (!found) {
1515 LastSymbolName = Entry.symbolName();
1516 NMSymbol L = {};
1517 L.Name = Entry.symbolName();
1518 L.Address = 0;
1519 L.Size = 0;
1520 L.TypeChar = 'U';
1521 // There is no symbol in the nlist symbol table for this so we set
1522 // Sym effectivly to null and the rest of code in here must test for
1523 // it and not do things like Sym.getFlags() for it.
1524 L.Sym = BasicSymbolRef();
1525 L.SymFlags = SymbolRef::SF_Global | SymbolRef::SF_Undefined;
1526 L.NType = MachO::N_EXT | MachO::N_UNDF;
1527 L.NSect = 0;
1528 // The REFERENCE_FLAG_UNDEFINED_LAZY is no longer used but here it
1529 // makes sence since we are creating this from a lazy bind entry.
1530 L.NDesc = MachO::REFERENCE_FLAG_UNDEFINED_LAZY;
1531 MachO::SET_LIBRARY_ORDINAL(L.NDesc, Entry.ordinal());
1532 L.IndirectName = StringRef();
1533 SymbolList.push_back(L);
1534 LOS << Entry.symbolName();
1535 LOS << '\0';
1536 LazysAdded++;
1539 if (LErr)
1540 error(std::move(LErr), MachO->getFileName());
1541 // Set the symbol names and indirect names for the added symbols.
1542 if (LazysAdded) {
1543 LOS.flush();
1544 const char *Q = LazysNameBuffer.c_str();
1545 for (unsigned K = 0; K < LazysAdded; K++) {
1546 SymbolList[I].Name = Q;
1547 Q += strlen(Q) + 1;
1548 if (SymbolList[I].TypeChar == 'I') {
1549 SymbolList[I].IndirectName = Q;
1550 Q += strlen(Q) + 1;
1552 I++;
1556 // Add the undefineds symbol from the weak bind entries which are not
1557 // strong symbols.
1558 unsigned WeaksAdded = 0;
1559 Error WErr = Error::success();
1560 LastSymbolName = StringRef();
1561 for (const llvm::object::MachOBindEntry &Entry :
1562 MachO->weakBindTable(WErr)) {
1563 bool found = false;
1564 unsigned J = 0;
1565 if (LastSymbolName == Entry.symbolName() ||
1566 Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
1567 found = true;
1568 } else {
1569 for (J = 0; J < SymbolList.size() && !found; ++J) {
1570 if (SymbolList[J].Name == Entry.symbolName()) {
1571 found = true;
1572 break;
1576 if (!found) {
1577 LastSymbolName = Entry.symbolName();
1578 NMSymbol W;
1579 memset(&W, '\0', sizeof(NMSymbol));
1580 W.Name = Entry.symbolName();
1581 W.Address = 0;
1582 W.Size = 0;
1583 W.TypeChar = 'U';
1584 // There is no symbol in the nlist symbol table for this so we set
1585 // Sym effectivly to null and the rest of code in here must test for
1586 // it and not do things like Sym.getFlags() for it.
1587 W.Sym = BasicSymbolRef();
1588 W.SymFlags = SymbolRef::SF_Global | SymbolRef::SF_Undefined;
1589 W.NType = MachO::N_EXT | MachO::N_UNDF;
1590 W.NSect = 0;
1591 // Odd that we are using N_WEAK_DEF on an undefined symbol but that is
1592 // what is created in this case by the linker when there are real
1593 // symbols in the nlist structs.
1594 W.NDesc = MachO::N_WEAK_DEF;
1595 W.IndirectName = StringRef();
1596 SymbolList.push_back(W);
1597 WOS << Entry.symbolName();
1598 WOS << '\0';
1599 WeaksAdded++;
1600 } else {
1601 // This is the case the symbol was previously been found and it could
1602 // have been added from a bind or lazy bind symbol. If so and not
1603 // a definition also mark it as weak.
1604 if (SymbolList[J].TypeChar == 'U')
1605 // See comment above about N_WEAK_DEF.
1606 SymbolList[J].NDesc |= MachO::N_WEAK_DEF;
1609 if (WErr)
1610 error(std::move(WErr), MachO->getFileName());
1611 // Set the symbol names and indirect names for the added symbols.
1612 if (WeaksAdded) {
1613 WOS.flush();
1614 const char *Q = WeaksNameBuffer.c_str();
1615 for (unsigned K = 0; K < WeaksAdded; K++) {
1616 SymbolList[I].Name = Q;
1617 Q += strlen(Q) + 1;
1618 if (SymbolList[I].TypeChar == 'I') {
1619 SymbolList[I].IndirectName = Q;
1620 Q += strlen(Q) + 1;
1622 I++;
1626 // Trying adding symbol from the function starts table and LC_MAIN entry
1627 // point.
1628 SmallVector<uint64_t, 8> FoundFns;
1629 uint64_t lc_main_offset = UINT64_MAX;
1630 for (const auto &Command : MachO->load_commands()) {
1631 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
1632 // We found a function starts segment, parse the addresses for
1633 // consumption.
1634 MachO::linkedit_data_command LLC =
1635 MachO->getLinkeditDataLoadCommand(Command);
1637 MachO->ReadULEB128s(LLC.dataoff, FoundFns);
1638 } else if (Command.C.cmd == MachO::LC_MAIN) {
1639 MachO::entry_point_command LCmain =
1640 MachO->getEntryPointCommand(Command);
1641 lc_main_offset = LCmain.entryoff;
1644 // See if these addresses are already in the symbol table.
1645 unsigned FunctionStartsAdded = 0;
1646 for (uint64_t f = 0; f < FoundFns.size(); f++) {
1647 bool found = false;
1648 for (unsigned J = 0; J < SymbolList.size() && !found; ++J) {
1649 if (SymbolList[J].Address == FoundFns[f] + BaseSegmentAddress)
1650 found = true;
1652 // See this address is not already in the symbol table fake up an
1653 // nlist for it.
1654 if (!found) {
1655 NMSymbol F = {};
1656 F.Name = "<redacted function X>";
1657 F.Address = FoundFns[f] + BaseSegmentAddress;
1658 F.Size = 0;
1659 // There is no symbol in the nlist symbol table for this so we set
1660 // Sym effectivly to null and the rest of code in here must test for
1661 // it and not do things like Sym.getFlags() for it.
1662 F.Sym = BasicSymbolRef();
1663 F.SymFlags = 0;
1664 F.NType = MachO::N_SECT;
1665 F.NSect = 0;
1666 StringRef SegmentName = StringRef();
1667 StringRef SectionName = StringRef();
1668 for (const SectionRef &Section : MachO->sections()) {
1669 Section.getName(SectionName);
1670 SegmentName = MachO->getSectionFinalSegmentName(
1671 Section.getRawDataRefImpl());
1672 F.NSect++;
1673 if (F.Address >= Section.getAddress() &&
1674 F.Address < Section.getAddress() + Section.getSize()) {
1675 F.Section = Section;
1676 break;
1679 if (SegmentName == "__TEXT" && SectionName == "__text")
1680 F.TypeChar = 't';
1681 else if (SegmentName == "__DATA" && SectionName == "__data")
1682 F.TypeChar = 'd';
1683 else if (SegmentName == "__DATA" && SectionName == "__bss")
1684 F.TypeChar = 'b';
1685 else
1686 F.TypeChar = 's';
1687 F.NDesc = 0;
1688 F.IndirectName = StringRef();
1689 SymbolList.push_back(F);
1690 if (FoundFns[f] == lc_main_offset)
1691 FOS << "<redacted LC_MAIN>";
1692 else
1693 FOS << "<redacted function " << f << ">";
1694 FOS << '\0';
1695 FunctionStartsAdded++;
1698 if (FunctionStartsAdded) {
1699 FOS.flush();
1700 const char *Q = FunctionStartsNameBuffer.c_str();
1701 for (unsigned K = 0; K < FunctionStartsAdded; K++) {
1702 SymbolList[I].Name = Q;
1703 Q += strlen(Q) + 1;
1704 if (SymbolList[I].TypeChar == 'I') {
1705 SymbolList[I].IndirectName = Q;
1706 Q += strlen(Q) + 1;
1708 I++;
1714 CurrentFilename = Obj.getFileName();
1715 sortAndPrintSymbolList(Obj, printName, ArchiveName, ArchitectureName);
1718 // checkMachOAndArchFlags() checks to see if the SymbolicFile is a Mach-O file
1719 // and if it is and there is a list of architecture flags is specified then
1720 // check to make sure this Mach-O file is one of those architectures or all
1721 // architectures was specificed. If not then an error is generated and this
1722 // routine returns false. Else it returns true.
1723 static bool checkMachOAndArchFlags(SymbolicFile *O, std::string &Filename) {
1724 auto *MachO = dyn_cast<MachOObjectFile>(O);
1726 if (!MachO || ArchAll || ArchFlags.empty())
1727 return true;
1729 MachO::mach_header H;
1730 MachO::mach_header_64 H_64;
1731 Triple T;
1732 const char *McpuDefault, *ArchFlag;
1733 if (MachO->is64Bit()) {
1734 H_64 = MachO->MachOObjectFile::getHeader64();
1735 T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype,
1736 &McpuDefault, &ArchFlag);
1737 } else {
1738 H = MachO->MachOObjectFile::getHeader();
1739 T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype,
1740 &McpuDefault, &ArchFlag);
1742 const std::string ArchFlagName(ArchFlag);
1743 if (none_of(ArchFlags, [&](const std::string &Name) {
1744 return Name == ArchFlagName;
1745 })) {
1746 error("No architecture specified", Filename);
1747 return false;
1749 return true;
1752 static void dumpSymbolNamesFromFile(std::string &Filename) {
1753 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
1754 MemoryBuffer::getFileOrSTDIN(Filename);
1755 if (error(BufferOrErr.getError(), Filename))
1756 return;
1758 LLVMContext Context;
1759 LLVMContext *ContextPtr = NoLLVMBitcode ? nullptr : &Context;
1760 Expected<std::unique_ptr<Binary>> BinaryOrErr =
1761 createBinary(BufferOrErr.get()->getMemBufferRef(), ContextPtr);
1762 if (!BinaryOrErr) {
1763 error(BinaryOrErr.takeError(), Filename);
1764 return;
1766 Binary &Bin = *BinaryOrErr.get();
1768 if (Archive *A = dyn_cast<Archive>(&Bin)) {
1769 if (ArchiveMap) {
1770 Archive::symbol_iterator I = A->symbol_begin();
1771 Archive::symbol_iterator E = A->symbol_end();
1772 if (I != E) {
1773 outs() << "Archive map\n";
1774 for (; I != E; ++I) {
1775 Expected<Archive::Child> C = I->getMember();
1776 if (!C) {
1777 error(C.takeError(), Filename);
1778 break;
1780 Expected<StringRef> FileNameOrErr = C->getName();
1781 if (!FileNameOrErr) {
1782 error(FileNameOrErr.takeError(), Filename);
1783 break;
1785 StringRef SymName = I->getName();
1786 outs() << SymName << " in " << FileNameOrErr.get() << "\n";
1788 outs() << "\n";
1793 Error Err = Error::success();
1794 for (auto &C : A->children(Err)) {
1795 Expected<std::unique_ptr<Binary>> ChildOrErr =
1796 C.getAsBinary(ContextPtr);
1797 if (!ChildOrErr) {
1798 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
1799 error(std::move(E), Filename, C);
1800 continue;
1802 if (SymbolicFile *O = dyn_cast<SymbolicFile>(&*ChildOrErr.get())) {
1803 if (!MachOPrintSizeWarning && PrintSize && isa<MachOObjectFile>(O)) {
1804 WithColor::warning(errs(), ToolName)
1805 << "sizes with -print-size for Mach-O files are always zero.\n";
1806 MachOPrintSizeWarning = true;
1808 if (!checkMachOAndArchFlags(O, Filename))
1809 return;
1810 if (!PrintFileName) {
1811 outs() << "\n";
1812 if (isa<MachOObjectFile>(O)) {
1813 outs() << Filename << "(" << O->getFileName() << ")";
1814 } else
1815 outs() << O->getFileName();
1816 outs() << ":\n";
1818 dumpSymbolNamesFromObject(*O, false, Filename);
1821 if (Err)
1822 error(std::move(Err), A->getFileName());
1824 return;
1826 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
1827 // If we have a list of architecture flags specified dump only those.
1828 if (!ArchAll && !ArchFlags.empty()) {
1829 // Look for a slice in the universal binary that matches each ArchFlag.
1830 bool ArchFound;
1831 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1832 ArchFound = false;
1833 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1834 E = UB->end_objects();
1835 I != E; ++I) {
1836 if (ArchFlags[i] == I->getArchFlagName()) {
1837 ArchFound = true;
1838 Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
1839 I->getAsObjectFile();
1840 std::string ArchiveName;
1841 std::string ArchitectureName;
1842 ArchiveName.clear();
1843 ArchitectureName.clear();
1844 if (ObjOrErr) {
1845 ObjectFile &Obj = *ObjOrErr.get();
1846 if (ArchFlags.size() > 1) {
1847 if (PrintFileName)
1848 ArchitectureName = I->getArchFlagName();
1849 else
1850 outs() << "\n" << Obj.getFileName() << " (for architecture "
1851 << I->getArchFlagName() << ")"
1852 << ":\n";
1854 dumpSymbolNamesFromObject(Obj, false, ArchiveName,
1855 ArchitectureName);
1856 } else if (auto E = isNotObjectErrorInvalidFileType(
1857 ObjOrErr.takeError())) {
1858 error(std::move(E), Filename, ArchFlags.size() > 1 ?
1859 StringRef(I->getArchFlagName()) : StringRef());
1860 continue;
1861 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
1862 I->getAsArchive()) {
1863 std::unique_ptr<Archive> &A = *AOrErr;
1864 Error Err = Error::success();
1865 for (auto &C : A->children(Err)) {
1866 Expected<std::unique_ptr<Binary>> ChildOrErr =
1867 C.getAsBinary(ContextPtr);
1868 if (!ChildOrErr) {
1869 if (auto E = isNotObjectErrorInvalidFileType(
1870 ChildOrErr.takeError())) {
1871 error(std::move(E), Filename, C, ArchFlags.size() > 1 ?
1872 StringRef(I->getArchFlagName()) : StringRef());
1874 continue;
1876 if (SymbolicFile *O =
1877 dyn_cast<SymbolicFile>(&*ChildOrErr.get())) {
1878 if (PrintFileName) {
1879 ArchiveName = A->getFileName();
1880 if (ArchFlags.size() > 1)
1881 ArchitectureName = I->getArchFlagName();
1882 } else {
1883 outs() << "\n" << A->getFileName();
1884 outs() << "(" << O->getFileName() << ")";
1885 if (ArchFlags.size() > 1) {
1886 outs() << " (for architecture " << I->getArchFlagName()
1887 << ")";
1889 outs() << ":\n";
1891 dumpSymbolNamesFromObject(*O, false, ArchiveName,
1892 ArchitectureName);
1895 if (Err)
1896 error(std::move(Err), A->getFileName());
1897 } else {
1898 consumeError(AOrErr.takeError());
1899 error(Filename + " for architecture " +
1900 StringRef(I->getArchFlagName()) +
1901 " is not a Mach-O file or an archive file",
1902 "Mach-O universal file");
1906 if (!ArchFound) {
1907 error(ArchFlags[i],
1908 "file: " + Filename + " does not contain architecture");
1909 return;
1912 return;
1914 // No architecture flags were specified so if this contains a slice that
1915 // matches the host architecture dump only that.
1916 if (!ArchAll) {
1917 Triple HostTriple = MachOObjectFile::getHostArch();
1918 StringRef HostArchName = HostTriple.getArchName();
1919 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1920 E = UB->end_objects();
1921 I != E; ++I) {
1922 if (HostArchName == I->getArchFlagName()) {
1923 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
1924 std::string ArchiveName;
1925 if (ObjOrErr) {
1926 ObjectFile &Obj = *ObjOrErr.get();
1927 dumpSymbolNamesFromObject(Obj, false);
1928 } else if (auto E = isNotObjectErrorInvalidFileType(
1929 ObjOrErr.takeError())) {
1930 error(std::move(E), Filename);
1931 return;
1932 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
1933 I->getAsArchive()) {
1934 std::unique_ptr<Archive> &A = *AOrErr;
1935 Error Err = Error::success();
1936 for (auto &C : A->children(Err)) {
1937 Expected<std::unique_ptr<Binary>> ChildOrErr =
1938 C.getAsBinary(ContextPtr);
1939 if (!ChildOrErr) {
1940 if (auto E = isNotObjectErrorInvalidFileType(
1941 ChildOrErr.takeError()))
1942 error(std::move(E), Filename, C);
1943 continue;
1945 if (SymbolicFile *O =
1946 dyn_cast<SymbolicFile>(&*ChildOrErr.get())) {
1947 if (PrintFileName)
1948 ArchiveName = A->getFileName();
1949 else
1950 outs() << "\n" << A->getFileName() << "(" << O->getFileName()
1951 << ")"
1952 << ":\n";
1953 dumpSymbolNamesFromObject(*O, false, ArchiveName);
1956 if (Err)
1957 error(std::move(Err), A->getFileName());
1958 } else {
1959 consumeError(AOrErr.takeError());
1960 error(Filename + " for architecture " +
1961 StringRef(I->getArchFlagName()) +
1962 " is not a Mach-O file or an archive file",
1963 "Mach-O universal file");
1965 return;
1969 // Either all architectures have been specified or none have been specified
1970 // and this does not contain the host architecture so dump all the slices.
1971 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
1972 for (const MachOUniversalBinary::ObjectForArch &O : UB->objects()) {
1973 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = O.getAsObjectFile();
1974 std::string ArchiveName;
1975 std::string ArchitectureName;
1976 ArchiveName.clear();
1977 ArchitectureName.clear();
1978 if (ObjOrErr) {
1979 ObjectFile &Obj = *ObjOrErr.get();
1980 if (PrintFileName) {
1981 if (isa<MachOObjectFile>(Obj) && moreThanOneArch)
1982 ArchitectureName = O.getArchFlagName();
1983 } else {
1984 if (moreThanOneArch)
1985 outs() << "\n";
1986 outs() << Obj.getFileName();
1987 if (isa<MachOObjectFile>(Obj) && moreThanOneArch)
1988 outs() << " (for architecture " << O.getArchFlagName() << ")";
1989 outs() << ":\n";
1991 dumpSymbolNamesFromObject(Obj, false, ArchiveName, ArchitectureName);
1992 } else if (auto E = isNotObjectErrorInvalidFileType(
1993 ObjOrErr.takeError())) {
1994 error(std::move(E), Filename, moreThanOneArch ?
1995 StringRef(O.getArchFlagName()) : StringRef());
1996 continue;
1997 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
1998 O.getAsArchive()) {
1999 std::unique_ptr<Archive> &A = *AOrErr;
2000 Error Err = Error::success();
2001 for (auto &C : A->children(Err)) {
2002 Expected<std::unique_ptr<Binary>> ChildOrErr =
2003 C.getAsBinary(ContextPtr);
2004 if (!ChildOrErr) {
2005 if (auto E = isNotObjectErrorInvalidFileType(
2006 ChildOrErr.takeError()))
2007 error(std::move(E), Filename, C, moreThanOneArch ?
2008 StringRef(ArchitectureName) : StringRef());
2009 continue;
2011 if (SymbolicFile *F = dyn_cast<SymbolicFile>(&*ChildOrErr.get())) {
2012 if (PrintFileName) {
2013 ArchiveName = A->getFileName();
2014 if (isa<MachOObjectFile>(F) && moreThanOneArch)
2015 ArchitectureName = O.getArchFlagName();
2016 } else {
2017 outs() << "\n" << A->getFileName();
2018 if (isa<MachOObjectFile>(F)) {
2019 outs() << "(" << F->getFileName() << ")";
2020 if (moreThanOneArch)
2021 outs() << " (for architecture " << O.getArchFlagName()
2022 << ")";
2023 } else
2024 outs() << ":" << F->getFileName();
2025 outs() << ":\n";
2027 dumpSymbolNamesFromObject(*F, false, ArchiveName, ArchitectureName);
2030 if (Err)
2031 error(std::move(Err), A->getFileName());
2032 } else {
2033 consumeError(AOrErr.takeError());
2034 error(Filename + " for architecture " +
2035 StringRef(O.getArchFlagName()) +
2036 " is not a Mach-O file or an archive file",
2037 "Mach-O universal file");
2040 return;
2042 if (SymbolicFile *O = dyn_cast<SymbolicFile>(&Bin)) {
2043 if (!MachOPrintSizeWarning && PrintSize && isa<MachOObjectFile>(O)) {
2044 WithColor::warning(errs(), ToolName)
2045 << "sizes with --print-size for Mach-O files are always zero.\n";
2046 MachOPrintSizeWarning = true;
2048 if (!checkMachOAndArchFlags(O, Filename))
2049 return;
2050 dumpSymbolNamesFromObject(*O, true);
2054 int main(int argc, char **argv) {
2055 InitLLVM X(argc, argv);
2056 cl::HideUnrelatedOptions(NMCat);
2057 cl::ParseCommandLineOptions(argc, argv, "llvm symbol table dumper\n");
2059 // llvm-nm only reads binary files.
2060 if (error(sys::ChangeStdinToBinary()))
2061 return 1;
2063 // These calls are needed so that we can read bitcode correctly.
2064 llvm::InitializeAllTargetInfos();
2065 llvm::InitializeAllTargetMCs();
2066 llvm::InitializeAllAsmParsers();
2068 ToolName = argv[0];
2069 if (BSDFormat)
2070 OutputFormat = bsd;
2071 if (POSIXFormat)
2072 OutputFormat = posix;
2073 if (DarwinFormat)
2074 OutputFormat = darwin;
2076 // The relative order of these is important. If you pass --size-sort it should
2077 // only print out the size. However, if you pass -S --size-sort, it should
2078 // print out both the size and address.
2079 if (SizeSort && !PrintSize)
2080 PrintAddress = false;
2081 if (OutputFormat == sysv || SizeSort)
2082 PrintSize = true;
2083 if (InputFilenames.empty())
2084 InputFilenames.push_back("a.out");
2085 if (InputFilenames.size() > 1)
2086 MultipleFiles = true;
2088 // If both --demangle and --no-demangle are specified then pick the last one.
2089 if (NoDemangle.getPosition() > Demangle.getPosition())
2090 Demangle = !NoDemangle;
2092 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
2093 if (ArchFlags[i] == "all") {
2094 ArchAll = true;
2095 } else {
2096 if (!MachOObjectFile::isValidArch(ArchFlags[i]))
2097 error("Unknown architecture named '" + ArchFlags[i] + "'",
2098 "for the --arch option");
2102 if (!SegSect.empty() && SegSect.size() != 2)
2103 error("bad number of arguments (must be two arguments)",
2104 "for the -s option");
2106 if (NoDyldInfo && (AddDyldInfo || DyldInfoOnly))
2107 error("--no-dyldinfo can't be used with --add-dyldinfo or --dyldinfo-only");
2109 llvm::for_each(InputFilenames, dumpSymbolNamesFromFile);
2111 if (HadError)
2112 return 1;