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 "llvm/ADT/OwningPtr.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/DebugInfo/DIContext.h"
20 #include "llvm/MC/MCAsmInfo.h"
21 #include "llvm/MC/MCDisassembler.h"
22 #include "llvm/MC/MCInst.h"
23 #include "llvm/MC/MCInstPrinter.h"
24 #include "llvm/MC/MCInstrAnalysis.h"
25 #include "llvm/MC/MCInstrDesc.h"
26 #include "llvm/MC/MCInstrInfo.h"
27 #include "llvm/MC/MCRegisterInfo.h"
28 #include "llvm/MC/MCSubtargetInfo.h"
29 #include "llvm/Object/MachO.h"
30 #include "llvm/Support/Casting.h"
31 #include "llvm/Support/CommandLine.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/Format.h"
34 #include "llvm/Support/GraphWriter.h"
35 #include "llvm/Support/MachO.h"
36 #include "llvm/Support/MemoryBuffer.h"
37 #include "llvm/Support/TargetRegistry.h"
38 #include "llvm/Support/TargetSelect.h"
39 #include "llvm/Support/raw_ostream.h"
40 #include "llvm/Support/system_error.h"
44 using namespace object;
47 UseDbg("g", cl::desc("Print line information from debug info if available"));
49 static cl::opt<std::string>
50 DSYMFile("dsym", cl::desc("Use .dSYM file for debug info"));
52 static const Target *GetTarget(const MachOObjectFile *MachOObj) {
53 // Figure out the target triple.
54 if (TripleName.empty()) {
55 llvm::Triple TT("unknown-unknown-unknown");
56 TT.setArch(Triple::ArchType(MachOObj->getArch()));
57 TripleName = TT.str();
60 // Get the target specific parser.
62 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
66 errs() << "llvm-objdump: error: unable to get target for '" << TripleName
67 << "', see --version and --triple.\n";
72 bool operator()(const SymbolRef &A, const SymbolRef &B) {
73 SymbolRef::Type AType, BType;
77 uint64_t AAddr, BAddr;
78 if (AType != SymbolRef::ST_Function)
82 if (BType != SymbolRef::ST_Function)
90 // Types for the storted data in code table that is built before disassembly
91 // and the predicate function to sort them.
92 typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
93 typedef std::vector<DiceTableEntry> DiceTable;
94 typedef DiceTable::iterator dice_table_iterator;
97 compareDiceTableEntries(const DiceTableEntry i,
98 const DiceTableEntry j) {
99 return i.first == j.first;
102 static void DumpDataInCode(const char *bytes, uint64_t Size,
103 unsigned short Kind) {
110 Value = bytes[3] << 24 |
114 outs() << "\t.long " << Value;
117 Value = bytes[1] << 8 |
119 outs() << "\t.short " << Value;
123 outs() << "\t.byte " << Value;
126 outs() << "\t@ KIND_DATA\n";
128 case macho::JumpTable8:
130 outs() << "\t.byte " << Value << "\t@ KIND_JUMP_TABLE8";
132 case macho::JumpTable16:
133 Value = bytes[1] << 8 |
135 outs() << "\t.short " << Value << "\t@ KIND_JUMP_TABLE16";
137 case macho::JumpTable32:
138 Value = bytes[3] << 24 |
142 outs() << "\t.long " << Value << "\t@ KIND_JUMP_TABLE32";
145 outs() << "\t@ data in code kind = " << Kind << "\n";
151 getSectionsAndSymbols(const macho::Header Header,
152 MachOObjectFile *MachOObj,
153 std::vector<SectionRef> &Sections,
154 std::vector<SymbolRef> &Symbols,
155 SmallVectorImpl<uint64_t> &FoundFns,
156 uint64_t &BaseSegmentAddress) {
158 for (symbol_iterator SI = MachOObj->begin_symbols(),
159 SE = MachOObj->end_symbols(); SI != SE; SI.increment(ec))
160 Symbols.push_back(*SI);
162 for (section_iterator SI = MachOObj->begin_sections(),
163 SE = MachOObj->end_sections(); SI != SE; SI.increment(ec)) {
166 SR.getName(SectName);
167 Sections.push_back(*SI);
170 MachOObjectFile::LoadCommandInfo Command =
171 MachOObj->getFirstLoadCommandInfo();
172 bool BaseSegmentAddressSet = false;
173 for (unsigned i = 0; ; ++i) {
174 if (Command.C.Type == macho::LCT_FunctionStarts) {
175 // We found a function starts segment, parse the addresses for later
177 macho::LinkeditDataLoadCommand LLC =
178 MachOObj->getLinkeditDataLoadCommand(Command);
180 MachOObj->ReadULEB128s(LLC.DataOffset, FoundFns);
182 else if (Command.C.Type == macho::LCT_Segment) {
183 macho::SegmentLoadCommand SLC =
184 MachOObj->getSegmentLoadCommand(Command);
185 StringRef SegName = SLC.Name;
186 if(!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
187 BaseSegmentAddressSet = true;
188 BaseSegmentAddress = SLC.VMAddress;
192 if (i == Header.NumLoadCommands - 1)
195 Command = MachOObj->getNextLoadCommandInfo(Command);
199 static void DisassembleInputMachO2(StringRef Filename,
200 MachOObjectFile *MachOOF);
202 void llvm::DisassembleInputMachO(StringRef Filename) {
203 OwningPtr<MemoryBuffer> Buff;
205 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
206 errs() << "llvm-objdump: " << Filename << ": " << ec.message() << "\n";
210 OwningPtr<MachOObjectFile> MachOOF(static_cast<MachOObjectFile*>(
211 ObjectFile::createMachOObjectFile(Buff.take())));
213 DisassembleInputMachO2(Filename, MachOOF.get());
216 static void DisassembleInputMachO2(StringRef Filename,
217 MachOObjectFile *MachOOF) {
218 const Target *TheTarget = GetTarget(MachOOF);
220 // GetTarget prints out stuff.
223 OwningPtr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
224 OwningPtr<MCInstrAnalysis>
225 InstrAnalysis(TheTarget->createMCInstrAnalysis(InstrInfo.get()));
227 // Set up disassembler.
228 OwningPtr<const MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
229 OwningPtr<const MCAsmInfo> AsmInfo(
230 TheTarget->createMCAsmInfo(*MRI, TripleName));
231 OwningPtr<const MCSubtargetInfo>
232 STI(TheTarget->createMCSubtargetInfo(TripleName, "", ""));
233 OwningPtr<const MCDisassembler> DisAsm(TheTarget->createMCDisassembler(*STI));
234 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
235 OwningPtr<MCInstPrinter>
236 IP(TheTarget->createMCInstPrinter(AsmPrinterVariant, *AsmInfo, *InstrInfo,
239 if (!InstrAnalysis || !AsmInfo || !STI || !DisAsm || !IP) {
240 errs() << "error: couldn't initialize disassembler for target "
241 << TripleName << '\n';
245 outs() << '\n' << Filename << ":\n\n";
247 macho::Header Header = MachOOF->getHeader();
249 // FIXME: FoundFns isn't used anymore. Using symbols/LC_FUNCTION_STARTS to
250 // determine function locations will eventually go in MCObjectDisassembler.
251 // FIXME: Using the -cfg command line option, this code used to be able to
252 // annotate relocations with the referenced symbol's name, and if this was
253 // inside a __[cf]string section, the data it points to. This is now replaced
254 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
255 std::vector<SectionRef> Sections;
256 std::vector<SymbolRef> Symbols;
257 SmallVector<uint64_t, 8> FoundFns;
258 uint64_t BaseSegmentAddress;
260 getSectionsAndSymbols(Header, MachOOF, Sections, Symbols, FoundFns,
263 // Make a copy of the unsorted symbol list. FIXME: duplication
264 std::vector<SymbolRef> UnsortedSymbols(Symbols);
265 // Sort the symbols by address, just in case they didn't come in that way.
266 std::sort(Symbols.begin(), Symbols.end(), SymbolSorter());
268 // Build a data in code table that is sorted on by the address of each entry.
269 uint64_t BaseAddress = 0;
270 if (Header.FileType == macho::HFT_Object)
271 Sections[0].getAddress(BaseAddress);
273 BaseAddress = BaseSegmentAddress;
276 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
277 DI != DE; DI.increment(ec)){
279 DI->getOffset(Offset);
280 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
282 array_pod_sort(Dices.begin(), Dices.end());
285 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
287 raw_ostream &DebugOut = nulls();
290 OwningPtr<DIContext> diContext;
291 ObjectFile *DbgObj = MachOOF;
292 // Try to find debug info and set up the DIContext for it.
294 // A separate DSym file path was specified, parse it as a macho file,
295 // get the sections and supply it to the section name parsing machinery.
296 if (!DSYMFile.empty()) {
297 OwningPtr<MemoryBuffer> Buf;
298 if (error_code ec = MemoryBuffer::getFileOrSTDIN(DSYMFile, Buf)) {
299 errs() << "llvm-objdump: " << Filename << ": " << ec.message() << '\n';
302 DbgObj = ObjectFile::createMachOObjectFile(Buf.take());
305 // Setup the DIContext
306 diContext.reset(DIContext::getDWARFContext(DbgObj));
309 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
311 bool SectIsText = false;
312 Sections[SectIdx].isText(SectIsText);
313 if (SectIsText == false)
317 if (Sections[SectIdx].getName(SectName) ||
318 SectName != "__text")
319 continue; // Skip non-text sections
321 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
323 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
324 if (SegmentName != "__TEXT")
328 Sections[SectIdx].getContents(Bytes);
329 StringRefMemoryObject memoryObject(Bytes);
330 bool symbolTableWorked = false;
332 // Parse relocations.
333 std::vector<std::pair<uint64_t, SymbolRef> > Relocs;
335 for (relocation_iterator RI = Sections[SectIdx].begin_relocations(),
336 RE = Sections[SectIdx].end_relocations(); RI != RE; RI.increment(ec)) {
337 uint64_t RelocOffset, SectionAddress;
338 RI->getOffset(RelocOffset);
339 Sections[SectIdx].getAddress(SectionAddress);
340 RelocOffset -= SectionAddress;
342 symbol_iterator RelocSym = RI->getSymbol();
344 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
346 array_pod_sort(Relocs.begin(), Relocs.end());
348 // Disassemble symbol by symbol.
349 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
351 Symbols[SymIdx].getName(SymName);
354 Symbols[SymIdx].getType(ST);
355 if (ST != SymbolRef::ST_Function)
358 // Make sure the symbol is defined in this section.
359 bool containsSym = false;
360 Sections[SectIdx].containsSymbol(Symbols[SymIdx], containsSym);
364 // Start at the address of the symbol relative to the section's address.
365 uint64_t SectionAddress = 0;
367 Sections[SectIdx].getAddress(SectionAddress);
368 Symbols[SymIdx].getAddress(Start);
369 Start -= SectionAddress;
371 // Stop disassembling either at the beginning of the next symbol or at
372 // the end of the section.
373 bool containsNextSym = false;
374 uint64_t NextSym = 0;
375 uint64_t NextSymIdx = SymIdx+1;
376 while (Symbols.size() > NextSymIdx) {
377 SymbolRef::Type NextSymType;
378 Symbols[NextSymIdx].getType(NextSymType);
379 if (NextSymType == SymbolRef::ST_Function) {
380 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx],
382 Symbols[NextSymIdx].getAddress(NextSym);
383 NextSym -= SectionAddress;
390 Sections[SectIdx].getSize(SectSize);
391 uint64_t End = containsNextSym ? NextSym : SectSize;
394 symbolTableWorked = true;
396 outs() << SymName << ":\n";
398 for (uint64_t Index = Start; Index < End; Index += Size) {
401 uint64_t SectAddress = 0;
402 Sections[SectIdx].getAddress(SectAddress);
403 outs() << format("%8" PRIx64 ":\t", SectAddress + Index);
405 // Check the data in code table here to see if this is data not an
406 // instruction to be disassembled.
408 Dice.push_back(std::make_pair(SectAddress + Index, DiceRef()));
409 dice_table_iterator DTI = std::search(Dices.begin(), Dices.end(),
410 Dice.begin(), Dice.end(),
411 compareDiceTableEntries);
412 if (DTI != Dices.end()){
414 DTI->second.getLength(Length);
415 DumpBytes(StringRef(Bytes.data() + Index, Length));
417 DTI->second.getKind(Kind);
418 DumpDataInCode(Bytes.data() + Index, Length, Kind);
422 if (DisAsm->getInstruction(Inst, Size, memoryObject, Index,
423 DebugOut, nulls())) {
424 DumpBytes(StringRef(Bytes.data() + Index, Size));
425 IP->printInst(&Inst, outs(), "");
430 diContext->getLineInfoForAddress(SectAddress + Index);
431 // Print valid line info if it changed.
432 if (dli != lastLine && dli.getLine() != 0)
433 outs() << "\t## " << dli.getFileName() << ':'
434 << dli.getLine() << ':' << dli.getColumn();
439 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
441 Size = 1; // skip illegible bytes
445 if (!symbolTableWorked) {
446 // Reading the symbol table didn't work, disassemble the whole section.
447 uint64_t SectAddress;
448 Sections[SectIdx].getAddress(SectAddress);
450 Sections[SectIdx].getSize(SectSize);
452 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
455 if (DisAsm->getInstruction(Inst, InstSize, memoryObject, Index,
456 DebugOut, nulls())) {
457 outs() << format("%8" PRIx64 ":\t", SectAddress + Index);
458 DumpBytes(StringRef(Bytes.data() + Index, InstSize));
459 IP->printInst(&Inst, outs(), "");
462 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
464 InstSize = 1; // skip illegible bytes