Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 1 | //===-- MachODump.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 file implements the MachO-specific dumper for llvm-objdump. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm-objdump.h" |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/STLExtras.h" |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringExtras.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Triple.h" |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/DIContext.h" |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCAsmInfo.h" |
Lang Hames | a1bc0f5 | 2014-04-15 04:40:56 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCContext.h" |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 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" |
Jim Grosbach | fd93a59 | 2012-03-05 19:33:20 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCRegisterInfo.h" |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCSubtargetInfo.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 29 | #include "llvm/Object/MachO.h" |
Rafael Espindola | 9b70925 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Casting.h" |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 31 | #include "llvm/Support/CommandLine.h" |
| 32 | #include "llvm/Support/Debug.h" |
| 33 | #include "llvm/Support/Format.h" |
| 34 | #include "llvm/Support/GraphWriter.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 35 | #include "llvm/Support/MachO.h" |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 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" |
| 41 | #include <algorithm> |
| 42 | #include <cstring> |
| 43 | using namespace llvm; |
| 44 | using namespace object; |
| 45 | |
| 46 | static cl::opt<bool> |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 47 | UseDbg("g", cl::desc("Print line information from debug info if available")); |
| 48 | |
| 49 | static cl::opt<std::string> |
| 50 | DSYMFile("dsym", cl::desc("Use .dSYM file for debug info")); |
| 51 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 52 | static const Target *GetTarget(const MachOObjectFile *MachOObj) { |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 53 | // Figure out the target triple. |
Cameron Zwarich | 88cc16a | 2012-02-03 06:35:22 +0000 | [diff] [blame] | 54 | if (TripleName.empty()) { |
| 55 | llvm::Triple TT("unknown-unknown-unknown"); |
Rafael Espindola | 93f4a62 | 2013-04-11 03:34:37 +0000 | [diff] [blame] | 56 | TT.setArch(Triple::ArchType(MachOObj->getArch())); |
Cameron Zwarich | 88cc16a | 2012-02-03 06:35:22 +0000 | [diff] [blame] | 57 | TripleName = TT.str(); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 60 | // Get the target specific parser. |
| 61 | std::string Error; |
| 62 | const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error); |
| 63 | if (TheTarget) |
| 64 | return TheTarget; |
| 65 | |
| 66 | errs() << "llvm-objdump: error: unable to get target for '" << TripleName |
| 67 | << "', see --version and --triple.\n"; |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame^] | 68 | return nullptr; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 71 | struct SymbolSorter { |
| 72 | bool operator()(const SymbolRef &A, const SymbolRef &B) { |
| 73 | SymbolRef::Type AType, BType; |
| 74 | A.getType(AType); |
| 75 | B.getType(BType); |
| 76 | |
| 77 | uint64_t AAddr, BAddr; |
| 78 | if (AType != SymbolRef::ST_Function) |
| 79 | AAddr = 0; |
| 80 | else |
| 81 | A.getAddress(AAddr); |
| 82 | if (BType != SymbolRef::ST_Function) |
| 83 | BAddr = 0; |
| 84 | else |
| 85 | B.getAddress(BAddr); |
| 86 | return AAddr < BAddr; |
| 87 | } |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 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; |
| 95 | |
| 96 | static bool |
| 97 | compareDiceTableEntries(const DiceTableEntry i, |
| 98 | const DiceTableEntry j) { |
| 99 | return i.first == j.first; |
| 100 | } |
| 101 | |
| 102 | static void DumpDataInCode(const char *bytes, uint64_t Size, |
| 103 | unsigned short Kind) { |
| 104 | uint64_t Value; |
| 105 | |
| 106 | switch (Kind) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 107 | case MachO::DICE_KIND_DATA: |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 108 | switch (Size) { |
| 109 | case 4: |
| 110 | Value = bytes[3] << 24 | |
| 111 | bytes[2] << 16 | |
| 112 | bytes[1] << 8 | |
| 113 | bytes[0]; |
| 114 | outs() << "\t.long " << Value; |
| 115 | break; |
| 116 | case 2: |
| 117 | Value = bytes[1] << 8 | |
| 118 | bytes[0]; |
| 119 | outs() << "\t.short " << Value; |
| 120 | break; |
| 121 | case 1: |
| 122 | Value = bytes[0]; |
| 123 | outs() << "\t.byte " << Value; |
| 124 | break; |
| 125 | } |
| 126 | outs() << "\t@ KIND_DATA\n"; |
| 127 | break; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 128 | case MachO::DICE_KIND_JUMP_TABLE8: |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 129 | Value = bytes[0]; |
| 130 | outs() << "\t.byte " << Value << "\t@ KIND_JUMP_TABLE8"; |
| 131 | break; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 132 | case MachO::DICE_KIND_JUMP_TABLE16: |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 133 | Value = bytes[1] << 8 | |
| 134 | bytes[0]; |
| 135 | outs() << "\t.short " << Value << "\t@ KIND_JUMP_TABLE16"; |
| 136 | break; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 137 | case MachO::DICE_KIND_JUMP_TABLE32: |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 138 | Value = bytes[3] << 24 | |
| 139 | bytes[2] << 16 | |
| 140 | bytes[1] << 8 | |
| 141 | bytes[0]; |
| 142 | outs() << "\t.long " << Value << "\t@ KIND_JUMP_TABLE32"; |
| 143 | break; |
| 144 | default: |
| 145 | outs() << "\t@ data in code kind = " << Kind << "\n"; |
| 146 | break; |
| 147 | } |
| 148 | } |
| 149 | |
Alexey Samsonov | 464d2e4 | 2014-03-17 07:28:19 +0000 | [diff] [blame] | 150 | static void getSectionsAndSymbols(const MachO::mach_header Header, |
| 151 | MachOObjectFile *MachOObj, |
| 152 | std::vector<SectionRef> &Sections, |
| 153 | std::vector<SymbolRef> &Symbols, |
| 154 | SmallVectorImpl<uint64_t> &FoundFns, |
| 155 | uint64_t &BaseSegmentAddress) { |
| 156 | for (const SymbolRef &Symbol : MachOObj->symbols()) |
| 157 | Symbols.push_back(Symbol); |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 158 | |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 159 | for (const SectionRef &Section : MachOObj->sections()) { |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 160 | StringRef SectName; |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 161 | Section.getName(SectName); |
| 162 | Sections.push_back(Section); |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 165 | MachOObjectFile::LoadCommandInfo Command = |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 166 | MachOObj->getFirstLoadCommandInfo(); |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 167 | bool BaseSegmentAddressSet = false; |
Rafael Espindola | feef8c2 | 2013-04-19 11:36:47 +0000 | [diff] [blame] | 168 | for (unsigned i = 0; ; ++i) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 169 | if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) { |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 170 | // We found a function starts segment, parse the addresses for later |
| 171 | // consumption. |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 172 | MachO::linkedit_data_command LLC = |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 173 | MachOObj->getLinkeditDataLoadCommand(Command); |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 174 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 175 | MachOObj->ReadULEB128s(LLC.dataoff, FoundFns); |
Benjamin Kramer | 8a529dc | 2011-09-21 22:16:43 +0000 | [diff] [blame] | 176 | } |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 177 | else if (Command.C.cmd == MachO::LC_SEGMENT) { |
| 178 | MachO::segment_command SLC = |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 179 | MachOObj->getSegmentLoadCommand(Command); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 180 | StringRef SegName = SLC.segname; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 181 | if(!BaseSegmentAddressSet && SegName != "__PAGEZERO") { |
| 182 | BaseSegmentAddressSet = true; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 183 | BaseSegmentAddress = SLC.vmaddr; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 184 | } |
| 185 | } |
Rafael Espindola | feef8c2 | 2013-04-19 11:36:47 +0000 | [diff] [blame] | 186 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 187 | if (i == Header.ncmds - 1) |
Rafael Espindola | feef8c2 | 2013-04-19 11:36:47 +0000 | [diff] [blame] | 188 | break; |
| 189 | else |
| 190 | Command = MachOObj->getNextLoadCommandInfo(Command); |
Benjamin Kramer | 8a529dc | 2011-09-21 22:16:43 +0000 | [diff] [blame] | 191 | } |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Rafael Espindola | 9b70925 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 194 | static void DisassembleInputMachO2(StringRef Filename, |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 195 | MachOObjectFile *MachOOF); |
Rafael Espindola | 9b70925 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 196 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 197 | void llvm::DisassembleInputMachO(StringRef Filename) { |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 198 | std::unique_ptr<MemoryBuffer> Buff; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 199 | |
| 200 | if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) { |
| 201 | errs() << "llvm-objdump: " << Filename << ": " << ec.message() << "\n"; |
| 202 | return; |
| 203 | } |
| 204 | |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 205 | std::unique_ptr<MachOObjectFile> MachOOF(static_cast<MachOObjectFile *>( |
Ahmed Charles | 96c9d95 | 2014-03-05 10:19:29 +0000 | [diff] [blame] | 206 | ObjectFile::createMachOObjectFile(Buff.release()).get())); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 207 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 208 | DisassembleInputMachO2(Filename, MachOOF.get()); |
Rafael Espindola | 9b70925 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Rafael Espindola | 9b70925 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 211 | static void DisassembleInputMachO2(StringRef Filename, |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 212 | MachOObjectFile *MachOOF) { |
Rafael Espindola | 9b70925 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 213 | const Target *TheTarget = GetTarget(MachOOF); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 214 | if (!TheTarget) { |
| 215 | // GetTarget prints out stuff. |
| 216 | return; |
| 217 | } |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 218 | std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo()); |
| 219 | std::unique_ptr<MCInstrAnalysis> InstrAnalysis( |
| 220 | TheTarget->createMCInstrAnalysis(InstrInfo.get())); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 221 | |
| 222 | // Set up disassembler. |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 223 | std::unique_ptr<const MCRegisterInfo> MRI( |
| 224 | TheTarget->createMCRegInfo(TripleName)); |
| 225 | std::unique_ptr<const MCAsmInfo> AsmInfo( |
Rafael Espindola | 227144c | 2013-05-13 01:16:13 +0000 | [diff] [blame] | 226 | TheTarget->createMCAsmInfo(*MRI, TripleName)); |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 227 | std::unique_ptr<const MCSubtargetInfo> STI( |
| 228 | TheTarget->createMCSubtargetInfo(TripleName, "", "")); |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame^] | 229 | MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr); |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 230 | std::unique_ptr<const MCDisassembler> DisAsm( |
Lang Hames | a1bc0f5 | 2014-04-15 04:40:56 +0000 | [diff] [blame] | 231 | TheTarget->createMCDisassembler(*STI, Ctx)); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 232 | int AsmPrinterVariant = AsmInfo->getAssemblerDialect(); |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 233 | std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter( |
| 234 | AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI, *STI)); |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 235 | |
| 236 | if (!InstrAnalysis || !AsmInfo || !STI || !DisAsm || !IP) { |
Michael J. Spencer | c1363cf | 2011-10-07 19:25:47 +0000 | [diff] [blame] | 237 | errs() << "error: couldn't initialize disassembler for target " |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 238 | << TripleName << '\n'; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 239 | return; |
| 240 | } |
| 241 | |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 242 | outs() << '\n' << Filename << ":\n\n"; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 243 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 244 | MachO::mach_header Header = MachOOF->getHeader(); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 245 | |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 246 | // FIXME: FoundFns isn't used anymore. Using symbols/LC_FUNCTION_STARTS to |
| 247 | // determine function locations will eventually go in MCObjectDisassembler. |
| 248 | // FIXME: Using the -cfg command line option, this code used to be able to |
| 249 | // annotate relocations with the referenced symbol's name, and if this was |
| 250 | // inside a __[cf]string section, the data it points to. This is now replaced |
| 251 | // by the upcoming MCSymbolizer, which needs the appropriate setup done above. |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 252 | std::vector<SectionRef> Sections; |
| 253 | std::vector<SymbolRef> Symbols; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 254 | SmallVector<uint64_t, 8> FoundFns; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 255 | uint64_t BaseSegmentAddress; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 256 | |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 257 | getSectionsAndSymbols(Header, MachOOF, Sections, Symbols, FoundFns, |
| 258 | BaseSegmentAddress); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 259 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 260 | // Sort the symbols by address, just in case they didn't come in that way. |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 261 | std::sort(Symbols.begin(), Symbols.end(), SymbolSorter()); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 262 | |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 263 | // Build a data in code table that is sorted on by the address of each entry. |
| 264 | uint64_t BaseAddress = 0; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 265 | if (Header.filetype == MachO::MH_OBJECT) |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 266 | Sections[0].getAddress(BaseAddress); |
| 267 | else |
| 268 | BaseAddress = BaseSegmentAddress; |
| 269 | DiceTable Dices; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 270 | for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices(); |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 271 | DI != DE; ++DI) { |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 272 | uint32_t Offset; |
| 273 | DI->getOffset(Offset); |
| 274 | Dices.push_back(std::make_pair(BaseAddress + Offset, *DI)); |
| 275 | } |
| 276 | array_pod_sort(Dices.begin(), Dices.end()); |
| 277 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 278 | #ifndef NDEBUG |
| 279 | raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls(); |
| 280 | #else |
| 281 | raw_ostream &DebugOut = nulls(); |
| 282 | #endif |
| 283 | |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 284 | std::unique_ptr<DIContext> diContext; |
Rafael Espindola | 9b70925 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 285 | ObjectFile *DbgObj = MachOOF; |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 286 | // Try to find debug info and set up the DIContext for it. |
| 287 | if (UseDbg) { |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 288 | // A separate DSym file path was specified, parse it as a macho file, |
| 289 | // get the sections and supply it to the section name parsing machinery. |
| 290 | if (!DSYMFile.empty()) { |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 291 | std::unique_ptr<MemoryBuffer> Buf; |
Rafael Espindola | 8c81172 | 2013-06-25 05:28:34 +0000 | [diff] [blame] | 292 | if (error_code ec = MemoryBuffer::getFileOrSTDIN(DSYMFile, Buf)) { |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 293 | errs() << "llvm-objdump: " << Filename << ": " << ec.message() << '\n'; |
| 294 | return; |
| 295 | } |
Ahmed Charles | 96c9d95 | 2014-03-05 10:19:29 +0000 | [diff] [blame] | 296 | DbgObj = ObjectFile::createMachOObjectFile(Buf.release()).get(); |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Eric Christopher | 7370b55 | 2012-11-12 21:40:38 +0000 | [diff] [blame] | 299 | // Setup the DIContext |
| 300 | diContext.reset(DIContext::getDWARFContext(DbgObj)); |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 303 | for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) { |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 304 | |
| 305 | bool SectIsText = false; |
| 306 | Sections[SectIdx].isText(SectIsText); |
| 307 | if (SectIsText == false) |
| 308 | continue; |
| 309 | |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 310 | StringRef SectName; |
| 311 | if (Sections[SectIdx].getName(SectName) || |
Rafael Espindola | a9f810b | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 312 | SectName != "__text") |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 313 | continue; // Skip non-text sections |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 314 | |
Rafael Espindola | a9f810b | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 315 | DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl(); |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 316 | |
Rafael Espindola | b0f76a4 | 2013-04-05 15:15:22 +0000 | [diff] [blame] | 317 | StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR); |
| 318 | if (SegmentName != "__TEXT") |
Rafael Espindola | a9f810b | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 319 | continue; |
| 320 | |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 321 | StringRef Bytes; |
| 322 | Sections[SectIdx].getContents(Bytes); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 323 | StringRefMemoryObject memoryObject(Bytes); |
| 324 | bool symbolTableWorked = false; |
| 325 | |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 326 | // Parse relocations. |
Alexey Samsonov | aa4d295 | 2014-03-14 14:22:49 +0000 | [diff] [blame] | 327 | std::vector<std::pair<uint64_t, SymbolRef>> Relocs; |
| 328 | for (const RelocationRef &Reloc : Sections[SectIdx].relocations()) { |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 329 | uint64_t RelocOffset, SectionAddress; |
Alexey Samsonov | aa4d295 | 2014-03-14 14:22:49 +0000 | [diff] [blame] | 330 | Reloc.getOffset(RelocOffset); |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 331 | Sections[SectIdx].getAddress(SectionAddress); |
| 332 | RelocOffset -= SectionAddress; |
| 333 | |
Alexey Samsonov | aa4d295 | 2014-03-14 14:22:49 +0000 | [diff] [blame] | 334 | symbol_iterator RelocSym = Reloc.getSymbol(); |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 335 | |
Rafael Espindola | 806f006 | 2013-06-05 01:33:53 +0000 | [diff] [blame] | 336 | Relocs.push_back(std::make_pair(RelocOffset, *RelocSym)); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 337 | } |
| 338 | array_pod_sort(Relocs.begin(), Relocs.end()); |
| 339 | |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 340 | // Disassemble symbol by symbol. |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 341 | for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) { |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 342 | StringRef SymName; |
| 343 | Symbols[SymIdx].getName(SymName); |
| 344 | |
| 345 | SymbolRef::Type ST; |
| 346 | Symbols[SymIdx].getType(ST); |
| 347 | if (ST != SymbolRef::ST_Function) |
| 348 | continue; |
| 349 | |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 350 | // Make sure the symbol is defined in this section. |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 351 | bool containsSym = false; |
| 352 | Sections[SectIdx].containsSymbol(Symbols[SymIdx], containsSym); |
| 353 | if (!containsSym) |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 354 | continue; |
| 355 | |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 356 | // Start at the address of the symbol relative to the section's address. |
Cameron Zwarich | 54478a5 | 2012-02-03 05:42:17 +0000 | [diff] [blame] | 357 | uint64_t SectionAddress = 0; |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 358 | uint64_t Start = 0; |
Cameron Zwarich | 54478a5 | 2012-02-03 05:42:17 +0000 | [diff] [blame] | 359 | Sections[SectIdx].getAddress(SectionAddress); |
Danil Malyshev | cbe72fc | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 360 | Symbols[SymIdx].getAddress(Start); |
Cameron Zwarich | 54478a5 | 2012-02-03 05:42:17 +0000 | [diff] [blame] | 361 | Start -= SectionAddress; |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 362 | |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 363 | // Stop disassembling either at the beginning of the next symbol or at |
| 364 | // the end of the section. |
Kevin Enderby | edd5872 | 2012-05-15 18:57:14 +0000 | [diff] [blame] | 365 | bool containsNextSym = false; |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 366 | uint64_t NextSym = 0; |
| 367 | uint64_t NextSymIdx = SymIdx+1; |
| 368 | while (Symbols.size() > NextSymIdx) { |
| 369 | SymbolRef::Type NextSymType; |
| 370 | Symbols[NextSymIdx].getType(NextSymType); |
| 371 | if (NextSymType == SymbolRef::ST_Function) { |
| 372 | Sections[SectIdx].containsSymbol(Symbols[NextSymIdx], |
| 373 | containsNextSym); |
Danil Malyshev | cbe72fc | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 374 | Symbols[NextSymIdx].getAddress(NextSym); |
Cameron Zwarich | 54478a5 | 2012-02-03 05:42:17 +0000 | [diff] [blame] | 375 | NextSym -= SectionAddress; |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 376 | break; |
| 377 | } |
| 378 | ++NextSymIdx; |
| 379 | } |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 380 | |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 381 | uint64_t SectSize; |
| 382 | Sections[SectIdx].getSize(SectSize); |
| 383 | uint64_t End = containsNextSym ? NextSym : SectSize; |
| 384 | uint64_t Size; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 385 | |
| 386 | symbolTableWorked = true; |
| 387 | |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 388 | outs() << SymName << ":\n"; |
| 389 | DILineInfo lastLine; |
| 390 | for (uint64_t Index = Start; Index < End; Index += Size) { |
| 391 | MCInst Inst; |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 392 | |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 393 | uint64_t SectAddress = 0; |
| 394 | Sections[SectIdx].getAddress(SectAddress); |
| 395 | outs() << format("%8" PRIx64 ":\t", SectAddress + Index); |
| 396 | |
| 397 | // Check the data in code table here to see if this is data not an |
| 398 | // instruction to be disassembled. |
| 399 | DiceTable Dice; |
| 400 | Dice.push_back(std::make_pair(SectAddress + Index, DiceRef())); |
| 401 | dice_table_iterator DTI = std::search(Dices.begin(), Dices.end(), |
| 402 | Dice.begin(), Dice.end(), |
| 403 | compareDiceTableEntries); |
| 404 | if (DTI != Dices.end()){ |
| 405 | uint16_t Length; |
| 406 | DTI->second.getLength(Length); |
| 407 | DumpBytes(StringRef(Bytes.data() + Index, Length)); |
| 408 | uint16_t Kind; |
| 409 | DTI->second.getKind(Kind); |
| 410 | DumpDataInCode(Bytes.data() + Index, Length, Kind); |
| 411 | continue; |
| 412 | } |
| 413 | |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 414 | if (DisAsm->getInstruction(Inst, Size, memoryObject, Index, |
| 415 | DebugOut, nulls())) { |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 416 | DumpBytes(StringRef(Bytes.data() + Index, Size)); |
| 417 | IP->printInst(&Inst, outs(), ""); |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 418 | |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 419 | // Print debug info. |
| 420 | if (diContext) { |
| 421 | DILineInfo dli = |
| 422 | diContext->getLineInfoForAddress(SectAddress + Index); |
| 423 | // Print valid line info if it changed. |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 424 | if (dli != lastLine && dli.Line != 0) |
| 425 | outs() << "\t## " << dli.FileName << ':' << dli.Line << ':' |
| 426 | << dli.Column; |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 427 | lastLine = dli; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 428 | } |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 429 | outs() << "\n"; |
| 430 | } else { |
| 431 | errs() << "llvm-objdump: warning: invalid instruction encoding\n"; |
| 432 | if (Size == 0) |
| 433 | Size = 1; // skip illegible bytes |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 434 | } |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 435 | } |
| 436 | } |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 437 | if (!symbolTableWorked) { |
Kevin Enderby | badd100 | 2012-05-18 00:13:56 +0000 | [diff] [blame] | 438 | // Reading the symbol table didn't work, disassemble the whole section. |
| 439 | uint64_t SectAddress; |
| 440 | Sections[SectIdx].getAddress(SectAddress); |
| 441 | uint64_t SectSize; |
| 442 | Sections[SectIdx].getSize(SectSize); |
| 443 | uint64_t InstSize; |
| 444 | for (uint64_t Index = 0; Index < SectSize; Index += InstSize) { |
Bill Wendling | 4e68e06 | 2012-07-19 00:17:40 +0000 | [diff] [blame] | 445 | MCInst Inst; |
Kevin Enderby | badd100 | 2012-05-18 00:13:56 +0000 | [diff] [blame] | 446 | |
Bill Wendling | 4e68e06 | 2012-07-19 00:17:40 +0000 | [diff] [blame] | 447 | if (DisAsm->getInstruction(Inst, InstSize, memoryObject, Index, |
| 448 | DebugOut, nulls())) { |
| 449 | outs() << format("%8" PRIx64 ":\t", SectAddress + Index); |
| 450 | DumpBytes(StringRef(Bytes.data() + Index, InstSize)); |
| 451 | IP->printInst(&Inst, outs(), ""); |
| 452 | outs() << "\n"; |
| 453 | } else { |
| 454 | errs() << "llvm-objdump: warning: invalid instruction encoding\n"; |
| 455 | if (InstSize == 0) |
| 456 | InstSize = 1; // skip illegible bytes |
| 457 | } |
Kevin Enderby | badd100 | 2012-05-18 00:13:56 +0000 | [diff] [blame] | 458 | } |
| 459 | } |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 460 | } |
| 461 | } |