Use CHECK-NEXT.
[llvm/stm8.git] / tools / llvm-nm / llvm-nm.cpp
blob014cb296500003249b55cd07d0efa5b6d475481f
1 //===-- llvm-nm.cpp - Symbol table dumping utility for llvm ---------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
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 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"
33 #include <algorithm>
34 #include <cctype>
35 #include <cerrno>
36 #include <cstring>
37 #include <vector>
38 using namespace llvm;
39 using namespace object;
41 namespace {
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));
53 cl::list<std::string>
54 InputFilenames(cl::Positional, cl::desc("<input bitcode files>"),
55 cl::ZeroOrMore);
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;
112 namespace {
113 struct NMSymbol {
114 uint64_t Address;
115 uint64_t Size;
116 char TypeChar;
117 StringRef Name;
120 static bool CompareSymbolAddress(const NMSymbol &a, const NMSymbol &b) {
121 if (a.Address < b.Address)
122 return true;
123 else if (a.Address == b.Address && a.Name < b.Name)
124 return true;
125 else
126 return false;
130 static bool CompareSymbolSize(const NMSymbol &a, const NMSymbol &b) {
131 if (a.Size < b.Size)
132 return true;
133 else if (a.Size == b.Size && a.Name < b.Name)
134 return true;
135 else
136 return false;
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";
151 outs().flush();
152 return true;
156 static void SortAndPrintSymbolList() {
157 if (!NoSort) {
158 if (NumericSort)
159 std::sort(SymbolList.begin(), SymbolList.end(), CompareSymbolAddress);
160 else if (SizeSort)
161 std::sort(SymbolList.begin(), SymbolList.end(), CompareSymbolSize);
162 else
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)
179 continue;
180 if ((i->TypeChar == 'U') && DefinedOnly)
181 continue;
182 if (SizeSort && !PrintAddress && i->Size == UnknownAddressOrSize)
183 continue;
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) {
202 if (PrintAddress)
203 outs() << SymbolAddrStr << ' ';
204 if (PrintSize) {
205 outs() << SymbolSizeStr;
206 if (i->Size != object::UnknownAddressOrSize)
207 outs() << ' ';
209 outs() << i->TypeChar << " " << i->Name << "\n";
210 } else if (OutputFormat == sysv) {
211 std::string PaddedName (i->Name);
212 while (PaddedName.length () < 20)
213 PaddedName += " ";
214 outs() << PaddedName << "|" << SymbolAddrStr << "| "
215 << i->TypeChar
216 << " | |" << SymbolSizeStr << "| |\n";
220 SymbolList.clear();
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';
237 return '?';
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())
247 return;
248 char TypeChar = TypeCharForSymbol(GV);
249 if (GV.hasLocalLinkage () && ExternalOnly)
250 return;
252 NMSymbol s;
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) {
272 error_code ec;
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;
277 bool internal;
278 if (error(i->isInternal(internal))) break;
279 if (!DebugSyms && internal)
280 continue;
281 NMSymbol s;
282 s.Size = object::UnknownAddressOrSize;
283 s.Address = object::UnknownAddressOrSize;
284 if (PrintSize || SizeSort) {
285 if (error(i->getSize(s.Size))) break;
287 if (PrintAddress)
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);
302 bool exists;
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();
310 Module *Result = 0;
311 if (Buffer.get())
312 Result = ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage);
314 if (Result) {
315 DumpSymbolNamesFromModule(Result);
316 delete Result;
317 } else
318 errs() << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
320 } else if (aPath.isArchive()) {
321 std::string ErrMsg;
322 Archive* archive = Archive::OpenAndLoad(sys::Path(Filename), Context,
323 &ErrorMessage);
324 if (!archive)
325 errs() << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
326 std::vector<Module *> Modules;
327 if (archive->getAllModules(Modules, &ErrorMessage)) {
328 errs() << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
329 return;
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";
337 return;
339 if (object::ObjectFile *o = dyn_cast<ObjectFile>(obj.get()))
340 DumpSymbolNamesFromObject(o);
341 } else {
342 errs() << ToolName << ": " << Filename << ": "
343 << "unrecognizable file type\n";
344 return;
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");
356 ToolName = argv[0];
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("-");
368 case 1: break;
369 default: MultipleFiles = true;
372 std::for_each(InputFilenames.begin(), InputFilenames.end(),
373 DumpSymbolNamesFromFile);
374 return 0;