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>
49 OutputFormat("format",
50 cl::desc("Specify output format"),
51 cl::values(clEnumVal(bsd, "BSD format"),
52 clEnumVal(sysv, "System V format"),
53 clEnumVal(posix, "POSIX.2 format"),
54 clEnumValEnd), cl::init(bsd));
55 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
56 cl::aliasopt(OutputFormat));
59 InputFilenames(cl::Positional, cl::desc("<input bitcode files>"),
62 cl::opt<bool> UndefinedOnly("undefined-only",
63 cl::desc("Show only undefined symbols"));
64 cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
65 cl::aliasopt(UndefinedOnly));
67 cl::opt<bool> DynamicSyms("dynamic",
68 cl::desc("Display the dynamic symbols instead "
69 "of normal symbols."));
70 cl::alias DynamicSyms2("D", cl::desc("Alias for --dynamic"),
71 cl::aliasopt(DynamicSyms));
73 cl::opt<bool> DefinedOnly("defined-only",
74 cl::desc("Show only defined symbols"));
76 cl::opt<bool> ExternalOnly("extern-only",
77 cl::desc("Show only external symbols"));
78 cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
79 cl::aliasopt(ExternalOnly));
81 cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
82 cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
84 cl::opt<bool> PrintFileName("print-file-name",
85 cl::desc("Precede each symbol with the object file it came from"));
87 cl::alias PrintFileNameA("A", cl::desc("Alias for --print-file-name"),
88 cl::aliasopt(PrintFileName));
89 cl::alias PrintFileNameo("o", cl::desc("Alias for --print-file-name"),
90 cl::aliasopt(PrintFileName));
92 cl::opt<bool> DebugSyms("debug-syms",
93 cl::desc("Show all symbols, even debugger only"));
94 cl::alias DebugSymsa("a", cl::desc("Alias for --debug-syms"),
95 cl::aliasopt(DebugSyms));
97 cl::opt<bool> NumericSort("numeric-sort",
98 cl::desc("Sort symbols by address"));
99 cl::alias NumericSortn("n", cl::desc("Alias for --numeric-sort"),
100 cl::aliasopt(NumericSort));
101 cl::alias NumericSortv("v", cl::desc("Alias for --numeric-sort"),
102 cl::aliasopt(NumericSort));
104 cl::opt<bool> NoSort("no-sort",
105 cl::desc("Show symbols in order encountered"));
106 cl::alias NoSortp("p", cl::desc("Alias for --no-sort"),
107 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",
120 cl::desc("Print the archive map"));
121 cl::alias ArchiveMaps("s", cl::desc("Alias for --print-armap"),
122 cl::aliasopt(ArchiveMap));
123 bool PrintAddress = true;
125 bool MultipleFiles = false;
127 bool HadError = false;
129 std::string ToolName;
133 static void error(Twine message, Twine path = Twine()) {
134 errs() << ToolName << ": " << path << ": " << message << ".\n";
137 static bool error(error_code ec, Twine path = Twine()) {
139 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)
166 static bool CompareSymbolSize(const NMSymbol &a, const NMSymbol &b) {
169 else if (a.Size == b.Size && a.Name < b.Name)
171 else if (a.Size == b.Size && a.Name == b.Name && a.Address < b.Address)
177 static bool CompareSymbolName(const NMSymbol &a, const NMSymbol &b) {
180 else if (a.Name == b.Name && a.Size < b.Size)
182 else if (a.Name == b.Name && a.Size == b.Size && a.Address < b.Address)
188 StringRef CurrentFilename;
189 typedef std::vector<NMSymbol> SymbolListT;
190 SymbolListT SymbolList;
193 static void SortAndPrintSymbolList() {
196 std::sort(SymbolList.begin(), SymbolList.end(), CompareSymbolAddress);
198 std::sort(SymbolList.begin(), SymbolList.end(), CompareSymbolSize);
200 std::sort(SymbolList.begin(), SymbolList.end(), CompareSymbolName);
203 if (OutputFormat == posix && MultipleFiles) {
204 outs() << '\n' << CurrentFilename << ":\n";
205 } else if (OutputFormat == bsd && MultipleFiles) {
206 outs() << "\n" << CurrentFilename << ":\n";
207 } else if (OutputFormat == sysv) {
208 outs() << "\n\nSymbols from " << CurrentFilename << ":\n\n"
209 << "Name Value Class Type"
210 << " Size Line Section\n";
213 for (SymbolListT::iterator i = SymbolList.begin(),
214 e = SymbolList.end(); i != e; ++i) {
215 if ((i->TypeChar != 'U') && UndefinedOnly)
217 if ((i->TypeChar == 'U') && DefinedOnly)
219 if (SizeSort && !PrintAddress && i->Size == UnknownAddressOrSize)
222 char SymbolAddrStr[10] = "";
223 char SymbolSizeStr[10] = "";
225 if (OutputFormat == sysv || i->Address == object::UnknownAddressOrSize)
226 strcpy(SymbolAddrStr, " ");
227 if (OutputFormat == sysv)
228 strcpy(SymbolSizeStr, " ");
230 if (i->Address != object::UnknownAddressOrSize)
231 format("%08" PRIx64, i->Address).print(SymbolAddrStr,
232 sizeof(SymbolAddrStr));
233 if (i->Size != object::UnknownAddressOrSize)
234 format("%08" PRIx64, i->Size).print(SymbolSizeStr, sizeof(SymbolSizeStr));
236 if (OutputFormat == posix) {
237 outs() << i->Name << " " << i->TypeChar << " "
238 << SymbolAddrStr << SymbolSizeStr << "\n";
239 } else if (OutputFormat == bsd) {
241 outs() << SymbolAddrStr << ' ';
243 outs() << SymbolSizeStr;
244 if (i->Size != object::UnknownAddressOrSize)
247 outs() << i->TypeChar << " " << i->Name << "\n";
248 } else if (OutputFormat == sysv) {
249 std::string PaddedName (i->Name);
250 while (PaddedName.length () < 20)
252 outs() << PaddedName << "|" << SymbolAddrStr << "| "
254 << " | |" << SymbolSizeStr << "| |\n";
261 static char TypeCharForSymbol(GlobalValue &GV) {
262 if (GV.isDeclaration()) return 'U';
263 if (GV.hasLinkOnceLinkage()) return 'C';
264 if (GV.hasCommonLinkage()) return 'C';
265 if (GV.hasWeakLinkage()) return 'W';
266 if (isa<Function>(GV) && GV.hasInternalLinkage()) return 't';
267 if (isa<Function>(GV)) return 'T';
268 if (isa<GlobalVariable>(GV) && GV.hasInternalLinkage()) return 'd';
269 if (isa<GlobalVariable>(GV)) return 'D';
270 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(&GV)) {
271 const GlobalValue *AliasedGV = GA->getAliasedGlobal();
272 if (isa<Function>(AliasedGV)) return 'T';
273 if (isa<GlobalVariable>(AliasedGV)) return 'D';
278 static void DumpSymbolNameForGlobalValue(GlobalValue &GV) {
279 // Private linkage and available_externally linkage don't exist in symtab.
280 if (GV.hasPrivateLinkage() ||
281 GV.hasLinkerPrivateLinkage() ||
282 GV.hasLinkerPrivateWeakLinkage() ||
283 GV.hasAvailableExternallyLinkage())
285 char TypeChar = TypeCharForSymbol(GV);
286 if (GV.hasLocalLinkage () && ExternalOnly)
290 s.Address = object::UnknownAddressOrSize;
291 s.Size = object::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 error_code getSymbolNMTypeChar(ELFObjectFile<ELFT> &Obj, symbol_iterator I,
312 typedef typename ELFObjectFile<ELFT>::Elf_Sym Elf_Sym;
313 typedef typename ELFObjectFile<ELFT>::Elf_Shdr Elf_Shdr;
315 DataRefImpl Symb = I->getRawDataRefImpl();
316 const Elf_Sym *ESym = Obj.getSymbol(Symb);
317 const ELFFile<ELFT> &EF = *Obj.getELFFile();
318 const Elf_Shdr *ESec = EF.getSection(ESym);
323 switch (ESec->sh_type) {
324 case ELF::SHT_PROGBITS:
325 case ELF::SHT_DYNAMIC:
326 switch (ESec->sh_flags) {
327 case(ELF::SHF_ALLOC | ELF::SHF_EXECINSTR) :
330 case(ELF::SHF_ALLOC | ELF::SHF_WRITE) :
334 case(ELF::SHF_ALLOC | ELF::SHF_MERGE) :
335 case(ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS) :
340 case ELF::SHT_NOBITS:
345 switch (EF.getSymbolTableIndex(ESym)) {
353 case ELF::SHN_COMMON:
358 switch (ESym->getBinding()) {
359 case ELF::STB_GLOBAL:
360 ret = ::toupper(ret);
363 if (EF.getSymbolTableIndex(ESym) == ELF::SHN_UNDEF)
365 else if (ESym->getType() == ELF::STT_OBJECT)
371 if (ret == '?' && ESym->getType() == ELF::STT_SECTION) {
373 error_code EC = I->getName(Name);
376 Result = StringSwitch<char>(Name)
377 .StartsWith(".debug", 'N')
378 .StartsWith(".note", 'n')
380 return object_error::success;
384 return object_error::success;
387 static error_code getSymbolNMTypeChar(COFFObjectFile &Obj, symbol_iterator I,
389 const coff_symbol *symb = Obj.getCOFFSymbol(I);
391 if (error_code ec = I->getName(name))
393 char ret = StringSwitch<char>(name)
394 .StartsWith(".debug", 'N')
395 .StartsWith(".sxdata", 'N')
400 return object_error::success;
403 uint32_t Characteristics = 0;
404 if (symb->SectionNumber > 0) {
405 section_iterator SecI = Obj.end_sections();
406 if (error_code ec = I->getSection(SecI))
408 const coff_section *Section = Obj.getCOFFSection(SecI);
409 Characteristics = Section->Characteristics;
412 switch (symb->SectionNumber) {
413 case COFF::IMAGE_SYM_UNDEFINED:
414 // Check storage classes.
415 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL) {
417 return object_error::success; // Don't do ::toupper.
418 } else if (symb->Value != 0) // Check for common symbols.
423 case COFF::IMAGE_SYM_ABSOLUTE:
426 case COFF::IMAGE_SYM_DEBUG:
430 // Check section type.
431 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
433 else if (Characteristics & COFF::IMAGE_SCN_MEM_READ &&
434 ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
436 else if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
438 else if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
440 else if (Characteristics & COFF::IMAGE_SCN_LNK_INFO)
443 // Check for section symbol.
444 else if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC &&
449 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
450 ret = ::toupper(static_cast<unsigned char>(ret));
453 return object_error::success;
456 static uint8_t getNType(MachOObjectFile &Obj, DataRefImpl Symb) {
458 MachO::nlist_64 STE = Obj.getSymbol64TableEntry(Symb);
461 MachO::nlist STE = Obj.getSymbolTableEntry(Symb);
465 static error_code getSymbolNMTypeChar(MachOObjectFile &Obj, symbol_iterator I,
467 DataRefImpl Symb = I->getRawDataRefImpl();
468 uint8_t NType = getNType(Obj, Symb);
471 switch (NType & MachO::N_TYPE) {
478 case MachO::N_SECT: {
479 section_iterator Sec = Obj.end_sections();
480 Obj.getSymbolSection(Symb, Sec);
481 DataRefImpl Ref = Sec->getRawDataRefImpl();
482 StringRef SectionName;
483 Obj.getSectionName(Ref, SectionName);
484 StringRef SegmentName = Obj.getSectionFinalSegmentName(Ref);
485 if (SegmentName == "__TEXT" && SectionName == "__text")
495 if (NType & (MachO::N_EXT | MachO::N_PEXT))
496 Char = toupper(static_cast<unsigned char>(Char));
498 return object_error::success;
501 static char getNMTypeChar(ObjectFile *Obj, symbol_iterator I) {
503 if (COFFObjectFile *COFF = dyn_cast<COFFObjectFile>(Obj)) {
504 error(getSymbolNMTypeChar(*COFF, I, Res));
507 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(Obj)) {
508 error(getSymbolNMTypeChar(*MachO, I, Res));
512 if (ELF32LEObjectFile *ELF = dyn_cast<ELF32LEObjectFile>(Obj)) {
513 error(getSymbolNMTypeChar(*ELF, I, Res));
516 if (ELF64LEObjectFile *ELF = dyn_cast<ELF64LEObjectFile>(Obj)) {
517 error(getSymbolNMTypeChar(*ELF, I, Res));
520 if (ELF32BEObjectFile *ELF = dyn_cast<ELF32BEObjectFile>(Obj)) {
521 error(getSymbolNMTypeChar(*ELF, I, Res));
524 ELF64BEObjectFile *ELF = cast<ELF64BEObjectFile>(Obj);
525 error(getSymbolNMTypeChar(*ELF, I, Res));
529 static void DumpSymbolNamesFromObject(ObjectFile *obj) {
531 symbol_iterator ibegin = obj->begin_symbols();
532 symbol_iterator iend = obj->end_symbols();
534 ibegin = obj->begin_dynamic_symbols();
535 iend = obj->end_dynamic_symbols();
537 for (symbol_iterator i = ibegin; i != iend; i.increment(ec)) {
538 if (error(ec)) break;
540 if (error(i->getFlags(symflags))) break;
541 if (!DebugSyms && (symflags & SymbolRef::SF_FormatSpecific))
544 s.Size = object::UnknownAddressOrSize;
545 s.Address = object::UnknownAddressOrSize;
546 if (PrintSize || SizeSort) {
547 if (error(i->getSize(s.Size))) break;
550 if (error(i->getAddress(s.Address))) break;
551 s.TypeChar = getNMTypeChar(obj, i);
552 if (error(i->getName(s.Name))) break;
553 SymbolList.push_back(s);
556 CurrentFilename = obj->getFileName();
557 SortAndPrintSymbolList();
560 static void DumpSymbolNamesFromFile(std::string &Filename) {
561 if (Filename != "-" && !sys::fs::exists(Filename)) {
562 errs() << ToolName << ": '" << Filename << "': " << "No such file\n";
566 OwningPtr<MemoryBuffer> Buffer;
567 if (error(MemoryBuffer::getFileOrSTDIN(Filename, Buffer), Filename))
570 sys::fs::file_magic magic = sys::fs::identify_magic(Buffer->getBuffer());
572 LLVMContext &Context = getGlobalContext();
573 if (magic == sys::fs::file_magic::bitcode) {
574 ErrorOr<Module *> ModuleOrErr = parseBitcodeFile(Buffer.get(), Context);
575 if (error(ModuleOrErr.getError(), Filename)) {
578 Module *Result = ModuleOrErr.get();
579 DumpSymbolNamesFromModule(Result);
582 } else if (magic == sys::fs::file_magic::archive) {
583 ErrorOr<Binary *> BinaryOrErr = object::createBinary(Buffer.take());
584 if (error(BinaryOrErr.getError(), Filename))
586 OwningPtr<Binary> arch(BinaryOrErr.get());
588 if (object::Archive *a = dyn_cast<object::Archive>(arch.get())) {
590 object::Archive::symbol_iterator I = a->symbol_begin();
591 object::Archive::symbol_iterator E = a->symbol_end();
593 outs() << "Archive map" << "\n";
594 for (; I != E; ++I) {
595 object::Archive::child_iterator c;
598 if (error(I->getMember(c)))
600 if (error(I->getName(symname)))
602 if (error(c->getName(filename)))
604 outs() << symname << " in " << filename << "\n";
610 for (object::Archive::child_iterator i = a->child_begin(),
611 e = a->child_end(); i != e; ++i) {
612 OwningPtr<Binary> child;
613 if (i->getAsBinary(child)) {
614 // Try opening it as a bitcode file.
615 OwningPtr<MemoryBuffer> buff;
616 if (error(i->getMemoryBuffer(buff)))
619 ErrorOr<Module *> ModuleOrErr = parseBitcodeFile(buff.get(), Context);
621 Module *Result = ModuleOrErr.get();
622 DumpSymbolNamesFromModule(Result);
627 if (object::ObjectFile *o = dyn_cast<ObjectFile>(child.get())) {
628 outs() << o->getFileName() << ":\n";
629 DumpSymbolNamesFromObject(o);
633 } else if (magic == sys::fs::file_magic::macho_universal_binary) {
634 ErrorOr<Binary *> BinaryOrErr = object::createBinary(Buffer.take());
635 if (error(BinaryOrErr.getError(), Filename))
637 OwningPtr<Binary> Bin(BinaryOrErr.get());
639 object::MachOUniversalBinary *UB =
640 cast<object::MachOUniversalBinary>(Bin.get());
641 for (object::MachOUniversalBinary::object_iterator
642 I = UB->begin_objects(),
643 E = UB->end_objects();
645 OwningPtr<ObjectFile> Obj;
646 if (!I->getAsObjectFile(Obj)) {
647 outs() << Obj->getFileName() << ":\n";
648 DumpSymbolNamesFromObject(Obj.get());
651 } else if (magic.is_object()) {
652 ErrorOr<Binary *> BinaryOrErr = object::createBinary(Buffer.take());
653 if (error(BinaryOrErr.getError(), Filename))
655 OwningPtr<Binary> obj(BinaryOrErr.get());
656 if (object::ObjectFile *o = dyn_cast<ObjectFile>(obj.get()))
657 DumpSymbolNamesFromObject(o);
659 errs() << ToolName << ": " << Filename << ": "
660 << "unrecognizable file type\n";
666 int main(int argc, char **argv) {
667 // Print a stack trace if we signal out.
668 sys::PrintStackTraceOnErrorSignal();
669 PrettyStackTraceProgram X(argc, argv);
671 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
672 cl::ParseCommandLineOptions(argc, argv, "llvm symbol table dumper\n");
674 // llvm-nm only reads binary files.
675 if (error(sys::ChangeStdinToBinary()))
679 if (BSDFormat) OutputFormat = bsd;
680 if (POSIXFormat) OutputFormat = posix;
682 // The relative order of these is important. If you pass --size-sort it should
683 // only print out the size. However, if you pass -S --size-sort, it should
684 // print out both the size and address.
685 if (SizeSort && !PrintSize) PrintAddress = false;
686 if (OutputFormat == sysv || SizeSort) PrintSize = true;
688 switch (InputFilenames.size()) {
689 case 0: InputFilenames.push_back("-");
691 default: MultipleFiles = true;
694 std::for_each(InputFilenames.begin(), InputFilenames.end(),
695 DumpSymbolNamesFromFile);