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