blob: 8065787945f40a753c40064caf925fe2472a5e7c [file] [log] [blame]
Michael J. Spencer92e1deb2011-01-20 06:39:06 +00001//===-- llvm-objdump.cpp - Object file 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 binutils "objdump", that is, it
11// dumps out a plethora of information about an object file depending on the
12// flags.
13//
Michael J. Spencerdd3aa9e2013-02-05 20:27:22 +000014// The flags and output of this program should be near identical to those of
15// binutils objdump.
16//
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000017//===----------------------------------------------------------------------===//
18
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000019#include "llvm-objdump.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000020#include "llvm/ADT/OwningPtr.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000021#include "llvm/ADT/STLExtras.h"
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +000022#include "llvm/ADT/StringExtras.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000023#include "llvm/ADT/Triple.h"
24#include "llvm/MC/MCAsmInfo.h"
Ahmed Bougachaef993562013-05-24 01:07:04 +000025#include "llvm/MC/MCAtom.h"
Ahmed Bougacha2c94d0f2013-05-24 00:39:57 +000026#include "llvm/MC/MCContext.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000027#include "llvm/MC/MCDisassembler.h"
Ahmed Bougachaef993562013-05-24 01:07:04 +000028#include "llvm/MC/MCFunction.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000029#include "llvm/MC/MCInst.h"
30#include "llvm/MC/MCInstPrinter.h"
Ahmed Bougachaef993562013-05-24 01:07:04 +000031#include "llvm/MC/MCInstrAnalysis.h"
Craig Topper17463b32012-04-02 06:09:36 +000032#include "llvm/MC/MCInstrInfo.h"
Ahmed Bougachaef993562013-05-24 01:07:04 +000033#include "llvm/MC/MCModule.h"
Ahmed Bougacha171ac8c2013-08-21 07:29:02 +000034#include "llvm/MC/MCModuleYAML.h"
Ahmed Bougachaef993562013-05-24 01:07:04 +000035#include "llvm/MC/MCObjectDisassembler.h"
Ahmed Bougacha2c94d0f2013-05-24 00:39:57 +000036#include "llvm/MC/MCObjectFileInfo.h"
37#include "llvm/MC/MCObjectSymbolizer.h"
Jim Grosbachc6449b62012-03-05 19:33:20 +000038#include "llvm/MC/MCRegisterInfo.h"
Ahmed Bougacha2c94d0f2013-05-24 00:39:57 +000039#include "llvm/MC/MCRelocationInfo.h"
Ahmed Bougachaef993562013-05-24 01:07:04 +000040#include "llvm/MC/MCSubtargetInfo.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000041#include "llvm/Object/Archive.h"
42#include "llvm/Object/COFF.h"
Rafael Espindolacef81b32012-12-21 03:47:03 +000043#include "llvm/Object/MachO.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000044#include "llvm/Object/ObjectFile.h"
Michael J. Spencer27781b72011-10-08 00:18:30 +000045#include "llvm/Support/Casting.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000046#include "llvm/Support/CommandLine.h"
47#include "llvm/Support/Debug.h"
Michael J. Spencer27781b72011-10-08 00:18:30 +000048#include "llvm/Support/FileSystem.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000049#include "llvm/Support/Format.h"
Benjamin Kramer853b0fd2011-07-25 23:04:36 +000050#include "llvm/Support/GraphWriter.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000051#include "llvm/Support/Host.h"
52#include "llvm/Support/ManagedStatic.h"
53#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000054#include "llvm/Support/MemoryObject.h"
55#include "llvm/Support/PrettyStackTrace.h"
56#include "llvm/Support/Signals.h"
57#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000058#include "llvm/Support/TargetRegistry.h"
59#include "llvm/Support/TargetSelect.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000060#include "llvm/Support/raw_ostream.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000061#include "llvm/Support/system_error.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000062#include <algorithm>
Benjamin Kramer81bbdfd2012-03-23 11:49:32 +000063#include <cctype>
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000064#include <cstring>
Ahmed Bougacha171ac8c2013-08-21 07:29:02 +000065
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000066using namespace llvm;
67using namespace object;
68
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000069static cl::list<std::string>
70InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000071
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000072static cl::opt<bool>
73Disassemble("disassemble",
74 cl::desc("Display assembler mnemonics for the machine instructions"));
75static cl::alias
76Disassembled("d", cl::desc("Alias for --disassemble"),
77 cl::aliasopt(Disassemble));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000078
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000079static cl::opt<bool>
Michael J. Spencer27781b72011-10-08 00:18:30 +000080Relocations("r", cl::desc("Display the relocation entries in the file"));
81
82static cl::opt<bool>
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +000083SectionContents("s", cl::desc("Display the content of each section"));
84
85static cl::opt<bool>
Michael J. Spencer22ff0f32011-10-18 19:32:17 +000086SymbolTable("t", cl::desc("Display the symbol table"));
87
88static cl::opt<bool>
Rafael Espindolacef81b32012-12-21 03:47:03 +000089MachOOpt("macho", cl::desc("Use MachO specific object file parser"));
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000090static cl::alias
Rafael Espindolacef81b32012-12-21 03:47:03 +000091MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachOOpt));
Benjamin Kramer685a2502011-07-20 19:37:35 +000092
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000093cl::opt<std::string>
94llvm::TripleName("triple", cl::desc("Target triple to disassemble for, "
95 "see -version for available targets"));
96
97cl::opt<std::string>
98llvm::ArchName("arch", cl::desc("Target arch to disassemble for, "
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000099 "see -version for available targets"));
100
Nick Lewycky023bb152011-10-10 21:21:34 +0000101static cl::opt<bool>
102SectionHeaders("section-headers", cl::desc("Display summaries of the headers "
103 "for each section."));
104static cl::alias
105SectionHeadersShort("headers", cl::desc("Alias for --section-headers"),
106 cl::aliasopt(SectionHeaders));
107static cl::alias
108SectionHeadersShorter("h", cl::desc("Alias for --section-headers"),
109 cl::aliasopt(SectionHeaders));
110
Jack Carterfd6d1652012-08-28 19:24:49 +0000111static cl::list<std::string>
112MAttrs("mattr",
113 cl::CommaSeparated,
114 cl::desc("Target specific attributes"),
115 cl::value_desc("a1,+a2,-a3,..."));
116
Eli Bendersky8b9da532012-11-20 22:57:02 +0000117static cl::opt<bool>
118NoShowRawInsn("no-show-raw-insn", cl::desc("When disassembling instructions, "
119 "do not print the instruction bytes."));
120
Michael J. Spencereef7b622012-12-05 20:12:35 +0000121static cl::opt<bool>
122UnwindInfo("unwind-info", cl::desc("Display unwind information"));
123
124static cl::alias
125UnwindInfoShort("u", cl::desc("Alias for --unwind-info"),
126 cl::aliasopt(UnwindInfo));
127
Michael J. Spencerb2c064c2013-01-06 03:56:49 +0000128static cl::opt<bool>
129PrivateHeaders("private-headers",
130 cl::desc("Display format specific file headers"));
131
132static cl::alias
133PrivateHeadersShort("p", cl::desc("Alias for --private-headers"),
134 cl::aliasopt(PrivateHeaders));
135
Ahmed Bougacha2c94d0f2013-05-24 00:39:57 +0000136static cl::opt<bool>
137Symbolize("symbolize", cl::desc("When disassembling instructions, "
138 "try to symbolize operands."));
139
Ahmed Bougachaef993562013-05-24 01:07:04 +0000140static cl::opt<bool>
141CFG("cfg", cl::desc("Create a CFG for every function found in the object"
142 " and write it to a graphviz file"));
143
Ahmed Bougacha171ac8c2013-08-21 07:29:02 +0000144// FIXME: Does it make sense to have a dedicated tool for yaml cfg output?
145static cl::opt<std::string>
146YAMLCFG("yaml-cfg",
147 cl::desc("Create a CFG and write it as a YAML MCModule."),
148 cl::value_desc("yaml output file"));
149
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000150static StringRef ToolName;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000151
Michael J. Spencereef7b622012-12-05 20:12:35 +0000152bool llvm::error(error_code ec) {
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000153 if (!ec) return false;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000154
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000155 outs() << ToolName << ": error reading file: " << ec.message() << ".\n";
156 outs().flush();
157 return true;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000158}
159
Jim Grosbach3f5d1a22012-08-07 17:53:14 +0000160static const Target *getTarget(const ObjectFile *Obj = NULL) {
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000161 // Figure out the target triple.
Kevin Enderby9ed9e5d2012-05-08 23:38:45 +0000162 llvm::Triple TheTriple("unknown-unknown-unknown");
Michael J. Spencerd11699d2011-01-20 07:22:04 +0000163 if (TripleName.empty()) {
Ahmed Bougacha2c94d0f2013-05-24 00:39:57 +0000164 if (Obj) {
Kevin Enderby9ed9e5d2012-05-08 23:38:45 +0000165 TheTriple.setArch(Triple::ArchType(Obj->getArch()));
Ahmed Bougacha2c94d0f2013-05-24 00:39:57 +0000166 // TheTriple defaults to ELF, and COFF doesn't have an environment:
167 // the best we can do here is indicate that it is mach-o.
168 if (Obj->isMachO())
169 TheTriple.setEnvironment(Triple::MachO);
170 }
Michael J. Spencerd11699d2011-01-20 07:22:04 +0000171 } else
Kevin Enderby9ed9e5d2012-05-08 23:38:45 +0000172 TheTriple.setTriple(Triple::normalize(TripleName));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000173
174 // Get the target specific parser.
175 std::string Error;
Kevin Enderby9ed9e5d2012-05-08 23:38:45 +0000176 const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple,
177 Error);
178 if (!TheTarget) {
179 errs() << ToolName << ": " << Error;
180 return 0;
181 }
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000182
Kevin Enderby9ed9e5d2012-05-08 23:38:45 +0000183 // Update the triple name and return the found target.
184 TripleName = TheTriple.getTriple();
185 return TheTarget;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000186}
187
Ahmed Bougachaef993562013-05-24 01:07:04 +0000188// Write a graphviz file for the CFG inside an MCFunction.
Ahmed Bougacha171ac8c2013-08-21 07:29:02 +0000189// FIXME: Use GraphWriter
Ahmed Bougachaef993562013-05-24 01:07:04 +0000190static void emitDOTFile(const char *FileName, const MCFunction &f,
191 MCInstPrinter *IP) {
192 // Start a new dot file.
193 std::string Error;
194 raw_fd_ostream Out(FileName, Error);
195 if (!Error.empty()) {
196 errs() << "llvm-objdump: warning: " << Error << '\n';
197 return;
198 }
199
200 Out << "digraph \"" << f.getName() << "\" {\n";
201 Out << "graph [ rankdir = \"LR\" ];\n";
202 for (MCFunction::const_iterator i = f.begin(), e = f.end(); i != e; ++i) {
203 // Only print blocks that have predecessors.
204 bool hasPreds = (*i)->pred_begin() != (*i)->pred_end();
205
206 if (!hasPreds && i != f.begin())
207 continue;
208
209 Out << '"' << (*i)->getInsts()->getBeginAddr() << "\" [ label=\"<a>";
210 // Print instructions.
211 for (unsigned ii = 0, ie = (*i)->getInsts()->size(); ii != ie;
212 ++ii) {
213 if (ii != 0) // Not the first line, start a new row.
214 Out << '|';
215 if (ii + 1 == ie) // Last line, add an end id.
216 Out << "<o>";
217
218 // Escape special chars and print the instruction in mnemonic form.
219 std::string Str;
220 raw_string_ostream OS(Str);
221 IP->printInst(&(*i)->getInsts()->at(ii).Inst, OS, "");
222 Out << DOT::EscapeString(OS.str());
223 }
224 Out << "\" shape=\"record\" ];\n";
225
226 // Add edges.
227 for (MCBasicBlock::succ_const_iterator si = (*i)->succ_begin(),
228 se = (*i)->succ_end(); si != se; ++si)
229 Out << (*i)->getInsts()->getBeginAddr() << ":o -> "
230 << (*si)->getInsts()->getBeginAddr() << ":a\n";
231 }
232 Out << "}\n";
233}
David Blaikie2d24e2a2011-12-20 02:50:00 +0000234
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000235void llvm::DumpBytes(StringRef bytes) {
236 static const char hex_rep[] = "0123456789abcdef";
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000237 // FIXME: The real way to do this is to figure out the longest instruction
238 // and align to that size before printing. I'll fix this when I get
239 // around to outputting relocations.
240 // 15 is the longest x86 instruction
241 // 3 is for the hex rep of a byte + a space.
242 // 1 is for the null terminator.
243 enum { OutputSize = (15 * 3) + 1 };
244 char output[OutputSize];
245
246 assert(bytes.size() <= 15
247 && "DumpBytes only supports instructions of up to 15 bytes");
248 memset(output, ' ', sizeof(output));
249 unsigned index = 0;
250 for (StringRef::iterator i = bytes.begin(),
251 e = bytes.end(); i != e; ++i) {
252 output[index] = hex_rep[(*i & 0xF0) >> 4];
253 output[index + 1] = hex_rep[*i & 0xF];
254 index += 3;
255 }
256
257 output[sizeof(output) - 1] = 0;
258 outs() << output;
259}
260
Michael J. Spencereef7b622012-12-05 20:12:35 +0000261bool llvm::RelocAddressLess(RelocationRef a, RelocationRef b) {
Michael J. Spencer942eb002011-10-13 22:17:18 +0000262 uint64_t a_addr, b_addr;
Rafael Espindola956ca722013-04-25 12:28:45 +0000263 if (error(a.getOffset(a_addr))) return false;
264 if (error(b.getOffset(b_addr))) return false;
Michael J. Spencer942eb002011-10-13 22:17:18 +0000265 return a_addr < b_addr;
266}
267
268static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
Jim Grosbach3f5d1a22012-08-07 17:53:14 +0000269 const Target *TheTarget = getTarget(Obj);
270 // getTarget() will have already issued a diagnostic if necessary, so
271 // just bail here if it failed.
272 if (!TheTarget)
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000273 return;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000274
Jack Carterfd6d1652012-08-28 19:24:49 +0000275 // Package up features to be passed to target/subtarget
276 std::string FeaturesStr;
277 if (MAttrs.size()) {
278 SubtargetFeatures Features;
279 for (unsigned i = 0; i != MAttrs.size(); ++i)
280 Features.AddFeature(MAttrs[i]);
281 FeaturesStr = Features.getString();
282 }
283
Ahmed Bougacha27a33ad2013-05-16 21:28:23 +0000284 OwningPtr<const MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
285 if (!MRI) {
286 errs() << "error: no register info for target " << TripleName << "\n";
287 return;
288 }
289
290 // Set up disassembler.
291 OwningPtr<const MCAsmInfo> AsmInfo(
292 TheTarget->createMCAsmInfo(*MRI, TripleName));
Ahmed Bougacha27a33ad2013-05-16 21:28:23 +0000293 if (!AsmInfo) {
294 errs() << "error: no assembly info for target " << TripleName << "\n";
295 return;
296 }
297
298 OwningPtr<const MCSubtargetInfo> STI(
299 TheTarget->createMCSubtargetInfo(TripleName, "", FeaturesStr));
Ahmed Bougacha27a33ad2013-05-16 21:28:23 +0000300 if (!STI) {
301 errs() << "error: no subtarget info for target " << TripleName << "\n";
302 return;
303 }
304
Ahmed Bougacha27a33ad2013-05-16 21:28:23 +0000305 OwningPtr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
306 if (!MII) {
307 errs() << "error: no instruction info for target " << TripleName << "\n";
308 return;
309 }
310
Ahmed Bougacha2c94d0f2013-05-24 00:39:57 +0000311 OwningPtr<MCDisassembler> DisAsm(TheTarget->createMCDisassembler(*STI));
312 if (!DisAsm) {
313 errs() << "error: no disassembler for target " << TripleName << "\n";
314 return;
315 }
316
317 OwningPtr<const MCObjectFileInfo> MOFI;
318 OwningPtr<MCContext> Ctx;
319
320 if (Symbolize) {
321 MOFI.reset(new MCObjectFileInfo);
Bill Wendling99cb6222013-06-18 07:20:20 +0000322 Ctx.reset(new MCContext(AsmInfo.get(), MRI.get(), MOFI.get()));
Ahmed Bougacha2c94d0f2013-05-24 00:39:57 +0000323 OwningPtr<MCRelocationInfo> RelInfo(
324 TheTarget->createMCRelocationInfo(TripleName, *Ctx.get()));
325 if (RelInfo) {
326 OwningPtr<MCSymbolizer> Symzer(
327 MCObjectSymbolizer::createObjectSymbolizer(*Ctx.get(), RelInfo, Obj));
328 if (Symzer)
329 DisAsm->setSymbolizer(Symzer);
330 }
331 }
332
Ahmed Bougachaef993562013-05-24 01:07:04 +0000333 OwningPtr<const MCInstrAnalysis>
334 MIA(TheTarget->createMCInstrAnalysis(MII.get()));
335
Ahmed Bougacha27a33ad2013-05-16 21:28:23 +0000336 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
337 OwningPtr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
338 AsmPrinterVariant, *AsmInfo, *MII, *MRI, *STI));
339 if (!IP) {
340 errs() << "error: no instruction printer for target " << TripleName
341 << '\n';
342 return;
343 }
344
Ahmed Bougacha171ac8c2013-08-21 07:29:02 +0000345 if (CFG || !YAMLCFG.empty()) {
Ahmed Bougachaef993562013-05-24 01:07:04 +0000346 OwningPtr<MCObjectDisassembler> OD(
347 new MCObjectDisassembler(*Obj, *DisAsm, *MIA));
348 OwningPtr<MCModule> Mod(OD->buildModule(/* withCFG */ true));
349 for (MCModule::const_atom_iterator AI = Mod->atom_begin(),
350 AE = Mod->atom_end();
351 AI != AE; ++AI) {
352 outs() << "Atom " << (*AI)->getName() << ": \n";
353 if (const MCTextAtom *TA = dyn_cast<MCTextAtom>(*AI)) {
354 for (MCTextAtom::const_iterator II = TA->begin(), IE = TA->end();
355 II != IE;
356 ++II) {
357 IP->printInst(&II->Inst, outs(), "");
358 outs() << "\n";
359 }
360 }
361 }
Ahmed Bougacha171ac8c2013-08-21 07:29:02 +0000362 if (CFG) {
363 for (MCModule::const_func_iterator FI = Mod->func_begin(),
364 FE = Mod->func_end();
365 FI != FE; ++FI) {
366 static int filenum = 0;
367 emitDOTFile((Twine((*FI)->getName()) + "_" +
368 utostr(filenum) + ".dot").str().c_str(),
369 **FI, IP.get());
370 ++filenum;
371 }
372 }
373 if (!YAMLCFG.empty()) {
374 std::string Error;
375 raw_fd_ostream YAMLOut(YAMLCFG.c_str(), Error);
376 if (!Error.empty()) {
Ahmed Bougacha7413b542013-08-21 16:13:25 +0000377 errs() << ToolName << ": warning: " << Error << '\n';
Ahmed Bougacha171ac8c2013-08-21 07:29:02 +0000378 return;
379 }
380 mcmodule2yaml(YAMLOut, *Mod, *MII, *MRI);
Ahmed Bougachaef993562013-05-24 01:07:04 +0000381 }
382 }
383
384
Michael J. Spencer25b15772011-06-25 17:55:23 +0000385 error_code ec;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000386 for (section_iterator i = Obj->begin_sections(),
Michael J. Spencer27781b72011-10-08 00:18:30 +0000387 e = Obj->end_sections();
388 i != e; i.increment(ec)) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000389 if (error(ec)) break;
390 bool text;
391 if (error(i->isText(text))) break;
392 if (!text) continue;
393
Michael J. Spencer942eb002011-10-13 22:17:18 +0000394 uint64_t SectionAddr;
395 if (error(i->getAddress(SectionAddr))) break;
396
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000397 // Make a list of all the symbols in this section.
398 std::vector<std::pair<uint64_t, StringRef> > Symbols;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000399 for (symbol_iterator si = Obj->begin_symbols(),
Michael J. Spencer27781b72011-10-08 00:18:30 +0000400 se = Obj->end_symbols();
401 si != se; si.increment(ec)) {
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000402 bool contains;
403 if (!error(i->containsSymbol(*si, contains)) && contains) {
404 uint64_t Address;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000405 if (error(si->getAddress(Address))) break;
Eric Christopher99ff2ba2013-04-03 18:31:23 +0000406 if (Address == UnknownAddressOrSize) continue;
Cameron Zwarichaab21912012-02-03 04:13:37 +0000407 Address -= SectionAddr;
408
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000409 StringRef Name;
410 if (error(si->getName(Name))) break;
411 Symbols.push_back(std::make_pair(Address, Name));
412 }
413 }
414
415 // Sort the symbols by address, just in case they didn't come in that way.
416 array_pod_sort(Symbols.begin(), Symbols.end());
417
Michael J. Spencer942eb002011-10-13 22:17:18 +0000418 // Make a list of all the relocations for this section.
419 std::vector<RelocationRef> Rels;
420 if (InlineRelocs) {
421 for (relocation_iterator ri = i->begin_relocations(),
422 re = i->end_relocations();
Jim Grosbach3f5d1a22012-08-07 17:53:14 +0000423 ri != re; ri.increment(ec)) {
Michael J. Spencer942eb002011-10-13 22:17:18 +0000424 if (error(ec)) break;
425 Rels.push_back(*ri);
426 }
427 }
428
429 // Sort relocations by address.
430 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
431
Rafael Espindolacef81b32012-12-21 03:47:03 +0000432 StringRef SegmentName = "";
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000433 if (const MachOObjectFile *MachO =
434 dyn_cast<const MachOObjectFile>(Obj)) {
Rafael Espindolacef81b32012-12-21 03:47:03 +0000435 DataRefImpl DR = i->getRawDataRefImpl();
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000436 SegmentName = MachO->getSectionFinalSegmentName(DR);
Rafael Espindolacef81b32012-12-21 03:47:03 +0000437 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000438 StringRef name;
439 if (error(i->getName(name))) break;
Rafael Espindolacef81b32012-12-21 03:47:03 +0000440 outs() << "Disassembly of section ";
441 if (!SegmentName.empty())
442 outs() << SegmentName << ",";
443 outs() << name << ':';
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000444
445 // If the section has no symbols just insert a dummy one and disassemble
446 // the whole section.
447 if (Symbols.empty())
448 Symbols.push_back(std::make_pair(0, name));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000449
Ahmed Bougacha2c94d0f2013-05-24 00:39:57 +0000450
451 SmallString<40> Comments;
452 raw_svector_ostream CommentStream(Comments);
453
Michael J. Spencer25b15772011-06-25 17:55:23 +0000454 StringRef Bytes;
455 if (error(i->getContents(Bytes))) break;
Ahmed Bougacha2c94d0f2013-05-24 00:39:57 +0000456 StringRefMemoryObject memoryObject(Bytes, SectionAddr);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000457 uint64_t Size;
458 uint64_t Index;
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000459 uint64_t SectSize;
460 if (error(i->getSize(SectSize))) break;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000461
Michael J. Spencer942eb002011-10-13 22:17:18 +0000462 std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
463 std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000464 // Disassemble symbol by symbol.
465 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
466 uint64_t Start = Symbols[si].first;
Michael J. Spencer178dbd42011-10-13 20:37:08 +0000467 uint64_t End;
468 // The end is either the size of the section or the beginning of the next
469 // symbol.
470 if (si == se - 1)
471 End = SectSize;
472 // Make sure this symbol takes up space.
473 else if (Symbols[si + 1].first != Start)
474 End = Symbols[si + 1].first - 1;
475 else
476 // This symbol has the same address as the next symbol. Skip it.
477 continue;
478
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000479 outs() << '\n' << Symbols[si].second << ":\n";
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000480
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000481#ifndef NDEBUG
482 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
483#else
484 raw_ostream &DebugOut = nulls();
485#endif
486
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000487 for (Index = Start; Index < End; Index += Size) {
488 MCInst Inst;
Owen Anderson98c5dda2011-09-15 23:38:46 +0000489
Ahmed Bougacha2c94d0f2013-05-24 00:39:57 +0000490 if (DisAsm->getInstruction(Inst, Size, memoryObject,
491 SectionAddr + Index,
492 DebugOut, CommentStream)) {
Eli Bendersky8b9da532012-11-20 22:57:02 +0000493 outs() << format("%8" PRIx64 ":", SectionAddr + Index);
494 if (!NoShowRawInsn) {
495 outs() << "\t";
496 DumpBytes(StringRef(Bytes.data() + Index, Size));
497 }
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000498 IP->printInst(&Inst, outs(), "");
Ahmed Bougacha2c94d0f2013-05-24 00:39:57 +0000499 outs() << CommentStream.str();
500 Comments.clear();
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000501 outs() << "\n";
502 } else {
503 errs() << ToolName << ": warning: invalid instruction encoding\n";
504 if (Size == 0)
505 Size = 1; // skip illegible bytes
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000506 }
Michael J. Spencer942eb002011-10-13 22:17:18 +0000507
508 // Print relocation for instruction.
509 while (rel_cur != rel_end) {
Owen Anderson0685e942011-10-25 20:35:53 +0000510 bool hidden = false;
Michael J. Spencer942eb002011-10-13 22:17:18 +0000511 uint64_t addr;
512 SmallString<16> name;
513 SmallString<32> val;
Owen Anderson0685e942011-10-25 20:35:53 +0000514
515 // If this relocation is hidden, skip it.
516 if (error(rel_cur->getHidden(hidden))) goto skip_print_rel;
517 if (hidden) goto skip_print_rel;
518
Rafael Espindola956ca722013-04-25 12:28:45 +0000519 if (error(rel_cur->getOffset(addr))) goto skip_print_rel;
Michael J. Spencer942eb002011-10-13 22:17:18 +0000520 // Stop when rel_cur's address is past the current instruction.
Owen Anderson34749ce2011-10-25 20:15:39 +0000521 if (addr >= Index + Size) break;
Michael J. Spencer942eb002011-10-13 22:17:18 +0000522 if (error(rel_cur->getTypeName(name))) goto skip_print_rel;
523 if (error(rel_cur->getValueString(val))) goto skip_print_rel;
524
Benjamin Kramer51cf8662012-03-10 02:04:38 +0000525 outs() << format("\t\t\t%8" PRIx64 ": ", SectionAddr + addr) << name
526 << "\t" << val << "\n";
Michael J. Spencer942eb002011-10-13 22:17:18 +0000527
528 skip_print_rel:
529 ++rel_cur;
530 }
Benjamin Kramer685a2502011-07-20 19:37:35 +0000531 }
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000532 }
533 }
534}
535
Michael J. Spencer27781b72011-10-08 00:18:30 +0000536static void PrintRelocations(const ObjectFile *o) {
537 error_code ec;
538 for (section_iterator si = o->begin_sections(), se = o->end_sections();
539 si != se; si.increment(ec)){
540 if (error(ec)) return;
541 if (si->begin_relocations() == si->end_relocations())
542 continue;
543 StringRef secname;
544 if (error(si->getName(secname))) continue;
545 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
546 for (relocation_iterator ri = si->begin_relocations(),
547 re = si->end_relocations();
548 ri != re; ri.increment(ec)) {
549 if (error(ec)) return;
550
Owen Anderson0685e942011-10-25 20:35:53 +0000551 bool hidden;
Michael J. Spencer27781b72011-10-08 00:18:30 +0000552 uint64_t address;
553 SmallString<32> relocname;
554 SmallString<32> valuestr;
Owen Anderson0685e942011-10-25 20:35:53 +0000555 if (error(ri->getHidden(hidden))) continue;
556 if (hidden) continue;
Michael J. Spencer27781b72011-10-08 00:18:30 +0000557 if (error(ri->getTypeName(relocname))) continue;
Rafael Espindola956ca722013-04-25 12:28:45 +0000558 if (error(ri->getOffset(address))) continue;
Michael J. Spencer27781b72011-10-08 00:18:30 +0000559 if (error(ri->getValueString(valuestr))) continue;
560 outs() << address << " " << relocname << " " << valuestr << "\n";
561 }
562 outs() << "\n";
563 }
564}
565
Nick Lewycky023bb152011-10-10 21:21:34 +0000566static void PrintSectionHeaders(const ObjectFile *o) {
567 outs() << "Sections:\n"
568 "Idx Name Size Address Type\n";
569 error_code ec;
570 unsigned i = 0;
571 for (section_iterator si = o->begin_sections(), se = o->end_sections();
572 si != se; si.increment(ec)) {
573 if (error(ec)) return;
574 StringRef Name;
575 if (error(si->getName(Name))) return;
576 uint64_t Address;
577 if (error(si->getAddress(Address))) return;
578 uint64_t Size;
579 if (error(si->getSize(Size))) return;
580 bool Text, Data, BSS;
581 if (error(si->isText(Text))) return;
582 if (error(si->isData(Data))) return;
583 if (error(si->isBSS(BSS))) return;
584 std::string Type = (std::string(Text ? "TEXT " : "") +
Michael J. Spencer14a5f462011-10-13 20:37:20 +0000585 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
Michael J. Spencer27b2b1b2013-01-10 22:40:50 +0000586 outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n",
Benjamin Kramer51cf8662012-03-10 02:04:38 +0000587 i, Name.str().c_str(), Size, Address, Type.c_str());
Nick Lewycky023bb152011-10-10 21:21:34 +0000588 ++i;
589 }
590}
591
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000592static void PrintSectionContents(const ObjectFile *o) {
593 error_code ec;
594 for (section_iterator si = o->begin_sections(),
595 se = o->end_sections();
596 si != se; si.increment(ec)) {
597 if (error(ec)) return;
598 StringRef Name;
599 StringRef Contents;
600 uint64_t BaseAddr;
Alexey Samsonov0eaa6f62013-04-16 10:53:11 +0000601 bool BSS;
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000602 if (error(si->getName(Name))) continue;
603 if (error(si->getContents(Contents))) continue;
604 if (error(si->getAddress(BaseAddr))) continue;
Alexey Samsonov0eaa6f62013-04-16 10:53:11 +0000605 if (error(si->isBSS(BSS))) continue;
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000606
607 outs() << "Contents of section " << Name << ":\n";
Alexey Samsonov0eaa6f62013-04-16 10:53:11 +0000608 if (BSS) {
609 outs() << format("<skipping contents of bss section at [%04" PRIx64
610 ", %04" PRIx64 ")>\n", BaseAddr,
611 BaseAddr + Contents.size());
612 continue;
613 }
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000614
615 // Dump out the content as hex and printable ascii characters.
616 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
Benjamin Kramer51cf8662012-03-10 02:04:38 +0000617 outs() << format(" %04" PRIx64 " ", BaseAddr + addr);
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000618 // Dump line of hex.
619 for (std::size_t i = 0; i < 16; ++i) {
620 if (i != 0 && i % 4 == 0)
621 outs() << ' ';
622 if (addr + i < end)
623 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
624 << hexdigit(Contents[addr + i] & 0xF, true);
625 else
626 outs() << " ";
627 }
628 // Print ascii.
629 outs() << " ";
630 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
Guy Benyei87d0b9e2013-02-12 21:21:59 +0000631 if (std::isprint(static_cast<unsigned char>(Contents[addr + i]) & 0xFF))
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000632 outs() << Contents[addr + i];
633 else
634 outs() << ".";
635 }
636 outs() << "\n";
637 }
638 }
639}
640
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000641static void PrintCOFFSymbolTable(const COFFObjectFile *coff) {
642 const coff_file_header *header;
643 if (error(coff->getHeader(header))) return;
644 int aux_count = 0;
645 const coff_symbol *symbol = 0;
646 for (int i = 0, e = header->NumberOfSymbols; i != e; ++i) {
647 if (aux_count--) {
648 // Figure out which type of aux this is.
649 if (symbol->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC
650 && symbol->Value == 0) { // Section definition.
651 const coff_aux_section_definition *asd;
652 if (error(coff->getAuxSymbol<coff_aux_section_definition>(i, asd)))
653 return;
654 outs() << "AUX "
655 << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x "
656 , unsigned(asd->Length)
657 , unsigned(asd->NumberOfRelocations)
658 , unsigned(asd->NumberOfLinenumbers)
659 , unsigned(asd->CheckSum))
660 << format("assoc %d comdat %d\n"
661 , unsigned(asd->Number)
662 , unsigned(asd->Selection));
Jim Grosbach3f5d1a22012-08-07 17:53:14 +0000663 } else
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000664 outs() << "AUX Unknown\n";
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000665 } else {
666 StringRef name;
667 if (error(coff->getSymbol(i, symbol))) return;
668 if (error(coff->getSymbolName(symbol, name))) return;
669 outs() << "[" << format("%2d", i) << "]"
670 << "(sec " << format("%2d", int(symbol->SectionNumber)) << ")"
671 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
672 << "(ty " << format("%3x", unsigned(symbol->Type)) << ")"
673 << "(scl " << format("%3x", unsigned(symbol->StorageClass)) << ") "
674 << "(nx " << unsigned(symbol->NumberOfAuxSymbols) << ") "
675 << "0x" << format("%08x", unsigned(symbol->Value)) << " "
676 << name << "\n";
677 aux_count = symbol->NumberOfAuxSymbols;
678 }
679 }
680}
681
682static void PrintSymbolTable(const ObjectFile *o) {
683 outs() << "SYMBOL TABLE:\n";
684
685 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o))
686 PrintCOFFSymbolTable(coff);
687 else {
688 error_code ec;
689 for (symbol_iterator si = o->begin_symbols(),
690 se = o->end_symbols(); si != se; si.increment(ec)) {
691 if (error(ec)) return;
692 StringRef Name;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000693 uint64_t Address;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000694 SymbolRef::Type Type;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000695 uint64_t Size;
David Meyerc46255a2012-02-28 23:47:53 +0000696 uint32_t Flags;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000697 section_iterator Section = o->end_sections();
698 if (error(si->getName(Name))) continue;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000699 if (error(si->getAddress(Address))) continue;
David Meyerc46255a2012-02-28 23:47:53 +0000700 if (error(si->getFlags(Flags))) continue;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000701 if (error(si->getType(Type))) continue;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000702 if (error(si->getSize(Size))) continue;
703 if (error(si->getSection(Section))) continue;
704
David Meyerc46255a2012-02-28 23:47:53 +0000705 bool Global = Flags & SymbolRef::SF_Global;
706 bool Weak = Flags & SymbolRef::SF_Weak;
707 bool Absolute = Flags & SymbolRef::SF_Absolute;
708
Danil Malyshevb0436a72011-11-29 17:40:10 +0000709 if (Address == UnknownAddressOrSize)
710 Address = 0;
711 if (Size == UnknownAddressOrSize)
712 Size = 0;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000713 char GlobLoc = ' ';
David Meyer2c677272012-02-29 02:11:55 +0000714 if (Type != SymbolRef::ST_Unknown)
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000715 GlobLoc = Global ? 'g' : 'l';
716 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
717 ? 'd' : ' ';
718 char FileFunc = ' ';
719 if (Type == SymbolRef::ST_File)
720 FileFunc = 'f';
721 else if (Type == SymbolRef::ST_Function)
722 FileFunc = 'F';
723
Michael J. Spencer27b2b1b2013-01-10 22:40:50 +0000724 const char *Fmt = o->getBytesInAddress() > 4 ? "%016" PRIx64 :
725 "%08" PRIx64;
726
727 outs() << format(Fmt, Address) << " "
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000728 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
729 << (Weak ? 'w' : ' ') // Weak?
730 << ' ' // Constructor. Not supported yet.
731 << ' ' // Warning. Not supported yet.
732 << ' ' // Indirect reference to another symbol.
733 << Debug // Debugging (d) or dynamic (D) symbol.
734 << FileFunc // Name of function (F), file (f) or object (O).
735 << ' ';
736 if (Absolute)
737 outs() << "*ABS*";
738 else if (Section == o->end_sections())
739 outs() << "*UND*";
740 else {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000741 if (const MachOObjectFile *MachO =
742 dyn_cast<const MachOObjectFile>(o)) {
Rafael Espindolacef81b32012-12-21 03:47:03 +0000743 DataRefImpl DR = Section->getRawDataRefImpl();
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000744 StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
Rafael Espindolacef81b32012-12-21 03:47:03 +0000745 outs() << SegmentName << ",";
746 }
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000747 StringRef SectionName;
748 if (error(Section->getName(SectionName)))
749 SectionName = "";
750 outs() << SectionName;
751 }
752 outs() << '\t'
Benjamin Kramer51cf8662012-03-10 02:04:38 +0000753 << format("%08" PRIx64 " ", Size)
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000754 << Name
755 << '\n';
756 }
757 }
758}
759
Michael J. Spencereef7b622012-12-05 20:12:35 +0000760static void PrintUnwindInfo(const ObjectFile *o) {
761 outs() << "Unwind info:\n\n";
762
763 if (const COFFObjectFile *coff = dyn_cast<COFFObjectFile>(o)) {
764 printCOFFUnwindInfo(coff);
765 } else {
766 // TODO: Extract DWARF dump tool to objdump.
767 errs() << "This operation is only currently supported "
768 "for COFF object files.\n";
769 return;
770 }
771}
772
Michael J. Spencer27781b72011-10-08 00:18:30 +0000773static void DumpObject(const ObjectFile *o) {
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000774 outs() << '\n';
775 outs() << o->getFileName()
776 << ":\tfile format " << o->getFileFormatName() << "\n\n";
777
Michael J. Spencer27781b72011-10-08 00:18:30 +0000778 if (Disassemble)
Michael J. Spencer942eb002011-10-13 22:17:18 +0000779 DisassembleObject(o, Relocations);
780 if (Relocations && !Disassemble)
Michael J. Spencer27781b72011-10-08 00:18:30 +0000781 PrintRelocations(o);
Nick Lewycky023bb152011-10-10 21:21:34 +0000782 if (SectionHeaders)
783 PrintSectionHeaders(o);
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000784 if (SectionContents)
785 PrintSectionContents(o);
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000786 if (SymbolTable)
787 PrintSymbolTable(o);
Michael J. Spencereef7b622012-12-05 20:12:35 +0000788 if (UnwindInfo)
789 PrintUnwindInfo(o);
Michael J. Spencerb2c064c2013-01-06 03:56:49 +0000790 if (PrivateHeaders && o->isELF())
791 printELFFileHeader(o);
Michael J. Spencer27781b72011-10-08 00:18:30 +0000792}
793
794/// @brief Dump each object file in \a a;
795static void DumpArchive(const Archive *a) {
796 for (Archive::child_iterator i = a->begin_children(),
797 e = a->end_children(); i != e; ++i) {
798 OwningPtr<Binary> child;
799 if (error_code ec = i->getAsBinary(child)) {
Michael J. Spencerf81285c2011-11-16 01:24:41 +0000800 // Ignore non-object files.
801 if (ec != object_error::invalid_file_type)
802 errs() << ToolName << ": '" << a->getFileName() << "': " << ec.message()
803 << ".\n";
Michael J. Spencer27781b72011-10-08 00:18:30 +0000804 continue;
805 }
806 if (ObjectFile *o = dyn_cast<ObjectFile>(child.get()))
807 DumpObject(o);
808 else
809 errs() << ToolName << ": '" << a->getFileName() << "': "
810 << "Unrecognized file type.\n";
811 }
812}
813
814/// @brief Open file and figure out how to dump it.
815static void DumpInput(StringRef file) {
816 // If file isn't stdin, check that it exists.
817 if (file != "-" && !sys::fs::exists(file)) {
818 errs() << ToolName << ": '" << file << "': " << "No such file\n";
819 return;
820 }
821
Rafael Espindolacef81b32012-12-21 03:47:03 +0000822 if (MachOOpt && Disassemble) {
Michael J. Spencer27781b72011-10-08 00:18:30 +0000823 DisassembleInputMachO(file);
824 return;
825 }
826
827 // Attempt to open the binary.
828 OwningPtr<Binary> binary;
829 if (error_code ec = createBinary(file, binary)) {
830 errs() << ToolName << ": '" << file << "': " << ec.message() << ".\n";
831 return;
832 }
833
Jim Grosbach3f5d1a22012-08-07 17:53:14 +0000834 if (Archive *a = dyn_cast<Archive>(binary.get()))
Michael J. Spencer27781b72011-10-08 00:18:30 +0000835 DumpArchive(a);
Jim Grosbach3f5d1a22012-08-07 17:53:14 +0000836 else if (ObjectFile *o = dyn_cast<ObjectFile>(binary.get()))
Michael J. Spencer27781b72011-10-08 00:18:30 +0000837 DumpObject(o);
Jim Grosbach3f5d1a22012-08-07 17:53:14 +0000838 else
Michael J. Spencer27781b72011-10-08 00:18:30 +0000839 errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n";
Michael J. Spencer27781b72011-10-08 00:18:30 +0000840}
841
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000842int main(int argc, char **argv) {
843 // Print a stack trace if we signal out.
844 sys::PrintStackTraceOnErrorSignal();
845 PrettyStackTraceProgram X(argc, argv);
846 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
847
848 // Initialize targets and assembly printers/parsers.
849 llvm::InitializeAllTargetInfos();
Evan Chenge78085a2011-07-22 21:58:54 +0000850 llvm::InitializeAllTargetMCs();
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000851 llvm::InitializeAllAsmParsers();
852 llvm::InitializeAllDisassemblers();
853
Pete Cooperff204962012-05-03 23:20:10 +0000854 // Register the target printer for --version.
855 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
856
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000857 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
858 TripleName = Triple::normalize(TripleName);
859
860 ToolName = argv[0];
861
862 // Defaults to a.out if no filenames specified.
863 if (InputFilenames.size() == 0)
864 InputFilenames.push_back("a.out");
865
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000866 if (!Disassemble
867 && !Relocations
868 && !SectionHeaders
869 && !SectionContents
Michael J. Spencereef7b622012-12-05 20:12:35 +0000870 && !SymbolTable
Michael J. Spencerb2c064c2013-01-06 03:56:49 +0000871 && !UnwindInfo
872 && !PrivateHeaders) {
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000873 cl::PrintHelpMessage();
874 return 2;
875 }
876
Michael J. Spencer27781b72011-10-08 00:18:30 +0000877 std::for_each(InputFilenames.begin(), InputFilenames.end(),
878 DumpInput);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000879
880 return 0;
881}