Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 1 | //===-- llvm-nm.cpp - Symbol table dumping utility for llvm ---------------===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 21c62da | 2007-12-29 20:44:31 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 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", |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 11 | // that is, it prints out the names of symbols in a bitcode file, |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 12 | // along with some information about each symbol. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 13 | // |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 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 | 4d5aad2 | 2007-05-06 05:36:18 +0000 | [diff] [blame] | 20 | #include "llvm/Bitcode/ReaderWriter.h" |
Chris Lattner | 44dadff | 2007-05-06 09:29:57 +0000 | [diff] [blame] | 21 | #include "llvm/Bitcode/Archive.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ManagedStatic.h" |
Chris Lattner | 4d5aad2 | 2007-05-06 05:36:18 +0000 | [diff] [blame] | 24 | #include "llvm/Support/MemoryBuffer.h" |
Chris Lattner | bed85ff | 2004-05-27 05:41:36 +0000 | [diff] [blame] | 25 | #include "llvm/System/Signals.h" |
Jeff Cohen | ca5183d | 2007-03-05 00:00:42 +0000 | [diff] [blame] | 26 | #include <algorithm> |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 27 | #include <cctype> |
Alkis Evlogimenos | 09233fb | 2004-04-21 16:11:40 +0000 | [diff] [blame] | 28 | #include <cerrno> |
Brian Gaeke | 08d03c7 | 2003-11-19 21:52:09 +0000 | [diff] [blame] | 29 | #include <cstring> |
Reid Spencer | 86f42bd | 2004-07-04 12:20:55 +0000 | [diff] [blame] | 30 | #include <iostream> |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 31 | using namespace llvm; |
| 32 | |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 33 | namespace { |
| 34 | enum OutputFormatTy { bsd, sysv, posix }; |
| 35 | cl::opt<OutputFormatTy> |
| 36 | OutputFormat("format", |
| 37 | cl::desc("Specify output format"), |
| 38 | cl::values(clEnumVal(bsd, "BSD format"), |
| 39 | clEnumVal(sysv, "System V format"), |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 40 | clEnumVal(posix, "POSIX.2 format"), |
Chris Lattner | 4d143ee | 2004-07-16 00:08:28 +0000 | [diff] [blame] | 41 | clEnumValEnd), cl::init(bsd)); |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 42 | cl::alias OutputFormat2("f", cl::desc("Alias for --format"), |
| 43 | cl::aliasopt(OutputFormat)); |
| 44 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 45 | cl::list<std::string> |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 46 | InputFilenames(cl::Positional, cl::desc("<input bitcode files>"), |
Chris Lattner | fc046d5 | 2003-10-16 18:45:23 +0000 | [diff] [blame] | 47 | cl::ZeroOrMore); |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 48 | |
| 49 | cl::opt<bool> UndefinedOnly("undefined-only", |
| 50 | cl::desc("Show only undefined symbols")); |
| 51 | cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"), |
| 52 | cl::aliasopt(UndefinedOnly)); |
| 53 | |
| 54 | cl::opt<bool> DefinedOnly("defined-only", |
| 55 | cl::desc("Show only defined symbols")); |
| 56 | |
| 57 | cl::opt<bool> ExternalOnly("extern-only", |
| 58 | cl::desc("Show only external symbols")); |
| 59 | cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"), |
| 60 | cl::aliasopt(ExternalOnly)); |
| 61 | |
| 62 | cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd")); |
| 63 | cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix")); |
| 64 | |
| 65 | bool MultipleFiles = false; |
| 66 | |
| 67 | std::string ToolName; |
Chris Lattner | d74ea2b | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 68 | } |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 69 | |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 70 | static char TypeCharForSymbol(GlobalValue &GV) { |
Lauro Ramos Venancio | 4a828ee | 2007-06-27 22:08:09 +0000 | [diff] [blame] | 71 | if (GV.isDeclaration()) return 'U'; |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 72 | if (GV.hasLinkOnceLinkage()) return 'C'; |
Dale Johannesen | 7d5633e | 2008-05-16 22:44:18 +0000 | [diff] [blame^] | 73 | if (GV.hasCommonLinkage()) return 'C'; |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 74 | if (GV.hasWeakLinkage()) return 'W'; |
Lauro Ramos Venancio | 4a828ee | 2007-06-27 22:08:09 +0000 | [diff] [blame] | 75 | if (isa<Function>(GV) && GV.hasInternalLinkage()) return 't'; |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 76 | if (isa<Function>(GV)) return 'T'; |
Lauro Ramos Venancio | 4a828ee | 2007-06-27 22:08:09 +0000 | [diff] [blame] | 77 | if (isa<GlobalVariable>(GV) && GV.hasInternalLinkage()) return 'd'; |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 78 | if (isa<GlobalVariable>(GV)) return 'D'; |
Lauro Ramos Venancio | 4a828ee | 2007-06-27 22:08:09 +0000 | [diff] [blame] | 79 | if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(&GV)) { |
| 80 | const GlobalValue *AliasedGV = GA->getAliasedGlobal(); |
| 81 | if (isa<Function>(AliasedGV)) return 'T'; |
| 82 | if (isa<GlobalVariable>(AliasedGV)) return 'D'; |
| 83 | } |
| 84 | return '?'; |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 87 | static void DumpSymbolNameForGlobalValue(GlobalValue &GV) { |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 88 | const std::string SymbolAddrStr = " "; // Not used yet... |
| 89 | char TypeChar = TypeCharForSymbol (GV); |
| 90 | if ((TypeChar != 'U') && UndefinedOnly) |
| 91 | return; |
| 92 | if ((TypeChar == 'U') && DefinedOnly) |
| 93 | return; |
| 94 | if (GV.hasInternalLinkage () && ExternalOnly) |
| 95 | return; |
| 96 | if (OutputFormat == posix) { |
| 97 | std::cout << GV.getName () << " " << TypeCharForSymbol (GV) << " " |
| 98 | << SymbolAddrStr << "\n"; |
| 99 | } else if (OutputFormat == bsd) { |
| 100 | std::cout << SymbolAddrStr << " " << TypeCharForSymbol (GV) << " " |
| 101 | << GV.getName () << "\n"; |
| 102 | } else if (OutputFormat == sysv) { |
| 103 | std::string PaddedName (GV.getName ()); |
| 104 | while (PaddedName.length () < 20) |
| 105 | PaddedName += " "; |
| 106 | std::cout << PaddedName << "|" << SymbolAddrStr << "| " |
| 107 | << TypeCharForSymbol (GV) |
| 108 | << " | | | |\n"; |
| 109 | } |
| 110 | } |
| 111 | |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 112 | static void DumpSymbolNamesFromModule(Module *M) { |
Brian Gaeke | 1c0b698 | 2003-11-16 23:34:13 +0000 | [diff] [blame] | 113 | const std::string &Filename = M->getModuleIdentifier (); |
| 114 | if (OutputFormat == posix && MultipleFiles) { |
| 115 | std::cout << Filename << ":\n"; |
| 116 | } else if (OutputFormat == bsd && MultipleFiles) { |
| 117 | std::cout << "\n" << Filename << ":\n"; |
| 118 | } else if (OutputFormat == sysv) { |
| 119 | std::cout << "\n\nSymbols from " << Filename << ":\n\n" |
| 120 | << "Name Value Class Type" |
| 121 | << " Size Line Section\n"; |
| 122 | } |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 123 | std::for_each (M->begin (), M->end (), DumpSymbolNameForGlobalValue); |
Lauro Ramos Venancio | 4a828ee | 2007-06-27 22:08:09 +0000 | [diff] [blame] | 124 | std::for_each (M->global_begin (), M->global_end (), |
| 125 | DumpSymbolNameForGlobalValue); |
| 126 | std::for_each (M->alias_begin (), M->alias_end (), |
| 127 | DumpSymbolNameForGlobalValue); |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 130 | static void DumpSymbolNamesFromFile(std::string &Filename) { |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 131 | std::string ErrorMessage; |
Reid Spencer | 11db4b8 | 2004-12-13 03:01:26 +0000 | [diff] [blame] | 132 | sys::Path aPath(Filename); |
Brian Gaeke | 8b1daa3 | 2003-11-19 22:15:00 +0000 | [diff] [blame] | 133 | // Note: Currently we do not support reading an archive from stdin. |
Chris Lattner | 44dadff | 2007-05-06 09:29:57 +0000 | [diff] [blame] | 134 | if (Filename == "-" || aPath.isBitcodeFile()) { |
Chris Lattner | 4d5aad2 | 2007-05-06 05:36:18 +0000 | [diff] [blame] | 135 | std::auto_ptr<MemoryBuffer> Buffer( |
Chris Lattner | 065344d | 2007-05-06 23:45:49 +0000 | [diff] [blame] | 136 | MemoryBuffer::getFileOrSTDIN(Filename, &ErrorMessage)); |
Chris Lattner | 4d5aad2 | 2007-05-06 05:36:18 +0000 | [diff] [blame] | 137 | Module *Result = 0; |
| 138 | if (Buffer.get()) |
| 139 | Result = ParseBitcodeFile(Buffer.get(), &ErrorMessage); |
| 140 | |
| 141 | if (Result) |
| 142 | DumpSymbolNamesFromModule(Result); |
| 143 | else { |
| 144 | std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n"; |
| 145 | return; |
| 146 | } |
| 147 | |
Reid Spencer | 11db4b8 | 2004-12-13 03:01:26 +0000 | [diff] [blame] | 148 | } else if (aPath.isArchive()) { |
Reid Spencer | 8d8a7ff | 2006-07-07 20:56:50 +0000 | [diff] [blame] | 149 | std::string ErrMsg; |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 150 | Archive* archive = Archive::OpenAndLoad(sys::Path(Filename), &ErrorMessage); |
Reid Spencer | 63efb77 | 2004-11-14 22:27:46 +0000 | [diff] [blame] | 151 | if (!archive) |
| 152 | std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n"; |
Brian Gaeke | 1c0b698 | 2003-11-16 23:34:13 +0000 | [diff] [blame] | 153 | std::vector<Module *> Modules; |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 154 | if (archive->getAllModules(Modules, &ErrorMessage)) { |
Reid Spencer | 63efb77 | 2004-11-14 22:27:46 +0000 | [diff] [blame] | 155 | std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n"; |
Brian Gaeke | 08d03c7 | 2003-11-19 21:52:09 +0000 | [diff] [blame] | 156 | return; |
| 157 | } |
Brian Gaeke | 1c0b698 | 2003-11-16 23:34:13 +0000 | [diff] [blame] | 158 | MultipleFiles = true; |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 159 | std::for_each (Modules.begin(), Modules.end(), DumpSymbolNamesFromModule); |
Brian Gaeke | 8204287 | 2003-11-19 21:57:30 +0000 | [diff] [blame] | 160 | } else { |
| 161 | std::cerr << ToolName << ": " << Filename << ": " |
| 162 | << "unrecognizable file type\n"; |
| 163 | return; |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
| 167 | int main(int argc, char **argv) { |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 168 | llvm_shutdown_obj X; // Call llvm_shutdown() on exit. |
Dan Gohman | 82a13c9 | 2007-10-08 15:45:12 +0000 | [diff] [blame] | 169 | cl::ParseCommandLineOptions(argc, argv, "llvm symbol table dumper\n"); |
Chris Lattner | 4d5aad2 | 2007-05-06 05:36:18 +0000 | [diff] [blame] | 170 | sys::PrintStackTraceOnErrorSignal(); |
Chris Lattner | f73b4ca | 2004-02-19 20:32:12 +0000 | [diff] [blame] | 171 | |
Chris Lattner | 4d5aad2 | 2007-05-06 05:36:18 +0000 | [diff] [blame] | 172 | ToolName = argv[0]; |
| 173 | if (BSDFormat) OutputFormat = bsd; |
| 174 | if (POSIXFormat) OutputFormat = posix; |
Chris Lattner | fc046d5 | 2003-10-16 18:45:23 +0000 | [diff] [blame] | 175 | |
Chris Lattner | 4d5aad2 | 2007-05-06 05:36:18 +0000 | [diff] [blame] | 176 | switch (InputFilenames.size()) { |
| 177 | case 0: InputFilenames.push_back("-"); |
| 178 | case 1: break; |
| 179 | default: MultipleFiles = true; |
Chris Lattner | fc046d5 | 2003-10-16 18:45:23 +0000 | [diff] [blame] | 180 | } |
Chris Lattner | 4d5aad2 | 2007-05-06 05:36:18 +0000 | [diff] [blame] | 181 | |
| 182 | std::for_each(InputFilenames.begin(), InputFilenames.end(), |
| 183 | DumpSymbolNamesFromFile); |
| 184 | return 0; |
Brian Gaeke | 972d3d7 | 2003-10-16 04:43:15 +0000 | [diff] [blame] | 185 | } |