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 "llvm/Bytecode/Archive.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/System/Signals.h"
32 enum OutputFormatTy { bsd, sysv, posix };
33 cl::opt<OutputFormatTy>
34 OutputFormat("format",
35 cl::desc("Specify output format"),
36 cl::values(clEnumVal(bsd, "BSD format"),
37 clEnumVal(sysv, "System V format"),
38 clEnumVal(posix, "POSIX.2 format"),
39 clEnumValEnd), cl::init(bsd));
40 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
41 cl::aliasopt(OutputFormat));
44 InputFilenames(cl::Positional, cl::desc("<input bytecode files>"),
47 cl::opt<bool> UndefinedOnly("undefined-only",
48 cl::desc("Show only undefined symbols"));
49 cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
50 cl::aliasopt(UndefinedOnly));
52 cl::opt<bool> DefinedOnly("defined-only",
53 cl::desc("Show only defined symbols"));
55 cl::opt<bool> ExternalOnly("extern-only",
56 cl::desc("Show only external symbols"));
57 cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
58 cl::aliasopt(ExternalOnly));
60 cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
61 cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
63 bool MultipleFiles = false;
68 char TypeCharForSymbol (GlobalValue &GV) {
69 if (GV.isExternal ()) return 'U';
70 if (GV.hasLinkOnceLinkage ()) return 'C';
71 if (GV.hasWeakLinkage ()) return 'W';
72 if (isa<Function> (GV) && GV.hasInternalLinkage ()) return 't';
73 if (isa<Function> (GV)) return 'T';
74 if (isa<GlobalVariable> (GV) && GV.hasInternalLinkage ()) return 'd';
75 if (isa<GlobalVariable> (GV)) return 'D';
79 void DumpSymbolNameForGlobalValue (GlobalValue &GV) {
80 const std::string SymbolAddrStr = " "; // Not used yet...
81 char TypeChar = TypeCharForSymbol (GV);
82 if ((TypeChar != 'U') && UndefinedOnly)
84 if ((TypeChar == 'U') && DefinedOnly)
86 if (GV.hasInternalLinkage () && ExternalOnly)
88 if (OutputFormat == posix) {
89 std::cout << GV.getName () << " " << TypeCharForSymbol (GV) << " "
90 << SymbolAddrStr << "\n";
91 } else if (OutputFormat == bsd) {
92 std::cout << SymbolAddrStr << " " << TypeCharForSymbol (GV) << " "
93 << GV.getName () << "\n";
94 } else if (OutputFormat == sysv) {
95 std::string PaddedName (GV.getName ());
96 while (PaddedName.length () < 20)
98 std::cout << PaddedName << "|" << SymbolAddrStr << "| "
99 << TypeCharForSymbol (GV)
104 void DumpSymbolNamesFromModule (Module *M) {
105 const std::string &Filename = M->getModuleIdentifier ();
106 if (OutputFormat == posix && MultipleFiles) {
107 std::cout << Filename << ":\n";
108 } else if (OutputFormat == bsd && MultipleFiles) {
109 std::cout << "\n" << Filename << ":\n";
110 } else if (OutputFormat == sysv) {
111 std::cout << "\n\nSymbols from " << Filename << ":\n\n"
112 << "Name Value Class Type"
113 << " Size Line Section\n";
115 std::for_each (M->begin (), M->end (), DumpSymbolNameForGlobalValue);
116 std::for_each (M->global_begin (), M->global_end (), DumpSymbolNameForGlobalValue);
119 void DumpSymbolNamesFromFile (std::string &Filename) {
120 std::string ErrorMessage;
121 sys::Path aPath(Filename);
122 if (Filename != "-" && !aPath.readable()) {
123 std::cerr << ToolName << ": " << Filename << ": " << strerror (errno)
127 // Note: Currently we do not support reading an archive from stdin.
128 if (Filename == "-" || aPath.isBytecodeFile()) {
129 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
131 DumpSymbolNamesFromModule (Result);
133 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
136 } else if (aPath.isArchive()) {
137 Archive* archive = Archive::OpenAndLoad(sys::Path(Filename));
139 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
140 std::vector<Module *> Modules;
141 if (archive->getAllModules(Modules,&ErrorMessage)) {
142 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
145 MultipleFiles = true;
146 std::for_each (Modules.begin (), Modules.end (), DumpSymbolNamesFromModule);
148 std::cerr << ToolName << ": " << Filename << ": "
149 << "unrecognizable file type\n";
154 int main(int argc, char **argv) {
156 cl::ParseCommandLineOptions(argc, argv, " llvm symbol table dumper\n");
157 sys::PrintStackTraceOnErrorSignal();
160 if (BSDFormat) OutputFormat = bsd;
161 if (POSIXFormat) OutputFormat = posix;
163 switch (InputFilenames.size()) {
164 case 0: InputFilenames.push_back("-");
166 default: MultipleFiles = true;
169 std::for_each (InputFilenames.begin (), InputFilenames.end (),
170 DumpSymbolNamesFromFile);
172 } catch (const std::string& msg) {
173 std::cerr << argv[0] << ": " << msg << "\n";
175 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";