1 //===-- llvm-nm.cpp - Symbol table dumping utility for llvm ---------------===//
3 // This program is a utility that works like traditional Unix "nm",
4 // that is, it prints out the names of symbols in a bytecode file,
5 // along with some information about each symbol.
7 // This "nm" does not print symbols' addresses. It supports many of
8 // the features of GNU "nm", including its different output formats.
10 //===----------------------------------------------------------------------===//
12 #include "Support/CommandLine.h"
13 #include "llvm/Bytecode/Reader.h"
14 #include "llvm/GlobalValue.h"
15 #include "llvm/Module.h"
19 enum OutputFormatTy { bsd, sysv, posix };
20 cl::opt<OutputFormatTy>
21 OutputFormat("format",
22 cl::desc("Specify output format"),
23 cl::values(clEnumVal(bsd, "BSD format"),
24 clEnumVal(sysv, "System V format"),
25 clEnumVal(posix, "POSIX.2 format"), 0), cl::init(bsd));
26 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
27 cl::aliasopt(OutputFormat));
30 InputFilenames(cl::Positional, cl::desc("<input bytecode files>"),
33 cl::opt<bool> UndefinedOnly("undefined-only",
34 cl::desc("Show only undefined symbols"));
35 cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
36 cl::aliasopt(UndefinedOnly));
38 cl::opt<bool> DefinedOnly("defined-only",
39 cl::desc("Show only defined symbols"));
41 cl::opt<bool> ExternalOnly("extern-only",
42 cl::desc("Show only external symbols"));
43 cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
44 cl::aliasopt(ExternalOnly));
46 cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
47 cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
49 bool MultipleFiles = false;
54 char TypeCharForSymbol (GlobalValue &GV) {
55 if (GV.isExternal ()) return 'U';
56 if (GV.hasLinkOnceLinkage ()) return 'C';
57 if (GV.hasWeakLinkage ()) return 'W';
58 if (isa<Function> (GV) && GV.hasInternalLinkage ()) return 't';
59 if (isa<Function> (GV)) return 'T';
60 if (isa<GlobalVariable> (GV) && GV.hasInternalLinkage ()) return 'd';
61 if (isa<GlobalVariable> (GV)) return 'D';
65 void DumpSymbolNameForGlobalValue (GlobalValue &GV) {
66 const std::string SymbolAddrStr = " "; // Not used yet...
67 char TypeChar = TypeCharForSymbol (GV);
68 if ((TypeChar != 'U') && UndefinedOnly)
70 if ((TypeChar == 'U') && DefinedOnly)
72 if (GV.hasInternalLinkage () && ExternalOnly)
74 if (OutputFormat == posix) {
75 std::cout << GV.getName () << " " << TypeCharForSymbol (GV) << " "
76 << SymbolAddrStr << "\n";
77 } else if (OutputFormat == bsd) {
78 std::cout << SymbolAddrStr << " " << TypeCharForSymbol (GV) << " "
79 << GV.getName () << "\n";
80 } else if (OutputFormat == sysv) {
81 std::string PaddedName (GV.getName ());
82 while (PaddedName.length () < 20)
84 std::cout << PaddedName << "|" << SymbolAddrStr << "| "
85 << TypeCharForSymbol (GV)
90 void DumpSymbolNamesFromModule (Module *M) {
91 std::for_each (M->begin (), M->end (), DumpSymbolNameForGlobalValue);
92 std::for_each (M->gbegin (), M->gend (), DumpSymbolNameForGlobalValue);
95 void DumpSymbolNamesFromFile (std::string &Filename) {
96 std::string ErrorMessage;
97 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
99 if (OutputFormat == posix && MultipleFiles) {
100 std::cout << Filename << ":\n";
101 } else if (OutputFormat == bsd && MultipleFiles) {
102 std::cout << "\n" << Filename << ":\n";
103 } else if (OutputFormat == sysv) {
104 std::cout << "\n\nSymbols from " << Filename << ":\n\n"
105 << "Name Value Class Type"
106 << " Size Line Section\n";
108 DumpSymbolNamesFromModule (Result);
110 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
114 int main(int argc, char **argv) {
115 cl::ParseCommandLineOptions(argc, argv, " llvm symbol table dumper\n");
117 if (BSDFormat) OutputFormat = bsd;
118 if (POSIXFormat) OutputFormat = posix;
120 switch (InputFilenames.size()) {
121 case 0: InputFilenames.push_back("-");
123 default: MultipleFiles = true;
126 std::for_each (InputFilenames.begin (), InputFilenames.end (),
127 DumpSymbolNamesFromFile);