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/LLVMContext.h"
20 #include "llvm/Bitcode/ReaderWriter.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/Object/Archive.h"
23 #include "llvm/Object/COFF.h"
24 #include "llvm/Object/ELFObjectFile.h"
25 #include "llvm/Object/MachO.h"
26 #include "llvm/Object/MachOUniversal.h"
27 #include "llvm/Object/ObjectFile.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/FileSystem.h"
30 #include "llvm/Support/Format.h"
31 #include "llvm/Support/ManagedStatic.h"
32 #include "llvm/Support/MemoryBuffer.h"
33 #include "llvm/Support/PrettyStackTrace.h"
34 #include "llvm/Support/Program.h"
35 #include "llvm/Support/Signals.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include "llvm/Support/system_error.h"
44 using namespace object;
47 enum OutputFormatTy { bsd, sysv, posix };
48 cl::opt<OutputFormatTy> OutputFormat(
49 "format", cl::desc("Specify output format"),
50 cl::values(clEnumVal(bsd, "BSD format"), clEnumVal(sysv, "System V format"),
51 clEnumVal(posix, "POSIX.2 format"), clEnumValEnd),
53 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
54 cl::aliasopt(OutputFormat));
56 cl::list<std::string> InputFilenames(cl::Positional,
57 cl::desc("<input bitcode files>"),
60 cl::opt<bool> UndefinedOnly("undefined-only",
61 cl::desc("Show only undefined symbols"));
62 cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
63 cl::aliasopt(UndefinedOnly));
65 cl::opt<bool> DynamicSyms("dynamic",
66 cl::desc("Display the dynamic symbols instead "
67 "of normal symbols."));
68 cl::alias DynamicSyms2("D", cl::desc("Alias for --dynamic"),
69 cl::aliasopt(DynamicSyms));
71 cl::opt<bool> DefinedOnly("defined-only",
72 cl::desc("Show only defined symbols"));
74 cl::opt<bool> ExternalOnly("extern-only",
75 cl::desc("Show only external symbols"));
76 cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
77 cl::aliasopt(ExternalOnly));
79 cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
80 cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
82 cl::opt<bool> PrintFileName(
84 cl::desc("Precede each symbol with the object file it came from"));
86 cl::alias PrintFileNameA("A", cl::desc("Alias for --print-file-name"),
87 cl::aliasopt(PrintFileName));
88 cl::alias PrintFileNameo("o", cl::desc("Alias for --print-file-name"),
89 cl::aliasopt(PrintFileName));
91 cl::opt<bool> DebugSyms("debug-syms",
92 cl::desc("Show all symbols, even debugger only"));
93 cl::alias DebugSymsa("a", cl::desc("Alias for --debug-syms"),
94 cl::aliasopt(DebugSyms));
96 cl::opt<bool> NumericSort("numeric-sort", cl::desc("Sort symbols by address"));
97 cl::alias NumericSortn("n", cl::desc("Alias for --numeric-sort"),
98 cl::aliasopt(NumericSort));
99 cl::alias NumericSortv("v", cl::desc("Alias for --numeric-sort"),
100 cl::aliasopt(NumericSort));
102 cl::opt<bool> NoSort("no-sort", cl::desc("Show symbols in order encountered"));
103 cl::alias NoSortp("p", cl::desc("Alias for --no-sort"), cl::aliasopt(NoSort));
105 cl::opt<bool> PrintSize("print-size",
106 cl::desc("Show symbol size instead of address"));
107 cl::alias PrintSizeS("S", cl::desc("Alias for --print-size"),
108 cl::aliasopt(PrintSize));
110 cl::opt<bool> SizeSort("size-sort", cl::desc("Sort symbols by size"));
112 cl::opt<bool> WithoutAliases("without-aliases", cl::Hidden,
113 cl::desc("Exclude aliases from output"));
115 cl::opt<bool> ArchiveMap("print-armap", cl::desc("Print the archive map"));
116 cl::alias ArchiveMaps("s", cl::desc("Alias for --print-armap"),
117 cl::aliasopt(ArchiveMap));
118 bool PrintAddress = true;
120 bool MultipleFiles = false;
122 bool HadError = false;
124 std::string ToolName;
127 static void error(Twine Message, Twine Path = Twine()) {
129 errs() << ToolName << ": " << Path << ": " << Message << ".\n";
132 static bool error(error_code EC, Twine Path = Twine()) {
134 error(EC.message(), Path);
149 static bool compareSymbolAddress(const NMSymbol &A, const NMSymbol &B) {
150 if (A.Address < B.Address)
152 else if (A.Address == B.Address && A.Name < B.Name)
154 else if (A.Address == B.Address && A.Name == B.Name && A.Size < B.Size)
160 static bool compareSymbolSize(const NMSymbol &A, const NMSymbol &B) {
163 else if (A.Size == B.Size && A.Name < B.Name)
165 else if (A.Size == B.Size && A.Name == B.Name && A.Address < B.Address)
171 static bool compareSymbolName(const NMSymbol &A, const NMSymbol &B) {
174 else if (A.Name == B.Name && A.Size < B.Size)
176 else if (A.Name == B.Name && A.Size == B.Size && A.Address < B.Address)
182 static StringRef CurrentFilename;
183 typedef std::vector<NMSymbol> SymbolListT;
184 static SymbolListT SymbolList;
186 static void sortAndPrintSymbolList() {
189 std::sort(SymbolList.begin(), SymbolList.end(), compareSymbolAddress);
191 std::sort(SymbolList.begin(), SymbolList.end(), compareSymbolSize);
193 std::sort(SymbolList.begin(), SymbolList.end(), compareSymbolName);
196 if (OutputFormat == posix && MultipleFiles) {
197 outs() << '\n' << CurrentFilename << ":\n";
198 } else if (OutputFormat == bsd && MultipleFiles) {
199 outs() << "\n" << CurrentFilename << ":\n";
200 } else if (OutputFormat == sysv) {
201 outs() << "\n\nSymbols from " << CurrentFilename << ":\n\n"
202 << "Name Value Class Type"
203 << " Size Line Section\n";
206 for (SymbolListT::iterator I = SymbolList.begin(), E = SymbolList.end();
208 if ((I->TypeChar != 'U') && UndefinedOnly)
210 if ((I->TypeChar == 'U') && DefinedOnly)
212 if (SizeSort && !PrintAddress && I->Size == UnknownAddressOrSize)
215 char SymbolAddrStr[10] = "";
216 char SymbolSizeStr[10] = "";
218 if (OutputFormat == sysv || I->Address == UnknownAddressOrSize)
219 strcpy(SymbolAddrStr, " ");
220 if (OutputFormat == sysv)
221 strcpy(SymbolSizeStr, " ");
223 if (I->Address != UnknownAddressOrSize)
224 format("%08" PRIx64, I->Address)
225 .print(SymbolAddrStr, sizeof(SymbolAddrStr));
226 if (I->Size != UnknownAddressOrSize)
227 format("%08" PRIx64, I->Size).print(SymbolSizeStr, sizeof(SymbolSizeStr));
229 if (OutputFormat == posix) {
230 outs() << I->Name << " " << I->TypeChar << " " << SymbolAddrStr
231 << SymbolSizeStr << "\n";
232 } else if (OutputFormat == bsd) {
234 outs() << SymbolAddrStr << ' ';
236 outs() << SymbolSizeStr;
237 if (I->Size != UnknownAddressOrSize)
240 outs() << I->TypeChar << " " << I->Name << "\n";
241 } else if (OutputFormat == sysv) {
242 std::string PaddedName(I->Name);
243 while (PaddedName.length() < 20)
245 outs() << PaddedName << "|" << SymbolAddrStr << "| " << I->TypeChar
246 << " | |" << SymbolSizeStr << "| |\n";
253 static char typeCharForSymbol(GlobalValue &GV) {
254 if (GV.isDeclaration())
256 if (GV.hasLinkOnceLinkage())
258 if (GV.hasCommonLinkage())
260 if (GV.hasWeakLinkage())
262 if (isa<Function>(GV) && GV.hasInternalLinkage())
264 if (isa<Function>(GV))
266 if (isa<GlobalVariable>(GV) && GV.hasInternalLinkage())
268 if (isa<GlobalVariable>(GV))
270 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(&GV)) {
271 const GlobalValue *AliasedGV = GA->getAliasedGlobal();
272 if (isa<Function>(AliasedGV))
274 if (isa<GlobalVariable>(AliasedGV))
280 static void dumpSymbolNameForGlobalValue(GlobalValue &GV) {
281 // Private linkage and available_externally linkage don't exist in symtab.
282 if (GV.hasPrivateLinkage() || GV.hasLinkerPrivateLinkage() ||
283 GV.hasLinkerPrivateWeakLinkage() || GV.hasAvailableExternallyLinkage())
285 char TypeChar = typeCharForSymbol(GV);
286 if (GV.hasLocalLinkage() && ExternalOnly)
290 S.Address = UnknownAddressOrSize;
291 S.Size = UnknownAddressOrSize;
292 S.TypeChar = TypeChar;
293 S.Name = GV.getName();
294 SymbolList.push_back(S);
297 static void dumpSymbolNamesFromModule(Module *M) {
298 CurrentFilename = M->getModuleIdentifier();
299 std::for_each(M->begin(), M->end(), dumpSymbolNameForGlobalValue);
300 std::for_each(M->global_begin(), M->global_end(),
301 dumpSymbolNameForGlobalValue);
303 std::for_each(M->alias_begin(), M->alias_end(),
304 dumpSymbolNameForGlobalValue);
306 sortAndPrintSymbolList();
309 template <class ELFT>
310 static char getSymbolNMTypeChar(ELFObjectFile<ELFT> &Obj, symbol_iterator I) {
311 typedef typename ELFObjectFile<ELFT>::Elf_Sym Elf_Sym;
312 typedef typename ELFObjectFile<ELFT>::Elf_Shdr Elf_Shdr;
314 DataRefImpl Symb = I->getRawDataRefImpl();
315 const Elf_Sym *ESym = Obj.getSymbol(Symb);
316 const ELFFile<ELFT> &EF = *Obj.getELFFile();
317 const Elf_Shdr *ESec = EF.getSection(ESym);
320 switch (ESec->sh_type) {
321 case ELF::SHT_PROGBITS:
322 case ELF::SHT_DYNAMIC:
323 switch (ESec->sh_flags) {
324 case(ELF::SHF_ALLOC | ELF::SHF_EXECINSTR) :
326 case(ELF::SHF_TLS | ELF::SHF_ALLOC | ELF::SHF_WRITE) :
327 case(ELF::SHF_ALLOC | ELF::SHF_WRITE) :
330 case(ELF::SHF_ALLOC | ELF::SHF_MERGE) :
331 case(ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS) :
335 case ELF::SHT_NOBITS:
340 if (ESym->getType() == ELF::STT_SECTION) {
342 if (error(I->getName(Name)))
344 return StringSwitch<char>(Name)
345 .StartsWith(".debug", 'N')
346 .StartsWith(".note", 'n')
353 static char getSymbolNMTypeChar(COFFObjectFile &Obj, symbol_iterator I) {
354 const coff_symbol *Symb = Obj.getCOFFSymbol(I);
356 if (error(I->getName(Name)))
358 char Ret = StringSwitch<char>(Name)
359 .StartsWith(".debug", 'N')
360 .StartsWith(".sxdata", 'N')
366 uint32_t Characteristics = 0;
367 if (Symb->SectionNumber > 0) {
368 section_iterator SecI = Obj.end_sections();
369 if (error(I->getSection(SecI)))
371 const coff_section *Section = Obj.getCOFFSection(SecI);
372 Characteristics = Section->Characteristics;
375 switch (Symb->SectionNumber) {
376 case COFF::IMAGE_SYM_DEBUG:
379 // Check section type.
380 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
382 else if (Characteristics & COFF::IMAGE_SCN_MEM_READ &&
383 ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
385 else if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
387 else if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
389 else if (Characteristics & COFF::IMAGE_SCN_LNK_INFO)
392 // Check for section symbol.
393 else if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC &&
401 static uint8_t getNType(MachOObjectFile &Obj, DataRefImpl Symb) {
403 MachO::nlist_64 STE = Obj.getSymbol64TableEntry(Symb);
406 MachO::nlist STE = Obj.getSymbolTableEntry(Symb);
410 static char getSymbolNMTypeChar(MachOObjectFile &Obj, symbol_iterator I) {
411 DataRefImpl Symb = I->getRawDataRefImpl();
412 uint8_t NType = getNType(Obj, Symb);
414 switch (NType & MachO::N_TYPE) {
417 case MachO::N_SECT: {
418 section_iterator Sec = Obj.end_sections();
419 Obj.getSymbolSection(Symb, Sec);
420 DataRefImpl Ref = Sec->getRawDataRefImpl();
421 StringRef SectionName;
422 Obj.getSectionName(Ref, SectionName);
423 StringRef SegmentName = Obj.getSectionFinalSegmentName(Ref);
424 if (SegmentName == "__TEXT" && SectionName == "__text")
434 template <class ELFT>
435 static bool isObject(ELFObjectFile<ELFT> &Obj, symbol_iterator I) {
436 typedef typename ELFObjectFile<ELFT>::Elf_Sym Elf_Sym;
438 DataRefImpl Symb = I->getRawDataRefImpl();
439 const Elf_Sym *ESym = Obj.getSymbol(Symb);
441 return ESym->getType() == ELF::STT_OBJECT;
444 static bool isObject(ObjectFile *Obj, symbol_iterator I) {
445 if (ELF32LEObjectFile *ELF = dyn_cast<ELF32LEObjectFile>(Obj))
446 return isObject(*ELF, I);
447 if (ELF64LEObjectFile *ELF = dyn_cast<ELF64LEObjectFile>(Obj))
448 return isObject(*ELF, I);
449 if (ELF32BEObjectFile *ELF = dyn_cast<ELF32BEObjectFile>(Obj))
450 return isObject(*ELF, I);
451 if (ELF64BEObjectFile *ELF = dyn_cast<ELF64BEObjectFile>(Obj))
452 return isObject(*ELF, I);
456 static char getNMTypeChar(ObjectFile *Obj, symbol_iterator I) {
457 uint32_t Symflags = I->getFlags();
458 if ((Symflags & object::SymbolRef::SF_Weak) && !isa<MachOObjectFile>(Obj)) {
459 char Ret = isObject(Obj, I) ? 'v' : 'w';
460 if (!(Symflags & object::SymbolRef::SF_Undefined))
465 if (Symflags & object::SymbolRef::SF_Undefined)
468 if (Symflags & object::SymbolRef::SF_Common)
472 if (Symflags & object::SymbolRef::SF_Absolute)
474 else if (COFFObjectFile *COFF = dyn_cast<COFFObjectFile>(Obj))
475 Ret = getSymbolNMTypeChar(*COFF, I);
476 else if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(Obj))
477 Ret = getSymbolNMTypeChar(*MachO, I);
478 else if (ELF32LEObjectFile *ELF = dyn_cast<ELF32LEObjectFile>(Obj))
479 Ret = getSymbolNMTypeChar(*ELF, I);
480 else if (ELF64LEObjectFile *ELF = dyn_cast<ELF64LEObjectFile>(Obj))
481 Ret = getSymbolNMTypeChar(*ELF, I);
482 else if (ELF32BEObjectFile *ELF = dyn_cast<ELF32BEObjectFile>(Obj))
483 Ret = getSymbolNMTypeChar(*ELF, I);
485 Ret = getSymbolNMTypeChar(*cast<ELF64BEObjectFile>(Obj), I);
487 if (Symflags & object::SymbolRef::SF_Global)
493 static void getDynamicSymbolIterators(ObjectFile *Obj, symbol_iterator &Begin,
494 symbol_iterator &End) {
495 if (ELF32LEObjectFile *ELF = dyn_cast<ELF32LEObjectFile>(Obj)) {
496 Begin = ELF->begin_dynamic_symbols();
497 End = ELF->end_dynamic_symbols();
500 if (ELF64LEObjectFile *ELF = dyn_cast<ELF64LEObjectFile>(Obj)) {
501 Begin = ELF->begin_dynamic_symbols();
502 End = ELF->end_dynamic_symbols();
505 if (ELF32BEObjectFile *ELF = dyn_cast<ELF32BEObjectFile>(Obj)) {
506 Begin = ELF->begin_dynamic_symbols();
507 End = ELF->end_dynamic_symbols();
510 ELF64BEObjectFile *ELF = cast<ELF64BEObjectFile>(Obj);
511 Begin = ELF->begin_dynamic_symbols();
512 End = ELF->end_dynamic_symbols();
516 static void dumpSymbolNamesFromObject(ObjectFile *Obj) {
517 symbol_iterator IBegin = Obj->begin_symbols();
518 symbol_iterator IEnd = Obj->end_symbols();
521 error("File format has no dynamic symbol table", Obj->getFileName());
524 getDynamicSymbolIterators(Obj, IBegin, IEnd);
526 for (symbol_iterator I = IBegin; I != IEnd; ++I) {
527 uint32_t SymFlags = I->getFlags();
528 if (!DebugSyms && (SymFlags & SymbolRef::SF_FormatSpecific))
531 S.Size = UnknownAddressOrSize;
532 S.Address = UnknownAddressOrSize;
533 if (PrintSize || SizeSort) {
534 if (error(I->getSize(S.Size)))
538 if (error(I->getAddress(S.Address)))
540 S.TypeChar = getNMTypeChar(Obj, I);
541 if (error(I->getName(S.Name)))
543 SymbolList.push_back(S);
546 CurrentFilename = Obj->getFileName();
547 sortAndPrintSymbolList();
550 static void dumpSymbolNamesFromFile(std::string &Filename) {
551 OwningPtr<MemoryBuffer> Buffer;
552 if (error(MemoryBuffer::getFileOrSTDIN(Filename, Buffer), Filename))
555 sys::fs::file_magic Magic = sys::fs::identify_magic(Buffer->getBuffer());
557 LLVMContext &Context = getGlobalContext();
558 if (Magic == sys::fs::file_magic::bitcode) {
559 ErrorOr<Module *> ModuleOrErr = parseBitcodeFile(Buffer.get(), Context);
560 if (error(ModuleOrErr.getError(), Filename)) {
563 Module *Result = ModuleOrErr.get();
564 dumpSymbolNamesFromModule(Result);
570 ErrorOr<Binary *> BinaryOrErr = createBinary(Buffer.take(), Magic);
571 if (error(BinaryOrErr.getError(), Filename))
573 OwningPtr<Binary> Bin(BinaryOrErr.get());
575 if (Archive *A = dyn_cast<Archive>(Bin.get())) {
577 Archive::symbol_iterator I = A->symbol_begin();
578 Archive::symbol_iterator E = A->symbol_end();
580 outs() << "Archive map\n";
581 for (; I != E; ++I) {
582 Archive::child_iterator C;
585 if (error(I->getMember(C)))
587 if (error(I->getName(SymName)))
589 if (error(C->getName(FileName)))
591 outs() << SymName << " in " << FileName << "\n";
597 for (Archive::child_iterator I = A->child_begin(), E = A->child_end();
599 OwningPtr<Binary> Child;
600 if (I->getAsBinary(Child)) {
601 // Try opening it as a bitcode file.
602 OwningPtr<MemoryBuffer> Buff;
603 if (error(I->getMemoryBuffer(Buff)))
606 ErrorOr<Module *> ModuleOrErr = parseBitcodeFile(Buff.get(), Context);
608 Module *Result = ModuleOrErr.get();
609 dumpSymbolNamesFromModule(Result);
614 if (ObjectFile *O = dyn_cast<ObjectFile>(Child.get())) {
615 outs() << O->getFileName() << ":\n";
616 dumpSymbolNamesFromObject(O);
621 if (MachOUniversalBinary *UB =
622 dyn_cast<object::MachOUniversalBinary>(Bin.get())) {
623 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
624 E = UB->end_objects();
626 OwningPtr<ObjectFile> Obj;
627 if (!I->getAsObjectFile(Obj)) {
628 outs() << Obj->getFileName() << ":\n";
629 dumpSymbolNamesFromObject(Obj.get());
634 if (ObjectFile *O = dyn_cast<ObjectFile>(Bin.get())) {
635 dumpSymbolNamesFromObject(O);
638 error("unrecognizable file type", Filename);
642 int main(int argc, char **argv) {
643 // Print a stack trace if we signal out.
644 sys::PrintStackTraceOnErrorSignal();
645 PrettyStackTraceProgram X(argc, argv);
647 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
648 cl::ParseCommandLineOptions(argc, argv, "llvm symbol table dumper\n");
650 // llvm-nm only reads binary files.
651 if (error(sys::ChangeStdinToBinary()))
658 OutputFormat = posix;
660 // The relative order of these is important. If you pass --size-sort it should
661 // only print out the size. However, if you pass -S --size-sort, it should
662 // print out both the size and address.
663 if (SizeSort && !PrintSize)
664 PrintAddress = false;
665 if (OutputFormat == sysv || SizeSort)
668 switch (InputFilenames.size()) {
670 InputFilenames.push_back("-");
674 MultipleFiles = true;
677 std::for_each(InputFilenames.begin(), InputFilenames.end(),
678 dumpSymbolNamesFromFile);