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"
25 enum OutputFormatTy { bsd, sysv, posix };
26 cl::opt<OutputFormatTy>
27 OutputFormat("format",
28 cl::desc("Specify output format"),
29 cl::values(clEnumVal(bsd, "BSD format"),
30 clEnumVal(sysv, "System V format"),
31 clEnumVal(posix, "POSIX.2 format"), 0), cl::init(bsd));
32 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
33 cl::aliasopt(OutputFormat));
36 InputFilenames(cl::Positional, cl::desc("<input bytecode files>"),
39 cl::opt<bool> UndefinedOnly("undefined-only",
40 cl::desc("Show only undefined symbols"));
41 cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
42 cl::aliasopt(UndefinedOnly));
44 cl::opt<bool> DefinedOnly("defined-only",
45 cl::desc("Show only defined symbols"));
47 cl::opt<bool> ExternalOnly("extern-only",
48 cl::desc("Show only external symbols"));
49 cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
50 cl::aliasopt(ExternalOnly));
52 cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
53 cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
55 bool MultipleFiles = false;
60 char TypeCharForSymbol (GlobalValue &GV) {
61 if (GV.isExternal ()) return 'U';
62 if (GV.hasLinkOnceLinkage ()) return 'C';
63 if (GV.hasWeakLinkage ()) return 'W';
64 if (isa<Function> (GV) && GV.hasInternalLinkage ()) return 't';
65 if (isa<Function> (GV)) return 'T';
66 if (isa<GlobalVariable> (GV) && GV.hasInternalLinkage ()) return 'd';
67 if (isa<GlobalVariable> (GV)) return 'D';
71 void DumpSymbolNameForGlobalValue (GlobalValue &GV) {
72 const std::string SymbolAddrStr = " "; // Not used yet...
73 char TypeChar = TypeCharForSymbol (GV);
74 if ((TypeChar != 'U') && UndefinedOnly)
76 if ((TypeChar == 'U') && DefinedOnly)
78 if (GV.hasInternalLinkage () && ExternalOnly)
80 if (OutputFormat == posix) {
81 std::cout << GV.getName () << " " << TypeCharForSymbol (GV) << " "
82 << SymbolAddrStr << "\n";
83 } else if (OutputFormat == bsd) {
84 std::cout << SymbolAddrStr << " " << TypeCharForSymbol (GV) << " "
85 << GV.getName () << "\n";
86 } else if (OutputFormat == sysv) {
87 std::string PaddedName (GV.getName ());
88 while (PaddedName.length () < 20)
90 std::cout << PaddedName << "|" << SymbolAddrStr << "| "
91 << TypeCharForSymbol (GV)
96 void DumpSymbolNamesFromModule (Module *M) {
97 std::for_each (M->begin (), M->end (), DumpSymbolNameForGlobalValue);
98 std::for_each (M->gbegin (), M->gend (), DumpSymbolNameForGlobalValue);
101 void DumpSymbolNamesFromFile (std::string &Filename) {
102 std::string ErrorMessage;
103 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
105 if (OutputFormat == posix && MultipleFiles) {
106 std::cout << Filename << ":\n";
107 } else if (OutputFormat == bsd && MultipleFiles) {
108 std::cout << "\n" << Filename << ":\n";
109 } else if (OutputFormat == sysv) {
110 std::cout << "\n\nSymbols from " << Filename << ":\n\n"
111 << "Name Value Class Type"
112 << " Size Line Section\n";
114 DumpSymbolNamesFromModule (Result);
116 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
120 int main(int argc, char **argv) {
121 cl::ParseCommandLineOptions(argc, argv, " llvm symbol table dumper\n");
123 if (BSDFormat) OutputFormat = bsd;
124 if (POSIXFormat) OutputFormat = posix;
126 switch (InputFilenames.size()) {
127 case 0: InputFilenames.push_back("-");
129 default: MultipleFiles = true;
132 std::for_each (InputFilenames.begin (), InputFilenames.end (),
133 DumpSymbolNamesFromFile);