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",
11 // that is, it prints out the names of symbols in a bitcode 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/LLVMContext.h"
20 #include "llvm/Module.h"
21 #include "llvm/Bitcode/ReaderWriter.h"
22 #include "llvm/Bitcode/Archive.h"
23 #include "llvm/Object/ObjectFile.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/ManagedStatic.h"
27 #include "llvm/Support/MemoryBuffer.h"
28 #include "llvm/Support/PrettyStackTrace.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include "llvm/Support/Signals.h"
31 #include "llvm/Support/Format.h"
32 #include "llvm/Support/system_error.h"
39 using namespace object
;
42 enum OutputFormatTy
{ bsd
, sysv
, posix
};
43 cl::opt
<OutputFormatTy
>
44 OutputFormat("format",
45 cl::desc("Specify output format"),
46 cl::values(clEnumVal(bsd
, "BSD format"),
47 clEnumVal(sysv
, "System V format"),
48 clEnumVal(posix
, "POSIX.2 format"),
49 clEnumValEnd
), cl::init(bsd
));
50 cl::alias
OutputFormat2("f", cl::desc("Alias for --format"),
51 cl::aliasopt(OutputFormat
));
54 InputFilenames(cl::Positional
, cl::desc("<input bitcode files>"),
57 cl::opt
<bool> UndefinedOnly("undefined-only",
58 cl::desc("Show only undefined symbols"));
59 cl::alias
UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
60 cl::aliasopt(UndefinedOnly
));
62 cl::opt
<bool> DefinedOnly("defined-only",
63 cl::desc("Show only defined symbols"));
65 cl::opt
<bool> ExternalOnly("extern-only",
66 cl::desc("Show only external symbols"));
67 cl::alias
ExternalOnly2("g", cl::desc("Alias for --extern-only"),
68 cl::aliasopt(ExternalOnly
));
70 cl::opt
<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
71 cl::opt
<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
73 cl::opt
<bool> PrintFileName("print-file-name",
74 cl::desc("Precede each symbol with the object file it came from"));
76 cl::alias
PrintFileNameA("A", cl::desc("Alias for --print-file-name"),
77 cl::aliasopt(PrintFileName
));
78 cl::alias
PrintFileNameo("o", cl::desc("Alias for --print-file-name"),
79 cl::aliasopt(PrintFileName
));
81 cl::opt
<bool> DebugSyms("debug-syms",
82 cl::desc("Show all symbols, even debugger only"));
83 cl::alias
DebugSymsa("a", cl::desc("Alias for --debug-syms"),
84 cl::aliasopt(DebugSyms
));
86 cl::opt
<bool> NumericSort("numeric-sort",
87 cl::desc("Sort symbols by address"));
88 cl::alias
NumericSortn("n", cl::desc("Alias for --numeric-sort"),
89 cl::aliasopt(NumericSort
));
90 cl::alias
NumericSortv("v", cl::desc("Alias for --numeric-sort"),
91 cl::aliasopt(NumericSort
));
93 cl::opt
<bool> NoSort("no-sort",
94 cl::desc("Show symbols in order encountered"));
95 cl::alias
NoSortp("p", cl::desc("Alias for --no-sort"),
96 cl::aliasopt(NoSort
));
98 cl::opt
<bool> PrintSize("print-size",
99 cl::desc("Show symbol size instead of address"));
100 cl::alias
PrintSizeS("S", cl::desc("Alias for --print-size"),
101 cl::aliasopt(PrintSize
));
103 cl::opt
<bool> SizeSort("size-sort", cl::desc("Sort symbols by size"));
105 bool PrintAddress
= true;
107 bool MultipleFiles
= false;
109 std::string ToolName
;
120 static bool CompareSymbolAddress(const NMSymbol
&a
, const NMSymbol
&b
) {
121 if (a
.Address
< b
.Address
)
123 else if (a
.Address
== b
.Address
&& a
.Name
< b
.Name
)
130 static bool CompareSymbolSize(const NMSymbol
&a
, const NMSymbol
&b
) {
133 else if (a
.Size
== b
.Size
&& a
.Name
< b
.Name
)
139 static bool CompareSymbolName(const NMSymbol
&a
, const NMSymbol
&b
) {
140 return a
.Name
< b
.Name
;
143 StringRef CurrentFilename
;
144 typedef std::vector
<NMSymbol
> SymbolListT
;
145 SymbolListT SymbolList
;
147 bool error(error_code ec
) {
148 if (!ec
) return false;
150 outs() << ToolName
<< ": error reading file: " << ec
.message() << ".\n";
156 static void SortAndPrintSymbolList() {
159 std::sort(SymbolList
.begin(), SymbolList
.end(), CompareSymbolAddress
);
161 std::sort(SymbolList
.begin(), SymbolList
.end(), CompareSymbolSize
);
163 std::sort(SymbolList
.begin(), SymbolList
.end(), CompareSymbolName
);
166 if (OutputFormat
== posix
&& MultipleFiles
) {
167 outs() << '\n' << CurrentFilename
<< ":\n";
168 } else if (OutputFormat
== bsd
&& MultipleFiles
) {
169 outs() << "\n" << CurrentFilename
<< ":\n";
170 } else if (OutputFormat
== sysv
) {
171 outs() << "\n\nSymbols from " << CurrentFilename
<< ":\n\n"
172 << "Name Value Class Type"
173 << " Size Line Section\n";
176 for (SymbolListT::iterator i
= SymbolList
.begin(),
177 e
= SymbolList
.end(); i
!= e
; ++i
) {
178 if ((i
->TypeChar
!= 'U') && UndefinedOnly
)
180 if ((i
->TypeChar
== 'U') && DefinedOnly
)
182 if (SizeSort
&& !PrintAddress
&& i
->Size
== UnknownAddressOrSize
)
185 char SymbolAddrStr
[10] = "";
186 char SymbolSizeStr
[10] = "";
188 if (OutputFormat
== sysv
|| i
->Address
== object::UnknownAddressOrSize
)
189 strcpy(SymbolAddrStr
, " ");
190 if (OutputFormat
== sysv
)
191 strcpy(SymbolSizeStr
, " ");
193 if (i
->Address
!= object::UnknownAddressOrSize
)
194 format("%08x", i
->Address
).print(SymbolAddrStr
, sizeof(SymbolAddrStr
));
195 if (i
->Size
!= object::UnknownAddressOrSize
)
196 format("%08x", i
->Size
).print(SymbolSizeStr
, sizeof(SymbolSizeStr
));
198 if (OutputFormat
== posix
) {
199 outs() << i
->Name
<< " " << i
->TypeChar
<< " "
200 << SymbolAddrStr
<< SymbolSizeStr
<< "\n";
201 } else if (OutputFormat
== bsd
) {
203 outs() << SymbolAddrStr
<< ' ';
205 outs() << SymbolSizeStr
;
206 if (i
->Size
!= object::UnknownAddressOrSize
)
209 outs() << i
->TypeChar
<< " " << i
->Name
<< "\n";
210 } else if (OutputFormat
== sysv
) {
211 std::string
PaddedName (i
->Name
);
212 while (PaddedName
.length () < 20)
214 outs() << PaddedName
<< "|" << SymbolAddrStr
<< "| "
216 << " | |" << SymbolSizeStr
<< "| |\n";
223 static char TypeCharForSymbol(GlobalValue
&GV
) {
224 if (GV
.isDeclaration()) return 'U';
225 if (GV
.hasLinkOnceLinkage()) return 'C';
226 if (GV
.hasCommonLinkage()) return 'C';
227 if (GV
.hasWeakLinkage()) return 'W';
228 if (isa
<Function
>(GV
) && GV
.hasInternalLinkage()) return 't';
229 if (isa
<Function
>(GV
)) return 'T';
230 if (isa
<GlobalVariable
>(GV
) && GV
.hasInternalLinkage()) return 'd';
231 if (isa
<GlobalVariable
>(GV
)) return 'D';
232 if (const GlobalAlias
*GA
= dyn_cast
<GlobalAlias
>(&GV
)) {
233 const GlobalValue
*AliasedGV
= GA
->getAliasedGlobal();
234 if (isa
<Function
>(AliasedGV
)) return 'T';
235 if (isa
<GlobalVariable
>(AliasedGV
)) return 'D';
240 static void DumpSymbolNameForGlobalValue(GlobalValue
&GV
) {
241 // Private linkage and available_externally linkage don't exist in symtab.
242 if (GV
.hasPrivateLinkage() ||
243 GV
.hasLinkerPrivateLinkage() ||
244 GV
.hasLinkerPrivateWeakLinkage() ||
245 GV
.hasLinkerPrivateWeakDefAutoLinkage() ||
246 GV
.hasAvailableExternallyLinkage())
248 char TypeChar
= TypeCharForSymbol(GV
);
249 if (GV
.hasLocalLinkage () && ExternalOnly
)
253 s
.Address
= object::UnknownAddressOrSize
;
254 s
.Size
= object::UnknownAddressOrSize
;
255 s
.TypeChar
= TypeChar
;
256 s
.Name
= GV
.getName();
257 SymbolList
.push_back(s
);
260 static void DumpSymbolNamesFromModule(Module
*M
) {
261 CurrentFilename
= M
->getModuleIdentifier();
262 std::for_each (M
->begin(), M
->end(), DumpSymbolNameForGlobalValue
);
263 std::for_each (M
->global_begin(), M
->global_end(),
264 DumpSymbolNameForGlobalValue
);
265 std::for_each (M
->alias_begin(), M
->alias_end(),
266 DumpSymbolNameForGlobalValue
);
268 SortAndPrintSymbolList();
271 static void DumpSymbolNamesFromObject(ObjectFile
*obj
) {
273 for (ObjectFile::symbol_iterator i
= obj
->begin_symbols(),
274 e
= obj
->end_symbols();
275 i
!= e
; i
.increment(ec
)) {
276 if (error(ec
)) break;
278 if (error(i
->isInternal(internal
))) break;
279 if (!DebugSyms
&& internal
)
282 s
.Size
= object::UnknownAddressOrSize
;
283 s
.Address
= object::UnknownAddressOrSize
;
284 if (PrintSize
|| SizeSort
) {
285 if (error(i
->getSize(s
.Size
))) break;
288 if (error(i
->getAddress(s
.Address
))) break;
289 if (error(i
->getNMTypeChar(s
.TypeChar
))) break;
290 if (error(i
->getName(s
.Name
))) break;
291 SymbolList
.push_back(s
);
294 CurrentFilename
= obj
->getFileName();
295 SortAndPrintSymbolList();
298 static void DumpSymbolNamesFromFile(std::string
&Filename
) {
299 LLVMContext
&Context
= getGlobalContext();
300 std::string ErrorMessage
;
301 sys::Path
aPath(Filename
);
303 if (sys::fs::exists(aPath
.str(), exists
) || !exists
)
304 errs() << ToolName
<< ": '" << Filename
<< "': " << "No such file\n";
305 // Note: Currently we do not support reading an archive from stdin.
306 if (Filename
== "-" || aPath
.isBitcodeFile()) {
307 OwningPtr
<MemoryBuffer
> Buffer
;
308 if (error_code ec
= MemoryBuffer::getFileOrSTDIN(Filename
, Buffer
))
309 ErrorMessage
= ec
.message();
312 Result
= ParseBitcodeFile(Buffer
.get(), Context
, &ErrorMessage
);
315 DumpSymbolNamesFromModule(Result
);
318 errs() << ToolName
<< ": " << Filename
<< ": " << ErrorMessage
<< "\n";
320 } else if (aPath
.isArchive()) {
322 Archive
* archive
= Archive::OpenAndLoad(sys::Path(Filename
), Context
,
325 errs() << ToolName
<< ": " << Filename
<< ": " << ErrorMessage
<< "\n";
326 std::vector
<Module
*> Modules
;
327 if (archive
->getAllModules(Modules
, &ErrorMessage
)) {
328 errs() << ToolName
<< ": " << Filename
<< ": " << ErrorMessage
<< "\n";
331 MultipleFiles
= true;
332 std::for_each (Modules
.begin(), Modules
.end(), DumpSymbolNamesFromModule
);
333 } else if (aPath
.isObjectFile()) {
334 OwningPtr
<Binary
> obj
;
335 if (error_code ec
= object::createBinary(aPath
.str(), obj
)) {
336 errs() << ToolName
<< ": " << Filename
<< ": " << ec
.message() << ".\n";
339 if (object::ObjectFile
*o
= dyn_cast
<ObjectFile
>(obj
.get()))
340 DumpSymbolNamesFromObject(o
);
342 errs() << ToolName
<< ": " << Filename
<< ": "
343 << "unrecognizable file type\n";
348 int main(int argc
, char **argv
) {
349 // Print a stack trace if we signal out.
350 sys::PrintStackTraceOnErrorSignal();
351 PrettyStackTraceProgram
X(argc
, argv
);
353 llvm_shutdown_obj Y
; // Call llvm_shutdown() on exit.
354 cl::ParseCommandLineOptions(argc
, argv
, "llvm symbol table dumper\n");
357 if (BSDFormat
) OutputFormat
= bsd
;
358 if (POSIXFormat
) OutputFormat
= posix
;
360 // The relative order of these is important. If you pass --size-sort it should
361 // only print out the size. However, if you pass -S --size-sort, it should
362 // print out both the size and address.
363 if (SizeSort
&& !PrintSize
) PrintAddress
= false;
364 if (OutputFormat
== sysv
|| SizeSort
) PrintSize
= true;
366 switch (InputFilenames
.size()) {
367 case 0: InputFilenames
.push_back("-");
369 default: MultipleFiles
= true;
372 std::for_each(InputFilenames
.begin(), InputFilenames
.end(),
373 DumpSymbolNamesFromFile
);