1 //===-- llvm-nm.cpp - Symbol table dumping utility for llvm ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This program is a utility that works like traditional Unix "nm", that is, it
11 // prints out the names of symbols in a bitcode or object file, along with some
12 // information about each symbol.
14 // This "nm" supports many of the features of GNU "nm", including its different
17 //===----------------------------------------------------------------------===//
19 #include "llvm/LLVMContext.h"
20 #include "llvm/Module.h"
21 #include "llvm/Bitcode/ReaderWriter.h"
22 #include "llvm/Bitcode/Archive.h"
23 #include "llvm/Object/Archive.h"
24 #include "llvm/Object/ObjectFile.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/FileSystem.h"
27 #include "llvm/Support/ManagedStatic.h"
28 #include "llvm/Support/MemoryBuffer.h"
29 #include "llvm/Support/PrettyStackTrace.h"
30 #include "llvm/Support/Program.h"
31 #include "llvm/Support/raw_ostream.h"
32 #include "llvm/Support/Signals.h"
33 #include "llvm/Support/Format.h"
34 #include "llvm/Support/system_error.h"
41 using namespace object;
44 enum OutputFormatTy { bsd, sysv, posix };
45 cl::opt<OutputFormatTy>
46 OutputFormat("format",
47 cl::desc("Specify output format"),
48 cl::values(clEnumVal(bsd, "BSD format"),
49 clEnumVal(sysv, "System V format"),
50 clEnumVal(posix, "POSIX.2 format"),
51 clEnumValEnd), cl::init(bsd));
52 cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
53 cl::aliasopt(OutputFormat));
56 InputFilenames(cl::Positional, cl::desc("<input bitcode files>"),
59 cl::opt<bool> UndefinedOnly("undefined-only",
60 cl::desc("Show only undefined symbols"));
61 cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
62 cl::aliasopt(UndefinedOnly));
64 cl::opt<bool> DynamicSyms("dynamic",
65 cl::desc("Display the dynamic symbols instead "
66 "of normal symbols."));
67 cl::alias DynamicSyms2("D", cl::desc("Alias for --dynamic"),
68 cl::aliasopt(DynamicSyms));
70 cl::opt<bool> DefinedOnly("defined-only",
71 cl::desc("Show only defined symbols"));
73 cl::opt<bool> ExternalOnly("extern-only",
74 cl::desc("Show only external symbols"));
75 cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
76 cl::aliasopt(ExternalOnly));
78 cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
79 cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
81 cl::opt<bool> PrintFileName("print-file-name",
82 cl::desc("Precede each symbol with the object file it came from"));
84 cl::alias PrintFileNameA("A", cl::desc("Alias for --print-file-name"),
85 cl::aliasopt(PrintFileName));
86 cl::alias PrintFileNameo("o", cl::desc("Alias for --print-file-name"),
87 cl::aliasopt(PrintFileName));
89 cl::opt<bool> DebugSyms("debug-syms",
90 cl::desc("Show all symbols, even debugger only"));
91 cl::alias DebugSymsa("a", cl::desc("Alias for --debug-syms"),
92 cl::aliasopt(DebugSyms));
94 cl::opt<bool> NumericSort("numeric-sort",
95 cl::desc("Sort symbols by address"));
96 cl::alias NumericSortn("n", cl::desc("Alias for --numeric-sort"),
97 cl::aliasopt(NumericSort));
98 cl::alias NumericSortv("v", cl::desc("Alias for --numeric-sort"),
99 cl::aliasopt(NumericSort));
101 cl::opt<bool> NoSort("no-sort",
102 cl::desc("Show symbols in order encountered"));
103 cl::alias NoSortp("p", cl::desc("Alias for --no-sort"),
104 cl::aliasopt(NoSort));
106 cl::opt<bool> PrintSize("print-size",
107 cl::desc("Show symbol size instead of address"));
108 cl::alias PrintSizeS("S", cl::desc("Alias for --print-size"),
109 cl::aliasopt(PrintSize));
111 cl::opt<bool> SizeSort("size-sort", cl::desc("Sort symbols by size"));
113 cl::opt<bool> WithoutAliases("without-aliases", cl::Hidden,
114 cl::desc("Exclude aliases from output"));
116 bool PrintAddress = true;
118 bool MultipleFiles = false;
120 std::string ToolName;
124 static void error(Twine message, Twine path = Twine()) {
125 errs() << ToolName << ": " << path << ": " << message << ".\n";
128 static bool error(error_code ec, Twine path = Twine()) {
130 error(ec.message(), path);
144 static bool CompareSymbolAddress(const NMSymbol &a, const NMSymbol &b) {
145 if (a.Address < b.Address)
147 else if (a.Address == b.Address && a.Name < b.Name)
154 static bool CompareSymbolSize(const NMSymbol &a, const NMSymbol &b) {
157 else if (a.Size == b.Size && a.Name < b.Name)
163 static bool CompareSymbolName(const NMSymbol &a, const NMSymbol &b) {
164 return a.Name < b.Name;
167 StringRef CurrentFilename;
168 typedef std::vector<NMSymbol> SymbolListT;
169 SymbolListT SymbolList;
172 static void SortAndPrintSymbolList() {
175 std::sort(SymbolList.begin(), SymbolList.end(), CompareSymbolAddress);
177 std::sort(SymbolList.begin(), SymbolList.end(), CompareSymbolSize);
179 std::sort(SymbolList.begin(), SymbolList.end(), CompareSymbolName);
182 if (OutputFormat == posix && MultipleFiles) {
183 outs() << '\n' << CurrentFilename << ":\n";
184 } else if (OutputFormat == bsd && MultipleFiles) {
185 outs() << "\n" << CurrentFilename << ":\n";
186 } else if (OutputFormat == sysv) {
187 outs() << "\n\nSymbols from " << CurrentFilename << ":\n\n"
188 << "Name Value Class Type"
189 << " Size Line Section\n";
192 for (SymbolListT::iterator i = SymbolList.begin(),
193 e = SymbolList.end(); i != e; ++i) {
194 if ((i->TypeChar != 'U') && UndefinedOnly)
196 if ((i->TypeChar == 'U') && DefinedOnly)
198 if (SizeSort && !PrintAddress && i->Size == UnknownAddressOrSize)
201 char SymbolAddrStr[10] = "";
202 char SymbolSizeStr[10] = "";
204 if (OutputFormat == sysv || i->Address == object::UnknownAddressOrSize)
205 strcpy(SymbolAddrStr, " ");
206 if (OutputFormat == sysv)
207 strcpy(SymbolSizeStr, " ");
209 if (i->Address != object::UnknownAddressOrSize)
210 format("%08" PRIx64, i->Address).print(SymbolAddrStr,
211 sizeof(SymbolAddrStr));
212 if (i->Size != object::UnknownAddressOrSize)
213 format("%08" PRIx64, i->Size).print(SymbolSizeStr, sizeof(SymbolSizeStr));
215 if (OutputFormat == posix) {
216 outs() << i->Name << " " << i->TypeChar << " "
217 << SymbolAddrStr << SymbolSizeStr << "\n";
218 } else if (OutputFormat == bsd) {
220 outs() << SymbolAddrStr << ' ';
222 outs() << SymbolSizeStr;
223 if (i->Size != object::UnknownAddressOrSize)
226 outs() << i->TypeChar << " " << i->Name << "\n";
227 } else if (OutputFormat == sysv) {
228 std::string PaddedName (i->Name);
229 while (PaddedName.length () < 20)
231 outs() << PaddedName << "|" << SymbolAddrStr << "| "
233 << " | |" << SymbolSizeStr << "| |\n";
240 static char TypeCharForSymbol(GlobalValue &GV) {
241 if (GV.isDeclaration()) return 'U';
242 if (GV.hasLinkOnceLinkage()) return 'C';
243 if (GV.hasCommonLinkage()) return 'C';
244 if (GV.hasWeakLinkage()) return 'W';
245 if (isa<Function>(GV) && GV.hasInternalLinkage()) return 't';
246 if (isa<Function>(GV)) return 'T';
247 if (isa<GlobalVariable>(GV) && GV.hasInternalLinkage()) return 'd';
248 if (isa<GlobalVariable>(GV)) return 'D';
249 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(&GV)) {
250 const GlobalValue *AliasedGV = GA->getAliasedGlobal();
251 if (isa<Function>(AliasedGV)) return 'T';
252 if (isa<GlobalVariable>(AliasedGV)) return 'D';
257 static void DumpSymbolNameForGlobalValue(GlobalValue &GV) {
258 // Private linkage and available_externally linkage don't exist in symtab.
259 if (GV.hasPrivateLinkage() ||
260 GV.hasLinkerPrivateLinkage() ||
261 GV.hasLinkerPrivateWeakLinkage() ||
262 GV.hasAvailableExternallyLinkage())
264 char TypeChar = TypeCharForSymbol(GV);
265 if (GV.hasLocalLinkage () && ExternalOnly)
269 s.Address = object::UnknownAddressOrSize;
270 s.Size = object::UnknownAddressOrSize;
271 s.TypeChar = TypeChar;
272 s.Name = GV.getName();
273 SymbolList.push_back(s);
276 static void DumpSymbolNamesFromModule(Module *M) {
277 CurrentFilename = M->getModuleIdentifier();
278 std::for_each (M->begin(), M->end(), DumpSymbolNameForGlobalValue);
279 std::for_each (M->global_begin(), M->global_end(),
280 DumpSymbolNameForGlobalValue);
282 std::for_each (M->alias_begin(), M->alias_end(),
283 DumpSymbolNameForGlobalValue);
285 SortAndPrintSymbolList();
288 static void DumpSymbolNamesFromObject(ObjectFile *obj) {
290 symbol_iterator ibegin = obj->begin_symbols();
291 symbol_iterator iend = obj->end_symbols();
293 ibegin = obj->begin_dynamic_symbols();
294 iend = obj->end_dynamic_symbols();
296 for (symbol_iterator i = ibegin; i != iend; i.increment(ec)) {
297 if (error(ec)) break;
299 if (error(i->getFlags(symflags))) break;
300 if (!DebugSyms && (symflags & SymbolRef::SF_FormatSpecific))
303 s.Size = object::UnknownAddressOrSize;
304 s.Address = object::UnknownAddressOrSize;
305 if (PrintSize || SizeSort) {
306 if (error(i->getSize(s.Size))) break;
309 if (error(i->getAddress(s.Address))) break;
310 if (error(i->getNMTypeChar(s.TypeChar))) break;
311 if (error(i->getName(s.Name))) break;
312 SymbolList.push_back(s);
315 CurrentFilename = obj->getFileName();
316 SortAndPrintSymbolList();
319 static void DumpSymbolNamesFromFile(std::string &Filename) {
320 if (Filename != "-" && !sys::fs::exists(Filename)) {
321 errs() << ToolName << ": '" << Filename << "': " << "No such file\n";
325 OwningPtr<MemoryBuffer> Buffer;
326 if (error(MemoryBuffer::getFileOrSTDIN(Filename, Buffer), Filename))
329 sys::fs::file_magic magic = sys::fs::identify_magic(Buffer->getBuffer());
331 LLVMContext &Context = getGlobalContext();
332 std::string ErrorMessage;
333 if (magic == sys::fs::file_magic::bitcode) {
335 Result = ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage);
337 DumpSymbolNamesFromModule(Result);
340 error(ErrorMessage, Filename);
343 } else if (magic == sys::fs::file_magic::archive) {
344 OwningPtr<Binary> arch;
345 if (error(object::createBinary(Buffer.take(), arch), Filename))
348 if (object::Archive *a = dyn_cast<object::Archive>(arch.get())) {
349 for (object::Archive::child_iterator i = a->begin_children(),
350 e = a->end_children(); i != e; ++i) {
351 OwningPtr<Binary> child;
352 if (i->getAsBinary(child)) {
353 // Try opening it as a bitcode file.
354 OwningPtr<MemoryBuffer> buff(i->getBuffer());
357 Result = ParseBitcodeFile(buff.get(), Context, &ErrorMessage);
360 DumpSymbolNamesFromModule(Result);
365 if (object::ObjectFile *o = dyn_cast<ObjectFile>(child.get())) {
366 outs() << o->getFileName() << ":\n";
367 DumpSymbolNamesFromObject(o);
371 } else if (magic.is_object()) {
372 OwningPtr<Binary> obj;
373 if (error(object::createBinary(Buffer.take(), obj), Filename))
375 if (object::ObjectFile *o = dyn_cast<ObjectFile>(obj.get()))
376 DumpSymbolNamesFromObject(o);
378 errs() << ToolName << ": " << Filename << ": "
379 << "unrecognizable file type\n";
384 int main(int argc, char **argv) {
385 // Print a stack trace if we signal out.
386 sys::PrintStackTraceOnErrorSignal();
387 PrettyStackTraceProgram X(argc, argv);
389 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
390 cl::ParseCommandLineOptions(argc, argv, "llvm symbol table dumper\n");
392 // llvm-nm only reads binary files.
393 if (error(sys::Program::ChangeStdinToBinary()))
397 if (BSDFormat) OutputFormat = bsd;
398 if (POSIXFormat) OutputFormat = posix;
400 // The relative order of these is important. If you pass --size-sort it should
401 // only print out the size. However, if you pass -S --size-sort, it should
402 // print out both the size and address.
403 if (SizeSort && !PrintSize) PrintAddress = false;
404 if (OutputFormat == sysv || SizeSort) PrintSize = true;
406 switch (InputFilenames.size()) {
407 case 0: InputFilenames.push_back("-");
409 default: MultipleFiles = true;
412 std::for_each(InputFilenames.begin(), InputFilenames.end(),
413 DumpSymbolNamesFromFile);