1 //===-- llvm-nm.cpp - Symbol table dumping utility for llvm ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This program is a utility that works like traditional Unix "nm",
11 // that is, it prints out the names of symbols in a bytecode file,
12 // along with some information about each symbol.
14 // This "nm" does not print symbols' addresses. It supports many of
15 // the features of GNU "nm", including its different output formats.
17 //===----------------------------------------------------------------------===//
19 #include "llvm/Module.h"
20 #include "llvm/Bytecode/Reader.h"
21 #include "Support/CommandLine.h"
22 #include "Support/FileUtilities.h"
23 #include "llvm/System/Signals.h"
31 enum OutputFormatTy { bsd, sysv, posix };
32 cl::opt<OutputFormatTy>
33 OutputFormat("format",
34 cl::desc("Specify output format"),
35 cl::values(clEnumVal(bsd, "BSD format"),
36 clEnumVal(sysv, "System V format"),
37 clEnumVal(posix, "POSIX.2 format"), 0), cl::init(bsd));
38 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
39 cl::aliasopt(OutputFormat));
42 InputFilenames(cl::Positional, cl::desc("<input bytecode files>"),
45 cl::opt<bool> UndefinedOnly("undefined-only",
46 cl::desc("Show only undefined symbols"));
47 cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
48 cl::aliasopt(UndefinedOnly));
50 cl::opt<bool> DefinedOnly("defined-only",
51 cl::desc("Show only defined symbols"));
53 cl::opt<bool> ExternalOnly("extern-only",
54 cl::desc("Show only external symbols"));
55 cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
56 cl::aliasopt(ExternalOnly));
58 cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
59 cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
61 bool MultipleFiles = false;
66 char TypeCharForSymbol (GlobalValue &GV) {
67 if (GV.isExternal ()) return 'U';
68 if (GV.hasLinkOnceLinkage ()) return 'C';
69 if (GV.hasWeakLinkage ()) return 'W';
70 if (isa<Function> (GV) && GV.hasInternalLinkage ()) return 't';
71 if (isa<Function> (GV)) return 'T';
72 if (isa<GlobalVariable> (GV) && GV.hasInternalLinkage ()) return 'd';
73 if (isa<GlobalVariable> (GV)) return 'D';
77 void DumpSymbolNameForGlobalValue (GlobalValue &GV) {
78 const std::string SymbolAddrStr = " "; // Not used yet...
79 char TypeChar = TypeCharForSymbol (GV);
80 if ((TypeChar != 'U') && UndefinedOnly)
82 if ((TypeChar == 'U') && DefinedOnly)
84 if (GV.hasInternalLinkage () && ExternalOnly)
86 if (OutputFormat == posix) {
87 std::cout << GV.getName () << " " << TypeCharForSymbol (GV) << " "
88 << SymbolAddrStr << "\n";
89 } else if (OutputFormat == bsd) {
90 std::cout << SymbolAddrStr << " " << TypeCharForSymbol (GV) << " "
91 << GV.getName () << "\n";
92 } else if (OutputFormat == sysv) {
93 std::string PaddedName (GV.getName ());
94 while (PaddedName.length () < 20)
96 std::cout << PaddedName << "|" << SymbolAddrStr << "| "
97 << TypeCharForSymbol (GV)
102 void DumpSymbolNamesFromModule (Module *M) {
103 const std::string &Filename = M->getModuleIdentifier ();
104 if (OutputFormat == posix && MultipleFiles) {
105 std::cout << Filename << ":\n";
106 } else if (OutputFormat == bsd && MultipleFiles) {
107 std::cout << "\n" << Filename << ":\n";
108 } else if (OutputFormat == sysv) {
109 std::cout << "\n\nSymbols from " << Filename << ":\n\n"
110 << "Name Value Class Type"
111 << " Size Line Section\n";
113 std::for_each (M->begin (), M->end (), DumpSymbolNameForGlobalValue);
114 std::for_each (M->gbegin (), M->gend (), DumpSymbolNameForGlobalValue);
117 void DumpSymbolNamesFromFile (std::string &Filename) {
118 std::string ErrorMessage;
119 if (Filename != "-" && !FileOpenable (Filename)) {
120 std::cerr << ToolName << ": " << Filename << ": " << strerror (errno)
124 // Note: Currently we do not support reading an archive from stdin.
125 if (Filename == "-" || IsBytecode (Filename)) {
126 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
128 DumpSymbolNamesFromModule (Result);
130 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
133 } else if (IsArchive (Filename)) {
134 std::vector<Module *> Modules;
135 if (ReadArchiveFile (Filename, Modules, &ErrorMessage)) {
136 std::cerr << ToolName << ": " << Filename << ": "
137 << ErrorMessage << "\n";
140 MultipleFiles = true;
141 std::for_each (Modules.begin (), Modules.end (), DumpSymbolNamesFromModule);
143 std::cerr << ToolName << ": " << Filename << ": "
144 << "unrecognizable file type\n";
149 int main(int argc, char **argv) {
150 cl::ParseCommandLineOptions(argc, argv, " llvm symbol table dumper\n");
151 PrintStackTraceOnErrorSignal();
154 if (BSDFormat) OutputFormat = bsd;
155 if (POSIXFormat) OutputFormat = posix;
157 switch (InputFilenames.size()) {
158 case 0: InputFilenames.push_back("-");
160 default: MultipleFiles = true;
163 std::for_each (InputFilenames.begin (), InputFilenames.end (),
164 DumpSymbolNamesFromFile);