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"
23 #include "Support/Signals.h"
30 enum OutputFormatTy { bsd, sysv, posix };
31 cl::opt<OutputFormatTy>
32 OutputFormat("format",
33 cl::desc("Specify output format"),
34 cl::values(clEnumVal(bsd, "BSD format"),
35 clEnumVal(sysv, "System V format"),
36 clEnumVal(posix, "POSIX.2 format"), 0), cl::init(bsd));
37 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
38 cl::aliasopt(OutputFormat));
41 InputFilenames(cl::Positional, cl::desc("<input bytecode files>"),
44 cl::opt<bool> UndefinedOnly("undefined-only",
45 cl::desc("Show only undefined symbols"));
46 cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
47 cl::aliasopt(UndefinedOnly));
49 cl::opt<bool> DefinedOnly("defined-only",
50 cl::desc("Show only defined symbols"));
52 cl::opt<bool> ExternalOnly("extern-only",
53 cl::desc("Show only external symbols"));
54 cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
55 cl::aliasopt(ExternalOnly));
57 cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
58 cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
60 bool MultipleFiles = false;
65 char TypeCharForSymbol (GlobalValue &GV) {
66 if (GV.isExternal ()) return 'U';
67 if (GV.hasLinkOnceLinkage ()) return 'C';
68 if (GV.hasWeakLinkage ()) return 'W';
69 if (isa<Function> (GV) && GV.hasInternalLinkage ()) return 't';
70 if (isa<Function> (GV)) return 'T';
71 if (isa<GlobalVariable> (GV) && GV.hasInternalLinkage ()) return 'd';
72 if (isa<GlobalVariable> (GV)) return 'D';
76 void DumpSymbolNameForGlobalValue (GlobalValue &GV) {
77 const std::string SymbolAddrStr = " "; // Not used yet...
78 char TypeChar = TypeCharForSymbol (GV);
79 if ((TypeChar != 'U') && UndefinedOnly)
81 if ((TypeChar == 'U') && DefinedOnly)
83 if (GV.hasInternalLinkage () && ExternalOnly)
85 if (OutputFormat == posix) {
86 std::cout << GV.getName () << " " << TypeCharForSymbol (GV) << " "
87 << SymbolAddrStr << "\n";
88 } else if (OutputFormat == bsd) {
89 std::cout << SymbolAddrStr << " " << TypeCharForSymbol (GV) << " "
90 << GV.getName () << "\n";
91 } else if (OutputFormat == sysv) {
92 std::string PaddedName (GV.getName ());
93 while (PaddedName.length () < 20)
95 std::cout << PaddedName << "|" << SymbolAddrStr << "| "
96 << TypeCharForSymbol (GV)
101 void DumpSymbolNamesFromModule (Module *M) {
102 const std::string &Filename = M->getModuleIdentifier ();
103 if (OutputFormat == posix && MultipleFiles) {
104 std::cout << Filename << ":\n";
105 } else if (OutputFormat == bsd && MultipleFiles) {
106 std::cout << "\n" << Filename << ":\n";
107 } else if (OutputFormat == sysv) {
108 std::cout << "\n\nSymbols from " << Filename << ":\n\n"
109 << "Name Value Class Type"
110 << " Size Line Section\n";
112 std::for_each (M->begin (), M->end (), DumpSymbolNameForGlobalValue);
113 std::for_each (M->gbegin (), M->gend (), DumpSymbolNameForGlobalValue);
116 void DumpSymbolNamesFromFile (std::string &Filename) {
117 std::string ErrorMessage;
118 if (Filename != "-" && !FileOpenable (Filename)) {
119 std::cerr << ToolName << ": " << Filename << ": " << strerror (errno)
123 // Note: Currently we do not support reading an archive from stdin.
124 if (Filename == "-" || IsBytecode (Filename)) {
125 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
127 DumpSymbolNamesFromModule (Result);
129 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
132 } else if (IsArchive (Filename)) {
133 std::vector<Module *> Modules;
134 if (ReadArchiveFile (Filename, Modules, &ErrorMessage)) {
135 std::cerr << ToolName << ": " << Filename << ": "
136 << ErrorMessage << "\n";
139 MultipleFiles = true;
140 std::for_each (Modules.begin (), Modules.end (), DumpSymbolNamesFromModule);
142 std::cerr << ToolName << ": " << Filename << ": "
143 << "unrecognizable file type\n";
148 int main(int argc, char **argv) {
149 cl::ParseCommandLineOptions(argc, argv, " llvm symbol table dumper\n");
150 PrintStackTraceOnErrorSignal();
153 if (BSDFormat) OutputFormat = bsd;
154 if (POSIXFormat) OutputFormat = posix;
156 switch (InputFilenames.size()) {
157 case 0: InputFilenames.push_back("-");
159 default: MultipleFiles = true;
162 std::for_each (InputFilenames.begin (), InputFilenames.end (),
163 DumpSymbolNamesFromFile);