Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 1 | //===-- llvm-nm.cpp - Symbol table dumping utility for llvm ---------------===// |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 9 | // |
| 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. |
| 13 | // |
| 14 | // This "nm" does not print symbols' addresses. It supports many of |
| 15 | // the features of GNU "nm", including its different output formats. |
| 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 19 | #include "llvm/Module.h" |
Chris Lattner | 08020c1 | 2003-10-28 19:08:15 +0000 | [diff] [blame] | 20 | #include "llvm/Bytecode/Reader.h" |
| 21 | #include "Support/CommandLine.h" |
Brian Gaeke | 1c0b698 | 2003-11-16 23:34:13 +0000 | [diff] [blame] | 22 | #include "Support/FileUtilities.h" |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 23 | #include <cctype> |
Brian Gaeke | 08d03c7 | 2003-11-19 21:52:09 +0000 | [diff] [blame^] | 24 | #include <cstring> |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 25 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 28 | namespace { |
| 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)); |
| 38 | |
| 39 | cl::list<std::string> |
| 40 | InputFilenames(cl::Positional, cl::desc("<input bytecode files>"), |
Chris Lattner | fc046d5 | 2003-10-16 18:45:23 +0000 | [diff] [blame] | 41 | cl::ZeroOrMore); |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 42 | |
| 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)); |
| 47 | |
| 48 | cl::opt<bool> DefinedOnly("defined-only", |
| 49 | cl::desc("Show only defined symbols")); |
| 50 | |
| 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)); |
| 55 | |
| 56 | cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd")); |
| 57 | cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix")); |
| 58 | |
| 59 | bool MultipleFiles = false; |
| 60 | |
| 61 | std::string ToolName; |
| 62 | }; |
| 63 | |
| 64 | char TypeCharForSymbol (GlobalValue &GV) { |
| 65 | if (GV.isExternal ()) return 'U'; |
| 66 | if (GV.hasLinkOnceLinkage ()) return 'C'; |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 67 | if (GV.hasWeakLinkage ()) return 'W'; |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 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'; |
| 72 | return '?'; |
| 73 | } |
| 74 | |
| 75 | void DumpSymbolNameForGlobalValue (GlobalValue &GV) { |
| 76 | const std::string SymbolAddrStr = " "; // Not used yet... |
| 77 | char TypeChar = TypeCharForSymbol (GV); |
| 78 | if ((TypeChar != 'U') && UndefinedOnly) |
| 79 | return; |
| 80 | if ((TypeChar == 'U') && DefinedOnly) |
| 81 | return; |
| 82 | if (GV.hasInternalLinkage () && ExternalOnly) |
| 83 | return; |
| 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) |
| 93 | PaddedName += " "; |
| 94 | std::cout << PaddedName << "|" << SymbolAddrStr << "| " |
| 95 | << TypeCharForSymbol (GV) |
| 96 | << " | | | |\n"; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void DumpSymbolNamesFromModule (Module *M) { |
Brian Gaeke | 1c0b698 | 2003-11-16 23:34:13 +0000 | [diff] [blame] | 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"; |
| 110 | } |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 111 | std::for_each (M->begin (), M->end (), DumpSymbolNameForGlobalValue); |
| 112 | std::for_each (M->gbegin (), M->gend (), DumpSymbolNameForGlobalValue); |
| 113 | } |
| 114 | |
| 115 | void DumpSymbolNamesFromFile (std::string &Filename) { |
| 116 | std::string ErrorMessage; |
Brian Gaeke | 08d03c7 | 2003-11-19 21:52:09 +0000 | [diff] [blame^] | 117 | if (!FileOpenable (Filename)) { |
| 118 | std::cerr << ToolName << ": " << Filename << ": " << strerror (errno) |
| 119 | << "\n"; |
| 120 | return; |
| 121 | } |
Brian Gaeke | 1c0b698 | 2003-11-16 23:34:13 +0000 | [diff] [blame] | 122 | if (IsBytecode (Filename)) { |
| 123 | Module *Result = ParseBytecodeFile(Filename, &ErrorMessage); |
| 124 | if (Result) { |
| 125 | DumpSymbolNamesFromModule (Result); |
| 126 | } else { |
| 127 | std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n"; |
Brian Gaeke | 08d03c7 | 2003-11-19 21:52:09 +0000 | [diff] [blame^] | 128 | return; |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 129 | } |
Brian Gaeke | 1c0b698 | 2003-11-16 23:34:13 +0000 | [diff] [blame] | 130 | } else if (IsArchive (Filename)) { |
| 131 | std::vector<Module *> Modules; |
Brian Gaeke | 08d03c7 | 2003-11-19 21:52:09 +0000 | [diff] [blame^] | 132 | if (ReadArchiveFile (Filename, Modules, &ErrorMessage)) { |
Brian Gaeke | 1c0b698 | 2003-11-16 23:34:13 +0000 | [diff] [blame] | 133 | std::cerr << ToolName << ": " << Filename << ": " |
| 134 | << ErrorMessage << "\n"; |
Brian Gaeke | 08d03c7 | 2003-11-19 21:52:09 +0000 | [diff] [blame^] | 135 | return; |
| 136 | } |
Brian Gaeke | 1c0b698 | 2003-11-16 23:34:13 +0000 | [diff] [blame] | 137 | MultipleFiles = true; |
| 138 | std::for_each (Modules.begin (), Modules.end (), DumpSymbolNamesFromModule); |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | int main(int argc, char **argv) { |
| 143 | cl::ParseCommandLineOptions(argc, argv, " llvm symbol table dumper\n"); |
| 144 | ToolName = argv[0]; |
| 145 | if (BSDFormat) OutputFormat = bsd; |
| 146 | if (POSIXFormat) OutputFormat = posix; |
Chris Lattner | fc046d5 | 2003-10-16 18:45:23 +0000 | [diff] [blame] | 147 | |
| 148 | switch (InputFilenames.size()) { |
| 149 | case 0: InputFilenames.push_back("-"); |
| 150 | case 1: break; |
| 151 | default: MultipleFiles = true; |
| 152 | } |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 153 | |
| 154 | std::for_each (InputFilenames.begin (), InputFilenames.end (), |
| 155 | DumpSymbolNamesFromFile); |
| 156 | return 0; |
| 157 | } |