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"
40 #include "llvm/Support/system_error.h"
47 using namespace object;
50 enum OutputFormatTy { bsd, sysv, posix };
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"), clEnumValEnd),
56 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
57 cl::aliasopt(OutputFormat));
59 cl::list<std::string> InputFilenames(cl::Positional, cl::desc("<input 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(
86 cl::desc("Precede each symbol with the object file it came from"));
88 cl::alias PrintFileNameA("A", cl::desc("Alias for --print-file-name"),
89 cl::aliasopt(PrintFileName));
90 cl::alias PrintFileNameo("o", cl::desc("Alias for --print-file-name"),
91 cl::aliasopt(PrintFileName));
93 cl::opt<bool> DebugSyms("debug-syms",
94 cl::desc("Show all symbols, even debugger only"));
95 cl::alias DebugSymsa("a", cl::desc("Alias for --debug-syms"),
96 cl::aliasopt(DebugSyms));
98 cl::opt<bool> NumericSort("numeric-sort", 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", cl::desc("Show symbols in order encountered"));
105 cl::alias NoSortp("p", cl::desc("Alias for --no-sort"), cl::aliasopt(NoSort));
107 cl::opt<bool> PrintSize("print-size",
108 cl::desc("Show symbol size instead of address"));
109 cl::alias PrintSizeS("S", cl::desc("Alias for --print-size"),
110 cl::aliasopt(PrintSize));
112 cl::opt<bool> SizeSort("size-sort", cl::desc("Sort symbols by size"));
114 cl::opt<bool> WithoutAliases("without-aliases", cl::Hidden,
115 cl::desc("Exclude aliases from output"));
117 cl::opt<bool> ArchiveMap("print-armap", cl::desc("Print the archive map"));
118 cl::alias ArchiveMaps("s", cl::desc("Alias for --print-armap"),
119 cl::aliasopt(ArchiveMap));
120 bool PrintAddress = true;
122 bool MultipleFiles = false;
124 bool HadError = false;
126 std::string ToolName;
129 static void error(Twine Message, Twine Path = Twine()) {
131 errs() << ToolName << ": " << Path << ": " << Message << ".\n";
134 static bool error(error_code EC, Twine Path = Twine()) {
136 error(EC.message(), Path);
151 static bool compareSymbolAddress(const NMSymbol &A, const NMSymbol &B) {
152 if (A.Address < B.Address)
154 else if (A.Address == B.Address && A.Name < B.Name)
156 else if (A.Address == B.Address && A.Name == B.Name && A.Size < B.Size)
162 static bool compareSymbolSize(const NMSymbol &A, const NMSymbol &B) {
165 else if (A.Size == B.Size && A.Name < B.Name)
167 else if (A.Size == B.Size && A.Name == B.Name && A.Address < B.Address)
173 static bool compareSymbolName(const NMSymbol &A, const NMSymbol &B) {
176 else if (A.Name == B.Name && A.Size < B.Size)
178 else if (A.Name == B.Name && A.Size == B.Size && A.Address < B.Address)
184 static char isSymbolList64Bit(SymbolicFile *Obj) {
185 if (isa<IRObjectFile>(Obj))
187 else if (isa<COFFObjectFile>(Obj))
189 else if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(Obj))
190 return MachO->is64Bit();
191 else if (isa<ELF32LEObjectFile>(Obj))
193 else if (isa<ELF64LEObjectFile>(Obj))
195 else if (isa<ELF32BEObjectFile>(Obj))
197 else if(isa<ELF64BEObjectFile>(Obj))
203 static StringRef CurrentFilename;
204 typedef std::vector<NMSymbol> SymbolListT;
205 static SymbolListT SymbolList;
207 static void sortAndPrintSymbolList(SymbolicFile *Obj) {
210 std::sort(SymbolList.begin(), SymbolList.end(), compareSymbolAddress);
212 std::sort(SymbolList.begin(), SymbolList.end(), compareSymbolSize);
214 std::sort(SymbolList.begin(), SymbolList.end(), compareSymbolName);
217 if (OutputFormat == posix && MultipleFiles) {
218 outs() << '\n' << CurrentFilename << ":\n";
219 } else if (OutputFormat == bsd && MultipleFiles) {
220 outs() << "\n" << CurrentFilename << ":\n";
221 } else if (OutputFormat == sysv) {
222 outs() << "\n\nSymbols from " << CurrentFilename << ":\n\n"
223 << "Name Value Class Type"
224 << " Size Line Section\n";
227 const char *printBlanks, *printFormat;
228 if (isSymbolList64Bit(Obj)) {
230 printFormat = "%016" PRIx64;
233 printFormat = "%08" PRIx64;
236 for (SymbolListT::iterator I = SymbolList.begin(), E = SymbolList.end();
238 if ((I->TypeChar != 'U') && UndefinedOnly)
240 if ((I->TypeChar == 'U') && DefinedOnly)
242 if (SizeSort && !PrintAddress && I->Size == UnknownAddressOrSize)
245 char SymbolAddrStr[18] = "";
246 char SymbolSizeStr[18] = "";
248 if (OutputFormat == sysv || I->Address == UnknownAddressOrSize)
249 strcpy(SymbolAddrStr, printBlanks);
250 if (OutputFormat == sysv)
251 strcpy(SymbolSizeStr, printBlanks);
253 if (I->Address != UnknownAddressOrSize)
254 format(printFormat, I->Address)
255 .print(SymbolAddrStr, sizeof(SymbolAddrStr));
256 if (I->Size != UnknownAddressOrSize)
257 format(printFormat, I->Size).print(SymbolSizeStr, sizeof(SymbolSizeStr));
259 if (OutputFormat == posix) {
260 outs() << I->Name << " " << I->TypeChar << " " << SymbolAddrStr
261 << SymbolSizeStr << "\n";
262 } else if (OutputFormat == bsd) {
264 outs() << SymbolAddrStr << ' ';
266 outs() << SymbolSizeStr;
267 if (I->Size != UnknownAddressOrSize)
270 outs() << I->TypeChar << " " << I->Name << "\n";
271 } else if (OutputFormat == sysv) {
272 std::string PaddedName(I->Name);
273 while (PaddedName.length() < 20)
275 outs() << PaddedName << "|" << SymbolAddrStr << "| " << I->TypeChar
276 << " | |" << SymbolSizeStr << "| |\n";
283 template <class ELFT>
284 static char getSymbolNMTypeChar(ELFObjectFile<ELFT> &Obj,
285 basic_symbol_iterator I) {
286 typedef typename ELFObjectFile<ELFT>::Elf_Sym Elf_Sym;
287 typedef typename ELFObjectFile<ELFT>::Elf_Shdr Elf_Shdr;
290 symbol_iterator SymI(I);
292 DataRefImpl Symb = I->getRawDataRefImpl();
293 const Elf_Sym *ESym = Obj.getSymbol(Symb);
294 const ELFFile<ELFT> &EF = *Obj.getELFFile();
295 const Elf_Shdr *ESec = EF.getSection(ESym);
298 switch (ESec->sh_type) {
299 case ELF::SHT_PROGBITS:
300 case ELF::SHT_DYNAMIC:
301 switch (ESec->sh_flags) {
302 case(ELF::SHF_ALLOC | ELF::SHF_EXECINSTR) :
304 case(ELF::SHF_TLS | ELF::SHF_ALLOC | ELF::SHF_WRITE) :
305 case(ELF::SHF_ALLOC | ELF::SHF_WRITE) :
308 case(ELF::SHF_ALLOC | ELF::SHF_MERGE) :
309 case(ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS) :
313 case ELF::SHT_NOBITS:
318 if (ESym->getType() == ELF::STT_SECTION) {
320 if (error(SymI->getName(Name)))
322 return StringSwitch<char>(Name)
323 .StartsWith(".debug", 'N')
324 .StartsWith(".note", 'n')
331 static char getSymbolNMTypeChar(COFFObjectFile &Obj, symbol_iterator I) {
332 const coff_symbol *Symb = Obj.getCOFFSymbol(*I);
334 symbol_iterator SymI(I);
337 if (error(SymI->getName(Name)))
340 char Ret = StringSwitch<char>(Name)
341 .StartsWith(".debug", 'N')
342 .StartsWith(".sxdata", 'N')
348 uint32_t Characteristics = 0;
349 if (!COFF::isReservedSectionNumber(Symb->SectionNumber)) {
350 section_iterator SecI = Obj.section_end();
351 if (error(SymI->getSection(SecI)))
353 const coff_section *Section = Obj.getCOFFSection(*SecI);
354 Characteristics = Section->Characteristics;
357 switch (Symb->SectionNumber) {
358 case COFF::IMAGE_SYM_DEBUG:
361 // Check section type.
362 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
364 else if (Characteristics & COFF::IMAGE_SCN_MEM_READ &&
365 ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
367 else if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
369 else if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
371 else if (Characteristics & COFF::IMAGE_SCN_LNK_INFO)
374 // Check for section symbol.
375 else if (Symb->isSectionDefinition())
382 static uint8_t getNType(MachOObjectFile &Obj, DataRefImpl Symb) {
384 MachO::nlist_64 STE = Obj.getSymbol64TableEntry(Symb);
387 MachO::nlist STE = Obj.getSymbolTableEntry(Symb);
391 static char getSymbolNMTypeChar(MachOObjectFile &Obj, basic_symbol_iterator I) {
392 DataRefImpl Symb = I->getRawDataRefImpl();
393 uint8_t NType = getNType(Obj, Symb);
395 switch (NType & MachO::N_TYPE) {
400 case MachO::N_SECT: {
401 section_iterator Sec = Obj.section_end();
402 Obj.getSymbolSection(Symb, Sec);
403 DataRefImpl Ref = Sec->getRawDataRefImpl();
404 StringRef SectionName;
405 Obj.getSectionName(Ref, SectionName);
406 StringRef SegmentName = Obj.getSectionFinalSegmentName(Ref);
407 if (SegmentName == "__TEXT" && SectionName == "__text")
417 static char getSymbolNMTypeChar(const GlobalValue &GV) {
418 if (GV.getType()->getElementType()->isFunctionTy())
420 // FIXME: should we print 'b'? At the IR level we cannot be sure if this
421 // will be in bss or not, but we could approximate.
425 static char getSymbolNMTypeChar(IRObjectFile &Obj, basic_symbol_iterator I) {
426 const GlobalValue &GV = Obj.getSymbolGV(I->getRawDataRefImpl());
427 return getSymbolNMTypeChar(GV);
430 template <class ELFT>
431 static bool isObject(ELFObjectFile<ELFT> &Obj, symbol_iterator I) {
432 typedef typename ELFObjectFile<ELFT>::Elf_Sym Elf_Sym;
434 DataRefImpl Symb = I->getRawDataRefImpl();
435 const Elf_Sym *ESym = Obj.getSymbol(Symb);
437 return ESym->getType() == ELF::STT_OBJECT;
440 static bool isObject(SymbolicFile *Obj, basic_symbol_iterator I) {
441 if (ELF32LEObjectFile *ELF = dyn_cast<ELF32LEObjectFile>(Obj))
442 return isObject(*ELF, I);
443 if (ELF64LEObjectFile *ELF = dyn_cast<ELF64LEObjectFile>(Obj))
444 return isObject(*ELF, I);
445 if (ELF32BEObjectFile *ELF = dyn_cast<ELF32BEObjectFile>(Obj))
446 return isObject(*ELF, I);
447 if (ELF64BEObjectFile *ELF = dyn_cast<ELF64BEObjectFile>(Obj))
448 return isObject(*ELF, I);
452 static char getNMTypeChar(SymbolicFile *Obj, basic_symbol_iterator I) {
453 uint32_t Symflags = I->getFlags();
454 if ((Symflags & object::SymbolRef::SF_Weak) && !isa<MachOObjectFile>(Obj)) {
455 char Ret = isObject(Obj, I) ? 'v' : 'w';
456 if (!(Symflags & object::SymbolRef::SF_Undefined))
461 if (Symflags & object::SymbolRef::SF_Undefined)
464 if (Symflags & object::SymbolRef::SF_Common)
468 if (Symflags & object::SymbolRef::SF_Absolute)
470 else if (IRObjectFile *IR = dyn_cast<IRObjectFile>(Obj))
471 Ret = getSymbolNMTypeChar(*IR, I);
472 else if (COFFObjectFile *COFF = dyn_cast<COFFObjectFile>(Obj))
473 Ret = getSymbolNMTypeChar(*COFF, I);
474 else if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(Obj))
475 Ret = getSymbolNMTypeChar(*MachO, I);
476 else if (ELF32LEObjectFile *ELF = dyn_cast<ELF32LEObjectFile>(Obj))
477 Ret = getSymbolNMTypeChar(*ELF, I);
478 else if (ELF64LEObjectFile *ELF = dyn_cast<ELF64LEObjectFile>(Obj))
479 Ret = getSymbolNMTypeChar(*ELF, I);
480 else if (ELF32BEObjectFile *ELF = dyn_cast<ELF32BEObjectFile>(Obj))
481 Ret = getSymbolNMTypeChar(*ELF, I);
483 Ret = getSymbolNMTypeChar(*cast<ELF64BEObjectFile>(Obj), I);
485 if (Symflags & object::SymbolRef::SF_Global)
491 static void dumpSymbolNamesFromObject(SymbolicFile *Obj) {
492 basic_symbol_iterator IBegin = Obj->symbol_begin();
493 basic_symbol_iterator IEnd = Obj->symbol_end();
496 error("File format has no dynamic symbol table", Obj->getFileName());
499 std::pair<symbol_iterator, symbol_iterator> IDyn =
500 getELFDynamicSymbolIterators(Obj);
504 std::string NameBuffer;
505 raw_string_ostream OS(NameBuffer);
506 for (basic_symbol_iterator I = IBegin; I != IEnd; ++I) {
507 uint32_t SymFlags = I->getFlags();
508 if (!DebugSyms && (SymFlags & SymbolRef::SF_FormatSpecific))
510 if (WithoutAliases) {
511 if (IRObjectFile *IR = dyn_cast<IRObjectFile>(Obj)) {
512 const GlobalValue &GV = IR->getSymbolGV(I->getRawDataRefImpl());
513 if(isa<GlobalAlias>(GV))
518 S.Size = UnknownAddressOrSize;
519 S.Address = UnknownAddressOrSize;
520 if ((PrintSize || SizeSort) && isa<ObjectFile>(Obj)) {
521 symbol_iterator SymI = I;
522 if (error(SymI->getSize(S.Size)))
525 if (PrintAddress && isa<ObjectFile>(Obj))
526 if (error(symbol_iterator(I)->getAddress(S.Address)))
528 S.TypeChar = getNMTypeChar(Obj, I);
529 if (error(I->printName(OS)))
532 SymbolList.push_back(S);
536 const char *P = NameBuffer.c_str();
537 for (unsigned I = 0; I < SymbolList.size(); ++I) {
538 SymbolList[I].Name = P;
542 CurrentFilename = Obj->getFileName();
543 sortAndPrintSymbolList(Obj);
546 static void dumpSymbolNamesFromFile(std::string &Filename) {
547 std::unique_ptr<MemoryBuffer> Buffer;
548 if (error(MemoryBuffer::getFileOrSTDIN(Filename, Buffer), Filename))
551 LLVMContext &Context = getGlobalContext();
552 ErrorOr<Binary *> BinaryOrErr = createBinary(Buffer.release(), &Context);
553 if (error(BinaryOrErr.getError(), Filename))
555 std::unique_ptr<Binary> Bin(BinaryOrErr.get());
557 if (Archive *A = dyn_cast<Archive>(Bin.get())) {
559 Archive::symbol_iterator I = A->symbol_begin();
560 Archive::symbol_iterator E = A->symbol_end();
562 outs() << "Archive map\n";
563 for (; I != E; ++I) {
564 Archive::child_iterator C;
567 if (error(I->getMember(C)))
569 if (error(I->getName(SymName)))
571 if (error(C->getName(FileName)))
573 outs() << SymName << " in " << FileName << "\n";
579 for (Archive::child_iterator I = A->child_begin(), E = A->child_end();
581 std::unique_ptr<Binary> Child;
582 if (I->getAsBinary(Child, &Context))
584 if (SymbolicFile *O = dyn_cast<SymbolicFile>(Child.get())) {
585 outs() << O->getFileName() << ":\n";
586 dumpSymbolNamesFromObject(O);
591 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(Bin.get())) {
592 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
593 E = UB->end_objects();
595 std::unique_ptr<ObjectFile> Obj;
596 std::unique_ptr<Archive> A;
597 if (!I->getAsObjectFile(Obj)) {
598 outs() << Obj->getFileName() << ":\n";
599 dumpSymbolNamesFromObject(Obj.get());
601 else if (!I->getAsArchive(A)) {
602 for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end();
604 std::unique_ptr<Binary> Child;
605 if (AI->getAsBinary(Child, &Context))
607 if (SymbolicFile *O = dyn_cast<SymbolicFile>(Child.get())) {
608 outs() << A->getFileName() << ":";
609 outs() << O->getFileName() << ":\n";
610 dumpSymbolNamesFromObject(O);
617 if (SymbolicFile *O = dyn_cast<SymbolicFile>(Bin.get())) {
618 dumpSymbolNamesFromObject(O);
621 error("unrecognizable file type", Filename);
625 int main(int argc, char **argv) {
626 // Print a stack trace if we signal out.
627 sys::PrintStackTraceOnErrorSignal();
628 PrettyStackTraceProgram X(argc, argv);
630 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
631 cl::ParseCommandLineOptions(argc, argv, "llvm symbol table dumper\n");
633 // llvm-nm only reads binary files.
634 if (error(sys::ChangeStdinToBinary()))
641 OutputFormat = posix;
643 // The relative order of these is important. If you pass --size-sort it should
644 // only print out the size. However, if you pass -S --size-sort, it should
645 // print out both the size and address.
646 if (SizeSort && !PrintSize)
647 PrintAddress = false;
648 if (OutputFormat == sysv || SizeSort)
651 switch (InputFilenames.size()) {
653 InputFilenames.push_back("-");
657 MultipleFiles = true;
660 std::for_each(InputFilenames.begin(), InputFilenames.end(),
661 dumpSymbolNamesFromFile);