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" |
Chris Lattner | bed85ff | 2004-05-27 05:41:36 +0000 | [diff] [blame] | 23 | #include "llvm/System/Signals.h" |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 24 | #include <cctype> |
Alkis Evlogimenos | 09233fb | 2004-04-21 16:11:40 +0000 | [diff] [blame] | 25 | #include <cerrno> |
Brian Gaeke | 08d03c7 | 2003-11-19 21:52:09 +0000 | [diff] [blame] | 26 | #include <cstring> |
Reid Spencer | 86f42bd | 2004-07-04 12:20:55 +0000 | [diff] [blame^] | 27 | #include <iostream> |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 28 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 31 | namespace { |
| 32 | enum OutputFormatTy { bsd, sysv, posix }; |
| 33 | cl::opt<OutputFormatTy> |
| 34 | OutputFormat("format", |
| 35 | cl::desc("Specify output format"), |
| 36 | cl::values(clEnumVal(bsd, "BSD format"), |
| 37 | clEnumVal(sysv, "System V format"), |
| 38 | clEnumVal(posix, "POSIX.2 format"), 0), cl::init(bsd)); |
| 39 | cl::alias OutputFormat2("f", cl::desc("Alias for --format"), |
| 40 | cl::aliasopt(OutputFormat)); |
| 41 | |
| 42 | cl::list<std::string> |
| 43 | InputFilenames(cl::Positional, cl::desc("<input bytecode files>"), |
Chris Lattner | fc046d5 | 2003-10-16 18:45:23 +0000 | [diff] [blame] | 44 | cl::ZeroOrMore); |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 45 | |
| 46 | cl::opt<bool> UndefinedOnly("undefined-only", |
| 47 | cl::desc("Show only undefined symbols")); |
| 48 | cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"), |
| 49 | cl::aliasopt(UndefinedOnly)); |
| 50 | |
| 51 | cl::opt<bool> DefinedOnly("defined-only", |
| 52 | cl::desc("Show only defined symbols")); |
| 53 | |
| 54 | cl::opt<bool> ExternalOnly("extern-only", |
| 55 | cl::desc("Show only external symbols")); |
| 56 | cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"), |
| 57 | cl::aliasopt(ExternalOnly)); |
| 58 | |
| 59 | cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd")); |
| 60 | cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix")); |
| 61 | |
| 62 | bool MultipleFiles = false; |
| 63 | |
| 64 | std::string ToolName; |
| 65 | }; |
| 66 | |
| 67 | char TypeCharForSymbol (GlobalValue &GV) { |
| 68 | if (GV.isExternal ()) return 'U'; |
| 69 | if (GV.hasLinkOnceLinkage ()) return 'C'; |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 70 | if (GV.hasWeakLinkage ()) return 'W'; |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 71 | if (isa<Function> (GV) && GV.hasInternalLinkage ()) return 't'; |
| 72 | if (isa<Function> (GV)) return 'T'; |
| 73 | if (isa<GlobalVariable> (GV) && GV.hasInternalLinkage ()) return 'd'; |
| 74 | if (isa<GlobalVariable> (GV)) return 'D'; |
| 75 | return '?'; |
| 76 | } |
| 77 | |
| 78 | void DumpSymbolNameForGlobalValue (GlobalValue &GV) { |
| 79 | const std::string SymbolAddrStr = " "; // Not used yet... |
| 80 | char TypeChar = TypeCharForSymbol (GV); |
| 81 | if ((TypeChar != 'U') && UndefinedOnly) |
| 82 | return; |
| 83 | if ((TypeChar == 'U') && DefinedOnly) |
| 84 | return; |
| 85 | if (GV.hasInternalLinkage () && ExternalOnly) |
| 86 | return; |
| 87 | if (OutputFormat == posix) { |
| 88 | std::cout << GV.getName () << " " << TypeCharForSymbol (GV) << " " |
| 89 | << SymbolAddrStr << "\n"; |
| 90 | } else if (OutputFormat == bsd) { |
| 91 | std::cout << SymbolAddrStr << " " << TypeCharForSymbol (GV) << " " |
| 92 | << GV.getName () << "\n"; |
| 93 | } else if (OutputFormat == sysv) { |
| 94 | std::string PaddedName (GV.getName ()); |
| 95 | while (PaddedName.length () < 20) |
| 96 | PaddedName += " "; |
| 97 | std::cout << PaddedName << "|" << SymbolAddrStr << "| " |
| 98 | << TypeCharForSymbol (GV) |
| 99 | << " | | | |\n"; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | void DumpSymbolNamesFromModule (Module *M) { |
Brian Gaeke | 1c0b698 | 2003-11-16 23:34:13 +0000 | [diff] [blame] | 104 | const std::string &Filename = M->getModuleIdentifier (); |
| 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"; |
| 113 | } |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 114 | std::for_each (M->begin (), M->end (), DumpSymbolNameForGlobalValue); |
| 115 | std::for_each (M->gbegin (), M->gend (), DumpSymbolNameForGlobalValue); |
| 116 | } |
| 117 | |
| 118 | void DumpSymbolNamesFromFile (std::string &Filename) { |
| 119 | std::string ErrorMessage; |
Brian Gaeke | 8b1daa3 | 2003-11-19 22:15:00 +0000 | [diff] [blame] | 120 | if (Filename != "-" && !FileOpenable (Filename)) { |
Brian Gaeke | 08d03c7 | 2003-11-19 21:52:09 +0000 | [diff] [blame] | 121 | std::cerr << ToolName << ": " << Filename << ": " << strerror (errno) |
| 122 | << "\n"; |
| 123 | return; |
| 124 | } |
Brian Gaeke | 8b1daa3 | 2003-11-19 22:15:00 +0000 | [diff] [blame] | 125 | // Note: Currently we do not support reading an archive from stdin. |
| 126 | if (Filename == "-" || IsBytecode (Filename)) { |
Brian Gaeke | 1c0b698 | 2003-11-16 23:34:13 +0000 | [diff] [blame] | 127 | Module *Result = ParseBytecodeFile(Filename, &ErrorMessage); |
| 128 | if (Result) { |
| 129 | DumpSymbolNamesFromModule (Result); |
| 130 | } else { |
| 131 | std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n"; |
Brian Gaeke | 08d03c7 | 2003-11-19 21:52:09 +0000 | [diff] [blame] | 132 | return; |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 133 | } |
Brian Gaeke | 1c0b698 | 2003-11-16 23:34:13 +0000 | [diff] [blame] | 134 | } else if (IsArchive (Filename)) { |
| 135 | std::vector<Module *> Modules; |
Brian Gaeke | 08d03c7 | 2003-11-19 21:52:09 +0000 | [diff] [blame] | 136 | if (ReadArchiveFile (Filename, Modules, &ErrorMessage)) { |
Brian Gaeke | 1c0b698 | 2003-11-16 23:34:13 +0000 | [diff] [blame] | 137 | std::cerr << ToolName << ": " << Filename << ": " |
| 138 | << ErrorMessage << "\n"; |
Brian Gaeke | 08d03c7 | 2003-11-19 21:52:09 +0000 | [diff] [blame] | 139 | return; |
| 140 | } |
Brian Gaeke | 1c0b698 | 2003-11-16 23:34:13 +0000 | [diff] [blame] | 141 | MultipleFiles = true; |
| 142 | std::for_each (Modules.begin (), Modules.end (), DumpSymbolNamesFromModule); |
Brian Gaeke | 8204287 | 2003-11-19 21:57:30 +0000 | [diff] [blame] | 143 | } else { |
| 144 | std::cerr << ToolName << ": " << Filename << ": " |
| 145 | << "unrecognizable file type\n"; |
| 146 | return; |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | int main(int argc, char **argv) { |
| 151 | cl::ParseCommandLineOptions(argc, argv, " llvm symbol table dumper\n"); |
Chris Lattner | f73b4ca | 2004-02-19 20:32:12 +0000 | [diff] [blame] | 152 | PrintStackTraceOnErrorSignal(); |
| 153 | |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 154 | ToolName = argv[0]; |
| 155 | if (BSDFormat) OutputFormat = bsd; |
| 156 | if (POSIXFormat) OutputFormat = posix; |
Chris Lattner | fc046d5 | 2003-10-16 18:45:23 +0000 | [diff] [blame] | 157 | |
| 158 | switch (InputFilenames.size()) { |
| 159 | case 0: InputFilenames.push_back("-"); |
| 160 | case 1: break; |
| 161 | default: MultipleFiles = true; |
| 162 | } |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 163 | |
| 164 | std::for_each (InputFilenames.begin (), InputFilenames.end (), |
| 165 | DumpSymbolNamesFromFile); |
| 166 | return 0; |
| 167 | } |