blob: fded8a1f35600aea6f8dea60aafb5e4505bb8429 [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. Spencer22ff0f32011-10-18 19:32:17 +000019#include "llvm/Object/COFF.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000020#include "llvm/Object/ObjectFile.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000021#include "llvm/ADT/OwningPtr.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"
Benjamin Kramer739b65b2011-07-15 18:39:24 +000024#include "llvm/ADT/STLExtras.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000025#include "llvm/MC/MCAsmInfo.h"
26#include "llvm/MC/MCDisassembler.h"
27#include "llvm/MC/MCInst.h"
28#include "llvm/MC/MCInstPrinter.h"
James Molloyb9505852011-09-07 17:24:38 +000029#include "llvm/MC/MCSubtargetInfo.h"
Michael J. Spencer27781b72011-10-08 00:18:30 +000030#include "llvm/Support/Casting.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000031#include "llvm/Support/CommandLine.h"
32#include "llvm/Support/Debug.h"
Michael J. Spencer27781b72011-10-08 00:18:30 +000033#include "llvm/Support/FileSystem.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000034#include "llvm/Support/Format.h"
Benjamin Kramer853b0fd2011-07-25 23:04:36 +000035#include "llvm/Support/GraphWriter.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000036#include "llvm/Support/Host.h"
37#include "llvm/Support/ManagedStatic.h"
38#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000039#include "llvm/Support/MemoryObject.h"
40#include "llvm/Support/PrettyStackTrace.h"
41#include "llvm/Support/Signals.h"
42#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000043#include "llvm/Support/TargetRegistry.h"
44#include "llvm/Support/TargetSelect.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000045#include "llvm/Support/raw_ostream.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000046#include "llvm/Support/system_error.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000047#include <algorithm>
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000048#include <cstring>
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000049using namespace llvm;
50using namespace object;
51
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000052static cl::list<std::string>
53InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000054
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000055static cl::opt<bool>
56Disassemble("disassemble",
57 cl::desc("Display assembler mnemonics for the machine instructions"));
58static cl::alias
59Disassembled("d", cl::desc("Alias for --disassemble"),
60 cl::aliasopt(Disassemble));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000061
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000062static cl::opt<bool>
Michael J. Spencer27781b72011-10-08 00:18:30 +000063Relocations("r", cl::desc("Display the relocation entries in the file"));
64
65static cl::opt<bool>
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +000066SectionContents("s", cl::desc("Display the content of each section"));
67
68static cl::opt<bool>
Michael J. Spencer22ff0f32011-10-18 19:32:17 +000069SymbolTable("t", cl::desc("Display the symbol table"));
70
71static cl::opt<bool>
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000072MachO("macho", cl::desc("Use MachO specific object file parser"));
73static cl::alias
74MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachO));
Benjamin Kramer685a2502011-07-20 19:37:35 +000075
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000076cl::opt<std::string>
77llvm::TripleName("triple", cl::desc("Target triple to disassemble for, "
78 "see -version for available targets"));
79
80cl::opt<std::string>
81llvm::ArchName("arch", cl::desc("Target arch to disassemble for, "
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000082 "see -version for available targets"));
83
Nick Lewycky023bb152011-10-10 21:21:34 +000084static cl::opt<bool>
85SectionHeaders("section-headers", cl::desc("Display summaries of the headers "
86 "for each section."));
87static cl::alias
88SectionHeadersShort("headers", cl::desc("Alias for --section-headers"),
89 cl::aliasopt(SectionHeaders));
90static cl::alias
91SectionHeadersShorter("h", cl::desc("Alias for --section-headers"),
92 cl::aliasopt(SectionHeaders));
93
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000094static StringRef ToolName;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000095
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000096static bool error(error_code ec) {
97 if (!ec) return false;
Michael J. Spencer25b15772011-06-25 17:55:23 +000098
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000099 outs() << ToolName << ": error reading file: " << ec.message() << ".\n";
100 outs().flush();
101 return true;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000102}
103
104static const Target *GetTarget(const ObjectFile *Obj = NULL) {
105 // Figure out the target triple.
106 llvm::Triple TT("unknown-unknown-unknown");
Michael J. Spencerd11699d2011-01-20 07:22:04 +0000107 if (TripleName.empty()) {
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000108 if (Obj)
109 TT.setArch(Triple::ArchType(Obj->getArch()));
Michael J. Spencerd11699d2011-01-20 07:22:04 +0000110 } else
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000111 TT.setTriple(Triple::normalize(TripleName));
112
113 if (!ArchName.empty())
114 TT.setArchName(ArchName);
115
116 TripleName = TT.str();
117
118 // Get the target specific parser.
119 std::string Error;
120 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
121 if (TheTarget)
122 return TheTarget;
123
124 errs() << ToolName << ": error: unable to get target for '" << TripleName
125 << "', see --version and --triple.\n";
126 return 0;
127}
128
David Blaikie2d24e2a2011-12-20 02:50:00 +0000129void llvm::StringRefMemoryObject::anchor() { }
130
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000131void llvm::DumpBytes(StringRef bytes) {
132 static const char hex_rep[] = "0123456789abcdef";
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000133 // FIXME: The real way to do this is to figure out the longest instruction
134 // and align to that size before printing. I'll fix this when I get
135 // around to outputting relocations.
136 // 15 is the longest x86 instruction
137 // 3 is for the hex rep of a byte + a space.
138 // 1 is for the null terminator.
139 enum { OutputSize = (15 * 3) + 1 };
140 char output[OutputSize];
141
142 assert(bytes.size() <= 15
143 && "DumpBytes only supports instructions of up to 15 bytes");
144 memset(output, ' ', sizeof(output));
145 unsigned index = 0;
146 for (StringRef::iterator i = bytes.begin(),
147 e = bytes.end(); i != e; ++i) {
148 output[index] = hex_rep[(*i & 0xF0) >> 4];
149 output[index + 1] = hex_rep[*i & 0xF];
150 index += 3;
151 }
152
153 output[sizeof(output) - 1] = 0;
154 outs() << output;
155}
156
Michael J. Spencer942eb002011-10-13 22:17:18 +0000157static bool RelocAddressLess(RelocationRef a, RelocationRef b) {
158 uint64_t a_addr, b_addr;
159 if (error(a.getAddress(a_addr))) return false;
160 if (error(b.getAddress(b_addr))) return false;
161 return a_addr < b_addr;
162}
163
164static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
Michael J. Spencer27781b72011-10-08 00:18:30 +0000165 const Target *TheTarget = GetTarget(Obj);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000166 if (!TheTarget) {
167 // GetTarget prints out stuff.
168 return;
169 }
170
Michael J. Spencer25b15772011-06-25 17:55:23 +0000171 error_code ec;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000172 for (section_iterator i = Obj->begin_sections(),
Michael J. Spencer27781b72011-10-08 00:18:30 +0000173 e = Obj->end_sections();
174 i != e; i.increment(ec)) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000175 if (error(ec)) break;
176 bool text;
177 if (error(i->isText(text))) break;
178 if (!text) continue;
179
Michael J. Spencer942eb002011-10-13 22:17:18 +0000180 uint64_t SectionAddr;
181 if (error(i->getAddress(SectionAddr))) break;
182
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000183 // Make a list of all the symbols in this section.
184 std::vector<std::pair<uint64_t, StringRef> > Symbols;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000185 for (symbol_iterator si = Obj->begin_symbols(),
Michael J. Spencer27781b72011-10-08 00:18:30 +0000186 se = Obj->end_symbols();
187 si != se; si.increment(ec)) {
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000188 bool contains;
189 if (!error(i->containsSymbol(*si, contains)) && contains) {
190 uint64_t Address;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000191 if (error(si->getAddress(Address))) break;
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000192 StringRef Name;
193 if (error(si->getName(Name))) break;
194 Symbols.push_back(std::make_pair(Address, Name));
195 }
196 }
197
198 // Sort the symbols by address, just in case they didn't come in that way.
199 array_pod_sort(Symbols.begin(), Symbols.end());
200
Michael J. Spencer942eb002011-10-13 22:17:18 +0000201 // Make a list of all the relocations for this section.
202 std::vector<RelocationRef> Rels;
203 if (InlineRelocs) {
204 for (relocation_iterator ri = i->begin_relocations(),
205 re = i->end_relocations();
206 ri != re; ri.increment(ec)) {
207 if (error(ec)) break;
208 Rels.push_back(*ri);
209 }
210 }
211
212 // Sort relocations by address.
213 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
214
Michael J. Spencer25b15772011-06-25 17:55:23 +0000215 StringRef name;
216 if (error(i->getName(name))) break;
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000217 outs() << "Disassembly of section " << name << ':';
218
219 // If the section has no symbols just insert a dummy one and disassemble
220 // the whole section.
221 if (Symbols.empty())
222 Symbols.push_back(std::make_pair(0, name));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000223
224 // Set up disassembler.
Evan Cheng1abf2cb2011-07-14 23:50:31 +0000225 OwningPtr<const MCAsmInfo> AsmInfo(TheTarget->createMCAsmInfo(TripleName));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000226
227 if (!AsmInfo) {
228 errs() << "error: no assembly info for target " << TripleName << "\n";
229 return;
230 }
231
Michael J. Spencer27781b72011-10-08 00:18:30 +0000232 OwningPtr<const MCSubtargetInfo> STI(
233 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
James Molloyb9505852011-09-07 17:24:38 +0000234
235 if (!STI) {
236 errs() << "error: no subtarget info for target " << TripleName << "\n";
237 return;
238 }
239
Owen Anderson10c044e2011-10-27 21:55:13 +0000240 OwningPtr<const MCDisassembler> DisAsm(
Michael J. Spencer27781b72011-10-08 00:18:30 +0000241 TheTarget->createMCDisassembler(*STI));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000242 if (!DisAsm) {
243 errs() << "error: no disassembler for target " << TripleName << "\n";
244 return;
245 }
246
247 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
248 OwningPtr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
James Molloyb9505852011-09-07 17:24:38 +0000249 AsmPrinterVariant, *AsmInfo, *STI));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000250 if (!IP) {
Michael J. Spencer27781b72011-10-08 00:18:30 +0000251 errs() << "error: no instruction printer for target " << TripleName
252 << '\n';
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000253 return;
254 }
255
Michael J. Spencer25b15772011-06-25 17:55:23 +0000256 StringRef Bytes;
257 if (error(i->getContents(Bytes))) break;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000258 StringRefMemoryObject memoryObject(Bytes);
259 uint64_t Size;
260 uint64_t Index;
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000261 uint64_t SectSize;
262 if (error(i->getSize(SectSize))) break;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000263
Michael J. Spencer942eb002011-10-13 22:17:18 +0000264 std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
265 std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000266 // Disassemble symbol by symbol.
267 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
268 uint64_t Start = Symbols[si].first;
Michael J. Spencer178dbd42011-10-13 20:37:08 +0000269 uint64_t End;
270 // The end is either the size of the section or the beginning of the next
271 // symbol.
272 if (si == se - 1)
273 End = SectSize;
274 // Make sure this symbol takes up space.
275 else if (Symbols[si + 1].first != Start)
276 End = Symbols[si + 1].first - 1;
277 else
278 // This symbol has the same address as the next symbol. Skip it.
279 continue;
280
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000281 outs() << '\n' << Symbols[si].second << ":\n";
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000282
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000283#ifndef NDEBUG
284 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
285#else
286 raw_ostream &DebugOut = nulls();
287#endif
288
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000289 for (Index = Start; Index < End; Index += Size) {
290 MCInst Inst;
Owen Anderson98c5dda2011-09-15 23:38:46 +0000291
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000292 if (DisAsm->getInstruction(Inst, Size, memoryObject, Index,
293 DebugOut, nulls())) {
Stepan Dyatkovskiy9df3b912011-10-28 13:07:32 +0000294 outs() << format("%8"PRIx64":\t", SectionAddr + Index);
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000295 DumpBytes(StringRef(Bytes.data() + Index, Size));
296 IP->printInst(&Inst, outs(), "");
297 outs() << "\n";
298 } else {
299 errs() << ToolName << ": warning: invalid instruction encoding\n";
300 if (Size == 0)
301 Size = 1; // skip illegible bytes
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000302 }
Michael J. Spencer942eb002011-10-13 22:17:18 +0000303
304 // Print relocation for instruction.
305 while (rel_cur != rel_end) {
Owen Anderson0685e942011-10-25 20:35:53 +0000306 bool hidden = false;
Michael J. Spencer942eb002011-10-13 22:17:18 +0000307 uint64_t addr;
308 SmallString<16> name;
309 SmallString<32> val;
Owen Anderson0685e942011-10-25 20:35:53 +0000310
311 // If this relocation is hidden, skip it.
312 if (error(rel_cur->getHidden(hidden))) goto skip_print_rel;
313 if (hidden) goto skip_print_rel;
314
Michael J. Spencer942eb002011-10-13 22:17:18 +0000315 if (error(rel_cur->getAddress(addr))) goto skip_print_rel;
316 // Stop when rel_cur's address is past the current instruction.
Owen Anderson34749ce2011-10-25 20:15:39 +0000317 if (addr >= Index + Size) break;
Michael J. Spencer942eb002011-10-13 22:17:18 +0000318 if (error(rel_cur->getTypeName(name))) goto skip_print_rel;
319 if (error(rel_cur->getValueString(val))) goto skip_print_rel;
320
Stepan Dyatkovskiy9df3b912011-10-28 13:07:32 +0000321 outs() << format("\t\t\t%8"PRIx64": ", SectionAddr + addr) << name << "\t"
Michael J. Spencer942eb002011-10-13 22:17:18 +0000322 << val << "\n";
323
324 skip_print_rel:
325 ++rel_cur;
326 }
Benjamin Kramer685a2502011-07-20 19:37:35 +0000327 }
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000328 }
329 }
330}
331
Michael J. Spencer27781b72011-10-08 00:18:30 +0000332static void PrintRelocations(const ObjectFile *o) {
333 error_code ec;
334 for (section_iterator si = o->begin_sections(), se = o->end_sections();
335 si != se; si.increment(ec)){
336 if (error(ec)) return;
337 if (si->begin_relocations() == si->end_relocations())
338 continue;
339 StringRef secname;
340 if (error(si->getName(secname))) continue;
341 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
342 for (relocation_iterator ri = si->begin_relocations(),
343 re = si->end_relocations();
344 ri != re; ri.increment(ec)) {
345 if (error(ec)) return;
346
Owen Anderson0685e942011-10-25 20:35:53 +0000347 bool hidden;
Michael J. Spencer27781b72011-10-08 00:18:30 +0000348 uint64_t address;
349 SmallString<32> relocname;
350 SmallString<32> valuestr;
Owen Anderson0685e942011-10-25 20:35:53 +0000351 if (error(ri->getHidden(hidden))) continue;
352 if (hidden) continue;
Michael J. Spencer27781b72011-10-08 00:18:30 +0000353 if (error(ri->getTypeName(relocname))) continue;
354 if (error(ri->getAddress(address))) continue;
355 if (error(ri->getValueString(valuestr))) continue;
356 outs() << address << " " << relocname << " " << valuestr << "\n";
357 }
358 outs() << "\n";
359 }
360}
361
Nick Lewycky023bb152011-10-10 21:21:34 +0000362static void PrintSectionHeaders(const ObjectFile *o) {
363 outs() << "Sections:\n"
364 "Idx Name Size Address Type\n";
365 error_code ec;
366 unsigned i = 0;
367 for (section_iterator si = o->begin_sections(), se = o->end_sections();
368 si != se; si.increment(ec)) {
369 if (error(ec)) return;
370 StringRef Name;
371 if (error(si->getName(Name))) return;
372 uint64_t Address;
373 if (error(si->getAddress(Address))) return;
374 uint64_t Size;
375 if (error(si->getSize(Size))) return;
376 bool Text, Data, BSS;
377 if (error(si->isText(Text))) return;
378 if (error(si->isData(Data))) return;
379 if (error(si->isBSS(BSS))) return;
380 std::string Type = (std::string(Text ? "TEXT " : "") +
Michael J. Spencer14a5f462011-10-13 20:37:20 +0000381 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
NAKAMURA Takumif048c3f2011-10-11 12:51:50 +0000382 outs() << format("%3d %-13s %09"PRIx64" %017"PRIx64" %s\n", i, Name.str().c_str(), Size,
Nick Lewycky023bb152011-10-10 21:21:34 +0000383 Address, Type.c_str());
384 ++i;
385 }
386}
387
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000388static void PrintSectionContents(const ObjectFile *o) {
389 error_code ec;
390 for (section_iterator si = o->begin_sections(),
391 se = o->end_sections();
392 si != se; si.increment(ec)) {
393 if (error(ec)) return;
394 StringRef Name;
395 StringRef Contents;
396 uint64_t BaseAddr;
397 if (error(si->getName(Name))) continue;
398 if (error(si->getContents(Contents))) continue;
399 if (error(si->getAddress(BaseAddr))) continue;
400
401 outs() << "Contents of section " << Name << ":\n";
402
403 // Dump out the content as hex and printable ascii characters.
404 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
Stepan Dyatkovskiy9df3b912011-10-28 13:07:32 +0000405 outs() << format(" %04"PRIx64" ", BaseAddr + addr);
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000406 // Dump line of hex.
407 for (std::size_t i = 0; i < 16; ++i) {
408 if (i != 0 && i % 4 == 0)
409 outs() << ' ';
410 if (addr + i < end)
411 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
412 << hexdigit(Contents[addr + i] & 0xF, true);
413 else
414 outs() << " ";
415 }
416 // Print ascii.
417 outs() << " ";
418 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
419 if (std::isprint(Contents[addr + i] & 0xFF))
420 outs() << Contents[addr + i];
421 else
422 outs() << ".";
423 }
424 outs() << "\n";
425 }
426 }
427}
428
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000429static void PrintCOFFSymbolTable(const COFFObjectFile *coff) {
430 const coff_file_header *header;
431 if (error(coff->getHeader(header))) return;
432 int aux_count = 0;
433 const coff_symbol *symbol = 0;
434 for (int i = 0, e = header->NumberOfSymbols; i != e; ++i) {
435 if (aux_count--) {
436 // Figure out which type of aux this is.
437 if (symbol->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC
438 && symbol->Value == 0) { // Section definition.
439 const coff_aux_section_definition *asd;
440 if (error(coff->getAuxSymbol<coff_aux_section_definition>(i, asd)))
441 return;
442 outs() << "AUX "
443 << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x "
444 , unsigned(asd->Length)
445 , unsigned(asd->NumberOfRelocations)
446 , unsigned(asd->NumberOfLinenumbers)
447 , unsigned(asd->CheckSum))
448 << format("assoc %d comdat %d\n"
449 , unsigned(asd->Number)
450 , unsigned(asd->Selection));
451 } else {
452 outs() << "AUX Unknown\n";
453 }
454 } else {
455 StringRef name;
456 if (error(coff->getSymbol(i, symbol))) return;
457 if (error(coff->getSymbolName(symbol, name))) return;
458 outs() << "[" << format("%2d", i) << "]"
459 << "(sec " << format("%2d", int(symbol->SectionNumber)) << ")"
460 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
461 << "(ty " << format("%3x", unsigned(symbol->Type)) << ")"
462 << "(scl " << format("%3x", unsigned(symbol->StorageClass)) << ") "
463 << "(nx " << unsigned(symbol->NumberOfAuxSymbols) << ") "
464 << "0x" << format("%08x", unsigned(symbol->Value)) << " "
465 << name << "\n";
466 aux_count = symbol->NumberOfAuxSymbols;
467 }
468 }
469}
470
471static void PrintSymbolTable(const ObjectFile *o) {
472 outs() << "SYMBOL TABLE:\n";
473
474 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o))
475 PrintCOFFSymbolTable(coff);
476 else {
477 error_code ec;
478 for (symbol_iterator si = o->begin_symbols(),
479 se = o->end_symbols(); si != se; si.increment(ec)) {
480 if (error(ec)) return;
481 StringRef Name;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000482 uint64_t Address;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000483 bool Global;
484 SymbolRef::Type Type;
485 bool Weak;
486 bool Absolute;
487 uint64_t Size;
488 section_iterator Section = o->end_sections();
489 if (error(si->getName(Name))) continue;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000490 if (error(si->getAddress(Address))) continue;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000491 if (error(si->isGlobal(Global))) continue;
492 if (error(si->getType(Type))) continue;
493 if (error(si->isWeak(Weak))) continue;
494 if (error(si->isAbsolute(Absolute))) continue;
495 if (error(si->getSize(Size))) continue;
496 if (error(si->getSection(Section))) continue;
497
Danil Malyshevb0436a72011-11-29 17:40:10 +0000498 if (Address == UnknownAddressOrSize)
499 Address = 0;
500 if (Size == UnknownAddressOrSize)
501 Size = 0;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000502 char GlobLoc = ' ';
503 if (Type != SymbolRef::ST_External)
504 GlobLoc = Global ? 'g' : 'l';
505 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
506 ? 'd' : ' ';
507 char FileFunc = ' ';
508 if (Type == SymbolRef::ST_File)
509 FileFunc = 'f';
510 else if (Type == SymbolRef::ST_Function)
511 FileFunc = 'F';
512
Danil Malyshevb0436a72011-11-29 17:40:10 +0000513 outs() << format("%08"PRIx64, Address) << " "
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000514 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
515 << (Weak ? 'w' : ' ') // Weak?
516 << ' ' // Constructor. Not supported yet.
517 << ' ' // Warning. Not supported yet.
518 << ' ' // Indirect reference to another symbol.
519 << Debug // Debugging (d) or dynamic (D) symbol.
520 << FileFunc // Name of function (F), file (f) or object (O).
521 << ' ';
522 if (Absolute)
523 outs() << "*ABS*";
524 else if (Section == o->end_sections())
525 outs() << "*UND*";
526 else {
527 StringRef SectionName;
528 if (error(Section->getName(SectionName)))
529 SectionName = "";
530 outs() << SectionName;
531 }
532 outs() << '\t'
Stepan Dyatkovskiy9df3b912011-10-28 13:07:32 +0000533 << format("%08"PRIx64" ", Size)
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000534 << Name
535 << '\n';
536 }
537 }
538}
539
Michael J. Spencer27781b72011-10-08 00:18:30 +0000540static void DumpObject(const ObjectFile *o) {
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000541 outs() << '\n';
542 outs() << o->getFileName()
543 << ":\tfile format " << o->getFileFormatName() << "\n\n";
544
Michael J. Spencer27781b72011-10-08 00:18:30 +0000545 if (Disassemble)
Michael J. Spencer942eb002011-10-13 22:17:18 +0000546 DisassembleObject(o, Relocations);
547 if (Relocations && !Disassemble)
Michael J. Spencer27781b72011-10-08 00:18:30 +0000548 PrintRelocations(o);
Nick Lewycky023bb152011-10-10 21:21:34 +0000549 if (SectionHeaders)
550 PrintSectionHeaders(o);
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000551 if (SectionContents)
552 PrintSectionContents(o);
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000553 if (SymbolTable)
554 PrintSymbolTable(o);
Michael J. Spencer27781b72011-10-08 00:18:30 +0000555}
556
557/// @brief Dump each object file in \a a;
558static void DumpArchive(const Archive *a) {
559 for (Archive::child_iterator i = a->begin_children(),
560 e = a->end_children(); i != e; ++i) {
561 OwningPtr<Binary> child;
562 if (error_code ec = i->getAsBinary(child)) {
Michael J. Spencerf81285c2011-11-16 01:24:41 +0000563 // Ignore non-object files.
564 if (ec != object_error::invalid_file_type)
565 errs() << ToolName << ": '" << a->getFileName() << "': " << ec.message()
566 << ".\n";
Michael J. Spencer27781b72011-10-08 00:18:30 +0000567 continue;
568 }
569 if (ObjectFile *o = dyn_cast<ObjectFile>(child.get()))
570 DumpObject(o);
571 else
572 errs() << ToolName << ": '" << a->getFileName() << "': "
573 << "Unrecognized file type.\n";
574 }
575}
576
577/// @brief Open file and figure out how to dump it.
578static void DumpInput(StringRef file) {
579 // If file isn't stdin, check that it exists.
580 if (file != "-" && !sys::fs::exists(file)) {
581 errs() << ToolName << ": '" << file << "': " << "No such file\n";
582 return;
583 }
584
585 if (MachO && Disassemble) {
586 DisassembleInputMachO(file);
587 return;
588 }
589
590 // Attempt to open the binary.
591 OwningPtr<Binary> binary;
592 if (error_code ec = createBinary(file, binary)) {
593 errs() << ToolName << ": '" << file << "': " << ec.message() << ".\n";
594 return;
595 }
596
597 if (Archive *a = dyn_cast<Archive>(binary.get())) {
598 DumpArchive(a);
599 } else if (ObjectFile *o = dyn_cast<ObjectFile>(binary.get())) {
600 DumpObject(o);
601 } else {
602 errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n";
603 }
604}
605
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000606int main(int argc, char **argv) {
607 // Print a stack trace if we signal out.
608 sys::PrintStackTraceOnErrorSignal();
609 PrettyStackTraceProgram X(argc, argv);
610 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
611
612 // Initialize targets and assembly printers/parsers.
613 llvm::InitializeAllTargetInfos();
Evan Chenge78085a2011-07-22 21:58:54 +0000614 llvm::InitializeAllTargetMCs();
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000615 llvm::InitializeAllAsmParsers();
616 llvm::InitializeAllDisassemblers();
617
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000618 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
619 TripleName = Triple::normalize(TripleName);
620
621 ToolName = argv[0];
622
623 // Defaults to a.out if no filenames specified.
624 if (InputFilenames.size() == 0)
625 InputFilenames.push_back("a.out");
626
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000627 if (!Disassemble
628 && !Relocations
629 && !SectionHeaders
630 && !SectionContents
631 && !SymbolTable) {
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000632 cl::PrintHelpMessage();
633 return 2;
634 }
635
Michael J. Spencer27781b72011-10-08 00:18:30 +0000636 std::for_each(InputFilenames.begin(), InputFilenames.end(),
637 DumpInput);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000638
639 return 0;
640}