1 //===-- llvm-objdump.cpp - Object file dumping utility for llvm -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
14 //===----------------------------------------------------------------------===//
16 #include "llvm-objdump.h"
17 #include "MCFunction.h"
18 #include "llvm/Object/Archive.h"
19 #include "llvm/Object/ObjectFile.h"
20 #include "llvm/ADT/OwningPtr.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/MC/MCAsmInfo.h"
24 #include "llvm/MC/MCDisassembler.h"
25 #include "llvm/MC/MCInst.h"
26 #include "llvm/MC/MCInstPrinter.h"
27 #include "llvm/MC/MCSubtargetInfo.h"
28 #include "llvm/Support/Casting.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/FileSystem.h"
32 #include "llvm/Support/Format.h"
33 #include "llvm/Support/GraphWriter.h"
34 #include "llvm/Support/Host.h"
35 #include "llvm/Support/ManagedStatic.h"
36 #include "llvm/Support/MemoryBuffer.h"
37 #include "llvm/Support/MemoryObject.h"
38 #include "llvm/Support/PrettyStackTrace.h"
39 #include "llvm/Support/Signals.h"
40 #include "llvm/Support/SourceMgr.h"
41 #include "llvm/Support/TargetRegistry.h"
42 #include "llvm/Support/TargetSelect.h"
43 #include "llvm/Support/raw_ostream.h"
44 #include "llvm/Support/system_error.h"
48 using namespace object;
50 static cl::list<std::string>
51 InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore);
54 Disassemble("disassemble",
55 cl::desc("Display assembler mnemonics for the machine instructions"));
57 Disassembled("d", cl::desc("Alias for --disassemble"),
58 cl::aliasopt(Disassemble));
61 Relocations("r", cl::desc("Display the relocation entries in the file"));
64 MachO("macho", cl::desc("Use MachO specific object file parser"));
66 MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachO));
69 llvm::TripleName("triple", cl::desc("Target triple to disassemble for, "
70 "see -version for available targets"));
73 llvm::ArchName("arch", cl::desc("Target arch to disassemble for, "
74 "see -version for available targets"));
77 SectionHeaders("section-headers", cl::desc("Display summaries of the headers "
78 "for each section."));
80 SectionHeadersShort("headers", cl::desc("Alias for --section-headers"),
81 cl::aliasopt(SectionHeaders));
83 SectionHeadersShorter("h", cl::desc("Alias for --section-headers"),
84 cl::aliasopt(SectionHeaders));
86 static StringRef ToolName;
88 static bool error(error_code ec) {
89 if (!ec) return false;
91 outs() << ToolName << ": error reading file: " << ec.message() << ".\n";
96 static const Target *GetTarget(const ObjectFile *Obj = NULL) {
97 // Figure out the target triple.
98 llvm::Triple TT("unknown-unknown-unknown");
99 if (TripleName.empty()) {
101 TT.setArch(Triple::ArchType(Obj->getArch()));
103 TT.setTriple(Triple::normalize(TripleName));
105 if (!ArchName.empty())
106 TT.setArchName(ArchName);
108 TripleName = TT.str();
110 // Get the target specific parser.
112 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
116 errs() << ToolName << ": error: unable to get target for '" << TripleName
117 << "', see --version and --triple.\n";
121 void llvm::DumpBytes(StringRef bytes) {
122 static const char hex_rep[] = "0123456789abcdef";
123 // 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];
132 assert(bytes.size() <= 15
133 && "DumpBytes only supports instructions of up to 15 bytes");
134 memset(output, ' ', sizeof(output));
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];
143 output[sizeof(output) - 1] = 0;
147 static void DisassembleObject(const ObjectFile *Obj) {
148 const Target *TheTarget = GetTarget(Obj);
150 // GetTarget prints out stuff.
155 outs() << Obj->getFileName()
156 << ":\tfile format " << Obj->getFileFormatName() << "\n\n";
159 for (section_iterator i = Obj->begin_sections(),
160 e = Obj->end_sections();
161 i != e; i.increment(ec)) {
162 if (error(ec)) break;
164 if (error(i->isText(text))) break;
167 // Make a list of all the symbols in this section.
168 std::vector<std::pair<uint64_t, StringRef> > Symbols;
169 for (symbol_iterator si = Obj->begin_symbols(),
170 se = Obj->end_symbols();
171 si != se; si.increment(ec)) {
173 if (!error(i->containsSymbol(*si, contains)) && contains) {
175 if (error(si->getOffset(Address))) break;
177 if (error(si->getName(Name))) break;
178 Symbols.push_back(std::make_pair(Address, Name));
182 // Sort the symbols by address, just in case they didn't come in that way.
183 array_pod_sort(Symbols.begin(), Symbols.end());
186 if (error(i->getName(name))) break;
187 outs() << "Disassembly of section " << name << ':';
189 // If the section has no symbols just insert a dummy one and disassemble
190 // the whole section.
192 Symbols.push_back(std::make_pair(0, name));
194 // Set up disassembler.
195 OwningPtr<const MCAsmInfo> AsmInfo(TheTarget->createMCAsmInfo(TripleName));
198 errs() << "error: no assembly info for target " << TripleName << "\n";
202 OwningPtr<const MCSubtargetInfo> STI(
203 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
206 errs() << "error: no subtarget info for target " << TripleName << "\n";
210 OwningPtr<const MCDisassembler> DisAsm(
211 TheTarget->createMCDisassembler(*STI));
213 errs() << "error: no disassembler for target " << TripleName << "\n";
217 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
218 OwningPtr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
219 AsmPrinterVariant, *AsmInfo, *STI));
221 errs() << "error: no instruction printer for target " << TripleName
227 if (error(i->getContents(Bytes))) break;
228 StringRefMemoryObject memoryObject(Bytes);
232 if (error(i->getSize(SectSize))) break;
234 // 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";
241 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
243 raw_ostream &DebugOut = nulls();
246 for (Index = Start; Index < End; Index += Size) {
249 if (DisAsm->getInstruction(Inst, Size, memoryObject, Index,
250 DebugOut, nulls())) {
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(), "");
258 errs() << ToolName << ": warning: invalid instruction encoding\n";
260 Size = 1; // skip illegible bytes
267 static void PrintRelocations(const ObjectFile *o) {
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())
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;
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";
294 static void PrintSectionHeaders(const ObjectFile *o) {
295 outs() << "Sections:\n"
296 "Idx Name Size Address Type\n";
299 for (section_iterator si = o->begin_sections(), se = o->end_sections();
300 si != se; si.increment(ec)) {
301 if (error(ec)) return;
303 if (error(si->getName(Name))) return;
305 if (error(si->getAddress(Address))) return;
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" : ""));
314 outs() << format("%3d %-13s %09"PRIx64" %017"PRIx64" %s\n", i, Name.str().c_str(), Size,
315 Address, Type.c_str());
320 static void DumpObject(const ObjectFile *o) {
322 DisassembleObject(o);
326 PrintSectionHeaders(o);
329 /// @brief Dump each object file in \a a;
330 static 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()
339 if (ObjectFile *o = dyn_cast<ObjectFile>(child.get()))
342 errs() << ToolName << ": '" << a->getFileName() << "': "
343 << "Unrecognized file type.\n";
347 /// @brief Open file and figure out how to dump it.
348 static 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";
355 if (MachO && Disassemble) {
356 DisassembleInputMachO(file);
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";
367 if (Archive *a = dyn_cast<Archive>(binary.get())) {
369 } else if (ObjectFile *o = dyn_cast<ObjectFile>(binary.get())) {
372 errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n";
376 int 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.
382 // Initialize targets and assembly printers/parsers.
383 llvm::InitializeAllTargetInfos();
384 llvm::InitializeAllTargetMCs();
385 llvm::InitializeAllAsmParsers();
386 llvm::InitializeAllDisassemblers();
388 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
389 TripleName = Triple::normalize(TripleName);
393 // Defaults to a.out if no filenames specified.
394 if (InputFilenames.size() == 0)
395 InputFilenames.push_back("a.out");
397 if (!Disassemble && !Relocations && !SectionHeaders) {
398 cl::PrintHelpMessage();
402 std::for_each(InputFilenames.begin(), InputFilenames.end(),