blob: a8df3e57f98214434e4abe3078bbaf9db9566ad0 [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
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000076static StringRef ToolName;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000077
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000078static bool error(error_code ec) {
79 if (!ec) return false;
Michael J. Spencer25b15772011-06-25 17:55:23 +000080
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000081 outs() << ToolName << ": error reading file: " << ec.message() << ".\n";
82 outs().flush();
83 return true;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000084}
85
86static const Target *GetTarget(const ObjectFile *Obj = NULL) {
87 // Figure out the target triple.
88 llvm::Triple TT("unknown-unknown-unknown");
Michael J. Spencerd11699d2011-01-20 07:22:04 +000089 if (TripleName.empty()) {
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000090 if (Obj)
91 TT.setArch(Triple::ArchType(Obj->getArch()));
Michael J. Spencerd11699d2011-01-20 07:22:04 +000092 } else
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000093 TT.setTriple(Triple::normalize(TripleName));
94
95 if (!ArchName.empty())
96 TT.setArchName(ArchName);
97
98 TripleName = TT.str();
99
100 // Get the target specific parser.
101 std::string Error;
102 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
103 if (TheTarget)
104 return TheTarget;
105
106 errs() << ToolName << ": error: unable to get target for '" << TripleName
107 << "', see --version and --triple.\n";
108 return 0;
109}
110
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000111void llvm::DumpBytes(StringRef bytes) {
112 static const char hex_rep[] = "0123456789abcdef";
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000113 // FIXME: The real way to do this is to figure out the longest instruction
114 // and align to that size before printing. I'll fix this when I get
115 // around to outputting relocations.
116 // 15 is the longest x86 instruction
117 // 3 is for the hex rep of a byte + a space.
118 // 1 is for the null terminator.
119 enum { OutputSize = (15 * 3) + 1 };
120 char output[OutputSize];
121
122 assert(bytes.size() <= 15
123 && "DumpBytes only supports instructions of up to 15 bytes");
124 memset(output, ' ', sizeof(output));
125 unsigned index = 0;
126 for (StringRef::iterator i = bytes.begin(),
127 e = bytes.end(); i != e; ++i) {
128 output[index] = hex_rep[(*i & 0xF0) >> 4];
129 output[index + 1] = hex_rep[*i & 0xF];
130 index += 3;
131 }
132
133 output[sizeof(output) - 1] = 0;
134 outs() << output;
135}
136
Michael J. Spencer27781b72011-10-08 00:18:30 +0000137static void DisassembleObject(const ObjectFile *Obj) {
138 const Target *TheTarget = GetTarget(Obj);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000139 if (!TheTarget) {
140 // GetTarget prints out stuff.
141 return;
142 }
143
144 outs() << '\n';
Michael J. Spencer27781b72011-10-08 00:18:30 +0000145 outs() << Obj->getFileName()
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000146 << ":\tfile format " << Obj->getFileFormatName() << "\n\n";
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000147
Michael J. Spencer25b15772011-06-25 17:55:23 +0000148 error_code ec;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000149 for (section_iterator i = Obj->begin_sections(),
Michael J. Spencer27781b72011-10-08 00:18:30 +0000150 e = Obj->end_sections();
151 i != e; i.increment(ec)) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000152 if (error(ec)) break;
153 bool text;
154 if (error(i->isText(text))) break;
155 if (!text) continue;
156
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000157 // Make a list of all the symbols in this section.
158 std::vector<std::pair<uint64_t, StringRef> > Symbols;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000159 for (symbol_iterator si = Obj->begin_symbols(),
Michael J. Spencer27781b72011-10-08 00:18:30 +0000160 se = Obj->end_symbols();
161 si != se; si.increment(ec)) {
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000162 bool contains;
163 if (!error(i->containsSymbol(*si, contains)) && contains) {
164 uint64_t Address;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000165 if (error(si->getOffset(Address))) break;
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000166 StringRef Name;
167 if (error(si->getName(Name))) break;
168 Symbols.push_back(std::make_pair(Address, Name));
169 }
170 }
171
172 // Sort the symbols by address, just in case they didn't come in that way.
173 array_pod_sort(Symbols.begin(), Symbols.end());
174
Michael J. Spencer25b15772011-06-25 17:55:23 +0000175 StringRef name;
176 if (error(i->getName(name))) break;
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000177 outs() << "Disassembly of section " << name << ':';
178
179 // If the section has no symbols just insert a dummy one and disassemble
180 // the whole section.
181 if (Symbols.empty())
182 Symbols.push_back(std::make_pair(0, name));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000183
184 // Set up disassembler.
Evan Cheng1abf2cb2011-07-14 23:50:31 +0000185 OwningPtr<const MCAsmInfo> AsmInfo(TheTarget->createMCAsmInfo(TripleName));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000186
187 if (!AsmInfo) {
188 errs() << "error: no assembly info for target " << TripleName << "\n";
189 return;
190 }
191
Michael J. Spencer27781b72011-10-08 00:18:30 +0000192 OwningPtr<const MCSubtargetInfo> STI(
193 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
James Molloyb9505852011-09-07 17:24:38 +0000194
195 if (!STI) {
196 errs() << "error: no subtarget info for target " << TripleName << "\n";
197 return;
198 }
199
Michael J. Spencer27781b72011-10-08 00:18:30 +0000200 OwningPtr<const MCDisassembler> DisAsm(
201 TheTarget->createMCDisassembler(*STI));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000202 if (!DisAsm) {
203 errs() << "error: no disassembler for target " << TripleName << "\n";
204 return;
205 }
206
207 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
208 OwningPtr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
James Molloyb9505852011-09-07 17:24:38 +0000209 AsmPrinterVariant, *AsmInfo, *STI));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000210 if (!IP) {
Michael J. Spencer27781b72011-10-08 00:18:30 +0000211 errs() << "error: no instruction printer for target " << TripleName
212 << '\n';
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000213 return;
214 }
215
Michael J. Spencer25b15772011-06-25 17:55:23 +0000216 StringRef Bytes;
217 if (error(i->getContents(Bytes))) break;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000218 StringRefMemoryObject memoryObject(Bytes);
219 uint64_t Size;
220 uint64_t Index;
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000221 uint64_t SectSize;
222 if (error(i->getSize(SectSize))) break;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000223
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000224 // Disassemble symbol by symbol.
225 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
226 uint64_t Start = Symbols[si].first;
227 uint64_t End = si == se-1 ? SectSize : Symbols[si + 1].first - 1;
228 outs() << '\n' << Symbols[si].second << ":\n";
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000229
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000230#ifndef NDEBUG
231 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
232#else
233 raw_ostream &DebugOut = nulls();
234#endif
235
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000236 for (Index = Start; Index < End; Index += Size) {
237 MCInst Inst;
Owen Anderson98c5dda2011-09-15 23:38:46 +0000238
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000239 if (DisAsm->getInstruction(Inst, Size, memoryObject, Index,
240 DebugOut, nulls())) {
241 uint64_t addr;
242 if (error(i->getAddress(addr))) break;
243 outs() << format("%8x:\t", addr + Index);
244 DumpBytes(StringRef(Bytes.data() + Index, Size));
245 IP->printInst(&Inst, outs(), "");
246 outs() << "\n";
247 } else {
248 errs() << ToolName << ": warning: invalid instruction encoding\n";
249 if (Size == 0)
250 Size = 1; // skip illegible bytes
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000251 }
Benjamin Kramer685a2502011-07-20 19:37:35 +0000252 }
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000253 }
254 }
255}
256
Michael J. Spencer27781b72011-10-08 00:18:30 +0000257static void PrintRelocations(const ObjectFile *o) {
258 error_code ec;
259 for (section_iterator si = o->begin_sections(), se = o->end_sections();
260 si != se; si.increment(ec)){
261 if (error(ec)) return;
262 if (si->begin_relocations() == si->end_relocations())
263 continue;
264 StringRef secname;
265 if (error(si->getName(secname))) continue;
266 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
267 for (relocation_iterator ri = si->begin_relocations(),
268 re = si->end_relocations();
269 ri != re; ri.increment(ec)) {
270 if (error(ec)) return;
271
272 uint64_t address;
273 SmallString<32> relocname;
274 SmallString<32> valuestr;
275 if (error(ri->getTypeName(relocname))) continue;
276 if (error(ri->getAddress(address))) continue;
277 if (error(ri->getValueString(valuestr))) continue;
278 outs() << address << " " << relocname << " " << valuestr << "\n";
279 }
280 outs() << "\n";
281 }
282}
283
284static void DumpObject(const ObjectFile *o) {
285 if (Disassemble)
286 DisassembleObject(o);
287 if (Relocations)
288 PrintRelocations(o);
289}
290
291/// @brief Dump each object file in \a a;
292static void DumpArchive(const Archive *a) {
293 for (Archive::child_iterator i = a->begin_children(),
294 e = a->end_children(); i != e; ++i) {
295 OwningPtr<Binary> child;
296 if (error_code ec = i->getAsBinary(child)) {
297 errs() << ToolName << ": '" << a->getFileName() << "': " << ec.message()
298 << ".\n";
299 continue;
300 }
301 if (ObjectFile *o = dyn_cast<ObjectFile>(child.get()))
302 DumpObject(o);
303 else
304 errs() << ToolName << ": '" << a->getFileName() << "': "
305 << "Unrecognized file type.\n";
306 }
307}
308
309/// @brief Open file and figure out how to dump it.
310static void DumpInput(StringRef file) {
311 // If file isn't stdin, check that it exists.
312 if (file != "-" && !sys::fs::exists(file)) {
313 errs() << ToolName << ": '" << file << "': " << "No such file\n";
314 return;
315 }
316
317 if (MachO && Disassemble) {
318 DisassembleInputMachO(file);
319 return;
320 }
321
322 // Attempt to open the binary.
323 OwningPtr<Binary> binary;
324 if (error_code ec = createBinary(file, binary)) {
325 errs() << ToolName << ": '" << file << "': " << ec.message() << ".\n";
326 return;
327 }
328
329 if (Archive *a = dyn_cast<Archive>(binary.get())) {
330 DumpArchive(a);
331 } else if (ObjectFile *o = dyn_cast<ObjectFile>(binary.get())) {
332 DumpObject(o);
333 } else {
334 errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n";
335 }
336}
337
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000338int main(int argc, char **argv) {
339 // Print a stack trace if we signal out.
340 sys::PrintStackTraceOnErrorSignal();
341 PrettyStackTraceProgram X(argc, argv);
342 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
343
344 // Initialize targets and assembly printers/parsers.
345 llvm::InitializeAllTargetInfos();
Evan Chenge78085a2011-07-22 21:58:54 +0000346 llvm::InitializeAllTargetMCs();
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000347 llvm::InitializeAllAsmParsers();
348 llvm::InitializeAllDisassemblers();
349
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000350 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
351 TripleName = Triple::normalize(TripleName);
352
353 ToolName = argv[0];
354
355 // Defaults to a.out if no filenames specified.
356 if (InputFilenames.size() == 0)
357 InputFilenames.push_back("a.out");
358
Michael J. Spencer27781b72011-10-08 00:18:30 +0000359 if (!Disassemble && !Relocations) {
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000360 cl::PrintHelpMessage();
361 return 2;
362 }
363
Michael J. Spencer27781b72011-10-08 00:18:30 +0000364 std::for_each(InputFilenames.begin(), InputFilenames.end(),
365 DumpInput);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000366
367 return 0;
368}