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/Support/FileUtilities.h"
24 #include "llvm/System/Signals.h"
33 enum OutputFormatTy { bsd, sysv, posix };
34 cl::opt<OutputFormatTy>
35 OutputFormat("format",
36 cl::desc("Specify output format"),
37 cl::values(clEnumVal(bsd, "BSD format"),
38 clEnumVal(sysv, "System V format"),
39 clEnumVal(posix, "POSIX.2 format"),
40 clEnumValEnd), cl::init(bsd));
41 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
42 cl::aliasopt(OutputFormat));
45 InputFilenames(cl::Positional, cl::desc("<input bytecode files>"),
48 cl::opt<bool> UndefinedOnly("undefined-only",
49 cl::desc("Show only undefined symbols"));
50 cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
51 cl::aliasopt(UndefinedOnly));
53 cl::opt<bool> DefinedOnly("defined-only",
54 cl::desc("Show only defined symbols"));
56 cl::opt<bool> ExternalOnly("extern-only",
57 cl::desc("Show only external symbols"));
58 cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
59 cl::aliasopt(ExternalOnly));
61 cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
62 cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
64 bool MultipleFiles = false;
69 char TypeCharForSymbol (GlobalValue &GV) {
70 if (GV.isExternal ()) return 'U';
71 if (GV.hasLinkOnceLinkage ()) return 'C';
72 if (GV.hasWeakLinkage ()) return 'W';
73 if (isa<Function> (GV) && GV.hasInternalLinkage ()) return 't';
74 if (isa<Function> (GV)) return 'T';
75 if (isa<GlobalVariable> (GV) && GV.hasInternalLinkage ()) return 'd';
76 if (isa<GlobalVariable> (GV)) return 'D';
80 void DumpSymbolNameForGlobalValue (GlobalValue &GV) {
81 const std::string SymbolAddrStr = " "; // Not used yet...
82 char TypeChar = TypeCharForSymbol (GV);
83 if ((TypeChar != 'U') && UndefinedOnly)
85 if ((TypeChar == 'U') && DefinedOnly)
87 if (GV.hasInternalLinkage () && ExternalOnly)
89 if (OutputFormat == posix) {
90 std::cout << GV.getName () << " " << TypeCharForSymbol (GV) << " "
91 << SymbolAddrStr << "\n";
92 } else if (OutputFormat == bsd) {
93 std::cout << SymbolAddrStr << " " << TypeCharForSymbol (GV) << " "
94 << GV.getName () << "\n";
95 } else if (OutputFormat == sysv) {
96 std::string PaddedName (GV.getName ());
97 while (PaddedName.length () < 20)
99 std::cout << PaddedName << "|" << SymbolAddrStr << "| "
100 << TypeCharForSymbol (GV)
105 void DumpSymbolNamesFromModule (Module *M) {
106 const std::string &Filename = M->getModuleIdentifier ();
107 if (OutputFormat == posix && MultipleFiles) {
108 std::cout << Filename << ":\n";
109 } else if (OutputFormat == bsd && MultipleFiles) {
110 std::cout << "\n" << Filename << ":\n";
111 } else if (OutputFormat == sysv) {
112 std::cout << "\n\nSymbols from " << Filename << ":\n\n"
113 << "Name Value Class Type"
114 << " Size Line Section\n";
116 std::for_each (M->begin (), M->end (), DumpSymbolNameForGlobalValue);
117 std::for_each (M->gbegin (), M->gend (), DumpSymbolNameForGlobalValue);
120 void DumpSymbolNamesFromFile (std::string &Filename) {
121 std::string ErrorMessage;
122 sys::Path aPath(Filename);
123 if (Filename != "-" && !aPath.readable()) {
124 std::cerr << ToolName << ": " << Filename << ": " << strerror (errno)
128 // Note: Currently we do not support reading an archive from stdin.
129 if (Filename == "-" || aPath.isBytecodeFile()) {
130 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
132 DumpSymbolNamesFromModule (Result);
134 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
137 } else if (aPath.isArchive()) {
138 Archive* archive = Archive::OpenAndLoad(sys::Path(Filename));
140 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
141 std::vector<Module *> Modules;
142 if (archive->getAllModules(Modules,&ErrorMessage)) {
143 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
146 MultipleFiles = true;
147 std::for_each (Modules.begin (), Modules.end (), DumpSymbolNamesFromModule);
149 std::cerr << ToolName << ": " << Filename << ": "
150 << "unrecognizable file type\n";
155 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);