1 //===-- MachODump.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 file implements the MachO-specific dumper for llvm-objdump.
12 //===----------------------------------------------------------------------===//
14 #include "llvm-objdump.h"
15 #include "MCFunction.h"
16 #include "llvm/Support/MachO.h"
17 #include "llvm/Object/MachOObject.h"
18 #include "llvm/ADT/OwningPtr.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/MC/MCAsmInfo.h"
22 #include "llvm/MC/MCDisassembler.h"
23 #include "llvm/MC/MCInst.h"
24 #include "llvm/MC/MCInstPrinter.h"
25 #include "llvm/MC/MCInstrAnalysis.h"
26 #include "llvm/MC/MCInstrDesc.h"
27 #include "llvm/MC/MCInstrInfo.h"
28 #include "llvm/MC/MCSubtargetInfo.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/Format.h"
32 #include "llvm/Support/GraphWriter.h"
33 #include "llvm/Support/MemoryBuffer.h"
34 #include "llvm/Support/TargetRegistry.h"
35 #include "llvm/Support/TargetSelect.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include "llvm/Support/system_error.h"
41 using namespace object;
44 CFG("cfg", cl::desc("Create a CFG for every symbol in the object file and"
45 "write it to a graphviz file (MachO-only)"));
47 static const Target *GetTarget(const MachOObject *MachOObj) {
48 // Figure out the target triple.
49 llvm::Triple TT("unknown-unknown-unknown");
50 switch (MachOObj->getHeader().CPUType) {
51 case llvm::MachO::CPUTypeI386:
52 TT.setArch(Triple::ArchType(Triple::x86));
54 case llvm::MachO::CPUTypeX86_64:
55 TT.setArch(Triple::ArchType(Triple::x86_64));
57 case llvm::MachO::CPUTypeARM:
58 TT.setArch(Triple::ArchType(Triple::arm));
60 case llvm::MachO::CPUTypePowerPC:
61 TT.setArch(Triple::ArchType(Triple::ppc));
63 case llvm::MachO::CPUTypePowerPC64:
64 TT.setArch(Triple::ArchType(Triple::ppc64));
68 TripleName = TT.str();
70 // Get the target specific parser.
72 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
76 errs() << "llvm-objdump: error: unable to get target for '" << TripleName
77 << "', see --version and --triple.\n";
87 uint64_t RelocTableOffset;
94 bool operator<(const Symbol &RHS) const { return Value < RHS.Value; }
97 static void DumpAddress(uint64_t Address, ArrayRef<Section> Sections,
98 MachOObject *MachOObj, raw_ostream &OS) {
99 for (unsigned i = 0; i != Sections.size(); ++i) {
100 uint64_t addr = Address-Sections[i].Address;
101 if (Sections[i].Address <= Address &&
102 Sections[i].Address + Sections[i].Size > Address) {
103 StringRef bytes = MachOObj->getData(Sections[i].Offset,
105 if (!strcmp(Sections[i].Name, "__cstring"))
106 OS << '"' << bytes.substr(addr, bytes.find('\0', addr)) << '"';
107 if (!strcmp(Sections[i].Name, "__cfstring"))
108 OS << "@\"" << bytes.substr(addr, bytes.find('\0', addr)) << '"';
113 void llvm::DisassembleInputMachO(StringRef Filename) {
114 OwningPtr<MemoryBuffer> Buff;
116 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
117 errs() << "llvm-objdump: " << Filename << ": " << ec.message() << "\n";
121 OwningPtr<MachOObject> MachOObj(MachOObject::LoadFromBuffer(Buff.take()));
123 const Target *TheTarget = GetTarget(MachOObj.get());
125 // GetTarget prints out stuff.
128 const MCInstrInfo *InstrInfo = TheTarget->createMCInstrInfo();
129 OwningPtr<MCInstrAnalysis>
130 InstrAnalysis(TheTarget->createMCInstrAnalysis(InstrInfo));
132 // Set up disassembler.
133 OwningPtr<const MCAsmInfo> AsmInfo(TheTarget->createMCAsmInfo(TripleName));
136 errs() << "error: no assembly info for target " << TripleName << "\n";
140 OwningPtr<const MCSubtargetInfo>
141 STI(TheTarget->createMCSubtargetInfo(TripleName, "", ""));
144 errs() << "error: no subtarget info for target " << TripleName << "\n";
148 OwningPtr<const MCDisassembler> DisAsm(TheTarget->createMCDisassembler(*STI));
150 errs() << "error: no disassembler for target " << TripleName << "\n";
154 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
155 OwningPtr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
156 AsmPrinterVariant, *AsmInfo, *STI));
158 errs() << "error: no instruction printer for target " << TripleName << '\n';
163 outs() << Filename << ":\n\n";
165 const macho::Header &Header = MachOObj->getHeader();
167 const MachOObject::LoadCommandInfo *SymtabLCI = 0;
168 for (unsigned i = 0; i != Header.NumLoadCommands; ++i) {
169 const MachOObject::LoadCommandInfo &LCI = MachOObj->getLoadCommandInfo(i);
170 switch (LCI.Command.Type) {
171 case macho::LCT_Symtab:
177 // Read and register the symbol table data.
178 InMemoryStruct<macho::SymtabLoadCommand> SymtabLC;
179 MachOObj->ReadSymtabLoadCommand(*SymtabLCI, SymtabLC);
180 MachOObj->RegisterStringTable(*SymtabLC);
182 std::vector<Section> Sections;
183 std::vector<Symbol> Symbols;
184 std::vector<Symbol> UnsortedSymbols; // FIXME: duplication
185 SmallVector<uint64_t, 8> FoundFns;
187 for (unsigned i = 0; i != Header.NumLoadCommands; ++i) {
188 const MachOObject::LoadCommandInfo &LCI = MachOObj->getLoadCommandInfo(i);
189 if (LCI.Command.Type == macho::LCT_Segment) {
190 InMemoryStruct<macho::SegmentLoadCommand> SegmentLC;
191 MachOObj->ReadSegmentLoadCommand(LCI, SegmentLC);
193 for (unsigned SectNum = 0; SectNum != SegmentLC->NumSections; ++SectNum) {
194 InMemoryStruct<macho::Section> Sect;
195 MachOObj->ReadSection(LCI, SectNum, Sect);
198 memcpy(S.Name, Sect->Name, 16);
199 S.Address = Sect->Address;
201 S.Offset = Sect->Offset;
202 S.NumRelocs = Sect->NumRelocationTableEntries;
203 S.RelocTableOffset = Sect->RelocationTableOffset;
204 Sections.push_back(S);
206 for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) {
207 InMemoryStruct<macho::SymbolTableEntry> STE;
208 MachOObj->ReadSymbolTableEntry(SymtabLC->SymbolTableOffset, i, STE);
211 S.StringIndex = STE->StringIndex;
212 S.SectionIndex = STE->SectionIndex;
213 S.Value = STE->Value;
214 Symbols.push_back(S);
215 UnsortedSymbols.push_back(Symbols.back());
218 } else if (LCI.Command.Type == macho::LCT_Segment64) {
219 InMemoryStruct<macho::Segment64LoadCommand> Segment64LC;
220 MachOObj->ReadSegment64LoadCommand(LCI, Segment64LC);
222 for (unsigned SectNum = 0; SectNum != Segment64LC->NumSections; ++SectNum) {
223 InMemoryStruct<macho::Section64> Sect64;
224 MachOObj->ReadSection64(LCI, SectNum, Sect64);
227 memcpy(S.Name, Sect64->Name, 16);
228 S.Address = Sect64->Address;
229 S.Size = Sect64->Size;
230 S.Offset = Sect64->Offset;
231 S.NumRelocs = Sect64->NumRelocationTableEntries;
232 S.RelocTableOffset = Sect64->RelocationTableOffset;
233 Sections.push_back(S);
235 for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) {
236 InMemoryStruct<macho::Symbol64TableEntry> STE;
237 MachOObj->ReadSymbol64TableEntry(SymtabLC->SymbolTableOffset, i, STE);
240 S.StringIndex = STE->StringIndex;
241 S.SectionIndex = STE->SectionIndex;
242 S.Value = STE->Value;
243 Symbols.push_back(S);
244 UnsortedSymbols.push_back(Symbols.back());
247 } else if (LCI.Command.Type == macho::LCT_FunctionStarts) {
248 InMemoryStruct<macho::LinkeditDataLoadCommand> LLC;
249 MachOObj->ReadLinkeditDataLoadCommand(LCI, LLC);
251 MachOObj->ReadULEB128s(LLC->DataOffset, FoundFns);
255 std::map<uint64_t, MCFunction*> FunctionMap;
257 // Sort the symbols by address, just in case they didn't come in that way.
258 array_pod_sort(Symbols.begin(), Symbols.end());
261 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
263 raw_ostream &DebugOut = nulls();
266 SmallVector<MCFunction, 16> Functions;
268 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
269 if (strcmp(Sections[SectIdx].Name, "__text"))
272 uint64_t VMAddr = Sections[SectIdx].Address - Sections[SectIdx].Offset;
273 for (unsigned i = 0, e = FoundFns.size(); i != e; ++i)
274 FunctionMap.insert(std::pair<uint64_t,MCFunction*>(FoundFns[i]+VMAddr,0));
276 StringRef Bytes = MachOObj->getData(Sections[SectIdx].Offset,
277 Sections[SectIdx].Size);
278 StringRefMemoryObject memoryObject(Bytes);
279 bool symbolTableWorked = false;
281 std::vector<std::pair<uint64_t, uint32_t> > Relocs;
282 for (unsigned j = 0; j != Sections[SectIdx].NumRelocs; ++j) {
283 InMemoryStruct<macho::RelocationEntry> RE;
284 MachOObj->ReadRelocationEntry(Sections[SectIdx].RelocTableOffset, j, RE);
285 Relocs.push_back(std::make_pair(RE->Word0, RE->Word1 & 0xffffff));
287 array_pod_sort(Relocs.begin(), Relocs.end());
289 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
290 if ((unsigned)Symbols[SymIdx].SectionIndex - 1 != SectIdx)
293 uint64_t Start = Symbols[SymIdx].Value - Sections[SectIdx].Address;
294 uint64_t End = (SymIdx+1 == Symbols.size() ||
295 Symbols[SymIdx].SectionIndex != Symbols[SymIdx+1].SectionIndex) ?
296 Sections[SectIdx].Size :
297 Symbols[SymIdx+1].Value - Sections[SectIdx].Address;
303 symbolTableWorked = true;
306 outs() << MachOObj->getStringAtIndex(Symbols[SymIdx].StringIndex)
308 for (uint64_t Index = Start; Index < End; Index += Size) {
311 if (DisAsm->getInstruction(Inst, Size, memoryObject, Index,
312 DebugOut, nulls())) {
313 outs() << format("%8llx:\t", Sections[SectIdx].Address + Index);
314 DumpBytes(StringRef(Bytes.data() + Index, Size));
315 IP->printInst(&Inst, outs(), "");
318 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
320 Size = 1; // skip illegible bytes
324 // Create CFG and use it for disassembly.
325 SmallVector<uint64_t, 16> Calls;
327 MCFunction::createFunctionFromMC(
328 MachOObj->getStringAtIndex(Symbols[SymIdx].StringIndex),
330 memoryObject, Start, End,
331 InstrAnalysis.get(), DebugOut,
334 Functions.push_back(f);
335 FunctionMap[Start] = &Functions.back();
337 for (unsigned i = 0, e = Calls.size(); i != e; ++i)
338 FunctionMap.insert(std::pair<uint64_t, MCFunction*>(Calls[i], 0));
343 if (!symbolTableWorked) {
344 // Create CFG and use it for disassembly.
345 SmallVector<uint64_t, 16> Calls;
347 MCFunction::createFunctionFromMC("__TEXT", DisAsm.get(),
348 memoryObject, 0, Sections[SectIdx].Size,
349 InstrAnalysis.get(), DebugOut,
352 Functions.push_back(f);
353 FunctionMap[Sections[SectIdx].Offset] = &Functions.back();
355 for (unsigned i = 0, e = Calls.size(); i != e; ++i)
356 FunctionMap.insert(std::pair<uint64_t, MCFunction*>(Calls[i], 0));
358 for (std::map<uint64_t, MCFunction*>::iterator mi = FunctionMap.begin(),
359 me = FunctionMap.end(); mi != me; ++mi)
360 if (mi->second == 0) {
361 SmallVector<uint64_t, 16> Calls;
363 MCFunction::createFunctionFromMC("unknown", DisAsm.get(),
364 memoryObject, mi->first,
365 Sections[SectIdx].Size,
366 InstrAnalysis.get(), DebugOut,
368 Functions.push_back(f);
369 mi->second = &Functions.back();
370 for (unsigned i = 0, e = Calls.size(); i != e; ++i)
371 if (FunctionMap.insert(std::pair<uint64_t, MCFunction*>(Calls[i],0))
373 mi = FunctionMap.begin();
376 DenseSet<uint64_t> PrintedBlocks;
377 for (unsigned ffi = 0, ffe = Functions.size(); ffi != ffe; ++ffi) {
378 MCFunction &f = Functions[ffi];
379 for (MCFunction::iterator fi = f.begin(), fe = f.end(); fi != fe; ++fi){
380 if (!PrintedBlocks.insert(fi->first).second)
382 bool hasPreds = FunctionMap.find(fi->first) != FunctionMap.end();
384 // Only print blocks that have predecessors.
386 for (MCFunction::iterator pi = f.begin(), pe = f.end(); pi != pe;
388 if (pi->second.contains(fi->first)) {
394 if (!hasPreds && fi != f.begin()) {
395 uint64_t End = llvm::next(fi) == fe ? Sections[SectIdx].Size :
396 llvm::next(fi)->first;
397 outs() << "# " << End-fi->first << " bytes of data:\n";
398 for (unsigned pos = fi->first; pos != End; ++pos) {
399 outs() << format("%8x:\t", Sections[SectIdx].Address + pos);
400 DumpBytes(StringRef(Bytes.data() + pos, 1));
401 outs() << format("\t.byte 0x%02x\n", (uint8_t)Bytes[pos]);
406 if (fi->second.contains(fi->first))
407 outs() << "# Loop begin:\n";
409 for (unsigned ii = 0, ie = fi->second.getInsts().size(); ii != ie;
411 const MCDecodedInst &Inst = fi->second.getInsts()[ii];
412 if (FunctionMap.find(Sections[SectIdx].Address + Inst.Address) !=
414 outs() << FunctionMap[Sections[SectIdx].Address + Inst.Address]->
416 outs() << format("%8llx:\t", Sections[SectIdx].Address +
418 DumpBytes(StringRef(Bytes.data() + Inst.Address, Inst.Size));
420 if (fi->second.contains(fi->first))
422 IP->printInst(&Inst.Inst, outs(), "");
423 for (unsigned j = 0; j != Relocs.size(); ++j)
424 if (Relocs[j].first >= Sections[SectIdx].Address + Inst.Address &&
425 Relocs[j].first < Sections[SectIdx].Address + Inst.Address +
428 << MachOObj->getStringAtIndex(
429 UnsortedSymbols[Relocs[j].second].StringIndex)
431 DumpAddress(UnsortedSymbols[Relocs[j].second].Value, Sections,
432 MachOObj.get(), outs());
434 uint64_t targ = InstrAnalysis->evaluateBranch(Inst.Inst,
438 DumpAddress(targ, Sections, MachOObj.get(), outs());
444 // Start a new dot file.
446 raw_fd_ostream Out((f.getName().str() + ".dot").c_str(), Error);
447 if (!Error.empty()) {
448 errs() << "llvm-objdump: warning: " << Error << '\n';
452 Out << "digraph " << f.getName() << " {\n";
453 Out << "graph [ rankdir = \"LR\" ];\n";
454 for (MCFunction::iterator i = f.begin(), e = f.end(); i != e; ++i) {
455 bool hasPreds = false;
456 // Only print blocks that have predecessors.
458 for (MCFunction::iterator pi = f.begin(), pe = f.end(); pi != pe;
460 if (pi->second.contains(i->first)) {
465 if (!hasPreds && i != f.begin())
468 Out << '"' << i->first << "\" [ label=\"<a>";
469 // Print instructions.
470 for (unsigned ii = 0, ie = i->second.getInsts().size(); ii != ie;
472 // Escape special chars and print the instruction in mnemonic form.
474 raw_string_ostream OS(Str);
475 IP->printInst(&i->second.getInsts()[ii].Inst, OS, "");
476 Out << DOT::EscapeString(OS.str()) << '|';
478 Out << "<o>\" shape=\"record\" ];\n";
481 for (MCBasicBlock::succ_iterator si = i->second.succ_begin(),
482 se = i->second.succ_end(); si != se; ++si)
483 Out << i->first << ":o -> " << *si <<":a\n";