blob: e9231d0a0661c3e698cc62d3e9134c606110e11c [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"
Evan Cheng78011362011-08-23 20:15:21 +000027#include "llvm/MC/MCInstrAnalysis.h"
Benjamin Kramer685a2502011-07-20 19:37:35 +000028#include "llvm/MC/MCInstrDesc.h"
29#include "llvm/MC/MCInstrInfo.h"
James Molloyb9505852011-09-07 17:24:38 +000030#include "llvm/MC/MCSubtargetInfo.h"
Michael J. Spencer27781b72011-10-08 00:18:30 +000031#include "llvm/Support/Casting.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000032#include "llvm/Support/CommandLine.h"
33#include "llvm/Support/Debug.h"
Michael J. Spencer27781b72011-10-08 00:18:30 +000034#include "llvm/Support/FileSystem.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000035#include "llvm/Support/Format.h"
Benjamin Kramer853b0fd2011-07-25 23:04:36 +000036#include "llvm/Support/GraphWriter.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000037#include "llvm/Support/Host.h"
38#include "llvm/Support/ManagedStatic.h"
39#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000040#include "llvm/Support/MemoryObject.h"
41#include "llvm/Support/PrettyStackTrace.h"
42#include "llvm/Support/Signals.h"
43#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000044#include "llvm/Support/TargetRegistry.h"
45#include "llvm/Support/TargetSelect.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000046#include "llvm/Support/raw_ostream.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000047#include "llvm/Support/system_error.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000048#include <algorithm>
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000049#include <cstring>
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000050using namespace llvm;
51using namespace object;
52
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000053static cl::list<std::string>
54InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000055
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000056static cl::opt<bool>
57Disassemble("disassemble",
58 cl::desc("Display assembler mnemonics for the machine instructions"));
59static cl::alias
60Disassembled("d", cl::desc("Alias for --disassemble"),
61 cl::aliasopt(Disassemble));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000062
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000063static cl::opt<bool>
Michael J. Spencer27781b72011-10-08 00:18:30 +000064Relocations("r", cl::desc("Display the relocation entries in the file"));
65
66static cl::opt<bool>
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000067MachO("macho", cl::desc("Use MachO specific object file parser"));
68static cl::alias
69MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachO));
Benjamin Kramer685a2502011-07-20 19:37:35 +000070
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000071cl::opt<std::string>
72llvm::TripleName("triple", cl::desc("Target triple to disassemble for, "
73 "see -version for available targets"));
74
75cl::opt<std::string>
76llvm::ArchName("arch", cl::desc("Target arch to disassemble for, "
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000077 "see -version for available targets"));
78
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000079static StringRef ToolName;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000080
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000081static bool error(error_code ec) {
82 if (!ec) return false;
Michael J. Spencer25b15772011-06-25 17:55:23 +000083
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000084 outs() << ToolName << ": error reading file: " << ec.message() << ".\n";
85 outs().flush();
86 return true;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000087}
88
89static const Target *GetTarget(const ObjectFile *Obj = NULL) {
90 // Figure out the target triple.
91 llvm::Triple TT("unknown-unknown-unknown");
Michael J. Spencerd11699d2011-01-20 07:22:04 +000092 if (TripleName.empty()) {
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000093 if (Obj)
94 TT.setArch(Triple::ArchType(Obj->getArch()));
Michael J. Spencerd11699d2011-01-20 07:22:04 +000095 } else
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000096 TT.setTriple(Triple::normalize(TripleName));
97
98 if (!ArchName.empty())
99 TT.setArchName(ArchName);
100
101 TripleName = TT.str();
102
103 // Get the target specific parser.
104 std::string Error;
105 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
106 if (TheTarget)
107 return TheTarget;
108
109 errs() << ToolName << ": error: unable to get target for '" << TripleName
110 << "', see --version and --triple.\n";
111 return 0;
112}
113
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000114void llvm::DumpBytes(StringRef bytes) {
115 static const char hex_rep[] = "0123456789abcdef";
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000116 // FIXME: The real way to do this is to figure out the longest instruction
117 // and align to that size before printing. I'll fix this when I get
118 // around to outputting relocations.
119 // 15 is the longest x86 instruction
120 // 3 is for the hex rep of a byte + a space.
121 // 1 is for the null terminator.
122 enum { OutputSize = (15 * 3) + 1 };
123 char output[OutputSize];
124
125 assert(bytes.size() <= 15
126 && "DumpBytes only supports instructions of up to 15 bytes");
127 memset(output, ' ', sizeof(output));
128 unsigned index = 0;
129 for (StringRef::iterator i = bytes.begin(),
130 e = bytes.end(); i != e; ++i) {
131 output[index] = hex_rep[(*i & 0xF0) >> 4];
132 output[index + 1] = hex_rep[*i & 0xF];
133 index += 3;
134 }
135
136 output[sizeof(output) - 1] = 0;
137 outs() << output;
138}
139
Michael J. Spencer27781b72011-10-08 00:18:30 +0000140static void DisassembleObject(const ObjectFile *Obj) {
141 const Target *TheTarget = GetTarget(Obj);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000142 if (!TheTarget) {
143 // GetTarget prints out stuff.
144 return;
145 }
Benjamin Kramer685a2502011-07-20 19:37:35 +0000146 const MCInstrInfo *InstrInfo = TheTarget->createMCInstrInfo();
Benjamin Kramer41ab14b2011-08-08 18:56:44 +0000147 OwningPtr<MCInstrAnalysis>
148 InstrAnalysis(TheTarget->createMCInstrAnalysis(InstrInfo));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000149
150 outs() << '\n';
Michael J. Spencer27781b72011-10-08 00:18:30 +0000151 outs() << Obj->getFileName()
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000152 << ":\tfile format " << Obj->getFileFormatName() << "\n\n";
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000153
Michael J. Spencer25b15772011-06-25 17:55:23 +0000154 error_code ec;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000155 for (section_iterator i = Obj->begin_sections(),
Michael J. Spencer27781b72011-10-08 00:18:30 +0000156 e = Obj->end_sections();
157 i != e; i.increment(ec)) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000158 if (error(ec)) break;
159 bool text;
160 if (error(i->isText(text))) break;
161 if (!text) continue;
162
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000163 // Make a list of all the symbols in this section.
164 std::vector<std::pair<uint64_t, StringRef> > Symbols;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000165 for (symbol_iterator si = Obj->begin_symbols(),
Michael J. Spencer27781b72011-10-08 00:18:30 +0000166 se = Obj->end_symbols();
167 si != se; si.increment(ec)) {
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000168 bool contains;
169 if (!error(i->containsSymbol(*si, contains)) && contains) {
170 uint64_t Address;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000171 if (error(si->getOffset(Address))) break;
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000172 StringRef Name;
173 if (error(si->getName(Name))) break;
174 Symbols.push_back(std::make_pair(Address, Name));
175 }
176 }
177
178 // Sort the symbols by address, just in case they didn't come in that way.
179 array_pod_sort(Symbols.begin(), Symbols.end());
180
Michael J. Spencer25b15772011-06-25 17:55:23 +0000181 StringRef name;
182 if (error(i->getName(name))) break;
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000183 outs() << "Disassembly of section " << name << ':';
184
185 // If the section has no symbols just insert a dummy one and disassemble
186 // the whole section.
187 if (Symbols.empty())
188 Symbols.push_back(std::make_pair(0, name));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000189
190 // Set up disassembler.
Evan Cheng1abf2cb2011-07-14 23:50:31 +0000191 OwningPtr<const MCAsmInfo> AsmInfo(TheTarget->createMCAsmInfo(TripleName));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000192
193 if (!AsmInfo) {
194 errs() << "error: no assembly info for target " << TripleName << "\n";
195 return;
196 }
197
Michael J. Spencer27781b72011-10-08 00:18:30 +0000198 OwningPtr<const MCSubtargetInfo> STI(
199 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
James Molloyb9505852011-09-07 17:24:38 +0000200
201 if (!STI) {
202 errs() << "error: no subtarget info for target " << TripleName << "\n";
203 return;
204 }
205
Michael J. Spencer27781b72011-10-08 00:18:30 +0000206 OwningPtr<const MCDisassembler> DisAsm(
207 TheTarget->createMCDisassembler(*STI));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000208 if (!DisAsm) {
209 errs() << "error: no disassembler for target " << TripleName << "\n";
210 return;
211 }
212
213 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
214 OwningPtr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
James Molloyb9505852011-09-07 17:24:38 +0000215 AsmPrinterVariant, *AsmInfo, *STI));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000216 if (!IP) {
Michael J. Spencer27781b72011-10-08 00:18:30 +0000217 errs() << "error: no instruction printer for target " << TripleName
218 << '\n';
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000219 return;
220 }
221
Michael J. Spencer25b15772011-06-25 17:55:23 +0000222 StringRef Bytes;
223 if (error(i->getContents(Bytes))) break;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000224 StringRefMemoryObject memoryObject(Bytes);
225 uint64_t Size;
226 uint64_t Index;
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000227 uint64_t SectSize;
228 if (error(i->getSize(SectSize))) break;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000229
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000230 // Disassemble symbol by symbol.
231 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
232 uint64_t Start = Symbols[si].first;
233 uint64_t End = si == se-1 ? SectSize : Symbols[si + 1].first - 1;
234 outs() << '\n' << Symbols[si].second << ":\n";
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000235
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000236#ifndef NDEBUG
237 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
238#else
239 raw_ostream &DebugOut = nulls();
240#endif
241
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000242 for (Index = Start; Index < End; Index += Size) {
243 MCInst Inst;
Owen Anderson98c5dda2011-09-15 23:38:46 +0000244
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000245 if (DisAsm->getInstruction(Inst, Size, memoryObject, Index,
246 DebugOut, nulls())) {
247 uint64_t addr;
248 if (error(i->getAddress(addr))) break;
249 outs() << format("%8x:\t", addr + Index);
250 DumpBytes(StringRef(Bytes.data() + Index, Size));
251 IP->printInst(&Inst, outs(), "");
252 outs() << "\n";
253 } else {
254 errs() << ToolName << ": warning: invalid instruction encoding\n";
255 if (Size == 0)
256 Size = 1; // skip illegible bytes
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000257 }
Benjamin Kramer685a2502011-07-20 19:37:35 +0000258 }
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000259 }
260 }
261}
262
Michael J. Spencer27781b72011-10-08 00:18:30 +0000263static void PrintRelocations(const ObjectFile *o) {
264 error_code ec;
265 for (section_iterator si = o->begin_sections(), se = o->end_sections();
266 si != se; si.increment(ec)){
267 if (error(ec)) return;
268 if (si->begin_relocations() == si->end_relocations())
269 continue;
270 StringRef secname;
271 if (error(si->getName(secname))) continue;
272 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
273 for (relocation_iterator ri = si->begin_relocations(),
274 re = si->end_relocations();
275 ri != re; ri.increment(ec)) {
276 if (error(ec)) return;
277
278 uint64_t address;
279 SmallString<32> relocname;
280 SmallString<32> valuestr;
281 if (error(ri->getTypeName(relocname))) continue;
282 if (error(ri->getAddress(address))) continue;
283 if (error(ri->getValueString(valuestr))) continue;
284 outs() << address << " " << relocname << " " << valuestr << "\n";
285 }
286 outs() << "\n";
287 }
288}
289
290static void DumpObject(const ObjectFile *o) {
291 if (Disassemble)
292 DisassembleObject(o);
293 if (Relocations)
294 PrintRelocations(o);
295}
296
297/// @brief Dump each object file in \a a;
298static void DumpArchive(const Archive *a) {
299 for (Archive::child_iterator i = a->begin_children(),
300 e = a->end_children(); i != e; ++i) {
301 OwningPtr<Binary> child;
302 if (error_code ec = i->getAsBinary(child)) {
303 errs() << ToolName << ": '" << a->getFileName() << "': " << ec.message()
304 << ".\n";
305 continue;
306 }
307 if (ObjectFile *o = dyn_cast<ObjectFile>(child.get()))
308 DumpObject(o);
309 else
310 errs() << ToolName << ": '" << a->getFileName() << "': "
311 << "Unrecognized file type.\n";
312 }
313}
314
315/// @brief Open file and figure out how to dump it.
316static void DumpInput(StringRef file) {
317 // If file isn't stdin, check that it exists.
318 if (file != "-" && !sys::fs::exists(file)) {
319 errs() << ToolName << ": '" << file << "': " << "No such file\n";
320 return;
321 }
322
323 if (MachO && Disassemble) {
324 DisassembleInputMachO(file);
325 return;
326 }
327
328 // Attempt to open the binary.
329 OwningPtr<Binary> binary;
330 if (error_code ec = createBinary(file, binary)) {
331 errs() << ToolName << ": '" << file << "': " << ec.message() << ".\n";
332 return;
333 }
334
335 if (Archive *a = dyn_cast<Archive>(binary.get())) {
336 DumpArchive(a);
337 } else if (ObjectFile *o = dyn_cast<ObjectFile>(binary.get())) {
338 DumpObject(o);
339 } else {
340 errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n";
341 }
342}
343
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000344int main(int argc, char **argv) {
345 // Print a stack trace if we signal out.
346 sys::PrintStackTraceOnErrorSignal();
347 PrettyStackTraceProgram X(argc, argv);
348 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
349
350 // Initialize targets and assembly printers/parsers.
351 llvm::InitializeAllTargetInfos();
Evan Chenge78085a2011-07-22 21:58:54 +0000352 llvm::InitializeAllTargetMCs();
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000353 llvm::InitializeAllAsmParsers();
354 llvm::InitializeAllDisassemblers();
355
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000356 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
357 TripleName = Triple::normalize(TripleName);
358
359 ToolName = argv[0];
360
361 // Defaults to a.out if no filenames specified.
362 if (InputFilenames.size() == 0)
363 InputFilenames.push_back("a.out");
364
Michael J. Spencer27781b72011-10-08 00:18:30 +0000365 if (!Disassemble && !Relocations) {
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000366 cl::PrintHelpMessage();
367 return 2;
368 }
369
Michael J. Spencer27781b72011-10-08 00:18:30 +0000370 std::for_each(InputFilenames.begin(), InputFilenames.end(),
371 DumpInput);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000372
373 return 0;
374}