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" |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 15 | #include "llvm-c/Disassembler.h" |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringExtras.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/Triple.h" |
Kevin Enderby | 04bf693 | 2014-10-28 23:39:46 +0000 | [diff] [blame] | 19 | #include "llvm/Config/config.h" |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 20 | #include "llvm/DebugInfo/DIContext.h" |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCAsmInfo.h" |
Lang Hames | a1bc0f5 | 2014-04-15 04:40:56 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCContext.h" |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCDisassembler.h" |
| 24 | #include "llvm/MC/MCInst.h" |
| 25 | #include "llvm/MC/MCInstPrinter.h" |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCInstrDesc.h" |
| 27 | #include "llvm/MC/MCInstrInfo.h" |
Jim Grosbach | fd93a59 | 2012-03-05 19:33:20 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCRegisterInfo.h" |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 29 | #include "llvm/MC/MCSubtargetInfo.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 30 | #include "llvm/Object/MachO.h" |
Kevin Enderby | ef3ad2f | 2014-12-04 23:56:27 +0000 | [diff] [blame] | 31 | #include "llvm/Object/MachOUniversal.h" |
Rafael Espindola | 9b70925 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Casting.h" |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 33 | #include "llvm/Support/CommandLine.h" |
| 34 | #include "llvm/Support/Debug.h" |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Endian.h" |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Format.h" |
| 37 | #include "llvm/Support/GraphWriter.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 38 | #include "llvm/Support/MachO.h" |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 39 | #include "llvm/Support/MemoryBuffer.h" |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 40 | #include "llvm/Support/FormattedStream.h" |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 41 | #include "llvm/Support/TargetRegistry.h" |
| 42 | #include "llvm/Support/TargetSelect.h" |
| 43 | #include "llvm/Support/raw_ostream.h" |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 44 | #include <algorithm> |
| 45 | #include <cstring> |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 46 | #include <system_error> |
Kevin Enderby | 04bf693 | 2014-10-28 23:39:46 +0000 | [diff] [blame] | 47 | |
| 48 | #if HAVE_CXXABI_H |
| 49 | #include <cxxabi.h> |
| 50 | #endif |
| 51 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 52 | using namespace llvm; |
| 53 | using namespace object; |
| 54 | |
| 55 | static cl::opt<bool> |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 56 | UseDbg("g", |
| 57 | cl::desc("Print line information from debug info if available")); |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 58 | |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 59 | static cl::opt<std::string> DSYMFile("dsym", |
| 60 | cl::desc("Use .dSYM file for debug info")); |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 61 | |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 62 | static cl::opt<bool> FullLeadingAddr("full-leading-addr", |
| 63 | cl::desc("Print full leading address")); |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 64 | |
| 65 | static cl::opt<bool> |
| 66 | PrintImmHex("print-imm-hex", |
| 67 | cl::desc("Use hex format for immediate values")); |
| 68 | |
Kevin Enderby | ef3ad2f | 2014-12-04 23:56:27 +0000 | [diff] [blame] | 69 | static cl::list<std::string> |
| 70 | ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"), |
| 71 | cl::ZeroOrMore); |
| 72 | bool ArchAll = false; |
| 73 | |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 74 | static std::string ThumbTripleName; |
| 75 | |
| 76 | static const Target *GetTarget(const MachOObjectFile *MachOObj, |
| 77 | const char **McpuDefault, |
| 78 | const Target **ThumbTarget) { |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 79 | // Figure out the target triple. |
Cameron Zwarich | 88cc16a | 2012-02-03 06:35:22 +0000 | [diff] [blame] | 80 | if (TripleName.empty()) { |
| 81 | llvm::Triple TT("unknown-unknown-unknown"); |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 82 | llvm::Triple ThumbTriple = Triple(); |
| 83 | TT = MachOObj->getArch(McpuDefault, &ThumbTriple); |
Cameron Zwarich | 88cc16a | 2012-02-03 06:35:22 +0000 | [diff] [blame] | 84 | TripleName = TT.str(); |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 85 | ThumbTripleName = ThumbTriple.str(); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 88 | // Get the target specific parser. |
| 89 | std::string Error; |
| 90 | const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error); |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 91 | if (TheTarget && ThumbTripleName.empty()) |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 92 | return TheTarget; |
| 93 | |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 94 | *ThumbTarget = TargetRegistry::lookupTarget(ThumbTripleName, Error); |
| 95 | if (*ThumbTarget) |
| 96 | return TheTarget; |
| 97 | |
| 98 | errs() << "llvm-objdump: error: unable to get target for '"; |
| 99 | if (!TheTarget) |
| 100 | errs() << TripleName; |
| 101 | else |
| 102 | errs() << ThumbTripleName; |
| 103 | errs() << "', see --version and --triple.\n"; |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 104 | return nullptr; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 107 | struct SymbolSorter { |
| 108 | bool operator()(const SymbolRef &A, const SymbolRef &B) { |
| 109 | SymbolRef::Type AType, BType; |
| 110 | A.getType(AType); |
| 111 | B.getType(BType); |
| 112 | |
| 113 | uint64_t AAddr, BAddr; |
| 114 | if (AType != SymbolRef::ST_Function) |
| 115 | AAddr = 0; |
| 116 | else |
| 117 | A.getAddress(AAddr); |
| 118 | if (BType != SymbolRef::ST_Function) |
| 119 | BAddr = 0; |
| 120 | else |
| 121 | B.getAddress(BAddr); |
| 122 | return AAddr < BAddr; |
| 123 | } |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 124 | }; |
| 125 | |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 126 | // Types for the storted data in code table that is built before disassembly |
| 127 | // and the predicate function to sort them. |
| 128 | typedef std::pair<uint64_t, DiceRef> DiceTableEntry; |
| 129 | typedef std::vector<DiceTableEntry> DiceTable; |
| 130 | typedef DiceTable::iterator dice_table_iterator; |
| 131 | |
Kevin Enderby | 930fdc7 | 2014-11-06 19:00:13 +0000 | [diff] [blame] | 132 | // This is used to search for a data in code table entry for the PC being |
| 133 | // disassembled. The j parameter has the PC in j.first. A single data in code |
| 134 | // table entry can cover many bytes for each of its Kind's. So if the offset, |
| 135 | // aka the i.first value, of the data in code table entry plus its Length |
| 136 | // covers the PC being searched for this will return true. If not it will |
| 137 | // return false. |
David Majnemer | ea9b8ee | 2014-11-04 08:41:48 +0000 | [diff] [blame] | 138 | static bool compareDiceTableEntries(const DiceTableEntry &i, |
| 139 | const DiceTableEntry &j) { |
Kevin Enderby | 930fdc7 | 2014-11-06 19:00:13 +0000 | [diff] [blame] | 140 | uint16_t Length; |
| 141 | i.second.getLength(Length); |
| 142 | |
| 143 | return j.first >= i.first && j.first < i.first + Length; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Kevin Enderby | 930fdc7 | 2014-11-06 19:00:13 +0000 | [diff] [blame] | 146 | static uint64_t DumpDataInCode(const char *bytes, uint64_t Length, |
| 147 | unsigned short Kind) { |
| 148 | uint32_t Value, Size = 1; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 149 | |
| 150 | switch (Kind) { |
Kevin Enderby | 930fdc7 | 2014-11-06 19:00:13 +0000 | [diff] [blame] | 151 | default: |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 152 | case MachO::DICE_KIND_DATA: |
Kevin Enderby | 930fdc7 | 2014-11-06 19:00:13 +0000 | [diff] [blame] | 153 | if (Length >= 4) { |
| 154 | if (!NoShowRawInsn) |
| 155 | DumpBytes(StringRef(bytes, 4)); |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 156 | Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0]; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 157 | outs() << "\t.long " << Value; |
Kevin Enderby | 930fdc7 | 2014-11-06 19:00:13 +0000 | [diff] [blame] | 158 | Size = 4; |
| 159 | } else if (Length >= 2) { |
| 160 | if (!NoShowRawInsn) |
| 161 | DumpBytes(StringRef(bytes, 2)); |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 162 | Value = bytes[1] << 8 | bytes[0]; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 163 | outs() << "\t.short " << Value; |
Kevin Enderby | 930fdc7 | 2014-11-06 19:00:13 +0000 | [diff] [blame] | 164 | Size = 2; |
| 165 | } else { |
| 166 | if (!NoShowRawInsn) |
| 167 | DumpBytes(StringRef(bytes, 2)); |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 168 | Value = bytes[0]; |
| 169 | outs() << "\t.byte " << Value; |
Kevin Enderby | 930fdc7 | 2014-11-06 19:00:13 +0000 | [diff] [blame] | 170 | Size = 1; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 171 | } |
Kevin Enderby | 930fdc7 | 2014-11-06 19:00:13 +0000 | [diff] [blame] | 172 | if (Kind == MachO::DICE_KIND_DATA) |
| 173 | outs() << "\t@ KIND_DATA\n"; |
| 174 | else |
| 175 | outs() << "\t@ data in code kind = " << Kind << "\n"; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 176 | break; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 177 | case MachO::DICE_KIND_JUMP_TABLE8: |
Kevin Enderby | 930fdc7 | 2014-11-06 19:00:13 +0000 | [diff] [blame] | 178 | if (!NoShowRawInsn) |
| 179 | DumpBytes(StringRef(bytes, 1)); |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 180 | Value = bytes[0]; |
Kevin Enderby | 930fdc7 | 2014-11-06 19:00:13 +0000 | [diff] [blame] | 181 | outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n"; |
| 182 | Size = 1; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 183 | break; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 184 | case MachO::DICE_KIND_JUMP_TABLE16: |
Kevin Enderby | 930fdc7 | 2014-11-06 19:00:13 +0000 | [diff] [blame] | 185 | if (!NoShowRawInsn) |
| 186 | DumpBytes(StringRef(bytes, 2)); |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 187 | Value = bytes[1] << 8 | bytes[0]; |
Kevin Enderby | 930fdc7 | 2014-11-06 19:00:13 +0000 | [diff] [blame] | 188 | outs() << "\t.short " << format("%5u", Value & 0xffff) |
| 189 | << "\t@ KIND_JUMP_TABLE16\n"; |
| 190 | Size = 2; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 191 | break; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 192 | case MachO::DICE_KIND_JUMP_TABLE32: |
Kevin Enderby | 930fdc7 | 2014-11-06 19:00:13 +0000 | [diff] [blame] | 193 | case MachO::DICE_KIND_ABS_JUMP_TABLE32: |
| 194 | if (!NoShowRawInsn) |
| 195 | DumpBytes(StringRef(bytes, 4)); |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 196 | Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0]; |
Kevin Enderby | 930fdc7 | 2014-11-06 19:00:13 +0000 | [diff] [blame] | 197 | outs() << "\t.long " << Value; |
| 198 | if (Kind == MachO::DICE_KIND_JUMP_TABLE32) |
| 199 | outs() << "\t@ KIND_JUMP_TABLE32\n"; |
| 200 | else |
| 201 | outs() << "\t@ KIND_ABS_JUMP_TABLE32\n"; |
| 202 | Size = 4; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 203 | break; |
| 204 | } |
Kevin Enderby | 930fdc7 | 2014-11-06 19:00:13 +0000 | [diff] [blame] | 205 | return Size; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Alexey Samsonov | 464d2e4 | 2014-03-17 07:28:19 +0000 | [diff] [blame] | 208 | static void getSectionsAndSymbols(const MachO::mach_header Header, |
| 209 | MachOObjectFile *MachOObj, |
| 210 | std::vector<SectionRef> &Sections, |
| 211 | std::vector<SymbolRef> &Symbols, |
| 212 | SmallVectorImpl<uint64_t> &FoundFns, |
| 213 | uint64_t &BaseSegmentAddress) { |
Kevin Enderby | ef3ad2f | 2014-12-04 23:56:27 +0000 | [diff] [blame] | 214 | for (const SymbolRef &Symbol : MachOObj->symbols()) { |
| 215 | StringRef SymName; |
| 216 | Symbol.getName(SymName); |
| 217 | if (!SymName.startswith("ltmp")) |
| 218 | Symbols.push_back(Symbol); |
| 219 | } |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 220 | |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 221 | for (const SectionRef &Section : MachOObj->sections()) { |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 222 | StringRef SectName; |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 223 | Section.getName(SectName); |
| 224 | Sections.push_back(Section); |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 227 | MachOObjectFile::LoadCommandInfo Command = |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 228 | MachOObj->getFirstLoadCommandInfo(); |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 229 | bool BaseSegmentAddressSet = false; |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 230 | for (unsigned i = 0;; ++i) { |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 231 | if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) { |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 232 | // We found a function starts segment, parse the addresses for later |
| 233 | // consumption. |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 234 | MachO::linkedit_data_command LLC = |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 235 | MachOObj->getLinkeditDataLoadCommand(Command); |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 236 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 237 | MachOObj->ReadULEB128s(LLC.dataoff, FoundFns); |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 238 | } else if (Command.C.cmd == MachO::LC_SEGMENT) { |
| 239 | MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command); |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 240 | StringRef SegName = SLC.segname; |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 241 | if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") { |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 242 | BaseSegmentAddressSet = true; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 243 | BaseSegmentAddress = SLC.vmaddr; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 244 | } |
| 245 | } |
Rafael Espindola | feef8c2 | 2013-04-19 11:36:47 +0000 | [diff] [blame] | 246 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 247 | if (i == Header.ncmds - 1) |
Rafael Espindola | feef8c2 | 2013-04-19 11:36:47 +0000 | [diff] [blame] | 248 | break; |
| 249 | else |
| 250 | Command = MachOObj->getNextLoadCommandInfo(Command); |
Benjamin Kramer | 8a529dc | 2011-09-21 22:16:43 +0000 | [diff] [blame] | 251 | } |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Kevin Enderby | ef3ad2f | 2014-12-04 23:56:27 +0000 | [diff] [blame] | 254 | // checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file |
| 255 | // and if it is and there is a list of architecture flags is specified then |
| 256 | // check to make sure this Mach-O file is one of those architectures or all |
| 257 | // architectures were specified. If not then an error is generated and this |
| 258 | // routine returns false. Else it returns true. |
| 259 | static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) { |
| 260 | if (isa<MachOObjectFile>(O) && !ArchAll && ArchFlags.size() != 0) { |
| 261 | MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(O); |
| 262 | bool ArchFound = false; |
| 263 | MachO::mach_header H; |
| 264 | MachO::mach_header_64 H_64; |
| 265 | Triple T; |
| 266 | if (MachO->is64Bit()) { |
| 267 | H_64 = MachO->MachOObjectFile::getHeader64(); |
| 268 | T = MachOObjectFile::getArch(H_64.cputype, H_64.cpusubtype); |
| 269 | } else { |
| 270 | H = MachO->MachOObjectFile::getHeader(); |
| 271 | T = MachOObjectFile::getArch(H.cputype, H.cpusubtype); |
| 272 | } |
| 273 | unsigned i; |
| 274 | for (i = 0; i < ArchFlags.size(); ++i) { |
| 275 | if (ArchFlags[i] == T.getArchName()) |
| 276 | ArchFound = true; |
| 277 | break; |
| 278 | } |
| 279 | if (!ArchFound) { |
| 280 | errs() << "llvm-objdump: file: " + Filename + " does not contain " |
| 281 | << "architecture: " + ArchFlags[i] + "\n"; |
| 282 | return false; |
| 283 | } |
| 284 | } |
| 285 | return true; |
| 286 | } |
| 287 | |
| 288 | static void DisassembleInputMachO2(StringRef Filename, MachOObjectFile *MachOOF, |
| 289 | StringRef ArchiveMemberName = StringRef(), |
| 290 | StringRef ArchitectureName = StringRef()); |
Rafael Espindola | 9b70925 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 291 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 292 | void llvm::DisassembleInputMachO(StringRef Filename) { |
Kevin Enderby | ef3ad2f | 2014-12-04 23:56:27 +0000 | [diff] [blame] | 293 | // Check for -arch all and verifiy the -arch flags are valid. |
| 294 | for (unsigned i = 0; i < ArchFlags.size(); ++i) { |
| 295 | if (ArchFlags[i] == "all") { |
| 296 | ArchAll = true; |
| 297 | } else { |
| 298 | if (!MachOObjectFile::isValidArch(ArchFlags[i])) { |
| 299 | errs() << "llvm-objdump: Unknown architecture named '" + ArchFlags[i] + |
| 300 | "'for the -arch option\n"; |
| 301 | return; |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | // Attempt to open the binary. |
| 307 | ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename); |
| 308 | if (std::error_code EC = BinaryOrErr.getError()) { |
| 309 | errs() << "llvm-objdump: '" << Filename << "': " << EC.message() << ".\n"; |
Rafael Espindola | de882cd | 2014-12-03 23:29:34 +0000 | [diff] [blame] | 310 | return; |
Kevin Enderby | 3f0ffab | 2014-12-03 22:29:40 +0000 | [diff] [blame] | 311 | } |
Kevin Enderby | ef3ad2f | 2014-12-04 23:56:27 +0000 | [diff] [blame] | 312 | Binary &Bin = *BinaryOrErr.get().getBinary(); |
Kevin Enderby | 3f0ffab | 2014-12-03 22:29:40 +0000 | [diff] [blame] | 313 | |
Kevin Enderby | ef3ad2f | 2014-12-04 23:56:27 +0000 | [diff] [blame] | 314 | if (Archive *A = dyn_cast<Archive>(&Bin)) { |
| 315 | outs() << "Archive : " << Filename << "\n"; |
| 316 | for (Archive::child_iterator I = A->child_begin(), E = A->child_end(); |
| 317 | I != E; ++I) { |
| 318 | ErrorOr<std::unique_ptr<Binary>> ChildOrErr = I->getAsBinary(); |
| 319 | if (ChildOrErr.getError()) |
| 320 | continue; |
| 321 | if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) { |
| 322 | if (!checkMachOAndArchFlags(O, Filename)) |
| 323 | return; |
| 324 | DisassembleInputMachO2(Filename, O, O->getFileName()); |
| 325 | } |
| 326 | } |
| 327 | return; |
| 328 | } |
| 329 | if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) { |
| 330 | // If we have a list of architecture flags specified dump only those. |
| 331 | if (!ArchAll && ArchFlags.size() != 0) { |
| 332 | // Look for a slice in the universal binary that matches each ArchFlag. |
| 333 | bool ArchFound; |
| 334 | for (unsigned i = 0; i < ArchFlags.size(); ++i) { |
| 335 | ArchFound = false; |
| 336 | for (MachOUniversalBinary::object_iterator I = UB->begin_objects(), |
| 337 | E = UB->end_objects(); |
| 338 | I != E; ++I) { |
| 339 | if (ArchFlags[i] == I->getArchTypeName()) { |
| 340 | ArchFound = true; |
| 341 | ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = |
| 342 | I->getAsObjectFile(); |
Kevin Enderby | ef3ad2f | 2014-12-04 23:56:27 +0000 | [diff] [blame] | 343 | std::string ArchitectureName = ""; |
| 344 | if (ArchFlags.size() > 1) |
| 345 | ArchitectureName = I->getArchTypeName(); |
| 346 | if (ObjOrErr) { |
| 347 | ObjectFile &O = *ObjOrErr.get(); |
| 348 | if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O)) |
| 349 | DisassembleInputMachO2(Filename, MachOOF, "", ArchitectureName); |
Rafael Espindola | 0bfe828 | 2014-12-09 21:05:36 +0000 | [diff] [blame] | 350 | } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = |
| 351 | I->getAsArchive()) { |
| 352 | std::unique_ptr<Archive> &A = *AOrErr; |
Kevin Enderby | ef3ad2f | 2014-12-04 23:56:27 +0000 | [diff] [blame] | 353 | outs() << "Archive : " << Filename; |
| 354 | if (!ArchitectureName.empty()) |
| 355 | outs() << " (architecture " << ArchitectureName << ")"; |
| 356 | outs() << "\n"; |
| 357 | for (Archive::child_iterator AI = A->child_begin(), |
| 358 | AE = A->child_end(); |
| 359 | AI != AE; ++AI) { |
| 360 | ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary(); |
| 361 | if (ChildOrErr.getError()) |
| 362 | continue; |
| 363 | if (MachOObjectFile *O = |
| 364 | dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) |
| 365 | DisassembleInputMachO2(Filename, O, O->getFileName(), |
| 366 | ArchitectureName); |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | if (!ArchFound) { |
| 372 | errs() << "llvm-objdump: file: " + Filename + " does not contain " |
| 373 | << "architecture: " + ArchFlags[i] + "\n"; |
| 374 | return; |
| 375 | } |
| 376 | } |
| 377 | return; |
| 378 | } |
| 379 | // No architecture flags were specified so if this contains a slice that |
| 380 | // matches the host architecture dump only that. |
| 381 | if (!ArchAll) { |
| 382 | StringRef HostArchName = MachOObjectFile::getHostArch().getArchName(); |
| 383 | for (MachOUniversalBinary::object_iterator I = UB->begin_objects(), |
| 384 | E = UB->end_objects(); |
| 385 | I != E; ++I) { |
| 386 | if (HostArchName == I->getArchTypeName()) { |
| 387 | ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile(); |
Kevin Enderby | ef3ad2f | 2014-12-04 23:56:27 +0000 | [diff] [blame] | 388 | std::string ArchiveName; |
| 389 | ArchiveName.clear(); |
| 390 | if (ObjOrErr) { |
| 391 | ObjectFile &O = *ObjOrErr.get(); |
| 392 | if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O)) |
| 393 | DisassembleInputMachO2(Filename, MachOOF); |
Rafael Espindola | 0bfe828 | 2014-12-09 21:05:36 +0000 | [diff] [blame] | 394 | } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = |
| 395 | I->getAsArchive()) { |
| 396 | std::unique_ptr<Archive> &A = *AOrErr; |
Kevin Enderby | ef3ad2f | 2014-12-04 23:56:27 +0000 | [diff] [blame] | 397 | outs() << "Archive : " << Filename << "\n"; |
| 398 | for (Archive::child_iterator AI = A->child_begin(), |
| 399 | AE = A->child_end(); |
| 400 | AI != AE; ++AI) { |
| 401 | ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary(); |
| 402 | if (ChildOrErr.getError()) |
| 403 | continue; |
| 404 | if (MachOObjectFile *O = |
| 405 | dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) |
| 406 | DisassembleInputMachO2(Filename, O, O->getFileName()); |
| 407 | } |
| 408 | } |
| 409 | return; |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | // Either all architectures have been specified or none have been specified |
| 414 | // and this does not contain the host architecture so dump all the slices. |
| 415 | bool moreThanOneArch = UB->getNumberOfObjects() > 1; |
| 416 | for (MachOUniversalBinary::object_iterator I = UB->begin_objects(), |
| 417 | E = UB->end_objects(); |
| 418 | I != E; ++I) { |
| 419 | ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile(); |
Kevin Enderby | ef3ad2f | 2014-12-04 23:56:27 +0000 | [diff] [blame] | 420 | std::string ArchitectureName = ""; |
| 421 | if (moreThanOneArch) |
| 422 | ArchitectureName = I->getArchTypeName(); |
| 423 | if (ObjOrErr) { |
| 424 | ObjectFile &Obj = *ObjOrErr.get(); |
| 425 | if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj)) |
| 426 | DisassembleInputMachO2(Filename, MachOOF, "", ArchitectureName); |
Rafael Espindola | 0bfe828 | 2014-12-09 21:05:36 +0000 | [diff] [blame] | 427 | } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = I->getAsArchive()) { |
| 428 | std::unique_ptr<Archive> &A = *AOrErr; |
Kevin Enderby | ef3ad2f | 2014-12-04 23:56:27 +0000 | [diff] [blame] | 429 | outs() << "Archive : " << Filename; |
| 430 | if (!ArchitectureName.empty()) |
| 431 | outs() << " (architecture " << ArchitectureName << ")"; |
| 432 | outs() << "\n"; |
| 433 | for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end(); |
| 434 | AI != AE; ++AI) { |
| 435 | ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary(); |
| 436 | if (ChildOrErr.getError()) |
| 437 | continue; |
| 438 | if (MachOObjectFile *O = |
| 439 | dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) { |
| 440 | if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O)) |
| 441 | DisassembleInputMachO2(Filename, MachOOF, MachOOF->getFileName(), |
| 442 | ArchitectureName); |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | return; |
| 448 | } |
| 449 | if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) { |
| 450 | if (!checkMachOAndArchFlags(O, Filename)) |
| 451 | return; |
| 452 | if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O)) { |
| 453 | DisassembleInputMachO2(Filename, MachOOF); |
| 454 | } else |
| 455 | errs() << "llvm-objdump: '" << Filename << "': " |
| 456 | << "Object is not a Mach-O file type.\n"; |
| 457 | } else |
| 458 | errs() << "llvm-objdump: '" << Filename << "': " |
| 459 | << "Unrecognized file type.\n"; |
Rafael Espindola | 9b70925 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 462 | typedef DenseMap<uint64_t, StringRef> SymbolAddressMap; |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 463 | typedef std::pair<uint64_t, const char *> BindInfoEntry; |
| 464 | typedef std::vector<BindInfoEntry> BindTable; |
| 465 | typedef BindTable::iterator bind_table_iterator; |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 466 | |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 467 | // The block of info used by the Symbolizer call backs. |
| 468 | struct DisassembleInfo { |
| 469 | bool verbose; |
| 470 | MachOObjectFile *O; |
| 471 | SectionRef S; |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 472 | SymbolAddressMap *AddrMap; |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 473 | std::vector<SectionRef> *Sections; |
| 474 | const char *class_name; |
| 475 | const char *selector_name; |
| 476 | char *method; |
Kevin Enderby | 04bf693 | 2014-10-28 23:39:46 +0000 | [diff] [blame] | 477 | char *demangled_name; |
Kevin Enderby | ae3c126 | 2014-11-14 21:52:18 +0000 | [diff] [blame] | 478 | uint64_t adrp_addr; |
| 479 | uint32_t adrp_inst; |
Kevin Enderby | 078be60 | 2014-10-23 19:53:12 +0000 | [diff] [blame] | 480 | BindTable *bindtable; |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 481 | }; |
| 482 | |
Kevin Enderby | 9907d0a | 2014-11-04 00:43:16 +0000 | [diff] [blame] | 483 | // GuessSymbolName is passed the address of what might be a symbol and a |
| 484 | // pointer to the DisassembleInfo struct. It returns the name of a symbol |
| 485 | // with that address or nullptr if no symbol is found with that address. |
| 486 | static const char *GuessSymbolName(uint64_t value, |
| 487 | struct DisassembleInfo *info) { |
| 488 | const char *SymbolName = nullptr; |
| 489 | // A DenseMap can't lookup up some values. |
| 490 | if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) { |
| 491 | StringRef name = info->AddrMap->lookup(value); |
| 492 | if (!name.empty()) |
| 493 | SymbolName = name.data(); |
| 494 | } |
| 495 | return SymbolName; |
| 496 | } |
| 497 | |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 498 | // SymbolizerGetOpInfo() is the operand information call back function. |
| 499 | // This is called to get the symbolic information for operand(s) of an |
| 500 | // instruction when it is being done. This routine does this from |
| 501 | // the relocation information, symbol table, etc. That block of information |
| 502 | // is a pointer to the struct DisassembleInfo that was passed when the |
| 503 | // disassembler context was created and passed to back to here when |
| 504 | // called back by the disassembler for instruction operands that could have |
| 505 | // relocation information. The address of the instruction containing operand is |
| 506 | // at the Pc parameter. The immediate value the operand has is passed in |
| 507 | // op_info->Value and is at Offset past the start of the instruction and has a |
| 508 | // byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the |
| 509 | // LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol |
| 510 | // names and addends of the symbolic expression to add for the operand. The |
| 511 | // value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic |
| 512 | // information is returned then this function returns 1 else it returns 0. |
| 513 | int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset, |
| 514 | uint64_t Size, int TagType, void *TagBuf) { |
| 515 | struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo; |
| 516 | struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf; |
Kevin Enderby | ae3c126 | 2014-11-14 21:52:18 +0000 | [diff] [blame] | 517 | uint64_t value = op_info->Value; |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 518 | |
| 519 | // Make sure all fields returned are zero if we don't set them. |
| 520 | memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1)); |
| 521 | op_info->Value = value; |
| 522 | |
| 523 | // If the TagType is not the value 1 which it code knows about or if no |
| 524 | // verbose symbolic information is wanted then just return 0, indicating no |
| 525 | // information is being returned. |
| 526 | if (TagType != 1 || info->verbose == false) |
| 527 | return 0; |
| 528 | |
| 529 | unsigned int Arch = info->O->getArch(); |
| 530 | if (Arch == Triple::x86) { |
Kevin Enderby | 9907d0a | 2014-11-04 00:43:16 +0000 | [diff] [blame] | 531 | if (Size != 1 && Size != 2 && Size != 4 && Size != 0) |
| 532 | return 0; |
| 533 | // First search the section's relocation entries (if any) for an entry |
| 534 | // for this section offset. |
| 535 | uint32_t sect_addr = info->S.getAddress(); |
| 536 | uint32_t sect_offset = (Pc + Offset) - sect_addr; |
| 537 | bool reloc_found = false; |
| 538 | DataRefImpl Rel; |
| 539 | MachO::any_relocation_info RE; |
| 540 | bool isExtern = false; |
| 541 | SymbolRef Symbol; |
| 542 | bool r_scattered = false; |
| 543 | uint32_t r_value, pair_r_value, r_type; |
| 544 | for (const RelocationRef &Reloc : info->S.relocations()) { |
| 545 | uint64_t RelocOffset; |
| 546 | Reloc.getOffset(RelocOffset); |
| 547 | if (RelocOffset == sect_offset) { |
| 548 | Rel = Reloc.getRawDataRefImpl(); |
| 549 | RE = info->O->getRelocation(Rel); |
Kevin Enderby | 3eb73e1 | 2014-11-11 19:16:45 +0000 | [diff] [blame] | 550 | r_type = info->O->getAnyRelocationType(RE); |
Kevin Enderby | 9907d0a | 2014-11-04 00:43:16 +0000 | [diff] [blame] | 551 | r_scattered = info->O->isRelocationScattered(RE); |
| 552 | if (r_scattered) { |
| 553 | r_value = info->O->getScatteredRelocationValue(RE); |
Kevin Enderby | 9907d0a | 2014-11-04 00:43:16 +0000 | [diff] [blame] | 554 | if (r_type == MachO::GENERIC_RELOC_SECTDIFF || |
| 555 | r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) { |
| 556 | DataRefImpl RelNext = Rel; |
| 557 | info->O->moveRelocationNext(RelNext); |
| 558 | MachO::any_relocation_info RENext; |
| 559 | RENext = info->O->getRelocation(RelNext); |
| 560 | if (info->O->isRelocationScattered(RENext)) |
Kevin Enderby | 930fdc7 | 2014-11-06 19:00:13 +0000 | [diff] [blame] | 561 | pair_r_value = info->O->getScatteredRelocationValue(RENext); |
Kevin Enderby | 9907d0a | 2014-11-04 00:43:16 +0000 | [diff] [blame] | 562 | else |
| 563 | return 0; |
| 564 | } |
| 565 | } else { |
| 566 | isExtern = info->O->getPlainRelocationExternal(RE); |
| 567 | if (isExtern) { |
| 568 | symbol_iterator RelocSym = Reloc.getSymbol(); |
| 569 | Symbol = *RelocSym; |
| 570 | } |
| 571 | } |
| 572 | reloc_found = true; |
| 573 | break; |
| 574 | } |
| 575 | } |
| 576 | if (reloc_found && isExtern) { |
| 577 | StringRef SymName; |
| 578 | Symbol.getName(SymName); |
| 579 | const char *name = SymName.data(); |
| 580 | op_info->AddSymbol.Present = 1; |
| 581 | op_info->AddSymbol.Name = name; |
| 582 | // For i386 extern relocation entries the value in the instruction is |
| 583 | // the offset from the symbol, and value is already set in op_info->Value. |
| 584 | return 1; |
| 585 | } |
| 586 | if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF || |
| 587 | r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) { |
| 588 | const char *add = GuessSymbolName(r_value, info); |
| 589 | const char *sub = GuessSymbolName(pair_r_value, info); |
| 590 | uint32_t offset = value - (r_value - pair_r_value); |
| 591 | op_info->AddSymbol.Present = 1; |
| 592 | if (add != nullptr) |
| 593 | op_info->AddSymbol.Name = add; |
| 594 | else |
| 595 | op_info->AddSymbol.Value = r_value; |
| 596 | op_info->SubtractSymbol.Present = 1; |
| 597 | if (sub != nullptr) |
| 598 | op_info->SubtractSymbol.Name = sub; |
| 599 | else |
| 600 | op_info->SubtractSymbol.Value = pair_r_value; |
| 601 | op_info->Value = offset; |
| 602 | return 1; |
| 603 | } |
| 604 | // TODO: |
| 605 | // Second search the external relocation entries of a fully linked image |
| 606 | // (if any) for an entry that matches this segment offset. |
| 607 | // uint32_t seg_offset = (Pc + Offset); |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 608 | return 0; |
| 609 | } else if (Arch == Triple::x86_64) { |
| 610 | if (Size != 1 && Size != 2 && Size != 4 && Size != 0) |
| 611 | return 0; |
| 612 | // First search the section's relocation entries (if any) for an entry |
| 613 | // for this section offset. |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 614 | uint64_t sect_addr = info->S.getAddress(); |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 615 | uint64_t sect_offset = (Pc + Offset) - sect_addr; |
| 616 | bool reloc_found = false; |
| 617 | DataRefImpl Rel; |
| 618 | MachO::any_relocation_info RE; |
| 619 | bool isExtern = false; |
| 620 | SymbolRef Symbol; |
| 621 | for (const RelocationRef &Reloc : info->S.relocations()) { |
| 622 | uint64_t RelocOffset; |
| 623 | Reloc.getOffset(RelocOffset); |
| 624 | if (RelocOffset == sect_offset) { |
| 625 | Rel = Reloc.getRawDataRefImpl(); |
| 626 | RE = info->O->getRelocation(Rel); |
| 627 | // NOTE: Scattered relocations don't exist on x86_64. |
| 628 | isExtern = info->O->getPlainRelocationExternal(RE); |
| 629 | if (isExtern) { |
| 630 | symbol_iterator RelocSym = Reloc.getSymbol(); |
| 631 | Symbol = *RelocSym; |
| 632 | } |
| 633 | reloc_found = true; |
| 634 | break; |
| 635 | } |
| 636 | } |
| 637 | if (reloc_found && isExtern) { |
| 638 | // The Value passed in will be adjusted by the Pc if the instruction |
| 639 | // adds the Pc. But for x86_64 external relocation entries the Value |
| 640 | // is the offset from the external symbol. |
| 641 | if (info->O->getAnyRelocationPCRel(RE)) |
| 642 | op_info->Value -= Pc + Offset + Size; |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 643 | StringRef SymName; |
| 644 | Symbol.getName(SymName); |
| 645 | const char *name = SymName.data(); |
| 646 | unsigned Type = info->O->getAnyRelocationType(RE); |
| 647 | if (Type == MachO::X86_64_RELOC_SUBTRACTOR) { |
| 648 | DataRefImpl RelNext = Rel; |
| 649 | info->O->moveRelocationNext(RelNext); |
| 650 | MachO::any_relocation_info RENext = info->O->getRelocation(RelNext); |
| 651 | unsigned TypeNext = info->O->getAnyRelocationType(RENext); |
| 652 | bool isExternNext = info->O->getPlainRelocationExternal(RENext); |
| 653 | unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext); |
| 654 | if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) { |
| 655 | op_info->SubtractSymbol.Present = 1; |
| 656 | op_info->SubtractSymbol.Name = name; |
| 657 | symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum); |
| 658 | Symbol = *RelocSymNext; |
| 659 | StringRef SymNameNext; |
| 660 | Symbol.getName(SymNameNext); |
| 661 | name = SymNameNext.data(); |
| 662 | } |
| 663 | } |
| 664 | // TODO: add the VariantKinds to op_info->VariantKind for relocation types |
| 665 | // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT. |
| 666 | op_info->AddSymbol.Present = 1; |
| 667 | op_info->AddSymbol.Name = name; |
| 668 | return 1; |
| 669 | } |
| 670 | // TODO: |
| 671 | // Second search the external relocation entries of a fully linked image |
| 672 | // (if any) for an entry that matches this segment offset. |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 673 | // uint64_t seg_offset = (Pc + Offset); |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 674 | return 0; |
| 675 | } else if (Arch == Triple::arm) { |
Kevin Enderby | 930fdc7 | 2014-11-06 19:00:13 +0000 | [diff] [blame] | 676 | if (Offset != 0 || (Size != 4 && Size != 2)) |
| 677 | return 0; |
| 678 | // First search the section's relocation entries (if any) for an entry |
| 679 | // for this section offset. |
| 680 | uint32_t sect_addr = info->S.getAddress(); |
| 681 | uint32_t sect_offset = (Pc + Offset) - sect_addr; |
| 682 | bool reloc_found = false; |
| 683 | DataRefImpl Rel; |
| 684 | MachO::any_relocation_info RE; |
| 685 | bool isExtern = false; |
| 686 | SymbolRef Symbol; |
| 687 | bool r_scattered = false; |
| 688 | uint32_t r_value, pair_r_value, r_type, r_length, other_half; |
| 689 | for (const RelocationRef &Reloc : info->S.relocations()) { |
| 690 | uint64_t RelocOffset; |
| 691 | Reloc.getOffset(RelocOffset); |
| 692 | if (RelocOffset == sect_offset) { |
| 693 | Rel = Reloc.getRawDataRefImpl(); |
| 694 | RE = info->O->getRelocation(Rel); |
| 695 | r_length = info->O->getAnyRelocationLength(RE); |
| 696 | r_scattered = info->O->isRelocationScattered(RE); |
| 697 | if (r_scattered) { |
| 698 | r_value = info->O->getScatteredRelocationValue(RE); |
| 699 | r_type = info->O->getScatteredRelocationType(RE); |
| 700 | } else { |
| 701 | r_type = info->O->getAnyRelocationType(RE); |
| 702 | isExtern = info->O->getPlainRelocationExternal(RE); |
| 703 | if (isExtern) { |
| 704 | symbol_iterator RelocSym = Reloc.getSymbol(); |
| 705 | Symbol = *RelocSym; |
| 706 | } |
| 707 | } |
| 708 | if (r_type == MachO::ARM_RELOC_HALF || |
| 709 | r_type == MachO::ARM_RELOC_SECTDIFF || |
| 710 | r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF || |
| 711 | r_type == MachO::ARM_RELOC_HALF_SECTDIFF) { |
| 712 | DataRefImpl RelNext = Rel; |
| 713 | info->O->moveRelocationNext(RelNext); |
| 714 | MachO::any_relocation_info RENext; |
| 715 | RENext = info->O->getRelocation(RelNext); |
| 716 | other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff; |
| 717 | if (info->O->isRelocationScattered(RENext)) |
| 718 | pair_r_value = info->O->getScatteredRelocationValue(RENext); |
| 719 | } |
| 720 | reloc_found = true; |
| 721 | break; |
| 722 | } |
| 723 | } |
| 724 | if (reloc_found && isExtern) { |
| 725 | StringRef SymName; |
| 726 | Symbol.getName(SymName); |
| 727 | const char *name = SymName.data(); |
| 728 | op_info->AddSymbol.Present = 1; |
| 729 | op_info->AddSymbol.Name = name; |
| 730 | if (value != 0) { |
| 731 | switch (r_type) { |
| 732 | case MachO::ARM_RELOC_HALF: |
| 733 | if ((r_length & 0x1) == 1) { |
| 734 | op_info->Value = value << 16 | other_half; |
| 735 | op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16; |
| 736 | } else { |
| 737 | op_info->Value = other_half << 16 | value; |
| 738 | op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16; |
| 739 | } |
| 740 | break; |
| 741 | default: |
| 742 | break; |
| 743 | } |
| 744 | } else { |
| 745 | switch (r_type) { |
| 746 | case MachO::ARM_RELOC_HALF: |
| 747 | if ((r_length & 0x1) == 1) { |
| 748 | op_info->Value = value << 16 | other_half; |
| 749 | op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16; |
| 750 | } else { |
| 751 | op_info->Value = other_half << 16 | value; |
| 752 | op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16; |
| 753 | } |
| 754 | break; |
| 755 | default: |
| 756 | break; |
| 757 | } |
| 758 | } |
| 759 | return 1; |
| 760 | } |
| 761 | // If we have a branch that is not an external relocation entry then |
| 762 | // return 0 so the code in tryAddingSymbolicOperand() can use the |
| 763 | // SymbolLookUp call back with the branch target address to look up the |
| 764 | // symbol and possiblity add an annotation for a symbol stub. |
| 765 | if (reloc_found && isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 || |
| 766 | r_type == MachO::ARM_THUMB_RELOC_BR22)) |
| 767 | return 0; |
| 768 | |
| 769 | uint32_t offset = 0; |
| 770 | if (reloc_found) { |
| 771 | if (r_type == MachO::ARM_RELOC_HALF || |
| 772 | r_type == MachO::ARM_RELOC_HALF_SECTDIFF) { |
| 773 | if ((r_length & 0x1) == 1) |
| 774 | value = value << 16 | other_half; |
| 775 | else |
| 776 | value = other_half << 16 | value; |
| 777 | } |
| 778 | if (r_scattered && (r_type != MachO::ARM_RELOC_HALF && |
| 779 | r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) { |
| 780 | offset = value - r_value; |
| 781 | value = r_value; |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | if (reloc_found && r_type == MachO::ARM_RELOC_HALF_SECTDIFF) { |
| 786 | if ((r_length & 0x1) == 1) |
| 787 | op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16; |
| 788 | else |
| 789 | op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16; |
| 790 | const char *add = GuessSymbolName(r_value, info); |
| 791 | const char *sub = GuessSymbolName(pair_r_value, info); |
| 792 | int32_t offset = value - (r_value - pair_r_value); |
| 793 | op_info->AddSymbol.Present = 1; |
| 794 | if (add != nullptr) |
| 795 | op_info->AddSymbol.Name = add; |
| 796 | else |
| 797 | op_info->AddSymbol.Value = r_value; |
| 798 | op_info->SubtractSymbol.Present = 1; |
| 799 | if (sub != nullptr) |
| 800 | op_info->SubtractSymbol.Name = sub; |
| 801 | else |
| 802 | op_info->SubtractSymbol.Value = pair_r_value; |
| 803 | op_info->Value = offset; |
| 804 | return 1; |
| 805 | } |
| 806 | |
| 807 | if (reloc_found == false) |
| 808 | return 0; |
| 809 | |
| 810 | op_info->AddSymbol.Present = 1; |
| 811 | op_info->Value = offset; |
| 812 | if (reloc_found) { |
| 813 | if (r_type == MachO::ARM_RELOC_HALF) { |
| 814 | if ((r_length & 0x1) == 1) |
| 815 | op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16; |
| 816 | else |
| 817 | op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16; |
| 818 | } |
| 819 | } |
| 820 | const char *add = GuessSymbolName(value, info); |
| 821 | if (add != nullptr) { |
| 822 | op_info->AddSymbol.Name = add; |
| 823 | return 1; |
| 824 | } |
| 825 | op_info->AddSymbol.Value = value; |
| 826 | return 1; |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 827 | } else if (Arch == Triple::aarch64) { |
Kevin Enderby | ae3c126 | 2014-11-14 21:52:18 +0000 | [diff] [blame] | 828 | if (Offset != 0 || Size != 4) |
| 829 | return 0; |
| 830 | // First search the section's relocation entries (if any) for an entry |
| 831 | // for this section offset. |
| 832 | uint64_t sect_addr = info->S.getAddress(); |
| 833 | uint64_t sect_offset = (Pc + Offset) - sect_addr; |
| 834 | bool reloc_found = false; |
| 835 | DataRefImpl Rel; |
| 836 | MachO::any_relocation_info RE; |
| 837 | bool isExtern = false; |
| 838 | SymbolRef Symbol; |
| 839 | uint32_t r_type = 0; |
| 840 | for (const RelocationRef &Reloc : info->S.relocations()) { |
| 841 | uint64_t RelocOffset; |
| 842 | Reloc.getOffset(RelocOffset); |
| 843 | if (RelocOffset == sect_offset) { |
| 844 | Rel = Reloc.getRawDataRefImpl(); |
| 845 | RE = info->O->getRelocation(Rel); |
| 846 | r_type = info->O->getAnyRelocationType(RE); |
| 847 | if (r_type == MachO::ARM64_RELOC_ADDEND) { |
| 848 | DataRefImpl RelNext = Rel; |
| 849 | info->O->moveRelocationNext(RelNext); |
| 850 | MachO::any_relocation_info RENext = info->O->getRelocation(RelNext); |
| 851 | if (value == 0) { |
| 852 | value = info->O->getPlainRelocationSymbolNum(RENext); |
| 853 | op_info->Value = value; |
| 854 | } |
| 855 | } |
| 856 | // NOTE: Scattered relocations don't exist on arm64. |
| 857 | isExtern = info->O->getPlainRelocationExternal(RE); |
| 858 | if (isExtern) { |
| 859 | symbol_iterator RelocSym = Reloc.getSymbol(); |
| 860 | Symbol = *RelocSym; |
| 861 | } |
| 862 | reloc_found = true; |
| 863 | break; |
| 864 | } |
| 865 | } |
| 866 | if (reloc_found && isExtern) { |
| 867 | StringRef SymName; |
| 868 | Symbol.getName(SymName); |
| 869 | const char *name = SymName.data(); |
| 870 | op_info->AddSymbol.Present = 1; |
| 871 | op_info->AddSymbol.Name = name; |
| 872 | |
| 873 | switch (r_type) { |
| 874 | case MachO::ARM64_RELOC_PAGE21: |
| 875 | /* @page */ |
| 876 | op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE; |
| 877 | break; |
| 878 | case MachO::ARM64_RELOC_PAGEOFF12: |
| 879 | /* @pageoff */ |
| 880 | op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF; |
| 881 | break; |
| 882 | case MachO::ARM64_RELOC_GOT_LOAD_PAGE21: |
| 883 | /* @gotpage */ |
| 884 | op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE; |
| 885 | break; |
| 886 | case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12: |
| 887 | /* @gotpageoff */ |
| 888 | op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF; |
| 889 | break; |
| 890 | case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21: |
| 891 | /* @tvlppage is not implemented in llvm-mc */ |
| 892 | op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP; |
| 893 | break; |
| 894 | case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12: |
| 895 | /* @tvlppageoff is not implemented in llvm-mc */ |
| 896 | op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF; |
| 897 | break; |
| 898 | default: |
| 899 | case MachO::ARM64_RELOC_BRANCH26: |
| 900 | op_info->VariantKind = LLVMDisassembler_VariantKind_None; |
| 901 | break; |
| 902 | } |
| 903 | return 1; |
| 904 | } |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 905 | return 0; |
| 906 | } else { |
| 907 | return 0; |
| 908 | } |
| 909 | } |
| 910 | |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 911 | // GuessCstringPointer is passed the address of what might be a pointer to a |
| 912 | // literal string in a cstring section. If that address is in a cstring section |
| 913 | // it returns a pointer to that string. Else it returns nullptr. |
| 914 | const char *GuessCstringPointer(uint64_t ReferenceValue, |
| 915 | struct DisassembleInfo *info) { |
| 916 | uint32_t LoadCommandCount = info->O->getHeader().ncmds; |
| 917 | MachOObjectFile::LoadCommandInfo Load = info->O->getFirstLoadCommandInfo(); |
| 918 | for (unsigned I = 0;; ++I) { |
| 919 | if (Load.C.cmd == MachO::LC_SEGMENT_64) { |
| 920 | MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load); |
| 921 | for (unsigned J = 0; J < Seg.nsects; ++J) { |
| 922 | MachO::section_64 Sec = info->O->getSection64(Load, J); |
| 923 | uint32_t section_type = Sec.flags & MachO::SECTION_TYPE; |
| 924 | if (section_type == MachO::S_CSTRING_LITERALS && |
| 925 | ReferenceValue >= Sec.addr && |
| 926 | ReferenceValue < Sec.addr + Sec.size) { |
| 927 | uint64_t sect_offset = ReferenceValue - Sec.addr; |
| 928 | uint64_t object_offset = Sec.offset + sect_offset; |
| 929 | StringRef MachOContents = info->O->getData(); |
| 930 | uint64_t object_size = MachOContents.size(); |
| 931 | const char *object_addr = (const char *)MachOContents.data(); |
| 932 | if (object_offset < object_size) { |
| 933 | const char *name = object_addr + object_offset; |
| 934 | return name; |
| 935 | } else { |
| 936 | return nullptr; |
| 937 | } |
| 938 | } |
| 939 | } |
| 940 | } else if (Load.C.cmd == MachO::LC_SEGMENT) { |
| 941 | MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load); |
| 942 | for (unsigned J = 0; J < Seg.nsects; ++J) { |
| 943 | MachO::section Sec = info->O->getSection(Load, J); |
| 944 | uint32_t section_type = Sec.flags & MachO::SECTION_TYPE; |
| 945 | if (section_type == MachO::S_CSTRING_LITERALS && |
| 946 | ReferenceValue >= Sec.addr && |
| 947 | ReferenceValue < Sec.addr + Sec.size) { |
| 948 | uint64_t sect_offset = ReferenceValue - Sec.addr; |
| 949 | uint64_t object_offset = Sec.offset + sect_offset; |
| 950 | StringRef MachOContents = info->O->getData(); |
| 951 | uint64_t object_size = MachOContents.size(); |
| 952 | const char *object_addr = (const char *)MachOContents.data(); |
| 953 | if (object_offset < object_size) { |
| 954 | const char *name = object_addr + object_offset; |
| 955 | return name; |
| 956 | } else { |
| 957 | return nullptr; |
| 958 | } |
| 959 | } |
| 960 | } |
| 961 | } |
| 962 | if (I == LoadCommandCount - 1) |
| 963 | break; |
| 964 | else |
| 965 | Load = info->O->getNextLoadCommandInfo(Load); |
| 966 | } |
| 967 | return nullptr; |
| 968 | } |
| 969 | |
Kevin Enderby | 8597488 | 2014-09-26 22:20:44 +0000 | [diff] [blame] | 970 | // GuessIndirectSymbol returns the name of the indirect symbol for the |
| 971 | // ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe |
| 972 | // an address of a symbol stub or a lazy or non-lazy pointer to associate the |
| 973 | // symbol name being referenced by the stub or pointer. |
| 974 | static const char *GuessIndirectSymbol(uint64_t ReferenceValue, |
| 975 | struct DisassembleInfo *info) { |
| 976 | uint32_t LoadCommandCount = info->O->getHeader().ncmds; |
| 977 | MachOObjectFile::LoadCommandInfo Load = info->O->getFirstLoadCommandInfo(); |
| 978 | MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand(); |
| 979 | MachO::symtab_command Symtab = info->O->getSymtabLoadCommand(); |
| 980 | for (unsigned I = 0;; ++I) { |
| 981 | if (Load.C.cmd == MachO::LC_SEGMENT_64) { |
| 982 | MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load); |
| 983 | for (unsigned J = 0; J < Seg.nsects; ++J) { |
| 984 | MachO::section_64 Sec = info->O->getSection64(Load, J); |
| 985 | uint32_t section_type = Sec.flags & MachO::SECTION_TYPE; |
| 986 | if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS || |
| 987 | section_type == MachO::S_LAZY_SYMBOL_POINTERS || |
| 988 | section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS || |
| 989 | section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS || |
| 990 | section_type == MachO::S_SYMBOL_STUBS) && |
| 991 | ReferenceValue >= Sec.addr && |
| 992 | ReferenceValue < Sec.addr + Sec.size) { |
| 993 | uint32_t stride; |
| 994 | if (section_type == MachO::S_SYMBOL_STUBS) |
| 995 | stride = Sec.reserved2; |
| 996 | else |
| 997 | stride = 8; |
| 998 | if (stride == 0) |
| 999 | return nullptr; |
| 1000 | uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride; |
| 1001 | if (index < Dysymtab.nindirectsyms) { |
| 1002 | uint32_t indirect_symbol = |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 1003 | info->O->getIndirectSymbolTableEntry(Dysymtab, index); |
Kevin Enderby | 8597488 | 2014-09-26 22:20:44 +0000 | [diff] [blame] | 1004 | if (indirect_symbol < Symtab.nsyms) { |
| 1005 | symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol); |
| 1006 | SymbolRef Symbol = *Sym; |
| 1007 | StringRef SymName; |
| 1008 | Symbol.getName(SymName); |
| 1009 | const char *name = SymName.data(); |
| 1010 | return name; |
| 1011 | } |
| 1012 | } |
| 1013 | } |
| 1014 | } |
| 1015 | } else if (Load.C.cmd == MachO::LC_SEGMENT) { |
| 1016 | MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load); |
| 1017 | for (unsigned J = 0; J < Seg.nsects; ++J) { |
| 1018 | MachO::section Sec = info->O->getSection(Load, J); |
| 1019 | uint32_t section_type = Sec.flags & MachO::SECTION_TYPE; |
| 1020 | if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS || |
| 1021 | section_type == MachO::S_LAZY_SYMBOL_POINTERS || |
| 1022 | section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS || |
| 1023 | section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS || |
| 1024 | section_type == MachO::S_SYMBOL_STUBS) && |
| 1025 | ReferenceValue >= Sec.addr && |
| 1026 | ReferenceValue < Sec.addr + Sec.size) { |
| 1027 | uint32_t stride; |
| 1028 | if (section_type == MachO::S_SYMBOL_STUBS) |
| 1029 | stride = Sec.reserved2; |
| 1030 | else |
| 1031 | stride = 4; |
| 1032 | if (stride == 0) |
| 1033 | return nullptr; |
| 1034 | uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride; |
| 1035 | if (index < Dysymtab.nindirectsyms) { |
| 1036 | uint32_t indirect_symbol = |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 1037 | info->O->getIndirectSymbolTableEntry(Dysymtab, index); |
Kevin Enderby | 8597488 | 2014-09-26 22:20:44 +0000 | [diff] [blame] | 1038 | if (indirect_symbol < Symtab.nsyms) { |
| 1039 | symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol); |
| 1040 | SymbolRef Symbol = *Sym; |
| 1041 | StringRef SymName; |
| 1042 | Symbol.getName(SymName); |
| 1043 | const char *name = SymName.data(); |
| 1044 | return name; |
| 1045 | } |
| 1046 | } |
| 1047 | } |
| 1048 | } |
| 1049 | } |
| 1050 | if (I == LoadCommandCount - 1) |
| 1051 | break; |
| 1052 | else |
| 1053 | Load = info->O->getNextLoadCommandInfo(Load); |
| 1054 | } |
| 1055 | return nullptr; |
| 1056 | } |
| 1057 | |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 1058 | // method_reference() is called passing it the ReferenceName that might be |
| 1059 | // a reference it to an Objective-C method call. If so then it allocates and |
| 1060 | // assembles a method call string with the values last seen and saved in |
| 1061 | // the DisassembleInfo's class_name and selector_name fields. This is saved |
| 1062 | // into the method field of the info and any previous string is free'ed. |
| 1063 | // Then the class_name field in the info is set to nullptr. The method call |
| 1064 | // string is set into ReferenceName and ReferenceType is set to |
| 1065 | // LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call |
| 1066 | // then both ReferenceType and ReferenceName are left unchanged. |
| 1067 | static void method_reference(struct DisassembleInfo *info, |
| 1068 | uint64_t *ReferenceType, |
| 1069 | const char **ReferenceName) { |
Kevin Enderby | ae3c126 | 2014-11-14 21:52:18 +0000 | [diff] [blame] | 1070 | unsigned int Arch = info->O->getArch(); |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 1071 | if (*ReferenceName != nullptr) { |
| 1072 | if (strcmp(*ReferenceName, "_objc_msgSend") == 0) { |
Kevin Enderby | ae3c126 | 2014-11-14 21:52:18 +0000 | [diff] [blame] | 1073 | if (info->selector_name != nullptr) { |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 1074 | if (info->method != nullptr) |
| 1075 | free(info->method); |
| 1076 | if (info->class_name != nullptr) { |
| 1077 | info->method = (char *)malloc(5 + strlen(info->class_name) + |
| 1078 | strlen(info->selector_name)); |
| 1079 | if (info->method != nullptr) { |
| 1080 | strcpy(info->method, "+["); |
| 1081 | strcat(info->method, info->class_name); |
| 1082 | strcat(info->method, " "); |
| 1083 | strcat(info->method, info->selector_name); |
| 1084 | strcat(info->method, "]"); |
| 1085 | *ReferenceName = info->method; |
| 1086 | *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message; |
| 1087 | } |
| 1088 | } else { |
| 1089 | info->method = (char *)malloc(9 + strlen(info->selector_name)); |
| 1090 | if (info->method != nullptr) { |
Kevin Enderby | ae3c126 | 2014-11-14 21:52:18 +0000 | [diff] [blame] | 1091 | if (Arch == Triple::x86_64) |
| 1092 | strcpy(info->method, "-[%rdi "); |
| 1093 | else if (Arch == Triple::aarch64) |
| 1094 | strcpy(info->method, "-[x0 "); |
| 1095 | else |
| 1096 | strcpy(info->method, "-[r? "); |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 1097 | strcat(info->method, info->selector_name); |
| 1098 | strcat(info->method, "]"); |
| 1099 | *ReferenceName = info->method; |
| 1100 | *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message; |
| 1101 | } |
| 1102 | } |
| 1103 | info->class_name = nullptr; |
| 1104 | } |
| 1105 | } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) { |
Kevin Enderby | ae3c126 | 2014-11-14 21:52:18 +0000 | [diff] [blame] | 1106 | if (info->selector_name != nullptr) { |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 1107 | if (info->method != nullptr) |
| 1108 | free(info->method); |
| 1109 | info->method = (char *)malloc(17 + strlen(info->selector_name)); |
| 1110 | if (info->method != nullptr) { |
Kevin Enderby | ae3c126 | 2014-11-14 21:52:18 +0000 | [diff] [blame] | 1111 | if (Arch == Triple::x86_64) |
| 1112 | strcpy(info->method, "-[[%rdi super] "); |
| 1113 | else if (Arch == Triple::aarch64) |
| 1114 | strcpy(info->method, "-[[x0 super] "); |
| 1115 | else |
| 1116 | strcpy(info->method, "-[[r? super] "); |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 1117 | strcat(info->method, info->selector_name); |
| 1118 | strcat(info->method, "]"); |
| 1119 | *ReferenceName = info->method; |
| 1120 | *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message; |
| 1121 | } |
| 1122 | info->class_name = nullptr; |
| 1123 | } |
| 1124 | } |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | // GuessPointerPointer() is passed the address of what might be a pointer to |
| 1129 | // a reference to an Objective-C class, selector, message ref or cfstring. |
| 1130 | // If so the value of the pointer is returned and one of the booleans are set |
| 1131 | // to true. If not zero is returned and all the booleans are set to false. |
| 1132 | static uint64_t GuessPointerPointer(uint64_t ReferenceValue, |
| 1133 | struct DisassembleInfo *info, |
| 1134 | bool &classref, bool &selref, bool &msgref, |
| 1135 | bool &cfstring) { |
| 1136 | classref = false; |
| 1137 | selref = false; |
| 1138 | msgref = false; |
| 1139 | cfstring = false; |
| 1140 | uint32_t LoadCommandCount = info->O->getHeader().ncmds; |
| 1141 | MachOObjectFile::LoadCommandInfo Load = info->O->getFirstLoadCommandInfo(); |
| 1142 | for (unsigned I = 0;; ++I) { |
| 1143 | if (Load.C.cmd == MachO::LC_SEGMENT_64) { |
| 1144 | MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load); |
| 1145 | for (unsigned J = 0; J < Seg.nsects; ++J) { |
| 1146 | MachO::section_64 Sec = info->O->getSection64(Load, J); |
| 1147 | if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 || |
| 1148 | strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 || |
| 1149 | strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 || |
| 1150 | strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 || |
| 1151 | strncmp(Sec.sectname, "__cfstring", 16) == 0) && |
| 1152 | ReferenceValue >= Sec.addr && |
| 1153 | ReferenceValue < Sec.addr + Sec.size) { |
| 1154 | uint64_t sect_offset = ReferenceValue - Sec.addr; |
| 1155 | uint64_t object_offset = Sec.offset + sect_offset; |
| 1156 | StringRef MachOContents = info->O->getData(); |
| 1157 | uint64_t object_size = MachOContents.size(); |
| 1158 | const char *object_addr = (const char *)MachOContents.data(); |
| 1159 | if (object_offset < object_size) { |
| 1160 | uint64_t pointer_value; |
| 1161 | memcpy(&pointer_value, object_addr + object_offset, |
| 1162 | sizeof(uint64_t)); |
| 1163 | if (info->O->isLittleEndian() != sys::IsLittleEndianHost) |
| 1164 | sys::swapByteOrder(pointer_value); |
| 1165 | if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0) |
| 1166 | selref = true; |
| 1167 | else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 || |
| 1168 | strncmp(Sec.sectname, "__objc_superrefs", 16) == 0) |
| 1169 | classref = true; |
| 1170 | else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 && |
| 1171 | ReferenceValue + 8 < Sec.addr + Sec.size) { |
| 1172 | msgref = true; |
| 1173 | memcpy(&pointer_value, object_addr + object_offset + 8, |
| 1174 | sizeof(uint64_t)); |
| 1175 | if (info->O->isLittleEndian() != sys::IsLittleEndianHost) |
| 1176 | sys::swapByteOrder(pointer_value); |
| 1177 | } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0) |
| 1178 | cfstring = true; |
| 1179 | return pointer_value; |
| 1180 | } else { |
| 1181 | return 0; |
| 1182 | } |
| 1183 | } |
| 1184 | } |
| 1185 | } |
| 1186 | // TODO: Look for LC_SEGMENT for 32-bit Mach-O files. |
| 1187 | if (I == LoadCommandCount - 1) |
| 1188 | break; |
| 1189 | else |
| 1190 | Load = info->O->getNextLoadCommandInfo(Load); |
| 1191 | } |
| 1192 | return 0; |
| 1193 | } |
| 1194 | |
| 1195 | // get_pointer_64 returns a pointer to the bytes in the object file at the |
| 1196 | // Address from a section in the Mach-O file. And indirectly returns the |
| 1197 | // offset into the section, number of bytes left in the section past the offset |
| 1198 | // and which section is was being referenced. If the Address is not in a |
| 1199 | // section nullptr is returned. |
| 1200 | const char *get_pointer_64(uint64_t Address, uint32_t &offset, uint32_t &left, |
| 1201 | SectionRef &S, DisassembleInfo *info) { |
| 1202 | offset = 0; |
| 1203 | left = 0; |
| 1204 | S = SectionRef(); |
| 1205 | for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) { |
| 1206 | uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress(); |
| 1207 | uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize(); |
| 1208 | if (Address >= SectAddress && Address < SectAddress + SectSize) { |
| 1209 | S = (*(info->Sections))[SectIdx]; |
| 1210 | offset = Address - SectAddress; |
| 1211 | left = SectSize - offset; |
| 1212 | StringRef SectContents; |
| 1213 | ((*(info->Sections))[SectIdx]).getContents(SectContents); |
| 1214 | return SectContents.data() + offset; |
| 1215 | } |
| 1216 | } |
| 1217 | return nullptr; |
| 1218 | } |
| 1219 | |
| 1220 | // get_symbol_64() returns the name of a symbol (or nullptr) and the address of |
| 1221 | // the symbol indirectly through n_value. Based on the relocation information |
| 1222 | // for the specified section offset in the specified section reference. |
| 1223 | const char *get_symbol_64(uint32_t sect_offset, SectionRef S, |
| 1224 | DisassembleInfo *info, uint64_t &n_value) { |
| 1225 | n_value = 0; |
| 1226 | if (info->verbose == false) |
| 1227 | return nullptr; |
| 1228 | |
| 1229 | // See if there is an external relocation entry at the sect_offset. |
| 1230 | bool reloc_found = false; |
| 1231 | DataRefImpl Rel; |
| 1232 | MachO::any_relocation_info RE; |
| 1233 | bool isExtern = false; |
| 1234 | SymbolRef Symbol; |
| 1235 | for (const RelocationRef &Reloc : S.relocations()) { |
| 1236 | uint64_t RelocOffset; |
| 1237 | Reloc.getOffset(RelocOffset); |
| 1238 | if (RelocOffset == sect_offset) { |
| 1239 | Rel = Reloc.getRawDataRefImpl(); |
| 1240 | RE = info->O->getRelocation(Rel); |
| 1241 | if (info->O->isRelocationScattered(RE)) |
| 1242 | continue; |
| 1243 | isExtern = info->O->getPlainRelocationExternal(RE); |
| 1244 | if (isExtern) { |
| 1245 | symbol_iterator RelocSym = Reloc.getSymbol(); |
| 1246 | Symbol = *RelocSym; |
| 1247 | } |
| 1248 | reloc_found = true; |
| 1249 | break; |
| 1250 | } |
| 1251 | } |
| 1252 | // If there is an external relocation entry for a symbol in this section |
| 1253 | // at this section_offset then use that symbol's value for the n_value |
| 1254 | // and return its name. |
| 1255 | const char *SymbolName = nullptr; |
| 1256 | if (reloc_found && isExtern) { |
| 1257 | Symbol.getAddress(n_value); |
| 1258 | StringRef name; |
| 1259 | Symbol.getName(name); |
| 1260 | if (!name.empty()) { |
| 1261 | SymbolName = name.data(); |
| 1262 | return SymbolName; |
| 1263 | } |
| 1264 | } |
| 1265 | |
| 1266 | // TODO: For fully linked images, look through the external relocation |
| 1267 | // entries off the dynamic symtab command. For these the r_offset is from the |
| 1268 | // start of the first writeable segment in the Mach-O file. So the offset |
| 1269 | // to this section from that segment is passed to this routine by the caller, |
| 1270 | // as the database_offset. Which is the difference of the section's starting |
| 1271 | // address and the first writable segment. |
| 1272 | // |
| 1273 | // NOTE: need add passing the database_offset to this routine. |
| 1274 | |
| 1275 | // TODO: We did not find an external relocation entry so look up the |
| 1276 | // ReferenceValue as an address of a symbol and if found return that symbol's |
| 1277 | // name. |
| 1278 | // |
| 1279 | // NOTE: need add passing the ReferenceValue to this routine. Then that code |
| 1280 | // would simply be this: |
Kevin Enderby | 9907d0a | 2014-11-04 00:43:16 +0000 | [diff] [blame] | 1281 | // SymbolName = GuessSymbolName(ReferenceValue, info); |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 1282 | |
| 1283 | return SymbolName; |
| 1284 | } |
| 1285 | |
| 1286 | // These are structs in the Objective-C meta data and read to produce the |
| 1287 | // comments for disassembly. While these are part of the ABI they are no |
| 1288 | // public defintions. So the are here not in include/llvm/Support/MachO.h . |
| 1289 | |
| 1290 | // The cfstring object in a 64-bit Mach-O file. |
| 1291 | struct cfstring64_t { |
| 1292 | uint64_t isa; // class64_t * (64-bit pointer) |
| 1293 | uint64_t flags; // flag bits |
| 1294 | uint64_t characters; // char * (64-bit pointer) |
| 1295 | uint64_t length; // number of non-NULL characters in above |
| 1296 | }; |
| 1297 | |
| 1298 | // The class object in a 64-bit Mach-O file. |
| 1299 | struct class64_t { |
| 1300 | uint64_t isa; // class64_t * (64-bit pointer) |
| 1301 | uint64_t superclass; // class64_t * (64-bit pointer) |
| 1302 | uint64_t cache; // Cache (64-bit pointer) |
| 1303 | uint64_t vtable; // IMP * (64-bit pointer) |
| 1304 | uint64_t data; // class_ro64_t * (64-bit pointer) |
| 1305 | }; |
| 1306 | |
| 1307 | struct class_ro64_t { |
| 1308 | uint32_t flags; |
| 1309 | uint32_t instanceStart; |
| 1310 | uint32_t instanceSize; |
| 1311 | uint32_t reserved; |
| 1312 | uint64_t ivarLayout; // const uint8_t * (64-bit pointer) |
| 1313 | uint64_t name; // const char * (64-bit pointer) |
| 1314 | uint64_t baseMethods; // const method_list_t * (64-bit pointer) |
| 1315 | uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer) |
| 1316 | uint64_t ivars; // const ivar_list_t * (64-bit pointer) |
| 1317 | uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer) |
| 1318 | uint64_t baseProperties; // const struct objc_property_list (64-bit pointer) |
| 1319 | }; |
| 1320 | |
| 1321 | inline void swapStruct(struct cfstring64_t &cfs) { |
| 1322 | sys::swapByteOrder(cfs.isa); |
| 1323 | sys::swapByteOrder(cfs.flags); |
| 1324 | sys::swapByteOrder(cfs.characters); |
| 1325 | sys::swapByteOrder(cfs.length); |
| 1326 | } |
| 1327 | |
| 1328 | inline void swapStruct(struct class64_t &c) { |
| 1329 | sys::swapByteOrder(c.isa); |
| 1330 | sys::swapByteOrder(c.superclass); |
| 1331 | sys::swapByteOrder(c.cache); |
| 1332 | sys::swapByteOrder(c.vtable); |
| 1333 | sys::swapByteOrder(c.data); |
| 1334 | } |
| 1335 | |
| 1336 | inline void swapStruct(struct class_ro64_t &cro) { |
| 1337 | sys::swapByteOrder(cro.flags); |
| 1338 | sys::swapByteOrder(cro.instanceStart); |
| 1339 | sys::swapByteOrder(cro.instanceSize); |
| 1340 | sys::swapByteOrder(cro.reserved); |
| 1341 | sys::swapByteOrder(cro.ivarLayout); |
| 1342 | sys::swapByteOrder(cro.name); |
| 1343 | sys::swapByteOrder(cro.baseMethods); |
| 1344 | sys::swapByteOrder(cro.baseProtocols); |
| 1345 | sys::swapByteOrder(cro.ivars); |
| 1346 | sys::swapByteOrder(cro.weakIvarLayout); |
| 1347 | sys::swapByteOrder(cro.baseProperties); |
| 1348 | } |
| 1349 | |
| 1350 | static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue, |
| 1351 | struct DisassembleInfo *info); |
| 1352 | |
| 1353 | // get_objc2_64bit_class_name() is used for disassembly and is passed a pointer |
| 1354 | // to an Objective-C class and returns the class name. It is also passed the |
| 1355 | // address of the pointer, so when the pointer is zero as it can be in an .o |
| 1356 | // file, that is used to look for an external relocation entry with a symbol |
| 1357 | // name. |
| 1358 | const char *get_objc2_64bit_class_name(uint64_t pointer_value, |
| 1359 | uint64_t ReferenceValue, |
| 1360 | struct DisassembleInfo *info) { |
| 1361 | const char *r; |
| 1362 | uint32_t offset, left; |
| 1363 | SectionRef S; |
| 1364 | |
| 1365 | // The pointer_value can be 0 in an object file and have a relocation |
| 1366 | // entry for the class symbol at the ReferenceValue (the address of the |
| 1367 | // pointer). |
| 1368 | if (pointer_value == 0) { |
| 1369 | r = get_pointer_64(ReferenceValue, offset, left, S, info); |
| 1370 | if (r == nullptr || left < sizeof(uint64_t)) |
| 1371 | return nullptr; |
| 1372 | uint64_t n_value; |
| 1373 | const char *symbol_name = get_symbol_64(offset, S, info, n_value); |
| 1374 | if (symbol_name == nullptr) |
| 1375 | return nullptr; |
Hans Wennborg | db53e30 | 2014-10-23 21:59:17 +0000 | [diff] [blame] | 1376 | const char *class_name = strrchr(symbol_name, '$'); |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 1377 | if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0') |
| 1378 | return class_name + 2; |
| 1379 | else |
| 1380 | return nullptr; |
| 1381 | } |
| 1382 | |
| 1383 | // The case were the pointer_value is non-zero and points to a class defined |
| 1384 | // in this Mach-O file. |
| 1385 | r = get_pointer_64(pointer_value, offset, left, S, info); |
| 1386 | if (r == nullptr || left < sizeof(struct class64_t)) |
| 1387 | return nullptr; |
| 1388 | struct class64_t c; |
| 1389 | memcpy(&c, r, sizeof(struct class64_t)); |
| 1390 | if (info->O->isLittleEndian() != sys::IsLittleEndianHost) |
| 1391 | swapStruct(c); |
| 1392 | if (c.data == 0) |
| 1393 | return nullptr; |
| 1394 | r = get_pointer_64(c.data, offset, left, S, info); |
| 1395 | if (r == nullptr || left < sizeof(struct class_ro64_t)) |
| 1396 | return nullptr; |
| 1397 | struct class_ro64_t cro; |
| 1398 | memcpy(&cro, r, sizeof(struct class_ro64_t)); |
| 1399 | if (info->O->isLittleEndian() != sys::IsLittleEndianHost) |
| 1400 | swapStruct(cro); |
| 1401 | if (cro.name == 0) |
| 1402 | return nullptr; |
| 1403 | const char *name = get_pointer_64(cro.name, offset, left, S, info); |
| 1404 | return name; |
| 1405 | } |
| 1406 | |
| 1407 | // get_objc2_64bit_cfstring_name is used for disassembly and is passed a |
| 1408 | // pointer to a cfstring and returns its name or nullptr. |
| 1409 | const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue, |
| 1410 | struct DisassembleInfo *info) { |
| 1411 | const char *r, *name; |
| 1412 | uint32_t offset, left; |
| 1413 | SectionRef S; |
| 1414 | struct cfstring64_t cfs; |
| 1415 | uint64_t cfs_characters; |
| 1416 | |
| 1417 | r = get_pointer_64(ReferenceValue, offset, left, S, info); |
| 1418 | if (r == nullptr || left < sizeof(struct cfstring64_t)) |
| 1419 | return nullptr; |
| 1420 | memcpy(&cfs, r, sizeof(struct cfstring64_t)); |
| 1421 | if (info->O->isLittleEndian() != sys::IsLittleEndianHost) |
| 1422 | swapStruct(cfs); |
| 1423 | if (cfs.characters == 0) { |
| 1424 | uint64_t n_value; |
| 1425 | const char *symbol_name = get_symbol_64( |
| 1426 | offset + offsetof(struct cfstring64_t, characters), S, info, n_value); |
| 1427 | if (symbol_name == nullptr) |
| 1428 | return nullptr; |
| 1429 | cfs_characters = n_value; |
| 1430 | } else |
| 1431 | cfs_characters = cfs.characters; |
| 1432 | name = get_pointer_64(cfs_characters, offset, left, S, info); |
| 1433 | |
| 1434 | return name; |
| 1435 | } |
| 1436 | |
| 1437 | // get_objc2_64bit_selref() is used for disassembly and is passed a the address |
| 1438 | // of a pointer to an Objective-C selector reference when the pointer value is |
| 1439 | // zero as in a .o file and is likely to have a external relocation entry with |
| 1440 | // who's symbol's n_value is the real pointer to the selector name. If that is |
| 1441 | // the case the real pointer to the selector name is returned else 0 is |
| 1442 | // returned |
| 1443 | uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue, |
| 1444 | struct DisassembleInfo *info) { |
| 1445 | uint32_t offset, left; |
| 1446 | SectionRef S; |
| 1447 | |
| 1448 | const char *r = get_pointer_64(ReferenceValue, offset, left, S, info); |
| 1449 | if (r == nullptr || left < sizeof(uint64_t)) |
| 1450 | return 0; |
| 1451 | uint64_t n_value; |
| 1452 | const char *symbol_name = get_symbol_64(offset, S, info, n_value); |
| 1453 | if (symbol_name == nullptr) |
| 1454 | return 0; |
| 1455 | return n_value; |
| 1456 | } |
| 1457 | |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 1458 | // GuessLiteralPointer returns a string which for the item in the Mach-O file |
| 1459 | // for the address passed in as ReferenceValue for printing as a comment with |
| 1460 | // the instruction and also returns the corresponding type of that item |
| 1461 | // indirectly through ReferenceType. |
| 1462 | // |
| 1463 | // If ReferenceValue is an address of literal cstring then a pointer to the |
| 1464 | // cstring is returned and ReferenceType is set to |
| 1465 | // LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr . |
| 1466 | // |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 1467 | // If ReferenceValue is an address of an Objective-C CFString, Selector ref or |
| 1468 | // Class ref that name is returned and the ReferenceType is set accordingly. |
| 1469 | // |
| 1470 | // Lastly, literals which are Symbol address in a literal pool are looked for |
| 1471 | // and if found the symbol name is returned and ReferenceType is set to |
| 1472 | // LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr . |
| 1473 | // |
| 1474 | // If there is no item in the Mach-O file for the address passed in as |
| 1475 | // ReferenceValue nullptr is returned and ReferenceType is unchanged. |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 1476 | const char *GuessLiteralPointer(uint64_t ReferenceValue, uint64_t ReferencePC, |
| 1477 | uint64_t *ReferenceType, |
| 1478 | struct DisassembleInfo *info) { |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 1479 | // First see if there is an external relocation entry at the ReferencePC. |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1480 | uint64_t sect_addr = info->S.getAddress(); |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 1481 | uint64_t sect_offset = ReferencePC - sect_addr; |
| 1482 | bool reloc_found = false; |
| 1483 | DataRefImpl Rel; |
| 1484 | MachO::any_relocation_info RE; |
| 1485 | bool isExtern = false; |
| 1486 | SymbolRef Symbol; |
| 1487 | for (const RelocationRef &Reloc : info->S.relocations()) { |
| 1488 | uint64_t RelocOffset; |
| 1489 | Reloc.getOffset(RelocOffset); |
| 1490 | if (RelocOffset == sect_offset) { |
| 1491 | Rel = Reloc.getRawDataRefImpl(); |
| 1492 | RE = info->O->getRelocation(Rel); |
| 1493 | if (info->O->isRelocationScattered(RE)) |
| 1494 | continue; |
| 1495 | isExtern = info->O->getPlainRelocationExternal(RE); |
| 1496 | if (isExtern) { |
| 1497 | symbol_iterator RelocSym = Reloc.getSymbol(); |
| 1498 | Symbol = *RelocSym; |
| 1499 | } |
| 1500 | reloc_found = true; |
| 1501 | break; |
| 1502 | } |
| 1503 | } |
| 1504 | // If there is an external relocation entry for a symbol in a section |
| 1505 | // then used that symbol's value for the value of the reference. |
| 1506 | if (reloc_found && isExtern) { |
| 1507 | if (info->O->getAnyRelocationPCRel(RE)) { |
| 1508 | unsigned Type = info->O->getAnyRelocationType(RE); |
| 1509 | if (Type == MachO::X86_64_RELOC_SIGNED) { |
| 1510 | Symbol.getAddress(ReferenceValue); |
| 1511 | } |
| 1512 | } |
| 1513 | } |
| 1514 | |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 1515 | // Look for literals such as Objective-C CFStrings refs, Selector refs, |
| 1516 | // Message refs and Class refs. |
| 1517 | bool classref, selref, msgref, cfstring; |
| 1518 | uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref, |
| 1519 | selref, msgref, cfstring); |
| 1520 | if (classref == true && pointer_value == 0) { |
| 1521 | // Note the ReferenceValue is a pointer into the __objc_classrefs section. |
| 1522 | // And the pointer_value in that section is typically zero as it will be |
| 1523 | // set by dyld as part of the "bind information". |
| 1524 | const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info); |
| 1525 | if (name != nullptr) { |
| 1526 | *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref; |
Hans Wennborg | db53e30 | 2014-10-23 21:59:17 +0000 | [diff] [blame] | 1527 | const char *class_name = strrchr(name, '$'); |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 1528 | if (class_name != nullptr && class_name[1] == '_' && |
| 1529 | class_name[2] != '\0') { |
| 1530 | info->class_name = class_name + 2; |
| 1531 | return name; |
| 1532 | } |
| 1533 | } |
| 1534 | } |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 1535 | |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 1536 | if (classref == true) { |
| 1537 | *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref; |
| 1538 | const char *name = |
| 1539 | get_objc2_64bit_class_name(pointer_value, ReferenceValue, info); |
| 1540 | if (name != nullptr) |
| 1541 | info->class_name = name; |
| 1542 | else |
| 1543 | name = "bad class ref"; |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 1544 | return name; |
| 1545 | } |
| 1546 | |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 1547 | if (cfstring == true) { |
| 1548 | *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref; |
| 1549 | const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info); |
| 1550 | return name; |
| 1551 | } |
| 1552 | |
| 1553 | if (selref == true && pointer_value == 0) |
| 1554 | pointer_value = get_objc2_64bit_selref(ReferenceValue, info); |
| 1555 | |
| 1556 | if (pointer_value != 0) |
| 1557 | ReferenceValue = pointer_value; |
| 1558 | |
| 1559 | const char *name = GuessCstringPointer(ReferenceValue, info); |
| 1560 | if (name) { |
| 1561 | if (pointer_value != 0 && selref == true) { |
| 1562 | *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref; |
| 1563 | info->selector_name = name; |
| 1564 | } else if (pointer_value != 0 && msgref == true) { |
| 1565 | info->class_name = nullptr; |
| 1566 | *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref; |
| 1567 | info->selector_name = name; |
| 1568 | } else |
| 1569 | *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr; |
| 1570 | return name; |
| 1571 | } |
| 1572 | |
| 1573 | // Lastly look for an indirect symbol with this ReferenceValue which is in |
| 1574 | // a literal pool. If found return that symbol name. |
| 1575 | name = GuessIndirectSymbol(ReferenceValue, info); |
| 1576 | if (name) { |
| 1577 | *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr; |
| 1578 | return name; |
| 1579 | } |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 1580 | |
| 1581 | return nullptr; |
| 1582 | } |
| 1583 | |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 1584 | // SymbolizerSymbolLookUp is the symbol lookup function passed when creating |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 1585 | // the Symbolizer. It looks up the ReferenceValue using the info passed via the |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 1586 | // pointer to the struct DisassembleInfo that was passed when MCSymbolizer |
| 1587 | // is created and returns the symbol name that matches the ReferenceValue or |
| 1588 | // nullptr if none. The ReferenceType is passed in for the IN type of |
| 1589 | // reference the instruction is making from the values in defined in the header |
| 1590 | // "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific |
| 1591 | // Out type and the ReferenceName will also be set which is added as a comment |
| 1592 | // to the disassembled instruction. |
| 1593 | // |
Kevin Enderby | 04bf693 | 2014-10-28 23:39:46 +0000 | [diff] [blame] | 1594 | #if HAVE_CXXABI_H |
| 1595 | // If the symbol name is a C++ mangled name then the demangled name is |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 1596 | // returned through ReferenceName and ReferenceType is set to |
| 1597 | // LLVMDisassembler_ReferenceType_DeMangled_Name . |
Kevin Enderby | 04bf693 | 2014-10-28 23:39:46 +0000 | [diff] [blame] | 1598 | #endif |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 1599 | // |
| 1600 | // When this is called to get a symbol name for a branch target then the |
| 1601 | // ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then |
| 1602 | // SymbolValue will be looked for in the indirect symbol table to determine if |
| 1603 | // it is an address for a symbol stub. If so then the symbol name for that |
| 1604 | // stub is returned indirectly through ReferenceName and then ReferenceType is |
| 1605 | // set to LLVMDisassembler_ReferenceType_Out_SymbolStub. |
| 1606 | // |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 1607 | // When this is called with an value loaded via a PC relative load then |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 1608 | // ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the |
| 1609 | // SymbolValue is checked to be an address of literal pointer, symbol pointer, |
| 1610 | // or an Objective-C meta data reference. If so the output ReferenceType is |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 1611 | // set to correspond to that as well as setting the ReferenceName. |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 1612 | const char *SymbolizerSymbolLookUp(void *DisInfo, uint64_t ReferenceValue, |
| 1613 | uint64_t *ReferenceType, |
| 1614 | uint64_t ReferencePC, |
| 1615 | const char **ReferenceName) { |
| 1616 | struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo; |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 1617 | // If no verbose symbolic information is wanted then just return nullptr. |
| 1618 | if (info->verbose == false) { |
| 1619 | *ReferenceName = nullptr; |
| 1620 | *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 1621 | return nullptr; |
| 1622 | } |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 1623 | |
Kevin Enderby | 9907d0a | 2014-11-04 00:43:16 +0000 | [diff] [blame] | 1624 | const char *SymbolName = GuessSymbolName(ReferenceValue, info); |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 1625 | |
Kevin Enderby | 8597488 | 2014-09-26 22:20:44 +0000 | [diff] [blame] | 1626 | if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) { |
| 1627 | *ReferenceName = GuessIndirectSymbol(ReferenceValue, info); |
Kevin Enderby | 04bf693 | 2014-10-28 23:39:46 +0000 | [diff] [blame] | 1628 | if (*ReferenceName != nullptr) { |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 1629 | method_reference(info, ReferenceType, ReferenceName); |
| 1630 | if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message) |
| 1631 | *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub; |
| 1632 | } else |
Kevin Enderby | 04bf693 | 2014-10-28 23:39:46 +0000 | [diff] [blame] | 1633 | #if HAVE_CXXABI_H |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 1634 | if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) { |
Kevin Enderby | 04bf693 | 2014-10-28 23:39:46 +0000 | [diff] [blame] | 1635 | if (info->demangled_name != nullptr) |
| 1636 | free(info->demangled_name); |
| 1637 | int status; |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 1638 | info->demangled_name = |
| 1639 | abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status); |
Kevin Enderby | 04bf693 | 2014-10-28 23:39:46 +0000 | [diff] [blame] | 1640 | if (info->demangled_name != nullptr) { |
| 1641 | *ReferenceName = info->demangled_name; |
| 1642 | *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name; |
| 1643 | } else |
| 1644 | *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; |
| 1645 | } else |
| 1646 | #endif |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 1647 | *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; |
| 1648 | } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) { |
| 1649 | *ReferenceName = |
| 1650 | GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info); |
Kevin Enderby | 8597488 | 2014-09-26 22:20:44 +0000 | [diff] [blame] | 1651 | if (*ReferenceName) |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 1652 | method_reference(info, ReferenceType, ReferenceName); |
Kevin Enderby | 8597488 | 2014-09-26 22:20:44 +0000 | [diff] [blame] | 1653 | else |
| 1654 | *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; |
Kevin Enderby | ae3c126 | 2014-11-14 21:52:18 +0000 | [diff] [blame] | 1655 | // If this is arm64 and the reference is an adrp instruction save the |
| 1656 | // instruction, passed in ReferenceValue and the address of the instruction |
| 1657 | // for use later if we see and add immediate instruction. |
| 1658 | } else if (info->O->getArch() == Triple::aarch64 && |
| 1659 | *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) { |
| 1660 | info->adrp_inst = ReferenceValue; |
| 1661 | info->adrp_addr = ReferencePC; |
| 1662 | SymbolName = nullptr; |
| 1663 | *ReferenceName = nullptr; |
| 1664 | *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; |
| 1665 | // If this is arm64 and reference is an add immediate instruction and we |
| 1666 | // have |
| 1667 | // seen an adrp instruction just before it and the adrp's Xd register |
| 1668 | // matches |
| 1669 | // this add's Xn register reconstruct the value being referenced and look to |
| 1670 | // see if it is a literal pointer. Note the add immediate instruction is |
| 1671 | // passed in ReferenceValue. |
| 1672 | } else if (info->O->getArch() == Triple::aarch64 && |
| 1673 | *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri && |
| 1674 | ReferencePC - 4 == info->adrp_addr && |
| 1675 | (info->adrp_inst & 0x9f000000) == 0x90000000 && |
| 1676 | (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) { |
| 1677 | uint32_t addxri_inst; |
| 1678 | uint64_t adrp_imm, addxri_imm; |
| 1679 | |
| 1680 | adrp_imm = |
| 1681 | ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3); |
| 1682 | if (info->adrp_inst & 0x0200000) |
| 1683 | adrp_imm |= 0xfffffffffc000000LL; |
| 1684 | |
| 1685 | addxri_inst = ReferenceValue; |
| 1686 | addxri_imm = (addxri_inst >> 10) & 0xfff; |
| 1687 | if (((addxri_inst >> 22) & 0x3) == 1) |
| 1688 | addxri_imm <<= 12; |
| 1689 | |
| 1690 | ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) + |
| 1691 | (adrp_imm << 12) + addxri_imm; |
| 1692 | |
| 1693 | *ReferenceName = |
| 1694 | GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info); |
| 1695 | if (*ReferenceName == nullptr) |
| 1696 | *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; |
| 1697 | // If this is arm64 and the reference is a load register instruction and we |
| 1698 | // have seen an adrp instruction just before it and the adrp's Xd register |
| 1699 | // matches this add's Xn register reconstruct the value being referenced and |
| 1700 | // look to see if it is a literal pointer. Note the load register |
| 1701 | // instruction is passed in ReferenceValue. |
| 1702 | } else if (info->O->getArch() == Triple::aarch64 && |
| 1703 | *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui && |
| 1704 | ReferencePC - 4 == info->adrp_addr && |
| 1705 | (info->adrp_inst & 0x9f000000) == 0x90000000 && |
| 1706 | (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) { |
| 1707 | uint32_t ldrxui_inst; |
| 1708 | uint64_t adrp_imm, ldrxui_imm; |
| 1709 | |
| 1710 | adrp_imm = |
| 1711 | ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3); |
| 1712 | if (info->adrp_inst & 0x0200000) |
| 1713 | adrp_imm |= 0xfffffffffc000000LL; |
| 1714 | |
| 1715 | ldrxui_inst = ReferenceValue; |
| 1716 | ldrxui_imm = (ldrxui_inst >> 10) & 0xfff; |
| 1717 | |
| 1718 | ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) + |
| 1719 | (adrp_imm << 12) + (ldrxui_imm << 3); |
| 1720 | |
| 1721 | *ReferenceName = |
| 1722 | GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info); |
| 1723 | if (*ReferenceName == nullptr) |
| 1724 | *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; |
| 1725 | } |
| 1726 | // If this arm64 and is an load register (PC-relative) instruction the |
| 1727 | // ReferenceValue is the PC plus the immediate value. |
| 1728 | else if (info->O->getArch() == Triple::aarch64 && |
| 1729 | (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl || |
| 1730 | *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR)) { |
| 1731 | *ReferenceName = |
| 1732 | GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info); |
| 1733 | if (*ReferenceName == nullptr) |
| 1734 | *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; |
Kevin Enderby | 8597488 | 2014-09-26 22:20:44 +0000 | [diff] [blame] | 1735 | } |
Kevin Enderby | 04bf693 | 2014-10-28 23:39:46 +0000 | [diff] [blame] | 1736 | #if HAVE_CXXABI_H |
| 1737 | else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) { |
| 1738 | if (info->demangled_name != nullptr) |
| 1739 | free(info->demangled_name); |
| 1740 | int status; |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 1741 | info->demangled_name = |
| 1742 | abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status); |
Kevin Enderby | 04bf693 | 2014-10-28 23:39:46 +0000 | [diff] [blame] | 1743 | if (info->demangled_name != nullptr) { |
| 1744 | *ReferenceName = info->demangled_name; |
| 1745 | *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name; |
| 1746 | } |
| 1747 | } |
| 1748 | #endif |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 1749 | else { |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 1750 | *ReferenceName = nullptr; |
| 1751 | *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None; |
| 1752 | } |
| 1753 | |
| 1754 | return SymbolName; |
| 1755 | } |
| 1756 | |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 1757 | /// \brief Emits the comments that are stored in the CommentStream. |
| 1758 | /// Each comment in the CommentStream must end with a newline. |
| 1759 | static void emitComments(raw_svector_ostream &CommentStream, |
| 1760 | SmallString<128> &CommentsToEmit, |
| 1761 | formatted_raw_ostream &FormattedOS, |
| 1762 | const MCAsmInfo &MAI) { |
| 1763 | // Flush the stream before taking its content. |
| 1764 | CommentStream.flush(); |
| 1765 | StringRef Comments = CommentsToEmit.str(); |
| 1766 | // Get the default information for printing a comment. |
| 1767 | const char *CommentBegin = MAI.getCommentString(); |
| 1768 | unsigned CommentColumn = MAI.getCommentColumn(); |
| 1769 | bool IsFirst = true; |
| 1770 | while (!Comments.empty()) { |
| 1771 | if (!IsFirst) |
| 1772 | FormattedOS << '\n'; |
| 1773 | // Emit a line of comments. |
| 1774 | FormattedOS.PadToColumn(CommentColumn); |
| 1775 | size_t Position = Comments.find('\n'); |
| 1776 | FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position); |
| 1777 | // Move after the newline character. |
| 1778 | Comments = Comments.substr(Position + 1); |
| 1779 | IsFirst = false; |
| 1780 | } |
| 1781 | FormattedOS.flush(); |
| 1782 | |
| 1783 | // Tell the comment stream that the vector changed underneath it. |
| 1784 | CommentsToEmit.clear(); |
| 1785 | CommentStream.resync(); |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 1786 | } |
| 1787 | |
Kevin Enderby | ef3ad2f | 2014-12-04 23:56:27 +0000 | [diff] [blame] | 1788 | static void DisassembleInputMachO2(StringRef Filename, MachOObjectFile *MachOOF, |
| 1789 | StringRef ArchiveMemberName, |
| 1790 | StringRef ArchitectureName) { |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 1791 | const char *McpuDefault = nullptr; |
| 1792 | const Target *ThumbTarget = nullptr; |
| 1793 | const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 1794 | if (!TheTarget) { |
| 1795 | // GetTarget prints out stuff. |
| 1796 | return; |
| 1797 | } |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 1798 | if (MCPU.empty() && McpuDefault) |
| 1799 | MCPU = McpuDefault; |
| 1800 | |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 1801 | std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo()); |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 1802 | std::unique_ptr<const MCInstrInfo> ThumbInstrInfo; |
Kevin Enderby | ae3c126 | 2014-11-14 21:52:18 +0000 | [diff] [blame] | 1803 | if (ThumbTarget) |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 1804 | ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo()); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 1805 | |
Kevin Enderby | c959562 | 2014-08-06 23:24:41 +0000 | [diff] [blame] | 1806 | // Package up features to be passed to target/subtarget |
| 1807 | std::string FeaturesStr; |
| 1808 | if (MAttrs.size()) { |
| 1809 | SubtargetFeatures Features; |
| 1810 | for (unsigned i = 0; i != MAttrs.size(); ++i) |
| 1811 | Features.AddFeature(MAttrs[i]); |
| 1812 | FeaturesStr = Features.getString(); |
| 1813 | } |
| 1814 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 1815 | // Set up disassembler. |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 1816 | std::unique_ptr<const MCRegisterInfo> MRI( |
| 1817 | TheTarget->createMCRegInfo(TripleName)); |
| 1818 | std::unique_ptr<const MCAsmInfo> AsmInfo( |
Rafael Espindola | 227144c | 2013-05-13 01:16:13 +0000 | [diff] [blame] | 1819 | TheTarget->createMCAsmInfo(*MRI, TripleName)); |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 1820 | std::unique_ptr<const MCSubtargetInfo> STI( |
Kevin Enderby | c959562 | 2014-08-06 23:24:41 +0000 | [diff] [blame] | 1821 | TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr)); |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 1822 | MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr); |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 1823 | std::unique_ptr<MCDisassembler> DisAsm( |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 1824 | TheTarget->createMCDisassembler(*STI, Ctx)); |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 1825 | std::unique_ptr<MCSymbolizer> Symbolizer; |
| 1826 | struct DisassembleInfo SymbolizerInfo; |
| 1827 | std::unique_ptr<MCRelocationInfo> RelInfo( |
| 1828 | TheTarget->createMCRelocationInfo(TripleName, Ctx)); |
| 1829 | if (RelInfo) { |
| 1830 | Symbolizer.reset(TheTarget->createMCSymbolizer( |
| 1831 | TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp, |
| 1832 | &SymbolizerInfo, &Ctx, RelInfo.release())); |
| 1833 | DisAsm->setSymbolizer(std::move(Symbolizer)); |
| 1834 | } |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 1835 | int AsmPrinterVariant = AsmInfo->getAssemblerDialect(); |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 1836 | std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter( |
| 1837 | AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI, *STI)); |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 1838 | // Set the display preference for hex vs. decimal immediates. |
| 1839 | IP->setPrintImmHex(PrintImmHex); |
| 1840 | // Comment stream and backing vector. |
| 1841 | SmallString<128> CommentsToEmit; |
| 1842 | raw_svector_ostream CommentStream(CommentsToEmit); |
Kevin Enderby | ef3ad2f | 2014-12-04 23:56:27 +0000 | [diff] [blame] | 1843 | // FIXME: Setting the CommentStream in the InstPrinter is problematic in that |
| 1844 | // if it is done then arm64 comments for string literals don't get printed |
| 1845 | // and some constant get printed instead and not setting it causes intel |
| 1846 | // (32-bit and 64-bit) comments printed with different spacing before the |
| 1847 | // comment causing different diffs with the 'C' disassembler library API. |
| 1848 | // IP->setCommentStream(CommentStream); |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 1849 | |
Kevin Enderby | ae3c126 | 2014-11-14 21:52:18 +0000 | [diff] [blame] | 1850 | if (!AsmInfo || !STI || !DisAsm || !IP) { |
Michael J. Spencer | c1363cf | 2011-10-07 19:25:47 +0000 | [diff] [blame] | 1851 | errs() << "error: couldn't initialize disassembler for target " |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 1852 | << TripleName << '\n'; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 1853 | return; |
| 1854 | } |
| 1855 | |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 1856 | // Set up thumb disassembler. |
| 1857 | std::unique_ptr<const MCRegisterInfo> ThumbMRI; |
| 1858 | std::unique_ptr<const MCAsmInfo> ThumbAsmInfo; |
| 1859 | std::unique_ptr<const MCSubtargetInfo> ThumbSTI; |
Kevin Enderby | 930fdc7 | 2014-11-06 19:00:13 +0000 | [diff] [blame] | 1860 | std::unique_ptr<MCDisassembler> ThumbDisAsm; |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 1861 | std::unique_ptr<MCInstPrinter> ThumbIP; |
| 1862 | std::unique_ptr<MCContext> ThumbCtx; |
Kevin Enderby | 930fdc7 | 2014-11-06 19:00:13 +0000 | [diff] [blame] | 1863 | std::unique_ptr<MCSymbolizer> ThumbSymbolizer; |
| 1864 | struct DisassembleInfo ThumbSymbolizerInfo; |
| 1865 | std::unique_ptr<MCRelocationInfo> ThumbRelInfo; |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 1866 | if (ThumbTarget) { |
| 1867 | ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName)); |
| 1868 | ThumbAsmInfo.reset( |
| 1869 | ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName)); |
| 1870 | ThumbSTI.reset( |
| 1871 | ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MCPU, FeaturesStr)); |
| 1872 | ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr)); |
| 1873 | ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx)); |
Kevin Enderby | 930fdc7 | 2014-11-06 19:00:13 +0000 | [diff] [blame] | 1874 | MCContext *PtrThumbCtx = ThumbCtx.get(); |
| 1875 | ThumbRelInfo.reset( |
| 1876 | ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx)); |
| 1877 | if (ThumbRelInfo) { |
| 1878 | ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer( |
| 1879 | ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp, |
| 1880 | &ThumbSymbolizerInfo, PtrThumbCtx, ThumbRelInfo.release())); |
| 1881 | ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer)); |
| 1882 | } |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 1883 | int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect(); |
| 1884 | ThumbIP.reset(ThumbTarget->createMCInstPrinter( |
| 1885 | ThumbAsmPrinterVariant, *ThumbAsmInfo, *ThumbInstrInfo, *ThumbMRI, |
| 1886 | *ThumbSTI)); |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 1887 | // Set the display preference for hex vs. decimal immediates. |
| 1888 | ThumbIP->setPrintImmHex(PrintImmHex); |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
Kevin Enderby | ae3c126 | 2014-11-14 21:52:18 +0000 | [diff] [blame] | 1891 | if (ThumbTarget && (!ThumbAsmInfo || !ThumbSTI || !ThumbDisAsm || !ThumbIP)) { |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 1892 | errs() << "error: couldn't initialize disassembler for target " |
| 1893 | << ThumbTripleName << '\n'; |
| 1894 | return; |
| 1895 | } |
| 1896 | |
Kevin Enderby | ef3ad2f | 2014-12-04 23:56:27 +0000 | [diff] [blame] | 1897 | outs() << Filename; |
| 1898 | if (!ArchiveMemberName.empty()) |
| 1899 | outs() << '(' << ArchiveMemberName << ')'; |
| 1900 | if (!ArchitectureName.empty()) |
| 1901 | outs() << " (architecture " << ArchitectureName << ")"; |
| 1902 | outs() << ":\n"; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 1903 | |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1904 | MachO::mach_header Header = MachOOF->getHeader(); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 1905 | |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 1906 | // FIXME: Using the -cfg command line option, this code used to be able to |
| 1907 | // annotate relocations with the referenced symbol's name, and if this was |
| 1908 | // inside a __[cf]string section, the data it points to. This is now replaced |
| 1909 | // by the upcoming MCSymbolizer, which needs the appropriate setup done above. |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 1910 | std::vector<SectionRef> Sections; |
| 1911 | std::vector<SymbolRef> Symbols; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 1912 | SmallVector<uint64_t, 8> FoundFns; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 1913 | uint64_t BaseSegmentAddress; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 1914 | |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 1915 | getSectionsAndSymbols(Header, MachOOF, Sections, Symbols, FoundFns, |
| 1916 | BaseSegmentAddress); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 1917 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 1918 | // 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] | 1919 | std::sort(Symbols.begin(), Symbols.end(), SymbolSorter()); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 1920 | |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 1921 | // Build a data in code table that is sorted on by the address of each entry. |
| 1922 | uint64_t BaseAddress = 0; |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 1923 | if (Header.filetype == MachO::MH_OBJECT) |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1924 | BaseAddress = Sections[0].getAddress(); |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 1925 | else |
| 1926 | BaseAddress = BaseSegmentAddress; |
| 1927 | DiceTable Dices; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 1928 | for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices(); |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 1929 | DI != DE; ++DI) { |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 1930 | uint32_t Offset; |
| 1931 | DI->getOffset(Offset); |
| 1932 | Dices.push_back(std::make_pair(BaseAddress + Offset, *DI)); |
| 1933 | } |
| 1934 | array_pod_sort(Dices.begin(), Dices.end()); |
| 1935 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 1936 | #ifndef NDEBUG |
| 1937 | raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls(); |
| 1938 | #else |
| 1939 | raw_ostream &DebugOut = nulls(); |
| 1940 | #endif |
| 1941 | |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 1942 | std::unique_ptr<DIContext> diContext; |
Rafael Espindola | 9b70925 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 1943 | ObjectFile *DbgObj = MachOOF; |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 1944 | // Try to find debug info and set up the DIContext for it. |
| 1945 | if (UseDbg) { |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 1946 | // A separate DSym file path was specified, parse it as a macho file, |
| 1947 | // get the sections and supply it to the section name parsing machinery. |
| 1948 | if (!DSYMFile.empty()) { |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 1949 | ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr = |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 1950 | MemoryBuffer::getFileOrSTDIN(DSYMFile); |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 1951 | if (std::error_code EC = BufOrErr.getError()) { |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 1952 | errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n'; |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 1953 | return; |
| 1954 | } |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 1955 | DbgObj = |
| 1956 | ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef()) |
| 1957 | .get() |
| 1958 | .release(); |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 1959 | } |
| 1960 | |
Eric Christopher | 7370b55 | 2012-11-12 21:40:38 +0000 | [diff] [blame] | 1961 | // Setup the DIContext |
Rafael Espindola | a04bb5b | 2014-07-31 20:19:36 +0000 | [diff] [blame] | 1962 | diContext.reset(DIContext::getDWARFContext(*DbgObj)); |
Benjamin Kramer | 699128e | 2011-09-21 01:13:19 +0000 | [diff] [blame] | 1963 | } |
| 1964 | |
Kevin Enderby | ef3ad2f | 2014-12-04 23:56:27 +0000 | [diff] [blame] | 1965 | // TODO: For now this only disassembles the (__TEXT,__text) section (see the |
| 1966 | // checks in the code below at the top of this loop). It should allow a |
| 1967 | // darwin otool(1) like -s option to disassemble any named segment & section |
| 1968 | // that is marked as containing instructions with the attributes |
| 1969 | // S_ATTR_PURE_INSTRUCTIONS or S_ATTR_SOME_INSTRUCTIONS in the flags field of |
| 1970 | // the section structure. |
| 1971 | outs() << "(__TEXT,__text) section\n"; |
| 1972 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 1973 | for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) { |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 1974 | |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1975 | bool SectIsText = Sections[SectIdx].isText(); |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 1976 | if (SectIsText == false) |
| 1977 | continue; |
| 1978 | |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 1979 | StringRef SectName; |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 1980 | if (Sections[SectIdx].getName(SectName) || SectName != "__text") |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 1981 | continue; // Skip non-text sections |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 1982 | |
Rafael Espindola | a9f810b | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 1983 | DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl(); |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 1984 | |
Rafael Espindola | b0f76a4 | 2013-04-05 15:15:22 +0000 | [diff] [blame] | 1985 | StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR); |
| 1986 | if (SegmentName != "__TEXT") |
Rafael Espindola | a9f810b | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 1987 | continue; |
| 1988 | |
Rafael Espindola | 7fc5b87 | 2014-11-12 02:04:27 +0000 | [diff] [blame] | 1989 | StringRef BytesStr; |
| 1990 | Sections[SectIdx].getContents(BytesStr); |
Aaron Ballman | 106fd7b | 2014-11-12 14:01:17 +0000 | [diff] [blame] | 1991 | ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()), |
| 1992 | BytesStr.size()); |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1993 | uint64_t SectAddress = Sections[SectIdx].getAddress(); |
Rafael Espindola | bd604f2 | 2014-11-07 00:52:15 +0000 | [diff] [blame] | 1994 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 1995 | bool symbolTableWorked = false; |
| 1996 | |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 1997 | // Parse relocations. |
Alexey Samsonov | aa4d295 | 2014-03-14 14:22:49 +0000 | [diff] [blame] | 1998 | std::vector<std::pair<uint64_t, SymbolRef>> Relocs; |
| 1999 | for (const RelocationRef &Reloc : Sections[SectIdx].relocations()) { |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 2000 | uint64_t RelocOffset; |
Alexey Samsonov | aa4d295 | 2014-03-14 14:22:49 +0000 | [diff] [blame] | 2001 | Reloc.getOffset(RelocOffset); |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 2002 | uint64_t SectionAddress = Sections[SectIdx].getAddress(); |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 2003 | RelocOffset -= SectionAddress; |
| 2004 | |
Alexey Samsonov | aa4d295 | 2014-03-14 14:22:49 +0000 | [diff] [blame] | 2005 | symbol_iterator RelocSym = Reloc.getSymbol(); |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 2006 | |
Rafael Espindola | 806f006 | 2013-06-05 01:33:53 +0000 | [diff] [blame] | 2007 | Relocs.push_back(std::make_pair(RelocOffset, *RelocSym)); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 2008 | } |
| 2009 | array_pod_sort(Relocs.begin(), Relocs.end()); |
| 2010 | |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 2011 | // Create a map of symbol addresses to symbol names for use by |
| 2012 | // the SymbolizerSymbolLookUp() routine. |
| 2013 | SymbolAddressMap AddrMap; |
| 2014 | for (const SymbolRef &Symbol : MachOOF->symbols()) { |
| 2015 | SymbolRef::Type ST; |
| 2016 | Symbol.getType(ST); |
| 2017 | if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data || |
| 2018 | ST == SymbolRef::ST_Other) { |
| 2019 | uint64_t Address; |
| 2020 | Symbol.getAddress(Address); |
| 2021 | StringRef SymName; |
| 2022 | Symbol.getName(SymName); |
| 2023 | AddrMap[Address] = SymName; |
| 2024 | } |
| 2025 | } |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 2026 | // Set up the block of info used by the Symbolizer call backs. |
| 2027 | SymbolizerInfo.verbose = true; |
| 2028 | SymbolizerInfo.O = MachOOF; |
| 2029 | SymbolizerInfo.S = Sections[SectIdx]; |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 2030 | SymbolizerInfo.AddrMap = &AddrMap; |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 2031 | SymbolizerInfo.Sections = &Sections; |
| 2032 | SymbolizerInfo.class_name = nullptr; |
| 2033 | SymbolizerInfo.selector_name = nullptr; |
| 2034 | SymbolizerInfo.method = nullptr; |
Kevin Enderby | 04bf693 | 2014-10-28 23:39:46 +0000 | [diff] [blame] | 2035 | SymbolizerInfo.demangled_name = nullptr; |
Kevin Enderby | 078be60 | 2014-10-23 19:53:12 +0000 | [diff] [blame] | 2036 | SymbolizerInfo.bindtable = nullptr; |
Kevin Enderby | 1073822 | 2014-11-19 20:20:16 +0000 | [diff] [blame] | 2037 | SymbolizerInfo.adrp_addr = 0; |
| 2038 | SymbolizerInfo.adrp_inst = 0; |
Kevin Enderby | 930fdc7 | 2014-11-06 19:00:13 +0000 | [diff] [blame] | 2039 | // Same for the ThumbSymbolizer |
| 2040 | ThumbSymbolizerInfo.verbose = true; |
| 2041 | ThumbSymbolizerInfo.O = MachOOF; |
| 2042 | ThumbSymbolizerInfo.S = Sections[SectIdx]; |
| 2043 | ThumbSymbolizerInfo.AddrMap = &AddrMap; |
| 2044 | ThumbSymbolizerInfo.Sections = &Sections; |
| 2045 | ThumbSymbolizerInfo.class_name = nullptr; |
| 2046 | ThumbSymbolizerInfo.selector_name = nullptr; |
| 2047 | ThumbSymbolizerInfo.method = nullptr; |
| 2048 | ThumbSymbolizerInfo.demangled_name = nullptr; |
| 2049 | ThumbSymbolizerInfo.bindtable = nullptr; |
Kevin Enderby | 1073822 | 2014-11-19 20:20:16 +0000 | [diff] [blame] | 2050 | ThumbSymbolizerInfo.adrp_addr = 0; |
| 2051 | ThumbSymbolizerInfo.adrp_inst = 0; |
Kevin Enderby | 98c9acc | 2014-09-16 18:00:57 +0000 | [diff] [blame] | 2052 | |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 2053 | // Disassemble symbol by symbol. |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 2054 | for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) { |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 2055 | StringRef SymName; |
| 2056 | Symbols[SymIdx].getName(SymName); |
| 2057 | |
| 2058 | SymbolRef::Type ST; |
| 2059 | Symbols[SymIdx].getType(ST); |
| 2060 | if (ST != SymbolRef::ST_Function) |
| 2061 | continue; |
| 2062 | |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 2063 | // Make sure the symbol is defined in this section. |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 2064 | bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]); |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 2065 | if (!containsSym) |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 2066 | continue; |
| 2067 | |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 2068 | // Start at the address of the symbol relative to the section's address. |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 2069 | uint64_t Start = 0; |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 2070 | uint64_t SectionAddress = Sections[SectIdx].getAddress(); |
Danil Malyshev | cbe72fc | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 2071 | Symbols[SymIdx].getAddress(Start); |
Cameron Zwarich | 54478a5 | 2012-02-03 05:42:17 +0000 | [diff] [blame] | 2072 | Start -= SectionAddress; |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 2073 | |
Benjamin Kramer | 2ad2eb5 | 2011-09-20 17:53:01 +0000 | [diff] [blame] | 2074 | // Stop disassembling either at the beginning of the next symbol or at |
| 2075 | // the end of the section. |
Kevin Enderby | edd5872 | 2012-05-15 18:57:14 +0000 | [diff] [blame] | 2076 | bool containsNextSym = false; |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 2077 | uint64_t NextSym = 0; |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 2078 | uint64_t NextSymIdx = SymIdx + 1; |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 2079 | while (Symbols.size() > NextSymIdx) { |
| 2080 | SymbolRef::Type NextSymType; |
| 2081 | Symbols[NextSymIdx].getType(NextSymType); |
| 2082 | if (NextSymType == SymbolRef::ST_Function) { |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 2083 | containsNextSym = |
| 2084 | Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]); |
Danil Malyshev | cbe72fc | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 2085 | Symbols[NextSymIdx].getAddress(NextSym); |
Cameron Zwarich | 54478a5 | 2012-02-03 05:42:17 +0000 | [diff] [blame] | 2086 | NextSym -= SectionAddress; |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 2087 | break; |
| 2088 | } |
| 2089 | ++NextSymIdx; |
| 2090 | } |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 2091 | |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 2092 | uint64_t SectSize = Sections[SectIdx].getSize(); |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 2093 | uint64_t End = containsNextSym ? NextSym : SectSize; |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 2094 | uint64_t Size; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 2095 | |
| 2096 | symbolTableWorked = true; |
Rafael Espindola | bd604f2 | 2014-11-07 00:52:15 +0000 | [diff] [blame] | 2097 | |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 2098 | DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl(); |
| 2099 | bool isThumb = |
| 2100 | (MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb) && ThumbTarget; |
| 2101 | |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 2102 | outs() << SymName << ":\n"; |
| 2103 | DILineInfo lastLine; |
| 2104 | for (uint64_t Index = Start; Index < End; Index += Size) { |
| 2105 | MCInst Inst; |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 2106 | |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 2107 | uint64_t PC = SectAddress + Index; |
| 2108 | if (FullLeadingAddr) { |
| 2109 | if (MachOOF->is64Bit()) |
| 2110 | outs() << format("%016" PRIx64, PC); |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 2111 | else |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 2112 | outs() << format("%08" PRIx64, PC); |
| 2113 | } else { |
| 2114 | outs() << format("%8" PRIx64 ":", PC); |
| 2115 | } |
| 2116 | if (!NoShowRawInsn) |
| 2117 | outs() << "\t"; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 2118 | |
| 2119 | // Check the data in code table here to see if this is data not an |
| 2120 | // instruction to be disassembled. |
| 2121 | DiceTable Dice; |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 2122 | Dice.push_back(std::make_pair(PC, DiceRef())); |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 2123 | dice_table_iterator DTI = |
| 2124 | std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(), |
| 2125 | compareDiceTableEntries); |
| 2126 | if (DTI != Dices.end()) { |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 2127 | uint16_t Length; |
| 2128 | DTI->second.getLength(Length); |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 2129 | uint16_t Kind; |
| 2130 | DTI->second.getKind(Kind); |
Aaron Ballman | 106fd7b | 2014-11-12 14:01:17 +0000 | [diff] [blame] | 2131 | Size = DumpDataInCode(reinterpret_cast<const char *>(Bytes.data()) + |
| 2132 | Index, |
| 2133 | Length, Kind); |
Kevin Enderby | 930fdc7 | 2014-11-06 19:00:13 +0000 | [diff] [blame] | 2134 | if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) && |
| 2135 | (PC == (DTI->first + Length - 1)) && (Length & 1)) |
| 2136 | Size++; |
Kevin Enderby | 273ae01 | 2013-06-06 17:20:50 +0000 | [diff] [blame] | 2137 | continue; |
| 2138 | } |
| 2139 | |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 2140 | SmallVector<char, 64> AnnotationsBytes; |
| 2141 | raw_svector_ostream Annotations(AnnotationsBytes); |
| 2142 | |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 2143 | bool gotInst; |
| 2144 | if (isThumb) |
Rafael Espindola | 7fc5b87 | 2014-11-12 02:04:27 +0000 | [diff] [blame] | 2145 | gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index), |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 2146 | PC, DebugOut, Annotations); |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 2147 | else |
Rafael Espindola | 7fc5b87 | 2014-11-12 02:04:27 +0000 | [diff] [blame] | 2148 | gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC, |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 2149 | DebugOut, Annotations); |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 2150 | if (gotInst) { |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 2151 | if (!NoShowRawInsn) { |
Aaron Ballman | 106fd7b | 2014-11-12 14:01:17 +0000 | [diff] [blame] | 2152 | DumpBytes(StringRef( |
| 2153 | reinterpret_cast<const char *>(Bytes.data()) + Index, Size)); |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 2154 | } |
| 2155 | formatted_raw_ostream FormattedOS(outs()); |
| 2156 | Annotations.flush(); |
| 2157 | StringRef AnnotationsStr = Annotations.str(); |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 2158 | if (isThumb) |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 2159 | ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr); |
Kevin Enderby | ec5ca03 | 2014-08-18 20:21:02 +0000 | [diff] [blame] | 2160 | else |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 2161 | IP->printInst(&Inst, FormattedOS, AnnotationsStr); |
| 2162 | emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo); |
Owen Anderson | d9243c4 | 2011-10-17 21:37:35 +0000 | [diff] [blame] | 2163 | |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 2164 | // Print debug info. |
| 2165 | if (diContext) { |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 2166 | DILineInfo dli = diContext->getLineInfoForAddress(PC); |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 2167 | // Print valid line info if it changed. |
Alexey Samsonov | d010999 | 2014-04-18 21:36:39 +0000 | [diff] [blame] | 2168 | if (dli != lastLine && dli.Line != 0) |
| 2169 | outs() << "\t## " << dli.FileName << ':' << dli.Line << ':' |
| 2170 | << dli.Column; |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 2171 | lastLine = dli; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 2172 | } |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 2173 | outs() << "\n"; |
| 2174 | } else { |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 2175 | unsigned int Arch = MachOOF->getArch(); |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 2176 | if (Arch == Triple::x86_64 || Arch == Triple::x86) { |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 2177 | outs() << format("\t.byte 0x%02x #bad opcode\n", |
| 2178 | *(Bytes.data() + Index) & 0xff); |
| 2179 | Size = 1; // skip exactly one illegible byte and move on. |
Kevin Enderby | ae3c126 | 2014-11-14 21:52:18 +0000 | [diff] [blame] | 2180 | } else if (Arch == Triple::aarch64) { |
| 2181 | uint32_t opcode = (*(Bytes.data() + Index) & 0xff) | |
| 2182 | (*(Bytes.data() + Index + 1) & 0xff) << 8 | |
| 2183 | (*(Bytes.data() + Index + 2) & 0xff) << 16 | |
| 2184 | (*(Bytes.data() + Index + 3) & 0xff) << 24; |
| 2185 | outs() << format("\t.long\t0x%08x\n", opcode); |
| 2186 | Size = 4; |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 2187 | } else { |
| 2188 | errs() << "llvm-objdump: warning: invalid instruction encoding\n"; |
| 2189 | if (Size == 0) |
| 2190 | Size = 1; // skip illegible bytes |
| 2191 | } |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 2192 | } |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 2193 | } |
| 2194 | } |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 2195 | if (!symbolTableWorked) { |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 2196 | // Reading the symbol table didn't work, disassemble the whole section. |
| 2197 | uint64_t SectAddress = Sections[SectIdx].getAddress(); |
| 2198 | uint64_t SectSize = Sections[SectIdx].getSize(); |
Kevin Enderby | badd100 | 2012-05-18 00:13:56 +0000 | [diff] [blame] | 2199 | uint64_t InstSize; |
| 2200 | for (uint64_t Index = 0; Index < SectSize; Index += InstSize) { |
Bill Wendling | 4e68e06 | 2012-07-19 00:17:40 +0000 | [diff] [blame] | 2201 | MCInst Inst; |
Kevin Enderby | badd100 | 2012-05-18 00:13:56 +0000 | [diff] [blame] | 2202 | |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 2203 | uint64_t PC = SectAddress + Index; |
Rafael Espindola | 7fc5b87 | 2014-11-12 02:04:27 +0000 | [diff] [blame] | 2204 | if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC, |
| 2205 | DebugOut, nulls())) { |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 2206 | if (FullLeadingAddr) { |
| 2207 | if (MachOOF->is64Bit()) |
| 2208 | outs() << format("%016" PRIx64, PC); |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 2209 | else |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 2210 | outs() << format("%08" PRIx64, PC); |
| 2211 | } else { |
| 2212 | outs() << format("%8" PRIx64 ":", PC); |
| 2213 | } |
| 2214 | if (!NoShowRawInsn) { |
| 2215 | outs() << "\t"; |
Aaron Ballman | 106fd7b | 2014-11-12 14:01:17 +0000 | [diff] [blame] | 2216 | DumpBytes( |
| 2217 | StringRef(reinterpret_cast<const char *>(Bytes.data()) + Index, |
| 2218 | InstSize)); |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 2219 | } |
Bill Wendling | 4e68e06 | 2012-07-19 00:17:40 +0000 | [diff] [blame] | 2220 | IP->printInst(&Inst, outs(), ""); |
| 2221 | outs() << "\n"; |
| 2222 | } else { |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 2223 | unsigned int Arch = MachOOF->getArch(); |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 2224 | if (Arch == Triple::x86_64 || Arch == Triple::x86) { |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 2225 | outs() << format("\t.byte 0x%02x #bad opcode\n", |
| 2226 | *(Bytes.data() + Index) & 0xff); |
| 2227 | InstSize = 1; // skip exactly one illegible byte and move on. |
| 2228 | } else { |
| 2229 | errs() << "llvm-objdump: warning: invalid instruction encoding\n"; |
| 2230 | if (InstSize == 0) |
| 2231 | InstSize = 1; // skip illegible bytes |
| 2232 | } |
Bill Wendling | 4e68e06 | 2012-07-19 00:17:40 +0000 | [diff] [blame] | 2233 | } |
Kevin Enderby | badd100 | 2012-05-18 00:13:56 +0000 | [diff] [blame] | 2234 | } |
| 2235 | } |
Kevin Enderby | ef3ad2f | 2014-12-04 23:56:27 +0000 | [diff] [blame] | 2236 | // The TripleName's need to be reset if we are called again for a different |
| 2237 | // archtecture. |
| 2238 | TripleName = ""; |
| 2239 | ThumbTripleName = ""; |
| 2240 | |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 2241 | if (SymbolizerInfo.method != nullptr) |
| 2242 | free(SymbolizerInfo.method); |
Kevin Enderby | 04bf693 | 2014-10-28 23:39:46 +0000 | [diff] [blame] | 2243 | if (SymbolizerInfo.demangled_name != nullptr) |
| 2244 | free(SymbolizerInfo.demangled_name); |
Kevin Enderby | 078be60 | 2014-10-23 19:53:12 +0000 | [diff] [blame] | 2245 | if (SymbolizerInfo.bindtable != nullptr) |
| 2246 | delete SymbolizerInfo.bindtable; |
Kevin Enderby | 930fdc7 | 2014-11-06 19:00:13 +0000 | [diff] [blame] | 2247 | if (ThumbSymbolizerInfo.method != nullptr) |
| 2248 | free(ThumbSymbolizerInfo.method); |
| 2249 | if (ThumbSymbolizerInfo.demangled_name != nullptr) |
| 2250 | free(ThumbSymbolizerInfo.demangled_name); |
| 2251 | if (ThumbSymbolizerInfo.bindtable != nullptr) |
| 2252 | delete ThumbSymbolizerInfo.bindtable; |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 2253 | } |
| 2254 | } |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 2255 | |
Tim Northover | 39c70bb | 2014-08-12 11:52:59 +0000 | [diff] [blame] | 2256 | //===----------------------------------------------------------------------===// |
| 2257 | // __compact_unwind section dumping |
| 2258 | //===----------------------------------------------------------------------===// |
| 2259 | |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 2260 | namespace { |
Tim Northover | 39c70bb | 2014-08-12 11:52:59 +0000 | [diff] [blame] | 2261 | |
| 2262 | template <typename T> static uint64_t readNext(const char *&Buf) { |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 2263 | using llvm::support::little; |
| 2264 | using llvm::support::unaligned; |
Tim Northover | 39c70bb | 2014-08-12 11:52:59 +0000 | [diff] [blame] | 2265 | |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 2266 | uint64_t Val = support::endian::read<T, little, unaligned>(Buf); |
| 2267 | Buf += sizeof(T); |
| 2268 | return Val; |
| 2269 | } |
Tim Northover | 39c70bb | 2014-08-12 11:52:59 +0000 | [diff] [blame] | 2270 | |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 2271 | struct CompactUnwindEntry { |
| 2272 | uint32_t OffsetInSection; |
| 2273 | |
| 2274 | uint64_t FunctionAddr; |
| 2275 | uint32_t Length; |
| 2276 | uint32_t CompactEncoding; |
| 2277 | uint64_t PersonalityAddr; |
| 2278 | uint64_t LSDAAddr; |
| 2279 | |
| 2280 | RelocationRef FunctionReloc; |
| 2281 | RelocationRef PersonalityReloc; |
| 2282 | RelocationRef LSDAReloc; |
| 2283 | |
| 2284 | CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64) |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 2285 | : OffsetInSection(Offset) { |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 2286 | if (Is64) |
| 2287 | read<uint64_t>(Contents.data() + Offset); |
| 2288 | else |
| 2289 | read<uint32_t>(Contents.data() + Offset); |
| 2290 | } |
| 2291 | |
| 2292 | private: |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 2293 | template <typename UIntPtr> void read(const char *Buf) { |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 2294 | FunctionAddr = readNext<UIntPtr>(Buf); |
| 2295 | Length = readNext<uint32_t>(Buf); |
| 2296 | CompactEncoding = readNext<uint32_t>(Buf); |
| 2297 | PersonalityAddr = readNext<UIntPtr>(Buf); |
| 2298 | LSDAAddr = readNext<UIntPtr>(Buf); |
| 2299 | } |
| 2300 | }; |
| 2301 | } |
| 2302 | |
| 2303 | /// Given a relocation from __compact_unwind, consisting of the RelocationRef |
| 2304 | /// and data being relocated, determine the best base Name and Addend to use for |
| 2305 | /// display purposes. |
| 2306 | /// |
| 2307 | /// 1. An Extern relocation will directly reference a symbol (and the data is |
| 2308 | /// then already an addend), so use that. |
| 2309 | /// 2. Otherwise the data is an offset in the object file's layout; try to find |
| 2310 | // a symbol before it in the same section, and use the offset from there. |
| 2311 | /// 3. Finally, if all that fails, fall back to an offset from the start of the |
| 2312 | /// referenced section. |
| 2313 | static void findUnwindRelocNameAddend(const MachOObjectFile *Obj, |
| 2314 | std::map<uint64_t, SymbolRef> &Symbols, |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 2315 | const RelocationRef &Reloc, uint64_t Addr, |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 2316 | StringRef &Name, uint64_t &Addend) { |
| 2317 | if (Reloc.getSymbol() != Obj->symbol_end()) { |
| 2318 | Reloc.getSymbol()->getName(Name); |
| 2319 | Addend = Addr; |
| 2320 | return; |
| 2321 | } |
| 2322 | |
| 2323 | auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl()); |
| 2324 | SectionRef RelocSection = Obj->getRelocationSection(RE); |
| 2325 | |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 2326 | uint64_t SectionAddr = RelocSection.getAddress(); |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 2327 | |
| 2328 | auto Sym = Symbols.upper_bound(Addr); |
| 2329 | if (Sym == Symbols.begin()) { |
| 2330 | // The first symbol in the object is after this reference, the best we can |
| 2331 | // do is section-relative notation. |
| 2332 | RelocSection.getName(Name); |
| 2333 | Addend = Addr - SectionAddr; |
| 2334 | return; |
| 2335 | } |
| 2336 | |
| 2337 | // Go back one so that SymbolAddress <= Addr. |
| 2338 | --Sym; |
| 2339 | |
| 2340 | section_iterator SymSection = Obj->section_end(); |
| 2341 | Sym->second.getSection(SymSection); |
| 2342 | if (RelocSection == *SymSection) { |
| 2343 | // There's a valid symbol in the same section before this reference. |
| 2344 | Sym->second.getName(Name); |
| 2345 | Addend = Addr - Sym->first; |
| 2346 | return; |
| 2347 | } |
| 2348 | |
| 2349 | // There is a symbol before this reference, but it's in a different |
| 2350 | // section. Probably not helpful to mention it, so use the section name. |
| 2351 | RelocSection.getName(Name); |
| 2352 | Addend = Addr - SectionAddr; |
| 2353 | } |
| 2354 | |
| 2355 | static void printUnwindRelocDest(const MachOObjectFile *Obj, |
| 2356 | std::map<uint64_t, SymbolRef> &Symbols, |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 2357 | const RelocationRef &Reloc, uint64_t Addr) { |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 2358 | StringRef Name; |
| 2359 | uint64_t Addend; |
| 2360 | |
Tim Northover | 0b0add5 | 2014-09-09 10:45:06 +0000 | [diff] [blame] | 2361 | if (!Reloc.getObjectFile()) |
| 2362 | return; |
| 2363 | |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 2364 | findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend); |
| 2365 | |
| 2366 | outs() << Name; |
| 2367 | if (Addend) |
Tim Northover | 63a2562 | 2014-08-11 09:14:06 +0000 | [diff] [blame] | 2368 | outs() << " + " << format("0x%" PRIx64, Addend); |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 2369 | } |
| 2370 | |
| 2371 | static void |
| 2372 | printMachOCompactUnwindSection(const MachOObjectFile *Obj, |
| 2373 | std::map<uint64_t, SymbolRef> &Symbols, |
| 2374 | const SectionRef &CompactUnwind) { |
| 2375 | |
| 2376 | assert(Obj->isLittleEndian() && |
| 2377 | "There should not be a big-endian .o with __compact_unwind"); |
| 2378 | |
| 2379 | bool Is64 = Obj->is64Bit(); |
| 2380 | uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t); |
| 2381 | uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t); |
| 2382 | |
| 2383 | StringRef Contents; |
| 2384 | CompactUnwind.getContents(Contents); |
| 2385 | |
| 2386 | SmallVector<CompactUnwindEntry, 4> CompactUnwinds; |
| 2387 | |
| 2388 | // First populate the initial raw offsets, encodings and so on from the entry. |
| 2389 | for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) { |
| 2390 | CompactUnwindEntry Entry(Contents.data(), Offset, Is64); |
| 2391 | CompactUnwinds.push_back(Entry); |
| 2392 | } |
| 2393 | |
| 2394 | // Next we need to look at the relocations to find out what objects are |
| 2395 | // actually being referred to. |
| 2396 | for (const RelocationRef &Reloc : CompactUnwind.relocations()) { |
| 2397 | uint64_t RelocAddress; |
| 2398 | Reloc.getOffset(RelocAddress); |
| 2399 | |
| 2400 | uint32_t EntryIdx = RelocAddress / EntrySize; |
| 2401 | uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize; |
| 2402 | CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx]; |
| 2403 | |
| 2404 | if (OffsetInEntry == 0) |
| 2405 | Entry.FunctionReloc = Reloc; |
| 2406 | else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t)) |
| 2407 | Entry.PersonalityReloc = Reloc; |
| 2408 | else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t)) |
| 2409 | Entry.LSDAReloc = Reloc; |
| 2410 | else |
| 2411 | llvm_unreachable("Unexpected relocation in __compact_unwind section"); |
| 2412 | } |
| 2413 | |
| 2414 | // Finally, we're ready to print the data we've gathered. |
| 2415 | outs() << "Contents of __compact_unwind section:\n"; |
| 2416 | for (auto &Entry : CompactUnwinds) { |
Tim Northover | 06af260 | 2014-08-08 12:08:51 +0000 | [diff] [blame] | 2417 | outs() << " Entry at offset " |
| 2418 | << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n"; |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 2419 | |
| 2420 | // 1. Start of the region this entry applies to. |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 2421 | outs() << " start: " << format("0x%" PRIx64, |
| 2422 | Entry.FunctionAddr) << ' '; |
| 2423 | printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr); |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 2424 | outs() << '\n'; |
| 2425 | |
| 2426 | // 2. Length of the region this entry applies to. |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 2427 | outs() << " length: " << format("0x%" PRIx32, Entry.Length) |
| 2428 | << '\n'; |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 2429 | // 3. The 32-bit compact encoding. |
| 2430 | outs() << " compact encoding: " |
Tim Northover | b911bf8 | 2014-08-08 12:00:09 +0000 | [diff] [blame] | 2431 | << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n'; |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 2432 | |
| 2433 | // 4. The personality function, if present. |
| 2434 | if (Entry.PersonalityReloc.getObjectFile()) { |
| 2435 | outs() << " personality function: " |
Tim Northover | b911bf8 | 2014-08-08 12:00:09 +0000 | [diff] [blame] | 2436 | << format("0x%" PRIx64, Entry.PersonalityAddr) << ' '; |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 2437 | printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc, |
| 2438 | Entry.PersonalityAddr); |
| 2439 | outs() << '\n'; |
| 2440 | } |
| 2441 | |
| 2442 | // 5. This entry's language-specific data area. |
| 2443 | if (Entry.LSDAReloc.getObjectFile()) { |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 2444 | outs() << " LSDA: " << format("0x%" PRIx64, |
| 2445 | Entry.LSDAAddr) << ' '; |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 2446 | printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr); |
| 2447 | outs() << '\n'; |
| 2448 | } |
| 2449 | } |
| 2450 | } |
| 2451 | |
Tim Northover | 39c70bb | 2014-08-12 11:52:59 +0000 | [diff] [blame] | 2452 | //===----------------------------------------------------------------------===// |
| 2453 | // __unwind_info section dumping |
| 2454 | //===----------------------------------------------------------------------===// |
| 2455 | |
| 2456 | static void printRegularSecondLevelUnwindPage(const char *PageStart) { |
| 2457 | const char *Pos = PageStart; |
| 2458 | uint32_t Kind = readNext<uint32_t>(Pos); |
| 2459 | (void)Kind; |
| 2460 | assert(Kind == 2 && "kind for a regular 2nd level index should be 2"); |
| 2461 | |
| 2462 | uint16_t EntriesStart = readNext<uint16_t>(Pos); |
| 2463 | uint16_t NumEntries = readNext<uint16_t>(Pos); |
| 2464 | |
| 2465 | Pos = PageStart + EntriesStart; |
| 2466 | for (unsigned i = 0; i < NumEntries; ++i) { |
| 2467 | uint32_t FunctionOffset = readNext<uint32_t>(Pos); |
| 2468 | uint32_t Encoding = readNext<uint32_t>(Pos); |
| 2469 | |
| 2470 | outs() << " [" << i << "]: " |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 2471 | << "function offset=" << format("0x%08" PRIx32, FunctionOffset) |
| 2472 | << ", " |
| 2473 | << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n'; |
Tim Northover | 39c70bb | 2014-08-12 11:52:59 +0000 | [diff] [blame] | 2474 | } |
| 2475 | } |
| 2476 | |
| 2477 | static void printCompressedSecondLevelUnwindPage( |
| 2478 | const char *PageStart, uint32_t FunctionBase, |
| 2479 | const SmallVectorImpl<uint32_t> &CommonEncodings) { |
| 2480 | const char *Pos = PageStart; |
| 2481 | uint32_t Kind = readNext<uint32_t>(Pos); |
| 2482 | (void)Kind; |
| 2483 | assert(Kind == 3 && "kind for a compressed 2nd level index should be 3"); |
| 2484 | |
| 2485 | uint16_t EntriesStart = readNext<uint16_t>(Pos); |
| 2486 | uint16_t NumEntries = readNext<uint16_t>(Pos); |
| 2487 | |
| 2488 | uint16_t EncodingsStart = readNext<uint16_t>(Pos); |
| 2489 | readNext<uint16_t>(Pos); |
Aaron Ballman | 80930af | 2014-08-14 13:53:19 +0000 | [diff] [blame] | 2490 | const auto *PageEncodings = reinterpret_cast<const support::ulittle32_t *>( |
| 2491 | PageStart + EncodingsStart); |
Tim Northover | 39c70bb | 2014-08-12 11:52:59 +0000 | [diff] [blame] | 2492 | |
| 2493 | Pos = PageStart + EntriesStart; |
| 2494 | for (unsigned i = 0; i < NumEntries; ++i) { |
| 2495 | uint32_t Entry = readNext<uint32_t>(Pos); |
| 2496 | uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff); |
| 2497 | uint32_t EncodingIdx = Entry >> 24; |
| 2498 | |
| 2499 | uint32_t Encoding; |
| 2500 | if (EncodingIdx < CommonEncodings.size()) |
| 2501 | Encoding = CommonEncodings[EncodingIdx]; |
| 2502 | else |
| 2503 | Encoding = PageEncodings[EncodingIdx - CommonEncodings.size()]; |
| 2504 | |
| 2505 | outs() << " [" << i << "]: " |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 2506 | << "function offset=" << format("0x%08" PRIx32, FunctionOffset) |
| 2507 | << ", " |
| 2508 | << "encoding[" << EncodingIdx |
| 2509 | << "]=" << format("0x%08" PRIx32, Encoding) << '\n'; |
Tim Northover | 39c70bb | 2014-08-12 11:52:59 +0000 | [diff] [blame] | 2510 | } |
| 2511 | } |
| 2512 | |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 2513 | static void printMachOUnwindInfoSection(const MachOObjectFile *Obj, |
| 2514 | std::map<uint64_t, SymbolRef> &Symbols, |
| 2515 | const SectionRef &UnwindInfo) { |
Tim Northover | 39c70bb | 2014-08-12 11:52:59 +0000 | [diff] [blame] | 2516 | |
| 2517 | assert(Obj->isLittleEndian() && |
| 2518 | "There should not be a big-endian .o with __unwind_info"); |
| 2519 | |
| 2520 | outs() << "Contents of __unwind_info section:\n"; |
| 2521 | |
| 2522 | StringRef Contents; |
| 2523 | UnwindInfo.getContents(Contents); |
| 2524 | const char *Pos = Contents.data(); |
| 2525 | |
| 2526 | //===---------------------------------- |
| 2527 | // Section header |
| 2528 | //===---------------------------------- |
| 2529 | |
| 2530 | uint32_t Version = readNext<uint32_t>(Pos); |
| 2531 | outs() << " Version: " |
| 2532 | << format("0x%" PRIx32, Version) << '\n'; |
| 2533 | assert(Version == 1 && "only understand version 1"); |
| 2534 | |
| 2535 | uint32_t CommonEncodingsStart = readNext<uint32_t>(Pos); |
| 2536 | outs() << " Common encodings array section offset: " |
| 2537 | << format("0x%" PRIx32, CommonEncodingsStart) << '\n'; |
| 2538 | uint32_t NumCommonEncodings = readNext<uint32_t>(Pos); |
| 2539 | outs() << " Number of common encodings in array: " |
| 2540 | << format("0x%" PRIx32, NumCommonEncodings) << '\n'; |
| 2541 | |
| 2542 | uint32_t PersonalitiesStart = readNext<uint32_t>(Pos); |
| 2543 | outs() << " Personality function array section offset: " |
| 2544 | << format("0x%" PRIx32, PersonalitiesStart) << '\n'; |
| 2545 | uint32_t NumPersonalities = readNext<uint32_t>(Pos); |
| 2546 | outs() << " Number of personality functions in array: " |
| 2547 | << format("0x%" PRIx32, NumPersonalities) << '\n'; |
| 2548 | |
| 2549 | uint32_t IndicesStart = readNext<uint32_t>(Pos); |
| 2550 | outs() << " Index array section offset: " |
| 2551 | << format("0x%" PRIx32, IndicesStart) << '\n'; |
| 2552 | uint32_t NumIndices = readNext<uint32_t>(Pos); |
| 2553 | outs() << " Number of indices in array: " |
| 2554 | << format("0x%" PRIx32, NumIndices) << '\n'; |
| 2555 | |
| 2556 | //===---------------------------------- |
| 2557 | // A shared list of common encodings |
| 2558 | //===---------------------------------- |
| 2559 | |
| 2560 | // These occupy indices in the range [0, N] whenever an encoding is referenced |
| 2561 | // from a compressed 2nd level index table. In practice the linker only |
| 2562 | // creates ~128 of these, so that indices are available to embed encodings in |
| 2563 | // the 2nd level index. |
| 2564 | |
| 2565 | SmallVector<uint32_t, 64> CommonEncodings; |
| 2566 | outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n"; |
| 2567 | Pos = Contents.data() + CommonEncodingsStart; |
| 2568 | for (unsigned i = 0; i < NumCommonEncodings; ++i) { |
| 2569 | uint32_t Encoding = readNext<uint32_t>(Pos); |
| 2570 | CommonEncodings.push_back(Encoding); |
| 2571 | |
| 2572 | outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding) |
| 2573 | << '\n'; |
| 2574 | } |
| 2575 | |
Tim Northover | 39c70bb | 2014-08-12 11:52:59 +0000 | [diff] [blame] | 2576 | //===---------------------------------- |
| 2577 | // Personality functions used in this executable |
| 2578 | //===---------------------------------- |
| 2579 | |
| 2580 | // There should be only a handful of these (one per source language, |
| 2581 | // roughly). Particularly since they only get 2 bits in the compact encoding. |
| 2582 | |
| 2583 | outs() << " Personality functions: (count = " << NumPersonalities << ")\n"; |
| 2584 | Pos = Contents.data() + PersonalitiesStart; |
| 2585 | for (unsigned i = 0; i < NumPersonalities; ++i) { |
| 2586 | uint32_t PersonalityFn = readNext<uint32_t>(Pos); |
| 2587 | outs() << " personality[" << i + 1 |
| 2588 | << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n'; |
| 2589 | } |
| 2590 | |
| 2591 | //===---------------------------------- |
| 2592 | // The level 1 index entries |
| 2593 | //===---------------------------------- |
| 2594 | |
| 2595 | // These specify an approximate place to start searching for the more detailed |
| 2596 | // information, sorted by PC. |
| 2597 | |
| 2598 | struct IndexEntry { |
| 2599 | uint32_t FunctionOffset; |
| 2600 | uint32_t SecondLevelPageStart; |
| 2601 | uint32_t LSDAStart; |
| 2602 | }; |
| 2603 | |
| 2604 | SmallVector<IndexEntry, 4> IndexEntries; |
| 2605 | |
| 2606 | outs() << " Top level indices: (count = " << NumIndices << ")\n"; |
| 2607 | Pos = Contents.data() + IndicesStart; |
| 2608 | for (unsigned i = 0; i < NumIndices; ++i) { |
| 2609 | IndexEntry Entry; |
| 2610 | |
| 2611 | Entry.FunctionOffset = readNext<uint32_t>(Pos); |
| 2612 | Entry.SecondLevelPageStart = readNext<uint32_t>(Pos); |
| 2613 | Entry.LSDAStart = readNext<uint32_t>(Pos); |
| 2614 | IndexEntries.push_back(Entry); |
| 2615 | |
| 2616 | outs() << " [" << i << "]: " |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 2617 | << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset) |
| 2618 | << ", " |
Tim Northover | 39c70bb | 2014-08-12 11:52:59 +0000 | [diff] [blame] | 2619 | << "2nd level page offset=" |
| 2620 | << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", " |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 2621 | << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n'; |
Tim Northover | 39c70bb | 2014-08-12 11:52:59 +0000 | [diff] [blame] | 2622 | } |
| 2623 | |
Tim Northover | 39c70bb | 2014-08-12 11:52:59 +0000 | [diff] [blame] | 2624 | //===---------------------------------- |
| 2625 | // Next come the LSDA tables |
| 2626 | //===---------------------------------- |
| 2627 | |
| 2628 | // The LSDA layout is rather implicit: it's a contiguous array of entries from |
| 2629 | // the first top-level index's LSDAOffset to the last (sentinel). |
| 2630 | |
| 2631 | outs() << " LSDA descriptors:\n"; |
| 2632 | Pos = Contents.data() + IndexEntries[0].LSDAStart; |
| 2633 | int NumLSDAs = (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) / |
| 2634 | (2 * sizeof(uint32_t)); |
| 2635 | for (int i = 0; i < NumLSDAs; ++i) { |
| 2636 | uint32_t FunctionOffset = readNext<uint32_t>(Pos); |
| 2637 | uint32_t LSDAOffset = readNext<uint32_t>(Pos); |
| 2638 | outs() << " [" << i << "]: " |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 2639 | << "function offset=" << format("0x%08" PRIx32, FunctionOffset) |
| 2640 | << ", " |
| 2641 | << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n'; |
Tim Northover | 39c70bb | 2014-08-12 11:52:59 +0000 | [diff] [blame] | 2642 | } |
| 2643 | |
| 2644 | //===---------------------------------- |
| 2645 | // Finally, the 2nd level indices |
| 2646 | //===---------------------------------- |
| 2647 | |
| 2648 | // Generally these are 4K in size, and have 2 possible forms: |
| 2649 | // + Regular stores up to 511 entries with disparate encodings |
| 2650 | // + Compressed stores up to 1021 entries if few enough compact encoding |
| 2651 | // values are used. |
| 2652 | outs() << " Second level indices:\n"; |
| 2653 | for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) { |
| 2654 | // The final sentinel top-level index has no associated 2nd level page |
| 2655 | if (IndexEntries[i].SecondLevelPageStart == 0) |
| 2656 | break; |
| 2657 | |
| 2658 | outs() << " Second level index[" << i << "]: " |
| 2659 | << "offset in section=" |
| 2660 | << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart) |
| 2661 | << ", " |
| 2662 | << "base function offset=" |
| 2663 | << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n'; |
| 2664 | |
| 2665 | Pos = Contents.data() + IndexEntries[i].SecondLevelPageStart; |
Aaron Ballman | 80930af | 2014-08-14 13:53:19 +0000 | [diff] [blame] | 2666 | uint32_t Kind = *reinterpret_cast<const support::ulittle32_t *>(Pos); |
Tim Northover | 39c70bb | 2014-08-12 11:52:59 +0000 | [diff] [blame] | 2667 | if (Kind == 2) |
| 2668 | printRegularSecondLevelUnwindPage(Pos); |
| 2669 | else if (Kind == 3) |
| 2670 | printCompressedSecondLevelUnwindPage(Pos, IndexEntries[i].FunctionOffset, |
| 2671 | CommonEncodings); |
| 2672 | else |
| 2673 | llvm_unreachable("Do not know how to print this kind of 2nd level page"); |
Tim Northover | 39c70bb | 2014-08-12 11:52:59 +0000 | [diff] [blame] | 2674 | } |
| 2675 | } |
| 2676 | |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 2677 | void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) { |
| 2678 | std::map<uint64_t, SymbolRef> Symbols; |
| 2679 | for (const SymbolRef &SymRef : Obj->symbols()) { |
| 2680 | // Discard any undefined or absolute symbols. They're not going to take part |
| 2681 | // in the convenience lookup for unwind info and just take up resources. |
| 2682 | section_iterator Section = Obj->section_end(); |
| 2683 | SymRef.getSection(Section); |
| 2684 | if (Section == Obj->section_end()) |
| 2685 | continue; |
| 2686 | |
| 2687 | uint64_t Addr; |
| 2688 | SymRef.getAddress(Addr); |
| 2689 | Symbols.insert(std::make_pair(Addr, SymRef)); |
| 2690 | } |
| 2691 | |
| 2692 | for (const SectionRef &Section : Obj->sections()) { |
| 2693 | StringRef SectName; |
| 2694 | Section.getName(SectName); |
| 2695 | if (SectName == "__compact_unwind") |
| 2696 | printMachOCompactUnwindSection(Obj, Symbols, Section); |
| 2697 | else if (SectName == "__unwind_info") |
Tim Northover | 39c70bb | 2014-08-12 11:52:59 +0000 | [diff] [blame] | 2698 | printMachOUnwindInfoSection(Obj, Symbols, Section); |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 2699 | else if (SectName == "__eh_frame") |
| 2700 | outs() << "llvm-objdump: warning: unhandled __eh_frame section\n"; |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 2701 | } |
| 2702 | } |
Kevin Enderby | b76d386 | 2014-08-22 20:35:18 +0000 | [diff] [blame] | 2703 | |
| 2704 | static void PrintMachHeader(uint32_t magic, uint32_t cputype, |
| 2705 | uint32_t cpusubtype, uint32_t filetype, |
| 2706 | uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags, |
| 2707 | bool verbose) { |
| 2708 | outs() << "Mach header\n"; |
| 2709 | outs() << " magic cputype cpusubtype caps filetype ncmds " |
| 2710 | "sizeofcmds flags\n"; |
| 2711 | if (verbose) { |
| 2712 | if (magic == MachO::MH_MAGIC) |
| 2713 | outs() << " MH_MAGIC"; |
| 2714 | else if (magic == MachO::MH_MAGIC_64) |
| 2715 | outs() << "MH_MAGIC_64"; |
| 2716 | else |
| 2717 | outs() << format(" 0x%08" PRIx32, magic); |
| 2718 | switch (cputype) { |
| 2719 | case MachO::CPU_TYPE_I386: |
| 2720 | outs() << " I386"; |
| 2721 | switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { |
| 2722 | case MachO::CPU_SUBTYPE_I386_ALL: |
| 2723 | outs() << " ALL"; |
| 2724 | break; |
| 2725 | default: |
| 2726 | outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); |
| 2727 | break; |
| 2728 | } |
| 2729 | break; |
| 2730 | case MachO::CPU_TYPE_X86_64: |
| 2731 | outs() << " X86_64"; |
| 2732 | case MachO::CPU_SUBTYPE_X86_64_ALL: |
| 2733 | outs() << " ALL"; |
| 2734 | break; |
| 2735 | case MachO::CPU_SUBTYPE_X86_64_H: |
| 2736 | outs() << " Haswell"; |
Aaron Ballman | 9d515ff | 2014-08-24 13:25:16 +0000 | [diff] [blame] | 2737 | outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); |
Kevin Enderby | b76d386 | 2014-08-22 20:35:18 +0000 | [diff] [blame] | 2738 | break; |
| 2739 | case MachO::CPU_TYPE_ARM: |
| 2740 | outs() << " ARM"; |
| 2741 | switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { |
| 2742 | case MachO::CPU_SUBTYPE_ARM_ALL: |
| 2743 | outs() << " ALL"; |
| 2744 | break; |
| 2745 | case MachO::CPU_SUBTYPE_ARM_V4T: |
| 2746 | outs() << " V4T"; |
| 2747 | break; |
| 2748 | case MachO::CPU_SUBTYPE_ARM_V5TEJ: |
| 2749 | outs() << " V5TEJ"; |
| 2750 | break; |
| 2751 | case MachO::CPU_SUBTYPE_ARM_XSCALE: |
| 2752 | outs() << " XSCALE"; |
| 2753 | break; |
| 2754 | case MachO::CPU_SUBTYPE_ARM_V6: |
| 2755 | outs() << " V6"; |
| 2756 | break; |
| 2757 | case MachO::CPU_SUBTYPE_ARM_V6M: |
| 2758 | outs() << " V6M"; |
| 2759 | break; |
| 2760 | case MachO::CPU_SUBTYPE_ARM_V7: |
| 2761 | outs() << " V7"; |
| 2762 | break; |
| 2763 | case MachO::CPU_SUBTYPE_ARM_V7EM: |
| 2764 | outs() << " V7EM"; |
| 2765 | break; |
| 2766 | case MachO::CPU_SUBTYPE_ARM_V7K: |
| 2767 | outs() << " V7K"; |
| 2768 | break; |
| 2769 | case MachO::CPU_SUBTYPE_ARM_V7M: |
| 2770 | outs() << " V7M"; |
| 2771 | break; |
| 2772 | case MachO::CPU_SUBTYPE_ARM_V7S: |
| 2773 | outs() << " V7S"; |
| 2774 | break; |
| 2775 | default: |
| 2776 | outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); |
| 2777 | break; |
| 2778 | } |
| 2779 | break; |
| 2780 | case MachO::CPU_TYPE_ARM64: |
| 2781 | outs() << " ARM64"; |
| 2782 | switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { |
| 2783 | case MachO::CPU_SUBTYPE_ARM64_ALL: |
| 2784 | outs() << " ALL"; |
| 2785 | break; |
| 2786 | default: |
| 2787 | outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); |
| 2788 | break; |
| 2789 | } |
| 2790 | break; |
| 2791 | case MachO::CPU_TYPE_POWERPC: |
| 2792 | outs() << " PPC"; |
| 2793 | switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { |
| 2794 | case MachO::CPU_SUBTYPE_POWERPC_ALL: |
| 2795 | outs() << " ALL"; |
| 2796 | break; |
| 2797 | default: |
| 2798 | outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); |
| 2799 | break; |
| 2800 | } |
| 2801 | break; |
| 2802 | case MachO::CPU_TYPE_POWERPC64: |
| 2803 | outs() << " PPC64"; |
| 2804 | switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) { |
| 2805 | case MachO::CPU_SUBTYPE_POWERPC_ALL: |
| 2806 | outs() << " ALL"; |
| 2807 | break; |
| 2808 | default: |
| 2809 | outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); |
| 2810 | break; |
| 2811 | } |
| 2812 | break; |
| 2813 | } |
| 2814 | if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) { |
Kevin Enderby | 8ae63c1 | 2014-09-04 16:54:47 +0000 | [diff] [blame] | 2815 | outs() << " LIB64"; |
Kevin Enderby | b76d386 | 2014-08-22 20:35:18 +0000 | [diff] [blame] | 2816 | } else { |
| 2817 | outs() << format(" 0x%02" PRIx32, |
| 2818 | (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24); |
| 2819 | } |
| 2820 | switch (filetype) { |
| 2821 | case MachO::MH_OBJECT: |
| 2822 | outs() << " OBJECT"; |
| 2823 | break; |
| 2824 | case MachO::MH_EXECUTE: |
| 2825 | outs() << " EXECUTE"; |
| 2826 | break; |
| 2827 | case MachO::MH_FVMLIB: |
| 2828 | outs() << " FVMLIB"; |
| 2829 | break; |
| 2830 | case MachO::MH_CORE: |
| 2831 | outs() << " CORE"; |
| 2832 | break; |
| 2833 | case MachO::MH_PRELOAD: |
| 2834 | outs() << " PRELOAD"; |
| 2835 | break; |
| 2836 | case MachO::MH_DYLIB: |
| 2837 | outs() << " DYLIB"; |
| 2838 | break; |
| 2839 | case MachO::MH_DYLIB_STUB: |
| 2840 | outs() << " DYLIB_STUB"; |
| 2841 | break; |
| 2842 | case MachO::MH_DYLINKER: |
| 2843 | outs() << " DYLINKER"; |
| 2844 | break; |
| 2845 | case MachO::MH_BUNDLE: |
| 2846 | outs() << " BUNDLE"; |
| 2847 | break; |
| 2848 | case MachO::MH_DSYM: |
| 2849 | outs() << " DSYM"; |
| 2850 | break; |
| 2851 | case MachO::MH_KEXT_BUNDLE: |
| 2852 | outs() << " KEXTBUNDLE"; |
| 2853 | break; |
| 2854 | default: |
| 2855 | outs() << format(" %10u", filetype); |
| 2856 | break; |
| 2857 | } |
| 2858 | outs() << format(" %5u", ncmds); |
| 2859 | outs() << format(" %10u", sizeofcmds); |
| 2860 | uint32_t f = flags; |
| 2861 | if (f & MachO::MH_NOUNDEFS) { |
| 2862 | outs() << " NOUNDEFS"; |
| 2863 | f &= ~MachO::MH_NOUNDEFS; |
| 2864 | } |
| 2865 | if (f & MachO::MH_INCRLINK) { |
| 2866 | outs() << " INCRLINK"; |
| 2867 | f &= ~MachO::MH_INCRLINK; |
| 2868 | } |
| 2869 | if (f & MachO::MH_DYLDLINK) { |
| 2870 | outs() << " DYLDLINK"; |
| 2871 | f &= ~MachO::MH_DYLDLINK; |
| 2872 | } |
| 2873 | if (f & MachO::MH_BINDATLOAD) { |
| 2874 | outs() << " BINDATLOAD"; |
| 2875 | f &= ~MachO::MH_BINDATLOAD; |
| 2876 | } |
| 2877 | if (f & MachO::MH_PREBOUND) { |
| 2878 | outs() << " PREBOUND"; |
| 2879 | f &= ~MachO::MH_PREBOUND; |
| 2880 | } |
| 2881 | if (f & MachO::MH_SPLIT_SEGS) { |
| 2882 | outs() << " SPLIT_SEGS"; |
| 2883 | f &= ~MachO::MH_SPLIT_SEGS; |
| 2884 | } |
| 2885 | if (f & MachO::MH_LAZY_INIT) { |
| 2886 | outs() << " LAZY_INIT"; |
| 2887 | f &= ~MachO::MH_LAZY_INIT; |
| 2888 | } |
| 2889 | if (f & MachO::MH_TWOLEVEL) { |
| 2890 | outs() << " TWOLEVEL"; |
| 2891 | f &= ~MachO::MH_TWOLEVEL; |
| 2892 | } |
| 2893 | if (f & MachO::MH_FORCE_FLAT) { |
| 2894 | outs() << " FORCE_FLAT"; |
| 2895 | f &= ~MachO::MH_FORCE_FLAT; |
| 2896 | } |
| 2897 | if (f & MachO::MH_NOMULTIDEFS) { |
| 2898 | outs() << " NOMULTIDEFS"; |
| 2899 | f &= ~MachO::MH_NOMULTIDEFS; |
| 2900 | } |
| 2901 | if (f & MachO::MH_NOFIXPREBINDING) { |
| 2902 | outs() << " NOFIXPREBINDING"; |
| 2903 | f &= ~MachO::MH_NOFIXPREBINDING; |
| 2904 | } |
| 2905 | if (f & MachO::MH_PREBINDABLE) { |
| 2906 | outs() << " PREBINDABLE"; |
| 2907 | f &= ~MachO::MH_PREBINDABLE; |
| 2908 | } |
| 2909 | if (f & MachO::MH_ALLMODSBOUND) { |
| 2910 | outs() << " ALLMODSBOUND"; |
| 2911 | f &= ~MachO::MH_ALLMODSBOUND; |
| 2912 | } |
| 2913 | if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) { |
| 2914 | outs() << " SUBSECTIONS_VIA_SYMBOLS"; |
| 2915 | f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS; |
| 2916 | } |
| 2917 | if (f & MachO::MH_CANONICAL) { |
| 2918 | outs() << " CANONICAL"; |
| 2919 | f &= ~MachO::MH_CANONICAL; |
| 2920 | } |
| 2921 | if (f & MachO::MH_WEAK_DEFINES) { |
| 2922 | outs() << " WEAK_DEFINES"; |
| 2923 | f &= ~MachO::MH_WEAK_DEFINES; |
| 2924 | } |
| 2925 | if (f & MachO::MH_BINDS_TO_WEAK) { |
| 2926 | outs() << " BINDS_TO_WEAK"; |
| 2927 | f &= ~MachO::MH_BINDS_TO_WEAK; |
| 2928 | } |
| 2929 | if (f & MachO::MH_ALLOW_STACK_EXECUTION) { |
| 2930 | outs() << " ALLOW_STACK_EXECUTION"; |
| 2931 | f &= ~MachO::MH_ALLOW_STACK_EXECUTION; |
| 2932 | } |
| 2933 | if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) { |
| 2934 | outs() << " DEAD_STRIPPABLE_DYLIB"; |
| 2935 | f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB; |
| 2936 | } |
| 2937 | if (f & MachO::MH_PIE) { |
| 2938 | outs() << " PIE"; |
| 2939 | f &= ~MachO::MH_PIE; |
| 2940 | } |
| 2941 | if (f & MachO::MH_NO_REEXPORTED_DYLIBS) { |
| 2942 | outs() << " NO_REEXPORTED_DYLIBS"; |
| 2943 | f &= ~MachO::MH_NO_REEXPORTED_DYLIBS; |
| 2944 | } |
| 2945 | if (f & MachO::MH_HAS_TLV_DESCRIPTORS) { |
| 2946 | outs() << " MH_HAS_TLV_DESCRIPTORS"; |
| 2947 | f &= ~MachO::MH_HAS_TLV_DESCRIPTORS; |
| 2948 | } |
| 2949 | if (f & MachO::MH_NO_HEAP_EXECUTION) { |
| 2950 | outs() << " MH_NO_HEAP_EXECUTION"; |
| 2951 | f &= ~MachO::MH_NO_HEAP_EXECUTION; |
| 2952 | } |
| 2953 | if (f & MachO::MH_APP_EXTENSION_SAFE) { |
| 2954 | outs() << " APP_EXTENSION_SAFE"; |
| 2955 | f &= ~MachO::MH_APP_EXTENSION_SAFE; |
| 2956 | } |
| 2957 | if (f != 0 || flags == 0) |
| 2958 | outs() << format(" 0x%08" PRIx32, f); |
| 2959 | } else { |
| 2960 | outs() << format(" 0x%08" PRIx32, magic); |
| 2961 | outs() << format(" %7d", cputype); |
| 2962 | outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK); |
| 2963 | outs() << format(" 0x%02" PRIx32, |
| 2964 | (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24); |
| 2965 | outs() << format(" %10u", filetype); |
| 2966 | outs() << format(" %5u", ncmds); |
| 2967 | outs() << format(" %10u", sizeofcmds); |
| 2968 | outs() << format(" 0x%08" PRIx32, flags); |
| 2969 | } |
| 2970 | outs() << "\n"; |
| 2971 | } |
| 2972 | |
Kevin Enderby | 956366c | 2014-08-29 22:30:52 +0000 | [diff] [blame] | 2973 | static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize, |
| 2974 | StringRef SegName, uint64_t vmaddr, |
| 2975 | uint64_t vmsize, uint64_t fileoff, |
| 2976 | uint64_t filesize, uint32_t maxprot, |
| 2977 | uint32_t initprot, uint32_t nsects, |
| 2978 | uint32_t flags, uint32_t object_size, |
| 2979 | bool verbose) { |
| 2980 | uint64_t expected_cmdsize; |
| 2981 | if (cmd == MachO::LC_SEGMENT) { |
| 2982 | outs() << " cmd LC_SEGMENT\n"; |
| 2983 | expected_cmdsize = nsects; |
| 2984 | expected_cmdsize *= sizeof(struct MachO::section); |
| 2985 | expected_cmdsize += sizeof(struct MachO::segment_command); |
| 2986 | } else { |
| 2987 | outs() << " cmd LC_SEGMENT_64\n"; |
| 2988 | expected_cmdsize = nsects; |
| 2989 | expected_cmdsize *= sizeof(struct MachO::section_64); |
| 2990 | expected_cmdsize += sizeof(struct MachO::segment_command_64); |
| 2991 | } |
| 2992 | outs() << " cmdsize " << cmdsize; |
| 2993 | if (cmdsize != expected_cmdsize) |
| 2994 | outs() << " Inconsistent size\n"; |
| 2995 | else |
| 2996 | outs() << "\n"; |
| 2997 | outs() << " segname " << SegName << "\n"; |
| 2998 | if (cmd == MachO::LC_SEGMENT_64) { |
| 2999 | outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n"; |
| 3000 | outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n"; |
| 3001 | } else { |
Kevin Enderby | adb7c43 | 2014-12-16 18:58:11 +0000 | [diff] [blame] | 3002 | outs() << " vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n"; |
| 3003 | outs() << " vmsize " << format("0x%08" PRIx64, vmsize) << "\n"; |
Kevin Enderby | 956366c | 2014-08-29 22:30:52 +0000 | [diff] [blame] | 3004 | } |
| 3005 | outs() << " fileoff " << fileoff; |
| 3006 | if (fileoff > object_size) |
| 3007 | outs() << " (past end of file)\n"; |
| 3008 | else |
| 3009 | outs() << "\n"; |
| 3010 | outs() << " filesize " << filesize; |
| 3011 | if (fileoff + filesize > object_size) |
| 3012 | outs() << " (past end of file)\n"; |
| 3013 | else |
| 3014 | outs() << "\n"; |
| 3015 | if (verbose) { |
| 3016 | if ((maxprot & |
| 3017 | ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE | |
| 3018 | MachO::VM_PROT_EXECUTE)) != 0) |
| 3019 | outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n"; |
| 3020 | else { |
| 3021 | if (maxprot & MachO::VM_PROT_READ) |
| 3022 | outs() << " maxprot r"; |
| 3023 | else |
| 3024 | outs() << " maxprot -"; |
| 3025 | if (maxprot & MachO::VM_PROT_WRITE) |
| 3026 | outs() << "w"; |
| 3027 | else |
| 3028 | outs() << "-"; |
| 3029 | if (maxprot & MachO::VM_PROT_EXECUTE) |
| 3030 | outs() << "x\n"; |
| 3031 | else |
| 3032 | outs() << "-\n"; |
| 3033 | } |
| 3034 | if ((initprot & |
| 3035 | ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE | |
| 3036 | MachO::VM_PROT_EXECUTE)) != 0) |
| 3037 | outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n"; |
| 3038 | else { |
| 3039 | if (initprot & MachO::VM_PROT_READ) |
| 3040 | outs() << " initprot r"; |
| 3041 | else |
| 3042 | outs() << " initprot -"; |
| 3043 | if (initprot & MachO::VM_PROT_WRITE) |
| 3044 | outs() << "w"; |
| 3045 | else |
| 3046 | outs() << "-"; |
| 3047 | if (initprot & MachO::VM_PROT_EXECUTE) |
| 3048 | outs() << "x\n"; |
| 3049 | else |
| 3050 | outs() << "-\n"; |
| 3051 | } |
| 3052 | } else { |
| 3053 | outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n"; |
| 3054 | outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n"; |
| 3055 | } |
| 3056 | outs() << " nsects " << nsects << "\n"; |
| 3057 | if (verbose) { |
| 3058 | outs() << " flags"; |
| 3059 | if (flags == 0) |
| 3060 | outs() << " (none)\n"; |
| 3061 | else { |
| 3062 | if (flags & MachO::SG_HIGHVM) { |
| 3063 | outs() << " HIGHVM"; |
| 3064 | flags &= ~MachO::SG_HIGHVM; |
| 3065 | } |
| 3066 | if (flags & MachO::SG_FVMLIB) { |
| 3067 | outs() << " FVMLIB"; |
| 3068 | flags &= ~MachO::SG_FVMLIB; |
| 3069 | } |
| 3070 | if (flags & MachO::SG_NORELOC) { |
| 3071 | outs() << " NORELOC"; |
| 3072 | flags &= ~MachO::SG_NORELOC; |
| 3073 | } |
| 3074 | if (flags & MachO::SG_PROTECTED_VERSION_1) { |
| 3075 | outs() << " PROTECTED_VERSION_1"; |
| 3076 | flags &= ~MachO::SG_PROTECTED_VERSION_1; |
| 3077 | } |
| 3078 | if (flags) |
| 3079 | outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n"; |
| 3080 | else |
| 3081 | outs() << "\n"; |
| 3082 | } |
| 3083 | } else { |
| 3084 | outs() << " flags " << format("0x%" PRIx32, flags) << "\n"; |
| 3085 | } |
| 3086 | } |
| 3087 | |
| 3088 | static void PrintSection(const char *sectname, const char *segname, |
| 3089 | uint64_t addr, uint64_t size, uint32_t offset, |
| 3090 | uint32_t align, uint32_t reloff, uint32_t nreloc, |
| 3091 | uint32_t flags, uint32_t reserved1, uint32_t reserved2, |
| 3092 | uint32_t cmd, const char *sg_segname, |
| 3093 | uint32_t filetype, uint32_t object_size, |
| 3094 | bool verbose) { |
| 3095 | outs() << "Section\n"; |
| 3096 | outs() << " sectname " << format("%.16s\n", sectname); |
| 3097 | outs() << " segname " << format("%.16s", segname); |
| 3098 | if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0) |
| 3099 | outs() << " (does not match segment)\n"; |
| 3100 | else |
| 3101 | outs() << "\n"; |
| 3102 | if (cmd == MachO::LC_SEGMENT_64) { |
| 3103 | outs() << " addr " << format("0x%016" PRIx64, addr) << "\n"; |
| 3104 | outs() << " size " << format("0x%016" PRIx64, size); |
| 3105 | } else { |
Kevin Enderby | 75594b6 | 2014-12-16 21:00:25 +0000 | [diff] [blame] | 3106 | outs() << " addr " << format("0x%08" PRIx64, addr) << "\n"; |
| 3107 | outs() << " size " << format("0x%08" PRIx64, size); |
Kevin Enderby | 956366c | 2014-08-29 22:30:52 +0000 | [diff] [blame] | 3108 | } |
| 3109 | if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size) |
| 3110 | outs() << " (past end of file)\n"; |
| 3111 | else |
| 3112 | outs() << "\n"; |
| 3113 | outs() << " offset " << offset; |
| 3114 | if (offset > object_size) |
| 3115 | outs() << " (past end of file)\n"; |
| 3116 | else |
| 3117 | outs() << "\n"; |
| 3118 | uint32_t align_shifted = 1 << align; |
| 3119 | outs() << " align 2^" << align << " (" << align_shifted << ")\n"; |
| 3120 | outs() << " reloff " << reloff; |
| 3121 | if (reloff > object_size) |
| 3122 | outs() << " (past end of file)\n"; |
| 3123 | else |
| 3124 | outs() << "\n"; |
| 3125 | outs() << " nreloc " << nreloc; |
| 3126 | if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size) |
| 3127 | outs() << " (past end of file)\n"; |
| 3128 | else |
| 3129 | outs() << "\n"; |
| 3130 | uint32_t section_type = flags & MachO::SECTION_TYPE; |
| 3131 | if (verbose) { |
| 3132 | outs() << " type"; |
| 3133 | if (section_type == MachO::S_REGULAR) |
| 3134 | outs() << " S_REGULAR\n"; |
| 3135 | else if (section_type == MachO::S_ZEROFILL) |
| 3136 | outs() << " S_ZEROFILL\n"; |
| 3137 | else if (section_type == MachO::S_CSTRING_LITERALS) |
| 3138 | outs() << " S_CSTRING_LITERALS\n"; |
| 3139 | else if (section_type == MachO::S_4BYTE_LITERALS) |
| 3140 | outs() << " S_4BYTE_LITERALS\n"; |
| 3141 | else if (section_type == MachO::S_8BYTE_LITERALS) |
| 3142 | outs() << " S_8BYTE_LITERALS\n"; |
| 3143 | else if (section_type == MachO::S_16BYTE_LITERALS) |
| 3144 | outs() << " S_16BYTE_LITERALS\n"; |
| 3145 | else if (section_type == MachO::S_LITERAL_POINTERS) |
| 3146 | outs() << " S_LITERAL_POINTERS\n"; |
| 3147 | else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS) |
| 3148 | outs() << " S_NON_LAZY_SYMBOL_POINTERS\n"; |
| 3149 | else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS) |
| 3150 | outs() << " S_LAZY_SYMBOL_POINTERS\n"; |
| 3151 | else if (section_type == MachO::S_SYMBOL_STUBS) |
| 3152 | outs() << " S_SYMBOL_STUBS\n"; |
| 3153 | else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS) |
| 3154 | outs() << " S_MOD_INIT_FUNC_POINTERS\n"; |
| 3155 | else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS) |
| 3156 | outs() << " S_MOD_TERM_FUNC_POINTERS\n"; |
| 3157 | else if (section_type == MachO::S_COALESCED) |
| 3158 | outs() << " S_COALESCED\n"; |
| 3159 | else if (section_type == MachO::S_INTERPOSING) |
| 3160 | outs() << " S_INTERPOSING\n"; |
| 3161 | else if (section_type == MachO::S_DTRACE_DOF) |
| 3162 | outs() << " S_DTRACE_DOF\n"; |
| 3163 | else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS) |
| 3164 | outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n"; |
| 3165 | else if (section_type == MachO::S_THREAD_LOCAL_REGULAR) |
| 3166 | outs() << " S_THREAD_LOCAL_REGULAR\n"; |
| 3167 | else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL) |
| 3168 | outs() << " S_THREAD_LOCAL_ZEROFILL\n"; |
| 3169 | else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES) |
| 3170 | outs() << " S_THREAD_LOCAL_VARIABLES\n"; |
| 3171 | else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS) |
| 3172 | outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n"; |
| 3173 | else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS) |
| 3174 | outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n"; |
| 3175 | else |
| 3176 | outs() << format("0x%08" PRIx32, section_type) << "\n"; |
| 3177 | outs() << "attributes"; |
| 3178 | uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES; |
| 3179 | if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS) |
| 3180 | outs() << " PURE_INSTRUCTIONS"; |
| 3181 | if (section_attributes & MachO::S_ATTR_NO_TOC) |
| 3182 | outs() << " NO_TOC"; |
| 3183 | if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS) |
| 3184 | outs() << " STRIP_STATIC_SYMS"; |
| 3185 | if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP) |
| 3186 | outs() << " NO_DEAD_STRIP"; |
| 3187 | if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT) |
| 3188 | outs() << " LIVE_SUPPORT"; |
| 3189 | if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE) |
| 3190 | outs() << " SELF_MODIFYING_CODE"; |
| 3191 | if (section_attributes & MachO::S_ATTR_DEBUG) |
| 3192 | outs() << " DEBUG"; |
| 3193 | if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS) |
| 3194 | outs() << " SOME_INSTRUCTIONS"; |
| 3195 | if (section_attributes & MachO::S_ATTR_EXT_RELOC) |
| 3196 | outs() << " EXT_RELOC"; |
| 3197 | if (section_attributes & MachO::S_ATTR_LOC_RELOC) |
| 3198 | outs() << " LOC_RELOC"; |
| 3199 | if (section_attributes == 0) |
| 3200 | outs() << " (none)"; |
| 3201 | outs() << "\n"; |
| 3202 | } else |
| 3203 | outs() << " flags " << format("0x%08" PRIx32, flags) << "\n"; |
| 3204 | outs() << " reserved1 " << reserved1; |
| 3205 | if (section_type == MachO::S_SYMBOL_STUBS || |
| 3206 | section_type == MachO::S_LAZY_SYMBOL_POINTERS || |
| 3207 | section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS || |
| 3208 | section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS || |
| 3209 | section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS) |
| 3210 | outs() << " (index into indirect symbol table)\n"; |
| 3211 | else |
| 3212 | outs() << "\n"; |
| 3213 | outs() << " reserved2 " << reserved2; |
| 3214 | if (section_type == MachO::S_SYMBOL_STUBS) |
| 3215 | outs() << " (size of stubs)\n"; |
| 3216 | else |
| 3217 | outs() << "\n"; |
| 3218 | } |
| 3219 | |
David Majnemer | 73cc6ff | 2014-11-13 19:48:56 +0000 | [diff] [blame] | 3220 | static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit, |
Kevin Enderby | 956366c | 2014-08-29 22:30:52 +0000 | [diff] [blame] | 3221 | uint32_t object_size) { |
| 3222 | outs() << " cmd LC_SYMTAB\n"; |
| 3223 | outs() << " cmdsize " << st.cmdsize; |
| 3224 | if (st.cmdsize != sizeof(struct MachO::symtab_command)) |
| 3225 | outs() << " Incorrect size\n"; |
| 3226 | else |
| 3227 | outs() << "\n"; |
| 3228 | outs() << " symoff " << st.symoff; |
| 3229 | if (st.symoff > object_size) |
| 3230 | outs() << " (past end of file)\n"; |
| 3231 | else |
| 3232 | outs() << "\n"; |
| 3233 | outs() << " nsyms " << st.nsyms; |
| 3234 | uint64_t big_size; |
David Majnemer | 73cc6ff | 2014-11-13 19:48:56 +0000 | [diff] [blame] | 3235 | if (Is64Bit) { |
Kevin Enderby | 956366c | 2014-08-29 22:30:52 +0000 | [diff] [blame] | 3236 | big_size = st.nsyms; |
| 3237 | big_size *= sizeof(struct MachO::nlist_64); |
| 3238 | big_size += st.symoff; |
| 3239 | if (big_size > object_size) |
| 3240 | outs() << " (past end of file)\n"; |
| 3241 | else |
| 3242 | outs() << "\n"; |
| 3243 | } else { |
| 3244 | big_size = st.nsyms; |
| 3245 | big_size *= sizeof(struct MachO::nlist); |
| 3246 | big_size += st.symoff; |
| 3247 | if (big_size > object_size) |
| 3248 | outs() << " (past end of file)\n"; |
| 3249 | else |
| 3250 | outs() << "\n"; |
| 3251 | } |
| 3252 | outs() << " stroff " << st.stroff; |
| 3253 | if (st.stroff > object_size) |
| 3254 | outs() << " (past end of file)\n"; |
| 3255 | else |
| 3256 | outs() << "\n"; |
| 3257 | outs() << " strsize " << st.strsize; |
| 3258 | big_size = st.stroff; |
| 3259 | big_size += st.strsize; |
| 3260 | if (big_size > object_size) |
| 3261 | outs() << " (past end of file)\n"; |
| 3262 | else |
| 3263 | outs() << "\n"; |
| 3264 | } |
| 3265 | |
| 3266 | static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst, |
| 3267 | uint32_t nsyms, uint32_t object_size, |
David Majnemer | 73cc6ff | 2014-11-13 19:48:56 +0000 | [diff] [blame] | 3268 | bool Is64Bit) { |
Kevin Enderby | 956366c | 2014-08-29 22:30:52 +0000 | [diff] [blame] | 3269 | outs() << " cmd LC_DYSYMTAB\n"; |
| 3270 | outs() << " cmdsize " << dyst.cmdsize; |
| 3271 | if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command)) |
| 3272 | outs() << " Incorrect size\n"; |
| 3273 | else |
| 3274 | outs() << "\n"; |
| 3275 | outs() << " ilocalsym " << dyst.ilocalsym; |
| 3276 | if (dyst.ilocalsym > nsyms) |
| 3277 | outs() << " (greater than the number of symbols)\n"; |
| 3278 | else |
| 3279 | outs() << "\n"; |
| 3280 | outs() << " nlocalsym " << dyst.nlocalsym; |
| 3281 | uint64_t big_size; |
| 3282 | big_size = dyst.ilocalsym; |
| 3283 | big_size += dyst.nlocalsym; |
| 3284 | if (big_size > nsyms) |
| 3285 | outs() << " (past the end of the symbol table)\n"; |
| 3286 | else |
| 3287 | outs() << "\n"; |
| 3288 | outs() << " iextdefsym " << dyst.iextdefsym; |
| 3289 | if (dyst.iextdefsym > nsyms) |
| 3290 | outs() << " (greater than the number of symbols)\n"; |
| 3291 | else |
| 3292 | outs() << "\n"; |
| 3293 | outs() << " nextdefsym " << dyst.nextdefsym; |
| 3294 | big_size = dyst.iextdefsym; |
| 3295 | big_size += dyst.nextdefsym; |
| 3296 | if (big_size > nsyms) |
| 3297 | outs() << " (past the end of the symbol table)\n"; |
| 3298 | else |
| 3299 | outs() << "\n"; |
| 3300 | outs() << " iundefsym " << dyst.iundefsym; |
| 3301 | if (dyst.iundefsym > nsyms) |
| 3302 | outs() << " (greater than the number of symbols)\n"; |
| 3303 | else |
| 3304 | outs() << "\n"; |
| 3305 | outs() << " nundefsym " << dyst.nundefsym; |
| 3306 | big_size = dyst.iundefsym; |
| 3307 | big_size += dyst.nundefsym; |
| 3308 | if (big_size > nsyms) |
| 3309 | outs() << " (past the end of the symbol table)\n"; |
| 3310 | else |
| 3311 | outs() << "\n"; |
| 3312 | outs() << " tocoff " << dyst.tocoff; |
| 3313 | if (dyst.tocoff > object_size) |
| 3314 | outs() << " (past end of file)\n"; |
| 3315 | else |
| 3316 | outs() << "\n"; |
| 3317 | outs() << " ntoc " << dyst.ntoc; |
| 3318 | big_size = dyst.ntoc; |
| 3319 | big_size *= sizeof(struct MachO::dylib_table_of_contents); |
| 3320 | big_size += dyst.tocoff; |
| 3321 | if (big_size > object_size) |
| 3322 | outs() << " (past end of file)\n"; |
| 3323 | else |
| 3324 | outs() << "\n"; |
| 3325 | outs() << " modtaboff " << dyst.modtaboff; |
| 3326 | if (dyst.modtaboff > object_size) |
| 3327 | outs() << " (past end of file)\n"; |
| 3328 | else |
| 3329 | outs() << "\n"; |
| 3330 | outs() << " nmodtab " << dyst.nmodtab; |
| 3331 | uint64_t modtabend; |
David Majnemer | 73cc6ff | 2014-11-13 19:48:56 +0000 | [diff] [blame] | 3332 | if (Is64Bit) { |
Kevin Enderby | 956366c | 2014-08-29 22:30:52 +0000 | [diff] [blame] | 3333 | modtabend = dyst.nmodtab; |
| 3334 | modtabend *= sizeof(struct MachO::dylib_module_64); |
| 3335 | modtabend += dyst.modtaboff; |
| 3336 | } else { |
| 3337 | modtabend = dyst.nmodtab; |
| 3338 | modtabend *= sizeof(struct MachO::dylib_module); |
| 3339 | modtabend += dyst.modtaboff; |
| 3340 | } |
| 3341 | if (modtabend > object_size) |
| 3342 | outs() << " (past end of file)\n"; |
| 3343 | else |
| 3344 | outs() << "\n"; |
| 3345 | outs() << " extrefsymoff " << dyst.extrefsymoff; |
| 3346 | if (dyst.extrefsymoff > object_size) |
| 3347 | outs() << " (past end of file)\n"; |
| 3348 | else |
| 3349 | outs() << "\n"; |
| 3350 | outs() << " nextrefsyms " << dyst.nextrefsyms; |
| 3351 | big_size = dyst.nextrefsyms; |
| 3352 | big_size *= sizeof(struct MachO::dylib_reference); |
| 3353 | big_size += dyst.extrefsymoff; |
| 3354 | if (big_size > object_size) |
| 3355 | outs() << " (past end of file)\n"; |
| 3356 | else |
| 3357 | outs() << "\n"; |
| 3358 | outs() << " indirectsymoff " << dyst.indirectsymoff; |
| 3359 | if (dyst.indirectsymoff > object_size) |
| 3360 | outs() << " (past end of file)\n"; |
| 3361 | else |
| 3362 | outs() << "\n"; |
| 3363 | outs() << " nindirectsyms " << dyst.nindirectsyms; |
| 3364 | big_size = dyst.nindirectsyms; |
| 3365 | big_size *= sizeof(uint32_t); |
| 3366 | big_size += dyst.indirectsymoff; |
| 3367 | if (big_size > object_size) |
| 3368 | outs() << " (past end of file)\n"; |
| 3369 | else |
| 3370 | outs() << "\n"; |
| 3371 | outs() << " extreloff " << dyst.extreloff; |
| 3372 | if (dyst.extreloff > object_size) |
| 3373 | outs() << " (past end of file)\n"; |
| 3374 | else |
| 3375 | outs() << "\n"; |
| 3376 | outs() << " nextrel " << dyst.nextrel; |
| 3377 | big_size = dyst.nextrel; |
| 3378 | big_size *= sizeof(struct MachO::relocation_info); |
| 3379 | big_size += dyst.extreloff; |
| 3380 | if (big_size > object_size) |
| 3381 | outs() << " (past end of file)\n"; |
| 3382 | else |
| 3383 | outs() << "\n"; |
| 3384 | outs() << " locreloff " << dyst.locreloff; |
| 3385 | if (dyst.locreloff > object_size) |
| 3386 | outs() << " (past end of file)\n"; |
| 3387 | else |
| 3388 | outs() << "\n"; |
| 3389 | outs() << " nlocrel " << dyst.nlocrel; |
| 3390 | big_size = dyst.nlocrel; |
| 3391 | big_size *= sizeof(struct MachO::relocation_info); |
| 3392 | big_size += dyst.locreloff; |
| 3393 | if (big_size > object_size) |
| 3394 | outs() << " (past end of file)\n"; |
| 3395 | else |
| 3396 | outs() << "\n"; |
| 3397 | } |
| 3398 | |
Kevin Enderby | 8ae63c1 | 2014-09-04 16:54:47 +0000 | [diff] [blame] | 3399 | static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc, |
| 3400 | uint32_t object_size) { |
| 3401 | if (dc.cmd == MachO::LC_DYLD_INFO) |
| 3402 | outs() << " cmd LC_DYLD_INFO\n"; |
| 3403 | else |
| 3404 | outs() << " cmd LC_DYLD_INFO_ONLY\n"; |
| 3405 | outs() << " cmdsize " << dc.cmdsize; |
| 3406 | if (dc.cmdsize != sizeof(struct MachO::dyld_info_command)) |
| 3407 | outs() << " Incorrect size\n"; |
| 3408 | else |
| 3409 | outs() << "\n"; |
| 3410 | outs() << " rebase_off " << dc.rebase_off; |
| 3411 | if (dc.rebase_off > object_size) |
| 3412 | outs() << " (past end of file)\n"; |
| 3413 | else |
| 3414 | outs() << "\n"; |
| 3415 | outs() << " rebase_size " << dc.rebase_size; |
| 3416 | uint64_t big_size; |
| 3417 | big_size = dc.rebase_off; |
| 3418 | big_size += dc.rebase_size; |
| 3419 | if (big_size > object_size) |
| 3420 | outs() << " (past end of file)\n"; |
| 3421 | else |
| 3422 | outs() << "\n"; |
| 3423 | outs() << " bind_off " << dc.bind_off; |
| 3424 | if (dc.bind_off > object_size) |
| 3425 | outs() << " (past end of file)\n"; |
| 3426 | else |
| 3427 | outs() << "\n"; |
| 3428 | outs() << " bind_size " << dc.bind_size; |
| 3429 | big_size = dc.bind_off; |
| 3430 | big_size += dc.bind_size; |
| 3431 | if (big_size > object_size) |
| 3432 | outs() << " (past end of file)\n"; |
| 3433 | else |
| 3434 | outs() << "\n"; |
| 3435 | outs() << " weak_bind_off " << dc.weak_bind_off; |
| 3436 | if (dc.weak_bind_off > object_size) |
| 3437 | outs() << " (past end of file)\n"; |
| 3438 | else |
| 3439 | outs() << "\n"; |
| 3440 | outs() << " weak_bind_size " << dc.weak_bind_size; |
| 3441 | big_size = dc.weak_bind_off; |
| 3442 | big_size += dc.weak_bind_size; |
| 3443 | if (big_size > object_size) |
| 3444 | outs() << " (past end of file)\n"; |
| 3445 | else |
| 3446 | outs() << "\n"; |
| 3447 | outs() << " lazy_bind_off " << dc.lazy_bind_off; |
| 3448 | if (dc.lazy_bind_off > object_size) |
| 3449 | outs() << " (past end of file)\n"; |
| 3450 | else |
| 3451 | outs() << "\n"; |
| 3452 | outs() << " lazy_bind_size " << dc.lazy_bind_size; |
| 3453 | big_size = dc.lazy_bind_off; |
| 3454 | big_size += dc.lazy_bind_size; |
| 3455 | if (big_size > object_size) |
| 3456 | outs() << " (past end of file)\n"; |
| 3457 | else |
| 3458 | outs() << "\n"; |
| 3459 | outs() << " export_off " << dc.export_off; |
| 3460 | if (dc.export_off > object_size) |
| 3461 | outs() << " (past end of file)\n"; |
| 3462 | else |
| 3463 | outs() << "\n"; |
| 3464 | outs() << " export_size " << dc.export_size; |
| 3465 | big_size = dc.export_off; |
| 3466 | big_size += dc.export_size; |
| 3467 | if (big_size > object_size) |
| 3468 | outs() << " (past end of file)\n"; |
| 3469 | else |
| 3470 | outs() << "\n"; |
| 3471 | } |
| 3472 | |
| 3473 | static void PrintDyldLoadCommand(MachO::dylinker_command dyld, |
| 3474 | const char *Ptr) { |
| 3475 | if (dyld.cmd == MachO::LC_ID_DYLINKER) |
| 3476 | outs() << " cmd LC_ID_DYLINKER\n"; |
| 3477 | else if (dyld.cmd == MachO::LC_LOAD_DYLINKER) |
| 3478 | outs() << " cmd LC_LOAD_DYLINKER\n"; |
| 3479 | else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT) |
| 3480 | outs() << " cmd LC_DYLD_ENVIRONMENT\n"; |
| 3481 | else |
| 3482 | outs() << " cmd ?(" << dyld.cmd << ")\n"; |
| 3483 | outs() << " cmdsize " << dyld.cmdsize; |
| 3484 | if (dyld.cmdsize < sizeof(struct MachO::dylinker_command)) |
| 3485 | outs() << " Incorrect size\n"; |
| 3486 | else |
| 3487 | outs() << "\n"; |
| 3488 | if (dyld.name >= dyld.cmdsize) |
| 3489 | outs() << " name ?(bad offset " << dyld.name << ")\n"; |
| 3490 | else { |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 3491 | const char *P = (const char *)(Ptr) + dyld.name; |
Kevin Enderby | 8ae63c1 | 2014-09-04 16:54:47 +0000 | [diff] [blame] | 3492 | outs() << " name " << P << " (offset " << dyld.name << ")\n"; |
| 3493 | } |
| 3494 | } |
| 3495 | |
| 3496 | static void PrintUuidLoadCommand(MachO::uuid_command uuid) { |
| 3497 | outs() << " cmd LC_UUID\n"; |
| 3498 | outs() << " cmdsize " << uuid.cmdsize; |
| 3499 | if (uuid.cmdsize != sizeof(struct MachO::uuid_command)) |
| 3500 | outs() << " Incorrect size\n"; |
| 3501 | else |
| 3502 | outs() << "\n"; |
| 3503 | outs() << " uuid "; |
| 3504 | outs() << format("%02" PRIX32, uuid.uuid[0]); |
| 3505 | outs() << format("%02" PRIX32, uuid.uuid[1]); |
| 3506 | outs() << format("%02" PRIX32, uuid.uuid[2]); |
| 3507 | outs() << format("%02" PRIX32, uuid.uuid[3]); |
| 3508 | outs() << "-"; |
| 3509 | outs() << format("%02" PRIX32, uuid.uuid[4]); |
| 3510 | outs() << format("%02" PRIX32, uuid.uuid[5]); |
| 3511 | outs() << "-"; |
| 3512 | outs() << format("%02" PRIX32, uuid.uuid[6]); |
| 3513 | outs() << format("%02" PRIX32, uuid.uuid[7]); |
| 3514 | outs() << "-"; |
| 3515 | outs() << format("%02" PRIX32, uuid.uuid[8]); |
| 3516 | outs() << format("%02" PRIX32, uuid.uuid[9]); |
| 3517 | outs() << "-"; |
| 3518 | outs() << format("%02" PRIX32, uuid.uuid[10]); |
| 3519 | outs() << format("%02" PRIX32, uuid.uuid[11]); |
| 3520 | outs() << format("%02" PRIX32, uuid.uuid[12]); |
| 3521 | outs() << format("%02" PRIX32, uuid.uuid[13]); |
| 3522 | outs() << format("%02" PRIX32, uuid.uuid[14]); |
| 3523 | outs() << format("%02" PRIX32, uuid.uuid[15]); |
| 3524 | outs() << "\n"; |
| 3525 | } |
| 3526 | |
Jean-Daniel Dupas | 00cc1f5 | 2014-12-04 07:37:02 +0000 | [diff] [blame] | 3527 | static void PrintRpathLoadCommand(MachO::rpath_command rpath, |
| 3528 | const char *Ptr) { |
| 3529 | outs() << " cmd LC_RPATH\n"; |
| 3530 | outs() << " cmdsize " << rpath.cmdsize; |
| 3531 | if (rpath.cmdsize < sizeof(struct MachO::rpath_command)) |
| 3532 | outs() << " Incorrect size\n"; |
| 3533 | else |
| 3534 | outs() << "\n"; |
| 3535 | if (rpath.path >= rpath.cmdsize) |
| 3536 | outs() << " path ?(bad offset " << rpath.path << ")\n"; |
| 3537 | else { |
| 3538 | const char *P = (const char *)(Ptr) + rpath.path; |
| 3539 | outs() << " path " << P << " (offset " << rpath.path << ")\n"; |
| 3540 | } |
| 3541 | } |
| 3542 | |
Kevin Enderby | 8ae63c1 | 2014-09-04 16:54:47 +0000 | [diff] [blame] | 3543 | static void PrintVersionMinLoadCommand(MachO::version_min_command vd) { |
| 3544 | if (vd.cmd == MachO::LC_VERSION_MIN_MACOSX) |
| 3545 | outs() << " cmd LC_VERSION_MIN_MACOSX\n"; |
| 3546 | else if (vd.cmd == MachO::LC_VERSION_MIN_IPHONEOS) |
| 3547 | outs() << " cmd LC_VERSION_MIN_IPHONEOS\n"; |
| 3548 | else |
| 3549 | outs() << " cmd " << vd.cmd << " (?)\n"; |
| 3550 | outs() << " cmdsize " << vd.cmdsize; |
| 3551 | if (vd.cmdsize != sizeof(struct MachO::version_min_command)) |
| 3552 | outs() << " Incorrect size\n"; |
| 3553 | else |
| 3554 | outs() << "\n"; |
| 3555 | outs() << " version " << ((vd.version >> 16) & 0xffff) << "." |
| 3556 | << ((vd.version >> 8) & 0xff); |
| 3557 | if ((vd.version & 0xff) != 0) |
| 3558 | outs() << "." << (vd.version & 0xff); |
| 3559 | outs() << "\n"; |
| 3560 | if (vd.sdk == 0) |
Kevin Enderby | 5753829 | 2014-12-17 01:01:30 +0000 | [diff] [blame] | 3561 | outs() << " sdk n/a"; |
Kevin Enderby | 8ae63c1 | 2014-09-04 16:54:47 +0000 | [diff] [blame] | 3562 | else { |
| 3563 | outs() << " sdk " << ((vd.sdk >> 16) & 0xffff) << "." |
| 3564 | << ((vd.sdk >> 8) & 0xff); |
| 3565 | } |
| 3566 | if ((vd.sdk & 0xff) != 0) |
| 3567 | outs() << "." << (vd.sdk & 0xff); |
| 3568 | outs() << "\n"; |
| 3569 | } |
| 3570 | |
| 3571 | static void PrintSourceVersionCommand(MachO::source_version_command sd) { |
| 3572 | outs() << " cmd LC_SOURCE_VERSION\n"; |
| 3573 | outs() << " cmdsize " << sd.cmdsize; |
| 3574 | if (sd.cmdsize != sizeof(struct MachO::source_version_command)) |
| 3575 | outs() << " Incorrect size\n"; |
| 3576 | else |
| 3577 | outs() << "\n"; |
| 3578 | uint64_t a = (sd.version >> 40) & 0xffffff; |
| 3579 | uint64_t b = (sd.version >> 30) & 0x3ff; |
| 3580 | uint64_t c = (sd.version >> 20) & 0x3ff; |
| 3581 | uint64_t d = (sd.version >> 10) & 0x3ff; |
| 3582 | uint64_t e = sd.version & 0x3ff; |
| 3583 | outs() << " version " << a << "." << b; |
| 3584 | if (e != 0) |
| 3585 | outs() << "." << c << "." << d << "." << e; |
| 3586 | else if (d != 0) |
| 3587 | outs() << "." << c << "." << d; |
| 3588 | else if (c != 0) |
| 3589 | outs() << "." << c; |
| 3590 | outs() << "\n"; |
| 3591 | } |
| 3592 | |
| 3593 | static void PrintEntryPointCommand(MachO::entry_point_command ep) { |
| 3594 | outs() << " cmd LC_MAIN\n"; |
| 3595 | outs() << " cmdsize " << ep.cmdsize; |
| 3596 | if (ep.cmdsize != sizeof(struct MachO::entry_point_command)) |
| 3597 | outs() << " Incorrect size\n"; |
| 3598 | else |
| 3599 | outs() << "\n"; |
| 3600 | outs() << " entryoff " << ep.entryoff << "\n"; |
| 3601 | outs() << " stacksize " << ep.stacksize << "\n"; |
| 3602 | } |
| 3603 | |
Kevin Enderby | 0804f467 | 2014-12-16 23:25:52 +0000 | [diff] [blame] | 3604 | static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec, |
| 3605 | uint32_t object_size) { |
| 3606 | outs() << " cmd LC_ENCRYPTION_INFO\n"; |
| 3607 | outs() << " cmdsize " << ec.cmdsize; |
| 3608 | if (ec.cmdsize != sizeof(struct MachO::encryption_info_command)) |
| 3609 | outs() << " Incorrect size\n"; |
| 3610 | else |
| 3611 | outs() << "\n"; |
| 3612 | outs() << " cryptoff " << ec.cryptoff; |
| 3613 | if (ec.cryptoff > object_size) |
| 3614 | outs() << " (past end of file)\n"; |
| 3615 | else |
| 3616 | outs() << "\n"; |
| 3617 | outs() << " cryptsize " << ec.cryptsize; |
| 3618 | if (ec.cryptsize > object_size) |
| 3619 | outs() << " (past end of file)\n"; |
| 3620 | else |
| 3621 | outs() << "\n"; |
| 3622 | outs() << " cryptid " << ec.cryptid << "\n"; |
| 3623 | } |
| 3624 | |
Kevin Enderby | 5753829 | 2014-12-17 01:01:30 +0000 | [diff] [blame] | 3625 | static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec, |
| 3626 | uint32_t object_size) { |
| 3627 | outs() << " cmd LC_ENCRYPTION_INFO_64\n"; |
| 3628 | outs() << " cmdsize " << ec.cmdsize; |
| 3629 | if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64)) |
| 3630 | outs() << " Incorrect size\n"; |
| 3631 | else |
| 3632 | outs() << "\n"; |
| 3633 | outs() << " cryptoff " << ec.cryptoff; |
| 3634 | if (ec.cryptoff > object_size) |
| 3635 | outs() << " (past end of file)\n"; |
| 3636 | else |
| 3637 | outs() << "\n"; |
| 3638 | outs() << " cryptsize " << ec.cryptsize; |
| 3639 | if (ec.cryptsize > object_size) |
| 3640 | outs() << " (past end of file)\n"; |
| 3641 | else |
| 3642 | outs() << "\n"; |
| 3643 | outs() << " cryptid " << ec.cryptid << "\n"; |
| 3644 | outs() << " pad " << ec.pad << "\n"; |
| 3645 | } |
| 3646 | |
Kevin Enderby | d0b6b7f | 2014-12-18 00:53:40 +0000 | [diff] [blame] | 3647 | static void PrintLinkerOptionCommand(MachO::linker_option_command lo, |
| 3648 | const char *Ptr) { |
| 3649 | outs() << " cmd LC_LINKER_OPTION\n"; |
| 3650 | outs() << " cmdsize " << lo.cmdsize; |
| 3651 | if (lo.cmdsize < sizeof(struct MachO::linker_option_command)) |
| 3652 | outs() << " Incorrect size\n"; |
| 3653 | else |
| 3654 | outs() << "\n"; |
| 3655 | outs() << " count " << lo.count << "\n"; |
| 3656 | const char *string = Ptr + sizeof(struct MachO::linker_option_command); |
| 3657 | uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command); |
| 3658 | uint32_t i = 0; |
| 3659 | while (left > 0) { |
| 3660 | while (*string == '\0' && left > 0) { |
| 3661 | string++; |
| 3662 | left--; |
| 3663 | } |
| 3664 | if (left > 0) { |
| 3665 | i++; |
| 3666 | outs() << " string #" << i << " " << format("%.*s\n", left, string); |
David Majnemer | d4449ed | 2014-12-20 08:24:43 +0000 | [diff] [blame] | 3667 | uint32_t NullPos = StringRef(string, left).find('\0'); |
| 3668 | uint32_t len = std::min(NullPos, left) + 1; |
Kevin Enderby | d0b6b7f | 2014-12-18 00:53:40 +0000 | [diff] [blame] | 3669 | string += len; |
| 3670 | left -= len; |
| 3671 | } |
| 3672 | } |
| 3673 | if (lo.count != i) |
| 3674 | outs() << " count " << lo.count << " does not match number of strings " << i |
| 3675 | << "\n"; |
| 3676 | } |
| 3677 | |
Kevin Enderby | b4b7931 | 2014-12-18 19:24:35 +0000 | [diff] [blame] | 3678 | static void PrintSubFrameworkCommand(MachO::sub_framework_command sub, |
| 3679 | const char *Ptr) { |
| 3680 | outs() << " cmd LC_SUB_FRAMEWORK\n"; |
| 3681 | outs() << " cmdsize " << sub.cmdsize; |
| 3682 | if (sub.cmdsize < sizeof(struct MachO::sub_framework_command)) |
| 3683 | outs() << " Incorrect size\n"; |
| 3684 | else |
| 3685 | outs() << "\n"; |
| 3686 | if (sub.umbrella < sub.cmdsize) { |
| 3687 | const char *P = Ptr + sub.umbrella; |
| 3688 | outs() << " umbrella " << P << " (offset " << sub.umbrella << ")\n"; |
| 3689 | } else { |
| 3690 | outs() << " umbrella ?(bad offset " << sub.umbrella << ")\n"; |
| 3691 | } |
| 3692 | } |
| 3693 | |
Kevin Enderby | a2bd8d9 | 2014-12-18 23:13:26 +0000 | [diff] [blame] | 3694 | static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub, |
| 3695 | const char *Ptr) { |
| 3696 | outs() << " cmd LC_SUB_UMBRELLA\n"; |
| 3697 | outs() << " cmdsize " << sub.cmdsize; |
| 3698 | if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command)) |
| 3699 | outs() << " Incorrect size\n"; |
| 3700 | else |
| 3701 | outs() << "\n"; |
| 3702 | if (sub.sub_umbrella < sub.cmdsize) { |
| 3703 | const char *P = Ptr + sub.sub_umbrella; |
| 3704 | outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n"; |
| 3705 | } else { |
| 3706 | outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n"; |
| 3707 | } |
| 3708 | } |
| 3709 | |
Kevin Enderby | 36c8d3a | 2014-12-19 19:48:16 +0000 | [diff] [blame] | 3710 | static void PrintSubLibraryCommand(MachO::sub_library_command sub, |
| 3711 | const char *Ptr) { |
| 3712 | outs() << " cmd LC_SUB_LIBRARY\n"; |
| 3713 | outs() << " cmdsize " << sub.cmdsize; |
| 3714 | if (sub.cmdsize < sizeof(struct MachO::sub_library_command)) |
| 3715 | outs() << " Incorrect size\n"; |
| 3716 | else |
| 3717 | outs() << "\n"; |
| 3718 | if (sub.sub_library < sub.cmdsize) { |
| 3719 | const char *P = Ptr + sub.sub_library; |
| 3720 | outs() << " sub_library " << P << " (offset " << sub.sub_library << ")\n"; |
| 3721 | } else { |
| 3722 | outs() << " sub_library ?(bad offset " << sub.sub_library << ")\n"; |
| 3723 | } |
| 3724 | } |
| 3725 | |
Kevin Enderby | 186eac3 | 2014-12-19 21:06:24 +0000 | [diff] [blame] | 3726 | static void PrintSubClientCommand(MachO::sub_client_command sub, |
| 3727 | const char *Ptr) { |
| 3728 | outs() << " cmd LC_SUB_CLIENT\n"; |
| 3729 | outs() << " cmdsize " << sub.cmdsize; |
| 3730 | if (sub.cmdsize < sizeof(struct MachO::sub_client_command)) |
| 3731 | outs() << " Incorrect size\n"; |
| 3732 | else |
| 3733 | outs() << "\n"; |
| 3734 | if (sub.client < sub.cmdsize) { |
| 3735 | const char *P = Ptr + sub.client; |
| 3736 | outs() << " client " << P << " (offset " << sub.client << ")\n"; |
| 3737 | } else { |
| 3738 | outs() << " client ?(bad offset " << sub.client << ")\n"; |
| 3739 | } |
| 3740 | } |
Kevin Enderby | 36c8d3a | 2014-12-19 19:48:16 +0000 | [diff] [blame] | 3741 | |
Kevin Enderby | 52e4ce4 | 2014-12-19 22:25:22 +0000 | [diff] [blame] | 3742 | static void PrintRoutinesCommand(MachO::routines_command r) { |
| 3743 | outs() << " cmd LC_ROUTINES\n"; |
| 3744 | outs() << " cmdsize " << r.cmdsize; |
| 3745 | if (r.cmdsize != sizeof(struct MachO::routines_command)) |
| 3746 | outs() << " Incorrect size\n"; |
| 3747 | else |
| 3748 | outs() << "\n"; |
| 3749 | outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n"; |
| 3750 | outs() << " init_module " << r.init_module << "\n"; |
| 3751 | outs() << " reserved1 " << r.reserved1 << "\n"; |
| 3752 | outs() << " reserved2 " << r.reserved2 << "\n"; |
| 3753 | outs() << " reserved3 " << r.reserved3 << "\n"; |
| 3754 | outs() << " reserved4 " << r.reserved4 << "\n"; |
| 3755 | outs() << " reserved5 " << r.reserved5 << "\n"; |
| 3756 | outs() << " reserved6 " << r.reserved6 << "\n"; |
| 3757 | } |
| 3758 | |
| 3759 | static void PrintRoutinesCommand64(MachO::routines_command_64 r) { |
| 3760 | outs() << " cmd LC_ROUTINES_64\n"; |
| 3761 | outs() << " cmdsize " << r.cmdsize; |
| 3762 | if (r.cmdsize != sizeof(struct MachO::routines_command_64)) |
| 3763 | outs() << " Incorrect size\n"; |
| 3764 | else |
| 3765 | outs() << "\n"; |
| 3766 | outs() << " init_address " << format("0x%016" PRIx64, r.init_address) << "\n"; |
| 3767 | outs() << " init_module " << r.init_module << "\n"; |
| 3768 | outs() << " reserved1 " << r.reserved1 << "\n"; |
| 3769 | outs() << " reserved2 " << r.reserved2 << "\n"; |
| 3770 | outs() << " reserved3 " << r.reserved3 << "\n"; |
| 3771 | outs() << " reserved4 " << r.reserved4 << "\n"; |
| 3772 | outs() << " reserved5 " << r.reserved5 << "\n"; |
| 3773 | outs() << " reserved6 " << r.reserved6 << "\n"; |
| 3774 | } |
| 3775 | |
Kevin Enderby | 48ef534 | 2014-12-23 22:56:39 +0000 | [diff] [blame] | 3776 | static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) { |
| 3777 | outs() << " rax " << format("0x%016" PRIx64, cpu64.rax); |
| 3778 | outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx); |
| 3779 | outs() << " rcx " << format("0x%016" PRIx64, cpu64.rcx) << "\n"; |
| 3780 | outs() << " rdx " << format("0x%016" PRIx64, cpu64.rdx); |
| 3781 | outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi); |
| 3782 | outs() << " rsi " << format("0x%016" PRIx64, cpu64.rsi) << "\n"; |
| 3783 | outs() << " rbp " << format("0x%016" PRIx64, cpu64.rbp); |
| 3784 | outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp); |
| 3785 | outs() << " r8 " << format("0x%016" PRIx64, cpu64.r8) << "\n"; |
| 3786 | outs() << " r9 " << format("0x%016" PRIx64, cpu64.r9); |
| 3787 | outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10); |
| 3788 | outs() << " r11 " << format("0x%016" PRIx64, cpu64.r11) << "\n"; |
| 3789 | outs() << " r12 " << format("0x%016" PRIx64, cpu64.r12); |
| 3790 | outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13); |
| 3791 | outs() << " r14 " << format("0x%016" PRIx64, cpu64.r14) << "\n"; |
| 3792 | outs() << " r15 " << format("0x%016" PRIx64, cpu64.r15); |
| 3793 | outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n"; |
| 3794 | outs() << "rflags " << format("0x%016" PRIx64, cpu64.rflags); |
| 3795 | outs() << " cs " << format("0x%016" PRIx64, cpu64.cs); |
| 3796 | outs() << " fs " << format("0x%016" PRIx64, cpu64.fs) << "\n"; |
| 3797 | outs() << " gs " << format("0x%016" PRIx64, cpu64.gs) << "\n"; |
| 3798 | } |
| 3799 | |
Kevin Enderby | 227df34 | 2014-12-23 23:43:59 +0000 | [diff] [blame] | 3800 | static void Print_mmst_reg(MachO::mmst_reg_t &r) { |
Kevin Enderby | 48ef534 | 2014-12-23 22:56:39 +0000 | [diff] [blame] | 3801 | uint32_t f; |
| 3802 | outs() << "\t mmst_reg "; |
| 3803 | for (f = 0; f < 10; f++) |
| 3804 | outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " "; |
| 3805 | outs() << "\n"; |
| 3806 | outs() << "\t mmst_rsrv "; |
| 3807 | for (f = 0; f < 6; f++) |
| 3808 | outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " "; |
| 3809 | outs() << "\n"; |
| 3810 | } |
| 3811 | |
Kevin Enderby | aefb003 | 2014-12-24 00:16:51 +0000 | [diff] [blame^] | 3812 | static void Print_xmm_reg(MachO::xmm_reg_t &r) { |
Kevin Enderby | 48ef534 | 2014-12-23 22:56:39 +0000 | [diff] [blame] | 3813 | uint32_t f; |
| 3814 | outs() << "\t xmm_reg "; |
| 3815 | for (f = 0; f < 16; f++) |
| 3816 | outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " "; |
| 3817 | outs() << "\n"; |
| 3818 | } |
| 3819 | |
| 3820 | static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) { |
| 3821 | outs() << "\t fpu_reserved[0] " << fpu.fpu_reserved[0]; |
| 3822 | outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n"; |
| 3823 | outs() << "\t control: invalid " << fpu.fpu_fcw.invalid; |
| 3824 | outs() << " denorm " << fpu.fpu_fcw.denorm; |
| 3825 | outs() << " zdiv " << fpu.fpu_fcw.zdiv; |
| 3826 | outs() << " ovrfl " << fpu.fpu_fcw.ovrfl; |
| 3827 | outs() << " undfl " << fpu.fpu_fcw.undfl; |
| 3828 | outs() << " precis " << fpu.fpu_fcw.precis << "\n"; |
| 3829 | outs() << "\t\t pc "; |
| 3830 | if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B) |
| 3831 | outs() << "FP_PREC_24B "; |
| 3832 | else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B) |
| 3833 | outs() << "FP_PREC_53B "; |
| 3834 | else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B) |
| 3835 | outs() << "FP_PREC_64B "; |
| 3836 | else |
| 3837 | outs() << fpu.fpu_fcw.pc << " "; |
| 3838 | outs() << "rc "; |
| 3839 | if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR) |
| 3840 | outs() << "FP_RND_NEAR "; |
| 3841 | else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN) |
| 3842 | outs() << "FP_RND_DOWN "; |
| 3843 | else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP) |
| 3844 | outs() << "FP_RND_UP "; |
| 3845 | else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP) |
| 3846 | outs() << "FP_CHOP "; |
| 3847 | outs() << "\n"; |
| 3848 | outs() << "\t status: invalid " << fpu.fpu_fsw.invalid; |
| 3849 | outs() << " denorm " << fpu.fpu_fsw.denorm; |
| 3850 | outs() << " zdiv " << fpu.fpu_fsw.zdiv; |
| 3851 | outs() << " ovrfl " << fpu.fpu_fsw.ovrfl; |
| 3852 | outs() << " undfl " << fpu.fpu_fsw.undfl; |
| 3853 | outs() << " precis " << fpu.fpu_fsw.precis; |
| 3854 | outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n"; |
| 3855 | outs() << "\t errsumm " << fpu.fpu_fsw.errsumm; |
| 3856 | outs() << " c0 " << fpu.fpu_fsw.c0; |
| 3857 | outs() << " c1 " << fpu.fpu_fsw.c1; |
| 3858 | outs() << " c2 " << fpu.fpu_fsw.c2; |
| 3859 | outs() << " tos " << fpu.fpu_fsw.tos; |
| 3860 | outs() << " c3 " << fpu.fpu_fsw.c3; |
| 3861 | outs() << " busy " << fpu.fpu_fsw.busy << "\n"; |
| 3862 | outs() << "\t fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw); |
| 3863 | outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1); |
| 3864 | outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop); |
| 3865 | outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n"; |
| 3866 | outs() << "\t fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs); |
| 3867 | outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2); |
| 3868 | outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp); |
| 3869 | outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n"; |
| 3870 | outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3); |
| 3871 | outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr); |
| 3872 | outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask); |
| 3873 | outs() << "\n"; |
| 3874 | outs() << "\t fpu_stmm0:\n"; |
| 3875 | Print_mmst_reg(fpu.fpu_stmm0); |
| 3876 | outs() << "\t fpu_stmm1:\n"; |
| 3877 | Print_mmst_reg(fpu.fpu_stmm1); |
| 3878 | outs() << "\t fpu_stmm2:\n"; |
| 3879 | Print_mmst_reg(fpu.fpu_stmm2); |
| 3880 | outs() << "\t fpu_stmm3:\n"; |
| 3881 | Print_mmst_reg(fpu.fpu_stmm3); |
| 3882 | outs() << "\t fpu_stmm4:\n"; |
| 3883 | Print_mmst_reg(fpu.fpu_stmm4); |
| 3884 | outs() << "\t fpu_stmm5:\n"; |
| 3885 | Print_mmst_reg(fpu.fpu_stmm5); |
| 3886 | outs() << "\t fpu_stmm6:\n"; |
| 3887 | Print_mmst_reg(fpu.fpu_stmm6); |
| 3888 | outs() << "\t fpu_stmm7:\n"; |
| 3889 | Print_mmst_reg(fpu.fpu_stmm7); |
| 3890 | outs() << "\t fpu_xmm0:\n"; |
| 3891 | Print_xmm_reg(fpu.fpu_xmm0); |
| 3892 | outs() << "\t fpu_xmm1:\n"; |
| 3893 | Print_xmm_reg(fpu.fpu_xmm1); |
| 3894 | outs() << "\t fpu_xmm2:\n"; |
| 3895 | Print_xmm_reg(fpu.fpu_xmm2); |
| 3896 | outs() << "\t fpu_xmm3:\n"; |
| 3897 | Print_xmm_reg(fpu.fpu_xmm3); |
| 3898 | outs() << "\t fpu_xmm4:\n"; |
| 3899 | Print_xmm_reg(fpu.fpu_xmm4); |
| 3900 | outs() << "\t fpu_xmm5:\n"; |
| 3901 | Print_xmm_reg(fpu.fpu_xmm5); |
| 3902 | outs() << "\t fpu_xmm6:\n"; |
| 3903 | Print_xmm_reg(fpu.fpu_xmm6); |
| 3904 | outs() << "\t fpu_xmm7:\n"; |
| 3905 | Print_xmm_reg(fpu.fpu_xmm7); |
| 3906 | outs() << "\t fpu_xmm8:\n"; |
| 3907 | Print_xmm_reg(fpu.fpu_xmm8); |
| 3908 | outs() << "\t fpu_xmm9:\n"; |
| 3909 | Print_xmm_reg(fpu.fpu_xmm9); |
| 3910 | outs() << "\t fpu_xmm10:\n"; |
| 3911 | Print_xmm_reg(fpu.fpu_xmm10); |
| 3912 | outs() << "\t fpu_xmm11:\n"; |
| 3913 | Print_xmm_reg(fpu.fpu_xmm11); |
| 3914 | outs() << "\t fpu_xmm12:\n"; |
| 3915 | Print_xmm_reg(fpu.fpu_xmm12); |
| 3916 | outs() << "\t fpu_xmm13:\n"; |
| 3917 | Print_xmm_reg(fpu.fpu_xmm13); |
| 3918 | outs() << "\t fpu_xmm14:\n"; |
| 3919 | Print_xmm_reg(fpu.fpu_xmm14); |
| 3920 | outs() << "\t fpu_xmm15:\n"; |
| 3921 | Print_xmm_reg(fpu.fpu_xmm15); |
| 3922 | outs() << "\t fpu_rsrv4:\n"; |
| 3923 | for (uint32_t f = 0; f < 6; f++) { |
| 3924 | outs() << "\t "; |
| 3925 | for (uint32_t g = 0; g < 16; g++) |
| 3926 | outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f*g]) << " "; |
| 3927 | outs() << "\n"; |
| 3928 | } |
| 3929 | outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1); |
| 3930 | outs() << "\n"; |
| 3931 | } |
| 3932 | |
| 3933 | static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) { |
| 3934 | outs() << "\t trapno " << format("0x%08" PRIx32, exc64.trapno); |
| 3935 | outs() << " err " << format("0x%08" PRIx32, exc64.err); |
| 3936 | outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n"; |
| 3937 | } |
| 3938 | |
| 3939 | static void PrintThreadCommand(MachO::thread_command t, const char *Ptr, |
| 3940 | bool isLittleEndian, uint32_t cputype) { |
| 3941 | if (t.cmd == MachO::LC_THREAD) |
| 3942 | outs() << " cmd LC_THREAD\n"; |
| 3943 | else if (t.cmd == MachO::LC_UNIXTHREAD) |
| 3944 | outs() << " cmd LC_UNIXTHREAD\n"; |
| 3945 | else |
| 3946 | outs() << " cmd " << t.cmd << " (unknown)\n"; |
| 3947 | outs() << " cmdsize " << t.cmdsize; |
| 3948 | if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t)) |
| 3949 | outs() << " Incorrect size\n"; |
| 3950 | else |
| 3951 | outs() << "\n"; |
| 3952 | |
| 3953 | const char *begin = Ptr + sizeof(struct MachO::thread_command); |
| 3954 | const char *end = Ptr + t.cmdsize; |
| 3955 | uint32_t flavor, count, left; |
| 3956 | if (cputype == MachO::CPU_TYPE_X86_64) { |
| 3957 | while (begin < end) { |
| 3958 | if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { |
| 3959 | memcpy((char *)&flavor, begin, sizeof(uint32_t)); |
| 3960 | begin += sizeof(uint32_t); |
| 3961 | } else{ |
| 3962 | flavor = 0; |
| 3963 | begin = end; |
| 3964 | } |
| 3965 | if (isLittleEndian != sys::IsLittleEndianHost) |
| 3966 | sys::swapByteOrder(flavor); |
| 3967 | if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { |
| 3968 | memcpy((char *)&count, begin, sizeof(uint32_t)); |
| 3969 | begin += sizeof(uint32_t); |
| 3970 | } else{ |
| 3971 | count = 0; |
| 3972 | begin = end; |
| 3973 | } |
| 3974 | if (isLittleEndian != sys::IsLittleEndianHost) |
| 3975 | sys::swapByteOrder(count); |
| 3976 | if (flavor == MachO::x86_THREAD_STATE64) { |
| 3977 | outs() << " flavor x86_THREAD_STATE64\n"; |
| 3978 | if (count == MachO::x86_THREAD_STATE64_COUNT) |
| 3979 | outs() << " count x86_THREAD_STATE64_COUNT\n"; |
| 3980 | else |
| 3981 | outs() << " count " << count |
| 3982 | << " (not x86_THREAD_STATE64_COUNT)\n"; |
| 3983 | MachO::x86_thread_state64_t cpu64; |
| 3984 | left = end - begin; |
| 3985 | if (left >= sizeof(MachO::x86_thread_state64_t)) { |
| 3986 | memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t)); |
| 3987 | begin += sizeof(MachO::x86_thread_state64_t); |
| 3988 | } else { |
| 3989 | memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t)); |
| 3990 | memcpy(&cpu64, begin, left); |
| 3991 | begin += left; |
| 3992 | } |
| 3993 | if (isLittleEndian != sys::IsLittleEndianHost) |
| 3994 | swapStruct(cpu64); |
| 3995 | Print_x86_thread_state64_t(cpu64); |
| 3996 | } else if (flavor == MachO::x86_THREAD_STATE) { |
| 3997 | outs() << " flavor x86_THREAD_STATE\n"; |
| 3998 | if (count == MachO::x86_THREAD_STATE_COUNT) |
| 3999 | outs() << " count x86_THREAD_STATE_COUNT\n"; |
| 4000 | else |
| 4001 | outs() << " count " << count |
| 4002 | << " (not x86_THREAD_STATE_COUNT)\n"; |
| 4003 | struct MachO::x86_thread_state_t ts; |
| 4004 | left = end - begin; |
| 4005 | if (left >= sizeof(MachO::x86_thread_state_t)) { |
| 4006 | memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t)); |
| 4007 | begin += sizeof(MachO::x86_thread_state_t); |
| 4008 | } else { |
| 4009 | memset(&ts, '\0', sizeof(MachO::x86_thread_state_t)); |
| 4010 | memcpy(&ts, begin, left); |
| 4011 | begin += left; |
| 4012 | } |
| 4013 | if (isLittleEndian != sys::IsLittleEndianHost) |
| 4014 | swapStruct(ts); |
| 4015 | if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) { |
| 4016 | outs() << "\t tsh.flavor x86_THREAD_STATE64 "; |
| 4017 | if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT) |
| 4018 | outs() << "tsh.count x86_THREAD_STATE64_COUNT\n"; |
| 4019 | else |
| 4020 | outs() << "tsh.count " << ts.tsh.count |
| 4021 | << " (not x86_THREAD_STATE64_COUNT\n"; |
| 4022 | Print_x86_thread_state64_t(ts.uts.ts64); |
| 4023 | } else { |
| 4024 | outs() << "\t tsh.flavor " << ts.tsh.flavor |
| 4025 | << " tsh.count " << ts.tsh.count << "\n"; |
| 4026 | } |
| 4027 | } else if (flavor == MachO::x86_FLOAT_STATE) { |
| 4028 | outs() << " flavor x86_FLOAT_STATE\n"; |
| 4029 | if (count == MachO::x86_FLOAT_STATE_COUNT) |
| 4030 | outs() << " count x86_FLOAT_STATE_COUNT\n"; |
| 4031 | else |
| 4032 | outs() << " count " << count |
| 4033 | << " (not x86_FLOAT_STATE_COUNT)\n"; |
| 4034 | struct MachO::x86_float_state_t fs; |
| 4035 | left = end - begin; |
| 4036 | if (left >= sizeof(MachO::x86_float_state_t)) { |
| 4037 | memcpy(&fs, begin, sizeof(MachO::x86_float_state_t)); |
| 4038 | begin += sizeof(MachO::x86_float_state_t); |
| 4039 | } else { |
| 4040 | memset(&fs, '\0', sizeof(MachO::x86_float_state_t)); |
| 4041 | memcpy(&fs, begin, left); |
| 4042 | begin += left; |
| 4043 | } |
| 4044 | if (isLittleEndian != sys::IsLittleEndianHost) |
| 4045 | swapStruct(fs); |
| 4046 | if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) { |
| 4047 | outs() << "\t fsh.flavor x86_FLOAT_STATE64 "; |
| 4048 | if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT) |
| 4049 | outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n"; |
| 4050 | else |
| 4051 | outs() << "fsh.count " << fs.fsh.count |
| 4052 | << " (not x86_FLOAT_STATE64_COUNT\n"; |
| 4053 | Print_x86_float_state_t(fs.ufs.fs64); |
| 4054 | } else { |
| 4055 | outs() << "\t fsh.flavor " << fs.fsh.flavor |
| 4056 | << " fsh.count " << fs.fsh.count << "\n"; |
| 4057 | } |
| 4058 | } else if (flavor == MachO::x86_EXCEPTION_STATE) { |
| 4059 | outs() << " flavor x86_EXCEPTION_STATE\n"; |
| 4060 | if (count == MachO::x86_EXCEPTION_STATE_COUNT) |
| 4061 | outs() << " count x86_EXCEPTION_STATE_COUNT\n"; |
| 4062 | else |
| 4063 | outs() << " count " << count |
| 4064 | << " (not x86_EXCEPTION_STATE_COUNT)\n"; |
| 4065 | struct MachO::x86_exception_state_t es; |
| 4066 | left = end - begin; |
| 4067 | if (left >= sizeof(MachO::x86_exception_state_t)) { |
| 4068 | memcpy(&es, begin, sizeof(MachO::x86_exception_state_t)); |
| 4069 | begin += sizeof(MachO::x86_exception_state_t); |
| 4070 | } else { |
| 4071 | memset(&es, '\0', sizeof(MachO::x86_exception_state_t)); |
| 4072 | memcpy(&es, begin, left); |
| 4073 | begin += left; |
| 4074 | } |
| 4075 | if (isLittleEndian != sys::IsLittleEndianHost) |
| 4076 | swapStruct(es); |
| 4077 | if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) { |
| 4078 | outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n"; |
| 4079 | if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT) |
| 4080 | outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n"; |
| 4081 | else |
| 4082 | outs() << "\t esh.count " << es.esh.count |
| 4083 | << " (not x86_EXCEPTION_STATE64_COUNT\n"; |
| 4084 | Print_x86_exception_state_t(es.ues.es64); |
| 4085 | } else { |
| 4086 | outs() << "\t esh.flavor " << es.esh.flavor |
| 4087 | << " esh.count " << es.esh.count << "\n"; |
| 4088 | } |
| 4089 | } else { |
| 4090 | outs() << " flavor " << flavor << " (unknown)\n"; |
| 4091 | outs() << " count " << count << "\n"; |
| 4092 | outs() << " state (unknown)\n"; |
| 4093 | begin += count * sizeof(uint32_t); |
| 4094 | } |
| 4095 | } |
| 4096 | } else { |
| 4097 | while (begin < end) { |
| 4098 | if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { |
| 4099 | memcpy((char *)&flavor, begin, sizeof(uint32_t)); |
| 4100 | begin += sizeof(uint32_t); |
| 4101 | } else{ |
| 4102 | flavor = 0; |
| 4103 | begin = end; |
| 4104 | } |
| 4105 | if (isLittleEndian != sys::IsLittleEndianHost) |
| 4106 | sys::swapByteOrder(flavor); |
| 4107 | if (end - begin > (ptrdiff_t)sizeof(uint32_t)) { |
| 4108 | memcpy((char *)&count, begin, sizeof(uint32_t)); |
| 4109 | begin += sizeof(uint32_t); |
| 4110 | } else{ |
| 4111 | count = 0; |
| 4112 | begin = end; |
| 4113 | } |
| 4114 | if (isLittleEndian != sys::IsLittleEndianHost) |
| 4115 | sys::swapByteOrder(count); |
| 4116 | outs() << " flavor " << flavor << "\n"; |
| 4117 | outs() << " count " << count << "\n"; |
| 4118 | outs() << " state (Unknown cputype/cpusubtype)\n"; |
| 4119 | begin += count * sizeof(uint32_t); |
| 4120 | } |
| 4121 | } |
| 4122 | } |
| 4123 | |
Kevin Enderby | 8ae63c1 | 2014-09-04 16:54:47 +0000 | [diff] [blame] | 4124 | static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) { |
| 4125 | if (dl.cmd == MachO::LC_ID_DYLIB) |
| 4126 | outs() << " cmd LC_ID_DYLIB\n"; |
| 4127 | else if (dl.cmd == MachO::LC_LOAD_DYLIB) |
| 4128 | outs() << " cmd LC_LOAD_DYLIB\n"; |
| 4129 | else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB) |
| 4130 | outs() << " cmd LC_LOAD_WEAK_DYLIB\n"; |
| 4131 | else if (dl.cmd == MachO::LC_REEXPORT_DYLIB) |
| 4132 | outs() << " cmd LC_REEXPORT_DYLIB\n"; |
| 4133 | else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB) |
| 4134 | outs() << " cmd LC_LAZY_LOAD_DYLIB\n"; |
| 4135 | else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB) |
| 4136 | outs() << " cmd LC_LOAD_UPWARD_DYLIB\n"; |
| 4137 | else |
| 4138 | outs() << " cmd " << dl.cmd << " (unknown)\n"; |
| 4139 | outs() << " cmdsize " << dl.cmdsize; |
| 4140 | if (dl.cmdsize < sizeof(struct MachO::dylib_command)) |
| 4141 | outs() << " Incorrect size\n"; |
| 4142 | else |
| 4143 | outs() << "\n"; |
| 4144 | if (dl.dylib.name < dl.cmdsize) { |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 4145 | const char *P = (const char *)(Ptr) + dl.dylib.name; |
Kevin Enderby | 8ae63c1 | 2014-09-04 16:54:47 +0000 | [diff] [blame] | 4146 | outs() << " name " << P << " (offset " << dl.dylib.name << ")\n"; |
| 4147 | } else { |
| 4148 | outs() << " name ?(bad offset " << dl.dylib.name << ")\n"; |
| 4149 | } |
| 4150 | outs() << " time stamp " << dl.dylib.timestamp << " "; |
| 4151 | time_t t = dl.dylib.timestamp; |
| 4152 | outs() << ctime(&t); |
| 4153 | outs() << " current version "; |
| 4154 | if (dl.dylib.current_version == 0xffffffff) |
| 4155 | outs() << "n/a\n"; |
| 4156 | else |
| 4157 | outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "." |
| 4158 | << ((dl.dylib.current_version >> 8) & 0xff) << "." |
| 4159 | << (dl.dylib.current_version & 0xff) << "\n"; |
| 4160 | outs() << "compatibility version "; |
| 4161 | if (dl.dylib.compatibility_version == 0xffffffff) |
| 4162 | outs() << "n/a\n"; |
| 4163 | else |
| 4164 | outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "." |
| 4165 | << ((dl.dylib.compatibility_version >> 8) & 0xff) << "." |
| 4166 | << (dl.dylib.compatibility_version & 0xff) << "\n"; |
| 4167 | } |
| 4168 | |
| 4169 | static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld, |
| 4170 | uint32_t object_size) { |
| 4171 | if (ld.cmd == MachO::LC_CODE_SIGNATURE) |
| 4172 | outs() << " cmd LC_FUNCTION_STARTS\n"; |
| 4173 | else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO) |
| 4174 | outs() << " cmd LC_SEGMENT_SPLIT_INFO\n"; |
| 4175 | else if (ld.cmd == MachO::LC_FUNCTION_STARTS) |
| 4176 | outs() << " cmd LC_FUNCTION_STARTS\n"; |
| 4177 | else if (ld.cmd == MachO::LC_DATA_IN_CODE) |
| 4178 | outs() << " cmd LC_DATA_IN_CODE\n"; |
| 4179 | else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS) |
| 4180 | outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n"; |
| 4181 | else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) |
| 4182 | outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n"; |
| 4183 | else |
| 4184 | outs() << " cmd " << ld.cmd << " (?)\n"; |
| 4185 | outs() << " cmdsize " << ld.cmdsize; |
| 4186 | if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command)) |
| 4187 | outs() << " Incorrect size\n"; |
| 4188 | else |
| 4189 | outs() << "\n"; |
| 4190 | outs() << " dataoff " << ld.dataoff; |
| 4191 | if (ld.dataoff > object_size) |
| 4192 | outs() << " (past end of file)\n"; |
| 4193 | else |
| 4194 | outs() << "\n"; |
| 4195 | outs() << " datasize " << ld.datasize; |
| 4196 | uint64_t big_size = ld.dataoff; |
| 4197 | big_size += ld.datasize; |
| 4198 | if (big_size > object_size) |
| 4199 | outs() << " (past end of file)\n"; |
| 4200 | else |
| 4201 | outs() << "\n"; |
| 4202 | } |
| 4203 | |
Kevin Enderby | 956366c | 2014-08-29 22:30:52 +0000 | [diff] [blame] | 4204 | static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t ncmds, |
| 4205 | uint32_t filetype, uint32_t cputype, |
| 4206 | bool verbose) { |
| 4207 | StringRef Buf = Obj->getData(); |
| 4208 | MachOObjectFile::LoadCommandInfo Command = Obj->getFirstLoadCommandInfo(); |
| 4209 | for (unsigned i = 0;; ++i) { |
| 4210 | outs() << "Load command " << i << "\n"; |
| 4211 | if (Command.C.cmd == MachO::LC_SEGMENT) { |
| 4212 | MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command); |
| 4213 | const char *sg_segname = SLC.segname; |
| 4214 | PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr, |
| 4215 | SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot, |
| 4216 | SLC.initprot, SLC.nsects, SLC.flags, Buf.size(), |
| 4217 | verbose); |
| 4218 | for (unsigned j = 0; j < SLC.nsects; j++) { |
Kevin Enderby | c971338 | 2014-12-16 01:14:45 +0000 | [diff] [blame] | 4219 | MachO::section S = Obj->getSection(Command, j); |
Kevin Enderby | 956366c | 2014-08-29 22:30:52 +0000 | [diff] [blame] | 4220 | PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align, |
| 4221 | S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2, |
| 4222 | SLC.cmd, sg_segname, filetype, Buf.size(), verbose); |
| 4223 | } |
| 4224 | } else if (Command.C.cmd == MachO::LC_SEGMENT_64) { |
| 4225 | MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command); |
| 4226 | const char *sg_segname = SLC_64.segname; |
| 4227 | PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname, |
| 4228 | SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff, |
| 4229 | SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot, |
| 4230 | SLC_64.nsects, SLC_64.flags, Buf.size(), verbose); |
| 4231 | for (unsigned j = 0; j < SLC_64.nsects; j++) { |
| 4232 | MachO::section_64 S_64 = Obj->getSection64(Command, j); |
| 4233 | PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size, |
| 4234 | S_64.offset, S_64.align, S_64.reloff, S_64.nreloc, |
| 4235 | S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd, |
| 4236 | sg_segname, filetype, Buf.size(), verbose); |
| 4237 | } |
| 4238 | } else if (Command.C.cmd == MachO::LC_SYMTAB) { |
| 4239 | MachO::symtab_command Symtab = Obj->getSymtabLoadCommand(); |
David Majnemer | 73cc6ff | 2014-11-13 19:48:56 +0000 | [diff] [blame] | 4240 | PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size()); |
Kevin Enderby | 956366c | 2014-08-29 22:30:52 +0000 | [diff] [blame] | 4241 | } else if (Command.C.cmd == MachO::LC_DYSYMTAB) { |
| 4242 | MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand(); |
| 4243 | MachO::symtab_command Symtab = Obj->getSymtabLoadCommand(); |
David Majnemer | 73cc6ff | 2014-11-13 19:48:56 +0000 | [diff] [blame] | 4244 | PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(), |
| 4245 | Obj->is64Bit()); |
Kevin Enderby | 8ae63c1 | 2014-09-04 16:54:47 +0000 | [diff] [blame] | 4246 | } else if (Command.C.cmd == MachO::LC_DYLD_INFO || |
| 4247 | Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) { |
| 4248 | MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command); |
| 4249 | PrintDyldInfoLoadCommand(DyldInfo, Buf.size()); |
| 4250 | } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER || |
| 4251 | Command.C.cmd == MachO::LC_ID_DYLINKER || |
| 4252 | Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) { |
| 4253 | MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command); |
| 4254 | PrintDyldLoadCommand(Dyld, Command.Ptr); |
| 4255 | } else if (Command.C.cmd == MachO::LC_UUID) { |
| 4256 | MachO::uuid_command Uuid = Obj->getUuidCommand(Command); |
| 4257 | PrintUuidLoadCommand(Uuid); |
Jean-Daniel Dupas | 00cc1f5 | 2014-12-04 07:37:02 +0000 | [diff] [blame] | 4258 | } else if (Command.C.cmd == MachO::LC_RPATH) { |
| 4259 | MachO::rpath_command Rpath = Obj->getRpathCommand(Command); |
| 4260 | PrintRpathLoadCommand(Rpath, Command.Ptr); |
Kevin Enderby | 1ff0ecc | 2014-12-16 21:48:27 +0000 | [diff] [blame] | 4261 | } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX || |
| 4262 | Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS) { |
Kevin Enderby | 8ae63c1 | 2014-09-04 16:54:47 +0000 | [diff] [blame] | 4263 | MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command); |
| 4264 | PrintVersionMinLoadCommand(Vd); |
| 4265 | } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) { |
| 4266 | MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command); |
| 4267 | PrintSourceVersionCommand(Sd); |
| 4268 | } else if (Command.C.cmd == MachO::LC_MAIN) { |
| 4269 | MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command); |
| 4270 | PrintEntryPointCommand(Ep); |
Kevin Enderby | 0804f467 | 2014-12-16 23:25:52 +0000 | [diff] [blame] | 4271 | } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) { |
| 4272 | MachO::encryption_info_command Ei = Obj->getEncryptionInfoCommand(Command); |
| 4273 | PrintEncryptionInfoCommand(Ei, Buf.size()); |
Kevin Enderby | 5753829 | 2014-12-17 01:01:30 +0000 | [diff] [blame] | 4274 | } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) { |
| 4275 | MachO::encryption_info_command_64 Ei = Obj->getEncryptionInfoCommand64(Command); |
| 4276 | PrintEncryptionInfoCommand64(Ei, Buf.size()); |
Kevin Enderby | d0b6b7f | 2014-12-18 00:53:40 +0000 | [diff] [blame] | 4277 | } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) { |
| 4278 | MachO::linker_option_command Lo = Obj->getLinkerOptionLoadCommand(Command); |
| 4279 | PrintLinkerOptionCommand(Lo, Command.Ptr); |
Kevin Enderby | b4b7931 | 2014-12-18 19:24:35 +0000 | [diff] [blame] | 4280 | } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) { |
| 4281 | MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command); |
| 4282 | PrintSubFrameworkCommand(Sf, Command.Ptr); |
Kevin Enderby | a2bd8d9 | 2014-12-18 23:13:26 +0000 | [diff] [blame] | 4283 | } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) { |
| 4284 | MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command); |
| 4285 | PrintSubUmbrellaCommand(Sf, Command.Ptr); |
Kevin Enderby | 36c8d3a | 2014-12-19 19:48:16 +0000 | [diff] [blame] | 4286 | } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) { |
| 4287 | MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command); |
| 4288 | PrintSubLibraryCommand(Sl, Command.Ptr); |
Kevin Enderby | 186eac3 | 2014-12-19 21:06:24 +0000 | [diff] [blame] | 4289 | } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) { |
| 4290 | MachO::sub_client_command Sc = Obj->getSubClientCommand(Command); |
| 4291 | PrintSubClientCommand(Sc, Command.Ptr); |
Kevin Enderby | 52e4ce4 | 2014-12-19 22:25:22 +0000 | [diff] [blame] | 4292 | } else if (Command.C.cmd == MachO::LC_ROUTINES) { |
| 4293 | MachO::routines_command Rc = Obj->getRoutinesCommand(Command); |
| 4294 | PrintRoutinesCommand(Rc); |
| 4295 | } else if (Command.C.cmd == MachO::LC_ROUTINES_64) { |
| 4296 | MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command); |
| 4297 | PrintRoutinesCommand64(Rc); |
Kevin Enderby | 48ef534 | 2014-12-23 22:56:39 +0000 | [diff] [blame] | 4298 | } else if (Command.C.cmd == MachO::LC_THREAD || |
| 4299 | Command.C.cmd == MachO::LC_UNIXTHREAD) { |
| 4300 | MachO::thread_command Tc = Obj->getThreadCommand(Command); |
| 4301 | PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype); |
Nick Kledzik | 1555891 | 2014-10-16 18:58:20 +0000 | [diff] [blame] | 4302 | } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB || |
| 4303 | Command.C.cmd == MachO::LC_ID_DYLIB || |
| 4304 | Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB || |
| 4305 | Command.C.cmd == MachO::LC_REEXPORT_DYLIB || |
| 4306 | Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB || |
| 4307 | Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) { |
Kevin Enderby | 8ae63c1 | 2014-09-04 16:54:47 +0000 | [diff] [blame] | 4308 | MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command); |
| 4309 | PrintDylibCommand(Dl, Command.Ptr); |
| 4310 | } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE || |
| 4311 | Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO || |
| 4312 | Command.C.cmd == MachO::LC_FUNCTION_STARTS || |
| 4313 | Command.C.cmd == MachO::LC_DATA_IN_CODE || |
| 4314 | Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS || |
| 4315 | Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) { |
| 4316 | MachO::linkedit_data_command Ld = |
| 4317 | Obj->getLinkeditDataLoadCommand(Command); |
| 4318 | PrintLinkEditDataCommand(Ld, Buf.size()); |
Kevin Enderby | 956366c | 2014-08-29 22:30:52 +0000 | [diff] [blame] | 4319 | } else { |
| 4320 | outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd) |
| 4321 | << ")\n"; |
| 4322 | outs() << " cmdsize " << Command.C.cmdsize << "\n"; |
| 4323 | // TODO: get and print the raw bytes of the load command. |
| 4324 | } |
| 4325 | // TODO: print all the other kinds of load commands. |
| 4326 | if (i == ncmds - 1) |
| 4327 | break; |
| 4328 | else |
| 4329 | Command = Obj->getNextLoadCommandInfo(Command); |
| 4330 | } |
| 4331 | } |
| 4332 | |
| 4333 | static void getAndPrintMachHeader(const MachOObjectFile *Obj, uint32_t &ncmds, |
| 4334 | uint32_t &filetype, uint32_t &cputype, |
| 4335 | bool verbose) { |
Kevin Enderby | b76d386 | 2014-08-22 20:35:18 +0000 | [diff] [blame] | 4336 | if (Obj->is64Bit()) { |
| 4337 | MachO::mach_header_64 H_64; |
| 4338 | H_64 = Obj->getHeader64(); |
| 4339 | PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype, |
| 4340 | H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose); |
Kevin Enderby | 956366c | 2014-08-29 22:30:52 +0000 | [diff] [blame] | 4341 | ncmds = H_64.ncmds; |
| 4342 | filetype = H_64.filetype; |
| 4343 | cputype = H_64.cputype; |
Kevin Enderby | b76d386 | 2014-08-22 20:35:18 +0000 | [diff] [blame] | 4344 | } else { |
| 4345 | MachO::mach_header H; |
| 4346 | H = Obj->getHeader(); |
| 4347 | PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds, |
| 4348 | H.sizeofcmds, H.flags, verbose); |
Kevin Enderby | 956366c | 2014-08-29 22:30:52 +0000 | [diff] [blame] | 4349 | ncmds = H.ncmds; |
| 4350 | filetype = H.filetype; |
| 4351 | cputype = H.cputype; |
Kevin Enderby | b76d386 | 2014-08-22 20:35:18 +0000 | [diff] [blame] | 4352 | } |
| 4353 | } |
| 4354 | |
| 4355 | void llvm::printMachOFileHeader(const object::ObjectFile *Obj) { |
| 4356 | const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj); |
Kevin Enderby | 956366c | 2014-08-29 22:30:52 +0000 | [diff] [blame] | 4357 | uint32_t ncmds = 0; |
| 4358 | uint32_t filetype = 0; |
| 4359 | uint32_t cputype = 0; |
| 4360 | getAndPrintMachHeader(file, ncmds, filetype, cputype, true); |
| 4361 | PrintLoadCommands(file, ncmds, filetype, cputype, true); |
Kevin Enderby | b76d386 | 2014-08-22 20:35:18 +0000 | [diff] [blame] | 4362 | } |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 4363 | |
| 4364 | //===----------------------------------------------------------------------===// |
| 4365 | // export trie dumping |
| 4366 | //===----------------------------------------------------------------------===// |
| 4367 | |
| 4368 | void llvm::printMachOExportsTrie(const object::MachOObjectFile *Obj) { |
Nick Kledzik | ac7cbdc | 2014-09-02 18:50:24 +0000 | [diff] [blame] | 4369 | for (const llvm::object::ExportEntry &Entry : Obj->exports()) { |
| 4370 | uint64_t Flags = Entry.flags(); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 4371 | bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT); |
| 4372 | bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION); |
| 4373 | bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) == |
| 4374 | MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL); |
| 4375 | bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) == |
| 4376 | MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE); |
| 4377 | bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER); |
| 4378 | if (ReExport) |
| 4379 | outs() << "[re-export] "; |
| 4380 | else |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 4381 | outs() << format("0x%08llX ", |
| 4382 | Entry.address()); // FIXME:add in base address |
Nick Kledzik | ac7cbdc | 2014-09-02 18:50:24 +0000 | [diff] [blame] | 4383 | outs() << Entry.name(); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 4384 | if (WeakDef || ThreadLocal || Resolver || Abs) { |
Nick Kledzik | ac7cbdc | 2014-09-02 18:50:24 +0000 | [diff] [blame] | 4385 | bool NeedsComma = false; |
Nick Kledzik | 1d1ac4b | 2014-09-03 01:12:52 +0000 | [diff] [blame] | 4386 | outs() << " ["; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 4387 | if (WeakDef) { |
| 4388 | outs() << "weak_def"; |
Nick Kledzik | ac7cbdc | 2014-09-02 18:50:24 +0000 | [diff] [blame] | 4389 | NeedsComma = true; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 4390 | } |
| 4391 | if (ThreadLocal) { |
Nick Kledzik | ac7cbdc | 2014-09-02 18:50:24 +0000 | [diff] [blame] | 4392 | if (NeedsComma) |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 4393 | outs() << ", "; |
| 4394 | outs() << "per-thread"; |
Nick Kledzik | ac7cbdc | 2014-09-02 18:50:24 +0000 | [diff] [blame] | 4395 | NeedsComma = true; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 4396 | } |
| 4397 | if (Abs) { |
Nick Kledzik | ac7cbdc | 2014-09-02 18:50:24 +0000 | [diff] [blame] | 4398 | if (NeedsComma) |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 4399 | outs() << ", "; |
| 4400 | outs() << "absolute"; |
Nick Kledzik | ac7cbdc | 2014-09-02 18:50:24 +0000 | [diff] [blame] | 4401 | NeedsComma = true; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 4402 | } |
| 4403 | if (Resolver) { |
Nick Kledzik | ac7cbdc | 2014-09-02 18:50:24 +0000 | [diff] [blame] | 4404 | if (NeedsComma) |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 4405 | outs() << ", "; |
Nick Kledzik | ac7cbdc | 2014-09-02 18:50:24 +0000 | [diff] [blame] | 4406 | outs() << format("resolver=0x%08llX", Entry.other()); |
| 4407 | NeedsComma = true; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 4408 | } |
| 4409 | outs() << "]"; |
| 4410 | } |
| 4411 | if (ReExport) { |
| 4412 | StringRef DylibName = "unknown"; |
Nick Kledzik | ac7cbdc | 2014-09-02 18:50:24 +0000 | [diff] [blame] | 4413 | int Ordinal = Entry.other() - 1; |
| 4414 | Obj->getLibraryShortNameByIndex(Ordinal, DylibName); |
| 4415 | if (Entry.otherName().empty()) |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 4416 | outs() << " (from " << DylibName << ")"; |
| 4417 | else |
Nick Kledzik | ac7cbdc | 2014-09-02 18:50:24 +0000 | [diff] [blame] | 4418 | outs() << " (" << Entry.otherName() << " from " << DylibName << ")"; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 4419 | } |
| 4420 | outs() << "\n"; |
| 4421 | } |
| 4422 | } |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 4423 | |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 4424 | //===----------------------------------------------------------------------===// |
| 4425 | // rebase table dumping |
| 4426 | //===----------------------------------------------------------------------===// |
| 4427 | |
| 4428 | namespace { |
| 4429 | class SegInfo { |
| 4430 | public: |
| 4431 | SegInfo(const object::MachOObjectFile *Obj); |
| 4432 | |
| 4433 | StringRef segmentName(uint32_t SegIndex); |
| 4434 | StringRef sectionName(uint32_t SegIndex, uint64_t SegOffset); |
| 4435 | uint64_t address(uint32_t SegIndex, uint64_t SegOffset); |
| 4436 | |
| 4437 | private: |
| 4438 | struct SectionInfo { |
| 4439 | uint64_t Address; |
| 4440 | uint64_t Size; |
| 4441 | StringRef SectionName; |
| 4442 | StringRef SegmentName; |
| 4443 | uint64_t OffsetInSegment; |
| 4444 | uint64_t SegmentStartAddress; |
| 4445 | uint32_t SegmentIndex; |
| 4446 | }; |
| 4447 | const SectionInfo &findSection(uint32_t SegIndex, uint64_t SegOffset); |
| 4448 | SmallVector<SectionInfo, 32> Sections; |
| 4449 | }; |
| 4450 | } |
| 4451 | |
| 4452 | SegInfo::SegInfo(const object::MachOObjectFile *Obj) { |
| 4453 | // Build table of sections so segIndex/offset pairs can be translated. |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 4454 | uint32_t CurSegIndex = Obj->hasPageZeroSegment() ? 1 : 0; |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 4455 | StringRef CurSegName; |
| 4456 | uint64_t CurSegAddress; |
| 4457 | for (const SectionRef &Section : Obj->sections()) { |
| 4458 | SectionInfo Info; |
| 4459 | if (error(Section.getName(Info.SectionName))) |
| 4460 | return; |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 4461 | Info.Address = Section.getAddress(); |
| 4462 | Info.Size = Section.getSize(); |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 4463 | Info.SegmentName = |
| 4464 | Obj->getSectionFinalSegmentName(Section.getRawDataRefImpl()); |
| 4465 | if (!Info.SegmentName.equals(CurSegName)) { |
| 4466 | ++CurSegIndex; |
| 4467 | CurSegName = Info.SegmentName; |
| 4468 | CurSegAddress = Info.Address; |
| 4469 | } |
| 4470 | Info.SegmentIndex = CurSegIndex - 1; |
| 4471 | Info.OffsetInSegment = Info.Address - CurSegAddress; |
| 4472 | Info.SegmentStartAddress = CurSegAddress; |
| 4473 | Sections.push_back(Info); |
| 4474 | } |
| 4475 | } |
| 4476 | |
| 4477 | StringRef SegInfo::segmentName(uint32_t SegIndex) { |
| 4478 | for (const SectionInfo &SI : Sections) { |
| 4479 | if (SI.SegmentIndex == SegIndex) |
| 4480 | return SI.SegmentName; |
| 4481 | } |
| 4482 | llvm_unreachable("invalid segIndex"); |
| 4483 | } |
| 4484 | |
| 4485 | const SegInfo::SectionInfo &SegInfo::findSection(uint32_t SegIndex, |
| 4486 | uint64_t OffsetInSeg) { |
| 4487 | for (const SectionInfo &SI : Sections) { |
| 4488 | if (SI.SegmentIndex != SegIndex) |
| 4489 | continue; |
| 4490 | if (SI.OffsetInSegment > OffsetInSeg) |
| 4491 | continue; |
| 4492 | if (OffsetInSeg >= (SI.OffsetInSegment + SI.Size)) |
| 4493 | continue; |
| 4494 | return SI; |
| 4495 | } |
| 4496 | llvm_unreachable("segIndex and offset not in any section"); |
| 4497 | } |
| 4498 | |
| 4499 | StringRef SegInfo::sectionName(uint32_t SegIndex, uint64_t OffsetInSeg) { |
| 4500 | return findSection(SegIndex, OffsetInSeg).SectionName; |
| 4501 | } |
| 4502 | |
| 4503 | uint64_t SegInfo::address(uint32_t SegIndex, uint64_t OffsetInSeg) { |
| 4504 | const SectionInfo &SI = findSection(SegIndex, OffsetInSeg); |
| 4505 | return SI.SegmentStartAddress + OffsetInSeg; |
| 4506 | } |
| 4507 | |
| 4508 | void llvm::printMachORebaseTable(const object::MachOObjectFile *Obj) { |
| 4509 | // Build table of sections so names can used in final output. |
| 4510 | SegInfo sectionTable(Obj); |
| 4511 | |
| 4512 | outs() << "segment section address type\n"; |
| 4513 | for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable()) { |
| 4514 | uint32_t SegIndex = Entry.segmentIndex(); |
| 4515 | uint64_t OffsetInSeg = Entry.segmentOffset(); |
| 4516 | StringRef SegmentName = sectionTable.segmentName(SegIndex); |
| 4517 | StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg); |
| 4518 | uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg); |
| 4519 | |
| 4520 | // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 4521 | outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n", |
| 4522 | SegmentName.str().c_str(), SectionName.str().c_str(), |
| 4523 | Address, Entry.typeName().str().c_str()); |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 4524 | } |
| 4525 | } |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 4526 | |
| 4527 | static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) { |
| 4528 | StringRef DylibName; |
| 4529 | switch (Ordinal) { |
| 4530 | case MachO::BIND_SPECIAL_DYLIB_SELF: |
| 4531 | return "this-image"; |
| 4532 | case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE: |
| 4533 | return "main-executable"; |
| 4534 | case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP: |
| 4535 | return "flat-namespace"; |
| 4536 | default: |
Nick Kledzik | abd2987 | 2014-09-16 22:03:13 +0000 | [diff] [blame] | 4537 | if (Ordinal > 0) { |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 4538 | std::error_code EC = |
| 4539 | Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName); |
Nick Kledzik | abd2987 | 2014-09-16 22:03:13 +0000 | [diff] [blame] | 4540 | if (EC) |
Nick Kledzik | 51d2c2b | 2014-10-14 23:29:38 +0000 | [diff] [blame] | 4541 | return "<<bad library ordinal>>"; |
Nick Kledzik | abd2987 | 2014-09-16 22:03:13 +0000 | [diff] [blame] | 4542 | return DylibName; |
| 4543 | } |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 4544 | } |
Nick Kledzik | abd2987 | 2014-09-16 22:03:13 +0000 | [diff] [blame] | 4545 | return "<<unknown special ordinal>>"; |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 4546 | } |
| 4547 | |
| 4548 | //===----------------------------------------------------------------------===// |
| 4549 | // bind table dumping |
| 4550 | //===----------------------------------------------------------------------===// |
| 4551 | |
| 4552 | void llvm::printMachOBindTable(const object::MachOObjectFile *Obj) { |
| 4553 | // Build table of sections so names can used in final output. |
| 4554 | SegInfo sectionTable(Obj); |
| 4555 | |
Nick Kledzik | 5ffacc1 | 2014-09-30 00:19:58 +0000 | [diff] [blame] | 4556 | outs() << "segment section address type " |
| 4557 | "addend dylib symbol\n"; |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 4558 | for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable()) { |
| 4559 | uint32_t SegIndex = Entry.segmentIndex(); |
| 4560 | uint64_t OffsetInSeg = Entry.segmentOffset(); |
| 4561 | StringRef SegmentName = sectionTable.segmentName(SegIndex); |
| 4562 | StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg); |
| 4563 | uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg); |
| 4564 | |
| 4565 | // Table lines look like: |
| 4566 | // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard |
Nick Kledzik | 5ffacc1 | 2014-09-30 00:19:58 +0000 | [diff] [blame] | 4567 | StringRef Attr; |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 4568 | if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT) |
Nick Kledzik | 5ffacc1 | 2014-09-30 00:19:58 +0000 | [diff] [blame] | 4569 | Attr = " (weak_import)"; |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 4570 | outs() << left_justify(SegmentName, 8) << " " |
Nick Kledzik | 5ffacc1 | 2014-09-30 00:19:58 +0000 | [diff] [blame] | 4571 | << left_justify(SectionName, 18) << " " |
| 4572 | << format_hex(Address, 10, true) << " " |
| 4573 | << left_justify(Entry.typeName(), 8) << " " |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 4574 | << format_decimal(Entry.addend(), 8) << " " |
Nick Kledzik | 5ffacc1 | 2014-09-30 00:19:58 +0000 | [diff] [blame] | 4575 | << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " " |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 4576 | << Entry.symbolName() << Attr << "\n"; |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 4577 | } |
| 4578 | } |
| 4579 | |
| 4580 | //===----------------------------------------------------------------------===// |
| 4581 | // lazy bind table dumping |
| 4582 | //===----------------------------------------------------------------------===// |
| 4583 | |
| 4584 | void llvm::printMachOLazyBindTable(const object::MachOObjectFile *Obj) { |
| 4585 | // Build table of sections so names can used in final output. |
| 4586 | SegInfo sectionTable(Obj); |
| 4587 | |
Nick Kledzik | 5ffacc1 | 2014-09-30 00:19:58 +0000 | [diff] [blame] | 4588 | outs() << "segment section address " |
| 4589 | "dylib symbol\n"; |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 4590 | for (const llvm::object::MachOBindEntry &Entry : Obj->lazyBindTable()) { |
| 4591 | uint32_t SegIndex = Entry.segmentIndex(); |
| 4592 | uint64_t OffsetInSeg = Entry.segmentOffset(); |
| 4593 | StringRef SegmentName = sectionTable.segmentName(SegIndex); |
| 4594 | StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg); |
| 4595 | uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg); |
| 4596 | |
| 4597 | // Table lines look like: |
| 4598 | // __DATA __got 0x00012010 libSystem ___stack_chk_guard |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 4599 | outs() << left_justify(SegmentName, 8) << " " |
Nick Kledzik | 5ffacc1 | 2014-09-30 00:19:58 +0000 | [diff] [blame] | 4600 | << left_justify(SectionName, 18) << " " |
| 4601 | << format_hex(Address, 10, true) << " " |
| 4602 | << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " " |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 4603 | << Entry.symbolName() << "\n"; |
| 4604 | } |
| 4605 | } |
| 4606 | |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 4607 | //===----------------------------------------------------------------------===// |
| 4608 | // weak bind table dumping |
| 4609 | //===----------------------------------------------------------------------===// |
| 4610 | |
| 4611 | void llvm::printMachOWeakBindTable(const object::MachOObjectFile *Obj) { |
| 4612 | // Build table of sections so names can used in final output. |
| 4613 | SegInfo sectionTable(Obj); |
| 4614 | |
Nick Kledzik | 5ffacc1 | 2014-09-30 00:19:58 +0000 | [diff] [blame] | 4615 | outs() << "segment section address " |
| 4616 | "type addend symbol\n"; |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 4617 | for (const llvm::object::MachOBindEntry &Entry : Obj->weakBindTable()) { |
| 4618 | // Strong symbols don't have a location to update. |
| 4619 | if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) { |
Nick Kledzik | 5ffacc1 | 2014-09-30 00:19:58 +0000 | [diff] [blame] | 4620 | outs() << " strong " |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 4621 | << Entry.symbolName() << "\n"; |
| 4622 | continue; |
| 4623 | } |
| 4624 | uint32_t SegIndex = Entry.segmentIndex(); |
| 4625 | uint64_t OffsetInSeg = Entry.segmentOffset(); |
| 4626 | StringRef SegmentName = sectionTable.segmentName(SegIndex); |
| 4627 | StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg); |
| 4628 | uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg); |
| 4629 | |
| 4630 | // Table lines look like: |
| 4631 | // __DATA __data 0x00001000 pointer 0 _foo |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 4632 | outs() << left_justify(SegmentName, 8) << " " |
Nick Kledzik | 5ffacc1 | 2014-09-30 00:19:58 +0000 | [diff] [blame] | 4633 | << left_justify(SectionName, 18) << " " |
| 4634 | << format_hex(Address, 10, true) << " " |
| 4635 | << left_justify(Entry.typeName(), 8) << " " |
Kevin Enderby | b28ed01 | 2014-10-29 21:28:24 +0000 | [diff] [blame] | 4636 | << format_decimal(Entry.addend(), 8) << " " << Entry.symbolName() |
| 4637 | << "\n"; |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 4638 | } |
| 4639 | } |
| 4640 | |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 4641 | // get_dyld_bind_info_symbolname() is used for disassembly and passed an |
| 4642 | // address, ReferenceValue, in the Mach-O file and looks in the dyld bind |
| 4643 | // information for that address. If the address is found its binding symbol |
| 4644 | // name is returned. If not nullptr is returned. |
| 4645 | static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue, |
| 4646 | struct DisassembleInfo *info) { |
Kevin Enderby | 078be60 | 2014-10-23 19:53:12 +0000 | [diff] [blame] | 4647 | if (info->bindtable == nullptr) { |
| 4648 | info->bindtable = new (BindTable); |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 4649 | SegInfo sectionTable(info->O); |
| 4650 | for (const llvm::object::MachOBindEntry &Entry : info->O->bindTable()) { |
| 4651 | uint32_t SegIndex = Entry.segmentIndex(); |
| 4652 | uint64_t OffsetInSeg = Entry.segmentOffset(); |
| 4653 | uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg); |
| 4654 | const char *SymbolName = nullptr; |
| 4655 | StringRef name = Entry.symbolName(); |
| 4656 | if (!name.empty()) |
| 4657 | SymbolName = name.data(); |
Kevin Enderby | 078be60 | 2014-10-23 19:53:12 +0000 | [diff] [blame] | 4658 | info->bindtable->push_back(std::make_pair(Address, SymbolName)); |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 4659 | } |
| 4660 | } |
Kevin Enderby | 078be60 | 2014-10-23 19:53:12 +0000 | [diff] [blame] | 4661 | for (bind_table_iterator BI = info->bindtable->begin(), |
| 4662 | BE = info->bindtable->end(); |
Kevin Enderby | 6f326ce | 2014-10-23 19:37:31 +0000 | [diff] [blame] | 4663 | BI != BE; ++BI) { |
| 4664 | uint64_t Address = BI->first; |
| 4665 | if (ReferenceValue == Address) { |
| 4666 | const char *SymbolName = BI->second; |
| 4667 | return SymbolName; |
| 4668 | } |
| 4669 | } |
| 4670 | return nullptr; |
| 4671 | } |