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"
29 enum OutputFormatTy { bsd, sysv, posix };
30 cl::opt<OutputFormatTy>
31 OutputFormat("format",
32 cl::desc("Specify output format"),
33 cl::values(clEnumVal(bsd, "BSD format"),
34 clEnumVal(sysv, "System V format"),
35 clEnumVal(posix, "POSIX.2 format"), 0), cl::init(bsd));
36 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
37 cl::aliasopt(OutputFormat));
40 InputFilenames(cl::Positional, cl::desc("<input bytecode files>"),
43 cl::opt<bool> UndefinedOnly("undefined-only",
44 cl::desc("Show only undefined symbols"));
45 cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
46 cl::aliasopt(UndefinedOnly));
48 cl::opt<bool> DefinedOnly("defined-only",
49 cl::desc("Show only defined symbols"));
51 cl::opt<bool> ExternalOnly("extern-only",
52 cl::desc("Show only external symbols"));
53 cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
54 cl::aliasopt(ExternalOnly));
56 cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
57 cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
59 bool MultipleFiles = false;
64 char TypeCharForSymbol (GlobalValue &GV) {
65 if (GV.isExternal ()) return 'U';
66 if (GV.hasLinkOnceLinkage ()) return 'C';
67 if (GV.hasWeakLinkage ()) return 'W';
68 if (isa<Function> (GV) && GV.hasInternalLinkage ()) return 't';
69 if (isa<Function> (GV)) return 'T';
70 if (isa<GlobalVariable> (GV) && GV.hasInternalLinkage ()) return 'd';
71 if (isa<GlobalVariable> (GV)) return 'D';
75 void DumpSymbolNameForGlobalValue (GlobalValue &GV) {
76 const std::string SymbolAddrStr = " "; // Not used yet...
77 char TypeChar = TypeCharForSymbol (GV);
78 if ((TypeChar != 'U') && UndefinedOnly)
80 if ((TypeChar == 'U') && DefinedOnly)
82 if (GV.hasInternalLinkage () && ExternalOnly)
84 if (OutputFormat == posix) {
85 std::cout << GV.getName () << " " << TypeCharForSymbol (GV) << " "
86 << SymbolAddrStr << "\n";
87 } else if (OutputFormat == bsd) {
88 std::cout << SymbolAddrStr << " " << TypeCharForSymbol (GV) << " "
89 << GV.getName () << "\n";
90 } else if (OutputFormat == sysv) {
91 std::string PaddedName (GV.getName ());
92 while (PaddedName.length () < 20)
94 std::cout << PaddedName << "|" << SymbolAddrStr << "| "
95 << TypeCharForSymbol (GV)
100 void DumpSymbolNamesFromModule (Module *M) {
101 const std::string &Filename = M->getModuleIdentifier ();
102 if (OutputFormat == posix && MultipleFiles) {
103 std::cout << Filename << ":\n";
104 } else if (OutputFormat == bsd && MultipleFiles) {
105 std::cout << "\n" << Filename << ":\n";
106 } else if (OutputFormat == sysv) {
107 std::cout << "\n\nSymbols from " << Filename << ":\n\n"
108 << "Name Value Class Type"
109 << " Size Line Section\n";
111 std::for_each (M->begin (), M->end (), DumpSymbolNameForGlobalValue);
112 std::for_each (M->gbegin (), M->gend (), DumpSymbolNameForGlobalValue);
115 void DumpSymbolNamesFromFile (std::string &Filename) {
116 std::string ErrorMessage;
117 if (!FileOpenable (Filename)) {
118 std::cerr << ToolName << ": " << Filename << ": " << strerror (errno)
122 if (IsBytecode (Filename)) {
123 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
125 DumpSymbolNamesFromModule (Result);
127 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
130 } else if (IsArchive (Filename)) {
131 std::vector<Module *> Modules;
132 if (ReadArchiveFile (Filename, Modules, &ErrorMessage)) {
133 std::cerr << ToolName << ": " << Filename << ": "
134 << ErrorMessage << "\n";
137 MultipleFiles = true;
138 std::for_each (Modules.begin (), Modules.end (), DumpSymbolNamesFromModule);
140 std::cerr << ToolName << ": " << Filename << ": "
141 << "unrecognizable file type\n";
146 int main(int argc, char **argv) {
147 cl::ParseCommandLineOptions(argc, argv, " llvm symbol table dumper\n");
149 if (BSDFormat) OutputFormat = bsd;
150 if (POSIXFormat) OutputFormat = posix;
152 switch (InputFilenames.size()) {
153 case 0: InputFilenames.push_back("-");
155 default: MultipleFiles = true;
158 std::for_each (InputFilenames.begin (), InputFilenames.end (),
159 DumpSymbolNamesFromFile);