1 //===-- llvm-nm.cpp - Symbol table dumping utility for llvm ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // 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.
14 // This "nm" supports many of the features of GNU "nm", including its different
17 //===----------------------------------------------------------------------===//
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/GlobalAlias.h"
21 #include "llvm/IR/GlobalVariable.h"
22 #include "llvm/IR/LLVMContext.h"
23 #include "llvm/Object/Archive.h"
24 #include "llvm/Object/COFF.h"
25 #include "llvm/Object/ELFObjectFile.h"
26 #include "llvm/Object/IRObjectFile.h"
27 #include "llvm/Object/MachO.h"
28 #include "llvm/Object/MachOUniversal.h"
29 #include "llvm/Object/ObjectFile.h"
30 #include "llvm/Support/COFF.h"
31 #include "llvm/Support/CommandLine.h"
32 #include "llvm/Support/FileSystem.h"
33 #include "llvm/Support/Format.h"
34 #include "llvm/Support/ManagedStatic.h"
35 #include "llvm/Support/MemoryBuffer.h"
36 #include "llvm/Support/PrettyStackTrace.h"
37 #include "llvm/Support/Program.h"
38 #include "llvm/Support/Signals.h"
39 #include "llvm/Support/raw_ostream.h"
44 #include <system_error>
47 using namespace object;
50 enum OutputFormatTy { bsd, sysv, posix, darwin };
51 cl::opt<OutputFormatTy> OutputFormat(
52 "format", cl::desc("Specify output format"),
53 cl::values(clEnumVal(bsd, "BSD format"), clEnumVal(sysv, "System V format"),
54 clEnumVal(posix, "POSIX.2 format"),
55 clEnumVal(darwin, "Darwin -m format"), clEnumValEnd),
57 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
58 cl::aliasopt(OutputFormat));
60 cl::list<std::string> InputFilenames(cl::Positional, cl::desc("<input files>"),
63 cl::opt<bool> UndefinedOnly("undefined-only",
64 cl::desc("Show only undefined symbols"));
65 cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
66 cl::aliasopt(UndefinedOnly));
68 cl::opt<bool> DynamicSyms("dynamic",
69 cl::desc("Display the dynamic symbols instead "
70 "of normal symbols."));
71 cl::alias DynamicSyms2("D", cl::desc("Alias for --dynamic"),
72 cl::aliasopt(DynamicSyms));
74 cl::opt<bool> DefinedOnly("defined-only",
75 cl::desc("Show only defined symbols"));
77 cl::opt<bool> ExternalOnly("extern-only",
78 cl::desc("Show only external symbols"));
79 cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
80 cl::aliasopt(ExternalOnly));
82 cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
83 cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
84 cl::opt<bool> DarwinFormat("m", cl::desc("Alias for --format=darwin"));
86 cl::opt<bool> PrintFileName(
88 cl::desc("Precede each symbol with the object file it came from"));
90 cl::alias PrintFileNameA("A", cl::desc("Alias for --print-file-name"),
91 cl::aliasopt(PrintFileName));
92 cl::alias PrintFileNameo("o", cl::desc("Alias for --print-file-name"),
93 cl::aliasopt(PrintFileName));
95 cl::opt<bool> DebugSyms("debug-syms",
96 cl::desc("Show all symbols, even debugger only"));
97 cl::alias DebugSymsa("a", cl::desc("Alias for --debug-syms"),
98 cl::aliasopt(DebugSyms));
100 cl::opt<bool> NumericSort("numeric-sort", cl::desc("Sort symbols by address"));
101 cl::alias NumericSortn("n", cl::desc("Alias for --numeric-sort"),
102 cl::aliasopt(NumericSort));
103 cl::alias NumericSortv("v", cl::desc("Alias for --numeric-sort"),
104 cl::aliasopt(NumericSort));
106 cl::opt<bool> NoSort("no-sort", cl::desc("Show symbols in order encountered"));
107 cl::alias NoSortp("p", cl::desc("Alias for --no-sort"), cl::aliasopt(NoSort));
109 cl::opt<bool> PrintSize("print-size",
110 cl::desc("Show symbol size instead of address"));
111 cl::alias PrintSizeS("S", cl::desc("Alias for --print-size"),
112 cl::aliasopt(PrintSize));
114 cl::opt<bool> SizeSort("size-sort", cl::desc("Sort symbols by size"));
116 cl::opt<bool> WithoutAliases("without-aliases", cl::Hidden,
117 cl::desc("Exclude aliases from output"));
119 cl::opt<bool> ArchiveMap("print-armap", cl::desc("Print the archive map"));
120 cl::alias ArchiveMaps("s", cl::desc("Alias for --print-armap"),
121 cl::aliasopt(ArchiveMap));
122 bool PrintAddress = true;
124 bool MultipleFiles = false;
126 bool HadError = false;
128 std::string ToolName;
131 static void error(Twine Message, Twine Path = Twine()) {
133 errs() << ToolName << ": " << Path << ": " << Message << ".\n";
136 static bool error(std::error_code EC, Twine Path = Twine()) {
138 error(EC.message(), Path);
154 static bool compareSymbolAddress(const NMSymbol &A, const NMSymbol &B) {
155 if (A.Address < B.Address)
157 else if (A.Address == B.Address && A.Name < B.Name)
159 else if (A.Address == B.Address && A.Name == B.Name && A.Size < B.Size)
165 static bool compareSymbolSize(const NMSymbol &A, const NMSymbol &B) {
168 else if (A.Size == B.Size && A.Name < B.Name)
170 else if (A.Size == B.Size && A.Name == B.Name && A.Address < B.Address)
176 static bool compareSymbolName(const NMSymbol &A, const NMSymbol &B) {
179 else if (A.Name == B.Name && A.Size < B.Size)
181 else if (A.Name == B.Name && A.Size == B.Size && A.Address < B.Address)
187 static char isSymbolList64Bit(SymbolicFile *Obj) {
188 if (isa<IRObjectFile>(Obj))
190 else if (isa<COFFObjectFile>(Obj))
192 else if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(Obj))
193 return MachO->is64Bit();
194 else if (isa<ELF32LEObjectFile>(Obj))
196 else if (isa<ELF64LEObjectFile>(Obj))
198 else if (isa<ELF32BEObjectFile>(Obj))
200 else if(isa<ELF64BEObjectFile>(Obj))
206 static StringRef CurrentFilename;
207 typedef std::vector<NMSymbol> SymbolListT;
208 static SymbolListT SymbolList;
210 // darwinPrintSymbol() is used to print a symbol from a Mach-O file when the
211 // the OutputFormat is darwin. It produces the same output as darwin's nm(1) -m
213 static void darwinPrintSymbol(MachOObjectFile *MachO, SymbolListT::iterator I,
214 char *SymbolAddrStr, const char *printBlanks) {
215 MachO::mach_header H;
216 MachO::mach_header_64 H_64;
217 uint32_t Filetype, Flags;
218 MachO::nlist_64 STE_64;
223 if (MachO->is64Bit()) {
224 H_64 = MachO->MachOObjectFile::getHeader64();
225 Filetype = H_64.filetype;
227 STE_64 = MachO->getSymbol64TableEntry(I->Symb);
228 NType = STE_64.n_type;
229 NDesc = STE_64.n_desc;
230 NValue = STE_64.n_value;
232 H = MachO->MachOObjectFile::getHeader();
233 Filetype = H.filetype;
235 STE = MachO->getSymbolTableEntry(I->Symb);
238 NValue = STE.n_value;
242 if ((NType & MachO::N_TYPE) == MachO::N_INDR)
243 strcpy(SymbolAddrStr, printBlanks);
244 outs() << SymbolAddrStr << ' ';
247 switch (NType & MachO::N_TYPE) {
250 outs() << "(common) ";
251 if (MachO::GET_COMM_ALIGN(NDesc) != 0)
252 outs() << "(alignment 2^" <<
253 (int)MachO::GET_COMM_ALIGN(NDesc) << ") ";
255 if ((NType & MachO::N_TYPE) == MachO::N_PBUD)
256 outs() << "(prebound ";
259 if ((NDesc & MachO::REFERENCE_TYPE) ==
260 MachO::REFERENCE_FLAG_UNDEFINED_LAZY)
261 outs() << "undefined [lazy bound]) ";
262 else if ((NDesc & MachO::REFERENCE_TYPE) ==
263 MachO::REFERENCE_FLAG_UNDEFINED_LAZY)
264 outs() << "undefined [private lazy bound]) ";
265 else if ((NDesc & MachO::REFERENCE_TYPE) ==
266 MachO::REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY)
267 outs() << "undefined [private]) ";
269 outs() << "undefined) ";
273 outs() << "(absolute) ";
276 outs() << "(indirect) ";
278 case MachO::N_SECT: {
279 section_iterator Sec = MachO->section_end();
280 MachO->getSymbolSection(I->Symb, Sec);
281 DataRefImpl Ref = Sec->getRawDataRefImpl();
282 StringRef SectionName;
283 MachO->getSectionName(Ref, SectionName);
284 StringRef SegmentName = MachO->getSectionFinalSegmentName(Ref);
285 outs() << "(" << SegmentName << "," << SectionName << ") ";
293 if (NType & MachO::N_EXT) {
294 if (NDesc & MachO::REFERENCED_DYNAMICALLY)
295 outs() << "[referenced dynamically] ";
296 if (NType & MachO::N_PEXT) {
297 if ((NDesc & MachO::N_WEAK_DEF) == MachO::N_WEAK_DEF)
298 outs() << "weak private external ";
300 outs() << "private external ";
302 if ((NDesc & MachO::N_WEAK_REF) == MachO::N_WEAK_REF ||
303 (NDesc & MachO::N_WEAK_DEF) == MachO::N_WEAK_DEF){
304 if ((NDesc & (MachO::N_WEAK_REF | MachO::N_WEAK_DEF)) ==
305 (MachO::N_WEAK_REF | MachO::N_WEAK_DEF))
306 outs() << "weak external automatically hidden ";
308 outs() << "weak external ";
311 outs() << "external ";
314 if (NType & MachO::N_PEXT)
315 outs() << "non-external (was a private external) ";
317 outs() << "non-external ";
320 if (Filetype == MachO::MH_OBJECT &&
321 (NDesc & MachO::N_NO_DEAD_STRIP) == MachO::N_NO_DEAD_STRIP)
322 outs() << "[no dead strip] ";
324 if (Filetype == MachO::MH_OBJECT &&
325 ((NType & MachO::N_TYPE) != MachO::N_UNDF) &&
326 (NDesc & MachO::N_SYMBOL_RESOLVER) == MachO::N_SYMBOL_RESOLVER)
327 outs() << "[symbol resolver] ";
329 if (Filetype == MachO::MH_OBJECT &&
330 ((NType & MachO::N_TYPE) != MachO::N_UNDF) &&
331 (NDesc & MachO::N_ALT_ENTRY) == MachO::N_ALT_ENTRY)
332 outs() << "[alt entry] ";
334 if ((NDesc & MachO::N_ARM_THUMB_DEF) == MachO::N_ARM_THUMB_DEF)
335 outs() << "[Thumb] ";
337 if ((NType & MachO::N_TYPE) == MachO::N_INDR) {
338 outs() << I->Name << " (for ";
339 StringRef IndirectName;
340 if (MachO->getIndirectName(I->Symb, IndirectName))
343 outs() << IndirectName << ")";
348 if ((Flags & MachO::MH_TWOLEVEL) == MachO::MH_TWOLEVEL &&
349 (((NType & MachO::N_TYPE) == MachO::N_UNDF &&
351 (NType & MachO::N_TYPE) == MachO::N_PBUD)) {
352 uint32_t LibraryOrdinal = MachO::GET_LIBRARY_ORDINAL(NDesc);
353 if (LibraryOrdinal != 0) {
354 if (LibraryOrdinal == MachO::EXECUTABLE_ORDINAL)
355 outs() << " (from executable)";
356 else if (LibraryOrdinal == MachO::DYNAMIC_LOOKUP_ORDINAL)
357 outs() << " (dynamically looked up)";
359 StringRef LibraryName;
360 if (MachO->getLibraryShortNameByIndex(LibraryOrdinal - 1,
362 outs() << " (from bad library ordinal " <<
363 LibraryOrdinal << ")";
365 outs() << " (from " << LibraryName << ")";
373 static void sortAndPrintSymbolList(SymbolicFile *Obj, bool printName) {
376 std::sort(SymbolList.begin(), SymbolList.end(), compareSymbolAddress);
378 std::sort(SymbolList.begin(), SymbolList.end(), compareSymbolSize);
380 std::sort(SymbolList.begin(), SymbolList.end(), compareSymbolName);
383 if (OutputFormat == posix && MultipleFiles && printName) {
384 outs() << '\n' << CurrentFilename << ":\n";
385 } else if (OutputFormat == bsd && MultipleFiles && printName) {
386 outs() << "\n" << CurrentFilename << ":\n";
387 } else if (OutputFormat == sysv) {
388 outs() << "\n\nSymbols from " << CurrentFilename << ":\n\n"
389 << "Name Value Class Type"
390 << " Size Line Section\n";
393 const char *printBlanks, *printFormat;
394 if (isSymbolList64Bit(Obj)) {
396 printFormat = "%016" PRIx64;
399 printFormat = "%08" PRIx64;
402 for (SymbolListT::iterator I = SymbolList.begin(), E = SymbolList.end();
404 if ((I->TypeChar != 'U') && UndefinedOnly)
406 if ((I->TypeChar == 'U') && DefinedOnly)
408 if (SizeSort && !PrintAddress && I->Size == UnknownAddressOrSize)
411 char SymbolAddrStr[18] = "";
412 char SymbolSizeStr[18] = "";
414 if (OutputFormat == sysv || I->Address == UnknownAddressOrSize)
415 strcpy(SymbolAddrStr, printBlanks);
416 if (OutputFormat == sysv)
417 strcpy(SymbolSizeStr, printBlanks);
419 if (I->Address != UnknownAddressOrSize)
420 format(printFormat, I->Address)
421 .print(SymbolAddrStr, sizeof(SymbolAddrStr));
422 if (I->Size != UnknownAddressOrSize)
423 format(printFormat, I->Size).print(SymbolSizeStr, sizeof(SymbolSizeStr));
425 // If OutputFormat is darwin and we have a MachOObjectFile print as darwin's
426 // nm(1) -m output, else if OutputFormat is darwin and not a Mach-O object
427 // fall back to OutputFormat bsd (see below).
428 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(Obj);
429 if (OutputFormat == darwin && MachO) {
430 darwinPrintSymbol(MachO, I, SymbolAddrStr, printBlanks);
431 } else if (OutputFormat == posix) {
432 outs() << I->Name << " " << I->TypeChar << " " << SymbolAddrStr
433 << SymbolSizeStr << "\n";
434 } else if (OutputFormat == bsd || (OutputFormat == darwin && !MachO)) {
436 outs() << SymbolAddrStr << ' ';
438 outs() << SymbolSizeStr;
439 if (I->Size != UnknownAddressOrSize)
442 outs() << I->TypeChar << " " << I->Name << "\n";
443 } else if (OutputFormat == sysv) {
444 std::string PaddedName(I->Name);
445 while (PaddedName.length() < 20)
447 outs() << PaddedName << "|" << SymbolAddrStr << "| " << I->TypeChar
448 << " | |" << SymbolSizeStr << "| |\n";
455 template <class ELFT>
456 static char getSymbolNMTypeChar(ELFObjectFile<ELFT> &Obj,
457 basic_symbol_iterator I) {
458 typedef typename ELFObjectFile<ELFT>::Elf_Sym Elf_Sym;
459 typedef typename ELFObjectFile<ELFT>::Elf_Shdr Elf_Shdr;
462 symbol_iterator SymI(I);
464 DataRefImpl Symb = I->getRawDataRefImpl();
465 const Elf_Sym *ESym = Obj.getSymbol(Symb);
466 const ELFFile<ELFT> &EF = *Obj.getELFFile();
467 const Elf_Shdr *ESec = EF.getSection(ESym);
470 switch (ESec->sh_type) {
471 case ELF::SHT_PROGBITS:
472 case ELF::SHT_DYNAMIC:
473 switch (ESec->sh_flags) {
474 case(ELF::SHF_ALLOC | ELF::SHF_EXECINSTR) :
476 case(ELF::SHF_TLS | ELF::SHF_ALLOC | ELF::SHF_WRITE) :
477 case(ELF::SHF_ALLOC | ELF::SHF_WRITE) :
480 case(ELF::SHF_ALLOC | ELF::SHF_MERGE) :
481 case(ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS) :
485 case ELF::SHT_NOBITS:
490 if (ESym->getType() == ELF::STT_SECTION) {
492 if (error(SymI->getName(Name)))
494 return StringSwitch<char>(Name)
495 .StartsWith(".debug", 'N')
496 .StartsWith(".note", 'n')
503 static char getSymbolNMTypeChar(COFFObjectFile &Obj, symbol_iterator I) {
504 const coff_symbol *Symb = Obj.getCOFFSymbol(*I);
506 symbol_iterator SymI(I);
509 if (error(SymI->getName(Name)))
512 char Ret = StringSwitch<char>(Name)
513 .StartsWith(".debug", 'N')
514 .StartsWith(".sxdata", 'N')
520 uint32_t Characteristics = 0;
521 if (!COFF::isReservedSectionNumber(Symb->SectionNumber)) {
522 section_iterator SecI = Obj.section_end();
523 if (error(SymI->getSection(SecI)))
525 const coff_section *Section = Obj.getCOFFSection(*SecI);
526 Characteristics = Section->Characteristics;
529 switch (Symb->SectionNumber) {
530 case COFF::IMAGE_SYM_DEBUG:
533 // Check section type.
534 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
536 else if (Characteristics & COFF::IMAGE_SCN_MEM_READ &&
537 ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
539 else if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
541 else if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
543 else if (Characteristics & COFF::IMAGE_SCN_LNK_INFO)
546 // Check for section symbol.
547 else if (Symb->isSectionDefinition())
554 static uint8_t getNType(MachOObjectFile &Obj, DataRefImpl Symb) {
556 MachO::nlist_64 STE = Obj.getSymbol64TableEntry(Symb);
559 MachO::nlist STE = Obj.getSymbolTableEntry(Symb);
563 static char getSymbolNMTypeChar(MachOObjectFile &Obj, basic_symbol_iterator I) {
564 DataRefImpl Symb = I->getRawDataRefImpl();
565 uint8_t NType = getNType(Obj, Symb);
567 switch (NType & MachO::N_TYPE) {
572 case MachO::N_SECT: {
573 section_iterator Sec = Obj.section_end();
574 Obj.getSymbolSection(Symb, Sec);
575 DataRefImpl Ref = Sec->getRawDataRefImpl();
576 StringRef SectionName;
577 Obj.getSectionName(Ref, SectionName);
578 StringRef SegmentName = Obj.getSectionFinalSegmentName(Ref);
579 if (SegmentName == "__TEXT" && SectionName == "__text")
581 else if (SegmentName == "__DATA" && SectionName == "__data")
583 else if (SegmentName == "__DATA" && SectionName == "__bss")
593 static char getSymbolNMTypeChar(const GlobalValue &GV) {
594 if (GV.getType()->getElementType()->isFunctionTy())
596 // FIXME: should we print 'b'? At the IR level we cannot be sure if this
597 // will be in bss or not, but we could approximate.
601 static char getSymbolNMTypeChar(IRObjectFile &Obj, basic_symbol_iterator I) {
602 const GlobalValue &GV = Obj.getSymbolGV(I->getRawDataRefImpl());
603 return getSymbolNMTypeChar(GV);
606 template <class ELFT>
607 static bool isObject(ELFObjectFile<ELFT> &Obj, symbol_iterator I) {
608 typedef typename ELFObjectFile<ELFT>::Elf_Sym Elf_Sym;
610 DataRefImpl Symb = I->getRawDataRefImpl();
611 const Elf_Sym *ESym = Obj.getSymbol(Symb);
613 return ESym->getType() == ELF::STT_OBJECT;
616 static bool isObject(SymbolicFile *Obj, basic_symbol_iterator I) {
617 if (ELF32LEObjectFile *ELF = dyn_cast<ELF32LEObjectFile>(Obj))
618 return isObject(*ELF, I);
619 if (ELF64LEObjectFile *ELF = dyn_cast<ELF64LEObjectFile>(Obj))
620 return isObject(*ELF, I);
621 if (ELF32BEObjectFile *ELF = dyn_cast<ELF32BEObjectFile>(Obj))
622 return isObject(*ELF, I);
623 if (ELF64BEObjectFile *ELF = dyn_cast<ELF64BEObjectFile>(Obj))
624 return isObject(*ELF, I);
628 static char getNMTypeChar(SymbolicFile *Obj, basic_symbol_iterator I) {
629 uint32_t Symflags = I->getFlags();
630 if ((Symflags & object::SymbolRef::SF_Weak) && !isa<MachOObjectFile>(Obj)) {
631 char Ret = isObject(Obj, I) ? 'v' : 'w';
632 if (!(Symflags & object::SymbolRef::SF_Undefined))
637 if (Symflags & object::SymbolRef::SF_Undefined)
640 if (Symflags & object::SymbolRef::SF_Common)
644 if (Symflags & object::SymbolRef::SF_Absolute)
646 else if (IRObjectFile *IR = dyn_cast<IRObjectFile>(Obj))
647 Ret = getSymbolNMTypeChar(*IR, I);
648 else if (COFFObjectFile *COFF = dyn_cast<COFFObjectFile>(Obj))
649 Ret = getSymbolNMTypeChar(*COFF, I);
650 else if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(Obj))
651 Ret = getSymbolNMTypeChar(*MachO, I);
652 else if (ELF32LEObjectFile *ELF = dyn_cast<ELF32LEObjectFile>(Obj))
653 Ret = getSymbolNMTypeChar(*ELF, I);
654 else if (ELF64LEObjectFile *ELF = dyn_cast<ELF64LEObjectFile>(Obj))
655 Ret = getSymbolNMTypeChar(*ELF, I);
656 else if (ELF32BEObjectFile *ELF = dyn_cast<ELF32BEObjectFile>(Obj))
657 Ret = getSymbolNMTypeChar(*ELF, I);
659 Ret = getSymbolNMTypeChar(*cast<ELF64BEObjectFile>(Obj), I);
661 if (Symflags & object::SymbolRef::SF_Global)
667 static void dumpSymbolNamesFromObject(SymbolicFile *Obj, bool printName) {
668 basic_symbol_iterator IBegin = Obj->symbol_begin();
669 basic_symbol_iterator IEnd = Obj->symbol_end();
672 error("File format has no dynamic symbol table", Obj->getFileName());
675 std::pair<symbol_iterator, symbol_iterator> IDyn =
676 getELFDynamicSymbolIterators(Obj);
680 std::string NameBuffer;
681 raw_string_ostream OS(NameBuffer);
682 for (basic_symbol_iterator I = IBegin; I != IEnd; ++I) {
683 uint32_t SymFlags = I->getFlags();
684 if (!DebugSyms && (SymFlags & SymbolRef::SF_FormatSpecific))
686 if (WithoutAliases) {
687 if (IRObjectFile *IR = dyn_cast<IRObjectFile>(Obj)) {
688 const GlobalValue &GV = IR->getSymbolGV(I->getRawDataRefImpl());
689 if(isa<GlobalAlias>(GV))
694 S.Size = UnknownAddressOrSize;
695 S.Address = UnknownAddressOrSize;
696 if ((PrintSize || SizeSort) && isa<ObjectFile>(Obj)) {
697 symbol_iterator SymI = I;
698 if (error(SymI->getSize(S.Size)))
701 if (PrintAddress && isa<ObjectFile>(Obj))
702 if (error(symbol_iterator(I)->getAddress(S.Address)))
704 S.TypeChar = getNMTypeChar(Obj, I);
705 if (error(I->printName(OS)))
708 S.Symb = I->getRawDataRefImpl();
709 SymbolList.push_back(S);
713 const char *P = NameBuffer.c_str();
714 for (unsigned I = 0; I < SymbolList.size(); ++I) {
715 SymbolList[I].Name = P;
719 CurrentFilename = Obj->getFileName();
720 sortAndPrintSymbolList(Obj, printName);
723 static void dumpSymbolNamesFromFile(std::string &Filename) {
724 std::unique_ptr<MemoryBuffer> Buffer;
725 if (error(MemoryBuffer::getFileOrSTDIN(Filename, Buffer), Filename))
728 LLVMContext &Context = getGlobalContext();
729 ErrorOr<Binary *> BinaryOrErr = createBinary(Buffer.get(), &Context);
730 if (error(BinaryOrErr.getError(), Filename))
733 std::unique_ptr<Binary> Bin(BinaryOrErr.get());
735 if (Archive *A = dyn_cast<Archive>(Bin.get())) {
737 Archive::symbol_iterator I = A->symbol_begin();
738 Archive::symbol_iterator E = A->symbol_end();
740 outs() << "Archive map\n";
741 for (; I != E; ++I) {
742 ErrorOr<Archive::child_iterator> C = I->getMember();
743 if (error(C.getError()))
745 ErrorOr<StringRef> FileNameOrErr = C.get()->getName();
746 if (error(FileNameOrErr.getError()))
748 StringRef SymName = I->getName();
749 outs() << SymName << " in " << FileNameOrErr.get() << "\n";
755 for (Archive::child_iterator I = A->child_begin(), E = A->child_end();
757 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = I->getAsBinary(&Context);
758 if (ChildOrErr.getError())
760 if (SymbolicFile *O = dyn_cast<SymbolicFile>(&*ChildOrErr.get())) {
762 if (isa<MachOObjectFile>(O)) {
763 outs() << Filename << "(" << O->getFileName() << ")";
765 outs() << O->getFileName();
767 dumpSymbolNamesFromObject(O, false);
772 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(Bin.get())) {
773 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
774 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
775 E = UB->end_objects();
777 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
778 std::unique_ptr<Archive> A;
780 std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get());
783 outs() << Obj->getFileName();
784 if (isa<MachOObjectFile>(Obj.get()) && moreThanOneArch)
785 outs() << " (for architecture " << I->getArchTypeName() << ")";
787 dumpSymbolNamesFromObject(Obj.get(), false);
789 else if (!I->getAsArchive(A)) {
790 for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end();
792 ErrorOr<std::unique_ptr<Binary>> ChildOrErr =
793 AI->getAsBinary(&Context);
794 if (ChildOrErr.getError())
796 if (SymbolicFile *O = dyn_cast<SymbolicFile>(&*ChildOrErr.get())) {
797 outs() << "\n" << A->getFileName();
798 if (isa<MachOObjectFile>(O)) {
799 outs() << "(" << O->getFileName() << ")";
801 outs() << " (for architecture " << I->getArchTypeName() << ")";
803 outs() << ":" << O->getFileName();
805 dumpSymbolNamesFromObject(O, false);
812 if (SymbolicFile *O = dyn_cast<SymbolicFile>(Bin.get())) {
813 dumpSymbolNamesFromObject(O, true);
816 error("unrecognizable file type", Filename);
820 int main(int argc, char **argv) {
821 // Print a stack trace if we signal out.
822 sys::PrintStackTraceOnErrorSignal();
823 PrettyStackTraceProgram X(argc, argv);
825 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
826 cl::ParseCommandLineOptions(argc, argv, "llvm symbol table dumper\n");
828 // llvm-nm only reads binary files.
829 if (error(sys::ChangeStdinToBinary()))
836 OutputFormat = posix;
838 OutputFormat = darwin;
840 // The relative order of these is important. If you pass --size-sort it should
841 // only print out the size. However, if you pass -S --size-sort, it should
842 // print out both the size and address.
843 if (SizeSort && !PrintSize)
844 PrintAddress = false;
845 if (OutputFormat == sysv || SizeSort)
848 switch (InputFilenames.size()) {
850 InputFilenames.push_back("a.out");
854 MultipleFiles = true;
857 std::for_each(InputFilenames.begin(), InputFilenames.end(),
858 dumpSymbolNamesFromFile);