blob: b5e7a067b7c33fec4b3dc29ab787fcdf40d3f091 [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
Mehdi Aminib550cb12016-04-18 09:17:29 +000014#include "llvm/Object/MachO.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000015#include "llvm-objdump.h"
Kevin Enderby98c9acc2014-09-16 18:00:57 +000016#include "llvm-c/Disassembler.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000017#include "llvm/ADT/STLExtras.h"
Ahmed Bougachaaa790682013-05-24 01:07:04 +000018#include "llvm/ADT/StringExtras.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000019#include "llvm/ADT/Triple.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"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000023#include "llvm/MC/MCAsmInfo.h"
Lang Hamesa1bc0f52014-04-15 04:40:56 +000024#include "llvm/MC/MCContext.h"
Benjamin Kramerf57c1972016-01-26 16:44:37 +000025#include "llvm/MC/MCDisassembler/MCDisassembler.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000026#include "llvm/MC/MCInst.h"
27#include "llvm/MC/MCInstPrinter.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000028#include "llvm/MC/MCInstrDesc.h"
29#include "llvm/MC/MCInstrInfo.h"
Jim Grosbachfd93a592012-03-05 19:33:20 +000030#include "llvm/MC/MCRegisterInfo.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000031#include "llvm/MC/MCSubtargetInfo.h"
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +000032#include "llvm/Object/MachOUniversal.h"
Rafael Espindola9b709252013-04-13 01:45:40 +000033#include "llvm/Support/Casting.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000034#include "llvm/Support/CommandLine.h"
35#include "llvm/Support/Debug.h"
Tim Northover4bd286a2014-08-01 13:07:19 +000036#include "llvm/Support/Endian.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000037#include "llvm/Support/Format.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000038#include "llvm/Support/FormattedStream.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000039#include "llvm/Support/GraphWriter.h"
Kevin Enderby9a509442015-01-27 21:28:24 +000040#include "llvm/Support/LEB128.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000041#include "llvm/Support/MachO.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000042#include "llvm/Support/MemoryBuffer.h"
43#include "llvm/Support/TargetRegistry.h"
44#include "llvm/Support/TargetSelect.h"
Kevin Enderby9873e2c2016-05-23 21:34:12 +000045#include "llvm/Support/ToolOutputFile.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000046#include "llvm/Support/raw_ostream.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000047#include <algorithm>
48#include <cstring>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000049#include <system_error>
Kevin Enderby04bf6932014-10-28 23:39:46 +000050
51#if HAVE_CXXABI_H
52#include <cxxabi.h>
53#endif
54
Kevin Enderby9873e2c2016-05-23 21:34:12 +000055#ifdef HAVE_LIBXAR
56extern "C" {
57#include <xar/xar.h>
58}
59#endif
60
Benjamin Kramer43a772e2011-09-19 17:56:04 +000061using namespace llvm;
62using namespace object;
63
64static cl::opt<bool>
Kevin Enderbyb28ed012014-10-29 21:28:24 +000065 UseDbg("g",
66 cl::desc("Print line information from debug info if available"));
Benjamin Kramer699128e2011-09-21 01:13:19 +000067
Kevin Enderbyb28ed012014-10-29 21:28:24 +000068static cl::opt<std::string> DSYMFile("dsym",
69 cl::desc("Use .dSYM file for debug info"));
Benjamin Kramer699128e2011-09-21 01:13:19 +000070
Kevin Enderbyb28ed012014-10-29 21:28:24 +000071static cl::opt<bool> FullLeadingAddr("full-leading-addr",
72 cl::desc("Print full leading address"));
Kevin Enderbybf246f52014-09-24 23:08:22 +000073
Kevin Enderbyab5e6c92015-03-17 21:07:39 +000074static cl::opt<bool> NoLeadingAddr("no-leading-addr",
75 cl::desc("Print no leading address"));
76
Kevin Enderby13023a12015-01-15 23:19:11 +000077cl::opt<bool> llvm::UniversalHeaders("universal-headers",
78 cl::desc("Print Mach-O universal headers "
79 "(requires -macho)"));
80
Kevin Enderby131d1772015-01-09 19:22:37 +000081cl::opt<bool>
Kevin Enderby13023a12015-01-15 23:19:11 +000082 llvm::ArchiveHeaders("archive-headers",
83 cl::desc("Print archive headers for Mach-O archives "
84 "(requires -macho)"));
Kevin Enderby131d1772015-01-09 19:22:37 +000085
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +000086cl::opt<bool>
Kevin Enderby8972e482015-04-30 20:30:42 +000087 ArchiveMemberOffsets("archive-member-offsets",
88 cl::desc("Print the offset to each archive member for "
89 "Mach-O archives (requires -macho and "
90 "-archive-headers)"));
91
92cl::opt<bool>
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +000093 llvm::IndirectSymbols("indirect-symbols",
94 cl::desc("Print indirect symbol table for Mach-O "
95 "objects (requires -macho)"));
96
Kevin Enderby69fe98d2015-01-23 18:52:17 +000097cl::opt<bool>
98 llvm::DataInCode("data-in-code",
99 cl::desc("Print the data in code table for Mach-O objects "
100 "(requires -macho)"));
101
Kevin Enderby9a509442015-01-27 21:28:24 +0000102cl::opt<bool>
103 llvm::LinkOptHints("link-opt-hints",
104 cl::desc("Print the linker optimization hints for "
105 "Mach-O objects (requires -macho)"));
106
Kevin Enderbycd66be52015-03-11 22:06:32 +0000107cl::opt<bool>
108 llvm::InfoPlist("info-plist",
109 cl::desc("Print the info plist section as strings for "
110 "Mach-O objects (requires -macho)"));
111
Kevin Enderbyf0640752015-03-13 17:56:32 +0000112cl::opt<bool>
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000113 llvm::DylibsUsed("dylibs-used",
114 cl::desc("Print the shared libraries used for linked "
115 "Mach-O files (requires -macho)"));
116
117cl::opt<bool>
118 llvm::DylibId("dylib-id",
119 cl::desc("Print the shared library's id for the dylib Mach-O "
120 "file (requires -macho)"));
121
122cl::opt<bool>
Kevin Enderbyf0640752015-03-13 17:56:32 +0000123 llvm::NonVerbose("non-verbose",
124 cl::desc("Print the info for Mach-O objects in "
125 "non-verbose or numeric form (requires -macho)"));
126
Kevin Enderby0fc11822015-04-01 20:57:01 +0000127cl::opt<bool>
128 llvm::ObjcMetaData("objc-meta-data",
129 cl::desc("Print the Objective-C runtime meta data for "
130 "Mach-O files (requires -macho)"));
131
Kevin Enderby6a221752015-03-17 17:10:57 +0000132cl::opt<std::string> llvm::DisSymName(
133 "dis-symname",
134 cl::desc("disassemble just this symbol's instructions (requires -macho"));
135
Kevin Enderby8e29ec92015-03-17 22:26:11 +0000136static cl::opt<bool> NoSymbolicOperands(
137 "no-symbolic-operands",
138 cl::desc("do not symbolic operands when disassembling (requires -macho)"));
139
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000140static cl::list<std::string>
141 ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
142 cl::ZeroOrMore);
Hans Wennborgcc9deb42015-09-29 18:02:48 +0000143
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000144bool ArchAll = false;
145
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000146static std::string ThumbTripleName;
147
148static const Target *GetTarget(const MachOObjectFile *MachOObj,
149 const char **McpuDefault,
150 const Target **ThumbTarget) {
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000151 // Figure out the target triple.
Tim Northover9e8eb412016-04-22 23:21:13 +0000152 llvm::Triple TT(TripleName);
Cameron Zwarich88cc16a2012-02-03 06:35:22 +0000153 if (TripleName.empty()) {
Tim Northover9e8eb412016-04-22 23:21:13 +0000154 TT = MachOObj->getArchTriple(McpuDefault);
Cameron Zwarich88cc16a2012-02-03 06:35:22 +0000155 TripleName = TT.str();
Tim Northover9e8eb412016-04-22 23:21:13 +0000156 }
157
158 if (TT.getArch() == Triple::arm) {
159 // We've inferred a 32-bit ARM target from the object file. All MachO CPUs
160 // that support ARM are also capable of Thumb mode.
161 llvm::Triple ThumbTriple = TT;
162 std::string ThumbName = (Twine("thumb") + TT.getArchName().substr(3)).str();
163 ThumbTriple.setArchName(ThumbName);
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000164 ThumbTripleName = ThumbTriple.str();
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000165 }
166
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000167 // Get the target specific parser.
168 std::string Error;
169 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000170 if (TheTarget && ThumbTripleName.empty())
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000171 return TheTarget;
172
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000173 *ThumbTarget = TargetRegistry::lookupTarget(ThumbTripleName, Error);
174 if (*ThumbTarget)
175 return TheTarget;
176
177 errs() << "llvm-objdump: error: unable to get target for '";
178 if (!TheTarget)
179 errs() << TripleName;
180 else
181 errs() << ThumbTripleName;
182 errs() << "', see --version and --triple.\n";
Craig Toppere6cb63e2014-04-25 04:24:47 +0000183 return nullptr;
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000184}
185
Owen Andersond9243c42011-10-17 21:37:35 +0000186struct SymbolSorter {
187 bool operator()(const SymbolRef &A, const SymbolRef &B) {
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000188 Expected<SymbolRef::Type> ATypeOrErr = A.getType();
189 if (!ATypeOrErr) {
190 std::string Buf;
191 raw_string_ostream OS(Buf);
192 logAllUnhandledErrors(ATypeOrErr.takeError(), OS, "");
193 OS.flush();
194 report_fatal_error(Buf);
195 }
Kevin Enderby5afbc1c2016-03-23 20:27:00 +0000196 SymbolRef::Type AType = *ATypeOrErr;
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000197 Expected<SymbolRef::Type> BTypeOrErr = B.getType();
198 if (!BTypeOrErr) {
199 std::string Buf;
200 raw_string_ostream OS(Buf);
201 logAllUnhandledErrors(BTypeOrErr.takeError(), OS, "");
202 OS.flush();
203 report_fatal_error(Buf);
204 }
Kevin Enderby74f58d42016-03-23 21:45:21 +0000205 SymbolRef::Type BType = *BTypeOrErr;
Kevin Enderby5afbc1c2016-03-23 20:27:00 +0000206 uint64_t AAddr = (AType != SymbolRef::ST_Function) ? 0 : A.getValue();
207 uint64_t BAddr = (BType != SymbolRef::ST_Function) ? 0 : B.getValue();
Owen Andersond9243c42011-10-17 21:37:35 +0000208 return AAddr < BAddr;
209 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000210};
211
Kevin Enderby273ae012013-06-06 17:20:50 +0000212// Types for the storted data in code table that is built before disassembly
213// and the predicate function to sort them.
214typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
215typedef std::vector<DiceTableEntry> DiceTable;
216typedef DiceTable::iterator dice_table_iterator;
217
Kevin Enderby930fdc72014-11-06 19:00:13 +0000218// This is used to search for a data in code table entry for the PC being
219// disassembled. The j parameter has the PC in j.first. A single data in code
220// table entry can cover many bytes for each of its Kind's. So if the offset,
221// aka the i.first value, of the data in code table entry plus its Length
222// covers the PC being searched for this will return true. If not it will
223// return false.
David Majnemerea9b8ee2014-11-04 08:41:48 +0000224static bool compareDiceTableEntries(const DiceTableEntry &i,
225 const DiceTableEntry &j) {
Kevin Enderby930fdc72014-11-06 19:00:13 +0000226 uint16_t Length;
227 i.second.getLength(Length);
228
229 return j.first >= i.first && j.first < i.first + Length;
Kevin Enderby273ae012013-06-06 17:20:50 +0000230}
231
Colin LeMahieufc32b1b2015-03-18 19:27:31 +0000232static uint64_t DumpDataInCode(const uint8_t *bytes, uint64_t Length,
Kevin Enderby930fdc72014-11-06 19:00:13 +0000233 unsigned short Kind) {
234 uint32_t Value, Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000235
236 switch (Kind) {
Kevin Enderby930fdc72014-11-06 19:00:13 +0000237 default:
Charles Davis8bdfafd2013-09-01 04:28:48 +0000238 case MachO::DICE_KIND_DATA:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000239 if (Length >= 4) {
240 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000241 dumpBytes(makeArrayRef(bytes, 4), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000242 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
Kevin Enderby273ae012013-06-06 17:20:50 +0000243 outs() << "\t.long " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000244 Size = 4;
245 } else if (Length >= 2) {
246 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000247 dumpBytes(makeArrayRef(bytes, 2), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000248 Value = bytes[1] << 8 | bytes[0];
Kevin Enderby273ae012013-06-06 17:20:50 +0000249 outs() << "\t.short " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000250 Size = 2;
251 } else {
252 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000253 dumpBytes(makeArrayRef(bytes, 2), outs());
Kevin Enderby273ae012013-06-06 17:20:50 +0000254 Value = bytes[0];
255 outs() << "\t.byte " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000256 Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000257 }
Kevin Enderby930fdc72014-11-06 19:00:13 +0000258 if (Kind == MachO::DICE_KIND_DATA)
259 outs() << "\t@ KIND_DATA\n";
260 else
261 outs() << "\t@ data in code kind = " << Kind << "\n";
Kevin Enderby273ae012013-06-06 17:20:50 +0000262 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000263 case MachO::DICE_KIND_JUMP_TABLE8:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000264 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000265 dumpBytes(makeArrayRef(bytes, 1), outs());
Kevin Enderby273ae012013-06-06 17:20:50 +0000266 Value = bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000267 outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n";
268 Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000269 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000270 case MachO::DICE_KIND_JUMP_TABLE16:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000271 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000272 dumpBytes(makeArrayRef(bytes, 2), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000273 Value = bytes[1] << 8 | bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000274 outs() << "\t.short " << format("%5u", Value & 0xffff)
275 << "\t@ KIND_JUMP_TABLE16\n";
276 Size = 2;
Kevin Enderby273ae012013-06-06 17:20:50 +0000277 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000278 case MachO::DICE_KIND_JUMP_TABLE32:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000279 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
280 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000281 dumpBytes(makeArrayRef(bytes, 4), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000282 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000283 outs() << "\t.long " << Value;
284 if (Kind == MachO::DICE_KIND_JUMP_TABLE32)
285 outs() << "\t@ KIND_JUMP_TABLE32\n";
286 else
287 outs() << "\t@ KIND_ABS_JUMP_TABLE32\n";
288 Size = 4;
Kevin Enderby273ae012013-06-06 17:20:50 +0000289 break;
290 }
Kevin Enderby930fdc72014-11-06 19:00:13 +0000291 return Size;
Kevin Enderby273ae012013-06-06 17:20:50 +0000292}
293
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000294static void getSectionsAndSymbols(MachOObjectFile *MachOObj,
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000295 std::vector<SectionRef> &Sections,
296 std::vector<SymbolRef> &Symbols,
297 SmallVectorImpl<uint64_t> &FoundFns,
298 uint64_t &BaseSegmentAddress) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000299 for (const SymbolRef &Symbol : MachOObj->symbols()) {
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000300 Expected<StringRef> SymName = Symbol.getName();
301 if (!SymName) {
302 std::string Buf;
303 raw_string_ostream OS(Buf);
304 logAllUnhandledErrors(SymName.takeError(), OS, "");
305 OS.flush();
306 report_fatal_error(Buf);
307 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000308 if (!SymName->startswith("ltmp"))
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000309 Symbols.push_back(Symbol);
310 }
Owen Andersond9243c42011-10-17 21:37:35 +0000311
Alexey Samsonov48803e52014-03-13 14:37:36 +0000312 for (const SectionRef &Section : MachOObj->sections()) {
Owen Andersond9243c42011-10-17 21:37:35 +0000313 StringRef SectName;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000314 Section.getName(SectName);
315 Sections.push_back(Section);
Owen Andersond9243c42011-10-17 21:37:35 +0000316 }
317
Kevin Enderby273ae012013-06-06 17:20:50 +0000318 bool BaseSegmentAddressSet = false;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000319 for (const auto &Command : MachOObj->load_commands()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000320 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
Benjamin Kramer699128e2011-09-21 01:13:19 +0000321 // We found a function starts segment, parse the addresses for later
322 // consumption.
Charles Davis8bdfafd2013-09-01 04:28:48 +0000323 MachO::linkedit_data_command LLC =
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000324 MachOObj->getLinkeditDataLoadCommand(Command);
Benjamin Kramer699128e2011-09-21 01:13:19 +0000325
Charles Davis8bdfafd2013-09-01 04:28:48 +0000326 MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000327 } else if (Command.C.cmd == MachO::LC_SEGMENT) {
328 MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000329 StringRef SegName = SLC.segname;
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000330 if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
Kevin Enderby273ae012013-06-06 17:20:50 +0000331 BaseSegmentAddressSet = true;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000332 BaseSegmentAddress = SLC.vmaddr;
Kevin Enderby273ae012013-06-06 17:20:50 +0000333 }
334 }
Benjamin Kramer8a529dc2011-09-21 22:16:43 +0000335 }
Benjamin Kramer699128e2011-09-21 01:13:19 +0000336}
337
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000338static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose,
339 uint32_t n, uint32_t count,
340 uint32_t stride, uint64_t addr) {
341 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
342 uint32_t nindirectsyms = Dysymtab.nindirectsyms;
343 if (n > nindirectsyms)
344 outs() << " (entries start past the end of the indirect symbol "
345 "table) (reserved1 field greater than the table size)";
346 else if (n + count > nindirectsyms)
347 outs() << " (entries extends past the end of the indirect symbol "
348 "table)";
349 outs() << "\n";
350 uint32_t cputype = O->getHeader().cputype;
351 if (cputype & MachO::CPU_ARCH_ABI64)
352 outs() << "address index";
353 else
354 outs() << "address index";
355 if (verbose)
356 outs() << " name\n";
357 else
358 outs() << "\n";
359 for (uint32_t j = 0; j < count && n + j < nindirectsyms; j++) {
360 if (cputype & MachO::CPU_ARCH_ABI64)
361 outs() << format("0x%016" PRIx64, addr + j * stride) << " ";
362 else
Tim Northover43978372016-04-26 18:29:16 +0000363 outs() << format("0x%08" PRIx32, (uint32_t)addr + j * stride) << " ";
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000364 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
365 uint32_t indirect_symbol = O->getIndirectSymbolTableEntry(Dysymtab, n + j);
366 if (indirect_symbol == MachO::INDIRECT_SYMBOL_LOCAL) {
367 outs() << "LOCAL\n";
368 continue;
369 }
370 if (indirect_symbol ==
371 (MachO::INDIRECT_SYMBOL_LOCAL | MachO::INDIRECT_SYMBOL_ABS)) {
372 outs() << "LOCAL ABSOLUTE\n";
373 continue;
374 }
375 if (indirect_symbol == MachO::INDIRECT_SYMBOL_ABS) {
376 outs() << "ABSOLUTE\n";
377 continue;
378 }
379 outs() << format("%5u ", indirect_symbol);
Kevin Enderbyf0640752015-03-13 17:56:32 +0000380 if (verbose) {
381 MachO::symtab_command Symtab = O->getSymtabLoadCommand();
382 if (indirect_symbol < Symtab.nsyms) {
383 symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol);
384 SymbolRef Symbol = *Sym;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000385 Expected<StringRef> SymName = Symbol.getName();
386 if (!SymName) {
387 std::string Buf;
388 raw_string_ostream OS(Buf);
389 logAllUnhandledErrors(SymName.takeError(), OS, "");
390 OS.flush();
391 report_fatal_error(Buf);
392 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000393 outs() << *SymName;
Kevin Enderbyf0640752015-03-13 17:56:32 +0000394 } else {
395 outs() << "?";
396 }
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000397 }
398 outs() << "\n";
399 }
400}
401
402static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) {
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000403 for (const auto &Load : O->load_commands()) {
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000404 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
405 MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
406 for (unsigned J = 0; J < Seg.nsects; ++J) {
407 MachO::section_64 Sec = O->getSection64(Load, J);
408 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
409 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
410 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
411 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
412 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
413 section_type == MachO::S_SYMBOL_STUBS) {
414 uint32_t stride;
415 if (section_type == MachO::S_SYMBOL_STUBS)
416 stride = Sec.reserved2;
417 else
418 stride = 8;
419 if (stride == 0) {
420 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
421 << Sec.sectname << ") "
422 << "(size of stubs in reserved2 field is zero)\n";
423 continue;
424 }
425 uint32_t count = Sec.size / stride;
426 outs() << "Indirect symbols for (" << Sec.segname << ","
427 << Sec.sectname << ") " << count << " entries";
428 uint32_t n = Sec.reserved1;
429 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
430 }
431 }
432 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
433 MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
434 for (unsigned J = 0; J < Seg.nsects; ++J) {
435 MachO::section Sec = O->getSection(Load, J);
436 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
437 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
438 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
439 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
440 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
441 section_type == MachO::S_SYMBOL_STUBS) {
442 uint32_t stride;
443 if (section_type == MachO::S_SYMBOL_STUBS)
444 stride = Sec.reserved2;
445 else
446 stride = 4;
447 if (stride == 0) {
448 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
449 << Sec.sectname << ") "
450 << "(size of stubs in reserved2 field is zero)\n";
451 continue;
452 }
453 uint32_t count = Sec.size / stride;
454 outs() << "Indirect symbols for (" << Sec.segname << ","
455 << Sec.sectname << ") " << count << " entries";
456 uint32_t n = Sec.reserved1;
457 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
458 }
459 }
460 }
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000461 }
462}
463
Kevin Enderby69fe98d2015-01-23 18:52:17 +0000464static void PrintDataInCodeTable(MachOObjectFile *O, bool verbose) {
465 MachO::linkedit_data_command DIC = O->getDataInCodeLoadCommand();
466 uint32_t nentries = DIC.datasize / sizeof(struct MachO::data_in_code_entry);
467 outs() << "Data in code table (" << nentries << " entries)\n";
468 outs() << "offset length kind\n";
469 for (dice_iterator DI = O->begin_dices(), DE = O->end_dices(); DI != DE;
470 ++DI) {
471 uint32_t Offset;
472 DI->getOffset(Offset);
473 outs() << format("0x%08" PRIx32, Offset) << " ";
474 uint16_t Length;
475 DI->getLength(Length);
476 outs() << format("%6u", Length) << " ";
477 uint16_t Kind;
478 DI->getKind(Kind);
479 if (verbose) {
480 switch (Kind) {
481 case MachO::DICE_KIND_DATA:
482 outs() << "DATA";
483 break;
484 case MachO::DICE_KIND_JUMP_TABLE8:
485 outs() << "JUMP_TABLE8";
486 break;
487 case MachO::DICE_KIND_JUMP_TABLE16:
488 outs() << "JUMP_TABLE16";
489 break;
490 case MachO::DICE_KIND_JUMP_TABLE32:
491 outs() << "JUMP_TABLE32";
492 break;
493 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
494 outs() << "ABS_JUMP_TABLE32";
495 break;
496 default:
497 outs() << format("0x%04" PRIx32, Kind);
498 break;
499 }
500 } else
501 outs() << format("0x%04" PRIx32, Kind);
502 outs() << "\n";
503 }
504}
505
Kevin Enderby9a509442015-01-27 21:28:24 +0000506static void PrintLinkOptHints(MachOObjectFile *O) {
507 MachO::linkedit_data_command LohLC = O->getLinkOptHintsLoadCommand();
508 const char *loh = O->getData().substr(LohLC.dataoff, 1).data();
509 uint32_t nloh = LohLC.datasize;
510 outs() << "Linker optimiztion hints (" << nloh << " total bytes)\n";
511 for (uint32_t i = 0; i < nloh;) {
512 unsigned n;
513 uint64_t identifier = decodeULEB128((const uint8_t *)(loh + i), &n);
514 i += n;
515 outs() << " identifier " << identifier << " ";
516 if (i >= nloh)
517 return;
518 switch (identifier) {
519 case 1:
520 outs() << "AdrpAdrp\n";
521 break;
522 case 2:
523 outs() << "AdrpLdr\n";
524 break;
525 case 3:
526 outs() << "AdrpAddLdr\n";
527 break;
528 case 4:
529 outs() << "AdrpLdrGotLdr\n";
530 break;
531 case 5:
532 outs() << "AdrpAddStr\n";
533 break;
534 case 6:
535 outs() << "AdrpLdrGotStr\n";
536 break;
537 case 7:
538 outs() << "AdrpAdd\n";
539 break;
540 case 8:
541 outs() << "AdrpLdrGot\n";
542 break;
543 default:
544 outs() << "Unknown identifier value\n";
545 break;
546 }
547 uint64_t narguments = decodeULEB128((const uint8_t *)(loh + i), &n);
548 i += n;
549 outs() << " narguments " << narguments << "\n";
550 if (i >= nloh)
551 return;
552
553 for (uint32_t j = 0; j < narguments; j++) {
554 uint64_t value = decodeULEB128((const uint8_t *)(loh + i), &n);
555 i += n;
556 outs() << "\tvalue " << format("0x%" PRIx64, value) << "\n";
557 if (i >= nloh)
558 return;
559 }
560 }
561}
562
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000563static void PrintDylibs(MachOObjectFile *O, bool JustId) {
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000564 unsigned Index = 0;
565 for (const auto &Load : O->load_commands()) {
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000566 if ((JustId && Load.C.cmd == MachO::LC_ID_DYLIB) ||
567 (!JustId && (Load.C.cmd == MachO::LC_ID_DYLIB ||
568 Load.C.cmd == MachO::LC_LOAD_DYLIB ||
569 Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
570 Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
571 Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
572 Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB))) {
573 MachO::dylib_command dl = O->getDylibIDLoadCommand(Load);
574 if (dl.dylib.name < dl.cmdsize) {
575 const char *p = (const char *)(Load.Ptr) + dl.dylib.name;
576 if (JustId)
577 outs() << p << "\n";
578 else {
579 outs() << "\t" << p;
580 outs() << " (compatibility version "
581 << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
582 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
583 << (dl.dylib.compatibility_version & 0xff) << ",";
584 outs() << " current version "
585 << ((dl.dylib.current_version >> 16) & 0xffff) << "."
586 << ((dl.dylib.current_version >> 8) & 0xff) << "."
587 << (dl.dylib.current_version & 0xff) << ")\n";
588 }
589 } else {
590 outs() << "\tBad offset (" << dl.dylib.name << ") for name of ";
591 if (Load.C.cmd == MachO::LC_ID_DYLIB)
592 outs() << "LC_ID_DYLIB ";
593 else if (Load.C.cmd == MachO::LC_LOAD_DYLIB)
594 outs() << "LC_LOAD_DYLIB ";
595 else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB)
596 outs() << "LC_LOAD_WEAK_DYLIB ";
597 else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB)
598 outs() << "LC_LAZY_LOAD_DYLIB ";
599 else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB)
600 outs() << "LC_REEXPORT_DYLIB ";
601 else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
602 outs() << "LC_LOAD_UPWARD_DYLIB ";
603 else
604 outs() << "LC_??? ";
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000605 outs() << "command " << Index++ << "\n";
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000606 }
607 }
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000608 }
609}
610
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000611typedef DenseMap<uint64_t, StringRef> SymbolAddressMap;
612
613static void CreateSymbolAddressMap(MachOObjectFile *O,
614 SymbolAddressMap *AddrMap) {
615 // Create a map of symbol addresses to symbol names.
616 for (const SymbolRef &Symbol : O->symbols()) {
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000617 Expected<SymbolRef::Type> STOrErr = Symbol.getType();
618 if (!STOrErr) {
619 std::string Buf;
620 raw_string_ostream OS(Buf);
621 logAllUnhandledErrors(STOrErr.takeError(), OS, "");
622 OS.flush();
623 report_fatal_error(Buf);
624 }
Kevin Enderby5afbc1c2016-03-23 20:27:00 +0000625 SymbolRef::Type ST = *STOrErr;
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000626 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
627 ST == SymbolRef::ST_Other) {
Rafael Espindoladea00162015-07-03 17:44:18 +0000628 uint64_t Address = Symbol.getValue();
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000629 Expected<StringRef> SymNameOrErr = Symbol.getName();
630 if (!SymNameOrErr) {
631 std::string Buf;
632 raw_string_ostream OS(Buf);
633 logAllUnhandledErrors(SymNameOrErr.takeError(), OS, "");
634 OS.flush();
635 report_fatal_error(Buf);
636 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000637 StringRef SymName = *SymNameOrErr;
Kevin Enderby846c0002015-04-16 17:19:59 +0000638 if (!SymName.startswith(".objc"))
639 (*AddrMap)[Address] = SymName;
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000640 }
641 }
642}
643
644// GuessSymbolName is passed the address of what might be a symbol and a
645// pointer to the SymbolAddressMap. It returns the name of a symbol
646// with that address or nullptr if no symbol is found with that address.
647static const char *GuessSymbolName(uint64_t value, SymbolAddressMap *AddrMap) {
648 const char *SymbolName = nullptr;
649 // A DenseMap can't lookup up some values.
650 if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) {
651 StringRef name = AddrMap->lookup(value);
652 if (!name.empty())
653 SymbolName = name.data();
654 }
655 return SymbolName;
656}
657
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000658static void DumpCstringChar(const char c) {
659 char p[2];
660 p[0] = c;
661 p[1] = '\0';
662 outs().write_escaped(p);
663}
664
Kevin Enderby10ba0412015-02-04 21:38:42 +0000665static void DumpCstringSection(MachOObjectFile *O, const char *sect,
666 uint32_t sect_size, uint64_t sect_addr,
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000667 bool print_addresses) {
Kevin Enderby10ba0412015-02-04 21:38:42 +0000668 for (uint32_t i = 0; i < sect_size; i++) {
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000669 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 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000675 for (; i < sect_size && sect[i] != '\0'; i++)
676 DumpCstringChar(sect[i]);
Kevin Enderby10ba0412015-02-04 21:38:42 +0000677 if (i < sect_size && sect[i] == '\0')
678 outs() << "\n";
679 }
680}
681
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000682static void DumpLiteral4(uint32_t l, float f) {
683 outs() << format("0x%08" PRIx32, l);
684 if ((l & 0x7f800000) != 0x7f800000)
685 outs() << format(" (%.16e)\n", f);
686 else {
687 if (l == 0x7f800000)
688 outs() << " (+Infinity)\n";
689 else if (l == 0xff800000)
690 outs() << " (-Infinity)\n";
691 else if ((l & 0x00400000) == 0x00400000)
692 outs() << " (non-signaling Not-a-Number)\n";
693 else
694 outs() << " (signaling Not-a-Number)\n";
695 }
696}
697
698static void DumpLiteral4Section(MachOObjectFile *O, const char *sect,
699 uint32_t sect_size, uint64_t sect_addr,
700 bool print_addresses) {
701 for (uint32_t i = 0; i < sect_size; i += sizeof(float)) {
702 if (print_addresses) {
703 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000704 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000705 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000706 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000707 }
708 float f;
709 memcpy(&f, sect + i, sizeof(float));
710 if (O->isLittleEndian() != sys::IsLittleEndianHost)
711 sys::swapByteOrder(f);
712 uint32_t l;
713 memcpy(&l, sect + i, sizeof(uint32_t));
714 if (O->isLittleEndian() != sys::IsLittleEndianHost)
715 sys::swapByteOrder(l);
716 DumpLiteral4(l, f);
717 }
718}
719
720static void DumpLiteral8(MachOObjectFile *O, uint32_t l0, uint32_t l1,
721 double d) {
722 outs() << format("0x%08" PRIx32, l0) << " " << format("0x%08" PRIx32, l1);
723 uint32_t Hi, Lo;
Davide Italianob627d9f2015-12-12 21:50:11 +0000724 Hi = (O->isLittleEndian()) ? l1 : l0;
725 Lo = (O->isLittleEndian()) ? l0 : l1;
726
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000727 // Hi is the high word, so this is equivalent to if(isfinite(d))
728 if ((Hi & 0x7ff00000) != 0x7ff00000)
729 outs() << format(" (%.16e)\n", d);
730 else {
731 if (Hi == 0x7ff00000 && Lo == 0)
732 outs() << " (+Infinity)\n";
733 else if (Hi == 0xfff00000 && Lo == 0)
734 outs() << " (-Infinity)\n";
735 else if ((Hi & 0x00080000) == 0x00080000)
736 outs() << " (non-signaling Not-a-Number)\n";
737 else
738 outs() << " (signaling Not-a-Number)\n";
739 }
740}
741
742static void DumpLiteral8Section(MachOObjectFile *O, const char *sect,
743 uint32_t sect_size, uint64_t sect_addr,
744 bool print_addresses) {
745 for (uint32_t i = 0; i < sect_size; i += sizeof(double)) {
746 if (print_addresses) {
747 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000748 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000749 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000750 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000751 }
752 double d;
753 memcpy(&d, sect + i, sizeof(double));
754 if (O->isLittleEndian() != sys::IsLittleEndianHost)
755 sys::swapByteOrder(d);
756 uint32_t l0, l1;
757 memcpy(&l0, sect + i, sizeof(uint32_t));
758 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
759 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
760 sys::swapByteOrder(l0);
761 sys::swapByteOrder(l1);
762 }
763 DumpLiteral8(O, l0, l1, d);
764 }
765}
766
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000767static void DumpLiteral16(uint32_t l0, uint32_t l1, uint32_t l2, uint32_t l3) {
768 outs() << format("0x%08" PRIx32, l0) << " ";
769 outs() << format("0x%08" PRIx32, l1) << " ";
770 outs() << format("0x%08" PRIx32, l2) << " ";
771 outs() << format("0x%08" PRIx32, l3) << "\n";
772}
773
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000774static void DumpLiteral16Section(MachOObjectFile *O, const char *sect,
775 uint32_t sect_size, uint64_t sect_addr,
776 bool print_addresses) {
777 for (uint32_t i = 0; i < sect_size; i += 16) {
778 if (print_addresses) {
779 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000780 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000781 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000782 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000783 }
784 uint32_t l0, l1, l2, l3;
785 memcpy(&l0, sect + i, sizeof(uint32_t));
786 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
787 memcpy(&l2, sect + i + 2 * sizeof(uint32_t), sizeof(uint32_t));
788 memcpy(&l3, sect + i + 3 * sizeof(uint32_t), sizeof(uint32_t));
789 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
790 sys::swapByteOrder(l0);
791 sys::swapByteOrder(l1);
792 sys::swapByteOrder(l2);
793 sys::swapByteOrder(l3);
794 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000795 DumpLiteral16(l0, l1, l2, l3);
796 }
797}
798
799static void DumpLiteralPointerSection(MachOObjectFile *O,
800 const SectionRef &Section,
801 const char *sect, uint32_t sect_size,
802 uint64_t sect_addr,
803 bool print_addresses) {
804 // Collect the literal sections in this Mach-O file.
805 std::vector<SectionRef> LiteralSections;
806 for (const SectionRef &Section : O->sections()) {
807 DataRefImpl Ref = Section.getRawDataRefImpl();
808 uint32_t section_type;
809 if (O->is64Bit()) {
810 const MachO::section_64 Sec = O->getSection64(Ref);
811 section_type = Sec.flags & MachO::SECTION_TYPE;
812 } else {
813 const MachO::section Sec = O->getSection(Ref);
814 section_type = Sec.flags & MachO::SECTION_TYPE;
815 }
816 if (section_type == MachO::S_CSTRING_LITERALS ||
817 section_type == MachO::S_4BYTE_LITERALS ||
818 section_type == MachO::S_8BYTE_LITERALS ||
819 section_type == MachO::S_16BYTE_LITERALS)
820 LiteralSections.push_back(Section);
821 }
822
823 // Set the size of the literal pointer.
824 uint32_t lp_size = O->is64Bit() ? 8 : 4;
825
Eric Christopher572e03a2015-06-19 01:53:21 +0000826 // Collect the external relocation symbols for the literal pointers.
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000827 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
828 for (const RelocationRef &Reloc : Section.relocations()) {
829 DataRefImpl Rel;
830 MachO::any_relocation_info RE;
831 bool isExtern = false;
832 Rel = Reloc.getRawDataRefImpl();
833 RE = O->getRelocation(Rel);
834 isExtern = O->getPlainRelocationExternal(RE);
835 if (isExtern) {
Rafael Espindola96d071c2015-06-29 23:29:12 +0000836 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000837 symbol_iterator RelocSym = Reloc.getSymbol();
838 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
839 }
840 }
841 array_pod_sort(Relocs.begin(), Relocs.end());
842
843 // Dump each literal pointer.
844 for (uint32_t i = 0; i < sect_size; i += lp_size) {
845 if (print_addresses) {
846 if (O->is64Bit())
847 outs() << format("%016" PRIx64, sect_addr + i) << " ";
848 else
849 outs() << format("%08" PRIx64, sect_addr + i) << " ";
850 }
851 uint64_t lp;
852 if (O->is64Bit()) {
853 memcpy(&lp, sect + i, sizeof(uint64_t));
854 if (O->isLittleEndian() != sys::IsLittleEndianHost)
855 sys::swapByteOrder(lp);
856 } else {
857 uint32_t li;
858 memcpy(&li, sect + i, sizeof(uint32_t));
859 if (O->isLittleEndian() != sys::IsLittleEndianHost)
860 sys::swapByteOrder(li);
861 lp = li;
862 }
863
864 // First look for an external relocation entry for this literal pointer.
David Blaikie33dd45d02015-03-23 18:39:02 +0000865 auto Reloc = std::find_if(
866 Relocs.begin(), Relocs.end(),
867 [&](const std::pair<uint64_t, SymbolRef> &P) { return P.first == i; });
868 if (Reloc != Relocs.end()) {
869 symbol_iterator RelocSym = Reloc->second;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000870 Expected<StringRef> SymName = RelocSym->getName();
871 if (!SymName) {
872 std::string Buf;
873 raw_string_ostream OS(Buf);
874 logAllUnhandledErrors(SymName.takeError(), OS, "");
875 OS.flush();
876 report_fatal_error(Buf);
877 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000878 outs() << "external relocation entry for symbol:" << *SymName << "\n";
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000879 continue;
David Blaikie33dd45d02015-03-23 18:39:02 +0000880 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000881
882 // For local references see what the section the literal pointer points to.
David Blaikie33dd45d02015-03-23 18:39:02 +0000883 auto Sect = std::find_if(LiteralSections.begin(), LiteralSections.end(),
884 [&](const SectionRef &R) {
885 return lp >= R.getAddress() &&
886 lp < R.getAddress() + R.getSize();
887 });
888 if (Sect == LiteralSections.end()) {
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000889 outs() << format("0x%" PRIx64, lp) << " (not in a literal section)\n";
David Blaikie33dd45d02015-03-23 18:39:02 +0000890 continue;
891 }
892
893 uint64_t SectAddress = Sect->getAddress();
894 uint64_t SectSize = Sect->getSize();
895
896 StringRef SectName;
897 Sect->getName(SectName);
898 DataRefImpl Ref = Sect->getRawDataRefImpl();
899 StringRef SegmentName = O->getSectionFinalSegmentName(Ref);
900 outs() << SegmentName << ":" << SectName << ":";
901
902 uint32_t section_type;
903 if (O->is64Bit()) {
904 const MachO::section_64 Sec = O->getSection64(Ref);
905 section_type = Sec.flags & MachO::SECTION_TYPE;
906 } else {
907 const MachO::section Sec = O->getSection(Ref);
908 section_type = Sec.flags & MachO::SECTION_TYPE;
909 }
910
911 StringRef BytesStr;
912 Sect->getContents(BytesStr);
913 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
914
915 switch (section_type) {
916 case MachO::S_CSTRING_LITERALS:
917 for (uint64_t i = lp - SectAddress; i < SectSize && Contents[i] != '\0';
918 i++) {
919 DumpCstringChar(Contents[i]);
920 }
921 outs() << "\n";
922 break;
923 case MachO::S_4BYTE_LITERALS:
924 float f;
925 memcpy(&f, Contents + (lp - SectAddress), sizeof(float));
926 uint32_t l;
927 memcpy(&l, Contents + (lp - SectAddress), sizeof(uint32_t));
928 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
929 sys::swapByteOrder(f);
930 sys::swapByteOrder(l);
931 }
932 DumpLiteral4(l, f);
933 break;
934 case MachO::S_8BYTE_LITERALS: {
935 double d;
936 memcpy(&d, Contents + (lp - SectAddress), sizeof(double));
937 uint32_t l0, l1;
938 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
939 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
940 sizeof(uint32_t));
941 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
942 sys::swapByteOrder(f);
943 sys::swapByteOrder(l0);
944 sys::swapByteOrder(l1);
945 }
946 DumpLiteral8(O, l0, l1, d);
947 break;
948 }
949 case MachO::S_16BYTE_LITERALS: {
950 uint32_t l0, l1, l2, l3;
951 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
952 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
953 sizeof(uint32_t));
954 memcpy(&l2, Contents + (lp - SectAddress) + 2 * sizeof(uint32_t),
955 sizeof(uint32_t));
956 memcpy(&l3, Contents + (lp - SectAddress) + 3 * sizeof(uint32_t),
957 sizeof(uint32_t));
958 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
959 sys::swapByteOrder(l0);
960 sys::swapByteOrder(l1);
961 sys::swapByteOrder(l2);
962 sys::swapByteOrder(l3);
963 }
964 DumpLiteral16(l0, l1, l2, l3);
965 break;
966 }
967 }
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000968 }
969}
970
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000971static void DumpInitTermPointerSection(MachOObjectFile *O, const char *sect,
972 uint32_t sect_size, uint64_t sect_addr,
973 SymbolAddressMap *AddrMap,
974 bool verbose) {
975 uint32_t stride;
Davide Italiano3eb47e22015-12-15 23:14:21 +0000976 stride = (O->is64Bit()) ? sizeof(uint64_t) : sizeof(uint32_t);
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000977 for (uint32_t i = 0; i < sect_size; i += stride) {
978 const char *SymbolName = nullptr;
979 if (O->is64Bit()) {
980 outs() << format("0x%016" PRIx64, sect_addr + i * stride) << " ";
981 uint64_t pointer_value;
982 memcpy(&pointer_value, sect + i, stride);
983 if (O->isLittleEndian() != sys::IsLittleEndianHost)
984 sys::swapByteOrder(pointer_value);
985 outs() << format("0x%016" PRIx64, pointer_value);
986 if (verbose)
987 SymbolName = GuessSymbolName(pointer_value, AddrMap);
988 } else {
989 outs() << format("0x%08" PRIx64, sect_addr + i * stride) << " ";
990 uint32_t pointer_value;
991 memcpy(&pointer_value, sect + i, stride);
992 if (O->isLittleEndian() != sys::IsLittleEndianHost)
993 sys::swapByteOrder(pointer_value);
994 outs() << format("0x%08" PRIx32, pointer_value);
995 if (verbose)
996 SymbolName = GuessSymbolName(pointer_value, AddrMap);
997 }
998 if (SymbolName)
999 outs() << " " << SymbolName;
1000 outs() << "\n";
1001 }
1002}
1003
1004static void DumpRawSectionContents(MachOObjectFile *O, const char *sect,
1005 uint32_t size, uint64_t addr) {
1006 uint32_t cputype = O->getHeader().cputype;
1007 if (cputype == MachO::CPU_TYPE_I386 || cputype == MachO::CPU_TYPE_X86_64) {
1008 uint32_t j;
1009 for (uint32_t i = 0; i < size; i += j, addr += j) {
1010 if (O->is64Bit())
1011 outs() << format("%016" PRIx64, addr) << "\t";
1012 else
Kevin Enderbyf0640752015-03-13 17:56:32 +00001013 outs() << format("%08" PRIx64, addr) << "\t";
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001014 for (j = 0; j < 16 && i + j < size; j++) {
1015 uint8_t byte_word = *(sect + i + j);
1016 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
1017 }
1018 outs() << "\n";
1019 }
1020 } else {
1021 uint32_t j;
1022 for (uint32_t i = 0; i < size; i += j, addr += j) {
1023 if (O->is64Bit())
1024 outs() << format("%016" PRIx64, addr) << "\t";
1025 else
Kevin Enderbyc4930852016-04-27 22:36:18 +00001026 outs() << format("%08" PRIx64, addr) << "\t";
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001027 for (j = 0; j < 4 * sizeof(int32_t) && i + j < size;
1028 j += sizeof(int32_t)) {
Kevin Enderby8eccdad2016-04-27 23:43:00 +00001029 if (i + j + sizeof(int32_t) <= size) {
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001030 uint32_t long_word;
1031 memcpy(&long_word, sect + i + j, sizeof(int32_t));
1032 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1033 sys::swapByteOrder(long_word);
1034 outs() << format("%08" PRIx32, long_word) << " ";
1035 } else {
1036 for (uint32_t k = 0; i + j + k < size; k++) {
Kevin Enderby8eccdad2016-04-27 23:43:00 +00001037 uint8_t byte_word = *(sect + i + j + k);
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001038 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
1039 }
1040 }
1041 }
1042 outs() << "\n";
1043 }
1044 }
1045}
1046
Kevin Enderby95df54c2015-02-04 01:01:38 +00001047static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
1048 StringRef DisSegName, StringRef DisSectName);
Kevin Enderby4ad9bde2015-04-16 22:33:20 +00001049static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
1050 uint32_t size, uint32_t addr);
Kevin Enderby9873e2c2016-05-23 21:34:12 +00001051#ifdef HAVE_LIBXAR
1052static void DumpBitcodeSection(MachOObjectFile *O, const char *sect,
1053 uint32_t size, bool verbose,
1054 bool PrintXarHeader, bool PrintXarFileHeaders,
1055 std::string XarMemberName);
1056#endif // defined(HAVE_LIBXAR)
Kevin Enderby95df54c2015-02-04 01:01:38 +00001057
1058static void DumpSectionContents(StringRef Filename, MachOObjectFile *O,
1059 bool verbose) {
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001060 SymbolAddressMap AddrMap;
1061 if (verbose)
1062 CreateSymbolAddressMap(O, &AddrMap);
1063
Colin LeMahieufcc32762015-07-29 19:08:10 +00001064 for (unsigned i = 0; i < FilterSections.size(); ++i) {
1065 StringRef DumpSection = FilterSections[i];
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001066 std::pair<StringRef, StringRef> DumpSegSectName;
1067 DumpSegSectName = DumpSection.split(',');
1068 StringRef DumpSegName, DumpSectName;
1069 if (DumpSegSectName.second.size()) {
1070 DumpSegName = DumpSegSectName.first;
1071 DumpSectName = DumpSegSectName.second;
1072 } else {
1073 DumpSegName = "";
1074 DumpSectName = DumpSegSectName.first;
1075 }
1076 for (const SectionRef &Section : O->sections()) {
1077 StringRef SectName;
1078 Section.getName(SectName);
1079 DataRefImpl Ref = Section.getRawDataRefImpl();
1080 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1081 if ((DumpSegName.empty() || SegName == DumpSegName) &&
1082 (SectName == DumpSectName)) {
Adrian Prantlc2401dd2015-03-27 17:31:15 +00001083
Kevin Enderby95df54c2015-02-04 01:01:38 +00001084 uint32_t section_flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001085 if (O->is64Bit()) {
1086 const MachO::section_64 Sec = O->getSection64(Ref);
Kevin Enderby95df54c2015-02-04 01:01:38 +00001087 section_flags = Sec.flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001088
1089 } else {
1090 const MachO::section Sec = O->getSection(Ref);
Kevin Enderby95df54c2015-02-04 01:01:38 +00001091 section_flags = Sec.flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001092 }
Kevin Enderby95df54c2015-02-04 01:01:38 +00001093 uint32_t section_type = section_flags & MachO::SECTION_TYPE;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001094
1095 StringRef BytesStr;
1096 Section.getContents(BytesStr);
1097 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1098 uint32_t sect_size = BytesStr.size();
1099 uint64_t sect_addr = Section.getAddress();
1100
Adrian Prantlc2401dd2015-03-27 17:31:15 +00001101 outs() << "Contents of (" << SegName << "," << SectName
1102 << ") section\n";
1103
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001104 if (verbose) {
Kevin Enderby95df54c2015-02-04 01:01:38 +00001105 if ((section_flags & MachO::S_ATTR_PURE_INSTRUCTIONS) ||
1106 (section_flags & MachO::S_ATTR_SOME_INSTRUCTIONS)) {
1107 DisassembleMachO(Filename, O, SegName, SectName);
1108 continue;
1109 }
Kevin Enderbycd66be52015-03-11 22:06:32 +00001110 if (SegName == "__TEXT" && SectName == "__info_plist") {
1111 outs() << sect;
1112 continue;
1113 }
Kevin Enderby4ad9bde2015-04-16 22:33:20 +00001114 if (SegName == "__OBJC" && SectName == "__protocol") {
1115 DumpProtocolSection(O, sect, sect_size, sect_addr);
1116 continue;
1117 }
Kevin Enderby9873e2c2016-05-23 21:34:12 +00001118#ifdef HAVE_LIBXAR
1119 if (SegName == "__LLVM" && SectName == "__bundle") {
1120 DumpBitcodeSection(O, sect, sect_size, verbose, !NoSymbolicOperands,
1121 ArchiveHeaders, "");
1122 continue;
1123 }
1124#endif // defined(HAVE_LIBXAR)
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001125 switch (section_type) {
1126 case MachO::S_REGULAR:
1127 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1128 break;
1129 case MachO::S_ZEROFILL:
1130 outs() << "zerofill section and has no contents in the file\n";
1131 break;
Kevin Enderby10ba0412015-02-04 21:38:42 +00001132 case MachO::S_CSTRING_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001133 DumpCstringSection(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby10ba0412015-02-04 21:38:42 +00001134 break;
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001135 case MachO::S_4BYTE_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001136 DumpLiteral4Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001137 break;
1138 case MachO::S_8BYTE_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001139 DumpLiteral8Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001140 break;
1141 case MachO::S_16BYTE_LITERALS:
Kevin Enderby0fc11822015-04-01 20:57:01 +00001142 DumpLiteral16Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1143 break;
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001144 case MachO::S_LITERAL_POINTERS:
1145 DumpLiteralPointerSection(O, Section, sect, sect_size, sect_addr,
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001146 !NoLeadingAddr);
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001147 break;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001148 case MachO::S_MOD_INIT_FUNC_POINTERS:
1149 case MachO::S_MOD_TERM_FUNC_POINTERS:
1150 DumpInitTermPointerSection(O, sect, sect_size, sect_addr, &AddrMap,
1151 verbose);
1152 break;
1153 default:
1154 outs() << "Unknown section type ("
1155 << format("0x%08" PRIx32, section_type) << ")\n";
1156 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1157 break;
1158 }
1159 } else {
1160 if (section_type == MachO::S_ZEROFILL)
1161 outs() << "zerofill section and has no contents in the file\n";
1162 else
1163 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1164 }
1165 }
1166 }
1167 }
1168}
1169
Kevin Enderbycd66be52015-03-11 22:06:32 +00001170static void DumpInfoPlistSectionContents(StringRef Filename,
1171 MachOObjectFile *O) {
1172 for (const SectionRef &Section : O->sections()) {
1173 StringRef SectName;
1174 Section.getName(SectName);
1175 DataRefImpl Ref = Section.getRawDataRefImpl();
1176 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1177 if (SegName == "__TEXT" && SectName == "__info_plist") {
1178 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
1179 StringRef BytesStr;
1180 Section.getContents(BytesStr);
1181 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1182 outs() << sect;
1183 return;
1184 }
1185 }
1186}
1187
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001188// checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file
1189// and if it is and there is a list of architecture flags is specified then
1190// check to make sure this Mach-O file is one of those architectures or all
1191// architectures were specified. If not then an error is generated and this
1192// routine returns false. Else it returns true.
1193static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
1194 if (isa<MachOObjectFile>(O) && !ArchAll && ArchFlags.size() != 0) {
1195 MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(O);
1196 bool ArchFound = false;
1197 MachO::mach_header H;
1198 MachO::mach_header_64 H_64;
1199 Triple T;
1200 if (MachO->is64Bit()) {
1201 H_64 = MachO->MachOObjectFile::getHeader64();
Tim Northover9e8eb412016-04-22 23:21:13 +00001202 T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001203 } else {
1204 H = MachO->MachOObjectFile::getHeader();
Tim Northover9e8eb412016-04-22 23:21:13 +00001205 T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001206 }
1207 unsigned i;
1208 for (i = 0; i < ArchFlags.size(); ++i) {
1209 if (ArchFlags[i] == T.getArchName())
1210 ArchFound = true;
1211 break;
1212 }
1213 if (!ArchFound) {
1214 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1215 << "architecture: " + ArchFlags[i] + "\n";
1216 return false;
1217 }
1218 }
1219 return true;
1220}
1221
Kevin Enderby0fc11822015-04-01 20:57:01 +00001222static void printObjcMetaData(MachOObjectFile *O, bool verbose);
1223
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001224// ProcessMachO() is passed a single opened Mach-O file, which may be an
1225// archive member and or in a slice of a universal file. It prints the
1226// the file name and header info and then processes it according to the
1227// command line options.
1228static void ProcessMachO(StringRef Filename, MachOObjectFile *MachOOF,
1229 StringRef ArchiveMemberName = StringRef(),
1230 StringRef ArchitectureName = StringRef()) {
Kevin Enderby131d1772015-01-09 19:22:37 +00001231 // If we are doing some processing here on the Mach-O file print the header
1232 // info. And don't print it otherwise like in the case of printing the
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +00001233 // UniversalHeaders or ArchiveHeaders.
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001234 if (Disassemble || PrivateHeaders || ExportsTrie || Rebase || Bind || SymbolTable ||
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001235 LazyBind || WeakBind || IndirectSymbols || DataInCode || LinkOptHints ||
Colin LeMahieufcc32762015-07-29 19:08:10 +00001236 DylibsUsed || DylibId || ObjcMetaData || (FilterSections.size() != 0)) {
Kevin Enderby131d1772015-01-09 19:22:37 +00001237 outs() << Filename;
1238 if (!ArchiveMemberName.empty())
1239 outs() << '(' << ArchiveMemberName << ')';
1240 if (!ArchitectureName.empty())
1241 outs() << " (architecture " << ArchitectureName << ")";
1242 outs() << ":\n";
1243 }
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001244
1245 if (Disassemble)
Kevin Enderby95df54c2015-02-04 01:01:38 +00001246 DisassembleMachO(Filename, MachOOF, "__TEXT", "__text");
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +00001247 if (IndirectSymbols)
Kevin Enderbyf0640752015-03-13 17:56:32 +00001248 PrintIndirectSymbols(MachOOF, !NonVerbose);
Kevin Enderby69fe98d2015-01-23 18:52:17 +00001249 if (DataInCode)
Kevin Enderbyf0640752015-03-13 17:56:32 +00001250 PrintDataInCodeTable(MachOOF, !NonVerbose);
Kevin Enderby9a509442015-01-27 21:28:24 +00001251 if (LinkOptHints)
1252 PrintLinkOptHints(MachOOF);
Kevin Enderby98da6132015-01-20 21:47:46 +00001253 if (Relocations)
1254 PrintRelocations(MachOOF);
1255 if (SectionHeaders)
1256 PrintSectionHeaders(MachOOF);
1257 if (SectionContents)
1258 PrintSectionContents(MachOOF);
Colin LeMahieufcc32762015-07-29 19:08:10 +00001259 if (FilterSections.size() != 0)
Kevin Enderbyf0640752015-03-13 17:56:32 +00001260 DumpSectionContents(Filename, MachOOF, !NonVerbose);
Kevin Enderbycd66be52015-03-11 22:06:32 +00001261 if (InfoPlist)
1262 DumpInfoPlistSectionContents(Filename, MachOOF);
Kevin Enderbybc847fa2015-03-16 20:08:09 +00001263 if (DylibsUsed)
1264 PrintDylibs(MachOOF, false);
1265 if (DylibId)
1266 PrintDylibs(MachOOF, true);
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001267 if (SymbolTable) {
1268 StringRef ArchiveName = ArchiveMemberName == StringRef() ? "" : Filename;
Kevin Enderby9acb1092016-05-31 20:35:34 +00001269 PrintSymbolTable(MachOOF, ArchiveName, ArchitectureName);
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001270 }
Kevin Enderby98da6132015-01-20 21:47:46 +00001271 if (UnwindInfo)
1272 printMachOUnwindInfo(MachOOF);
Kevin Enderby0ae163f2016-01-13 00:25:36 +00001273 if (PrivateHeaders) {
1274 printMachOFileHeader(MachOOF);
1275 printMachOLoadCommands(MachOOF);
1276 }
1277 if (FirstPrivateHeader)
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001278 printMachOFileHeader(MachOOF);
Kevin Enderby0fc11822015-04-01 20:57:01 +00001279 if (ObjcMetaData)
1280 printObjcMetaData(MachOOF, !NonVerbose);
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001281 if (ExportsTrie)
1282 printExportsTrie(MachOOF);
1283 if (Rebase)
1284 printRebaseTable(MachOOF);
1285 if (Bind)
1286 printBindTable(MachOOF);
1287 if (LazyBind)
1288 printLazyBindTable(MachOOF);
1289 if (WeakBind)
1290 printWeakBindTable(MachOOF);
Igor Laevsky03a670c2016-01-26 15:09:42 +00001291
1292 if (DwarfDumpType != DIDT_Null) {
1293 std::unique_ptr<DIContext> DICtx(new DWARFContextInMemory(*MachOOF));
1294 // Dump the complete DWARF structure.
1295 DICtx->dump(outs(), DwarfDumpType, true /* DumpEH */);
1296 }
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001297}
1298
Kevin Enderby131d1772015-01-09 19:22:37 +00001299// printUnknownCPUType() helps print_fat_headers for unknown CPU's.
1300static void printUnknownCPUType(uint32_t cputype, uint32_t cpusubtype) {
1301 outs() << " cputype (" << cputype << ")\n";
1302 outs() << " cpusubtype (" << cpusubtype << ")\n";
1303}
1304
1305// printCPUType() helps print_fat_headers by printing the cputype and
1306// pusubtype (symbolically for the one's it knows about).
1307static void printCPUType(uint32_t cputype, uint32_t cpusubtype) {
1308 switch (cputype) {
1309 case MachO::CPU_TYPE_I386:
1310 switch (cpusubtype) {
1311 case MachO::CPU_SUBTYPE_I386_ALL:
1312 outs() << " cputype CPU_TYPE_I386\n";
1313 outs() << " cpusubtype CPU_SUBTYPE_I386_ALL\n";
1314 break;
1315 default:
1316 printUnknownCPUType(cputype, cpusubtype);
1317 break;
1318 }
1319 break;
1320 case MachO::CPU_TYPE_X86_64:
1321 switch (cpusubtype) {
1322 case MachO::CPU_SUBTYPE_X86_64_ALL:
1323 outs() << " cputype CPU_TYPE_X86_64\n";
1324 outs() << " cpusubtype CPU_SUBTYPE_X86_64_ALL\n";
1325 break;
1326 case MachO::CPU_SUBTYPE_X86_64_H:
1327 outs() << " cputype CPU_TYPE_X86_64\n";
1328 outs() << " cpusubtype CPU_SUBTYPE_X86_64_H\n";
1329 break;
1330 default:
1331 printUnknownCPUType(cputype, cpusubtype);
1332 break;
1333 }
1334 break;
1335 case MachO::CPU_TYPE_ARM:
1336 switch (cpusubtype) {
1337 case MachO::CPU_SUBTYPE_ARM_ALL:
1338 outs() << " cputype CPU_TYPE_ARM\n";
1339 outs() << " cpusubtype CPU_SUBTYPE_ARM_ALL\n";
1340 break;
1341 case MachO::CPU_SUBTYPE_ARM_V4T:
1342 outs() << " cputype CPU_TYPE_ARM\n";
1343 outs() << " cpusubtype CPU_SUBTYPE_ARM_V4T\n";
1344 break;
1345 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
1346 outs() << " cputype CPU_TYPE_ARM\n";
1347 outs() << " cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n";
1348 break;
1349 case MachO::CPU_SUBTYPE_ARM_XSCALE:
1350 outs() << " cputype CPU_TYPE_ARM\n";
1351 outs() << " cpusubtype CPU_SUBTYPE_ARM_XSCALE\n";
1352 break;
1353 case MachO::CPU_SUBTYPE_ARM_V6:
1354 outs() << " cputype CPU_TYPE_ARM\n";
1355 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6\n";
1356 break;
1357 case MachO::CPU_SUBTYPE_ARM_V6M:
1358 outs() << " cputype CPU_TYPE_ARM\n";
1359 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6M\n";
1360 break;
1361 case MachO::CPU_SUBTYPE_ARM_V7:
1362 outs() << " cputype CPU_TYPE_ARM\n";
1363 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7\n";
1364 break;
1365 case MachO::CPU_SUBTYPE_ARM_V7EM:
1366 outs() << " cputype CPU_TYPE_ARM\n";
1367 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7EM\n";
1368 break;
1369 case MachO::CPU_SUBTYPE_ARM_V7K:
1370 outs() << " cputype CPU_TYPE_ARM\n";
1371 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7K\n";
1372 break;
1373 case MachO::CPU_SUBTYPE_ARM_V7M:
1374 outs() << " cputype CPU_TYPE_ARM\n";
1375 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7M\n";
1376 break;
1377 case MachO::CPU_SUBTYPE_ARM_V7S:
1378 outs() << " cputype CPU_TYPE_ARM\n";
1379 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7S\n";
1380 break;
1381 default:
1382 printUnknownCPUType(cputype, cpusubtype);
1383 break;
1384 }
1385 break;
1386 case MachO::CPU_TYPE_ARM64:
1387 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1388 case MachO::CPU_SUBTYPE_ARM64_ALL:
1389 outs() << " cputype CPU_TYPE_ARM64\n";
1390 outs() << " cpusubtype CPU_SUBTYPE_ARM64_ALL\n";
1391 break;
1392 default:
1393 printUnknownCPUType(cputype, cpusubtype);
1394 break;
1395 }
1396 break;
1397 default:
1398 printUnknownCPUType(cputype, cpusubtype);
1399 break;
1400 }
1401}
1402
1403static void printMachOUniversalHeaders(const object::MachOUniversalBinary *UB,
1404 bool verbose) {
1405 outs() << "Fat headers\n";
Kevin Enderby606a3382016-06-21 21:55:01 +00001406 if (verbose) {
1407 if (UB->getMagic() == MachO::FAT_MAGIC)
1408 outs() << "fat_magic FAT_MAGIC\n";
1409 else // UB->getMagic() == MachO::FAT_MAGIC_64
1410 outs() << "fat_magic FAT_MAGIC_64\n";
1411 } else
Kevin Enderby131d1772015-01-09 19:22:37 +00001412 outs() << "fat_magic " << format("0x%" PRIx32, MachO::FAT_MAGIC) << "\n";
1413
1414 uint32_t nfat_arch = UB->getNumberOfObjects();
1415 StringRef Buf = UB->getData();
1416 uint64_t size = Buf.size();
1417 uint64_t big_size = sizeof(struct MachO::fat_header) +
1418 nfat_arch * sizeof(struct MachO::fat_arch);
1419 outs() << "nfat_arch " << UB->getNumberOfObjects();
1420 if (nfat_arch == 0)
1421 outs() << " (malformed, contains zero architecture types)\n";
1422 else if (big_size > size)
1423 outs() << " (malformed, architectures past end of file)\n";
1424 else
1425 outs() << "\n";
1426
1427 for (uint32_t i = 0; i < nfat_arch; ++i) {
1428 MachOUniversalBinary::ObjectForArch OFA(UB, i);
1429 uint32_t cputype = OFA.getCPUType();
1430 uint32_t cpusubtype = OFA.getCPUSubType();
1431 outs() << "architecture ";
1432 for (uint32_t j = 0; i != 0 && j <= i - 1; j++) {
1433 MachOUniversalBinary::ObjectForArch other_OFA(UB, j);
1434 uint32_t other_cputype = other_OFA.getCPUType();
1435 uint32_t other_cpusubtype = other_OFA.getCPUSubType();
Kevin Enderby0512bd72015-01-09 21:55:03 +00001436 if (cputype != 0 && cpusubtype != 0 && cputype == other_cputype &&
Kevin Enderby131d1772015-01-09 19:22:37 +00001437 (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) ==
Kevin Enderby0512bd72015-01-09 21:55:03 +00001438 (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK)) {
Kevin Enderby131d1772015-01-09 19:22:37 +00001439 outs() << "(illegal duplicate architecture) ";
1440 break;
Kevin Enderby0512bd72015-01-09 21:55:03 +00001441 }
Kevin Enderby131d1772015-01-09 19:22:37 +00001442 }
1443 if (verbose) {
1444 outs() << OFA.getArchTypeName() << "\n";
1445 printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1446 } else {
1447 outs() << i << "\n";
1448 outs() << " cputype " << cputype << "\n";
1449 outs() << " cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK)
1450 << "\n";
1451 }
1452 if (verbose &&
1453 (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64)
1454 outs() << " capabilities CPU_SUBTYPE_LIB64\n";
1455 else
1456 outs() << " capabilities "
1457 << format("0x%" PRIx32,
1458 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n";
1459 outs() << " offset " << OFA.getOffset();
1460 if (OFA.getOffset() > size)
1461 outs() << " (past end of file)";
1462 if (OFA.getOffset() % (1 << OFA.getAlign()) != 0)
1463 outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")";
1464 outs() << "\n";
1465 outs() << " size " << OFA.getSize();
1466 big_size = OFA.getOffset() + OFA.getSize();
1467 if (big_size > size)
1468 outs() << " (past end of file)";
1469 outs() << "\n";
1470 outs() << " align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign())
1471 << ")\n";
1472 }
1473}
1474
Kevin Enderby6524bd82016-07-19 20:47:07 +00001475static void printArchiveChild(StringRef Filename, const Archive::Child &C,
1476 bool verbose, bool print_offset,
1477 StringRef ArchitectureName = StringRef()) {
Kevin Enderby13023a12015-01-15 23:19:11 +00001478 if (print_offset)
1479 outs() << C.getChildOffset() << "\t";
Vedant Kumar4031d9f2016-08-03 19:02:50 +00001480 Expected<sys::fs::perms> ModeOrErr = C.getAccessMode();
1481 if (!ModeOrErr)
1482 report_error(Filename, C, ModeOrErr.takeError(), ArchitectureName);
1483 sys::fs::perms Mode = ModeOrErr.get();
Kevin Enderby13023a12015-01-15 23:19:11 +00001484 if (verbose) {
1485 // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
1486 // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
1487 outs() << "-";
Davide Italianobb9a6cc2015-09-07 20:47:03 +00001488 outs() << ((Mode & sys::fs::owner_read) ? "r" : "-");
1489 outs() << ((Mode & sys::fs::owner_write) ? "w" : "-");
1490 outs() << ((Mode & sys::fs::owner_exe) ? "x" : "-");
1491 outs() << ((Mode & sys::fs::group_read) ? "r" : "-");
1492 outs() << ((Mode & sys::fs::group_write) ? "w" : "-");
1493 outs() << ((Mode & sys::fs::group_exe) ? "x" : "-");
1494 outs() << ((Mode & sys::fs::others_read) ? "r" : "-");
1495 outs() << ((Mode & sys::fs::others_write) ? "w" : "-");
1496 outs() << ((Mode & sys::fs::others_exe) ? "x" : "-");
Kevin Enderby13023a12015-01-15 23:19:11 +00001497 } else {
1498 outs() << format("0%o ", Mode);
1499 }
1500
Vedant Kumar4031d9f2016-08-03 19:02:50 +00001501 Expected<unsigned> UIDOrErr = C.getUID();
1502 if (!UIDOrErr)
1503 report_error(Filename, C, UIDOrErr.takeError(), ArchitectureName);
1504 unsigned UID = UIDOrErr.get();
Kevin Enderby13023a12015-01-15 23:19:11 +00001505 outs() << format("%3d/", UID);
Vedant Kumar4031d9f2016-08-03 19:02:50 +00001506 Expected<unsigned> GIDOrErr = C.getGID();
1507 if (!GIDOrErr)
1508 report_error(Filename, C, GIDOrErr.takeError(), ArchitectureName);
1509 unsigned GID = GIDOrErr.get();
Kevin Enderby13023a12015-01-15 23:19:11 +00001510 outs() << format("%-3d ", GID);
Kevin Enderby6524bd82016-07-19 20:47:07 +00001511 Expected<uint64_t> Size = C.getRawSize();
1512 if (!Size)
1513 report_error(Filename, C, Size.takeError(), ArchitectureName);
Kevin Enderby7a969422015-11-05 19:24:56 +00001514 outs() << format("%5" PRId64, Size.get()) << " ";
Kevin Enderby13023a12015-01-15 23:19:11 +00001515
1516 StringRef RawLastModified = C.getRawLastModified();
1517 if (verbose) {
1518 unsigned Seconds;
1519 if (RawLastModified.getAsInteger(10, Seconds))
Vedant Kumar4031d9f2016-08-03 19:02:50 +00001520 outs() << "(date: \"" << RawLastModified
1521 << "\" contains non-decimal chars) ";
Kevin Enderby13023a12015-01-15 23:19:11 +00001522 else {
1523 // Since cime(3) returns a 26 character string of the form:
1524 // "Sun Sep 16 01:03:52 1973\n\0"
1525 // just print 24 characters.
1526 time_t t = Seconds;
1527 outs() << format("%.24s ", ctime(&t));
1528 }
1529 } else {
1530 outs() << RawLastModified << " ";
1531 }
1532
1533 if (verbose) {
Kevin Enderbyf4586032016-07-29 17:44:13 +00001534 Expected<StringRef> NameOrErr = C.getName();
1535 if (!NameOrErr) {
1536 consumeError(NameOrErr.takeError());
1537 Expected<StringRef> NameOrErr = C.getRawName();
1538 if (!NameOrErr)
1539 report_error(Filename, C, NameOrErr.takeError(), ArchitectureName);
1540 StringRef RawName = NameOrErr.get();
Kevin Enderby13023a12015-01-15 23:19:11 +00001541 outs() << RawName << "\n";
1542 } else {
1543 StringRef Name = NameOrErr.get();
1544 outs() << Name << "\n";
1545 }
1546 } else {
Kevin Enderbyf4586032016-07-29 17:44:13 +00001547 Expected<StringRef> NameOrErr = C.getRawName();
1548 if (!NameOrErr)
1549 report_error(Filename, C, NameOrErr.takeError(), ArchitectureName);
1550 StringRef RawName = NameOrErr.get();
Kevin Enderby13023a12015-01-15 23:19:11 +00001551 outs() << RawName << "\n";
1552 }
1553}
1554
Kevin Enderby6524bd82016-07-19 20:47:07 +00001555static void printArchiveHeaders(StringRef Filename, Archive *A, bool verbose,
1556 bool print_offset,
1557 StringRef ArchitectureName = StringRef()) {
Lang Hamesfc209622016-07-14 02:24:01 +00001558 Error Err;
1559 for (const auto &C : A->children(Err, false))
Kevin Enderby6524bd82016-07-19 20:47:07 +00001560 printArchiveChild(Filename, C, verbose, print_offset, ArchitectureName);
1561
Lang Hamesfc209622016-07-14 02:24:01 +00001562 if (Err)
Kevin Enderby6524bd82016-07-19 20:47:07 +00001563 report_error(Filename, std::move(Err));
Kevin Enderby13023a12015-01-15 23:19:11 +00001564}
1565
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001566// ParseInputMachO() parses the named Mach-O file in Filename and handles the
1567// -arch flags selecting just those slices as specified by them and also parses
1568// archive files. Then for each individual Mach-O file ProcessMachO() is
1569// called to process the file based on the command line options.
1570void llvm::ParseInputMachO(StringRef Filename) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001571 // Check for -arch all and verifiy the -arch flags are valid.
1572 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1573 if (ArchFlags[i] == "all") {
1574 ArchAll = true;
1575 } else {
1576 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
1577 errs() << "llvm-objdump: Unknown architecture named '" + ArchFlags[i] +
1578 "'for the -arch option\n";
1579 return;
1580 }
1581 }
1582 }
1583
1584 // Attempt to open the binary.
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +00001585 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename);
1586 if (!BinaryOrErr)
1587 report_error(Filename, BinaryOrErr.takeError());
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001588 Binary &Bin = *BinaryOrErr.get().getBinary();
Kevin Enderby3f0ffab2014-12-03 22:29:40 +00001589
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001590 if (Archive *A = dyn_cast<Archive>(&Bin)) {
1591 outs() << "Archive : " << Filename << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001592 if (ArchiveHeaders)
Kevin Enderby6524bd82016-07-19 20:47:07 +00001593 printArchiveHeaders(Filename, A, !NonVerbose, ArchiveMemberOffsets);
1594
Lang Hamesfc209622016-07-14 02:24:01 +00001595 Error Err;
1596 for (auto &C : A->children(Err)) {
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001597 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1598 if (!ChildOrErr) {
1599 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
1600 report_error(Filename, C, std::move(E));
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001601 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001602 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001603 if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1604 if (!checkMachOAndArchFlags(O, Filename))
1605 return;
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001606 ProcessMachO(Filename, O, O->getFileName());
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001607 }
1608 }
Lang Hamesfc209622016-07-14 02:24:01 +00001609 if (Err)
1610 report_error(Filename, std::move(Err));
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001611 return;
1612 }
Kevin Enderby131d1772015-01-09 19:22:37 +00001613 if (UniversalHeaders) {
1614 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin))
Kevin Enderbyf0640752015-03-13 17:56:32 +00001615 printMachOUniversalHeaders(UB, !NonVerbose);
Kevin Enderby131d1772015-01-09 19:22:37 +00001616 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001617 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
1618 // If we have a list of architecture flags specified dump only those.
1619 if (!ArchAll && ArchFlags.size() != 0) {
1620 // Look for a slice in the universal binary that matches each ArchFlag.
1621 bool ArchFound;
1622 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1623 ArchFound = false;
1624 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1625 E = UB->end_objects();
1626 I != E; ++I) {
1627 if (ArchFlags[i] == I->getArchTypeName()) {
1628 ArchFound = true;
Kevin Enderby9acb1092016-05-31 20:35:34 +00001629 Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001630 I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001631 std::string ArchitectureName = "";
1632 if (ArchFlags.size() > 1)
1633 ArchitectureName = I->getArchTypeName();
1634 if (ObjOrErr) {
1635 ObjectFile &O = *ObjOrErr.get();
1636 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001637 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
Kevin Enderby9acb1092016-05-31 20:35:34 +00001638 } else if (auto E = isNotObjectErrorInvalidFileType(
1639 ObjOrErr.takeError())) {
1640 report_error(Filename, StringRef(), std::move(E),
1641 ArchitectureName);
1642 continue;
Kevin Enderby42398052016-06-28 23:16:13 +00001643 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001644 I->getAsArchive()) {
1645 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001646 outs() << "Archive : " << Filename;
1647 if (!ArchitectureName.empty())
1648 outs() << " (architecture " << ArchitectureName << ")";
1649 outs() << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001650 if (ArchiveHeaders)
Kevin Enderby6524bd82016-07-19 20:47:07 +00001651 printArchiveHeaders(Filename, A.get(), !NonVerbose,
1652 ArchiveMemberOffsets, ArchitectureName);
Lang Hamesfc209622016-07-14 02:24:01 +00001653 Error Err;
1654 for (auto &C : A->children(Err)) {
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001655 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1656 if (!ChildOrErr) {
1657 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
Kevin Enderby9acb1092016-05-31 20:35:34 +00001658 report_error(Filename, C, std::move(E), ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001659 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001660 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001661 if (MachOObjectFile *O =
1662 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001663 ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001664 }
Lang Hamesfc209622016-07-14 02:24:01 +00001665 if (Err)
1666 report_error(Filename, std::move(Err));
Kevin Enderby42398052016-06-28 23:16:13 +00001667 } else {
1668 consumeError(AOrErr.takeError());
1669 error("Mach-O universal file: " + Filename + " for " +
1670 "architecture " + StringRef(I->getArchTypeName()) +
1671 " is not a Mach-O file or an archive file");
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001672 }
1673 }
1674 }
1675 if (!ArchFound) {
1676 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1677 << "architecture: " + ArchFlags[i] + "\n";
1678 return;
1679 }
1680 }
1681 return;
1682 }
1683 // No architecture flags were specified so if this contains a slice that
1684 // matches the host architecture dump only that.
1685 if (!ArchAll) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001686 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1687 E = UB->end_objects();
1688 I != E; ++I) {
Kevin Enderby0512bd72015-01-09 21:55:03 +00001689 if (MachOObjectFile::getHostArch().getArchName() ==
1690 I->getArchTypeName()) {
Kevin Enderby9acb1092016-05-31 20:35:34 +00001691 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001692 std::string ArchiveName;
1693 ArchiveName.clear();
1694 if (ObjOrErr) {
1695 ObjectFile &O = *ObjOrErr.get();
1696 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001697 ProcessMachO(Filename, MachOOF);
Kevin Enderby9acb1092016-05-31 20:35:34 +00001698 } else if (auto E = isNotObjectErrorInvalidFileType(
1699 ObjOrErr.takeError())) {
1700 report_error(Filename, std::move(E));
1701 continue;
Kevin Enderby42398052016-06-28 23:16:13 +00001702 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001703 I->getAsArchive()) {
1704 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001705 outs() << "Archive : " << Filename << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001706 if (ArchiveHeaders)
Kevin Enderby6524bd82016-07-19 20:47:07 +00001707 printArchiveHeaders(Filename, A.get(), !NonVerbose,
1708 ArchiveMemberOffsets);
Lang Hamesfc209622016-07-14 02:24:01 +00001709 Error Err;
1710 for (auto &C : A->children(Err)) {
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001711 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1712 if (!ChildOrErr) {
1713 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
1714 report_error(Filename, C, std::move(E));
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001715 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001716 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001717 if (MachOObjectFile *O =
1718 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001719 ProcessMachO(Filename, O, O->getFileName());
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001720 }
Lang Hamesfc209622016-07-14 02:24:01 +00001721 if (Err)
1722 report_error(Filename, std::move(Err));
Kevin Enderby42398052016-06-28 23:16:13 +00001723 } else {
1724 consumeError(AOrErr.takeError());
1725 error("Mach-O universal file: " + Filename + " for architecture " +
1726 StringRef(I->getArchTypeName()) +
1727 " is not a Mach-O file or an archive file");
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001728 }
1729 return;
1730 }
1731 }
1732 }
1733 // Either all architectures have been specified or none have been specified
1734 // and this does not contain the host architecture so dump all the slices.
1735 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
1736 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1737 E = UB->end_objects();
1738 I != E; ++I) {
Kevin Enderby9acb1092016-05-31 20:35:34 +00001739 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001740 std::string ArchitectureName = "";
1741 if (moreThanOneArch)
1742 ArchitectureName = I->getArchTypeName();
1743 if (ObjOrErr) {
1744 ObjectFile &Obj = *ObjOrErr.get();
1745 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001746 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
Kevin Enderby9acb1092016-05-31 20:35:34 +00001747 } else if (auto E = isNotObjectErrorInvalidFileType(
1748 ObjOrErr.takeError())) {
1749 report_error(StringRef(), Filename, std::move(E), ArchitectureName);
1750 continue;
Kevin Enderby42398052016-06-28 23:16:13 +00001751 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
1752 I->getAsArchive()) {
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001753 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001754 outs() << "Archive : " << Filename;
1755 if (!ArchitectureName.empty())
1756 outs() << " (architecture " << ArchitectureName << ")";
1757 outs() << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001758 if (ArchiveHeaders)
Kevin Enderby6524bd82016-07-19 20:47:07 +00001759 printArchiveHeaders(Filename, A.get(), !NonVerbose,
1760 ArchiveMemberOffsets, ArchitectureName);
Lang Hamesfc209622016-07-14 02:24:01 +00001761 Error Err;
1762 for (auto &C : A->children(Err)) {
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001763 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1764 if (!ChildOrErr) {
1765 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
Kevin Enderby9acb1092016-05-31 20:35:34 +00001766 report_error(Filename, C, std::move(E), ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001767 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001768 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001769 if (MachOObjectFile *O =
1770 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1771 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001772 ProcessMachO(Filename, MachOOF, MachOOF->getFileName(),
1773 ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001774 }
1775 }
Lang Hamesfc209622016-07-14 02:24:01 +00001776 if (Err)
1777 report_error(Filename, std::move(Err));
Kevin Enderby42398052016-06-28 23:16:13 +00001778 } else {
1779 consumeError(AOrErr.takeError());
1780 error("Mach-O universal file: " + Filename + " for architecture " +
1781 StringRef(I->getArchTypeName()) +
1782 " is not a Mach-O file or an archive file");
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001783 }
1784 }
1785 return;
1786 }
1787 if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) {
1788 if (!checkMachOAndArchFlags(O, Filename))
1789 return;
1790 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O)) {
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001791 ProcessMachO(Filename, MachOOF);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001792 } else
1793 errs() << "llvm-objdump: '" << Filename << "': "
1794 << "Object is not a Mach-O file type.\n";
Davide Italiano25d84582016-01-13 04:11:36 +00001795 return;
1796 }
1797 llvm_unreachable("Input object can't be invalid at this point");
Rafael Espindola9b709252013-04-13 01:45:40 +00001798}
1799
Kevin Enderby6f326ce2014-10-23 19:37:31 +00001800typedef std::pair<uint64_t, const char *> BindInfoEntry;
1801typedef std::vector<BindInfoEntry> BindTable;
1802typedef BindTable::iterator bind_table_iterator;
Kevin Enderbybf246f52014-09-24 23:08:22 +00001803
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001804// The block of info used by the Symbolizer call backs.
1805struct DisassembleInfo {
1806 bool verbose;
1807 MachOObjectFile *O;
1808 SectionRef S;
Kevin Enderbybf246f52014-09-24 23:08:22 +00001809 SymbolAddressMap *AddrMap;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00001810 std::vector<SectionRef> *Sections;
1811 const char *class_name;
1812 const char *selector_name;
1813 char *method;
Kevin Enderby04bf6932014-10-28 23:39:46 +00001814 char *demangled_name;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00001815 uint64_t adrp_addr;
1816 uint32_t adrp_inst;
Kevin Enderby078be602014-10-23 19:53:12 +00001817 BindTable *bindtable;
Kevin Enderbyaac75382015-10-08 16:56:35 +00001818 uint32_t depth;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001819};
1820
1821// SymbolizerGetOpInfo() is the operand information call back function.
1822// This is called to get the symbolic information for operand(s) of an
1823// instruction when it is being done. This routine does this from
1824// the relocation information, symbol table, etc. That block of information
1825// is a pointer to the struct DisassembleInfo that was passed when the
1826// disassembler context was created and passed to back to here when
1827// called back by the disassembler for instruction operands that could have
1828// relocation information. The address of the instruction containing operand is
1829// at the Pc parameter. The immediate value the operand has is passed in
1830// op_info->Value and is at Offset past the start of the instruction and has a
1831// byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
1832// LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
1833// names and addends of the symbolic expression to add for the operand. The
1834// value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
1835// information is returned then this function returns 1 else it returns 0.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00001836static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
1837 uint64_t Size, int TagType, void *TagBuf) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001838 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
1839 struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00001840 uint64_t value = op_info->Value;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001841
1842 // Make sure all fields returned are zero if we don't set them.
1843 memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1));
1844 op_info->Value = value;
1845
1846 // If the TagType is not the value 1 which it code knows about or if no
1847 // verbose symbolic information is wanted then just return 0, indicating no
1848 // information is being returned.
David Blaikie33dd45d02015-03-23 18:39:02 +00001849 if (TagType != 1 || !info->verbose)
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001850 return 0;
1851
1852 unsigned int Arch = info->O->getArch();
1853 if (Arch == Triple::x86) {
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001854 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1855 return 0;
Kevin Enderbyd90a4172015-10-10 00:05:01 +00001856 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
1857 // TODO:
1858 // Search the external relocation entries of a fully linked image
1859 // (if any) for an entry that matches this segment offset.
1860 // uint32_t seg_offset = (Pc + Offset);
1861 return 0;
1862 }
1863 // In MH_OBJECT filetypes search the section's relocation entries (if any)
1864 // for an entry for this section offset.
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001865 uint32_t sect_addr = info->S.getAddress();
1866 uint32_t sect_offset = (Pc + Offset) - sect_addr;
1867 bool reloc_found = false;
1868 DataRefImpl Rel;
1869 MachO::any_relocation_info RE;
1870 bool isExtern = false;
1871 SymbolRef Symbol;
1872 bool r_scattered = false;
1873 uint32_t r_value, pair_r_value, r_type;
1874 for (const RelocationRef &Reloc : info->S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00001875 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001876 if (RelocOffset == sect_offset) {
1877 Rel = Reloc.getRawDataRefImpl();
1878 RE = info->O->getRelocation(Rel);
Kevin Enderby3eb73e12014-11-11 19:16:45 +00001879 r_type = info->O->getAnyRelocationType(RE);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001880 r_scattered = info->O->isRelocationScattered(RE);
1881 if (r_scattered) {
1882 r_value = info->O->getScatteredRelocationValue(RE);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001883 if (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1884 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
1885 DataRefImpl RelNext = Rel;
1886 info->O->moveRelocationNext(RelNext);
1887 MachO::any_relocation_info RENext;
1888 RENext = info->O->getRelocation(RelNext);
1889 if (info->O->isRelocationScattered(RENext))
Kevin Enderby930fdc72014-11-06 19:00:13 +00001890 pair_r_value = info->O->getScatteredRelocationValue(RENext);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001891 else
1892 return 0;
1893 }
1894 } else {
1895 isExtern = info->O->getPlainRelocationExternal(RE);
1896 if (isExtern) {
1897 symbol_iterator RelocSym = Reloc.getSymbol();
1898 Symbol = *RelocSym;
1899 }
1900 }
1901 reloc_found = true;
1902 break;
1903 }
1904 }
1905 if (reloc_found && isExtern) {
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00001906 Expected<StringRef> SymName = Symbol.getName();
1907 if (!SymName) {
1908 std::string Buf;
1909 raw_string_ostream OS(Buf);
1910 logAllUnhandledErrors(SymName.takeError(), OS, "");
1911 OS.flush();
1912 report_fatal_error(Buf);
1913 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001914 const char *name = SymName->data();
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001915 op_info->AddSymbol.Present = 1;
1916 op_info->AddSymbol.Name = name;
1917 // For i386 extern relocation entries the value in the instruction is
1918 // the offset from the symbol, and value is already set in op_info->Value.
1919 return 1;
1920 }
1921 if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1922 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) {
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001923 const char *add = GuessSymbolName(r_value, info->AddrMap);
1924 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001925 uint32_t offset = value - (r_value - pair_r_value);
1926 op_info->AddSymbol.Present = 1;
1927 if (add != nullptr)
1928 op_info->AddSymbol.Name = add;
1929 else
1930 op_info->AddSymbol.Value = r_value;
1931 op_info->SubtractSymbol.Present = 1;
1932 if (sub != nullptr)
1933 op_info->SubtractSymbol.Name = sub;
1934 else
1935 op_info->SubtractSymbol.Value = pair_r_value;
1936 op_info->Value = offset;
1937 return 1;
1938 }
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001939 return 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00001940 }
1941 if (Arch == Triple::x86_64) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001942 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1943 return 0;
Kevin Enderbyd90a4172015-10-10 00:05:01 +00001944 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
1945 // TODO:
1946 // Search the external relocation entries of a fully linked image
1947 // (if any) for an entry that matches this segment offset.
1948 // uint64_t seg_offset = (Pc + Offset);
1949 return 0;
1950 }
1951 // In MH_OBJECT filetypes search the section's relocation entries (if any)
1952 // for an entry for this section offset.
Rafael Espindola80291272014-10-08 15:28:58 +00001953 uint64_t sect_addr = info->S.getAddress();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001954 uint64_t sect_offset = (Pc + Offset) - sect_addr;
1955 bool reloc_found = false;
1956 DataRefImpl Rel;
1957 MachO::any_relocation_info RE;
1958 bool isExtern = false;
1959 SymbolRef Symbol;
1960 for (const RelocationRef &Reloc : info->S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00001961 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001962 if (RelocOffset == sect_offset) {
1963 Rel = Reloc.getRawDataRefImpl();
1964 RE = info->O->getRelocation(Rel);
1965 // NOTE: Scattered relocations don't exist on x86_64.
1966 isExtern = info->O->getPlainRelocationExternal(RE);
1967 if (isExtern) {
1968 symbol_iterator RelocSym = Reloc.getSymbol();
1969 Symbol = *RelocSym;
1970 }
1971 reloc_found = true;
1972 break;
1973 }
1974 }
1975 if (reloc_found && isExtern) {
1976 // The Value passed in will be adjusted by the Pc if the instruction
1977 // adds the Pc. But for x86_64 external relocation entries the Value
1978 // is the offset from the external symbol.
1979 if (info->O->getAnyRelocationPCRel(RE))
1980 op_info->Value -= Pc + Offset + Size;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00001981 Expected<StringRef> SymName = Symbol.getName();
1982 if (!SymName) {
1983 std::string Buf;
1984 raw_string_ostream OS(Buf);
1985 logAllUnhandledErrors(SymName.takeError(), OS, "");
1986 OS.flush();
1987 report_fatal_error(Buf);
1988 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001989 const char *name = SymName->data();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001990 unsigned Type = info->O->getAnyRelocationType(RE);
1991 if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
1992 DataRefImpl RelNext = Rel;
1993 info->O->moveRelocationNext(RelNext);
1994 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
1995 unsigned TypeNext = info->O->getAnyRelocationType(RENext);
1996 bool isExternNext = info->O->getPlainRelocationExternal(RENext);
1997 unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext);
1998 if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) {
1999 op_info->SubtractSymbol.Present = 1;
2000 op_info->SubtractSymbol.Name = name;
2001 symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
2002 Symbol = *RelocSymNext;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002003 Expected<StringRef> SymNameNext = Symbol.getName();
2004 if (!SymNameNext) {
2005 std::string Buf;
2006 raw_string_ostream OS(Buf);
2007 logAllUnhandledErrors(SymNameNext.takeError(), OS, "");
2008 OS.flush();
2009 report_fatal_error(Buf);
2010 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002011 name = SymNameNext->data();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002012 }
2013 }
2014 // TODO: add the VariantKinds to op_info->VariantKind for relocation types
2015 // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
2016 op_info->AddSymbol.Present = 1;
2017 op_info->AddSymbol.Name = name;
2018 return 1;
2019 }
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002020 return 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00002021 }
2022 if (Arch == Triple::arm) {
Kevin Enderby930fdc72014-11-06 19:00:13 +00002023 if (Offset != 0 || (Size != 4 && Size != 2))
2024 return 0;
Kevin Enderbyd90a4172015-10-10 00:05:01 +00002025 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2026 // TODO:
2027 // Search the external relocation entries of a fully linked image
2028 // (if any) for an entry that matches this segment offset.
2029 // uint32_t seg_offset = (Pc + Offset);
2030 return 0;
2031 }
2032 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2033 // for an entry for this section offset.
Kevin Enderby930fdc72014-11-06 19:00:13 +00002034 uint32_t sect_addr = info->S.getAddress();
2035 uint32_t sect_offset = (Pc + Offset) - sect_addr;
Kevin Enderby930fdc72014-11-06 19:00:13 +00002036 DataRefImpl Rel;
2037 MachO::any_relocation_info RE;
2038 bool isExtern = false;
2039 SymbolRef Symbol;
2040 bool r_scattered = false;
2041 uint32_t r_value, pair_r_value, r_type, r_length, other_half;
David Blaikie33dd45d02015-03-23 18:39:02 +00002042 auto Reloc =
2043 std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
2044 [&](const RelocationRef &Reloc) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00002045 uint64_t RelocOffset = Reloc.getOffset();
David Blaikie33dd45d02015-03-23 18:39:02 +00002046 return RelocOffset == sect_offset;
2047 });
2048
2049 if (Reloc == info->S.relocations().end())
2050 return 0;
2051
2052 Rel = Reloc->getRawDataRefImpl();
2053 RE = info->O->getRelocation(Rel);
2054 r_length = info->O->getAnyRelocationLength(RE);
2055 r_scattered = info->O->isRelocationScattered(RE);
2056 if (r_scattered) {
2057 r_value = info->O->getScatteredRelocationValue(RE);
2058 r_type = info->O->getScatteredRelocationType(RE);
2059 } else {
2060 r_type = info->O->getAnyRelocationType(RE);
2061 isExtern = info->O->getPlainRelocationExternal(RE);
2062 if (isExtern) {
2063 symbol_iterator RelocSym = Reloc->getSymbol();
2064 Symbol = *RelocSym;
Kevin Enderby930fdc72014-11-06 19:00:13 +00002065 }
2066 }
David Blaikie33dd45d02015-03-23 18:39:02 +00002067 if (r_type == MachO::ARM_RELOC_HALF ||
2068 r_type == MachO::ARM_RELOC_SECTDIFF ||
2069 r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
2070 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2071 DataRefImpl RelNext = Rel;
2072 info->O->moveRelocationNext(RelNext);
2073 MachO::any_relocation_info RENext;
2074 RENext = info->O->getRelocation(RelNext);
2075 other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff;
2076 if (info->O->isRelocationScattered(RENext))
2077 pair_r_value = info->O->getScatteredRelocationValue(RENext);
2078 }
2079
2080 if (isExtern) {
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002081 Expected<StringRef> SymName = Symbol.getName();
2082 if (!SymName) {
2083 std::string Buf;
2084 raw_string_ostream OS(Buf);
2085 logAllUnhandledErrors(SymName.takeError(), OS, "");
2086 OS.flush();
2087 report_fatal_error(Buf);
2088 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002089 const char *name = SymName->data();
Kevin Enderby930fdc72014-11-06 19:00:13 +00002090 op_info->AddSymbol.Present = 1;
2091 op_info->AddSymbol.Name = name;
Sylvestre Ledru648cced2015-02-05 17:00:23 +00002092 switch (r_type) {
2093 case MachO::ARM_RELOC_HALF:
2094 if ((r_length & 0x1) == 1) {
2095 op_info->Value = value << 16 | other_half;
2096 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2097 } else {
2098 op_info->Value = other_half << 16 | value;
2099 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Sylvestre Ledrufe0c7ad2015-02-05 16:35:44 +00002100 }
Sylvestre Ledru648cced2015-02-05 17:00:23 +00002101 break;
2102 default:
2103 break;
Kevin Enderby930fdc72014-11-06 19:00:13 +00002104 }
2105 return 1;
2106 }
2107 // If we have a branch that is not an external relocation entry then
2108 // return 0 so the code in tryAddingSymbolicOperand() can use the
2109 // SymbolLookUp call back with the branch target address to look up the
2110 // symbol and possiblity add an annotation for a symbol stub.
David Blaikie33dd45d02015-03-23 18:39:02 +00002111 if (isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 ||
2112 r_type == MachO::ARM_THUMB_RELOC_BR22))
Kevin Enderby930fdc72014-11-06 19:00:13 +00002113 return 0;
2114
2115 uint32_t offset = 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00002116 if (r_type == MachO::ARM_RELOC_HALF ||
2117 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2118 if ((r_length & 0x1) == 1)
2119 value = value << 16 | other_half;
2120 else
2121 value = other_half << 16 | value;
2122 }
2123 if (r_scattered && (r_type != MachO::ARM_RELOC_HALF &&
2124 r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) {
2125 offset = value - r_value;
2126 value = r_value;
Kevin Enderby930fdc72014-11-06 19:00:13 +00002127 }
2128
David Blaikie33dd45d02015-03-23 18:39:02 +00002129 if (r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
Kevin Enderby930fdc72014-11-06 19:00:13 +00002130 if ((r_length & 0x1) == 1)
2131 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2132 else
2133 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00002134 const char *add = GuessSymbolName(r_value, info->AddrMap);
2135 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
Kevin Enderby930fdc72014-11-06 19:00:13 +00002136 int32_t offset = value - (r_value - pair_r_value);
2137 op_info->AddSymbol.Present = 1;
2138 if (add != nullptr)
2139 op_info->AddSymbol.Name = add;
2140 else
2141 op_info->AddSymbol.Value = r_value;
2142 op_info->SubtractSymbol.Present = 1;
2143 if (sub != nullptr)
2144 op_info->SubtractSymbol.Name = sub;
2145 else
2146 op_info->SubtractSymbol.Value = pair_r_value;
2147 op_info->Value = offset;
2148 return 1;
2149 }
2150
Kevin Enderby930fdc72014-11-06 19:00:13 +00002151 op_info->AddSymbol.Present = 1;
2152 op_info->Value = offset;
David Blaikie33dd45d02015-03-23 18:39:02 +00002153 if (r_type == MachO::ARM_RELOC_HALF) {
2154 if ((r_length & 0x1) == 1)
2155 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2156 else
2157 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Kevin Enderby930fdc72014-11-06 19:00:13 +00002158 }
Kevin Enderbyf6d25852015-01-31 00:37:11 +00002159 const char *add = GuessSymbolName(value, info->AddrMap);
Kevin Enderby930fdc72014-11-06 19:00:13 +00002160 if (add != nullptr) {
2161 op_info->AddSymbol.Name = add;
2162 return 1;
2163 }
2164 op_info->AddSymbol.Value = value;
2165 return 1;
David Blaikie33dd45d02015-03-23 18:39:02 +00002166 }
2167 if (Arch == Triple::aarch64) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002168 if (Offset != 0 || Size != 4)
2169 return 0;
Kevin Enderbyd90a4172015-10-10 00:05:01 +00002170 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2171 // TODO:
2172 // Search the external relocation entries of a fully linked image
2173 // (if any) for an entry that matches this segment offset.
2174 // uint64_t seg_offset = (Pc + Offset);
2175 return 0;
2176 }
2177 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2178 // for an entry for this section offset.
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002179 uint64_t sect_addr = info->S.getAddress();
2180 uint64_t sect_offset = (Pc + Offset) - sect_addr;
David Blaikie33dd45d02015-03-23 18:39:02 +00002181 auto Reloc =
2182 std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
2183 [&](const RelocationRef &Reloc) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00002184 uint64_t RelocOffset = Reloc.getOffset();
David Blaikie33dd45d02015-03-23 18:39:02 +00002185 return RelocOffset == sect_offset;
2186 });
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002187
David Blaikie33dd45d02015-03-23 18:39:02 +00002188 if (Reloc == info->S.relocations().end())
2189 return 0;
2190
2191 DataRefImpl Rel = Reloc->getRawDataRefImpl();
2192 MachO::any_relocation_info RE = info->O->getRelocation(Rel);
2193 uint32_t r_type = info->O->getAnyRelocationType(RE);
2194 if (r_type == MachO::ARM64_RELOC_ADDEND) {
2195 DataRefImpl RelNext = Rel;
2196 info->O->moveRelocationNext(RelNext);
2197 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2198 if (value == 0) {
2199 value = info->O->getPlainRelocationSymbolNum(RENext);
2200 op_info->Value = value;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002201 }
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002202 }
David Blaikie33dd45d02015-03-23 18:39:02 +00002203 // NOTE: Scattered relocations don't exist on arm64.
2204 if (!info->O->getPlainRelocationExternal(RE))
2205 return 0;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002206 Expected<StringRef> SymName = Reloc->getSymbol()->getName();
2207 if (!SymName) {
2208 std::string Buf;
2209 raw_string_ostream OS(Buf);
2210 logAllUnhandledErrors(SymName.takeError(), OS, "");
2211 OS.flush();
2212 report_fatal_error(Buf);
2213 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002214 const char *name = SymName->data();
David Blaikie33dd45d02015-03-23 18:39:02 +00002215 op_info->AddSymbol.Present = 1;
2216 op_info->AddSymbol.Name = name;
2217
2218 switch (r_type) {
2219 case MachO::ARM64_RELOC_PAGE21:
2220 /* @page */
2221 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE;
2222 break;
2223 case MachO::ARM64_RELOC_PAGEOFF12:
2224 /* @pageoff */
2225 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF;
2226 break;
2227 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
2228 /* @gotpage */
2229 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE;
2230 break;
2231 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
2232 /* @gotpageoff */
2233 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF;
2234 break;
2235 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
2236 /* @tvlppage is not implemented in llvm-mc */
2237 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP;
2238 break;
2239 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
2240 /* @tvlppageoff is not implemented in llvm-mc */
2241 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF;
2242 break;
2243 default:
2244 case MachO::ARM64_RELOC_BRANCH26:
2245 op_info->VariantKind = LLVMDisassembler_VariantKind_None;
2246 break;
2247 }
2248 return 1;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002249 }
David Blaikie33dd45d02015-03-23 18:39:02 +00002250 return 0;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002251}
2252
Kevin Enderbybf246f52014-09-24 23:08:22 +00002253// GuessCstringPointer is passed the address of what might be a pointer to a
2254// literal string in a cstring section. If that address is in a cstring section
2255// it returns a pointer to that string. Else it returns nullptr.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00002256static const char *GuessCstringPointer(uint64_t ReferenceValue,
2257 struct DisassembleInfo *info) {
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002258 for (const auto &Load : info->O->load_commands()) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00002259 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2260 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2261 for (unsigned J = 0; J < Seg.nsects; ++J) {
2262 MachO::section_64 Sec = info->O->getSection64(Load, J);
2263 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2264 if (section_type == MachO::S_CSTRING_LITERALS &&
2265 ReferenceValue >= Sec.addr &&
2266 ReferenceValue < Sec.addr + Sec.size) {
2267 uint64_t sect_offset = ReferenceValue - Sec.addr;
2268 uint64_t object_offset = Sec.offset + sect_offset;
2269 StringRef MachOContents = info->O->getData();
2270 uint64_t object_size = MachOContents.size();
2271 const char *object_addr = (const char *)MachOContents.data();
2272 if (object_offset < object_size) {
2273 const char *name = object_addr + object_offset;
2274 return name;
2275 } else {
2276 return nullptr;
2277 }
2278 }
2279 }
2280 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2281 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2282 for (unsigned J = 0; J < Seg.nsects; ++J) {
2283 MachO::section Sec = info->O->getSection(Load, J);
2284 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2285 if (section_type == MachO::S_CSTRING_LITERALS &&
2286 ReferenceValue >= Sec.addr &&
2287 ReferenceValue < Sec.addr + Sec.size) {
2288 uint64_t sect_offset = ReferenceValue - Sec.addr;
2289 uint64_t object_offset = Sec.offset + sect_offset;
2290 StringRef MachOContents = info->O->getData();
2291 uint64_t object_size = MachOContents.size();
2292 const char *object_addr = (const char *)MachOContents.data();
2293 if (object_offset < object_size) {
2294 const char *name = object_addr + object_offset;
2295 return name;
2296 } else {
2297 return nullptr;
2298 }
2299 }
2300 }
2301 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00002302 }
2303 return nullptr;
2304}
2305
Kevin Enderby85974882014-09-26 22:20:44 +00002306// GuessIndirectSymbol returns the name of the indirect symbol for the
2307// ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe
2308// an address of a symbol stub or a lazy or non-lazy pointer to associate the
2309// symbol name being referenced by the stub or pointer.
2310static const char *GuessIndirectSymbol(uint64_t ReferenceValue,
2311 struct DisassembleInfo *info) {
Kevin Enderby85974882014-09-26 22:20:44 +00002312 MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand();
2313 MachO::symtab_command Symtab = info->O->getSymtabLoadCommand();
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002314 for (const auto &Load : info->O->load_commands()) {
Kevin Enderby85974882014-09-26 22:20:44 +00002315 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2316 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2317 for (unsigned J = 0; J < Seg.nsects; ++J) {
2318 MachO::section_64 Sec = info->O->getSection64(Load, J);
2319 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2320 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2321 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2322 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2323 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2324 section_type == MachO::S_SYMBOL_STUBS) &&
2325 ReferenceValue >= Sec.addr &&
2326 ReferenceValue < Sec.addr + Sec.size) {
2327 uint32_t stride;
2328 if (section_type == MachO::S_SYMBOL_STUBS)
2329 stride = Sec.reserved2;
2330 else
2331 stride = 8;
2332 if (stride == 0)
2333 return nullptr;
2334 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2335 if (index < Dysymtab.nindirectsyms) {
2336 uint32_t indirect_symbol =
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002337 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
Kevin Enderby85974882014-09-26 22:20:44 +00002338 if (indirect_symbol < Symtab.nsyms) {
2339 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2340 SymbolRef Symbol = *Sym;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002341 Expected<StringRef> SymName = Symbol.getName();
2342 if (!SymName) {
2343 std::string Buf;
2344 raw_string_ostream OS(Buf);
2345 logAllUnhandledErrors(SymName.takeError(), OS, "");
2346 OS.flush();
2347 report_fatal_error(Buf);
2348 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002349 const char *name = SymName->data();
Kevin Enderby85974882014-09-26 22:20:44 +00002350 return name;
2351 }
2352 }
2353 }
2354 }
2355 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2356 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2357 for (unsigned J = 0; J < Seg.nsects; ++J) {
2358 MachO::section Sec = info->O->getSection(Load, J);
2359 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2360 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2361 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2362 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2363 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2364 section_type == MachO::S_SYMBOL_STUBS) &&
2365 ReferenceValue >= Sec.addr &&
2366 ReferenceValue < Sec.addr + Sec.size) {
2367 uint32_t stride;
2368 if (section_type == MachO::S_SYMBOL_STUBS)
2369 stride = Sec.reserved2;
2370 else
2371 stride = 4;
2372 if (stride == 0)
2373 return nullptr;
2374 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2375 if (index < Dysymtab.nindirectsyms) {
2376 uint32_t indirect_symbol =
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002377 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
Kevin Enderby85974882014-09-26 22:20:44 +00002378 if (indirect_symbol < Symtab.nsyms) {
2379 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2380 SymbolRef Symbol = *Sym;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002381 Expected<StringRef> SymName = Symbol.getName();
2382 if (!SymName) {
2383 std::string Buf;
2384 raw_string_ostream OS(Buf);
2385 logAllUnhandledErrors(SymName.takeError(), OS, "");
2386 OS.flush();
2387 report_fatal_error(Buf);
2388 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002389 const char *name = SymName->data();
Kevin Enderby85974882014-09-26 22:20:44 +00002390 return name;
2391 }
2392 }
2393 }
2394 }
2395 }
Kevin Enderby85974882014-09-26 22:20:44 +00002396 }
2397 return nullptr;
2398}
2399
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002400// method_reference() is called passing it the ReferenceName that might be
2401// a reference it to an Objective-C method call. If so then it allocates and
2402// assembles a method call string with the values last seen and saved in
2403// the DisassembleInfo's class_name and selector_name fields. This is saved
2404// into the method field of the info and any previous string is free'ed.
2405// Then the class_name field in the info is set to nullptr. The method call
2406// string is set into ReferenceName and ReferenceType is set to
2407// LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call
2408// then both ReferenceType and ReferenceName are left unchanged.
2409static void method_reference(struct DisassembleInfo *info,
2410 uint64_t *ReferenceType,
2411 const char **ReferenceName) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002412 unsigned int Arch = info->O->getArch();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002413 if (*ReferenceName != nullptr) {
2414 if (strcmp(*ReferenceName, "_objc_msgSend") == 0) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002415 if (info->selector_name != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002416 if (info->method != nullptr)
2417 free(info->method);
2418 if (info->class_name != nullptr) {
2419 info->method = (char *)malloc(5 + strlen(info->class_name) +
2420 strlen(info->selector_name));
2421 if (info->method != nullptr) {
2422 strcpy(info->method, "+[");
2423 strcat(info->method, info->class_name);
2424 strcat(info->method, " ");
2425 strcat(info->method, info->selector_name);
2426 strcat(info->method, "]");
2427 *ReferenceName = info->method;
2428 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2429 }
2430 } else {
2431 info->method = (char *)malloc(9 + strlen(info->selector_name));
2432 if (info->method != nullptr) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002433 if (Arch == Triple::x86_64)
2434 strcpy(info->method, "-[%rdi ");
2435 else if (Arch == Triple::aarch64)
2436 strcpy(info->method, "-[x0 ");
2437 else
2438 strcpy(info->method, "-[r? ");
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002439 strcat(info->method, info->selector_name);
2440 strcat(info->method, "]");
2441 *ReferenceName = info->method;
2442 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2443 }
2444 }
2445 info->class_name = nullptr;
2446 }
2447 } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002448 if (info->selector_name != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002449 if (info->method != nullptr)
2450 free(info->method);
2451 info->method = (char *)malloc(17 + strlen(info->selector_name));
2452 if (info->method != nullptr) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002453 if (Arch == Triple::x86_64)
2454 strcpy(info->method, "-[[%rdi super] ");
2455 else if (Arch == Triple::aarch64)
2456 strcpy(info->method, "-[[x0 super] ");
2457 else
2458 strcpy(info->method, "-[[r? super] ");
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002459 strcat(info->method, info->selector_name);
2460 strcat(info->method, "]");
2461 *ReferenceName = info->method;
2462 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2463 }
2464 info->class_name = nullptr;
2465 }
2466 }
2467 }
2468}
2469
2470// GuessPointerPointer() is passed the address of what might be a pointer to
2471// a reference to an Objective-C class, selector, message ref or cfstring.
2472// If so the value of the pointer is returned and one of the booleans are set
2473// to true. If not zero is returned and all the booleans are set to false.
2474static uint64_t GuessPointerPointer(uint64_t ReferenceValue,
2475 struct DisassembleInfo *info,
2476 bool &classref, bool &selref, bool &msgref,
2477 bool &cfstring) {
2478 classref = false;
2479 selref = false;
2480 msgref = false;
2481 cfstring = false;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002482 for (const auto &Load : info->O->load_commands()) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002483 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2484 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2485 for (unsigned J = 0; J < Seg.nsects; ++J) {
2486 MachO::section_64 Sec = info->O->getSection64(Load, J);
2487 if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 ||
2488 strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2489 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 ||
2490 strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 ||
2491 strncmp(Sec.sectname, "__cfstring", 16) == 0) &&
2492 ReferenceValue >= Sec.addr &&
2493 ReferenceValue < Sec.addr + Sec.size) {
2494 uint64_t sect_offset = ReferenceValue - Sec.addr;
2495 uint64_t object_offset = Sec.offset + sect_offset;
2496 StringRef MachOContents = info->O->getData();
2497 uint64_t object_size = MachOContents.size();
2498 const char *object_addr = (const char *)MachOContents.data();
2499 if (object_offset < object_size) {
2500 uint64_t pointer_value;
2501 memcpy(&pointer_value, object_addr + object_offset,
2502 sizeof(uint64_t));
2503 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2504 sys::swapByteOrder(pointer_value);
2505 if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0)
2506 selref = true;
2507 else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2508 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0)
2509 classref = true;
2510 else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 &&
2511 ReferenceValue + 8 < Sec.addr + Sec.size) {
2512 msgref = true;
2513 memcpy(&pointer_value, object_addr + object_offset + 8,
2514 sizeof(uint64_t));
2515 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2516 sys::swapByteOrder(pointer_value);
2517 } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0)
2518 cfstring = true;
2519 return pointer_value;
2520 } else {
2521 return 0;
2522 }
2523 }
2524 }
2525 }
2526 // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002527 }
2528 return 0;
2529}
2530
2531// get_pointer_64 returns a pointer to the bytes in the object file at the
2532// Address from a section in the Mach-O file. And indirectly returns the
2533// offset into the section, number of bytes left in the section past the offset
2534// and which section is was being referenced. If the Address is not in a
2535// section nullptr is returned.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00002536static const char *get_pointer_64(uint64_t Address, uint32_t &offset,
2537 uint32_t &left, SectionRef &S,
Kevin Enderby846c0002015-04-16 17:19:59 +00002538 DisassembleInfo *info,
2539 bool objc_only = false) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002540 offset = 0;
2541 left = 0;
2542 S = SectionRef();
2543 for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) {
2544 uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress();
2545 uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize();
Kevin Enderby46e642f2015-10-08 22:50:55 +00002546 if (SectSize == 0)
2547 continue;
Kevin Enderby846c0002015-04-16 17:19:59 +00002548 if (objc_only) {
2549 StringRef SectName;
2550 ((*(info->Sections))[SectIdx]).getName(SectName);
2551 DataRefImpl Ref = ((*(info->Sections))[SectIdx]).getRawDataRefImpl();
2552 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
2553 if (SegName != "__OBJC" && SectName != "__cstring")
2554 continue;
2555 }
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002556 if (Address >= SectAddress && Address < SectAddress + SectSize) {
2557 S = (*(info->Sections))[SectIdx];
2558 offset = Address - SectAddress;
2559 left = SectSize - offset;
2560 StringRef SectContents;
2561 ((*(info->Sections))[SectIdx]).getContents(SectContents);
2562 return SectContents.data() + offset;
2563 }
2564 }
2565 return nullptr;
2566}
2567
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002568static const char *get_pointer_32(uint32_t Address, uint32_t &offset,
2569 uint32_t &left, SectionRef &S,
Kevin Enderby846c0002015-04-16 17:19:59 +00002570 DisassembleInfo *info,
2571 bool objc_only = false) {
2572 return get_pointer_64(Address, offset, left, S, info, objc_only);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002573}
2574
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002575// get_symbol_64() returns the name of a symbol (or nullptr) and the address of
2576// the symbol indirectly through n_value. Based on the relocation information
2577// for the specified section offset in the specified section reference.
Kevin Enderby0fc11822015-04-01 20:57:01 +00002578// If no relocation information is found and a non-zero ReferenceValue for the
2579// symbol is passed, look up that address in the info's AddrMap.
Rafael Espindolad7a32ea2015-06-24 10:20:30 +00002580static const char *get_symbol_64(uint32_t sect_offset, SectionRef S,
2581 DisassembleInfo *info, uint64_t &n_value,
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +00002582 uint64_t ReferenceValue = 0) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002583 n_value = 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00002584 if (!info->verbose)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002585 return nullptr;
2586
2587 // See if there is an external relocation entry at the sect_offset.
2588 bool reloc_found = false;
2589 DataRefImpl Rel;
2590 MachO::any_relocation_info RE;
2591 bool isExtern = false;
2592 SymbolRef Symbol;
2593 for (const RelocationRef &Reloc : S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00002594 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002595 if (RelocOffset == sect_offset) {
2596 Rel = Reloc.getRawDataRefImpl();
2597 RE = info->O->getRelocation(Rel);
2598 if (info->O->isRelocationScattered(RE))
2599 continue;
2600 isExtern = info->O->getPlainRelocationExternal(RE);
2601 if (isExtern) {
2602 symbol_iterator RelocSym = Reloc.getSymbol();
2603 Symbol = *RelocSym;
2604 }
2605 reloc_found = true;
2606 break;
2607 }
2608 }
2609 // If there is an external relocation entry for a symbol in this section
2610 // at this section_offset then use that symbol's value for the n_value
2611 // and return its name.
2612 const char *SymbolName = nullptr;
2613 if (reloc_found && isExtern) {
Rafael Espindoladea00162015-07-03 17:44:18 +00002614 n_value = Symbol.getValue();
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002615 Expected<StringRef> NameOrError = Symbol.getName();
2616 if (!NameOrError) {
2617 std::string Buf;
2618 raw_string_ostream OS(Buf);
2619 logAllUnhandledErrors(NameOrError.takeError(), OS, "");
2620 OS.flush();
2621 report_fatal_error(Buf);
2622 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002623 StringRef Name = *NameOrError;
2624 if (!Name.empty()) {
2625 SymbolName = Name.data();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002626 return SymbolName;
2627 }
2628 }
2629
2630 // TODO: For fully linked images, look through the external relocation
2631 // entries off the dynamic symtab command. For these the r_offset is from the
2632 // start of the first writeable segment in the Mach-O file. So the offset
2633 // to this section from that segment is passed to this routine by the caller,
2634 // as the database_offset. Which is the difference of the section's starting
2635 // address and the first writable segment.
2636 //
2637 // NOTE: need add passing the database_offset to this routine.
2638
Kevin Enderby0fc11822015-04-01 20:57:01 +00002639 // We did not find an external relocation entry so look up the ReferenceValue
2640 // as an address of a symbol and if found return that symbol's name.
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +00002641 SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002642
2643 return SymbolName;
2644}
2645
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002646static const char *get_symbol_32(uint32_t sect_offset, SectionRef S,
2647 DisassembleInfo *info,
2648 uint32_t ReferenceValue) {
2649 uint64_t n_value64;
2650 return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue);
2651}
2652
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002653// These are structs in the Objective-C meta data and read to produce the
2654// comments for disassembly. While these are part of the ABI they are no
2655// public defintions. So the are here not in include/llvm/Support/MachO.h .
2656
2657// The cfstring object in a 64-bit Mach-O file.
2658struct cfstring64_t {
2659 uint64_t isa; // class64_t * (64-bit pointer)
2660 uint64_t flags; // flag bits
2661 uint64_t characters; // char * (64-bit pointer)
2662 uint64_t length; // number of non-NULL characters in above
2663};
2664
2665// The class object in a 64-bit Mach-O file.
2666struct class64_t {
2667 uint64_t isa; // class64_t * (64-bit pointer)
2668 uint64_t superclass; // class64_t * (64-bit pointer)
2669 uint64_t cache; // Cache (64-bit pointer)
2670 uint64_t vtable; // IMP * (64-bit pointer)
2671 uint64_t data; // class_ro64_t * (64-bit pointer)
2672};
2673
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002674struct class32_t {
2675 uint32_t isa; /* class32_t * (32-bit pointer) */
2676 uint32_t superclass; /* class32_t * (32-bit pointer) */
2677 uint32_t cache; /* Cache (32-bit pointer) */
2678 uint32_t vtable; /* IMP * (32-bit pointer) */
2679 uint32_t data; /* class_ro32_t * (32-bit pointer) */
2680};
2681
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002682struct class_ro64_t {
2683 uint32_t flags;
2684 uint32_t instanceStart;
2685 uint32_t instanceSize;
2686 uint32_t reserved;
2687 uint64_t ivarLayout; // const uint8_t * (64-bit pointer)
2688 uint64_t name; // const char * (64-bit pointer)
2689 uint64_t baseMethods; // const method_list_t * (64-bit pointer)
2690 uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer)
2691 uint64_t ivars; // const ivar_list_t * (64-bit pointer)
2692 uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer)
2693 uint64_t baseProperties; // const struct objc_property_list (64-bit pointer)
2694};
2695
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002696struct class_ro32_t {
2697 uint32_t flags;
2698 uint32_t instanceStart;
2699 uint32_t instanceSize;
2700 uint32_t ivarLayout; /* const uint8_t * (32-bit pointer) */
2701 uint32_t name; /* const char * (32-bit pointer) */
2702 uint32_t baseMethods; /* const method_list_t * (32-bit pointer) */
2703 uint32_t baseProtocols; /* const protocol_list_t * (32-bit pointer) */
2704 uint32_t ivars; /* const ivar_list_t * (32-bit pointer) */
2705 uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */
2706 uint32_t baseProperties; /* const struct objc_property_list *
2707 (32-bit pointer) */
2708};
2709
2710/* Values for class_ro{64,32}_t->flags */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002711#define RO_META (1 << 0)
2712#define RO_ROOT (1 << 1)
2713#define RO_HAS_CXX_STRUCTORS (1 << 2)
2714
2715struct method_list64_t {
2716 uint32_t entsize;
2717 uint32_t count;
2718 /* struct method64_t first; These structures follow inline */
2719};
2720
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002721struct method_list32_t {
2722 uint32_t entsize;
2723 uint32_t count;
2724 /* struct method32_t first; These structures follow inline */
2725};
2726
Kevin Enderby0fc11822015-04-01 20:57:01 +00002727struct method64_t {
2728 uint64_t name; /* SEL (64-bit pointer) */
2729 uint64_t types; /* const char * (64-bit pointer) */
2730 uint64_t imp; /* IMP (64-bit pointer) */
2731};
2732
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002733struct method32_t {
2734 uint32_t name; /* SEL (32-bit pointer) */
2735 uint32_t types; /* const char * (32-bit pointer) */
2736 uint32_t imp; /* IMP (32-bit pointer) */
2737};
2738
Kevin Enderby0fc11822015-04-01 20:57:01 +00002739struct protocol_list64_t {
2740 uint64_t count; /* uintptr_t (a 64-bit value) */
2741 /* struct protocol64_t * list[0]; These pointers follow inline */
2742};
2743
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002744struct protocol_list32_t {
2745 uint32_t count; /* uintptr_t (a 32-bit value) */
2746 /* struct protocol32_t * list[0]; These pointers follow inline */
2747};
2748
Kevin Enderby0fc11822015-04-01 20:57:01 +00002749struct protocol64_t {
2750 uint64_t isa; /* id * (64-bit pointer) */
2751 uint64_t name; /* const char * (64-bit pointer) */
2752 uint64_t protocols; /* struct protocol_list64_t *
2753 (64-bit pointer) */
2754 uint64_t instanceMethods; /* method_list_t * (64-bit pointer) */
2755 uint64_t classMethods; /* method_list_t * (64-bit pointer) */
2756 uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */
2757 uint64_t optionalClassMethods; /* method_list_t * (64-bit pointer) */
2758 uint64_t instanceProperties; /* struct objc_property_list *
2759 (64-bit pointer) */
2760};
2761
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002762struct protocol32_t {
2763 uint32_t isa; /* id * (32-bit pointer) */
2764 uint32_t name; /* const char * (32-bit pointer) */
2765 uint32_t protocols; /* struct protocol_list_t *
2766 (32-bit pointer) */
2767 uint32_t instanceMethods; /* method_list_t * (32-bit pointer) */
2768 uint32_t classMethods; /* method_list_t * (32-bit pointer) */
2769 uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */
2770 uint32_t optionalClassMethods; /* method_list_t * (32-bit pointer) */
2771 uint32_t instanceProperties; /* struct objc_property_list *
2772 (32-bit pointer) */
2773};
2774
Kevin Enderby0fc11822015-04-01 20:57:01 +00002775struct ivar_list64_t {
2776 uint32_t entsize;
2777 uint32_t count;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002778 /* struct ivar64_t first; These structures follow inline */
2779};
2780
2781struct ivar_list32_t {
2782 uint32_t entsize;
2783 uint32_t count;
2784 /* struct ivar32_t first; These structures follow inline */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002785};
2786
2787struct ivar64_t {
2788 uint64_t offset; /* uintptr_t * (64-bit pointer) */
2789 uint64_t name; /* const char * (64-bit pointer) */
2790 uint64_t type; /* const char * (64-bit pointer) */
2791 uint32_t alignment;
2792 uint32_t size;
2793};
2794
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002795struct ivar32_t {
2796 uint32_t offset; /* uintptr_t * (32-bit pointer) */
2797 uint32_t name; /* const char * (32-bit pointer) */
2798 uint32_t type; /* const char * (32-bit pointer) */
2799 uint32_t alignment;
2800 uint32_t size;
2801};
2802
Kevin Enderby0fc11822015-04-01 20:57:01 +00002803struct objc_property_list64 {
2804 uint32_t entsize;
2805 uint32_t count;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002806 /* struct objc_property64 first; These structures follow inline */
2807};
2808
2809struct objc_property_list32 {
2810 uint32_t entsize;
2811 uint32_t count;
2812 /* struct objc_property32 first; These structures follow inline */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002813};
2814
2815struct objc_property64 {
2816 uint64_t name; /* const char * (64-bit pointer) */
2817 uint64_t attributes; /* const char * (64-bit pointer) */
2818};
2819
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002820struct objc_property32 {
2821 uint32_t name; /* const char * (32-bit pointer) */
2822 uint32_t attributes; /* const char * (32-bit pointer) */
2823};
2824
Kevin Enderby0fc11822015-04-01 20:57:01 +00002825struct category64_t {
2826 uint64_t name; /* const char * (64-bit pointer) */
2827 uint64_t cls; /* struct class_t * (64-bit pointer) */
2828 uint64_t instanceMethods; /* struct method_list_t * (64-bit pointer) */
2829 uint64_t classMethods; /* struct method_list_t * (64-bit pointer) */
2830 uint64_t protocols; /* struct protocol_list_t * (64-bit pointer) */
2831 uint64_t instanceProperties; /* struct objc_property_list *
2832 (64-bit pointer) */
2833};
2834
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002835struct category32_t {
2836 uint32_t name; /* const char * (32-bit pointer) */
2837 uint32_t cls; /* struct class_t * (32-bit pointer) */
2838 uint32_t instanceMethods; /* struct method_list_t * (32-bit pointer) */
2839 uint32_t classMethods; /* struct method_list_t * (32-bit pointer) */
2840 uint32_t protocols; /* struct protocol_list_t * (32-bit pointer) */
2841 uint32_t instanceProperties; /* struct objc_property_list *
2842 (32-bit pointer) */
2843};
2844
Kevin Enderby0fc11822015-04-01 20:57:01 +00002845struct objc_image_info64 {
2846 uint32_t version;
2847 uint32_t flags;
2848};
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002849struct objc_image_info32 {
2850 uint32_t version;
2851 uint32_t flags;
2852};
Kevin Enderby846c0002015-04-16 17:19:59 +00002853struct imageInfo_t {
2854 uint32_t version;
2855 uint32_t flags;
2856};
Kevin Enderby0fc11822015-04-01 20:57:01 +00002857/* masks for objc_image_info.flags */
2858#define OBJC_IMAGE_IS_REPLACEMENT (1 << 0)
2859#define OBJC_IMAGE_SUPPORTS_GC (1 << 1)
2860
2861struct message_ref64 {
2862 uint64_t imp; /* IMP (64-bit pointer) */
2863 uint64_t sel; /* SEL (64-bit pointer) */
2864};
2865
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002866struct message_ref32 {
2867 uint32_t imp; /* IMP (32-bit pointer) */
2868 uint32_t sel; /* SEL (32-bit pointer) */
2869};
2870
Kevin Enderby846c0002015-04-16 17:19:59 +00002871// Objective-C 1 (32-bit only) meta data structs.
2872
2873struct objc_module_t {
2874 uint32_t version;
2875 uint32_t size;
2876 uint32_t name; /* char * (32-bit pointer) */
2877 uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */
2878};
2879
2880struct objc_symtab_t {
2881 uint32_t sel_ref_cnt;
2882 uint32_t refs; /* SEL * (32-bit pointer) */
2883 uint16_t cls_def_cnt;
2884 uint16_t cat_def_cnt;
2885 // uint32_t defs[1]; /* void * (32-bit pointer) variable size */
2886};
2887
2888struct objc_class_t {
2889 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2890 uint32_t super_class; /* struct objc_class * (32-bit pointer) */
2891 uint32_t name; /* const char * (32-bit pointer) */
2892 int32_t version;
2893 int32_t info;
2894 int32_t instance_size;
2895 uint32_t ivars; /* struct objc_ivar_list * (32-bit pointer) */
2896 uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */
2897 uint32_t cache; /* struct objc_cache * (32-bit pointer) */
2898 uint32_t protocols; /* struct objc_protocol_list * (32-bit pointer) */
2899};
2900
2901#define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask))
2902// class is not a metaclass
2903#define CLS_CLASS 0x1
2904// class is a metaclass
2905#define CLS_META 0x2
2906
2907struct objc_category_t {
2908 uint32_t category_name; /* char * (32-bit pointer) */
2909 uint32_t class_name; /* char * (32-bit pointer) */
2910 uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */
2911 uint32_t class_methods; /* struct objc_method_list * (32-bit pointer) */
2912 uint32_t protocols; /* struct objc_protocol_list * (32-bit ptr) */
2913};
2914
2915struct objc_ivar_t {
2916 uint32_t ivar_name; /* char * (32-bit pointer) */
2917 uint32_t ivar_type; /* char * (32-bit pointer) */
2918 int32_t ivar_offset;
2919};
2920
2921struct objc_ivar_list_t {
2922 int32_t ivar_count;
2923 // struct objc_ivar_t ivar_list[1]; /* variable length structure */
2924};
2925
2926struct objc_method_list_t {
2927 uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */
2928 int32_t method_count;
2929 // struct objc_method_t method_list[1]; /* variable length structure */
2930};
2931
2932struct objc_method_t {
2933 uint32_t method_name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2934 uint32_t method_types; /* char * (32-bit pointer) */
2935 uint32_t method_imp; /* IMP, aka function pointer, (*IMP)(id, SEL, ...)
2936 (32-bit pointer) */
2937};
2938
2939struct objc_protocol_list_t {
2940 uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */
2941 int32_t count;
2942 // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t *
2943 // (32-bit pointer) */
2944};
2945
2946struct objc_protocol_t {
2947 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2948 uint32_t protocol_name; /* char * (32-bit pointer) */
2949 uint32_t protocol_list; /* struct objc_protocol_list * (32-bit pointer) */
2950 uint32_t instance_methods; /* struct objc_method_description_list *
2951 (32-bit pointer) */
2952 uint32_t class_methods; /* struct objc_method_description_list *
2953 (32-bit pointer) */
2954};
2955
2956struct objc_method_description_list_t {
2957 int32_t count;
2958 // struct objc_method_description_t list[1];
2959};
2960
2961struct objc_method_description_t {
2962 uint32_t name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2963 uint32_t types; /* char * (32-bit pointer) */
2964};
2965
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002966inline void swapStruct(struct cfstring64_t &cfs) {
2967 sys::swapByteOrder(cfs.isa);
2968 sys::swapByteOrder(cfs.flags);
2969 sys::swapByteOrder(cfs.characters);
2970 sys::swapByteOrder(cfs.length);
2971}
2972
2973inline void swapStruct(struct class64_t &c) {
2974 sys::swapByteOrder(c.isa);
2975 sys::swapByteOrder(c.superclass);
2976 sys::swapByteOrder(c.cache);
2977 sys::swapByteOrder(c.vtable);
2978 sys::swapByteOrder(c.data);
2979}
2980
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002981inline void swapStruct(struct class32_t &c) {
2982 sys::swapByteOrder(c.isa);
2983 sys::swapByteOrder(c.superclass);
2984 sys::swapByteOrder(c.cache);
2985 sys::swapByteOrder(c.vtable);
2986 sys::swapByteOrder(c.data);
2987}
2988
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002989inline void swapStruct(struct class_ro64_t &cro) {
2990 sys::swapByteOrder(cro.flags);
2991 sys::swapByteOrder(cro.instanceStart);
2992 sys::swapByteOrder(cro.instanceSize);
2993 sys::swapByteOrder(cro.reserved);
2994 sys::swapByteOrder(cro.ivarLayout);
2995 sys::swapByteOrder(cro.name);
2996 sys::swapByteOrder(cro.baseMethods);
2997 sys::swapByteOrder(cro.baseProtocols);
2998 sys::swapByteOrder(cro.ivars);
2999 sys::swapByteOrder(cro.weakIvarLayout);
3000 sys::swapByteOrder(cro.baseProperties);
3001}
3002
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003003inline void swapStruct(struct class_ro32_t &cro) {
3004 sys::swapByteOrder(cro.flags);
3005 sys::swapByteOrder(cro.instanceStart);
3006 sys::swapByteOrder(cro.instanceSize);
3007 sys::swapByteOrder(cro.ivarLayout);
3008 sys::swapByteOrder(cro.name);
3009 sys::swapByteOrder(cro.baseMethods);
3010 sys::swapByteOrder(cro.baseProtocols);
3011 sys::swapByteOrder(cro.ivars);
3012 sys::swapByteOrder(cro.weakIvarLayout);
3013 sys::swapByteOrder(cro.baseProperties);
3014}
3015
Kevin Enderby0fc11822015-04-01 20:57:01 +00003016inline void swapStruct(struct method_list64_t &ml) {
3017 sys::swapByteOrder(ml.entsize);
3018 sys::swapByteOrder(ml.count);
3019}
3020
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003021inline void swapStruct(struct method_list32_t &ml) {
3022 sys::swapByteOrder(ml.entsize);
3023 sys::swapByteOrder(ml.count);
3024}
3025
Kevin Enderby0fc11822015-04-01 20:57:01 +00003026inline void swapStruct(struct method64_t &m) {
3027 sys::swapByteOrder(m.name);
3028 sys::swapByteOrder(m.types);
3029 sys::swapByteOrder(m.imp);
3030}
3031
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003032inline void swapStruct(struct method32_t &m) {
3033 sys::swapByteOrder(m.name);
3034 sys::swapByteOrder(m.types);
3035 sys::swapByteOrder(m.imp);
3036}
3037
Kevin Enderby0fc11822015-04-01 20:57:01 +00003038inline void swapStruct(struct protocol_list64_t &pl) {
3039 sys::swapByteOrder(pl.count);
3040}
3041
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003042inline void swapStruct(struct protocol_list32_t &pl) {
3043 sys::swapByteOrder(pl.count);
3044}
3045
Kevin Enderby0fc11822015-04-01 20:57:01 +00003046inline void swapStruct(struct protocol64_t &p) {
3047 sys::swapByteOrder(p.isa);
3048 sys::swapByteOrder(p.name);
3049 sys::swapByteOrder(p.protocols);
3050 sys::swapByteOrder(p.instanceMethods);
3051 sys::swapByteOrder(p.classMethods);
3052 sys::swapByteOrder(p.optionalInstanceMethods);
3053 sys::swapByteOrder(p.optionalClassMethods);
3054 sys::swapByteOrder(p.instanceProperties);
3055}
3056
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003057inline void swapStruct(struct protocol32_t &p) {
3058 sys::swapByteOrder(p.isa);
3059 sys::swapByteOrder(p.name);
3060 sys::swapByteOrder(p.protocols);
3061 sys::swapByteOrder(p.instanceMethods);
3062 sys::swapByteOrder(p.classMethods);
3063 sys::swapByteOrder(p.optionalInstanceMethods);
3064 sys::swapByteOrder(p.optionalClassMethods);
3065 sys::swapByteOrder(p.instanceProperties);
3066}
3067
Kevin Enderby0fc11822015-04-01 20:57:01 +00003068inline void swapStruct(struct ivar_list64_t &il) {
3069 sys::swapByteOrder(il.entsize);
3070 sys::swapByteOrder(il.count);
3071}
3072
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003073inline void swapStruct(struct ivar_list32_t &il) {
3074 sys::swapByteOrder(il.entsize);
3075 sys::swapByteOrder(il.count);
3076}
3077
Kevin Enderby0fc11822015-04-01 20:57:01 +00003078inline void swapStruct(struct ivar64_t &i) {
3079 sys::swapByteOrder(i.offset);
3080 sys::swapByteOrder(i.name);
3081 sys::swapByteOrder(i.type);
3082 sys::swapByteOrder(i.alignment);
3083 sys::swapByteOrder(i.size);
3084}
3085
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003086inline void swapStruct(struct ivar32_t &i) {
3087 sys::swapByteOrder(i.offset);
3088 sys::swapByteOrder(i.name);
3089 sys::swapByteOrder(i.type);
3090 sys::swapByteOrder(i.alignment);
3091 sys::swapByteOrder(i.size);
3092}
3093
Kevin Enderby0fc11822015-04-01 20:57:01 +00003094inline void swapStruct(struct objc_property_list64 &pl) {
3095 sys::swapByteOrder(pl.entsize);
3096 sys::swapByteOrder(pl.count);
3097}
3098
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003099inline void swapStruct(struct objc_property_list32 &pl) {
3100 sys::swapByteOrder(pl.entsize);
3101 sys::swapByteOrder(pl.count);
3102}
3103
Kevin Enderby0fc11822015-04-01 20:57:01 +00003104inline void swapStruct(struct objc_property64 &op) {
3105 sys::swapByteOrder(op.name);
3106 sys::swapByteOrder(op.attributes);
3107}
3108
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003109inline void swapStruct(struct objc_property32 &op) {
3110 sys::swapByteOrder(op.name);
3111 sys::swapByteOrder(op.attributes);
3112}
3113
Kevin Enderby0fc11822015-04-01 20:57:01 +00003114inline void swapStruct(struct category64_t &c) {
3115 sys::swapByteOrder(c.name);
3116 sys::swapByteOrder(c.cls);
3117 sys::swapByteOrder(c.instanceMethods);
3118 sys::swapByteOrder(c.classMethods);
3119 sys::swapByteOrder(c.protocols);
3120 sys::swapByteOrder(c.instanceProperties);
3121}
3122
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003123inline void swapStruct(struct category32_t &c) {
3124 sys::swapByteOrder(c.name);
3125 sys::swapByteOrder(c.cls);
3126 sys::swapByteOrder(c.instanceMethods);
3127 sys::swapByteOrder(c.classMethods);
3128 sys::swapByteOrder(c.protocols);
3129 sys::swapByteOrder(c.instanceProperties);
3130}
3131
Kevin Enderby0fc11822015-04-01 20:57:01 +00003132inline void swapStruct(struct objc_image_info64 &o) {
3133 sys::swapByteOrder(o.version);
3134 sys::swapByteOrder(o.flags);
3135}
3136
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003137inline void swapStruct(struct objc_image_info32 &o) {
3138 sys::swapByteOrder(o.version);
3139 sys::swapByteOrder(o.flags);
3140}
3141
Kevin Enderby846c0002015-04-16 17:19:59 +00003142inline void swapStruct(struct imageInfo_t &o) {
3143 sys::swapByteOrder(o.version);
3144 sys::swapByteOrder(o.flags);
3145}
3146
Kevin Enderby0fc11822015-04-01 20:57:01 +00003147inline void swapStruct(struct message_ref64 &mr) {
3148 sys::swapByteOrder(mr.imp);
3149 sys::swapByteOrder(mr.sel);
3150}
3151
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003152inline void swapStruct(struct message_ref32 &mr) {
3153 sys::swapByteOrder(mr.imp);
3154 sys::swapByteOrder(mr.sel);
3155}
3156
Kevin Enderby846c0002015-04-16 17:19:59 +00003157inline void swapStruct(struct objc_module_t &module) {
3158 sys::swapByteOrder(module.version);
3159 sys::swapByteOrder(module.size);
3160 sys::swapByteOrder(module.name);
3161 sys::swapByteOrder(module.symtab);
Jingyue Wufedecc42015-04-16 18:43:44 +00003162}
Kevin Enderby846c0002015-04-16 17:19:59 +00003163
3164inline void swapStruct(struct objc_symtab_t &symtab) {
3165 sys::swapByteOrder(symtab.sel_ref_cnt);
3166 sys::swapByteOrder(symtab.refs);
3167 sys::swapByteOrder(symtab.cls_def_cnt);
3168 sys::swapByteOrder(symtab.cat_def_cnt);
Jingyue Wufedecc42015-04-16 18:43:44 +00003169}
Kevin Enderby846c0002015-04-16 17:19:59 +00003170
3171inline void swapStruct(struct objc_class_t &objc_class) {
3172 sys::swapByteOrder(objc_class.isa);
3173 sys::swapByteOrder(objc_class.super_class);
3174 sys::swapByteOrder(objc_class.name);
3175 sys::swapByteOrder(objc_class.version);
3176 sys::swapByteOrder(objc_class.info);
3177 sys::swapByteOrder(objc_class.instance_size);
3178 sys::swapByteOrder(objc_class.ivars);
3179 sys::swapByteOrder(objc_class.methodLists);
3180 sys::swapByteOrder(objc_class.cache);
3181 sys::swapByteOrder(objc_class.protocols);
Jingyue Wufedecc42015-04-16 18:43:44 +00003182}
Kevin Enderby846c0002015-04-16 17:19:59 +00003183
3184inline void swapStruct(struct objc_category_t &objc_category) {
3185 sys::swapByteOrder(objc_category.category_name);
3186 sys::swapByteOrder(objc_category.class_name);
3187 sys::swapByteOrder(objc_category.instance_methods);
3188 sys::swapByteOrder(objc_category.class_methods);
3189 sys::swapByteOrder(objc_category.protocols);
3190}
3191
3192inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) {
3193 sys::swapByteOrder(objc_ivar_list.ivar_count);
3194}
3195
3196inline void swapStruct(struct objc_ivar_t &objc_ivar) {
3197 sys::swapByteOrder(objc_ivar.ivar_name);
3198 sys::swapByteOrder(objc_ivar.ivar_type);
3199 sys::swapByteOrder(objc_ivar.ivar_offset);
Jingyue Wufedecc42015-04-16 18:43:44 +00003200}
Kevin Enderby846c0002015-04-16 17:19:59 +00003201
3202inline void swapStruct(struct objc_method_list_t &method_list) {
3203 sys::swapByteOrder(method_list.obsolete);
3204 sys::swapByteOrder(method_list.method_count);
3205}
3206
3207inline void swapStruct(struct objc_method_t &method) {
3208 sys::swapByteOrder(method.method_name);
3209 sys::swapByteOrder(method.method_types);
3210 sys::swapByteOrder(method.method_imp);
3211}
3212
3213inline void swapStruct(struct objc_protocol_list_t &protocol_list) {
3214 sys::swapByteOrder(protocol_list.next);
3215 sys::swapByteOrder(protocol_list.count);
3216}
3217
3218inline void swapStruct(struct objc_protocol_t &protocol) {
3219 sys::swapByteOrder(protocol.isa);
3220 sys::swapByteOrder(protocol.protocol_name);
3221 sys::swapByteOrder(protocol.protocol_list);
3222 sys::swapByteOrder(protocol.instance_methods);
3223 sys::swapByteOrder(protocol.class_methods);
3224}
3225
3226inline void swapStruct(struct objc_method_description_list_t &mdl) {
3227 sys::swapByteOrder(mdl.count);
3228}
3229
3230inline void swapStruct(struct objc_method_description_t &md) {
3231 sys::swapByteOrder(md.name);
3232 sys::swapByteOrder(md.types);
3233}
3234
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003235static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
3236 struct DisassembleInfo *info);
3237
3238// get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
3239// to an Objective-C class and returns the class name. It is also passed the
3240// address of the pointer, so when the pointer is zero as it can be in an .o
3241// file, that is used to look for an external relocation entry with a symbol
3242// name.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003243static const char *get_objc2_64bit_class_name(uint64_t pointer_value,
3244 uint64_t ReferenceValue,
3245 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003246 const char *r;
3247 uint32_t offset, left;
3248 SectionRef S;
3249
3250 // The pointer_value can be 0 in an object file and have a relocation
3251 // entry for the class symbol at the ReferenceValue (the address of the
3252 // pointer).
3253 if (pointer_value == 0) {
3254 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3255 if (r == nullptr || left < sizeof(uint64_t))
3256 return nullptr;
3257 uint64_t n_value;
3258 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3259 if (symbol_name == nullptr)
3260 return nullptr;
Hans Wennborgdb53e302014-10-23 21:59:17 +00003261 const char *class_name = strrchr(symbol_name, '$');
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003262 if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0')
3263 return class_name + 2;
3264 else
3265 return nullptr;
3266 }
3267
3268 // The case were the pointer_value is non-zero and points to a class defined
3269 // in this Mach-O file.
3270 r = get_pointer_64(pointer_value, offset, left, S, info);
3271 if (r == nullptr || left < sizeof(struct class64_t))
3272 return nullptr;
3273 struct class64_t c;
3274 memcpy(&c, r, sizeof(struct class64_t));
3275 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3276 swapStruct(c);
3277 if (c.data == 0)
3278 return nullptr;
3279 r = get_pointer_64(c.data, offset, left, S, info);
3280 if (r == nullptr || left < sizeof(struct class_ro64_t))
3281 return nullptr;
3282 struct class_ro64_t cro;
3283 memcpy(&cro, r, sizeof(struct class_ro64_t));
3284 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3285 swapStruct(cro);
3286 if (cro.name == 0)
3287 return nullptr;
3288 const char *name = get_pointer_64(cro.name, offset, left, S, info);
3289 return name;
3290}
3291
3292// get_objc2_64bit_cfstring_name is used for disassembly and is passed a
3293// pointer to a cfstring and returns its name or nullptr.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003294static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue,
3295 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003296 const char *r, *name;
3297 uint32_t offset, left;
3298 SectionRef S;
3299 struct cfstring64_t cfs;
3300 uint64_t cfs_characters;
3301
3302 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3303 if (r == nullptr || left < sizeof(struct cfstring64_t))
3304 return nullptr;
3305 memcpy(&cfs, r, sizeof(struct cfstring64_t));
3306 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3307 swapStruct(cfs);
3308 if (cfs.characters == 0) {
3309 uint64_t n_value;
3310 const char *symbol_name = get_symbol_64(
3311 offset + offsetof(struct cfstring64_t, characters), S, info, n_value);
3312 if (symbol_name == nullptr)
3313 return nullptr;
3314 cfs_characters = n_value;
3315 } else
3316 cfs_characters = cfs.characters;
3317 name = get_pointer_64(cfs_characters, offset, left, S, info);
3318
3319 return name;
3320}
3321
3322// get_objc2_64bit_selref() is used for disassembly and is passed a the address
3323// of a pointer to an Objective-C selector reference when the pointer value is
3324// zero as in a .o file and is likely to have a external relocation entry with
3325// who's symbol's n_value is the real pointer to the selector name. If that is
3326// the case the real pointer to the selector name is returned else 0 is
3327// returned
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003328static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue,
3329 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003330 uint32_t offset, left;
3331 SectionRef S;
3332
3333 const char *r = get_pointer_64(ReferenceValue, offset, left, S, info);
3334 if (r == nullptr || left < sizeof(uint64_t))
3335 return 0;
3336 uint64_t n_value;
3337 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3338 if (symbol_name == nullptr)
3339 return 0;
3340 return n_value;
3341}
3342
Kevin Enderby0fc11822015-04-01 20:57:01 +00003343static const SectionRef get_section(MachOObjectFile *O, const char *segname,
3344 const char *sectname) {
3345 for (const SectionRef &Section : O->sections()) {
3346 StringRef SectName;
3347 Section.getName(SectName);
3348 DataRefImpl Ref = Section.getRawDataRefImpl();
3349 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3350 if (SegName == segname && SectName == sectname)
3351 return Section;
3352 }
3353 return SectionRef();
3354}
3355
3356static void
3357walk_pointer_list_64(const char *listname, const SectionRef S,
3358 MachOObjectFile *O, struct DisassembleInfo *info,
3359 void (*func)(uint64_t, struct DisassembleInfo *info)) {
3360 if (S == SectionRef())
3361 return;
3362
3363 StringRef SectName;
3364 S.getName(SectName);
3365 DataRefImpl Ref = S.getRawDataRefImpl();
3366 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3367 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3368
3369 StringRef BytesStr;
3370 S.getContents(BytesStr);
3371 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3372
3373 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) {
3374 uint32_t left = S.getSize() - i;
3375 uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t);
3376 uint64_t p = 0;
3377 memcpy(&p, Contents + i, size);
3378 if (i + sizeof(uint64_t) > S.getSize())
3379 outs() << listname << " list pointer extends past end of (" << SegName
3380 << "," << SectName << ") section\n";
3381 outs() << format("%016" PRIx64, S.getAddress() + i) << " ";
3382
3383 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3384 sys::swapByteOrder(p);
3385
3386 uint64_t n_value = 0;
3387 const char *name = get_symbol_64(i, S, info, n_value, p);
3388 if (name == nullptr)
3389 name = get_dyld_bind_info_symbolname(S.getAddress() + i, info);
3390
3391 if (n_value != 0) {
3392 outs() << format("0x%" PRIx64, n_value);
3393 if (p != 0)
3394 outs() << " + " << format("0x%" PRIx64, p);
3395 } else
3396 outs() << format("0x%" PRIx64, p);
3397 if (name != nullptr)
3398 outs() << " " << name;
3399 outs() << "\n";
3400
3401 p += n_value;
3402 if (func)
3403 func(p, info);
3404 }
3405}
3406
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003407static void
3408walk_pointer_list_32(const char *listname, const SectionRef S,
3409 MachOObjectFile *O, struct DisassembleInfo *info,
3410 void (*func)(uint32_t, struct DisassembleInfo *info)) {
3411 if (S == SectionRef())
3412 return;
3413
3414 StringRef SectName;
3415 S.getName(SectName);
3416 DataRefImpl Ref = S.getRawDataRefImpl();
3417 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3418 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3419
3420 StringRef BytesStr;
3421 S.getContents(BytesStr);
3422 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3423
3424 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) {
3425 uint32_t left = S.getSize() - i;
3426 uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t);
3427 uint32_t p = 0;
3428 memcpy(&p, Contents + i, size);
3429 if (i + sizeof(uint32_t) > S.getSize())
3430 outs() << listname << " list pointer extends past end of (" << SegName
3431 << "," << SectName << ") section\n";
Kevin Enderbycf261312015-04-06 22:33:43 +00003432 uint32_t Address = S.getAddress() + i;
3433 outs() << format("%08" PRIx32, Address) << " ";
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003434
3435 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3436 sys::swapByteOrder(p);
3437 outs() << format("0x%" PRIx32, p);
3438
3439 const char *name = get_symbol_32(i, S, info, p);
3440 if (name != nullptr)
3441 outs() << " " << name;
3442 outs() << "\n";
3443
3444 if (func)
3445 func(p, info);
3446 }
3447}
3448
3449static void print_layout_map(const char *layout_map, uint32_t left) {
Kevin Enderbya59824a2015-10-06 22:27:08 +00003450 if (layout_map == nullptr)
3451 return;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003452 outs() << " layout map: ";
3453 do {
3454 outs() << format("0x%02" PRIx32, (*layout_map) & 0xff) << " ";
3455 left--;
3456 layout_map++;
3457 } while (*layout_map != '\0' && left != 0);
3458 outs() << "\n";
3459}
3460
Kevin Enderby0fc11822015-04-01 20:57:01 +00003461static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) {
3462 uint32_t offset, left;
3463 SectionRef S;
3464 const char *layout_map;
3465
3466 if (p == 0)
3467 return;
3468 layout_map = get_pointer_64(p, offset, left, S, info);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003469 print_layout_map(layout_map, left);
3470}
3471
3472static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) {
3473 uint32_t offset, left;
3474 SectionRef S;
3475 const char *layout_map;
3476
3477 if (p == 0)
3478 return;
3479 layout_map = get_pointer_32(p, offset, left, S, info);
3480 print_layout_map(layout_map, left);
Kevin Enderby0fc11822015-04-01 20:57:01 +00003481}
3482
3483static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info,
3484 const char *indent) {
3485 struct method_list64_t ml;
3486 struct method64_t m;
3487 const char *r;
3488 uint32_t offset, xoffset, left, i;
3489 SectionRef S, xS;
3490 const char *name, *sym_name;
3491 uint64_t n_value;
3492
3493 r = get_pointer_64(p, offset, left, S, info);
3494 if (r == nullptr)
3495 return;
3496 memset(&ml, '\0', sizeof(struct method_list64_t));
3497 if (left < sizeof(struct method_list64_t)) {
3498 memcpy(&ml, r, left);
3499 outs() << " (method_list_t entends past the end of the section)\n";
3500 } else
3501 memcpy(&ml, r, sizeof(struct method_list64_t));
3502 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3503 swapStruct(ml);
3504 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3505 outs() << indent << "\t\t count " << ml.count << "\n";
3506
3507 p += sizeof(struct method_list64_t);
3508 offset += sizeof(struct method_list64_t);
3509 for (i = 0; i < ml.count; i++) {
3510 r = get_pointer_64(p, offset, left, S, info);
3511 if (r == nullptr)
3512 return;
3513 memset(&m, '\0', sizeof(struct method64_t));
3514 if (left < sizeof(struct method64_t)) {
Kevin Enderbya59824a2015-10-06 22:27:08 +00003515 memcpy(&m, r, left);
3516 outs() << indent << " (method_t extends past the end of the section)\n";
Kevin Enderby0fc11822015-04-01 20:57:01 +00003517 } else
3518 memcpy(&m, r, sizeof(struct method64_t));
3519 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3520 swapStruct(m);
3521
3522 outs() << indent << "\t\t name ";
3523 sym_name = get_symbol_64(offset + offsetof(struct method64_t, name), S,
3524 info, n_value, m.name);
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.name != 0)
3531 outs() << " + " << format("0x%" PRIx64, m.name);
3532 } else
3533 outs() << format("0x%" PRIx64, m.name);
3534 name = get_pointer_64(m.name + 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 types ";
3540 sym_name = get_symbol_64(offset + offsetof(struct method64_t, types), S,
3541 info, n_value, m.types);
3542 if (n_value != 0) {
3543 if (info->verbose && sym_name != nullptr)
3544 outs() << sym_name;
3545 else
3546 outs() << format("0x%" PRIx64, n_value);
3547 if (m.types != 0)
3548 outs() << " + " << format("0x%" PRIx64, m.types);
3549 } else
3550 outs() << format("0x%" PRIx64, m.types);
3551 name = get_pointer_64(m.types + n_value, xoffset, left, xS, info);
3552 if (name != nullptr)
3553 outs() << format(" %.*s", left, name);
3554 outs() << "\n";
3555
3556 outs() << indent << "\t\t imp ";
3557 name = get_symbol_64(offset + offsetof(struct method64_t, imp), S, info,
3558 n_value, m.imp);
3559 if (info->verbose && name == nullptr) {
3560 if (n_value != 0) {
3561 outs() << format("0x%" PRIx64, n_value) << " ";
3562 if (m.imp != 0)
3563 outs() << "+ " << format("0x%" PRIx64, m.imp) << " ";
3564 } else
3565 outs() << format("0x%" PRIx64, m.imp) << " ";
3566 }
3567 if (name != nullptr)
3568 outs() << name;
3569 outs() << "\n";
3570
3571 p += sizeof(struct method64_t);
3572 offset += sizeof(struct method64_t);
3573 }
3574}
3575
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003576static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info,
3577 const char *indent) {
3578 struct method_list32_t ml;
3579 struct method32_t m;
Kevin Enderby846c0002015-04-16 17:19:59 +00003580 const char *r, *name;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003581 uint32_t offset, xoffset, left, i;
3582 SectionRef S, xS;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003583
3584 r = get_pointer_32(p, offset, left, S, info);
3585 if (r == nullptr)
3586 return;
3587 memset(&ml, '\0', sizeof(struct method_list32_t));
3588 if (left < sizeof(struct method_list32_t)) {
3589 memcpy(&ml, r, left);
3590 outs() << " (method_list_t entends past the end of the section)\n";
3591 } else
3592 memcpy(&ml, r, sizeof(struct method_list32_t));
3593 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3594 swapStruct(ml);
3595 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3596 outs() << indent << "\t\t count " << ml.count << "\n";
3597
3598 p += sizeof(struct method_list32_t);
3599 offset += sizeof(struct method_list32_t);
3600 for (i = 0; i < ml.count; i++) {
3601 r = get_pointer_32(p, offset, left, S, info);
3602 if (r == nullptr)
3603 return;
3604 memset(&m, '\0', sizeof(struct method32_t));
3605 if (left < sizeof(struct method32_t)) {
3606 memcpy(&ml, r, left);
3607 outs() << indent << " (method_t entends past the end of the section)\n";
3608 } else
3609 memcpy(&m, r, sizeof(struct method32_t));
3610 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3611 swapStruct(m);
3612
3613 outs() << indent << "\t\t name " << format("0x%" PRIx32, m.name);
3614 name = get_pointer_32(m.name, xoffset, left, xS, info);
3615 if (name != nullptr)
3616 outs() << format(" %.*s", left, name);
3617 outs() << "\n";
3618
3619 outs() << indent << "\t\t types " << format("0x%" PRIx32, m.types);
3620 name = get_pointer_32(m.types, xoffset, left, xS, info);
3621 if (name != nullptr)
3622 outs() << format(" %.*s", left, name);
3623 outs() << "\n";
3624
3625 outs() << indent << "\t\t imp " << format("0x%" PRIx32, m.imp);
3626 name = get_symbol_32(offset + offsetof(struct method32_t, imp), S, info,
3627 m.imp);
3628 if (name != nullptr)
3629 outs() << " " << name;
3630 outs() << "\n";
3631
3632 p += sizeof(struct method32_t);
3633 offset += sizeof(struct method32_t);
3634 }
3635}
3636
Kevin Enderby846c0002015-04-16 17:19:59 +00003637static bool print_method_list(uint32_t p, struct DisassembleInfo *info) {
3638 uint32_t offset, left, xleft;
3639 SectionRef S;
3640 struct objc_method_list_t method_list;
3641 struct objc_method_t method;
3642 const char *r, *methods, *name, *SymbolName;
3643 int32_t i;
3644
3645 r = get_pointer_32(p, offset, left, S, info, true);
3646 if (r == nullptr)
3647 return true;
3648
3649 outs() << "\n";
3650 if (left > sizeof(struct objc_method_list_t)) {
3651 memcpy(&method_list, r, sizeof(struct objc_method_list_t));
3652 } else {
3653 outs() << "\t\t objc_method_list extends past end of the section\n";
3654 memset(&method_list, '\0', sizeof(struct objc_method_list_t));
3655 memcpy(&method_list, r, left);
3656 }
3657 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3658 swapStruct(method_list);
3659
3660 outs() << "\t\t obsolete "
3661 << format("0x%08" PRIx32, method_list.obsolete) << "\n";
3662 outs() << "\t\t method_count " << method_list.method_count << "\n";
3663
3664 methods = r + sizeof(struct objc_method_list_t);
3665 for (i = 0; i < method_list.method_count; i++) {
3666 if ((i + 1) * sizeof(struct objc_method_t) > left) {
3667 outs() << "\t\t remaining method's extend past the of the section\n";
3668 break;
3669 }
3670 memcpy(&method, methods + i * sizeof(struct objc_method_t),
3671 sizeof(struct objc_method_t));
3672 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3673 swapStruct(method);
3674
3675 outs() << "\t\t method_name "
3676 << format("0x%08" PRIx32, method.method_name);
3677 if (info->verbose) {
3678 name = get_pointer_32(method.method_name, offset, xleft, S, info, true);
3679 if (name != nullptr)
3680 outs() << format(" %.*s", xleft, name);
3681 else
3682 outs() << " (not in an __OBJC section)";
3683 }
3684 outs() << "\n";
3685
3686 outs() << "\t\t method_types "
3687 << format("0x%08" PRIx32, method.method_types);
3688 if (info->verbose) {
3689 name = get_pointer_32(method.method_types, offset, xleft, S, info, true);
3690 if (name != nullptr)
3691 outs() << format(" %.*s", xleft, name);
3692 else
3693 outs() << " (not in an __OBJC section)";
3694 }
3695 outs() << "\n";
3696
3697 outs() << "\t\t method_imp "
3698 << format("0x%08" PRIx32, method.method_imp) << " ";
3699 if (info->verbose) {
3700 SymbolName = GuessSymbolName(method.method_imp, info->AddrMap);
3701 if (SymbolName != nullptr)
3702 outs() << SymbolName;
3703 }
3704 outs() << "\n";
3705 }
3706 return false;
3707}
3708
Kevin Enderby0fc11822015-04-01 20:57:01 +00003709static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) {
3710 struct protocol_list64_t pl;
3711 uint64_t q, n_value;
3712 struct protocol64_t pc;
3713 const char *r;
3714 uint32_t offset, xoffset, left, i;
3715 SectionRef S, xS;
3716 const char *name, *sym_name;
3717
3718 r = get_pointer_64(p, offset, left, S, info);
3719 if (r == nullptr)
3720 return;
3721 memset(&pl, '\0', sizeof(struct protocol_list64_t));
3722 if (left < sizeof(struct protocol_list64_t)) {
3723 memcpy(&pl, r, left);
3724 outs() << " (protocol_list_t entends past the end of the section)\n";
3725 } else
3726 memcpy(&pl, r, sizeof(struct protocol_list64_t));
3727 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3728 swapStruct(pl);
3729 outs() << " count " << pl.count << "\n";
3730
3731 p += sizeof(struct protocol_list64_t);
3732 offset += sizeof(struct protocol_list64_t);
3733 for (i = 0; i < pl.count; i++) {
3734 r = get_pointer_64(p, offset, left, S, info);
3735 if (r == nullptr)
3736 return;
3737 q = 0;
3738 if (left < sizeof(uint64_t)) {
3739 memcpy(&q, r, left);
3740 outs() << " (protocol_t * entends past the end of the section)\n";
3741 } else
3742 memcpy(&q, r, sizeof(uint64_t));
3743 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3744 sys::swapByteOrder(q);
3745
3746 outs() << "\t\t list[" << i << "] ";
3747 sym_name = get_symbol_64(offset, S, info, n_value, q);
3748 if (n_value != 0) {
3749 if (info->verbose && sym_name != nullptr)
3750 outs() << sym_name;
3751 else
3752 outs() << format("0x%" PRIx64, n_value);
3753 if (q != 0)
3754 outs() << " + " << format("0x%" PRIx64, q);
3755 } else
3756 outs() << format("0x%" PRIx64, q);
3757 outs() << " (struct protocol_t *)\n";
3758
3759 r = get_pointer_64(q + n_value, offset, left, S, info);
3760 if (r == nullptr)
3761 return;
3762 memset(&pc, '\0', sizeof(struct protocol64_t));
3763 if (left < sizeof(struct protocol64_t)) {
3764 memcpy(&pc, r, left);
3765 outs() << " (protocol_t entends past the end of the section)\n";
3766 } else
3767 memcpy(&pc, r, sizeof(struct protocol64_t));
3768 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3769 swapStruct(pc);
3770
3771 outs() << "\t\t\t isa " << format("0x%" PRIx64, pc.isa) << "\n";
3772
3773 outs() << "\t\t\t name ";
3774 sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name), S,
3775 info, n_value, pc.name);
3776 if (n_value != 0) {
3777 if (info->verbose && sym_name != nullptr)
3778 outs() << sym_name;
3779 else
3780 outs() << format("0x%" PRIx64, n_value);
3781 if (pc.name != 0)
3782 outs() << " + " << format("0x%" PRIx64, pc.name);
3783 } else
3784 outs() << format("0x%" PRIx64, pc.name);
3785 name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info);
3786 if (name != nullptr)
3787 outs() << format(" %.*s", left, name);
3788 outs() << "\n";
3789
3790 outs() << "\t\t\tprotocols " << format("0x%" PRIx64, pc.protocols) << "\n";
3791
3792 outs() << "\t\t instanceMethods ";
3793 sym_name =
3794 get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods),
3795 S, info, n_value, pc.instanceMethods);
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.instanceMethods != 0)
3802 outs() << " + " << format("0x%" PRIx64, pc.instanceMethods);
3803 } else
3804 outs() << format("0x%" PRIx64, pc.instanceMethods);
3805 outs() << " (struct method_list_t *)\n";
3806 if (pc.instanceMethods + n_value != 0)
3807 print_method_list64_t(pc.instanceMethods + n_value, info, "\t");
3808
3809 outs() << "\t\t classMethods ";
3810 sym_name =
3811 get_symbol_64(offset + offsetof(struct protocol64_t, classMethods), S,
3812 info, n_value, pc.classMethods);
3813 if (n_value != 0) {
3814 if (info->verbose && sym_name != nullptr)
3815 outs() << sym_name;
3816 else
3817 outs() << format("0x%" PRIx64, n_value);
3818 if (pc.classMethods != 0)
3819 outs() << " + " << format("0x%" PRIx64, pc.classMethods);
3820 } else
3821 outs() << format("0x%" PRIx64, pc.classMethods);
3822 outs() << " (struct method_list_t *)\n";
3823 if (pc.classMethods + n_value != 0)
3824 print_method_list64_t(pc.classMethods + n_value, info, "\t");
3825
3826 outs() << "\t optionalInstanceMethods "
3827 << format("0x%" PRIx64, pc.optionalInstanceMethods) << "\n";
3828 outs() << "\t optionalClassMethods "
3829 << format("0x%" PRIx64, pc.optionalClassMethods) << "\n";
3830 outs() << "\t instanceProperties "
3831 << format("0x%" PRIx64, pc.instanceProperties) << "\n";
3832
3833 p += sizeof(uint64_t);
3834 offset += sizeof(uint64_t);
3835 }
3836}
3837
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003838static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) {
3839 struct protocol_list32_t pl;
3840 uint32_t q;
3841 struct protocol32_t pc;
3842 const char *r;
3843 uint32_t offset, xoffset, left, i;
3844 SectionRef S, xS;
3845 const char *name;
3846
3847 r = get_pointer_32(p, offset, left, S, info);
3848 if (r == nullptr)
3849 return;
3850 memset(&pl, '\0', sizeof(struct protocol_list32_t));
3851 if (left < sizeof(struct protocol_list32_t)) {
3852 memcpy(&pl, r, left);
3853 outs() << " (protocol_list_t entends past the end of the section)\n";
3854 } else
3855 memcpy(&pl, r, sizeof(struct protocol_list32_t));
3856 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3857 swapStruct(pl);
3858 outs() << " count " << pl.count << "\n";
3859
3860 p += sizeof(struct protocol_list32_t);
3861 offset += sizeof(struct protocol_list32_t);
3862 for (i = 0; i < pl.count; i++) {
3863 r = get_pointer_32(p, offset, left, S, info);
3864 if (r == nullptr)
3865 return;
3866 q = 0;
3867 if (left < sizeof(uint32_t)) {
3868 memcpy(&q, r, left);
3869 outs() << " (protocol_t * entends past the end of the section)\n";
3870 } else
3871 memcpy(&q, r, sizeof(uint32_t));
3872 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3873 sys::swapByteOrder(q);
3874 outs() << "\t\t list[" << i << "] " << format("0x%" PRIx32, q)
3875 << " (struct protocol_t *)\n";
3876 r = get_pointer_32(q, offset, left, S, info);
3877 if (r == nullptr)
3878 return;
3879 memset(&pc, '\0', sizeof(struct protocol32_t));
3880 if (left < sizeof(struct protocol32_t)) {
3881 memcpy(&pc, r, left);
3882 outs() << " (protocol_t entends past the end of the section)\n";
3883 } else
3884 memcpy(&pc, r, sizeof(struct protocol32_t));
3885 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3886 swapStruct(pc);
3887 outs() << "\t\t\t isa " << format("0x%" PRIx32, pc.isa) << "\n";
3888 outs() << "\t\t\t name " << format("0x%" PRIx32, pc.name);
3889 name = get_pointer_32(pc.name, xoffset, left, xS, info);
3890 if (name != nullptr)
3891 outs() << format(" %.*s", left, name);
3892 outs() << "\n";
3893 outs() << "\t\t\tprotocols " << format("0x%" PRIx32, pc.protocols) << "\n";
3894 outs() << "\t\t instanceMethods "
3895 << format("0x%" PRIx32, pc.instanceMethods)
3896 << " (struct method_list_t *)\n";
3897 if (pc.instanceMethods != 0)
3898 print_method_list32_t(pc.instanceMethods, info, "\t");
3899 outs() << "\t\t classMethods " << format("0x%" PRIx32, pc.classMethods)
3900 << " (struct method_list_t *)\n";
3901 if (pc.classMethods != 0)
3902 print_method_list32_t(pc.classMethods, info, "\t");
3903 outs() << "\t optionalInstanceMethods "
3904 << format("0x%" PRIx32, pc.optionalInstanceMethods) << "\n";
3905 outs() << "\t optionalClassMethods "
3906 << format("0x%" PRIx32, pc.optionalClassMethods) << "\n";
3907 outs() << "\t instanceProperties "
3908 << format("0x%" PRIx32, pc.instanceProperties) << "\n";
3909 p += sizeof(uint32_t);
3910 offset += sizeof(uint32_t);
3911 }
3912}
3913
Kevin Enderby846c0002015-04-16 17:19:59 +00003914static void print_indent(uint32_t indent) {
3915 for (uint32_t i = 0; i < indent;) {
3916 if (indent - i >= 8) {
3917 outs() << "\t";
3918 i += 8;
3919 } else {
3920 for (uint32_t j = i; j < indent; j++)
3921 outs() << " ";
3922 return;
3923 }
3924 }
3925}
3926
3927static bool print_method_description_list(uint32_t p, uint32_t indent,
3928 struct DisassembleInfo *info) {
3929 uint32_t offset, left, xleft;
3930 SectionRef S;
3931 struct objc_method_description_list_t mdl;
3932 struct objc_method_description_t md;
3933 const char *r, *list, *name;
3934 int32_t i;
3935
3936 r = get_pointer_32(p, offset, left, S, info, true);
3937 if (r == nullptr)
3938 return true;
3939
3940 outs() << "\n";
3941 if (left > sizeof(struct objc_method_description_list_t)) {
3942 memcpy(&mdl, r, sizeof(struct objc_method_description_list_t));
3943 } else {
3944 print_indent(indent);
3945 outs() << " objc_method_description_list extends past end of the section\n";
3946 memset(&mdl, '\0', sizeof(struct objc_method_description_list_t));
3947 memcpy(&mdl, r, left);
3948 }
3949 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3950 swapStruct(mdl);
3951
3952 print_indent(indent);
3953 outs() << " count " << mdl.count << "\n";
3954
3955 list = r + sizeof(struct objc_method_description_list_t);
3956 for (i = 0; i < mdl.count; i++) {
3957 if ((i + 1) * sizeof(struct objc_method_description_t) > left) {
3958 print_indent(indent);
3959 outs() << " remaining list entries extend past the of the section\n";
3960 break;
3961 }
3962 print_indent(indent);
3963 outs() << " list[" << i << "]\n";
3964 memcpy(&md, list + i * sizeof(struct objc_method_description_t),
3965 sizeof(struct objc_method_description_t));
3966 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3967 swapStruct(md);
3968
3969 print_indent(indent);
3970 outs() << " name " << format("0x%08" PRIx32, md.name);
3971 if (info->verbose) {
3972 name = get_pointer_32(md.name, offset, xleft, S, info, true);
3973 if (name != nullptr)
3974 outs() << format(" %.*s", xleft, name);
3975 else
3976 outs() << " (not in an __OBJC section)";
3977 }
3978 outs() << "\n";
3979
3980 print_indent(indent);
3981 outs() << " types " << format("0x%08" PRIx32, md.types);
3982 if (info->verbose) {
3983 name = get_pointer_32(md.types, offset, xleft, S, info, true);
3984 if (name != nullptr)
3985 outs() << format(" %.*s", xleft, name);
3986 else
3987 outs() << " (not in an __OBJC section)";
3988 }
3989 outs() << "\n";
3990 }
3991 return false;
3992}
3993
3994static bool print_protocol_list(uint32_t p, uint32_t indent,
3995 struct DisassembleInfo *info);
3996
3997static bool print_protocol(uint32_t p, uint32_t indent,
3998 struct DisassembleInfo *info) {
3999 uint32_t offset, left;
4000 SectionRef S;
4001 struct objc_protocol_t protocol;
4002 const char *r, *name;
4003
4004 r = get_pointer_32(p, offset, left, S, info, true);
4005 if (r == nullptr)
4006 return true;
4007
4008 outs() << "\n";
4009 if (left >= sizeof(struct objc_protocol_t)) {
4010 memcpy(&protocol, r, sizeof(struct objc_protocol_t));
4011 } else {
4012 print_indent(indent);
4013 outs() << " Protocol extends past end of the section\n";
4014 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
4015 memcpy(&protocol, r, left);
4016 }
4017 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4018 swapStruct(protocol);
4019
4020 print_indent(indent);
4021 outs() << " isa " << format("0x%08" PRIx32, protocol.isa)
4022 << "\n";
4023
4024 print_indent(indent);
4025 outs() << " protocol_name "
4026 << format("0x%08" PRIx32, protocol.protocol_name);
4027 if (info->verbose) {
4028 name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true);
4029 if (name != nullptr)
4030 outs() << format(" %.*s", left, name);
4031 else
4032 outs() << " (not in an __OBJC section)";
4033 }
4034 outs() << "\n";
4035
4036 print_indent(indent);
4037 outs() << " protocol_list "
4038 << format("0x%08" PRIx32, protocol.protocol_list);
4039 if (print_protocol_list(protocol.protocol_list, indent + 4, info))
4040 outs() << " (not in an __OBJC section)\n";
4041
4042 print_indent(indent);
4043 outs() << " instance_methods "
4044 << format("0x%08" PRIx32, protocol.instance_methods);
4045 if (print_method_description_list(protocol.instance_methods, indent, info))
4046 outs() << " (not in an __OBJC section)\n";
4047
4048 print_indent(indent);
4049 outs() << " class_methods "
4050 << format("0x%08" PRIx32, protocol.class_methods);
4051 if (print_method_description_list(protocol.class_methods, indent, info))
4052 outs() << " (not in an __OBJC section)\n";
4053
4054 return false;
4055}
4056
4057static bool print_protocol_list(uint32_t p, uint32_t indent,
4058 struct DisassembleInfo *info) {
4059 uint32_t offset, left, l;
4060 SectionRef S;
4061 struct objc_protocol_list_t protocol_list;
4062 const char *r, *list;
4063 int32_t i;
4064
4065 r = get_pointer_32(p, offset, left, S, info, true);
4066 if (r == nullptr)
4067 return true;
4068
4069 outs() << "\n";
4070 if (left > sizeof(struct objc_protocol_list_t)) {
4071 memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t));
4072 } else {
4073 outs() << "\t\t objc_protocol_list_t extends past end of the section\n";
4074 memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t));
4075 memcpy(&protocol_list, r, left);
4076 }
4077 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4078 swapStruct(protocol_list);
4079
4080 print_indent(indent);
4081 outs() << " next " << format("0x%08" PRIx32, protocol_list.next)
4082 << "\n";
4083 print_indent(indent);
4084 outs() << " count " << protocol_list.count << "\n";
4085
4086 list = r + sizeof(struct objc_protocol_list_t);
4087 for (i = 0; i < protocol_list.count; i++) {
4088 if ((i + 1) * sizeof(uint32_t) > left) {
4089 outs() << "\t\t remaining list entries extend past the of the section\n";
4090 break;
4091 }
4092 memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t));
4093 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4094 sys::swapByteOrder(l);
4095
4096 print_indent(indent);
4097 outs() << " list[" << i << "] " << format("0x%08" PRIx32, l);
4098 if (print_protocol(l, indent, info))
4099 outs() << "(not in an __OBJC section)\n";
4100 }
4101 return false;
4102}
4103
Kevin Enderby0fc11822015-04-01 20:57:01 +00004104static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) {
4105 struct ivar_list64_t il;
4106 struct ivar64_t i;
4107 const char *r;
4108 uint32_t offset, xoffset, left, j;
4109 SectionRef S, xS;
4110 const char *name, *sym_name, *ivar_offset_p;
4111 uint64_t ivar_offset, n_value;
4112
4113 r = get_pointer_64(p, offset, left, S, info);
4114 if (r == nullptr)
4115 return;
4116 memset(&il, '\0', sizeof(struct ivar_list64_t));
4117 if (left < sizeof(struct ivar_list64_t)) {
4118 memcpy(&il, r, left);
4119 outs() << " (ivar_list_t entends past the end of the section)\n";
4120 } else
4121 memcpy(&il, r, sizeof(struct ivar_list64_t));
4122 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4123 swapStruct(il);
4124 outs() << " entsize " << il.entsize << "\n";
4125 outs() << " count " << il.count << "\n";
4126
4127 p += sizeof(struct ivar_list64_t);
4128 offset += sizeof(struct ivar_list64_t);
4129 for (j = 0; j < il.count; j++) {
4130 r = get_pointer_64(p, offset, left, S, info);
4131 if (r == nullptr)
4132 return;
4133 memset(&i, '\0', sizeof(struct ivar64_t));
4134 if (left < sizeof(struct ivar64_t)) {
4135 memcpy(&i, r, left);
4136 outs() << " (ivar_t entends past the end of the section)\n";
4137 } else
4138 memcpy(&i, r, sizeof(struct ivar64_t));
4139 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4140 swapStruct(i);
4141
4142 outs() << "\t\t\t offset ";
4143 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset), S,
4144 info, n_value, i.offset);
4145 if (n_value != 0) {
4146 if (info->verbose && sym_name != nullptr)
4147 outs() << sym_name;
4148 else
4149 outs() << format("0x%" PRIx64, n_value);
4150 if (i.offset != 0)
4151 outs() << " + " << format("0x%" PRIx64, i.offset);
4152 } else
4153 outs() << format("0x%" PRIx64, i.offset);
4154 ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info);
4155 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4156 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4157 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4158 sys::swapByteOrder(ivar_offset);
4159 outs() << " " << ivar_offset << "\n";
4160 } else
4161 outs() << "\n";
4162
4163 outs() << "\t\t\t name ";
4164 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name), S, info,
4165 n_value, i.name);
4166 if (n_value != 0) {
4167 if (info->verbose && sym_name != nullptr)
4168 outs() << sym_name;
4169 else
4170 outs() << format("0x%" PRIx64, n_value);
4171 if (i.name != 0)
4172 outs() << " + " << format("0x%" PRIx64, i.name);
4173 } else
4174 outs() << format("0x%" PRIx64, i.name);
4175 name = get_pointer_64(i.name + n_value, xoffset, left, xS, info);
4176 if (name != nullptr)
4177 outs() << format(" %.*s", left, name);
4178 outs() << "\n";
4179
4180 outs() << "\t\t\t type ";
4181 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type), S, info,
4182 n_value, i.name);
4183 name = get_pointer_64(i.type + n_value, xoffset, left, xS, info);
4184 if (n_value != 0) {
4185 if (info->verbose && sym_name != nullptr)
4186 outs() << sym_name;
4187 else
4188 outs() << format("0x%" PRIx64, n_value);
4189 if (i.type != 0)
4190 outs() << " + " << format("0x%" PRIx64, i.type);
4191 } else
4192 outs() << format("0x%" PRIx64, i.type);
4193 if (name != nullptr)
4194 outs() << format(" %.*s", left, name);
4195 outs() << "\n";
4196
4197 outs() << "\t\t\talignment " << i.alignment << "\n";
4198 outs() << "\t\t\t size " << i.size << "\n";
4199
4200 p += sizeof(struct ivar64_t);
4201 offset += sizeof(struct ivar64_t);
4202 }
4203}
4204
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004205static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) {
4206 struct ivar_list32_t il;
4207 struct ivar32_t i;
4208 const char *r;
4209 uint32_t offset, xoffset, left, j;
4210 SectionRef S, xS;
4211 const char *name, *ivar_offset_p;
4212 uint32_t ivar_offset;
4213
4214 r = get_pointer_32(p, offset, left, S, info);
4215 if (r == nullptr)
4216 return;
4217 memset(&il, '\0', sizeof(struct ivar_list32_t));
4218 if (left < sizeof(struct ivar_list32_t)) {
4219 memcpy(&il, r, left);
4220 outs() << " (ivar_list_t entends past the end of the section)\n";
4221 } else
4222 memcpy(&il, r, sizeof(struct ivar_list32_t));
4223 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4224 swapStruct(il);
4225 outs() << " entsize " << il.entsize << "\n";
4226 outs() << " count " << il.count << "\n";
4227
4228 p += sizeof(struct ivar_list32_t);
4229 offset += sizeof(struct ivar_list32_t);
4230 for (j = 0; j < il.count; j++) {
4231 r = get_pointer_32(p, offset, left, S, info);
4232 if (r == nullptr)
4233 return;
4234 memset(&i, '\0', sizeof(struct ivar32_t));
4235 if (left < sizeof(struct ivar32_t)) {
4236 memcpy(&i, r, left);
4237 outs() << " (ivar_t entends past the end of the section)\n";
4238 } else
4239 memcpy(&i, r, sizeof(struct ivar32_t));
4240 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4241 swapStruct(i);
4242
4243 outs() << "\t\t\t offset " << format("0x%" PRIx32, i.offset);
4244 ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info);
4245 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4246 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4247 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4248 sys::swapByteOrder(ivar_offset);
4249 outs() << " " << ivar_offset << "\n";
4250 } else
4251 outs() << "\n";
4252
4253 outs() << "\t\t\t name " << format("0x%" PRIx32, i.name);
4254 name = get_pointer_32(i.name, xoffset, left, xS, info);
4255 if (name != nullptr)
4256 outs() << format(" %.*s", left, name);
4257 outs() << "\n";
4258
4259 outs() << "\t\t\t type " << format("0x%" PRIx32, i.type);
4260 name = get_pointer_32(i.type, xoffset, left, xS, info);
4261 if (name != nullptr)
4262 outs() << format(" %.*s", left, name);
4263 outs() << "\n";
4264
4265 outs() << "\t\t\talignment " << i.alignment << "\n";
4266 outs() << "\t\t\t size " << i.size << "\n";
4267
4268 p += sizeof(struct ivar32_t);
4269 offset += sizeof(struct ivar32_t);
4270 }
4271}
4272
Kevin Enderby0fc11822015-04-01 20:57:01 +00004273static void print_objc_property_list64(uint64_t p,
4274 struct DisassembleInfo *info) {
4275 struct objc_property_list64 opl;
4276 struct objc_property64 op;
4277 const char *r;
4278 uint32_t offset, xoffset, left, j;
4279 SectionRef S, xS;
4280 const char *name, *sym_name;
4281 uint64_t n_value;
4282
4283 r = get_pointer_64(p, offset, left, S, info);
4284 if (r == nullptr)
4285 return;
4286 memset(&opl, '\0', sizeof(struct objc_property_list64));
4287 if (left < sizeof(struct objc_property_list64)) {
4288 memcpy(&opl, r, left);
4289 outs() << " (objc_property_list entends past the end of the section)\n";
4290 } else
4291 memcpy(&opl, r, sizeof(struct objc_property_list64));
4292 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4293 swapStruct(opl);
4294 outs() << " entsize " << opl.entsize << "\n";
4295 outs() << " count " << opl.count << "\n";
4296
4297 p += sizeof(struct objc_property_list64);
4298 offset += sizeof(struct objc_property_list64);
4299 for (j = 0; j < opl.count; j++) {
4300 r = get_pointer_64(p, offset, left, S, info);
4301 if (r == nullptr)
4302 return;
4303 memset(&op, '\0', sizeof(struct objc_property64));
4304 if (left < sizeof(struct objc_property64)) {
4305 memcpy(&op, r, left);
4306 outs() << " (objc_property entends past the end of the section)\n";
4307 } else
4308 memcpy(&op, r, sizeof(struct objc_property64));
4309 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4310 swapStruct(op);
4311
4312 outs() << "\t\t\t name ";
4313 sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name), S,
4314 info, n_value, op.name);
4315 if (n_value != 0) {
4316 if (info->verbose && sym_name != nullptr)
4317 outs() << sym_name;
4318 else
4319 outs() << format("0x%" PRIx64, n_value);
4320 if (op.name != 0)
4321 outs() << " + " << format("0x%" PRIx64, op.name);
4322 } else
4323 outs() << format("0x%" PRIx64, op.name);
4324 name = get_pointer_64(op.name + n_value, xoffset, left, xS, info);
4325 if (name != nullptr)
4326 outs() << format(" %.*s", left, name);
4327 outs() << "\n";
4328
4329 outs() << "\t\t\tattributes ";
4330 sym_name =
4331 get_symbol_64(offset + offsetof(struct objc_property64, attributes), S,
4332 info, n_value, op.attributes);
4333 if (n_value != 0) {
4334 if (info->verbose && sym_name != nullptr)
4335 outs() << sym_name;
4336 else
4337 outs() << format("0x%" PRIx64, n_value);
4338 if (op.attributes != 0)
4339 outs() << " + " << format("0x%" PRIx64, op.attributes);
4340 } else
4341 outs() << format("0x%" PRIx64, op.attributes);
4342 name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info);
4343 if (name != nullptr)
4344 outs() << format(" %.*s", left, name);
4345 outs() << "\n";
4346
4347 p += sizeof(struct objc_property64);
4348 offset += sizeof(struct objc_property64);
4349 }
4350}
4351
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004352static void print_objc_property_list32(uint32_t p,
4353 struct DisassembleInfo *info) {
4354 struct objc_property_list32 opl;
4355 struct objc_property32 op;
4356 const char *r;
4357 uint32_t offset, xoffset, left, j;
4358 SectionRef S, xS;
4359 const char *name;
4360
4361 r = get_pointer_32(p, offset, left, S, info);
4362 if (r == nullptr)
4363 return;
4364 memset(&opl, '\0', sizeof(struct objc_property_list32));
4365 if (left < sizeof(struct objc_property_list32)) {
4366 memcpy(&opl, r, left);
4367 outs() << " (objc_property_list entends past the end of the section)\n";
4368 } else
4369 memcpy(&opl, r, sizeof(struct objc_property_list32));
4370 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4371 swapStruct(opl);
4372 outs() << " entsize " << opl.entsize << "\n";
4373 outs() << " count " << opl.count << "\n";
4374
4375 p += sizeof(struct objc_property_list32);
4376 offset += sizeof(struct objc_property_list32);
4377 for (j = 0; j < opl.count; j++) {
4378 r = get_pointer_32(p, offset, left, S, info);
4379 if (r == nullptr)
4380 return;
4381 memset(&op, '\0', sizeof(struct objc_property32));
4382 if (left < sizeof(struct objc_property32)) {
4383 memcpy(&op, r, left);
4384 outs() << " (objc_property entends past the end of the section)\n";
4385 } else
4386 memcpy(&op, r, sizeof(struct objc_property32));
4387 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4388 swapStruct(op);
4389
4390 outs() << "\t\t\t name " << format("0x%" PRIx32, op.name);
4391 name = get_pointer_32(op.name, xoffset, left, xS, info);
4392 if (name != nullptr)
4393 outs() << format(" %.*s", left, name);
4394 outs() << "\n";
4395
4396 outs() << "\t\t\tattributes " << format("0x%" PRIx32, op.attributes);
4397 name = get_pointer_32(op.attributes, xoffset, left, xS, info);
4398 if (name != nullptr)
4399 outs() << format(" %.*s", left, name);
4400 outs() << "\n";
4401
4402 p += sizeof(struct objc_property32);
4403 offset += sizeof(struct objc_property32);
4404 }
4405}
4406
Richard Smith81ff44d2015-10-09 22:09:56 +00004407static bool print_class_ro64_t(uint64_t p, struct DisassembleInfo *info,
Kevin Enderby0fc11822015-04-01 20:57:01 +00004408 bool &is_meta_class) {
4409 struct class_ro64_t cro;
4410 const char *r;
4411 uint32_t offset, xoffset, left;
4412 SectionRef S, xS;
4413 const char *name, *sym_name;
4414 uint64_t n_value;
4415
4416 r = get_pointer_64(p, offset, left, S, info);
4417 if (r == nullptr || left < sizeof(struct class_ro64_t))
Richard Smith81ff44d2015-10-09 22:09:56 +00004418 return false;
Kevin Enderby0fc11822015-04-01 20:57:01 +00004419 memset(&cro, '\0', sizeof(struct class_ro64_t));
4420 if (left < sizeof(struct class_ro64_t)) {
4421 memcpy(&cro, r, left);
4422 outs() << " (class_ro_t entends past the end of the section)\n";
4423 } else
4424 memcpy(&cro, r, sizeof(struct class_ro64_t));
4425 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4426 swapStruct(cro);
4427 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4428 if (cro.flags & RO_META)
4429 outs() << " RO_META";
4430 if (cro.flags & RO_ROOT)
4431 outs() << " RO_ROOT";
4432 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4433 outs() << " RO_HAS_CXX_STRUCTORS";
4434 outs() << "\n";
4435 outs() << " instanceStart " << cro.instanceStart << "\n";
4436 outs() << " instanceSize " << cro.instanceSize << "\n";
4437 outs() << " reserved " << format("0x%" PRIx32, cro.reserved)
4438 << "\n";
4439 outs() << " ivarLayout " << format("0x%" PRIx64, cro.ivarLayout)
4440 << "\n";
4441 print_layout_map64(cro.ivarLayout, info);
4442
4443 outs() << " name ";
4444 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name), S,
4445 info, n_value, cro.name);
4446 if (n_value != 0) {
4447 if (info->verbose && sym_name != nullptr)
4448 outs() << sym_name;
4449 else
4450 outs() << format("0x%" PRIx64, n_value);
4451 if (cro.name != 0)
4452 outs() << " + " << format("0x%" PRIx64, cro.name);
4453 } else
4454 outs() << format("0x%" PRIx64, cro.name);
4455 name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info);
4456 if (name != nullptr)
4457 outs() << format(" %.*s", left, name);
4458 outs() << "\n";
4459
4460 outs() << " baseMethods ";
4461 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods),
4462 S, info, n_value, cro.baseMethods);
4463 if (n_value != 0) {
4464 if (info->verbose && sym_name != nullptr)
4465 outs() << sym_name;
4466 else
4467 outs() << format("0x%" PRIx64, n_value);
4468 if (cro.baseMethods != 0)
4469 outs() << " + " << format("0x%" PRIx64, cro.baseMethods);
4470 } else
4471 outs() << format("0x%" PRIx64, cro.baseMethods);
4472 outs() << " (struct method_list_t *)\n";
4473 if (cro.baseMethods + n_value != 0)
4474 print_method_list64_t(cro.baseMethods + n_value, info, "");
4475
4476 outs() << " baseProtocols ";
4477 sym_name =
4478 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols), S,
4479 info, n_value, cro.baseProtocols);
4480 if (n_value != 0) {
4481 if (info->verbose && sym_name != nullptr)
4482 outs() << sym_name;
4483 else
4484 outs() << format("0x%" PRIx64, n_value);
4485 if (cro.baseProtocols != 0)
4486 outs() << " + " << format("0x%" PRIx64, cro.baseProtocols);
4487 } else
4488 outs() << format("0x%" PRIx64, cro.baseProtocols);
4489 outs() << "\n";
4490 if (cro.baseProtocols + n_value != 0)
4491 print_protocol_list64_t(cro.baseProtocols + n_value, info);
4492
4493 outs() << " ivars ";
4494 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars), S,
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004495 info, n_value, cro.ivars);
Kevin Enderby0fc11822015-04-01 20:57:01 +00004496 if (n_value != 0) {
4497 if (info->verbose && sym_name != nullptr)
4498 outs() << sym_name;
4499 else
4500 outs() << format("0x%" PRIx64, n_value);
4501 if (cro.ivars != 0)
4502 outs() << " + " << format("0x%" PRIx64, cro.ivars);
4503 } else
4504 outs() << format("0x%" PRIx64, cro.ivars);
4505 outs() << "\n";
4506 if (cro.ivars + n_value != 0)
4507 print_ivar_list64_t(cro.ivars + n_value, info);
4508
4509 outs() << " weakIvarLayout ";
4510 sym_name =
4511 get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout), S,
4512 info, n_value, cro.weakIvarLayout);
4513 if (n_value != 0) {
4514 if (info->verbose && sym_name != nullptr)
4515 outs() << sym_name;
4516 else
4517 outs() << format("0x%" PRIx64, n_value);
4518 if (cro.weakIvarLayout != 0)
4519 outs() << " + " << format("0x%" PRIx64, cro.weakIvarLayout);
4520 } else
4521 outs() << format("0x%" PRIx64, cro.weakIvarLayout);
4522 outs() << "\n";
4523 print_layout_map64(cro.weakIvarLayout + n_value, info);
4524
4525 outs() << " baseProperties ";
4526 sym_name =
4527 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties), S,
4528 info, n_value, cro.baseProperties);
4529 if (n_value != 0) {
4530 if (info->verbose && sym_name != nullptr)
4531 outs() << sym_name;
4532 else
4533 outs() << format("0x%" PRIx64, n_value);
4534 if (cro.baseProperties != 0)
4535 outs() << " + " << format("0x%" PRIx64, cro.baseProperties);
4536 } else
4537 outs() << format("0x%" PRIx64, cro.baseProperties);
4538 outs() << "\n";
4539 if (cro.baseProperties + n_value != 0)
4540 print_objc_property_list64(cro.baseProperties + n_value, info);
4541
Rafael Espindolab9091322015-10-24 23:19:10 +00004542 is_meta_class = (cro.flags & RO_META) != 0;
Richard Smith81ff44d2015-10-09 22:09:56 +00004543 return true;
Kevin Enderby0fc11822015-04-01 20:57:01 +00004544}
4545
Richard Smith81ff44d2015-10-09 22:09:56 +00004546static bool print_class_ro32_t(uint32_t p, struct DisassembleInfo *info,
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004547 bool &is_meta_class) {
4548 struct class_ro32_t cro;
4549 const char *r;
4550 uint32_t offset, xoffset, left;
4551 SectionRef S, xS;
4552 const char *name;
4553
4554 r = get_pointer_32(p, offset, left, S, info);
4555 if (r == nullptr)
Richard Smith81ff44d2015-10-09 22:09:56 +00004556 return false;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004557 memset(&cro, '\0', sizeof(struct class_ro32_t));
4558 if (left < sizeof(struct class_ro32_t)) {
4559 memcpy(&cro, r, left);
4560 outs() << " (class_ro_t entends past the end of the section)\n";
4561 } else
4562 memcpy(&cro, r, sizeof(struct class_ro32_t));
4563 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4564 swapStruct(cro);
4565 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4566 if (cro.flags & RO_META)
4567 outs() << " RO_META";
4568 if (cro.flags & RO_ROOT)
4569 outs() << " RO_ROOT";
4570 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4571 outs() << " RO_HAS_CXX_STRUCTORS";
4572 outs() << "\n";
4573 outs() << " instanceStart " << cro.instanceStart << "\n";
4574 outs() << " instanceSize " << cro.instanceSize << "\n";
4575 outs() << " ivarLayout " << format("0x%" PRIx32, cro.ivarLayout)
4576 << "\n";
4577 print_layout_map32(cro.ivarLayout, info);
4578
4579 outs() << " name " << format("0x%" PRIx32, cro.name);
4580 name = get_pointer_32(cro.name, xoffset, left, xS, info);
4581 if (name != nullptr)
4582 outs() << format(" %.*s", left, name);
4583 outs() << "\n";
4584
4585 outs() << " baseMethods "
4586 << format("0x%" PRIx32, cro.baseMethods)
4587 << " (struct method_list_t *)\n";
4588 if (cro.baseMethods != 0)
4589 print_method_list32_t(cro.baseMethods, info, "");
4590
4591 outs() << " baseProtocols "
4592 << format("0x%" PRIx32, cro.baseProtocols) << "\n";
4593 if (cro.baseProtocols != 0)
4594 print_protocol_list32_t(cro.baseProtocols, info);
4595 outs() << " ivars " << format("0x%" PRIx32, cro.ivars)
4596 << "\n";
4597 if (cro.ivars != 0)
4598 print_ivar_list32_t(cro.ivars, info);
4599 outs() << " weakIvarLayout "
4600 << format("0x%" PRIx32, cro.weakIvarLayout) << "\n";
4601 print_layout_map32(cro.weakIvarLayout, info);
4602 outs() << " baseProperties "
4603 << format("0x%" PRIx32, cro.baseProperties) << "\n";
4604 if (cro.baseProperties != 0)
4605 print_objc_property_list32(cro.baseProperties, info);
Rafael Espindolab9091322015-10-24 23:19:10 +00004606 is_meta_class = (cro.flags & RO_META) != 0;
Richard Smith81ff44d2015-10-09 22:09:56 +00004607 return true;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004608}
4609
Kevin Enderby0fc11822015-04-01 20:57:01 +00004610static void print_class64_t(uint64_t p, struct DisassembleInfo *info) {
4611 struct class64_t c;
4612 const char *r;
4613 uint32_t offset, left;
4614 SectionRef S;
4615 const char *name;
4616 uint64_t isa_n_value, n_value;
4617
4618 r = get_pointer_64(p, offset, left, S, info);
4619 if (r == nullptr || left < sizeof(struct class64_t))
4620 return;
4621 memset(&c, '\0', sizeof(struct class64_t));
4622 if (left < sizeof(struct class64_t)) {
4623 memcpy(&c, r, left);
4624 outs() << " (class_t entends past the end of the section)\n";
4625 } else
4626 memcpy(&c, r, sizeof(struct class64_t));
4627 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4628 swapStruct(c);
4629
4630 outs() << " isa " << format("0x%" PRIx64, c.isa);
4631 name = get_symbol_64(offset + offsetof(struct class64_t, isa), S, info,
4632 isa_n_value, c.isa);
4633 if (name != nullptr)
4634 outs() << " " << name;
4635 outs() << "\n";
4636
4637 outs() << " superclass " << format("0x%" PRIx64, c.superclass);
4638 name = get_symbol_64(offset + offsetof(struct class64_t, superclass), S, info,
4639 n_value, c.superclass);
4640 if (name != nullptr)
4641 outs() << " " << name;
4642 outs() << "\n";
4643
4644 outs() << " cache " << format("0x%" PRIx64, c.cache);
4645 name = get_symbol_64(offset + offsetof(struct class64_t, cache), S, info,
4646 n_value, c.cache);
4647 if (name != nullptr)
4648 outs() << " " << name;
4649 outs() << "\n";
4650
4651 outs() << " vtable " << format("0x%" PRIx64, c.vtable);
4652 name = get_symbol_64(offset + offsetof(struct class64_t, vtable), S, info,
4653 n_value, c.vtable);
4654 if (name != nullptr)
4655 outs() << " " << name;
4656 outs() << "\n";
4657
4658 name = get_symbol_64(offset + offsetof(struct class64_t, data), S, info,
4659 n_value, c.data);
4660 outs() << " data ";
4661 if (n_value != 0) {
4662 if (info->verbose && name != nullptr)
4663 outs() << name;
4664 else
4665 outs() << format("0x%" PRIx64, n_value);
4666 if (c.data != 0)
4667 outs() << " + " << format("0x%" PRIx64, c.data);
4668 } else
4669 outs() << format("0x%" PRIx64, c.data);
4670 outs() << " (struct class_ro_t *)";
4671
4672 // This is a Swift class if some of the low bits of the pointer are set.
4673 if ((c.data + n_value) & 0x7)
4674 outs() << " Swift class";
4675 outs() << "\n";
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004676 bool is_meta_class;
Richard Smith81ff44d2015-10-09 22:09:56 +00004677 if (!print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class))
4678 return;
Kevin Enderby0fc11822015-04-01 20:57:01 +00004679
Kevin Enderbyaac75382015-10-08 16:56:35 +00004680 if (!is_meta_class &&
4681 c.isa + isa_n_value != p &&
4682 c.isa + isa_n_value != 0 &&
4683 info->depth < 100) {
4684 info->depth++;
4685 outs() << "Meta Class\n";
4686 print_class64_t(c.isa + isa_n_value, info);
Kevin Enderby0fc11822015-04-01 20:57:01 +00004687 }
4688}
4689
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004690static void print_class32_t(uint32_t p, struct DisassembleInfo *info) {
4691 struct class32_t c;
4692 const char *r;
4693 uint32_t offset, left;
4694 SectionRef S;
4695 const char *name;
4696
4697 r = get_pointer_32(p, offset, left, S, info);
4698 if (r == nullptr)
4699 return;
4700 memset(&c, '\0', sizeof(struct class32_t));
4701 if (left < sizeof(struct class32_t)) {
4702 memcpy(&c, r, left);
4703 outs() << " (class_t entends past the end of the section)\n";
4704 } else
4705 memcpy(&c, r, sizeof(struct class32_t));
4706 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4707 swapStruct(c);
4708
4709 outs() << " isa " << format("0x%" PRIx32, c.isa);
4710 name =
4711 get_symbol_32(offset + offsetof(struct class32_t, isa), S, info, c.isa);
4712 if (name != nullptr)
4713 outs() << " " << name;
4714 outs() << "\n";
4715
4716 outs() << " superclass " << format("0x%" PRIx32, c.superclass);
4717 name = get_symbol_32(offset + offsetof(struct class32_t, superclass), S, info,
4718 c.superclass);
4719 if (name != nullptr)
4720 outs() << " " << name;
4721 outs() << "\n";
4722
4723 outs() << " cache " << format("0x%" PRIx32, c.cache);
4724 name = get_symbol_32(offset + offsetof(struct class32_t, cache), S, info,
4725 c.cache);
4726 if (name != nullptr)
4727 outs() << " " << name;
4728 outs() << "\n";
4729
4730 outs() << " vtable " << format("0x%" PRIx32, c.vtable);
4731 name = get_symbol_32(offset + offsetof(struct class32_t, vtable), S, info,
4732 c.vtable);
4733 if (name != nullptr)
4734 outs() << " " << name;
4735 outs() << "\n";
4736
4737 name =
4738 get_symbol_32(offset + offsetof(struct class32_t, data), S, info, c.data);
4739 outs() << " data " << format("0x%" PRIx32, c.data)
4740 << " (struct class_ro_t *)";
4741
4742 // This is a Swift class if some of the low bits of the pointer are set.
4743 if (c.data & 0x3)
4744 outs() << " Swift class";
4745 outs() << "\n";
4746 bool is_meta_class;
Richard Smith81ff44d2015-10-09 22:09:56 +00004747 if (!print_class_ro32_t(c.data & ~0x3, info, is_meta_class))
4748 return;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004749
Hans Wennborgcc9deb42015-09-29 18:02:48 +00004750 if (!is_meta_class) {
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004751 outs() << "Meta Class\n";
4752 print_class32_t(c.isa, info);
4753 }
4754}
4755
Kevin Enderby846c0002015-04-16 17:19:59 +00004756static void print_objc_class_t(struct objc_class_t *objc_class,
4757 struct DisassembleInfo *info) {
4758 uint32_t offset, left, xleft;
4759 const char *name, *p, *ivar_list;
4760 SectionRef S;
4761 int32_t i;
4762 struct objc_ivar_list_t objc_ivar_list;
4763 struct objc_ivar_t ivar;
4764
4765 outs() << "\t\t isa " << format("0x%08" PRIx32, objc_class->isa);
4766 if (info->verbose && CLS_GETINFO(objc_class, CLS_META)) {
4767 name = get_pointer_32(objc_class->isa, 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 super_class "
4776 << format("0x%08" PRIx32, objc_class->super_class);
4777 if (info->verbose) {
4778 name = get_pointer_32(objc_class->super_class, offset, left, S, info, true);
4779 if (name != nullptr)
4780 outs() << format(" %.*s", left, name);
4781 else
4782 outs() << " (not in an __OBJC section)";
4783 }
4784 outs() << "\n";
4785
4786 outs() << "\t\t name " << format("0x%08" PRIx32, objc_class->name);
4787 if (info->verbose) {
4788 name = get_pointer_32(objc_class->name, offset, left, S, info, true);
4789 if (name != nullptr)
4790 outs() << format(" %.*s", left, name);
4791 else
4792 outs() << " (not in an __OBJC section)";
4793 }
4794 outs() << "\n";
4795
4796 outs() << "\t\t version " << format("0x%08" PRIx32, objc_class->version)
4797 << "\n";
4798
4799 outs() << "\t\t info " << format("0x%08" PRIx32, objc_class->info);
4800 if (info->verbose) {
4801 if (CLS_GETINFO(objc_class, CLS_CLASS))
4802 outs() << " CLS_CLASS";
4803 else if (CLS_GETINFO(objc_class, CLS_META))
4804 outs() << " CLS_META";
4805 }
4806 outs() << "\n";
4807
4808 outs() << "\t instance_size "
4809 << format("0x%08" PRIx32, objc_class->instance_size) << "\n";
4810
4811 p = get_pointer_32(objc_class->ivars, offset, left, S, info, true);
4812 outs() << "\t\t ivars " << format("0x%08" PRIx32, objc_class->ivars);
4813 if (p != nullptr) {
4814 if (left > sizeof(struct objc_ivar_list_t)) {
4815 outs() << "\n";
4816 memcpy(&objc_ivar_list, p, sizeof(struct objc_ivar_list_t));
4817 } else {
4818 outs() << " (entends past the end of the section)\n";
4819 memset(&objc_ivar_list, '\0', sizeof(struct objc_ivar_list_t));
4820 memcpy(&objc_ivar_list, p, left);
4821 }
4822 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4823 swapStruct(objc_ivar_list);
4824 outs() << "\t\t ivar_count " << objc_ivar_list.ivar_count << "\n";
4825 ivar_list = p + sizeof(struct objc_ivar_list_t);
4826 for (i = 0; i < objc_ivar_list.ivar_count; i++) {
4827 if ((i + 1) * sizeof(struct objc_ivar_t) > left) {
4828 outs() << "\t\t remaining ivar's extend past the of the section\n";
4829 break;
4830 }
4831 memcpy(&ivar, ivar_list + i * sizeof(struct objc_ivar_t),
4832 sizeof(struct objc_ivar_t));
4833 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4834 swapStruct(ivar);
4835
4836 outs() << "\t\t\tivar_name " << format("0x%08" PRIx32, ivar.ivar_name);
4837 if (info->verbose) {
4838 name = get_pointer_32(ivar.ivar_name, offset, xleft, S, info, true);
4839 if (name != nullptr)
4840 outs() << format(" %.*s", xleft, name);
4841 else
4842 outs() << " (not in an __OBJC section)";
4843 }
4844 outs() << "\n";
4845
4846 outs() << "\t\t\tivar_type " << format("0x%08" PRIx32, ivar.ivar_type);
4847 if (info->verbose) {
4848 name = get_pointer_32(ivar.ivar_type, offset, xleft, S, info, true);
4849 if (name != nullptr)
4850 outs() << format(" %.*s", xleft, name);
4851 else
4852 outs() << " (not in an __OBJC section)";
4853 }
4854 outs() << "\n";
4855
4856 outs() << "\t\t ivar_offset "
4857 << format("0x%08" PRIx32, ivar.ivar_offset) << "\n";
4858 }
4859 } else {
4860 outs() << " (not in an __OBJC section)\n";
4861 }
4862
4863 outs() << "\t\t methods " << format("0x%08" PRIx32, objc_class->methodLists);
4864 if (print_method_list(objc_class->methodLists, info))
4865 outs() << " (not in an __OBJC section)\n";
4866
4867 outs() << "\t\t cache " << format("0x%08" PRIx32, objc_class->cache)
4868 << "\n";
4869
4870 outs() << "\t\tprotocols " << format("0x%08" PRIx32, objc_class->protocols);
4871 if (print_protocol_list(objc_class->protocols, 16, info))
4872 outs() << " (not in an __OBJC section)\n";
4873}
4874
4875static void print_objc_objc_category_t(struct objc_category_t *objc_category,
4876 struct DisassembleInfo *info) {
4877 uint32_t offset, left;
4878 const char *name;
4879 SectionRef S;
4880
4881 outs() << "\t category name "
4882 << format("0x%08" PRIx32, objc_category->category_name);
4883 if (info->verbose) {
4884 name = get_pointer_32(objc_category->category_name, offset, left, S, info,
4885 true);
4886 if (name != nullptr)
4887 outs() << format(" %.*s", left, name);
4888 else
4889 outs() << " (not in an __OBJC section)";
4890 }
4891 outs() << "\n";
4892
4893 outs() << "\t\t class name "
4894 << format("0x%08" PRIx32, objc_category->class_name);
4895 if (info->verbose) {
4896 name =
4897 get_pointer_32(objc_category->class_name, offset, left, S, info, true);
4898 if (name != nullptr)
4899 outs() << format(" %.*s", left, name);
4900 else
4901 outs() << " (not in an __OBJC section)";
4902 }
4903 outs() << "\n";
4904
4905 outs() << "\t instance methods "
4906 << format("0x%08" PRIx32, objc_category->instance_methods);
4907 if (print_method_list(objc_category->instance_methods, info))
4908 outs() << " (not in an __OBJC section)\n";
4909
4910 outs() << "\t class methods "
4911 << format("0x%08" PRIx32, objc_category->class_methods);
4912 if (print_method_list(objc_category->class_methods, info))
4913 outs() << " (not in an __OBJC section)\n";
4914}
4915
Kevin Enderby0fc11822015-04-01 20:57:01 +00004916static void print_category64_t(uint64_t p, struct DisassembleInfo *info) {
4917 struct category64_t c;
4918 const char *r;
4919 uint32_t offset, xoffset, left;
4920 SectionRef S, xS;
4921 const char *name, *sym_name;
4922 uint64_t n_value;
4923
4924 r = get_pointer_64(p, offset, left, S, info);
4925 if (r == nullptr)
4926 return;
4927 memset(&c, '\0', sizeof(struct category64_t));
4928 if (left < sizeof(struct category64_t)) {
4929 memcpy(&c, r, left);
4930 outs() << " (category_t entends past the end of the section)\n";
4931 } else
4932 memcpy(&c, r, sizeof(struct category64_t));
4933 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4934 swapStruct(c);
4935
4936 outs() << " name ";
4937 sym_name = get_symbol_64(offset + offsetof(struct category64_t, name), S,
4938 info, n_value, c.name);
4939 if (n_value != 0) {
4940 if (info->verbose && sym_name != nullptr)
4941 outs() << sym_name;
4942 else
4943 outs() << format("0x%" PRIx64, n_value);
4944 if (c.name != 0)
4945 outs() << " + " << format("0x%" PRIx64, c.name);
4946 } else
4947 outs() << format("0x%" PRIx64, c.name);
4948 name = get_pointer_64(c.name + n_value, xoffset, left, xS, info);
4949 if (name != nullptr)
4950 outs() << format(" %.*s", left, name);
4951 outs() << "\n";
4952
4953 outs() << " cls ";
4954 sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls), S, info,
4955 n_value, c.cls);
4956 if (n_value != 0) {
4957 if (info->verbose && sym_name != nullptr)
4958 outs() << sym_name;
4959 else
4960 outs() << format("0x%" PRIx64, n_value);
4961 if (c.cls != 0)
4962 outs() << " + " << format("0x%" PRIx64, c.cls);
4963 } else
4964 outs() << format("0x%" PRIx64, c.cls);
4965 outs() << "\n";
4966 if (c.cls + n_value != 0)
4967 print_class64_t(c.cls + n_value, info);
4968
4969 outs() << " instanceMethods ";
4970 sym_name =
4971 get_symbol_64(offset + offsetof(struct category64_t, instanceMethods), S,
4972 info, n_value, c.instanceMethods);
4973 if (n_value != 0) {
4974 if (info->verbose && sym_name != nullptr)
4975 outs() << sym_name;
4976 else
4977 outs() << format("0x%" PRIx64, n_value);
4978 if (c.instanceMethods != 0)
4979 outs() << " + " << format("0x%" PRIx64, c.instanceMethods);
4980 } else
4981 outs() << format("0x%" PRIx64, c.instanceMethods);
4982 outs() << "\n";
4983 if (c.instanceMethods + n_value != 0)
4984 print_method_list64_t(c.instanceMethods + n_value, info, "");
4985
4986 outs() << " classMethods ";
4987 sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods),
4988 S, info, n_value, c.classMethods);
4989 if (n_value != 0) {
4990 if (info->verbose && sym_name != nullptr)
4991 outs() << sym_name;
4992 else
4993 outs() << format("0x%" PRIx64, n_value);
4994 if (c.classMethods != 0)
4995 outs() << " + " << format("0x%" PRIx64, c.classMethods);
4996 } else
4997 outs() << format("0x%" PRIx64, c.classMethods);
4998 outs() << "\n";
4999 if (c.classMethods + n_value != 0)
5000 print_method_list64_t(c.classMethods + n_value, info, "");
5001
5002 outs() << " protocols ";
5003 sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols), S,
5004 info, n_value, c.protocols);
5005 if (n_value != 0) {
5006 if (info->verbose && sym_name != nullptr)
5007 outs() << sym_name;
5008 else
5009 outs() << format("0x%" PRIx64, n_value);
5010 if (c.protocols != 0)
5011 outs() << " + " << format("0x%" PRIx64, c.protocols);
5012 } else
5013 outs() << format("0x%" PRIx64, c.protocols);
5014 outs() << "\n";
5015 if (c.protocols + n_value != 0)
5016 print_protocol_list64_t(c.protocols + n_value, info);
5017
5018 outs() << "instanceProperties ";
5019 sym_name =
5020 get_symbol_64(offset + offsetof(struct category64_t, instanceProperties),
5021 S, info, n_value, c.instanceProperties);
5022 if (n_value != 0) {
5023 if (info->verbose && sym_name != nullptr)
5024 outs() << sym_name;
5025 else
5026 outs() << format("0x%" PRIx64, n_value);
5027 if (c.instanceProperties != 0)
5028 outs() << " + " << format("0x%" PRIx64, c.instanceProperties);
5029 } else
5030 outs() << format("0x%" PRIx64, c.instanceProperties);
5031 outs() << "\n";
5032 if (c.instanceProperties + n_value != 0)
5033 print_objc_property_list64(c.instanceProperties + n_value, info);
5034}
5035
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005036static void print_category32_t(uint32_t p, struct DisassembleInfo *info) {
5037 struct category32_t c;
5038 const char *r;
5039 uint32_t offset, left;
5040 SectionRef S, xS;
5041 const char *name;
5042
5043 r = get_pointer_32(p, offset, left, S, info);
5044 if (r == nullptr)
5045 return;
5046 memset(&c, '\0', sizeof(struct category32_t));
5047 if (left < sizeof(struct category32_t)) {
5048 memcpy(&c, r, left);
5049 outs() << " (category_t entends past the end of the section)\n";
5050 } else
5051 memcpy(&c, r, sizeof(struct category32_t));
5052 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5053 swapStruct(c);
5054
5055 outs() << " name " << format("0x%" PRIx32, c.name);
5056 name = get_symbol_32(offset + offsetof(struct category32_t, name), S, info,
5057 c.name);
Hans Wennborgcc9deb42015-09-29 18:02:48 +00005058 if (name)
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005059 outs() << " " << name;
5060 outs() << "\n";
5061
5062 outs() << " cls " << format("0x%" PRIx32, c.cls) << "\n";
5063 if (c.cls != 0)
5064 print_class32_t(c.cls, info);
5065 outs() << " instanceMethods " << format("0x%" PRIx32, c.instanceMethods)
5066 << "\n";
5067 if (c.instanceMethods != 0)
5068 print_method_list32_t(c.instanceMethods, info, "");
5069 outs() << " classMethods " << format("0x%" PRIx32, c.classMethods)
5070 << "\n";
5071 if (c.classMethods != 0)
5072 print_method_list32_t(c.classMethods, info, "");
5073 outs() << " protocols " << format("0x%" PRIx32, c.protocols) << "\n";
5074 if (c.protocols != 0)
5075 print_protocol_list32_t(c.protocols, info);
5076 outs() << "instanceProperties " << format("0x%" PRIx32, c.instanceProperties)
5077 << "\n";
5078 if (c.instanceProperties != 0)
5079 print_objc_property_list32(c.instanceProperties, info);
5080}
5081
Kevin Enderby0fc11822015-04-01 20:57:01 +00005082static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) {
5083 uint32_t i, left, offset, xoffset;
5084 uint64_t p, n_value;
5085 struct message_ref64 mr;
5086 const char *name, *sym_name;
5087 const char *r;
5088 SectionRef xS;
5089
5090 if (S == SectionRef())
5091 return;
5092
5093 StringRef SectName;
5094 S.getName(SectName);
5095 DataRefImpl Ref = S.getRawDataRefImpl();
5096 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5097 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5098 offset = 0;
5099 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
5100 p = S.getAddress() + i;
5101 r = get_pointer_64(p, offset, left, S, info);
5102 if (r == nullptr)
5103 return;
5104 memset(&mr, '\0', sizeof(struct message_ref64));
5105 if (left < sizeof(struct message_ref64)) {
5106 memcpy(&mr, r, left);
5107 outs() << " (message_ref entends past the end of the section)\n";
5108 } else
5109 memcpy(&mr, r, sizeof(struct message_ref64));
5110 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5111 swapStruct(mr);
5112
5113 outs() << " imp ";
5114 name = get_symbol_64(offset + offsetof(struct message_ref64, imp), S, info,
5115 n_value, mr.imp);
5116 if (n_value != 0) {
5117 outs() << format("0x%" PRIx64, n_value) << " ";
5118 if (mr.imp != 0)
5119 outs() << "+ " << format("0x%" PRIx64, mr.imp) << " ";
5120 } else
5121 outs() << format("0x%" PRIx64, mr.imp) << " ";
5122 if (name != nullptr)
5123 outs() << " " << name;
5124 outs() << "\n";
5125
5126 outs() << " sel ";
5127 sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel), S,
5128 info, n_value, mr.sel);
5129 if (n_value != 0) {
5130 if (info->verbose && sym_name != nullptr)
5131 outs() << sym_name;
5132 else
5133 outs() << format("0x%" PRIx64, n_value);
5134 if (mr.sel != 0)
5135 outs() << " + " << format("0x%" PRIx64, mr.sel);
5136 } else
5137 outs() << format("0x%" PRIx64, mr.sel);
5138 name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info);
5139 if (name != nullptr)
5140 outs() << format(" %.*s", left, name);
5141 outs() << "\n";
5142
5143 offset += sizeof(struct message_ref64);
5144 }
5145}
5146
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005147static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) {
5148 uint32_t i, left, offset, xoffset, p;
5149 struct message_ref32 mr;
5150 const char *name, *r;
5151 SectionRef xS;
5152
5153 if (S == SectionRef())
5154 return;
5155
5156 StringRef SectName;
5157 S.getName(SectName);
5158 DataRefImpl Ref = S.getRawDataRefImpl();
5159 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5160 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5161 offset = 0;
5162 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
5163 p = S.getAddress() + i;
5164 r = get_pointer_32(p, offset, left, S, info);
5165 if (r == nullptr)
5166 return;
5167 memset(&mr, '\0', sizeof(struct message_ref32));
5168 if (left < sizeof(struct message_ref32)) {
5169 memcpy(&mr, r, left);
5170 outs() << " (message_ref entends past the end of the section)\n";
5171 } else
5172 memcpy(&mr, r, sizeof(struct message_ref32));
5173 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5174 swapStruct(mr);
5175
5176 outs() << " imp " << format("0x%" PRIx32, mr.imp);
5177 name = get_symbol_32(offset + offsetof(struct message_ref32, imp), S, info,
5178 mr.imp);
5179 if (name != nullptr)
5180 outs() << " " << name;
5181 outs() << "\n";
5182
5183 outs() << " sel " << format("0x%" PRIx32, mr.sel);
5184 name = get_pointer_32(mr.sel, xoffset, left, xS, info);
5185 if (name != nullptr)
5186 outs() << " " << name;
5187 outs() << "\n";
5188
5189 offset += sizeof(struct message_ref32);
5190 }
5191}
5192
Kevin Enderby0fc11822015-04-01 20:57:01 +00005193static void print_image_info64(SectionRef S, struct DisassembleInfo *info) {
5194 uint32_t left, offset, swift_version;
5195 uint64_t p;
5196 struct objc_image_info64 o;
5197 const char *r;
5198
Kevin Enderbyaf7c9d02015-10-09 16:48:44 +00005199 if (S == SectionRef())
5200 return;
5201
Kevin Enderby0fc11822015-04-01 20:57:01 +00005202 StringRef SectName;
5203 S.getName(SectName);
5204 DataRefImpl Ref = S.getRawDataRefImpl();
5205 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5206 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5207 p = S.getAddress();
5208 r = get_pointer_64(p, offset, left, S, info);
5209 if (r == nullptr)
5210 return;
5211 memset(&o, '\0', sizeof(struct objc_image_info64));
5212 if (left < sizeof(struct objc_image_info64)) {
5213 memcpy(&o, r, left);
5214 outs() << " (objc_image_info entends past the end of the section)\n";
5215 } else
5216 memcpy(&o, r, sizeof(struct objc_image_info64));
5217 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5218 swapStruct(o);
5219 outs() << " version " << o.version << "\n";
5220 outs() << " flags " << format("0x%" PRIx32, o.flags);
5221 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5222 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5223 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5224 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5225 swift_version = (o.flags >> 8) & 0xff;
5226 if (swift_version != 0) {
5227 if (swift_version == 1)
5228 outs() << " Swift 1.0";
5229 else if (swift_version == 2)
5230 outs() << " Swift 1.1";
5231 else
5232 outs() << " unknown future Swift version (" << swift_version << ")";
5233 }
5234 outs() << "\n";
5235}
5236
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005237static void print_image_info32(SectionRef S, struct DisassembleInfo *info) {
5238 uint32_t left, offset, swift_version, p;
5239 struct objc_image_info32 o;
5240 const char *r;
5241
Kevin Enderby19be2512016-04-21 19:49:29 +00005242 if (S == SectionRef())
5243 return;
5244
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005245 StringRef SectName;
5246 S.getName(SectName);
5247 DataRefImpl Ref = S.getRawDataRefImpl();
5248 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5249 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5250 p = S.getAddress();
5251 r = get_pointer_32(p, offset, left, S, info);
5252 if (r == nullptr)
5253 return;
5254 memset(&o, '\0', sizeof(struct objc_image_info32));
5255 if (left < sizeof(struct objc_image_info32)) {
5256 memcpy(&o, r, left);
5257 outs() << " (objc_image_info entends past the end of the section)\n";
5258 } else
5259 memcpy(&o, r, sizeof(struct objc_image_info32));
5260 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5261 swapStruct(o);
5262 outs() << " version " << o.version << "\n";
5263 outs() << " flags " << format("0x%" PRIx32, o.flags);
5264 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5265 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5266 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5267 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5268 swift_version = (o.flags >> 8) & 0xff;
5269 if (swift_version != 0) {
5270 if (swift_version == 1)
5271 outs() << " Swift 1.0";
5272 else if (swift_version == 2)
5273 outs() << " Swift 1.1";
5274 else
5275 outs() << " unknown future Swift version (" << swift_version << ")";
5276 }
5277 outs() << "\n";
5278}
5279
Kevin Enderby846c0002015-04-16 17:19:59 +00005280static void print_image_info(SectionRef S, struct DisassembleInfo *info) {
5281 uint32_t left, offset, p;
5282 struct imageInfo_t o;
5283 const char *r;
5284
5285 StringRef SectName;
5286 S.getName(SectName);
5287 DataRefImpl Ref = S.getRawDataRefImpl();
5288 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5289 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5290 p = S.getAddress();
5291 r = get_pointer_32(p, offset, left, S, info);
5292 if (r == nullptr)
5293 return;
5294 memset(&o, '\0', sizeof(struct imageInfo_t));
5295 if (left < sizeof(struct imageInfo_t)) {
5296 memcpy(&o, r, left);
5297 outs() << " (imageInfo entends past the end of the section)\n";
5298 } else
5299 memcpy(&o, r, sizeof(struct imageInfo_t));
5300 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5301 swapStruct(o);
5302 outs() << " version " << o.version << "\n";
5303 outs() << " flags " << format("0x%" PRIx32, o.flags);
5304 if (o.flags & 0x1)
5305 outs() << " F&C";
5306 if (o.flags & 0x2)
5307 outs() << " GC";
5308 if (o.flags & 0x4)
5309 outs() << " GC-only";
5310 else
5311 outs() << " RR";
5312 outs() << "\n";
5313}
5314
Kevin Enderby0fc11822015-04-01 20:57:01 +00005315static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) {
5316 SymbolAddressMap AddrMap;
5317 if (verbose)
5318 CreateSymbolAddressMap(O, &AddrMap);
5319
5320 std::vector<SectionRef> Sections;
5321 for (const SectionRef &Section : O->sections()) {
5322 StringRef SectName;
5323 Section.getName(SectName);
5324 Sections.push_back(Section);
5325 }
5326
5327 struct DisassembleInfo info;
5328 // Set up the block of info used by the Symbolizer call backs.
5329 info.verbose = verbose;
5330 info.O = O;
5331 info.AddrMap = &AddrMap;
5332 info.Sections = &Sections;
5333 info.class_name = nullptr;
5334 info.selector_name = nullptr;
5335 info.method = nullptr;
5336 info.demangled_name = nullptr;
5337 info.bindtable = nullptr;
5338 info.adrp_addr = 0;
5339 info.adrp_inst = 0;
5340
Kevin Enderbyaac75382015-10-08 16:56:35 +00005341 info.depth = 0;
Davide Italiano62507042015-12-11 22:27:59 +00005342 SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5343 if (CL == SectionRef())
5344 CL = get_section(O, "__DATA", "__objc_classlist");
5345 info.S = CL;
5346 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005347
Davide Italiano62507042015-12-11 22:27:59 +00005348 SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5349 if (CR == SectionRef())
5350 CR = get_section(O, "__DATA", "__objc_classrefs");
5351 info.S = CR;
5352 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005353
Davide Italiano62507042015-12-11 22:27:59 +00005354 SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5355 if (SR == SectionRef())
5356 SR = get_section(O, "__DATA", "__objc_superrefs");
5357 info.S = SR;
5358 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005359
Davide Italiano62507042015-12-11 22:27:59 +00005360 SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5361 if (CA == SectionRef())
5362 CA = get_section(O, "__DATA", "__objc_catlist");
5363 info.S = CA;
5364 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005365
Davide Italiano62507042015-12-11 22:27:59 +00005366 SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5367 if (PL == SectionRef())
5368 PL = get_section(O, "__DATA", "__objc_protolist");
5369 info.S = PL;
5370 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005371
Davide Italiano62507042015-12-11 22:27:59 +00005372 SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5373 if (MR == SectionRef())
5374 MR = get_section(O, "__DATA", "__objc_msgrefs");
5375 info.S = MR;
5376 print_message_refs64(MR, &info);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005377
Davide Italiano62507042015-12-11 22:27:59 +00005378 SectionRef II = get_section(O, "__OBJC2", "__image_info");
5379 if (II == SectionRef())
5380 II = get_section(O, "__DATA", "__objc_imageinfo");
5381 info.S = II;
5382 print_image_info64(II, &info);
Kevin Enderby0bc6ed42015-04-01 21:50:45 +00005383
5384 if (info.bindtable != nullptr)
5385 delete info.bindtable;
Kevin Enderby0fc11822015-04-01 20:57:01 +00005386}
5387
5388static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) {
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005389 SymbolAddressMap AddrMap;
5390 if (verbose)
5391 CreateSymbolAddressMap(O, &AddrMap);
5392
5393 std::vector<SectionRef> Sections;
5394 for (const SectionRef &Section : O->sections()) {
5395 StringRef SectName;
5396 Section.getName(SectName);
5397 Sections.push_back(Section);
5398 }
5399
5400 struct DisassembleInfo info;
5401 // Set up the block of info used by the Symbolizer call backs.
5402 info.verbose = verbose;
5403 info.O = O;
5404 info.AddrMap = &AddrMap;
5405 info.Sections = &Sections;
5406 info.class_name = nullptr;
5407 info.selector_name = nullptr;
5408 info.method = nullptr;
5409 info.demangled_name = nullptr;
5410 info.bindtable = nullptr;
5411 info.adrp_addr = 0;
5412 info.adrp_inst = 0;
5413
5414 const SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5415 if (CL != SectionRef()) {
5416 info.S = CL;
5417 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5418 } else {
5419 const SectionRef CL = get_section(O, "__DATA", "__objc_classlist");
5420 info.S = CL;
5421 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5422 }
5423
5424 const SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5425 if (CR != SectionRef()) {
5426 info.S = CR;
5427 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5428 } else {
5429 const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs");
5430 info.S = CR;
5431 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5432 }
5433
5434 const SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5435 if (SR != SectionRef()) {
5436 info.S = SR;
5437 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5438 } else {
5439 const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs");
5440 info.S = SR;
5441 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5442 }
5443
5444 const SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5445 if (CA != SectionRef()) {
5446 info.S = CA;
5447 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5448 } else {
5449 const SectionRef CA = get_section(O, "__DATA", "__objc_catlist");
5450 info.S = CA;
5451 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5452 }
5453
5454 const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5455 if (PL != SectionRef()) {
5456 info.S = PL;
5457 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5458 } else {
5459 const SectionRef PL = get_section(O, "__DATA", "__objc_protolist");
5460 info.S = PL;
5461 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5462 }
5463
5464 const SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5465 if (MR != SectionRef()) {
5466 info.S = MR;
5467 print_message_refs32(MR, &info);
5468 } else {
5469 const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs");
5470 info.S = MR;
5471 print_message_refs32(MR, &info);
5472 }
5473
5474 const SectionRef II = get_section(O, "__OBJC2", "__image_info");
5475 if (II != SectionRef()) {
5476 info.S = II;
5477 print_image_info32(II, &info);
5478 } else {
5479 const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo");
5480 info.S = II;
5481 print_image_info32(II, &info);
5482 }
Kevin Enderby0fc11822015-04-01 20:57:01 +00005483}
5484
5485static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) {
Kevin Enderby846c0002015-04-16 17:19:59 +00005486 uint32_t i, j, p, offset, xoffset, left, defs_left, def;
5487 const char *r, *name, *defs;
5488 struct objc_module_t module;
5489 SectionRef S, xS;
5490 struct objc_symtab_t symtab;
5491 struct objc_class_t objc_class;
5492 struct objc_category_t objc_category;
5493
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005494 outs() << "Objective-C segment\n";
Kevin Enderby846c0002015-04-16 17:19:59 +00005495 S = get_section(O, "__OBJC", "__module_info");
5496 if (S == SectionRef())
5497 return false;
5498
5499 SymbolAddressMap AddrMap;
5500 if (verbose)
5501 CreateSymbolAddressMap(O, &AddrMap);
5502
5503 std::vector<SectionRef> Sections;
5504 for (const SectionRef &Section : O->sections()) {
5505 StringRef SectName;
5506 Section.getName(SectName);
5507 Sections.push_back(Section);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005508 }
Kevin Enderby846c0002015-04-16 17:19:59 +00005509
5510 struct DisassembleInfo info;
5511 // Set up the block of info used by the Symbolizer call backs.
5512 info.verbose = verbose;
5513 info.O = O;
5514 info.AddrMap = &AddrMap;
5515 info.Sections = &Sections;
5516 info.class_name = nullptr;
5517 info.selector_name = nullptr;
5518 info.method = nullptr;
5519 info.demangled_name = nullptr;
5520 info.bindtable = nullptr;
5521 info.adrp_addr = 0;
5522 info.adrp_inst = 0;
5523
5524 for (i = 0; i < S.getSize(); i += sizeof(struct objc_module_t)) {
5525 p = S.getAddress() + i;
5526 r = get_pointer_32(p, offset, left, S, &info, true);
5527 if (r == nullptr)
5528 return true;
5529 memset(&module, '\0', sizeof(struct objc_module_t));
5530 if (left < sizeof(struct objc_module_t)) {
5531 memcpy(&module, r, left);
5532 outs() << " (module extends past end of __module_info section)\n";
5533 } else
5534 memcpy(&module, r, sizeof(struct objc_module_t));
5535 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5536 swapStruct(module);
5537
5538 outs() << "Module " << format("0x%" PRIx32, p) << "\n";
5539 outs() << " version " << module.version << "\n";
5540 outs() << " size " << module.size << "\n";
5541 outs() << " name ";
5542 name = get_pointer_32(module.name, xoffset, left, xS, &info, true);
5543 if (name != nullptr)
5544 outs() << format("%.*s", left, name);
5545 else
5546 outs() << format("0x%08" PRIx32, module.name)
5547 << "(not in an __OBJC section)";
5548 outs() << "\n";
5549
5550 r = get_pointer_32(module.symtab, xoffset, left, xS, &info, true);
5551 if (module.symtab == 0 || r == nullptr) {
5552 outs() << " symtab " << format("0x%08" PRIx32, module.symtab)
5553 << " (not in an __OBJC section)\n";
5554 continue;
5555 }
5556 outs() << " symtab " << format("0x%08" PRIx32, module.symtab) << "\n";
5557 memset(&symtab, '\0', sizeof(struct objc_symtab_t));
5558 defs_left = 0;
5559 defs = nullptr;
5560 if (left < sizeof(struct objc_symtab_t)) {
5561 memcpy(&symtab, r, left);
5562 outs() << "\tsymtab extends past end of an __OBJC section)\n";
5563 } else {
5564 memcpy(&symtab, r, sizeof(struct objc_symtab_t));
5565 if (left > sizeof(struct objc_symtab_t)) {
5566 defs_left = left - sizeof(struct objc_symtab_t);
5567 defs = r + sizeof(struct objc_symtab_t);
5568 }
5569 }
5570 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5571 swapStruct(symtab);
5572
5573 outs() << "\tsel_ref_cnt " << symtab.sel_ref_cnt << "\n";
5574 r = get_pointer_32(symtab.refs, xoffset, left, xS, &info, true);
5575 outs() << "\trefs " << format("0x%08" PRIx32, symtab.refs);
5576 if (r == nullptr)
5577 outs() << " (not in an __OBJC section)";
5578 outs() << "\n";
5579 outs() << "\tcls_def_cnt " << symtab.cls_def_cnt << "\n";
5580 outs() << "\tcat_def_cnt " << symtab.cat_def_cnt << "\n";
5581 if (symtab.cls_def_cnt > 0)
5582 outs() << "\tClass Definitions\n";
5583 for (j = 0; j < symtab.cls_def_cnt; j++) {
5584 if ((j + 1) * sizeof(uint32_t) > defs_left) {
5585 outs() << "\t(remaining class defs entries entends past the end of the "
5586 << "section)\n";
5587 break;
5588 }
5589 memcpy(&def, defs + j * sizeof(uint32_t), sizeof(uint32_t));
5590 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5591 sys::swapByteOrder(def);
5592
5593 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5594 outs() << "\tdefs[" << j << "] " << format("0x%08" PRIx32, def);
5595 if (r != nullptr) {
5596 if (left > sizeof(struct objc_class_t)) {
5597 outs() << "\n";
5598 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5599 } else {
5600 outs() << " (entends past the end of the section)\n";
5601 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5602 memcpy(&objc_class, r, left);
5603 }
5604 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5605 swapStruct(objc_class);
5606 print_objc_class_t(&objc_class, &info);
5607 } else {
5608 outs() << "(not in an __OBJC section)\n";
5609 }
5610
5611 if (CLS_GETINFO(&objc_class, CLS_CLASS)) {
5612 outs() << "\tMeta Class";
5613 r = get_pointer_32(objc_class.isa, xoffset, left, xS, &info, true);
5614 if (r != nullptr) {
5615 if (left > sizeof(struct objc_class_t)) {
5616 outs() << "\n";
5617 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5618 } else {
5619 outs() << " (entends past the end of the section)\n";
5620 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5621 memcpy(&objc_class, r, left);
5622 }
5623 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5624 swapStruct(objc_class);
5625 print_objc_class_t(&objc_class, &info);
5626 } else {
5627 outs() << "(not in an __OBJC section)\n";
5628 }
5629 }
5630 }
5631 if (symtab.cat_def_cnt > 0)
5632 outs() << "\tCategory Definitions\n";
5633 for (j = 0; j < symtab.cat_def_cnt; j++) {
5634 if ((j + symtab.cls_def_cnt + 1) * sizeof(uint32_t) > defs_left) {
5635 outs() << "\t(remaining category defs entries entends past the end of "
5636 << "the section)\n";
5637 break;
5638 }
5639 memcpy(&def, defs + (j + symtab.cls_def_cnt) * sizeof(uint32_t),
5640 sizeof(uint32_t));
5641 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5642 sys::swapByteOrder(def);
5643
5644 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5645 outs() << "\tdefs[" << j + symtab.cls_def_cnt << "] "
5646 << format("0x%08" PRIx32, def);
5647 if (r != nullptr) {
5648 if (left > sizeof(struct objc_category_t)) {
5649 outs() << "\n";
5650 memcpy(&objc_category, r, sizeof(struct objc_category_t));
5651 } else {
5652 outs() << " (entends past the end of the section)\n";
5653 memset(&objc_category, '\0', sizeof(struct objc_category_t));
5654 memcpy(&objc_category, r, left);
5655 }
5656 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5657 swapStruct(objc_category);
5658 print_objc_objc_category_t(&objc_category, &info);
5659 } else {
5660 outs() << "(not in an __OBJC section)\n";
5661 }
5662 }
5663 }
5664 const SectionRef II = get_section(O, "__OBJC", "__image_info");
5665 if (II != SectionRef())
5666 print_image_info(II, &info);
5667
5668 return true;
Kevin Enderby0fc11822015-04-01 20:57:01 +00005669}
5670
Kevin Enderby4ad9bde2015-04-16 22:33:20 +00005671static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
5672 uint32_t size, uint32_t addr) {
5673 SymbolAddressMap AddrMap;
5674 CreateSymbolAddressMap(O, &AddrMap);
5675
5676 std::vector<SectionRef> Sections;
5677 for (const SectionRef &Section : O->sections()) {
5678 StringRef SectName;
5679 Section.getName(SectName);
5680 Sections.push_back(Section);
5681 }
5682
5683 struct DisassembleInfo info;
5684 // Set up the block of info used by the Symbolizer call backs.
5685 info.verbose = true;
5686 info.O = O;
5687 info.AddrMap = &AddrMap;
5688 info.Sections = &Sections;
5689 info.class_name = nullptr;
5690 info.selector_name = nullptr;
5691 info.method = nullptr;
5692 info.demangled_name = nullptr;
5693 info.bindtable = nullptr;
5694 info.adrp_addr = 0;
5695 info.adrp_inst = 0;
5696
5697 const char *p;
5698 struct objc_protocol_t protocol;
5699 uint32_t left, paddr;
5700 for (p = sect; p < sect + size; p += sizeof(struct objc_protocol_t)) {
5701 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
5702 left = size - (p - sect);
5703 if (left < sizeof(struct objc_protocol_t)) {
5704 outs() << "Protocol extends past end of __protocol section\n";
5705 memcpy(&protocol, p, left);
5706 } else
5707 memcpy(&protocol, p, sizeof(struct objc_protocol_t));
5708 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5709 swapStruct(protocol);
5710 paddr = addr + (p - sect);
5711 outs() << "Protocol " << format("0x%" PRIx32, paddr);
5712 if (print_protocol(paddr, 0, &info))
5713 outs() << "(not in an __OBJC section)\n";
5714 }
5715}
5716
Kevin Enderby9873e2c2016-05-23 21:34:12 +00005717#ifdef HAVE_LIBXAR
5718inline void swapStruct(struct xar_header &xar) {
5719 sys::swapByteOrder(xar.magic);
5720 sys::swapByteOrder(xar.size);
5721 sys::swapByteOrder(xar.version);
5722 sys::swapByteOrder(xar.toc_length_compressed);
5723 sys::swapByteOrder(xar.toc_length_uncompressed);
5724 sys::swapByteOrder(xar.cksum_alg);
5725}
5726
5727static void PrintModeVerbose(uint32_t mode) {
5728 switch(mode & S_IFMT){
5729 case S_IFDIR:
5730 outs() << "d";
5731 break;
5732 case S_IFCHR:
5733 outs() << "c";
5734 break;
5735 case S_IFBLK:
5736 outs() << "b";
5737 break;
5738 case S_IFREG:
5739 outs() << "-";
5740 break;
5741 case S_IFLNK:
5742 outs() << "l";
5743 break;
5744 case S_IFSOCK:
5745 outs() << "s";
5746 break;
5747 default:
5748 outs() << "?";
5749 break;
5750 }
5751
5752 /* owner permissions */
5753 if(mode & S_IREAD)
5754 outs() << "r";
5755 else
5756 outs() << "-";
5757 if(mode & S_IWRITE)
5758 outs() << "w";
5759 else
5760 outs() << "-";
5761 if(mode & S_ISUID)
5762 outs() << "s";
5763 else if(mode & S_IEXEC)
5764 outs() << "x";
5765 else
5766 outs() << "-";
5767
5768 /* group permissions */
5769 if(mode & (S_IREAD >> 3))
5770 outs() << "r";
5771 else
5772 outs() << "-";
5773 if(mode & (S_IWRITE >> 3))
5774 outs() << "w";
5775 else
5776 outs() << "-";
5777 if(mode & S_ISGID)
5778 outs() << "s";
5779 else if(mode & (S_IEXEC >> 3))
5780 outs() << "x";
5781 else
5782 outs() << "-";
5783
5784 /* other permissions */
5785 if(mode & (S_IREAD >> 6))
5786 outs() << "r";
5787 else
5788 outs() << "-";
5789 if(mode & (S_IWRITE >> 6))
5790 outs() << "w";
5791 else
5792 outs() << "-";
5793 if(mode & S_ISVTX)
5794 outs() << "t";
5795 else if(mode & (S_IEXEC >> 6))
5796 outs() << "x";
5797 else
5798 outs() << "-";
5799}
5800
5801static void PrintXarFilesSummary(const char *XarFilename, xar_t xar) {
5802 xar_iter_t xi;
5803 xar_file_t xf;
5804 xar_iter_t xp;
5805 const char *key, *type, *mode, *user, *group, *size, *mtime, *name, *m;
5806 char *endp;
5807 uint32_t mode_value;
5808
5809 xi = xar_iter_new();
5810 if (!xi) {
5811 errs() << "Can't obtain an xar iterator for xar archive "
5812 << XarFilename << "\n";
5813 return;
5814 }
5815
5816 // Go through the xar's files.
5817 for (xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)) {
5818 xp = xar_iter_new();
5819 if(!xp){
5820 errs() << "Can't obtain an xar iterator for xar archive "
5821 << XarFilename << "\n";
5822 return;
5823 }
5824 type = nullptr;
5825 mode = nullptr;
5826 user = nullptr;
5827 group = nullptr;
5828 size = nullptr;
5829 mtime = nullptr;
5830 name = nullptr;
5831 for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){
5832 const char *val = nullptr;
5833 xar_prop_get(xf, key, &val);
5834#if 0 // Useful for debugging.
5835 outs() << "key: " << key << " value: " << val << "\n";
5836#endif
5837 if(strcmp(key, "type") == 0)
5838 type = val;
5839 if(strcmp(key, "mode") == 0)
5840 mode = val;
5841 if(strcmp(key, "user") == 0)
5842 user = val;
5843 if(strcmp(key, "group") == 0)
5844 group = val;
5845 if(strcmp(key, "data/size") == 0)
5846 size = val;
5847 if(strcmp(key, "mtime") == 0)
5848 mtime = val;
5849 if(strcmp(key, "name") == 0)
5850 name = val;
5851 }
5852 if(mode != nullptr){
5853 mode_value = strtoul(mode, &endp, 8);
5854 if(*endp != '\0')
5855 outs() << "(mode: \"" << mode << "\" contains non-octal chars) ";
5856 if(strcmp(type, "file") == 0)
5857 mode_value |= S_IFREG;
5858 PrintModeVerbose(mode_value);
5859 outs() << " ";
5860 }
5861 if(user != nullptr)
5862 outs() << format("%10s/", user);
5863 if(group != nullptr)
5864 outs() << format("%-10s ", group);
5865 if(size != nullptr)
5866 outs() << format("%7s ", size);
5867 if(mtime != nullptr){
5868 for(m = mtime; *m != 'T' && *m != '\0'; m++)
5869 outs() << *m;
5870 if(*m == 'T')
5871 m++;
5872 outs() << " ";
5873 for( ; *m != 'Z' && *m != '\0'; m++)
5874 outs() << *m;
5875 outs() << " ";
5876 }
5877 if(name != nullptr)
5878 outs() << name;
5879 outs() << "\n";
5880 }
5881}
5882
5883static void DumpBitcodeSection(MachOObjectFile *O, const char *sect,
5884 uint32_t size, bool verbose,
5885 bool PrintXarHeader, bool PrintXarFileHeaders,
5886 std::string XarMemberName) {
5887 if(size < sizeof(struct xar_header)) {
5888 outs() << "size of (__LLVM,__bundle) section too small (smaller than size "
5889 "of struct xar_header)\n";
5890 return;
5891 }
5892 struct xar_header XarHeader;
5893 memcpy(&XarHeader, sect, sizeof(struct xar_header));
5894 if (sys::IsLittleEndianHost)
5895 swapStruct(XarHeader);
5896 if (PrintXarHeader) {
5897 if (!XarMemberName.empty())
5898 outs() << "In xar member " << XarMemberName << ": ";
5899 else
5900 outs() << "For (__LLVM,__bundle) section: ";
5901 outs() << "xar header\n";
5902 if (XarHeader.magic == XAR_HEADER_MAGIC)
5903 outs() << " magic XAR_HEADER_MAGIC\n";
5904 else
5905 outs() << " magic "
5906 << format_hex(XarHeader.magic, 10, true)
5907 << " (not XAR_HEADER_MAGIC)\n";
5908 outs() << " size " << XarHeader.size << "\n";
5909 outs() << " version " << XarHeader.version << "\n";
5910 outs() << " toc_length_compressed " << XarHeader.toc_length_compressed
5911 << "\n";
5912 outs() << "toc_length_uncompressed " << XarHeader.toc_length_uncompressed
5913 << "\n";
5914 outs() << " cksum_alg ";
5915 switch (XarHeader.cksum_alg) {
5916 case XAR_CKSUM_NONE:
5917 outs() << "XAR_CKSUM_NONE\n";
5918 break;
5919 case XAR_CKSUM_SHA1:
5920 outs() << "XAR_CKSUM_SHA1\n";
5921 break;
5922 case XAR_CKSUM_MD5:
5923 outs() << "XAR_CKSUM_MD5\n";
5924 break;
Kevin Enderby42882282016-05-23 22:18:59 +00005925#ifdef XAR_CKSUM_SHA256
Kevin Enderby9873e2c2016-05-23 21:34:12 +00005926 case XAR_CKSUM_SHA256:
5927 outs() << "XAR_CKSUM_SHA256\n";
5928 break;
Kevin Enderby42882282016-05-23 22:18:59 +00005929#endif
5930#ifdef XAR_CKSUM_SHA512
Kevin Enderby9873e2c2016-05-23 21:34:12 +00005931 case XAR_CKSUM_SHA512:
5932 outs() << "XAR_CKSUM_SHA512\n";
5933 break;
Kevin Enderby42882282016-05-23 22:18:59 +00005934#endif
Kevin Enderby9873e2c2016-05-23 21:34:12 +00005935 default:
5936 outs() << XarHeader.cksum_alg << "\n";
5937 }
5938 }
5939
5940 SmallString<128> XarFilename;
5941 int FD;
5942 std::error_code XarEC =
5943 sys::fs::createTemporaryFile("llvm-objdump", "xar", FD, XarFilename);
5944 if (XarEC) {
5945 errs() << XarEC.message() << "\n";
5946 return;
5947 }
5948 tool_output_file XarFile(XarFilename, FD);
5949 raw_fd_ostream &XarOut = XarFile.os();
5950 StringRef XarContents(sect, size);
5951 XarOut << XarContents;
5952 XarOut.close();
5953 if (XarOut.has_error())
5954 return;
5955
5956 xar_t xar = xar_open(XarFilename.c_str(), READ);
5957 if (!xar) {
5958 errs() << "Can't create temporary xar archive " << XarFilename << "\n";
5959 return;
5960 }
5961
5962 SmallString<128> TocFilename;
5963 std::error_code TocEC =
5964 sys::fs::createTemporaryFile("llvm-objdump", "toc", TocFilename);
5965 if (TocEC) {
5966 errs() << TocEC.message() << "\n";
5967 return;
5968 }
5969 xar_serialize(xar, TocFilename.c_str());
5970
5971 if (PrintXarFileHeaders) {
5972 if (!XarMemberName.empty())
5973 outs() << "In xar member " << XarMemberName << ": ";
5974 else
5975 outs() << "For (__LLVM,__bundle) section: ";
5976 outs() << "xar archive files:\n";
5977 PrintXarFilesSummary(XarFilename.c_str(), xar);
5978 }
5979
5980 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
5981 MemoryBuffer::getFileOrSTDIN(TocFilename.c_str());
5982 if (std::error_code EC = FileOrErr.getError()) {
5983 errs() << EC.message() << "\n";
5984 return;
5985 }
5986 std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
5987
5988 if (!XarMemberName.empty())
5989 outs() << "In xar member " << XarMemberName << ": ";
5990 else
5991 outs() << "For (__LLVM,__bundle) section: ";
5992 outs() << "xar table of contents:\n";
5993 outs() << Buffer->getBuffer() << "\n";
5994
5995 // TODO: Go through the xar's files.
5996 xar_iter_t xi = xar_iter_new();
5997 if(!xi){
5998 errs() << "Can't obtain an xar iterator for xar archive "
5999 << XarFilename.c_str() << "\n";
6000 xar_close(xar);
6001 return;
6002 }
6003 for(xar_file_t xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)){
6004 const char *key;
6005 xar_iter_t xp;
6006 const char *member_name, *member_type, *member_size_string;
6007 size_t member_size;
6008
6009 xp = xar_iter_new();
6010 if(!xp){
6011 errs() << "Can't obtain an xar iterator for xar archive "
6012 << XarFilename.c_str() << "\n";
6013 xar_close(xar);
6014 return;
6015 }
6016 member_name = NULL;
6017 member_type = NULL;
6018 member_size_string = NULL;
6019 for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){
6020 const char *val = nullptr;
6021 xar_prop_get(xf, key, &val);
6022#if 0 // Useful for debugging.
6023 outs() << "key: " << key << " value: " << val << "\n";
6024#endif
6025 if(strcmp(key, "name") == 0)
6026 member_name = val;
6027 if(strcmp(key, "type") == 0)
6028 member_type = val;
6029 if(strcmp(key, "data/size") == 0)
6030 member_size_string = val;
6031 }
6032 /*
6033 * If we find a file with a name, date/size and type properties
6034 * and with the type being "file" see if that is a xar file.
6035 */
6036 if (member_name != NULL && member_type != NULL &&
6037 strcmp(member_type, "file") == 0 &&
6038 member_size_string != NULL){
6039 // Extract the file into a buffer.
6040 char *endptr;
6041 member_size = strtoul(member_size_string, &endptr, 10);
6042 if (*endptr == '\0' && member_size != 0) {
6043 char *buffer = (char *) ::operator new (member_size);
6044 if (xar_extract_tobuffersz(xar, xf, &buffer, &member_size) == 0) {
6045#if 0 // Useful for debugging.
6046 outs() << "xar member: " << member_name << " extracted\n";
6047#endif
6048 // Set the XarMemberName we want to see printed in the header.
6049 std::string OldXarMemberName;
6050 // If XarMemberName is already set this is nested. So
6051 // save the old name and create the nested name.
6052 if (!XarMemberName.empty()) {
6053 OldXarMemberName = XarMemberName;
6054 XarMemberName =
6055 (Twine("[") + XarMemberName + "]" + member_name).str();
6056 } else {
6057 OldXarMemberName = "";
6058 XarMemberName = member_name;
6059 }
6060 // See if this is could be a xar file (nested).
6061 if (member_size >= sizeof(struct xar_header)) {
6062#if 0 // Useful for debugging.
6063 outs() << "could be a xar file: " << member_name << "\n";
6064#endif
6065 memcpy((char *)&XarHeader, buffer, sizeof(struct xar_header));
6066 if (sys::IsLittleEndianHost)
6067 swapStruct(XarHeader);
6068 if(XarHeader.magic == XAR_HEADER_MAGIC)
6069 DumpBitcodeSection(O, buffer, member_size, verbose,
6070 PrintXarHeader, PrintXarFileHeaders,
6071 XarMemberName);
6072 }
6073 XarMemberName = OldXarMemberName;
6074 }
6075 delete buffer;
6076 }
6077 }
6078 xar_iter_free(xp);
6079 }
6080 xar_close(xar);
6081}
6082#endif // defined(HAVE_LIBXAR)
6083
Kevin Enderby0fc11822015-04-01 20:57:01 +00006084static void printObjcMetaData(MachOObjectFile *O, bool verbose) {
6085 if (O->is64Bit())
6086 printObjc2_64bit_MetaData(O, verbose);
6087 else {
6088 MachO::mach_header H;
6089 H = O->getHeader();
6090 if (H.cputype == MachO::CPU_TYPE_ARM)
6091 printObjc2_32bit_MetaData(O, verbose);
6092 else {
6093 // This is the 32-bit non-arm cputype case. Which is normally
6094 // the first Objective-C ABI. But it may be the case of a
6095 // binary for the iOS simulator which is the second Objective-C
6096 // ABI. In that case printObjc1_32bit_MetaData() will determine that
6097 // and return false.
Hans Wennborgcc9deb42015-09-29 18:02:48 +00006098 if (!printObjc1_32bit_MetaData(O, verbose))
Kevin Enderby0fc11822015-04-01 20:57:01 +00006099 printObjc2_32bit_MetaData(O, verbose);
6100 }
6101 }
6102}
6103
Kevin Enderbybf246f52014-09-24 23:08:22 +00006104// GuessLiteralPointer returns a string which for the item in the Mach-O file
6105// for the address passed in as ReferenceValue for printing as a comment with
6106// the instruction and also returns the corresponding type of that item
6107// indirectly through ReferenceType.
6108//
6109// If ReferenceValue is an address of literal cstring then a pointer to the
6110// cstring is returned and ReferenceType is set to
6111// LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr .
6112//
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006113// If ReferenceValue is an address of an Objective-C CFString, Selector ref or
6114// Class ref that name is returned and the ReferenceType is set accordingly.
6115//
6116// Lastly, literals which are Symbol address in a literal pool are looked for
6117// and if found the symbol name is returned and ReferenceType is set to
6118// LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr .
6119//
6120// If there is no item in the Mach-O file for the address passed in as
6121// ReferenceValue nullptr is returned and ReferenceType is unchanged.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00006122static const char *GuessLiteralPointer(uint64_t ReferenceValue,
6123 uint64_t ReferencePC,
6124 uint64_t *ReferenceType,
6125 struct DisassembleInfo *info) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00006126 // First see if there is an external relocation entry at the ReferencePC.
Kevin Enderbyd90a4172015-10-10 00:05:01 +00006127 if (info->O->getHeader().filetype == MachO::MH_OBJECT) {
6128 uint64_t sect_addr = info->S.getAddress();
6129 uint64_t sect_offset = ReferencePC - sect_addr;
6130 bool reloc_found = false;
6131 DataRefImpl Rel;
6132 MachO::any_relocation_info RE;
6133 bool isExtern = false;
6134 SymbolRef Symbol;
6135 for (const RelocationRef &Reloc : info->S.relocations()) {
6136 uint64_t RelocOffset = Reloc.getOffset();
6137 if (RelocOffset == sect_offset) {
6138 Rel = Reloc.getRawDataRefImpl();
6139 RE = info->O->getRelocation(Rel);
6140 if (info->O->isRelocationScattered(RE))
6141 continue;
6142 isExtern = info->O->getPlainRelocationExternal(RE);
6143 if (isExtern) {
6144 symbol_iterator RelocSym = Reloc.getSymbol();
6145 Symbol = *RelocSym;
6146 }
6147 reloc_found = true;
6148 break;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006149 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006150 }
Kevin Enderbyd90a4172015-10-10 00:05:01 +00006151 // If there is an external relocation entry for a symbol in a section
6152 // then used that symbol's value for the value of the reference.
6153 if (reloc_found && isExtern) {
6154 if (info->O->getAnyRelocationPCRel(RE)) {
6155 unsigned Type = info->O->getAnyRelocationType(RE);
6156 if (Type == MachO::X86_64_RELOC_SIGNED) {
6157 ReferenceValue = Symbol.getValue();
6158 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006159 }
6160 }
6161 }
6162
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006163 // Look for literals such as Objective-C CFStrings refs, Selector refs,
6164 // Message refs and Class refs.
6165 bool classref, selref, msgref, cfstring;
6166 uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref,
6167 selref, msgref, cfstring);
David Blaikie33dd45d02015-03-23 18:39:02 +00006168 if (classref && pointer_value == 0) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006169 // Note the ReferenceValue is a pointer into the __objc_classrefs section.
6170 // And the pointer_value in that section is typically zero as it will be
6171 // set by dyld as part of the "bind information".
6172 const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info);
6173 if (name != nullptr) {
6174 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
Hans Wennborgdb53e302014-10-23 21:59:17 +00006175 const char *class_name = strrchr(name, '$');
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006176 if (class_name != nullptr && class_name[1] == '_' &&
6177 class_name[2] != '\0') {
6178 info->class_name = class_name + 2;
6179 return name;
6180 }
6181 }
6182 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006183
David Blaikie33dd45d02015-03-23 18:39:02 +00006184 if (classref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006185 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
6186 const char *name =
6187 get_objc2_64bit_class_name(pointer_value, ReferenceValue, info);
6188 if (name != nullptr)
6189 info->class_name = name;
6190 else
6191 name = "bad class ref";
Kevin Enderbybf246f52014-09-24 23:08:22 +00006192 return name;
6193 }
6194
David Blaikie33dd45d02015-03-23 18:39:02 +00006195 if (cfstring) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006196 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref;
6197 const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info);
6198 return name;
6199 }
6200
David Blaikie33dd45d02015-03-23 18:39:02 +00006201 if (selref && pointer_value == 0)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006202 pointer_value = get_objc2_64bit_selref(ReferenceValue, info);
6203
6204 if (pointer_value != 0)
6205 ReferenceValue = pointer_value;
6206
6207 const char *name = GuessCstringPointer(ReferenceValue, info);
6208 if (name) {
David Blaikie33dd45d02015-03-23 18:39:02 +00006209 if (pointer_value != 0 && selref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006210 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref;
6211 info->selector_name = name;
David Blaikie33dd45d02015-03-23 18:39:02 +00006212 } else if (pointer_value != 0 && msgref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006213 info->class_name = nullptr;
6214 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref;
6215 info->selector_name = name;
6216 } else
6217 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr;
6218 return name;
6219 }
6220
6221 // Lastly look for an indirect symbol with this ReferenceValue which is in
6222 // a literal pool. If found return that symbol name.
6223 name = GuessIndirectSymbol(ReferenceValue, info);
6224 if (name) {
6225 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr;
6226 return name;
6227 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006228
6229 return nullptr;
6230}
6231
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006232// SymbolizerSymbolLookUp is the symbol lookup function passed when creating
Kevin Enderbybf246f52014-09-24 23:08:22 +00006233// the Symbolizer. It looks up the ReferenceValue using the info passed via the
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006234// pointer to the struct DisassembleInfo that was passed when MCSymbolizer
6235// is created and returns the symbol name that matches the ReferenceValue or
6236// nullptr if none. The ReferenceType is passed in for the IN type of
6237// reference the instruction is making from the values in defined in the header
6238// "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific
6239// Out type and the ReferenceName will also be set which is added as a comment
6240// to the disassembled instruction.
6241//
Kevin Enderby04bf6932014-10-28 23:39:46 +00006242#if HAVE_CXXABI_H
6243// If the symbol name is a C++ mangled name then the demangled name is
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006244// returned through ReferenceName and ReferenceType is set to
6245// LLVMDisassembler_ReferenceType_DeMangled_Name .
Kevin Enderby04bf6932014-10-28 23:39:46 +00006246#endif
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006247//
6248// When this is called to get a symbol name for a branch target then the
6249// ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
6250// SymbolValue will be looked for in the indirect symbol table to determine if
6251// it is an address for a symbol stub. If so then the symbol name for that
6252// stub is returned indirectly through ReferenceName and then ReferenceType is
6253// set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
6254//
Kevin Enderbybf246f52014-09-24 23:08:22 +00006255// When this is called with an value loaded via a PC relative load then
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006256// ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
6257// SymbolValue is checked to be an address of literal pointer, symbol pointer,
6258// or an Objective-C meta data reference. If so the output ReferenceType is
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006259// set to correspond to that as well as setting the ReferenceName.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00006260static const char *SymbolizerSymbolLookUp(void *DisInfo,
6261 uint64_t ReferenceValue,
6262 uint64_t *ReferenceType,
6263 uint64_t ReferencePC,
6264 const char **ReferenceName) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006265 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006266 // If no verbose symbolic information is wanted then just return nullptr.
David Blaikie33dd45d02015-03-23 18:39:02 +00006267 if (!info->verbose) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00006268 *ReferenceName = nullptr;
6269 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006270 return nullptr;
6271 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006272
Kevin Enderbyf6d25852015-01-31 00:37:11 +00006273 const char *SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
Kevin Enderbybf246f52014-09-24 23:08:22 +00006274
Kevin Enderby85974882014-09-26 22:20:44 +00006275 if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) {
6276 *ReferenceName = GuessIndirectSymbol(ReferenceValue, info);
Kevin Enderby04bf6932014-10-28 23:39:46 +00006277 if (*ReferenceName != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006278 method_reference(info, ReferenceType, ReferenceName);
6279 if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message)
6280 *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub;
6281 } else
Kevin Enderby04bf6932014-10-28 23:39:46 +00006282#if HAVE_CXXABI_H
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006283 if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
Kevin Enderby04bf6932014-10-28 23:39:46 +00006284 if (info->demangled_name != nullptr)
6285 free(info->demangled_name);
6286 int status;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006287 info->demangled_name =
6288 abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status);
Kevin Enderby04bf6932014-10-28 23:39:46 +00006289 if (info->demangled_name != nullptr) {
6290 *ReferenceName = info->demangled_name;
6291 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
6292 } else
6293 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6294 } else
6295#endif
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006296 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6297 } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) {
6298 *ReferenceName =
6299 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
Kevin Enderby85974882014-09-26 22:20:44 +00006300 if (*ReferenceName)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006301 method_reference(info, ReferenceType, ReferenceName);
Kevin Enderby85974882014-09-26 22:20:44 +00006302 else
6303 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006304 // If this is arm64 and the reference is an adrp instruction save the
6305 // instruction, passed in ReferenceValue and the address of the instruction
6306 // for use later if we see and add immediate instruction.
6307 } else if (info->O->getArch() == Triple::aarch64 &&
6308 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) {
6309 info->adrp_inst = ReferenceValue;
6310 info->adrp_addr = ReferencePC;
6311 SymbolName = nullptr;
6312 *ReferenceName = nullptr;
6313 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6314 // If this is arm64 and reference is an add immediate instruction and we
6315 // have
6316 // seen an adrp instruction just before it and the adrp's Xd register
6317 // matches
6318 // this add's Xn register reconstruct the value being referenced and look to
6319 // see if it is a literal pointer. Note the add immediate instruction is
6320 // passed in ReferenceValue.
6321 } else if (info->O->getArch() == Triple::aarch64 &&
6322 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri &&
6323 ReferencePC - 4 == info->adrp_addr &&
6324 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
6325 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
6326 uint32_t addxri_inst;
6327 uint64_t adrp_imm, addxri_imm;
6328
6329 adrp_imm =
6330 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
6331 if (info->adrp_inst & 0x0200000)
6332 adrp_imm |= 0xfffffffffc000000LL;
6333
6334 addxri_inst = ReferenceValue;
6335 addxri_imm = (addxri_inst >> 10) & 0xfff;
6336 if (((addxri_inst >> 22) & 0x3) == 1)
6337 addxri_imm <<= 12;
6338
6339 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
6340 (adrp_imm << 12) + addxri_imm;
6341
6342 *ReferenceName =
6343 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6344 if (*ReferenceName == nullptr)
6345 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6346 // If this is arm64 and the reference is a load register instruction and we
6347 // have seen an adrp instruction just before it and the adrp's Xd register
6348 // matches this add's Xn register reconstruct the value being referenced and
6349 // look to see if it is a literal pointer. Note the load register
6350 // instruction is passed in ReferenceValue.
6351 } else if (info->O->getArch() == Triple::aarch64 &&
6352 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui &&
6353 ReferencePC - 4 == info->adrp_addr &&
6354 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
6355 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
6356 uint32_t ldrxui_inst;
6357 uint64_t adrp_imm, ldrxui_imm;
6358
6359 adrp_imm =
6360 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
6361 if (info->adrp_inst & 0x0200000)
6362 adrp_imm |= 0xfffffffffc000000LL;
6363
6364 ldrxui_inst = ReferenceValue;
6365 ldrxui_imm = (ldrxui_inst >> 10) & 0xfff;
6366
6367 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
6368 (adrp_imm << 12) + (ldrxui_imm << 3);
6369
6370 *ReferenceName =
6371 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6372 if (*ReferenceName == nullptr)
6373 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6374 }
6375 // If this arm64 and is an load register (PC-relative) instruction the
6376 // ReferenceValue is the PC plus the immediate value.
6377 else if (info->O->getArch() == Triple::aarch64 &&
6378 (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl ||
6379 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR)) {
6380 *ReferenceName =
6381 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6382 if (*ReferenceName == nullptr)
6383 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Kevin Enderby85974882014-09-26 22:20:44 +00006384 }
Kevin Enderby04bf6932014-10-28 23:39:46 +00006385#if HAVE_CXXABI_H
6386 else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
6387 if (info->demangled_name != nullptr)
6388 free(info->demangled_name);
6389 int status;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006390 info->demangled_name =
6391 abi::__cxa_demangle(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 }
6397#endif
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006398 else {
Kevin Enderbybf246f52014-09-24 23:08:22 +00006399 *ReferenceName = nullptr;
6400 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6401 }
6402
6403 return SymbolName;
6404}
6405
Kevin Enderbybf246f52014-09-24 23:08:22 +00006406/// \brief Emits the comments that are stored in the CommentStream.
6407/// Each comment in the CommentStream must end with a newline.
6408static void emitComments(raw_svector_ostream &CommentStream,
6409 SmallString<128> &CommentsToEmit,
6410 formatted_raw_ostream &FormattedOS,
6411 const MCAsmInfo &MAI) {
6412 // Flush the stream before taking its content.
Kevin Enderbybf246f52014-09-24 23:08:22 +00006413 StringRef Comments = CommentsToEmit.str();
6414 // Get the default information for printing a comment.
6415 const char *CommentBegin = MAI.getCommentString();
6416 unsigned CommentColumn = MAI.getCommentColumn();
6417 bool IsFirst = true;
6418 while (!Comments.empty()) {
6419 if (!IsFirst)
6420 FormattedOS << '\n';
6421 // Emit a line of comments.
6422 FormattedOS.PadToColumn(CommentColumn);
6423 size_t Position = Comments.find('\n');
6424 FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
6425 // Move after the newline character.
6426 Comments = Comments.substr(Position + 1);
6427 IsFirst = false;
6428 }
6429 FormattedOS.flush();
6430
6431 // Tell the comment stream that the vector changed underneath it.
6432 CommentsToEmit.clear();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006433}
6434
Kevin Enderby95df54c2015-02-04 01:01:38 +00006435static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
6436 StringRef DisSegName, StringRef DisSectName) {
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006437 const char *McpuDefault = nullptr;
6438 const Target *ThumbTarget = nullptr;
6439 const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006440 if (!TheTarget) {
6441 // GetTarget prints out stuff.
6442 return;
6443 }
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006444 if (MCPU.empty() && McpuDefault)
6445 MCPU = McpuDefault;
6446
Ahmed Charles56440fd2014-03-06 05:51:42 +00006447 std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006448 std::unique_ptr<const MCInstrInfo> ThumbInstrInfo;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006449 if (ThumbTarget)
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006450 ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo());
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006451
Kevin Enderbyc9595622014-08-06 23:24:41 +00006452 // Package up features to be passed to target/subtarget
6453 std::string FeaturesStr;
6454 if (MAttrs.size()) {
6455 SubtargetFeatures Features;
6456 for (unsigned i = 0; i != MAttrs.size(); ++i)
6457 Features.AddFeature(MAttrs[i]);
6458 FeaturesStr = Features.getString();
6459 }
6460
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006461 // Set up disassembler.
Ahmed Charles56440fd2014-03-06 05:51:42 +00006462 std::unique_ptr<const MCRegisterInfo> MRI(
6463 TheTarget->createMCRegInfo(TripleName));
6464 std::unique_ptr<const MCAsmInfo> AsmInfo(
Rafael Espindola227144c2013-05-13 01:16:13 +00006465 TheTarget->createMCAsmInfo(*MRI, TripleName));
Ahmed Charles56440fd2014-03-06 05:51:42 +00006466 std::unique_ptr<const MCSubtargetInfo> STI(
Kevin Enderbyc9595622014-08-06 23:24:41 +00006467 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
Craig Toppere6cb63e2014-04-25 04:24:47 +00006468 MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr);
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006469 std::unique_ptr<MCDisassembler> DisAsm(
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006470 TheTarget->createMCDisassembler(*STI, Ctx));
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006471 std::unique_ptr<MCSymbolizer> Symbolizer;
6472 struct DisassembleInfo SymbolizerInfo;
6473 std::unique_ptr<MCRelocationInfo> RelInfo(
6474 TheTarget->createMCRelocationInfo(TripleName, Ctx));
6475 if (RelInfo) {
6476 Symbolizer.reset(TheTarget->createMCSymbolizer(
6477 TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
David Blaikie186db432015-01-18 20:45:48 +00006478 &SymbolizerInfo, &Ctx, std::move(RelInfo)));
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006479 DisAsm->setSymbolizer(std::move(Symbolizer));
6480 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006481 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
Ahmed Charles56440fd2014-03-06 05:51:42 +00006482 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
Daniel Sanders50f17232015-09-15 16:17:27 +00006483 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI));
Kevin Enderbybf246f52014-09-24 23:08:22 +00006484 // Set the display preference for hex vs. decimal immediates.
6485 IP->setPrintImmHex(PrintImmHex);
6486 // Comment stream and backing vector.
6487 SmallString<128> CommentsToEmit;
6488 raw_svector_ostream CommentStream(CommentsToEmit);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00006489 // FIXME: Setting the CommentStream in the InstPrinter is problematic in that
6490 // if it is done then arm64 comments for string literals don't get printed
6491 // and some constant get printed instead and not setting it causes intel
6492 // (32-bit and 64-bit) comments printed with different spacing before the
6493 // comment causing different diffs with the 'C' disassembler library API.
6494 // IP->setCommentStream(CommentStream);
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006495
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006496 if (!AsmInfo || !STI || !DisAsm || !IP) {
Michael J. Spencerc1363cf2011-10-07 19:25:47 +00006497 errs() << "error: couldn't initialize disassembler for target "
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006498 << TripleName << '\n';
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006499 return;
6500 }
6501
Tim Northover09ca33e2016-04-22 23:23:31 +00006502 // Set up separate thumb disassembler if needed.
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006503 std::unique_ptr<const MCRegisterInfo> ThumbMRI;
6504 std::unique_ptr<const MCAsmInfo> ThumbAsmInfo;
6505 std::unique_ptr<const MCSubtargetInfo> ThumbSTI;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006506 std::unique_ptr<MCDisassembler> ThumbDisAsm;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006507 std::unique_ptr<MCInstPrinter> ThumbIP;
6508 std::unique_ptr<MCContext> ThumbCtx;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006509 std::unique_ptr<MCSymbolizer> ThumbSymbolizer;
6510 struct DisassembleInfo ThumbSymbolizerInfo;
6511 std::unique_ptr<MCRelocationInfo> ThumbRelInfo;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006512 if (ThumbTarget) {
6513 ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName));
6514 ThumbAsmInfo.reset(
6515 ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName));
6516 ThumbSTI.reset(
6517 ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MCPU, FeaturesStr));
6518 ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr));
6519 ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx));
Kevin Enderby930fdc72014-11-06 19:00:13 +00006520 MCContext *PtrThumbCtx = ThumbCtx.get();
6521 ThumbRelInfo.reset(
6522 ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx));
6523 if (ThumbRelInfo) {
6524 ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer(
6525 ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
David Blaikie186db432015-01-18 20:45:48 +00006526 &ThumbSymbolizerInfo, PtrThumbCtx, std::move(ThumbRelInfo)));
Kevin Enderby930fdc72014-11-06 19:00:13 +00006527 ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer));
6528 }
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006529 int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect();
6530 ThumbIP.reset(ThumbTarget->createMCInstPrinter(
Daniel Sanders50f17232015-09-15 16:17:27 +00006531 Triple(ThumbTripleName), ThumbAsmPrinterVariant, *ThumbAsmInfo,
6532 *ThumbInstrInfo, *ThumbMRI));
Kevin Enderbybf246f52014-09-24 23:08:22 +00006533 // Set the display preference for hex vs. decimal immediates.
6534 ThumbIP->setPrintImmHex(PrintImmHex);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006535 }
6536
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006537 if (ThumbTarget && (!ThumbAsmInfo || !ThumbSTI || !ThumbDisAsm || !ThumbIP)) {
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006538 errs() << "error: couldn't initialize disassembler for target "
6539 << ThumbTripleName << '\n';
6540 return;
6541 }
6542
Charles Davis8bdfafd2013-09-01 04:28:48 +00006543 MachO::mach_header Header = MachOOF->getHeader();
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006544
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006545 // FIXME: Using the -cfg command line option, this code used to be able to
6546 // annotate relocations with the referenced symbol's name, and if this was
6547 // inside a __[cf]string section, the data it points to. This is now replaced
6548 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
Owen Andersond9243c42011-10-17 21:37:35 +00006549 std::vector<SectionRef> Sections;
6550 std::vector<SymbolRef> Symbols;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006551 SmallVector<uint64_t, 8> FoundFns;
Kevin Enderby273ae012013-06-06 17:20:50 +00006552 uint64_t BaseSegmentAddress;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006553
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00006554 getSectionsAndSymbols(MachOOF, Sections, Symbols, FoundFns,
Kevin Enderby273ae012013-06-06 17:20:50 +00006555 BaseSegmentAddress);
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006556
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006557 // Sort the symbols by address, just in case they didn't come in that way.
Owen Andersond9243c42011-10-17 21:37:35 +00006558 std::sort(Symbols.begin(), Symbols.end(), SymbolSorter());
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006559
Kevin Enderby273ae012013-06-06 17:20:50 +00006560 // Build a data in code table that is sorted on by the address of each entry.
6561 uint64_t BaseAddress = 0;
Charles Davis8bdfafd2013-09-01 04:28:48 +00006562 if (Header.filetype == MachO::MH_OBJECT)
Rafael Espindola80291272014-10-08 15:28:58 +00006563 BaseAddress = Sections[0].getAddress();
Kevin Enderby273ae012013-06-06 17:20:50 +00006564 else
6565 BaseAddress = BaseSegmentAddress;
6566 DiceTable Dices;
Kevin Enderby273ae012013-06-06 17:20:50 +00006567 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
Rafael Espindola5e812af2014-01-30 02:49:50 +00006568 DI != DE; ++DI) {
Kevin Enderby273ae012013-06-06 17:20:50 +00006569 uint32_t Offset;
6570 DI->getOffset(Offset);
6571 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
6572 }
6573 array_pod_sort(Dices.begin(), Dices.end());
6574
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006575#ifndef NDEBUG
6576 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
6577#else
6578 raw_ostream &DebugOut = nulls();
6579#endif
6580
Ahmed Charles56440fd2014-03-06 05:51:42 +00006581 std::unique_ptr<DIContext> diContext;
Rafael Espindola9b709252013-04-13 01:45:40 +00006582 ObjectFile *DbgObj = MachOOF;
Benjamin Kramer699128e2011-09-21 01:13:19 +00006583 // Try to find debug info and set up the DIContext for it.
6584 if (UseDbg) {
Benjamin Kramer699128e2011-09-21 01:13:19 +00006585 // A separate DSym file path was specified, parse it as a macho file,
6586 // get the sections and supply it to the section name parsing machinery.
6587 if (!DSYMFile.empty()) {
Rafael Espindola48af1c22014-08-19 18:44:46 +00006588 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
Rafael Espindolaadf21f22014-07-06 17:43:13 +00006589 MemoryBuffer::getFileOrSTDIN(DSYMFile);
Rafael Espindola48af1c22014-08-19 18:44:46 +00006590 if (std::error_code EC = BufOrErr.getError()) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +00006591 errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n';
Benjamin Kramer699128e2011-09-21 01:13:19 +00006592 return;
6593 }
Rafael Espindola48af1c22014-08-19 18:44:46 +00006594 DbgObj =
6595 ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef())
6596 .get()
6597 .release();
Benjamin Kramer699128e2011-09-21 01:13:19 +00006598 }
6599
Eric Christopher7370b552012-11-12 21:40:38 +00006600 // Setup the DIContext
Zachary Turner6489d7b2015-04-23 17:37:47 +00006601 diContext.reset(new DWARFContextInMemory(*DbgObj));
Benjamin Kramer699128e2011-09-21 01:13:19 +00006602 }
6603
Colin LeMahieufcc32762015-07-29 19:08:10 +00006604 if (FilterSections.size() == 0)
Kevin Enderby95df54c2015-02-04 01:01:38 +00006605 outs() << "(" << DisSegName << "," << DisSectName << ") section\n";
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00006606
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006607 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
Owen Andersond9243c42011-10-17 21:37:35 +00006608 StringRef SectName;
Kevin Enderby95df54c2015-02-04 01:01:38 +00006609 if (Sections[SectIdx].getName(SectName) || SectName != DisSectName)
6610 continue;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006611
Rafael Espindolaa9f810b2012-12-21 03:47:03 +00006612 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006613
Rafael Espindolab0f76a42013-04-05 15:15:22 +00006614 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
Kevin Enderby95df54c2015-02-04 01:01:38 +00006615 if (SegmentName != DisSegName)
Rafael Espindolaa9f810b2012-12-21 03:47:03 +00006616 continue;
6617
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006618 StringRef BytesStr;
6619 Sections[SectIdx].getContents(BytesStr);
Aaron Ballman106fd7b2014-11-12 14:01:17 +00006620 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
6621 BytesStr.size());
Rafael Espindola80291272014-10-08 15:28:58 +00006622 uint64_t SectAddress = Sections[SectIdx].getAddress();
Rafael Espindolabd604f22014-11-07 00:52:15 +00006623
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006624 bool symbolTableWorked = false;
6625
Kevin Enderbybf246f52014-09-24 23:08:22 +00006626 // Create a map of symbol addresses to symbol names for use by
6627 // the SymbolizerSymbolLookUp() routine.
6628 SymbolAddressMap AddrMap;
Kevin Enderby6a221752015-03-17 17:10:57 +00006629 bool DisSymNameFound = false;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006630 for (const SymbolRef &Symbol : MachOOF->symbols()) {
Kevin Enderby7bd8d992016-05-02 20:28:12 +00006631 Expected<SymbolRef::Type> STOrErr = Symbol.getType();
6632 if (!STOrErr) {
6633 std::string Buf;
6634 raw_string_ostream OS(Buf);
6635 logAllUnhandledErrors(STOrErr.takeError(), OS, "");
6636 OS.flush();
6637 report_fatal_error(Buf);
6638 }
Kevin Enderby5afbc1c2016-03-23 20:27:00 +00006639 SymbolRef::Type ST = *STOrErr;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006640 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
6641 ST == SymbolRef::ST_Other) {
Rafael Espindoladea00162015-07-03 17:44:18 +00006642 uint64_t Address = Symbol.getValue();
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00006643 Expected<StringRef> SymNameOrErr = Symbol.getName();
6644 if (!SymNameOrErr) {
6645 std::string Buf;
6646 raw_string_ostream OS(Buf);
6647 logAllUnhandledErrors(SymNameOrErr.takeError(), OS, "");
6648 OS.flush();
6649 report_fatal_error(Buf);
6650 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00006651 StringRef SymName = *SymNameOrErr;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006652 AddrMap[Address] = SymName;
Kevin Enderby6a221752015-03-17 17:10:57 +00006653 if (!DisSymName.empty() && DisSymName == SymName)
6654 DisSymNameFound = true;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006655 }
6656 }
David Blaikie33dd45d02015-03-23 18:39:02 +00006657 if (!DisSymName.empty() && !DisSymNameFound) {
Kevin Enderby6a221752015-03-17 17:10:57 +00006658 outs() << "Can't find -dis-symname: " << DisSymName << "\n";
6659 return;
6660 }
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006661 // Set up the block of info used by the Symbolizer call backs.
Kevin Enderby8e29ec92015-03-17 22:26:11 +00006662 SymbolizerInfo.verbose = !NoSymbolicOperands;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006663 SymbolizerInfo.O = MachOOF;
6664 SymbolizerInfo.S = Sections[SectIdx];
Kevin Enderbybf246f52014-09-24 23:08:22 +00006665 SymbolizerInfo.AddrMap = &AddrMap;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006666 SymbolizerInfo.Sections = &Sections;
6667 SymbolizerInfo.class_name = nullptr;
6668 SymbolizerInfo.selector_name = nullptr;
6669 SymbolizerInfo.method = nullptr;
Kevin Enderby04bf6932014-10-28 23:39:46 +00006670 SymbolizerInfo.demangled_name = nullptr;
Kevin Enderby078be602014-10-23 19:53:12 +00006671 SymbolizerInfo.bindtable = nullptr;
Kevin Enderby10738222014-11-19 20:20:16 +00006672 SymbolizerInfo.adrp_addr = 0;
6673 SymbolizerInfo.adrp_inst = 0;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006674 // Same for the ThumbSymbolizer
Kevin Enderby8e29ec92015-03-17 22:26:11 +00006675 ThumbSymbolizerInfo.verbose = !NoSymbolicOperands;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006676 ThumbSymbolizerInfo.O = MachOOF;
6677 ThumbSymbolizerInfo.S = Sections[SectIdx];
6678 ThumbSymbolizerInfo.AddrMap = &AddrMap;
6679 ThumbSymbolizerInfo.Sections = &Sections;
6680 ThumbSymbolizerInfo.class_name = nullptr;
6681 ThumbSymbolizerInfo.selector_name = nullptr;
6682 ThumbSymbolizerInfo.method = nullptr;
6683 ThumbSymbolizerInfo.demangled_name = nullptr;
6684 ThumbSymbolizerInfo.bindtable = nullptr;
Kevin Enderby10738222014-11-19 20:20:16 +00006685 ThumbSymbolizerInfo.adrp_addr = 0;
6686 ThumbSymbolizerInfo.adrp_inst = 0;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006687
Kevin Enderby4b627be2016-04-28 20:14:13 +00006688 unsigned int Arch = MachOOF->getArch();
6689
Tim Northoverf203ab52016-07-14 22:13:32 +00006690 // Skip all symbols if this is a stubs file.
6691 if (Bytes.size() == 0)
6692 return;
6693
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006694 // Disassemble symbol by symbol.
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006695 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00006696 Expected<StringRef> SymNameOrErr = Symbols[SymIdx].getName();
6697 if (!SymNameOrErr) {
6698 std::string Buf;
6699 raw_string_ostream OS(Buf);
6700 logAllUnhandledErrors(SymNameOrErr.takeError(), OS, "");
6701 OS.flush();
6702 report_fatal_error(Buf);
6703 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00006704 StringRef SymName = *SymNameOrErr;
Owen Andersond9243c42011-10-17 21:37:35 +00006705
Kevin Enderby7bd8d992016-05-02 20:28:12 +00006706 Expected<SymbolRef::Type> STOrErr = Symbols[SymIdx].getType();
6707 if (!STOrErr) {
6708 std::string Buf;
6709 raw_string_ostream OS(Buf);
6710 logAllUnhandledErrors(STOrErr.takeError(), OS, "");
6711 OS.flush();
6712 report_fatal_error(Buf);
6713 }
Kevin Enderby5afbc1c2016-03-23 20:27:00 +00006714 SymbolRef::Type ST = *STOrErr;
Kuba Breckade833222015-11-12 09:40:29 +00006715 if (ST != SymbolRef::ST_Function && ST != SymbolRef::ST_Data)
Owen Andersond9243c42011-10-17 21:37:35 +00006716 continue;
6717
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006718 // Make sure the symbol is defined in this section.
Rafael Espindola80291272014-10-08 15:28:58 +00006719 bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]);
Kevin Enderbyd8a6e832016-06-15 21:14:01 +00006720 if (!containsSym) {
6721 if (!DisSymName.empty() && DisSymName == SymName) {
6722 outs() << "-dis-symname: " << DisSymName << " not in the section\n";
6723 return;
6724 }
6725 continue;
6726 }
6727 // The __mh_execute_header is special and we need to deal with that fact
6728 // this symbol is before the start of the (__TEXT,__text) section and at the
6729 // address of the start of the __TEXT segment. This is because this symbol
6730 // is an N_SECT symbol in the (__TEXT,__text) but its address is before the
6731 // start of the section in a standard MH_EXECUTE filetype.
6732 if (!DisSymName.empty() && DisSymName == "__mh_execute_header") {
6733 outs() << "-dis-symname: __mh_execute_header not in any section\n";
6734 return;
6735 }
Tim Northoverfbefee32016-07-14 23:13:03 +00006736 // When this code is trying to disassemble a symbol at a time and in the
6737 // case there is only the __mh_execute_header symbol left as in a stripped
6738 // executable, we need to deal with this by ignoring this symbol so the
6739 // whole section is disassembled and this symbol is then not displayed.
6740 if (SymName == "__mh_execute_header" || SymName == "__mh_dylib_header" ||
6741 SymName == "__mh_bundle_header" || SymName == "__mh_object_header" ||
6742 SymName == "__mh_preload_header" || SymName == "__mh_dylinker_header")
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006743 continue;
6744
Kevin Enderby6a221752015-03-17 17:10:57 +00006745 // If we are only disassembling one symbol see if this is that symbol.
6746 if (!DisSymName.empty() && DisSymName != SymName)
6747 continue;
6748
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006749 // Start at the address of the symbol relative to the section's address.
Tim Northoverf203ab52016-07-14 22:13:32 +00006750 uint64_t SectSize = Sections[SectIdx].getSize();
Rafael Espindoladea00162015-07-03 17:44:18 +00006751 uint64_t Start = Symbols[SymIdx].getValue();
Rafael Espindola80291272014-10-08 15:28:58 +00006752 uint64_t SectionAddress = Sections[SectIdx].getAddress();
Cameron Zwarich54478a52012-02-03 05:42:17 +00006753 Start -= SectionAddress;
Owen Andersond9243c42011-10-17 21:37:35 +00006754
Tim Northoverf203ab52016-07-14 22:13:32 +00006755 if (Start > SectSize) {
6756 outs() << "section data ends, " << SymName
6757 << " lies outside valid range\n";
6758 return;
6759 }
6760
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006761 // Stop disassembling either at the beginning of the next symbol or at
6762 // the end of the section.
Kevin Enderbyedd58722012-05-15 18:57:14 +00006763 bool containsNextSym = false;
Owen Andersond9243c42011-10-17 21:37:35 +00006764 uint64_t NextSym = 0;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006765 uint64_t NextSymIdx = SymIdx + 1;
Owen Andersond9243c42011-10-17 21:37:35 +00006766 while (Symbols.size() > NextSymIdx) {
Kevin Enderby7bd8d992016-05-02 20:28:12 +00006767 Expected<SymbolRef::Type> STOrErr = Symbols[NextSymIdx].getType();
6768 if (!STOrErr) {
6769 std::string Buf;
6770 raw_string_ostream OS(Buf);
6771 logAllUnhandledErrors(STOrErr.takeError(), OS, "");
6772 OS.flush();
6773 report_fatal_error(Buf);
6774 }
Kevin Enderby5afbc1c2016-03-23 20:27:00 +00006775 SymbolRef::Type NextSymType = *STOrErr;
Owen Andersond9243c42011-10-17 21:37:35 +00006776 if (NextSymType == SymbolRef::ST_Function) {
Rafael Espindola80291272014-10-08 15:28:58 +00006777 containsNextSym =
6778 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
Rafael Espindoladea00162015-07-03 17:44:18 +00006779 NextSym = Symbols[NextSymIdx].getValue();
Cameron Zwarich54478a52012-02-03 05:42:17 +00006780 NextSym -= SectionAddress;
Owen Andersond9243c42011-10-17 21:37:35 +00006781 break;
6782 }
6783 ++NextSymIdx;
6784 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006785
Tim Northoverf203ab52016-07-14 22:13:32 +00006786 uint64_t End = containsNextSym ? std::min(NextSym, SectSize) : SectSize;
Owen Andersond9243c42011-10-17 21:37:35 +00006787 uint64_t Size;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006788
6789 symbolTableWorked = true;
Rafael Espindolabd604f22014-11-07 00:52:15 +00006790
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006791 DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
Tim Northover09ca33e2016-04-22 23:23:31 +00006792 bool IsThumb = MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb;
6793
6794 // We only need the dedicated Thumb target if there's a real choice
6795 // (i.e. we're not targeting M-class) and the function is Thumb.
6796 bool UseThumbTarget = IsThumb && ThumbTarget;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006797
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006798 outs() << SymName << ":\n";
6799 DILineInfo lastLine;
6800 for (uint64_t Index = Start; Index < End; Index += Size) {
6801 MCInst Inst;
Owen Andersond9243c42011-10-17 21:37:35 +00006802
Kevin Enderbybf246f52014-09-24 23:08:22 +00006803 uint64_t PC = SectAddress + Index;
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00006804 if (!NoLeadingAddr) {
6805 if (FullLeadingAddr) {
6806 if (MachOOF->is64Bit())
6807 outs() << format("%016" PRIx64, PC);
6808 else
6809 outs() << format("%08" PRIx64, PC);
6810 } else {
6811 outs() << format("%8" PRIx64 ":", PC);
6812 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006813 }
Kevin Enderby4b627be2016-04-28 20:14:13 +00006814 if (!NoShowRawInsn || Arch == Triple::arm)
Kevin Enderbybf246f52014-09-24 23:08:22 +00006815 outs() << "\t";
Kevin Enderby273ae012013-06-06 17:20:50 +00006816
6817 // Check the data in code table here to see if this is data not an
6818 // instruction to be disassembled.
6819 DiceTable Dice;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006820 Dice.push_back(std::make_pair(PC, DiceRef()));
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006821 dice_table_iterator DTI =
6822 std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(),
6823 compareDiceTableEntries);
6824 if (DTI != Dices.end()) {
Kevin Enderby273ae012013-06-06 17:20:50 +00006825 uint16_t Length;
6826 DTI->second.getLength(Length);
Kevin Enderby273ae012013-06-06 17:20:50 +00006827 uint16_t Kind;
6828 DTI->second.getKind(Kind);
Colin LeMahieufc32b1b2015-03-18 19:27:31 +00006829 Size = DumpDataInCode(Bytes.data() + Index, Length, Kind);
Kevin Enderby930fdc72014-11-06 19:00:13 +00006830 if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) &&
6831 (PC == (DTI->first + Length - 1)) && (Length & 1))
6832 Size++;
Kevin Enderby273ae012013-06-06 17:20:50 +00006833 continue;
6834 }
6835
Kevin Enderbybf246f52014-09-24 23:08:22 +00006836 SmallVector<char, 64> AnnotationsBytes;
6837 raw_svector_ostream Annotations(AnnotationsBytes);
6838
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006839 bool gotInst;
Tim Northover09ca33e2016-04-22 23:23:31 +00006840 if (UseThumbTarget)
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006841 gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006842 PC, DebugOut, Annotations);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006843 else
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006844 gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC,
Kevin Enderbybf246f52014-09-24 23:08:22 +00006845 DebugOut, Annotations);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006846 if (gotInst) {
Kevin Enderby4b627be2016-04-28 20:14:13 +00006847 if (!NoShowRawInsn || Arch == Triple::arm) {
Craig Topper0013be12015-09-21 05:32:41 +00006848 dumpBytes(makeArrayRef(Bytes.data() + Index, Size), outs());
Kevin Enderbybf246f52014-09-24 23:08:22 +00006849 }
6850 formatted_raw_ostream FormattedOS(outs());
Kevin Enderbybf246f52014-09-24 23:08:22 +00006851 StringRef AnnotationsStr = Annotations.str();
Tim Northover09ca33e2016-04-22 23:23:31 +00006852 if (UseThumbTarget)
Akira Hatanakab46d0232015-03-27 20:36:02 +00006853 ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr, *ThumbSTI);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006854 else
Akira Hatanaka1d079942015-03-28 20:44:05 +00006855 IP->printInst(&Inst, FormattedOS, AnnotationsStr, *STI);
Kevin Enderbybf246f52014-09-24 23:08:22 +00006856 emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo);
Owen Andersond9243c42011-10-17 21:37:35 +00006857
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006858 // Print debug info.
6859 if (diContext) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006860 DILineInfo dli = diContext->getLineInfoForAddress(PC);
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006861 // Print valid line info if it changed.
Alexey Samsonovd0109992014-04-18 21:36:39 +00006862 if (dli != lastLine && dli.Line != 0)
6863 outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
6864 << dli.Column;
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006865 lastLine = dli;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006866 }
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006867 outs() << "\n";
6868 } else {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006869 unsigned int Arch = MachOOF->getArch();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006870 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006871 outs() << format("\t.byte 0x%02x #bad opcode\n",
6872 *(Bytes.data() + Index) & 0xff);
6873 Size = 1; // skip exactly one illegible byte and move on.
Tim Northover09ca33e2016-04-22 23:23:31 +00006874 } else if (Arch == Triple::aarch64 ||
6875 (Arch == Triple::arm && !IsThumb)) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006876 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
6877 (*(Bytes.data() + Index + 1) & 0xff) << 8 |
6878 (*(Bytes.data() + Index + 2) & 0xff) << 16 |
6879 (*(Bytes.data() + Index + 3) & 0xff) << 24;
6880 outs() << format("\t.long\t0x%08x\n", opcode);
6881 Size = 4;
Tim Northover09ca33e2016-04-22 23:23:31 +00006882 } else if (Arch == Triple::arm) {
6883 assert(IsThumb && "ARM mode should have been dealt with above");
6884 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
6885 (*(Bytes.data() + Index + 1) & 0xff) << 8;
6886 outs() << format("\t.short\t0x%04x\n", opcode);
6887 Size = 2;
6888 } else{
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006889 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6890 if (Size == 0)
6891 Size = 1; // skip illegible bytes
6892 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006893 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006894 }
6895 }
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006896 if (!symbolTableWorked) {
Rafael Espindola80291272014-10-08 15:28:58 +00006897 // Reading the symbol table didn't work, disassemble the whole section.
6898 uint64_t SectAddress = Sections[SectIdx].getAddress();
6899 uint64_t SectSize = Sections[SectIdx].getSize();
Kevin Enderbybadd1002012-05-18 00:13:56 +00006900 uint64_t InstSize;
6901 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
Bill Wendling4e68e062012-07-19 00:17:40 +00006902 MCInst Inst;
Kevin Enderbybadd1002012-05-18 00:13:56 +00006903
Kevin Enderbybf246f52014-09-24 23:08:22 +00006904 uint64_t PC = SectAddress + Index;
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006905 if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC,
6906 DebugOut, nulls())) {
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00006907 if (!NoLeadingAddr) {
6908 if (FullLeadingAddr) {
6909 if (MachOOF->is64Bit())
6910 outs() << format("%016" PRIx64, PC);
6911 else
6912 outs() << format("%08" PRIx64, PC);
6913 } else {
6914 outs() << format("%8" PRIx64 ":", PC);
6915 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006916 }
Kevin Enderby4b627be2016-04-28 20:14:13 +00006917 if (!NoShowRawInsn || Arch == Triple::arm) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00006918 outs() << "\t";
Craig Topper0013be12015-09-21 05:32:41 +00006919 dumpBytes(makeArrayRef(Bytes.data() + Index, InstSize), outs());
Kevin Enderbybf246f52014-09-24 23:08:22 +00006920 }
Akira Hatanaka1d079942015-03-28 20:44:05 +00006921 IP->printInst(&Inst, outs(), "", *STI);
Bill Wendling4e68e062012-07-19 00:17:40 +00006922 outs() << "\n";
6923 } else {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006924 unsigned int Arch = MachOOF->getArch();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006925 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006926 outs() << format("\t.byte 0x%02x #bad opcode\n",
6927 *(Bytes.data() + Index) & 0xff);
6928 InstSize = 1; // skip exactly one illegible byte and move on.
6929 } else {
6930 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6931 if (InstSize == 0)
6932 InstSize = 1; // skip illegible bytes
6933 }
Bill Wendling4e68e062012-07-19 00:17:40 +00006934 }
Kevin Enderbybadd1002012-05-18 00:13:56 +00006935 }
6936 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00006937 // The TripleName's need to be reset if we are called again for a different
6938 // archtecture.
6939 TripleName = "";
6940 ThumbTripleName = "";
6941
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006942 if (SymbolizerInfo.method != nullptr)
6943 free(SymbolizerInfo.method);
Kevin Enderby04bf6932014-10-28 23:39:46 +00006944 if (SymbolizerInfo.demangled_name != nullptr)
6945 free(SymbolizerInfo.demangled_name);
Kevin Enderby078be602014-10-23 19:53:12 +00006946 if (SymbolizerInfo.bindtable != nullptr)
6947 delete SymbolizerInfo.bindtable;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006948 if (ThumbSymbolizerInfo.method != nullptr)
6949 free(ThumbSymbolizerInfo.method);
6950 if (ThumbSymbolizerInfo.demangled_name != nullptr)
6951 free(ThumbSymbolizerInfo.demangled_name);
6952 if (ThumbSymbolizerInfo.bindtable != nullptr)
6953 delete ThumbSymbolizerInfo.bindtable;
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();
7020 if (!NameOrErr) {
7021 std::string Buf;
7022 raw_string_ostream OS(Buf);
7023 logAllUnhandledErrors(NameOrErr.takeError(), OS, "");
7024 OS.flush();
7025 report_fatal_error(Buf);
7026 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00007027 Name = *NameOrErr;
Tim Northover4bd286a2014-08-01 13:07:19 +00007028 Addend = Addr;
7029 return;
7030 }
7031
7032 auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
Keno Fischerc780e8e2015-05-21 21:24:32 +00007033 SectionRef RelocSection = Obj->getAnyRelocationSection(RE);
Tim Northover4bd286a2014-08-01 13:07:19 +00007034
Rafael Espindola80291272014-10-08 15:28:58 +00007035 uint64_t SectionAddr = RelocSection.getAddress();
Tim Northover4bd286a2014-08-01 13:07:19 +00007036
7037 auto Sym = Symbols.upper_bound(Addr);
7038 if (Sym == Symbols.begin()) {
7039 // The first symbol in the object is after this reference, the best we can
7040 // do is section-relative notation.
7041 RelocSection.getName(Name);
7042 Addend = Addr - SectionAddr;
7043 return;
7044 }
7045
7046 // Go back one so that SymbolAddress <= Addr.
7047 --Sym;
7048
Kevin Enderby7bd8d992016-05-02 20:28:12 +00007049 auto SectOrErr = Sym->second.getSection();
7050 if (!SectOrErr) {
7051 std::string Buf;
7052 raw_string_ostream OS(Buf);
7053 logAllUnhandledErrors(SectOrErr.takeError(), OS, "");
7054 OS.flush();
7055 report_fatal_error(Buf);
7056 }
7057 section_iterator SymSection = *SectOrErr;
Tim Northover4bd286a2014-08-01 13:07:19 +00007058 if (RelocSection == *SymSection) {
7059 // There's a valid symbol in the same section before this reference.
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00007060 Expected<StringRef> NameOrErr = Sym->second.getName();
7061 if (!NameOrErr) {
7062 std::string Buf;
7063 raw_string_ostream OS(Buf);
7064 logAllUnhandledErrors(NameOrErr.takeError(), OS, "");
7065 OS.flush();
7066 report_fatal_error(Buf);
7067 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00007068 Name = *NameOrErr;
Tim Northover4bd286a2014-08-01 13:07:19 +00007069 Addend = Addr - Sym->first;
7070 return;
7071 }
7072
7073 // There is a symbol before this reference, but it's in a different
7074 // section. Probably not helpful to mention it, so use the section name.
7075 RelocSection.getName(Name);
7076 Addend = Addr - SectionAddr;
7077}
7078
7079static void printUnwindRelocDest(const MachOObjectFile *Obj,
7080 std::map<uint64_t, SymbolRef> &Symbols,
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007081 const RelocationRef &Reloc, uint64_t Addr) {
Tim Northover4bd286a2014-08-01 13:07:19 +00007082 StringRef Name;
7083 uint64_t Addend;
7084
Rafael Espindola854038e2015-06-26 14:51:16 +00007085 if (!Reloc.getObject())
Tim Northover0b0add52014-09-09 10:45:06 +00007086 return;
7087
Tim Northover4bd286a2014-08-01 13:07:19 +00007088 findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend);
7089
7090 outs() << Name;
7091 if (Addend)
Tim Northover63a25622014-08-11 09:14:06 +00007092 outs() << " + " << format("0x%" PRIx64, Addend);
Tim Northover4bd286a2014-08-01 13:07:19 +00007093}
7094
7095static void
7096printMachOCompactUnwindSection(const MachOObjectFile *Obj,
7097 std::map<uint64_t, SymbolRef> &Symbols,
7098 const SectionRef &CompactUnwind) {
7099
7100 assert(Obj->isLittleEndian() &&
7101 "There should not be a big-endian .o with __compact_unwind");
7102
7103 bool Is64 = Obj->is64Bit();
7104 uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
7105 uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
7106
7107 StringRef Contents;
7108 CompactUnwind.getContents(Contents);
7109
7110 SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
7111
7112 // First populate the initial raw offsets, encodings and so on from the entry.
7113 for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) {
7114 CompactUnwindEntry Entry(Contents.data(), Offset, Is64);
7115 CompactUnwinds.push_back(Entry);
7116 }
7117
7118 // Next we need to look at the relocations to find out what objects are
7119 // actually being referred to.
7120 for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00007121 uint64_t RelocAddress = Reloc.getOffset();
Tim Northover4bd286a2014-08-01 13:07:19 +00007122
7123 uint32_t EntryIdx = RelocAddress / EntrySize;
7124 uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
7125 CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx];
7126
7127 if (OffsetInEntry == 0)
7128 Entry.FunctionReloc = Reloc;
7129 else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t))
7130 Entry.PersonalityReloc = Reloc;
7131 else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t))
7132 Entry.LSDAReloc = Reloc;
7133 else
7134 llvm_unreachable("Unexpected relocation in __compact_unwind section");
7135 }
7136
7137 // Finally, we're ready to print the data we've gathered.
7138 outs() << "Contents of __compact_unwind section:\n";
7139 for (auto &Entry : CompactUnwinds) {
Tim Northover06af2602014-08-08 12:08:51 +00007140 outs() << " Entry at offset "
7141 << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n";
Tim Northover4bd286a2014-08-01 13:07:19 +00007142
7143 // 1. Start of the region this entry applies to.
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007144 outs() << " start: " << format("0x%" PRIx64,
7145 Entry.FunctionAddr) << ' ';
7146 printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr);
Tim Northover4bd286a2014-08-01 13:07:19 +00007147 outs() << '\n';
7148
7149 // 2. Length of the region this entry applies to.
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007150 outs() << " length: " << format("0x%" PRIx32, Entry.Length)
7151 << '\n';
Tim Northover4bd286a2014-08-01 13:07:19 +00007152 // 3. The 32-bit compact encoding.
7153 outs() << " compact encoding: "
Tim Northoverb911bf82014-08-08 12:00:09 +00007154 << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n';
Tim Northover4bd286a2014-08-01 13:07:19 +00007155
7156 // 4. The personality function, if present.
Rafael Espindola854038e2015-06-26 14:51:16 +00007157 if (Entry.PersonalityReloc.getObject()) {
Tim Northover4bd286a2014-08-01 13:07:19 +00007158 outs() << " personality function: "
Tim Northoverb911bf82014-08-08 12:00:09 +00007159 << format("0x%" PRIx64, Entry.PersonalityAddr) << ' ';
Tim Northover4bd286a2014-08-01 13:07:19 +00007160 printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc,
7161 Entry.PersonalityAddr);
7162 outs() << '\n';
7163 }
7164
7165 // 5. This entry's language-specific data area.
Rafael Espindola854038e2015-06-26 14:51:16 +00007166 if (Entry.LSDAReloc.getObject()) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007167 outs() << " LSDA: " << format("0x%" PRIx64,
7168 Entry.LSDAAddr) << ' ';
Tim Northover4bd286a2014-08-01 13:07:19 +00007169 printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr);
7170 outs() << '\n';
7171 }
7172 }
7173}
7174
Tim Northover39c70bb2014-08-12 11:52:59 +00007175//===----------------------------------------------------------------------===//
7176// __unwind_info section dumping
7177//===----------------------------------------------------------------------===//
7178
7179static void printRegularSecondLevelUnwindPage(const char *PageStart) {
7180 const char *Pos = PageStart;
7181 uint32_t Kind = readNext<uint32_t>(Pos);
7182 (void)Kind;
7183 assert(Kind == 2 && "kind for a regular 2nd level index should be 2");
7184
7185 uint16_t EntriesStart = readNext<uint16_t>(Pos);
7186 uint16_t NumEntries = readNext<uint16_t>(Pos);
7187
7188 Pos = PageStart + EntriesStart;
7189 for (unsigned i = 0; i < NumEntries; ++i) {
7190 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
7191 uint32_t Encoding = readNext<uint32_t>(Pos);
7192
7193 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007194 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
7195 << ", "
7196 << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00007197 }
7198}
7199
7200static void printCompressedSecondLevelUnwindPage(
7201 const char *PageStart, uint32_t FunctionBase,
7202 const SmallVectorImpl<uint32_t> &CommonEncodings) {
7203 const char *Pos = PageStart;
7204 uint32_t Kind = readNext<uint32_t>(Pos);
7205 (void)Kind;
7206 assert(Kind == 3 && "kind for a compressed 2nd level index should be 3");
7207
7208 uint16_t EntriesStart = readNext<uint16_t>(Pos);
7209 uint16_t NumEntries = readNext<uint16_t>(Pos);
7210
7211 uint16_t EncodingsStart = readNext<uint16_t>(Pos);
7212 readNext<uint16_t>(Pos);
Aaron Ballman80930af2014-08-14 13:53:19 +00007213 const auto *PageEncodings = reinterpret_cast<const support::ulittle32_t *>(
7214 PageStart + EncodingsStart);
Tim Northover39c70bb2014-08-12 11:52:59 +00007215
7216 Pos = PageStart + EntriesStart;
7217 for (unsigned i = 0; i < NumEntries; ++i) {
7218 uint32_t Entry = readNext<uint32_t>(Pos);
7219 uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff);
7220 uint32_t EncodingIdx = Entry >> 24;
7221
7222 uint32_t Encoding;
7223 if (EncodingIdx < CommonEncodings.size())
7224 Encoding = CommonEncodings[EncodingIdx];
7225 else
7226 Encoding = PageEncodings[EncodingIdx - CommonEncodings.size()];
7227
7228 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007229 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
7230 << ", "
7231 << "encoding[" << EncodingIdx
7232 << "]=" << format("0x%08" PRIx32, Encoding) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00007233 }
7234}
7235
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007236static void printMachOUnwindInfoSection(const MachOObjectFile *Obj,
7237 std::map<uint64_t, SymbolRef> &Symbols,
7238 const SectionRef &UnwindInfo) {
Tim Northover39c70bb2014-08-12 11:52:59 +00007239
7240 assert(Obj->isLittleEndian() &&
7241 "There should not be a big-endian .o with __unwind_info");
7242
7243 outs() << "Contents of __unwind_info section:\n";
7244
7245 StringRef Contents;
7246 UnwindInfo.getContents(Contents);
7247 const char *Pos = Contents.data();
7248
7249 //===----------------------------------
7250 // Section header
7251 //===----------------------------------
7252
7253 uint32_t Version = readNext<uint32_t>(Pos);
7254 outs() << " Version: "
7255 << format("0x%" PRIx32, Version) << '\n';
7256 assert(Version == 1 && "only understand version 1");
7257
7258 uint32_t CommonEncodingsStart = readNext<uint32_t>(Pos);
7259 outs() << " Common encodings array section offset: "
7260 << format("0x%" PRIx32, CommonEncodingsStart) << '\n';
7261 uint32_t NumCommonEncodings = readNext<uint32_t>(Pos);
7262 outs() << " Number of common encodings in array: "
7263 << format("0x%" PRIx32, NumCommonEncodings) << '\n';
7264
7265 uint32_t PersonalitiesStart = readNext<uint32_t>(Pos);
7266 outs() << " Personality function array section offset: "
7267 << format("0x%" PRIx32, PersonalitiesStart) << '\n';
7268 uint32_t NumPersonalities = readNext<uint32_t>(Pos);
7269 outs() << " Number of personality functions in array: "
7270 << format("0x%" PRIx32, NumPersonalities) << '\n';
7271
7272 uint32_t IndicesStart = readNext<uint32_t>(Pos);
7273 outs() << " Index array section offset: "
7274 << format("0x%" PRIx32, IndicesStart) << '\n';
7275 uint32_t NumIndices = readNext<uint32_t>(Pos);
7276 outs() << " Number of indices in array: "
7277 << format("0x%" PRIx32, NumIndices) << '\n';
7278
7279 //===----------------------------------
7280 // A shared list of common encodings
7281 //===----------------------------------
7282
7283 // These occupy indices in the range [0, N] whenever an encoding is referenced
7284 // from a compressed 2nd level index table. In practice the linker only
7285 // creates ~128 of these, so that indices are available to embed encodings in
7286 // the 2nd level index.
7287
7288 SmallVector<uint32_t, 64> CommonEncodings;
7289 outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n";
7290 Pos = Contents.data() + CommonEncodingsStart;
7291 for (unsigned i = 0; i < NumCommonEncodings; ++i) {
7292 uint32_t Encoding = readNext<uint32_t>(Pos);
7293 CommonEncodings.push_back(Encoding);
7294
7295 outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding)
7296 << '\n';
7297 }
7298
Tim Northover39c70bb2014-08-12 11:52:59 +00007299 //===----------------------------------
7300 // Personality functions used in this executable
7301 //===----------------------------------
7302
7303 // There should be only a handful of these (one per source language,
7304 // roughly). Particularly since they only get 2 bits in the compact encoding.
7305
7306 outs() << " Personality functions: (count = " << NumPersonalities << ")\n";
7307 Pos = Contents.data() + PersonalitiesStart;
7308 for (unsigned i = 0; i < NumPersonalities; ++i) {
7309 uint32_t PersonalityFn = readNext<uint32_t>(Pos);
7310 outs() << " personality[" << i + 1
7311 << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n';
7312 }
7313
7314 //===----------------------------------
7315 // The level 1 index entries
7316 //===----------------------------------
7317
7318 // These specify an approximate place to start searching for the more detailed
7319 // information, sorted by PC.
7320
7321 struct IndexEntry {
7322 uint32_t FunctionOffset;
7323 uint32_t SecondLevelPageStart;
7324 uint32_t LSDAStart;
7325 };
7326
7327 SmallVector<IndexEntry, 4> IndexEntries;
7328
7329 outs() << " Top level indices: (count = " << NumIndices << ")\n";
7330 Pos = Contents.data() + IndicesStart;
7331 for (unsigned i = 0; i < NumIndices; ++i) {
7332 IndexEntry Entry;
7333
7334 Entry.FunctionOffset = readNext<uint32_t>(Pos);
7335 Entry.SecondLevelPageStart = readNext<uint32_t>(Pos);
7336 Entry.LSDAStart = readNext<uint32_t>(Pos);
7337 IndexEntries.push_back(Entry);
7338
7339 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007340 << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset)
7341 << ", "
Tim Northover39c70bb2014-08-12 11:52:59 +00007342 << "2nd level page offset="
7343 << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007344 << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00007345 }
7346
Tim Northover39c70bb2014-08-12 11:52:59 +00007347 //===----------------------------------
7348 // Next come the LSDA tables
7349 //===----------------------------------
7350
7351 // The LSDA layout is rather implicit: it's a contiguous array of entries from
7352 // the first top-level index's LSDAOffset to the last (sentinel).
7353
7354 outs() << " LSDA descriptors:\n";
7355 Pos = Contents.data() + IndexEntries[0].LSDAStart;
7356 int NumLSDAs = (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) /
7357 (2 * sizeof(uint32_t));
7358 for (int i = 0; i < NumLSDAs; ++i) {
7359 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
7360 uint32_t LSDAOffset = readNext<uint32_t>(Pos);
7361 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007362 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
7363 << ", "
7364 << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00007365 }
7366
7367 //===----------------------------------
7368 // Finally, the 2nd level indices
7369 //===----------------------------------
7370
7371 // Generally these are 4K in size, and have 2 possible forms:
7372 // + Regular stores up to 511 entries with disparate encodings
7373 // + Compressed stores up to 1021 entries if few enough compact encoding
7374 // values are used.
7375 outs() << " Second level indices:\n";
7376 for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) {
7377 // The final sentinel top-level index has no associated 2nd level page
7378 if (IndexEntries[i].SecondLevelPageStart == 0)
7379 break;
7380
7381 outs() << " Second level index[" << i << "]: "
7382 << "offset in section="
7383 << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart)
7384 << ", "
7385 << "base function offset="
7386 << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n';
7387
7388 Pos = Contents.data() + IndexEntries[i].SecondLevelPageStart;
Aaron Ballman80930af2014-08-14 13:53:19 +00007389 uint32_t Kind = *reinterpret_cast<const support::ulittle32_t *>(Pos);
Tim Northover39c70bb2014-08-12 11:52:59 +00007390 if (Kind == 2)
7391 printRegularSecondLevelUnwindPage(Pos);
7392 else if (Kind == 3)
7393 printCompressedSecondLevelUnwindPage(Pos, IndexEntries[i].FunctionOffset,
7394 CommonEncodings);
7395 else
7396 llvm_unreachable("Do not know how to print this kind of 2nd level page");
Tim Northover39c70bb2014-08-12 11:52:59 +00007397 }
7398}
7399
Tim Northover4bd286a2014-08-01 13:07:19 +00007400void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) {
7401 std::map<uint64_t, SymbolRef> Symbols;
7402 for (const SymbolRef &SymRef : Obj->symbols()) {
7403 // Discard any undefined or absolute symbols. They're not going to take part
7404 // in the convenience lookup for unwind info and just take up resources.
Kevin Enderby7bd8d992016-05-02 20:28:12 +00007405 auto SectOrErr = SymRef.getSection();
7406 if (!SectOrErr) {
7407 // TODO: Actually report errors helpfully.
7408 consumeError(SectOrErr.takeError());
7409 continue;
7410 }
7411 section_iterator Section = *SectOrErr;
Tim Northover4bd286a2014-08-01 13:07:19 +00007412 if (Section == Obj->section_end())
7413 continue;
7414
Rafael Espindoladea00162015-07-03 17:44:18 +00007415 uint64_t Addr = SymRef.getValue();
Tim Northover4bd286a2014-08-01 13:07:19 +00007416 Symbols.insert(std::make_pair(Addr, SymRef));
7417 }
7418
7419 for (const SectionRef &Section : Obj->sections()) {
7420 StringRef SectName;
7421 Section.getName(SectName);
7422 if (SectName == "__compact_unwind")
7423 printMachOCompactUnwindSection(Obj, Symbols, Section);
7424 else if (SectName == "__unwind_info")
Tim Northover39c70bb2014-08-12 11:52:59 +00007425 printMachOUnwindInfoSection(Obj, Symbols, Section);
Tim Northover4bd286a2014-08-01 13:07:19 +00007426 }
7427}
Kevin Enderbyb76d3862014-08-22 20:35:18 +00007428
7429static void PrintMachHeader(uint32_t magic, uint32_t cputype,
7430 uint32_t cpusubtype, uint32_t filetype,
7431 uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
7432 bool verbose) {
7433 outs() << "Mach header\n";
7434 outs() << " magic cputype cpusubtype caps filetype ncmds "
7435 "sizeofcmds flags\n";
7436 if (verbose) {
7437 if (magic == MachO::MH_MAGIC)
7438 outs() << " MH_MAGIC";
7439 else if (magic == MachO::MH_MAGIC_64)
7440 outs() << "MH_MAGIC_64";
7441 else
7442 outs() << format(" 0x%08" PRIx32, magic);
7443 switch (cputype) {
7444 case MachO::CPU_TYPE_I386:
7445 outs() << " I386";
7446 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7447 case MachO::CPU_SUBTYPE_I386_ALL:
7448 outs() << " ALL";
7449 break;
7450 default:
7451 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7452 break;
7453 }
7454 break;
7455 case MachO::CPU_TYPE_X86_64:
7456 outs() << " X86_64";
Kevin Enderby131d1772015-01-09 19:22:37 +00007457 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7458 case MachO::CPU_SUBTYPE_X86_64_ALL:
7459 outs() << " ALL";
7460 break;
7461 case MachO::CPU_SUBTYPE_X86_64_H:
7462 outs() << " Haswell";
7463 break;
7464 default:
7465 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7466 break;
7467 }
Kevin Enderbyb76d3862014-08-22 20:35:18 +00007468 break;
7469 case MachO::CPU_TYPE_ARM:
7470 outs() << " ARM";
7471 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7472 case MachO::CPU_SUBTYPE_ARM_ALL:
7473 outs() << " ALL";
7474 break;
7475 case MachO::CPU_SUBTYPE_ARM_V4T:
7476 outs() << " V4T";
7477 break;
7478 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
7479 outs() << " V5TEJ";
7480 break;
7481 case MachO::CPU_SUBTYPE_ARM_XSCALE:
7482 outs() << " XSCALE";
7483 break;
7484 case MachO::CPU_SUBTYPE_ARM_V6:
7485 outs() << " V6";
7486 break;
7487 case MachO::CPU_SUBTYPE_ARM_V6M:
7488 outs() << " V6M";
7489 break;
7490 case MachO::CPU_SUBTYPE_ARM_V7:
7491 outs() << " V7";
7492 break;
7493 case MachO::CPU_SUBTYPE_ARM_V7EM:
7494 outs() << " V7EM";
7495 break;
7496 case MachO::CPU_SUBTYPE_ARM_V7K:
7497 outs() << " V7K";
7498 break;
7499 case MachO::CPU_SUBTYPE_ARM_V7M:
7500 outs() << " V7M";
7501 break;
7502 case MachO::CPU_SUBTYPE_ARM_V7S:
7503 outs() << " V7S";
7504 break;
7505 default:
7506 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7507 break;
7508 }
7509 break;
7510 case MachO::CPU_TYPE_ARM64:
7511 outs() << " ARM64";
7512 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7513 case MachO::CPU_SUBTYPE_ARM64_ALL:
7514 outs() << " ALL";
7515 break;
7516 default:
7517 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7518 break;
7519 }
7520 break;
7521 case MachO::CPU_TYPE_POWERPC:
7522 outs() << " PPC";
7523 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7524 case MachO::CPU_SUBTYPE_POWERPC_ALL:
7525 outs() << " ALL";
7526 break;
7527 default:
7528 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7529 break;
7530 }
7531 break;
7532 case MachO::CPU_TYPE_POWERPC64:
7533 outs() << " PPC64";
7534 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7535 case MachO::CPU_SUBTYPE_POWERPC_ALL:
7536 outs() << " ALL";
7537 break;
7538 default:
7539 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7540 break;
7541 }
7542 break;
Kevin Enderby40fdbf82016-01-26 18:20:49 +00007543 default:
7544 outs() << format(" %7d", cputype);
7545 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7546 break;
Kevin Enderbyb76d3862014-08-22 20:35:18 +00007547 }
7548 if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007549 outs() << " LIB64";
Kevin Enderbyb76d3862014-08-22 20:35:18 +00007550 } else {
7551 outs() << format(" 0x%02" PRIx32,
7552 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
7553 }
7554 switch (filetype) {
7555 case MachO::MH_OBJECT:
7556 outs() << " OBJECT";
7557 break;
7558 case MachO::MH_EXECUTE:
7559 outs() << " EXECUTE";
7560 break;
7561 case MachO::MH_FVMLIB:
7562 outs() << " FVMLIB";
7563 break;
7564 case MachO::MH_CORE:
7565 outs() << " CORE";
7566 break;
7567 case MachO::MH_PRELOAD:
7568 outs() << " PRELOAD";
7569 break;
7570 case MachO::MH_DYLIB:
7571 outs() << " DYLIB";
7572 break;
7573 case MachO::MH_DYLIB_STUB:
7574 outs() << " DYLIB_STUB";
7575 break;
7576 case MachO::MH_DYLINKER:
7577 outs() << " DYLINKER";
7578 break;
7579 case MachO::MH_BUNDLE:
7580 outs() << " BUNDLE";
7581 break;
7582 case MachO::MH_DSYM:
7583 outs() << " DSYM";
7584 break;
7585 case MachO::MH_KEXT_BUNDLE:
7586 outs() << " KEXTBUNDLE";
7587 break;
7588 default:
7589 outs() << format(" %10u", filetype);
7590 break;
7591 }
7592 outs() << format(" %5u", ncmds);
7593 outs() << format(" %10u", sizeofcmds);
7594 uint32_t f = flags;
7595 if (f & MachO::MH_NOUNDEFS) {
7596 outs() << " NOUNDEFS";
7597 f &= ~MachO::MH_NOUNDEFS;
7598 }
7599 if (f & MachO::MH_INCRLINK) {
7600 outs() << " INCRLINK";
7601 f &= ~MachO::MH_INCRLINK;
7602 }
7603 if (f & MachO::MH_DYLDLINK) {
7604 outs() << " DYLDLINK";
7605 f &= ~MachO::MH_DYLDLINK;
7606 }
7607 if (f & MachO::MH_BINDATLOAD) {
7608 outs() << " BINDATLOAD";
7609 f &= ~MachO::MH_BINDATLOAD;
7610 }
7611 if (f & MachO::MH_PREBOUND) {
7612 outs() << " PREBOUND";
7613 f &= ~MachO::MH_PREBOUND;
7614 }
7615 if (f & MachO::MH_SPLIT_SEGS) {
7616 outs() << " SPLIT_SEGS";
7617 f &= ~MachO::MH_SPLIT_SEGS;
7618 }
7619 if (f & MachO::MH_LAZY_INIT) {
7620 outs() << " LAZY_INIT";
7621 f &= ~MachO::MH_LAZY_INIT;
7622 }
7623 if (f & MachO::MH_TWOLEVEL) {
7624 outs() << " TWOLEVEL";
7625 f &= ~MachO::MH_TWOLEVEL;
7626 }
7627 if (f & MachO::MH_FORCE_FLAT) {
7628 outs() << " FORCE_FLAT";
7629 f &= ~MachO::MH_FORCE_FLAT;
7630 }
7631 if (f & MachO::MH_NOMULTIDEFS) {
7632 outs() << " NOMULTIDEFS";
7633 f &= ~MachO::MH_NOMULTIDEFS;
7634 }
7635 if (f & MachO::MH_NOFIXPREBINDING) {
7636 outs() << " NOFIXPREBINDING";
7637 f &= ~MachO::MH_NOFIXPREBINDING;
7638 }
7639 if (f & MachO::MH_PREBINDABLE) {
7640 outs() << " PREBINDABLE";
7641 f &= ~MachO::MH_PREBINDABLE;
7642 }
7643 if (f & MachO::MH_ALLMODSBOUND) {
7644 outs() << " ALLMODSBOUND";
7645 f &= ~MachO::MH_ALLMODSBOUND;
7646 }
7647 if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
7648 outs() << " SUBSECTIONS_VIA_SYMBOLS";
7649 f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
7650 }
7651 if (f & MachO::MH_CANONICAL) {
7652 outs() << " CANONICAL";
7653 f &= ~MachO::MH_CANONICAL;
7654 }
7655 if (f & MachO::MH_WEAK_DEFINES) {
7656 outs() << " WEAK_DEFINES";
7657 f &= ~MachO::MH_WEAK_DEFINES;
7658 }
7659 if (f & MachO::MH_BINDS_TO_WEAK) {
7660 outs() << " BINDS_TO_WEAK";
7661 f &= ~MachO::MH_BINDS_TO_WEAK;
7662 }
7663 if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
7664 outs() << " ALLOW_STACK_EXECUTION";
7665 f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
7666 }
7667 if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
7668 outs() << " DEAD_STRIPPABLE_DYLIB";
7669 f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
7670 }
7671 if (f & MachO::MH_PIE) {
7672 outs() << " PIE";
7673 f &= ~MachO::MH_PIE;
7674 }
7675 if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
7676 outs() << " NO_REEXPORTED_DYLIBS";
7677 f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
7678 }
7679 if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
7680 outs() << " MH_HAS_TLV_DESCRIPTORS";
7681 f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
7682 }
7683 if (f & MachO::MH_NO_HEAP_EXECUTION) {
7684 outs() << " MH_NO_HEAP_EXECUTION";
7685 f &= ~MachO::MH_NO_HEAP_EXECUTION;
7686 }
7687 if (f & MachO::MH_APP_EXTENSION_SAFE) {
7688 outs() << " APP_EXTENSION_SAFE";
7689 f &= ~MachO::MH_APP_EXTENSION_SAFE;
7690 }
7691 if (f != 0 || flags == 0)
7692 outs() << format(" 0x%08" PRIx32, f);
7693 } else {
7694 outs() << format(" 0x%08" PRIx32, magic);
7695 outs() << format(" %7d", cputype);
7696 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7697 outs() << format(" 0x%02" PRIx32,
7698 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
7699 outs() << format(" %10u", filetype);
7700 outs() << format(" %5u", ncmds);
7701 outs() << format(" %10u", sizeofcmds);
7702 outs() << format(" 0x%08" PRIx32, flags);
7703 }
7704 outs() << "\n";
7705}
7706
Kevin Enderby956366c2014-08-29 22:30:52 +00007707static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
7708 StringRef SegName, uint64_t vmaddr,
7709 uint64_t vmsize, uint64_t fileoff,
7710 uint64_t filesize, uint32_t maxprot,
7711 uint32_t initprot, uint32_t nsects,
7712 uint32_t flags, uint32_t object_size,
7713 bool verbose) {
7714 uint64_t expected_cmdsize;
7715 if (cmd == MachO::LC_SEGMENT) {
7716 outs() << " cmd LC_SEGMENT\n";
7717 expected_cmdsize = nsects;
7718 expected_cmdsize *= sizeof(struct MachO::section);
7719 expected_cmdsize += sizeof(struct MachO::segment_command);
7720 } else {
7721 outs() << " cmd LC_SEGMENT_64\n";
7722 expected_cmdsize = nsects;
7723 expected_cmdsize *= sizeof(struct MachO::section_64);
7724 expected_cmdsize += sizeof(struct MachO::segment_command_64);
7725 }
7726 outs() << " cmdsize " << cmdsize;
7727 if (cmdsize != expected_cmdsize)
7728 outs() << " Inconsistent size\n";
7729 else
7730 outs() << "\n";
7731 outs() << " segname " << SegName << "\n";
7732 if (cmd == MachO::LC_SEGMENT_64) {
7733 outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n";
7734 outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n";
7735 } else {
Kevin Enderbyadb7c432014-12-16 18:58:11 +00007736 outs() << " vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n";
7737 outs() << " vmsize " << format("0x%08" PRIx64, vmsize) << "\n";
Kevin Enderby956366c2014-08-29 22:30:52 +00007738 }
7739 outs() << " fileoff " << fileoff;
7740 if (fileoff > object_size)
7741 outs() << " (past end of file)\n";
7742 else
7743 outs() << "\n";
7744 outs() << " filesize " << filesize;
7745 if (fileoff + filesize > object_size)
7746 outs() << " (past end of file)\n";
7747 else
7748 outs() << "\n";
7749 if (verbose) {
7750 if ((maxprot &
7751 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7752 MachO::VM_PROT_EXECUTE)) != 0)
7753 outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n";
7754 else {
Davide Italiano37ff06a2015-09-02 16:53:25 +00007755 outs() << " maxprot ";
7756 outs() << ((maxprot & MachO::VM_PROT_READ) ? "r" : "-");
7757 outs() << ((maxprot & MachO::VM_PROT_WRITE) ? "w" : "-");
7758 outs() << ((maxprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
Kevin Enderby956366c2014-08-29 22:30:52 +00007759 }
7760 if ((initprot &
7761 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7762 MachO::VM_PROT_EXECUTE)) != 0)
7763 outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n";
7764 else {
Davide Italiano37ff06a2015-09-02 16:53:25 +00007765 outs() << " initprot ";
7766 outs() << ((initprot & MachO::VM_PROT_READ) ? "r" : "-");
7767 outs() << ((initprot & MachO::VM_PROT_WRITE) ? "w" : "-");
7768 outs() << ((initprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
Kevin Enderby956366c2014-08-29 22:30:52 +00007769 }
7770 } else {
7771 outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n";
7772 outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n";
7773 }
7774 outs() << " nsects " << nsects << "\n";
7775 if (verbose) {
7776 outs() << " flags";
7777 if (flags == 0)
7778 outs() << " (none)\n";
7779 else {
7780 if (flags & MachO::SG_HIGHVM) {
7781 outs() << " HIGHVM";
7782 flags &= ~MachO::SG_HIGHVM;
7783 }
7784 if (flags & MachO::SG_FVMLIB) {
7785 outs() << " FVMLIB";
7786 flags &= ~MachO::SG_FVMLIB;
7787 }
7788 if (flags & MachO::SG_NORELOC) {
7789 outs() << " NORELOC";
7790 flags &= ~MachO::SG_NORELOC;
7791 }
7792 if (flags & MachO::SG_PROTECTED_VERSION_1) {
7793 outs() << " PROTECTED_VERSION_1";
7794 flags &= ~MachO::SG_PROTECTED_VERSION_1;
7795 }
7796 if (flags)
7797 outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n";
7798 else
7799 outs() << "\n";
7800 }
7801 } else {
7802 outs() << " flags " << format("0x%" PRIx32, flags) << "\n";
7803 }
7804}
7805
7806static void PrintSection(const char *sectname, const char *segname,
7807 uint64_t addr, uint64_t size, uint32_t offset,
7808 uint32_t align, uint32_t reloff, uint32_t nreloc,
7809 uint32_t flags, uint32_t reserved1, uint32_t reserved2,
7810 uint32_t cmd, const char *sg_segname,
7811 uint32_t filetype, uint32_t object_size,
7812 bool verbose) {
7813 outs() << "Section\n";
7814 outs() << " sectname " << format("%.16s\n", sectname);
7815 outs() << " segname " << format("%.16s", segname);
7816 if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0)
7817 outs() << " (does not match segment)\n";
7818 else
7819 outs() << "\n";
7820 if (cmd == MachO::LC_SEGMENT_64) {
7821 outs() << " addr " << format("0x%016" PRIx64, addr) << "\n";
7822 outs() << " size " << format("0x%016" PRIx64, size);
7823 } else {
Kevin Enderby75594b62014-12-16 21:00:25 +00007824 outs() << " addr " << format("0x%08" PRIx64, addr) << "\n";
7825 outs() << " size " << format("0x%08" PRIx64, size);
Kevin Enderby956366c2014-08-29 22:30:52 +00007826 }
7827 if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size)
7828 outs() << " (past end of file)\n";
7829 else
7830 outs() << "\n";
7831 outs() << " offset " << offset;
7832 if (offset > object_size)
7833 outs() << " (past end of file)\n";
7834 else
7835 outs() << "\n";
7836 uint32_t align_shifted = 1 << align;
7837 outs() << " align 2^" << align << " (" << align_shifted << ")\n";
7838 outs() << " reloff " << reloff;
7839 if (reloff > object_size)
7840 outs() << " (past end of file)\n";
7841 else
7842 outs() << "\n";
7843 outs() << " nreloc " << nreloc;
7844 if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size)
7845 outs() << " (past end of file)\n";
7846 else
7847 outs() << "\n";
7848 uint32_t section_type = flags & MachO::SECTION_TYPE;
7849 if (verbose) {
7850 outs() << " type";
7851 if (section_type == MachO::S_REGULAR)
7852 outs() << " S_REGULAR\n";
7853 else if (section_type == MachO::S_ZEROFILL)
7854 outs() << " S_ZEROFILL\n";
7855 else if (section_type == MachO::S_CSTRING_LITERALS)
7856 outs() << " S_CSTRING_LITERALS\n";
7857 else if (section_type == MachO::S_4BYTE_LITERALS)
7858 outs() << " S_4BYTE_LITERALS\n";
7859 else if (section_type == MachO::S_8BYTE_LITERALS)
7860 outs() << " S_8BYTE_LITERALS\n";
7861 else if (section_type == MachO::S_16BYTE_LITERALS)
7862 outs() << " S_16BYTE_LITERALS\n";
7863 else if (section_type == MachO::S_LITERAL_POINTERS)
7864 outs() << " S_LITERAL_POINTERS\n";
7865 else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS)
7866 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
7867 else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS)
7868 outs() << " S_LAZY_SYMBOL_POINTERS\n";
7869 else if (section_type == MachO::S_SYMBOL_STUBS)
7870 outs() << " S_SYMBOL_STUBS\n";
7871 else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS)
7872 outs() << " S_MOD_INIT_FUNC_POINTERS\n";
7873 else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS)
7874 outs() << " S_MOD_TERM_FUNC_POINTERS\n";
7875 else if (section_type == MachO::S_COALESCED)
7876 outs() << " S_COALESCED\n";
7877 else if (section_type == MachO::S_INTERPOSING)
7878 outs() << " S_INTERPOSING\n";
7879 else if (section_type == MachO::S_DTRACE_DOF)
7880 outs() << " S_DTRACE_DOF\n";
7881 else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS)
7882 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
7883 else if (section_type == MachO::S_THREAD_LOCAL_REGULAR)
7884 outs() << " S_THREAD_LOCAL_REGULAR\n";
7885 else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL)
7886 outs() << " S_THREAD_LOCAL_ZEROFILL\n";
7887 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES)
7888 outs() << " S_THREAD_LOCAL_VARIABLES\n";
7889 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7890 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
7891 else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS)
7892 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
7893 else
7894 outs() << format("0x%08" PRIx32, section_type) << "\n";
7895 outs() << "attributes";
7896 uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES;
7897 if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS)
7898 outs() << " PURE_INSTRUCTIONS";
7899 if (section_attributes & MachO::S_ATTR_NO_TOC)
7900 outs() << " NO_TOC";
7901 if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS)
7902 outs() << " STRIP_STATIC_SYMS";
7903 if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP)
7904 outs() << " NO_DEAD_STRIP";
7905 if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT)
7906 outs() << " LIVE_SUPPORT";
7907 if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE)
7908 outs() << " SELF_MODIFYING_CODE";
7909 if (section_attributes & MachO::S_ATTR_DEBUG)
7910 outs() << " DEBUG";
7911 if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS)
7912 outs() << " SOME_INSTRUCTIONS";
7913 if (section_attributes & MachO::S_ATTR_EXT_RELOC)
7914 outs() << " EXT_RELOC";
7915 if (section_attributes & MachO::S_ATTR_LOC_RELOC)
7916 outs() << " LOC_RELOC";
7917 if (section_attributes == 0)
7918 outs() << " (none)";
7919 outs() << "\n";
7920 } else
7921 outs() << " flags " << format("0x%08" PRIx32, flags) << "\n";
7922 outs() << " reserved1 " << reserved1;
7923 if (section_type == MachO::S_SYMBOL_STUBS ||
7924 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
7925 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
7926 section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
7927 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7928 outs() << " (index into indirect symbol table)\n";
7929 else
7930 outs() << "\n";
7931 outs() << " reserved2 " << reserved2;
7932 if (section_type == MachO::S_SYMBOL_STUBS)
7933 outs() << " (size of stubs)\n";
7934 else
7935 outs() << "\n";
7936}
7937
David Majnemer73cc6ff2014-11-13 19:48:56 +00007938static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit,
Kevin Enderby956366c2014-08-29 22:30:52 +00007939 uint32_t object_size) {
7940 outs() << " cmd LC_SYMTAB\n";
7941 outs() << " cmdsize " << st.cmdsize;
7942 if (st.cmdsize != sizeof(struct MachO::symtab_command))
7943 outs() << " Incorrect size\n";
7944 else
7945 outs() << "\n";
7946 outs() << " symoff " << st.symoff;
7947 if (st.symoff > object_size)
7948 outs() << " (past end of file)\n";
7949 else
7950 outs() << "\n";
7951 outs() << " nsyms " << st.nsyms;
7952 uint64_t big_size;
David Majnemer73cc6ff2014-11-13 19:48:56 +00007953 if (Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00007954 big_size = st.nsyms;
7955 big_size *= sizeof(struct MachO::nlist_64);
7956 big_size += st.symoff;
7957 if (big_size > object_size)
7958 outs() << " (past end of file)\n";
7959 else
7960 outs() << "\n";
7961 } else {
7962 big_size = st.nsyms;
7963 big_size *= sizeof(struct MachO::nlist);
7964 big_size += st.symoff;
7965 if (big_size > object_size)
7966 outs() << " (past end of file)\n";
7967 else
7968 outs() << "\n";
7969 }
7970 outs() << " stroff " << st.stroff;
7971 if (st.stroff > object_size)
7972 outs() << " (past end of file)\n";
7973 else
7974 outs() << "\n";
7975 outs() << " strsize " << st.strsize;
7976 big_size = st.stroff;
7977 big_size += st.strsize;
7978 if (big_size > object_size)
7979 outs() << " (past end of file)\n";
7980 else
7981 outs() << "\n";
7982}
7983
7984static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
7985 uint32_t nsyms, uint32_t object_size,
David Majnemer73cc6ff2014-11-13 19:48:56 +00007986 bool Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00007987 outs() << " cmd LC_DYSYMTAB\n";
7988 outs() << " cmdsize " << dyst.cmdsize;
7989 if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
7990 outs() << " Incorrect size\n";
7991 else
7992 outs() << "\n";
7993 outs() << " ilocalsym " << dyst.ilocalsym;
7994 if (dyst.ilocalsym > nsyms)
7995 outs() << " (greater than the number of symbols)\n";
7996 else
7997 outs() << "\n";
7998 outs() << " nlocalsym " << dyst.nlocalsym;
7999 uint64_t big_size;
8000 big_size = dyst.ilocalsym;
8001 big_size += dyst.nlocalsym;
8002 if (big_size > nsyms)
8003 outs() << " (past the end of the symbol table)\n";
8004 else
8005 outs() << "\n";
8006 outs() << " iextdefsym " << dyst.iextdefsym;
8007 if (dyst.iextdefsym > nsyms)
8008 outs() << " (greater than the number of symbols)\n";
8009 else
8010 outs() << "\n";
8011 outs() << " nextdefsym " << dyst.nextdefsym;
8012 big_size = dyst.iextdefsym;
8013 big_size += dyst.nextdefsym;
8014 if (big_size > nsyms)
8015 outs() << " (past the end of the symbol table)\n";
8016 else
8017 outs() << "\n";
8018 outs() << " iundefsym " << dyst.iundefsym;
8019 if (dyst.iundefsym > nsyms)
8020 outs() << " (greater than the number of symbols)\n";
8021 else
8022 outs() << "\n";
8023 outs() << " nundefsym " << dyst.nundefsym;
8024 big_size = dyst.iundefsym;
8025 big_size += dyst.nundefsym;
8026 if (big_size > nsyms)
8027 outs() << " (past the end of the symbol table)\n";
8028 else
8029 outs() << "\n";
8030 outs() << " tocoff " << dyst.tocoff;
8031 if (dyst.tocoff > object_size)
8032 outs() << " (past end of file)\n";
8033 else
8034 outs() << "\n";
8035 outs() << " ntoc " << dyst.ntoc;
8036 big_size = dyst.ntoc;
8037 big_size *= sizeof(struct MachO::dylib_table_of_contents);
8038 big_size += dyst.tocoff;
8039 if (big_size > object_size)
8040 outs() << " (past end of file)\n";
8041 else
8042 outs() << "\n";
8043 outs() << " modtaboff " << dyst.modtaboff;
8044 if (dyst.modtaboff > object_size)
8045 outs() << " (past end of file)\n";
8046 else
8047 outs() << "\n";
8048 outs() << " nmodtab " << dyst.nmodtab;
8049 uint64_t modtabend;
David Majnemer73cc6ff2014-11-13 19:48:56 +00008050 if (Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00008051 modtabend = dyst.nmodtab;
8052 modtabend *= sizeof(struct MachO::dylib_module_64);
8053 modtabend += dyst.modtaboff;
8054 } else {
8055 modtabend = dyst.nmodtab;
8056 modtabend *= sizeof(struct MachO::dylib_module);
8057 modtabend += dyst.modtaboff;
8058 }
8059 if (modtabend > object_size)
8060 outs() << " (past end of file)\n";
8061 else
8062 outs() << "\n";
8063 outs() << " extrefsymoff " << dyst.extrefsymoff;
8064 if (dyst.extrefsymoff > object_size)
8065 outs() << " (past end of file)\n";
8066 else
8067 outs() << "\n";
8068 outs() << " nextrefsyms " << dyst.nextrefsyms;
8069 big_size = dyst.nextrefsyms;
8070 big_size *= sizeof(struct MachO::dylib_reference);
8071 big_size += dyst.extrefsymoff;
8072 if (big_size > object_size)
8073 outs() << " (past end of file)\n";
8074 else
8075 outs() << "\n";
8076 outs() << " indirectsymoff " << dyst.indirectsymoff;
8077 if (dyst.indirectsymoff > object_size)
8078 outs() << " (past end of file)\n";
8079 else
8080 outs() << "\n";
8081 outs() << " nindirectsyms " << dyst.nindirectsyms;
8082 big_size = dyst.nindirectsyms;
8083 big_size *= sizeof(uint32_t);
8084 big_size += dyst.indirectsymoff;
8085 if (big_size > object_size)
8086 outs() << " (past end of file)\n";
8087 else
8088 outs() << "\n";
8089 outs() << " extreloff " << dyst.extreloff;
8090 if (dyst.extreloff > object_size)
8091 outs() << " (past end of file)\n";
8092 else
8093 outs() << "\n";
8094 outs() << " nextrel " << dyst.nextrel;
8095 big_size = dyst.nextrel;
8096 big_size *= sizeof(struct MachO::relocation_info);
8097 big_size += dyst.extreloff;
8098 if (big_size > object_size)
8099 outs() << " (past end of file)\n";
8100 else
8101 outs() << "\n";
8102 outs() << " locreloff " << dyst.locreloff;
8103 if (dyst.locreloff > object_size)
8104 outs() << " (past end of file)\n";
8105 else
8106 outs() << "\n";
8107 outs() << " nlocrel " << dyst.nlocrel;
8108 big_size = dyst.nlocrel;
8109 big_size *= sizeof(struct MachO::relocation_info);
8110 big_size += dyst.locreloff;
8111 if (big_size > object_size)
8112 outs() << " (past end of file)\n";
8113 else
8114 outs() << "\n";
8115}
8116
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008117static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc,
8118 uint32_t object_size) {
8119 if (dc.cmd == MachO::LC_DYLD_INFO)
8120 outs() << " cmd LC_DYLD_INFO\n";
8121 else
8122 outs() << " cmd LC_DYLD_INFO_ONLY\n";
8123 outs() << " cmdsize " << dc.cmdsize;
8124 if (dc.cmdsize != sizeof(struct MachO::dyld_info_command))
8125 outs() << " Incorrect size\n";
8126 else
8127 outs() << "\n";
8128 outs() << " rebase_off " << dc.rebase_off;
8129 if (dc.rebase_off > object_size)
8130 outs() << " (past end of file)\n";
8131 else
8132 outs() << "\n";
8133 outs() << " rebase_size " << dc.rebase_size;
8134 uint64_t big_size;
8135 big_size = dc.rebase_off;
8136 big_size += dc.rebase_size;
8137 if (big_size > object_size)
8138 outs() << " (past end of file)\n";
8139 else
8140 outs() << "\n";
8141 outs() << " bind_off " << dc.bind_off;
8142 if (dc.bind_off > object_size)
8143 outs() << " (past end of file)\n";
8144 else
8145 outs() << "\n";
8146 outs() << " bind_size " << dc.bind_size;
8147 big_size = dc.bind_off;
8148 big_size += dc.bind_size;
8149 if (big_size > object_size)
8150 outs() << " (past end of file)\n";
8151 else
8152 outs() << "\n";
8153 outs() << " weak_bind_off " << dc.weak_bind_off;
8154 if (dc.weak_bind_off > object_size)
8155 outs() << " (past end of file)\n";
8156 else
8157 outs() << "\n";
8158 outs() << " weak_bind_size " << dc.weak_bind_size;
8159 big_size = dc.weak_bind_off;
8160 big_size += dc.weak_bind_size;
8161 if (big_size > object_size)
8162 outs() << " (past end of file)\n";
8163 else
8164 outs() << "\n";
8165 outs() << " lazy_bind_off " << dc.lazy_bind_off;
8166 if (dc.lazy_bind_off > object_size)
8167 outs() << " (past end of file)\n";
8168 else
8169 outs() << "\n";
8170 outs() << " lazy_bind_size " << dc.lazy_bind_size;
8171 big_size = dc.lazy_bind_off;
8172 big_size += dc.lazy_bind_size;
8173 if (big_size > object_size)
8174 outs() << " (past end of file)\n";
8175 else
8176 outs() << "\n";
8177 outs() << " export_off " << dc.export_off;
8178 if (dc.export_off > object_size)
8179 outs() << " (past end of file)\n";
8180 else
8181 outs() << "\n";
8182 outs() << " export_size " << dc.export_size;
8183 big_size = dc.export_off;
8184 big_size += dc.export_size;
8185 if (big_size > object_size)
8186 outs() << " (past end of file)\n";
8187 else
8188 outs() << "\n";
8189}
8190
8191static void PrintDyldLoadCommand(MachO::dylinker_command dyld,
8192 const char *Ptr) {
8193 if (dyld.cmd == MachO::LC_ID_DYLINKER)
8194 outs() << " cmd LC_ID_DYLINKER\n";
8195 else if (dyld.cmd == MachO::LC_LOAD_DYLINKER)
8196 outs() << " cmd LC_LOAD_DYLINKER\n";
8197 else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT)
8198 outs() << " cmd LC_DYLD_ENVIRONMENT\n";
8199 else
8200 outs() << " cmd ?(" << dyld.cmd << ")\n";
8201 outs() << " cmdsize " << dyld.cmdsize;
8202 if (dyld.cmdsize < sizeof(struct MachO::dylinker_command))
8203 outs() << " Incorrect size\n";
8204 else
8205 outs() << "\n";
8206 if (dyld.name >= dyld.cmdsize)
8207 outs() << " name ?(bad offset " << dyld.name << ")\n";
8208 else {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008209 const char *P = (const char *)(Ptr) + dyld.name;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008210 outs() << " name " << P << " (offset " << dyld.name << ")\n";
8211 }
8212}
8213
8214static void PrintUuidLoadCommand(MachO::uuid_command uuid) {
8215 outs() << " cmd LC_UUID\n";
8216 outs() << " cmdsize " << uuid.cmdsize;
8217 if (uuid.cmdsize != sizeof(struct MachO::uuid_command))
8218 outs() << " Incorrect size\n";
8219 else
8220 outs() << "\n";
8221 outs() << " uuid ";
Davide Italianoc74277a2015-12-07 00:03:28 +00008222 for (int i = 0; i < 16; ++i) {
8223 outs() << format("%02" PRIX32, uuid.uuid[i]);
8224 if (i == 3 || i == 5 || i == 7 || i == 9)
8225 outs() << "-";
8226 }
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008227 outs() << "\n";
8228}
8229
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008230static void PrintRpathLoadCommand(MachO::rpath_command rpath, const char *Ptr) {
Jean-Daniel Dupas00cc1f52014-12-04 07:37:02 +00008231 outs() << " cmd LC_RPATH\n";
8232 outs() << " cmdsize " << rpath.cmdsize;
8233 if (rpath.cmdsize < sizeof(struct MachO::rpath_command))
8234 outs() << " Incorrect size\n";
8235 else
8236 outs() << "\n";
8237 if (rpath.path >= rpath.cmdsize)
8238 outs() << " path ?(bad offset " << rpath.path << ")\n";
8239 else {
8240 const char *P = (const char *)(Ptr) + rpath.path;
8241 outs() << " path " << P << " (offset " << rpath.path << ")\n";
8242 }
8243}
8244
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008245static void PrintVersionMinLoadCommand(MachO::version_min_command vd) {
Tim Northoverbfbfb122015-11-02 21:26:58 +00008246 StringRef LoadCmdName;
8247 switch (vd.cmd) {
8248 case MachO::LC_VERSION_MIN_MACOSX:
8249 LoadCmdName = "LC_VERSION_MIN_MACOSX";
8250 break;
8251 case MachO::LC_VERSION_MIN_IPHONEOS:
8252 LoadCmdName = "LC_VERSION_MIN_IPHONEOS";
8253 break;
8254 case MachO::LC_VERSION_MIN_TVOS:
8255 LoadCmdName = "LC_VERSION_MIN_TVOS";
8256 break;
8257 case MachO::LC_VERSION_MIN_WATCHOS:
8258 LoadCmdName = "LC_VERSION_MIN_WATCHOS";
8259 break;
8260 default:
8261 llvm_unreachable("Unknown version min load command");
8262 }
8263
8264 outs() << " cmd " << LoadCmdName << '\n';
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008265 outs() << " cmdsize " << vd.cmdsize;
8266 if (vd.cmdsize != sizeof(struct MachO::version_min_command))
8267 outs() << " Incorrect size\n";
8268 else
8269 outs() << "\n";
Davide Italiano56baef32015-08-26 12:26:11 +00008270 outs() << " version "
8271 << MachOObjectFile::getVersionMinMajor(vd, false) << "."
8272 << MachOObjectFile::getVersionMinMinor(vd, false);
8273 uint32_t Update = MachOObjectFile::getVersionMinUpdate(vd, false);
8274 if (Update != 0)
8275 outs() << "." << Update;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008276 outs() << "\n";
8277 if (vd.sdk == 0)
Kevin Enderby57538292014-12-17 01:01:30 +00008278 outs() << " sdk n/a";
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008279 else {
Davide Italiano56baef32015-08-26 12:26:11 +00008280 outs() << " sdk "
8281 << MachOObjectFile::getVersionMinMajor(vd, true) << "."
8282 << MachOObjectFile::getVersionMinMinor(vd, true);
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008283 }
Davide Italiano56baef32015-08-26 12:26:11 +00008284 Update = MachOObjectFile::getVersionMinUpdate(vd, true);
8285 if (Update != 0)
8286 outs() << "." << Update;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008287 outs() << "\n";
8288}
8289
8290static void PrintSourceVersionCommand(MachO::source_version_command sd) {
8291 outs() << " cmd LC_SOURCE_VERSION\n";
8292 outs() << " cmdsize " << sd.cmdsize;
8293 if (sd.cmdsize != sizeof(struct MachO::source_version_command))
8294 outs() << " Incorrect size\n";
8295 else
8296 outs() << "\n";
8297 uint64_t a = (sd.version >> 40) & 0xffffff;
8298 uint64_t b = (sd.version >> 30) & 0x3ff;
8299 uint64_t c = (sd.version >> 20) & 0x3ff;
8300 uint64_t d = (sd.version >> 10) & 0x3ff;
8301 uint64_t e = sd.version & 0x3ff;
8302 outs() << " version " << a << "." << b;
8303 if (e != 0)
8304 outs() << "." << c << "." << d << "." << e;
8305 else if (d != 0)
8306 outs() << "." << c << "." << d;
8307 else if (c != 0)
8308 outs() << "." << c;
8309 outs() << "\n";
8310}
8311
8312static void PrintEntryPointCommand(MachO::entry_point_command ep) {
8313 outs() << " cmd LC_MAIN\n";
8314 outs() << " cmdsize " << ep.cmdsize;
8315 if (ep.cmdsize != sizeof(struct MachO::entry_point_command))
8316 outs() << " Incorrect size\n";
8317 else
8318 outs() << "\n";
8319 outs() << " entryoff " << ep.entryoff << "\n";
8320 outs() << " stacksize " << ep.stacksize << "\n";
8321}
8322
Kevin Enderby0804f4672014-12-16 23:25:52 +00008323static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec,
8324 uint32_t object_size) {
8325 outs() << " cmd LC_ENCRYPTION_INFO\n";
8326 outs() << " cmdsize " << ec.cmdsize;
8327 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command))
8328 outs() << " Incorrect size\n";
8329 else
8330 outs() << "\n";
8331 outs() << " cryptoff " << ec.cryptoff;
8332 if (ec.cryptoff > object_size)
8333 outs() << " (past end of file)\n";
8334 else
8335 outs() << "\n";
8336 outs() << " cryptsize " << ec.cryptsize;
8337 if (ec.cryptsize > object_size)
8338 outs() << " (past end of file)\n";
8339 else
8340 outs() << "\n";
8341 outs() << " cryptid " << ec.cryptid << "\n";
8342}
8343
Kevin Enderby57538292014-12-17 01:01:30 +00008344static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec,
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008345 uint32_t object_size) {
Kevin Enderby57538292014-12-17 01:01:30 +00008346 outs() << " cmd LC_ENCRYPTION_INFO_64\n";
8347 outs() << " cmdsize " << ec.cmdsize;
8348 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64))
8349 outs() << " Incorrect size\n";
8350 else
8351 outs() << "\n";
8352 outs() << " cryptoff " << ec.cryptoff;
8353 if (ec.cryptoff > object_size)
8354 outs() << " (past end of file)\n";
8355 else
8356 outs() << "\n";
8357 outs() << " cryptsize " << ec.cryptsize;
8358 if (ec.cryptsize > object_size)
8359 outs() << " (past end of file)\n";
8360 else
8361 outs() << "\n";
8362 outs() << " cryptid " << ec.cryptid << "\n";
8363 outs() << " pad " << ec.pad << "\n";
8364}
8365
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00008366static void PrintLinkerOptionCommand(MachO::linker_option_command lo,
8367 const char *Ptr) {
8368 outs() << " cmd LC_LINKER_OPTION\n";
8369 outs() << " cmdsize " << lo.cmdsize;
8370 if (lo.cmdsize < sizeof(struct MachO::linker_option_command))
8371 outs() << " Incorrect size\n";
8372 else
8373 outs() << "\n";
8374 outs() << " count " << lo.count << "\n";
8375 const char *string = Ptr + sizeof(struct MachO::linker_option_command);
8376 uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command);
8377 uint32_t i = 0;
8378 while (left > 0) {
8379 while (*string == '\0' && left > 0) {
8380 string++;
8381 left--;
8382 }
8383 if (left > 0) {
8384 i++;
8385 outs() << " string #" << i << " " << format("%.*s\n", left, string);
David Majnemerd4449ed2014-12-20 08:24:43 +00008386 uint32_t NullPos = StringRef(string, left).find('\0');
8387 uint32_t len = std::min(NullPos, left) + 1;
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00008388 string += len;
8389 left -= len;
8390 }
8391 }
8392 if (lo.count != i)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008393 outs() << " count " << lo.count << " does not match number of strings "
8394 << i << "\n";
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00008395}
8396
Kevin Enderbyb4b79312014-12-18 19:24:35 +00008397static void PrintSubFrameworkCommand(MachO::sub_framework_command sub,
8398 const char *Ptr) {
8399 outs() << " cmd LC_SUB_FRAMEWORK\n";
8400 outs() << " cmdsize " << sub.cmdsize;
8401 if (sub.cmdsize < sizeof(struct MachO::sub_framework_command))
8402 outs() << " Incorrect size\n";
8403 else
8404 outs() << "\n";
8405 if (sub.umbrella < sub.cmdsize) {
8406 const char *P = Ptr + sub.umbrella;
8407 outs() << " umbrella " << P << " (offset " << sub.umbrella << ")\n";
8408 } else {
8409 outs() << " umbrella ?(bad offset " << sub.umbrella << ")\n";
8410 }
8411}
8412
Kevin Enderbya2bd8d92014-12-18 23:13:26 +00008413static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub,
8414 const char *Ptr) {
8415 outs() << " cmd LC_SUB_UMBRELLA\n";
8416 outs() << " cmdsize " << sub.cmdsize;
8417 if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command))
8418 outs() << " Incorrect size\n";
8419 else
8420 outs() << "\n";
8421 if (sub.sub_umbrella < sub.cmdsize) {
8422 const char *P = Ptr + sub.sub_umbrella;
8423 outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n";
8424 } else {
8425 outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n";
8426 }
8427}
8428
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00008429static void PrintSubLibraryCommand(MachO::sub_library_command sub,
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008430 const char *Ptr) {
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00008431 outs() << " cmd LC_SUB_LIBRARY\n";
8432 outs() << " cmdsize " << sub.cmdsize;
8433 if (sub.cmdsize < sizeof(struct MachO::sub_library_command))
8434 outs() << " Incorrect size\n";
8435 else
8436 outs() << "\n";
8437 if (sub.sub_library < sub.cmdsize) {
8438 const char *P = Ptr + sub.sub_library;
8439 outs() << " sub_library " << P << " (offset " << sub.sub_library << ")\n";
8440 } else {
8441 outs() << " sub_library ?(bad offset " << sub.sub_library << ")\n";
8442 }
8443}
8444
Kevin Enderby186eac32014-12-19 21:06:24 +00008445static void PrintSubClientCommand(MachO::sub_client_command sub,
8446 const char *Ptr) {
8447 outs() << " cmd LC_SUB_CLIENT\n";
8448 outs() << " cmdsize " << sub.cmdsize;
8449 if (sub.cmdsize < sizeof(struct MachO::sub_client_command))
8450 outs() << " Incorrect size\n";
8451 else
8452 outs() << "\n";
8453 if (sub.client < sub.cmdsize) {
8454 const char *P = Ptr + sub.client;
8455 outs() << " client " << P << " (offset " << sub.client << ")\n";
8456 } else {
8457 outs() << " client ?(bad offset " << sub.client << ")\n";
8458 }
8459}
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00008460
Kevin Enderby52e4ce42014-12-19 22:25:22 +00008461static void PrintRoutinesCommand(MachO::routines_command r) {
8462 outs() << " cmd LC_ROUTINES\n";
8463 outs() << " cmdsize " << r.cmdsize;
8464 if (r.cmdsize != sizeof(struct MachO::routines_command))
8465 outs() << " Incorrect size\n";
8466 else
8467 outs() << "\n";
8468 outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n";
8469 outs() << " init_module " << r.init_module << "\n";
8470 outs() << " reserved1 " << r.reserved1 << "\n";
8471 outs() << " reserved2 " << r.reserved2 << "\n";
8472 outs() << " reserved3 " << r.reserved3 << "\n";
8473 outs() << " reserved4 " << r.reserved4 << "\n";
8474 outs() << " reserved5 " << r.reserved5 << "\n";
8475 outs() << " reserved6 " << r.reserved6 << "\n";
8476}
8477
8478static void PrintRoutinesCommand64(MachO::routines_command_64 r) {
8479 outs() << " cmd LC_ROUTINES_64\n";
8480 outs() << " cmdsize " << r.cmdsize;
8481 if (r.cmdsize != sizeof(struct MachO::routines_command_64))
8482 outs() << " Incorrect size\n";
8483 else
8484 outs() << "\n";
8485 outs() << " init_address " << format("0x%016" PRIx64, r.init_address) << "\n";
8486 outs() << " init_module " << r.init_module << "\n";
8487 outs() << " reserved1 " << r.reserved1 << "\n";
8488 outs() << " reserved2 " << r.reserved2 << "\n";
8489 outs() << " reserved3 " << r.reserved3 << "\n";
8490 outs() << " reserved4 " << r.reserved4 << "\n";
8491 outs() << " reserved5 " << r.reserved5 << "\n";
8492 outs() << " reserved6 " << r.reserved6 << "\n";
8493}
8494
Kevin Enderby48ef5342014-12-23 22:56:39 +00008495static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) {
8496 outs() << " rax " << format("0x%016" PRIx64, cpu64.rax);
8497 outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx);
8498 outs() << " rcx " << format("0x%016" PRIx64, cpu64.rcx) << "\n";
8499 outs() << " rdx " << format("0x%016" PRIx64, cpu64.rdx);
8500 outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi);
8501 outs() << " rsi " << format("0x%016" PRIx64, cpu64.rsi) << "\n";
8502 outs() << " rbp " << format("0x%016" PRIx64, cpu64.rbp);
8503 outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp);
8504 outs() << " r8 " << format("0x%016" PRIx64, cpu64.r8) << "\n";
8505 outs() << " r9 " << format("0x%016" PRIx64, cpu64.r9);
8506 outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10);
8507 outs() << " r11 " << format("0x%016" PRIx64, cpu64.r11) << "\n";
8508 outs() << " r12 " << format("0x%016" PRIx64, cpu64.r12);
8509 outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13);
8510 outs() << " r14 " << format("0x%016" PRIx64, cpu64.r14) << "\n";
8511 outs() << " r15 " << format("0x%016" PRIx64, cpu64.r15);
8512 outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n";
8513 outs() << "rflags " << format("0x%016" PRIx64, cpu64.rflags);
8514 outs() << " cs " << format("0x%016" PRIx64, cpu64.cs);
8515 outs() << " fs " << format("0x%016" PRIx64, cpu64.fs) << "\n";
8516 outs() << " gs " << format("0x%016" PRIx64, cpu64.gs) << "\n";
8517}
8518
Kevin Enderby227df342014-12-23 23:43:59 +00008519static void Print_mmst_reg(MachO::mmst_reg_t &r) {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008520 uint32_t f;
8521 outs() << "\t mmst_reg ";
8522 for (f = 0; f < 10; f++)
8523 outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " ";
8524 outs() << "\n";
8525 outs() << "\t mmst_rsrv ";
8526 for (f = 0; f < 6; f++)
8527 outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " ";
8528 outs() << "\n";
8529}
8530
Kevin Enderbyaefb0032014-12-24 00:16:51 +00008531static void Print_xmm_reg(MachO::xmm_reg_t &r) {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008532 uint32_t f;
8533 outs() << "\t xmm_reg ";
8534 for (f = 0; f < 16; f++)
8535 outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " ";
8536 outs() << "\n";
8537}
8538
8539static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) {
8540 outs() << "\t fpu_reserved[0] " << fpu.fpu_reserved[0];
8541 outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n";
8542 outs() << "\t control: invalid " << fpu.fpu_fcw.invalid;
8543 outs() << " denorm " << fpu.fpu_fcw.denorm;
8544 outs() << " zdiv " << fpu.fpu_fcw.zdiv;
8545 outs() << " ovrfl " << fpu.fpu_fcw.ovrfl;
8546 outs() << " undfl " << fpu.fpu_fcw.undfl;
8547 outs() << " precis " << fpu.fpu_fcw.precis << "\n";
8548 outs() << "\t\t pc ";
8549 if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B)
8550 outs() << "FP_PREC_24B ";
8551 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B)
8552 outs() << "FP_PREC_53B ";
8553 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B)
8554 outs() << "FP_PREC_64B ";
8555 else
8556 outs() << fpu.fpu_fcw.pc << " ";
8557 outs() << "rc ";
8558 if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR)
8559 outs() << "FP_RND_NEAR ";
8560 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN)
8561 outs() << "FP_RND_DOWN ";
8562 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP)
8563 outs() << "FP_RND_UP ";
8564 else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008565 outs() << "FP_CHOP ";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008566 outs() << "\n";
8567 outs() << "\t status: invalid " << fpu.fpu_fsw.invalid;
8568 outs() << " denorm " << fpu.fpu_fsw.denorm;
8569 outs() << " zdiv " << fpu.fpu_fsw.zdiv;
8570 outs() << " ovrfl " << fpu.fpu_fsw.ovrfl;
8571 outs() << " undfl " << fpu.fpu_fsw.undfl;
8572 outs() << " precis " << fpu.fpu_fsw.precis;
8573 outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n";
8574 outs() << "\t errsumm " << fpu.fpu_fsw.errsumm;
8575 outs() << " c0 " << fpu.fpu_fsw.c0;
8576 outs() << " c1 " << fpu.fpu_fsw.c1;
8577 outs() << " c2 " << fpu.fpu_fsw.c2;
8578 outs() << " tos " << fpu.fpu_fsw.tos;
8579 outs() << " c3 " << fpu.fpu_fsw.c3;
8580 outs() << " busy " << fpu.fpu_fsw.busy << "\n";
8581 outs() << "\t fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw);
8582 outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1);
8583 outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop);
8584 outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n";
8585 outs() << "\t fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs);
8586 outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2);
8587 outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp);
8588 outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n";
8589 outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3);
8590 outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr);
8591 outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask);
8592 outs() << "\n";
8593 outs() << "\t fpu_stmm0:\n";
8594 Print_mmst_reg(fpu.fpu_stmm0);
8595 outs() << "\t fpu_stmm1:\n";
8596 Print_mmst_reg(fpu.fpu_stmm1);
8597 outs() << "\t fpu_stmm2:\n";
8598 Print_mmst_reg(fpu.fpu_stmm2);
8599 outs() << "\t fpu_stmm3:\n";
8600 Print_mmst_reg(fpu.fpu_stmm3);
8601 outs() << "\t fpu_stmm4:\n";
8602 Print_mmst_reg(fpu.fpu_stmm4);
8603 outs() << "\t fpu_stmm5:\n";
8604 Print_mmst_reg(fpu.fpu_stmm5);
8605 outs() << "\t fpu_stmm6:\n";
8606 Print_mmst_reg(fpu.fpu_stmm6);
8607 outs() << "\t fpu_stmm7:\n";
8608 Print_mmst_reg(fpu.fpu_stmm7);
8609 outs() << "\t fpu_xmm0:\n";
8610 Print_xmm_reg(fpu.fpu_xmm0);
8611 outs() << "\t fpu_xmm1:\n";
8612 Print_xmm_reg(fpu.fpu_xmm1);
8613 outs() << "\t fpu_xmm2:\n";
8614 Print_xmm_reg(fpu.fpu_xmm2);
8615 outs() << "\t fpu_xmm3:\n";
8616 Print_xmm_reg(fpu.fpu_xmm3);
8617 outs() << "\t fpu_xmm4:\n";
8618 Print_xmm_reg(fpu.fpu_xmm4);
8619 outs() << "\t fpu_xmm5:\n";
8620 Print_xmm_reg(fpu.fpu_xmm5);
8621 outs() << "\t fpu_xmm6:\n";
8622 Print_xmm_reg(fpu.fpu_xmm6);
8623 outs() << "\t fpu_xmm7:\n";
8624 Print_xmm_reg(fpu.fpu_xmm7);
8625 outs() << "\t fpu_xmm8:\n";
8626 Print_xmm_reg(fpu.fpu_xmm8);
8627 outs() << "\t fpu_xmm9:\n";
8628 Print_xmm_reg(fpu.fpu_xmm9);
8629 outs() << "\t fpu_xmm10:\n";
8630 Print_xmm_reg(fpu.fpu_xmm10);
8631 outs() << "\t fpu_xmm11:\n";
8632 Print_xmm_reg(fpu.fpu_xmm11);
8633 outs() << "\t fpu_xmm12:\n";
8634 Print_xmm_reg(fpu.fpu_xmm12);
8635 outs() << "\t fpu_xmm13:\n";
8636 Print_xmm_reg(fpu.fpu_xmm13);
8637 outs() << "\t fpu_xmm14:\n";
8638 Print_xmm_reg(fpu.fpu_xmm14);
8639 outs() << "\t fpu_xmm15:\n";
8640 Print_xmm_reg(fpu.fpu_xmm15);
8641 outs() << "\t fpu_rsrv4:\n";
8642 for (uint32_t f = 0; f < 6; f++) {
8643 outs() << "\t ";
8644 for (uint32_t g = 0; g < 16; g++)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008645 outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f * g]) << " ";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008646 outs() << "\n";
8647 }
8648 outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1);
8649 outs() << "\n";
8650}
8651
8652static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) {
8653 outs() << "\t trapno " << format("0x%08" PRIx32, exc64.trapno);
8654 outs() << " err " << format("0x%08" PRIx32, exc64.err);
8655 outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n";
8656}
8657
8658static void PrintThreadCommand(MachO::thread_command t, const char *Ptr,
8659 bool isLittleEndian, uint32_t cputype) {
8660 if (t.cmd == MachO::LC_THREAD)
8661 outs() << " cmd LC_THREAD\n";
8662 else if (t.cmd == MachO::LC_UNIXTHREAD)
8663 outs() << " cmd LC_UNIXTHREAD\n";
8664 else
8665 outs() << " cmd " << t.cmd << " (unknown)\n";
8666 outs() << " cmdsize " << t.cmdsize;
8667 if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t))
8668 outs() << " Incorrect size\n";
8669 else
8670 outs() << "\n";
8671
8672 const char *begin = Ptr + sizeof(struct MachO::thread_command);
8673 const char *end = Ptr + t.cmdsize;
8674 uint32_t flavor, count, left;
8675 if (cputype == MachO::CPU_TYPE_X86_64) {
8676 while (begin < end) {
8677 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8678 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8679 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008680 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008681 flavor = 0;
8682 begin = end;
8683 }
8684 if (isLittleEndian != sys::IsLittleEndianHost)
8685 sys::swapByteOrder(flavor);
8686 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8687 memcpy((char *)&count, begin, sizeof(uint32_t));
8688 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008689 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008690 count = 0;
8691 begin = end;
8692 }
8693 if (isLittleEndian != sys::IsLittleEndianHost)
8694 sys::swapByteOrder(count);
8695 if (flavor == MachO::x86_THREAD_STATE64) {
8696 outs() << " flavor x86_THREAD_STATE64\n";
8697 if (count == MachO::x86_THREAD_STATE64_COUNT)
8698 outs() << " count x86_THREAD_STATE64_COUNT\n";
8699 else
8700 outs() << " count " << count
8701 << " (not x86_THREAD_STATE64_COUNT)\n";
8702 MachO::x86_thread_state64_t cpu64;
8703 left = end - begin;
8704 if (left >= sizeof(MachO::x86_thread_state64_t)) {
8705 memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t));
8706 begin += sizeof(MachO::x86_thread_state64_t);
8707 } else {
8708 memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t));
8709 memcpy(&cpu64, begin, left);
8710 begin += left;
8711 }
8712 if (isLittleEndian != sys::IsLittleEndianHost)
8713 swapStruct(cpu64);
8714 Print_x86_thread_state64_t(cpu64);
8715 } else if (flavor == MachO::x86_THREAD_STATE) {
8716 outs() << " flavor x86_THREAD_STATE\n";
8717 if (count == MachO::x86_THREAD_STATE_COUNT)
8718 outs() << " count x86_THREAD_STATE_COUNT\n";
8719 else
8720 outs() << " count " << count
8721 << " (not x86_THREAD_STATE_COUNT)\n";
8722 struct MachO::x86_thread_state_t ts;
8723 left = end - begin;
8724 if (left >= sizeof(MachO::x86_thread_state_t)) {
8725 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
8726 begin += sizeof(MachO::x86_thread_state_t);
8727 } else {
8728 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
8729 memcpy(&ts, begin, left);
8730 begin += left;
8731 }
8732 if (isLittleEndian != sys::IsLittleEndianHost)
8733 swapStruct(ts);
8734 if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) {
8735 outs() << "\t tsh.flavor x86_THREAD_STATE64 ";
8736 if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT)
8737 outs() << "tsh.count x86_THREAD_STATE64_COUNT\n";
8738 else
8739 outs() << "tsh.count " << ts.tsh.count
8740 << " (not x86_THREAD_STATE64_COUNT\n";
8741 Print_x86_thread_state64_t(ts.uts.ts64);
8742 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008743 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
8744 << ts.tsh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008745 }
8746 } else if (flavor == MachO::x86_FLOAT_STATE) {
8747 outs() << " flavor x86_FLOAT_STATE\n";
8748 if (count == MachO::x86_FLOAT_STATE_COUNT)
8749 outs() << " count x86_FLOAT_STATE_COUNT\n";
8750 else
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008751 outs() << " count " << count << " (not x86_FLOAT_STATE_COUNT)\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008752 struct MachO::x86_float_state_t fs;
8753 left = end - begin;
8754 if (left >= sizeof(MachO::x86_float_state_t)) {
8755 memcpy(&fs, begin, sizeof(MachO::x86_float_state_t));
8756 begin += sizeof(MachO::x86_float_state_t);
8757 } else {
8758 memset(&fs, '\0', sizeof(MachO::x86_float_state_t));
8759 memcpy(&fs, begin, left);
8760 begin += left;
8761 }
8762 if (isLittleEndian != sys::IsLittleEndianHost)
8763 swapStruct(fs);
8764 if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) {
8765 outs() << "\t fsh.flavor x86_FLOAT_STATE64 ";
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008766 if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT)
Kevin Enderby48ef5342014-12-23 22:56:39 +00008767 outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n";
8768 else
8769 outs() << "fsh.count " << fs.fsh.count
8770 << " (not x86_FLOAT_STATE64_COUNT\n";
8771 Print_x86_float_state_t(fs.ufs.fs64);
8772 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008773 outs() << "\t fsh.flavor " << fs.fsh.flavor << " fsh.count "
8774 << fs.fsh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008775 }
8776 } else if (flavor == MachO::x86_EXCEPTION_STATE) {
8777 outs() << " flavor x86_EXCEPTION_STATE\n";
8778 if (count == MachO::x86_EXCEPTION_STATE_COUNT)
8779 outs() << " count x86_EXCEPTION_STATE_COUNT\n";
8780 else
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008781 outs() << " count " << count
Kevin Enderby48ef5342014-12-23 22:56:39 +00008782 << " (not x86_EXCEPTION_STATE_COUNT)\n";
8783 struct MachO::x86_exception_state_t es;
8784 left = end - begin;
8785 if (left >= sizeof(MachO::x86_exception_state_t)) {
8786 memcpy(&es, begin, sizeof(MachO::x86_exception_state_t));
8787 begin += sizeof(MachO::x86_exception_state_t);
8788 } else {
8789 memset(&es, '\0', sizeof(MachO::x86_exception_state_t));
8790 memcpy(&es, begin, left);
8791 begin += left;
8792 }
8793 if (isLittleEndian != sys::IsLittleEndianHost)
8794 swapStruct(es);
8795 if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) {
8796 outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n";
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008797 if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT)
Kevin Enderby48ef5342014-12-23 22:56:39 +00008798 outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n";
8799 else
8800 outs() << "\t esh.count " << es.esh.count
8801 << " (not x86_EXCEPTION_STATE64_COUNT\n";
8802 Print_x86_exception_state_t(es.ues.es64);
8803 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008804 outs() << "\t esh.flavor " << es.esh.flavor << " esh.count "
8805 << es.esh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008806 }
8807 } else {
8808 outs() << " flavor " << flavor << " (unknown)\n";
8809 outs() << " count " << count << "\n";
8810 outs() << " state (unknown)\n";
8811 begin += count * sizeof(uint32_t);
8812 }
8813 }
8814 } else {
8815 while (begin < end) {
8816 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8817 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8818 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008819 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008820 flavor = 0;
8821 begin = end;
8822 }
8823 if (isLittleEndian != sys::IsLittleEndianHost)
8824 sys::swapByteOrder(flavor);
8825 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8826 memcpy((char *)&count, begin, sizeof(uint32_t));
8827 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008828 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008829 count = 0;
8830 begin = end;
8831 }
8832 if (isLittleEndian != sys::IsLittleEndianHost)
8833 sys::swapByteOrder(count);
8834 outs() << " flavor " << flavor << "\n";
8835 outs() << " count " << count << "\n";
8836 outs() << " state (Unknown cputype/cpusubtype)\n";
8837 begin += count * sizeof(uint32_t);
8838 }
8839 }
8840}
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008841
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008842static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) {
8843 if (dl.cmd == MachO::LC_ID_DYLIB)
8844 outs() << " cmd LC_ID_DYLIB\n";
8845 else if (dl.cmd == MachO::LC_LOAD_DYLIB)
8846 outs() << " cmd LC_LOAD_DYLIB\n";
8847 else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB)
8848 outs() << " cmd LC_LOAD_WEAK_DYLIB\n";
8849 else if (dl.cmd == MachO::LC_REEXPORT_DYLIB)
8850 outs() << " cmd LC_REEXPORT_DYLIB\n";
8851 else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB)
8852 outs() << " cmd LC_LAZY_LOAD_DYLIB\n";
8853 else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
8854 outs() << " cmd LC_LOAD_UPWARD_DYLIB\n";
8855 else
8856 outs() << " cmd " << dl.cmd << " (unknown)\n";
8857 outs() << " cmdsize " << dl.cmdsize;
8858 if (dl.cmdsize < sizeof(struct MachO::dylib_command))
8859 outs() << " Incorrect size\n";
8860 else
8861 outs() << "\n";
8862 if (dl.dylib.name < dl.cmdsize) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008863 const char *P = (const char *)(Ptr) + dl.dylib.name;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008864 outs() << " name " << P << " (offset " << dl.dylib.name << ")\n";
8865 } else {
8866 outs() << " name ?(bad offset " << dl.dylib.name << ")\n";
8867 }
8868 outs() << " time stamp " << dl.dylib.timestamp << " ";
8869 time_t t = dl.dylib.timestamp;
8870 outs() << ctime(&t);
8871 outs() << " current version ";
8872 if (dl.dylib.current_version == 0xffffffff)
8873 outs() << "n/a\n";
8874 else
8875 outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "."
8876 << ((dl.dylib.current_version >> 8) & 0xff) << "."
8877 << (dl.dylib.current_version & 0xff) << "\n";
8878 outs() << "compatibility version ";
8879 if (dl.dylib.compatibility_version == 0xffffffff)
8880 outs() << "n/a\n";
8881 else
8882 outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
8883 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
8884 << (dl.dylib.compatibility_version & 0xff) << "\n";
8885}
8886
8887static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
8888 uint32_t object_size) {
8889 if (ld.cmd == MachO::LC_CODE_SIGNATURE)
Kevin Enderby1be37a32016-04-28 21:07:20 +00008890 outs() << " cmd LC_CODE_SIGNATURE\n";
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008891 else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO)
8892 outs() << " cmd LC_SEGMENT_SPLIT_INFO\n";
8893 else if (ld.cmd == MachO::LC_FUNCTION_STARTS)
8894 outs() << " cmd LC_FUNCTION_STARTS\n";
8895 else if (ld.cmd == MachO::LC_DATA_IN_CODE)
8896 outs() << " cmd LC_DATA_IN_CODE\n";
8897 else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS)
8898 outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n";
8899 else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT)
8900 outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n";
8901 else
8902 outs() << " cmd " << ld.cmd << " (?)\n";
8903 outs() << " cmdsize " << ld.cmdsize;
8904 if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command))
8905 outs() << " Incorrect size\n";
8906 else
8907 outs() << "\n";
8908 outs() << " dataoff " << ld.dataoff;
8909 if (ld.dataoff > object_size)
8910 outs() << " (past end of file)\n";
8911 else
8912 outs() << "\n";
8913 outs() << " datasize " << ld.datasize;
8914 uint64_t big_size = ld.dataoff;
8915 big_size += ld.datasize;
8916 if (big_size > object_size)
8917 outs() << " (past end of file)\n";
8918 else
8919 outs() << "\n";
8920}
8921
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00008922static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t filetype,
8923 uint32_t cputype, bool verbose) {
Kevin Enderby956366c2014-08-29 22:30:52 +00008924 StringRef Buf = Obj->getData();
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00008925 unsigned Index = 0;
8926 for (const auto &Command : Obj->load_commands()) {
8927 outs() << "Load command " << Index++ << "\n";
Kevin Enderby956366c2014-08-29 22:30:52 +00008928 if (Command.C.cmd == MachO::LC_SEGMENT) {
8929 MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
8930 const char *sg_segname = SLC.segname;
8931 PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr,
8932 SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot,
8933 SLC.initprot, SLC.nsects, SLC.flags, Buf.size(),
8934 verbose);
8935 for (unsigned j = 0; j < SLC.nsects; j++) {
Kevin Enderbyc9713382014-12-16 01:14:45 +00008936 MachO::section S = Obj->getSection(Command, j);
Kevin Enderby956366c2014-08-29 22:30:52 +00008937 PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align,
8938 S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2,
8939 SLC.cmd, sg_segname, filetype, Buf.size(), verbose);
8940 }
8941 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
8942 MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command);
8943 const char *sg_segname = SLC_64.segname;
8944 PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname,
8945 SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff,
8946 SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot,
8947 SLC_64.nsects, SLC_64.flags, Buf.size(), verbose);
8948 for (unsigned j = 0; j < SLC_64.nsects; j++) {
8949 MachO::section_64 S_64 = Obj->getSection64(Command, j);
8950 PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size,
8951 S_64.offset, S_64.align, S_64.reloff, S_64.nreloc,
8952 S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd,
8953 sg_segname, filetype, Buf.size(), verbose);
8954 }
8955 } else if (Command.C.cmd == MachO::LC_SYMTAB) {
8956 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
David Majnemer73cc6ff2014-11-13 19:48:56 +00008957 PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size());
Kevin Enderby956366c2014-08-29 22:30:52 +00008958 } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
8959 MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
8960 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
David Majnemer73cc6ff2014-11-13 19:48:56 +00008961 PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(),
8962 Obj->is64Bit());
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008963 } else if (Command.C.cmd == MachO::LC_DYLD_INFO ||
8964 Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
8965 MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command);
8966 PrintDyldInfoLoadCommand(DyldInfo, Buf.size());
8967 } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER ||
8968 Command.C.cmd == MachO::LC_ID_DYLINKER ||
8969 Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
8970 MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command);
8971 PrintDyldLoadCommand(Dyld, Command.Ptr);
8972 } else if (Command.C.cmd == MachO::LC_UUID) {
8973 MachO::uuid_command Uuid = Obj->getUuidCommand(Command);
8974 PrintUuidLoadCommand(Uuid);
Jean-Daniel Dupas00cc1f52014-12-04 07:37:02 +00008975 } else if (Command.C.cmd == MachO::LC_RPATH) {
8976 MachO::rpath_command Rpath = Obj->getRpathCommand(Command);
8977 PrintRpathLoadCommand(Rpath, Command.Ptr);
Kevin Enderby1ff0ecc2014-12-16 21:48:27 +00008978 } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX ||
Tim Northoverbfbfb122015-11-02 21:26:58 +00008979 Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS ||
8980 Command.C.cmd == MachO::LC_VERSION_MIN_TVOS ||
8981 Command.C.cmd == MachO::LC_VERSION_MIN_WATCHOS) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008982 MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command);
8983 PrintVersionMinLoadCommand(Vd);
8984 } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) {
8985 MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command);
8986 PrintSourceVersionCommand(Sd);
8987 } else if (Command.C.cmd == MachO::LC_MAIN) {
8988 MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command);
8989 PrintEntryPointCommand(Ep);
Kevin Enderby0804f4672014-12-16 23:25:52 +00008990 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008991 MachO::encryption_info_command Ei =
8992 Obj->getEncryptionInfoCommand(Command);
Kevin Enderby0804f4672014-12-16 23:25:52 +00008993 PrintEncryptionInfoCommand(Ei, Buf.size());
Kevin Enderby57538292014-12-17 01:01:30 +00008994 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008995 MachO::encryption_info_command_64 Ei =
8996 Obj->getEncryptionInfoCommand64(Command);
Kevin Enderby57538292014-12-17 01:01:30 +00008997 PrintEncryptionInfoCommand64(Ei, Buf.size());
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00008998 } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008999 MachO::linker_option_command Lo =
9000 Obj->getLinkerOptionLoadCommand(Command);
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00009001 PrintLinkerOptionCommand(Lo, Command.Ptr);
Kevin Enderbyb4b79312014-12-18 19:24:35 +00009002 } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) {
9003 MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command);
9004 PrintSubFrameworkCommand(Sf, Command.Ptr);
Kevin Enderbya2bd8d92014-12-18 23:13:26 +00009005 } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) {
9006 MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command);
9007 PrintSubUmbrellaCommand(Sf, Command.Ptr);
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00009008 } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) {
9009 MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command);
9010 PrintSubLibraryCommand(Sl, Command.Ptr);
Kevin Enderby186eac32014-12-19 21:06:24 +00009011 } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) {
9012 MachO::sub_client_command Sc = Obj->getSubClientCommand(Command);
9013 PrintSubClientCommand(Sc, Command.Ptr);
Kevin Enderby52e4ce42014-12-19 22:25:22 +00009014 } else if (Command.C.cmd == MachO::LC_ROUTINES) {
9015 MachO::routines_command Rc = Obj->getRoutinesCommand(Command);
9016 PrintRoutinesCommand(Rc);
9017 } else if (Command.C.cmd == MachO::LC_ROUTINES_64) {
9018 MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command);
9019 PrintRoutinesCommand64(Rc);
Kevin Enderby48ef5342014-12-23 22:56:39 +00009020 } else if (Command.C.cmd == MachO::LC_THREAD ||
9021 Command.C.cmd == MachO::LC_UNIXTHREAD) {
9022 MachO::thread_command Tc = Obj->getThreadCommand(Command);
9023 PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype);
Nick Kledzik15558912014-10-16 18:58:20 +00009024 } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
9025 Command.C.cmd == MachO::LC_ID_DYLIB ||
9026 Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
9027 Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
9028 Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
9029 Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00009030 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
9031 PrintDylibCommand(Dl, Command.Ptr);
9032 } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE ||
9033 Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO ||
9034 Command.C.cmd == MachO::LC_FUNCTION_STARTS ||
9035 Command.C.cmd == MachO::LC_DATA_IN_CODE ||
9036 Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS ||
9037 Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
9038 MachO::linkedit_data_command Ld =
9039 Obj->getLinkeditDataLoadCommand(Command);
9040 PrintLinkEditDataCommand(Ld, Buf.size());
Kevin Enderby956366c2014-08-29 22:30:52 +00009041 } else {
9042 outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd)
9043 << ")\n";
9044 outs() << " cmdsize " << Command.C.cmdsize << "\n";
9045 // TODO: get and print the raw bytes of the load command.
9046 }
9047 // TODO: print all the other kinds of load commands.
Kevin Enderby956366c2014-08-29 22:30:52 +00009048 }
9049}
9050
Kevin Enderby0ae163f2016-01-13 00:25:36 +00009051static void PrintMachHeader(const MachOObjectFile *Obj, bool verbose) {
Kevin Enderbyb76d3862014-08-22 20:35:18 +00009052 if (Obj->is64Bit()) {
9053 MachO::mach_header_64 H_64;
9054 H_64 = Obj->getHeader64();
9055 PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
9056 H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
9057 } else {
9058 MachO::mach_header H;
9059 H = Obj->getHeader();
9060 PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
9061 H.sizeofcmds, H.flags, verbose);
9062 }
9063}
9064
9065void llvm::printMachOFileHeader(const object::ObjectFile *Obj) {
9066 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
Kevin Enderby0ae163f2016-01-13 00:25:36 +00009067 PrintMachHeader(file, !NonVerbose);
9068}
9069
9070void llvm::printMachOLoadCommands(const object::ObjectFile *Obj) {
9071 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
Kevin Enderby956366c2014-08-29 22:30:52 +00009072 uint32_t filetype = 0;
9073 uint32_t cputype = 0;
Kevin Enderby0ae163f2016-01-13 00:25:36 +00009074 if (file->is64Bit()) {
9075 MachO::mach_header_64 H_64;
9076 H_64 = file->getHeader64();
9077 filetype = H_64.filetype;
9078 cputype = H_64.cputype;
9079 } else {
9080 MachO::mach_header H;
9081 H = file->getHeader();
9082 filetype = H.filetype;
9083 cputype = H.cputype;
9084 }
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00009085 PrintLoadCommands(file, filetype, cputype, !NonVerbose);
Kevin Enderbyb76d3862014-08-22 20:35:18 +00009086}
Nick Kledzikd04bc352014-08-30 00:20:14 +00009087
9088//===----------------------------------------------------------------------===//
9089// export trie dumping
9090//===----------------------------------------------------------------------===//
9091
9092void llvm::printMachOExportsTrie(const object::MachOObjectFile *Obj) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009093 for (const llvm::object::ExportEntry &Entry : Obj->exports()) {
9094 uint64_t Flags = Entry.flags();
Nick Kledzikd04bc352014-08-30 00:20:14 +00009095 bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
9096 bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
9097 bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
9098 MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL);
9099 bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
9100 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
9101 bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
9102 if (ReExport)
9103 outs() << "[re-export] ";
9104 else
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009105 outs() << format("0x%08llX ",
9106 Entry.address()); // FIXME:add in base address
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009107 outs() << Entry.name();
Nick Kledzikd04bc352014-08-30 00:20:14 +00009108 if (WeakDef || ThreadLocal || Resolver || Abs) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009109 bool NeedsComma = false;
Nick Kledzik1d1ac4b2014-09-03 01:12:52 +00009110 outs() << " [";
Nick Kledzikd04bc352014-08-30 00:20:14 +00009111 if (WeakDef) {
9112 outs() << "weak_def";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009113 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00009114 }
9115 if (ThreadLocal) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009116 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00009117 outs() << ", ";
9118 outs() << "per-thread";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009119 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00009120 }
9121 if (Abs) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009122 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00009123 outs() << ", ";
9124 outs() << "absolute";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009125 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00009126 }
9127 if (Resolver) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009128 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00009129 outs() << ", ";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009130 outs() << format("resolver=0x%08llX", Entry.other());
9131 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00009132 }
9133 outs() << "]";
9134 }
9135 if (ReExport) {
9136 StringRef DylibName = "unknown";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009137 int Ordinal = Entry.other() - 1;
9138 Obj->getLibraryShortNameByIndex(Ordinal, DylibName);
9139 if (Entry.otherName().empty())
Nick Kledzikd04bc352014-08-30 00:20:14 +00009140 outs() << " (from " << DylibName << ")";
9141 else
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009142 outs() << " (" << Entry.otherName() << " from " << DylibName << ")";
Nick Kledzikd04bc352014-08-30 00:20:14 +00009143 }
9144 outs() << "\n";
9145 }
9146}
Nick Kledzikac431442014-09-12 21:34:15 +00009147
Nick Kledzikac431442014-09-12 21:34:15 +00009148//===----------------------------------------------------------------------===//
9149// rebase table dumping
9150//===----------------------------------------------------------------------===//
9151
9152namespace {
9153class SegInfo {
9154public:
9155 SegInfo(const object::MachOObjectFile *Obj);
9156
9157 StringRef segmentName(uint32_t SegIndex);
9158 StringRef sectionName(uint32_t SegIndex, uint64_t SegOffset);
9159 uint64_t address(uint32_t SegIndex, uint64_t SegOffset);
Kevin Enderbyaf7c9d02015-10-09 16:48:44 +00009160 bool isValidSegIndexAndOffset(uint32_t SegIndex, uint64_t SegOffset);
Nick Kledzikac431442014-09-12 21:34:15 +00009161
9162private:
9163 struct SectionInfo {
9164 uint64_t Address;
9165 uint64_t Size;
9166 StringRef SectionName;
9167 StringRef SegmentName;
9168 uint64_t OffsetInSegment;
9169 uint64_t SegmentStartAddress;
9170 uint32_t SegmentIndex;
9171 };
9172 const SectionInfo &findSection(uint32_t SegIndex, uint64_t SegOffset);
9173 SmallVector<SectionInfo, 32> Sections;
9174};
9175}
9176
9177SegInfo::SegInfo(const object::MachOObjectFile *Obj) {
9178 // Build table of sections so segIndex/offset pairs can be translated.
Nick Kledzik56ebef42014-09-16 01:41:51 +00009179 uint32_t CurSegIndex = Obj->hasPageZeroSegment() ? 1 : 0;
Nick Kledzikac431442014-09-12 21:34:15 +00009180 StringRef CurSegName;
9181 uint64_t CurSegAddress;
9182 for (const SectionRef &Section : Obj->sections()) {
9183 SectionInfo Info;
Davide Italianoccd53fe2015-08-05 07:18:31 +00009184 error(Section.getName(Info.SectionName));
Rafael Espindola80291272014-10-08 15:28:58 +00009185 Info.Address = Section.getAddress();
9186 Info.Size = Section.getSize();
Nick Kledzikac431442014-09-12 21:34:15 +00009187 Info.SegmentName =
9188 Obj->getSectionFinalSegmentName(Section.getRawDataRefImpl());
9189 if (!Info.SegmentName.equals(CurSegName)) {
9190 ++CurSegIndex;
9191 CurSegName = Info.SegmentName;
9192 CurSegAddress = Info.Address;
9193 }
9194 Info.SegmentIndex = CurSegIndex - 1;
9195 Info.OffsetInSegment = Info.Address - CurSegAddress;
9196 Info.SegmentStartAddress = CurSegAddress;
9197 Sections.push_back(Info);
9198 }
9199}
9200
9201StringRef SegInfo::segmentName(uint32_t SegIndex) {
9202 for (const SectionInfo &SI : Sections) {
9203 if (SI.SegmentIndex == SegIndex)
9204 return SI.SegmentName;
9205 }
9206 llvm_unreachable("invalid segIndex");
9207}
9208
Kevin Enderbyaf7c9d02015-10-09 16:48:44 +00009209bool SegInfo::isValidSegIndexAndOffset(uint32_t SegIndex,
9210 uint64_t OffsetInSeg) {
9211 for (const SectionInfo &SI : Sections) {
9212 if (SI.SegmentIndex != SegIndex)
9213 continue;
9214 if (SI.OffsetInSegment > OffsetInSeg)
9215 continue;
9216 if (OffsetInSeg >= (SI.OffsetInSegment + SI.Size))
9217 continue;
9218 return true;
9219 }
9220 return false;
9221}
9222
Nick Kledzikac431442014-09-12 21:34:15 +00009223const SegInfo::SectionInfo &SegInfo::findSection(uint32_t SegIndex,
9224 uint64_t OffsetInSeg) {
9225 for (const SectionInfo &SI : Sections) {
9226 if (SI.SegmentIndex != SegIndex)
9227 continue;
9228 if (SI.OffsetInSegment > OffsetInSeg)
9229 continue;
9230 if (OffsetInSeg >= (SI.OffsetInSegment + SI.Size))
9231 continue;
9232 return SI;
9233 }
9234 llvm_unreachable("segIndex and offset not in any section");
9235}
9236
9237StringRef SegInfo::sectionName(uint32_t SegIndex, uint64_t OffsetInSeg) {
9238 return findSection(SegIndex, OffsetInSeg).SectionName;
9239}
9240
9241uint64_t SegInfo::address(uint32_t SegIndex, uint64_t OffsetInSeg) {
9242 const SectionInfo &SI = findSection(SegIndex, OffsetInSeg);
9243 return SI.SegmentStartAddress + OffsetInSeg;
9244}
9245
9246void llvm::printMachORebaseTable(const object::MachOObjectFile *Obj) {
9247 // Build table of sections so names can used in final output.
9248 SegInfo sectionTable(Obj);
9249
9250 outs() << "segment section address type\n";
9251 for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable()) {
9252 uint32_t SegIndex = Entry.segmentIndex();
9253 uint64_t OffsetInSeg = Entry.segmentOffset();
9254 StringRef SegmentName = sectionTable.segmentName(SegIndex);
9255 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
9256 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
9257
9258 // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009259 outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n",
9260 SegmentName.str().c_str(), SectionName.str().c_str(),
9261 Address, Entry.typeName().str().c_str());
Nick Kledzikac431442014-09-12 21:34:15 +00009262 }
9263}
Nick Kledzik56ebef42014-09-16 01:41:51 +00009264
9265static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
9266 StringRef DylibName;
9267 switch (Ordinal) {
9268 case MachO::BIND_SPECIAL_DYLIB_SELF:
9269 return "this-image";
9270 case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
9271 return "main-executable";
9272 case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
9273 return "flat-namespace";
9274 default:
Nick Kledzikabd29872014-09-16 22:03:13 +00009275 if (Ordinal > 0) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009276 std::error_code EC =
9277 Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName);
Nick Kledzikabd29872014-09-16 22:03:13 +00009278 if (EC)
Nick Kledzik51d2c2b2014-10-14 23:29:38 +00009279 return "<<bad library ordinal>>";
Nick Kledzikabd29872014-09-16 22:03:13 +00009280 return DylibName;
9281 }
Nick Kledzik56ebef42014-09-16 01:41:51 +00009282 }
Nick Kledzikabd29872014-09-16 22:03:13 +00009283 return "<<unknown special ordinal>>";
Nick Kledzik56ebef42014-09-16 01:41:51 +00009284}
9285
9286//===----------------------------------------------------------------------===//
9287// bind table dumping
9288//===----------------------------------------------------------------------===//
9289
9290void llvm::printMachOBindTable(const object::MachOObjectFile *Obj) {
9291 // Build table of sections so names can used in final output.
9292 SegInfo sectionTable(Obj);
9293
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009294 outs() << "segment section address type "
9295 "addend dylib symbol\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00009296 for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable()) {
9297 uint32_t SegIndex = Entry.segmentIndex();
9298 uint64_t OffsetInSeg = Entry.segmentOffset();
9299 StringRef SegmentName = sectionTable.segmentName(SegIndex);
9300 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
9301 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
9302
9303 // Table lines look like:
9304 // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009305 StringRef Attr;
Nick Kledzik56ebef42014-09-16 01:41:51 +00009306 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT)
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009307 Attr = " (weak_import)";
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009308 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009309 << left_justify(SectionName, 18) << " "
9310 << format_hex(Address, 10, true) << " "
9311 << left_justify(Entry.typeName(), 8) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009312 << format_decimal(Entry.addend(), 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009313 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009314 << Entry.symbolName() << Attr << "\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00009315 }
9316}
9317
9318//===----------------------------------------------------------------------===//
9319// lazy bind table dumping
9320//===----------------------------------------------------------------------===//
9321
9322void llvm::printMachOLazyBindTable(const object::MachOObjectFile *Obj) {
9323 // Build table of sections so names can used in final output.
9324 SegInfo sectionTable(Obj);
9325
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009326 outs() << "segment section address "
9327 "dylib symbol\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00009328 for (const llvm::object::MachOBindEntry &Entry : Obj->lazyBindTable()) {
9329 uint32_t SegIndex = Entry.segmentIndex();
9330 uint64_t OffsetInSeg = Entry.segmentOffset();
9331 StringRef SegmentName = sectionTable.segmentName(SegIndex);
9332 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
9333 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
9334
9335 // Table lines look like:
9336 // __DATA __got 0x00012010 libSystem ___stack_chk_guard
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009337 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009338 << left_justify(SectionName, 18) << " "
9339 << format_hex(Address, 10, true) << " "
9340 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
Nick Kledzik56ebef42014-09-16 01:41:51 +00009341 << Entry.symbolName() << "\n";
9342 }
9343}
9344
Nick Kledzik56ebef42014-09-16 01:41:51 +00009345//===----------------------------------------------------------------------===//
9346// weak bind table dumping
9347//===----------------------------------------------------------------------===//
9348
9349void llvm::printMachOWeakBindTable(const object::MachOObjectFile *Obj) {
9350 // Build table of sections so names can used in final output.
9351 SegInfo sectionTable(Obj);
9352
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009353 outs() << "segment section address "
9354 "type addend symbol\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00009355 for (const llvm::object::MachOBindEntry &Entry : Obj->weakBindTable()) {
9356 // Strong symbols don't have a location to update.
9357 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009358 outs() << " strong "
Nick Kledzik56ebef42014-09-16 01:41:51 +00009359 << Entry.symbolName() << "\n";
9360 continue;
9361 }
9362 uint32_t SegIndex = Entry.segmentIndex();
9363 uint64_t OffsetInSeg = Entry.segmentOffset();
9364 StringRef SegmentName = sectionTable.segmentName(SegIndex);
9365 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
9366 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
9367
9368 // Table lines look like:
9369 // __DATA __data 0x00001000 pointer 0 _foo
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009370 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009371 << left_justify(SectionName, 18) << " "
9372 << format_hex(Address, 10, true) << " "
9373 << left_justify(Entry.typeName(), 8) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009374 << format_decimal(Entry.addend(), 8) << " " << Entry.symbolName()
9375 << "\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00009376 }
9377}
9378
Kevin Enderby6f326ce2014-10-23 19:37:31 +00009379// get_dyld_bind_info_symbolname() is used for disassembly and passed an
9380// address, ReferenceValue, in the Mach-O file and looks in the dyld bind
9381// information for that address. If the address is found its binding symbol
9382// name is returned. If not nullptr is returned.
9383static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
9384 struct DisassembleInfo *info) {
Kevin Enderby078be602014-10-23 19:53:12 +00009385 if (info->bindtable == nullptr) {
9386 info->bindtable = new (BindTable);
Kevin Enderby6f326ce2014-10-23 19:37:31 +00009387 SegInfo sectionTable(info->O);
9388 for (const llvm::object::MachOBindEntry &Entry : info->O->bindTable()) {
9389 uint32_t SegIndex = Entry.segmentIndex();
9390 uint64_t OffsetInSeg = Entry.segmentOffset();
Kevin Enderbyaf7c9d02015-10-09 16:48:44 +00009391 if (!sectionTable.isValidSegIndexAndOffset(SegIndex, OffsetInSeg))
9392 continue;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00009393 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
9394 const char *SymbolName = nullptr;
9395 StringRef name = Entry.symbolName();
9396 if (!name.empty())
9397 SymbolName = name.data();
Kevin Enderby078be602014-10-23 19:53:12 +00009398 info->bindtable->push_back(std::make_pair(Address, SymbolName));
Kevin Enderby6f326ce2014-10-23 19:37:31 +00009399 }
9400 }
Kevin Enderby078be602014-10-23 19:53:12 +00009401 for (bind_table_iterator BI = info->bindtable->begin(),
9402 BE = info->bindtable->end();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00009403 BI != BE; ++BI) {
9404 uint64_t Address = BI->first;
9405 if (ReferenceValue == Address) {
9406 const char *SymbolName = BI->second;
9407 return SymbolName;
9408 }
9409 }
9410 return nullptr;
9411}