blob: 66669cfe5d70109b00077ee56177de32562891bd [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"
21#include "llvm/ADT/Triple.h"
Benjamin Kramer739b65b2011-07-15 18:39:24 +000022#include "llvm/ADT/STLExtras.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000023#include "llvm/MC/MCAsmInfo.h"
24#include "llvm/MC/MCDisassembler.h"
25#include "llvm/MC/MCInst.h"
26#include "llvm/MC/MCInstPrinter.h"
James Molloyb9505852011-09-07 17:24:38 +000027#include "llvm/MC/MCSubtargetInfo.h"
Michael J. Spencer27781b72011-10-08 00:18:30 +000028#include "llvm/Support/Casting.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000029#include "llvm/Support/CommandLine.h"
30#include "llvm/Support/Debug.h"
Michael J. Spencer27781b72011-10-08 00:18:30 +000031#include "llvm/Support/FileSystem.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000032#include "llvm/Support/Format.h"
Benjamin Kramer853b0fd2011-07-25 23:04:36 +000033#include "llvm/Support/GraphWriter.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000034#include "llvm/Support/Host.h"
35#include "llvm/Support/ManagedStatic.h"
36#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000037#include "llvm/Support/MemoryObject.h"
38#include "llvm/Support/PrettyStackTrace.h"
39#include "llvm/Support/Signals.h"
40#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000041#include "llvm/Support/TargetRegistry.h"
42#include "llvm/Support/TargetSelect.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000043#include "llvm/Support/raw_ostream.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000044#include "llvm/Support/system_error.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000045#include <algorithm>
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000046#include <cstring>
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000047using namespace llvm;
48using namespace object;
49
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000050static cl::list<std::string>
51InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000052
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000053static cl::opt<bool>
54Disassemble("disassemble",
55 cl::desc("Display assembler mnemonics for the machine instructions"));
56static cl::alias
57Disassembled("d", cl::desc("Alias for --disassemble"),
58 cl::aliasopt(Disassemble));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000059
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000060static cl::opt<bool>
Michael J. Spencer27781b72011-10-08 00:18:30 +000061Relocations("r", cl::desc("Display the relocation entries in the file"));
62
63static cl::opt<bool>
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000064MachO("macho", cl::desc("Use MachO specific object file parser"));
65static cl::alias
66MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachO));
Benjamin Kramer685a2502011-07-20 19:37:35 +000067
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000068cl::opt<std::string>
69llvm::TripleName("triple", cl::desc("Target triple to disassemble for, "
70 "see -version for available targets"));
71
72cl::opt<std::string>
73llvm::ArchName("arch", cl::desc("Target arch to disassemble for, "
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000074 "see -version for available targets"));
75
Nick Lewycky023bb152011-10-10 21:21:34 +000076static cl::opt<bool>
77SectionHeaders("section-headers", cl::desc("Display summaries of the headers "
78 "for each section."));
79static cl::alias
80SectionHeadersShort("headers", cl::desc("Alias for --section-headers"),
81 cl::aliasopt(SectionHeaders));
82static cl::alias
83SectionHeadersShorter("h", cl::desc("Alias for --section-headers"),
84 cl::aliasopt(SectionHeaders));
85
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000086static StringRef ToolName;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000087
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000088static bool error(error_code ec) {
89 if (!ec) return false;
Michael J. Spencer25b15772011-06-25 17:55:23 +000090
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000091 outs() << ToolName << ": error reading file: " << ec.message() << ".\n";
92 outs().flush();
93 return true;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000094}
95
96static const Target *GetTarget(const ObjectFile *Obj = NULL) {
97 // Figure out the target triple.
98 llvm::Triple TT("unknown-unknown-unknown");
Michael J. Spencerd11699d2011-01-20 07:22:04 +000099 if (TripleName.empty()) {
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000100 if (Obj)
101 TT.setArch(Triple::ArchType(Obj->getArch()));
Michael J. Spencerd11699d2011-01-20 07:22:04 +0000102 } else
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000103 TT.setTriple(Triple::normalize(TripleName));
104
105 if (!ArchName.empty())
106 TT.setArchName(ArchName);
107
108 TripleName = TT.str();
109
110 // Get the target specific parser.
111 std::string Error;
112 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
113 if (TheTarget)
114 return TheTarget;
115
116 errs() << ToolName << ": error: unable to get target for '" << TripleName
117 << "', see --version and --triple.\n";
118 return 0;
119}
120
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000121void llvm::DumpBytes(StringRef bytes) {
122 static const char hex_rep[] = "0123456789abcdef";
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000123 // FIXME: The real way to do this is to figure out the longest instruction
124 // and align to that size before printing. I'll fix this when I get
125 // around to outputting relocations.
126 // 15 is the longest x86 instruction
127 // 3 is for the hex rep of a byte + a space.
128 // 1 is for the null terminator.
129 enum { OutputSize = (15 * 3) + 1 };
130 char output[OutputSize];
131
132 assert(bytes.size() <= 15
133 && "DumpBytes only supports instructions of up to 15 bytes");
134 memset(output, ' ', sizeof(output));
135 unsigned index = 0;
136 for (StringRef::iterator i = bytes.begin(),
137 e = bytes.end(); i != e; ++i) {
138 output[index] = hex_rep[(*i & 0xF0) >> 4];
139 output[index + 1] = hex_rep[*i & 0xF];
140 index += 3;
141 }
142
143 output[sizeof(output) - 1] = 0;
144 outs() << output;
145}
146
Michael J. Spencer27781b72011-10-08 00:18:30 +0000147static void DisassembleObject(const ObjectFile *Obj) {
148 const Target *TheTarget = GetTarget(Obj);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000149 if (!TheTarget) {
150 // GetTarget prints out stuff.
151 return;
152 }
153
154 outs() << '\n';
Michael J. Spencer27781b72011-10-08 00:18:30 +0000155 outs() << Obj->getFileName()
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000156 << ":\tfile format " << Obj->getFileFormatName() << "\n\n";
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000157
Michael J. Spencer25b15772011-06-25 17:55:23 +0000158 error_code ec;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000159 for (section_iterator i = Obj->begin_sections(),
Michael J. Spencer27781b72011-10-08 00:18:30 +0000160 e = Obj->end_sections();
161 i != e; i.increment(ec)) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000162 if (error(ec)) break;
163 bool text;
164 if (error(i->isText(text))) break;
165 if (!text) continue;
166
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000167 // Make a list of all the symbols in this section.
168 std::vector<std::pair<uint64_t, StringRef> > Symbols;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000169 for (symbol_iterator si = Obj->begin_symbols(),
Michael J. Spencer27781b72011-10-08 00:18:30 +0000170 se = Obj->end_symbols();
171 si != se; si.increment(ec)) {
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000172 bool contains;
173 if (!error(i->containsSymbol(*si, contains)) && contains) {
174 uint64_t Address;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000175 if (error(si->getOffset(Address))) break;
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000176 StringRef Name;
177 if (error(si->getName(Name))) break;
178 Symbols.push_back(std::make_pair(Address, Name));
179 }
180 }
181
182 // Sort the symbols by address, just in case they didn't come in that way.
183 array_pod_sort(Symbols.begin(), Symbols.end());
184
Michael J. Spencer25b15772011-06-25 17:55:23 +0000185 StringRef name;
186 if (error(i->getName(name))) break;
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000187 outs() << "Disassembly of section " << name << ':';
188
189 // If the section has no symbols just insert a dummy one and disassemble
190 // the whole section.
191 if (Symbols.empty())
192 Symbols.push_back(std::make_pair(0, name));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000193
194 // Set up disassembler.
Evan Cheng1abf2cb2011-07-14 23:50:31 +0000195 OwningPtr<const MCAsmInfo> AsmInfo(TheTarget->createMCAsmInfo(TripleName));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000196
197 if (!AsmInfo) {
198 errs() << "error: no assembly info for target " << TripleName << "\n";
199 return;
200 }
201
Michael J. Spencer27781b72011-10-08 00:18:30 +0000202 OwningPtr<const MCSubtargetInfo> STI(
203 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
James Molloyb9505852011-09-07 17:24:38 +0000204
205 if (!STI) {
206 errs() << "error: no subtarget info for target " << TripleName << "\n";
207 return;
208 }
209
Michael J. Spencer27781b72011-10-08 00:18:30 +0000210 OwningPtr<const MCDisassembler> DisAsm(
211 TheTarget->createMCDisassembler(*STI));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000212 if (!DisAsm) {
213 errs() << "error: no disassembler for target " << TripleName << "\n";
214 return;
215 }
216
217 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
218 OwningPtr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
James Molloyb9505852011-09-07 17:24:38 +0000219 AsmPrinterVariant, *AsmInfo, *STI));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000220 if (!IP) {
Michael J. Spencer27781b72011-10-08 00:18:30 +0000221 errs() << "error: no instruction printer for target " << TripleName
222 << '\n';
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000223 return;
224 }
225
Michael J. Spencer25b15772011-06-25 17:55:23 +0000226 StringRef Bytes;
227 if (error(i->getContents(Bytes))) break;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000228 StringRefMemoryObject memoryObject(Bytes);
229 uint64_t Size;
230 uint64_t Index;
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000231 uint64_t SectSize;
232 if (error(i->getSize(SectSize))) break;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000233
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000234 // Disassemble symbol by symbol.
235 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
236 uint64_t Start = Symbols[si].first;
237 uint64_t End = si == se-1 ? SectSize : Symbols[si + 1].first - 1;
238 outs() << '\n' << Symbols[si].second << ":\n";
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000239
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000240#ifndef NDEBUG
241 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
242#else
243 raw_ostream &DebugOut = nulls();
244#endif
245
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000246 for (Index = Start; Index < End; Index += Size) {
247 MCInst Inst;
Owen Anderson98c5dda2011-09-15 23:38:46 +0000248
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000249 if (DisAsm->getInstruction(Inst, Size, memoryObject, Index,
250 DebugOut, nulls())) {
251 uint64_t addr;
252 if (error(i->getAddress(addr))) break;
253 outs() << format("%8x:\t", addr + Index);
254 DumpBytes(StringRef(Bytes.data() + Index, Size));
255 IP->printInst(&Inst, outs(), "");
256 outs() << "\n";
257 } else {
258 errs() << ToolName << ": warning: invalid instruction encoding\n";
259 if (Size == 0)
260 Size = 1; // skip illegible bytes
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000261 }
Benjamin Kramer685a2502011-07-20 19:37:35 +0000262 }
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000263 }
264 }
265}
266
Michael J. Spencer27781b72011-10-08 00:18:30 +0000267static void PrintRelocations(const ObjectFile *o) {
268 error_code ec;
269 for (section_iterator si = o->begin_sections(), se = o->end_sections();
270 si != se; si.increment(ec)){
271 if (error(ec)) return;
272 if (si->begin_relocations() == si->end_relocations())
273 continue;
274 StringRef secname;
275 if (error(si->getName(secname))) continue;
276 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
277 for (relocation_iterator ri = si->begin_relocations(),
278 re = si->end_relocations();
279 ri != re; ri.increment(ec)) {
280 if (error(ec)) return;
281
282 uint64_t address;
283 SmallString<32> relocname;
284 SmallString<32> valuestr;
285 if (error(ri->getTypeName(relocname))) continue;
286 if (error(ri->getAddress(address))) continue;
287 if (error(ri->getValueString(valuestr))) continue;
288 outs() << address << " " << relocname << " " << valuestr << "\n";
289 }
290 outs() << "\n";
291 }
292}
293
Nick Lewycky023bb152011-10-10 21:21:34 +0000294static void PrintSectionHeaders(const ObjectFile *o) {
295 outs() << "Sections:\n"
296 "Idx Name Size Address Type\n";
297 error_code ec;
298 unsigned i = 0;
299 for (section_iterator si = o->begin_sections(), se = o->end_sections();
300 si != se; si.increment(ec)) {
301 if (error(ec)) return;
302 StringRef Name;
303 if (error(si->getName(Name))) return;
304 uint64_t Address;
305 if (error(si->getAddress(Address))) return;
306 uint64_t Size;
307 if (error(si->getSize(Size))) return;
308 bool Text, Data, BSS;
309 if (error(si->isText(Text))) return;
310 if (error(si->isData(Data))) return;
311 if (error(si->isBSS(BSS))) return;
312 std::string Type = (std::string(Text ? "TEXT " : "") +
313 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
NAKAMURA Takumif048c3f2011-10-11 12:51:50 +0000314 outs() << format("%3d %-13s %09"PRIx64" %017"PRIx64" %s\n", i, Name.str().c_str(), Size,
Nick Lewycky023bb152011-10-10 21:21:34 +0000315 Address, Type.c_str());
316 ++i;
317 }
318}
319
Michael J. Spencer27781b72011-10-08 00:18:30 +0000320static void DumpObject(const ObjectFile *o) {
321 if (Disassemble)
322 DisassembleObject(o);
323 if (Relocations)
324 PrintRelocations(o);
Nick Lewycky023bb152011-10-10 21:21:34 +0000325 if (SectionHeaders)
326 PrintSectionHeaders(o);
Michael J. Spencer27781b72011-10-08 00:18:30 +0000327}
328
329/// @brief Dump each object file in \a a;
330static void DumpArchive(const Archive *a) {
331 for (Archive::child_iterator i = a->begin_children(),
332 e = a->end_children(); i != e; ++i) {
333 OwningPtr<Binary> child;
334 if (error_code ec = i->getAsBinary(child)) {
335 errs() << ToolName << ": '" << a->getFileName() << "': " << ec.message()
336 << ".\n";
337 continue;
338 }
339 if (ObjectFile *o = dyn_cast<ObjectFile>(child.get()))
340 DumpObject(o);
341 else
342 errs() << ToolName << ": '" << a->getFileName() << "': "
343 << "Unrecognized file type.\n";
344 }
345}
346
347/// @brief Open file and figure out how to dump it.
348static void DumpInput(StringRef file) {
349 // If file isn't stdin, check that it exists.
350 if (file != "-" && !sys::fs::exists(file)) {
351 errs() << ToolName << ": '" << file << "': " << "No such file\n";
352 return;
353 }
354
355 if (MachO && Disassemble) {
356 DisassembleInputMachO(file);
357 return;
358 }
359
360 // Attempt to open the binary.
361 OwningPtr<Binary> binary;
362 if (error_code ec = createBinary(file, binary)) {
363 errs() << ToolName << ": '" << file << "': " << ec.message() << ".\n";
364 return;
365 }
366
367 if (Archive *a = dyn_cast<Archive>(binary.get())) {
368 DumpArchive(a);
369 } else if (ObjectFile *o = dyn_cast<ObjectFile>(binary.get())) {
370 DumpObject(o);
371 } else {
372 errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n";
373 }
374}
375
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000376int main(int argc, char **argv) {
377 // Print a stack trace if we signal out.
378 sys::PrintStackTraceOnErrorSignal();
379 PrettyStackTraceProgram X(argc, argv);
380 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
381
382 // Initialize targets and assembly printers/parsers.
383 llvm::InitializeAllTargetInfos();
Evan Chenge78085a2011-07-22 21:58:54 +0000384 llvm::InitializeAllTargetMCs();
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000385 llvm::InitializeAllAsmParsers();
386 llvm::InitializeAllDisassemblers();
387
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000388 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
389 TripleName = Triple::normalize(TripleName);
390
391 ToolName = argv[0];
392
393 // Defaults to a.out if no filenames specified.
394 if (InputFilenames.size() == 0)
395 InputFilenames.push_back("a.out");
396
Nick Lewycky023bb152011-10-10 21:21:34 +0000397 if (!Disassemble && !Relocations && !SectionHeaders) {
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000398 cl::PrintHelpMessage();
399 return 2;
400 }
401
Michael J. Spencer27781b72011-10-08 00:18:30 +0000402 std::for_each(InputFilenames.begin(), InputFilenames.end(),
403 DumpInput);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000404
405 return 0;
406}