blob: 0c966e266325ae83c1bf5c7f41a11185632c0bb0 [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
16#include "llvm/Object/ObjectFile.h"
17// This config must be included before llvm-config.h.
18#include "llvm/Config/config.h"
19#include "../../lib/MC/MCDisassembler/EDDisassembler.h"
20#include "../../lib/MC/MCDisassembler/EDInst.h"
21#include "../../lib/MC/MCDisassembler/EDOperand.h"
22#include "../../lib/MC/MCDisassembler/EDToken.h"
23#include "llvm/ADT/OwningPtr.h"
24#include "llvm/ADT/Triple.h"
25#include "llvm/MC/MCAsmInfo.h"
26#include "llvm/MC/MCDisassembler.h"
27#include "llvm/MC/MCInst.h"
28#include "llvm/MC/MCInstPrinter.h"
29#include "llvm/Support/CommandLine.h"
30#include "llvm/Support/Debug.h"
31#include "llvm/Support/Format.h"
32#include "llvm/Support/Host.h"
33#include "llvm/Support/ManagedStatic.h"
34#include "llvm/Support/MemoryBuffer.h"
35#include "llvm/Support/MemoryBuffer.h"
36#include "llvm/Support/MemoryObject.h"
37#include "llvm/Support/PrettyStackTrace.h"
38#include "llvm/Support/Signals.h"
39#include "llvm/Support/SourceMgr.h"
40#include "llvm/Support/raw_ostream.h"
41#include "llvm/Support/raw_ostream.h"
42#include "llvm/Support/system_error.h"
43#include "llvm/Target/TargetRegistry.h"
44#include "llvm/Target/TargetSelect.h"
45#include <algorithm>
46#include <cctype>
47#include <cerrno>
48#include <cstring>
49#include <vector>
50using namespace llvm;
51using namespace object;
52
53namespace {
54 cl::list<std::string>
55 InputFilenames(cl::Positional, cl::desc("<input object files>"),
56 cl::ZeroOrMore);
57
58 cl::opt<bool>
59 Disassemble("disassemble",
60 cl::desc("Display assembler mnemonics for the machine instructions"));
61 cl::alias
62 Disassembled("d", cl::desc("Alias for --disassemble"),
63 cl::aliasopt(Disassemble));
64
65 cl::opt<std::string>
66 TripleName("triple", cl::desc("Target triple to disassemble for, "
67 "see -version for available targets"));
68
69 cl::opt<std::string>
70 ArchName("arch", cl::desc("Target arch to disassemble for, "
71 "see -version for available targets"));
72
73 StringRef ToolName;
74}
75
76static const Target *GetTarget(const ObjectFile *Obj = NULL) {
77 // Figure out the target triple.
78 llvm::Triple TT("unknown-unknown-unknown");
79 if (TripleName.empty())
80 if (Obj)
81 TT.setArch(Triple::ArchType(Obj->getArch()));
82 else
83 TT.setTriple(Triple::normalize(TripleName));
84
85 if (!ArchName.empty())
86 TT.setArchName(ArchName);
87
88 TripleName = TT.str();
89
90 // Get the target specific parser.
91 std::string Error;
92 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
93 if (TheTarget)
94 return TheTarget;
95
96 errs() << ToolName << ": error: unable to get target for '" << TripleName
97 << "', see --version and --triple.\n";
98 return 0;
99}
100
101namespace {
102class StringRefMemoryObject : public MemoryObject {
103private:
104 StringRef Bytes;
105public:
106 StringRefMemoryObject(StringRef bytes) : Bytes(bytes) {}
107
108 uint64_t getBase() const { return 0; }
109 uint64_t getExtent() const { return Bytes.size(); }
110
111 int readByte(uint64_t Addr, uint8_t *Byte) const {
112 if (Addr > getExtent())
113 return -1;
114 *Byte = Bytes[Addr];
115 return 0;
116 }
117};
118}
119
120static void DumpBytes(StringRef bytes) {
121 static char hex_rep[] = "0123456789abcdef";
122 // FIXME: The real way to do this is to figure out the longest instruction
123 // and align to that size before printing. I'll fix this when I get
124 // around to outputting relocations.
125 // 15 is the longest x86 instruction
126 // 3 is for the hex rep of a byte + a space.
127 // 1 is for the null terminator.
128 enum { OutputSize = (15 * 3) + 1 };
129 char output[OutputSize];
130
131 assert(bytes.size() <= 15
132 && "DumpBytes only supports instructions of up to 15 bytes");
133 memset(output, ' ', sizeof(output));
134 unsigned index = 0;
135 for (StringRef::iterator i = bytes.begin(),
136 e = bytes.end(); i != e; ++i) {
137 output[index] = hex_rep[(*i & 0xF0) >> 4];
138 output[index + 1] = hex_rep[*i & 0xF];
139 index += 3;
140 }
141
142 output[sizeof(output) - 1] = 0;
143 outs() << output;
144}
145
146static void DisassembleInput(const StringRef &Filename) {
147 OwningPtr<MemoryBuffer> Buff;
148
149 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
150 errs() << ToolName << ": " << Filename << ": " << ec.message() << "\n";
151 return;
152 }
153
154 OwningPtr<ObjectFile> Obj(ObjectFile::createObjectFile(Buff.take()));
155
156 const Target *TheTarget = GetTarget(Obj.get());
157 if (!TheTarget) {
158 // GetTarget prints out stuff.
159 return;
160 }
161
162 outs() << '\n';
163 outs() << Filename
164 << ":\tfile format " << Obj->getFileFormatName() << "\n\n\n";
165
166 for (ObjectFile::section_iterator i = Obj->begin_sections(),
167 e = Obj->end_sections();
168 i != e; ++i) {
169 if (!i->isText())
170 continue;
171 outs() << "Disassembly of section " << i->getName() << ":\n\n";
172
173 // Set up disassembler.
174 OwningPtr<const MCAsmInfo> AsmInfo(TheTarget->createAsmInfo(TripleName));
175
176 if (!AsmInfo) {
177 errs() << "error: no assembly info for target " << TripleName << "\n";
178 return;
179 }
180
181 OwningPtr<const MCDisassembler> DisAsm(TheTarget->createMCDisassembler());
182 if (!DisAsm) {
183 errs() << "error: no disassembler for target " << TripleName << "\n";
184 return;
185 }
186
187 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
188 OwningPtr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
189 AsmPrinterVariant, *AsmInfo));
190 if (!IP) {
191 errs() << "error: no instruction printer for target " << TripleName << '\n';
192 return;
193 }
194
195 StringRef Bytes = i->getContents();
196 StringRefMemoryObject memoryObject(Bytes);
197 uint64_t Size;
198 uint64_t Index;
199
200 for (Index = 0; Index < Bytes.size(); Index += Size) {
201 MCInst Inst;
202
203# ifndef NDEBUG
204 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
205# else
206 raw_ostream &DebugOut = nulls();
207# endif
208
209 if (DisAsm->getInstruction(Inst, Size, memoryObject, Index, DebugOut)) {
210 outs() << format("%8x:\t", i->getAddress() + Index);
211 DumpBytes(StringRef(Bytes.data() + Index, Size));
212 IP->printInst(&Inst, outs());
213 outs() << "\n";
214 } else {
215 errs() << ToolName << ": warning: invalid instruction encoding\n";
216 if (Size == 0)
217 Size = 1; // skip illegible bytes
218 }
219 }
220 }
221}
222
223int main(int argc, char **argv) {
224 // Print a stack trace if we signal out.
225 sys::PrintStackTraceOnErrorSignal();
226 PrettyStackTraceProgram X(argc, argv);
227 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
228
229 // Initialize targets and assembly printers/parsers.
230 llvm::InitializeAllTargetInfos();
231 // FIXME: We shouldn't need to initialize the Target(Machine)s.
232 llvm::InitializeAllTargets();
233 llvm::InitializeAllAsmPrinters();
234 llvm::InitializeAllAsmParsers();
235 llvm::InitializeAllDisassemblers();
236
237 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
238 TripleName = Triple::normalize(TripleName);
239
240 ToolName = argv[0];
241
242 // Defaults to a.out if no filenames specified.
243 if (InputFilenames.size() == 0)
244 InputFilenames.push_back("a.out");
245
246 // -d is the only flag that is currently implemented, so just print help if
247 // it is not set.
248 if (!Disassemble) {
249 cl::PrintHelpMessage();
250 return 2;
251 }
252
253 std::for_each(InputFilenames.begin(), InputFilenames.end(),
254 DisassembleInput);
255
256 return 0;
257}