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"
28 enum OutputFormatTy { bsd, sysv, posix };
29 cl::opt<OutputFormatTy>
30 OutputFormat("format",
31 cl::desc("Specify output format"),
32 cl::values(clEnumVal(bsd, "BSD format"),
33 clEnumVal(sysv, "System V format"),
34 clEnumVal(posix, "POSIX.2 format"), 0), cl::init(bsd));
35 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
36 cl::aliasopt(OutputFormat));
39 InputFilenames(cl::Positional, cl::desc("<input bytecode files>"),
42 cl::opt<bool> UndefinedOnly("undefined-only",
43 cl::desc("Show only undefined symbols"));
44 cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
45 cl::aliasopt(UndefinedOnly));
47 cl::opt<bool> DefinedOnly("defined-only",
48 cl::desc("Show only defined symbols"));
50 cl::opt<bool> ExternalOnly("extern-only",
51 cl::desc("Show only external symbols"));
52 cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
53 cl::aliasopt(ExternalOnly));
55 cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
56 cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
58 bool MultipleFiles = false;
63 char TypeCharForSymbol (GlobalValue &GV) {
64 if (GV.isExternal ()) return 'U';
65 if (GV.hasLinkOnceLinkage ()) return 'C';
66 if (GV.hasWeakLinkage ()) return 'W';
67 if (isa<Function> (GV) && GV.hasInternalLinkage ()) return 't';
68 if (isa<Function> (GV)) return 'T';
69 if (isa<GlobalVariable> (GV) && GV.hasInternalLinkage ()) return 'd';
70 if (isa<GlobalVariable> (GV)) return 'D';
74 void DumpSymbolNameForGlobalValue (GlobalValue &GV) {
75 const std::string SymbolAddrStr = " "; // Not used yet...
76 char TypeChar = TypeCharForSymbol (GV);
77 if ((TypeChar != 'U') && UndefinedOnly)
79 if ((TypeChar == 'U') && DefinedOnly)
81 if (GV.hasInternalLinkage () && ExternalOnly)
83 if (OutputFormat == posix) {
84 std::cout << GV.getName () << " " << TypeCharForSymbol (GV) << " "
85 << SymbolAddrStr << "\n";
86 } else if (OutputFormat == bsd) {
87 std::cout << SymbolAddrStr << " " << TypeCharForSymbol (GV) << " "
88 << GV.getName () << "\n";
89 } else if (OutputFormat == sysv) {
90 std::string PaddedName (GV.getName ());
91 while (PaddedName.length () < 20)
93 std::cout << PaddedName << "|" << SymbolAddrStr << "| "
94 << TypeCharForSymbol (GV)
99 void DumpSymbolNamesFromModule (Module *M) {
100 const std::string &Filename = M->getModuleIdentifier ();
101 if (OutputFormat == posix && MultipleFiles) {
102 std::cout << Filename << ":\n";
103 } else if (OutputFormat == bsd && MultipleFiles) {
104 std::cout << "\n" << Filename << ":\n";
105 } else if (OutputFormat == sysv) {
106 std::cout << "\n\nSymbols from " << Filename << ":\n\n"
107 << "Name Value Class Type"
108 << " Size Line Section\n";
110 std::for_each (M->begin (), M->end (), DumpSymbolNameForGlobalValue);
111 std::for_each (M->gbegin (), M->gend (), DumpSymbolNameForGlobalValue);
114 void DumpSymbolNamesFromFile (std::string &Filename) {
115 std::string ErrorMessage;
116 if (IsBytecode (Filename)) {
117 Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
119 DumpSymbolNamesFromModule (Result);
121 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
123 } else if (IsArchive (Filename)) {
124 std::vector<Module *> Modules;
125 if (ReadArchiveFile (Filename, Modules, &ErrorMessage))
126 std::cerr << ToolName << ": " << Filename << ": "
127 << ErrorMessage << "\n";
128 MultipleFiles = true;
129 std::for_each (Modules.begin (), Modules.end (), DumpSymbolNamesFromModule);
133 int main(int argc, char **argv) {
134 cl::ParseCommandLineOptions(argc, argv, " llvm symbol table dumper\n");
136 if (BSDFormat) OutputFormat = bsd;
137 if (POSIXFormat) OutputFormat = posix;
139 switch (InputFilenames.size()) {
140 case 0: InputFilenames.push_back("-");
142 default: MultipleFiles = true;
145 std::for_each (InputFilenames.begin (), InputFilenames.end (),
146 DumpSymbolNamesFromFile);