blob: 6d3f38333eb857e31a6e4b4a692a999ce1feb8bf [file] [log] [blame]
Benjamin Kramer43a772e2011-09-19 17:56:04 +00001//===-- 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 Enderby98c9acc2014-09-16 18:00:57 +000015#include "llvm-c/Disassembler.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000016#include "llvm/ADT/STLExtras.h"
Ahmed Bougachaaa790682013-05-24 01:07:04 +000017#include "llvm/ADT/StringExtras.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000018#include "llvm/ADT/Triple.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000019#include "llvm/BinaryFormat/MachO.h"
Kevin Enderby04bf6932014-10-28 23:39:46 +000020#include "llvm/Config/config.h"
Zachary Turner6489d7b2015-04-23 17:37:47 +000021#include "llvm/DebugInfo/DIContext.h"
22#include "llvm/DebugInfo/DWARF/DWARFContext.h"
Rafael Espindolab940b662016-09-06 19:16:48 +000023#include "llvm/Demangle/Demangle.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000024#include "llvm/MC/MCAsmInfo.h"
Lang Hamesa1bc0f52014-04-15 04:40:56 +000025#include "llvm/MC/MCContext.h"
Benjamin Kramerf57c1972016-01-26 16:44:37 +000026#include "llvm/MC/MCDisassembler/MCDisassembler.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000027#include "llvm/MC/MCInst.h"
28#include "llvm/MC/MCInstPrinter.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000029#include "llvm/MC/MCInstrDesc.h"
30#include "llvm/MC/MCInstrInfo.h"
Jim Grosbachfd93a592012-03-05 19:33:20 +000031#include "llvm/MC/MCRegisterInfo.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000032#include "llvm/MC/MCSubtargetInfo.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000033#include "llvm/Object/MachO.h"
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +000034#include "llvm/Object/MachOUniversal.h"
Rafael Espindola9b709252013-04-13 01:45:40 +000035#include "llvm/Support/Casting.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000036#include "llvm/Support/CommandLine.h"
37#include "llvm/Support/Debug.h"
Tim Northover4bd286a2014-08-01 13:07:19 +000038#include "llvm/Support/Endian.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000039#include "llvm/Support/Format.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000040#include "llvm/Support/FormattedStream.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000041#include "llvm/Support/GraphWriter.h"
Kevin Enderby9a509442015-01-27 21:28:24 +000042#include "llvm/Support/LEB128.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000043#include "llvm/Support/MemoryBuffer.h"
44#include "llvm/Support/TargetRegistry.h"
45#include "llvm/Support/TargetSelect.h"
Kevin Enderby9873e2c2016-05-23 21:34:12 +000046#include "llvm/Support/ToolOutputFile.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000047#include "llvm/Support/raw_ostream.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000048#include <algorithm>
49#include <cstring>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000050#include <system_error>
Kevin Enderby04bf6932014-10-28 23:39:46 +000051
Kevin Enderby9873e2c2016-05-23 21:34:12 +000052#ifdef HAVE_LIBXAR
53extern "C" {
54#include <xar/xar.h>
55}
56#endif
57
Benjamin Kramer43a772e2011-09-19 17:56:04 +000058using namespace llvm;
59using namespace object;
60
61static cl::opt<bool>
Kevin Enderbyb28ed012014-10-29 21:28:24 +000062 UseDbg("g",
63 cl::desc("Print line information from debug info if available"));
Benjamin Kramer699128e2011-09-21 01:13:19 +000064
Kevin Enderbyb28ed012014-10-29 21:28:24 +000065static cl::opt<std::string> DSYMFile("dsym",
66 cl::desc("Use .dSYM file for debug info"));
Benjamin Kramer699128e2011-09-21 01:13:19 +000067
Kevin Enderbyb28ed012014-10-29 21:28:24 +000068static cl::opt<bool> FullLeadingAddr("full-leading-addr",
69 cl::desc("Print full leading address"));
Kevin Enderbybf246f52014-09-24 23:08:22 +000070
Kevin Enderbyf9d60f02016-11-29 21:43:40 +000071static cl::opt<bool> NoLeadingHeaders("no-leading-headers",
72 cl::desc("Print no leading headers"));
73
Kevin Enderby13023a12015-01-15 23:19:11 +000074cl::opt<bool> llvm::UniversalHeaders("universal-headers",
75 cl::desc("Print Mach-O universal headers "
76 "(requires -macho)"));
77
Kevin Enderby131d1772015-01-09 19:22:37 +000078cl::opt<bool>
Kevin Enderby13023a12015-01-15 23:19:11 +000079 llvm::ArchiveHeaders("archive-headers",
80 cl::desc("Print archive headers for Mach-O archives "
81 "(requires -macho)"));
Kevin Enderby131d1772015-01-09 19:22:37 +000082
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +000083cl::opt<bool>
Kevin Enderby8972e482015-04-30 20:30:42 +000084 ArchiveMemberOffsets("archive-member-offsets",
85 cl::desc("Print the offset to each archive member for "
86 "Mach-O archives (requires -macho and "
87 "-archive-headers)"));
88
89cl::opt<bool>
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +000090 llvm::IndirectSymbols("indirect-symbols",
91 cl::desc("Print indirect symbol table for Mach-O "
92 "objects (requires -macho)"));
93
Kevin Enderby69fe98d2015-01-23 18:52:17 +000094cl::opt<bool>
95 llvm::DataInCode("data-in-code",
96 cl::desc("Print the data in code table for Mach-O objects "
97 "(requires -macho)"));
98
Kevin Enderby9a509442015-01-27 21:28:24 +000099cl::opt<bool>
100 llvm::LinkOptHints("link-opt-hints",
101 cl::desc("Print the linker optimization hints for "
102 "Mach-O objects (requires -macho)"));
103
Kevin Enderbycd66be52015-03-11 22:06:32 +0000104cl::opt<bool>
105 llvm::InfoPlist("info-plist",
106 cl::desc("Print the info plist section as strings for "
107 "Mach-O objects (requires -macho)"));
108
Kevin Enderbyf0640752015-03-13 17:56:32 +0000109cl::opt<bool>
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000110 llvm::DylibsUsed("dylibs-used",
111 cl::desc("Print the shared libraries used for linked "
112 "Mach-O files (requires -macho)"));
113
114cl::opt<bool>
115 llvm::DylibId("dylib-id",
116 cl::desc("Print the shared library's id for the dylib Mach-O "
117 "file (requires -macho)"));
118
119cl::opt<bool>
Kevin Enderbyf0640752015-03-13 17:56:32 +0000120 llvm::NonVerbose("non-verbose",
121 cl::desc("Print the info for Mach-O objects in "
122 "non-verbose or numeric form (requires -macho)"));
123
Kevin Enderby0fc11822015-04-01 20:57:01 +0000124cl::opt<bool>
125 llvm::ObjcMetaData("objc-meta-data",
126 cl::desc("Print the Objective-C runtime meta data for "
127 "Mach-O files (requires -macho)"));
128
Kevin Enderby6a221752015-03-17 17:10:57 +0000129cl::opt<std::string> llvm::DisSymName(
130 "dis-symname",
Saleem Abdulrasoolec6a7742016-09-08 23:17:34 +0000131 cl::desc("disassemble just this symbol's instructions (requires -macho)"));
Kevin Enderby6a221752015-03-17 17:10:57 +0000132
Kevin Enderby8e29ec92015-03-17 22:26:11 +0000133static cl::opt<bool> NoSymbolicOperands(
134 "no-symbolic-operands",
135 cl::desc("do not symbolic operands when disassembling (requires -macho)"));
136
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000137static cl::list<std::string>
138 ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
139 cl::ZeroOrMore);
Hans Wennborgcc9deb42015-09-29 18:02:48 +0000140
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000141bool ArchAll = false;
142
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000143static std::string ThumbTripleName;
144
145static const Target *GetTarget(const MachOObjectFile *MachOObj,
146 const char **McpuDefault,
147 const Target **ThumbTarget) {
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000148 // Figure out the target triple.
Tim Northover9e8eb412016-04-22 23:21:13 +0000149 llvm::Triple TT(TripleName);
Cameron Zwarich88cc16a2012-02-03 06:35:22 +0000150 if (TripleName.empty()) {
Tim Northover9e8eb412016-04-22 23:21:13 +0000151 TT = MachOObj->getArchTriple(McpuDefault);
Cameron Zwarich88cc16a2012-02-03 06:35:22 +0000152 TripleName = TT.str();
Tim Northover9e8eb412016-04-22 23:21:13 +0000153 }
154
155 if (TT.getArch() == Triple::arm) {
156 // We've inferred a 32-bit ARM target from the object file. All MachO CPUs
157 // that support ARM are also capable of Thumb mode.
158 llvm::Triple ThumbTriple = TT;
159 std::string ThumbName = (Twine("thumb") + TT.getArchName().substr(3)).str();
160 ThumbTriple.setArchName(ThumbName);
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000161 ThumbTripleName = ThumbTriple.str();
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000162 }
163
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000164 // Get the target specific parser.
165 std::string Error;
166 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000167 if (TheTarget && ThumbTripleName.empty())
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000168 return TheTarget;
169
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000170 *ThumbTarget = TargetRegistry::lookupTarget(ThumbTripleName, Error);
171 if (*ThumbTarget)
172 return TheTarget;
173
174 errs() << "llvm-objdump: error: unable to get target for '";
175 if (!TheTarget)
176 errs() << TripleName;
177 else
178 errs() << ThumbTripleName;
179 errs() << "', see --version and --triple.\n";
Craig Toppere6cb63e2014-04-25 04:24:47 +0000180 return nullptr;
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000181}
182
Owen Andersond9243c42011-10-17 21:37:35 +0000183struct SymbolSorter {
184 bool operator()(const SymbolRef &A, const SymbolRef &B) {
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000185 Expected<SymbolRef::Type> ATypeOrErr = A.getType();
Kevin Enderby844c4ac2016-11-15 23:07:41 +0000186 if (!ATypeOrErr)
187 report_error(A.getObject()->getFileName(), ATypeOrErr.takeError());
Kevin Enderby5afbc1c2016-03-23 20:27:00 +0000188 SymbolRef::Type AType = *ATypeOrErr;
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000189 Expected<SymbolRef::Type> BTypeOrErr = B.getType();
Kevin Enderby844c4ac2016-11-15 23:07:41 +0000190 if (!BTypeOrErr)
191 report_error(B.getObject()->getFileName(), BTypeOrErr.takeError());
Kevin Enderby74f58d42016-03-23 21:45:21 +0000192 SymbolRef::Type BType = *BTypeOrErr;
Kevin Enderby5afbc1c2016-03-23 20:27:00 +0000193 uint64_t AAddr = (AType != SymbolRef::ST_Function) ? 0 : A.getValue();
194 uint64_t BAddr = (BType != SymbolRef::ST_Function) ? 0 : B.getValue();
Owen Andersond9243c42011-10-17 21:37:35 +0000195 return AAddr < BAddr;
196 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000197};
198
Kevin Enderby273ae012013-06-06 17:20:50 +0000199// Types for the storted data in code table that is built before disassembly
200// and the predicate function to sort them.
201typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
202typedef std::vector<DiceTableEntry> DiceTable;
203typedef DiceTable::iterator dice_table_iterator;
204
Kevin Enderby930fdc72014-11-06 19:00:13 +0000205// This is used to search for a data in code table entry for the PC being
206// disassembled. The j parameter has the PC in j.first. A single data in code
207// table entry can cover many bytes for each of its Kind's. So if the offset,
208// aka the i.first value, of the data in code table entry plus its Length
209// covers the PC being searched for this will return true. If not it will
210// return false.
David Majnemerea9b8ee2014-11-04 08:41:48 +0000211static bool compareDiceTableEntries(const DiceTableEntry &i,
212 const DiceTableEntry &j) {
Kevin Enderby930fdc72014-11-06 19:00:13 +0000213 uint16_t Length;
214 i.second.getLength(Length);
215
216 return j.first >= i.first && j.first < i.first + Length;
Kevin Enderby273ae012013-06-06 17:20:50 +0000217}
218
Colin LeMahieufc32b1b2015-03-18 19:27:31 +0000219static uint64_t DumpDataInCode(const uint8_t *bytes, uint64_t Length,
Kevin Enderby930fdc72014-11-06 19:00:13 +0000220 unsigned short Kind) {
221 uint32_t Value, Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000222
223 switch (Kind) {
Kevin Enderby930fdc72014-11-06 19:00:13 +0000224 default:
Charles Davis8bdfafd2013-09-01 04:28:48 +0000225 case MachO::DICE_KIND_DATA:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000226 if (Length >= 4) {
227 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000228 dumpBytes(makeArrayRef(bytes, 4), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000229 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
Kevin Enderby273ae012013-06-06 17:20:50 +0000230 outs() << "\t.long " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000231 Size = 4;
232 } else if (Length >= 2) {
233 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000234 dumpBytes(makeArrayRef(bytes, 2), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000235 Value = bytes[1] << 8 | bytes[0];
Kevin Enderby273ae012013-06-06 17:20:50 +0000236 outs() << "\t.short " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000237 Size = 2;
238 } else {
239 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000240 dumpBytes(makeArrayRef(bytes, 2), outs());
Kevin Enderby273ae012013-06-06 17:20:50 +0000241 Value = bytes[0];
242 outs() << "\t.byte " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000243 Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000244 }
Kevin Enderby930fdc72014-11-06 19:00:13 +0000245 if (Kind == MachO::DICE_KIND_DATA)
246 outs() << "\t@ KIND_DATA\n";
247 else
248 outs() << "\t@ data in code kind = " << Kind << "\n";
Kevin Enderby273ae012013-06-06 17:20:50 +0000249 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000250 case MachO::DICE_KIND_JUMP_TABLE8:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000251 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000252 dumpBytes(makeArrayRef(bytes, 1), outs());
Kevin Enderby273ae012013-06-06 17:20:50 +0000253 Value = bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000254 outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n";
255 Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000256 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000257 case MachO::DICE_KIND_JUMP_TABLE16:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000258 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000259 dumpBytes(makeArrayRef(bytes, 2), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000260 Value = bytes[1] << 8 | bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000261 outs() << "\t.short " << format("%5u", Value & 0xffff)
262 << "\t@ KIND_JUMP_TABLE16\n";
263 Size = 2;
Kevin Enderby273ae012013-06-06 17:20:50 +0000264 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000265 case MachO::DICE_KIND_JUMP_TABLE32:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000266 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
267 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000268 dumpBytes(makeArrayRef(bytes, 4), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000269 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000270 outs() << "\t.long " << Value;
271 if (Kind == MachO::DICE_KIND_JUMP_TABLE32)
272 outs() << "\t@ KIND_JUMP_TABLE32\n";
273 else
274 outs() << "\t@ KIND_ABS_JUMP_TABLE32\n";
275 Size = 4;
Kevin Enderby273ae012013-06-06 17:20:50 +0000276 break;
277 }
Kevin Enderby930fdc72014-11-06 19:00:13 +0000278 return Size;
Kevin Enderby273ae012013-06-06 17:20:50 +0000279}
280
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000281static void getSectionsAndSymbols(MachOObjectFile *MachOObj,
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000282 std::vector<SectionRef> &Sections,
283 std::vector<SymbolRef> &Symbols,
284 SmallVectorImpl<uint64_t> &FoundFns,
285 uint64_t &BaseSegmentAddress) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000286 for (const SymbolRef &Symbol : MachOObj->symbols()) {
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000287 Expected<StringRef> SymName = Symbol.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +0000288 if (!SymName)
289 report_error(MachOObj->getFileName(), SymName.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000290 if (!SymName->startswith("ltmp"))
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000291 Symbols.push_back(Symbol);
292 }
Owen Andersond9243c42011-10-17 21:37:35 +0000293
Alexey Samsonov48803e52014-03-13 14:37:36 +0000294 for (const SectionRef &Section : MachOObj->sections()) {
Owen Andersond9243c42011-10-17 21:37:35 +0000295 StringRef SectName;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000296 Section.getName(SectName);
297 Sections.push_back(Section);
Owen Andersond9243c42011-10-17 21:37:35 +0000298 }
299
Kevin Enderby273ae012013-06-06 17:20:50 +0000300 bool BaseSegmentAddressSet = false;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000301 for (const auto &Command : MachOObj->load_commands()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000302 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
Benjamin Kramer699128e2011-09-21 01:13:19 +0000303 // We found a function starts segment, parse the addresses for later
304 // consumption.
Charles Davis8bdfafd2013-09-01 04:28:48 +0000305 MachO::linkedit_data_command LLC =
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000306 MachOObj->getLinkeditDataLoadCommand(Command);
Benjamin Kramer699128e2011-09-21 01:13:19 +0000307
Charles Davis8bdfafd2013-09-01 04:28:48 +0000308 MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000309 } else if (Command.C.cmd == MachO::LC_SEGMENT) {
310 MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000311 StringRef SegName = SLC.segname;
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000312 if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
Kevin Enderby273ae012013-06-06 17:20:50 +0000313 BaseSegmentAddressSet = true;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000314 BaseSegmentAddress = SLC.vmaddr;
Kevin Enderby273ae012013-06-06 17:20:50 +0000315 }
316 }
Benjamin Kramer8a529dc2011-09-21 22:16:43 +0000317 }
Benjamin Kramer699128e2011-09-21 01:13:19 +0000318}
319
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000320static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose,
321 uint32_t n, uint32_t count,
322 uint32_t stride, uint64_t addr) {
323 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
324 uint32_t nindirectsyms = Dysymtab.nindirectsyms;
325 if (n > nindirectsyms)
326 outs() << " (entries start past the end of the indirect symbol "
327 "table) (reserved1 field greater than the table size)";
328 else if (n + count > nindirectsyms)
329 outs() << " (entries extends past the end of the indirect symbol "
330 "table)";
331 outs() << "\n";
332 uint32_t cputype = O->getHeader().cputype;
333 if (cputype & MachO::CPU_ARCH_ABI64)
334 outs() << "address index";
335 else
336 outs() << "address index";
337 if (verbose)
338 outs() << " name\n";
339 else
340 outs() << "\n";
341 for (uint32_t j = 0; j < count && n + j < nindirectsyms; j++) {
342 if (cputype & MachO::CPU_ARCH_ABI64)
343 outs() << format("0x%016" PRIx64, addr + j * stride) << " ";
344 else
Tim Northover43978372016-04-26 18:29:16 +0000345 outs() << format("0x%08" PRIx32, (uint32_t)addr + j * stride) << " ";
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000346 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
347 uint32_t indirect_symbol = O->getIndirectSymbolTableEntry(Dysymtab, n + j);
348 if (indirect_symbol == MachO::INDIRECT_SYMBOL_LOCAL) {
349 outs() << "LOCAL\n";
350 continue;
351 }
352 if (indirect_symbol ==
353 (MachO::INDIRECT_SYMBOL_LOCAL | MachO::INDIRECT_SYMBOL_ABS)) {
354 outs() << "LOCAL ABSOLUTE\n";
355 continue;
356 }
357 if (indirect_symbol == MachO::INDIRECT_SYMBOL_ABS) {
358 outs() << "ABSOLUTE\n";
359 continue;
360 }
361 outs() << format("%5u ", indirect_symbol);
Kevin Enderbyf0640752015-03-13 17:56:32 +0000362 if (verbose) {
363 MachO::symtab_command Symtab = O->getSymtabLoadCommand();
364 if (indirect_symbol < Symtab.nsyms) {
365 symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol);
366 SymbolRef Symbol = *Sym;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000367 Expected<StringRef> SymName = Symbol.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +0000368 if (!SymName)
369 report_error(O->getFileName(), SymName.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000370 outs() << *SymName;
Kevin Enderbyf0640752015-03-13 17:56:32 +0000371 } else {
372 outs() << "?";
373 }
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000374 }
375 outs() << "\n";
376 }
377}
378
379static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) {
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000380 for (const auto &Load : O->load_commands()) {
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000381 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
382 MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
383 for (unsigned J = 0; J < Seg.nsects; ++J) {
384 MachO::section_64 Sec = O->getSection64(Load, J);
385 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
386 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
387 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
388 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
389 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
390 section_type == MachO::S_SYMBOL_STUBS) {
391 uint32_t stride;
392 if (section_type == MachO::S_SYMBOL_STUBS)
393 stride = Sec.reserved2;
394 else
395 stride = 8;
396 if (stride == 0) {
397 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
398 << Sec.sectname << ") "
399 << "(size of stubs in reserved2 field is zero)\n";
400 continue;
401 }
402 uint32_t count = Sec.size / stride;
403 outs() << "Indirect symbols for (" << Sec.segname << ","
404 << Sec.sectname << ") " << count << " entries";
405 uint32_t n = Sec.reserved1;
406 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
407 }
408 }
409 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
410 MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
411 for (unsigned J = 0; J < Seg.nsects; ++J) {
412 MachO::section Sec = O->getSection(Load, J);
413 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
414 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
415 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
416 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
417 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
418 section_type == MachO::S_SYMBOL_STUBS) {
419 uint32_t stride;
420 if (section_type == MachO::S_SYMBOL_STUBS)
421 stride = Sec.reserved2;
422 else
423 stride = 4;
424 if (stride == 0) {
425 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
426 << Sec.sectname << ") "
427 << "(size of stubs in reserved2 field is zero)\n";
428 continue;
429 }
430 uint32_t count = Sec.size / stride;
431 outs() << "Indirect symbols for (" << Sec.segname << ","
432 << Sec.sectname << ") " << count << " entries";
433 uint32_t n = Sec.reserved1;
434 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
435 }
436 }
437 }
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000438 }
439}
440
Kevin Enderby69fe98d2015-01-23 18:52:17 +0000441static void PrintDataInCodeTable(MachOObjectFile *O, bool verbose) {
442 MachO::linkedit_data_command DIC = O->getDataInCodeLoadCommand();
443 uint32_t nentries = DIC.datasize / sizeof(struct MachO::data_in_code_entry);
444 outs() << "Data in code table (" << nentries << " entries)\n";
445 outs() << "offset length kind\n";
446 for (dice_iterator DI = O->begin_dices(), DE = O->end_dices(); DI != DE;
447 ++DI) {
448 uint32_t Offset;
449 DI->getOffset(Offset);
450 outs() << format("0x%08" PRIx32, Offset) << " ";
451 uint16_t Length;
452 DI->getLength(Length);
453 outs() << format("%6u", Length) << " ";
454 uint16_t Kind;
455 DI->getKind(Kind);
456 if (verbose) {
457 switch (Kind) {
458 case MachO::DICE_KIND_DATA:
459 outs() << "DATA";
460 break;
461 case MachO::DICE_KIND_JUMP_TABLE8:
462 outs() << "JUMP_TABLE8";
463 break;
464 case MachO::DICE_KIND_JUMP_TABLE16:
465 outs() << "JUMP_TABLE16";
466 break;
467 case MachO::DICE_KIND_JUMP_TABLE32:
468 outs() << "JUMP_TABLE32";
469 break;
470 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
471 outs() << "ABS_JUMP_TABLE32";
472 break;
473 default:
474 outs() << format("0x%04" PRIx32, Kind);
475 break;
476 }
477 } else
478 outs() << format("0x%04" PRIx32, Kind);
479 outs() << "\n";
480 }
481}
482
Kevin Enderby9a509442015-01-27 21:28:24 +0000483static void PrintLinkOptHints(MachOObjectFile *O) {
484 MachO::linkedit_data_command LohLC = O->getLinkOptHintsLoadCommand();
485 const char *loh = O->getData().substr(LohLC.dataoff, 1).data();
486 uint32_t nloh = LohLC.datasize;
487 outs() << "Linker optimiztion hints (" << nloh << " total bytes)\n";
488 for (uint32_t i = 0; i < nloh;) {
489 unsigned n;
490 uint64_t identifier = decodeULEB128((const uint8_t *)(loh + i), &n);
491 i += n;
492 outs() << " identifier " << identifier << " ";
493 if (i >= nloh)
494 return;
495 switch (identifier) {
496 case 1:
497 outs() << "AdrpAdrp\n";
498 break;
499 case 2:
500 outs() << "AdrpLdr\n";
501 break;
502 case 3:
503 outs() << "AdrpAddLdr\n";
504 break;
505 case 4:
506 outs() << "AdrpLdrGotLdr\n";
507 break;
508 case 5:
509 outs() << "AdrpAddStr\n";
510 break;
511 case 6:
512 outs() << "AdrpLdrGotStr\n";
513 break;
514 case 7:
515 outs() << "AdrpAdd\n";
516 break;
517 case 8:
518 outs() << "AdrpLdrGot\n";
519 break;
520 default:
521 outs() << "Unknown identifier value\n";
522 break;
523 }
524 uint64_t narguments = decodeULEB128((const uint8_t *)(loh + i), &n);
525 i += n;
526 outs() << " narguments " << narguments << "\n";
527 if (i >= nloh)
528 return;
529
530 for (uint32_t j = 0; j < narguments; j++) {
531 uint64_t value = decodeULEB128((const uint8_t *)(loh + i), &n);
532 i += n;
533 outs() << "\tvalue " << format("0x%" PRIx64, value) << "\n";
534 if (i >= nloh)
535 return;
536 }
537 }
538}
539
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000540static void PrintDylibs(MachOObjectFile *O, bool JustId) {
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000541 unsigned Index = 0;
542 for (const auto &Load : O->load_commands()) {
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000543 if ((JustId && Load.C.cmd == MachO::LC_ID_DYLIB) ||
544 (!JustId && (Load.C.cmd == MachO::LC_ID_DYLIB ||
545 Load.C.cmd == MachO::LC_LOAD_DYLIB ||
546 Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
547 Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
548 Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
549 Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB))) {
550 MachO::dylib_command dl = O->getDylibIDLoadCommand(Load);
551 if (dl.dylib.name < dl.cmdsize) {
552 const char *p = (const char *)(Load.Ptr) + dl.dylib.name;
553 if (JustId)
554 outs() << p << "\n";
555 else {
556 outs() << "\t" << p;
557 outs() << " (compatibility version "
558 << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
559 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
560 << (dl.dylib.compatibility_version & 0xff) << ",";
561 outs() << " current version "
562 << ((dl.dylib.current_version >> 16) & 0xffff) << "."
563 << ((dl.dylib.current_version >> 8) & 0xff) << "."
564 << (dl.dylib.current_version & 0xff) << ")\n";
565 }
566 } else {
567 outs() << "\tBad offset (" << dl.dylib.name << ") for name of ";
568 if (Load.C.cmd == MachO::LC_ID_DYLIB)
569 outs() << "LC_ID_DYLIB ";
570 else if (Load.C.cmd == MachO::LC_LOAD_DYLIB)
571 outs() << "LC_LOAD_DYLIB ";
572 else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB)
573 outs() << "LC_LOAD_WEAK_DYLIB ";
574 else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB)
575 outs() << "LC_LAZY_LOAD_DYLIB ";
576 else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB)
577 outs() << "LC_REEXPORT_DYLIB ";
578 else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
579 outs() << "LC_LOAD_UPWARD_DYLIB ";
580 else
581 outs() << "LC_??? ";
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000582 outs() << "command " << Index++ << "\n";
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000583 }
584 }
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000585 }
586}
587
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000588typedef DenseMap<uint64_t, StringRef> SymbolAddressMap;
589
590static void CreateSymbolAddressMap(MachOObjectFile *O,
591 SymbolAddressMap *AddrMap) {
592 // Create a map of symbol addresses to symbol names.
593 for (const SymbolRef &Symbol : O->symbols()) {
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000594 Expected<SymbolRef::Type> STOrErr = Symbol.getType();
Kevin Enderby844c4ac2016-11-15 23:07:41 +0000595 if (!STOrErr)
596 report_error(O->getFileName(), STOrErr.takeError());
Kevin Enderby5afbc1c2016-03-23 20:27:00 +0000597 SymbolRef::Type ST = *STOrErr;
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000598 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
599 ST == SymbolRef::ST_Other) {
Rafael Espindoladea00162015-07-03 17:44:18 +0000600 uint64_t Address = Symbol.getValue();
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000601 Expected<StringRef> SymNameOrErr = Symbol.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +0000602 if (!SymNameOrErr)
603 report_error(O->getFileName(), SymNameOrErr.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000604 StringRef SymName = *SymNameOrErr;
Kevin Enderby846c0002015-04-16 17:19:59 +0000605 if (!SymName.startswith(".objc"))
606 (*AddrMap)[Address] = SymName;
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000607 }
608 }
609}
610
611// GuessSymbolName is passed the address of what might be a symbol and a
612// pointer to the SymbolAddressMap. It returns the name of a symbol
613// with that address or nullptr if no symbol is found with that address.
614static const char *GuessSymbolName(uint64_t value, SymbolAddressMap *AddrMap) {
615 const char *SymbolName = nullptr;
616 // A DenseMap can't lookup up some values.
617 if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) {
618 StringRef name = AddrMap->lookup(value);
619 if (!name.empty())
620 SymbolName = name.data();
621 }
622 return SymbolName;
623}
624
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000625static void DumpCstringChar(const char c) {
626 char p[2];
627 p[0] = c;
628 p[1] = '\0';
629 outs().write_escaped(p);
630}
631
Kevin Enderby10ba0412015-02-04 21:38:42 +0000632static void DumpCstringSection(MachOObjectFile *O, const char *sect,
633 uint32_t sect_size, uint64_t sect_addr,
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000634 bool print_addresses) {
Kevin Enderby10ba0412015-02-04 21:38:42 +0000635 for (uint32_t i = 0; i < sect_size; i++) {
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000636 if (print_addresses) {
637 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000638 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000639 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000640 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000641 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000642 for (; i < sect_size && sect[i] != '\0'; i++)
643 DumpCstringChar(sect[i]);
Kevin Enderby10ba0412015-02-04 21:38:42 +0000644 if (i < sect_size && sect[i] == '\0')
645 outs() << "\n";
646 }
647}
648
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000649static void DumpLiteral4(uint32_t l, float f) {
650 outs() << format("0x%08" PRIx32, l);
651 if ((l & 0x7f800000) != 0x7f800000)
652 outs() << format(" (%.16e)\n", f);
653 else {
654 if (l == 0x7f800000)
655 outs() << " (+Infinity)\n";
656 else if (l == 0xff800000)
657 outs() << " (-Infinity)\n";
658 else if ((l & 0x00400000) == 0x00400000)
659 outs() << " (non-signaling Not-a-Number)\n";
660 else
661 outs() << " (signaling Not-a-Number)\n";
662 }
663}
664
665static void DumpLiteral4Section(MachOObjectFile *O, const char *sect,
666 uint32_t sect_size, uint64_t sect_addr,
667 bool print_addresses) {
668 for (uint32_t i = 0; i < sect_size; i += sizeof(float)) {
669 if (print_addresses) {
670 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000671 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000672 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000673 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000674 }
675 float f;
676 memcpy(&f, sect + i, sizeof(float));
677 if (O->isLittleEndian() != sys::IsLittleEndianHost)
678 sys::swapByteOrder(f);
679 uint32_t l;
680 memcpy(&l, sect + i, sizeof(uint32_t));
681 if (O->isLittleEndian() != sys::IsLittleEndianHost)
682 sys::swapByteOrder(l);
683 DumpLiteral4(l, f);
684 }
685}
686
687static void DumpLiteral8(MachOObjectFile *O, uint32_t l0, uint32_t l1,
688 double d) {
689 outs() << format("0x%08" PRIx32, l0) << " " << format("0x%08" PRIx32, l1);
690 uint32_t Hi, Lo;
Davide Italianob627d9f2015-12-12 21:50:11 +0000691 Hi = (O->isLittleEndian()) ? l1 : l0;
692 Lo = (O->isLittleEndian()) ? l0 : l1;
693
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000694 // Hi is the high word, so this is equivalent to if(isfinite(d))
695 if ((Hi & 0x7ff00000) != 0x7ff00000)
696 outs() << format(" (%.16e)\n", d);
697 else {
698 if (Hi == 0x7ff00000 && Lo == 0)
699 outs() << " (+Infinity)\n";
700 else if (Hi == 0xfff00000 && Lo == 0)
701 outs() << " (-Infinity)\n";
702 else if ((Hi & 0x00080000) == 0x00080000)
703 outs() << " (non-signaling Not-a-Number)\n";
704 else
705 outs() << " (signaling Not-a-Number)\n";
706 }
707}
708
709static void DumpLiteral8Section(MachOObjectFile *O, const char *sect,
710 uint32_t sect_size, uint64_t sect_addr,
711 bool print_addresses) {
712 for (uint32_t i = 0; i < sect_size; i += sizeof(double)) {
713 if (print_addresses) {
714 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000715 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000716 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000717 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000718 }
719 double d;
720 memcpy(&d, sect + i, sizeof(double));
721 if (O->isLittleEndian() != sys::IsLittleEndianHost)
722 sys::swapByteOrder(d);
723 uint32_t l0, l1;
724 memcpy(&l0, sect + i, sizeof(uint32_t));
725 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
726 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
727 sys::swapByteOrder(l0);
728 sys::swapByteOrder(l1);
729 }
730 DumpLiteral8(O, l0, l1, d);
731 }
732}
733
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000734static void DumpLiteral16(uint32_t l0, uint32_t l1, uint32_t l2, uint32_t l3) {
735 outs() << format("0x%08" PRIx32, l0) << " ";
736 outs() << format("0x%08" PRIx32, l1) << " ";
737 outs() << format("0x%08" PRIx32, l2) << " ";
738 outs() << format("0x%08" PRIx32, l3) << "\n";
739}
740
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000741static void DumpLiteral16Section(MachOObjectFile *O, const char *sect,
742 uint32_t sect_size, uint64_t sect_addr,
743 bool print_addresses) {
744 for (uint32_t i = 0; i < sect_size; i += 16) {
745 if (print_addresses) {
746 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000747 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000748 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000749 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000750 }
751 uint32_t l0, l1, l2, l3;
752 memcpy(&l0, sect + i, sizeof(uint32_t));
753 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
754 memcpy(&l2, sect + i + 2 * sizeof(uint32_t), sizeof(uint32_t));
755 memcpy(&l3, sect + i + 3 * sizeof(uint32_t), sizeof(uint32_t));
756 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
757 sys::swapByteOrder(l0);
758 sys::swapByteOrder(l1);
759 sys::swapByteOrder(l2);
760 sys::swapByteOrder(l3);
761 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000762 DumpLiteral16(l0, l1, l2, l3);
763 }
764}
765
766static void DumpLiteralPointerSection(MachOObjectFile *O,
767 const SectionRef &Section,
768 const char *sect, uint32_t sect_size,
769 uint64_t sect_addr,
770 bool print_addresses) {
771 // Collect the literal sections in this Mach-O file.
772 std::vector<SectionRef> LiteralSections;
773 for (const SectionRef &Section : O->sections()) {
774 DataRefImpl Ref = Section.getRawDataRefImpl();
775 uint32_t section_type;
776 if (O->is64Bit()) {
777 const MachO::section_64 Sec = O->getSection64(Ref);
778 section_type = Sec.flags & MachO::SECTION_TYPE;
779 } else {
780 const MachO::section Sec = O->getSection(Ref);
781 section_type = Sec.flags & MachO::SECTION_TYPE;
782 }
783 if (section_type == MachO::S_CSTRING_LITERALS ||
784 section_type == MachO::S_4BYTE_LITERALS ||
785 section_type == MachO::S_8BYTE_LITERALS ||
786 section_type == MachO::S_16BYTE_LITERALS)
787 LiteralSections.push_back(Section);
788 }
789
790 // Set the size of the literal pointer.
791 uint32_t lp_size = O->is64Bit() ? 8 : 4;
792
Eric Christopher572e03a2015-06-19 01:53:21 +0000793 // Collect the external relocation symbols for the literal pointers.
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000794 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
795 for (const RelocationRef &Reloc : Section.relocations()) {
796 DataRefImpl Rel;
797 MachO::any_relocation_info RE;
798 bool isExtern = false;
799 Rel = Reloc.getRawDataRefImpl();
800 RE = O->getRelocation(Rel);
801 isExtern = O->getPlainRelocationExternal(RE);
802 if (isExtern) {
Rafael Espindola96d071c2015-06-29 23:29:12 +0000803 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000804 symbol_iterator RelocSym = Reloc.getSymbol();
805 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
806 }
807 }
808 array_pod_sort(Relocs.begin(), Relocs.end());
809
810 // Dump each literal pointer.
811 for (uint32_t i = 0; i < sect_size; i += lp_size) {
812 if (print_addresses) {
813 if (O->is64Bit())
814 outs() << format("%016" PRIx64, sect_addr + i) << " ";
815 else
816 outs() << format("%08" PRIx64, sect_addr + i) << " ";
817 }
818 uint64_t lp;
819 if (O->is64Bit()) {
820 memcpy(&lp, sect + i, sizeof(uint64_t));
821 if (O->isLittleEndian() != sys::IsLittleEndianHost)
822 sys::swapByteOrder(lp);
823 } else {
824 uint32_t li;
825 memcpy(&li, sect + i, sizeof(uint32_t));
826 if (O->isLittleEndian() != sys::IsLittleEndianHost)
827 sys::swapByteOrder(li);
828 lp = li;
829 }
830
831 // First look for an external relocation entry for this literal pointer.
David Majnemer42531262016-08-12 03:55:06 +0000832 auto Reloc = find_if(Relocs, [&](const std::pair<uint64_t, SymbolRef> &P) {
833 return P.first == i;
834 });
David Blaikie33dd45d02015-03-23 18:39:02 +0000835 if (Reloc != Relocs.end()) {
836 symbol_iterator RelocSym = Reloc->second;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000837 Expected<StringRef> SymName = RelocSym->getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +0000838 if (!SymName)
839 report_error(O->getFileName(), SymName.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000840 outs() << "external relocation entry for symbol:" << *SymName << "\n";
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000841 continue;
David Blaikie33dd45d02015-03-23 18:39:02 +0000842 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000843
844 // For local references see what the section the literal pointer points to.
David Majnemer562e8292016-08-12 00:18:03 +0000845 auto Sect = find_if(LiteralSections, [&](const SectionRef &R) {
846 return lp >= R.getAddress() && lp < R.getAddress() + R.getSize();
847 });
David Blaikie33dd45d02015-03-23 18:39:02 +0000848 if (Sect == LiteralSections.end()) {
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000849 outs() << format("0x%" PRIx64, lp) << " (not in a literal section)\n";
David Blaikie33dd45d02015-03-23 18:39:02 +0000850 continue;
851 }
852
853 uint64_t SectAddress = Sect->getAddress();
854 uint64_t SectSize = Sect->getSize();
855
856 StringRef SectName;
857 Sect->getName(SectName);
858 DataRefImpl Ref = Sect->getRawDataRefImpl();
859 StringRef SegmentName = O->getSectionFinalSegmentName(Ref);
860 outs() << SegmentName << ":" << SectName << ":";
861
862 uint32_t section_type;
863 if (O->is64Bit()) {
864 const MachO::section_64 Sec = O->getSection64(Ref);
865 section_type = Sec.flags & MachO::SECTION_TYPE;
866 } else {
867 const MachO::section Sec = O->getSection(Ref);
868 section_type = Sec.flags & MachO::SECTION_TYPE;
869 }
870
871 StringRef BytesStr;
872 Sect->getContents(BytesStr);
873 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
874
875 switch (section_type) {
876 case MachO::S_CSTRING_LITERALS:
877 for (uint64_t i = lp - SectAddress; i < SectSize && Contents[i] != '\0';
878 i++) {
879 DumpCstringChar(Contents[i]);
880 }
881 outs() << "\n";
882 break;
883 case MachO::S_4BYTE_LITERALS:
884 float f;
885 memcpy(&f, Contents + (lp - SectAddress), sizeof(float));
886 uint32_t l;
887 memcpy(&l, Contents + (lp - SectAddress), sizeof(uint32_t));
888 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
889 sys::swapByteOrder(f);
890 sys::swapByteOrder(l);
891 }
892 DumpLiteral4(l, f);
893 break;
894 case MachO::S_8BYTE_LITERALS: {
895 double d;
896 memcpy(&d, Contents + (lp - SectAddress), sizeof(double));
897 uint32_t l0, l1;
898 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
899 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
900 sizeof(uint32_t));
901 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
902 sys::swapByteOrder(f);
903 sys::swapByteOrder(l0);
904 sys::swapByteOrder(l1);
905 }
906 DumpLiteral8(O, l0, l1, d);
907 break;
908 }
909 case MachO::S_16BYTE_LITERALS: {
910 uint32_t l0, l1, l2, l3;
911 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
912 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
913 sizeof(uint32_t));
914 memcpy(&l2, Contents + (lp - SectAddress) + 2 * sizeof(uint32_t),
915 sizeof(uint32_t));
916 memcpy(&l3, Contents + (lp - SectAddress) + 3 * sizeof(uint32_t),
917 sizeof(uint32_t));
918 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
919 sys::swapByteOrder(l0);
920 sys::swapByteOrder(l1);
921 sys::swapByteOrder(l2);
922 sys::swapByteOrder(l3);
923 }
924 DumpLiteral16(l0, l1, l2, l3);
925 break;
926 }
927 }
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000928 }
929}
930
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000931static void DumpInitTermPointerSection(MachOObjectFile *O, const char *sect,
932 uint32_t sect_size, uint64_t sect_addr,
933 SymbolAddressMap *AddrMap,
934 bool verbose) {
935 uint32_t stride;
Davide Italiano3eb47e22015-12-15 23:14:21 +0000936 stride = (O->is64Bit()) ? sizeof(uint64_t) : sizeof(uint32_t);
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000937 for (uint32_t i = 0; i < sect_size; i += stride) {
938 const char *SymbolName = nullptr;
939 if (O->is64Bit()) {
940 outs() << format("0x%016" PRIx64, sect_addr + i * stride) << " ";
941 uint64_t pointer_value;
942 memcpy(&pointer_value, sect + i, stride);
943 if (O->isLittleEndian() != sys::IsLittleEndianHost)
944 sys::swapByteOrder(pointer_value);
945 outs() << format("0x%016" PRIx64, pointer_value);
946 if (verbose)
947 SymbolName = GuessSymbolName(pointer_value, AddrMap);
948 } else {
949 outs() << format("0x%08" PRIx64, sect_addr + i * stride) << " ";
950 uint32_t pointer_value;
951 memcpy(&pointer_value, sect + i, stride);
952 if (O->isLittleEndian() != sys::IsLittleEndianHost)
953 sys::swapByteOrder(pointer_value);
954 outs() << format("0x%08" PRIx32, pointer_value);
955 if (verbose)
956 SymbolName = GuessSymbolName(pointer_value, AddrMap);
957 }
958 if (SymbolName)
959 outs() << " " << SymbolName;
960 outs() << "\n";
961 }
962}
963
964static void DumpRawSectionContents(MachOObjectFile *O, const char *sect,
965 uint32_t size, uint64_t addr) {
966 uint32_t cputype = O->getHeader().cputype;
967 if (cputype == MachO::CPU_TYPE_I386 || cputype == MachO::CPU_TYPE_X86_64) {
968 uint32_t j;
969 for (uint32_t i = 0; i < size; i += j, addr += j) {
970 if (O->is64Bit())
971 outs() << format("%016" PRIx64, addr) << "\t";
972 else
Kevin Enderbyf0640752015-03-13 17:56:32 +0000973 outs() << format("%08" PRIx64, addr) << "\t";
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000974 for (j = 0; j < 16 && i + j < size; j++) {
975 uint8_t byte_word = *(sect + i + j);
976 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
977 }
978 outs() << "\n";
979 }
980 } else {
981 uint32_t j;
982 for (uint32_t i = 0; i < size; i += j, addr += j) {
983 if (O->is64Bit())
984 outs() << format("%016" PRIx64, addr) << "\t";
985 else
Kevin Enderbyc4930852016-04-27 22:36:18 +0000986 outs() << format("%08" PRIx64, addr) << "\t";
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000987 for (j = 0; j < 4 * sizeof(int32_t) && i + j < size;
988 j += sizeof(int32_t)) {
Kevin Enderby8eccdad2016-04-27 23:43:00 +0000989 if (i + j + sizeof(int32_t) <= size) {
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000990 uint32_t long_word;
991 memcpy(&long_word, sect + i + j, sizeof(int32_t));
992 if (O->isLittleEndian() != sys::IsLittleEndianHost)
993 sys::swapByteOrder(long_word);
994 outs() << format("%08" PRIx32, long_word) << " ";
995 } else {
996 for (uint32_t k = 0; i + j + k < size; k++) {
Kevin Enderby8eccdad2016-04-27 23:43:00 +0000997 uint8_t byte_word = *(sect + i + j + k);
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000998 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
999 }
1000 }
1001 }
1002 outs() << "\n";
1003 }
1004 }
1005}
1006
Kevin Enderby95df54c2015-02-04 01:01:38 +00001007static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
1008 StringRef DisSegName, StringRef DisSectName);
Kevin Enderby4ad9bde2015-04-16 22:33:20 +00001009static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
1010 uint32_t size, uint32_t addr);
Kevin Enderby9873e2c2016-05-23 21:34:12 +00001011#ifdef HAVE_LIBXAR
1012static void DumpBitcodeSection(MachOObjectFile *O, const char *sect,
1013 uint32_t size, bool verbose,
1014 bool PrintXarHeader, bool PrintXarFileHeaders,
1015 std::string XarMemberName);
1016#endif // defined(HAVE_LIBXAR)
Kevin Enderby95df54c2015-02-04 01:01:38 +00001017
1018static void DumpSectionContents(StringRef Filename, MachOObjectFile *O,
1019 bool verbose) {
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001020 SymbolAddressMap AddrMap;
1021 if (verbose)
1022 CreateSymbolAddressMap(O, &AddrMap);
1023
Colin LeMahieufcc32762015-07-29 19:08:10 +00001024 for (unsigned i = 0; i < FilterSections.size(); ++i) {
1025 StringRef DumpSection = FilterSections[i];
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001026 std::pair<StringRef, StringRef> DumpSegSectName;
1027 DumpSegSectName = DumpSection.split(',');
1028 StringRef DumpSegName, DumpSectName;
1029 if (DumpSegSectName.second.size()) {
1030 DumpSegName = DumpSegSectName.first;
1031 DumpSectName = DumpSegSectName.second;
1032 } else {
1033 DumpSegName = "";
1034 DumpSectName = DumpSegSectName.first;
1035 }
1036 for (const SectionRef &Section : O->sections()) {
1037 StringRef SectName;
1038 Section.getName(SectName);
1039 DataRefImpl Ref = Section.getRawDataRefImpl();
1040 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1041 if ((DumpSegName.empty() || SegName == DumpSegName) &&
1042 (SectName == DumpSectName)) {
Adrian Prantlc2401dd2015-03-27 17:31:15 +00001043
Kevin Enderby95df54c2015-02-04 01:01:38 +00001044 uint32_t section_flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001045 if (O->is64Bit()) {
1046 const MachO::section_64 Sec = O->getSection64(Ref);
Kevin Enderby95df54c2015-02-04 01:01:38 +00001047 section_flags = Sec.flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001048
1049 } else {
1050 const MachO::section Sec = O->getSection(Ref);
Kevin Enderby95df54c2015-02-04 01:01:38 +00001051 section_flags = Sec.flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001052 }
Kevin Enderby95df54c2015-02-04 01:01:38 +00001053 uint32_t section_type = section_flags & MachO::SECTION_TYPE;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001054
1055 StringRef BytesStr;
1056 Section.getContents(BytesStr);
1057 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1058 uint32_t sect_size = BytesStr.size();
1059 uint64_t sect_addr = Section.getAddress();
1060
Adrian Prantlc2401dd2015-03-27 17:31:15 +00001061 outs() << "Contents of (" << SegName << "," << SectName
1062 << ") section\n";
1063
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001064 if (verbose) {
Kevin Enderby95df54c2015-02-04 01:01:38 +00001065 if ((section_flags & MachO::S_ATTR_PURE_INSTRUCTIONS) ||
1066 (section_flags & MachO::S_ATTR_SOME_INSTRUCTIONS)) {
1067 DisassembleMachO(Filename, O, SegName, SectName);
1068 continue;
1069 }
Kevin Enderbycd66be52015-03-11 22:06:32 +00001070 if (SegName == "__TEXT" && SectName == "__info_plist") {
1071 outs() << sect;
1072 continue;
1073 }
Kevin Enderby4ad9bde2015-04-16 22:33:20 +00001074 if (SegName == "__OBJC" && SectName == "__protocol") {
1075 DumpProtocolSection(O, sect, sect_size, sect_addr);
1076 continue;
1077 }
Kevin Enderby9873e2c2016-05-23 21:34:12 +00001078#ifdef HAVE_LIBXAR
1079 if (SegName == "__LLVM" && SectName == "__bundle") {
1080 DumpBitcodeSection(O, sect, sect_size, verbose, !NoSymbolicOperands,
1081 ArchiveHeaders, "");
1082 continue;
1083 }
1084#endif // defined(HAVE_LIBXAR)
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001085 switch (section_type) {
1086 case MachO::S_REGULAR:
1087 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1088 break;
1089 case MachO::S_ZEROFILL:
1090 outs() << "zerofill section and has no contents in the file\n";
1091 break;
Kevin Enderby10ba0412015-02-04 21:38:42 +00001092 case MachO::S_CSTRING_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001093 DumpCstringSection(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby10ba0412015-02-04 21:38:42 +00001094 break;
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001095 case MachO::S_4BYTE_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001096 DumpLiteral4Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001097 break;
1098 case MachO::S_8BYTE_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001099 DumpLiteral8Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001100 break;
1101 case MachO::S_16BYTE_LITERALS:
Kevin Enderby0fc11822015-04-01 20:57:01 +00001102 DumpLiteral16Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1103 break;
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001104 case MachO::S_LITERAL_POINTERS:
1105 DumpLiteralPointerSection(O, Section, sect, sect_size, sect_addr,
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001106 !NoLeadingAddr);
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001107 break;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001108 case MachO::S_MOD_INIT_FUNC_POINTERS:
1109 case MachO::S_MOD_TERM_FUNC_POINTERS:
1110 DumpInitTermPointerSection(O, sect, sect_size, sect_addr, &AddrMap,
1111 verbose);
1112 break;
1113 default:
1114 outs() << "Unknown section type ("
1115 << format("0x%08" PRIx32, section_type) << ")\n";
1116 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1117 break;
1118 }
1119 } else {
1120 if (section_type == MachO::S_ZEROFILL)
1121 outs() << "zerofill section and has no contents in the file\n";
1122 else
1123 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1124 }
1125 }
1126 }
1127 }
1128}
1129
Kevin Enderbycd66be52015-03-11 22:06:32 +00001130static void DumpInfoPlistSectionContents(StringRef Filename,
1131 MachOObjectFile *O) {
1132 for (const SectionRef &Section : O->sections()) {
1133 StringRef SectName;
1134 Section.getName(SectName);
1135 DataRefImpl Ref = Section.getRawDataRefImpl();
1136 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1137 if (SegName == "__TEXT" && SectName == "__info_plist") {
Kevin Enderby30cf2e82017-06-20 21:00:25 +00001138 if (!NoLeadingHeaders)
1139 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
Kevin Enderbycd66be52015-03-11 22:06:32 +00001140 StringRef BytesStr;
1141 Section.getContents(BytesStr);
1142 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
Kevin Enderby418659f2017-02-06 21:01:08 +00001143 outs() << format("%.*s", BytesStr.size(), sect) << "\n";
Kevin Enderbycd66be52015-03-11 22:06:32 +00001144 return;
1145 }
1146 }
1147}
1148
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001149// checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file
1150// and if it is and there is a list of architecture flags is specified then
1151// check to make sure this Mach-O file is one of those architectures or all
1152// architectures were specified. If not then an error is generated and this
1153// routine returns false. Else it returns true.
1154static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
David Majnemer91160d82016-10-31 17:11:31 +00001155 auto *MachO = dyn_cast<MachOObjectFile>(O);
1156
1157 if (!MachO || ArchAll || ArchFlags.empty())
1158 return true;
1159
1160 MachO::mach_header H;
1161 MachO::mach_header_64 H_64;
1162 Triple T;
Kevin Enderby59343a92016-12-16 22:54:02 +00001163 const char *McpuDefault, *ArchFlag;
David Majnemer91160d82016-10-31 17:11:31 +00001164 if (MachO->is64Bit()) {
1165 H_64 = MachO->MachOObjectFile::getHeader64();
Kevin Enderby59343a92016-12-16 22:54:02 +00001166 T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype,
1167 &McpuDefault, &ArchFlag);
David Majnemer91160d82016-10-31 17:11:31 +00001168 } else {
1169 H = MachO->MachOObjectFile::getHeader();
Kevin Enderby59343a92016-12-16 22:54:02 +00001170 T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype,
1171 &McpuDefault, &ArchFlag);
David Majnemer91160d82016-10-31 17:11:31 +00001172 }
Kevin Enderby59343a92016-12-16 22:54:02 +00001173 const std::string ArchFlagName(ArchFlag);
David Majnemer91160d82016-10-31 17:11:31 +00001174 if (none_of(ArchFlags, [&](const std::string &Name) {
Kevin Enderby59343a92016-12-16 22:54:02 +00001175 return Name == ArchFlagName;
David Majnemer91160d82016-10-31 17:11:31 +00001176 })) {
1177 errs() << "llvm-objdump: " + Filename + ": No architecture specified.\n";
1178 return false;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001179 }
1180 return true;
1181}
1182
Kevin Enderby0fc11822015-04-01 20:57:01 +00001183static void printObjcMetaData(MachOObjectFile *O, bool verbose);
1184
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001185// ProcessMachO() is passed a single opened Mach-O file, which may be an
1186// archive member and or in a slice of a universal file. It prints the
1187// the file name and header info and then processes it according to the
1188// command line options.
Kevin Enderby844c4ac2016-11-15 23:07:41 +00001189static void ProcessMachO(StringRef Name, MachOObjectFile *MachOOF,
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001190 StringRef ArchiveMemberName = StringRef(),
1191 StringRef ArchitectureName = StringRef()) {
Kevin Enderby131d1772015-01-09 19:22:37 +00001192 // If we are doing some processing here on the Mach-O file print the header
1193 // info. And don't print it otherwise like in the case of printing the
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +00001194 // UniversalHeaders or ArchiveHeaders.
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001195 if (Disassemble || PrivateHeaders || ExportsTrie || Rebase || Bind || SymbolTable ||
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001196 LazyBind || WeakBind || IndirectSymbols || DataInCode || LinkOptHints ||
Colin LeMahieufcc32762015-07-29 19:08:10 +00001197 DylibsUsed || DylibId || ObjcMetaData || (FilterSections.size() != 0)) {
Kevin Enderbyf9d60f02016-11-29 21:43:40 +00001198 if (!NoLeadingHeaders) {
1199 outs() << Name;
1200 if (!ArchiveMemberName.empty())
1201 outs() << '(' << ArchiveMemberName << ')';
1202 if (!ArchitectureName.empty())
1203 outs() << " (architecture " << ArchitectureName << ")";
1204 outs() << ":\n";
1205 }
Kevin Enderby131d1772015-01-09 19:22:37 +00001206 }
Kevin Enderby844c4ac2016-11-15 23:07:41 +00001207 // To use the report_error() form with an ArchiveName and FileName set
1208 // these up based on what is passed for Name and ArchiveMemberName.
1209 StringRef ArchiveName;
1210 StringRef FileName;
1211 if (!ArchiveMemberName.empty()) {
1212 ArchiveName = Name;
1213 FileName = ArchiveMemberName;
1214 } else {
1215 ArchiveName = StringRef();
1216 FileName = Name;
1217 }
1218
1219 // If we need the symbol table to do the operation then check it here to
1220 // produce a good error message as to where the Mach-O file comes from in
1221 // the error message.
1222 if (Disassemble || IndirectSymbols || FilterSections.size() != 0 ||
1223 UnwindInfo)
1224 if (Error Err = MachOOF->checkSymbolTable())
1225 report_error(ArchiveName, FileName, std::move(Err), ArchitectureName);
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001226
Kevin Enderbyaf2999a2017-06-22 19:50:56 +00001227 if (Disassemble) {
1228 if (MachOOF->getHeader().filetype == MachO::MH_KEXT_BUNDLE &&
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +00001229 MachOOF->getHeader().cputype == MachO::CPU_TYPE_ARM64)
Kevin Enderbyaf2999a2017-06-22 19:50:56 +00001230 DisassembleMachO(FileName, MachOOF, "__TEXT_EXEC", "__text");
1231 else
1232 DisassembleMachO(FileName, MachOOF, "__TEXT", "__text");
1233 }
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +00001234 if (IndirectSymbols)
Kevin Enderbyf0640752015-03-13 17:56:32 +00001235 PrintIndirectSymbols(MachOOF, !NonVerbose);
Kevin Enderby69fe98d2015-01-23 18:52:17 +00001236 if (DataInCode)
Kevin Enderbyf0640752015-03-13 17:56:32 +00001237 PrintDataInCodeTable(MachOOF, !NonVerbose);
Kevin Enderby9a509442015-01-27 21:28:24 +00001238 if (LinkOptHints)
1239 PrintLinkOptHints(MachOOF);
Kevin Enderby98da6132015-01-20 21:47:46 +00001240 if (Relocations)
1241 PrintRelocations(MachOOF);
1242 if (SectionHeaders)
1243 PrintSectionHeaders(MachOOF);
1244 if (SectionContents)
1245 PrintSectionContents(MachOOF);
Colin LeMahieufcc32762015-07-29 19:08:10 +00001246 if (FilterSections.size() != 0)
Kevin Enderby844c4ac2016-11-15 23:07:41 +00001247 DumpSectionContents(FileName, MachOOF, !NonVerbose);
Kevin Enderbycd66be52015-03-11 22:06:32 +00001248 if (InfoPlist)
Kevin Enderby844c4ac2016-11-15 23:07:41 +00001249 DumpInfoPlistSectionContents(FileName, MachOOF);
Kevin Enderbybc847fa2015-03-16 20:08:09 +00001250 if (DylibsUsed)
1251 PrintDylibs(MachOOF, false);
1252 if (DylibId)
1253 PrintDylibs(MachOOF, true);
Kevin Enderby844c4ac2016-11-15 23:07:41 +00001254 if (SymbolTable)
Kevin Enderby9acb1092016-05-31 20:35:34 +00001255 PrintSymbolTable(MachOOF, ArchiveName, ArchitectureName);
Kevin Enderby98da6132015-01-20 21:47:46 +00001256 if (UnwindInfo)
1257 printMachOUnwindInfo(MachOOF);
Kevin Enderby0ae163f2016-01-13 00:25:36 +00001258 if (PrivateHeaders) {
1259 printMachOFileHeader(MachOOF);
1260 printMachOLoadCommands(MachOOF);
1261 }
1262 if (FirstPrivateHeader)
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001263 printMachOFileHeader(MachOOF);
Kevin Enderby0fc11822015-04-01 20:57:01 +00001264 if (ObjcMetaData)
1265 printObjcMetaData(MachOOF, !NonVerbose);
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001266 if (ExportsTrie)
1267 printExportsTrie(MachOOF);
1268 if (Rebase)
1269 printRebaseTable(MachOOF);
1270 if (Bind)
1271 printBindTable(MachOOF);
1272 if (LazyBind)
1273 printLazyBindTable(MachOOF);
1274 if (WeakBind)
1275 printWeakBindTable(MachOOF);
Igor Laevsky03a670c2016-01-26 15:09:42 +00001276
1277 if (DwarfDumpType != DIDT_Null) {
Rafael Espindolac398e672017-07-19 22:27:28 +00001278 std::unique_ptr<DIContext> DICtx = DWARFContext::create(*MachOOF);
Igor Laevsky03a670c2016-01-26 15:09:42 +00001279 // Dump the complete DWARF structure.
Adrian Prantlf4bc1f72017-06-01 18:18:23 +00001280 DIDumpOptions DumpOpts;
1281 DumpOpts.DumpType = DwarfDumpType;
1282 DumpOpts.DumpEH = true;
1283 DICtx->dump(outs(), DumpOpts);
Igor Laevsky03a670c2016-01-26 15:09:42 +00001284 }
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001285}
1286
Kevin Enderby131d1772015-01-09 19:22:37 +00001287// printUnknownCPUType() helps print_fat_headers for unknown CPU's.
1288static void printUnknownCPUType(uint32_t cputype, uint32_t cpusubtype) {
1289 outs() << " cputype (" << cputype << ")\n";
1290 outs() << " cpusubtype (" << cpusubtype << ")\n";
1291}
1292
1293// printCPUType() helps print_fat_headers by printing the cputype and
1294// pusubtype (symbolically for the one's it knows about).
1295static void printCPUType(uint32_t cputype, uint32_t cpusubtype) {
1296 switch (cputype) {
1297 case MachO::CPU_TYPE_I386:
1298 switch (cpusubtype) {
1299 case MachO::CPU_SUBTYPE_I386_ALL:
1300 outs() << " cputype CPU_TYPE_I386\n";
1301 outs() << " cpusubtype CPU_SUBTYPE_I386_ALL\n";
1302 break;
1303 default:
1304 printUnknownCPUType(cputype, cpusubtype);
1305 break;
1306 }
1307 break;
1308 case MachO::CPU_TYPE_X86_64:
1309 switch (cpusubtype) {
1310 case MachO::CPU_SUBTYPE_X86_64_ALL:
1311 outs() << " cputype CPU_TYPE_X86_64\n";
1312 outs() << " cpusubtype CPU_SUBTYPE_X86_64_ALL\n";
1313 break;
1314 case MachO::CPU_SUBTYPE_X86_64_H:
1315 outs() << " cputype CPU_TYPE_X86_64\n";
1316 outs() << " cpusubtype CPU_SUBTYPE_X86_64_H\n";
1317 break;
1318 default:
1319 printUnknownCPUType(cputype, cpusubtype);
1320 break;
1321 }
1322 break;
1323 case MachO::CPU_TYPE_ARM:
1324 switch (cpusubtype) {
1325 case MachO::CPU_SUBTYPE_ARM_ALL:
1326 outs() << " cputype CPU_TYPE_ARM\n";
1327 outs() << " cpusubtype CPU_SUBTYPE_ARM_ALL\n";
1328 break;
1329 case MachO::CPU_SUBTYPE_ARM_V4T:
1330 outs() << " cputype CPU_TYPE_ARM\n";
1331 outs() << " cpusubtype CPU_SUBTYPE_ARM_V4T\n";
1332 break;
1333 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
1334 outs() << " cputype CPU_TYPE_ARM\n";
1335 outs() << " cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n";
1336 break;
1337 case MachO::CPU_SUBTYPE_ARM_XSCALE:
1338 outs() << " cputype CPU_TYPE_ARM\n";
1339 outs() << " cpusubtype CPU_SUBTYPE_ARM_XSCALE\n";
1340 break;
1341 case MachO::CPU_SUBTYPE_ARM_V6:
1342 outs() << " cputype CPU_TYPE_ARM\n";
1343 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6\n";
1344 break;
1345 case MachO::CPU_SUBTYPE_ARM_V6M:
1346 outs() << " cputype CPU_TYPE_ARM\n";
1347 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6M\n";
1348 break;
1349 case MachO::CPU_SUBTYPE_ARM_V7:
1350 outs() << " cputype CPU_TYPE_ARM\n";
1351 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7\n";
1352 break;
1353 case MachO::CPU_SUBTYPE_ARM_V7EM:
1354 outs() << " cputype CPU_TYPE_ARM\n";
1355 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7EM\n";
1356 break;
1357 case MachO::CPU_SUBTYPE_ARM_V7K:
1358 outs() << " cputype CPU_TYPE_ARM\n";
1359 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7K\n";
1360 break;
1361 case MachO::CPU_SUBTYPE_ARM_V7M:
1362 outs() << " cputype CPU_TYPE_ARM\n";
1363 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7M\n";
1364 break;
1365 case MachO::CPU_SUBTYPE_ARM_V7S:
1366 outs() << " cputype CPU_TYPE_ARM\n";
1367 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7S\n";
1368 break;
1369 default:
1370 printUnknownCPUType(cputype, cpusubtype);
1371 break;
1372 }
1373 break;
1374 case MachO::CPU_TYPE_ARM64:
1375 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1376 case MachO::CPU_SUBTYPE_ARM64_ALL:
1377 outs() << " cputype CPU_TYPE_ARM64\n";
1378 outs() << " cpusubtype CPU_SUBTYPE_ARM64_ALL\n";
1379 break;
1380 default:
1381 printUnknownCPUType(cputype, cpusubtype);
1382 break;
1383 }
1384 break;
1385 default:
1386 printUnknownCPUType(cputype, cpusubtype);
1387 break;
1388 }
1389}
1390
1391static void printMachOUniversalHeaders(const object::MachOUniversalBinary *UB,
1392 bool verbose) {
1393 outs() << "Fat headers\n";
Kevin Enderby606a3382016-06-21 21:55:01 +00001394 if (verbose) {
1395 if (UB->getMagic() == MachO::FAT_MAGIC)
1396 outs() << "fat_magic FAT_MAGIC\n";
1397 else // UB->getMagic() == MachO::FAT_MAGIC_64
1398 outs() << "fat_magic FAT_MAGIC_64\n";
1399 } else
Kevin Enderby131d1772015-01-09 19:22:37 +00001400 outs() << "fat_magic " << format("0x%" PRIx32, MachO::FAT_MAGIC) << "\n";
1401
1402 uint32_t nfat_arch = UB->getNumberOfObjects();
1403 StringRef Buf = UB->getData();
1404 uint64_t size = Buf.size();
1405 uint64_t big_size = sizeof(struct MachO::fat_header) +
1406 nfat_arch * sizeof(struct MachO::fat_arch);
1407 outs() << "nfat_arch " << UB->getNumberOfObjects();
1408 if (nfat_arch == 0)
1409 outs() << " (malformed, contains zero architecture types)\n";
1410 else if (big_size > size)
1411 outs() << " (malformed, architectures past end of file)\n";
1412 else
1413 outs() << "\n";
1414
1415 for (uint32_t i = 0; i < nfat_arch; ++i) {
1416 MachOUniversalBinary::ObjectForArch OFA(UB, i);
1417 uint32_t cputype = OFA.getCPUType();
1418 uint32_t cpusubtype = OFA.getCPUSubType();
1419 outs() << "architecture ";
1420 for (uint32_t j = 0; i != 0 && j <= i - 1; j++) {
1421 MachOUniversalBinary::ObjectForArch other_OFA(UB, j);
1422 uint32_t other_cputype = other_OFA.getCPUType();
1423 uint32_t other_cpusubtype = other_OFA.getCPUSubType();
Kevin Enderby0512bd72015-01-09 21:55:03 +00001424 if (cputype != 0 && cpusubtype != 0 && cputype == other_cputype &&
Kevin Enderby131d1772015-01-09 19:22:37 +00001425 (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) ==
Kevin Enderby0512bd72015-01-09 21:55:03 +00001426 (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK)) {
Kevin Enderby131d1772015-01-09 19:22:37 +00001427 outs() << "(illegal duplicate architecture) ";
1428 break;
Kevin Enderby0512bd72015-01-09 21:55:03 +00001429 }
Kevin Enderby131d1772015-01-09 19:22:37 +00001430 }
1431 if (verbose) {
Kevin Enderby59343a92016-12-16 22:54:02 +00001432 outs() << OFA.getArchFlagName() << "\n";
Kevin Enderby131d1772015-01-09 19:22:37 +00001433 printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1434 } else {
1435 outs() << i << "\n";
1436 outs() << " cputype " << cputype << "\n";
1437 outs() << " cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK)
1438 << "\n";
1439 }
1440 if (verbose &&
1441 (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64)
1442 outs() << " capabilities CPU_SUBTYPE_LIB64\n";
1443 else
1444 outs() << " capabilities "
1445 << format("0x%" PRIx32,
1446 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n";
1447 outs() << " offset " << OFA.getOffset();
1448 if (OFA.getOffset() > size)
1449 outs() << " (past end of file)";
1450 if (OFA.getOffset() % (1 << OFA.getAlign()) != 0)
1451 outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")";
1452 outs() << "\n";
1453 outs() << " size " << OFA.getSize();
1454 big_size = OFA.getOffset() + OFA.getSize();
1455 if (big_size > size)
1456 outs() << " (past end of file)";
1457 outs() << "\n";
1458 outs() << " align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign())
1459 << ")\n";
1460 }
1461}
1462
Kevin Enderby6524bd82016-07-19 20:47:07 +00001463static void printArchiveChild(StringRef Filename, const Archive::Child &C,
1464 bool verbose, bool print_offset,
1465 StringRef ArchitectureName = StringRef()) {
Kevin Enderby13023a12015-01-15 23:19:11 +00001466 if (print_offset)
1467 outs() << C.getChildOffset() << "\t";
Vedant Kumar4031d9f2016-08-03 19:02:50 +00001468 Expected<sys::fs::perms> ModeOrErr = C.getAccessMode();
1469 if (!ModeOrErr)
1470 report_error(Filename, C, ModeOrErr.takeError(), ArchitectureName);
1471 sys::fs::perms Mode = ModeOrErr.get();
Kevin Enderby13023a12015-01-15 23:19:11 +00001472 if (verbose) {
1473 // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
1474 // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
1475 outs() << "-";
Davide Italianobb9a6cc2015-09-07 20:47:03 +00001476 outs() << ((Mode & sys::fs::owner_read) ? "r" : "-");
1477 outs() << ((Mode & sys::fs::owner_write) ? "w" : "-");
1478 outs() << ((Mode & sys::fs::owner_exe) ? "x" : "-");
1479 outs() << ((Mode & sys::fs::group_read) ? "r" : "-");
1480 outs() << ((Mode & sys::fs::group_write) ? "w" : "-");
1481 outs() << ((Mode & sys::fs::group_exe) ? "x" : "-");
1482 outs() << ((Mode & sys::fs::others_read) ? "r" : "-");
1483 outs() << ((Mode & sys::fs::others_write) ? "w" : "-");
1484 outs() << ((Mode & sys::fs::others_exe) ? "x" : "-");
Kevin Enderby13023a12015-01-15 23:19:11 +00001485 } else {
1486 outs() << format("0%o ", Mode);
1487 }
1488
Vedant Kumar4031d9f2016-08-03 19:02:50 +00001489 Expected<unsigned> UIDOrErr = C.getUID();
1490 if (!UIDOrErr)
1491 report_error(Filename, C, UIDOrErr.takeError(), ArchitectureName);
1492 unsigned UID = UIDOrErr.get();
Kevin Enderby13023a12015-01-15 23:19:11 +00001493 outs() << format("%3d/", UID);
Vedant Kumar4031d9f2016-08-03 19:02:50 +00001494 Expected<unsigned> GIDOrErr = C.getGID();
1495 if (!GIDOrErr)
1496 report_error(Filename, C, GIDOrErr.takeError(), ArchitectureName);
1497 unsigned GID = GIDOrErr.get();
Kevin Enderby13023a12015-01-15 23:19:11 +00001498 outs() << format("%-3d ", GID);
Kevin Enderby6524bd82016-07-19 20:47:07 +00001499 Expected<uint64_t> Size = C.getRawSize();
1500 if (!Size)
1501 report_error(Filename, C, Size.takeError(), ArchitectureName);
Kevin Enderby7a969422015-11-05 19:24:56 +00001502 outs() << format("%5" PRId64, Size.get()) << " ";
Kevin Enderby13023a12015-01-15 23:19:11 +00001503
1504 StringRef RawLastModified = C.getRawLastModified();
1505 if (verbose) {
1506 unsigned Seconds;
1507 if (RawLastModified.getAsInteger(10, Seconds))
Vedant Kumar4031d9f2016-08-03 19:02:50 +00001508 outs() << "(date: \"" << RawLastModified
1509 << "\" contains non-decimal chars) ";
Kevin Enderby13023a12015-01-15 23:19:11 +00001510 else {
1511 // Since cime(3) returns a 26 character string of the form:
1512 // "Sun Sep 16 01:03:52 1973\n\0"
1513 // just print 24 characters.
1514 time_t t = Seconds;
1515 outs() << format("%.24s ", ctime(&t));
1516 }
1517 } else {
1518 outs() << RawLastModified << " ";
1519 }
1520
1521 if (verbose) {
Kevin Enderbyf4586032016-07-29 17:44:13 +00001522 Expected<StringRef> NameOrErr = C.getName();
1523 if (!NameOrErr) {
1524 consumeError(NameOrErr.takeError());
1525 Expected<StringRef> NameOrErr = C.getRawName();
1526 if (!NameOrErr)
1527 report_error(Filename, C, NameOrErr.takeError(), ArchitectureName);
1528 StringRef RawName = NameOrErr.get();
Kevin Enderby13023a12015-01-15 23:19:11 +00001529 outs() << RawName << "\n";
1530 } else {
1531 StringRef Name = NameOrErr.get();
1532 outs() << Name << "\n";
1533 }
1534 } else {
Kevin Enderbyf4586032016-07-29 17:44:13 +00001535 Expected<StringRef> NameOrErr = C.getRawName();
1536 if (!NameOrErr)
1537 report_error(Filename, C, NameOrErr.takeError(), ArchitectureName);
1538 StringRef RawName = NameOrErr.get();
Kevin Enderby13023a12015-01-15 23:19:11 +00001539 outs() << RawName << "\n";
1540 }
1541}
1542
Kevin Enderby6524bd82016-07-19 20:47:07 +00001543static void printArchiveHeaders(StringRef Filename, Archive *A, bool verbose,
1544 bool print_offset,
1545 StringRef ArchitectureName = StringRef()) {
Mehdi Amini41af4302016-11-11 04:28:40 +00001546 Error Err = Error::success();
1547 ;
Lang Hamesfc209622016-07-14 02:24:01 +00001548 for (const auto &C : A->children(Err, false))
Kevin Enderby6524bd82016-07-19 20:47:07 +00001549 printArchiveChild(Filename, C, verbose, print_offset, ArchitectureName);
1550
Lang Hamesfc209622016-07-14 02:24:01 +00001551 if (Err)
Kevin Enderby844c4ac2016-11-15 23:07:41 +00001552 report_error(StringRef(), Filename, std::move(Err), ArchitectureName);
Kevin Enderby13023a12015-01-15 23:19:11 +00001553}
1554
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001555// ParseInputMachO() parses the named Mach-O file in Filename and handles the
1556// -arch flags selecting just those slices as specified by them and also parses
1557// archive files. Then for each individual Mach-O file ProcessMachO() is
1558// called to process the file based on the command line options.
1559void llvm::ParseInputMachO(StringRef Filename) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001560 // Check for -arch all and verifiy the -arch flags are valid.
1561 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1562 if (ArchFlags[i] == "all") {
1563 ArchAll = true;
1564 } else {
1565 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
1566 errs() << "llvm-objdump: Unknown architecture named '" + ArchFlags[i] +
1567 "'for the -arch option\n";
1568 return;
1569 }
1570 }
1571 }
1572
1573 // Attempt to open the binary.
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +00001574 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename);
Kevin Enderby98898f22017-01-30 20:53:17 +00001575 if (!BinaryOrErr) {
1576 if (auto E = isNotObjectErrorInvalidFileType(BinaryOrErr.takeError()))
1577 report_error(Filename, std::move(E));
1578 else
1579 outs() << Filename << ": is not an object file\n";
1580 return;
1581 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001582 Binary &Bin = *BinaryOrErr.get().getBinary();
Kevin Enderby3f0ffab2014-12-03 22:29:40 +00001583
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001584 if (Archive *A = dyn_cast<Archive>(&Bin)) {
1585 outs() << "Archive : " << Filename << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001586 if (ArchiveHeaders)
Kevin Enderby6524bd82016-07-19 20:47:07 +00001587 printArchiveHeaders(Filename, A, !NonVerbose, ArchiveMemberOffsets);
1588
Mehdi Amini41af4302016-11-11 04:28:40 +00001589 Error Err = Error::success();
Lang Hamesfc209622016-07-14 02:24:01 +00001590 for (auto &C : A->children(Err)) {
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001591 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1592 if (!ChildOrErr) {
1593 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
1594 report_error(Filename, C, std::move(E));
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001595 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001596 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001597 if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1598 if (!checkMachOAndArchFlags(O, Filename))
1599 return;
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001600 ProcessMachO(Filename, O, O->getFileName());
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001601 }
1602 }
Lang Hamesfc209622016-07-14 02:24:01 +00001603 if (Err)
1604 report_error(Filename, std::move(Err));
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001605 return;
1606 }
Kevin Enderby131d1772015-01-09 19:22:37 +00001607 if (UniversalHeaders) {
1608 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin))
Kevin Enderbyf0640752015-03-13 17:56:32 +00001609 printMachOUniversalHeaders(UB, !NonVerbose);
Kevin Enderby131d1772015-01-09 19:22:37 +00001610 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001611 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
1612 // If we have a list of architecture flags specified dump only those.
1613 if (!ArchAll && ArchFlags.size() != 0) {
1614 // Look for a slice in the universal binary that matches each ArchFlag.
1615 bool ArchFound;
1616 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1617 ArchFound = false;
1618 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1619 E = UB->end_objects();
1620 I != E; ++I) {
Kevin Enderby59343a92016-12-16 22:54:02 +00001621 if (ArchFlags[i] == I->getArchFlagName()) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001622 ArchFound = true;
Kevin Enderby9acb1092016-05-31 20:35:34 +00001623 Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001624 I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001625 std::string ArchitectureName = "";
1626 if (ArchFlags.size() > 1)
Kevin Enderby59343a92016-12-16 22:54:02 +00001627 ArchitectureName = I->getArchFlagName();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001628 if (ObjOrErr) {
1629 ObjectFile &O = *ObjOrErr.get();
1630 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001631 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
Kevin Enderby9acb1092016-05-31 20:35:34 +00001632 } else if (auto E = isNotObjectErrorInvalidFileType(
1633 ObjOrErr.takeError())) {
1634 report_error(Filename, StringRef(), std::move(E),
1635 ArchitectureName);
1636 continue;
Kevin Enderby42398052016-06-28 23:16:13 +00001637 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001638 I->getAsArchive()) {
1639 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001640 outs() << "Archive : " << Filename;
1641 if (!ArchitectureName.empty())
1642 outs() << " (architecture " << ArchitectureName << ")";
1643 outs() << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001644 if (ArchiveHeaders)
Kevin Enderby6524bd82016-07-19 20:47:07 +00001645 printArchiveHeaders(Filename, A.get(), !NonVerbose,
1646 ArchiveMemberOffsets, ArchitectureName);
Mehdi Amini41af4302016-11-11 04:28:40 +00001647 Error Err = Error::success();
Lang Hamesfc209622016-07-14 02:24:01 +00001648 for (auto &C : A->children(Err)) {
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001649 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1650 if (!ChildOrErr) {
1651 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
Kevin Enderby9acb1092016-05-31 20:35:34 +00001652 report_error(Filename, C, std::move(E), ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001653 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001654 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001655 if (MachOObjectFile *O =
1656 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001657 ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001658 }
Lang Hamesfc209622016-07-14 02:24:01 +00001659 if (Err)
1660 report_error(Filename, std::move(Err));
Kevin Enderby42398052016-06-28 23:16:13 +00001661 } else {
1662 consumeError(AOrErr.takeError());
1663 error("Mach-O universal file: " + Filename + " for " +
Kevin Enderby59343a92016-12-16 22:54:02 +00001664 "architecture " + StringRef(I->getArchFlagName()) +
Kevin Enderby42398052016-06-28 23:16:13 +00001665 " is not a Mach-O file or an archive file");
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001666 }
1667 }
1668 }
1669 if (!ArchFound) {
1670 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1671 << "architecture: " + ArchFlags[i] + "\n";
1672 return;
1673 }
1674 }
1675 return;
1676 }
1677 // No architecture flags were specified so if this contains a slice that
1678 // matches the host architecture dump only that.
1679 if (!ArchAll) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001680 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1681 E = UB->end_objects();
1682 I != E; ++I) {
Kevin Enderby0512bd72015-01-09 21:55:03 +00001683 if (MachOObjectFile::getHostArch().getArchName() ==
Kevin Enderby59343a92016-12-16 22:54:02 +00001684 I->getArchFlagName()) {
Kevin Enderby9acb1092016-05-31 20:35:34 +00001685 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001686 std::string ArchiveName;
1687 ArchiveName.clear();
1688 if (ObjOrErr) {
1689 ObjectFile &O = *ObjOrErr.get();
1690 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001691 ProcessMachO(Filename, MachOOF);
Kevin Enderby9acb1092016-05-31 20:35:34 +00001692 } else if (auto E = isNotObjectErrorInvalidFileType(
1693 ObjOrErr.takeError())) {
1694 report_error(Filename, std::move(E));
1695 continue;
Kevin Enderby42398052016-06-28 23:16:13 +00001696 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001697 I->getAsArchive()) {
1698 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001699 outs() << "Archive : " << Filename << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001700 if (ArchiveHeaders)
Kevin Enderby6524bd82016-07-19 20:47:07 +00001701 printArchiveHeaders(Filename, A.get(), !NonVerbose,
1702 ArchiveMemberOffsets);
Mehdi Amini41af4302016-11-11 04:28:40 +00001703 Error Err = Error::success();
Lang Hamesfc209622016-07-14 02:24:01 +00001704 for (auto &C : A->children(Err)) {
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001705 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1706 if (!ChildOrErr) {
1707 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
1708 report_error(Filename, C, std::move(E));
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001709 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001710 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001711 if (MachOObjectFile *O =
1712 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001713 ProcessMachO(Filename, O, O->getFileName());
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001714 }
Lang Hamesfc209622016-07-14 02:24:01 +00001715 if (Err)
1716 report_error(Filename, std::move(Err));
Kevin Enderby42398052016-06-28 23:16:13 +00001717 } else {
1718 consumeError(AOrErr.takeError());
1719 error("Mach-O universal file: " + Filename + " for architecture " +
Kevin Enderby59343a92016-12-16 22:54:02 +00001720 StringRef(I->getArchFlagName()) +
Kevin Enderby42398052016-06-28 23:16:13 +00001721 " is not a Mach-O file or an archive file");
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001722 }
1723 return;
1724 }
1725 }
1726 }
1727 // Either all architectures have been specified or none have been specified
1728 // and this does not contain the host architecture so dump all the slices.
1729 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
1730 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1731 E = UB->end_objects();
1732 I != E; ++I) {
Kevin Enderby9acb1092016-05-31 20:35:34 +00001733 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001734 std::string ArchitectureName = "";
1735 if (moreThanOneArch)
Kevin Enderby59343a92016-12-16 22:54:02 +00001736 ArchitectureName = I->getArchFlagName();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001737 if (ObjOrErr) {
1738 ObjectFile &Obj = *ObjOrErr.get();
1739 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001740 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
Kevin Enderby9acb1092016-05-31 20:35:34 +00001741 } else if (auto E = isNotObjectErrorInvalidFileType(
1742 ObjOrErr.takeError())) {
1743 report_error(StringRef(), Filename, std::move(E), ArchitectureName);
1744 continue;
Kevin Enderby42398052016-06-28 23:16:13 +00001745 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
1746 I->getAsArchive()) {
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001747 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001748 outs() << "Archive : " << Filename;
1749 if (!ArchitectureName.empty())
1750 outs() << " (architecture " << ArchitectureName << ")";
1751 outs() << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001752 if (ArchiveHeaders)
Kevin Enderby6524bd82016-07-19 20:47:07 +00001753 printArchiveHeaders(Filename, A.get(), !NonVerbose,
1754 ArchiveMemberOffsets, ArchitectureName);
Mehdi Amini41af4302016-11-11 04:28:40 +00001755 Error Err = Error::success();
Lang Hamesfc209622016-07-14 02:24:01 +00001756 for (auto &C : A->children(Err)) {
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001757 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1758 if (!ChildOrErr) {
1759 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
Kevin Enderby9acb1092016-05-31 20:35:34 +00001760 report_error(Filename, C, std::move(E), ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001761 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001762 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001763 if (MachOObjectFile *O =
1764 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1765 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001766 ProcessMachO(Filename, MachOOF, MachOOF->getFileName(),
1767 ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001768 }
1769 }
Lang Hamesfc209622016-07-14 02:24:01 +00001770 if (Err)
1771 report_error(Filename, std::move(Err));
Kevin Enderby42398052016-06-28 23:16:13 +00001772 } else {
1773 consumeError(AOrErr.takeError());
1774 error("Mach-O universal file: " + Filename + " for architecture " +
Kevin Enderby59343a92016-12-16 22:54:02 +00001775 StringRef(I->getArchFlagName()) +
Kevin Enderby42398052016-06-28 23:16:13 +00001776 " is not a Mach-O file or an archive file");
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001777 }
1778 }
1779 return;
1780 }
1781 if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) {
1782 if (!checkMachOAndArchFlags(O, Filename))
1783 return;
1784 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O)) {
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001785 ProcessMachO(Filename, MachOOF);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001786 } else
1787 errs() << "llvm-objdump: '" << Filename << "': "
1788 << "Object is not a Mach-O file type.\n";
Davide Italiano25d84582016-01-13 04:11:36 +00001789 return;
1790 }
1791 llvm_unreachable("Input object can't be invalid at this point");
Rafael Espindola9b709252013-04-13 01:45:40 +00001792}
1793
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001794// The block of info used by the Symbolizer call backs.
1795struct DisassembleInfo {
1796 bool verbose;
1797 MachOObjectFile *O;
1798 SectionRef S;
Kevin Enderbybf246f52014-09-24 23:08:22 +00001799 SymbolAddressMap *AddrMap;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00001800 std::vector<SectionRef> *Sections;
1801 const char *class_name;
1802 const char *selector_name;
1803 char *method;
Kevin Enderby04bf6932014-10-28 23:39:46 +00001804 char *demangled_name;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00001805 uint64_t adrp_addr;
1806 uint32_t adrp_inst;
Saleem Abdulrasool1d84d9a2017-01-08 19:14:15 +00001807 std::unique_ptr<SymbolAddressMap> bindtable;
Kevin Enderbyaac75382015-10-08 16:56:35 +00001808 uint32_t depth;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001809};
1810
1811// SymbolizerGetOpInfo() is the operand information call back function.
1812// This is called to get the symbolic information for operand(s) of an
1813// instruction when it is being done. This routine does this from
1814// the relocation information, symbol table, etc. That block of information
1815// is a pointer to the struct DisassembleInfo that was passed when the
1816// disassembler context was created and passed to back to here when
1817// called back by the disassembler for instruction operands that could have
1818// relocation information. The address of the instruction containing operand is
1819// at the Pc parameter. The immediate value the operand has is passed in
1820// op_info->Value and is at Offset past the start of the instruction and has a
1821// byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
1822// LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
1823// names and addends of the symbolic expression to add for the operand. The
1824// value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
1825// information is returned then this function returns 1 else it returns 0.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00001826static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
1827 uint64_t Size, int TagType, void *TagBuf) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001828 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
1829 struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00001830 uint64_t value = op_info->Value;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001831
1832 // Make sure all fields returned are zero if we don't set them.
1833 memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1));
1834 op_info->Value = value;
1835
1836 // If the TagType is not the value 1 which it code knows about or if no
1837 // verbose symbolic information is wanted then just return 0, indicating no
1838 // information is being returned.
David Blaikie33dd45d02015-03-23 18:39:02 +00001839 if (TagType != 1 || !info->verbose)
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001840 return 0;
1841
1842 unsigned int Arch = info->O->getArch();
1843 if (Arch == Triple::x86) {
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001844 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1845 return 0;
Kevin Enderbyd90a4172015-10-10 00:05:01 +00001846 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
1847 // TODO:
1848 // Search the external relocation entries of a fully linked image
1849 // (if any) for an entry that matches this segment offset.
1850 // uint32_t seg_offset = (Pc + Offset);
1851 return 0;
1852 }
1853 // In MH_OBJECT filetypes search the section's relocation entries (if any)
1854 // for an entry for this section offset.
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001855 uint32_t sect_addr = info->S.getAddress();
1856 uint32_t sect_offset = (Pc + Offset) - sect_addr;
1857 bool reloc_found = false;
1858 DataRefImpl Rel;
1859 MachO::any_relocation_info RE;
1860 bool isExtern = false;
1861 SymbolRef Symbol;
1862 bool r_scattered = false;
1863 uint32_t r_value, pair_r_value, r_type;
1864 for (const RelocationRef &Reloc : info->S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00001865 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001866 if (RelocOffset == sect_offset) {
1867 Rel = Reloc.getRawDataRefImpl();
1868 RE = info->O->getRelocation(Rel);
Kevin Enderby3eb73e12014-11-11 19:16:45 +00001869 r_type = info->O->getAnyRelocationType(RE);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001870 r_scattered = info->O->isRelocationScattered(RE);
1871 if (r_scattered) {
1872 r_value = info->O->getScatteredRelocationValue(RE);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001873 if (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1874 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
1875 DataRefImpl RelNext = Rel;
1876 info->O->moveRelocationNext(RelNext);
1877 MachO::any_relocation_info RENext;
1878 RENext = info->O->getRelocation(RelNext);
1879 if (info->O->isRelocationScattered(RENext))
Kevin Enderby930fdc72014-11-06 19:00:13 +00001880 pair_r_value = info->O->getScatteredRelocationValue(RENext);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001881 else
1882 return 0;
1883 }
1884 } else {
1885 isExtern = info->O->getPlainRelocationExternal(RE);
1886 if (isExtern) {
1887 symbol_iterator RelocSym = Reloc.getSymbol();
1888 Symbol = *RelocSym;
1889 }
1890 }
1891 reloc_found = true;
1892 break;
1893 }
1894 }
1895 if (reloc_found && isExtern) {
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00001896 Expected<StringRef> SymName = Symbol.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00001897 if (!SymName)
1898 report_error(info->O->getFileName(), SymName.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001899 const char *name = SymName->data();
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001900 op_info->AddSymbol.Present = 1;
1901 op_info->AddSymbol.Name = name;
1902 // For i386 extern relocation entries the value in the instruction is
1903 // the offset from the symbol, and value is already set in op_info->Value.
1904 return 1;
1905 }
1906 if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1907 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) {
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001908 const char *add = GuessSymbolName(r_value, info->AddrMap);
1909 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001910 uint32_t offset = value - (r_value - pair_r_value);
1911 op_info->AddSymbol.Present = 1;
1912 if (add != nullptr)
1913 op_info->AddSymbol.Name = add;
1914 else
1915 op_info->AddSymbol.Value = r_value;
1916 op_info->SubtractSymbol.Present = 1;
1917 if (sub != nullptr)
1918 op_info->SubtractSymbol.Name = sub;
1919 else
1920 op_info->SubtractSymbol.Value = pair_r_value;
1921 op_info->Value = offset;
1922 return 1;
1923 }
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001924 return 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00001925 }
1926 if (Arch == Triple::x86_64) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001927 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1928 return 0;
Kevin Enderbyabf10f22017-06-22 17:41:22 +00001929 // For non MH_OBJECT types, like MH_KEXT_BUNDLE, Search the external
1930 // relocation entries of a linked image (if any) for an entry that matches
1931 // this segment offset.
Kevin Enderbyd90a4172015-10-10 00:05:01 +00001932 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
Kevin Enderbyabf10f22017-06-22 17:41:22 +00001933 uint64_t seg_offset = Pc + Offset;
1934 bool reloc_found = false;
1935 DataRefImpl Rel;
1936 MachO::any_relocation_info RE;
1937 bool isExtern = false;
1938 SymbolRef Symbol;
1939 for (const RelocationRef &Reloc : info->O->external_relocations()) {
1940 uint64_t RelocOffset = Reloc.getOffset();
1941 if (RelocOffset == seg_offset) {
1942 Rel = Reloc.getRawDataRefImpl();
1943 RE = info->O->getRelocation(Rel);
1944 // external relocation entries should always be external.
1945 isExtern = info->O->getPlainRelocationExternal(RE);
1946 if (isExtern) {
1947 symbol_iterator RelocSym = Reloc.getSymbol();
1948 Symbol = *RelocSym;
1949 }
1950 reloc_found = true;
1951 break;
1952 }
1953 }
1954 if (reloc_found && isExtern) {
1955 // The Value passed in will be adjusted by the Pc if the instruction
1956 // adds the Pc. But for x86_64 external relocation entries the Value
1957 // is the offset from the external symbol.
1958 if (info->O->getAnyRelocationPCRel(RE))
1959 op_info->Value -= Pc + Offset + Size;
1960 Expected<StringRef> SymName = Symbol.getName();
1961 if (!SymName)
1962 report_error(info->O->getFileName(), SymName.takeError());
1963 const char *name = SymName->data();
1964 op_info->AddSymbol.Present = 1;
1965 op_info->AddSymbol.Name = name;
1966 return 1;
1967 }
Kevin Enderbyd90a4172015-10-10 00:05:01 +00001968 return 0;
1969 }
1970 // In MH_OBJECT filetypes search the section's relocation entries (if any)
1971 // for an entry for this section offset.
Rafael Espindola80291272014-10-08 15:28:58 +00001972 uint64_t sect_addr = info->S.getAddress();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001973 uint64_t sect_offset = (Pc + Offset) - sect_addr;
1974 bool reloc_found = false;
1975 DataRefImpl Rel;
1976 MachO::any_relocation_info RE;
1977 bool isExtern = false;
1978 SymbolRef Symbol;
1979 for (const RelocationRef &Reloc : info->S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00001980 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001981 if (RelocOffset == sect_offset) {
1982 Rel = Reloc.getRawDataRefImpl();
1983 RE = info->O->getRelocation(Rel);
1984 // NOTE: Scattered relocations don't exist on x86_64.
1985 isExtern = info->O->getPlainRelocationExternal(RE);
1986 if (isExtern) {
1987 symbol_iterator RelocSym = Reloc.getSymbol();
1988 Symbol = *RelocSym;
1989 }
1990 reloc_found = true;
1991 break;
1992 }
1993 }
1994 if (reloc_found && isExtern) {
1995 // The Value passed in will be adjusted by the Pc if the instruction
1996 // adds the Pc. But for x86_64 external relocation entries the Value
1997 // is the offset from the external symbol.
1998 if (info->O->getAnyRelocationPCRel(RE))
1999 op_info->Value -= Pc + Offset + Size;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002000 Expected<StringRef> SymName = Symbol.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00002001 if (!SymName)
2002 report_error(info->O->getFileName(), SymName.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002003 const char *name = SymName->data();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002004 unsigned Type = info->O->getAnyRelocationType(RE);
2005 if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
2006 DataRefImpl RelNext = Rel;
2007 info->O->moveRelocationNext(RelNext);
2008 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2009 unsigned TypeNext = info->O->getAnyRelocationType(RENext);
2010 bool isExternNext = info->O->getPlainRelocationExternal(RENext);
2011 unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext);
2012 if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) {
2013 op_info->SubtractSymbol.Present = 1;
2014 op_info->SubtractSymbol.Name = name;
2015 symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
2016 Symbol = *RelocSymNext;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002017 Expected<StringRef> SymNameNext = Symbol.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00002018 if (!SymNameNext)
2019 report_error(info->O->getFileName(), SymNameNext.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002020 name = SymNameNext->data();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002021 }
2022 }
2023 // TODO: add the VariantKinds to op_info->VariantKind for relocation types
2024 // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
2025 op_info->AddSymbol.Present = 1;
2026 op_info->AddSymbol.Name = name;
2027 return 1;
2028 }
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002029 return 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00002030 }
2031 if (Arch == Triple::arm) {
Kevin Enderby930fdc72014-11-06 19:00:13 +00002032 if (Offset != 0 || (Size != 4 && Size != 2))
2033 return 0;
Kevin Enderbyd90a4172015-10-10 00:05:01 +00002034 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2035 // TODO:
2036 // Search the external relocation entries of a fully linked image
2037 // (if any) for an entry that matches this segment offset.
2038 // uint32_t seg_offset = (Pc + Offset);
2039 return 0;
2040 }
2041 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2042 // for an entry for this section offset.
Kevin Enderby930fdc72014-11-06 19:00:13 +00002043 uint32_t sect_addr = info->S.getAddress();
2044 uint32_t sect_offset = (Pc + Offset) - sect_addr;
Kevin Enderby930fdc72014-11-06 19:00:13 +00002045 DataRefImpl Rel;
2046 MachO::any_relocation_info RE;
2047 bool isExtern = false;
2048 SymbolRef Symbol;
2049 bool r_scattered = false;
2050 uint32_t r_value, pair_r_value, r_type, r_length, other_half;
David Blaikie33dd45d02015-03-23 18:39:02 +00002051 auto Reloc =
David Majnemer562e8292016-08-12 00:18:03 +00002052 find_if(info->S.relocations(), [&](const RelocationRef &Reloc) {
2053 uint64_t RelocOffset = Reloc.getOffset();
2054 return RelocOffset == sect_offset;
2055 });
David Blaikie33dd45d02015-03-23 18:39:02 +00002056
2057 if (Reloc == info->S.relocations().end())
2058 return 0;
2059
2060 Rel = Reloc->getRawDataRefImpl();
2061 RE = info->O->getRelocation(Rel);
2062 r_length = info->O->getAnyRelocationLength(RE);
2063 r_scattered = info->O->isRelocationScattered(RE);
2064 if (r_scattered) {
2065 r_value = info->O->getScatteredRelocationValue(RE);
2066 r_type = info->O->getScatteredRelocationType(RE);
2067 } else {
2068 r_type = info->O->getAnyRelocationType(RE);
2069 isExtern = info->O->getPlainRelocationExternal(RE);
2070 if (isExtern) {
2071 symbol_iterator RelocSym = Reloc->getSymbol();
2072 Symbol = *RelocSym;
Kevin Enderby930fdc72014-11-06 19:00:13 +00002073 }
2074 }
David Blaikie33dd45d02015-03-23 18:39:02 +00002075 if (r_type == MachO::ARM_RELOC_HALF ||
2076 r_type == MachO::ARM_RELOC_SECTDIFF ||
2077 r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
2078 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2079 DataRefImpl RelNext = Rel;
2080 info->O->moveRelocationNext(RelNext);
2081 MachO::any_relocation_info RENext;
2082 RENext = info->O->getRelocation(RelNext);
2083 other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff;
2084 if (info->O->isRelocationScattered(RENext))
2085 pair_r_value = info->O->getScatteredRelocationValue(RENext);
2086 }
2087
2088 if (isExtern) {
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002089 Expected<StringRef> SymName = Symbol.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00002090 if (!SymName)
2091 report_error(info->O->getFileName(), SymName.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002092 const char *name = SymName->data();
Kevin Enderby930fdc72014-11-06 19:00:13 +00002093 op_info->AddSymbol.Present = 1;
2094 op_info->AddSymbol.Name = name;
Sylvestre Ledru648cced2015-02-05 17:00:23 +00002095 switch (r_type) {
2096 case MachO::ARM_RELOC_HALF:
2097 if ((r_length & 0x1) == 1) {
2098 op_info->Value = value << 16 | other_half;
2099 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2100 } else {
2101 op_info->Value = other_half << 16 | value;
2102 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Sylvestre Ledrufe0c7ad2015-02-05 16:35:44 +00002103 }
Sylvestre Ledru648cced2015-02-05 17:00:23 +00002104 break;
2105 default:
2106 break;
Kevin Enderby930fdc72014-11-06 19:00:13 +00002107 }
2108 return 1;
2109 }
2110 // If we have a branch that is not an external relocation entry then
2111 // return 0 so the code in tryAddingSymbolicOperand() can use the
2112 // SymbolLookUp call back with the branch target address to look up the
Simon Pilgrimdae11f72016-11-20 13:31:13 +00002113 // symbol and possibility add an annotation for a symbol stub.
David Blaikie33dd45d02015-03-23 18:39:02 +00002114 if (isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 ||
2115 r_type == MachO::ARM_THUMB_RELOC_BR22))
Kevin Enderby930fdc72014-11-06 19:00:13 +00002116 return 0;
2117
2118 uint32_t offset = 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00002119 if (r_type == MachO::ARM_RELOC_HALF ||
2120 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2121 if ((r_length & 0x1) == 1)
2122 value = value << 16 | other_half;
2123 else
2124 value = other_half << 16 | value;
2125 }
2126 if (r_scattered && (r_type != MachO::ARM_RELOC_HALF &&
2127 r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) {
2128 offset = value - r_value;
2129 value = r_value;
Kevin Enderby930fdc72014-11-06 19:00:13 +00002130 }
2131
David Blaikie33dd45d02015-03-23 18:39:02 +00002132 if (r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
Kevin Enderby930fdc72014-11-06 19:00:13 +00002133 if ((r_length & 0x1) == 1)
2134 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2135 else
2136 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00002137 const char *add = GuessSymbolName(r_value, info->AddrMap);
2138 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
Kevin Enderby930fdc72014-11-06 19:00:13 +00002139 int32_t offset = value - (r_value - pair_r_value);
2140 op_info->AddSymbol.Present = 1;
2141 if (add != nullptr)
2142 op_info->AddSymbol.Name = add;
2143 else
2144 op_info->AddSymbol.Value = r_value;
2145 op_info->SubtractSymbol.Present = 1;
2146 if (sub != nullptr)
2147 op_info->SubtractSymbol.Name = sub;
2148 else
2149 op_info->SubtractSymbol.Value = pair_r_value;
2150 op_info->Value = offset;
2151 return 1;
2152 }
2153
Kevin Enderby930fdc72014-11-06 19:00:13 +00002154 op_info->AddSymbol.Present = 1;
2155 op_info->Value = offset;
David Blaikie33dd45d02015-03-23 18:39:02 +00002156 if (r_type == MachO::ARM_RELOC_HALF) {
2157 if ((r_length & 0x1) == 1)
2158 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2159 else
2160 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Kevin Enderby930fdc72014-11-06 19:00:13 +00002161 }
Kevin Enderbyf6d25852015-01-31 00:37:11 +00002162 const char *add = GuessSymbolName(value, info->AddrMap);
Kevin Enderby930fdc72014-11-06 19:00:13 +00002163 if (add != nullptr) {
2164 op_info->AddSymbol.Name = add;
2165 return 1;
2166 }
2167 op_info->AddSymbol.Value = value;
2168 return 1;
David Blaikie33dd45d02015-03-23 18:39:02 +00002169 }
2170 if (Arch == Triple::aarch64) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002171 if (Offset != 0 || Size != 4)
2172 return 0;
Kevin Enderbyd90a4172015-10-10 00:05:01 +00002173 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2174 // TODO:
2175 // Search the external relocation entries of a fully linked image
2176 // (if any) for an entry that matches this segment offset.
2177 // uint64_t seg_offset = (Pc + Offset);
2178 return 0;
2179 }
2180 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2181 // for an entry for this section offset.
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002182 uint64_t sect_addr = info->S.getAddress();
2183 uint64_t sect_offset = (Pc + Offset) - sect_addr;
David Blaikie33dd45d02015-03-23 18:39:02 +00002184 auto Reloc =
David Majnemer562e8292016-08-12 00:18:03 +00002185 find_if(info->S.relocations(), [&](const RelocationRef &Reloc) {
2186 uint64_t RelocOffset = Reloc.getOffset();
2187 return RelocOffset == sect_offset;
2188 });
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002189
David Blaikie33dd45d02015-03-23 18:39:02 +00002190 if (Reloc == info->S.relocations().end())
2191 return 0;
2192
2193 DataRefImpl Rel = Reloc->getRawDataRefImpl();
2194 MachO::any_relocation_info RE = info->O->getRelocation(Rel);
2195 uint32_t r_type = info->O->getAnyRelocationType(RE);
2196 if (r_type == MachO::ARM64_RELOC_ADDEND) {
2197 DataRefImpl RelNext = Rel;
2198 info->O->moveRelocationNext(RelNext);
2199 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2200 if (value == 0) {
2201 value = info->O->getPlainRelocationSymbolNum(RENext);
2202 op_info->Value = value;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002203 }
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002204 }
David Blaikie33dd45d02015-03-23 18:39:02 +00002205 // NOTE: Scattered relocations don't exist on arm64.
2206 if (!info->O->getPlainRelocationExternal(RE))
2207 return 0;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002208 Expected<StringRef> SymName = Reloc->getSymbol()->getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00002209 if (!SymName)
2210 report_error(info->O->getFileName(), SymName.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002211 const char *name = SymName->data();
David Blaikie33dd45d02015-03-23 18:39:02 +00002212 op_info->AddSymbol.Present = 1;
2213 op_info->AddSymbol.Name = name;
2214
2215 switch (r_type) {
2216 case MachO::ARM64_RELOC_PAGE21:
2217 /* @page */
2218 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE;
2219 break;
2220 case MachO::ARM64_RELOC_PAGEOFF12:
2221 /* @pageoff */
2222 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF;
2223 break;
2224 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
2225 /* @gotpage */
2226 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE;
2227 break;
2228 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
2229 /* @gotpageoff */
2230 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF;
2231 break;
2232 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
2233 /* @tvlppage is not implemented in llvm-mc */
2234 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP;
2235 break;
2236 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
2237 /* @tvlppageoff is not implemented in llvm-mc */
2238 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF;
2239 break;
2240 default:
2241 case MachO::ARM64_RELOC_BRANCH26:
2242 op_info->VariantKind = LLVMDisassembler_VariantKind_None;
2243 break;
2244 }
2245 return 1;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002246 }
David Blaikie33dd45d02015-03-23 18:39:02 +00002247 return 0;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002248}
2249
Kevin Enderbybf246f52014-09-24 23:08:22 +00002250// GuessCstringPointer is passed the address of what might be a pointer to a
2251// literal string in a cstring section. If that address is in a cstring section
2252// it returns a pointer to that string. Else it returns nullptr.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00002253static const char *GuessCstringPointer(uint64_t ReferenceValue,
2254 struct DisassembleInfo *info) {
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002255 for (const auto &Load : info->O->load_commands()) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00002256 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2257 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2258 for (unsigned J = 0; J < Seg.nsects; ++J) {
2259 MachO::section_64 Sec = info->O->getSection64(Load, J);
2260 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2261 if (section_type == MachO::S_CSTRING_LITERALS &&
2262 ReferenceValue >= Sec.addr &&
2263 ReferenceValue < Sec.addr + Sec.size) {
2264 uint64_t sect_offset = ReferenceValue - Sec.addr;
2265 uint64_t object_offset = Sec.offset + sect_offset;
2266 StringRef MachOContents = info->O->getData();
2267 uint64_t object_size = MachOContents.size();
2268 const char *object_addr = (const char *)MachOContents.data();
2269 if (object_offset < object_size) {
2270 const char *name = object_addr + object_offset;
2271 return name;
2272 } else {
2273 return nullptr;
2274 }
2275 }
2276 }
2277 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2278 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2279 for (unsigned J = 0; J < Seg.nsects; ++J) {
2280 MachO::section Sec = info->O->getSection(Load, J);
2281 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2282 if (section_type == MachO::S_CSTRING_LITERALS &&
2283 ReferenceValue >= Sec.addr &&
2284 ReferenceValue < Sec.addr + Sec.size) {
2285 uint64_t sect_offset = ReferenceValue - Sec.addr;
2286 uint64_t object_offset = Sec.offset + sect_offset;
2287 StringRef MachOContents = info->O->getData();
2288 uint64_t object_size = MachOContents.size();
2289 const char *object_addr = (const char *)MachOContents.data();
2290 if (object_offset < object_size) {
2291 const char *name = object_addr + object_offset;
2292 return name;
2293 } else {
2294 return nullptr;
2295 }
2296 }
2297 }
2298 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00002299 }
2300 return nullptr;
2301}
2302
Kevin Enderby85974882014-09-26 22:20:44 +00002303// GuessIndirectSymbol returns the name of the indirect symbol for the
2304// ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe
2305// an address of a symbol stub or a lazy or non-lazy pointer to associate the
2306// symbol name being referenced by the stub or pointer.
2307static const char *GuessIndirectSymbol(uint64_t ReferenceValue,
2308 struct DisassembleInfo *info) {
Kevin Enderby85974882014-09-26 22:20:44 +00002309 MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand();
2310 MachO::symtab_command Symtab = info->O->getSymtabLoadCommand();
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002311 for (const auto &Load : info->O->load_commands()) {
Kevin Enderby85974882014-09-26 22:20:44 +00002312 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2313 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2314 for (unsigned J = 0; J < Seg.nsects; ++J) {
2315 MachO::section_64 Sec = info->O->getSection64(Load, J);
2316 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2317 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2318 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2319 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2320 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2321 section_type == MachO::S_SYMBOL_STUBS) &&
2322 ReferenceValue >= Sec.addr &&
2323 ReferenceValue < Sec.addr + Sec.size) {
2324 uint32_t stride;
2325 if (section_type == MachO::S_SYMBOL_STUBS)
2326 stride = Sec.reserved2;
2327 else
2328 stride = 8;
2329 if (stride == 0)
2330 return nullptr;
2331 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2332 if (index < Dysymtab.nindirectsyms) {
2333 uint32_t indirect_symbol =
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002334 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
Kevin Enderby85974882014-09-26 22:20:44 +00002335 if (indirect_symbol < Symtab.nsyms) {
2336 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2337 SymbolRef Symbol = *Sym;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002338 Expected<StringRef> SymName = Symbol.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00002339 if (!SymName)
2340 report_error(info->O->getFileName(), SymName.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002341 const char *name = SymName->data();
Kevin Enderby85974882014-09-26 22:20:44 +00002342 return name;
2343 }
2344 }
2345 }
2346 }
2347 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2348 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2349 for (unsigned J = 0; J < Seg.nsects; ++J) {
2350 MachO::section Sec = info->O->getSection(Load, J);
2351 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2352 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2353 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2354 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2355 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2356 section_type == MachO::S_SYMBOL_STUBS) &&
2357 ReferenceValue >= Sec.addr &&
2358 ReferenceValue < Sec.addr + Sec.size) {
2359 uint32_t stride;
2360 if (section_type == MachO::S_SYMBOL_STUBS)
2361 stride = Sec.reserved2;
2362 else
2363 stride = 4;
2364 if (stride == 0)
2365 return nullptr;
2366 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2367 if (index < Dysymtab.nindirectsyms) {
2368 uint32_t indirect_symbol =
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002369 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
Kevin Enderby85974882014-09-26 22:20:44 +00002370 if (indirect_symbol < Symtab.nsyms) {
2371 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2372 SymbolRef Symbol = *Sym;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002373 Expected<StringRef> SymName = Symbol.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00002374 if (!SymName)
2375 report_error(info->O->getFileName(), SymName.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002376 const char *name = SymName->data();
Kevin Enderby85974882014-09-26 22:20:44 +00002377 return name;
2378 }
2379 }
2380 }
2381 }
2382 }
Kevin Enderby85974882014-09-26 22:20:44 +00002383 }
2384 return nullptr;
2385}
2386
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002387// method_reference() is called passing it the ReferenceName that might be
2388// a reference it to an Objective-C method call. If so then it allocates and
2389// assembles a method call string with the values last seen and saved in
2390// the DisassembleInfo's class_name and selector_name fields. This is saved
2391// into the method field of the info and any previous string is free'ed.
2392// Then the class_name field in the info is set to nullptr. The method call
2393// string is set into ReferenceName and ReferenceType is set to
2394// LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call
2395// then both ReferenceType and ReferenceName are left unchanged.
2396static void method_reference(struct DisassembleInfo *info,
2397 uint64_t *ReferenceType,
2398 const char **ReferenceName) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002399 unsigned int Arch = info->O->getArch();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002400 if (*ReferenceName != nullptr) {
2401 if (strcmp(*ReferenceName, "_objc_msgSend") == 0) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002402 if (info->selector_name != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002403 if (info->method != nullptr)
2404 free(info->method);
2405 if (info->class_name != nullptr) {
2406 info->method = (char *)malloc(5 + strlen(info->class_name) +
2407 strlen(info->selector_name));
2408 if (info->method != nullptr) {
2409 strcpy(info->method, "+[");
2410 strcat(info->method, info->class_name);
2411 strcat(info->method, " ");
2412 strcat(info->method, info->selector_name);
2413 strcat(info->method, "]");
2414 *ReferenceName = info->method;
2415 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2416 }
2417 } else {
2418 info->method = (char *)malloc(9 + strlen(info->selector_name));
2419 if (info->method != nullptr) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002420 if (Arch == Triple::x86_64)
2421 strcpy(info->method, "-[%rdi ");
2422 else if (Arch == Triple::aarch64)
2423 strcpy(info->method, "-[x0 ");
2424 else
2425 strcpy(info->method, "-[r? ");
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002426 strcat(info->method, info->selector_name);
2427 strcat(info->method, "]");
2428 *ReferenceName = info->method;
2429 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2430 }
2431 }
2432 info->class_name = nullptr;
2433 }
2434 } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002435 if (info->selector_name != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002436 if (info->method != nullptr)
2437 free(info->method);
2438 info->method = (char *)malloc(17 + strlen(info->selector_name));
2439 if (info->method != nullptr) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002440 if (Arch == Triple::x86_64)
2441 strcpy(info->method, "-[[%rdi super] ");
2442 else if (Arch == Triple::aarch64)
2443 strcpy(info->method, "-[[x0 super] ");
2444 else
2445 strcpy(info->method, "-[[r? super] ");
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002446 strcat(info->method, info->selector_name);
2447 strcat(info->method, "]");
2448 *ReferenceName = info->method;
2449 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2450 }
2451 info->class_name = nullptr;
2452 }
2453 }
2454 }
2455}
2456
2457// GuessPointerPointer() is passed the address of what might be a pointer to
2458// a reference to an Objective-C class, selector, message ref or cfstring.
2459// If so the value of the pointer is returned and one of the booleans are set
2460// to true. If not zero is returned and all the booleans are set to false.
2461static uint64_t GuessPointerPointer(uint64_t ReferenceValue,
2462 struct DisassembleInfo *info,
2463 bool &classref, bool &selref, bool &msgref,
2464 bool &cfstring) {
2465 classref = false;
2466 selref = false;
2467 msgref = false;
2468 cfstring = false;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002469 for (const auto &Load : info->O->load_commands()) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002470 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2471 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2472 for (unsigned J = 0; J < Seg.nsects; ++J) {
2473 MachO::section_64 Sec = info->O->getSection64(Load, J);
2474 if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 ||
2475 strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2476 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 ||
2477 strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 ||
2478 strncmp(Sec.sectname, "__cfstring", 16) == 0) &&
2479 ReferenceValue >= Sec.addr &&
2480 ReferenceValue < Sec.addr + Sec.size) {
2481 uint64_t sect_offset = ReferenceValue - Sec.addr;
2482 uint64_t object_offset = Sec.offset + sect_offset;
2483 StringRef MachOContents = info->O->getData();
2484 uint64_t object_size = MachOContents.size();
2485 const char *object_addr = (const char *)MachOContents.data();
2486 if (object_offset < object_size) {
2487 uint64_t pointer_value;
2488 memcpy(&pointer_value, object_addr + object_offset,
2489 sizeof(uint64_t));
2490 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2491 sys::swapByteOrder(pointer_value);
2492 if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0)
2493 selref = true;
2494 else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2495 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0)
2496 classref = true;
2497 else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 &&
2498 ReferenceValue + 8 < Sec.addr + Sec.size) {
2499 msgref = true;
2500 memcpy(&pointer_value, object_addr + object_offset + 8,
2501 sizeof(uint64_t));
2502 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2503 sys::swapByteOrder(pointer_value);
2504 } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0)
2505 cfstring = true;
2506 return pointer_value;
2507 } else {
2508 return 0;
2509 }
2510 }
2511 }
2512 }
2513 // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002514 }
2515 return 0;
2516}
2517
2518// get_pointer_64 returns a pointer to the bytes in the object file at the
2519// Address from a section in the Mach-O file. And indirectly returns the
2520// offset into the section, number of bytes left in the section past the offset
2521// and which section is was being referenced. If the Address is not in a
2522// section nullptr is returned.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00002523static const char *get_pointer_64(uint64_t Address, uint32_t &offset,
2524 uint32_t &left, SectionRef &S,
Kevin Enderby846c0002015-04-16 17:19:59 +00002525 DisassembleInfo *info,
2526 bool objc_only = false) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002527 offset = 0;
2528 left = 0;
2529 S = SectionRef();
2530 for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) {
2531 uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress();
2532 uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize();
Kevin Enderby46e642f2015-10-08 22:50:55 +00002533 if (SectSize == 0)
2534 continue;
Kevin Enderby846c0002015-04-16 17:19:59 +00002535 if (objc_only) {
2536 StringRef SectName;
2537 ((*(info->Sections))[SectIdx]).getName(SectName);
2538 DataRefImpl Ref = ((*(info->Sections))[SectIdx]).getRawDataRefImpl();
2539 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
2540 if (SegName != "__OBJC" && SectName != "__cstring")
2541 continue;
2542 }
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002543 if (Address >= SectAddress && Address < SectAddress + SectSize) {
2544 S = (*(info->Sections))[SectIdx];
2545 offset = Address - SectAddress;
2546 left = SectSize - offset;
2547 StringRef SectContents;
2548 ((*(info->Sections))[SectIdx]).getContents(SectContents);
2549 return SectContents.data() + offset;
2550 }
2551 }
2552 return nullptr;
2553}
2554
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002555static const char *get_pointer_32(uint32_t Address, uint32_t &offset,
2556 uint32_t &left, SectionRef &S,
Kevin Enderby846c0002015-04-16 17:19:59 +00002557 DisassembleInfo *info,
2558 bool objc_only = false) {
2559 return get_pointer_64(Address, offset, left, S, info, objc_only);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002560}
2561
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002562// get_symbol_64() returns the name of a symbol (or nullptr) and the address of
2563// the symbol indirectly through n_value. Based on the relocation information
2564// for the specified section offset in the specified section reference.
Kevin Enderby0fc11822015-04-01 20:57:01 +00002565// If no relocation information is found and a non-zero ReferenceValue for the
2566// symbol is passed, look up that address in the info's AddrMap.
Rafael Espindolad7a32ea2015-06-24 10:20:30 +00002567static const char *get_symbol_64(uint32_t sect_offset, SectionRef S,
2568 DisassembleInfo *info, uint64_t &n_value,
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +00002569 uint64_t ReferenceValue = 0) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002570 n_value = 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00002571 if (!info->verbose)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002572 return nullptr;
2573
2574 // See if there is an external relocation entry at the sect_offset.
2575 bool reloc_found = false;
2576 DataRefImpl Rel;
2577 MachO::any_relocation_info RE;
2578 bool isExtern = false;
2579 SymbolRef Symbol;
2580 for (const RelocationRef &Reloc : S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00002581 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002582 if (RelocOffset == sect_offset) {
2583 Rel = Reloc.getRawDataRefImpl();
2584 RE = info->O->getRelocation(Rel);
2585 if (info->O->isRelocationScattered(RE))
2586 continue;
2587 isExtern = info->O->getPlainRelocationExternal(RE);
2588 if (isExtern) {
2589 symbol_iterator RelocSym = Reloc.getSymbol();
2590 Symbol = *RelocSym;
2591 }
2592 reloc_found = true;
2593 break;
2594 }
2595 }
2596 // If there is an external relocation entry for a symbol in this section
2597 // at this section_offset then use that symbol's value for the n_value
2598 // and return its name.
2599 const char *SymbolName = nullptr;
2600 if (reloc_found && isExtern) {
Rafael Espindoladea00162015-07-03 17:44:18 +00002601 n_value = Symbol.getValue();
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002602 Expected<StringRef> NameOrError = Symbol.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00002603 if (!NameOrError)
2604 report_error(info->O->getFileName(), NameOrError.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002605 StringRef Name = *NameOrError;
2606 if (!Name.empty()) {
2607 SymbolName = Name.data();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002608 return SymbolName;
2609 }
2610 }
2611
2612 // TODO: For fully linked images, look through the external relocation
2613 // entries off the dynamic symtab command. For these the r_offset is from the
2614 // start of the first writeable segment in the Mach-O file. So the offset
2615 // to this section from that segment is passed to this routine by the caller,
2616 // as the database_offset. Which is the difference of the section's starting
2617 // address and the first writable segment.
2618 //
2619 // NOTE: need add passing the database_offset to this routine.
2620
Kevin Enderby0fc11822015-04-01 20:57:01 +00002621 // We did not find an external relocation entry so look up the ReferenceValue
2622 // as an address of a symbol and if found return that symbol's name.
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +00002623 SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002624
2625 return SymbolName;
2626}
2627
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002628static const char *get_symbol_32(uint32_t sect_offset, SectionRef S,
2629 DisassembleInfo *info,
2630 uint32_t ReferenceValue) {
2631 uint64_t n_value64;
2632 return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue);
2633}
2634
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002635// These are structs in the Objective-C meta data and read to produce the
2636// comments for disassembly. While these are part of the ABI they are no
Zachary Turner264b5d92017-06-07 03:48:56 +00002637// public defintions. So the are here not in include/llvm/BinaryFormat/MachO.h
2638// .
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002639
2640// The cfstring object in a 64-bit Mach-O file.
2641struct cfstring64_t {
2642 uint64_t isa; // class64_t * (64-bit pointer)
2643 uint64_t flags; // flag bits
2644 uint64_t characters; // char * (64-bit pointer)
2645 uint64_t length; // number of non-NULL characters in above
2646};
2647
2648// The class object in a 64-bit Mach-O file.
2649struct class64_t {
2650 uint64_t isa; // class64_t * (64-bit pointer)
2651 uint64_t superclass; // class64_t * (64-bit pointer)
2652 uint64_t cache; // Cache (64-bit pointer)
2653 uint64_t vtable; // IMP * (64-bit pointer)
2654 uint64_t data; // class_ro64_t * (64-bit pointer)
2655};
2656
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002657struct class32_t {
2658 uint32_t isa; /* class32_t * (32-bit pointer) */
2659 uint32_t superclass; /* class32_t * (32-bit pointer) */
2660 uint32_t cache; /* Cache (32-bit pointer) */
2661 uint32_t vtable; /* IMP * (32-bit pointer) */
2662 uint32_t data; /* class_ro32_t * (32-bit pointer) */
2663};
2664
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002665struct class_ro64_t {
2666 uint32_t flags;
2667 uint32_t instanceStart;
2668 uint32_t instanceSize;
2669 uint32_t reserved;
2670 uint64_t ivarLayout; // const uint8_t * (64-bit pointer)
2671 uint64_t name; // const char * (64-bit pointer)
2672 uint64_t baseMethods; // const method_list_t * (64-bit pointer)
2673 uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer)
2674 uint64_t ivars; // const ivar_list_t * (64-bit pointer)
2675 uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer)
2676 uint64_t baseProperties; // const struct objc_property_list (64-bit pointer)
2677};
2678
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002679struct class_ro32_t {
2680 uint32_t flags;
2681 uint32_t instanceStart;
2682 uint32_t instanceSize;
2683 uint32_t ivarLayout; /* const uint8_t * (32-bit pointer) */
2684 uint32_t name; /* const char * (32-bit pointer) */
2685 uint32_t baseMethods; /* const method_list_t * (32-bit pointer) */
2686 uint32_t baseProtocols; /* const protocol_list_t * (32-bit pointer) */
2687 uint32_t ivars; /* const ivar_list_t * (32-bit pointer) */
2688 uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */
2689 uint32_t baseProperties; /* const struct objc_property_list *
2690 (32-bit pointer) */
2691};
2692
2693/* Values for class_ro{64,32}_t->flags */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002694#define RO_META (1 << 0)
2695#define RO_ROOT (1 << 1)
2696#define RO_HAS_CXX_STRUCTORS (1 << 2)
2697
2698struct method_list64_t {
2699 uint32_t entsize;
2700 uint32_t count;
2701 /* struct method64_t first; These structures follow inline */
2702};
2703
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002704struct method_list32_t {
2705 uint32_t entsize;
2706 uint32_t count;
2707 /* struct method32_t first; These structures follow inline */
2708};
2709
Kevin Enderby0fc11822015-04-01 20:57:01 +00002710struct method64_t {
2711 uint64_t name; /* SEL (64-bit pointer) */
2712 uint64_t types; /* const char * (64-bit pointer) */
2713 uint64_t imp; /* IMP (64-bit pointer) */
2714};
2715
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002716struct method32_t {
2717 uint32_t name; /* SEL (32-bit pointer) */
2718 uint32_t types; /* const char * (32-bit pointer) */
2719 uint32_t imp; /* IMP (32-bit pointer) */
2720};
2721
Kevin Enderby0fc11822015-04-01 20:57:01 +00002722struct protocol_list64_t {
2723 uint64_t count; /* uintptr_t (a 64-bit value) */
2724 /* struct protocol64_t * list[0]; These pointers follow inline */
2725};
2726
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002727struct protocol_list32_t {
2728 uint32_t count; /* uintptr_t (a 32-bit value) */
2729 /* struct protocol32_t * list[0]; These pointers follow inline */
2730};
2731
Kevin Enderby0fc11822015-04-01 20:57:01 +00002732struct protocol64_t {
2733 uint64_t isa; /* id * (64-bit pointer) */
2734 uint64_t name; /* const char * (64-bit pointer) */
2735 uint64_t protocols; /* struct protocol_list64_t *
2736 (64-bit pointer) */
2737 uint64_t instanceMethods; /* method_list_t * (64-bit pointer) */
2738 uint64_t classMethods; /* method_list_t * (64-bit pointer) */
2739 uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */
2740 uint64_t optionalClassMethods; /* method_list_t * (64-bit pointer) */
2741 uint64_t instanceProperties; /* struct objc_property_list *
2742 (64-bit pointer) */
2743};
2744
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002745struct protocol32_t {
2746 uint32_t isa; /* id * (32-bit pointer) */
2747 uint32_t name; /* const char * (32-bit pointer) */
2748 uint32_t protocols; /* struct protocol_list_t *
2749 (32-bit pointer) */
2750 uint32_t instanceMethods; /* method_list_t * (32-bit pointer) */
2751 uint32_t classMethods; /* method_list_t * (32-bit pointer) */
2752 uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */
2753 uint32_t optionalClassMethods; /* method_list_t * (32-bit pointer) */
2754 uint32_t instanceProperties; /* struct objc_property_list *
2755 (32-bit pointer) */
2756};
2757
Kevin Enderby0fc11822015-04-01 20:57:01 +00002758struct ivar_list64_t {
2759 uint32_t entsize;
2760 uint32_t count;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002761 /* struct ivar64_t first; These structures follow inline */
2762};
2763
2764struct ivar_list32_t {
2765 uint32_t entsize;
2766 uint32_t count;
2767 /* struct ivar32_t first; These structures follow inline */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002768};
2769
2770struct ivar64_t {
2771 uint64_t offset; /* uintptr_t * (64-bit pointer) */
2772 uint64_t name; /* const char * (64-bit pointer) */
2773 uint64_t type; /* const char * (64-bit pointer) */
2774 uint32_t alignment;
2775 uint32_t size;
2776};
2777
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002778struct ivar32_t {
2779 uint32_t offset; /* uintptr_t * (32-bit pointer) */
2780 uint32_t name; /* const char * (32-bit pointer) */
2781 uint32_t type; /* const char * (32-bit pointer) */
2782 uint32_t alignment;
2783 uint32_t size;
2784};
2785
Kevin Enderby0fc11822015-04-01 20:57:01 +00002786struct objc_property_list64 {
2787 uint32_t entsize;
2788 uint32_t count;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002789 /* struct objc_property64 first; These structures follow inline */
2790};
2791
2792struct objc_property_list32 {
2793 uint32_t entsize;
2794 uint32_t count;
2795 /* struct objc_property32 first; These structures follow inline */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002796};
2797
2798struct objc_property64 {
2799 uint64_t name; /* const char * (64-bit pointer) */
2800 uint64_t attributes; /* const char * (64-bit pointer) */
2801};
2802
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002803struct objc_property32 {
2804 uint32_t name; /* const char * (32-bit pointer) */
2805 uint32_t attributes; /* const char * (32-bit pointer) */
2806};
2807
Kevin Enderby0fc11822015-04-01 20:57:01 +00002808struct category64_t {
2809 uint64_t name; /* const char * (64-bit pointer) */
2810 uint64_t cls; /* struct class_t * (64-bit pointer) */
2811 uint64_t instanceMethods; /* struct method_list_t * (64-bit pointer) */
2812 uint64_t classMethods; /* struct method_list_t * (64-bit pointer) */
2813 uint64_t protocols; /* struct protocol_list_t * (64-bit pointer) */
2814 uint64_t instanceProperties; /* struct objc_property_list *
2815 (64-bit pointer) */
2816};
2817
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002818struct category32_t {
2819 uint32_t name; /* const char * (32-bit pointer) */
2820 uint32_t cls; /* struct class_t * (32-bit pointer) */
2821 uint32_t instanceMethods; /* struct method_list_t * (32-bit pointer) */
2822 uint32_t classMethods; /* struct method_list_t * (32-bit pointer) */
2823 uint32_t protocols; /* struct protocol_list_t * (32-bit pointer) */
2824 uint32_t instanceProperties; /* struct objc_property_list *
2825 (32-bit pointer) */
2826};
2827
Kevin Enderby0fc11822015-04-01 20:57:01 +00002828struct objc_image_info64 {
2829 uint32_t version;
2830 uint32_t flags;
2831};
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002832struct objc_image_info32 {
2833 uint32_t version;
2834 uint32_t flags;
2835};
Kevin Enderby846c0002015-04-16 17:19:59 +00002836struct imageInfo_t {
2837 uint32_t version;
2838 uint32_t flags;
2839};
Kevin Enderby0fc11822015-04-01 20:57:01 +00002840/* masks for objc_image_info.flags */
2841#define OBJC_IMAGE_IS_REPLACEMENT (1 << 0)
2842#define OBJC_IMAGE_SUPPORTS_GC (1 << 1)
2843
2844struct message_ref64 {
2845 uint64_t imp; /* IMP (64-bit pointer) */
2846 uint64_t sel; /* SEL (64-bit pointer) */
2847};
2848
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002849struct message_ref32 {
2850 uint32_t imp; /* IMP (32-bit pointer) */
2851 uint32_t sel; /* SEL (32-bit pointer) */
2852};
2853
Kevin Enderby846c0002015-04-16 17:19:59 +00002854// Objective-C 1 (32-bit only) meta data structs.
2855
2856struct objc_module_t {
2857 uint32_t version;
2858 uint32_t size;
2859 uint32_t name; /* char * (32-bit pointer) */
2860 uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */
2861};
2862
2863struct objc_symtab_t {
2864 uint32_t sel_ref_cnt;
2865 uint32_t refs; /* SEL * (32-bit pointer) */
2866 uint16_t cls_def_cnt;
2867 uint16_t cat_def_cnt;
2868 // uint32_t defs[1]; /* void * (32-bit pointer) variable size */
2869};
2870
2871struct objc_class_t {
2872 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2873 uint32_t super_class; /* struct objc_class * (32-bit pointer) */
2874 uint32_t name; /* const char * (32-bit pointer) */
2875 int32_t version;
2876 int32_t info;
2877 int32_t instance_size;
2878 uint32_t ivars; /* struct objc_ivar_list * (32-bit pointer) */
2879 uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */
2880 uint32_t cache; /* struct objc_cache * (32-bit pointer) */
2881 uint32_t protocols; /* struct objc_protocol_list * (32-bit pointer) */
2882};
2883
2884#define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask))
2885// class is not a metaclass
2886#define CLS_CLASS 0x1
2887// class is a metaclass
2888#define CLS_META 0x2
2889
2890struct objc_category_t {
2891 uint32_t category_name; /* char * (32-bit pointer) */
2892 uint32_t class_name; /* char * (32-bit pointer) */
2893 uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */
2894 uint32_t class_methods; /* struct objc_method_list * (32-bit pointer) */
2895 uint32_t protocols; /* struct objc_protocol_list * (32-bit ptr) */
2896};
2897
2898struct objc_ivar_t {
2899 uint32_t ivar_name; /* char * (32-bit pointer) */
2900 uint32_t ivar_type; /* char * (32-bit pointer) */
2901 int32_t ivar_offset;
2902};
2903
2904struct objc_ivar_list_t {
2905 int32_t ivar_count;
2906 // struct objc_ivar_t ivar_list[1]; /* variable length structure */
2907};
2908
2909struct objc_method_list_t {
2910 uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */
2911 int32_t method_count;
2912 // struct objc_method_t method_list[1]; /* variable length structure */
2913};
2914
2915struct objc_method_t {
2916 uint32_t method_name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2917 uint32_t method_types; /* char * (32-bit pointer) */
2918 uint32_t method_imp; /* IMP, aka function pointer, (*IMP)(id, SEL, ...)
2919 (32-bit pointer) */
2920};
2921
2922struct objc_protocol_list_t {
2923 uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */
2924 int32_t count;
2925 // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t *
2926 // (32-bit pointer) */
2927};
2928
2929struct objc_protocol_t {
2930 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2931 uint32_t protocol_name; /* char * (32-bit pointer) */
2932 uint32_t protocol_list; /* struct objc_protocol_list * (32-bit pointer) */
2933 uint32_t instance_methods; /* struct objc_method_description_list *
2934 (32-bit pointer) */
2935 uint32_t class_methods; /* struct objc_method_description_list *
2936 (32-bit pointer) */
2937};
2938
2939struct objc_method_description_list_t {
2940 int32_t count;
2941 // struct objc_method_description_t list[1];
2942};
2943
2944struct objc_method_description_t {
2945 uint32_t name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2946 uint32_t types; /* char * (32-bit pointer) */
2947};
2948
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002949inline void swapStruct(struct cfstring64_t &cfs) {
2950 sys::swapByteOrder(cfs.isa);
2951 sys::swapByteOrder(cfs.flags);
2952 sys::swapByteOrder(cfs.characters);
2953 sys::swapByteOrder(cfs.length);
2954}
2955
2956inline void swapStruct(struct class64_t &c) {
2957 sys::swapByteOrder(c.isa);
2958 sys::swapByteOrder(c.superclass);
2959 sys::swapByteOrder(c.cache);
2960 sys::swapByteOrder(c.vtable);
2961 sys::swapByteOrder(c.data);
2962}
2963
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002964inline void swapStruct(struct class32_t &c) {
2965 sys::swapByteOrder(c.isa);
2966 sys::swapByteOrder(c.superclass);
2967 sys::swapByteOrder(c.cache);
2968 sys::swapByteOrder(c.vtable);
2969 sys::swapByteOrder(c.data);
2970}
2971
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002972inline void swapStruct(struct class_ro64_t &cro) {
2973 sys::swapByteOrder(cro.flags);
2974 sys::swapByteOrder(cro.instanceStart);
2975 sys::swapByteOrder(cro.instanceSize);
2976 sys::swapByteOrder(cro.reserved);
2977 sys::swapByteOrder(cro.ivarLayout);
2978 sys::swapByteOrder(cro.name);
2979 sys::swapByteOrder(cro.baseMethods);
2980 sys::swapByteOrder(cro.baseProtocols);
2981 sys::swapByteOrder(cro.ivars);
2982 sys::swapByteOrder(cro.weakIvarLayout);
2983 sys::swapByteOrder(cro.baseProperties);
2984}
2985
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002986inline void swapStruct(struct class_ro32_t &cro) {
2987 sys::swapByteOrder(cro.flags);
2988 sys::swapByteOrder(cro.instanceStart);
2989 sys::swapByteOrder(cro.instanceSize);
2990 sys::swapByteOrder(cro.ivarLayout);
2991 sys::swapByteOrder(cro.name);
2992 sys::swapByteOrder(cro.baseMethods);
2993 sys::swapByteOrder(cro.baseProtocols);
2994 sys::swapByteOrder(cro.ivars);
2995 sys::swapByteOrder(cro.weakIvarLayout);
2996 sys::swapByteOrder(cro.baseProperties);
2997}
2998
Kevin Enderby0fc11822015-04-01 20:57:01 +00002999inline void swapStruct(struct method_list64_t &ml) {
3000 sys::swapByteOrder(ml.entsize);
3001 sys::swapByteOrder(ml.count);
3002}
3003
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003004inline void swapStruct(struct method_list32_t &ml) {
3005 sys::swapByteOrder(ml.entsize);
3006 sys::swapByteOrder(ml.count);
3007}
3008
Kevin Enderby0fc11822015-04-01 20:57:01 +00003009inline void swapStruct(struct method64_t &m) {
3010 sys::swapByteOrder(m.name);
3011 sys::swapByteOrder(m.types);
3012 sys::swapByteOrder(m.imp);
3013}
3014
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003015inline void swapStruct(struct method32_t &m) {
3016 sys::swapByteOrder(m.name);
3017 sys::swapByteOrder(m.types);
3018 sys::swapByteOrder(m.imp);
3019}
3020
Kevin Enderby0fc11822015-04-01 20:57:01 +00003021inline void swapStruct(struct protocol_list64_t &pl) {
3022 sys::swapByteOrder(pl.count);
3023}
3024
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003025inline void swapStruct(struct protocol_list32_t &pl) {
3026 sys::swapByteOrder(pl.count);
3027}
3028
Kevin Enderby0fc11822015-04-01 20:57:01 +00003029inline void swapStruct(struct protocol64_t &p) {
3030 sys::swapByteOrder(p.isa);
3031 sys::swapByteOrder(p.name);
3032 sys::swapByteOrder(p.protocols);
3033 sys::swapByteOrder(p.instanceMethods);
3034 sys::swapByteOrder(p.classMethods);
3035 sys::swapByteOrder(p.optionalInstanceMethods);
3036 sys::swapByteOrder(p.optionalClassMethods);
3037 sys::swapByteOrder(p.instanceProperties);
3038}
3039
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003040inline void swapStruct(struct protocol32_t &p) {
3041 sys::swapByteOrder(p.isa);
3042 sys::swapByteOrder(p.name);
3043 sys::swapByteOrder(p.protocols);
3044 sys::swapByteOrder(p.instanceMethods);
3045 sys::swapByteOrder(p.classMethods);
3046 sys::swapByteOrder(p.optionalInstanceMethods);
3047 sys::swapByteOrder(p.optionalClassMethods);
3048 sys::swapByteOrder(p.instanceProperties);
3049}
3050
Kevin Enderby0fc11822015-04-01 20:57:01 +00003051inline void swapStruct(struct ivar_list64_t &il) {
3052 sys::swapByteOrder(il.entsize);
3053 sys::swapByteOrder(il.count);
3054}
3055
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003056inline void swapStruct(struct ivar_list32_t &il) {
3057 sys::swapByteOrder(il.entsize);
3058 sys::swapByteOrder(il.count);
3059}
3060
Kevin Enderby0fc11822015-04-01 20:57:01 +00003061inline void swapStruct(struct ivar64_t &i) {
3062 sys::swapByteOrder(i.offset);
3063 sys::swapByteOrder(i.name);
3064 sys::swapByteOrder(i.type);
3065 sys::swapByteOrder(i.alignment);
3066 sys::swapByteOrder(i.size);
3067}
3068
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003069inline void swapStruct(struct ivar32_t &i) {
3070 sys::swapByteOrder(i.offset);
3071 sys::swapByteOrder(i.name);
3072 sys::swapByteOrder(i.type);
3073 sys::swapByteOrder(i.alignment);
3074 sys::swapByteOrder(i.size);
3075}
3076
Kevin Enderby0fc11822015-04-01 20:57:01 +00003077inline void swapStruct(struct objc_property_list64 &pl) {
3078 sys::swapByteOrder(pl.entsize);
3079 sys::swapByteOrder(pl.count);
3080}
3081
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003082inline void swapStruct(struct objc_property_list32 &pl) {
3083 sys::swapByteOrder(pl.entsize);
3084 sys::swapByteOrder(pl.count);
3085}
3086
Kevin Enderby0fc11822015-04-01 20:57:01 +00003087inline void swapStruct(struct objc_property64 &op) {
3088 sys::swapByteOrder(op.name);
3089 sys::swapByteOrder(op.attributes);
3090}
3091
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003092inline void swapStruct(struct objc_property32 &op) {
3093 sys::swapByteOrder(op.name);
3094 sys::swapByteOrder(op.attributes);
3095}
3096
Kevin Enderby0fc11822015-04-01 20:57:01 +00003097inline void swapStruct(struct category64_t &c) {
3098 sys::swapByteOrder(c.name);
3099 sys::swapByteOrder(c.cls);
3100 sys::swapByteOrder(c.instanceMethods);
3101 sys::swapByteOrder(c.classMethods);
3102 sys::swapByteOrder(c.protocols);
3103 sys::swapByteOrder(c.instanceProperties);
3104}
3105
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003106inline void swapStruct(struct category32_t &c) {
3107 sys::swapByteOrder(c.name);
3108 sys::swapByteOrder(c.cls);
3109 sys::swapByteOrder(c.instanceMethods);
3110 sys::swapByteOrder(c.classMethods);
3111 sys::swapByteOrder(c.protocols);
3112 sys::swapByteOrder(c.instanceProperties);
3113}
3114
Kevin Enderby0fc11822015-04-01 20:57:01 +00003115inline void swapStruct(struct objc_image_info64 &o) {
3116 sys::swapByteOrder(o.version);
3117 sys::swapByteOrder(o.flags);
3118}
3119
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003120inline void swapStruct(struct objc_image_info32 &o) {
3121 sys::swapByteOrder(o.version);
3122 sys::swapByteOrder(o.flags);
3123}
3124
Kevin Enderby846c0002015-04-16 17:19:59 +00003125inline void swapStruct(struct imageInfo_t &o) {
3126 sys::swapByteOrder(o.version);
3127 sys::swapByteOrder(o.flags);
3128}
3129
Kevin Enderby0fc11822015-04-01 20:57:01 +00003130inline void swapStruct(struct message_ref64 &mr) {
3131 sys::swapByteOrder(mr.imp);
3132 sys::swapByteOrder(mr.sel);
3133}
3134
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003135inline void swapStruct(struct message_ref32 &mr) {
3136 sys::swapByteOrder(mr.imp);
3137 sys::swapByteOrder(mr.sel);
3138}
3139
Kevin Enderby846c0002015-04-16 17:19:59 +00003140inline void swapStruct(struct objc_module_t &module) {
3141 sys::swapByteOrder(module.version);
3142 sys::swapByteOrder(module.size);
3143 sys::swapByteOrder(module.name);
3144 sys::swapByteOrder(module.symtab);
Jingyue Wufedecc42015-04-16 18:43:44 +00003145}
Kevin Enderby846c0002015-04-16 17:19:59 +00003146
3147inline void swapStruct(struct objc_symtab_t &symtab) {
3148 sys::swapByteOrder(symtab.sel_ref_cnt);
3149 sys::swapByteOrder(symtab.refs);
3150 sys::swapByteOrder(symtab.cls_def_cnt);
3151 sys::swapByteOrder(symtab.cat_def_cnt);
Jingyue Wufedecc42015-04-16 18:43:44 +00003152}
Kevin Enderby846c0002015-04-16 17:19:59 +00003153
3154inline void swapStruct(struct objc_class_t &objc_class) {
3155 sys::swapByteOrder(objc_class.isa);
3156 sys::swapByteOrder(objc_class.super_class);
3157 sys::swapByteOrder(objc_class.name);
3158 sys::swapByteOrder(objc_class.version);
3159 sys::swapByteOrder(objc_class.info);
3160 sys::swapByteOrder(objc_class.instance_size);
3161 sys::swapByteOrder(objc_class.ivars);
3162 sys::swapByteOrder(objc_class.methodLists);
3163 sys::swapByteOrder(objc_class.cache);
3164 sys::swapByteOrder(objc_class.protocols);
Jingyue Wufedecc42015-04-16 18:43:44 +00003165}
Kevin Enderby846c0002015-04-16 17:19:59 +00003166
3167inline void swapStruct(struct objc_category_t &objc_category) {
3168 sys::swapByteOrder(objc_category.category_name);
3169 sys::swapByteOrder(objc_category.class_name);
3170 sys::swapByteOrder(objc_category.instance_methods);
3171 sys::swapByteOrder(objc_category.class_methods);
3172 sys::swapByteOrder(objc_category.protocols);
3173}
3174
3175inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) {
3176 sys::swapByteOrder(objc_ivar_list.ivar_count);
3177}
3178
3179inline void swapStruct(struct objc_ivar_t &objc_ivar) {
3180 sys::swapByteOrder(objc_ivar.ivar_name);
3181 sys::swapByteOrder(objc_ivar.ivar_type);
3182 sys::swapByteOrder(objc_ivar.ivar_offset);
Jingyue Wufedecc42015-04-16 18:43:44 +00003183}
Kevin Enderby846c0002015-04-16 17:19:59 +00003184
3185inline void swapStruct(struct objc_method_list_t &method_list) {
3186 sys::swapByteOrder(method_list.obsolete);
3187 sys::swapByteOrder(method_list.method_count);
3188}
3189
3190inline void swapStruct(struct objc_method_t &method) {
3191 sys::swapByteOrder(method.method_name);
3192 sys::swapByteOrder(method.method_types);
3193 sys::swapByteOrder(method.method_imp);
3194}
3195
3196inline void swapStruct(struct objc_protocol_list_t &protocol_list) {
3197 sys::swapByteOrder(protocol_list.next);
3198 sys::swapByteOrder(protocol_list.count);
3199}
3200
3201inline void swapStruct(struct objc_protocol_t &protocol) {
3202 sys::swapByteOrder(protocol.isa);
3203 sys::swapByteOrder(protocol.protocol_name);
3204 sys::swapByteOrder(protocol.protocol_list);
3205 sys::swapByteOrder(protocol.instance_methods);
3206 sys::swapByteOrder(protocol.class_methods);
3207}
3208
3209inline void swapStruct(struct objc_method_description_list_t &mdl) {
3210 sys::swapByteOrder(mdl.count);
3211}
3212
3213inline void swapStruct(struct objc_method_description_t &md) {
3214 sys::swapByteOrder(md.name);
3215 sys::swapByteOrder(md.types);
3216}
3217
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003218static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
3219 struct DisassembleInfo *info);
3220
3221// get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
3222// to an Objective-C class and returns the class name. It is also passed the
3223// address of the pointer, so when the pointer is zero as it can be in an .o
3224// file, that is used to look for an external relocation entry with a symbol
3225// name.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003226static const char *get_objc2_64bit_class_name(uint64_t pointer_value,
3227 uint64_t ReferenceValue,
3228 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003229 const char *r;
3230 uint32_t offset, left;
3231 SectionRef S;
3232
3233 // The pointer_value can be 0 in an object file and have a relocation
3234 // entry for the class symbol at the ReferenceValue (the address of the
3235 // pointer).
3236 if (pointer_value == 0) {
3237 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3238 if (r == nullptr || left < sizeof(uint64_t))
3239 return nullptr;
3240 uint64_t n_value;
3241 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3242 if (symbol_name == nullptr)
3243 return nullptr;
Hans Wennborgdb53e302014-10-23 21:59:17 +00003244 const char *class_name = strrchr(symbol_name, '$');
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003245 if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0')
3246 return class_name + 2;
3247 else
3248 return nullptr;
3249 }
3250
3251 // The case were the pointer_value is non-zero and points to a class defined
3252 // in this Mach-O file.
3253 r = get_pointer_64(pointer_value, offset, left, S, info);
3254 if (r == nullptr || left < sizeof(struct class64_t))
3255 return nullptr;
3256 struct class64_t c;
3257 memcpy(&c, r, sizeof(struct class64_t));
3258 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3259 swapStruct(c);
3260 if (c.data == 0)
3261 return nullptr;
3262 r = get_pointer_64(c.data, offset, left, S, info);
3263 if (r == nullptr || left < sizeof(struct class_ro64_t))
3264 return nullptr;
3265 struct class_ro64_t cro;
3266 memcpy(&cro, r, sizeof(struct class_ro64_t));
3267 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3268 swapStruct(cro);
3269 if (cro.name == 0)
3270 return nullptr;
3271 const char *name = get_pointer_64(cro.name, offset, left, S, info);
3272 return name;
3273}
3274
3275// get_objc2_64bit_cfstring_name is used for disassembly and is passed a
3276// pointer to a cfstring and returns its name or nullptr.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003277static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue,
3278 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003279 const char *r, *name;
3280 uint32_t offset, left;
3281 SectionRef S;
3282 struct cfstring64_t cfs;
3283 uint64_t cfs_characters;
3284
3285 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3286 if (r == nullptr || left < sizeof(struct cfstring64_t))
3287 return nullptr;
3288 memcpy(&cfs, r, sizeof(struct cfstring64_t));
3289 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3290 swapStruct(cfs);
3291 if (cfs.characters == 0) {
3292 uint64_t n_value;
3293 const char *symbol_name = get_symbol_64(
3294 offset + offsetof(struct cfstring64_t, characters), S, info, n_value);
3295 if (symbol_name == nullptr)
3296 return nullptr;
3297 cfs_characters = n_value;
3298 } else
3299 cfs_characters = cfs.characters;
3300 name = get_pointer_64(cfs_characters, offset, left, S, info);
3301
3302 return name;
3303}
3304
3305// get_objc2_64bit_selref() is used for disassembly and is passed a the address
3306// of a pointer to an Objective-C selector reference when the pointer value is
3307// zero as in a .o file and is likely to have a external relocation entry with
3308// who's symbol's n_value is the real pointer to the selector name. If that is
3309// the case the real pointer to the selector name is returned else 0 is
3310// returned
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003311static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue,
3312 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003313 uint32_t offset, left;
3314 SectionRef S;
3315
3316 const char *r = get_pointer_64(ReferenceValue, offset, left, S, info);
3317 if (r == nullptr || left < sizeof(uint64_t))
3318 return 0;
3319 uint64_t n_value;
3320 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3321 if (symbol_name == nullptr)
3322 return 0;
3323 return n_value;
3324}
3325
Kevin Enderby0fc11822015-04-01 20:57:01 +00003326static const SectionRef get_section(MachOObjectFile *O, const char *segname,
3327 const char *sectname) {
3328 for (const SectionRef &Section : O->sections()) {
3329 StringRef SectName;
3330 Section.getName(SectName);
3331 DataRefImpl Ref = Section.getRawDataRefImpl();
3332 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3333 if (SegName == segname && SectName == sectname)
3334 return Section;
3335 }
3336 return SectionRef();
3337}
3338
3339static void
3340walk_pointer_list_64(const char *listname, const SectionRef S,
3341 MachOObjectFile *O, struct DisassembleInfo *info,
3342 void (*func)(uint64_t, struct DisassembleInfo *info)) {
3343 if (S == SectionRef())
3344 return;
3345
3346 StringRef SectName;
3347 S.getName(SectName);
3348 DataRefImpl Ref = S.getRawDataRefImpl();
3349 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3350 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3351
3352 StringRef BytesStr;
3353 S.getContents(BytesStr);
3354 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3355
3356 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) {
3357 uint32_t left = S.getSize() - i;
3358 uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t);
3359 uint64_t p = 0;
3360 memcpy(&p, Contents + i, size);
3361 if (i + sizeof(uint64_t) > S.getSize())
3362 outs() << listname << " list pointer extends past end of (" << SegName
3363 << "," << SectName << ") section\n";
3364 outs() << format("%016" PRIx64, S.getAddress() + i) << " ";
3365
3366 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3367 sys::swapByteOrder(p);
3368
3369 uint64_t n_value = 0;
3370 const char *name = get_symbol_64(i, S, info, n_value, p);
3371 if (name == nullptr)
3372 name = get_dyld_bind_info_symbolname(S.getAddress() + i, info);
3373
3374 if (n_value != 0) {
3375 outs() << format("0x%" PRIx64, n_value);
3376 if (p != 0)
3377 outs() << " + " << format("0x%" PRIx64, p);
3378 } else
3379 outs() << format("0x%" PRIx64, p);
3380 if (name != nullptr)
3381 outs() << " " << name;
3382 outs() << "\n";
3383
3384 p += n_value;
3385 if (func)
3386 func(p, info);
3387 }
3388}
3389
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003390static void
3391walk_pointer_list_32(const char *listname, const SectionRef S,
3392 MachOObjectFile *O, struct DisassembleInfo *info,
3393 void (*func)(uint32_t, struct DisassembleInfo *info)) {
3394 if (S == SectionRef())
3395 return;
3396
3397 StringRef SectName;
3398 S.getName(SectName);
3399 DataRefImpl Ref = S.getRawDataRefImpl();
3400 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3401 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3402
3403 StringRef BytesStr;
3404 S.getContents(BytesStr);
3405 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3406
3407 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) {
3408 uint32_t left = S.getSize() - i;
3409 uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t);
3410 uint32_t p = 0;
3411 memcpy(&p, Contents + i, size);
3412 if (i + sizeof(uint32_t) > S.getSize())
3413 outs() << listname << " list pointer extends past end of (" << SegName
3414 << "," << SectName << ") section\n";
Kevin Enderbycf261312015-04-06 22:33:43 +00003415 uint32_t Address = S.getAddress() + i;
3416 outs() << format("%08" PRIx32, Address) << " ";
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003417
3418 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3419 sys::swapByteOrder(p);
3420 outs() << format("0x%" PRIx32, p);
3421
3422 const char *name = get_symbol_32(i, S, info, p);
3423 if (name != nullptr)
3424 outs() << " " << name;
3425 outs() << "\n";
3426
3427 if (func)
3428 func(p, info);
3429 }
3430}
3431
3432static void print_layout_map(const char *layout_map, uint32_t left) {
Kevin Enderbya59824a2015-10-06 22:27:08 +00003433 if (layout_map == nullptr)
3434 return;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003435 outs() << " layout map: ";
3436 do {
3437 outs() << format("0x%02" PRIx32, (*layout_map) & 0xff) << " ";
3438 left--;
3439 layout_map++;
3440 } while (*layout_map != '\0' && left != 0);
3441 outs() << "\n";
3442}
3443
Kevin Enderby0fc11822015-04-01 20:57:01 +00003444static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) {
3445 uint32_t offset, left;
3446 SectionRef S;
3447 const char *layout_map;
3448
3449 if (p == 0)
3450 return;
3451 layout_map = get_pointer_64(p, offset, left, S, info);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003452 print_layout_map(layout_map, left);
3453}
3454
3455static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) {
3456 uint32_t offset, left;
3457 SectionRef S;
3458 const char *layout_map;
3459
3460 if (p == 0)
3461 return;
3462 layout_map = get_pointer_32(p, offset, left, S, info);
3463 print_layout_map(layout_map, left);
Kevin Enderby0fc11822015-04-01 20:57:01 +00003464}
3465
3466static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info,
3467 const char *indent) {
3468 struct method_list64_t ml;
3469 struct method64_t m;
3470 const char *r;
3471 uint32_t offset, xoffset, left, i;
3472 SectionRef S, xS;
3473 const char *name, *sym_name;
3474 uint64_t n_value;
3475
3476 r = get_pointer_64(p, offset, left, S, info);
3477 if (r == nullptr)
3478 return;
3479 memset(&ml, '\0', sizeof(struct method_list64_t));
3480 if (left < sizeof(struct method_list64_t)) {
3481 memcpy(&ml, r, left);
3482 outs() << " (method_list_t entends past the end of the section)\n";
3483 } else
3484 memcpy(&ml, r, sizeof(struct method_list64_t));
3485 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3486 swapStruct(ml);
3487 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3488 outs() << indent << "\t\t count " << ml.count << "\n";
3489
3490 p += sizeof(struct method_list64_t);
3491 offset += sizeof(struct method_list64_t);
3492 for (i = 0; i < ml.count; i++) {
3493 r = get_pointer_64(p, offset, left, S, info);
3494 if (r == nullptr)
3495 return;
3496 memset(&m, '\0', sizeof(struct method64_t));
3497 if (left < sizeof(struct method64_t)) {
Kevin Enderbya59824a2015-10-06 22:27:08 +00003498 memcpy(&m, r, left);
3499 outs() << indent << " (method_t extends past the end of the section)\n";
Kevin Enderby0fc11822015-04-01 20:57:01 +00003500 } else
3501 memcpy(&m, r, sizeof(struct method64_t));
3502 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3503 swapStruct(m);
3504
3505 outs() << indent << "\t\t name ";
3506 sym_name = get_symbol_64(offset + offsetof(struct method64_t, name), S,
3507 info, n_value, m.name);
3508 if (n_value != 0) {
3509 if (info->verbose && sym_name != nullptr)
3510 outs() << sym_name;
3511 else
3512 outs() << format("0x%" PRIx64, n_value);
3513 if (m.name != 0)
3514 outs() << " + " << format("0x%" PRIx64, m.name);
3515 } else
3516 outs() << format("0x%" PRIx64, m.name);
3517 name = get_pointer_64(m.name + n_value, xoffset, left, xS, info);
3518 if (name != nullptr)
3519 outs() << format(" %.*s", left, name);
3520 outs() << "\n";
3521
3522 outs() << indent << "\t\t types ";
3523 sym_name = get_symbol_64(offset + offsetof(struct method64_t, types), S,
3524 info, n_value, m.types);
3525 if (n_value != 0) {
3526 if (info->verbose && sym_name != nullptr)
3527 outs() << sym_name;
3528 else
3529 outs() << format("0x%" PRIx64, n_value);
3530 if (m.types != 0)
3531 outs() << " + " << format("0x%" PRIx64, m.types);
3532 } else
3533 outs() << format("0x%" PRIx64, m.types);
3534 name = get_pointer_64(m.types + n_value, xoffset, left, xS, info);
3535 if (name != nullptr)
3536 outs() << format(" %.*s", left, name);
3537 outs() << "\n";
3538
3539 outs() << indent << "\t\t imp ";
3540 name = get_symbol_64(offset + offsetof(struct method64_t, imp), S, info,
3541 n_value, m.imp);
3542 if (info->verbose && name == nullptr) {
3543 if (n_value != 0) {
3544 outs() << format("0x%" PRIx64, n_value) << " ";
3545 if (m.imp != 0)
3546 outs() << "+ " << format("0x%" PRIx64, m.imp) << " ";
3547 } else
3548 outs() << format("0x%" PRIx64, m.imp) << " ";
3549 }
3550 if (name != nullptr)
3551 outs() << name;
3552 outs() << "\n";
3553
3554 p += sizeof(struct method64_t);
3555 offset += sizeof(struct method64_t);
3556 }
3557}
3558
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003559static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info,
3560 const char *indent) {
3561 struct method_list32_t ml;
3562 struct method32_t m;
Kevin Enderby846c0002015-04-16 17:19:59 +00003563 const char *r, *name;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003564 uint32_t offset, xoffset, left, i;
3565 SectionRef S, xS;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003566
3567 r = get_pointer_32(p, offset, left, S, info);
3568 if (r == nullptr)
3569 return;
3570 memset(&ml, '\0', sizeof(struct method_list32_t));
3571 if (left < sizeof(struct method_list32_t)) {
3572 memcpy(&ml, r, left);
3573 outs() << " (method_list_t entends past the end of the section)\n";
3574 } else
3575 memcpy(&ml, r, sizeof(struct method_list32_t));
3576 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3577 swapStruct(ml);
3578 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3579 outs() << indent << "\t\t count " << ml.count << "\n";
3580
3581 p += sizeof(struct method_list32_t);
3582 offset += sizeof(struct method_list32_t);
3583 for (i = 0; i < ml.count; i++) {
3584 r = get_pointer_32(p, offset, left, S, info);
3585 if (r == nullptr)
3586 return;
3587 memset(&m, '\0', sizeof(struct method32_t));
3588 if (left < sizeof(struct method32_t)) {
3589 memcpy(&ml, r, left);
3590 outs() << indent << " (method_t entends past the end of the section)\n";
3591 } else
3592 memcpy(&m, r, sizeof(struct method32_t));
3593 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3594 swapStruct(m);
3595
3596 outs() << indent << "\t\t name " << format("0x%" PRIx32, m.name);
3597 name = get_pointer_32(m.name, xoffset, left, xS, info);
3598 if (name != nullptr)
3599 outs() << format(" %.*s", left, name);
3600 outs() << "\n";
3601
3602 outs() << indent << "\t\t types " << format("0x%" PRIx32, m.types);
3603 name = get_pointer_32(m.types, xoffset, left, xS, info);
3604 if (name != nullptr)
3605 outs() << format(" %.*s", left, name);
3606 outs() << "\n";
3607
3608 outs() << indent << "\t\t imp " << format("0x%" PRIx32, m.imp);
3609 name = get_symbol_32(offset + offsetof(struct method32_t, imp), S, info,
3610 m.imp);
3611 if (name != nullptr)
3612 outs() << " " << name;
3613 outs() << "\n";
3614
3615 p += sizeof(struct method32_t);
3616 offset += sizeof(struct method32_t);
3617 }
3618}
3619
Kevin Enderby846c0002015-04-16 17:19:59 +00003620static bool print_method_list(uint32_t p, struct DisassembleInfo *info) {
3621 uint32_t offset, left, xleft;
3622 SectionRef S;
3623 struct objc_method_list_t method_list;
3624 struct objc_method_t method;
3625 const char *r, *methods, *name, *SymbolName;
3626 int32_t i;
3627
3628 r = get_pointer_32(p, offset, left, S, info, true);
3629 if (r == nullptr)
3630 return true;
3631
3632 outs() << "\n";
3633 if (left > sizeof(struct objc_method_list_t)) {
3634 memcpy(&method_list, r, sizeof(struct objc_method_list_t));
3635 } else {
3636 outs() << "\t\t objc_method_list extends past end of the section\n";
3637 memset(&method_list, '\0', sizeof(struct objc_method_list_t));
3638 memcpy(&method_list, r, left);
3639 }
3640 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3641 swapStruct(method_list);
3642
3643 outs() << "\t\t obsolete "
3644 << format("0x%08" PRIx32, method_list.obsolete) << "\n";
3645 outs() << "\t\t method_count " << method_list.method_count << "\n";
3646
3647 methods = r + sizeof(struct objc_method_list_t);
3648 for (i = 0; i < method_list.method_count; i++) {
3649 if ((i + 1) * sizeof(struct objc_method_t) > left) {
3650 outs() << "\t\t remaining method's extend past the of the section\n";
3651 break;
3652 }
3653 memcpy(&method, methods + i * sizeof(struct objc_method_t),
3654 sizeof(struct objc_method_t));
3655 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3656 swapStruct(method);
3657
3658 outs() << "\t\t method_name "
3659 << format("0x%08" PRIx32, method.method_name);
3660 if (info->verbose) {
3661 name = get_pointer_32(method.method_name, offset, xleft, S, info, true);
3662 if (name != nullptr)
3663 outs() << format(" %.*s", xleft, name);
3664 else
3665 outs() << " (not in an __OBJC section)";
3666 }
3667 outs() << "\n";
3668
3669 outs() << "\t\t method_types "
3670 << format("0x%08" PRIx32, method.method_types);
3671 if (info->verbose) {
3672 name = get_pointer_32(method.method_types, offset, xleft, S, info, true);
3673 if (name != nullptr)
3674 outs() << format(" %.*s", xleft, name);
3675 else
3676 outs() << " (not in an __OBJC section)";
3677 }
3678 outs() << "\n";
3679
3680 outs() << "\t\t method_imp "
3681 << format("0x%08" PRIx32, method.method_imp) << " ";
3682 if (info->verbose) {
3683 SymbolName = GuessSymbolName(method.method_imp, info->AddrMap);
3684 if (SymbolName != nullptr)
3685 outs() << SymbolName;
3686 }
3687 outs() << "\n";
3688 }
3689 return false;
3690}
3691
Kevin Enderby0fc11822015-04-01 20:57:01 +00003692static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) {
3693 struct protocol_list64_t pl;
3694 uint64_t q, n_value;
3695 struct protocol64_t pc;
3696 const char *r;
3697 uint32_t offset, xoffset, left, i;
3698 SectionRef S, xS;
3699 const char *name, *sym_name;
3700
3701 r = get_pointer_64(p, offset, left, S, info);
3702 if (r == nullptr)
3703 return;
3704 memset(&pl, '\0', sizeof(struct protocol_list64_t));
3705 if (left < sizeof(struct protocol_list64_t)) {
3706 memcpy(&pl, r, left);
3707 outs() << " (protocol_list_t entends past the end of the section)\n";
3708 } else
3709 memcpy(&pl, r, sizeof(struct protocol_list64_t));
3710 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3711 swapStruct(pl);
3712 outs() << " count " << pl.count << "\n";
3713
3714 p += sizeof(struct protocol_list64_t);
3715 offset += sizeof(struct protocol_list64_t);
3716 for (i = 0; i < pl.count; i++) {
3717 r = get_pointer_64(p, offset, left, S, info);
3718 if (r == nullptr)
3719 return;
3720 q = 0;
3721 if (left < sizeof(uint64_t)) {
3722 memcpy(&q, r, left);
3723 outs() << " (protocol_t * entends past the end of the section)\n";
3724 } else
3725 memcpy(&q, r, sizeof(uint64_t));
3726 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3727 sys::swapByteOrder(q);
3728
3729 outs() << "\t\t list[" << i << "] ";
3730 sym_name = get_symbol_64(offset, S, info, n_value, q);
3731 if (n_value != 0) {
3732 if (info->verbose && sym_name != nullptr)
3733 outs() << sym_name;
3734 else
3735 outs() << format("0x%" PRIx64, n_value);
3736 if (q != 0)
3737 outs() << " + " << format("0x%" PRIx64, q);
3738 } else
3739 outs() << format("0x%" PRIx64, q);
3740 outs() << " (struct protocol_t *)\n";
3741
3742 r = get_pointer_64(q + n_value, offset, left, S, info);
3743 if (r == nullptr)
3744 return;
3745 memset(&pc, '\0', sizeof(struct protocol64_t));
3746 if (left < sizeof(struct protocol64_t)) {
3747 memcpy(&pc, r, left);
3748 outs() << " (protocol_t entends past the end of the section)\n";
3749 } else
3750 memcpy(&pc, r, sizeof(struct protocol64_t));
3751 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3752 swapStruct(pc);
3753
3754 outs() << "\t\t\t isa " << format("0x%" PRIx64, pc.isa) << "\n";
3755
3756 outs() << "\t\t\t name ";
3757 sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name), S,
3758 info, n_value, pc.name);
3759 if (n_value != 0) {
3760 if (info->verbose && sym_name != nullptr)
3761 outs() << sym_name;
3762 else
3763 outs() << format("0x%" PRIx64, n_value);
3764 if (pc.name != 0)
3765 outs() << " + " << format("0x%" PRIx64, pc.name);
3766 } else
3767 outs() << format("0x%" PRIx64, pc.name);
3768 name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info);
3769 if (name != nullptr)
3770 outs() << format(" %.*s", left, name);
3771 outs() << "\n";
3772
3773 outs() << "\t\t\tprotocols " << format("0x%" PRIx64, pc.protocols) << "\n";
3774
3775 outs() << "\t\t instanceMethods ";
3776 sym_name =
3777 get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods),
3778 S, info, n_value, pc.instanceMethods);
3779 if (n_value != 0) {
3780 if (info->verbose && sym_name != nullptr)
3781 outs() << sym_name;
3782 else
3783 outs() << format("0x%" PRIx64, n_value);
3784 if (pc.instanceMethods != 0)
3785 outs() << " + " << format("0x%" PRIx64, pc.instanceMethods);
3786 } else
3787 outs() << format("0x%" PRIx64, pc.instanceMethods);
3788 outs() << " (struct method_list_t *)\n";
3789 if (pc.instanceMethods + n_value != 0)
3790 print_method_list64_t(pc.instanceMethods + n_value, info, "\t");
3791
3792 outs() << "\t\t classMethods ";
3793 sym_name =
3794 get_symbol_64(offset + offsetof(struct protocol64_t, classMethods), S,
3795 info, n_value, pc.classMethods);
3796 if (n_value != 0) {
3797 if (info->verbose && sym_name != nullptr)
3798 outs() << sym_name;
3799 else
3800 outs() << format("0x%" PRIx64, n_value);
3801 if (pc.classMethods != 0)
3802 outs() << " + " << format("0x%" PRIx64, pc.classMethods);
3803 } else
3804 outs() << format("0x%" PRIx64, pc.classMethods);
3805 outs() << " (struct method_list_t *)\n";
3806 if (pc.classMethods + n_value != 0)
3807 print_method_list64_t(pc.classMethods + n_value, info, "\t");
3808
3809 outs() << "\t optionalInstanceMethods "
3810 << format("0x%" PRIx64, pc.optionalInstanceMethods) << "\n";
3811 outs() << "\t optionalClassMethods "
3812 << format("0x%" PRIx64, pc.optionalClassMethods) << "\n";
3813 outs() << "\t instanceProperties "
3814 << format("0x%" PRIx64, pc.instanceProperties) << "\n";
3815
3816 p += sizeof(uint64_t);
3817 offset += sizeof(uint64_t);
3818 }
3819}
3820
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003821static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) {
3822 struct protocol_list32_t pl;
3823 uint32_t q;
3824 struct protocol32_t pc;
3825 const char *r;
3826 uint32_t offset, xoffset, left, i;
3827 SectionRef S, xS;
3828 const char *name;
3829
3830 r = get_pointer_32(p, offset, left, S, info);
3831 if (r == nullptr)
3832 return;
3833 memset(&pl, '\0', sizeof(struct protocol_list32_t));
3834 if (left < sizeof(struct protocol_list32_t)) {
3835 memcpy(&pl, r, left);
3836 outs() << " (protocol_list_t entends past the end of the section)\n";
3837 } else
3838 memcpy(&pl, r, sizeof(struct protocol_list32_t));
3839 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3840 swapStruct(pl);
3841 outs() << " count " << pl.count << "\n";
3842
3843 p += sizeof(struct protocol_list32_t);
3844 offset += sizeof(struct protocol_list32_t);
3845 for (i = 0; i < pl.count; i++) {
3846 r = get_pointer_32(p, offset, left, S, info);
3847 if (r == nullptr)
3848 return;
3849 q = 0;
3850 if (left < sizeof(uint32_t)) {
3851 memcpy(&q, r, left);
3852 outs() << " (protocol_t * entends past the end of the section)\n";
3853 } else
3854 memcpy(&q, r, sizeof(uint32_t));
3855 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3856 sys::swapByteOrder(q);
3857 outs() << "\t\t list[" << i << "] " << format("0x%" PRIx32, q)
3858 << " (struct protocol_t *)\n";
3859 r = get_pointer_32(q, offset, left, S, info);
3860 if (r == nullptr)
3861 return;
3862 memset(&pc, '\0', sizeof(struct protocol32_t));
3863 if (left < sizeof(struct protocol32_t)) {
3864 memcpy(&pc, r, left);
3865 outs() << " (protocol_t entends past the end of the section)\n";
3866 } else
3867 memcpy(&pc, r, sizeof(struct protocol32_t));
3868 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3869 swapStruct(pc);
3870 outs() << "\t\t\t isa " << format("0x%" PRIx32, pc.isa) << "\n";
3871 outs() << "\t\t\t name " << format("0x%" PRIx32, pc.name);
3872 name = get_pointer_32(pc.name, xoffset, left, xS, info);
3873 if (name != nullptr)
3874 outs() << format(" %.*s", left, name);
3875 outs() << "\n";
3876 outs() << "\t\t\tprotocols " << format("0x%" PRIx32, pc.protocols) << "\n";
3877 outs() << "\t\t instanceMethods "
3878 << format("0x%" PRIx32, pc.instanceMethods)
3879 << " (struct method_list_t *)\n";
3880 if (pc.instanceMethods != 0)
3881 print_method_list32_t(pc.instanceMethods, info, "\t");
3882 outs() << "\t\t classMethods " << format("0x%" PRIx32, pc.classMethods)
3883 << " (struct method_list_t *)\n";
3884 if (pc.classMethods != 0)
3885 print_method_list32_t(pc.classMethods, info, "\t");
3886 outs() << "\t optionalInstanceMethods "
3887 << format("0x%" PRIx32, pc.optionalInstanceMethods) << "\n";
3888 outs() << "\t optionalClassMethods "
3889 << format("0x%" PRIx32, pc.optionalClassMethods) << "\n";
3890 outs() << "\t instanceProperties "
3891 << format("0x%" PRIx32, pc.instanceProperties) << "\n";
3892 p += sizeof(uint32_t);
3893 offset += sizeof(uint32_t);
3894 }
3895}
3896
Kevin Enderby846c0002015-04-16 17:19:59 +00003897static void print_indent(uint32_t indent) {
3898 for (uint32_t i = 0; i < indent;) {
3899 if (indent - i >= 8) {
3900 outs() << "\t";
3901 i += 8;
3902 } else {
3903 for (uint32_t j = i; j < indent; j++)
3904 outs() << " ";
3905 return;
3906 }
3907 }
3908}
3909
3910static bool print_method_description_list(uint32_t p, uint32_t indent,
3911 struct DisassembleInfo *info) {
3912 uint32_t offset, left, xleft;
3913 SectionRef S;
3914 struct objc_method_description_list_t mdl;
3915 struct objc_method_description_t md;
3916 const char *r, *list, *name;
3917 int32_t i;
3918
3919 r = get_pointer_32(p, offset, left, S, info, true);
3920 if (r == nullptr)
3921 return true;
3922
3923 outs() << "\n";
3924 if (left > sizeof(struct objc_method_description_list_t)) {
3925 memcpy(&mdl, r, sizeof(struct objc_method_description_list_t));
3926 } else {
3927 print_indent(indent);
3928 outs() << " objc_method_description_list extends past end of the section\n";
3929 memset(&mdl, '\0', sizeof(struct objc_method_description_list_t));
3930 memcpy(&mdl, r, left);
3931 }
3932 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3933 swapStruct(mdl);
3934
3935 print_indent(indent);
3936 outs() << " count " << mdl.count << "\n";
3937
3938 list = r + sizeof(struct objc_method_description_list_t);
3939 for (i = 0; i < mdl.count; i++) {
3940 if ((i + 1) * sizeof(struct objc_method_description_t) > left) {
3941 print_indent(indent);
3942 outs() << " remaining list entries extend past the of the section\n";
3943 break;
3944 }
3945 print_indent(indent);
3946 outs() << " list[" << i << "]\n";
3947 memcpy(&md, list + i * sizeof(struct objc_method_description_t),
3948 sizeof(struct objc_method_description_t));
3949 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3950 swapStruct(md);
3951
3952 print_indent(indent);
3953 outs() << " name " << format("0x%08" PRIx32, md.name);
3954 if (info->verbose) {
3955 name = get_pointer_32(md.name, offset, xleft, S, info, true);
3956 if (name != nullptr)
3957 outs() << format(" %.*s", xleft, name);
3958 else
3959 outs() << " (not in an __OBJC section)";
3960 }
3961 outs() << "\n";
3962
3963 print_indent(indent);
3964 outs() << " types " << format("0x%08" PRIx32, md.types);
3965 if (info->verbose) {
3966 name = get_pointer_32(md.types, offset, xleft, S, info, true);
3967 if (name != nullptr)
3968 outs() << format(" %.*s", xleft, name);
3969 else
3970 outs() << " (not in an __OBJC section)";
3971 }
3972 outs() << "\n";
3973 }
3974 return false;
3975}
3976
3977static bool print_protocol_list(uint32_t p, uint32_t indent,
3978 struct DisassembleInfo *info);
3979
3980static bool print_protocol(uint32_t p, uint32_t indent,
3981 struct DisassembleInfo *info) {
3982 uint32_t offset, left;
3983 SectionRef S;
3984 struct objc_protocol_t protocol;
3985 const char *r, *name;
3986
3987 r = get_pointer_32(p, offset, left, S, info, true);
3988 if (r == nullptr)
3989 return true;
3990
3991 outs() << "\n";
3992 if (left >= sizeof(struct objc_protocol_t)) {
3993 memcpy(&protocol, r, sizeof(struct objc_protocol_t));
3994 } else {
3995 print_indent(indent);
3996 outs() << " Protocol extends past end of the section\n";
3997 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
3998 memcpy(&protocol, r, left);
3999 }
4000 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4001 swapStruct(protocol);
4002
4003 print_indent(indent);
4004 outs() << " isa " << format("0x%08" PRIx32, protocol.isa)
4005 << "\n";
4006
4007 print_indent(indent);
4008 outs() << " protocol_name "
4009 << format("0x%08" PRIx32, protocol.protocol_name);
4010 if (info->verbose) {
4011 name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true);
4012 if (name != nullptr)
4013 outs() << format(" %.*s", left, name);
4014 else
4015 outs() << " (not in an __OBJC section)";
4016 }
4017 outs() << "\n";
4018
4019 print_indent(indent);
4020 outs() << " protocol_list "
4021 << format("0x%08" PRIx32, protocol.protocol_list);
4022 if (print_protocol_list(protocol.protocol_list, indent + 4, info))
4023 outs() << " (not in an __OBJC section)\n";
4024
4025 print_indent(indent);
4026 outs() << " instance_methods "
4027 << format("0x%08" PRIx32, protocol.instance_methods);
4028 if (print_method_description_list(protocol.instance_methods, indent, info))
4029 outs() << " (not in an __OBJC section)\n";
4030
4031 print_indent(indent);
4032 outs() << " class_methods "
4033 << format("0x%08" PRIx32, protocol.class_methods);
4034 if (print_method_description_list(protocol.class_methods, indent, info))
4035 outs() << " (not in an __OBJC section)\n";
4036
4037 return false;
4038}
4039
4040static bool print_protocol_list(uint32_t p, uint32_t indent,
4041 struct DisassembleInfo *info) {
4042 uint32_t offset, left, l;
4043 SectionRef S;
4044 struct objc_protocol_list_t protocol_list;
4045 const char *r, *list;
4046 int32_t i;
4047
4048 r = get_pointer_32(p, offset, left, S, info, true);
4049 if (r == nullptr)
4050 return true;
4051
4052 outs() << "\n";
4053 if (left > sizeof(struct objc_protocol_list_t)) {
4054 memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t));
4055 } else {
4056 outs() << "\t\t objc_protocol_list_t extends past end of the section\n";
4057 memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t));
4058 memcpy(&protocol_list, r, left);
4059 }
4060 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4061 swapStruct(protocol_list);
4062
4063 print_indent(indent);
4064 outs() << " next " << format("0x%08" PRIx32, protocol_list.next)
4065 << "\n";
4066 print_indent(indent);
4067 outs() << " count " << protocol_list.count << "\n";
4068
4069 list = r + sizeof(struct objc_protocol_list_t);
4070 for (i = 0; i < protocol_list.count; i++) {
4071 if ((i + 1) * sizeof(uint32_t) > left) {
4072 outs() << "\t\t remaining list entries extend past the of the section\n";
4073 break;
4074 }
4075 memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t));
4076 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4077 sys::swapByteOrder(l);
4078
4079 print_indent(indent);
4080 outs() << " list[" << i << "] " << format("0x%08" PRIx32, l);
4081 if (print_protocol(l, indent, info))
4082 outs() << "(not in an __OBJC section)\n";
4083 }
4084 return false;
4085}
4086
Kevin Enderby0fc11822015-04-01 20:57:01 +00004087static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) {
4088 struct ivar_list64_t il;
4089 struct ivar64_t i;
4090 const char *r;
4091 uint32_t offset, xoffset, left, j;
4092 SectionRef S, xS;
4093 const char *name, *sym_name, *ivar_offset_p;
4094 uint64_t ivar_offset, n_value;
4095
4096 r = get_pointer_64(p, offset, left, S, info);
4097 if (r == nullptr)
4098 return;
4099 memset(&il, '\0', sizeof(struct ivar_list64_t));
4100 if (left < sizeof(struct ivar_list64_t)) {
4101 memcpy(&il, r, left);
4102 outs() << " (ivar_list_t entends past the end of the section)\n";
4103 } else
4104 memcpy(&il, r, sizeof(struct ivar_list64_t));
4105 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4106 swapStruct(il);
4107 outs() << " entsize " << il.entsize << "\n";
4108 outs() << " count " << il.count << "\n";
4109
4110 p += sizeof(struct ivar_list64_t);
4111 offset += sizeof(struct ivar_list64_t);
4112 for (j = 0; j < il.count; j++) {
4113 r = get_pointer_64(p, offset, left, S, info);
4114 if (r == nullptr)
4115 return;
4116 memset(&i, '\0', sizeof(struct ivar64_t));
4117 if (left < sizeof(struct ivar64_t)) {
4118 memcpy(&i, r, left);
4119 outs() << " (ivar_t entends past the end of the section)\n";
4120 } else
4121 memcpy(&i, r, sizeof(struct ivar64_t));
4122 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4123 swapStruct(i);
4124
4125 outs() << "\t\t\t offset ";
4126 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset), S,
4127 info, n_value, i.offset);
4128 if (n_value != 0) {
4129 if (info->verbose && sym_name != nullptr)
4130 outs() << sym_name;
4131 else
4132 outs() << format("0x%" PRIx64, n_value);
4133 if (i.offset != 0)
4134 outs() << " + " << format("0x%" PRIx64, i.offset);
4135 } else
4136 outs() << format("0x%" PRIx64, i.offset);
4137 ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info);
4138 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4139 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4140 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4141 sys::swapByteOrder(ivar_offset);
4142 outs() << " " << ivar_offset << "\n";
4143 } else
4144 outs() << "\n";
4145
4146 outs() << "\t\t\t name ";
4147 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name), S, info,
4148 n_value, i.name);
4149 if (n_value != 0) {
4150 if (info->verbose && sym_name != nullptr)
4151 outs() << sym_name;
4152 else
4153 outs() << format("0x%" PRIx64, n_value);
4154 if (i.name != 0)
4155 outs() << " + " << format("0x%" PRIx64, i.name);
4156 } else
4157 outs() << format("0x%" PRIx64, i.name);
4158 name = get_pointer_64(i.name + n_value, xoffset, left, xS, info);
4159 if (name != nullptr)
4160 outs() << format(" %.*s", left, name);
4161 outs() << "\n";
4162
4163 outs() << "\t\t\t type ";
4164 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type), S, info,
4165 n_value, i.name);
4166 name = get_pointer_64(i.type + n_value, xoffset, left, xS, info);
4167 if (n_value != 0) {
4168 if (info->verbose && sym_name != nullptr)
4169 outs() << sym_name;
4170 else
4171 outs() << format("0x%" PRIx64, n_value);
4172 if (i.type != 0)
4173 outs() << " + " << format("0x%" PRIx64, i.type);
4174 } else
4175 outs() << format("0x%" PRIx64, i.type);
4176 if (name != nullptr)
4177 outs() << format(" %.*s", left, name);
4178 outs() << "\n";
4179
4180 outs() << "\t\t\talignment " << i.alignment << "\n";
4181 outs() << "\t\t\t size " << i.size << "\n";
4182
4183 p += sizeof(struct ivar64_t);
4184 offset += sizeof(struct ivar64_t);
4185 }
4186}
4187
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004188static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) {
4189 struct ivar_list32_t il;
4190 struct ivar32_t i;
4191 const char *r;
4192 uint32_t offset, xoffset, left, j;
4193 SectionRef S, xS;
4194 const char *name, *ivar_offset_p;
4195 uint32_t ivar_offset;
4196
4197 r = get_pointer_32(p, offset, left, S, info);
4198 if (r == nullptr)
4199 return;
4200 memset(&il, '\0', sizeof(struct ivar_list32_t));
4201 if (left < sizeof(struct ivar_list32_t)) {
4202 memcpy(&il, r, left);
4203 outs() << " (ivar_list_t entends past the end of the section)\n";
4204 } else
4205 memcpy(&il, r, sizeof(struct ivar_list32_t));
4206 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4207 swapStruct(il);
4208 outs() << " entsize " << il.entsize << "\n";
4209 outs() << " count " << il.count << "\n";
4210
4211 p += sizeof(struct ivar_list32_t);
4212 offset += sizeof(struct ivar_list32_t);
4213 for (j = 0; j < il.count; j++) {
4214 r = get_pointer_32(p, offset, left, S, info);
4215 if (r == nullptr)
4216 return;
4217 memset(&i, '\0', sizeof(struct ivar32_t));
4218 if (left < sizeof(struct ivar32_t)) {
4219 memcpy(&i, r, left);
4220 outs() << " (ivar_t entends past the end of the section)\n";
4221 } else
4222 memcpy(&i, r, sizeof(struct ivar32_t));
4223 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4224 swapStruct(i);
4225
4226 outs() << "\t\t\t offset " << format("0x%" PRIx32, i.offset);
4227 ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info);
4228 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4229 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4230 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4231 sys::swapByteOrder(ivar_offset);
4232 outs() << " " << ivar_offset << "\n";
4233 } else
4234 outs() << "\n";
4235
4236 outs() << "\t\t\t name " << format("0x%" PRIx32, i.name);
4237 name = get_pointer_32(i.name, xoffset, left, xS, info);
4238 if (name != nullptr)
4239 outs() << format(" %.*s", left, name);
4240 outs() << "\n";
4241
4242 outs() << "\t\t\t type " << format("0x%" PRIx32, i.type);
4243 name = get_pointer_32(i.type, xoffset, left, xS, info);
4244 if (name != nullptr)
4245 outs() << format(" %.*s", left, name);
4246 outs() << "\n";
4247
4248 outs() << "\t\t\talignment " << i.alignment << "\n";
4249 outs() << "\t\t\t size " << i.size << "\n";
4250
4251 p += sizeof(struct ivar32_t);
4252 offset += sizeof(struct ivar32_t);
4253 }
4254}
4255
Kevin Enderby0fc11822015-04-01 20:57:01 +00004256static void print_objc_property_list64(uint64_t p,
4257 struct DisassembleInfo *info) {
4258 struct objc_property_list64 opl;
4259 struct objc_property64 op;
4260 const char *r;
4261 uint32_t offset, xoffset, left, j;
4262 SectionRef S, xS;
4263 const char *name, *sym_name;
4264 uint64_t n_value;
4265
4266 r = get_pointer_64(p, offset, left, S, info);
4267 if (r == nullptr)
4268 return;
4269 memset(&opl, '\0', sizeof(struct objc_property_list64));
4270 if (left < sizeof(struct objc_property_list64)) {
4271 memcpy(&opl, r, left);
4272 outs() << " (objc_property_list entends past the end of the section)\n";
4273 } else
4274 memcpy(&opl, r, sizeof(struct objc_property_list64));
4275 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4276 swapStruct(opl);
4277 outs() << " entsize " << opl.entsize << "\n";
4278 outs() << " count " << opl.count << "\n";
4279
4280 p += sizeof(struct objc_property_list64);
4281 offset += sizeof(struct objc_property_list64);
4282 for (j = 0; j < opl.count; j++) {
4283 r = get_pointer_64(p, offset, left, S, info);
4284 if (r == nullptr)
4285 return;
4286 memset(&op, '\0', sizeof(struct objc_property64));
4287 if (left < sizeof(struct objc_property64)) {
4288 memcpy(&op, r, left);
4289 outs() << " (objc_property entends past the end of the section)\n";
4290 } else
4291 memcpy(&op, r, sizeof(struct objc_property64));
4292 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4293 swapStruct(op);
4294
4295 outs() << "\t\t\t name ";
4296 sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name), S,
4297 info, n_value, op.name);
4298 if (n_value != 0) {
4299 if (info->verbose && sym_name != nullptr)
4300 outs() << sym_name;
4301 else
4302 outs() << format("0x%" PRIx64, n_value);
4303 if (op.name != 0)
4304 outs() << " + " << format("0x%" PRIx64, op.name);
4305 } else
4306 outs() << format("0x%" PRIx64, op.name);
4307 name = get_pointer_64(op.name + n_value, xoffset, left, xS, info);
4308 if (name != nullptr)
4309 outs() << format(" %.*s", left, name);
4310 outs() << "\n";
4311
4312 outs() << "\t\t\tattributes ";
4313 sym_name =
4314 get_symbol_64(offset + offsetof(struct objc_property64, attributes), S,
4315 info, n_value, op.attributes);
4316 if (n_value != 0) {
4317 if (info->verbose && sym_name != nullptr)
4318 outs() << sym_name;
4319 else
4320 outs() << format("0x%" PRIx64, n_value);
4321 if (op.attributes != 0)
4322 outs() << " + " << format("0x%" PRIx64, op.attributes);
4323 } else
4324 outs() << format("0x%" PRIx64, op.attributes);
4325 name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info);
4326 if (name != nullptr)
4327 outs() << format(" %.*s", left, name);
4328 outs() << "\n";
4329
4330 p += sizeof(struct objc_property64);
4331 offset += sizeof(struct objc_property64);
4332 }
4333}
4334
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004335static void print_objc_property_list32(uint32_t p,
4336 struct DisassembleInfo *info) {
4337 struct objc_property_list32 opl;
4338 struct objc_property32 op;
4339 const char *r;
4340 uint32_t offset, xoffset, left, j;
4341 SectionRef S, xS;
4342 const char *name;
4343
4344 r = get_pointer_32(p, offset, left, S, info);
4345 if (r == nullptr)
4346 return;
4347 memset(&opl, '\0', sizeof(struct objc_property_list32));
4348 if (left < sizeof(struct objc_property_list32)) {
4349 memcpy(&opl, r, left);
4350 outs() << " (objc_property_list entends past the end of the section)\n";
4351 } else
4352 memcpy(&opl, r, sizeof(struct objc_property_list32));
4353 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4354 swapStruct(opl);
4355 outs() << " entsize " << opl.entsize << "\n";
4356 outs() << " count " << opl.count << "\n";
4357
4358 p += sizeof(struct objc_property_list32);
4359 offset += sizeof(struct objc_property_list32);
4360 for (j = 0; j < opl.count; j++) {
4361 r = get_pointer_32(p, offset, left, S, info);
4362 if (r == nullptr)
4363 return;
4364 memset(&op, '\0', sizeof(struct objc_property32));
4365 if (left < sizeof(struct objc_property32)) {
4366 memcpy(&op, r, left);
4367 outs() << " (objc_property entends past the end of the section)\n";
4368 } else
4369 memcpy(&op, r, sizeof(struct objc_property32));
4370 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4371 swapStruct(op);
4372
4373 outs() << "\t\t\t name " << format("0x%" PRIx32, op.name);
4374 name = get_pointer_32(op.name, xoffset, left, xS, info);
4375 if (name != nullptr)
4376 outs() << format(" %.*s", left, name);
4377 outs() << "\n";
4378
4379 outs() << "\t\t\tattributes " << format("0x%" PRIx32, op.attributes);
4380 name = get_pointer_32(op.attributes, xoffset, left, xS, info);
4381 if (name != nullptr)
4382 outs() << format(" %.*s", left, name);
4383 outs() << "\n";
4384
4385 p += sizeof(struct objc_property32);
4386 offset += sizeof(struct objc_property32);
4387 }
4388}
4389
Richard Smith81ff44d2015-10-09 22:09:56 +00004390static bool print_class_ro64_t(uint64_t p, struct DisassembleInfo *info,
Kevin Enderby0fc11822015-04-01 20:57:01 +00004391 bool &is_meta_class) {
4392 struct class_ro64_t cro;
4393 const char *r;
4394 uint32_t offset, xoffset, left;
4395 SectionRef S, xS;
4396 const char *name, *sym_name;
4397 uint64_t n_value;
4398
4399 r = get_pointer_64(p, offset, left, S, info);
4400 if (r == nullptr || left < sizeof(struct class_ro64_t))
Richard Smith81ff44d2015-10-09 22:09:56 +00004401 return false;
Chandler Carruth33e58902016-11-04 07:10:24 +00004402 memcpy(&cro, r, sizeof(struct class_ro64_t));
Kevin Enderby0fc11822015-04-01 20:57:01 +00004403 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4404 swapStruct(cro);
4405 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4406 if (cro.flags & RO_META)
4407 outs() << " RO_META";
4408 if (cro.flags & RO_ROOT)
4409 outs() << " RO_ROOT";
4410 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4411 outs() << " RO_HAS_CXX_STRUCTORS";
4412 outs() << "\n";
4413 outs() << " instanceStart " << cro.instanceStart << "\n";
4414 outs() << " instanceSize " << cro.instanceSize << "\n";
4415 outs() << " reserved " << format("0x%" PRIx32, cro.reserved)
4416 << "\n";
4417 outs() << " ivarLayout " << format("0x%" PRIx64, cro.ivarLayout)
4418 << "\n";
4419 print_layout_map64(cro.ivarLayout, info);
4420
4421 outs() << " name ";
4422 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name), S,
4423 info, n_value, cro.name);
4424 if (n_value != 0) {
4425 if (info->verbose && sym_name != nullptr)
4426 outs() << sym_name;
4427 else
4428 outs() << format("0x%" PRIx64, n_value);
4429 if (cro.name != 0)
4430 outs() << " + " << format("0x%" PRIx64, cro.name);
4431 } else
4432 outs() << format("0x%" PRIx64, cro.name);
4433 name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info);
4434 if (name != nullptr)
4435 outs() << format(" %.*s", left, name);
4436 outs() << "\n";
4437
4438 outs() << " baseMethods ";
4439 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods),
4440 S, info, n_value, cro.baseMethods);
4441 if (n_value != 0) {
4442 if (info->verbose && sym_name != nullptr)
4443 outs() << sym_name;
4444 else
4445 outs() << format("0x%" PRIx64, n_value);
4446 if (cro.baseMethods != 0)
4447 outs() << " + " << format("0x%" PRIx64, cro.baseMethods);
4448 } else
4449 outs() << format("0x%" PRIx64, cro.baseMethods);
4450 outs() << " (struct method_list_t *)\n";
4451 if (cro.baseMethods + n_value != 0)
4452 print_method_list64_t(cro.baseMethods + n_value, info, "");
4453
4454 outs() << " baseProtocols ";
4455 sym_name =
4456 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols), S,
4457 info, n_value, cro.baseProtocols);
4458 if (n_value != 0) {
4459 if (info->verbose && sym_name != nullptr)
4460 outs() << sym_name;
4461 else
4462 outs() << format("0x%" PRIx64, n_value);
4463 if (cro.baseProtocols != 0)
4464 outs() << " + " << format("0x%" PRIx64, cro.baseProtocols);
4465 } else
4466 outs() << format("0x%" PRIx64, cro.baseProtocols);
4467 outs() << "\n";
4468 if (cro.baseProtocols + n_value != 0)
4469 print_protocol_list64_t(cro.baseProtocols + n_value, info);
4470
4471 outs() << " ivars ";
4472 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars), S,
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004473 info, n_value, cro.ivars);
Kevin Enderby0fc11822015-04-01 20:57:01 +00004474 if (n_value != 0) {
4475 if (info->verbose && sym_name != nullptr)
4476 outs() << sym_name;
4477 else
4478 outs() << format("0x%" PRIx64, n_value);
4479 if (cro.ivars != 0)
4480 outs() << " + " << format("0x%" PRIx64, cro.ivars);
4481 } else
4482 outs() << format("0x%" PRIx64, cro.ivars);
4483 outs() << "\n";
4484 if (cro.ivars + n_value != 0)
4485 print_ivar_list64_t(cro.ivars + n_value, info);
4486
4487 outs() << " weakIvarLayout ";
4488 sym_name =
4489 get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout), S,
4490 info, n_value, cro.weakIvarLayout);
4491 if (n_value != 0) {
4492 if (info->verbose && sym_name != nullptr)
4493 outs() << sym_name;
4494 else
4495 outs() << format("0x%" PRIx64, n_value);
4496 if (cro.weakIvarLayout != 0)
4497 outs() << " + " << format("0x%" PRIx64, cro.weakIvarLayout);
4498 } else
4499 outs() << format("0x%" PRIx64, cro.weakIvarLayout);
4500 outs() << "\n";
4501 print_layout_map64(cro.weakIvarLayout + n_value, info);
4502
4503 outs() << " baseProperties ";
4504 sym_name =
4505 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties), S,
4506 info, n_value, cro.baseProperties);
4507 if (n_value != 0) {
4508 if (info->verbose && sym_name != nullptr)
4509 outs() << sym_name;
4510 else
4511 outs() << format("0x%" PRIx64, n_value);
4512 if (cro.baseProperties != 0)
4513 outs() << " + " << format("0x%" PRIx64, cro.baseProperties);
4514 } else
4515 outs() << format("0x%" PRIx64, cro.baseProperties);
4516 outs() << "\n";
4517 if (cro.baseProperties + n_value != 0)
4518 print_objc_property_list64(cro.baseProperties + n_value, info);
4519
Rafael Espindolab9091322015-10-24 23:19:10 +00004520 is_meta_class = (cro.flags & RO_META) != 0;
Richard Smith81ff44d2015-10-09 22:09:56 +00004521 return true;
Kevin Enderby0fc11822015-04-01 20:57:01 +00004522}
4523
Richard Smith81ff44d2015-10-09 22:09:56 +00004524static bool print_class_ro32_t(uint32_t p, struct DisassembleInfo *info,
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004525 bool &is_meta_class) {
4526 struct class_ro32_t cro;
4527 const char *r;
4528 uint32_t offset, xoffset, left;
4529 SectionRef S, xS;
4530 const char *name;
4531
4532 r = get_pointer_32(p, offset, left, S, info);
4533 if (r == nullptr)
Richard Smith81ff44d2015-10-09 22:09:56 +00004534 return false;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004535 memset(&cro, '\0', sizeof(struct class_ro32_t));
4536 if (left < sizeof(struct class_ro32_t)) {
4537 memcpy(&cro, r, left);
4538 outs() << " (class_ro_t entends past the end of the section)\n";
4539 } else
4540 memcpy(&cro, r, sizeof(struct class_ro32_t));
4541 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4542 swapStruct(cro);
4543 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4544 if (cro.flags & RO_META)
4545 outs() << " RO_META";
4546 if (cro.flags & RO_ROOT)
4547 outs() << " RO_ROOT";
4548 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4549 outs() << " RO_HAS_CXX_STRUCTORS";
4550 outs() << "\n";
4551 outs() << " instanceStart " << cro.instanceStart << "\n";
4552 outs() << " instanceSize " << cro.instanceSize << "\n";
4553 outs() << " ivarLayout " << format("0x%" PRIx32, cro.ivarLayout)
4554 << "\n";
4555 print_layout_map32(cro.ivarLayout, info);
4556
4557 outs() << " name " << format("0x%" PRIx32, cro.name);
4558 name = get_pointer_32(cro.name, xoffset, left, xS, info);
4559 if (name != nullptr)
4560 outs() << format(" %.*s", left, name);
4561 outs() << "\n";
4562
4563 outs() << " baseMethods "
4564 << format("0x%" PRIx32, cro.baseMethods)
4565 << " (struct method_list_t *)\n";
4566 if (cro.baseMethods != 0)
4567 print_method_list32_t(cro.baseMethods, info, "");
4568
4569 outs() << " baseProtocols "
4570 << format("0x%" PRIx32, cro.baseProtocols) << "\n";
4571 if (cro.baseProtocols != 0)
4572 print_protocol_list32_t(cro.baseProtocols, info);
4573 outs() << " ivars " << format("0x%" PRIx32, cro.ivars)
4574 << "\n";
4575 if (cro.ivars != 0)
4576 print_ivar_list32_t(cro.ivars, info);
4577 outs() << " weakIvarLayout "
4578 << format("0x%" PRIx32, cro.weakIvarLayout) << "\n";
4579 print_layout_map32(cro.weakIvarLayout, info);
4580 outs() << " baseProperties "
4581 << format("0x%" PRIx32, cro.baseProperties) << "\n";
4582 if (cro.baseProperties != 0)
4583 print_objc_property_list32(cro.baseProperties, info);
Rafael Espindolab9091322015-10-24 23:19:10 +00004584 is_meta_class = (cro.flags & RO_META) != 0;
Richard Smith81ff44d2015-10-09 22:09:56 +00004585 return true;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004586}
4587
Kevin Enderby0fc11822015-04-01 20:57:01 +00004588static void print_class64_t(uint64_t p, struct DisassembleInfo *info) {
4589 struct class64_t c;
4590 const char *r;
4591 uint32_t offset, left;
4592 SectionRef S;
4593 const char *name;
4594 uint64_t isa_n_value, n_value;
4595
4596 r = get_pointer_64(p, offset, left, S, info);
4597 if (r == nullptr || left < sizeof(struct class64_t))
4598 return;
Chandler Carruth33e58902016-11-04 07:10:24 +00004599 memcpy(&c, r, sizeof(struct class64_t));
Kevin Enderby0fc11822015-04-01 20:57:01 +00004600 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4601 swapStruct(c);
4602
4603 outs() << " isa " << format("0x%" PRIx64, c.isa);
4604 name = get_symbol_64(offset + offsetof(struct class64_t, isa), S, info,
4605 isa_n_value, c.isa);
4606 if (name != nullptr)
4607 outs() << " " << name;
4608 outs() << "\n";
4609
4610 outs() << " superclass " << format("0x%" PRIx64, c.superclass);
4611 name = get_symbol_64(offset + offsetof(struct class64_t, superclass), S, info,
4612 n_value, c.superclass);
4613 if (name != nullptr)
4614 outs() << " " << name;
Kevin Enderby1ce38582017-06-20 22:55:11 +00004615 else {
4616 name = get_dyld_bind_info_symbolname(S.getAddress() +
4617 offset + offsetof(struct class64_t, superclass), info);
4618 if (name != nullptr)
4619 outs() << " " << name;
4620 }
Kevin Enderby0fc11822015-04-01 20:57:01 +00004621 outs() << "\n";
4622
4623 outs() << " cache " << format("0x%" PRIx64, c.cache);
4624 name = get_symbol_64(offset + offsetof(struct class64_t, cache), S, info,
4625 n_value, c.cache);
4626 if (name != nullptr)
4627 outs() << " " << name;
4628 outs() << "\n";
4629
4630 outs() << " vtable " << format("0x%" PRIx64, c.vtable);
4631 name = get_symbol_64(offset + offsetof(struct class64_t, vtable), S, info,
4632 n_value, c.vtable);
4633 if (name != nullptr)
4634 outs() << " " << name;
4635 outs() << "\n";
4636
4637 name = get_symbol_64(offset + offsetof(struct class64_t, data), S, info,
4638 n_value, c.data);
4639 outs() << " data ";
4640 if (n_value != 0) {
4641 if (info->verbose && name != nullptr)
4642 outs() << name;
4643 else
4644 outs() << format("0x%" PRIx64, n_value);
4645 if (c.data != 0)
4646 outs() << " + " << format("0x%" PRIx64, c.data);
4647 } else
4648 outs() << format("0x%" PRIx64, c.data);
4649 outs() << " (struct class_ro_t *)";
4650
4651 // This is a Swift class if some of the low bits of the pointer are set.
4652 if ((c.data + n_value) & 0x7)
4653 outs() << " Swift class";
4654 outs() << "\n";
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004655 bool is_meta_class;
Richard Smith81ff44d2015-10-09 22:09:56 +00004656 if (!print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class))
4657 return;
Kevin Enderby0fc11822015-04-01 20:57:01 +00004658
Kevin Enderbyaac75382015-10-08 16:56:35 +00004659 if (!is_meta_class &&
4660 c.isa + isa_n_value != p &&
4661 c.isa + isa_n_value != 0 &&
4662 info->depth < 100) {
4663 info->depth++;
4664 outs() << "Meta Class\n";
4665 print_class64_t(c.isa + isa_n_value, info);
Kevin Enderby0fc11822015-04-01 20:57:01 +00004666 }
4667}
4668
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004669static void print_class32_t(uint32_t p, struct DisassembleInfo *info) {
4670 struct class32_t c;
4671 const char *r;
4672 uint32_t offset, left;
4673 SectionRef S;
4674 const char *name;
4675
4676 r = get_pointer_32(p, offset, left, S, info);
4677 if (r == nullptr)
4678 return;
4679 memset(&c, '\0', sizeof(struct class32_t));
4680 if (left < sizeof(struct class32_t)) {
4681 memcpy(&c, r, left);
4682 outs() << " (class_t entends past the end of the section)\n";
4683 } else
4684 memcpy(&c, r, sizeof(struct class32_t));
4685 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4686 swapStruct(c);
4687
4688 outs() << " isa " << format("0x%" PRIx32, c.isa);
4689 name =
4690 get_symbol_32(offset + offsetof(struct class32_t, isa), S, info, c.isa);
4691 if (name != nullptr)
4692 outs() << " " << name;
4693 outs() << "\n";
4694
4695 outs() << " superclass " << format("0x%" PRIx32, c.superclass);
4696 name = get_symbol_32(offset + offsetof(struct class32_t, superclass), S, info,
4697 c.superclass);
4698 if (name != nullptr)
4699 outs() << " " << name;
4700 outs() << "\n";
4701
4702 outs() << " cache " << format("0x%" PRIx32, c.cache);
4703 name = get_symbol_32(offset + offsetof(struct class32_t, cache), S, info,
4704 c.cache);
4705 if (name != nullptr)
4706 outs() << " " << name;
4707 outs() << "\n";
4708
4709 outs() << " vtable " << format("0x%" PRIx32, c.vtable);
4710 name = get_symbol_32(offset + offsetof(struct class32_t, vtable), S, info,
4711 c.vtable);
4712 if (name != nullptr)
4713 outs() << " " << name;
4714 outs() << "\n";
4715
4716 name =
4717 get_symbol_32(offset + offsetof(struct class32_t, data), S, info, c.data);
4718 outs() << " data " << format("0x%" PRIx32, c.data)
4719 << " (struct class_ro_t *)";
4720
4721 // This is a Swift class if some of the low bits of the pointer are set.
4722 if (c.data & 0x3)
4723 outs() << " Swift class";
4724 outs() << "\n";
4725 bool is_meta_class;
Richard Smith81ff44d2015-10-09 22:09:56 +00004726 if (!print_class_ro32_t(c.data & ~0x3, info, is_meta_class))
4727 return;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004728
Hans Wennborgcc9deb42015-09-29 18:02:48 +00004729 if (!is_meta_class) {
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004730 outs() << "Meta Class\n";
4731 print_class32_t(c.isa, info);
4732 }
4733}
4734
Kevin Enderby846c0002015-04-16 17:19:59 +00004735static void print_objc_class_t(struct objc_class_t *objc_class,
4736 struct DisassembleInfo *info) {
4737 uint32_t offset, left, xleft;
4738 const char *name, *p, *ivar_list;
4739 SectionRef S;
4740 int32_t i;
4741 struct objc_ivar_list_t objc_ivar_list;
4742 struct objc_ivar_t ivar;
4743
4744 outs() << "\t\t isa " << format("0x%08" PRIx32, objc_class->isa);
4745 if (info->verbose && CLS_GETINFO(objc_class, CLS_META)) {
4746 name = get_pointer_32(objc_class->isa, offset, left, S, info, true);
4747 if (name != nullptr)
4748 outs() << format(" %.*s", left, name);
4749 else
4750 outs() << " (not in an __OBJC section)";
4751 }
4752 outs() << "\n";
4753
4754 outs() << "\t super_class "
4755 << format("0x%08" PRIx32, objc_class->super_class);
4756 if (info->verbose) {
4757 name = get_pointer_32(objc_class->super_class, offset, left, S, info, true);
4758 if (name != nullptr)
4759 outs() << format(" %.*s", left, name);
4760 else
4761 outs() << " (not in an __OBJC section)";
4762 }
4763 outs() << "\n";
4764
4765 outs() << "\t\t name " << format("0x%08" PRIx32, objc_class->name);
4766 if (info->verbose) {
4767 name = get_pointer_32(objc_class->name, offset, left, S, info, true);
4768 if (name != nullptr)
4769 outs() << format(" %.*s", left, name);
4770 else
4771 outs() << " (not in an __OBJC section)";
4772 }
4773 outs() << "\n";
4774
4775 outs() << "\t\t version " << format("0x%08" PRIx32, objc_class->version)
4776 << "\n";
4777
4778 outs() << "\t\t info " << format("0x%08" PRIx32, objc_class->info);
4779 if (info->verbose) {
4780 if (CLS_GETINFO(objc_class, CLS_CLASS))
4781 outs() << " CLS_CLASS";
4782 else if (CLS_GETINFO(objc_class, CLS_META))
4783 outs() << " CLS_META";
4784 }
4785 outs() << "\n";
4786
4787 outs() << "\t instance_size "
4788 << format("0x%08" PRIx32, objc_class->instance_size) << "\n";
4789
4790 p = get_pointer_32(objc_class->ivars, offset, left, S, info, true);
4791 outs() << "\t\t ivars " << format("0x%08" PRIx32, objc_class->ivars);
4792 if (p != nullptr) {
4793 if (left > sizeof(struct objc_ivar_list_t)) {
4794 outs() << "\n";
4795 memcpy(&objc_ivar_list, p, sizeof(struct objc_ivar_list_t));
4796 } else {
4797 outs() << " (entends past the end of the section)\n";
4798 memset(&objc_ivar_list, '\0', sizeof(struct objc_ivar_list_t));
4799 memcpy(&objc_ivar_list, p, left);
4800 }
4801 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4802 swapStruct(objc_ivar_list);
4803 outs() << "\t\t ivar_count " << objc_ivar_list.ivar_count << "\n";
4804 ivar_list = p + sizeof(struct objc_ivar_list_t);
4805 for (i = 0; i < objc_ivar_list.ivar_count; i++) {
4806 if ((i + 1) * sizeof(struct objc_ivar_t) > left) {
4807 outs() << "\t\t remaining ivar's extend past the of the section\n";
4808 break;
4809 }
4810 memcpy(&ivar, ivar_list + i * sizeof(struct objc_ivar_t),
4811 sizeof(struct objc_ivar_t));
4812 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4813 swapStruct(ivar);
4814
4815 outs() << "\t\t\tivar_name " << format("0x%08" PRIx32, ivar.ivar_name);
4816 if (info->verbose) {
4817 name = get_pointer_32(ivar.ivar_name, offset, xleft, S, info, true);
4818 if (name != nullptr)
4819 outs() << format(" %.*s", xleft, name);
4820 else
4821 outs() << " (not in an __OBJC section)";
4822 }
4823 outs() << "\n";
4824
4825 outs() << "\t\t\tivar_type " << format("0x%08" PRIx32, ivar.ivar_type);
4826 if (info->verbose) {
4827 name = get_pointer_32(ivar.ivar_type, offset, xleft, S, info, true);
4828 if (name != nullptr)
4829 outs() << format(" %.*s", xleft, name);
4830 else
4831 outs() << " (not in an __OBJC section)";
4832 }
4833 outs() << "\n";
4834
4835 outs() << "\t\t ivar_offset "
4836 << format("0x%08" PRIx32, ivar.ivar_offset) << "\n";
4837 }
4838 } else {
4839 outs() << " (not in an __OBJC section)\n";
4840 }
4841
4842 outs() << "\t\t methods " << format("0x%08" PRIx32, objc_class->methodLists);
4843 if (print_method_list(objc_class->methodLists, info))
4844 outs() << " (not in an __OBJC section)\n";
4845
4846 outs() << "\t\t cache " << format("0x%08" PRIx32, objc_class->cache)
4847 << "\n";
4848
4849 outs() << "\t\tprotocols " << format("0x%08" PRIx32, objc_class->protocols);
4850 if (print_protocol_list(objc_class->protocols, 16, info))
4851 outs() << " (not in an __OBJC section)\n";
4852}
4853
4854static void print_objc_objc_category_t(struct objc_category_t *objc_category,
4855 struct DisassembleInfo *info) {
4856 uint32_t offset, left;
4857 const char *name;
4858 SectionRef S;
4859
4860 outs() << "\t category name "
4861 << format("0x%08" PRIx32, objc_category->category_name);
4862 if (info->verbose) {
4863 name = get_pointer_32(objc_category->category_name, offset, left, S, info,
4864 true);
4865 if (name != nullptr)
4866 outs() << format(" %.*s", left, name);
4867 else
4868 outs() << " (not in an __OBJC section)";
4869 }
4870 outs() << "\n";
4871
4872 outs() << "\t\t class name "
4873 << format("0x%08" PRIx32, objc_category->class_name);
4874 if (info->verbose) {
4875 name =
4876 get_pointer_32(objc_category->class_name, offset, left, S, info, true);
4877 if (name != nullptr)
4878 outs() << format(" %.*s", left, name);
4879 else
4880 outs() << " (not in an __OBJC section)";
4881 }
4882 outs() << "\n";
4883
4884 outs() << "\t instance methods "
4885 << format("0x%08" PRIx32, objc_category->instance_methods);
4886 if (print_method_list(objc_category->instance_methods, info))
4887 outs() << " (not in an __OBJC section)\n";
4888
4889 outs() << "\t class methods "
4890 << format("0x%08" PRIx32, objc_category->class_methods);
4891 if (print_method_list(objc_category->class_methods, info))
4892 outs() << " (not in an __OBJC section)\n";
4893}
4894
Kevin Enderby0fc11822015-04-01 20:57:01 +00004895static void print_category64_t(uint64_t p, struct DisassembleInfo *info) {
4896 struct category64_t c;
4897 const char *r;
4898 uint32_t offset, xoffset, left;
4899 SectionRef S, xS;
4900 const char *name, *sym_name;
4901 uint64_t n_value;
4902
4903 r = get_pointer_64(p, offset, left, S, info);
4904 if (r == nullptr)
4905 return;
4906 memset(&c, '\0', sizeof(struct category64_t));
4907 if (left < sizeof(struct category64_t)) {
4908 memcpy(&c, r, left);
4909 outs() << " (category_t entends past the end of the section)\n";
4910 } else
4911 memcpy(&c, r, sizeof(struct category64_t));
4912 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4913 swapStruct(c);
4914
4915 outs() << " name ";
4916 sym_name = get_symbol_64(offset + offsetof(struct category64_t, name), S,
4917 info, n_value, c.name);
4918 if (n_value != 0) {
4919 if (info->verbose && sym_name != nullptr)
4920 outs() << sym_name;
4921 else
4922 outs() << format("0x%" PRIx64, n_value);
4923 if (c.name != 0)
4924 outs() << " + " << format("0x%" PRIx64, c.name);
4925 } else
4926 outs() << format("0x%" PRIx64, c.name);
4927 name = get_pointer_64(c.name + n_value, xoffset, left, xS, info);
4928 if (name != nullptr)
4929 outs() << format(" %.*s", left, name);
4930 outs() << "\n";
4931
4932 outs() << " cls ";
4933 sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls), S, info,
4934 n_value, c.cls);
4935 if (n_value != 0) {
4936 if (info->verbose && sym_name != nullptr)
4937 outs() << sym_name;
4938 else
4939 outs() << format("0x%" PRIx64, n_value);
4940 if (c.cls != 0)
4941 outs() << " + " << format("0x%" PRIx64, c.cls);
4942 } else
4943 outs() << format("0x%" PRIx64, c.cls);
4944 outs() << "\n";
4945 if (c.cls + n_value != 0)
4946 print_class64_t(c.cls + n_value, info);
4947
4948 outs() << " instanceMethods ";
4949 sym_name =
4950 get_symbol_64(offset + offsetof(struct category64_t, instanceMethods), S,
4951 info, n_value, c.instanceMethods);
4952 if (n_value != 0) {
4953 if (info->verbose && sym_name != nullptr)
4954 outs() << sym_name;
4955 else
4956 outs() << format("0x%" PRIx64, n_value);
4957 if (c.instanceMethods != 0)
4958 outs() << " + " << format("0x%" PRIx64, c.instanceMethods);
4959 } else
4960 outs() << format("0x%" PRIx64, c.instanceMethods);
4961 outs() << "\n";
4962 if (c.instanceMethods + n_value != 0)
4963 print_method_list64_t(c.instanceMethods + n_value, info, "");
4964
4965 outs() << " classMethods ";
4966 sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods),
4967 S, info, n_value, c.classMethods);
4968 if (n_value != 0) {
4969 if (info->verbose && sym_name != nullptr)
4970 outs() << sym_name;
4971 else
4972 outs() << format("0x%" PRIx64, n_value);
4973 if (c.classMethods != 0)
4974 outs() << " + " << format("0x%" PRIx64, c.classMethods);
4975 } else
4976 outs() << format("0x%" PRIx64, c.classMethods);
4977 outs() << "\n";
4978 if (c.classMethods + n_value != 0)
4979 print_method_list64_t(c.classMethods + n_value, info, "");
4980
4981 outs() << " protocols ";
4982 sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols), S,
4983 info, n_value, c.protocols);
4984 if (n_value != 0) {
4985 if (info->verbose && sym_name != nullptr)
4986 outs() << sym_name;
4987 else
4988 outs() << format("0x%" PRIx64, n_value);
4989 if (c.protocols != 0)
4990 outs() << " + " << format("0x%" PRIx64, c.protocols);
4991 } else
4992 outs() << format("0x%" PRIx64, c.protocols);
4993 outs() << "\n";
4994 if (c.protocols + n_value != 0)
4995 print_protocol_list64_t(c.protocols + n_value, info);
4996
4997 outs() << "instanceProperties ";
4998 sym_name =
4999 get_symbol_64(offset + offsetof(struct category64_t, instanceProperties),
5000 S, info, n_value, c.instanceProperties);
5001 if (n_value != 0) {
5002 if (info->verbose && sym_name != nullptr)
5003 outs() << sym_name;
5004 else
5005 outs() << format("0x%" PRIx64, n_value);
5006 if (c.instanceProperties != 0)
5007 outs() << " + " << format("0x%" PRIx64, c.instanceProperties);
5008 } else
5009 outs() << format("0x%" PRIx64, c.instanceProperties);
5010 outs() << "\n";
5011 if (c.instanceProperties + n_value != 0)
5012 print_objc_property_list64(c.instanceProperties + n_value, info);
5013}
5014
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005015static void print_category32_t(uint32_t p, struct DisassembleInfo *info) {
5016 struct category32_t c;
5017 const char *r;
5018 uint32_t offset, left;
5019 SectionRef S, xS;
5020 const char *name;
5021
5022 r = get_pointer_32(p, offset, left, S, info);
5023 if (r == nullptr)
5024 return;
5025 memset(&c, '\0', sizeof(struct category32_t));
5026 if (left < sizeof(struct category32_t)) {
5027 memcpy(&c, r, left);
5028 outs() << " (category_t entends past the end of the section)\n";
5029 } else
5030 memcpy(&c, r, sizeof(struct category32_t));
5031 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5032 swapStruct(c);
5033
5034 outs() << " name " << format("0x%" PRIx32, c.name);
5035 name = get_symbol_32(offset + offsetof(struct category32_t, name), S, info,
5036 c.name);
Hans Wennborgcc9deb42015-09-29 18:02:48 +00005037 if (name)
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005038 outs() << " " << name;
5039 outs() << "\n";
5040
5041 outs() << " cls " << format("0x%" PRIx32, c.cls) << "\n";
5042 if (c.cls != 0)
5043 print_class32_t(c.cls, info);
5044 outs() << " instanceMethods " << format("0x%" PRIx32, c.instanceMethods)
5045 << "\n";
5046 if (c.instanceMethods != 0)
5047 print_method_list32_t(c.instanceMethods, info, "");
5048 outs() << " classMethods " << format("0x%" PRIx32, c.classMethods)
5049 << "\n";
5050 if (c.classMethods != 0)
5051 print_method_list32_t(c.classMethods, info, "");
5052 outs() << " protocols " << format("0x%" PRIx32, c.protocols) << "\n";
5053 if (c.protocols != 0)
5054 print_protocol_list32_t(c.protocols, info);
5055 outs() << "instanceProperties " << format("0x%" PRIx32, c.instanceProperties)
5056 << "\n";
5057 if (c.instanceProperties != 0)
5058 print_objc_property_list32(c.instanceProperties, info);
5059}
5060
Kevin Enderby0fc11822015-04-01 20:57:01 +00005061static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) {
5062 uint32_t i, left, offset, xoffset;
5063 uint64_t p, n_value;
5064 struct message_ref64 mr;
5065 const char *name, *sym_name;
5066 const char *r;
5067 SectionRef xS;
5068
5069 if (S == SectionRef())
5070 return;
5071
5072 StringRef SectName;
5073 S.getName(SectName);
5074 DataRefImpl Ref = S.getRawDataRefImpl();
5075 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5076 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5077 offset = 0;
5078 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
5079 p = S.getAddress() + i;
5080 r = get_pointer_64(p, offset, left, S, info);
5081 if (r == nullptr)
5082 return;
5083 memset(&mr, '\0', sizeof(struct message_ref64));
5084 if (left < sizeof(struct message_ref64)) {
5085 memcpy(&mr, r, left);
5086 outs() << " (message_ref entends past the end of the section)\n";
5087 } else
5088 memcpy(&mr, r, sizeof(struct message_ref64));
5089 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5090 swapStruct(mr);
5091
5092 outs() << " imp ";
5093 name = get_symbol_64(offset + offsetof(struct message_ref64, imp), S, info,
5094 n_value, mr.imp);
5095 if (n_value != 0) {
5096 outs() << format("0x%" PRIx64, n_value) << " ";
5097 if (mr.imp != 0)
5098 outs() << "+ " << format("0x%" PRIx64, mr.imp) << " ";
5099 } else
5100 outs() << format("0x%" PRIx64, mr.imp) << " ";
5101 if (name != nullptr)
5102 outs() << " " << name;
5103 outs() << "\n";
5104
5105 outs() << " sel ";
5106 sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel), S,
5107 info, n_value, mr.sel);
5108 if (n_value != 0) {
5109 if (info->verbose && sym_name != nullptr)
5110 outs() << sym_name;
5111 else
5112 outs() << format("0x%" PRIx64, n_value);
5113 if (mr.sel != 0)
5114 outs() << " + " << format("0x%" PRIx64, mr.sel);
5115 } else
5116 outs() << format("0x%" PRIx64, mr.sel);
5117 name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info);
5118 if (name != nullptr)
5119 outs() << format(" %.*s", left, name);
5120 outs() << "\n";
5121
5122 offset += sizeof(struct message_ref64);
5123 }
5124}
5125
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005126static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) {
5127 uint32_t i, left, offset, xoffset, p;
5128 struct message_ref32 mr;
5129 const char *name, *r;
5130 SectionRef xS;
5131
5132 if (S == SectionRef())
5133 return;
5134
5135 StringRef SectName;
5136 S.getName(SectName);
5137 DataRefImpl Ref = S.getRawDataRefImpl();
5138 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5139 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5140 offset = 0;
5141 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
5142 p = S.getAddress() + i;
5143 r = get_pointer_32(p, offset, left, S, info);
5144 if (r == nullptr)
5145 return;
5146 memset(&mr, '\0', sizeof(struct message_ref32));
5147 if (left < sizeof(struct message_ref32)) {
5148 memcpy(&mr, r, left);
5149 outs() << " (message_ref entends past the end of the section)\n";
5150 } else
5151 memcpy(&mr, r, sizeof(struct message_ref32));
5152 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5153 swapStruct(mr);
5154
5155 outs() << " imp " << format("0x%" PRIx32, mr.imp);
5156 name = get_symbol_32(offset + offsetof(struct message_ref32, imp), S, info,
5157 mr.imp);
5158 if (name != nullptr)
5159 outs() << " " << name;
5160 outs() << "\n";
5161
5162 outs() << " sel " << format("0x%" PRIx32, mr.sel);
5163 name = get_pointer_32(mr.sel, xoffset, left, xS, info);
5164 if (name != nullptr)
5165 outs() << " " << name;
5166 outs() << "\n";
5167
5168 offset += sizeof(struct message_ref32);
5169 }
5170}
5171
Kevin Enderby0fc11822015-04-01 20:57:01 +00005172static void print_image_info64(SectionRef S, struct DisassembleInfo *info) {
5173 uint32_t left, offset, swift_version;
5174 uint64_t p;
5175 struct objc_image_info64 o;
5176 const char *r;
5177
Kevin Enderbyaf7c9d02015-10-09 16:48:44 +00005178 if (S == SectionRef())
5179 return;
5180
Kevin Enderby0fc11822015-04-01 20:57:01 +00005181 StringRef SectName;
5182 S.getName(SectName);
5183 DataRefImpl Ref = S.getRawDataRefImpl();
5184 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5185 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5186 p = S.getAddress();
5187 r = get_pointer_64(p, offset, left, S, info);
5188 if (r == nullptr)
5189 return;
5190 memset(&o, '\0', sizeof(struct objc_image_info64));
5191 if (left < sizeof(struct objc_image_info64)) {
5192 memcpy(&o, r, left);
5193 outs() << " (objc_image_info entends past the end of the section)\n";
5194 } else
5195 memcpy(&o, r, sizeof(struct objc_image_info64));
5196 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5197 swapStruct(o);
5198 outs() << " version " << o.version << "\n";
5199 outs() << " flags " << format("0x%" PRIx32, o.flags);
5200 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5201 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5202 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5203 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5204 swift_version = (o.flags >> 8) & 0xff;
5205 if (swift_version != 0) {
5206 if (swift_version == 1)
5207 outs() << " Swift 1.0";
5208 else if (swift_version == 2)
5209 outs() << " Swift 1.1";
5210 else
5211 outs() << " unknown future Swift version (" << swift_version << ")";
5212 }
5213 outs() << "\n";
5214}
5215
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005216static void print_image_info32(SectionRef S, struct DisassembleInfo *info) {
5217 uint32_t left, offset, swift_version, p;
5218 struct objc_image_info32 o;
5219 const char *r;
5220
Kevin Enderby19be2512016-04-21 19:49:29 +00005221 if (S == SectionRef())
5222 return;
5223
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005224 StringRef SectName;
5225 S.getName(SectName);
5226 DataRefImpl Ref = S.getRawDataRefImpl();
5227 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5228 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5229 p = S.getAddress();
5230 r = get_pointer_32(p, offset, left, S, info);
5231 if (r == nullptr)
5232 return;
5233 memset(&o, '\0', sizeof(struct objc_image_info32));
5234 if (left < sizeof(struct objc_image_info32)) {
5235 memcpy(&o, r, left);
5236 outs() << " (objc_image_info entends past the end of the section)\n";
5237 } else
5238 memcpy(&o, r, sizeof(struct objc_image_info32));
5239 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5240 swapStruct(o);
5241 outs() << " version " << o.version << "\n";
5242 outs() << " flags " << format("0x%" PRIx32, o.flags);
5243 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5244 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5245 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5246 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5247 swift_version = (o.flags >> 8) & 0xff;
5248 if (swift_version != 0) {
5249 if (swift_version == 1)
5250 outs() << " Swift 1.0";
5251 else if (swift_version == 2)
5252 outs() << " Swift 1.1";
5253 else
5254 outs() << " unknown future Swift version (" << swift_version << ")";
5255 }
5256 outs() << "\n";
5257}
5258
Kevin Enderby846c0002015-04-16 17:19:59 +00005259static void print_image_info(SectionRef S, struct DisassembleInfo *info) {
5260 uint32_t left, offset, p;
5261 struct imageInfo_t o;
5262 const char *r;
5263
5264 StringRef SectName;
5265 S.getName(SectName);
5266 DataRefImpl Ref = S.getRawDataRefImpl();
5267 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5268 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5269 p = S.getAddress();
5270 r = get_pointer_32(p, offset, left, S, info);
5271 if (r == nullptr)
5272 return;
5273 memset(&o, '\0', sizeof(struct imageInfo_t));
5274 if (left < sizeof(struct imageInfo_t)) {
5275 memcpy(&o, r, left);
5276 outs() << " (imageInfo entends past the end of the section)\n";
5277 } else
5278 memcpy(&o, r, sizeof(struct imageInfo_t));
5279 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5280 swapStruct(o);
5281 outs() << " version " << o.version << "\n";
5282 outs() << " flags " << format("0x%" PRIx32, o.flags);
5283 if (o.flags & 0x1)
5284 outs() << " F&C";
5285 if (o.flags & 0x2)
5286 outs() << " GC";
5287 if (o.flags & 0x4)
5288 outs() << " GC-only";
5289 else
5290 outs() << " RR";
5291 outs() << "\n";
5292}
5293
Kevin Enderby0fc11822015-04-01 20:57:01 +00005294static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) {
5295 SymbolAddressMap AddrMap;
5296 if (verbose)
5297 CreateSymbolAddressMap(O, &AddrMap);
5298
5299 std::vector<SectionRef> Sections;
5300 for (const SectionRef &Section : O->sections()) {
5301 StringRef SectName;
5302 Section.getName(SectName);
5303 Sections.push_back(Section);
5304 }
5305
5306 struct DisassembleInfo info;
5307 // Set up the block of info used by the Symbolizer call backs.
5308 info.verbose = verbose;
5309 info.O = O;
5310 info.AddrMap = &AddrMap;
5311 info.Sections = &Sections;
5312 info.class_name = nullptr;
5313 info.selector_name = nullptr;
5314 info.method = nullptr;
5315 info.demangled_name = nullptr;
5316 info.bindtable = nullptr;
5317 info.adrp_addr = 0;
5318 info.adrp_inst = 0;
5319
Kevin Enderbyaac75382015-10-08 16:56:35 +00005320 info.depth = 0;
Davide Italiano62507042015-12-11 22:27:59 +00005321 SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5322 if (CL == SectionRef())
5323 CL = get_section(O, "__DATA", "__objc_classlist");
Kevin Enderby5879a482017-02-09 17:56:26 +00005324 if (CL == SectionRef())
5325 CL = get_section(O, "__DATA_CONST", "__objc_classlist");
5326 if (CL == SectionRef())
5327 CL = get_section(O, "__DATA_DIRTY", "__objc_classlist");
Davide Italiano62507042015-12-11 22:27:59 +00005328 info.S = CL;
5329 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005330
Davide Italiano62507042015-12-11 22:27:59 +00005331 SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5332 if (CR == SectionRef())
5333 CR = get_section(O, "__DATA", "__objc_classrefs");
Kevin Enderby5879a482017-02-09 17:56:26 +00005334 if (CR == SectionRef())
5335 CR = get_section(O, "__DATA_CONST", "__objc_classrefs");
5336 if (CR == SectionRef())
5337 CR = get_section(O, "__DATA_DIRTY", "__objc_classrefs");
Davide Italiano62507042015-12-11 22:27:59 +00005338 info.S = CR;
5339 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005340
Davide Italiano62507042015-12-11 22:27:59 +00005341 SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5342 if (SR == SectionRef())
5343 SR = get_section(O, "__DATA", "__objc_superrefs");
Kevin Enderby5879a482017-02-09 17:56:26 +00005344 if (SR == SectionRef())
5345 SR = get_section(O, "__DATA_CONST", "__objc_superrefs");
5346 if (SR == SectionRef())
5347 SR = get_section(O, "__DATA_DIRTY", "__objc_superrefs");
Davide Italiano62507042015-12-11 22:27:59 +00005348 info.S = SR;
5349 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005350
Davide Italiano62507042015-12-11 22:27:59 +00005351 SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5352 if (CA == SectionRef())
5353 CA = get_section(O, "__DATA", "__objc_catlist");
Kevin Enderby5879a482017-02-09 17:56:26 +00005354 if (CA == SectionRef())
5355 CA = get_section(O, "__DATA_CONST", "__objc_catlist");
5356 if (CA == SectionRef())
5357 CA = get_section(O, "__DATA_DIRTY", "__objc_catlist");
Davide Italiano62507042015-12-11 22:27:59 +00005358 info.S = CA;
5359 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005360
Davide Italiano62507042015-12-11 22:27:59 +00005361 SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5362 if (PL == SectionRef())
5363 PL = get_section(O, "__DATA", "__objc_protolist");
Kevin Enderby5879a482017-02-09 17:56:26 +00005364 if (PL == SectionRef())
5365 PL = get_section(O, "__DATA_CONST", "__objc_protolist");
5366 if (PL == SectionRef())
5367 PL = get_section(O, "__DATA_DIRTY", "__objc_protolist");
Davide Italiano62507042015-12-11 22:27:59 +00005368 info.S = PL;
5369 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005370
Davide Italiano62507042015-12-11 22:27:59 +00005371 SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5372 if (MR == SectionRef())
5373 MR = get_section(O, "__DATA", "__objc_msgrefs");
Kevin Enderby5879a482017-02-09 17:56:26 +00005374 if (MR == SectionRef())
5375 MR = get_section(O, "__DATA_CONST", "__objc_msgrefs");
5376 if (MR == SectionRef())
5377 MR = get_section(O, "__DATA_DIRTY", "__objc_msgrefs");
Davide Italiano62507042015-12-11 22:27:59 +00005378 info.S = MR;
5379 print_message_refs64(MR, &info);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005380
Davide Italiano62507042015-12-11 22:27:59 +00005381 SectionRef II = get_section(O, "__OBJC2", "__image_info");
5382 if (II == SectionRef())
5383 II = get_section(O, "__DATA", "__objc_imageinfo");
Kevin Enderby5879a482017-02-09 17:56:26 +00005384 if (II == SectionRef())
5385 II = get_section(O, "__DATA_CONST", "__objc_imageinfo");
5386 if (II == SectionRef())
5387 II = get_section(O, "__DATA_DIRTY", "__objc_imageinfo");
Davide Italiano62507042015-12-11 22:27:59 +00005388 info.S = II;
5389 print_image_info64(II, &info);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005390}
5391
5392static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) {
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005393 SymbolAddressMap AddrMap;
5394 if (verbose)
5395 CreateSymbolAddressMap(O, &AddrMap);
5396
5397 std::vector<SectionRef> Sections;
5398 for (const SectionRef &Section : O->sections()) {
5399 StringRef SectName;
5400 Section.getName(SectName);
5401 Sections.push_back(Section);
5402 }
5403
5404 struct DisassembleInfo info;
5405 // Set up the block of info used by the Symbolizer call backs.
5406 info.verbose = verbose;
5407 info.O = O;
5408 info.AddrMap = &AddrMap;
5409 info.Sections = &Sections;
5410 info.class_name = nullptr;
5411 info.selector_name = nullptr;
5412 info.method = nullptr;
5413 info.demangled_name = nullptr;
5414 info.bindtable = nullptr;
5415 info.adrp_addr = 0;
5416 info.adrp_inst = 0;
5417
Kevin Enderby5879a482017-02-09 17:56:26 +00005418 SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5419 if (CL == SectionRef())
5420 CL = get_section(O, "__DATA", "__objc_classlist");
5421 if (CL == SectionRef())
5422 CL = get_section(O, "__DATA_CONST", "__objc_classlist");
5423 if (CL == SectionRef())
5424 CL = get_section(O, "__DATA_DIRTY", "__objc_classlist");
5425 info.S = CL;
5426 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005427
Kevin Enderby5879a482017-02-09 17:56:26 +00005428 SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5429 if (CR == SectionRef())
5430 CR = get_section(O, "__DATA", "__objc_classrefs");
5431 if (CR == SectionRef())
5432 CR = get_section(O, "__DATA_CONST", "__objc_classrefs");
5433 if (CR == SectionRef())
5434 CR = get_section(O, "__DATA_DIRTY", "__objc_classrefs");
5435 info.S = CR;
5436 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005437
Kevin Enderby5879a482017-02-09 17:56:26 +00005438 SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5439 if (SR == SectionRef())
5440 SR = get_section(O, "__DATA", "__objc_superrefs");
5441 if (SR == SectionRef())
5442 SR = get_section(O, "__DATA_CONST", "__objc_superrefs");
5443 if (SR == SectionRef())
5444 SR = get_section(O, "__DATA_DIRTY", "__objc_superrefs");
5445 info.S = SR;
5446 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005447
Kevin Enderby5879a482017-02-09 17:56:26 +00005448 SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5449 if (CA == SectionRef())
5450 CA = get_section(O, "__DATA", "__objc_catlist");
5451 if (CA == SectionRef())
5452 CA = get_section(O, "__DATA_CONST", "__objc_catlist");
5453 if (CA == SectionRef())
5454 CA = get_section(O, "__DATA_DIRTY", "__objc_catlist");
5455 info.S = CA;
5456 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005457
Kevin Enderby5879a482017-02-09 17:56:26 +00005458 SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5459 if (PL == SectionRef())
5460 PL = get_section(O, "__DATA", "__objc_protolist");
5461 if (PL == SectionRef())
5462 PL = get_section(O, "__DATA_CONST", "__objc_protolist");
5463 if (PL == SectionRef())
5464 PL = get_section(O, "__DATA_DIRTY", "__objc_protolist");
5465 info.S = PL;
5466 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005467
Kevin Enderby5879a482017-02-09 17:56:26 +00005468 SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5469 if (MR == SectionRef())
5470 MR = get_section(O, "__DATA", "__objc_msgrefs");
5471 if (MR == SectionRef())
5472 MR = get_section(O, "__DATA_CONST", "__objc_msgrefs");
5473 if (MR == SectionRef())
5474 MR = get_section(O, "__DATA_DIRTY", "__objc_msgrefs");
5475 info.S = MR;
5476 print_message_refs32(MR, &info);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005477
Kevin Enderby5879a482017-02-09 17:56:26 +00005478 SectionRef II = get_section(O, "__OBJC2", "__image_info");
5479 if (II == SectionRef())
5480 II = get_section(O, "__DATA", "__objc_imageinfo");
5481 if (II == SectionRef())
5482 II = get_section(O, "__DATA_CONST", "__objc_imageinfo");
5483 if (II == SectionRef())
5484 II = get_section(O, "__DATA_DIRTY", "__objc_imageinfo");
5485 info.S = II;
5486 print_image_info32(II, &info);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005487}
5488
5489static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) {
Kevin Enderby846c0002015-04-16 17:19:59 +00005490 uint32_t i, j, p, offset, xoffset, left, defs_left, def;
5491 const char *r, *name, *defs;
5492 struct objc_module_t module;
5493 SectionRef S, xS;
5494 struct objc_symtab_t symtab;
5495 struct objc_class_t objc_class;
5496 struct objc_category_t objc_category;
5497
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005498 outs() << "Objective-C segment\n";
Kevin Enderby846c0002015-04-16 17:19:59 +00005499 S = get_section(O, "__OBJC", "__module_info");
5500 if (S == SectionRef())
5501 return false;
5502
5503 SymbolAddressMap AddrMap;
5504 if (verbose)
5505 CreateSymbolAddressMap(O, &AddrMap);
5506
5507 std::vector<SectionRef> Sections;
5508 for (const SectionRef &Section : O->sections()) {
5509 StringRef SectName;
5510 Section.getName(SectName);
5511 Sections.push_back(Section);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005512 }
Kevin Enderby846c0002015-04-16 17:19:59 +00005513
5514 struct DisassembleInfo info;
5515 // Set up the block of info used by the Symbolizer call backs.
5516 info.verbose = verbose;
5517 info.O = O;
5518 info.AddrMap = &AddrMap;
5519 info.Sections = &Sections;
5520 info.class_name = nullptr;
5521 info.selector_name = nullptr;
5522 info.method = nullptr;
5523 info.demangled_name = nullptr;
5524 info.bindtable = nullptr;
5525 info.adrp_addr = 0;
5526 info.adrp_inst = 0;
5527
5528 for (i = 0; i < S.getSize(); i += sizeof(struct objc_module_t)) {
5529 p = S.getAddress() + i;
5530 r = get_pointer_32(p, offset, left, S, &info, true);
5531 if (r == nullptr)
5532 return true;
5533 memset(&module, '\0', sizeof(struct objc_module_t));
5534 if (left < sizeof(struct objc_module_t)) {
5535 memcpy(&module, r, left);
5536 outs() << " (module extends past end of __module_info section)\n";
5537 } else
5538 memcpy(&module, r, sizeof(struct objc_module_t));
5539 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5540 swapStruct(module);
5541
5542 outs() << "Module " << format("0x%" PRIx32, p) << "\n";
5543 outs() << " version " << module.version << "\n";
5544 outs() << " size " << module.size << "\n";
5545 outs() << " name ";
5546 name = get_pointer_32(module.name, xoffset, left, xS, &info, true);
5547 if (name != nullptr)
5548 outs() << format("%.*s", left, name);
5549 else
5550 outs() << format("0x%08" PRIx32, module.name)
5551 << "(not in an __OBJC section)";
5552 outs() << "\n";
5553
5554 r = get_pointer_32(module.symtab, xoffset, left, xS, &info, true);
5555 if (module.symtab == 0 || r == nullptr) {
5556 outs() << " symtab " << format("0x%08" PRIx32, module.symtab)
5557 << " (not in an __OBJC section)\n";
5558 continue;
5559 }
5560 outs() << " symtab " << format("0x%08" PRIx32, module.symtab) << "\n";
5561 memset(&symtab, '\0', sizeof(struct objc_symtab_t));
5562 defs_left = 0;
5563 defs = nullptr;
5564 if (left < sizeof(struct objc_symtab_t)) {
5565 memcpy(&symtab, r, left);
5566 outs() << "\tsymtab extends past end of an __OBJC section)\n";
5567 } else {
5568 memcpy(&symtab, r, sizeof(struct objc_symtab_t));
5569 if (left > sizeof(struct objc_symtab_t)) {
5570 defs_left = left - sizeof(struct objc_symtab_t);
5571 defs = r + sizeof(struct objc_symtab_t);
5572 }
5573 }
5574 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5575 swapStruct(symtab);
5576
5577 outs() << "\tsel_ref_cnt " << symtab.sel_ref_cnt << "\n";
5578 r = get_pointer_32(symtab.refs, xoffset, left, xS, &info, true);
5579 outs() << "\trefs " << format("0x%08" PRIx32, symtab.refs);
5580 if (r == nullptr)
5581 outs() << " (not in an __OBJC section)";
5582 outs() << "\n";
5583 outs() << "\tcls_def_cnt " << symtab.cls_def_cnt << "\n";
5584 outs() << "\tcat_def_cnt " << symtab.cat_def_cnt << "\n";
5585 if (symtab.cls_def_cnt > 0)
5586 outs() << "\tClass Definitions\n";
5587 for (j = 0; j < symtab.cls_def_cnt; j++) {
5588 if ((j + 1) * sizeof(uint32_t) > defs_left) {
5589 outs() << "\t(remaining class defs entries entends past the end of the "
5590 << "section)\n";
5591 break;
5592 }
5593 memcpy(&def, defs + j * sizeof(uint32_t), sizeof(uint32_t));
5594 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5595 sys::swapByteOrder(def);
5596
5597 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5598 outs() << "\tdefs[" << j << "] " << format("0x%08" PRIx32, def);
5599 if (r != nullptr) {
5600 if (left > sizeof(struct objc_class_t)) {
5601 outs() << "\n";
5602 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5603 } else {
5604 outs() << " (entends past the end of the section)\n";
5605 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5606 memcpy(&objc_class, r, left);
5607 }
5608 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5609 swapStruct(objc_class);
5610 print_objc_class_t(&objc_class, &info);
5611 } else {
5612 outs() << "(not in an __OBJC section)\n";
5613 }
5614
5615 if (CLS_GETINFO(&objc_class, CLS_CLASS)) {
5616 outs() << "\tMeta Class";
5617 r = get_pointer_32(objc_class.isa, xoffset, left, xS, &info, true);
5618 if (r != nullptr) {
5619 if (left > sizeof(struct objc_class_t)) {
5620 outs() << "\n";
5621 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5622 } else {
5623 outs() << " (entends past the end of the section)\n";
5624 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5625 memcpy(&objc_class, r, left);
5626 }
5627 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5628 swapStruct(objc_class);
5629 print_objc_class_t(&objc_class, &info);
5630 } else {
5631 outs() << "(not in an __OBJC section)\n";
5632 }
5633 }
5634 }
5635 if (symtab.cat_def_cnt > 0)
5636 outs() << "\tCategory Definitions\n";
5637 for (j = 0; j < symtab.cat_def_cnt; j++) {
5638 if ((j + symtab.cls_def_cnt + 1) * sizeof(uint32_t) > defs_left) {
5639 outs() << "\t(remaining category defs entries entends past the end of "
5640 << "the section)\n";
5641 break;
5642 }
5643 memcpy(&def, defs + (j + symtab.cls_def_cnt) * sizeof(uint32_t),
5644 sizeof(uint32_t));
5645 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5646 sys::swapByteOrder(def);
5647
5648 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5649 outs() << "\tdefs[" << j + symtab.cls_def_cnt << "] "
5650 << format("0x%08" PRIx32, def);
5651 if (r != nullptr) {
5652 if (left > sizeof(struct objc_category_t)) {
5653 outs() << "\n";
5654 memcpy(&objc_category, r, sizeof(struct objc_category_t));
5655 } else {
5656 outs() << " (entends past the end of the section)\n";
5657 memset(&objc_category, '\0', sizeof(struct objc_category_t));
5658 memcpy(&objc_category, r, left);
5659 }
5660 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5661 swapStruct(objc_category);
5662 print_objc_objc_category_t(&objc_category, &info);
5663 } else {
5664 outs() << "(not in an __OBJC section)\n";
5665 }
5666 }
5667 }
5668 const SectionRef II = get_section(O, "__OBJC", "__image_info");
5669 if (II != SectionRef())
5670 print_image_info(II, &info);
5671
5672 return true;
Kevin Enderby0fc11822015-04-01 20:57:01 +00005673}
5674
Kevin Enderby4ad9bde2015-04-16 22:33:20 +00005675static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
5676 uint32_t size, uint32_t addr) {
5677 SymbolAddressMap AddrMap;
5678 CreateSymbolAddressMap(O, &AddrMap);
5679
5680 std::vector<SectionRef> Sections;
5681 for (const SectionRef &Section : O->sections()) {
5682 StringRef SectName;
5683 Section.getName(SectName);
5684 Sections.push_back(Section);
5685 }
5686
5687 struct DisassembleInfo info;
5688 // Set up the block of info used by the Symbolizer call backs.
5689 info.verbose = true;
5690 info.O = O;
5691 info.AddrMap = &AddrMap;
5692 info.Sections = &Sections;
5693 info.class_name = nullptr;
5694 info.selector_name = nullptr;
5695 info.method = nullptr;
5696 info.demangled_name = nullptr;
5697 info.bindtable = nullptr;
5698 info.adrp_addr = 0;
5699 info.adrp_inst = 0;
5700
5701 const char *p;
5702 struct objc_protocol_t protocol;
5703 uint32_t left, paddr;
5704 for (p = sect; p < sect + size; p += sizeof(struct objc_protocol_t)) {
5705 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
5706 left = size - (p - sect);
5707 if (left < sizeof(struct objc_protocol_t)) {
5708 outs() << "Protocol extends past end of __protocol section\n";
5709 memcpy(&protocol, p, left);
5710 } else
5711 memcpy(&protocol, p, sizeof(struct objc_protocol_t));
5712 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5713 swapStruct(protocol);
5714 paddr = addr + (p - sect);
5715 outs() << "Protocol " << format("0x%" PRIx32, paddr);
5716 if (print_protocol(paddr, 0, &info))
5717 outs() << "(not in an __OBJC section)\n";
5718 }
5719}
5720
Kevin Enderby9873e2c2016-05-23 21:34:12 +00005721#ifdef HAVE_LIBXAR
5722inline void swapStruct(struct xar_header &xar) {
5723 sys::swapByteOrder(xar.magic);
5724 sys::swapByteOrder(xar.size);
5725 sys::swapByteOrder(xar.version);
5726 sys::swapByteOrder(xar.toc_length_compressed);
5727 sys::swapByteOrder(xar.toc_length_uncompressed);
5728 sys::swapByteOrder(xar.cksum_alg);
5729}
5730
5731static void PrintModeVerbose(uint32_t mode) {
5732 switch(mode & S_IFMT){
5733 case S_IFDIR:
5734 outs() << "d";
5735 break;
5736 case S_IFCHR:
5737 outs() << "c";
5738 break;
5739 case S_IFBLK:
5740 outs() << "b";
5741 break;
5742 case S_IFREG:
5743 outs() << "-";
5744 break;
5745 case S_IFLNK:
5746 outs() << "l";
5747 break;
5748 case S_IFSOCK:
5749 outs() << "s";
5750 break;
5751 default:
5752 outs() << "?";
5753 break;
5754 }
5755
5756 /* owner permissions */
5757 if(mode & S_IREAD)
5758 outs() << "r";
5759 else
5760 outs() << "-";
5761 if(mode & S_IWRITE)
5762 outs() << "w";
5763 else
5764 outs() << "-";
5765 if(mode & S_ISUID)
5766 outs() << "s";
5767 else if(mode & S_IEXEC)
5768 outs() << "x";
5769 else
5770 outs() << "-";
5771
5772 /* group permissions */
5773 if(mode & (S_IREAD >> 3))
5774 outs() << "r";
5775 else
5776 outs() << "-";
5777 if(mode & (S_IWRITE >> 3))
5778 outs() << "w";
5779 else
5780 outs() << "-";
5781 if(mode & S_ISGID)
5782 outs() << "s";
5783 else if(mode & (S_IEXEC >> 3))
5784 outs() << "x";
5785 else
5786 outs() << "-";
5787
5788 /* other permissions */
5789 if(mode & (S_IREAD >> 6))
5790 outs() << "r";
5791 else
5792 outs() << "-";
5793 if(mode & (S_IWRITE >> 6))
5794 outs() << "w";
5795 else
5796 outs() << "-";
5797 if(mode & S_ISVTX)
5798 outs() << "t";
5799 else if(mode & (S_IEXEC >> 6))
5800 outs() << "x";
5801 else
5802 outs() << "-";
5803}
5804
5805static void PrintXarFilesSummary(const char *XarFilename, xar_t xar) {
5806 xar_iter_t xi;
5807 xar_file_t xf;
5808 xar_iter_t xp;
5809 const char *key, *type, *mode, *user, *group, *size, *mtime, *name, *m;
5810 char *endp;
5811 uint32_t mode_value;
5812
5813 xi = xar_iter_new();
5814 if (!xi) {
5815 errs() << "Can't obtain an xar iterator for xar archive "
5816 << XarFilename << "\n";
5817 return;
5818 }
5819
5820 // Go through the xar's files.
5821 for (xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)) {
5822 xp = xar_iter_new();
5823 if(!xp){
5824 errs() << "Can't obtain an xar iterator for xar archive "
5825 << XarFilename << "\n";
5826 return;
5827 }
5828 type = nullptr;
5829 mode = nullptr;
5830 user = nullptr;
5831 group = nullptr;
5832 size = nullptr;
5833 mtime = nullptr;
5834 name = nullptr;
5835 for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){
5836 const char *val = nullptr;
5837 xar_prop_get(xf, key, &val);
5838#if 0 // Useful for debugging.
5839 outs() << "key: " << key << " value: " << val << "\n";
5840#endif
5841 if(strcmp(key, "type") == 0)
5842 type = val;
5843 if(strcmp(key, "mode") == 0)
5844 mode = val;
5845 if(strcmp(key, "user") == 0)
5846 user = val;
5847 if(strcmp(key, "group") == 0)
5848 group = val;
5849 if(strcmp(key, "data/size") == 0)
5850 size = val;
5851 if(strcmp(key, "mtime") == 0)
5852 mtime = val;
5853 if(strcmp(key, "name") == 0)
5854 name = val;
5855 }
5856 if(mode != nullptr){
5857 mode_value = strtoul(mode, &endp, 8);
5858 if(*endp != '\0')
5859 outs() << "(mode: \"" << mode << "\" contains non-octal chars) ";
5860 if(strcmp(type, "file") == 0)
5861 mode_value |= S_IFREG;
5862 PrintModeVerbose(mode_value);
5863 outs() << " ";
5864 }
5865 if(user != nullptr)
5866 outs() << format("%10s/", user);
5867 if(group != nullptr)
5868 outs() << format("%-10s ", group);
5869 if(size != nullptr)
5870 outs() << format("%7s ", size);
5871 if(mtime != nullptr){
5872 for(m = mtime; *m != 'T' && *m != '\0'; m++)
5873 outs() << *m;
5874 if(*m == 'T')
5875 m++;
5876 outs() << " ";
5877 for( ; *m != 'Z' && *m != '\0'; m++)
5878 outs() << *m;
5879 outs() << " ";
5880 }
5881 if(name != nullptr)
5882 outs() << name;
5883 outs() << "\n";
Francis Ricci1bae0ac2017-09-13 13:57:45 +00005884 xar_iter_free(xp);
Kevin Enderby9873e2c2016-05-23 21:34:12 +00005885 }
Francis Ricci1bae0ac2017-09-13 13:57:45 +00005886 xar_iter_free(xi);
Kevin Enderby9873e2c2016-05-23 21:34:12 +00005887}
5888
5889static void DumpBitcodeSection(MachOObjectFile *O, const char *sect,
5890 uint32_t size, bool verbose,
5891 bool PrintXarHeader, bool PrintXarFileHeaders,
5892 std::string XarMemberName) {
5893 if(size < sizeof(struct xar_header)) {
5894 outs() << "size of (__LLVM,__bundle) section too small (smaller than size "
5895 "of struct xar_header)\n";
5896 return;
5897 }
5898 struct xar_header XarHeader;
5899 memcpy(&XarHeader, sect, sizeof(struct xar_header));
5900 if (sys::IsLittleEndianHost)
5901 swapStruct(XarHeader);
5902 if (PrintXarHeader) {
5903 if (!XarMemberName.empty())
5904 outs() << "In xar member " << XarMemberName << ": ";
5905 else
5906 outs() << "For (__LLVM,__bundle) section: ";
5907 outs() << "xar header\n";
5908 if (XarHeader.magic == XAR_HEADER_MAGIC)
5909 outs() << " magic XAR_HEADER_MAGIC\n";
5910 else
5911 outs() << " magic "
5912 << format_hex(XarHeader.magic, 10, true)
5913 << " (not XAR_HEADER_MAGIC)\n";
5914 outs() << " size " << XarHeader.size << "\n";
5915 outs() << " version " << XarHeader.version << "\n";
5916 outs() << " toc_length_compressed " << XarHeader.toc_length_compressed
5917 << "\n";
5918 outs() << "toc_length_uncompressed " << XarHeader.toc_length_uncompressed
5919 << "\n";
5920 outs() << " cksum_alg ";
5921 switch (XarHeader.cksum_alg) {
5922 case XAR_CKSUM_NONE:
5923 outs() << "XAR_CKSUM_NONE\n";
5924 break;
5925 case XAR_CKSUM_SHA1:
5926 outs() << "XAR_CKSUM_SHA1\n";
5927 break;
5928 case XAR_CKSUM_MD5:
5929 outs() << "XAR_CKSUM_MD5\n";
5930 break;
Kevin Enderby42882282016-05-23 22:18:59 +00005931#ifdef XAR_CKSUM_SHA256
Kevin Enderby9873e2c2016-05-23 21:34:12 +00005932 case XAR_CKSUM_SHA256:
5933 outs() << "XAR_CKSUM_SHA256\n";
5934 break;
Kevin Enderby42882282016-05-23 22:18:59 +00005935#endif
5936#ifdef XAR_CKSUM_SHA512
Kevin Enderby9873e2c2016-05-23 21:34:12 +00005937 case XAR_CKSUM_SHA512:
5938 outs() << "XAR_CKSUM_SHA512\n";
5939 break;
Kevin Enderby42882282016-05-23 22:18:59 +00005940#endif
Kevin Enderby9873e2c2016-05-23 21:34:12 +00005941 default:
5942 outs() << XarHeader.cksum_alg << "\n";
5943 }
5944 }
5945
5946 SmallString<128> XarFilename;
5947 int FD;
5948 std::error_code XarEC =
5949 sys::fs::createTemporaryFile("llvm-objdump", "xar", FD, XarFilename);
5950 if (XarEC) {
5951 errs() << XarEC.message() << "\n";
5952 return;
5953 }
5954 tool_output_file XarFile(XarFilename, FD);
5955 raw_fd_ostream &XarOut = XarFile.os();
5956 StringRef XarContents(sect, size);
5957 XarOut << XarContents;
5958 XarOut.close();
5959 if (XarOut.has_error())
5960 return;
5961
5962 xar_t xar = xar_open(XarFilename.c_str(), READ);
5963 if (!xar) {
5964 errs() << "Can't create temporary xar archive " << XarFilename << "\n";
5965 return;
5966 }
5967
5968 SmallString<128> TocFilename;
5969 std::error_code TocEC =
5970 sys::fs::createTemporaryFile("llvm-objdump", "toc", TocFilename);
5971 if (TocEC) {
5972 errs() << TocEC.message() << "\n";
5973 return;
5974 }
5975 xar_serialize(xar, TocFilename.c_str());
5976
5977 if (PrintXarFileHeaders) {
5978 if (!XarMemberName.empty())
5979 outs() << "In xar member " << XarMemberName << ": ";
5980 else
5981 outs() << "For (__LLVM,__bundle) section: ";
5982 outs() << "xar archive files:\n";
5983 PrintXarFilesSummary(XarFilename.c_str(), xar);
5984 }
5985
5986 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
5987 MemoryBuffer::getFileOrSTDIN(TocFilename.c_str());
5988 if (std::error_code EC = FileOrErr.getError()) {
5989 errs() << EC.message() << "\n";
5990 return;
5991 }
5992 std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
5993
5994 if (!XarMemberName.empty())
5995 outs() << "In xar member " << XarMemberName << ": ";
5996 else
5997 outs() << "For (__LLVM,__bundle) section: ";
5998 outs() << "xar table of contents:\n";
5999 outs() << Buffer->getBuffer() << "\n";
6000
6001 // TODO: Go through the xar's files.
6002 xar_iter_t xi = xar_iter_new();
6003 if(!xi){
6004 errs() << "Can't obtain an xar iterator for xar archive "
6005 << XarFilename.c_str() << "\n";
6006 xar_close(xar);
6007 return;
6008 }
6009 for(xar_file_t xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)){
6010 const char *key;
6011 xar_iter_t xp;
6012 const char *member_name, *member_type, *member_size_string;
6013 size_t member_size;
6014
6015 xp = xar_iter_new();
6016 if(!xp){
6017 errs() << "Can't obtain an xar iterator for xar archive "
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +00006018 << XarFilename.c_str() << "\n";
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006019 xar_close(xar);
6020 return;
6021 }
6022 member_name = NULL;
6023 member_type = NULL;
6024 member_size_string = NULL;
6025 for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){
6026 const char *val = nullptr;
6027 xar_prop_get(xf, key, &val);
6028#if 0 // Useful for debugging.
6029 outs() << "key: " << key << " value: " << val << "\n";
6030#endif
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +00006031 if (strcmp(key, "name") == 0)
6032 member_name = val;
6033 if (strcmp(key, "type") == 0)
6034 member_type = val;
6035 if (strcmp(key, "data/size") == 0)
6036 member_size_string = val;
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006037 }
6038 /*
6039 * If we find a file with a name, date/size and type properties
6040 * and with the type being "file" see if that is a xar file.
6041 */
6042 if (member_name != NULL && member_type != NULL &&
6043 strcmp(member_type, "file") == 0 &&
6044 member_size_string != NULL){
6045 // Extract the file into a buffer.
6046 char *endptr;
6047 member_size = strtoul(member_size_string, &endptr, 10);
6048 if (*endptr == '\0' && member_size != 0) {
Francis Ricci1bae0ac2017-09-13 13:57:45 +00006049 char *buffer;
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +00006050 if (xar_extract_tobuffersz(xar, xf, &buffer, &member_size) == 0) {
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006051#if 0 // Useful for debugging.
6052 outs() << "xar member: " << member_name << " extracted\n";
6053#endif
6054 // Set the XarMemberName we want to see printed in the header.
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +00006055 std::string OldXarMemberName;
6056 // If XarMemberName is already set this is nested. So
6057 // save the old name and create the nested name.
6058 if (!XarMemberName.empty()) {
6059 OldXarMemberName = XarMemberName;
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006060 XarMemberName =
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +00006061 (Twine("[") + XarMemberName + "]" + member_name).str();
6062 } else {
6063 OldXarMemberName = "";
6064 XarMemberName = member_name;
6065 }
6066 // See if this is could be a xar file (nested).
6067 if (member_size >= sizeof(struct xar_header)) {
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006068#if 0 // Useful for debugging.
6069 outs() << "could be a xar file: " << member_name << "\n";
6070#endif
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +00006071 memcpy((char *)&XarHeader, buffer, sizeof(struct xar_header));
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006072 if (sys::IsLittleEndianHost)
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +00006073 swapStruct(XarHeader);
6074 if (XarHeader.magic == XAR_HEADER_MAGIC)
6075 DumpBitcodeSection(O, buffer, member_size, verbose,
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006076 PrintXarHeader, PrintXarFileHeaders,
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +00006077 XarMemberName);
6078 }
6079 XarMemberName = OldXarMemberName;
Francis Ricci1bae0ac2017-09-13 13:57:45 +00006080 delete buffer;
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +00006081 }
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006082 }
6083 }
6084 xar_iter_free(xp);
6085 }
Francis Ricci1bae0ac2017-09-13 13:57:45 +00006086 xar_iter_free(xi);
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006087 xar_close(xar);
6088}
6089#endif // defined(HAVE_LIBXAR)
6090
Kevin Enderby0fc11822015-04-01 20:57:01 +00006091static void printObjcMetaData(MachOObjectFile *O, bool verbose) {
6092 if (O->is64Bit())
6093 printObjc2_64bit_MetaData(O, verbose);
6094 else {
6095 MachO::mach_header H;
6096 H = O->getHeader();
6097 if (H.cputype == MachO::CPU_TYPE_ARM)
6098 printObjc2_32bit_MetaData(O, verbose);
6099 else {
6100 // This is the 32-bit non-arm cputype case. Which is normally
6101 // the first Objective-C ABI. But it may be the case of a
6102 // binary for the iOS simulator which is the second Objective-C
6103 // ABI. In that case printObjc1_32bit_MetaData() will determine that
6104 // and return false.
Hans Wennborgcc9deb42015-09-29 18:02:48 +00006105 if (!printObjc1_32bit_MetaData(O, verbose))
Kevin Enderby0fc11822015-04-01 20:57:01 +00006106 printObjc2_32bit_MetaData(O, verbose);
6107 }
6108 }
6109}
6110
Kevin Enderbybf246f52014-09-24 23:08:22 +00006111// GuessLiteralPointer returns a string which for the item in the Mach-O file
6112// for the address passed in as ReferenceValue for printing as a comment with
6113// the instruction and also returns the corresponding type of that item
6114// indirectly through ReferenceType.
6115//
6116// If ReferenceValue is an address of literal cstring then a pointer to the
6117// cstring is returned and ReferenceType is set to
6118// LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr .
6119//
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006120// If ReferenceValue is an address of an Objective-C CFString, Selector ref or
6121// Class ref that name is returned and the ReferenceType is set accordingly.
6122//
6123// Lastly, literals which are Symbol address in a literal pool are looked for
6124// and if found the symbol name is returned and ReferenceType is set to
6125// LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr .
6126//
6127// If there is no item in the Mach-O file for the address passed in as
6128// ReferenceValue nullptr is returned and ReferenceType is unchanged.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00006129static const char *GuessLiteralPointer(uint64_t ReferenceValue,
6130 uint64_t ReferencePC,
6131 uint64_t *ReferenceType,
6132 struct DisassembleInfo *info) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00006133 // First see if there is an external relocation entry at the ReferencePC.
Kevin Enderbyd90a4172015-10-10 00:05:01 +00006134 if (info->O->getHeader().filetype == MachO::MH_OBJECT) {
6135 uint64_t sect_addr = info->S.getAddress();
6136 uint64_t sect_offset = ReferencePC - sect_addr;
6137 bool reloc_found = false;
6138 DataRefImpl Rel;
6139 MachO::any_relocation_info RE;
6140 bool isExtern = false;
6141 SymbolRef Symbol;
6142 for (const RelocationRef &Reloc : info->S.relocations()) {
6143 uint64_t RelocOffset = Reloc.getOffset();
6144 if (RelocOffset == sect_offset) {
6145 Rel = Reloc.getRawDataRefImpl();
6146 RE = info->O->getRelocation(Rel);
6147 if (info->O->isRelocationScattered(RE))
6148 continue;
6149 isExtern = info->O->getPlainRelocationExternal(RE);
6150 if (isExtern) {
6151 symbol_iterator RelocSym = Reloc.getSymbol();
6152 Symbol = *RelocSym;
6153 }
6154 reloc_found = true;
6155 break;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006156 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006157 }
Kevin Enderbyd90a4172015-10-10 00:05:01 +00006158 // If there is an external relocation entry for a symbol in a section
6159 // then used that symbol's value for the value of the reference.
6160 if (reloc_found && isExtern) {
6161 if (info->O->getAnyRelocationPCRel(RE)) {
6162 unsigned Type = info->O->getAnyRelocationType(RE);
6163 if (Type == MachO::X86_64_RELOC_SIGNED) {
6164 ReferenceValue = Symbol.getValue();
6165 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006166 }
6167 }
6168 }
6169
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006170 // Look for literals such as Objective-C CFStrings refs, Selector refs,
6171 // Message refs and Class refs.
6172 bool classref, selref, msgref, cfstring;
6173 uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref,
6174 selref, msgref, cfstring);
David Blaikie33dd45d02015-03-23 18:39:02 +00006175 if (classref && pointer_value == 0) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006176 // Note the ReferenceValue is a pointer into the __objc_classrefs section.
6177 // And the pointer_value in that section is typically zero as it will be
6178 // set by dyld as part of the "bind information".
6179 const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info);
6180 if (name != nullptr) {
6181 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
Hans Wennborgdb53e302014-10-23 21:59:17 +00006182 const char *class_name = strrchr(name, '$');
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006183 if (class_name != nullptr && class_name[1] == '_' &&
6184 class_name[2] != '\0') {
6185 info->class_name = class_name + 2;
6186 return name;
6187 }
6188 }
6189 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006190
David Blaikie33dd45d02015-03-23 18:39:02 +00006191 if (classref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006192 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
6193 const char *name =
6194 get_objc2_64bit_class_name(pointer_value, ReferenceValue, info);
6195 if (name != nullptr)
6196 info->class_name = name;
6197 else
6198 name = "bad class ref";
Kevin Enderbybf246f52014-09-24 23:08:22 +00006199 return name;
6200 }
6201
David Blaikie33dd45d02015-03-23 18:39:02 +00006202 if (cfstring) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006203 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref;
6204 const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info);
6205 return name;
6206 }
6207
David Blaikie33dd45d02015-03-23 18:39:02 +00006208 if (selref && pointer_value == 0)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006209 pointer_value = get_objc2_64bit_selref(ReferenceValue, info);
6210
6211 if (pointer_value != 0)
6212 ReferenceValue = pointer_value;
6213
6214 const char *name = GuessCstringPointer(ReferenceValue, info);
6215 if (name) {
David Blaikie33dd45d02015-03-23 18:39:02 +00006216 if (pointer_value != 0 && selref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006217 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref;
6218 info->selector_name = name;
David Blaikie33dd45d02015-03-23 18:39:02 +00006219 } else if (pointer_value != 0 && msgref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006220 info->class_name = nullptr;
6221 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref;
6222 info->selector_name = name;
6223 } else
6224 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr;
6225 return name;
6226 }
6227
6228 // Lastly look for an indirect symbol with this ReferenceValue which is in
6229 // a literal pool. If found return that symbol name.
6230 name = GuessIndirectSymbol(ReferenceValue, info);
6231 if (name) {
6232 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr;
6233 return name;
6234 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006235
6236 return nullptr;
6237}
6238
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006239// SymbolizerSymbolLookUp is the symbol lookup function passed when creating
Kevin Enderbybf246f52014-09-24 23:08:22 +00006240// the Symbolizer. It looks up the ReferenceValue using the info passed via the
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006241// pointer to the struct DisassembleInfo that was passed when MCSymbolizer
6242// is created and returns the symbol name that matches the ReferenceValue or
6243// nullptr if none. The ReferenceType is passed in for the IN type of
6244// reference the instruction is making from the values in defined in the header
6245// "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific
6246// Out type and the ReferenceName will also be set which is added as a comment
6247// to the disassembled instruction.
6248//
Kevin Enderby04bf6932014-10-28 23:39:46 +00006249// If the symbol name is a C++ mangled name then the demangled name is
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006250// returned through ReferenceName and ReferenceType is set to
6251// LLVMDisassembler_ReferenceType_DeMangled_Name .
6252//
6253// When this is called to get a symbol name for a branch target then the
6254// ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
6255// SymbolValue will be looked for in the indirect symbol table to determine if
6256// it is an address for a symbol stub. If so then the symbol name for that
6257// stub is returned indirectly through ReferenceName and then ReferenceType is
6258// set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
6259//
Kevin Enderbybf246f52014-09-24 23:08:22 +00006260// When this is called with an value loaded via a PC relative load then
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006261// ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
6262// SymbolValue is checked to be an address of literal pointer, symbol pointer,
6263// or an Objective-C meta data reference. If so the output ReferenceType is
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006264// set to correspond to that as well as setting the ReferenceName.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00006265static const char *SymbolizerSymbolLookUp(void *DisInfo,
6266 uint64_t ReferenceValue,
6267 uint64_t *ReferenceType,
6268 uint64_t ReferencePC,
6269 const char **ReferenceName) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006270 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006271 // If no verbose symbolic information is wanted then just return nullptr.
David Blaikie33dd45d02015-03-23 18:39:02 +00006272 if (!info->verbose) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00006273 *ReferenceName = nullptr;
6274 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006275 return nullptr;
6276 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006277
Kevin Enderbyf6d25852015-01-31 00:37:11 +00006278 const char *SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
Kevin Enderbybf246f52014-09-24 23:08:22 +00006279
Kevin Enderby85974882014-09-26 22:20:44 +00006280 if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) {
6281 *ReferenceName = GuessIndirectSymbol(ReferenceValue, info);
Kevin Enderby04bf6932014-10-28 23:39:46 +00006282 if (*ReferenceName != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006283 method_reference(info, ReferenceType, ReferenceName);
6284 if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message)
6285 *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub;
Rafael Espindolab940b662016-09-06 19:16:48 +00006286 } else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
Kevin Enderby04bf6932014-10-28 23:39:46 +00006287 if (info->demangled_name != nullptr)
6288 free(info->demangled_name);
6289 int status;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006290 info->demangled_name =
Rafael Espindolab940b662016-09-06 19:16:48 +00006291 itaniumDemangle(SymbolName + 1, nullptr, nullptr, &status);
Kevin Enderby04bf6932014-10-28 23:39:46 +00006292 if (info->demangled_name != nullptr) {
6293 *ReferenceName = info->demangled_name;
6294 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
6295 } else
6296 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6297 } else
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006298 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6299 } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) {
6300 *ReferenceName =
6301 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
Kevin Enderby85974882014-09-26 22:20:44 +00006302 if (*ReferenceName)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006303 method_reference(info, ReferenceType, ReferenceName);
Kevin Enderby85974882014-09-26 22:20:44 +00006304 else
6305 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006306 // If this is arm64 and the reference is an adrp instruction save the
6307 // instruction, passed in ReferenceValue and the address of the instruction
6308 // for use later if we see and add immediate instruction.
6309 } else if (info->O->getArch() == Triple::aarch64 &&
6310 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) {
6311 info->adrp_inst = ReferenceValue;
6312 info->adrp_addr = ReferencePC;
6313 SymbolName = nullptr;
6314 *ReferenceName = nullptr;
6315 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6316 // If this is arm64 and reference is an add immediate instruction and we
6317 // have
6318 // seen an adrp instruction just before it and the adrp's Xd register
6319 // matches
6320 // this add's Xn register reconstruct the value being referenced and look to
6321 // see if it is a literal pointer. Note the add immediate instruction is
6322 // passed in ReferenceValue.
6323 } else if (info->O->getArch() == Triple::aarch64 &&
6324 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri &&
6325 ReferencePC - 4 == info->adrp_addr &&
6326 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
6327 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
6328 uint32_t addxri_inst;
6329 uint64_t adrp_imm, addxri_imm;
6330
6331 adrp_imm =
6332 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
6333 if (info->adrp_inst & 0x0200000)
6334 adrp_imm |= 0xfffffffffc000000LL;
6335
6336 addxri_inst = ReferenceValue;
6337 addxri_imm = (addxri_inst >> 10) & 0xfff;
6338 if (((addxri_inst >> 22) & 0x3) == 1)
6339 addxri_imm <<= 12;
6340
6341 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
6342 (adrp_imm << 12) + addxri_imm;
6343
6344 *ReferenceName =
6345 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6346 if (*ReferenceName == nullptr)
6347 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6348 // If this is arm64 and the reference is a load register instruction and we
6349 // have seen an adrp instruction just before it and the adrp's Xd register
6350 // matches this add's Xn register reconstruct the value being referenced and
6351 // look to see if it is a literal pointer. Note the load register
6352 // instruction is passed in ReferenceValue.
6353 } else if (info->O->getArch() == Triple::aarch64 &&
6354 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui &&
6355 ReferencePC - 4 == info->adrp_addr &&
6356 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
6357 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
6358 uint32_t ldrxui_inst;
6359 uint64_t adrp_imm, ldrxui_imm;
6360
6361 adrp_imm =
6362 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
6363 if (info->adrp_inst & 0x0200000)
6364 adrp_imm |= 0xfffffffffc000000LL;
6365
6366 ldrxui_inst = ReferenceValue;
6367 ldrxui_imm = (ldrxui_inst >> 10) & 0xfff;
6368
6369 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
6370 (adrp_imm << 12) + (ldrxui_imm << 3);
6371
6372 *ReferenceName =
6373 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6374 if (*ReferenceName == nullptr)
6375 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6376 }
6377 // If this arm64 and is an load register (PC-relative) instruction the
6378 // ReferenceValue is the PC plus the immediate value.
6379 else if (info->O->getArch() == Triple::aarch64 &&
6380 (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl ||
6381 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR)) {
6382 *ReferenceName =
6383 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6384 if (*ReferenceName == nullptr)
6385 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Rafael Espindolab940b662016-09-06 19:16:48 +00006386 } else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
Kevin Enderby04bf6932014-10-28 23:39:46 +00006387 if (info->demangled_name != nullptr)
6388 free(info->demangled_name);
6389 int status;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006390 info->demangled_name =
Rafael Espindolab940b662016-09-06 19:16:48 +00006391 itaniumDemangle(SymbolName + 1, nullptr, nullptr, &status);
Kevin Enderby04bf6932014-10-28 23:39:46 +00006392 if (info->demangled_name != nullptr) {
6393 *ReferenceName = info->demangled_name;
6394 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
6395 }
6396 }
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006397 else {
Kevin Enderbybf246f52014-09-24 23:08:22 +00006398 *ReferenceName = nullptr;
6399 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6400 }
6401
6402 return SymbolName;
6403}
6404
Kevin Enderbybf246f52014-09-24 23:08:22 +00006405/// \brief Emits the comments that are stored in the CommentStream.
6406/// Each comment in the CommentStream must end with a newline.
6407static void emitComments(raw_svector_ostream &CommentStream,
6408 SmallString<128> &CommentsToEmit,
6409 formatted_raw_ostream &FormattedOS,
6410 const MCAsmInfo &MAI) {
6411 // Flush the stream before taking its content.
Kevin Enderbybf246f52014-09-24 23:08:22 +00006412 StringRef Comments = CommentsToEmit.str();
6413 // Get the default information for printing a comment.
Mehdi Amini36d33fc2016-10-01 06:46:33 +00006414 StringRef CommentBegin = MAI.getCommentString();
Kevin Enderbybf246f52014-09-24 23:08:22 +00006415 unsigned CommentColumn = MAI.getCommentColumn();
6416 bool IsFirst = true;
6417 while (!Comments.empty()) {
6418 if (!IsFirst)
6419 FormattedOS << '\n';
6420 // Emit a line of comments.
6421 FormattedOS.PadToColumn(CommentColumn);
6422 size_t Position = Comments.find('\n');
6423 FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
6424 // Move after the newline character.
6425 Comments = Comments.substr(Position + 1);
6426 IsFirst = false;
6427 }
6428 FormattedOS.flush();
6429
6430 // Tell the comment stream that the vector changed underneath it.
6431 CommentsToEmit.clear();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006432}
6433
Kevin Enderby95df54c2015-02-04 01:01:38 +00006434static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
6435 StringRef DisSegName, StringRef DisSectName) {
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006436 const char *McpuDefault = nullptr;
6437 const Target *ThumbTarget = nullptr;
6438 const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006439 if (!TheTarget) {
6440 // GetTarget prints out stuff.
6441 return;
6442 }
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006443 if (MCPU.empty() && McpuDefault)
6444 MCPU = McpuDefault;
6445
Ahmed Charles56440fd2014-03-06 05:51:42 +00006446 std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006447 std::unique_ptr<const MCInstrInfo> ThumbInstrInfo;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006448 if (ThumbTarget)
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006449 ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo());
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006450
Kevin Enderbyc9595622014-08-06 23:24:41 +00006451 // Package up features to be passed to target/subtarget
6452 std::string FeaturesStr;
6453 if (MAttrs.size()) {
6454 SubtargetFeatures Features;
6455 for (unsigned i = 0; i != MAttrs.size(); ++i)
6456 Features.AddFeature(MAttrs[i]);
6457 FeaturesStr = Features.getString();
6458 }
6459
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006460 // Set up disassembler.
Ahmed Charles56440fd2014-03-06 05:51:42 +00006461 std::unique_ptr<const MCRegisterInfo> MRI(
6462 TheTarget->createMCRegInfo(TripleName));
6463 std::unique_ptr<const MCAsmInfo> AsmInfo(
Rafael Espindola227144c2013-05-13 01:16:13 +00006464 TheTarget->createMCAsmInfo(*MRI, TripleName));
Ahmed Charles56440fd2014-03-06 05:51:42 +00006465 std::unique_ptr<const MCSubtargetInfo> STI(
Kevin Enderbyc9595622014-08-06 23:24:41 +00006466 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
Craig Toppere6cb63e2014-04-25 04:24:47 +00006467 MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr);
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006468 std::unique_ptr<MCDisassembler> DisAsm(
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006469 TheTarget->createMCDisassembler(*STI, Ctx));
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006470 std::unique_ptr<MCSymbolizer> Symbolizer;
6471 struct DisassembleInfo SymbolizerInfo;
6472 std::unique_ptr<MCRelocationInfo> RelInfo(
6473 TheTarget->createMCRelocationInfo(TripleName, Ctx));
6474 if (RelInfo) {
6475 Symbolizer.reset(TheTarget->createMCSymbolizer(
6476 TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
David Blaikie186db432015-01-18 20:45:48 +00006477 &SymbolizerInfo, &Ctx, std::move(RelInfo)));
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006478 DisAsm->setSymbolizer(std::move(Symbolizer));
6479 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006480 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
Ahmed Charles56440fd2014-03-06 05:51:42 +00006481 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
Daniel Sanders50f17232015-09-15 16:17:27 +00006482 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI));
Kevin Enderbybf246f52014-09-24 23:08:22 +00006483 // Set the display preference for hex vs. decimal immediates.
6484 IP->setPrintImmHex(PrintImmHex);
6485 // Comment stream and backing vector.
6486 SmallString<128> CommentsToEmit;
6487 raw_svector_ostream CommentStream(CommentsToEmit);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00006488 // FIXME: Setting the CommentStream in the InstPrinter is problematic in that
6489 // if it is done then arm64 comments for string literals don't get printed
6490 // and some constant get printed instead and not setting it causes intel
6491 // (32-bit and 64-bit) comments printed with different spacing before the
6492 // comment causing different diffs with the 'C' disassembler library API.
6493 // IP->setCommentStream(CommentStream);
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006494
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006495 if (!AsmInfo || !STI || !DisAsm || !IP) {
Michael J. Spencerc1363cf2011-10-07 19:25:47 +00006496 errs() << "error: couldn't initialize disassembler for target "
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006497 << TripleName << '\n';
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006498 return;
6499 }
6500
Tim Northover09ca33e2016-04-22 23:23:31 +00006501 // Set up separate thumb disassembler if needed.
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006502 std::unique_ptr<const MCRegisterInfo> ThumbMRI;
6503 std::unique_ptr<const MCAsmInfo> ThumbAsmInfo;
6504 std::unique_ptr<const MCSubtargetInfo> ThumbSTI;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006505 std::unique_ptr<MCDisassembler> ThumbDisAsm;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006506 std::unique_ptr<MCInstPrinter> ThumbIP;
6507 std::unique_ptr<MCContext> ThumbCtx;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006508 std::unique_ptr<MCSymbolizer> ThumbSymbolizer;
6509 struct DisassembleInfo ThumbSymbolizerInfo;
6510 std::unique_ptr<MCRelocationInfo> ThumbRelInfo;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006511 if (ThumbTarget) {
6512 ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName));
6513 ThumbAsmInfo.reset(
6514 ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName));
6515 ThumbSTI.reset(
6516 ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MCPU, FeaturesStr));
6517 ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr));
6518 ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx));
Kevin Enderby930fdc72014-11-06 19:00:13 +00006519 MCContext *PtrThumbCtx = ThumbCtx.get();
6520 ThumbRelInfo.reset(
6521 ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx));
6522 if (ThumbRelInfo) {
6523 ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer(
6524 ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
David Blaikie186db432015-01-18 20:45:48 +00006525 &ThumbSymbolizerInfo, PtrThumbCtx, std::move(ThumbRelInfo)));
Kevin Enderby930fdc72014-11-06 19:00:13 +00006526 ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer));
6527 }
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006528 int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect();
6529 ThumbIP.reset(ThumbTarget->createMCInstPrinter(
Daniel Sanders50f17232015-09-15 16:17:27 +00006530 Triple(ThumbTripleName), ThumbAsmPrinterVariant, *ThumbAsmInfo,
6531 *ThumbInstrInfo, *ThumbMRI));
Kevin Enderbybf246f52014-09-24 23:08:22 +00006532 // Set the display preference for hex vs. decimal immediates.
6533 ThumbIP->setPrintImmHex(PrintImmHex);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006534 }
6535
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006536 if (ThumbTarget && (!ThumbAsmInfo || !ThumbSTI || !ThumbDisAsm || !ThumbIP)) {
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006537 errs() << "error: couldn't initialize disassembler for target "
6538 << ThumbTripleName << '\n';
6539 return;
6540 }
6541
Charles Davis8bdfafd2013-09-01 04:28:48 +00006542 MachO::mach_header Header = MachOOF->getHeader();
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006543
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006544 // FIXME: Using the -cfg command line option, this code used to be able to
6545 // annotate relocations with the referenced symbol's name, and if this was
6546 // inside a __[cf]string section, the data it points to. This is now replaced
6547 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
Owen Andersond9243c42011-10-17 21:37:35 +00006548 std::vector<SectionRef> Sections;
6549 std::vector<SymbolRef> Symbols;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006550 SmallVector<uint64_t, 8> FoundFns;
Kevin Enderby273ae012013-06-06 17:20:50 +00006551 uint64_t BaseSegmentAddress;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006552
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00006553 getSectionsAndSymbols(MachOOF, Sections, Symbols, FoundFns,
Kevin Enderby273ae012013-06-06 17:20:50 +00006554 BaseSegmentAddress);
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006555
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006556 // Sort the symbols by address, just in case they didn't come in that way.
Owen Andersond9243c42011-10-17 21:37:35 +00006557 std::sort(Symbols.begin(), Symbols.end(), SymbolSorter());
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006558
Kevin Enderby273ae012013-06-06 17:20:50 +00006559 // Build a data in code table that is sorted on by the address of each entry.
6560 uint64_t BaseAddress = 0;
Charles Davis8bdfafd2013-09-01 04:28:48 +00006561 if (Header.filetype == MachO::MH_OBJECT)
Rafael Espindola80291272014-10-08 15:28:58 +00006562 BaseAddress = Sections[0].getAddress();
Kevin Enderby273ae012013-06-06 17:20:50 +00006563 else
6564 BaseAddress = BaseSegmentAddress;
6565 DiceTable Dices;
Kevin Enderby273ae012013-06-06 17:20:50 +00006566 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
Rafael Espindola5e812af2014-01-30 02:49:50 +00006567 DI != DE; ++DI) {
Kevin Enderby273ae012013-06-06 17:20:50 +00006568 uint32_t Offset;
6569 DI->getOffset(Offset);
6570 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
6571 }
6572 array_pod_sort(Dices.begin(), Dices.end());
6573
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006574#ifndef NDEBUG
6575 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
6576#else
6577 raw_ostream &DebugOut = nulls();
6578#endif
6579
Ahmed Charles56440fd2014-03-06 05:51:42 +00006580 std::unique_ptr<DIContext> diContext;
Rafael Espindola9b709252013-04-13 01:45:40 +00006581 ObjectFile *DbgObj = MachOOF;
Benjamin Kramer699128e2011-09-21 01:13:19 +00006582 // Try to find debug info and set up the DIContext for it.
6583 if (UseDbg) {
Benjamin Kramer699128e2011-09-21 01:13:19 +00006584 // A separate DSym file path was specified, parse it as a macho file,
6585 // get the sections and supply it to the section name parsing machinery.
6586 if (!DSYMFile.empty()) {
Rafael Espindola48af1c22014-08-19 18:44:46 +00006587 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
Rafael Espindolaadf21f22014-07-06 17:43:13 +00006588 MemoryBuffer::getFileOrSTDIN(DSYMFile);
Rafael Espindola48af1c22014-08-19 18:44:46 +00006589 if (std::error_code EC = BufOrErr.getError()) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +00006590 errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n';
Benjamin Kramer699128e2011-09-21 01:13:19 +00006591 return;
6592 }
Rafael Espindola48af1c22014-08-19 18:44:46 +00006593 DbgObj =
6594 ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef())
6595 .get()
6596 .release();
Benjamin Kramer699128e2011-09-21 01:13:19 +00006597 }
6598
Eric Christopher7370b552012-11-12 21:40:38 +00006599 // Setup the DIContext
Rafael Espindolac398e672017-07-19 22:27:28 +00006600 diContext = DWARFContext::create(*DbgObj);
Benjamin Kramer699128e2011-09-21 01:13:19 +00006601 }
6602
Colin LeMahieufcc32762015-07-29 19:08:10 +00006603 if (FilterSections.size() == 0)
Kevin Enderby95df54c2015-02-04 01:01:38 +00006604 outs() << "(" << DisSegName << "," << DisSectName << ") section\n";
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00006605
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006606 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
Owen Andersond9243c42011-10-17 21:37:35 +00006607 StringRef SectName;
Kevin Enderby95df54c2015-02-04 01:01:38 +00006608 if (Sections[SectIdx].getName(SectName) || SectName != DisSectName)
6609 continue;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006610
Rafael Espindolaa9f810b2012-12-21 03:47:03 +00006611 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006612
Rafael Espindolab0f76a42013-04-05 15:15:22 +00006613 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
Kevin Enderby95df54c2015-02-04 01:01:38 +00006614 if (SegmentName != DisSegName)
Rafael Espindolaa9f810b2012-12-21 03:47:03 +00006615 continue;
6616
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006617 StringRef BytesStr;
6618 Sections[SectIdx].getContents(BytesStr);
Aaron Ballman106fd7b2014-11-12 14:01:17 +00006619 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
6620 BytesStr.size());
Rafael Espindola80291272014-10-08 15:28:58 +00006621 uint64_t SectAddress = Sections[SectIdx].getAddress();
Rafael Espindolabd604f22014-11-07 00:52:15 +00006622
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006623 bool symbolTableWorked = false;
6624
Kevin Enderbybf246f52014-09-24 23:08:22 +00006625 // Create a map of symbol addresses to symbol names for use by
6626 // the SymbolizerSymbolLookUp() routine.
6627 SymbolAddressMap AddrMap;
Kevin Enderby6a221752015-03-17 17:10:57 +00006628 bool DisSymNameFound = false;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006629 for (const SymbolRef &Symbol : MachOOF->symbols()) {
Kevin Enderby7bd8d992016-05-02 20:28:12 +00006630 Expected<SymbolRef::Type> STOrErr = Symbol.getType();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00006631 if (!STOrErr)
6632 report_error(MachOOF->getFileName(), STOrErr.takeError());
Kevin Enderby5afbc1c2016-03-23 20:27:00 +00006633 SymbolRef::Type ST = *STOrErr;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006634 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
6635 ST == SymbolRef::ST_Other) {
Rafael Espindoladea00162015-07-03 17:44:18 +00006636 uint64_t Address = Symbol.getValue();
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00006637 Expected<StringRef> SymNameOrErr = Symbol.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00006638 if (!SymNameOrErr)
6639 report_error(MachOOF->getFileName(), SymNameOrErr.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00006640 StringRef SymName = *SymNameOrErr;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006641 AddrMap[Address] = SymName;
Kevin Enderby6a221752015-03-17 17:10:57 +00006642 if (!DisSymName.empty() && DisSymName == SymName)
6643 DisSymNameFound = true;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006644 }
6645 }
David Blaikie33dd45d02015-03-23 18:39:02 +00006646 if (!DisSymName.empty() && !DisSymNameFound) {
Kevin Enderby6a221752015-03-17 17:10:57 +00006647 outs() << "Can't find -dis-symname: " << DisSymName << "\n";
6648 return;
6649 }
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006650 // Set up the block of info used by the Symbolizer call backs.
Kevin Enderby8e29ec92015-03-17 22:26:11 +00006651 SymbolizerInfo.verbose = !NoSymbolicOperands;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006652 SymbolizerInfo.O = MachOOF;
6653 SymbolizerInfo.S = Sections[SectIdx];
Kevin Enderbybf246f52014-09-24 23:08:22 +00006654 SymbolizerInfo.AddrMap = &AddrMap;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006655 SymbolizerInfo.Sections = &Sections;
6656 SymbolizerInfo.class_name = nullptr;
6657 SymbolizerInfo.selector_name = nullptr;
6658 SymbolizerInfo.method = nullptr;
Kevin Enderby04bf6932014-10-28 23:39:46 +00006659 SymbolizerInfo.demangled_name = nullptr;
Kevin Enderby078be602014-10-23 19:53:12 +00006660 SymbolizerInfo.bindtable = nullptr;
Kevin Enderby10738222014-11-19 20:20:16 +00006661 SymbolizerInfo.adrp_addr = 0;
6662 SymbolizerInfo.adrp_inst = 0;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006663 // Same for the ThumbSymbolizer
Kevin Enderby8e29ec92015-03-17 22:26:11 +00006664 ThumbSymbolizerInfo.verbose = !NoSymbolicOperands;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006665 ThumbSymbolizerInfo.O = MachOOF;
6666 ThumbSymbolizerInfo.S = Sections[SectIdx];
6667 ThumbSymbolizerInfo.AddrMap = &AddrMap;
6668 ThumbSymbolizerInfo.Sections = &Sections;
6669 ThumbSymbolizerInfo.class_name = nullptr;
6670 ThumbSymbolizerInfo.selector_name = nullptr;
6671 ThumbSymbolizerInfo.method = nullptr;
6672 ThumbSymbolizerInfo.demangled_name = nullptr;
6673 ThumbSymbolizerInfo.bindtable = nullptr;
Kevin Enderby10738222014-11-19 20:20:16 +00006674 ThumbSymbolizerInfo.adrp_addr = 0;
6675 ThumbSymbolizerInfo.adrp_inst = 0;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006676
Kevin Enderby4b627be2016-04-28 20:14:13 +00006677 unsigned int Arch = MachOOF->getArch();
6678
Tim Northoverf203ab52016-07-14 22:13:32 +00006679 // Skip all symbols if this is a stubs file.
6680 if (Bytes.size() == 0)
6681 return;
6682
Kevin Enderbyc138da32017-02-06 18:43:18 +00006683 // If the section has symbols but no symbol at the start of the section
6684 // these are used to make sure the bytes before the first symbol are
6685 // disassembled.
6686 bool FirstSymbol = true;
6687 bool FirstSymbolAtSectionStart = true;
6688
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006689 // Disassemble symbol by symbol.
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006690 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00006691 Expected<StringRef> SymNameOrErr = Symbols[SymIdx].getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00006692 if (!SymNameOrErr)
6693 report_error(MachOOF->getFileName(), SymNameOrErr.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00006694 StringRef SymName = *SymNameOrErr;
Owen Andersond9243c42011-10-17 21:37:35 +00006695
Kevin Enderby7bd8d992016-05-02 20:28:12 +00006696 Expected<SymbolRef::Type> STOrErr = Symbols[SymIdx].getType();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00006697 if (!STOrErr)
6698 report_error(MachOOF->getFileName(), STOrErr.takeError());
Kevin Enderby5afbc1c2016-03-23 20:27:00 +00006699 SymbolRef::Type ST = *STOrErr;
Kuba Breckade833222015-11-12 09:40:29 +00006700 if (ST != SymbolRef::ST_Function && ST != SymbolRef::ST_Data)
Owen Andersond9243c42011-10-17 21:37:35 +00006701 continue;
6702
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006703 // Make sure the symbol is defined in this section.
Rafael Espindola80291272014-10-08 15:28:58 +00006704 bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]);
Kevin Enderbyd8a6e832016-06-15 21:14:01 +00006705 if (!containsSym) {
6706 if (!DisSymName.empty() && DisSymName == SymName) {
6707 outs() << "-dis-symname: " << DisSymName << " not in the section\n";
6708 return;
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +00006709 }
Kevin Enderbyd8a6e832016-06-15 21:14:01 +00006710 continue;
6711 }
6712 // The __mh_execute_header is special and we need to deal with that fact
6713 // this symbol is before the start of the (__TEXT,__text) section and at the
6714 // address of the start of the __TEXT segment. This is because this symbol
6715 // is an N_SECT symbol in the (__TEXT,__text) but its address is before the
6716 // start of the section in a standard MH_EXECUTE filetype.
6717 if (!DisSymName.empty() && DisSymName == "__mh_execute_header") {
6718 outs() << "-dis-symname: __mh_execute_header not in any section\n";
6719 return;
6720 }
Tim Northoverfbefee32016-07-14 23:13:03 +00006721 // When this code is trying to disassemble a symbol at a time and in the
6722 // case there is only the __mh_execute_header symbol left as in a stripped
6723 // executable, we need to deal with this by ignoring this symbol so the
6724 // whole section is disassembled and this symbol is then not displayed.
6725 if (SymName == "__mh_execute_header" || SymName == "__mh_dylib_header" ||
6726 SymName == "__mh_bundle_header" || SymName == "__mh_object_header" ||
6727 SymName == "__mh_preload_header" || SymName == "__mh_dylinker_header")
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006728 continue;
6729
Kevin Enderby6a221752015-03-17 17:10:57 +00006730 // If we are only disassembling one symbol see if this is that symbol.
6731 if (!DisSymName.empty() && DisSymName != SymName)
6732 continue;
6733
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006734 // Start at the address of the symbol relative to the section's address.
Tim Northoverf203ab52016-07-14 22:13:32 +00006735 uint64_t SectSize = Sections[SectIdx].getSize();
Rafael Espindoladea00162015-07-03 17:44:18 +00006736 uint64_t Start = Symbols[SymIdx].getValue();
Rafael Espindola80291272014-10-08 15:28:58 +00006737 uint64_t SectionAddress = Sections[SectIdx].getAddress();
Cameron Zwarich54478a52012-02-03 05:42:17 +00006738 Start -= SectionAddress;
Owen Andersond9243c42011-10-17 21:37:35 +00006739
Tim Northoverf203ab52016-07-14 22:13:32 +00006740 if (Start > SectSize) {
6741 outs() << "section data ends, " << SymName
6742 << " lies outside valid range\n";
6743 return;
6744 }
6745
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006746 // Stop disassembling either at the beginning of the next symbol or at
6747 // the end of the section.
Kevin Enderbyedd58722012-05-15 18:57:14 +00006748 bool containsNextSym = false;
Owen Andersond9243c42011-10-17 21:37:35 +00006749 uint64_t NextSym = 0;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006750 uint64_t NextSymIdx = SymIdx + 1;
Owen Andersond9243c42011-10-17 21:37:35 +00006751 while (Symbols.size() > NextSymIdx) {
Kevin Enderby7bd8d992016-05-02 20:28:12 +00006752 Expected<SymbolRef::Type> STOrErr = Symbols[NextSymIdx].getType();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00006753 if (!STOrErr)
6754 report_error(MachOOF->getFileName(), STOrErr.takeError());
Kevin Enderby5afbc1c2016-03-23 20:27:00 +00006755 SymbolRef::Type NextSymType = *STOrErr;
Owen Andersond9243c42011-10-17 21:37:35 +00006756 if (NextSymType == SymbolRef::ST_Function) {
Rafael Espindola80291272014-10-08 15:28:58 +00006757 containsNextSym =
6758 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
Rafael Espindoladea00162015-07-03 17:44:18 +00006759 NextSym = Symbols[NextSymIdx].getValue();
Cameron Zwarich54478a52012-02-03 05:42:17 +00006760 NextSym -= SectionAddress;
Owen Andersond9243c42011-10-17 21:37:35 +00006761 break;
6762 }
6763 ++NextSymIdx;
6764 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006765
Tim Northoverf203ab52016-07-14 22:13:32 +00006766 uint64_t End = containsNextSym ? std::min(NextSym, SectSize) : SectSize;
Owen Andersond9243c42011-10-17 21:37:35 +00006767 uint64_t Size;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006768
6769 symbolTableWorked = true;
Rafael Espindolabd604f22014-11-07 00:52:15 +00006770
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006771 DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
Tim Northover09ca33e2016-04-22 23:23:31 +00006772 bool IsThumb = MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb;
6773
6774 // We only need the dedicated Thumb target if there's a real choice
6775 // (i.e. we're not targeting M-class) and the function is Thumb.
6776 bool UseThumbTarget = IsThumb && ThumbTarget;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006777
Kevin Enderbyc138da32017-02-06 18:43:18 +00006778 // If we are not specifying a symbol to start disassembly with and this
6779 // is the first symbol in the section but not at the start of the section
6780 // then move the disassembly index to the start of the section and
6781 // don't print the symbol name just yet. This is so the bytes before the
6782 // first symbol are disassembled.
6783 uint64_t SymbolStart = Start;
6784 if (DisSymName.empty() && FirstSymbol && Start != 0) {
6785 FirstSymbolAtSectionStart = false;
6786 Start = 0;
6787 }
6788 else
6789 outs() << SymName << ":\n";
6790
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006791 DILineInfo lastLine;
6792 for (uint64_t Index = Start; Index < End; Index += Size) {
6793 MCInst Inst;
Owen Andersond9243c42011-10-17 21:37:35 +00006794
Kevin Enderbyc138da32017-02-06 18:43:18 +00006795 // If this is the first symbol in the section and it was not at the
6796 // start of the section, see if we are at its Index now and if so print
6797 // the symbol name.
6798 if (FirstSymbol && !FirstSymbolAtSectionStart && Index == SymbolStart)
6799 outs() << SymName << ":\n";
6800
Kevin Enderbybf246f52014-09-24 23:08:22 +00006801 uint64_t PC = SectAddress + Index;
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00006802 if (!NoLeadingAddr) {
6803 if (FullLeadingAddr) {
6804 if (MachOOF->is64Bit())
6805 outs() << format("%016" PRIx64, PC);
6806 else
6807 outs() << format("%08" PRIx64, PC);
6808 } else {
6809 outs() << format("%8" PRIx64 ":", PC);
6810 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006811 }
Kevin Enderby4b627be2016-04-28 20:14:13 +00006812 if (!NoShowRawInsn || Arch == Triple::arm)
Kevin Enderbybf246f52014-09-24 23:08:22 +00006813 outs() << "\t";
Kevin Enderby273ae012013-06-06 17:20:50 +00006814
6815 // Check the data in code table here to see if this is data not an
6816 // instruction to be disassembled.
6817 DiceTable Dice;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006818 Dice.push_back(std::make_pair(PC, DiceRef()));
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006819 dice_table_iterator DTI =
6820 std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(),
6821 compareDiceTableEntries);
6822 if (DTI != Dices.end()) {
Kevin Enderby273ae012013-06-06 17:20:50 +00006823 uint16_t Length;
6824 DTI->second.getLength(Length);
Kevin Enderby273ae012013-06-06 17:20:50 +00006825 uint16_t Kind;
6826 DTI->second.getKind(Kind);
Colin LeMahieufc32b1b2015-03-18 19:27:31 +00006827 Size = DumpDataInCode(Bytes.data() + Index, Length, Kind);
Kevin Enderby930fdc72014-11-06 19:00:13 +00006828 if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) &&
6829 (PC == (DTI->first + Length - 1)) && (Length & 1))
6830 Size++;
Kevin Enderby273ae012013-06-06 17:20:50 +00006831 continue;
6832 }
6833
Kevin Enderbybf246f52014-09-24 23:08:22 +00006834 SmallVector<char, 64> AnnotationsBytes;
6835 raw_svector_ostream Annotations(AnnotationsBytes);
6836
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006837 bool gotInst;
Tim Northover09ca33e2016-04-22 23:23:31 +00006838 if (UseThumbTarget)
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006839 gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006840 PC, DebugOut, Annotations);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006841 else
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006842 gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC,
Kevin Enderbybf246f52014-09-24 23:08:22 +00006843 DebugOut, Annotations);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006844 if (gotInst) {
Kevin Enderby4b627be2016-04-28 20:14:13 +00006845 if (!NoShowRawInsn || Arch == Triple::arm) {
Craig Topper0013be12015-09-21 05:32:41 +00006846 dumpBytes(makeArrayRef(Bytes.data() + Index, Size), outs());
Kevin Enderbybf246f52014-09-24 23:08:22 +00006847 }
6848 formatted_raw_ostream FormattedOS(outs());
Kevin Enderbybf246f52014-09-24 23:08:22 +00006849 StringRef AnnotationsStr = Annotations.str();
Tim Northover09ca33e2016-04-22 23:23:31 +00006850 if (UseThumbTarget)
Akira Hatanakab46d0232015-03-27 20:36:02 +00006851 ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr, *ThumbSTI);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006852 else
Akira Hatanaka1d079942015-03-28 20:44:05 +00006853 IP->printInst(&Inst, FormattedOS, AnnotationsStr, *STI);
Kevin Enderbybf246f52014-09-24 23:08:22 +00006854 emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo);
Owen Andersond9243c42011-10-17 21:37:35 +00006855
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006856 // Print debug info.
6857 if (diContext) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006858 DILineInfo dli = diContext->getLineInfoForAddress(PC);
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006859 // Print valid line info if it changed.
Alexey Samsonovd0109992014-04-18 21:36:39 +00006860 if (dli != lastLine && dli.Line != 0)
6861 outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
6862 << dli.Column;
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006863 lastLine = dli;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006864 }
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006865 outs() << "\n";
6866 } else {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006867 unsigned int Arch = MachOOF->getArch();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006868 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006869 outs() << format("\t.byte 0x%02x #bad opcode\n",
6870 *(Bytes.data() + Index) & 0xff);
6871 Size = 1; // skip exactly one illegible byte and move on.
Tim Northover09ca33e2016-04-22 23:23:31 +00006872 } else if (Arch == Triple::aarch64 ||
6873 (Arch == Triple::arm && !IsThumb)) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006874 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
6875 (*(Bytes.data() + Index + 1) & 0xff) << 8 |
6876 (*(Bytes.data() + Index + 2) & 0xff) << 16 |
6877 (*(Bytes.data() + Index + 3) & 0xff) << 24;
6878 outs() << format("\t.long\t0x%08x\n", opcode);
6879 Size = 4;
Tim Northover09ca33e2016-04-22 23:23:31 +00006880 } else if (Arch == Triple::arm) {
6881 assert(IsThumb && "ARM mode should have been dealt with above");
6882 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
6883 (*(Bytes.data() + Index + 1) & 0xff) << 8;
6884 outs() << format("\t.short\t0x%04x\n", opcode);
6885 Size = 2;
6886 } else{
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006887 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6888 if (Size == 0)
6889 Size = 1; // skip illegible bytes
6890 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006891 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006892 }
Kevin Enderbyc138da32017-02-06 18:43:18 +00006893 // Now that we are done disassembled the first symbol set the bool that
6894 // were doing this to false.
6895 FirstSymbol = false;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006896 }
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006897 if (!symbolTableWorked) {
Rafael Espindola80291272014-10-08 15:28:58 +00006898 // Reading the symbol table didn't work, disassemble the whole section.
6899 uint64_t SectAddress = Sections[SectIdx].getAddress();
6900 uint64_t SectSize = Sections[SectIdx].getSize();
Kevin Enderbybadd1002012-05-18 00:13:56 +00006901 uint64_t InstSize;
6902 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
Bill Wendling4e68e062012-07-19 00:17:40 +00006903 MCInst Inst;
Kevin Enderbybadd1002012-05-18 00:13:56 +00006904
Kevin Enderbybf246f52014-09-24 23:08:22 +00006905 uint64_t PC = SectAddress + Index;
Kevin Enderby02d3a372017-01-31 18:09:10 +00006906 SmallVector<char, 64> AnnotationsBytes;
6907 raw_svector_ostream Annotations(AnnotationsBytes);
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006908 if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC,
Kevin Enderby02d3a372017-01-31 18:09:10 +00006909 DebugOut, Annotations)) {
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00006910 if (!NoLeadingAddr) {
6911 if (FullLeadingAddr) {
6912 if (MachOOF->is64Bit())
6913 outs() << format("%016" PRIx64, PC);
6914 else
6915 outs() << format("%08" PRIx64, PC);
6916 } else {
6917 outs() << format("%8" PRIx64 ":", PC);
6918 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006919 }
Kevin Enderby4b627be2016-04-28 20:14:13 +00006920 if (!NoShowRawInsn || Arch == Triple::arm) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00006921 outs() << "\t";
Craig Topper0013be12015-09-21 05:32:41 +00006922 dumpBytes(makeArrayRef(Bytes.data() + Index, InstSize), outs());
Kevin Enderbybf246f52014-09-24 23:08:22 +00006923 }
Kevin Enderby02d3a372017-01-31 18:09:10 +00006924 StringRef AnnotationsStr = Annotations.str();
6925 IP->printInst(&Inst, outs(), AnnotationsStr, *STI);
Bill Wendling4e68e062012-07-19 00:17:40 +00006926 outs() << "\n";
6927 } else {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006928 unsigned int Arch = MachOOF->getArch();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006929 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006930 outs() << format("\t.byte 0x%02x #bad opcode\n",
6931 *(Bytes.data() + Index) & 0xff);
6932 InstSize = 1; // skip exactly one illegible byte and move on.
6933 } else {
6934 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6935 if (InstSize == 0)
6936 InstSize = 1; // skip illegible bytes
6937 }
Bill Wendling4e68e062012-07-19 00:17:40 +00006938 }
Kevin Enderbybadd1002012-05-18 00:13:56 +00006939 }
6940 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00006941 // The TripleName's need to be reset if we are called again for a different
6942 // archtecture.
6943 TripleName = "";
6944 ThumbTripleName = "";
6945
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006946 if (SymbolizerInfo.method != nullptr)
6947 free(SymbolizerInfo.method);
Kevin Enderby04bf6932014-10-28 23:39:46 +00006948 if (SymbolizerInfo.demangled_name != nullptr)
6949 free(SymbolizerInfo.demangled_name);
Kevin Enderby930fdc72014-11-06 19:00:13 +00006950 if (ThumbSymbolizerInfo.method != nullptr)
6951 free(ThumbSymbolizerInfo.method);
6952 if (ThumbSymbolizerInfo.demangled_name != nullptr)
6953 free(ThumbSymbolizerInfo.demangled_name);
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006954 }
6955}
Tim Northover4bd286a2014-08-01 13:07:19 +00006956
Tim Northover39c70bb2014-08-12 11:52:59 +00006957//===----------------------------------------------------------------------===//
6958// __compact_unwind section dumping
6959//===----------------------------------------------------------------------===//
6960
Tim Northover4bd286a2014-08-01 13:07:19 +00006961namespace {
Tim Northover39c70bb2014-08-12 11:52:59 +00006962
6963template <typename T> static uint64_t readNext(const char *&Buf) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006964 using llvm::support::little;
6965 using llvm::support::unaligned;
Tim Northover39c70bb2014-08-12 11:52:59 +00006966
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006967 uint64_t Val = support::endian::read<T, little, unaligned>(Buf);
6968 Buf += sizeof(T);
6969 return Val;
6970}
Tim Northover39c70bb2014-08-12 11:52:59 +00006971
Tim Northover4bd286a2014-08-01 13:07:19 +00006972struct CompactUnwindEntry {
6973 uint32_t OffsetInSection;
6974
6975 uint64_t FunctionAddr;
6976 uint32_t Length;
6977 uint32_t CompactEncoding;
6978 uint64_t PersonalityAddr;
6979 uint64_t LSDAAddr;
6980
6981 RelocationRef FunctionReloc;
6982 RelocationRef PersonalityReloc;
6983 RelocationRef LSDAReloc;
6984
6985 CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64)
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006986 : OffsetInSection(Offset) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006987 if (Is64)
6988 read<uint64_t>(Contents.data() + Offset);
6989 else
6990 read<uint32_t>(Contents.data() + Offset);
6991 }
6992
6993private:
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006994 template <typename UIntPtr> void read(const char *Buf) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006995 FunctionAddr = readNext<UIntPtr>(Buf);
6996 Length = readNext<uint32_t>(Buf);
6997 CompactEncoding = readNext<uint32_t>(Buf);
6998 PersonalityAddr = readNext<UIntPtr>(Buf);
6999 LSDAAddr = readNext<UIntPtr>(Buf);
7000 }
7001};
7002}
7003
7004/// Given a relocation from __compact_unwind, consisting of the RelocationRef
7005/// and data being relocated, determine the best base Name and Addend to use for
7006/// display purposes.
7007///
7008/// 1. An Extern relocation will directly reference a symbol (and the data is
7009/// then already an addend), so use that.
7010/// 2. Otherwise the data is an offset in the object file's layout; try to find
7011// a symbol before it in the same section, and use the offset from there.
7012/// 3. Finally, if all that fails, fall back to an offset from the start of the
7013/// referenced section.
7014static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
7015 std::map<uint64_t, SymbolRef> &Symbols,
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007016 const RelocationRef &Reloc, uint64_t Addr,
Tim Northover4bd286a2014-08-01 13:07:19 +00007017 StringRef &Name, uint64_t &Addend) {
7018 if (Reloc.getSymbol() != Obj->symbol_end()) {
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00007019 Expected<StringRef> NameOrErr = Reloc.getSymbol()->getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00007020 if (!NameOrErr)
7021 report_error(Obj->getFileName(), NameOrErr.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00007022 Name = *NameOrErr;
Tim Northover4bd286a2014-08-01 13:07:19 +00007023 Addend = Addr;
7024 return;
7025 }
7026
7027 auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
Keno Fischerc780e8e2015-05-21 21:24:32 +00007028 SectionRef RelocSection = Obj->getAnyRelocationSection(RE);
Tim Northover4bd286a2014-08-01 13:07:19 +00007029
Rafael Espindola80291272014-10-08 15:28:58 +00007030 uint64_t SectionAddr = RelocSection.getAddress();
Tim Northover4bd286a2014-08-01 13:07:19 +00007031
7032 auto Sym = Symbols.upper_bound(Addr);
7033 if (Sym == Symbols.begin()) {
7034 // The first symbol in the object is after this reference, the best we can
7035 // do is section-relative notation.
7036 RelocSection.getName(Name);
7037 Addend = Addr - SectionAddr;
7038 return;
7039 }
7040
7041 // Go back one so that SymbolAddress <= Addr.
7042 --Sym;
7043
Kevin Enderby7bd8d992016-05-02 20:28:12 +00007044 auto SectOrErr = Sym->second.getSection();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00007045 if (!SectOrErr)
7046 report_error(Obj->getFileName(), SectOrErr.takeError());
Kevin Enderby7bd8d992016-05-02 20:28:12 +00007047 section_iterator SymSection = *SectOrErr;
Tim Northover4bd286a2014-08-01 13:07:19 +00007048 if (RelocSection == *SymSection) {
7049 // There's a valid symbol in the same section before this reference.
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00007050 Expected<StringRef> NameOrErr = Sym->second.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00007051 if (!NameOrErr)
7052 report_error(Obj->getFileName(), NameOrErr.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00007053 Name = *NameOrErr;
Tim Northover4bd286a2014-08-01 13:07:19 +00007054 Addend = Addr - Sym->first;
7055 return;
7056 }
7057
7058 // There is a symbol before this reference, but it's in a different
7059 // section. Probably not helpful to mention it, so use the section name.
7060 RelocSection.getName(Name);
7061 Addend = Addr - SectionAddr;
7062}
7063
7064static void printUnwindRelocDest(const MachOObjectFile *Obj,
7065 std::map<uint64_t, SymbolRef> &Symbols,
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007066 const RelocationRef &Reloc, uint64_t Addr) {
Tim Northover4bd286a2014-08-01 13:07:19 +00007067 StringRef Name;
7068 uint64_t Addend;
7069
Rafael Espindola854038e2015-06-26 14:51:16 +00007070 if (!Reloc.getObject())
Tim Northover0b0add52014-09-09 10:45:06 +00007071 return;
7072
Tim Northover4bd286a2014-08-01 13:07:19 +00007073 findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend);
7074
7075 outs() << Name;
7076 if (Addend)
Tim Northover63a25622014-08-11 09:14:06 +00007077 outs() << " + " << format("0x%" PRIx64, Addend);
Tim Northover4bd286a2014-08-01 13:07:19 +00007078}
7079
7080static void
7081printMachOCompactUnwindSection(const MachOObjectFile *Obj,
7082 std::map<uint64_t, SymbolRef> &Symbols,
7083 const SectionRef &CompactUnwind) {
7084
Tim Northoverbf55f7e2016-11-15 20:26:01 +00007085 if (!Obj->isLittleEndian()) {
7086 outs() << "Skipping big-endian __compact_unwind section\n";
7087 return;
7088 }
Tim Northover4bd286a2014-08-01 13:07:19 +00007089
7090 bool Is64 = Obj->is64Bit();
7091 uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
7092 uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
7093
7094 StringRef Contents;
7095 CompactUnwind.getContents(Contents);
7096
7097 SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
7098
7099 // First populate the initial raw offsets, encodings and so on from the entry.
7100 for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) {
7101 CompactUnwindEntry Entry(Contents.data(), Offset, Is64);
7102 CompactUnwinds.push_back(Entry);
7103 }
7104
7105 // Next we need to look at the relocations to find out what objects are
7106 // actually being referred to.
7107 for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00007108 uint64_t RelocAddress = Reloc.getOffset();
Tim Northover4bd286a2014-08-01 13:07:19 +00007109
7110 uint32_t EntryIdx = RelocAddress / EntrySize;
7111 uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
7112 CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx];
7113
7114 if (OffsetInEntry == 0)
7115 Entry.FunctionReloc = Reloc;
7116 else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t))
7117 Entry.PersonalityReloc = Reloc;
7118 else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t))
7119 Entry.LSDAReloc = Reloc;
Tim Northoverbf55f7e2016-11-15 20:26:01 +00007120 else {
7121 outs() << "Invalid relocation in __compact_unwind section\n";
7122 return;
7123 }
Tim Northover4bd286a2014-08-01 13:07:19 +00007124 }
7125
7126 // Finally, we're ready to print the data we've gathered.
7127 outs() << "Contents of __compact_unwind section:\n";
7128 for (auto &Entry : CompactUnwinds) {
Tim Northover06af2602014-08-08 12:08:51 +00007129 outs() << " Entry at offset "
7130 << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n";
Tim Northover4bd286a2014-08-01 13:07:19 +00007131
7132 // 1. Start of the region this entry applies to.
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007133 outs() << " start: " << format("0x%" PRIx64,
7134 Entry.FunctionAddr) << ' ';
7135 printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr);
Tim Northover4bd286a2014-08-01 13:07:19 +00007136 outs() << '\n';
7137
7138 // 2. Length of the region this entry applies to.
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007139 outs() << " length: " << format("0x%" PRIx32, Entry.Length)
7140 << '\n';
Tim Northover4bd286a2014-08-01 13:07:19 +00007141 // 3. The 32-bit compact encoding.
7142 outs() << " compact encoding: "
Tim Northoverb911bf82014-08-08 12:00:09 +00007143 << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n';
Tim Northover4bd286a2014-08-01 13:07:19 +00007144
7145 // 4. The personality function, if present.
Rafael Espindola854038e2015-06-26 14:51:16 +00007146 if (Entry.PersonalityReloc.getObject()) {
Tim Northover4bd286a2014-08-01 13:07:19 +00007147 outs() << " personality function: "
Tim Northoverb911bf82014-08-08 12:00:09 +00007148 << format("0x%" PRIx64, Entry.PersonalityAddr) << ' ';
Tim Northover4bd286a2014-08-01 13:07:19 +00007149 printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc,
7150 Entry.PersonalityAddr);
7151 outs() << '\n';
7152 }
7153
7154 // 5. This entry's language-specific data area.
Rafael Espindola854038e2015-06-26 14:51:16 +00007155 if (Entry.LSDAReloc.getObject()) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007156 outs() << " LSDA: " << format("0x%" PRIx64,
7157 Entry.LSDAAddr) << ' ';
Tim Northover4bd286a2014-08-01 13:07:19 +00007158 printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr);
7159 outs() << '\n';
7160 }
7161 }
7162}
7163
Tim Northover39c70bb2014-08-12 11:52:59 +00007164//===----------------------------------------------------------------------===//
7165// __unwind_info section dumping
7166//===----------------------------------------------------------------------===//
7167
7168static void printRegularSecondLevelUnwindPage(const char *PageStart) {
7169 const char *Pos = PageStart;
7170 uint32_t Kind = readNext<uint32_t>(Pos);
7171 (void)Kind;
7172 assert(Kind == 2 && "kind for a regular 2nd level index should be 2");
7173
7174 uint16_t EntriesStart = readNext<uint16_t>(Pos);
7175 uint16_t NumEntries = readNext<uint16_t>(Pos);
7176
7177 Pos = PageStart + EntriesStart;
7178 for (unsigned i = 0; i < NumEntries; ++i) {
7179 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
7180 uint32_t Encoding = readNext<uint32_t>(Pos);
7181
7182 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007183 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
7184 << ", "
7185 << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00007186 }
7187}
7188
7189static void printCompressedSecondLevelUnwindPage(
7190 const char *PageStart, uint32_t FunctionBase,
7191 const SmallVectorImpl<uint32_t> &CommonEncodings) {
7192 const char *Pos = PageStart;
7193 uint32_t Kind = readNext<uint32_t>(Pos);
7194 (void)Kind;
7195 assert(Kind == 3 && "kind for a compressed 2nd level index should be 3");
7196
7197 uint16_t EntriesStart = readNext<uint16_t>(Pos);
7198 uint16_t NumEntries = readNext<uint16_t>(Pos);
7199
7200 uint16_t EncodingsStart = readNext<uint16_t>(Pos);
7201 readNext<uint16_t>(Pos);
Aaron Ballman80930af2014-08-14 13:53:19 +00007202 const auto *PageEncodings = reinterpret_cast<const support::ulittle32_t *>(
7203 PageStart + EncodingsStart);
Tim Northover39c70bb2014-08-12 11:52:59 +00007204
7205 Pos = PageStart + EntriesStart;
7206 for (unsigned i = 0; i < NumEntries; ++i) {
7207 uint32_t Entry = readNext<uint32_t>(Pos);
7208 uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff);
7209 uint32_t EncodingIdx = Entry >> 24;
7210
7211 uint32_t Encoding;
7212 if (EncodingIdx < CommonEncodings.size())
7213 Encoding = CommonEncodings[EncodingIdx];
7214 else
7215 Encoding = PageEncodings[EncodingIdx - CommonEncodings.size()];
7216
7217 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007218 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
7219 << ", "
7220 << "encoding[" << EncodingIdx
7221 << "]=" << format("0x%08" PRIx32, Encoding) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00007222 }
7223}
7224
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007225static void printMachOUnwindInfoSection(const MachOObjectFile *Obj,
7226 std::map<uint64_t, SymbolRef> &Symbols,
7227 const SectionRef &UnwindInfo) {
Tim Northover39c70bb2014-08-12 11:52:59 +00007228
Tim Northoverbf55f7e2016-11-15 20:26:01 +00007229 if (!Obj->isLittleEndian()) {
7230 outs() << "Skipping big-endian __unwind_info section\n";
7231 return;
7232 }
Tim Northover39c70bb2014-08-12 11:52:59 +00007233
7234 outs() << "Contents of __unwind_info section:\n";
7235
7236 StringRef Contents;
7237 UnwindInfo.getContents(Contents);
7238 const char *Pos = Contents.data();
7239
7240 //===----------------------------------
7241 // Section header
7242 //===----------------------------------
7243
7244 uint32_t Version = readNext<uint32_t>(Pos);
7245 outs() << " Version: "
7246 << format("0x%" PRIx32, Version) << '\n';
Tim Northoverbf55f7e2016-11-15 20:26:01 +00007247 if (Version != 1) {
7248 outs() << " Skipping section with unknown version\n";
7249 return;
7250 }
Tim Northover39c70bb2014-08-12 11:52:59 +00007251
7252 uint32_t CommonEncodingsStart = readNext<uint32_t>(Pos);
7253 outs() << " Common encodings array section offset: "
7254 << format("0x%" PRIx32, CommonEncodingsStart) << '\n';
7255 uint32_t NumCommonEncodings = readNext<uint32_t>(Pos);
7256 outs() << " Number of common encodings in array: "
7257 << format("0x%" PRIx32, NumCommonEncodings) << '\n';
7258
7259 uint32_t PersonalitiesStart = readNext<uint32_t>(Pos);
7260 outs() << " Personality function array section offset: "
7261 << format("0x%" PRIx32, PersonalitiesStart) << '\n';
7262 uint32_t NumPersonalities = readNext<uint32_t>(Pos);
7263 outs() << " Number of personality functions in array: "
7264 << format("0x%" PRIx32, NumPersonalities) << '\n';
7265
7266 uint32_t IndicesStart = readNext<uint32_t>(Pos);
7267 outs() << " Index array section offset: "
7268 << format("0x%" PRIx32, IndicesStart) << '\n';
7269 uint32_t NumIndices = readNext<uint32_t>(Pos);
7270 outs() << " Number of indices in array: "
7271 << format("0x%" PRIx32, NumIndices) << '\n';
7272
7273 //===----------------------------------
7274 // A shared list of common encodings
7275 //===----------------------------------
7276
7277 // These occupy indices in the range [0, N] whenever an encoding is referenced
7278 // from a compressed 2nd level index table. In practice the linker only
7279 // creates ~128 of these, so that indices are available to embed encodings in
7280 // the 2nd level index.
7281
7282 SmallVector<uint32_t, 64> CommonEncodings;
7283 outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n";
7284 Pos = Contents.data() + CommonEncodingsStart;
7285 for (unsigned i = 0; i < NumCommonEncodings; ++i) {
7286 uint32_t Encoding = readNext<uint32_t>(Pos);
7287 CommonEncodings.push_back(Encoding);
7288
7289 outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding)
7290 << '\n';
7291 }
7292
Tim Northover39c70bb2014-08-12 11:52:59 +00007293 //===----------------------------------
7294 // Personality functions used in this executable
7295 //===----------------------------------
7296
7297 // There should be only a handful of these (one per source language,
7298 // roughly). Particularly since they only get 2 bits in the compact encoding.
7299
7300 outs() << " Personality functions: (count = " << NumPersonalities << ")\n";
7301 Pos = Contents.data() + PersonalitiesStart;
7302 for (unsigned i = 0; i < NumPersonalities; ++i) {
7303 uint32_t PersonalityFn = readNext<uint32_t>(Pos);
7304 outs() << " personality[" << i + 1
7305 << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n';
7306 }
7307
7308 //===----------------------------------
7309 // The level 1 index entries
7310 //===----------------------------------
7311
7312 // These specify an approximate place to start searching for the more detailed
7313 // information, sorted by PC.
7314
7315 struct IndexEntry {
7316 uint32_t FunctionOffset;
7317 uint32_t SecondLevelPageStart;
7318 uint32_t LSDAStart;
7319 };
7320
7321 SmallVector<IndexEntry, 4> IndexEntries;
7322
7323 outs() << " Top level indices: (count = " << NumIndices << ")\n";
7324 Pos = Contents.data() + IndicesStart;
7325 for (unsigned i = 0; i < NumIndices; ++i) {
7326 IndexEntry Entry;
7327
7328 Entry.FunctionOffset = readNext<uint32_t>(Pos);
7329 Entry.SecondLevelPageStart = readNext<uint32_t>(Pos);
7330 Entry.LSDAStart = readNext<uint32_t>(Pos);
7331 IndexEntries.push_back(Entry);
7332
7333 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007334 << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset)
7335 << ", "
Tim Northover39c70bb2014-08-12 11:52:59 +00007336 << "2nd level page offset="
7337 << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007338 << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00007339 }
7340
Tim Northover39c70bb2014-08-12 11:52:59 +00007341 //===----------------------------------
7342 // Next come the LSDA tables
7343 //===----------------------------------
7344
7345 // The LSDA layout is rather implicit: it's a contiguous array of entries from
7346 // the first top-level index's LSDAOffset to the last (sentinel).
7347
7348 outs() << " LSDA descriptors:\n";
7349 Pos = Contents.data() + IndexEntries[0].LSDAStart;
7350 int NumLSDAs = (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) /
7351 (2 * sizeof(uint32_t));
7352 for (int i = 0; i < NumLSDAs; ++i) {
7353 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
7354 uint32_t LSDAOffset = readNext<uint32_t>(Pos);
7355 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007356 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
7357 << ", "
7358 << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00007359 }
7360
7361 //===----------------------------------
7362 // Finally, the 2nd level indices
7363 //===----------------------------------
7364
7365 // Generally these are 4K in size, and have 2 possible forms:
7366 // + Regular stores up to 511 entries with disparate encodings
7367 // + Compressed stores up to 1021 entries if few enough compact encoding
7368 // values are used.
7369 outs() << " Second level indices:\n";
7370 for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) {
7371 // The final sentinel top-level index has no associated 2nd level page
7372 if (IndexEntries[i].SecondLevelPageStart == 0)
7373 break;
7374
7375 outs() << " Second level index[" << i << "]: "
7376 << "offset in section="
7377 << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart)
7378 << ", "
7379 << "base function offset="
7380 << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n';
7381
7382 Pos = Contents.data() + IndexEntries[i].SecondLevelPageStart;
Aaron Ballman80930af2014-08-14 13:53:19 +00007383 uint32_t Kind = *reinterpret_cast<const support::ulittle32_t *>(Pos);
Tim Northover39c70bb2014-08-12 11:52:59 +00007384 if (Kind == 2)
7385 printRegularSecondLevelUnwindPage(Pos);
7386 else if (Kind == 3)
7387 printCompressedSecondLevelUnwindPage(Pos, IndexEntries[i].FunctionOffset,
7388 CommonEncodings);
7389 else
Tim Northoverbf55f7e2016-11-15 20:26:01 +00007390 outs() << " Skipping 2nd level page with unknown kind " << Kind
7391 << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00007392 }
7393}
7394
Tim Northover4bd286a2014-08-01 13:07:19 +00007395void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) {
7396 std::map<uint64_t, SymbolRef> Symbols;
7397 for (const SymbolRef &SymRef : Obj->symbols()) {
7398 // Discard any undefined or absolute symbols. They're not going to take part
7399 // in the convenience lookup for unwind info and just take up resources.
Kevin Enderby7bd8d992016-05-02 20:28:12 +00007400 auto SectOrErr = SymRef.getSection();
7401 if (!SectOrErr) {
7402 // TODO: Actually report errors helpfully.
7403 consumeError(SectOrErr.takeError());
7404 continue;
7405 }
7406 section_iterator Section = *SectOrErr;
Tim Northover4bd286a2014-08-01 13:07:19 +00007407 if (Section == Obj->section_end())
7408 continue;
7409
Rafael Espindoladea00162015-07-03 17:44:18 +00007410 uint64_t Addr = SymRef.getValue();
Tim Northover4bd286a2014-08-01 13:07:19 +00007411 Symbols.insert(std::make_pair(Addr, SymRef));
7412 }
7413
7414 for (const SectionRef &Section : Obj->sections()) {
7415 StringRef SectName;
7416 Section.getName(SectName);
7417 if (SectName == "__compact_unwind")
7418 printMachOCompactUnwindSection(Obj, Symbols, Section);
7419 else if (SectName == "__unwind_info")
Tim Northover39c70bb2014-08-12 11:52:59 +00007420 printMachOUnwindInfoSection(Obj, Symbols, Section);
Tim Northover4bd286a2014-08-01 13:07:19 +00007421 }
7422}
Kevin Enderbyb76d3862014-08-22 20:35:18 +00007423
7424static void PrintMachHeader(uint32_t magic, uint32_t cputype,
7425 uint32_t cpusubtype, uint32_t filetype,
7426 uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
7427 bool verbose) {
7428 outs() << "Mach header\n";
7429 outs() << " magic cputype cpusubtype caps filetype ncmds "
7430 "sizeofcmds flags\n";
7431 if (verbose) {
7432 if (magic == MachO::MH_MAGIC)
7433 outs() << " MH_MAGIC";
7434 else if (magic == MachO::MH_MAGIC_64)
7435 outs() << "MH_MAGIC_64";
7436 else
7437 outs() << format(" 0x%08" PRIx32, magic);
7438 switch (cputype) {
7439 case MachO::CPU_TYPE_I386:
7440 outs() << " I386";
7441 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7442 case MachO::CPU_SUBTYPE_I386_ALL:
7443 outs() << " ALL";
7444 break;
7445 default:
7446 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7447 break;
7448 }
7449 break;
7450 case MachO::CPU_TYPE_X86_64:
7451 outs() << " X86_64";
Kevin Enderby131d1772015-01-09 19:22:37 +00007452 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7453 case MachO::CPU_SUBTYPE_X86_64_ALL:
7454 outs() << " ALL";
7455 break;
7456 case MachO::CPU_SUBTYPE_X86_64_H:
7457 outs() << " Haswell";
7458 break;
7459 default:
7460 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7461 break;
7462 }
Kevin Enderbyb76d3862014-08-22 20:35:18 +00007463 break;
7464 case MachO::CPU_TYPE_ARM:
7465 outs() << " ARM";
7466 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7467 case MachO::CPU_SUBTYPE_ARM_ALL:
7468 outs() << " ALL";
7469 break;
7470 case MachO::CPU_SUBTYPE_ARM_V4T:
7471 outs() << " V4T";
7472 break;
7473 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
7474 outs() << " V5TEJ";
7475 break;
7476 case MachO::CPU_SUBTYPE_ARM_XSCALE:
7477 outs() << " XSCALE";
7478 break;
7479 case MachO::CPU_SUBTYPE_ARM_V6:
7480 outs() << " V6";
7481 break;
7482 case MachO::CPU_SUBTYPE_ARM_V6M:
7483 outs() << " V6M";
7484 break;
7485 case MachO::CPU_SUBTYPE_ARM_V7:
7486 outs() << " V7";
7487 break;
7488 case MachO::CPU_SUBTYPE_ARM_V7EM:
7489 outs() << " V7EM";
7490 break;
7491 case MachO::CPU_SUBTYPE_ARM_V7K:
7492 outs() << " V7K";
7493 break;
7494 case MachO::CPU_SUBTYPE_ARM_V7M:
7495 outs() << " V7M";
7496 break;
7497 case MachO::CPU_SUBTYPE_ARM_V7S:
7498 outs() << " V7S";
7499 break;
7500 default:
7501 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7502 break;
7503 }
7504 break;
7505 case MachO::CPU_TYPE_ARM64:
7506 outs() << " ARM64";
7507 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7508 case MachO::CPU_SUBTYPE_ARM64_ALL:
7509 outs() << " ALL";
7510 break;
7511 default:
7512 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7513 break;
7514 }
7515 break;
7516 case MachO::CPU_TYPE_POWERPC:
7517 outs() << " PPC";
7518 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7519 case MachO::CPU_SUBTYPE_POWERPC_ALL:
7520 outs() << " ALL";
7521 break;
7522 default:
7523 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7524 break;
7525 }
7526 break;
7527 case MachO::CPU_TYPE_POWERPC64:
7528 outs() << " PPC64";
7529 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7530 case MachO::CPU_SUBTYPE_POWERPC_ALL:
7531 outs() << " ALL";
7532 break;
7533 default:
7534 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7535 break;
7536 }
7537 break;
Kevin Enderby40fdbf82016-01-26 18:20:49 +00007538 default:
7539 outs() << format(" %7d", cputype);
7540 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7541 break;
Kevin Enderbyb76d3862014-08-22 20:35:18 +00007542 }
7543 if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007544 outs() << " LIB64";
Kevin Enderbyb76d3862014-08-22 20:35:18 +00007545 } else {
7546 outs() << format(" 0x%02" PRIx32,
7547 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
7548 }
7549 switch (filetype) {
7550 case MachO::MH_OBJECT:
7551 outs() << " OBJECT";
7552 break;
7553 case MachO::MH_EXECUTE:
7554 outs() << " EXECUTE";
7555 break;
7556 case MachO::MH_FVMLIB:
7557 outs() << " FVMLIB";
7558 break;
7559 case MachO::MH_CORE:
7560 outs() << " CORE";
7561 break;
7562 case MachO::MH_PRELOAD:
7563 outs() << " PRELOAD";
7564 break;
7565 case MachO::MH_DYLIB:
7566 outs() << " DYLIB";
7567 break;
7568 case MachO::MH_DYLIB_STUB:
7569 outs() << " DYLIB_STUB";
7570 break;
7571 case MachO::MH_DYLINKER:
7572 outs() << " DYLINKER";
7573 break;
7574 case MachO::MH_BUNDLE:
7575 outs() << " BUNDLE";
7576 break;
7577 case MachO::MH_DSYM:
7578 outs() << " DSYM";
7579 break;
7580 case MachO::MH_KEXT_BUNDLE:
7581 outs() << " KEXTBUNDLE";
7582 break;
7583 default:
7584 outs() << format(" %10u", filetype);
7585 break;
7586 }
7587 outs() << format(" %5u", ncmds);
7588 outs() << format(" %10u", sizeofcmds);
7589 uint32_t f = flags;
7590 if (f & MachO::MH_NOUNDEFS) {
7591 outs() << " NOUNDEFS";
7592 f &= ~MachO::MH_NOUNDEFS;
7593 }
7594 if (f & MachO::MH_INCRLINK) {
7595 outs() << " INCRLINK";
7596 f &= ~MachO::MH_INCRLINK;
7597 }
7598 if (f & MachO::MH_DYLDLINK) {
7599 outs() << " DYLDLINK";
7600 f &= ~MachO::MH_DYLDLINK;
7601 }
7602 if (f & MachO::MH_BINDATLOAD) {
7603 outs() << " BINDATLOAD";
7604 f &= ~MachO::MH_BINDATLOAD;
7605 }
7606 if (f & MachO::MH_PREBOUND) {
7607 outs() << " PREBOUND";
7608 f &= ~MachO::MH_PREBOUND;
7609 }
7610 if (f & MachO::MH_SPLIT_SEGS) {
7611 outs() << " SPLIT_SEGS";
7612 f &= ~MachO::MH_SPLIT_SEGS;
7613 }
7614 if (f & MachO::MH_LAZY_INIT) {
7615 outs() << " LAZY_INIT";
7616 f &= ~MachO::MH_LAZY_INIT;
7617 }
7618 if (f & MachO::MH_TWOLEVEL) {
7619 outs() << " TWOLEVEL";
7620 f &= ~MachO::MH_TWOLEVEL;
7621 }
7622 if (f & MachO::MH_FORCE_FLAT) {
7623 outs() << " FORCE_FLAT";
7624 f &= ~MachO::MH_FORCE_FLAT;
7625 }
7626 if (f & MachO::MH_NOMULTIDEFS) {
7627 outs() << " NOMULTIDEFS";
7628 f &= ~MachO::MH_NOMULTIDEFS;
7629 }
7630 if (f & MachO::MH_NOFIXPREBINDING) {
7631 outs() << " NOFIXPREBINDING";
7632 f &= ~MachO::MH_NOFIXPREBINDING;
7633 }
7634 if (f & MachO::MH_PREBINDABLE) {
7635 outs() << " PREBINDABLE";
7636 f &= ~MachO::MH_PREBINDABLE;
7637 }
7638 if (f & MachO::MH_ALLMODSBOUND) {
7639 outs() << " ALLMODSBOUND";
7640 f &= ~MachO::MH_ALLMODSBOUND;
7641 }
7642 if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
7643 outs() << " SUBSECTIONS_VIA_SYMBOLS";
7644 f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
7645 }
7646 if (f & MachO::MH_CANONICAL) {
7647 outs() << " CANONICAL";
7648 f &= ~MachO::MH_CANONICAL;
7649 }
7650 if (f & MachO::MH_WEAK_DEFINES) {
7651 outs() << " WEAK_DEFINES";
7652 f &= ~MachO::MH_WEAK_DEFINES;
7653 }
7654 if (f & MachO::MH_BINDS_TO_WEAK) {
7655 outs() << " BINDS_TO_WEAK";
7656 f &= ~MachO::MH_BINDS_TO_WEAK;
7657 }
7658 if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
7659 outs() << " ALLOW_STACK_EXECUTION";
7660 f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
7661 }
7662 if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
7663 outs() << " DEAD_STRIPPABLE_DYLIB";
7664 f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
7665 }
7666 if (f & MachO::MH_PIE) {
7667 outs() << " PIE";
7668 f &= ~MachO::MH_PIE;
7669 }
7670 if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
7671 outs() << " NO_REEXPORTED_DYLIBS";
7672 f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
7673 }
7674 if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
7675 outs() << " MH_HAS_TLV_DESCRIPTORS";
7676 f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
7677 }
7678 if (f & MachO::MH_NO_HEAP_EXECUTION) {
7679 outs() << " MH_NO_HEAP_EXECUTION";
7680 f &= ~MachO::MH_NO_HEAP_EXECUTION;
7681 }
7682 if (f & MachO::MH_APP_EXTENSION_SAFE) {
7683 outs() << " APP_EXTENSION_SAFE";
7684 f &= ~MachO::MH_APP_EXTENSION_SAFE;
7685 }
Kevin Enderbydf0d6da2017-06-19 19:38:22 +00007686 if (f & MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO) {
7687 outs() << " NLIST_OUTOFSYNC_WITH_DYLDINFO";
7688 f &= ~MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO;
7689 }
Kevin Enderbyb76d3862014-08-22 20:35:18 +00007690 if (f != 0 || flags == 0)
7691 outs() << format(" 0x%08" PRIx32, f);
7692 } else {
7693 outs() << format(" 0x%08" PRIx32, magic);
7694 outs() << format(" %7d", cputype);
7695 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7696 outs() << format(" 0x%02" PRIx32,
7697 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
7698 outs() << format(" %10u", filetype);
7699 outs() << format(" %5u", ncmds);
7700 outs() << format(" %10u", sizeofcmds);
7701 outs() << format(" 0x%08" PRIx32, flags);
7702 }
7703 outs() << "\n";
7704}
7705
Kevin Enderby956366c2014-08-29 22:30:52 +00007706static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
7707 StringRef SegName, uint64_t vmaddr,
7708 uint64_t vmsize, uint64_t fileoff,
7709 uint64_t filesize, uint32_t maxprot,
7710 uint32_t initprot, uint32_t nsects,
7711 uint32_t flags, uint32_t object_size,
7712 bool verbose) {
7713 uint64_t expected_cmdsize;
7714 if (cmd == MachO::LC_SEGMENT) {
7715 outs() << " cmd LC_SEGMENT\n";
7716 expected_cmdsize = nsects;
7717 expected_cmdsize *= sizeof(struct MachO::section);
7718 expected_cmdsize += sizeof(struct MachO::segment_command);
7719 } else {
7720 outs() << " cmd LC_SEGMENT_64\n";
7721 expected_cmdsize = nsects;
7722 expected_cmdsize *= sizeof(struct MachO::section_64);
7723 expected_cmdsize += sizeof(struct MachO::segment_command_64);
7724 }
7725 outs() << " cmdsize " << cmdsize;
7726 if (cmdsize != expected_cmdsize)
7727 outs() << " Inconsistent size\n";
7728 else
7729 outs() << "\n";
7730 outs() << " segname " << SegName << "\n";
7731 if (cmd == MachO::LC_SEGMENT_64) {
7732 outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n";
7733 outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n";
7734 } else {
Kevin Enderbyadb7c432014-12-16 18:58:11 +00007735 outs() << " vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n";
7736 outs() << " vmsize " << format("0x%08" PRIx64, vmsize) << "\n";
Kevin Enderby956366c2014-08-29 22:30:52 +00007737 }
7738 outs() << " fileoff " << fileoff;
7739 if (fileoff > object_size)
7740 outs() << " (past end of file)\n";
7741 else
7742 outs() << "\n";
7743 outs() << " filesize " << filesize;
7744 if (fileoff + filesize > object_size)
7745 outs() << " (past end of file)\n";
7746 else
7747 outs() << "\n";
7748 if (verbose) {
7749 if ((maxprot &
7750 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7751 MachO::VM_PROT_EXECUTE)) != 0)
7752 outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n";
7753 else {
Davide Italiano37ff06a2015-09-02 16:53:25 +00007754 outs() << " maxprot ";
7755 outs() << ((maxprot & MachO::VM_PROT_READ) ? "r" : "-");
7756 outs() << ((maxprot & MachO::VM_PROT_WRITE) ? "w" : "-");
7757 outs() << ((maxprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
Kevin Enderby956366c2014-08-29 22:30:52 +00007758 }
7759 if ((initprot &
7760 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7761 MachO::VM_PROT_EXECUTE)) != 0)
Kevin Enderby41c9c002016-10-21 18:22:35 +00007762 outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n";
Kevin Enderby956366c2014-08-29 22:30:52 +00007763 else {
Kevin Enderby41c9c002016-10-21 18:22:35 +00007764 outs() << " initprot ";
Davide Italiano37ff06a2015-09-02 16:53:25 +00007765 outs() << ((initprot & MachO::VM_PROT_READ) ? "r" : "-");
7766 outs() << ((initprot & MachO::VM_PROT_WRITE) ? "w" : "-");
7767 outs() << ((initprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
Kevin Enderby956366c2014-08-29 22:30:52 +00007768 }
7769 } else {
7770 outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n";
7771 outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n";
7772 }
7773 outs() << " nsects " << nsects << "\n";
7774 if (verbose) {
7775 outs() << " flags";
7776 if (flags == 0)
7777 outs() << " (none)\n";
7778 else {
7779 if (flags & MachO::SG_HIGHVM) {
7780 outs() << " HIGHVM";
7781 flags &= ~MachO::SG_HIGHVM;
7782 }
7783 if (flags & MachO::SG_FVMLIB) {
7784 outs() << " FVMLIB";
7785 flags &= ~MachO::SG_FVMLIB;
7786 }
7787 if (flags & MachO::SG_NORELOC) {
7788 outs() << " NORELOC";
7789 flags &= ~MachO::SG_NORELOC;
7790 }
7791 if (flags & MachO::SG_PROTECTED_VERSION_1) {
7792 outs() << " PROTECTED_VERSION_1";
7793 flags &= ~MachO::SG_PROTECTED_VERSION_1;
7794 }
7795 if (flags)
7796 outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n";
7797 else
7798 outs() << "\n";
7799 }
7800 } else {
7801 outs() << " flags " << format("0x%" PRIx32, flags) << "\n";
7802 }
7803}
7804
7805static void PrintSection(const char *sectname, const char *segname,
7806 uint64_t addr, uint64_t size, uint32_t offset,
7807 uint32_t align, uint32_t reloff, uint32_t nreloc,
7808 uint32_t flags, uint32_t reserved1, uint32_t reserved2,
7809 uint32_t cmd, const char *sg_segname,
7810 uint32_t filetype, uint32_t object_size,
7811 bool verbose) {
7812 outs() << "Section\n";
7813 outs() << " sectname " << format("%.16s\n", sectname);
7814 outs() << " segname " << format("%.16s", segname);
7815 if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0)
7816 outs() << " (does not match segment)\n";
7817 else
7818 outs() << "\n";
7819 if (cmd == MachO::LC_SEGMENT_64) {
7820 outs() << " addr " << format("0x%016" PRIx64, addr) << "\n";
7821 outs() << " size " << format("0x%016" PRIx64, size);
7822 } else {
Kevin Enderby75594b62014-12-16 21:00:25 +00007823 outs() << " addr " << format("0x%08" PRIx64, addr) << "\n";
7824 outs() << " size " << format("0x%08" PRIx64, size);
Kevin Enderby956366c2014-08-29 22:30:52 +00007825 }
7826 if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size)
7827 outs() << " (past end of file)\n";
7828 else
7829 outs() << "\n";
7830 outs() << " offset " << offset;
7831 if (offset > object_size)
7832 outs() << " (past end of file)\n";
7833 else
7834 outs() << "\n";
7835 uint32_t align_shifted = 1 << align;
7836 outs() << " align 2^" << align << " (" << align_shifted << ")\n";
7837 outs() << " reloff " << reloff;
7838 if (reloff > object_size)
7839 outs() << " (past end of file)\n";
7840 else
7841 outs() << "\n";
7842 outs() << " nreloc " << nreloc;
7843 if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size)
7844 outs() << " (past end of file)\n";
7845 else
7846 outs() << "\n";
7847 uint32_t section_type = flags & MachO::SECTION_TYPE;
7848 if (verbose) {
7849 outs() << " type";
7850 if (section_type == MachO::S_REGULAR)
7851 outs() << " S_REGULAR\n";
7852 else if (section_type == MachO::S_ZEROFILL)
7853 outs() << " S_ZEROFILL\n";
7854 else if (section_type == MachO::S_CSTRING_LITERALS)
7855 outs() << " S_CSTRING_LITERALS\n";
7856 else if (section_type == MachO::S_4BYTE_LITERALS)
7857 outs() << " S_4BYTE_LITERALS\n";
7858 else if (section_type == MachO::S_8BYTE_LITERALS)
7859 outs() << " S_8BYTE_LITERALS\n";
7860 else if (section_type == MachO::S_16BYTE_LITERALS)
7861 outs() << " S_16BYTE_LITERALS\n";
7862 else if (section_type == MachO::S_LITERAL_POINTERS)
7863 outs() << " S_LITERAL_POINTERS\n";
7864 else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS)
7865 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
7866 else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS)
7867 outs() << " S_LAZY_SYMBOL_POINTERS\n";
7868 else if (section_type == MachO::S_SYMBOL_STUBS)
7869 outs() << " S_SYMBOL_STUBS\n";
7870 else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS)
7871 outs() << " S_MOD_INIT_FUNC_POINTERS\n";
7872 else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS)
7873 outs() << " S_MOD_TERM_FUNC_POINTERS\n";
7874 else if (section_type == MachO::S_COALESCED)
7875 outs() << " S_COALESCED\n";
7876 else if (section_type == MachO::S_INTERPOSING)
7877 outs() << " S_INTERPOSING\n";
7878 else if (section_type == MachO::S_DTRACE_DOF)
7879 outs() << " S_DTRACE_DOF\n";
7880 else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS)
7881 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
7882 else if (section_type == MachO::S_THREAD_LOCAL_REGULAR)
7883 outs() << " S_THREAD_LOCAL_REGULAR\n";
7884 else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL)
7885 outs() << " S_THREAD_LOCAL_ZEROFILL\n";
7886 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES)
7887 outs() << " S_THREAD_LOCAL_VARIABLES\n";
7888 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7889 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
7890 else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS)
7891 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
7892 else
7893 outs() << format("0x%08" PRIx32, section_type) << "\n";
7894 outs() << "attributes";
7895 uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES;
7896 if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS)
7897 outs() << " PURE_INSTRUCTIONS";
7898 if (section_attributes & MachO::S_ATTR_NO_TOC)
7899 outs() << " NO_TOC";
7900 if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS)
7901 outs() << " STRIP_STATIC_SYMS";
7902 if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP)
7903 outs() << " NO_DEAD_STRIP";
7904 if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT)
7905 outs() << " LIVE_SUPPORT";
7906 if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE)
7907 outs() << " SELF_MODIFYING_CODE";
7908 if (section_attributes & MachO::S_ATTR_DEBUG)
7909 outs() << " DEBUG";
7910 if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS)
7911 outs() << " SOME_INSTRUCTIONS";
7912 if (section_attributes & MachO::S_ATTR_EXT_RELOC)
7913 outs() << " EXT_RELOC";
7914 if (section_attributes & MachO::S_ATTR_LOC_RELOC)
7915 outs() << " LOC_RELOC";
7916 if (section_attributes == 0)
7917 outs() << " (none)";
7918 outs() << "\n";
7919 } else
7920 outs() << " flags " << format("0x%08" PRIx32, flags) << "\n";
7921 outs() << " reserved1 " << reserved1;
7922 if (section_type == MachO::S_SYMBOL_STUBS ||
7923 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
7924 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
7925 section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
7926 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7927 outs() << " (index into indirect symbol table)\n";
7928 else
7929 outs() << "\n";
7930 outs() << " reserved2 " << reserved2;
7931 if (section_type == MachO::S_SYMBOL_STUBS)
7932 outs() << " (size of stubs)\n";
7933 else
7934 outs() << "\n";
7935}
7936
David Majnemer73cc6ff2014-11-13 19:48:56 +00007937static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit,
Kevin Enderby956366c2014-08-29 22:30:52 +00007938 uint32_t object_size) {
7939 outs() << " cmd LC_SYMTAB\n";
7940 outs() << " cmdsize " << st.cmdsize;
7941 if (st.cmdsize != sizeof(struct MachO::symtab_command))
7942 outs() << " Incorrect size\n";
7943 else
7944 outs() << "\n";
7945 outs() << " symoff " << st.symoff;
7946 if (st.symoff > object_size)
7947 outs() << " (past end of file)\n";
7948 else
7949 outs() << "\n";
7950 outs() << " nsyms " << st.nsyms;
7951 uint64_t big_size;
David Majnemer73cc6ff2014-11-13 19:48:56 +00007952 if (Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00007953 big_size = st.nsyms;
7954 big_size *= sizeof(struct MachO::nlist_64);
7955 big_size += st.symoff;
7956 if (big_size > object_size)
7957 outs() << " (past end of file)\n";
7958 else
7959 outs() << "\n";
7960 } else {
7961 big_size = st.nsyms;
7962 big_size *= sizeof(struct MachO::nlist);
7963 big_size += st.symoff;
7964 if (big_size > object_size)
7965 outs() << " (past end of file)\n";
7966 else
7967 outs() << "\n";
7968 }
7969 outs() << " stroff " << st.stroff;
7970 if (st.stroff > object_size)
7971 outs() << " (past end of file)\n";
7972 else
7973 outs() << "\n";
7974 outs() << " strsize " << st.strsize;
7975 big_size = st.stroff;
7976 big_size += st.strsize;
7977 if (big_size > object_size)
7978 outs() << " (past end of file)\n";
7979 else
7980 outs() << "\n";
7981}
7982
7983static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
7984 uint32_t nsyms, uint32_t object_size,
David Majnemer73cc6ff2014-11-13 19:48:56 +00007985 bool Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00007986 outs() << " cmd LC_DYSYMTAB\n";
7987 outs() << " cmdsize " << dyst.cmdsize;
7988 if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
7989 outs() << " Incorrect size\n";
7990 else
7991 outs() << "\n";
7992 outs() << " ilocalsym " << dyst.ilocalsym;
7993 if (dyst.ilocalsym > nsyms)
7994 outs() << " (greater than the number of symbols)\n";
7995 else
7996 outs() << "\n";
7997 outs() << " nlocalsym " << dyst.nlocalsym;
7998 uint64_t big_size;
7999 big_size = dyst.ilocalsym;
8000 big_size += dyst.nlocalsym;
8001 if (big_size > nsyms)
8002 outs() << " (past the end of the symbol table)\n";
8003 else
8004 outs() << "\n";
8005 outs() << " iextdefsym " << dyst.iextdefsym;
8006 if (dyst.iextdefsym > nsyms)
8007 outs() << " (greater than the number of symbols)\n";
8008 else
8009 outs() << "\n";
8010 outs() << " nextdefsym " << dyst.nextdefsym;
8011 big_size = dyst.iextdefsym;
8012 big_size += dyst.nextdefsym;
8013 if (big_size > nsyms)
8014 outs() << " (past the end of the symbol table)\n";
8015 else
8016 outs() << "\n";
8017 outs() << " iundefsym " << dyst.iundefsym;
8018 if (dyst.iundefsym > nsyms)
8019 outs() << " (greater than the number of symbols)\n";
8020 else
8021 outs() << "\n";
8022 outs() << " nundefsym " << dyst.nundefsym;
8023 big_size = dyst.iundefsym;
8024 big_size += dyst.nundefsym;
8025 if (big_size > nsyms)
8026 outs() << " (past the end of the symbol table)\n";
8027 else
8028 outs() << "\n";
8029 outs() << " tocoff " << dyst.tocoff;
8030 if (dyst.tocoff > object_size)
8031 outs() << " (past end of file)\n";
8032 else
8033 outs() << "\n";
8034 outs() << " ntoc " << dyst.ntoc;
8035 big_size = dyst.ntoc;
8036 big_size *= sizeof(struct MachO::dylib_table_of_contents);
8037 big_size += dyst.tocoff;
8038 if (big_size > object_size)
8039 outs() << " (past end of file)\n";
8040 else
8041 outs() << "\n";
8042 outs() << " modtaboff " << dyst.modtaboff;
8043 if (dyst.modtaboff > object_size)
8044 outs() << " (past end of file)\n";
8045 else
8046 outs() << "\n";
8047 outs() << " nmodtab " << dyst.nmodtab;
8048 uint64_t modtabend;
David Majnemer73cc6ff2014-11-13 19:48:56 +00008049 if (Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00008050 modtabend = dyst.nmodtab;
8051 modtabend *= sizeof(struct MachO::dylib_module_64);
8052 modtabend += dyst.modtaboff;
8053 } else {
8054 modtabend = dyst.nmodtab;
8055 modtabend *= sizeof(struct MachO::dylib_module);
8056 modtabend += dyst.modtaboff;
8057 }
8058 if (modtabend > object_size)
8059 outs() << " (past end of file)\n";
8060 else
8061 outs() << "\n";
8062 outs() << " extrefsymoff " << dyst.extrefsymoff;
8063 if (dyst.extrefsymoff > object_size)
8064 outs() << " (past end of file)\n";
8065 else
8066 outs() << "\n";
8067 outs() << " nextrefsyms " << dyst.nextrefsyms;
8068 big_size = dyst.nextrefsyms;
8069 big_size *= sizeof(struct MachO::dylib_reference);
8070 big_size += dyst.extrefsymoff;
8071 if (big_size > object_size)
8072 outs() << " (past end of file)\n";
8073 else
8074 outs() << "\n";
8075 outs() << " indirectsymoff " << dyst.indirectsymoff;
8076 if (dyst.indirectsymoff > object_size)
8077 outs() << " (past end of file)\n";
8078 else
8079 outs() << "\n";
8080 outs() << " nindirectsyms " << dyst.nindirectsyms;
8081 big_size = dyst.nindirectsyms;
8082 big_size *= sizeof(uint32_t);
8083 big_size += dyst.indirectsymoff;
8084 if (big_size > object_size)
8085 outs() << " (past end of file)\n";
8086 else
8087 outs() << "\n";
8088 outs() << " extreloff " << dyst.extreloff;
8089 if (dyst.extreloff > object_size)
8090 outs() << " (past end of file)\n";
8091 else
8092 outs() << "\n";
8093 outs() << " nextrel " << dyst.nextrel;
8094 big_size = dyst.nextrel;
8095 big_size *= sizeof(struct MachO::relocation_info);
8096 big_size += dyst.extreloff;
8097 if (big_size > object_size)
8098 outs() << " (past end of file)\n";
8099 else
8100 outs() << "\n";
8101 outs() << " locreloff " << dyst.locreloff;
8102 if (dyst.locreloff > object_size)
8103 outs() << " (past end of file)\n";
8104 else
8105 outs() << "\n";
8106 outs() << " nlocrel " << dyst.nlocrel;
8107 big_size = dyst.nlocrel;
8108 big_size *= sizeof(struct MachO::relocation_info);
8109 big_size += dyst.locreloff;
8110 if (big_size > object_size)
8111 outs() << " (past end of file)\n";
8112 else
8113 outs() << "\n";
8114}
8115
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008116static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc,
8117 uint32_t object_size) {
8118 if (dc.cmd == MachO::LC_DYLD_INFO)
8119 outs() << " cmd LC_DYLD_INFO\n";
8120 else
8121 outs() << " cmd LC_DYLD_INFO_ONLY\n";
8122 outs() << " cmdsize " << dc.cmdsize;
8123 if (dc.cmdsize != sizeof(struct MachO::dyld_info_command))
8124 outs() << " Incorrect size\n";
8125 else
8126 outs() << "\n";
8127 outs() << " rebase_off " << dc.rebase_off;
8128 if (dc.rebase_off > object_size)
8129 outs() << " (past end of file)\n";
8130 else
8131 outs() << "\n";
8132 outs() << " rebase_size " << dc.rebase_size;
8133 uint64_t big_size;
8134 big_size = dc.rebase_off;
8135 big_size += dc.rebase_size;
8136 if (big_size > object_size)
8137 outs() << " (past end of file)\n";
8138 else
8139 outs() << "\n";
8140 outs() << " bind_off " << dc.bind_off;
8141 if (dc.bind_off > object_size)
8142 outs() << " (past end of file)\n";
8143 else
8144 outs() << "\n";
8145 outs() << " bind_size " << dc.bind_size;
8146 big_size = dc.bind_off;
8147 big_size += dc.bind_size;
8148 if (big_size > object_size)
8149 outs() << " (past end of file)\n";
8150 else
8151 outs() << "\n";
8152 outs() << " weak_bind_off " << dc.weak_bind_off;
8153 if (dc.weak_bind_off > object_size)
8154 outs() << " (past end of file)\n";
8155 else
8156 outs() << "\n";
8157 outs() << " weak_bind_size " << dc.weak_bind_size;
8158 big_size = dc.weak_bind_off;
8159 big_size += dc.weak_bind_size;
8160 if (big_size > object_size)
8161 outs() << " (past end of file)\n";
8162 else
8163 outs() << "\n";
8164 outs() << " lazy_bind_off " << dc.lazy_bind_off;
8165 if (dc.lazy_bind_off > object_size)
8166 outs() << " (past end of file)\n";
8167 else
8168 outs() << "\n";
8169 outs() << " lazy_bind_size " << dc.lazy_bind_size;
8170 big_size = dc.lazy_bind_off;
8171 big_size += dc.lazy_bind_size;
8172 if (big_size > object_size)
8173 outs() << " (past end of file)\n";
8174 else
8175 outs() << "\n";
8176 outs() << " export_off " << dc.export_off;
8177 if (dc.export_off > object_size)
8178 outs() << " (past end of file)\n";
8179 else
8180 outs() << "\n";
8181 outs() << " export_size " << dc.export_size;
8182 big_size = dc.export_off;
8183 big_size += dc.export_size;
8184 if (big_size > object_size)
8185 outs() << " (past end of file)\n";
8186 else
8187 outs() << "\n";
8188}
8189
8190static void PrintDyldLoadCommand(MachO::dylinker_command dyld,
8191 const char *Ptr) {
8192 if (dyld.cmd == MachO::LC_ID_DYLINKER)
8193 outs() << " cmd LC_ID_DYLINKER\n";
8194 else if (dyld.cmd == MachO::LC_LOAD_DYLINKER)
8195 outs() << " cmd LC_LOAD_DYLINKER\n";
8196 else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT)
8197 outs() << " cmd LC_DYLD_ENVIRONMENT\n";
8198 else
8199 outs() << " cmd ?(" << dyld.cmd << ")\n";
8200 outs() << " cmdsize " << dyld.cmdsize;
8201 if (dyld.cmdsize < sizeof(struct MachO::dylinker_command))
8202 outs() << " Incorrect size\n";
8203 else
8204 outs() << "\n";
8205 if (dyld.name >= dyld.cmdsize)
8206 outs() << " name ?(bad offset " << dyld.name << ")\n";
8207 else {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008208 const char *P = (const char *)(Ptr) + dyld.name;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008209 outs() << " name " << P << " (offset " << dyld.name << ")\n";
8210 }
8211}
8212
8213static void PrintUuidLoadCommand(MachO::uuid_command uuid) {
8214 outs() << " cmd LC_UUID\n";
8215 outs() << " cmdsize " << uuid.cmdsize;
8216 if (uuid.cmdsize != sizeof(struct MachO::uuid_command))
8217 outs() << " Incorrect size\n";
8218 else
8219 outs() << "\n";
8220 outs() << " uuid ";
Davide Italianoc74277a2015-12-07 00:03:28 +00008221 for (int i = 0; i < 16; ++i) {
8222 outs() << format("%02" PRIX32, uuid.uuid[i]);
8223 if (i == 3 || i == 5 || i == 7 || i == 9)
8224 outs() << "-";
8225 }
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008226 outs() << "\n";
8227}
8228
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008229static void PrintRpathLoadCommand(MachO::rpath_command rpath, const char *Ptr) {
Jean-Daniel Dupas00cc1f52014-12-04 07:37:02 +00008230 outs() << " cmd LC_RPATH\n";
8231 outs() << " cmdsize " << rpath.cmdsize;
8232 if (rpath.cmdsize < sizeof(struct MachO::rpath_command))
8233 outs() << " Incorrect size\n";
8234 else
8235 outs() << "\n";
8236 if (rpath.path >= rpath.cmdsize)
8237 outs() << " path ?(bad offset " << rpath.path << ")\n";
8238 else {
8239 const char *P = (const char *)(Ptr) + rpath.path;
8240 outs() << " path " << P << " (offset " << rpath.path << ")\n";
8241 }
8242}
8243
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008244static void PrintVersionMinLoadCommand(MachO::version_min_command vd) {
Tim Northoverbfbfb122015-11-02 21:26:58 +00008245 StringRef LoadCmdName;
8246 switch (vd.cmd) {
8247 case MachO::LC_VERSION_MIN_MACOSX:
8248 LoadCmdName = "LC_VERSION_MIN_MACOSX";
8249 break;
8250 case MachO::LC_VERSION_MIN_IPHONEOS:
8251 LoadCmdName = "LC_VERSION_MIN_IPHONEOS";
8252 break;
8253 case MachO::LC_VERSION_MIN_TVOS:
8254 LoadCmdName = "LC_VERSION_MIN_TVOS";
8255 break;
8256 case MachO::LC_VERSION_MIN_WATCHOS:
8257 LoadCmdName = "LC_VERSION_MIN_WATCHOS";
8258 break;
8259 default:
8260 llvm_unreachable("Unknown version min load command");
8261 }
8262
8263 outs() << " cmd " << LoadCmdName << '\n';
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008264 outs() << " cmdsize " << vd.cmdsize;
8265 if (vd.cmdsize != sizeof(struct MachO::version_min_command))
8266 outs() << " Incorrect size\n";
8267 else
8268 outs() << "\n";
Davide Italiano56baef32015-08-26 12:26:11 +00008269 outs() << " version "
8270 << MachOObjectFile::getVersionMinMajor(vd, false) << "."
8271 << MachOObjectFile::getVersionMinMinor(vd, false);
8272 uint32_t Update = MachOObjectFile::getVersionMinUpdate(vd, false);
8273 if (Update != 0)
8274 outs() << "." << Update;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008275 outs() << "\n";
8276 if (vd.sdk == 0)
Kevin Enderby57538292014-12-17 01:01:30 +00008277 outs() << " sdk n/a";
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008278 else {
Davide Italiano56baef32015-08-26 12:26:11 +00008279 outs() << " sdk "
8280 << MachOObjectFile::getVersionMinMajor(vd, true) << "."
8281 << MachOObjectFile::getVersionMinMinor(vd, true);
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008282 }
Davide Italiano56baef32015-08-26 12:26:11 +00008283 Update = MachOObjectFile::getVersionMinUpdate(vd, true);
8284 if (Update != 0)
8285 outs() << "." << Update;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008286 outs() << "\n";
8287}
8288
Kevin Enderbya4579c42017-01-19 17:36:31 +00008289static void PrintNoteLoadCommand(MachO::note_command Nt) {
8290 outs() << " cmd LC_NOTE\n";
8291 outs() << " cmdsize " << Nt.cmdsize;
8292 if (Nt.cmdsize != sizeof(struct MachO::note_command))
8293 outs() << " Incorrect size\n";
8294 else
8295 outs() << "\n";
8296 const char *d = Nt.data_owner;
8297 outs() << "data_owner " << format("%.16s\n", d);
8298 outs() << " offset " << Nt.offset << "\n";
8299 outs() << " size " << Nt.size << "\n";
8300}
8301
Steven Wu5b54a422017-01-23 20:07:55 +00008302static void PrintBuildToolVersion(MachO::build_tool_version bv) {
8303 outs() << " tool " << MachOObjectFile::getBuildTool(bv.tool) << "\n";
8304 outs() << " version " << MachOObjectFile::getVersionString(bv.version)
8305 << "\n";
8306}
8307
8308static void PrintBuildVersionLoadCommand(const MachOObjectFile *obj,
8309 MachO::build_version_command bd) {
8310 outs() << " cmd LC_BUILD_VERSION\n";
8311 outs() << " cmdsize " << bd.cmdsize;
8312 if (bd.cmdsize !=
8313 sizeof(struct MachO::build_version_command) +
8314 bd.ntools * sizeof(struct MachO::build_tool_version))
8315 outs() << " Incorrect size\n";
8316 else
8317 outs() << "\n";
8318 outs() << " platform " << MachOObjectFile::getBuildPlatform(bd.platform)
8319 << "\n";
8320 if (bd.sdk)
8321 outs() << " sdk " << MachOObjectFile::getVersionString(bd.sdk)
8322 << "\n";
8323 else
8324 outs() << " sdk n/a\n";
8325 outs() << " minos " << MachOObjectFile::getVersionString(bd.minos)
8326 << "\n";
8327 outs() << " ntools " << bd.ntools << "\n";
8328 for (unsigned i = 0; i < bd.ntools; ++i) {
8329 MachO::build_tool_version bv = obj->getBuildToolVersion(i);
8330 PrintBuildToolVersion(bv);
8331 }
8332}
8333
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008334static void PrintSourceVersionCommand(MachO::source_version_command sd) {
8335 outs() << " cmd LC_SOURCE_VERSION\n";
8336 outs() << " cmdsize " << sd.cmdsize;
8337 if (sd.cmdsize != sizeof(struct MachO::source_version_command))
8338 outs() << " Incorrect size\n";
8339 else
8340 outs() << "\n";
8341 uint64_t a = (sd.version >> 40) & 0xffffff;
8342 uint64_t b = (sd.version >> 30) & 0x3ff;
8343 uint64_t c = (sd.version >> 20) & 0x3ff;
8344 uint64_t d = (sd.version >> 10) & 0x3ff;
8345 uint64_t e = sd.version & 0x3ff;
8346 outs() << " version " << a << "." << b;
8347 if (e != 0)
8348 outs() << "." << c << "." << d << "." << e;
8349 else if (d != 0)
8350 outs() << "." << c << "." << d;
8351 else if (c != 0)
8352 outs() << "." << c;
8353 outs() << "\n";
8354}
8355
8356static void PrintEntryPointCommand(MachO::entry_point_command ep) {
8357 outs() << " cmd LC_MAIN\n";
8358 outs() << " cmdsize " << ep.cmdsize;
8359 if (ep.cmdsize != sizeof(struct MachO::entry_point_command))
8360 outs() << " Incorrect size\n";
8361 else
8362 outs() << "\n";
8363 outs() << " entryoff " << ep.entryoff << "\n";
8364 outs() << " stacksize " << ep.stacksize << "\n";
8365}
8366
Kevin Enderby0804f4672014-12-16 23:25:52 +00008367static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec,
8368 uint32_t object_size) {
8369 outs() << " cmd LC_ENCRYPTION_INFO\n";
8370 outs() << " cmdsize " << ec.cmdsize;
8371 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command))
8372 outs() << " Incorrect size\n";
8373 else
8374 outs() << "\n";
8375 outs() << " cryptoff " << ec.cryptoff;
8376 if (ec.cryptoff > object_size)
8377 outs() << " (past end of file)\n";
8378 else
8379 outs() << "\n";
8380 outs() << " cryptsize " << ec.cryptsize;
8381 if (ec.cryptsize > object_size)
8382 outs() << " (past end of file)\n";
8383 else
8384 outs() << "\n";
8385 outs() << " cryptid " << ec.cryptid << "\n";
8386}
8387
Kevin Enderby57538292014-12-17 01:01:30 +00008388static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec,
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008389 uint32_t object_size) {
Kevin Enderby57538292014-12-17 01:01:30 +00008390 outs() << " cmd LC_ENCRYPTION_INFO_64\n";
8391 outs() << " cmdsize " << ec.cmdsize;
8392 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64))
8393 outs() << " Incorrect size\n";
8394 else
8395 outs() << "\n";
8396 outs() << " cryptoff " << ec.cryptoff;
8397 if (ec.cryptoff > object_size)
8398 outs() << " (past end of file)\n";
8399 else
8400 outs() << "\n";
8401 outs() << " cryptsize " << ec.cryptsize;
8402 if (ec.cryptsize > object_size)
8403 outs() << " (past end of file)\n";
8404 else
8405 outs() << "\n";
8406 outs() << " cryptid " << ec.cryptid << "\n";
8407 outs() << " pad " << ec.pad << "\n";
8408}
8409
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00008410static void PrintLinkerOptionCommand(MachO::linker_option_command lo,
8411 const char *Ptr) {
8412 outs() << " cmd LC_LINKER_OPTION\n";
8413 outs() << " cmdsize " << lo.cmdsize;
8414 if (lo.cmdsize < sizeof(struct MachO::linker_option_command))
8415 outs() << " Incorrect size\n";
8416 else
8417 outs() << "\n";
8418 outs() << " count " << lo.count << "\n";
8419 const char *string = Ptr + sizeof(struct MachO::linker_option_command);
8420 uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command);
8421 uint32_t i = 0;
8422 while (left > 0) {
8423 while (*string == '\0' && left > 0) {
8424 string++;
8425 left--;
8426 }
8427 if (left > 0) {
8428 i++;
8429 outs() << " string #" << i << " " << format("%.*s\n", left, string);
David Majnemerd4449ed2014-12-20 08:24:43 +00008430 uint32_t NullPos = StringRef(string, left).find('\0');
8431 uint32_t len = std::min(NullPos, left) + 1;
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00008432 string += len;
8433 left -= len;
8434 }
8435 }
8436 if (lo.count != i)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008437 outs() << " count " << lo.count << " does not match number of strings "
8438 << i << "\n";
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00008439}
8440
Kevin Enderbyb4b79312014-12-18 19:24:35 +00008441static void PrintSubFrameworkCommand(MachO::sub_framework_command sub,
8442 const char *Ptr) {
8443 outs() << " cmd LC_SUB_FRAMEWORK\n";
8444 outs() << " cmdsize " << sub.cmdsize;
8445 if (sub.cmdsize < sizeof(struct MachO::sub_framework_command))
8446 outs() << " Incorrect size\n";
8447 else
8448 outs() << "\n";
8449 if (sub.umbrella < sub.cmdsize) {
8450 const char *P = Ptr + sub.umbrella;
8451 outs() << " umbrella " << P << " (offset " << sub.umbrella << ")\n";
8452 } else {
8453 outs() << " umbrella ?(bad offset " << sub.umbrella << ")\n";
8454 }
8455}
8456
Kevin Enderbya2bd8d92014-12-18 23:13:26 +00008457static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub,
8458 const char *Ptr) {
8459 outs() << " cmd LC_SUB_UMBRELLA\n";
8460 outs() << " cmdsize " << sub.cmdsize;
8461 if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command))
8462 outs() << " Incorrect size\n";
8463 else
8464 outs() << "\n";
8465 if (sub.sub_umbrella < sub.cmdsize) {
8466 const char *P = Ptr + sub.sub_umbrella;
8467 outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n";
8468 } else {
8469 outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n";
8470 }
8471}
8472
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00008473static void PrintSubLibraryCommand(MachO::sub_library_command sub,
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008474 const char *Ptr) {
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00008475 outs() << " cmd LC_SUB_LIBRARY\n";
8476 outs() << " cmdsize " << sub.cmdsize;
8477 if (sub.cmdsize < sizeof(struct MachO::sub_library_command))
8478 outs() << " Incorrect size\n";
8479 else
8480 outs() << "\n";
8481 if (sub.sub_library < sub.cmdsize) {
8482 const char *P = Ptr + sub.sub_library;
8483 outs() << " sub_library " << P << " (offset " << sub.sub_library << ")\n";
8484 } else {
8485 outs() << " sub_library ?(bad offset " << sub.sub_library << ")\n";
8486 }
8487}
8488
Kevin Enderby186eac32014-12-19 21:06:24 +00008489static void PrintSubClientCommand(MachO::sub_client_command sub,
8490 const char *Ptr) {
8491 outs() << " cmd LC_SUB_CLIENT\n";
8492 outs() << " cmdsize " << sub.cmdsize;
8493 if (sub.cmdsize < sizeof(struct MachO::sub_client_command))
8494 outs() << " Incorrect size\n";
8495 else
8496 outs() << "\n";
8497 if (sub.client < sub.cmdsize) {
8498 const char *P = Ptr + sub.client;
8499 outs() << " client " << P << " (offset " << sub.client << ")\n";
8500 } else {
8501 outs() << " client ?(bad offset " << sub.client << ")\n";
8502 }
8503}
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00008504
Kevin Enderby52e4ce42014-12-19 22:25:22 +00008505static void PrintRoutinesCommand(MachO::routines_command r) {
8506 outs() << " cmd LC_ROUTINES\n";
8507 outs() << " cmdsize " << r.cmdsize;
8508 if (r.cmdsize != sizeof(struct MachO::routines_command))
8509 outs() << " Incorrect size\n";
8510 else
8511 outs() << "\n";
8512 outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n";
8513 outs() << " init_module " << r.init_module << "\n";
8514 outs() << " reserved1 " << r.reserved1 << "\n";
8515 outs() << " reserved2 " << r.reserved2 << "\n";
8516 outs() << " reserved3 " << r.reserved3 << "\n";
8517 outs() << " reserved4 " << r.reserved4 << "\n";
8518 outs() << " reserved5 " << r.reserved5 << "\n";
8519 outs() << " reserved6 " << r.reserved6 << "\n";
8520}
8521
8522static void PrintRoutinesCommand64(MachO::routines_command_64 r) {
8523 outs() << " cmd LC_ROUTINES_64\n";
8524 outs() << " cmdsize " << r.cmdsize;
8525 if (r.cmdsize != sizeof(struct MachO::routines_command_64))
8526 outs() << " Incorrect size\n";
8527 else
8528 outs() << "\n";
8529 outs() << " init_address " << format("0x%016" PRIx64, r.init_address) << "\n";
8530 outs() << " init_module " << r.init_module << "\n";
8531 outs() << " reserved1 " << r.reserved1 << "\n";
8532 outs() << " reserved2 " << r.reserved2 << "\n";
8533 outs() << " reserved3 " << r.reserved3 << "\n";
8534 outs() << " reserved4 " << r.reserved4 << "\n";
8535 outs() << " reserved5 " << r.reserved5 << "\n";
8536 outs() << " reserved6 " << r.reserved6 << "\n";
8537}
8538
Kevin Enderbyc3a035d2017-01-23 21:13:29 +00008539static void Print_x86_thread_state32_t(MachO::x86_thread_state32_t &cpu32) {
8540 outs() << "\t eax " << format("0x%08" PRIx32, cpu32.eax);
8541 outs() << " ebx " << format("0x%08" PRIx32, cpu32.ebx);
8542 outs() << " ecx " << format("0x%08" PRIx32, cpu32.ecx);
8543 outs() << " edx " << format("0x%08" PRIx32, cpu32.edx) << "\n";
8544 outs() << "\t edi " << format("0x%08" PRIx32, cpu32.edi);
8545 outs() << " esi " << format("0x%08" PRIx32, cpu32.esi);
8546 outs() << " ebp " << format("0x%08" PRIx32, cpu32.ebp);
8547 outs() << " esp " << format("0x%08" PRIx32, cpu32.esp) << "\n";
8548 outs() << "\t ss " << format("0x%08" PRIx32, cpu32.ss);
8549 outs() << " eflags " << format("0x%08" PRIx32, cpu32.eflags);
8550 outs() << " eip " << format("0x%08" PRIx32, cpu32.eip);
8551 outs() << " cs " << format("0x%08" PRIx32, cpu32.cs) << "\n";
8552 outs() << "\t ds " << format("0x%08" PRIx32, cpu32.ds);
8553 outs() << " es " << format("0x%08" PRIx32, cpu32.es);
8554 outs() << " fs " << format("0x%08" PRIx32, cpu32.fs);
8555 outs() << " gs " << format("0x%08" PRIx32, cpu32.gs) << "\n";
8556}
8557
Kevin Enderby48ef5342014-12-23 22:56:39 +00008558static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) {
8559 outs() << " rax " << format("0x%016" PRIx64, cpu64.rax);
8560 outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx);
8561 outs() << " rcx " << format("0x%016" PRIx64, cpu64.rcx) << "\n";
8562 outs() << " rdx " << format("0x%016" PRIx64, cpu64.rdx);
8563 outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi);
8564 outs() << " rsi " << format("0x%016" PRIx64, cpu64.rsi) << "\n";
8565 outs() << " rbp " << format("0x%016" PRIx64, cpu64.rbp);
8566 outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp);
8567 outs() << " r8 " << format("0x%016" PRIx64, cpu64.r8) << "\n";
8568 outs() << " r9 " << format("0x%016" PRIx64, cpu64.r9);
8569 outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10);
8570 outs() << " r11 " << format("0x%016" PRIx64, cpu64.r11) << "\n";
8571 outs() << " r12 " << format("0x%016" PRIx64, cpu64.r12);
8572 outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13);
8573 outs() << " r14 " << format("0x%016" PRIx64, cpu64.r14) << "\n";
8574 outs() << " r15 " << format("0x%016" PRIx64, cpu64.r15);
8575 outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n";
8576 outs() << "rflags " << format("0x%016" PRIx64, cpu64.rflags);
8577 outs() << " cs " << format("0x%016" PRIx64, cpu64.cs);
8578 outs() << " fs " << format("0x%016" PRIx64, cpu64.fs) << "\n";
8579 outs() << " gs " << format("0x%016" PRIx64, cpu64.gs) << "\n";
8580}
8581
Kevin Enderby227df342014-12-23 23:43:59 +00008582static void Print_mmst_reg(MachO::mmst_reg_t &r) {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008583 uint32_t f;
8584 outs() << "\t mmst_reg ";
8585 for (f = 0; f < 10; f++)
8586 outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " ";
8587 outs() << "\n";
8588 outs() << "\t mmst_rsrv ";
8589 for (f = 0; f < 6; f++)
8590 outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " ";
8591 outs() << "\n";
8592}
8593
Kevin Enderbyaefb0032014-12-24 00:16:51 +00008594static void Print_xmm_reg(MachO::xmm_reg_t &r) {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008595 uint32_t f;
8596 outs() << "\t xmm_reg ";
8597 for (f = 0; f < 16; f++)
8598 outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " ";
8599 outs() << "\n";
8600}
8601
8602static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) {
8603 outs() << "\t fpu_reserved[0] " << fpu.fpu_reserved[0];
8604 outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n";
8605 outs() << "\t control: invalid " << fpu.fpu_fcw.invalid;
8606 outs() << " denorm " << fpu.fpu_fcw.denorm;
8607 outs() << " zdiv " << fpu.fpu_fcw.zdiv;
8608 outs() << " ovrfl " << fpu.fpu_fcw.ovrfl;
8609 outs() << " undfl " << fpu.fpu_fcw.undfl;
8610 outs() << " precis " << fpu.fpu_fcw.precis << "\n";
8611 outs() << "\t\t pc ";
8612 if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B)
8613 outs() << "FP_PREC_24B ";
8614 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B)
8615 outs() << "FP_PREC_53B ";
8616 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B)
8617 outs() << "FP_PREC_64B ";
8618 else
8619 outs() << fpu.fpu_fcw.pc << " ";
8620 outs() << "rc ";
8621 if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR)
8622 outs() << "FP_RND_NEAR ";
8623 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN)
8624 outs() << "FP_RND_DOWN ";
8625 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP)
8626 outs() << "FP_RND_UP ";
8627 else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008628 outs() << "FP_CHOP ";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008629 outs() << "\n";
8630 outs() << "\t status: invalid " << fpu.fpu_fsw.invalid;
8631 outs() << " denorm " << fpu.fpu_fsw.denorm;
8632 outs() << " zdiv " << fpu.fpu_fsw.zdiv;
8633 outs() << " ovrfl " << fpu.fpu_fsw.ovrfl;
8634 outs() << " undfl " << fpu.fpu_fsw.undfl;
8635 outs() << " precis " << fpu.fpu_fsw.precis;
8636 outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n";
8637 outs() << "\t errsumm " << fpu.fpu_fsw.errsumm;
8638 outs() << " c0 " << fpu.fpu_fsw.c0;
8639 outs() << " c1 " << fpu.fpu_fsw.c1;
8640 outs() << " c2 " << fpu.fpu_fsw.c2;
8641 outs() << " tos " << fpu.fpu_fsw.tos;
8642 outs() << " c3 " << fpu.fpu_fsw.c3;
8643 outs() << " busy " << fpu.fpu_fsw.busy << "\n";
8644 outs() << "\t fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw);
8645 outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1);
8646 outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop);
8647 outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n";
8648 outs() << "\t fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs);
8649 outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2);
8650 outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp);
8651 outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n";
8652 outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3);
8653 outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr);
8654 outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask);
8655 outs() << "\n";
8656 outs() << "\t fpu_stmm0:\n";
8657 Print_mmst_reg(fpu.fpu_stmm0);
8658 outs() << "\t fpu_stmm1:\n";
8659 Print_mmst_reg(fpu.fpu_stmm1);
8660 outs() << "\t fpu_stmm2:\n";
8661 Print_mmst_reg(fpu.fpu_stmm2);
8662 outs() << "\t fpu_stmm3:\n";
8663 Print_mmst_reg(fpu.fpu_stmm3);
8664 outs() << "\t fpu_stmm4:\n";
8665 Print_mmst_reg(fpu.fpu_stmm4);
8666 outs() << "\t fpu_stmm5:\n";
8667 Print_mmst_reg(fpu.fpu_stmm5);
8668 outs() << "\t fpu_stmm6:\n";
8669 Print_mmst_reg(fpu.fpu_stmm6);
8670 outs() << "\t fpu_stmm7:\n";
8671 Print_mmst_reg(fpu.fpu_stmm7);
8672 outs() << "\t fpu_xmm0:\n";
8673 Print_xmm_reg(fpu.fpu_xmm0);
8674 outs() << "\t fpu_xmm1:\n";
8675 Print_xmm_reg(fpu.fpu_xmm1);
8676 outs() << "\t fpu_xmm2:\n";
8677 Print_xmm_reg(fpu.fpu_xmm2);
8678 outs() << "\t fpu_xmm3:\n";
8679 Print_xmm_reg(fpu.fpu_xmm3);
8680 outs() << "\t fpu_xmm4:\n";
8681 Print_xmm_reg(fpu.fpu_xmm4);
8682 outs() << "\t fpu_xmm5:\n";
8683 Print_xmm_reg(fpu.fpu_xmm5);
8684 outs() << "\t fpu_xmm6:\n";
8685 Print_xmm_reg(fpu.fpu_xmm6);
8686 outs() << "\t fpu_xmm7:\n";
8687 Print_xmm_reg(fpu.fpu_xmm7);
8688 outs() << "\t fpu_xmm8:\n";
8689 Print_xmm_reg(fpu.fpu_xmm8);
8690 outs() << "\t fpu_xmm9:\n";
8691 Print_xmm_reg(fpu.fpu_xmm9);
8692 outs() << "\t fpu_xmm10:\n";
8693 Print_xmm_reg(fpu.fpu_xmm10);
8694 outs() << "\t fpu_xmm11:\n";
8695 Print_xmm_reg(fpu.fpu_xmm11);
8696 outs() << "\t fpu_xmm12:\n";
8697 Print_xmm_reg(fpu.fpu_xmm12);
8698 outs() << "\t fpu_xmm13:\n";
8699 Print_xmm_reg(fpu.fpu_xmm13);
8700 outs() << "\t fpu_xmm14:\n";
8701 Print_xmm_reg(fpu.fpu_xmm14);
8702 outs() << "\t fpu_xmm15:\n";
8703 Print_xmm_reg(fpu.fpu_xmm15);
8704 outs() << "\t fpu_rsrv4:\n";
8705 for (uint32_t f = 0; f < 6; f++) {
8706 outs() << "\t ";
8707 for (uint32_t g = 0; g < 16; g++)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008708 outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f * g]) << " ";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008709 outs() << "\n";
8710 }
8711 outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1);
8712 outs() << "\n";
8713}
8714
8715static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) {
8716 outs() << "\t trapno " << format("0x%08" PRIx32, exc64.trapno);
8717 outs() << " err " << format("0x%08" PRIx32, exc64.err);
8718 outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n";
8719}
8720
Kevin Enderby41c9c002016-10-21 18:22:35 +00008721static void Print_arm_thread_state32_t(MachO::arm_thread_state32_t &cpu32) {
8722 outs() << "\t r0 " << format("0x%08" PRIx32, cpu32.r[0]);
8723 outs() << " r1 " << format("0x%08" PRIx32, cpu32.r[1]);
8724 outs() << " r2 " << format("0x%08" PRIx32, cpu32.r[2]);
8725 outs() << " r3 " << format("0x%08" PRIx32, cpu32.r[3]) << "\n";
8726 outs() << "\t r4 " << format("0x%08" PRIx32, cpu32.r[4]);
8727 outs() << " r5 " << format("0x%08" PRIx32, cpu32.r[5]);
8728 outs() << " r6 " << format("0x%08" PRIx32, cpu32.r[6]);
8729 outs() << " r7 " << format("0x%08" PRIx32, cpu32.r[7]) << "\n";
8730 outs() << "\t r8 " << format("0x%08" PRIx32, cpu32.r[8]);
8731 outs() << " r9 " << format("0x%08" PRIx32, cpu32.r[9]);
8732 outs() << " r10 " << format("0x%08" PRIx32, cpu32.r[10]);
8733 outs() << " r11 " << format("0x%08" PRIx32, cpu32.r[11]) << "\n";
8734 outs() << "\t r12 " << format("0x%08" PRIx32, cpu32.r[12]);
8735 outs() << " sp " << format("0x%08" PRIx32, cpu32.sp);
8736 outs() << " lr " << format("0x%08" PRIx32, cpu32.lr);
8737 outs() << " pc " << format("0x%08" PRIx32, cpu32.pc) << "\n";
8738 outs() << "\t cpsr " << format("0x%08" PRIx32, cpu32.cpsr) << "\n";
8739}
8740
Kevin Enderby7747cb52016-11-03 20:51:28 +00008741static void Print_arm_thread_state64_t(MachO::arm_thread_state64_t &cpu64) {
8742 outs() << "\t x0 " << format("0x%016" PRIx64, cpu64.x[0]);
8743 outs() << " x1 " << format("0x%016" PRIx64, cpu64.x[1]);
8744 outs() << " x2 " << format("0x%016" PRIx64, cpu64.x[2]) << "\n";
8745 outs() << "\t x3 " << format("0x%016" PRIx64, cpu64.x[3]);
8746 outs() << " x4 " << format("0x%016" PRIx64, cpu64.x[4]);
8747 outs() << " x5 " << format("0x%016" PRIx64, cpu64.x[5]) << "\n";
8748 outs() << "\t x6 " << format("0x%016" PRIx64, cpu64.x[6]);
8749 outs() << " x7 " << format("0x%016" PRIx64, cpu64.x[7]);
8750 outs() << " x8 " << format("0x%016" PRIx64, cpu64.x[8]) << "\n";
8751 outs() << "\t x9 " << format("0x%016" PRIx64, cpu64.x[9]);
8752 outs() << " x10 " << format("0x%016" PRIx64, cpu64.x[10]);
8753 outs() << " x11 " << format("0x%016" PRIx64, cpu64.x[11]) << "\n";
8754 outs() << "\t x12 " << format("0x%016" PRIx64, cpu64.x[12]);
8755 outs() << " x13 " << format("0x%016" PRIx64, cpu64.x[13]);
8756 outs() << " x14 " << format("0x%016" PRIx64, cpu64.x[14]) << "\n";
8757 outs() << "\t x15 " << format("0x%016" PRIx64, cpu64.x[15]);
8758 outs() << " x16 " << format("0x%016" PRIx64, cpu64.x[16]);
8759 outs() << " x17 " << format("0x%016" PRIx64, cpu64.x[17]) << "\n";
8760 outs() << "\t x18 " << format("0x%016" PRIx64, cpu64.x[18]);
8761 outs() << " x19 " << format("0x%016" PRIx64, cpu64.x[19]);
8762 outs() << " x20 " << format("0x%016" PRIx64, cpu64.x[20]) << "\n";
8763 outs() << "\t x21 " << format("0x%016" PRIx64, cpu64.x[21]);
8764 outs() << " x22 " << format("0x%016" PRIx64, cpu64.x[22]);
8765 outs() << " x23 " << format("0x%016" PRIx64, cpu64.x[23]) << "\n";
8766 outs() << "\t x24 " << format("0x%016" PRIx64, cpu64.x[24]);
8767 outs() << " x25 " << format("0x%016" PRIx64, cpu64.x[25]);
8768 outs() << " x26 " << format("0x%016" PRIx64, cpu64.x[26]) << "\n";
8769 outs() << "\t x27 " << format("0x%016" PRIx64, cpu64.x[27]);
8770 outs() << " x28 " << format("0x%016" PRIx64, cpu64.x[28]);
8771 outs() << " fp " << format("0x%016" PRIx64, cpu64.fp) << "\n";
8772 outs() << "\t lr " << format("0x%016" PRIx64, cpu64.lr);
8773 outs() << " sp " << format("0x%016" PRIx64, cpu64.sp);
8774 outs() << " pc " << format("0x%016" PRIx64, cpu64.pc) << "\n";
8775 outs() << "\t cpsr " << format("0x%08" PRIx32, cpu64.cpsr) << "\n";
8776}
8777
Kevin Enderby48ef5342014-12-23 22:56:39 +00008778static void PrintThreadCommand(MachO::thread_command t, const char *Ptr,
8779 bool isLittleEndian, uint32_t cputype) {
8780 if (t.cmd == MachO::LC_THREAD)
8781 outs() << " cmd LC_THREAD\n";
8782 else if (t.cmd == MachO::LC_UNIXTHREAD)
8783 outs() << " cmd LC_UNIXTHREAD\n";
8784 else
8785 outs() << " cmd " << t.cmd << " (unknown)\n";
8786 outs() << " cmdsize " << t.cmdsize;
8787 if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t))
8788 outs() << " Incorrect size\n";
8789 else
8790 outs() << "\n";
8791
8792 const char *begin = Ptr + sizeof(struct MachO::thread_command);
8793 const char *end = Ptr + t.cmdsize;
8794 uint32_t flavor, count, left;
Kevin Enderbyc3a035d2017-01-23 21:13:29 +00008795 if (cputype == MachO::CPU_TYPE_I386) {
8796 while (begin < end) {
8797 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8798 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8799 begin += sizeof(uint32_t);
8800 } else {
8801 flavor = 0;
8802 begin = end;
8803 }
8804 if (isLittleEndian != sys::IsLittleEndianHost)
8805 sys::swapByteOrder(flavor);
8806 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8807 memcpy((char *)&count, begin, sizeof(uint32_t));
8808 begin += sizeof(uint32_t);
8809 } else {
8810 count = 0;
8811 begin = end;
8812 }
8813 if (isLittleEndian != sys::IsLittleEndianHost)
8814 sys::swapByteOrder(count);
8815 if (flavor == MachO::x86_THREAD_STATE32) {
8816 outs() << " flavor i386_THREAD_STATE\n";
8817 if (count == MachO::x86_THREAD_STATE32_COUNT)
8818 outs() << " count i386_THREAD_STATE_COUNT\n";
8819 else
8820 outs() << " count " << count
8821 << " (not x86_THREAD_STATE32_COUNT)\n";
8822 MachO::x86_thread_state32_t cpu32;
8823 left = end - begin;
8824 if (left >= sizeof(MachO::x86_thread_state32_t)) {
8825 memcpy(&cpu32, begin, sizeof(MachO::x86_thread_state32_t));
8826 begin += sizeof(MachO::x86_thread_state32_t);
8827 } else {
8828 memset(&cpu32, '\0', sizeof(MachO::x86_thread_state32_t));
8829 memcpy(&cpu32, begin, left);
8830 begin += left;
8831 }
8832 if (isLittleEndian != sys::IsLittleEndianHost)
8833 swapStruct(cpu32);
8834 Print_x86_thread_state32_t(cpu32);
8835 } else if (flavor == MachO::x86_THREAD_STATE) {
8836 outs() << " flavor x86_THREAD_STATE\n";
8837 if (count == MachO::x86_THREAD_STATE_COUNT)
8838 outs() << " count x86_THREAD_STATE_COUNT\n";
8839 else
8840 outs() << " count " << count
8841 << " (not x86_THREAD_STATE_COUNT)\n";
8842 struct MachO::x86_thread_state_t ts;
8843 left = end - begin;
8844 if (left >= sizeof(MachO::x86_thread_state_t)) {
8845 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
8846 begin += sizeof(MachO::x86_thread_state_t);
8847 } else {
8848 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
8849 memcpy(&ts, begin, left);
8850 begin += left;
8851 }
8852 if (isLittleEndian != sys::IsLittleEndianHost)
8853 swapStruct(ts);
8854 if (ts.tsh.flavor == MachO::x86_THREAD_STATE32) {
8855 outs() << "\t tsh.flavor x86_THREAD_STATE32 ";
8856 if (ts.tsh.count == MachO::x86_THREAD_STATE32_COUNT)
8857 outs() << "tsh.count x86_THREAD_STATE32_COUNT\n";
8858 else
8859 outs() << "tsh.count " << ts.tsh.count
8860 << " (not x86_THREAD_STATE32_COUNT\n";
8861 Print_x86_thread_state32_t(ts.uts.ts32);
8862 } else {
8863 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
8864 << ts.tsh.count << "\n";
8865 }
8866 } else {
8867 outs() << " flavor " << flavor << " (unknown)\n";
8868 outs() << " count " << count << "\n";
8869 outs() << " state (unknown)\n";
8870 begin += count * sizeof(uint32_t);
8871 }
8872 }
8873 } else if (cputype == MachO::CPU_TYPE_X86_64) {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008874 while (begin < end) {
8875 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8876 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8877 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008878 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008879 flavor = 0;
8880 begin = end;
8881 }
8882 if (isLittleEndian != sys::IsLittleEndianHost)
8883 sys::swapByteOrder(flavor);
8884 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8885 memcpy((char *)&count, begin, sizeof(uint32_t));
8886 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008887 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008888 count = 0;
8889 begin = end;
8890 }
8891 if (isLittleEndian != sys::IsLittleEndianHost)
8892 sys::swapByteOrder(count);
8893 if (flavor == MachO::x86_THREAD_STATE64) {
8894 outs() << " flavor x86_THREAD_STATE64\n";
8895 if (count == MachO::x86_THREAD_STATE64_COUNT)
8896 outs() << " count x86_THREAD_STATE64_COUNT\n";
8897 else
8898 outs() << " count " << count
8899 << " (not x86_THREAD_STATE64_COUNT)\n";
8900 MachO::x86_thread_state64_t cpu64;
8901 left = end - begin;
8902 if (left >= sizeof(MachO::x86_thread_state64_t)) {
8903 memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t));
8904 begin += sizeof(MachO::x86_thread_state64_t);
8905 } else {
8906 memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t));
8907 memcpy(&cpu64, begin, left);
8908 begin += left;
8909 }
8910 if (isLittleEndian != sys::IsLittleEndianHost)
8911 swapStruct(cpu64);
8912 Print_x86_thread_state64_t(cpu64);
8913 } else if (flavor == MachO::x86_THREAD_STATE) {
8914 outs() << " flavor x86_THREAD_STATE\n";
8915 if (count == MachO::x86_THREAD_STATE_COUNT)
8916 outs() << " count x86_THREAD_STATE_COUNT\n";
8917 else
8918 outs() << " count " << count
8919 << " (not x86_THREAD_STATE_COUNT)\n";
8920 struct MachO::x86_thread_state_t ts;
8921 left = end - begin;
8922 if (left >= sizeof(MachO::x86_thread_state_t)) {
8923 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
8924 begin += sizeof(MachO::x86_thread_state_t);
8925 } else {
8926 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
8927 memcpy(&ts, begin, left);
8928 begin += left;
8929 }
8930 if (isLittleEndian != sys::IsLittleEndianHost)
8931 swapStruct(ts);
8932 if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) {
8933 outs() << "\t tsh.flavor x86_THREAD_STATE64 ";
8934 if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT)
8935 outs() << "tsh.count x86_THREAD_STATE64_COUNT\n";
8936 else
8937 outs() << "tsh.count " << ts.tsh.count
8938 << " (not x86_THREAD_STATE64_COUNT\n";
8939 Print_x86_thread_state64_t(ts.uts.ts64);
8940 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008941 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
8942 << ts.tsh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008943 }
8944 } else if (flavor == MachO::x86_FLOAT_STATE) {
8945 outs() << " flavor x86_FLOAT_STATE\n";
8946 if (count == MachO::x86_FLOAT_STATE_COUNT)
8947 outs() << " count x86_FLOAT_STATE_COUNT\n";
8948 else
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008949 outs() << " count " << count << " (not x86_FLOAT_STATE_COUNT)\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008950 struct MachO::x86_float_state_t fs;
8951 left = end - begin;
8952 if (left >= sizeof(MachO::x86_float_state_t)) {
8953 memcpy(&fs, begin, sizeof(MachO::x86_float_state_t));
8954 begin += sizeof(MachO::x86_float_state_t);
8955 } else {
8956 memset(&fs, '\0', sizeof(MachO::x86_float_state_t));
8957 memcpy(&fs, begin, left);
8958 begin += left;
8959 }
8960 if (isLittleEndian != sys::IsLittleEndianHost)
8961 swapStruct(fs);
8962 if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) {
8963 outs() << "\t fsh.flavor x86_FLOAT_STATE64 ";
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008964 if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT)
Kevin Enderby48ef5342014-12-23 22:56:39 +00008965 outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n";
8966 else
8967 outs() << "fsh.count " << fs.fsh.count
8968 << " (not x86_FLOAT_STATE64_COUNT\n";
8969 Print_x86_float_state_t(fs.ufs.fs64);
8970 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008971 outs() << "\t fsh.flavor " << fs.fsh.flavor << " fsh.count "
8972 << fs.fsh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008973 }
8974 } else if (flavor == MachO::x86_EXCEPTION_STATE) {
8975 outs() << " flavor x86_EXCEPTION_STATE\n";
8976 if (count == MachO::x86_EXCEPTION_STATE_COUNT)
8977 outs() << " count x86_EXCEPTION_STATE_COUNT\n";
8978 else
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008979 outs() << " count " << count
Kevin Enderby48ef5342014-12-23 22:56:39 +00008980 << " (not x86_EXCEPTION_STATE_COUNT)\n";
8981 struct MachO::x86_exception_state_t es;
8982 left = end - begin;
8983 if (left >= sizeof(MachO::x86_exception_state_t)) {
8984 memcpy(&es, begin, sizeof(MachO::x86_exception_state_t));
8985 begin += sizeof(MachO::x86_exception_state_t);
8986 } else {
8987 memset(&es, '\0', sizeof(MachO::x86_exception_state_t));
8988 memcpy(&es, begin, left);
8989 begin += left;
8990 }
8991 if (isLittleEndian != sys::IsLittleEndianHost)
8992 swapStruct(es);
8993 if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) {
8994 outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n";
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008995 if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT)
Kevin Enderby48ef5342014-12-23 22:56:39 +00008996 outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n";
8997 else
8998 outs() << "\t esh.count " << es.esh.count
8999 << " (not x86_EXCEPTION_STATE64_COUNT\n";
9000 Print_x86_exception_state_t(es.ues.es64);
9001 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00009002 outs() << "\t esh.flavor " << es.esh.flavor << " esh.count "
9003 << es.esh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00009004 }
9005 } else {
9006 outs() << " flavor " << flavor << " (unknown)\n";
9007 outs() << " count " << count << "\n";
9008 outs() << " state (unknown)\n";
9009 begin += count * sizeof(uint32_t);
9010 }
9011 }
Kevin Enderby41c9c002016-10-21 18:22:35 +00009012 } else if (cputype == MachO::CPU_TYPE_ARM) {
9013 while (begin < end) {
9014 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9015 memcpy((char *)&flavor, begin, sizeof(uint32_t));
9016 begin += sizeof(uint32_t);
9017 } else {
9018 flavor = 0;
9019 begin = end;
9020 }
9021 if (isLittleEndian != sys::IsLittleEndianHost)
9022 sys::swapByteOrder(flavor);
9023 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9024 memcpy((char *)&count, begin, sizeof(uint32_t));
9025 begin += sizeof(uint32_t);
9026 } else {
9027 count = 0;
9028 begin = end;
9029 }
9030 if (isLittleEndian != sys::IsLittleEndianHost)
9031 sys::swapByteOrder(count);
9032 if (flavor == MachO::ARM_THREAD_STATE) {
9033 outs() << " flavor ARM_THREAD_STATE\n";
9034 if (count == MachO::ARM_THREAD_STATE_COUNT)
9035 outs() << " count ARM_THREAD_STATE_COUNT\n";
9036 else
9037 outs() << " count " << count
9038 << " (not ARM_THREAD_STATE_COUNT)\n";
9039 MachO::arm_thread_state32_t cpu32;
9040 left = end - begin;
9041 if (left >= sizeof(MachO::arm_thread_state32_t)) {
9042 memcpy(&cpu32, begin, sizeof(MachO::arm_thread_state32_t));
9043 begin += sizeof(MachO::arm_thread_state32_t);
9044 } else {
9045 memset(&cpu32, '\0', sizeof(MachO::arm_thread_state32_t));
9046 memcpy(&cpu32, begin, left);
9047 begin += left;
9048 }
9049 if (isLittleEndian != sys::IsLittleEndianHost)
9050 swapStruct(cpu32);
9051 Print_arm_thread_state32_t(cpu32);
9052 } else {
9053 outs() << " flavor " << flavor << " (unknown)\n";
9054 outs() << " count " << count << "\n";
9055 outs() << " state (unknown)\n";
9056 begin += count * sizeof(uint32_t);
9057 }
9058 }
Kevin Enderby7747cb52016-11-03 20:51:28 +00009059 } else if (cputype == MachO::CPU_TYPE_ARM64) {
9060 while (begin < end) {
9061 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9062 memcpy((char *)&flavor, begin, sizeof(uint32_t));
9063 begin += sizeof(uint32_t);
9064 } else {
9065 flavor = 0;
9066 begin = end;
9067 }
9068 if (isLittleEndian != sys::IsLittleEndianHost)
9069 sys::swapByteOrder(flavor);
9070 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9071 memcpy((char *)&count, begin, sizeof(uint32_t));
9072 begin += sizeof(uint32_t);
9073 } else {
9074 count = 0;
9075 begin = end;
9076 }
9077 if (isLittleEndian != sys::IsLittleEndianHost)
9078 sys::swapByteOrder(count);
9079 if (flavor == MachO::ARM_THREAD_STATE64) {
9080 outs() << " flavor ARM_THREAD_STATE64\n";
9081 if (count == MachO::ARM_THREAD_STATE64_COUNT)
9082 outs() << " count ARM_THREAD_STATE64_COUNT\n";
9083 else
9084 outs() << " count " << count
9085 << " (not ARM_THREAD_STATE64_COUNT)\n";
9086 MachO::arm_thread_state64_t cpu64;
9087 left = end - begin;
9088 if (left >= sizeof(MachO::arm_thread_state64_t)) {
9089 memcpy(&cpu64, begin, sizeof(MachO::arm_thread_state64_t));
9090 begin += sizeof(MachO::arm_thread_state64_t);
9091 } else {
9092 memset(&cpu64, '\0', sizeof(MachO::arm_thread_state64_t));
9093 memcpy(&cpu64, begin, left);
9094 begin += left;
9095 }
9096 if (isLittleEndian != sys::IsLittleEndianHost)
9097 swapStruct(cpu64);
9098 Print_arm_thread_state64_t(cpu64);
9099 } else {
9100 outs() << " flavor " << flavor << " (unknown)\n";
9101 outs() << " count " << count << "\n";
9102 outs() << " state (unknown)\n";
9103 begin += count * sizeof(uint32_t);
9104 }
9105 }
Kevin Enderby48ef5342014-12-23 22:56:39 +00009106 } else {
9107 while (begin < end) {
9108 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9109 memcpy((char *)&flavor, begin, sizeof(uint32_t));
9110 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00009111 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00009112 flavor = 0;
9113 begin = end;
9114 }
9115 if (isLittleEndian != sys::IsLittleEndianHost)
9116 sys::swapByteOrder(flavor);
9117 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9118 memcpy((char *)&count, begin, sizeof(uint32_t));
9119 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00009120 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00009121 count = 0;
9122 begin = end;
9123 }
9124 if (isLittleEndian != sys::IsLittleEndianHost)
9125 sys::swapByteOrder(count);
9126 outs() << " flavor " << flavor << "\n";
9127 outs() << " count " << count << "\n";
9128 outs() << " state (Unknown cputype/cpusubtype)\n";
9129 begin += count * sizeof(uint32_t);
9130 }
9131 }
9132}
Kevin Enderby66d51fc2015-01-08 00:25:24 +00009133
Kevin Enderby8ae63c12014-09-04 16:54:47 +00009134static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) {
9135 if (dl.cmd == MachO::LC_ID_DYLIB)
9136 outs() << " cmd LC_ID_DYLIB\n";
9137 else if (dl.cmd == MachO::LC_LOAD_DYLIB)
9138 outs() << " cmd LC_LOAD_DYLIB\n";
9139 else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB)
9140 outs() << " cmd LC_LOAD_WEAK_DYLIB\n";
9141 else if (dl.cmd == MachO::LC_REEXPORT_DYLIB)
9142 outs() << " cmd LC_REEXPORT_DYLIB\n";
9143 else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB)
9144 outs() << " cmd LC_LAZY_LOAD_DYLIB\n";
9145 else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
9146 outs() << " cmd LC_LOAD_UPWARD_DYLIB\n";
9147 else
9148 outs() << " cmd " << dl.cmd << " (unknown)\n";
9149 outs() << " cmdsize " << dl.cmdsize;
9150 if (dl.cmdsize < sizeof(struct MachO::dylib_command))
9151 outs() << " Incorrect size\n";
9152 else
9153 outs() << "\n";
9154 if (dl.dylib.name < dl.cmdsize) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009155 const char *P = (const char *)(Ptr) + dl.dylib.name;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00009156 outs() << " name " << P << " (offset " << dl.dylib.name << ")\n";
9157 } else {
9158 outs() << " name ?(bad offset " << dl.dylib.name << ")\n";
9159 }
9160 outs() << " time stamp " << dl.dylib.timestamp << " ";
9161 time_t t = dl.dylib.timestamp;
9162 outs() << ctime(&t);
9163 outs() << " current version ";
9164 if (dl.dylib.current_version == 0xffffffff)
9165 outs() << "n/a\n";
9166 else
9167 outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "."
9168 << ((dl.dylib.current_version >> 8) & 0xff) << "."
9169 << (dl.dylib.current_version & 0xff) << "\n";
9170 outs() << "compatibility version ";
9171 if (dl.dylib.compatibility_version == 0xffffffff)
9172 outs() << "n/a\n";
9173 else
9174 outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
9175 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
9176 << (dl.dylib.compatibility_version & 0xff) << "\n";
9177}
9178
9179static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
9180 uint32_t object_size) {
9181 if (ld.cmd == MachO::LC_CODE_SIGNATURE)
Kevin Enderby1be37a32016-04-28 21:07:20 +00009182 outs() << " cmd LC_CODE_SIGNATURE\n";
Kevin Enderby8ae63c12014-09-04 16:54:47 +00009183 else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO)
9184 outs() << " cmd LC_SEGMENT_SPLIT_INFO\n";
9185 else if (ld.cmd == MachO::LC_FUNCTION_STARTS)
9186 outs() << " cmd LC_FUNCTION_STARTS\n";
9187 else if (ld.cmd == MachO::LC_DATA_IN_CODE)
9188 outs() << " cmd LC_DATA_IN_CODE\n";
9189 else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS)
9190 outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n";
9191 else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT)
9192 outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n";
9193 else
9194 outs() << " cmd " << ld.cmd << " (?)\n";
9195 outs() << " cmdsize " << ld.cmdsize;
9196 if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command))
9197 outs() << " Incorrect size\n";
9198 else
9199 outs() << "\n";
9200 outs() << " dataoff " << ld.dataoff;
9201 if (ld.dataoff > object_size)
9202 outs() << " (past end of file)\n";
9203 else
9204 outs() << "\n";
9205 outs() << " datasize " << ld.datasize;
9206 uint64_t big_size = ld.dataoff;
9207 big_size += ld.datasize;
9208 if (big_size > object_size)
9209 outs() << " (past end of file)\n";
9210 else
9211 outs() << "\n";
9212}
9213
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00009214static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t filetype,
9215 uint32_t cputype, bool verbose) {
Kevin Enderby956366c2014-08-29 22:30:52 +00009216 StringRef Buf = Obj->getData();
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00009217 unsigned Index = 0;
9218 for (const auto &Command : Obj->load_commands()) {
9219 outs() << "Load command " << Index++ << "\n";
Kevin Enderby956366c2014-08-29 22:30:52 +00009220 if (Command.C.cmd == MachO::LC_SEGMENT) {
9221 MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
9222 const char *sg_segname = SLC.segname;
9223 PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr,
9224 SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot,
9225 SLC.initprot, SLC.nsects, SLC.flags, Buf.size(),
9226 verbose);
9227 for (unsigned j = 0; j < SLC.nsects; j++) {
Kevin Enderbyc9713382014-12-16 01:14:45 +00009228 MachO::section S = Obj->getSection(Command, j);
Kevin Enderby956366c2014-08-29 22:30:52 +00009229 PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align,
9230 S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2,
9231 SLC.cmd, sg_segname, filetype, Buf.size(), verbose);
9232 }
9233 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
9234 MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command);
9235 const char *sg_segname = SLC_64.segname;
9236 PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname,
9237 SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff,
9238 SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot,
9239 SLC_64.nsects, SLC_64.flags, Buf.size(), verbose);
9240 for (unsigned j = 0; j < SLC_64.nsects; j++) {
9241 MachO::section_64 S_64 = Obj->getSection64(Command, j);
9242 PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size,
9243 S_64.offset, S_64.align, S_64.reloff, S_64.nreloc,
9244 S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd,
9245 sg_segname, filetype, Buf.size(), verbose);
9246 }
9247 } else if (Command.C.cmd == MachO::LC_SYMTAB) {
9248 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
David Majnemer73cc6ff2014-11-13 19:48:56 +00009249 PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size());
Kevin Enderby956366c2014-08-29 22:30:52 +00009250 } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
9251 MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
9252 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
David Majnemer73cc6ff2014-11-13 19:48:56 +00009253 PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(),
9254 Obj->is64Bit());
Kevin Enderby8ae63c12014-09-04 16:54:47 +00009255 } else if (Command.C.cmd == MachO::LC_DYLD_INFO ||
9256 Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
9257 MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command);
9258 PrintDyldInfoLoadCommand(DyldInfo, Buf.size());
9259 } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER ||
9260 Command.C.cmd == MachO::LC_ID_DYLINKER ||
9261 Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
9262 MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command);
9263 PrintDyldLoadCommand(Dyld, Command.Ptr);
9264 } else if (Command.C.cmd == MachO::LC_UUID) {
9265 MachO::uuid_command Uuid = Obj->getUuidCommand(Command);
9266 PrintUuidLoadCommand(Uuid);
Jean-Daniel Dupas00cc1f52014-12-04 07:37:02 +00009267 } else if (Command.C.cmd == MachO::LC_RPATH) {
9268 MachO::rpath_command Rpath = Obj->getRpathCommand(Command);
9269 PrintRpathLoadCommand(Rpath, Command.Ptr);
Kevin Enderby1ff0ecc2014-12-16 21:48:27 +00009270 } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX ||
Tim Northoverbfbfb122015-11-02 21:26:58 +00009271 Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS ||
9272 Command.C.cmd == MachO::LC_VERSION_MIN_TVOS ||
9273 Command.C.cmd == MachO::LC_VERSION_MIN_WATCHOS) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00009274 MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command);
9275 PrintVersionMinLoadCommand(Vd);
Kevin Enderbya4579c42017-01-19 17:36:31 +00009276 } else if (Command.C.cmd == MachO::LC_NOTE) {
9277 MachO::note_command Nt = Obj->getNoteLoadCommand(Command);
9278 PrintNoteLoadCommand(Nt);
Steven Wu5b54a422017-01-23 20:07:55 +00009279 } else if (Command.C.cmd == MachO::LC_BUILD_VERSION) {
9280 MachO::build_version_command Bv =
9281 Obj->getBuildVersionLoadCommand(Command);
9282 PrintBuildVersionLoadCommand(Obj, Bv);
Kevin Enderby8ae63c12014-09-04 16:54:47 +00009283 } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) {
9284 MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command);
9285 PrintSourceVersionCommand(Sd);
9286 } else if (Command.C.cmd == MachO::LC_MAIN) {
9287 MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command);
9288 PrintEntryPointCommand(Ep);
Kevin Enderby0804f4672014-12-16 23:25:52 +00009289 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00009290 MachO::encryption_info_command Ei =
9291 Obj->getEncryptionInfoCommand(Command);
Kevin Enderby0804f4672014-12-16 23:25:52 +00009292 PrintEncryptionInfoCommand(Ei, Buf.size());
Kevin Enderby57538292014-12-17 01:01:30 +00009293 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00009294 MachO::encryption_info_command_64 Ei =
9295 Obj->getEncryptionInfoCommand64(Command);
Kevin Enderby57538292014-12-17 01:01:30 +00009296 PrintEncryptionInfoCommand64(Ei, Buf.size());
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00009297 } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00009298 MachO::linker_option_command Lo =
9299 Obj->getLinkerOptionLoadCommand(Command);
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00009300 PrintLinkerOptionCommand(Lo, Command.Ptr);
Kevin Enderbyb4b79312014-12-18 19:24:35 +00009301 } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) {
9302 MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command);
9303 PrintSubFrameworkCommand(Sf, Command.Ptr);
Kevin Enderbya2bd8d92014-12-18 23:13:26 +00009304 } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) {
9305 MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command);
9306 PrintSubUmbrellaCommand(Sf, Command.Ptr);
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00009307 } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) {
9308 MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command);
9309 PrintSubLibraryCommand(Sl, Command.Ptr);
Kevin Enderby186eac32014-12-19 21:06:24 +00009310 } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) {
9311 MachO::sub_client_command Sc = Obj->getSubClientCommand(Command);
9312 PrintSubClientCommand(Sc, Command.Ptr);
Kevin Enderby52e4ce42014-12-19 22:25:22 +00009313 } else if (Command.C.cmd == MachO::LC_ROUTINES) {
9314 MachO::routines_command Rc = Obj->getRoutinesCommand(Command);
9315 PrintRoutinesCommand(Rc);
9316 } else if (Command.C.cmd == MachO::LC_ROUTINES_64) {
9317 MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command);
9318 PrintRoutinesCommand64(Rc);
Kevin Enderby48ef5342014-12-23 22:56:39 +00009319 } else if (Command.C.cmd == MachO::LC_THREAD ||
9320 Command.C.cmd == MachO::LC_UNIXTHREAD) {
9321 MachO::thread_command Tc = Obj->getThreadCommand(Command);
9322 PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype);
Nick Kledzik15558912014-10-16 18:58:20 +00009323 } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
9324 Command.C.cmd == MachO::LC_ID_DYLIB ||
9325 Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
9326 Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
9327 Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
9328 Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00009329 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
9330 PrintDylibCommand(Dl, Command.Ptr);
9331 } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE ||
9332 Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO ||
9333 Command.C.cmd == MachO::LC_FUNCTION_STARTS ||
9334 Command.C.cmd == MachO::LC_DATA_IN_CODE ||
9335 Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS ||
9336 Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
9337 MachO::linkedit_data_command Ld =
9338 Obj->getLinkeditDataLoadCommand(Command);
9339 PrintLinkEditDataCommand(Ld, Buf.size());
Kevin Enderby956366c2014-08-29 22:30:52 +00009340 } else {
9341 outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd)
9342 << ")\n";
9343 outs() << " cmdsize " << Command.C.cmdsize << "\n";
9344 // TODO: get and print the raw bytes of the load command.
9345 }
9346 // TODO: print all the other kinds of load commands.
Kevin Enderby956366c2014-08-29 22:30:52 +00009347 }
9348}
9349
Kevin Enderby0ae163f2016-01-13 00:25:36 +00009350static void PrintMachHeader(const MachOObjectFile *Obj, bool verbose) {
Kevin Enderbyb76d3862014-08-22 20:35:18 +00009351 if (Obj->is64Bit()) {
9352 MachO::mach_header_64 H_64;
9353 H_64 = Obj->getHeader64();
9354 PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
9355 H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
9356 } else {
9357 MachO::mach_header H;
9358 H = Obj->getHeader();
9359 PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
9360 H.sizeofcmds, H.flags, verbose);
9361 }
9362}
9363
9364void llvm::printMachOFileHeader(const object::ObjectFile *Obj) {
9365 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
Kevin Enderby0ae163f2016-01-13 00:25:36 +00009366 PrintMachHeader(file, !NonVerbose);
9367}
9368
9369void llvm::printMachOLoadCommands(const object::ObjectFile *Obj) {
9370 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
Kevin Enderby956366c2014-08-29 22:30:52 +00009371 uint32_t filetype = 0;
9372 uint32_t cputype = 0;
Kevin Enderby0ae163f2016-01-13 00:25:36 +00009373 if (file->is64Bit()) {
9374 MachO::mach_header_64 H_64;
9375 H_64 = file->getHeader64();
9376 filetype = H_64.filetype;
9377 cputype = H_64.cputype;
9378 } else {
9379 MachO::mach_header H;
9380 H = file->getHeader();
9381 filetype = H.filetype;
9382 cputype = H.cputype;
9383 }
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00009384 PrintLoadCommands(file, filetype, cputype, !NonVerbose);
Kevin Enderbyb76d3862014-08-22 20:35:18 +00009385}
Nick Kledzikd04bc352014-08-30 00:20:14 +00009386
9387//===----------------------------------------------------------------------===//
9388// export trie dumping
9389//===----------------------------------------------------------------------===//
9390
9391void llvm::printMachOExportsTrie(const object::MachOObjectFile *Obj) {
Kevin Enderby0d5ec112017-06-19 21:23:07 +00009392 uint64_t BaseSegmentAddress = 0;
9393 for (const auto &Command : Obj->load_commands()) {
9394 if (Command.C.cmd == MachO::LC_SEGMENT) {
9395 MachO::segment_command Seg = Obj->getSegmentLoadCommand(Command);
9396 if (Seg.fileoff == 0 && Seg.filesize != 0) {
9397 BaseSegmentAddress = Seg.vmaddr;
9398 break;
9399 }
9400 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
9401 MachO::segment_command_64 Seg = Obj->getSegment64LoadCommand(Command);
9402 if (Seg.fileoff == 0 && Seg.filesize != 0) {
9403 BaseSegmentAddress = Seg.vmaddr;
9404 break;
9405 }
9406 }
9407 }
Kevin Enderby3e95bd22017-07-20 23:08:41 +00009408 Error Err = Error::success();
Alexander Shaposhnikove5740342017-07-29 00:30:45 +00009409 for (const llvm::object::ExportEntry &Entry : Obj->exports(Err)) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009410 uint64_t Flags = Entry.flags();
Nick Kledzikd04bc352014-08-30 00:20:14 +00009411 bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
9412 bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
9413 bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
9414 MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL);
9415 bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
9416 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
9417 bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
9418 if (ReExport)
9419 outs() << "[re-export] ";
9420 else
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009421 outs() << format("0x%08llX ",
Kevin Enderby0d5ec112017-06-19 21:23:07 +00009422 Entry.address() + BaseSegmentAddress);
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009423 outs() << Entry.name();
Nick Kledzikd04bc352014-08-30 00:20:14 +00009424 if (WeakDef || ThreadLocal || Resolver || Abs) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009425 bool NeedsComma = false;
Nick Kledzik1d1ac4b2014-09-03 01:12:52 +00009426 outs() << " [";
Nick Kledzikd04bc352014-08-30 00:20:14 +00009427 if (WeakDef) {
9428 outs() << "weak_def";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009429 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00009430 }
9431 if (ThreadLocal) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009432 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00009433 outs() << ", ";
9434 outs() << "per-thread";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009435 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00009436 }
9437 if (Abs) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009438 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00009439 outs() << ", ";
9440 outs() << "absolute";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009441 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00009442 }
9443 if (Resolver) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009444 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00009445 outs() << ", ";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009446 outs() << format("resolver=0x%08llX", Entry.other());
9447 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00009448 }
9449 outs() << "]";
9450 }
9451 if (ReExport) {
9452 StringRef DylibName = "unknown";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009453 int Ordinal = Entry.other() - 1;
9454 Obj->getLibraryShortNameByIndex(Ordinal, DylibName);
9455 if (Entry.otherName().empty())
Nick Kledzikd04bc352014-08-30 00:20:14 +00009456 outs() << " (from " << DylibName << ")";
9457 else
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009458 outs() << " (" << Entry.otherName() << " from " << DylibName << ")";
Nick Kledzikd04bc352014-08-30 00:20:14 +00009459 }
9460 outs() << "\n";
9461 }
Kevin Enderby3e95bd22017-07-20 23:08:41 +00009462 if (Err)
9463 report_error(Obj->getFileName(), std::move(Err));
Nick Kledzikd04bc352014-08-30 00:20:14 +00009464}
Nick Kledzikac431442014-09-12 21:34:15 +00009465
Nick Kledzikac431442014-09-12 21:34:15 +00009466//===----------------------------------------------------------------------===//
9467// rebase table dumping
9468//===----------------------------------------------------------------------===//
9469
Kevin Enderbya8d256c2017-03-20 19:46:55 +00009470void llvm::printMachORebaseTable(object::MachOObjectFile *Obj) {
Nick Kledzikac431442014-09-12 21:34:15 +00009471 outs() << "segment section address type\n";
Kevin Enderbya8d256c2017-03-20 19:46:55 +00009472 Error Err = Error::success();
9473 for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable(Err)) {
9474 StringRef SegmentName = Entry.segmentName();
9475 StringRef SectionName = Entry.sectionName();
9476 uint64_t Address = Entry.address();
Nick Kledzikac431442014-09-12 21:34:15 +00009477
9478 // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009479 outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n",
9480 SegmentName.str().c_str(), SectionName.str().c_str(),
9481 Address, Entry.typeName().str().c_str());
Nick Kledzikac431442014-09-12 21:34:15 +00009482 }
Kevin Enderbya8d256c2017-03-20 19:46:55 +00009483 if (Err)
9484 report_error(Obj->getFileName(), std::move(Err));
Nick Kledzikac431442014-09-12 21:34:15 +00009485}
Nick Kledzik56ebef42014-09-16 01:41:51 +00009486
9487static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
9488 StringRef DylibName;
9489 switch (Ordinal) {
9490 case MachO::BIND_SPECIAL_DYLIB_SELF:
9491 return "this-image";
9492 case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
9493 return "main-executable";
9494 case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
9495 return "flat-namespace";
9496 default:
Nick Kledzikabd29872014-09-16 22:03:13 +00009497 if (Ordinal > 0) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009498 std::error_code EC =
9499 Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName);
Nick Kledzikabd29872014-09-16 22:03:13 +00009500 if (EC)
Nick Kledzik51d2c2b2014-10-14 23:29:38 +00009501 return "<<bad library ordinal>>";
Nick Kledzikabd29872014-09-16 22:03:13 +00009502 return DylibName;
9503 }
Nick Kledzik56ebef42014-09-16 01:41:51 +00009504 }
Nick Kledzikabd29872014-09-16 22:03:13 +00009505 return "<<unknown special ordinal>>";
Nick Kledzik56ebef42014-09-16 01:41:51 +00009506}
9507
9508//===----------------------------------------------------------------------===//
9509// bind table dumping
9510//===----------------------------------------------------------------------===//
9511
Kevin Enderbya8d256c2017-03-20 19:46:55 +00009512void llvm::printMachOBindTable(object::MachOObjectFile *Obj) {
Nick Kledzik56ebef42014-09-16 01:41:51 +00009513 // Build table of sections so names can used in final output.
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009514 outs() << "segment section address type "
9515 "addend dylib symbol\n";
Kevin Enderbyfeb63b92017-02-28 21:47:07 +00009516 Error Err = Error::success();
9517 for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable(Err)) {
Kevin Enderbya8d256c2017-03-20 19:46:55 +00009518 StringRef SegmentName = Entry.segmentName();
9519 StringRef SectionName = Entry.sectionName();
9520 uint64_t Address = Entry.address();
Nick Kledzik56ebef42014-09-16 01:41:51 +00009521
9522 // Table lines look like:
9523 // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009524 StringRef Attr;
Nick Kledzik56ebef42014-09-16 01:41:51 +00009525 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT)
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009526 Attr = " (weak_import)";
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009527 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009528 << left_justify(SectionName, 18) << " "
9529 << format_hex(Address, 10, true) << " "
9530 << left_justify(Entry.typeName(), 8) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009531 << format_decimal(Entry.addend(), 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009532 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009533 << Entry.symbolName() << Attr << "\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00009534 }
Kevin Enderbyfeb63b92017-02-28 21:47:07 +00009535 if (Err)
9536 report_error(Obj->getFileName(), std::move(Err));
Nick Kledzik56ebef42014-09-16 01:41:51 +00009537}
9538
9539//===----------------------------------------------------------------------===//
9540// lazy bind table dumping
9541//===----------------------------------------------------------------------===//
9542
Kevin Enderbya8d256c2017-03-20 19:46:55 +00009543void llvm::printMachOLazyBindTable(object::MachOObjectFile *Obj) {
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009544 outs() << "segment section address "
9545 "dylib symbol\n";
Kevin Enderbyfeb63b92017-02-28 21:47:07 +00009546 Error Err = Error::success();
9547 for (const llvm::object::MachOBindEntry &Entry : Obj->lazyBindTable(Err)) {
Kevin Enderbya8d256c2017-03-20 19:46:55 +00009548 StringRef SegmentName = Entry.segmentName();
9549 StringRef SectionName = Entry.sectionName();
9550 uint64_t Address = Entry.address();
Nick Kledzik56ebef42014-09-16 01:41:51 +00009551
9552 // Table lines look like:
9553 // __DATA __got 0x00012010 libSystem ___stack_chk_guard
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009554 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009555 << left_justify(SectionName, 18) << " "
9556 << format_hex(Address, 10, true) << " "
9557 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
Nick Kledzik56ebef42014-09-16 01:41:51 +00009558 << Entry.symbolName() << "\n";
9559 }
Kevin Enderbyfeb63b92017-02-28 21:47:07 +00009560 if (Err)
9561 report_error(Obj->getFileName(), std::move(Err));
Nick Kledzik56ebef42014-09-16 01:41:51 +00009562}
9563
Nick Kledzik56ebef42014-09-16 01:41:51 +00009564//===----------------------------------------------------------------------===//
9565// weak bind table dumping
9566//===----------------------------------------------------------------------===//
9567
Kevin Enderbya8d256c2017-03-20 19:46:55 +00009568void llvm::printMachOWeakBindTable(object::MachOObjectFile *Obj) {
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009569 outs() << "segment section address "
9570 "type addend symbol\n";
Kevin Enderbyfeb63b92017-02-28 21:47:07 +00009571 Error Err = Error::success();
9572 for (const llvm::object::MachOBindEntry &Entry : Obj->weakBindTable(Err)) {
Nick Kledzik56ebef42014-09-16 01:41:51 +00009573 // Strong symbols don't have a location to update.
9574 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009575 outs() << " strong "
Nick Kledzik56ebef42014-09-16 01:41:51 +00009576 << Entry.symbolName() << "\n";
9577 continue;
9578 }
Kevin Enderbya8d256c2017-03-20 19:46:55 +00009579 StringRef SegmentName = Entry.segmentName();
9580 StringRef SectionName = Entry.sectionName();
9581 uint64_t Address = Entry.address();
Nick Kledzik56ebef42014-09-16 01:41:51 +00009582
9583 // Table lines look like:
9584 // __DATA __data 0x00001000 pointer 0 _foo
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009585 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009586 << left_justify(SectionName, 18) << " "
9587 << format_hex(Address, 10, true) << " "
9588 << left_justify(Entry.typeName(), 8) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009589 << format_decimal(Entry.addend(), 8) << " " << Entry.symbolName()
9590 << "\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00009591 }
Kevin Enderbyfeb63b92017-02-28 21:47:07 +00009592 if (Err)
9593 report_error(Obj->getFileName(), std::move(Err));
Nick Kledzik56ebef42014-09-16 01:41:51 +00009594}
9595
Kevin Enderby6f326ce2014-10-23 19:37:31 +00009596// get_dyld_bind_info_symbolname() is used for disassembly and passed an
9597// address, ReferenceValue, in the Mach-O file and looks in the dyld bind
9598// information for that address. If the address is found its binding symbol
9599// name is returned. If not nullptr is returned.
9600static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
9601 struct DisassembleInfo *info) {
Kevin Enderby078be602014-10-23 19:53:12 +00009602 if (info->bindtable == nullptr) {
Saleem Abdulrasool1d84d9a2017-01-08 19:14:15 +00009603 info->bindtable = llvm::make_unique<SymbolAddressMap>();
Kevin Enderbyfeb63b92017-02-28 21:47:07 +00009604 Error Err = Error::success();
9605 for (const llvm::object::MachOBindEntry &Entry : info->O->bindTable(Err)) {
Kevin Enderbya8d256c2017-03-20 19:46:55 +00009606 uint64_t Address = Entry.address();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00009607 StringRef name = Entry.symbolName();
9608 if (!name.empty())
Saleem Abdulrasool1d84d9a2017-01-08 19:14:15 +00009609 (*info->bindtable)[Address] = name;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00009610 }
Kevin Enderbyfeb63b92017-02-28 21:47:07 +00009611 if (Err)
9612 report_error(info->O->getFileName(), std::move(Err));
Kevin Enderby6f326ce2014-10-23 19:37:31 +00009613 }
Saleem Abdulrasool1d84d9a2017-01-08 19:14:15 +00009614 auto name = info->bindtable->lookup(ReferenceValue);
9615 return !name.empty() ? name.data() : nullptr;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00009616}