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"));
85 cl::opt<bool> PrintFileName(
87 cl::desc("Precede each symbol with the object file it came from"));
89 cl::alias PrintFileNameA("A", cl::desc("Alias for --print-file-name"),
90 cl::aliasopt(PrintFileName));
91 cl::alias PrintFileNameo("o", cl::desc("Alias for --print-file-name"),
92 cl::aliasopt(PrintFileName));
94 cl::opt<bool> DebugSyms("debug-syms",
95 cl::desc("Show all symbols, even debugger only"));
96 cl::alias DebugSymsa("a", cl::desc("Alias for --debug-syms"),
97 cl::aliasopt(DebugSyms));
99 cl::opt<bool> NumericSort("numeric-sort", cl::desc("Sort symbols by address"));
100 cl::alias NumericSortn("n", cl::desc("Alias for --numeric-sort"),
101 cl::aliasopt(NumericSort));
102 cl::alias NumericSortv("v", cl::desc("Alias for --numeric-sort"),
103 cl::aliasopt(NumericSort));
105 cl::opt<bool> NoSort("no-sort", cl::desc("Show symbols in order encountered"));
106 cl::alias NoSortp("p", cl::desc("Alias for --no-sort"), cl::aliasopt(NoSort));
108 cl::opt<bool> PrintSize("print-size",
109 cl::desc("Show symbol size instead of address"));
110 cl::alias PrintSizeS("S", cl::desc("Alias for --print-size"),
111 cl::aliasopt(PrintSize));
113 cl::opt<bool> SizeSort("size-sort", cl::desc("Sort symbols by size"));
115 cl::opt<bool> WithoutAliases("without-aliases", cl::Hidden,
116 cl::desc("Exclude aliases from output"));
118 cl::opt<bool> ArchiveMap("print-armap", cl::desc("Print the archive map"));
119 cl::alias ArchiveMaps("s", cl::desc("Alias for --print-armap"),
120 cl::aliasopt(ArchiveMap));
121 bool PrintAddress = true;
123 bool MultipleFiles = false;
125 bool HadError = false;
127 std::string ToolName;
130 static void error(Twine Message, Twine Path = Twine()) {
132 errs() << ToolName << ": " << Path << ": " << Message << ".\n";
135 static bool error(error_code EC, Twine Path = Twine()) {
137 error(EC.message(), Path);
153 static bool compareSymbolAddress(const NMSymbol &A, const NMSymbol &B) {
154 if (A.Address < B.Address)
156 else if (A.Address == B.Address && A.Name < B.Name)
158 else if (A.Address == B.Address && A.Name == B.Name && A.Size < B.Size)
164 static bool compareSymbolSize(const NMSymbol &A, const NMSymbol &B) {
167 else if (A.Size == B.Size && A.Name < B.Name)
169 else if (A.Size == B.Size && A.Name == B.Name && A.Address < B.Address)
175 static bool compareSymbolName(const NMSymbol &A, const NMSymbol &B) {
178 else if (A.Name == B.Name && A.Size < B.Size)
180 else if (A.Name == B.Name && A.Size == B.Size && A.Address < B.Address)
186 static char isSymbolList64Bit(SymbolicFile *Obj) {
187 if (isa<IRObjectFile>(Obj))
189 else if (isa<COFFObjectFile>(Obj))
191 else if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(Obj))
192 return MachO->is64Bit();
193 else if (isa<ELF32LEObjectFile>(Obj))
195 else if (isa<ELF64LEObjectFile>(Obj))
197 else if (isa<ELF32BEObjectFile>(Obj))
199 else if(isa<ELF64BEObjectFile>(Obj))
205 static StringRef CurrentFilename;
206 typedef std::vector<NMSymbol> SymbolListT;
207 static SymbolListT SymbolList;
209 // darwinPrintSymbol() is used to print a symbol from a Mach-O file when the
210 // the OutputFormat is darwin. It produces the same output as darwin's nm(1) -m
212 static void darwinPrintSymbol(MachOObjectFile *MachO, SymbolListT::iterator I,
213 char *SymbolAddrStr, const char *printBlanks) {
214 MachO::mach_header H;
215 MachO::mach_header_64 H_64;
216 uint32_t Filetype, Flags;
217 MachO::nlist_64 STE_64;
222 if (MachO->is64Bit()) {
223 H_64 = MachO->MachOObjectFile::getHeader64();
224 Filetype = H_64.filetype;
226 STE_64 = MachO->getSymbol64TableEntry(I->Symb);
227 NType = STE_64.n_type;
228 NDesc = STE_64.n_desc;
229 NValue = STE_64.n_value;
231 H = MachO->MachOObjectFile::getHeader();
232 Filetype = H.filetype;
234 STE = MachO->getSymbolTableEntry(I->Symb);
237 NValue = STE.n_value;
241 if ((NType & MachO::N_TYPE) == MachO::N_INDR)
242 strcpy(SymbolAddrStr, printBlanks);
243 outs() << SymbolAddrStr << ' ';
246 switch (NType & MachO::N_TYPE) {
249 outs() << "(common) ";
250 if (MachO::GET_COMM_ALIGN(NDesc) != 0)
251 outs() << "(alignment 2^" <<
252 (int)MachO::GET_COMM_ALIGN(NDesc) << ") ";
254 if ((NType & MachO::N_TYPE) == MachO::N_PBUD)
255 outs() << "(prebound ";
258 if ((NDesc & MachO::REFERENCE_TYPE) ==
259 MachO::REFERENCE_FLAG_UNDEFINED_LAZY)
260 outs() << "undefined [lazy bound]) ";
261 else if ((NDesc & MachO::REFERENCE_TYPE) ==
262 MachO::REFERENCE_FLAG_UNDEFINED_LAZY)
263 outs() << "undefined [private lazy bound]) ";
264 else if ((NDesc & MachO::REFERENCE_TYPE) ==
265 MachO::REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY)
266 outs() << "undefined [private]) ";
268 outs() << "undefined) ";
272 outs() << "(absolute) ";
275 outs() << "(indirect) ";
277 case MachO::N_SECT: {
278 section_iterator Sec = MachO->section_end();
279 MachO->getSymbolSection(I->Symb, Sec);
280 DataRefImpl Ref = Sec->getRawDataRefImpl();
281 StringRef SectionName;
282 MachO->getSectionName(Ref, SectionName);
283 StringRef SegmentName = MachO->getSectionFinalSegmentName(Ref);
284 outs() << "(" << SegmentName << "," << SectionName << ") ";
292 if (NType & MachO::N_EXT) {
293 if (NDesc & MachO::REFERENCED_DYNAMICALLY)
294 outs() << "[referenced dynamically] ";
295 if (NType & MachO::N_PEXT) {
296 if ((NDesc & MachO::N_WEAK_DEF) == MachO::N_WEAK_DEF)
297 outs() << "weak private external ";
299 outs() << "private external ";
301 if ((NDesc & MachO::N_WEAK_REF) == MachO::N_WEAK_REF ||
302 (NDesc & MachO::N_WEAK_DEF) == MachO::N_WEAK_DEF){
303 if ((NDesc & (MachO::N_WEAK_REF | MachO::N_WEAK_DEF)) ==
304 (MachO::N_WEAK_REF | MachO::N_WEAK_DEF))
305 outs() << "weak external automatically hidden ";
307 outs() << "weak external ";
310 outs() << "external ";
313 if (NType & MachO::N_PEXT)
314 outs() << "non-external (was a private external) ";
316 outs() << "non-external ";
319 if (Filetype == MachO::MH_OBJECT &&
320 (NDesc & MachO::N_NO_DEAD_STRIP) == MachO::N_NO_DEAD_STRIP)
321 outs() << "[no dead strip] ";
323 if (Filetype == MachO::MH_OBJECT &&
324 ((NType & MachO::N_TYPE) != MachO::N_UNDF) &&
325 (NDesc & MachO::N_SYMBOL_RESOLVER) == MachO::N_SYMBOL_RESOLVER)
326 outs() << "[symbol resolver] ";
328 if (Filetype == MachO::MH_OBJECT &&
329 ((NType & MachO::N_TYPE) != MachO::N_UNDF) &&
330 (NDesc & MachO::N_ALT_ENTRY) == MachO::N_ALT_ENTRY)
331 outs() << "[alt entry] ";
333 if ((NDesc & MachO::N_ARM_THUMB_DEF) == MachO::N_ARM_THUMB_DEF)
334 outs() << "[Thumb] ";
336 if ((NType & MachO::N_TYPE) == MachO::N_INDR) {
337 outs() << I->Name << " (for ";
338 StringRef IndirectName;
339 if (MachO->getIndirectName(I->Symb, IndirectName))
342 outs() << IndirectName << ")";
347 if ((Flags & MachO::MH_TWOLEVEL) == MachO::MH_TWOLEVEL &&
348 (((NType & MachO::N_TYPE) == MachO::N_UNDF &&
350 (NType & MachO::N_TYPE) == MachO::N_PBUD)) {
351 uint32_t LibraryOrdinal = MachO::GET_LIBRARY_ORDINAL(NDesc);
352 if (LibraryOrdinal != 0) {
353 if (LibraryOrdinal == MachO::EXECUTABLE_ORDINAL)
354 outs() << " (from executable)";
355 else if (LibraryOrdinal == MachO::DYNAMIC_LOOKUP_ORDINAL)
356 outs() << " (dynamically looked up)";
358 StringRef LibraryName;
359 if (MachO->getLibraryShortNameByIndex(LibraryOrdinal - 1,
361 outs() << " (from bad library ordinal " <<
362 LibraryOrdinal << ")";
364 outs() << " (from " << LibraryName << ")";
372 static void sortAndPrintSymbolList(SymbolicFile *Obj) {
375 std::sort(SymbolList.begin(), SymbolList.end(), compareSymbolAddress);
377 std::sort(SymbolList.begin(), SymbolList.end(), compareSymbolSize);
379 std::sort(SymbolList.begin(), SymbolList.end(), compareSymbolName);
382 if (OutputFormat == posix && MultipleFiles) {
383 outs() << '\n' << CurrentFilename << ":\n";
384 } else if (OutputFormat == bsd && MultipleFiles) {
385 outs() << "\n" << CurrentFilename << ":\n";
386 } else if (OutputFormat == sysv) {
387 outs() << "\n\nSymbols from " << CurrentFilename << ":\n\n"
388 << "Name Value Class Type"
389 << " Size Line Section\n";
392 const char *printBlanks, *printFormat;
393 if (isSymbolList64Bit(Obj)) {
395 printFormat = "%016" PRIx64;
398 printFormat = "%08" PRIx64;
401 for (SymbolListT::iterator I = SymbolList.begin(), E = SymbolList.end();
403 if ((I->TypeChar != 'U') && UndefinedOnly)
405 if ((I->TypeChar == 'U') && DefinedOnly)
407 if (SizeSort && !PrintAddress && I->Size == UnknownAddressOrSize)
410 char SymbolAddrStr[18] = "";
411 char SymbolSizeStr[18] = "";
413 if (OutputFormat == sysv || I->Address == UnknownAddressOrSize)
414 strcpy(SymbolAddrStr, printBlanks);
415 if (OutputFormat == sysv)
416 strcpy(SymbolSizeStr, printBlanks);
418 if (I->Address != UnknownAddressOrSize)
419 format(printFormat, I->Address)
420 .print(SymbolAddrStr, sizeof(SymbolAddrStr));
421 if (I->Size != UnknownAddressOrSize)
422 format(printFormat, I->Size).print(SymbolSizeStr, sizeof(SymbolSizeStr));
424 // If OutputFormat is darwin and we have a MachOObjectFile print as darwin's
425 // nm(1) -m output, else if OutputFormat is darwin and not a Mach-O object
426 // fall back to OutputFormat bsd (see below).
427 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(Obj);
428 if (OutputFormat == darwin && MachO) {
429 darwinPrintSymbol(MachO, I, SymbolAddrStr, printBlanks);
430 } else if (OutputFormat == posix) {
431 outs() << I->Name << " " << I->TypeChar << " " << SymbolAddrStr
432 << SymbolSizeStr << "\n";
433 } else if (OutputFormat == bsd || (OutputFormat == darwin && !MachO)) {
435 outs() << SymbolAddrStr << ' ';
437 outs() << SymbolSizeStr;
438 if (I->Size != UnknownAddressOrSize)
441 outs() << I->TypeChar << " " << I->Name << "\n";
442 } else if (OutputFormat == sysv) {
443 std::string PaddedName(I->Name);
444 while (PaddedName.length() < 20)
446 outs() << PaddedName << "|" << SymbolAddrStr << "| " << I->TypeChar
447 << " | |" << SymbolSizeStr << "| |\n";
454 template <class ELFT>
455 static char getSymbolNMTypeChar(ELFObjectFile<ELFT> &Obj,
456 basic_symbol_iterator I) {
457 typedef typename ELFObjectFile<ELFT>::Elf_Sym Elf_Sym;
458 typedef typename ELFObjectFile<ELFT>::Elf_Shdr Elf_Shdr;
461 symbol_iterator SymI(I);
463 DataRefImpl Symb = I->getRawDataRefImpl();
464 const Elf_Sym *ESym = Obj.getSymbol(Symb);
465 const ELFFile<ELFT> &EF = *Obj.getELFFile();
466 const Elf_Shdr *ESec = EF.getSection(ESym);
469 switch (ESec->sh_type) {
470 case ELF::SHT_PROGBITS:
471 case ELF::SHT_DYNAMIC:
472 switch (ESec->sh_flags) {
473 case(ELF::SHF_ALLOC | ELF::SHF_EXECINSTR) :
475 case(ELF::SHF_TLS | ELF::SHF_ALLOC | ELF::SHF_WRITE) :
476 case(ELF::SHF_ALLOC | ELF::SHF_WRITE) :
479 case(ELF::SHF_ALLOC | ELF::SHF_MERGE) :
480 case(ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS) :
484 case ELF::SHT_NOBITS:
489 if (ESym->getType() == ELF::STT_SECTION) {
491 if (error(SymI->getName(Name)))
493 return StringSwitch<char>(Name)
494 .StartsWith(".debug", 'N')
495 .StartsWith(".note", 'n')
502 static char getSymbolNMTypeChar(COFFObjectFile &Obj, symbol_iterator I) {
503 const coff_symbol *Symb = Obj.getCOFFSymbol(*I);
505 symbol_iterator SymI(I);
508 if (error(SymI->getName(Name)))
511 char Ret = StringSwitch<char>(Name)
512 .StartsWith(".debug", 'N')
513 .StartsWith(".sxdata", 'N')
519 uint32_t Characteristics = 0;
520 if (!COFF::isReservedSectionNumber(Symb->SectionNumber)) {
521 section_iterator SecI = Obj.section_end();
522 if (error(SymI->getSection(SecI)))
524 const coff_section *Section = Obj.getCOFFSection(*SecI);
525 Characteristics = Section->Characteristics;
528 switch (Symb->SectionNumber) {
529 case COFF::IMAGE_SYM_DEBUG:
532 // Check section type.
533 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
535 else if (Characteristics & COFF::IMAGE_SCN_MEM_READ &&
536 ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
538 else if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
540 else if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
542 else if (Characteristics & COFF::IMAGE_SCN_LNK_INFO)
545 // Check for section symbol.
546 else if (Symb->isSectionDefinition())
553 static uint8_t getNType(MachOObjectFile &Obj, DataRefImpl Symb) {
555 MachO::nlist_64 STE = Obj.getSymbol64TableEntry(Symb);
558 MachO::nlist STE = Obj.getSymbolTableEntry(Symb);
562 static char getSymbolNMTypeChar(MachOObjectFile &Obj, basic_symbol_iterator I) {
563 DataRefImpl Symb = I->getRawDataRefImpl();
564 uint8_t NType = getNType(Obj, Symb);
566 switch (NType & MachO::N_TYPE) {
571 case MachO::N_SECT: {
572 section_iterator Sec = Obj.section_end();
573 Obj.getSymbolSection(Symb, Sec);
574 DataRefImpl Ref = Sec->getRawDataRefImpl();
575 StringRef SectionName;
576 Obj.getSectionName(Ref, SectionName);
577 StringRef SegmentName = Obj.getSectionFinalSegmentName(Ref);
578 if (SegmentName == "__TEXT" && SectionName == "__text")
588 static char getSymbolNMTypeChar(const GlobalValue &GV) {
589 if (GV.getType()->getElementType()->isFunctionTy())
591 // FIXME: should we print 'b'? At the IR level we cannot be sure if this
592 // will be in bss or not, but we could approximate.
596 static char getSymbolNMTypeChar(IRObjectFile &Obj, basic_symbol_iterator I) {
597 const GlobalValue &GV = Obj.getSymbolGV(I->getRawDataRefImpl());
598 return getSymbolNMTypeChar(GV);
601 template <class ELFT>
602 static bool isObject(ELFObjectFile<ELFT> &Obj, symbol_iterator I) {
603 typedef typename ELFObjectFile<ELFT>::Elf_Sym Elf_Sym;
605 DataRefImpl Symb = I->getRawDataRefImpl();
606 const Elf_Sym *ESym = Obj.getSymbol(Symb);
608 return ESym->getType() == ELF::STT_OBJECT;
611 static bool isObject(SymbolicFile *Obj, basic_symbol_iterator I) {
612 if (ELF32LEObjectFile *ELF = dyn_cast<ELF32LEObjectFile>(Obj))
613 return isObject(*ELF, I);
614 if (ELF64LEObjectFile *ELF = dyn_cast<ELF64LEObjectFile>(Obj))
615 return isObject(*ELF, I);
616 if (ELF32BEObjectFile *ELF = dyn_cast<ELF32BEObjectFile>(Obj))
617 return isObject(*ELF, I);
618 if (ELF64BEObjectFile *ELF = dyn_cast<ELF64BEObjectFile>(Obj))
619 return isObject(*ELF, I);
623 static char getNMTypeChar(SymbolicFile *Obj, basic_symbol_iterator I) {
624 uint32_t Symflags = I->getFlags();
625 if ((Symflags & object::SymbolRef::SF_Weak) && !isa<MachOObjectFile>(Obj)) {
626 char Ret = isObject(Obj, I) ? 'v' : 'w';
627 if (!(Symflags & object::SymbolRef::SF_Undefined))
632 if (Symflags & object::SymbolRef::SF_Undefined)
635 if (Symflags & object::SymbolRef::SF_Common)
639 if (Symflags & object::SymbolRef::SF_Absolute)
641 else if (IRObjectFile *IR = dyn_cast<IRObjectFile>(Obj))
642 Ret = getSymbolNMTypeChar(*IR, I);
643 else if (COFFObjectFile *COFF = dyn_cast<COFFObjectFile>(Obj))
644 Ret = getSymbolNMTypeChar(*COFF, I);
645 else if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(Obj))
646 Ret = getSymbolNMTypeChar(*MachO, I);
647 else if (ELF32LEObjectFile *ELF = dyn_cast<ELF32LEObjectFile>(Obj))
648 Ret = getSymbolNMTypeChar(*ELF, I);
649 else if (ELF64LEObjectFile *ELF = dyn_cast<ELF64LEObjectFile>(Obj))
650 Ret = getSymbolNMTypeChar(*ELF, I);
651 else if (ELF32BEObjectFile *ELF = dyn_cast<ELF32BEObjectFile>(Obj))
652 Ret = getSymbolNMTypeChar(*ELF, I);
654 Ret = getSymbolNMTypeChar(*cast<ELF64BEObjectFile>(Obj), I);
656 if (Symflags & object::SymbolRef::SF_Global)
662 static void dumpSymbolNamesFromObject(SymbolicFile *Obj) {
663 basic_symbol_iterator IBegin = Obj->symbol_begin();
664 basic_symbol_iterator IEnd = Obj->symbol_end();
667 error("File format has no dynamic symbol table", Obj->getFileName());
670 std::pair<symbol_iterator, symbol_iterator> IDyn =
671 getELFDynamicSymbolIterators(Obj);
675 std::string NameBuffer;
676 raw_string_ostream OS(NameBuffer);
677 for (basic_symbol_iterator I = IBegin; I != IEnd; ++I) {
678 uint32_t SymFlags = I->getFlags();
679 if (!DebugSyms && (SymFlags & SymbolRef::SF_FormatSpecific))
681 if (WithoutAliases) {
682 if (IRObjectFile *IR = dyn_cast<IRObjectFile>(Obj)) {
683 const GlobalValue &GV = IR->getSymbolGV(I->getRawDataRefImpl());
684 if(isa<GlobalAlias>(GV))
689 S.Size = UnknownAddressOrSize;
690 S.Address = UnknownAddressOrSize;
691 if ((PrintSize || SizeSort) && isa<ObjectFile>(Obj)) {
692 symbol_iterator SymI = I;
693 if (error(SymI->getSize(S.Size)))
696 if (PrintAddress && isa<ObjectFile>(Obj))
697 if (error(symbol_iterator(I)->getAddress(S.Address)))
699 S.TypeChar = getNMTypeChar(Obj, I);
700 if (error(I->printName(OS)))
703 S.Symb = I->getRawDataRefImpl();
704 SymbolList.push_back(S);
708 const char *P = NameBuffer.c_str();
709 for (unsigned I = 0; I < SymbolList.size(); ++I) {
710 SymbolList[I].Name = P;
714 CurrentFilename = Obj->getFileName();
715 sortAndPrintSymbolList(Obj);
718 static void dumpSymbolNamesFromFile(std::string &Filename) {
719 std::unique_ptr<MemoryBuffer> Buffer;
720 if (error(MemoryBuffer::getFileOrSTDIN(Filename, Buffer), Filename))
723 LLVMContext &Context = getGlobalContext();
724 ErrorOr<Binary *> BinaryOrErr = createBinary(Buffer.release(), &Context);
725 if (error(BinaryOrErr.getError(), Filename))
727 std::unique_ptr<Binary> Bin(BinaryOrErr.get());
729 if (Archive *A = dyn_cast<Archive>(Bin.get())) {
731 Archive::symbol_iterator I = A->symbol_begin();
732 Archive::symbol_iterator E = A->symbol_end();
734 outs() << "Archive map\n";
735 for (; I != E; ++I) {
736 Archive::child_iterator C;
739 if (error(I->getMember(C)))
741 if (error(I->getName(SymName)))
743 if (error(C->getName(FileName)))
745 outs() << SymName << " in " << FileName << "\n";
751 for (Archive::child_iterator I = A->child_begin(), E = A->child_end();
753 std::unique_ptr<Binary> Child;
754 if (I->getAsBinary(Child, &Context))
756 if (SymbolicFile *O = dyn_cast<SymbolicFile>(Child.get())) {
757 outs() << O->getFileName() << ":\n";
758 dumpSymbolNamesFromObject(O);
763 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(Bin.get())) {
764 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
765 E = UB->end_objects();
767 std::unique_ptr<ObjectFile> Obj;
768 std::unique_ptr<Archive> A;
769 if (!I->getAsObjectFile(Obj)) {
770 outs() << Obj->getFileName() << ":\n";
771 dumpSymbolNamesFromObject(Obj.get());
773 else if (!I->getAsArchive(A)) {
774 for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end();
776 std::unique_ptr<Binary> Child;
777 if (AI->getAsBinary(Child, &Context))
779 if (SymbolicFile *O = dyn_cast<SymbolicFile>(Child.get())) {
780 outs() << A->getFileName() << ":";
781 outs() << O->getFileName() << ":\n";
782 dumpSymbolNamesFromObject(O);
789 if (SymbolicFile *O = dyn_cast<SymbolicFile>(Bin.get())) {
790 dumpSymbolNamesFromObject(O);
793 error("unrecognizable file type", Filename);
797 int main(int argc, char **argv) {
798 // Print a stack trace if we signal out.
799 sys::PrintStackTraceOnErrorSignal();
800 PrettyStackTraceProgram X(argc, argv);
802 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
803 cl::ParseCommandLineOptions(argc, argv, "llvm symbol table dumper\n");
805 // llvm-nm only reads binary files.
806 if (error(sys::ChangeStdinToBinary()))
813 OutputFormat = posix;
815 // The relative order of these is important. If you pass --size-sort it should
816 // only print out the size. However, if you pass -S --size-sort, it should
817 // print out both the size and address.
818 if (SizeSort && !PrintSize)
819 PrintAddress = false;
820 if (OutputFormat == sysv || SizeSort)
823 switch (InputFilenames.size()) {
825 InputFilenames.push_back("-");
829 MultipleFiles = true;
832 std::for_each(InputFilenames.begin(), InputFilenames.end(),
833 dumpSymbolNamesFromFile);