blob: 34f27e8a63a1a1affd6932a3b9313959474ceff0 [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//
14//===----------------------------------------------------------------------===//
15
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000016#include "llvm-objdump.h"
Benjamin Kramer685a2502011-07-20 19:37:35 +000017#include "MCFunction.h"
Michael J. Spencer27781b72011-10-08 00:18:30 +000018#include "llvm/Object/Archive.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000019#include "llvm/Object/ObjectFile.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000020#include "llvm/ADT/OwningPtr.h"
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +000021#include "llvm/ADT/StringExtras.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000022#include "llvm/ADT/Triple.h"
Benjamin Kramer739b65b2011-07-15 18:39:24 +000023#include "llvm/ADT/STLExtras.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000024#include "llvm/MC/MCAsmInfo.h"
25#include "llvm/MC/MCDisassembler.h"
26#include "llvm/MC/MCInst.h"
27#include "llvm/MC/MCInstPrinter.h"
James Molloyb9505852011-09-07 17:24:38 +000028#include "llvm/MC/MCSubtargetInfo.h"
Michael J. Spencer27781b72011-10-08 00:18:30 +000029#include "llvm/Support/Casting.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000030#include "llvm/Support/CommandLine.h"
31#include "llvm/Support/Debug.h"
Michael J. Spencer27781b72011-10-08 00:18:30 +000032#include "llvm/Support/FileSystem.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000033#include "llvm/Support/Format.h"
Benjamin Kramer853b0fd2011-07-25 23:04:36 +000034#include "llvm/Support/GraphWriter.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000035#include "llvm/Support/Host.h"
36#include "llvm/Support/ManagedStatic.h"
37#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000038#include "llvm/Support/MemoryObject.h"
39#include "llvm/Support/PrettyStackTrace.h"
40#include "llvm/Support/Signals.h"
41#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000042#include "llvm/Support/TargetRegistry.h"
43#include "llvm/Support/TargetSelect.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000044#include "llvm/Support/raw_ostream.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000045#include "llvm/Support/system_error.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000046#include <algorithm>
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000047#include <cstring>
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000048using namespace llvm;
49using namespace object;
50
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000051static cl::list<std::string>
52InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000053
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000054static cl::opt<bool>
55Disassemble("disassemble",
56 cl::desc("Display assembler mnemonics for the machine instructions"));
57static cl::alias
58Disassembled("d", cl::desc("Alias for --disassemble"),
59 cl::aliasopt(Disassemble));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000060
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000061static cl::opt<bool>
Michael J. Spencer27781b72011-10-08 00:18:30 +000062Relocations("r", cl::desc("Display the relocation entries in the file"));
63
64static cl::opt<bool>
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +000065SectionContents("s", cl::desc("Display the content of each section"));
66
67static cl::opt<bool>
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000068MachO("macho", cl::desc("Use MachO specific object file parser"));
69static cl::alias
70MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachO));
Benjamin Kramer685a2502011-07-20 19:37:35 +000071
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000072cl::opt<std::string>
73llvm::TripleName("triple", cl::desc("Target triple to disassemble for, "
74 "see -version for available targets"));
75
76cl::opt<std::string>
77llvm::ArchName("arch", cl::desc("Target arch to disassemble for, "
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000078 "see -version for available targets"));
79
Nick Lewycky023bb152011-10-10 21:21:34 +000080static cl::opt<bool>
81SectionHeaders("section-headers", cl::desc("Display summaries of the headers "
82 "for each section."));
83static cl::alias
84SectionHeadersShort("headers", cl::desc("Alias for --section-headers"),
85 cl::aliasopt(SectionHeaders));
86static cl::alias
87SectionHeadersShorter("h", cl::desc("Alias for --section-headers"),
88 cl::aliasopt(SectionHeaders));
89
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000090static StringRef ToolName;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000091
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000092static bool error(error_code ec) {
93 if (!ec) return false;
Michael J. Spencer25b15772011-06-25 17:55:23 +000094
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000095 outs() << ToolName << ": error reading file: " << ec.message() << ".\n";
96 outs().flush();
97 return true;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000098}
99
100static const Target *GetTarget(const ObjectFile *Obj = NULL) {
101 // Figure out the target triple.
102 llvm::Triple TT("unknown-unknown-unknown");
Michael J. Spencerd11699d2011-01-20 07:22:04 +0000103 if (TripleName.empty()) {
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000104 if (Obj)
105 TT.setArch(Triple::ArchType(Obj->getArch()));
Michael J. Spencerd11699d2011-01-20 07:22:04 +0000106 } else
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000107 TT.setTriple(Triple::normalize(TripleName));
108
109 if (!ArchName.empty())
110 TT.setArchName(ArchName);
111
112 TripleName = TT.str();
113
114 // Get the target specific parser.
115 std::string Error;
116 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
117 if (TheTarget)
118 return TheTarget;
119
120 errs() << ToolName << ": error: unable to get target for '" << TripleName
121 << "', see --version and --triple.\n";
122 return 0;
123}
124
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000125void llvm::DumpBytes(StringRef bytes) {
126 static const char hex_rep[] = "0123456789abcdef";
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000127 // FIXME: The real way to do this is to figure out the longest instruction
128 // and align to that size before printing. I'll fix this when I get
129 // around to outputting relocations.
130 // 15 is the longest x86 instruction
131 // 3 is for the hex rep of a byte + a space.
132 // 1 is for the null terminator.
133 enum { OutputSize = (15 * 3) + 1 };
134 char output[OutputSize];
135
136 assert(bytes.size() <= 15
137 && "DumpBytes only supports instructions of up to 15 bytes");
138 memset(output, ' ', sizeof(output));
139 unsigned index = 0;
140 for (StringRef::iterator i = bytes.begin(),
141 e = bytes.end(); i != e; ++i) {
142 output[index] = hex_rep[(*i & 0xF0) >> 4];
143 output[index + 1] = hex_rep[*i & 0xF];
144 index += 3;
145 }
146
147 output[sizeof(output) - 1] = 0;
148 outs() << output;
149}
150
Michael J. Spencer942eb002011-10-13 22:17:18 +0000151static bool RelocAddressLess(RelocationRef a, RelocationRef b) {
152 uint64_t a_addr, b_addr;
153 if (error(a.getAddress(a_addr))) return false;
154 if (error(b.getAddress(b_addr))) return false;
155 return a_addr < b_addr;
156}
157
158static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
Michael J. Spencer27781b72011-10-08 00:18:30 +0000159 const Target *TheTarget = GetTarget(Obj);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000160 if (!TheTarget) {
161 // GetTarget prints out stuff.
162 return;
163 }
164
Michael J. Spencer25b15772011-06-25 17:55:23 +0000165 error_code ec;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000166 for (section_iterator i = Obj->begin_sections(),
Michael J. Spencer27781b72011-10-08 00:18:30 +0000167 e = Obj->end_sections();
168 i != e; i.increment(ec)) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000169 if (error(ec)) break;
170 bool text;
171 if (error(i->isText(text))) break;
172 if (!text) continue;
173
Michael J. Spencer942eb002011-10-13 22:17:18 +0000174 uint64_t SectionAddr;
175 if (error(i->getAddress(SectionAddr))) break;
176
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000177 // Make a list of all the symbols in this section.
178 std::vector<std::pair<uint64_t, StringRef> > Symbols;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000179 for (symbol_iterator si = Obj->begin_symbols(),
Michael J. Spencer27781b72011-10-08 00:18:30 +0000180 se = Obj->end_symbols();
181 si != se; si.increment(ec)) {
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000182 bool contains;
183 if (!error(i->containsSymbol(*si, contains)) && contains) {
184 uint64_t Address;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000185 if (error(si->getOffset(Address))) break;
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000186 StringRef Name;
187 if (error(si->getName(Name))) break;
188 Symbols.push_back(std::make_pair(Address, Name));
189 }
190 }
191
192 // Sort the symbols by address, just in case they didn't come in that way.
193 array_pod_sort(Symbols.begin(), Symbols.end());
194
Michael J. Spencer942eb002011-10-13 22:17:18 +0000195 // Make a list of all the relocations for this section.
196 std::vector<RelocationRef> Rels;
197 if (InlineRelocs) {
198 for (relocation_iterator ri = i->begin_relocations(),
199 re = i->end_relocations();
200 ri != re; ri.increment(ec)) {
201 if (error(ec)) break;
202 Rels.push_back(*ri);
203 }
204 }
205
206 // Sort relocations by address.
207 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
208
Michael J. Spencer25b15772011-06-25 17:55:23 +0000209 StringRef name;
210 if (error(i->getName(name))) break;
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000211 outs() << "Disassembly of section " << name << ':';
212
213 // If the section has no symbols just insert a dummy one and disassemble
214 // the whole section.
215 if (Symbols.empty())
216 Symbols.push_back(std::make_pair(0, name));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000217
218 // Set up disassembler.
Evan Cheng1abf2cb2011-07-14 23:50:31 +0000219 OwningPtr<const MCAsmInfo> AsmInfo(TheTarget->createMCAsmInfo(TripleName));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000220
221 if (!AsmInfo) {
222 errs() << "error: no assembly info for target " << TripleName << "\n";
223 return;
224 }
225
Michael J. Spencer27781b72011-10-08 00:18:30 +0000226 OwningPtr<const MCSubtargetInfo> STI(
227 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
James Molloyb9505852011-09-07 17:24:38 +0000228
229 if (!STI) {
230 errs() << "error: no subtarget info for target " << TripleName << "\n";
231 return;
232 }
233
Michael J. Spencer27781b72011-10-08 00:18:30 +0000234 OwningPtr<const MCDisassembler> DisAsm(
235 TheTarget->createMCDisassembler(*STI));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000236 if (!DisAsm) {
237 errs() << "error: no disassembler for target " << TripleName << "\n";
238 return;
239 }
240
241 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
242 OwningPtr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
James Molloyb9505852011-09-07 17:24:38 +0000243 AsmPrinterVariant, *AsmInfo, *STI));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000244 if (!IP) {
Michael J. Spencer27781b72011-10-08 00:18:30 +0000245 errs() << "error: no instruction printer for target " << TripleName
246 << '\n';
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000247 return;
248 }
249
Michael J. Spencer25b15772011-06-25 17:55:23 +0000250 StringRef Bytes;
251 if (error(i->getContents(Bytes))) break;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000252 StringRefMemoryObject memoryObject(Bytes);
253 uint64_t Size;
254 uint64_t Index;
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000255 uint64_t SectSize;
256 if (error(i->getSize(SectSize))) break;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000257
Michael J. Spencer942eb002011-10-13 22:17:18 +0000258 std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
259 std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000260 // Disassemble symbol by symbol.
261 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
262 uint64_t Start = Symbols[si].first;
Michael J. Spencer178dbd42011-10-13 20:37:08 +0000263 uint64_t End;
264 // The end is either the size of the section or the beginning of the next
265 // symbol.
266 if (si == se - 1)
267 End = SectSize;
268 // Make sure this symbol takes up space.
269 else if (Symbols[si + 1].first != Start)
270 End = Symbols[si + 1].first - 1;
271 else
272 // This symbol has the same address as the next symbol. Skip it.
273 continue;
274
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000275 outs() << '\n' << Symbols[si].second << ":\n";
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000276
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000277#ifndef NDEBUG
278 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
279#else
280 raw_ostream &DebugOut = nulls();
281#endif
282
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000283 for (Index = Start; Index < End; Index += Size) {
284 MCInst Inst;
Owen Anderson98c5dda2011-09-15 23:38:46 +0000285
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000286 if (DisAsm->getInstruction(Inst, Size, memoryObject, Index,
287 DebugOut, nulls())) {
Michael J. Spencer942eb002011-10-13 22:17:18 +0000288 outs() << format("%8x:\t", SectionAddr + Index);
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000289 DumpBytes(StringRef(Bytes.data() + Index, Size));
290 IP->printInst(&Inst, outs(), "");
291 outs() << "\n";
292 } else {
293 errs() << ToolName << ": warning: invalid instruction encoding\n";
294 if (Size == 0)
295 Size = 1; // skip illegible bytes
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000296 }
Michael J. Spencer942eb002011-10-13 22:17:18 +0000297
298 // Print relocation for instruction.
299 while (rel_cur != rel_end) {
300 uint64_t addr;
301 SmallString<16> name;
302 SmallString<32> val;
303 if (error(rel_cur->getAddress(addr))) goto skip_print_rel;
304 // Stop when rel_cur's address is past the current instruction.
305 if (addr > Index + Size) break;
306 if (error(rel_cur->getTypeName(name))) goto skip_print_rel;
307 if (error(rel_cur->getValueString(val))) goto skip_print_rel;
308
309 outs() << format("\t\t\t%8x: ", SectionAddr + addr) << name << "\t"
310 << val << "\n";
311
312 skip_print_rel:
313 ++rel_cur;
314 }
Benjamin Kramer685a2502011-07-20 19:37:35 +0000315 }
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000316 }
317 }
318}
319
Michael J. Spencer27781b72011-10-08 00:18:30 +0000320static void PrintRelocations(const ObjectFile *o) {
321 error_code ec;
322 for (section_iterator si = o->begin_sections(), se = o->end_sections();
323 si != se; si.increment(ec)){
324 if (error(ec)) return;
325 if (si->begin_relocations() == si->end_relocations())
326 continue;
327 StringRef secname;
328 if (error(si->getName(secname))) continue;
329 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
330 for (relocation_iterator ri = si->begin_relocations(),
331 re = si->end_relocations();
332 ri != re; ri.increment(ec)) {
333 if (error(ec)) return;
334
335 uint64_t address;
336 SmallString<32> relocname;
337 SmallString<32> valuestr;
338 if (error(ri->getTypeName(relocname))) continue;
339 if (error(ri->getAddress(address))) continue;
340 if (error(ri->getValueString(valuestr))) continue;
341 outs() << address << " " << relocname << " " << valuestr << "\n";
342 }
343 outs() << "\n";
344 }
345}
346
Nick Lewycky023bb152011-10-10 21:21:34 +0000347static void PrintSectionHeaders(const ObjectFile *o) {
348 outs() << "Sections:\n"
349 "Idx Name Size Address Type\n";
350 error_code ec;
351 unsigned i = 0;
352 for (section_iterator si = o->begin_sections(), se = o->end_sections();
353 si != se; si.increment(ec)) {
354 if (error(ec)) return;
355 StringRef Name;
356 if (error(si->getName(Name))) return;
357 uint64_t Address;
358 if (error(si->getAddress(Address))) return;
359 uint64_t Size;
360 if (error(si->getSize(Size))) return;
361 bool Text, Data, BSS;
362 if (error(si->isText(Text))) return;
363 if (error(si->isData(Data))) return;
364 if (error(si->isBSS(BSS))) return;
365 std::string Type = (std::string(Text ? "TEXT " : "") +
Michael J. Spencer14a5f462011-10-13 20:37:20 +0000366 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
NAKAMURA Takumif048c3f2011-10-11 12:51:50 +0000367 outs() << format("%3d %-13s %09"PRIx64" %017"PRIx64" %s\n", i, Name.str().c_str(), Size,
Nick Lewycky023bb152011-10-10 21:21:34 +0000368 Address, Type.c_str());
369 ++i;
370 }
371}
372
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000373static void PrintSectionContents(const ObjectFile *o) {
374 error_code ec;
375 for (section_iterator si = o->begin_sections(),
376 se = o->end_sections();
377 si != se; si.increment(ec)) {
378 if (error(ec)) return;
379 StringRef Name;
380 StringRef Contents;
381 uint64_t BaseAddr;
382 if (error(si->getName(Name))) continue;
383 if (error(si->getContents(Contents))) continue;
384 if (error(si->getAddress(BaseAddr))) continue;
385
386 outs() << "Contents of section " << Name << ":\n";
387
388 // Dump out the content as hex and printable ascii characters.
389 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
390 outs() << format(" %04x ", BaseAddr + addr);
391 // Dump line of hex.
392 for (std::size_t i = 0; i < 16; ++i) {
393 if (i != 0 && i % 4 == 0)
394 outs() << ' ';
395 if (addr + i < end)
396 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
397 << hexdigit(Contents[addr + i] & 0xF, true);
398 else
399 outs() << " ";
400 }
401 // Print ascii.
402 outs() << " ";
403 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
404 if (std::isprint(Contents[addr + i] & 0xFF))
405 outs() << Contents[addr + i];
406 else
407 outs() << ".";
408 }
409 outs() << "\n";
410 }
411 }
412}
413
Michael J. Spencer27781b72011-10-08 00:18:30 +0000414static void DumpObject(const ObjectFile *o) {
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000415 outs() << '\n';
416 outs() << o->getFileName()
417 << ":\tfile format " << o->getFileFormatName() << "\n\n";
418
Michael J. Spencer27781b72011-10-08 00:18:30 +0000419 if (Disassemble)
Michael J. Spencer942eb002011-10-13 22:17:18 +0000420 DisassembleObject(o, Relocations);
421 if (Relocations && !Disassemble)
Michael J. Spencer27781b72011-10-08 00:18:30 +0000422 PrintRelocations(o);
Nick Lewycky023bb152011-10-10 21:21:34 +0000423 if (SectionHeaders)
424 PrintSectionHeaders(o);
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000425 if (SectionContents)
426 PrintSectionContents(o);
Michael J. Spencer27781b72011-10-08 00:18:30 +0000427}
428
429/// @brief Dump each object file in \a a;
430static void DumpArchive(const Archive *a) {
431 for (Archive::child_iterator i = a->begin_children(),
432 e = a->end_children(); i != e; ++i) {
433 OwningPtr<Binary> child;
434 if (error_code ec = i->getAsBinary(child)) {
435 errs() << ToolName << ": '" << a->getFileName() << "': " << ec.message()
436 << ".\n";
437 continue;
438 }
439 if (ObjectFile *o = dyn_cast<ObjectFile>(child.get()))
440 DumpObject(o);
441 else
442 errs() << ToolName << ": '" << a->getFileName() << "': "
443 << "Unrecognized file type.\n";
444 }
445}
446
447/// @brief Open file and figure out how to dump it.
448static void DumpInput(StringRef file) {
449 // If file isn't stdin, check that it exists.
450 if (file != "-" && !sys::fs::exists(file)) {
451 errs() << ToolName << ": '" << file << "': " << "No such file\n";
452 return;
453 }
454
455 if (MachO && Disassemble) {
456 DisassembleInputMachO(file);
457 return;
458 }
459
460 // Attempt to open the binary.
461 OwningPtr<Binary> binary;
462 if (error_code ec = createBinary(file, binary)) {
463 errs() << ToolName << ": '" << file << "': " << ec.message() << ".\n";
464 return;
465 }
466
467 if (Archive *a = dyn_cast<Archive>(binary.get())) {
468 DumpArchive(a);
469 } else if (ObjectFile *o = dyn_cast<ObjectFile>(binary.get())) {
470 DumpObject(o);
471 } else {
472 errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n";
473 }
474}
475
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000476int main(int argc, char **argv) {
477 // Print a stack trace if we signal out.
478 sys::PrintStackTraceOnErrorSignal();
479 PrettyStackTraceProgram X(argc, argv);
480 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
481
482 // Initialize targets and assembly printers/parsers.
483 llvm::InitializeAllTargetInfos();
Evan Chenge78085a2011-07-22 21:58:54 +0000484 llvm::InitializeAllTargetMCs();
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000485 llvm::InitializeAllAsmParsers();
486 llvm::InitializeAllDisassemblers();
487
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000488 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
489 TripleName = Triple::normalize(TripleName);
490
491 ToolName = argv[0];
492
493 // Defaults to a.out if no filenames specified.
494 if (InputFilenames.size() == 0)
495 InputFilenames.push_back("a.out");
496
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000497 if (!Disassemble && !Relocations && !SectionHeaders && !SectionContents) {
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000498 cl::PrintHelpMessage();
499 return 2;
500 }
501
Michael J. Spencer27781b72011-10-08 00:18:30 +0000502 std::for_each(InputFilenames.begin(), InputFilenames.end(),
503 DumpInput);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000504
505 return 0;
506}