blob: 2bafe559987e0851adce2e3dd52bef6c917a9474 [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;
1269 PrintSymbolTable(MachOOF, ArchiveName);
1270 }
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";
1406 if (verbose)
1407 outs() << "fat_magic FAT_MAGIC\n";
1408 else
1409 outs() << "fat_magic " << format("0x%" PRIx32, MachO::FAT_MAGIC) << "\n";
1410
1411 uint32_t nfat_arch = UB->getNumberOfObjects();
1412 StringRef Buf = UB->getData();
1413 uint64_t size = Buf.size();
1414 uint64_t big_size = sizeof(struct MachO::fat_header) +
1415 nfat_arch * sizeof(struct MachO::fat_arch);
1416 outs() << "nfat_arch " << UB->getNumberOfObjects();
1417 if (nfat_arch == 0)
1418 outs() << " (malformed, contains zero architecture types)\n";
1419 else if (big_size > size)
1420 outs() << " (malformed, architectures past end of file)\n";
1421 else
1422 outs() << "\n";
1423
1424 for (uint32_t i = 0; i < nfat_arch; ++i) {
1425 MachOUniversalBinary::ObjectForArch OFA(UB, i);
1426 uint32_t cputype = OFA.getCPUType();
1427 uint32_t cpusubtype = OFA.getCPUSubType();
1428 outs() << "architecture ";
1429 for (uint32_t j = 0; i != 0 && j <= i - 1; j++) {
1430 MachOUniversalBinary::ObjectForArch other_OFA(UB, j);
1431 uint32_t other_cputype = other_OFA.getCPUType();
1432 uint32_t other_cpusubtype = other_OFA.getCPUSubType();
Kevin Enderby0512bd72015-01-09 21:55:03 +00001433 if (cputype != 0 && cpusubtype != 0 && cputype == other_cputype &&
Kevin Enderby131d1772015-01-09 19:22:37 +00001434 (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) ==
Kevin Enderby0512bd72015-01-09 21:55:03 +00001435 (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK)) {
Kevin Enderby131d1772015-01-09 19:22:37 +00001436 outs() << "(illegal duplicate architecture) ";
1437 break;
Kevin Enderby0512bd72015-01-09 21:55:03 +00001438 }
Kevin Enderby131d1772015-01-09 19:22:37 +00001439 }
1440 if (verbose) {
1441 outs() << OFA.getArchTypeName() << "\n";
1442 printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1443 } else {
1444 outs() << i << "\n";
1445 outs() << " cputype " << cputype << "\n";
1446 outs() << " cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK)
1447 << "\n";
1448 }
1449 if (verbose &&
1450 (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64)
1451 outs() << " capabilities CPU_SUBTYPE_LIB64\n";
1452 else
1453 outs() << " capabilities "
1454 << format("0x%" PRIx32,
1455 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n";
1456 outs() << " offset " << OFA.getOffset();
1457 if (OFA.getOffset() > size)
1458 outs() << " (past end of file)";
1459 if (OFA.getOffset() % (1 << OFA.getAlign()) != 0)
1460 outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")";
1461 outs() << "\n";
1462 outs() << " size " << OFA.getSize();
1463 big_size = OFA.getOffset() + OFA.getSize();
1464 if (big_size > size)
1465 outs() << " (past end of file)";
1466 outs() << "\n";
1467 outs() << " align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign())
1468 << ")\n";
1469 }
1470}
1471
Rafael Espindola266b4fe2015-10-31 22:25:59 +00001472static void printArchiveChild(const Archive::Child &C, bool verbose,
Kevin Enderby13023a12015-01-15 23:19:11 +00001473 bool print_offset) {
1474 if (print_offset)
1475 outs() << C.getChildOffset() << "\t";
1476 sys::fs::perms Mode = C.getAccessMode();
1477 if (verbose) {
1478 // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
1479 // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
1480 outs() << "-";
Davide Italianobb9a6cc2015-09-07 20:47:03 +00001481 outs() << ((Mode & sys::fs::owner_read) ? "r" : "-");
1482 outs() << ((Mode & sys::fs::owner_write) ? "w" : "-");
1483 outs() << ((Mode & sys::fs::owner_exe) ? "x" : "-");
1484 outs() << ((Mode & sys::fs::group_read) ? "r" : "-");
1485 outs() << ((Mode & sys::fs::group_write) ? "w" : "-");
1486 outs() << ((Mode & sys::fs::group_exe) ? "x" : "-");
1487 outs() << ((Mode & sys::fs::others_read) ? "r" : "-");
1488 outs() << ((Mode & sys::fs::others_write) ? "w" : "-");
1489 outs() << ((Mode & sys::fs::others_exe) ? "x" : "-");
Kevin Enderby13023a12015-01-15 23:19:11 +00001490 } else {
1491 outs() << format("0%o ", Mode);
1492 }
1493
1494 unsigned UID = C.getUID();
1495 outs() << format("%3d/", UID);
1496 unsigned GID = C.getGID();
1497 outs() << format("%-3d ", GID);
Kevin Enderby7a969422015-11-05 19:24:56 +00001498 ErrorOr<uint64_t> Size = C.getRawSize();
1499 if (std::error_code EC = Size.getError())
1500 report_fatal_error(EC.message());
1501 outs() << format("%5" PRId64, Size.get()) << " ";
Kevin Enderby13023a12015-01-15 23:19:11 +00001502
1503 StringRef RawLastModified = C.getRawLastModified();
1504 if (verbose) {
1505 unsigned Seconds;
1506 if (RawLastModified.getAsInteger(10, Seconds))
1507 outs() << "(date: \"%s\" contains non-decimal chars) " << RawLastModified;
1508 else {
1509 // Since cime(3) returns a 26 character string of the form:
1510 // "Sun Sep 16 01:03:52 1973\n\0"
1511 // just print 24 characters.
1512 time_t t = Seconds;
1513 outs() << format("%.24s ", ctime(&t));
1514 }
1515 } else {
1516 outs() << RawLastModified << " ";
1517 }
1518
1519 if (verbose) {
1520 ErrorOr<StringRef> NameOrErr = C.getName();
1521 if (NameOrErr.getError()) {
1522 StringRef RawName = C.getRawName();
1523 outs() << RawName << "\n";
1524 } else {
1525 StringRef Name = NameOrErr.get();
1526 outs() << Name << "\n";
1527 }
1528 } else {
1529 StringRef RawName = C.getRawName();
1530 outs() << RawName << "\n";
1531 }
1532}
1533
1534static void printArchiveHeaders(Archive *A, bool verbose, bool print_offset) {
Rafael Espindola4a782fb2015-10-31 21:03:29 +00001535 for (Archive::child_iterator I = A->child_begin(false), E = A->child_end();
1536 I != E; ++I) {
Kevin Enderby7a969422015-11-05 19:24:56 +00001537 if (std::error_code EC = I->getError())
1538 report_fatal_error(EC.message());
1539 const Archive::Child &C = **I;
Kevin Enderby13023a12015-01-15 23:19:11 +00001540 printArchiveChild(C, verbose, print_offset);
1541 }
1542}
1543
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001544// ParseInputMachO() parses the named Mach-O file in Filename and handles the
1545// -arch flags selecting just those slices as specified by them and also parses
1546// archive files. Then for each individual Mach-O file ProcessMachO() is
1547// called to process the file based on the command line options.
1548void llvm::ParseInputMachO(StringRef Filename) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001549 // Check for -arch all and verifiy the -arch flags are valid.
1550 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1551 if (ArchFlags[i] == "all") {
1552 ArchAll = true;
1553 } else {
1554 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
1555 errs() << "llvm-objdump: Unknown architecture named '" + ArchFlags[i] +
1556 "'for the -arch option\n";
1557 return;
1558 }
1559 }
1560 }
1561
1562 // Attempt to open the binary.
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +00001563 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename);
1564 if (!BinaryOrErr)
1565 report_error(Filename, BinaryOrErr.takeError());
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001566 Binary &Bin = *BinaryOrErr.get().getBinary();
Kevin Enderby3f0ffab2014-12-03 22:29:40 +00001567
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001568 if (Archive *A = dyn_cast<Archive>(&Bin)) {
1569 outs() << "Archive : " << Filename << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001570 if (ArchiveHeaders)
Kevin Enderby8972e482015-04-30 20:30:42 +00001571 printArchiveHeaders(A, !NonVerbose, ArchiveMemberOffsets);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001572 for (Archive::child_iterator I = A->child_begin(), E = A->child_end();
1573 I != E; ++I) {
Davide Italianob13edeb2015-12-08 02:45:59 +00001574 if (std::error_code EC = I->getError())
1575 report_error(Filename, EC);
Kevin Enderby7a969422015-11-05 19:24:56 +00001576 auto &C = I->get();
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001577 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1578 if (!ChildOrErr) {
1579 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
1580 report_error(Filename, C, std::move(E));
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001581 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001582 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001583 if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1584 if (!checkMachOAndArchFlags(O, Filename))
1585 return;
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001586 ProcessMachO(Filename, O, O->getFileName());
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001587 }
1588 }
1589 return;
1590 }
Kevin Enderby131d1772015-01-09 19:22:37 +00001591 if (UniversalHeaders) {
1592 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin))
Kevin Enderbyf0640752015-03-13 17:56:32 +00001593 printMachOUniversalHeaders(UB, !NonVerbose);
Kevin Enderby131d1772015-01-09 19:22:37 +00001594 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001595 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
1596 // If we have a list of architecture flags specified dump only those.
1597 if (!ArchAll && ArchFlags.size() != 0) {
1598 // Look for a slice in the universal binary that matches each ArchFlag.
1599 bool ArchFound;
1600 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1601 ArchFound = false;
1602 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1603 E = UB->end_objects();
1604 I != E; ++I) {
1605 if (ArchFlags[i] == I->getArchTypeName()) {
1606 ArchFound = true;
1607 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr =
1608 I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001609 std::string ArchitectureName = "";
1610 if (ArchFlags.size() > 1)
1611 ArchitectureName = I->getArchTypeName();
1612 if (ObjOrErr) {
1613 ObjectFile &O = *ObjOrErr.get();
1614 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001615 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001616 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
1617 I->getAsArchive()) {
1618 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001619 outs() << "Archive : " << Filename;
1620 if (!ArchitectureName.empty())
1621 outs() << " (architecture " << ArchitectureName << ")";
1622 outs() << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001623 if (ArchiveHeaders)
Kevin Enderby8972e482015-04-30 20:30:42 +00001624 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001625 for (Archive::child_iterator AI = A->child_begin(),
1626 AE = A->child_end();
1627 AI != AE; ++AI) {
Davide Italianob13edeb2015-12-08 02:45:59 +00001628 if (std::error_code EC = AI->getError())
1629 report_error(Filename, EC);
Kevin Enderby7a969422015-11-05 19:24:56 +00001630 auto &C = AI->get();
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001631 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1632 if (!ChildOrErr) {
1633 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
1634 report_error(Filename, C, std::move(E));
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001635 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001636 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001637 if (MachOObjectFile *O =
1638 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001639 ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001640 }
1641 }
1642 }
1643 }
1644 if (!ArchFound) {
1645 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1646 << "architecture: " + ArchFlags[i] + "\n";
1647 return;
1648 }
1649 }
1650 return;
1651 }
1652 // No architecture flags were specified so if this contains a slice that
1653 // matches the host architecture dump only that.
1654 if (!ArchAll) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001655 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1656 E = UB->end_objects();
1657 I != E; ++I) {
Kevin Enderby0512bd72015-01-09 21:55:03 +00001658 if (MachOObjectFile::getHostArch().getArchName() ==
1659 I->getArchTypeName()) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001660 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001661 std::string ArchiveName;
1662 ArchiveName.clear();
1663 if (ObjOrErr) {
1664 ObjectFile &O = *ObjOrErr.get();
1665 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001666 ProcessMachO(Filename, MachOOF);
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001667 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
1668 I->getAsArchive()) {
1669 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001670 outs() << "Archive : " << Filename << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001671 if (ArchiveHeaders)
Kevin Enderby8972e482015-04-30 20:30:42 +00001672 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001673 for (Archive::child_iterator AI = A->child_begin(),
1674 AE = A->child_end();
1675 AI != AE; ++AI) {
Davide Italianob13edeb2015-12-08 02:45:59 +00001676 if (std::error_code EC = AI->getError())
1677 report_error(Filename, EC);
Kevin Enderby7a969422015-11-05 19:24:56 +00001678 auto &C = AI->get();
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001679 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1680 if (!ChildOrErr) {
1681 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
1682 report_error(Filename, C, std::move(E));
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001683 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001684 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001685 if (MachOObjectFile *O =
1686 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001687 ProcessMachO(Filename, O, O->getFileName());
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001688 }
1689 }
1690 return;
1691 }
1692 }
1693 }
1694 // Either all architectures have been specified or none have been specified
1695 // and this does not contain the host architecture so dump all the slices.
1696 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
1697 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1698 E = UB->end_objects();
1699 I != E; ++I) {
1700 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001701 std::string ArchitectureName = "";
1702 if (moreThanOneArch)
1703 ArchitectureName = I->getArchTypeName();
1704 if (ObjOrErr) {
1705 ObjectFile &Obj = *ObjOrErr.get();
1706 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001707 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001708 } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = I->getAsArchive()) {
1709 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001710 outs() << "Archive : " << Filename;
1711 if (!ArchitectureName.empty())
1712 outs() << " (architecture " << ArchitectureName << ")";
1713 outs() << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001714 if (ArchiveHeaders)
Kevin Enderby8972e482015-04-30 20:30:42 +00001715 printArchiveHeaders(A.get(), !NonVerbose, ArchiveMemberOffsets);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001716 for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end();
1717 AI != AE; ++AI) {
Davide Italianob13edeb2015-12-08 02:45:59 +00001718 if (std::error_code EC = AI->getError())
1719 report_error(Filename, EC);
Kevin Enderby7a969422015-11-05 19:24:56 +00001720 auto &C = AI->get();
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001721 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1722 if (!ChildOrErr) {
1723 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
1724 report_error(Filename, C, std::move(E));
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001725 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001726 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001727 if (MachOObjectFile *O =
1728 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1729 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001730 ProcessMachO(Filename, MachOOF, MachOOF->getFileName(),
1731 ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001732 }
1733 }
1734 }
1735 }
1736 return;
1737 }
1738 if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) {
1739 if (!checkMachOAndArchFlags(O, Filename))
1740 return;
1741 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O)) {
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001742 ProcessMachO(Filename, MachOOF);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001743 } else
1744 errs() << "llvm-objdump: '" << Filename << "': "
1745 << "Object is not a Mach-O file type.\n";
Davide Italiano25d84582016-01-13 04:11:36 +00001746 return;
1747 }
1748 llvm_unreachable("Input object can't be invalid at this point");
Rafael Espindola9b709252013-04-13 01:45:40 +00001749}
1750
Kevin Enderby6f326ce2014-10-23 19:37:31 +00001751typedef std::pair<uint64_t, const char *> BindInfoEntry;
1752typedef std::vector<BindInfoEntry> BindTable;
1753typedef BindTable::iterator bind_table_iterator;
Kevin Enderbybf246f52014-09-24 23:08:22 +00001754
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001755// The block of info used by the Symbolizer call backs.
1756struct DisassembleInfo {
1757 bool verbose;
1758 MachOObjectFile *O;
1759 SectionRef S;
Kevin Enderbybf246f52014-09-24 23:08:22 +00001760 SymbolAddressMap *AddrMap;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00001761 std::vector<SectionRef> *Sections;
1762 const char *class_name;
1763 const char *selector_name;
1764 char *method;
Kevin Enderby04bf6932014-10-28 23:39:46 +00001765 char *demangled_name;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00001766 uint64_t adrp_addr;
1767 uint32_t adrp_inst;
Kevin Enderby078be602014-10-23 19:53:12 +00001768 BindTable *bindtable;
Kevin Enderbyaac75382015-10-08 16:56:35 +00001769 uint32_t depth;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001770};
1771
1772// SymbolizerGetOpInfo() is the operand information call back function.
1773// This is called to get the symbolic information for operand(s) of an
1774// instruction when it is being done. This routine does this from
1775// the relocation information, symbol table, etc. That block of information
1776// is a pointer to the struct DisassembleInfo that was passed when the
1777// disassembler context was created and passed to back to here when
1778// called back by the disassembler for instruction operands that could have
1779// relocation information. The address of the instruction containing operand is
1780// at the Pc parameter. The immediate value the operand has is passed in
1781// op_info->Value and is at Offset past the start of the instruction and has a
1782// byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
1783// LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
1784// names and addends of the symbolic expression to add for the operand. The
1785// value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
1786// information is returned then this function returns 1 else it returns 0.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00001787static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
1788 uint64_t Size, int TagType, void *TagBuf) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001789 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
1790 struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00001791 uint64_t value = op_info->Value;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001792
1793 // Make sure all fields returned are zero if we don't set them.
1794 memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1));
1795 op_info->Value = value;
1796
1797 // If the TagType is not the value 1 which it code knows about or if no
1798 // verbose symbolic information is wanted then just return 0, indicating no
1799 // information is being returned.
David Blaikie33dd45d02015-03-23 18:39:02 +00001800 if (TagType != 1 || !info->verbose)
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001801 return 0;
1802
1803 unsigned int Arch = info->O->getArch();
1804 if (Arch == Triple::x86) {
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001805 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1806 return 0;
Kevin Enderbyd90a4172015-10-10 00:05:01 +00001807 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
1808 // TODO:
1809 // Search the external relocation entries of a fully linked image
1810 // (if any) for an entry that matches this segment offset.
1811 // uint32_t seg_offset = (Pc + Offset);
1812 return 0;
1813 }
1814 // In MH_OBJECT filetypes search the section's relocation entries (if any)
1815 // for an entry for this section offset.
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001816 uint32_t sect_addr = info->S.getAddress();
1817 uint32_t sect_offset = (Pc + Offset) - sect_addr;
1818 bool reloc_found = false;
1819 DataRefImpl Rel;
1820 MachO::any_relocation_info RE;
1821 bool isExtern = false;
1822 SymbolRef Symbol;
1823 bool r_scattered = false;
1824 uint32_t r_value, pair_r_value, r_type;
1825 for (const RelocationRef &Reloc : info->S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00001826 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001827 if (RelocOffset == sect_offset) {
1828 Rel = Reloc.getRawDataRefImpl();
1829 RE = info->O->getRelocation(Rel);
Kevin Enderby3eb73e12014-11-11 19:16:45 +00001830 r_type = info->O->getAnyRelocationType(RE);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001831 r_scattered = info->O->isRelocationScattered(RE);
1832 if (r_scattered) {
1833 r_value = info->O->getScatteredRelocationValue(RE);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001834 if (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1835 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
1836 DataRefImpl RelNext = Rel;
1837 info->O->moveRelocationNext(RelNext);
1838 MachO::any_relocation_info RENext;
1839 RENext = info->O->getRelocation(RelNext);
1840 if (info->O->isRelocationScattered(RENext))
Kevin Enderby930fdc72014-11-06 19:00:13 +00001841 pair_r_value = info->O->getScatteredRelocationValue(RENext);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001842 else
1843 return 0;
1844 }
1845 } else {
1846 isExtern = info->O->getPlainRelocationExternal(RE);
1847 if (isExtern) {
1848 symbol_iterator RelocSym = Reloc.getSymbol();
1849 Symbol = *RelocSym;
1850 }
1851 }
1852 reloc_found = true;
1853 break;
1854 }
1855 }
1856 if (reloc_found && isExtern) {
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00001857 Expected<StringRef> SymName = Symbol.getName();
1858 if (!SymName) {
1859 std::string Buf;
1860 raw_string_ostream OS(Buf);
1861 logAllUnhandledErrors(SymName.takeError(), OS, "");
1862 OS.flush();
1863 report_fatal_error(Buf);
1864 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001865 const char *name = SymName->data();
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001866 op_info->AddSymbol.Present = 1;
1867 op_info->AddSymbol.Name = name;
1868 // For i386 extern relocation entries the value in the instruction is
1869 // the offset from the symbol, and value is already set in op_info->Value.
1870 return 1;
1871 }
1872 if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1873 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) {
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001874 const char *add = GuessSymbolName(r_value, info->AddrMap);
1875 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001876 uint32_t offset = value - (r_value - pair_r_value);
1877 op_info->AddSymbol.Present = 1;
1878 if (add != nullptr)
1879 op_info->AddSymbol.Name = add;
1880 else
1881 op_info->AddSymbol.Value = r_value;
1882 op_info->SubtractSymbol.Present = 1;
1883 if (sub != nullptr)
1884 op_info->SubtractSymbol.Name = sub;
1885 else
1886 op_info->SubtractSymbol.Value = pair_r_value;
1887 op_info->Value = offset;
1888 return 1;
1889 }
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001890 return 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00001891 }
1892 if (Arch == Triple::x86_64) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001893 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1894 return 0;
Kevin Enderbyd90a4172015-10-10 00:05:01 +00001895 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
1896 // TODO:
1897 // Search the external relocation entries of a fully linked image
1898 // (if any) for an entry that matches this segment offset.
1899 // uint64_t seg_offset = (Pc + Offset);
1900 return 0;
1901 }
1902 // In MH_OBJECT filetypes search the section's relocation entries (if any)
1903 // for an entry for this section offset.
Rafael Espindola80291272014-10-08 15:28:58 +00001904 uint64_t sect_addr = info->S.getAddress();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001905 uint64_t sect_offset = (Pc + Offset) - sect_addr;
1906 bool reloc_found = false;
1907 DataRefImpl Rel;
1908 MachO::any_relocation_info RE;
1909 bool isExtern = false;
1910 SymbolRef Symbol;
1911 for (const RelocationRef &Reloc : info->S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00001912 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001913 if (RelocOffset == sect_offset) {
1914 Rel = Reloc.getRawDataRefImpl();
1915 RE = info->O->getRelocation(Rel);
1916 // NOTE: Scattered relocations don't exist on x86_64.
1917 isExtern = info->O->getPlainRelocationExternal(RE);
1918 if (isExtern) {
1919 symbol_iterator RelocSym = Reloc.getSymbol();
1920 Symbol = *RelocSym;
1921 }
1922 reloc_found = true;
1923 break;
1924 }
1925 }
1926 if (reloc_found && isExtern) {
1927 // The Value passed in will be adjusted by the Pc if the instruction
1928 // adds the Pc. But for x86_64 external relocation entries the Value
1929 // is the offset from the external symbol.
1930 if (info->O->getAnyRelocationPCRel(RE))
1931 op_info->Value -= Pc + Offset + Size;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00001932 Expected<StringRef> SymName = Symbol.getName();
1933 if (!SymName) {
1934 std::string Buf;
1935 raw_string_ostream OS(Buf);
1936 logAllUnhandledErrors(SymName.takeError(), OS, "");
1937 OS.flush();
1938 report_fatal_error(Buf);
1939 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001940 const char *name = SymName->data();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001941 unsigned Type = info->O->getAnyRelocationType(RE);
1942 if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
1943 DataRefImpl RelNext = Rel;
1944 info->O->moveRelocationNext(RelNext);
1945 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
1946 unsigned TypeNext = info->O->getAnyRelocationType(RENext);
1947 bool isExternNext = info->O->getPlainRelocationExternal(RENext);
1948 unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext);
1949 if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) {
1950 op_info->SubtractSymbol.Present = 1;
1951 op_info->SubtractSymbol.Name = name;
1952 symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
1953 Symbol = *RelocSymNext;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00001954 Expected<StringRef> SymNameNext = Symbol.getName();
1955 if (!SymNameNext) {
1956 std::string Buf;
1957 raw_string_ostream OS(Buf);
1958 logAllUnhandledErrors(SymNameNext.takeError(), OS, "");
1959 OS.flush();
1960 report_fatal_error(Buf);
1961 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001962 name = SymNameNext->data();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001963 }
1964 }
1965 // TODO: add the VariantKinds to op_info->VariantKind for relocation types
1966 // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
1967 op_info->AddSymbol.Present = 1;
1968 op_info->AddSymbol.Name = name;
1969 return 1;
1970 }
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001971 return 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00001972 }
1973 if (Arch == Triple::arm) {
Kevin Enderby930fdc72014-11-06 19:00:13 +00001974 if (Offset != 0 || (Size != 4 && Size != 2))
1975 return 0;
Kevin Enderbyd90a4172015-10-10 00:05:01 +00001976 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
1977 // TODO:
1978 // Search the external relocation entries of a fully linked image
1979 // (if any) for an entry that matches this segment offset.
1980 // uint32_t seg_offset = (Pc + Offset);
1981 return 0;
1982 }
1983 // In MH_OBJECT filetypes search the section's relocation entries (if any)
1984 // for an entry for this section offset.
Kevin Enderby930fdc72014-11-06 19:00:13 +00001985 uint32_t sect_addr = info->S.getAddress();
1986 uint32_t sect_offset = (Pc + Offset) - sect_addr;
Kevin Enderby930fdc72014-11-06 19:00:13 +00001987 DataRefImpl Rel;
1988 MachO::any_relocation_info RE;
1989 bool isExtern = false;
1990 SymbolRef Symbol;
1991 bool r_scattered = false;
1992 uint32_t r_value, pair_r_value, r_type, r_length, other_half;
David Blaikie33dd45d02015-03-23 18:39:02 +00001993 auto Reloc =
1994 std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
1995 [&](const RelocationRef &Reloc) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00001996 uint64_t RelocOffset = Reloc.getOffset();
David Blaikie33dd45d02015-03-23 18:39:02 +00001997 return RelocOffset == sect_offset;
1998 });
1999
2000 if (Reloc == info->S.relocations().end())
2001 return 0;
2002
2003 Rel = Reloc->getRawDataRefImpl();
2004 RE = info->O->getRelocation(Rel);
2005 r_length = info->O->getAnyRelocationLength(RE);
2006 r_scattered = info->O->isRelocationScattered(RE);
2007 if (r_scattered) {
2008 r_value = info->O->getScatteredRelocationValue(RE);
2009 r_type = info->O->getScatteredRelocationType(RE);
2010 } else {
2011 r_type = info->O->getAnyRelocationType(RE);
2012 isExtern = info->O->getPlainRelocationExternal(RE);
2013 if (isExtern) {
2014 symbol_iterator RelocSym = Reloc->getSymbol();
2015 Symbol = *RelocSym;
Kevin Enderby930fdc72014-11-06 19:00:13 +00002016 }
2017 }
David Blaikie33dd45d02015-03-23 18:39:02 +00002018 if (r_type == MachO::ARM_RELOC_HALF ||
2019 r_type == MachO::ARM_RELOC_SECTDIFF ||
2020 r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
2021 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2022 DataRefImpl RelNext = Rel;
2023 info->O->moveRelocationNext(RelNext);
2024 MachO::any_relocation_info RENext;
2025 RENext = info->O->getRelocation(RelNext);
2026 other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff;
2027 if (info->O->isRelocationScattered(RENext))
2028 pair_r_value = info->O->getScatteredRelocationValue(RENext);
2029 }
2030
2031 if (isExtern) {
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002032 Expected<StringRef> SymName = Symbol.getName();
2033 if (!SymName) {
2034 std::string Buf;
2035 raw_string_ostream OS(Buf);
2036 logAllUnhandledErrors(SymName.takeError(), OS, "");
2037 OS.flush();
2038 report_fatal_error(Buf);
2039 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002040 const char *name = SymName->data();
Kevin Enderby930fdc72014-11-06 19:00:13 +00002041 op_info->AddSymbol.Present = 1;
2042 op_info->AddSymbol.Name = name;
Sylvestre Ledru648cced2015-02-05 17:00:23 +00002043 switch (r_type) {
2044 case MachO::ARM_RELOC_HALF:
2045 if ((r_length & 0x1) == 1) {
2046 op_info->Value = value << 16 | other_half;
2047 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2048 } else {
2049 op_info->Value = other_half << 16 | value;
2050 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Sylvestre Ledrufe0c7ad2015-02-05 16:35:44 +00002051 }
Sylvestre Ledru648cced2015-02-05 17:00:23 +00002052 break;
2053 default:
2054 break;
Kevin Enderby930fdc72014-11-06 19:00:13 +00002055 }
2056 return 1;
2057 }
2058 // If we have a branch that is not an external relocation entry then
2059 // return 0 so the code in tryAddingSymbolicOperand() can use the
2060 // SymbolLookUp call back with the branch target address to look up the
2061 // symbol and possiblity add an annotation for a symbol stub.
David Blaikie33dd45d02015-03-23 18:39:02 +00002062 if (isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 ||
2063 r_type == MachO::ARM_THUMB_RELOC_BR22))
Kevin Enderby930fdc72014-11-06 19:00:13 +00002064 return 0;
2065
2066 uint32_t offset = 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00002067 if (r_type == MachO::ARM_RELOC_HALF ||
2068 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2069 if ((r_length & 0x1) == 1)
2070 value = value << 16 | other_half;
2071 else
2072 value = other_half << 16 | value;
2073 }
2074 if (r_scattered && (r_type != MachO::ARM_RELOC_HALF &&
2075 r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) {
2076 offset = value - r_value;
2077 value = r_value;
Kevin Enderby930fdc72014-11-06 19:00:13 +00002078 }
2079
David Blaikie33dd45d02015-03-23 18:39:02 +00002080 if (r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
Kevin Enderby930fdc72014-11-06 19:00:13 +00002081 if ((r_length & 0x1) == 1)
2082 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2083 else
2084 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00002085 const char *add = GuessSymbolName(r_value, info->AddrMap);
2086 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
Kevin Enderby930fdc72014-11-06 19:00:13 +00002087 int32_t offset = value - (r_value - pair_r_value);
2088 op_info->AddSymbol.Present = 1;
2089 if (add != nullptr)
2090 op_info->AddSymbol.Name = add;
2091 else
2092 op_info->AddSymbol.Value = r_value;
2093 op_info->SubtractSymbol.Present = 1;
2094 if (sub != nullptr)
2095 op_info->SubtractSymbol.Name = sub;
2096 else
2097 op_info->SubtractSymbol.Value = pair_r_value;
2098 op_info->Value = offset;
2099 return 1;
2100 }
2101
Kevin Enderby930fdc72014-11-06 19:00:13 +00002102 op_info->AddSymbol.Present = 1;
2103 op_info->Value = offset;
David Blaikie33dd45d02015-03-23 18:39:02 +00002104 if (r_type == MachO::ARM_RELOC_HALF) {
2105 if ((r_length & 0x1) == 1)
2106 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2107 else
2108 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Kevin Enderby930fdc72014-11-06 19:00:13 +00002109 }
Kevin Enderbyf6d25852015-01-31 00:37:11 +00002110 const char *add = GuessSymbolName(value, info->AddrMap);
Kevin Enderby930fdc72014-11-06 19:00:13 +00002111 if (add != nullptr) {
2112 op_info->AddSymbol.Name = add;
2113 return 1;
2114 }
2115 op_info->AddSymbol.Value = value;
2116 return 1;
David Blaikie33dd45d02015-03-23 18:39:02 +00002117 }
2118 if (Arch == Triple::aarch64) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002119 if (Offset != 0 || Size != 4)
2120 return 0;
Kevin Enderbyd90a4172015-10-10 00:05:01 +00002121 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2122 // TODO:
2123 // Search the external relocation entries of a fully linked image
2124 // (if any) for an entry that matches this segment offset.
2125 // uint64_t seg_offset = (Pc + Offset);
2126 return 0;
2127 }
2128 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2129 // for an entry for this section offset.
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002130 uint64_t sect_addr = info->S.getAddress();
2131 uint64_t sect_offset = (Pc + Offset) - sect_addr;
David Blaikie33dd45d02015-03-23 18:39:02 +00002132 auto Reloc =
2133 std::find_if(info->S.relocations().begin(), info->S.relocations().end(),
2134 [&](const RelocationRef &Reloc) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00002135 uint64_t RelocOffset = Reloc.getOffset();
David Blaikie33dd45d02015-03-23 18:39:02 +00002136 return RelocOffset == sect_offset;
2137 });
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002138
David Blaikie33dd45d02015-03-23 18:39:02 +00002139 if (Reloc == info->S.relocations().end())
2140 return 0;
2141
2142 DataRefImpl Rel = Reloc->getRawDataRefImpl();
2143 MachO::any_relocation_info RE = info->O->getRelocation(Rel);
2144 uint32_t r_type = info->O->getAnyRelocationType(RE);
2145 if (r_type == MachO::ARM64_RELOC_ADDEND) {
2146 DataRefImpl RelNext = Rel;
2147 info->O->moveRelocationNext(RelNext);
2148 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2149 if (value == 0) {
2150 value = info->O->getPlainRelocationSymbolNum(RENext);
2151 op_info->Value = value;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002152 }
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002153 }
David Blaikie33dd45d02015-03-23 18:39:02 +00002154 // NOTE: Scattered relocations don't exist on arm64.
2155 if (!info->O->getPlainRelocationExternal(RE))
2156 return 0;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002157 Expected<StringRef> SymName = Reloc->getSymbol()->getName();
2158 if (!SymName) {
2159 std::string Buf;
2160 raw_string_ostream OS(Buf);
2161 logAllUnhandledErrors(SymName.takeError(), OS, "");
2162 OS.flush();
2163 report_fatal_error(Buf);
2164 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002165 const char *name = SymName->data();
David Blaikie33dd45d02015-03-23 18:39:02 +00002166 op_info->AddSymbol.Present = 1;
2167 op_info->AddSymbol.Name = name;
2168
2169 switch (r_type) {
2170 case MachO::ARM64_RELOC_PAGE21:
2171 /* @page */
2172 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE;
2173 break;
2174 case MachO::ARM64_RELOC_PAGEOFF12:
2175 /* @pageoff */
2176 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF;
2177 break;
2178 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
2179 /* @gotpage */
2180 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE;
2181 break;
2182 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
2183 /* @gotpageoff */
2184 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF;
2185 break;
2186 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
2187 /* @tvlppage is not implemented in llvm-mc */
2188 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP;
2189 break;
2190 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
2191 /* @tvlppageoff is not implemented in llvm-mc */
2192 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF;
2193 break;
2194 default:
2195 case MachO::ARM64_RELOC_BRANCH26:
2196 op_info->VariantKind = LLVMDisassembler_VariantKind_None;
2197 break;
2198 }
2199 return 1;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002200 }
David Blaikie33dd45d02015-03-23 18:39:02 +00002201 return 0;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002202}
2203
Kevin Enderbybf246f52014-09-24 23:08:22 +00002204// GuessCstringPointer is passed the address of what might be a pointer to a
2205// literal string in a cstring section. If that address is in a cstring section
2206// it returns a pointer to that string. Else it returns nullptr.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00002207static const char *GuessCstringPointer(uint64_t ReferenceValue,
2208 struct DisassembleInfo *info) {
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002209 for (const auto &Load : info->O->load_commands()) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00002210 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2211 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2212 for (unsigned J = 0; J < Seg.nsects; ++J) {
2213 MachO::section_64 Sec = info->O->getSection64(Load, J);
2214 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2215 if (section_type == MachO::S_CSTRING_LITERALS &&
2216 ReferenceValue >= Sec.addr &&
2217 ReferenceValue < Sec.addr + Sec.size) {
2218 uint64_t sect_offset = ReferenceValue - Sec.addr;
2219 uint64_t object_offset = Sec.offset + sect_offset;
2220 StringRef MachOContents = info->O->getData();
2221 uint64_t object_size = MachOContents.size();
2222 const char *object_addr = (const char *)MachOContents.data();
2223 if (object_offset < object_size) {
2224 const char *name = object_addr + object_offset;
2225 return name;
2226 } else {
2227 return nullptr;
2228 }
2229 }
2230 }
2231 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2232 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2233 for (unsigned J = 0; J < Seg.nsects; ++J) {
2234 MachO::section Sec = info->O->getSection(Load, J);
2235 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2236 if (section_type == MachO::S_CSTRING_LITERALS &&
2237 ReferenceValue >= Sec.addr &&
2238 ReferenceValue < Sec.addr + Sec.size) {
2239 uint64_t sect_offset = ReferenceValue - Sec.addr;
2240 uint64_t object_offset = Sec.offset + sect_offset;
2241 StringRef MachOContents = info->O->getData();
2242 uint64_t object_size = MachOContents.size();
2243 const char *object_addr = (const char *)MachOContents.data();
2244 if (object_offset < object_size) {
2245 const char *name = object_addr + object_offset;
2246 return name;
2247 } else {
2248 return nullptr;
2249 }
2250 }
2251 }
2252 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00002253 }
2254 return nullptr;
2255}
2256
Kevin Enderby85974882014-09-26 22:20:44 +00002257// GuessIndirectSymbol returns the name of the indirect symbol for the
2258// ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe
2259// an address of a symbol stub or a lazy or non-lazy pointer to associate the
2260// symbol name being referenced by the stub or pointer.
2261static const char *GuessIndirectSymbol(uint64_t ReferenceValue,
2262 struct DisassembleInfo *info) {
Kevin Enderby85974882014-09-26 22:20:44 +00002263 MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand();
2264 MachO::symtab_command Symtab = info->O->getSymtabLoadCommand();
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002265 for (const auto &Load : info->O->load_commands()) {
Kevin Enderby85974882014-09-26 22:20:44 +00002266 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2267 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2268 for (unsigned J = 0; J < Seg.nsects; ++J) {
2269 MachO::section_64 Sec = info->O->getSection64(Load, J);
2270 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2271 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2272 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2273 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2274 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2275 section_type == MachO::S_SYMBOL_STUBS) &&
2276 ReferenceValue >= Sec.addr &&
2277 ReferenceValue < Sec.addr + Sec.size) {
2278 uint32_t stride;
2279 if (section_type == MachO::S_SYMBOL_STUBS)
2280 stride = Sec.reserved2;
2281 else
2282 stride = 8;
2283 if (stride == 0)
2284 return nullptr;
2285 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2286 if (index < Dysymtab.nindirectsyms) {
2287 uint32_t indirect_symbol =
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002288 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
Kevin Enderby85974882014-09-26 22:20:44 +00002289 if (indirect_symbol < Symtab.nsyms) {
2290 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2291 SymbolRef Symbol = *Sym;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002292 Expected<StringRef> SymName = Symbol.getName();
2293 if (!SymName) {
2294 std::string Buf;
2295 raw_string_ostream OS(Buf);
2296 logAllUnhandledErrors(SymName.takeError(), OS, "");
2297 OS.flush();
2298 report_fatal_error(Buf);
2299 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002300 const char *name = SymName->data();
Kevin Enderby85974882014-09-26 22:20:44 +00002301 return name;
2302 }
2303 }
2304 }
2305 }
2306 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2307 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2308 for (unsigned J = 0; J < Seg.nsects; ++J) {
2309 MachO::section Sec = info->O->getSection(Load, J);
2310 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2311 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2312 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2313 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2314 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2315 section_type == MachO::S_SYMBOL_STUBS) &&
2316 ReferenceValue >= Sec.addr &&
2317 ReferenceValue < Sec.addr + Sec.size) {
2318 uint32_t stride;
2319 if (section_type == MachO::S_SYMBOL_STUBS)
2320 stride = Sec.reserved2;
2321 else
2322 stride = 4;
2323 if (stride == 0)
2324 return nullptr;
2325 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2326 if (index < Dysymtab.nindirectsyms) {
2327 uint32_t indirect_symbol =
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002328 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
Kevin Enderby85974882014-09-26 22:20:44 +00002329 if (indirect_symbol < Symtab.nsyms) {
2330 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2331 SymbolRef Symbol = *Sym;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002332 Expected<StringRef> SymName = Symbol.getName();
2333 if (!SymName) {
2334 std::string Buf;
2335 raw_string_ostream OS(Buf);
2336 logAllUnhandledErrors(SymName.takeError(), OS, "");
2337 OS.flush();
2338 report_fatal_error(Buf);
2339 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002340 const char *name = SymName->data();
Kevin Enderby85974882014-09-26 22:20:44 +00002341 return name;
2342 }
2343 }
2344 }
2345 }
2346 }
Kevin Enderby85974882014-09-26 22:20:44 +00002347 }
2348 return nullptr;
2349}
2350
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002351// method_reference() is called passing it the ReferenceName that might be
2352// a reference it to an Objective-C method call. If so then it allocates and
2353// assembles a method call string with the values last seen and saved in
2354// the DisassembleInfo's class_name and selector_name fields. This is saved
2355// into the method field of the info and any previous string is free'ed.
2356// Then the class_name field in the info is set to nullptr. The method call
2357// string is set into ReferenceName and ReferenceType is set to
2358// LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call
2359// then both ReferenceType and ReferenceName are left unchanged.
2360static void method_reference(struct DisassembleInfo *info,
2361 uint64_t *ReferenceType,
2362 const char **ReferenceName) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002363 unsigned int Arch = info->O->getArch();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002364 if (*ReferenceName != nullptr) {
2365 if (strcmp(*ReferenceName, "_objc_msgSend") == 0) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002366 if (info->selector_name != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002367 if (info->method != nullptr)
2368 free(info->method);
2369 if (info->class_name != nullptr) {
2370 info->method = (char *)malloc(5 + strlen(info->class_name) +
2371 strlen(info->selector_name));
2372 if (info->method != nullptr) {
2373 strcpy(info->method, "+[");
2374 strcat(info->method, info->class_name);
2375 strcat(info->method, " ");
2376 strcat(info->method, info->selector_name);
2377 strcat(info->method, "]");
2378 *ReferenceName = info->method;
2379 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2380 }
2381 } else {
2382 info->method = (char *)malloc(9 + strlen(info->selector_name));
2383 if (info->method != nullptr) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002384 if (Arch == Triple::x86_64)
2385 strcpy(info->method, "-[%rdi ");
2386 else if (Arch == Triple::aarch64)
2387 strcpy(info->method, "-[x0 ");
2388 else
2389 strcpy(info->method, "-[r? ");
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002390 strcat(info->method, info->selector_name);
2391 strcat(info->method, "]");
2392 *ReferenceName = info->method;
2393 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2394 }
2395 }
2396 info->class_name = nullptr;
2397 }
2398 } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002399 if (info->selector_name != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002400 if (info->method != nullptr)
2401 free(info->method);
2402 info->method = (char *)malloc(17 + strlen(info->selector_name));
2403 if (info->method != nullptr) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002404 if (Arch == Triple::x86_64)
2405 strcpy(info->method, "-[[%rdi super] ");
2406 else if (Arch == Triple::aarch64)
2407 strcpy(info->method, "-[[x0 super] ");
2408 else
2409 strcpy(info->method, "-[[r? super] ");
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002410 strcat(info->method, info->selector_name);
2411 strcat(info->method, "]");
2412 *ReferenceName = info->method;
2413 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2414 }
2415 info->class_name = nullptr;
2416 }
2417 }
2418 }
2419}
2420
2421// GuessPointerPointer() is passed the address of what might be a pointer to
2422// a reference to an Objective-C class, selector, message ref or cfstring.
2423// If so the value of the pointer is returned and one of the booleans are set
2424// to true. If not zero is returned and all the booleans are set to false.
2425static uint64_t GuessPointerPointer(uint64_t ReferenceValue,
2426 struct DisassembleInfo *info,
2427 bool &classref, bool &selref, bool &msgref,
2428 bool &cfstring) {
2429 classref = false;
2430 selref = false;
2431 msgref = false;
2432 cfstring = false;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002433 for (const auto &Load : info->O->load_commands()) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002434 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2435 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2436 for (unsigned J = 0; J < Seg.nsects; ++J) {
2437 MachO::section_64 Sec = info->O->getSection64(Load, J);
2438 if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 ||
2439 strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2440 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 ||
2441 strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 ||
2442 strncmp(Sec.sectname, "__cfstring", 16) == 0) &&
2443 ReferenceValue >= Sec.addr &&
2444 ReferenceValue < Sec.addr + Sec.size) {
2445 uint64_t sect_offset = ReferenceValue - Sec.addr;
2446 uint64_t object_offset = Sec.offset + sect_offset;
2447 StringRef MachOContents = info->O->getData();
2448 uint64_t object_size = MachOContents.size();
2449 const char *object_addr = (const char *)MachOContents.data();
2450 if (object_offset < object_size) {
2451 uint64_t pointer_value;
2452 memcpy(&pointer_value, object_addr + object_offset,
2453 sizeof(uint64_t));
2454 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2455 sys::swapByteOrder(pointer_value);
2456 if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0)
2457 selref = true;
2458 else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2459 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0)
2460 classref = true;
2461 else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 &&
2462 ReferenceValue + 8 < Sec.addr + Sec.size) {
2463 msgref = true;
2464 memcpy(&pointer_value, object_addr + object_offset + 8,
2465 sizeof(uint64_t));
2466 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2467 sys::swapByteOrder(pointer_value);
2468 } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0)
2469 cfstring = true;
2470 return pointer_value;
2471 } else {
2472 return 0;
2473 }
2474 }
2475 }
2476 }
2477 // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002478 }
2479 return 0;
2480}
2481
2482// get_pointer_64 returns a pointer to the bytes in the object file at the
2483// Address from a section in the Mach-O file. And indirectly returns the
2484// offset into the section, number of bytes left in the section past the offset
2485// and which section is was being referenced. If the Address is not in a
2486// section nullptr is returned.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00002487static const char *get_pointer_64(uint64_t Address, uint32_t &offset,
2488 uint32_t &left, SectionRef &S,
Kevin Enderby846c0002015-04-16 17:19:59 +00002489 DisassembleInfo *info,
2490 bool objc_only = false) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002491 offset = 0;
2492 left = 0;
2493 S = SectionRef();
2494 for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) {
2495 uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress();
2496 uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize();
Kevin Enderby46e642f2015-10-08 22:50:55 +00002497 if (SectSize == 0)
2498 continue;
Kevin Enderby846c0002015-04-16 17:19:59 +00002499 if (objc_only) {
2500 StringRef SectName;
2501 ((*(info->Sections))[SectIdx]).getName(SectName);
2502 DataRefImpl Ref = ((*(info->Sections))[SectIdx]).getRawDataRefImpl();
2503 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
2504 if (SegName != "__OBJC" && SectName != "__cstring")
2505 continue;
2506 }
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002507 if (Address >= SectAddress && Address < SectAddress + SectSize) {
2508 S = (*(info->Sections))[SectIdx];
2509 offset = Address - SectAddress;
2510 left = SectSize - offset;
2511 StringRef SectContents;
2512 ((*(info->Sections))[SectIdx]).getContents(SectContents);
2513 return SectContents.data() + offset;
2514 }
2515 }
2516 return nullptr;
2517}
2518
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002519static const char *get_pointer_32(uint32_t Address, uint32_t &offset,
2520 uint32_t &left, SectionRef &S,
Kevin Enderby846c0002015-04-16 17:19:59 +00002521 DisassembleInfo *info,
2522 bool objc_only = false) {
2523 return get_pointer_64(Address, offset, left, S, info, objc_only);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002524}
2525
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002526// get_symbol_64() returns the name of a symbol (or nullptr) and the address of
2527// the symbol indirectly through n_value. Based on the relocation information
2528// for the specified section offset in the specified section reference.
Kevin Enderby0fc11822015-04-01 20:57:01 +00002529// If no relocation information is found and a non-zero ReferenceValue for the
2530// symbol is passed, look up that address in the info's AddrMap.
Rafael Espindolad7a32ea2015-06-24 10:20:30 +00002531static const char *get_symbol_64(uint32_t sect_offset, SectionRef S,
2532 DisassembleInfo *info, uint64_t &n_value,
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +00002533 uint64_t ReferenceValue = 0) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002534 n_value = 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00002535 if (!info->verbose)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002536 return nullptr;
2537
2538 // See if there is an external relocation entry at the sect_offset.
2539 bool reloc_found = false;
2540 DataRefImpl Rel;
2541 MachO::any_relocation_info RE;
2542 bool isExtern = false;
2543 SymbolRef Symbol;
2544 for (const RelocationRef &Reloc : S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00002545 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002546 if (RelocOffset == sect_offset) {
2547 Rel = Reloc.getRawDataRefImpl();
2548 RE = info->O->getRelocation(Rel);
2549 if (info->O->isRelocationScattered(RE))
2550 continue;
2551 isExtern = info->O->getPlainRelocationExternal(RE);
2552 if (isExtern) {
2553 symbol_iterator RelocSym = Reloc.getSymbol();
2554 Symbol = *RelocSym;
2555 }
2556 reloc_found = true;
2557 break;
2558 }
2559 }
2560 // If there is an external relocation entry for a symbol in this section
2561 // at this section_offset then use that symbol's value for the n_value
2562 // and return its name.
2563 const char *SymbolName = nullptr;
2564 if (reloc_found && isExtern) {
Rafael Espindoladea00162015-07-03 17:44:18 +00002565 n_value = Symbol.getValue();
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002566 Expected<StringRef> NameOrError = Symbol.getName();
2567 if (!NameOrError) {
2568 std::string Buf;
2569 raw_string_ostream OS(Buf);
2570 logAllUnhandledErrors(NameOrError.takeError(), OS, "");
2571 OS.flush();
2572 report_fatal_error(Buf);
2573 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002574 StringRef Name = *NameOrError;
2575 if (!Name.empty()) {
2576 SymbolName = Name.data();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002577 return SymbolName;
2578 }
2579 }
2580
2581 // TODO: For fully linked images, look through the external relocation
2582 // entries off the dynamic symtab command. For these the r_offset is from the
2583 // start of the first writeable segment in the Mach-O file. So the offset
2584 // to this section from that segment is passed to this routine by the caller,
2585 // as the database_offset. Which is the difference of the section's starting
2586 // address and the first writable segment.
2587 //
2588 // NOTE: need add passing the database_offset to this routine.
2589
Kevin Enderby0fc11822015-04-01 20:57:01 +00002590 // We did not find an external relocation entry so look up the ReferenceValue
2591 // as an address of a symbol and if found return that symbol's name.
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +00002592 SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002593
2594 return SymbolName;
2595}
2596
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002597static const char *get_symbol_32(uint32_t sect_offset, SectionRef S,
2598 DisassembleInfo *info,
2599 uint32_t ReferenceValue) {
2600 uint64_t n_value64;
2601 return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue);
2602}
2603
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002604// These are structs in the Objective-C meta data and read to produce the
2605// comments for disassembly. While these are part of the ABI they are no
2606// public defintions. So the are here not in include/llvm/Support/MachO.h .
2607
2608// The cfstring object in a 64-bit Mach-O file.
2609struct cfstring64_t {
2610 uint64_t isa; // class64_t * (64-bit pointer)
2611 uint64_t flags; // flag bits
2612 uint64_t characters; // char * (64-bit pointer)
2613 uint64_t length; // number of non-NULL characters in above
2614};
2615
2616// The class object in a 64-bit Mach-O file.
2617struct class64_t {
2618 uint64_t isa; // class64_t * (64-bit pointer)
2619 uint64_t superclass; // class64_t * (64-bit pointer)
2620 uint64_t cache; // Cache (64-bit pointer)
2621 uint64_t vtable; // IMP * (64-bit pointer)
2622 uint64_t data; // class_ro64_t * (64-bit pointer)
2623};
2624
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002625struct class32_t {
2626 uint32_t isa; /* class32_t * (32-bit pointer) */
2627 uint32_t superclass; /* class32_t * (32-bit pointer) */
2628 uint32_t cache; /* Cache (32-bit pointer) */
2629 uint32_t vtable; /* IMP * (32-bit pointer) */
2630 uint32_t data; /* class_ro32_t * (32-bit pointer) */
2631};
2632
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002633struct class_ro64_t {
2634 uint32_t flags;
2635 uint32_t instanceStart;
2636 uint32_t instanceSize;
2637 uint32_t reserved;
2638 uint64_t ivarLayout; // const uint8_t * (64-bit pointer)
2639 uint64_t name; // const char * (64-bit pointer)
2640 uint64_t baseMethods; // const method_list_t * (64-bit pointer)
2641 uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer)
2642 uint64_t ivars; // const ivar_list_t * (64-bit pointer)
2643 uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer)
2644 uint64_t baseProperties; // const struct objc_property_list (64-bit pointer)
2645};
2646
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002647struct class_ro32_t {
2648 uint32_t flags;
2649 uint32_t instanceStart;
2650 uint32_t instanceSize;
2651 uint32_t ivarLayout; /* const uint8_t * (32-bit pointer) */
2652 uint32_t name; /* const char * (32-bit pointer) */
2653 uint32_t baseMethods; /* const method_list_t * (32-bit pointer) */
2654 uint32_t baseProtocols; /* const protocol_list_t * (32-bit pointer) */
2655 uint32_t ivars; /* const ivar_list_t * (32-bit pointer) */
2656 uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */
2657 uint32_t baseProperties; /* const struct objc_property_list *
2658 (32-bit pointer) */
2659};
2660
2661/* Values for class_ro{64,32}_t->flags */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002662#define RO_META (1 << 0)
2663#define RO_ROOT (1 << 1)
2664#define RO_HAS_CXX_STRUCTORS (1 << 2)
2665
2666struct method_list64_t {
2667 uint32_t entsize;
2668 uint32_t count;
2669 /* struct method64_t first; These structures follow inline */
2670};
2671
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002672struct method_list32_t {
2673 uint32_t entsize;
2674 uint32_t count;
2675 /* struct method32_t first; These structures follow inline */
2676};
2677
Kevin Enderby0fc11822015-04-01 20:57:01 +00002678struct method64_t {
2679 uint64_t name; /* SEL (64-bit pointer) */
2680 uint64_t types; /* const char * (64-bit pointer) */
2681 uint64_t imp; /* IMP (64-bit pointer) */
2682};
2683
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002684struct method32_t {
2685 uint32_t name; /* SEL (32-bit pointer) */
2686 uint32_t types; /* const char * (32-bit pointer) */
2687 uint32_t imp; /* IMP (32-bit pointer) */
2688};
2689
Kevin Enderby0fc11822015-04-01 20:57:01 +00002690struct protocol_list64_t {
2691 uint64_t count; /* uintptr_t (a 64-bit value) */
2692 /* struct protocol64_t * list[0]; These pointers follow inline */
2693};
2694
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002695struct protocol_list32_t {
2696 uint32_t count; /* uintptr_t (a 32-bit value) */
2697 /* struct protocol32_t * list[0]; These pointers follow inline */
2698};
2699
Kevin Enderby0fc11822015-04-01 20:57:01 +00002700struct protocol64_t {
2701 uint64_t isa; /* id * (64-bit pointer) */
2702 uint64_t name; /* const char * (64-bit pointer) */
2703 uint64_t protocols; /* struct protocol_list64_t *
2704 (64-bit pointer) */
2705 uint64_t instanceMethods; /* method_list_t * (64-bit pointer) */
2706 uint64_t classMethods; /* method_list_t * (64-bit pointer) */
2707 uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */
2708 uint64_t optionalClassMethods; /* method_list_t * (64-bit pointer) */
2709 uint64_t instanceProperties; /* struct objc_property_list *
2710 (64-bit pointer) */
2711};
2712
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002713struct protocol32_t {
2714 uint32_t isa; /* id * (32-bit pointer) */
2715 uint32_t name; /* const char * (32-bit pointer) */
2716 uint32_t protocols; /* struct protocol_list_t *
2717 (32-bit pointer) */
2718 uint32_t instanceMethods; /* method_list_t * (32-bit pointer) */
2719 uint32_t classMethods; /* method_list_t * (32-bit pointer) */
2720 uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */
2721 uint32_t optionalClassMethods; /* method_list_t * (32-bit pointer) */
2722 uint32_t instanceProperties; /* struct objc_property_list *
2723 (32-bit pointer) */
2724};
2725
Kevin Enderby0fc11822015-04-01 20:57:01 +00002726struct ivar_list64_t {
2727 uint32_t entsize;
2728 uint32_t count;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002729 /* struct ivar64_t first; These structures follow inline */
2730};
2731
2732struct ivar_list32_t {
2733 uint32_t entsize;
2734 uint32_t count;
2735 /* struct ivar32_t first; These structures follow inline */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002736};
2737
2738struct ivar64_t {
2739 uint64_t offset; /* uintptr_t * (64-bit pointer) */
2740 uint64_t name; /* const char * (64-bit pointer) */
2741 uint64_t type; /* const char * (64-bit pointer) */
2742 uint32_t alignment;
2743 uint32_t size;
2744};
2745
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002746struct ivar32_t {
2747 uint32_t offset; /* uintptr_t * (32-bit pointer) */
2748 uint32_t name; /* const char * (32-bit pointer) */
2749 uint32_t type; /* const char * (32-bit pointer) */
2750 uint32_t alignment;
2751 uint32_t size;
2752};
2753
Kevin Enderby0fc11822015-04-01 20:57:01 +00002754struct objc_property_list64 {
2755 uint32_t entsize;
2756 uint32_t count;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002757 /* struct objc_property64 first; These structures follow inline */
2758};
2759
2760struct objc_property_list32 {
2761 uint32_t entsize;
2762 uint32_t count;
2763 /* struct objc_property32 first; These structures follow inline */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002764};
2765
2766struct objc_property64 {
2767 uint64_t name; /* const char * (64-bit pointer) */
2768 uint64_t attributes; /* const char * (64-bit pointer) */
2769};
2770
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002771struct objc_property32 {
2772 uint32_t name; /* const char * (32-bit pointer) */
2773 uint32_t attributes; /* const char * (32-bit pointer) */
2774};
2775
Kevin Enderby0fc11822015-04-01 20:57:01 +00002776struct category64_t {
2777 uint64_t name; /* const char * (64-bit pointer) */
2778 uint64_t cls; /* struct class_t * (64-bit pointer) */
2779 uint64_t instanceMethods; /* struct method_list_t * (64-bit pointer) */
2780 uint64_t classMethods; /* struct method_list_t * (64-bit pointer) */
2781 uint64_t protocols; /* struct protocol_list_t * (64-bit pointer) */
2782 uint64_t instanceProperties; /* struct objc_property_list *
2783 (64-bit pointer) */
2784};
2785
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002786struct category32_t {
2787 uint32_t name; /* const char * (32-bit pointer) */
2788 uint32_t cls; /* struct class_t * (32-bit pointer) */
2789 uint32_t instanceMethods; /* struct method_list_t * (32-bit pointer) */
2790 uint32_t classMethods; /* struct method_list_t * (32-bit pointer) */
2791 uint32_t protocols; /* struct protocol_list_t * (32-bit pointer) */
2792 uint32_t instanceProperties; /* struct objc_property_list *
2793 (32-bit pointer) */
2794};
2795
Kevin Enderby0fc11822015-04-01 20:57:01 +00002796struct objc_image_info64 {
2797 uint32_t version;
2798 uint32_t flags;
2799};
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002800struct objc_image_info32 {
2801 uint32_t version;
2802 uint32_t flags;
2803};
Kevin Enderby846c0002015-04-16 17:19:59 +00002804struct imageInfo_t {
2805 uint32_t version;
2806 uint32_t flags;
2807};
Kevin Enderby0fc11822015-04-01 20:57:01 +00002808/* masks for objc_image_info.flags */
2809#define OBJC_IMAGE_IS_REPLACEMENT (1 << 0)
2810#define OBJC_IMAGE_SUPPORTS_GC (1 << 1)
2811
2812struct message_ref64 {
2813 uint64_t imp; /* IMP (64-bit pointer) */
2814 uint64_t sel; /* SEL (64-bit pointer) */
2815};
2816
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002817struct message_ref32 {
2818 uint32_t imp; /* IMP (32-bit pointer) */
2819 uint32_t sel; /* SEL (32-bit pointer) */
2820};
2821
Kevin Enderby846c0002015-04-16 17:19:59 +00002822// Objective-C 1 (32-bit only) meta data structs.
2823
2824struct objc_module_t {
2825 uint32_t version;
2826 uint32_t size;
2827 uint32_t name; /* char * (32-bit pointer) */
2828 uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */
2829};
2830
2831struct objc_symtab_t {
2832 uint32_t sel_ref_cnt;
2833 uint32_t refs; /* SEL * (32-bit pointer) */
2834 uint16_t cls_def_cnt;
2835 uint16_t cat_def_cnt;
2836 // uint32_t defs[1]; /* void * (32-bit pointer) variable size */
2837};
2838
2839struct objc_class_t {
2840 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2841 uint32_t super_class; /* struct objc_class * (32-bit pointer) */
2842 uint32_t name; /* const char * (32-bit pointer) */
2843 int32_t version;
2844 int32_t info;
2845 int32_t instance_size;
2846 uint32_t ivars; /* struct objc_ivar_list * (32-bit pointer) */
2847 uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */
2848 uint32_t cache; /* struct objc_cache * (32-bit pointer) */
2849 uint32_t protocols; /* struct objc_protocol_list * (32-bit pointer) */
2850};
2851
2852#define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask))
2853// class is not a metaclass
2854#define CLS_CLASS 0x1
2855// class is a metaclass
2856#define CLS_META 0x2
2857
2858struct objc_category_t {
2859 uint32_t category_name; /* char * (32-bit pointer) */
2860 uint32_t class_name; /* char * (32-bit pointer) */
2861 uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */
2862 uint32_t class_methods; /* struct objc_method_list * (32-bit pointer) */
2863 uint32_t protocols; /* struct objc_protocol_list * (32-bit ptr) */
2864};
2865
2866struct objc_ivar_t {
2867 uint32_t ivar_name; /* char * (32-bit pointer) */
2868 uint32_t ivar_type; /* char * (32-bit pointer) */
2869 int32_t ivar_offset;
2870};
2871
2872struct objc_ivar_list_t {
2873 int32_t ivar_count;
2874 // struct objc_ivar_t ivar_list[1]; /* variable length structure */
2875};
2876
2877struct objc_method_list_t {
2878 uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */
2879 int32_t method_count;
2880 // struct objc_method_t method_list[1]; /* variable length structure */
2881};
2882
2883struct objc_method_t {
2884 uint32_t method_name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2885 uint32_t method_types; /* char * (32-bit pointer) */
2886 uint32_t method_imp; /* IMP, aka function pointer, (*IMP)(id, SEL, ...)
2887 (32-bit pointer) */
2888};
2889
2890struct objc_protocol_list_t {
2891 uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */
2892 int32_t count;
2893 // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t *
2894 // (32-bit pointer) */
2895};
2896
2897struct objc_protocol_t {
2898 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2899 uint32_t protocol_name; /* char * (32-bit pointer) */
2900 uint32_t protocol_list; /* struct objc_protocol_list * (32-bit pointer) */
2901 uint32_t instance_methods; /* struct objc_method_description_list *
2902 (32-bit pointer) */
2903 uint32_t class_methods; /* struct objc_method_description_list *
2904 (32-bit pointer) */
2905};
2906
2907struct objc_method_description_list_t {
2908 int32_t count;
2909 // struct objc_method_description_t list[1];
2910};
2911
2912struct objc_method_description_t {
2913 uint32_t name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2914 uint32_t types; /* char * (32-bit pointer) */
2915};
2916
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002917inline void swapStruct(struct cfstring64_t &cfs) {
2918 sys::swapByteOrder(cfs.isa);
2919 sys::swapByteOrder(cfs.flags);
2920 sys::swapByteOrder(cfs.characters);
2921 sys::swapByteOrder(cfs.length);
2922}
2923
2924inline void swapStruct(struct class64_t &c) {
2925 sys::swapByteOrder(c.isa);
2926 sys::swapByteOrder(c.superclass);
2927 sys::swapByteOrder(c.cache);
2928 sys::swapByteOrder(c.vtable);
2929 sys::swapByteOrder(c.data);
2930}
2931
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002932inline void swapStruct(struct class32_t &c) {
2933 sys::swapByteOrder(c.isa);
2934 sys::swapByteOrder(c.superclass);
2935 sys::swapByteOrder(c.cache);
2936 sys::swapByteOrder(c.vtable);
2937 sys::swapByteOrder(c.data);
2938}
2939
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002940inline void swapStruct(struct class_ro64_t &cro) {
2941 sys::swapByteOrder(cro.flags);
2942 sys::swapByteOrder(cro.instanceStart);
2943 sys::swapByteOrder(cro.instanceSize);
2944 sys::swapByteOrder(cro.reserved);
2945 sys::swapByteOrder(cro.ivarLayout);
2946 sys::swapByteOrder(cro.name);
2947 sys::swapByteOrder(cro.baseMethods);
2948 sys::swapByteOrder(cro.baseProtocols);
2949 sys::swapByteOrder(cro.ivars);
2950 sys::swapByteOrder(cro.weakIvarLayout);
2951 sys::swapByteOrder(cro.baseProperties);
2952}
2953
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002954inline void swapStruct(struct class_ro32_t &cro) {
2955 sys::swapByteOrder(cro.flags);
2956 sys::swapByteOrder(cro.instanceStart);
2957 sys::swapByteOrder(cro.instanceSize);
2958 sys::swapByteOrder(cro.ivarLayout);
2959 sys::swapByteOrder(cro.name);
2960 sys::swapByteOrder(cro.baseMethods);
2961 sys::swapByteOrder(cro.baseProtocols);
2962 sys::swapByteOrder(cro.ivars);
2963 sys::swapByteOrder(cro.weakIvarLayout);
2964 sys::swapByteOrder(cro.baseProperties);
2965}
2966
Kevin Enderby0fc11822015-04-01 20:57:01 +00002967inline void swapStruct(struct method_list64_t &ml) {
2968 sys::swapByteOrder(ml.entsize);
2969 sys::swapByteOrder(ml.count);
2970}
2971
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002972inline void swapStruct(struct method_list32_t &ml) {
2973 sys::swapByteOrder(ml.entsize);
2974 sys::swapByteOrder(ml.count);
2975}
2976
Kevin Enderby0fc11822015-04-01 20:57:01 +00002977inline void swapStruct(struct method64_t &m) {
2978 sys::swapByteOrder(m.name);
2979 sys::swapByteOrder(m.types);
2980 sys::swapByteOrder(m.imp);
2981}
2982
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002983inline void swapStruct(struct method32_t &m) {
2984 sys::swapByteOrder(m.name);
2985 sys::swapByteOrder(m.types);
2986 sys::swapByteOrder(m.imp);
2987}
2988
Kevin Enderby0fc11822015-04-01 20:57:01 +00002989inline void swapStruct(struct protocol_list64_t &pl) {
2990 sys::swapByteOrder(pl.count);
2991}
2992
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002993inline void swapStruct(struct protocol_list32_t &pl) {
2994 sys::swapByteOrder(pl.count);
2995}
2996
Kevin Enderby0fc11822015-04-01 20:57:01 +00002997inline void swapStruct(struct protocol64_t &p) {
2998 sys::swapByteOrder(p.isa);
2999 sys::swapByteOrder(p.name);
3000 sys::swapByteOrder(p.protocols);
3001 sys::swapByteOrder(p.instanceMethods);
3002 sys::swapByteOrder(p.classMethods);
3003 sys::swapByteOrder(p.optionalInstanceMethods);
3004 sys::swapByteOrder(p.optionalClassMethods);
3005 sys::swapByteOrder(p.instanceProperties);
3006}
3007
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003008inline void swapStruct(struct protocol32_t &p) {
3009 sys::swapByteOrder(p.isa);
3010 sys::swapByteOrder(p.name);
3011 sys::swapByteOrder(p.protocols);
3012 sys::swapByteOrder(p.instanceMethods);
3013 sys::swapByteOrder(p.classMethods);
3014 sys::swapByteOrder(p.optionalInstanceMethods);
3015 sys::swapByteOrder(p.optionalClassMethods);
3016 sys::swapByteOrder(p.instanceProperties);
3017}
3018
Kevin Enderby0fc11822015-04-01 20:57:01 +00003019inline void swapStruct(struct ivar_list64_t &il) {
3020 sys::swapByteOrder(il.entsize);
3021 sys::swapByteOrder(il.count);
3022}
3023
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003024inline void swapStruct(struct ivar_list32_t &il) {
3025 sys::swapByteOrder(il.entsize);
3026 sys::swapByteOrder(il.count);
3027}
3028
Kevin Enderby0fc11822015-04-01 20:57:01 +00003029inline void swapStruct(struct ivar64_t &i) {
3030 sys::swapByteOrder(i.offset);
3031 sys::swapByteOrder(i.name);
3032 sys::swapByteOrder(i.type);
3033 sys::swapByteOrder(i.alignment);
3034 sys::swapByteOrder(i.size);
3035}
3036
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003037inline void swapStruct(struct ivar32_t &i) {
3038 sys::swapByteOrder(i.offset);
3039 sys::swapByteOrder(i.name);
3040 sys::swapByteOrder(i.type);
3041 sys::swapByteOrder(i.alignment);
3042 sys::swapByteOrder(i.size);
3043}
3044
Kevin Enderby0fc11822015-04-01 20:57:01 +00003045inline void swapStruct(struct objc_property_list64 &pl) {
3046 sys::swapByteOrder(pl.entsize);
3047 sys::swapByteOrder(pl.count);
3048}
3049
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003050inline void swapStruct(struct objc_property_list32 &pl) {
3051 sys::swapByteOrder(pl.entsize);
3052 sys::swapByteOrder(pl.count);
3053}
3054
Kevin Enderby0fc11822015-04-01 20:57:01 +00003055inline void swapStruct(struct objc_property64 &op) {
3056 sys::swapByteOrder(op.name);
3057 sys::swapByteOrder(op.attributes);
3058}
3059
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003060inline void swapStruct(struct objc_property32 &op) {
3061 sys::swapByteOrder(op.name);
3062 sys::swapByteOrder(op.attributes);
3063}
3064
Kevin Enderby0fc11822015-04-01 20:57:01 +00003065inline void swapStruct(struct category64_t &c) {
3066 sys::swapByteOrder(c.name);
3067 sys::swapByteOrder(c.cls);
3068 sys::swapByteOrder(c.instanceMethods);
3069 sys::swapByteOrder(c.classMethods);
3070 sys::swapByteOrder(c.protocols);
3071 sys::swapByteOrder(c.instanceProperties);
3072}
3073
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003074inline void swapStruct(struct category32_t &c) {
3075 sys::swapByteOrder(c.name);
3076 sys::swapByteOrder(c.cls);
3077 sys::swapByteOrder(c.instanceMethods);
3078 sys::swapByteOrder(c.classMethods);
3079 sys::swapByteOrder(c.protocols);
3080 sys::swapByteOrder(c.instanceProperties);
3081}
3082
Kevin Enderby0fc11822015-04-01 20:57:01 +00003083inline void swapStruct(struct objc_image_info64 &o) {
3084 sys::swapByteOrder(o.version);
3085 sys::swapByteOrder(o.flags);
3086}
3087
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003088inline void swapStruct(struct objc_image_info32 &o) {
3089 sys::swapByteOrder(o.version);
3090 sys::swapByteOrder(o.flags);
3091}
3092
Kevin Enderby846c0002015-04-16 17:19:59 +00003093inline void swapStruct(struct imageInfo_t &o) {
3094 sys::swapByteOrder(o.version);
3095 sys::swapByteOrder(o.flags);
3096}
3097
Kevin Enderby0fc11822015-04-01 20:57:01 +00003098inline void swapStruct(struct message_ref64 &mr) {
3099 sys::swapByteOrder(mr.imp);
3100 sys::swapByteOrder(mr.sel);
3101}
3102
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003103inline void swapStruct(struct message_ref32 &mr) {
3104 sys::swapByteOrder(mr.imp);
3105 sys::swapByteOrder(mr.sel);
3106}
3107
Kevin Enderby846c0002015-04-16 17:19:59 +00003108inline void swapStruct(struct objc_module_t &module) {
3109 sys::swapByteOrder(module.version);
3110 sys::swapByteOrder(module.size);
3111 sys::swapByteOrder(module.name);
3112 sys::swapByteOrder(module.symtab);
Jingyue Wufedecc42015-04-16 18:43:44 +00003113}
Kevin Enderby846c0002015-04-16 17:19:59 +00003114
3115inline void swapStruct(struct objc_symtab_t &symtab) {
3116 sys::swapByteOrder(symtab.sel_ref_cnt);
3117 sys::swapByteOrder(symtab.refs);
3118 sys::swapByteOrder(symtab.cls_def_cnt);
3119 sys::swapByteOrder(symtab.cat_def_cnt);
Jingyue Wufedecc42015-04-16 18:43:44 +00003120}
Kevin Enderby846c0002015-04-16 17:19:59 +00003121
3122inline void swapStruct(struct objc_class_t &objc_class) {
3123 sys::swapByteOrder(objc_class.isa);
3124 sys::swapByteOrder(objc_class.super_class);
3125 sys::swapByteOrder(objc_class.name);
3126 sys::swapByteOrder(objc_class.version);
3127 sys::swapByteOrder(objc_class.info);
3128 sys::swapByteOrder(objc_class.instance_size);
3129 sys::swapByteOrder(objc_class.ivars);
3130 sys::swapByteOrder(objc_class.methodLists);
3131 sys::swapByteOrder(objc_class.cache);
3132 sys::swapByteOrder(objc_class.protocols);
Jingyue Wufedecc42015-04-16 18:43:44 +00003133}
Kevin Enderby846c0002015-04-16 17:19:59 +00003134
3135inline void swapStruct(struct objc_category_t &objc_category) {
3136 sys::swapByteOrder(objc_category.category_name);
3137 sys::swapByteOrder(objc_category.class_name);
3138 sys::swapByteOrder(objc_category.instance_methods);
3139 sys::swapByteOrder(objc_category.class_methods);
3140 sys::swapByteOrder(objc_category.protocols);
3141}
3142
3143inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) {
3144 sys::swapByteOrder(objc_ivar_list.ivar_count);
3145}
3146
3147inline void swapStruct(struct objc_ivar_t &objc_ivar) {
3148 sys::swapByteOrder(objc_ivar.ivar_name);
3149 sys::swapByteOrder(objc_ivar.ivar_type);
3150 sys::swapByteOrder(objc_ivar.ivar_offset);
Jingyue Wufedecc42015-04-16 18:43:44 +00003151}
Kevin Enderby846c0002015-04-16 17:19:59 +00003152
3153inline void swapStruct(struct objc_method_list_t &method_list) {
3154 sys::swapByteOrder(method_list.obsolete);
3155 sys::swapByteOrder(method_list.method_count);
3156}
3157
3158inline void swapStruct(struct objc_method_t &method) {
3159 sys::swapByteOrder(method.method_name);
3160 sys::swapByteOrder(method.method_types);
3161 sys::swapByteOrder(method.method_imp);
3162}
3163
3164inline void swapStruct(struct objc_protocol_list_t &protocol_list) {
3165 sys::swapByteOrder(protocol_list.next);
3166 sys::swapByteOrder(protocol_list.count);
3167}
3168
3169inline void swapStruct(struct objc_protocol_t &protocol) {
3170 sys::swapByteOrder(protocol.isa);
3171 sys::swapByteOrder(protocol.protocol_name);
3172 sys::swapByteOrder(protocol.protocol_list);
3173 sys::swapByteOrder(protocol.instance_methods);
3174 sys::swapByteOrder(protocol.class_methods);
3175}
3176
3177inline void swapStruct(struct objc_method_description_list_t &mdl) {
3178 sys::swapByteOrder(mdl.count);
3179}
3180
3181inline void swapStruct(struct objc_method_description_t &md) {
3182 sys::swapByteOrder(md.name);
3183 sys::swapByteOrder(md.types);
3184}
3185
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003186static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
3187 struct DisassembleInfo *info);
3188
3189// get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
3190// to an Objective-C class and returns the class name. It is also passed the
3191// address of the pointer, so when the pointer is zero as it can be in an .o
3192// file, that is used to look for an external relocation entry with a symbol
3193// name.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003194static const char *get_objc2_64bit_class_name(uint64_t pointer_value,
3195 uint64_t ReferenceValue,
3196 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003197 const char *r;
3198 uint32_t offset, left;
3199 SectionRef S;
3200
3201 // The pointer_value can be 0 in an object file and have a relocation
3202 // entry for the class symbol at the ReferenceValue (the address of the
3203 // pointer).
3204 if (pointer_value == 0) {
3205 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3206 if (r == nullptr || left < sizeof(uint64_t))
3207 return nullptr;
3208 uint64_t n_value;
3209 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3210 if (symbol_name == nullptr)
3211 return nullptr;
Hans Wennborgdb53e302014-10-23 21:59:17 +00003212 const char *class_name = strrchr(symbol_name, '$');
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003213 if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0')
3214 return class_name + 2;
3215 else
3216 return nullptr;
3217 }
3218
3219 // The case were the pointer_value is non-zero and points to a class defined
3220 // in this Mach-O file.
3221 r = get_pointer_64(pointer_value, offset, left, S, info);
3222 if (r == nullptr || left < sizeof(struct class64_t))
3223 return nullptr;
3224 struct class64_t c;
3225 memcpy(&c, r, sizeof(struct class64_t));
3226 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3227 swapStruct(c);
3228 if (c.data == 0)
3229 return nullptr;
3230 r = get_pointer_64(c.data, offset, left, S, info);
3231 if (r == nullptr || left < sizeof(struct class_ro64_t))
3232 return nullptr;
3233 struct class_ro64_t cro;
3234 memcpy(&cro, r, sizeof(struct class_ro64_t));
3235 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3236 swapStruct(cro);
3237 if (cro.name == 0)
3238 return nullptr;
3239 const char *name = get_pointer_64(cro.name, offset, left, S, info);
3240 return name;
3241}
3242
3243// get_objc2_64bit_cfstring_name is used for disassembly and is passed a
3244// pointer to a cfstring and returns its name or nullptr.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003245static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue,
3246 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003247 const char *r, *name;
3248 uint32_t offset, left;
3249 SectionRef S;
3250 struct cfstring64_t cfs;
3251 uint64_t cfs_characters;
3252
3253 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3254 if (r == nullptr || left < sizeof(struct cfstring64_t))
3255 return nullptr;
3256 memcpy(&cfs, r, sizeof(struct cfstring64_t));
3257 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3258 swapStruct(cfs);
3259 if (cfs.characters == 0) {
3260 uint64_t n_value;
3261 const char *symbol_name = get_symbol_64(
3262 offset + offsetof(struct cfstring64_t, characters), S, info, n_value);
3263 if (symbol_name == nullptr)
3264 return nullptr;
3265 cfs_characters = n_value;
3266 } else
3267 cfs_characters = cfs.characters;
3268 name = get_pointer_64(cfs_characters, offset, left, S, info);
3269
3270 return name;
3271}
3272
3273// get_objc2_64bit_selref() is used for disassembly and is passed a the address
3274// of a pointer to an Objective-C selector reference when the pointer value is
3275// zero as in a .o file and is likely to have a external relocation entry with
3276// who's symbol's n_value is the real pointer to the selector name. If that is
3277// the case the real pointer to the selector name is returned else 0 is
3278// returned
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003279static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue,
3280 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003281 uint32_t offset, left;
3282 SectionRef S;
3283
3284 const char *r = get_pointer_64(ReferenceValue, offset, left, S, info);
3285 if (r == nullptr || left < sizeof(uint64_t))
3286 return 0;
3287 uint64_t n_value;
3288 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3289 if (symbol_name == nullptr)
3290 return 0;
3291 return n_value;
3292}
3293
Kevin Enderby0fc11822015-04-01 20:57:01 +00003294static const SectionRef get_section(MachOObjectFile *O, const char *segname,
3295 const char *sectname) {
3296 for (const SectionRef &Section : O->sections()) {
3297 StringRef SectName;
3298 Section.getName(SectName);
3299 DataRefImpl Ref = Section.getRawDataRefImpl();
3300 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3301 if (SegName == segname && SectName == sectname)
3302 return Section;
3303 }
3304 return SectionRef();
3305}
3306
3307static void
3308walk_pointer_list_64(const char *listname, const SectionRef S,
3309 MachOObjectFile *O, struct DisassembleInfo *info,
3310 void (*func)(uint64_t, struct DisassembleInfo *info)) {
3311 if (S == SectionRef())
3312 return;
3313
3314 StringRef SectName;
3315 S.getName(SectName);
3316 DataRefImpl Ref = S.getRawDataRefImpl();
3317 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3318 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3319
3320 StringRef BytesStr;
3321 S.getContents(BytesStr);
3322 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3323
3324 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) {
3325 uint32_t left = S.getSize() - i;
3326 uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t);
3327 uint64_t p = 0;
3328 memcpy(&p, Contents + i, size);
3329 if (i + sizeof(uint64_t) > S.getSize())
3330 outs() << listname << " list pointer extends past end of (" << SegName
3331 << "," << SectName << ") section\n";
3332 outs() << format("%016" PRIx64, S.getAddress() + i) << " ";
3333
3334 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3335 sys::swapByteOrder(p);
3336
3337 uint64_t n_value = 0;
3338 const char *name = get_symbol_64(i, S, info, n_value, p);
3339 if (name == nullptr)
3340 name = get_dyld_bind_info_symbolname(S.getAddress() + i, info);
3341
3342 if (n_value != 0) {
3343 outs() << format("0x%" PRIx64, n_value);
3344 if (p != 0)
3345 outs() << " + " << format("0x%" PRIx64, p);
3346 } else
3347 outs() << format("0x%" PRIx64, p);
3348 if (name != nullptr)
3349 outs() << " " << name;
3350 outs() << "\n";
3351
3352 p += n_value;
3353 if (func)
3354 func(p, info);
3355 }
3356}
3357
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003358static void
3359walk_pointer_list_32(const char *listname, const SectionRef S,
3360 MachOObjectFile *O, struct DisassembleInfo *info,
3361 void (*func)(uint32_t, struct DisassembleInfo *info)) {
3362 if (S == SectionRef())
3363 return;
3364
3365 StringRef SectName;
3366 S.getName(SectName);
3367 DataRefImpl Ref = S.getRawDataRefImpl();
3368 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3369 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3370
3371 StringRef BytesStr;
3372 S.getContents(BytesStr);
3373 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3374
3375 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) {
3376 uint32_t left = S.getSize() - i;
3377 uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t);
3378 uint32_t p = 0;
3379 memcpy(&p, Contents + i, size);
3380 if (i + sizeof(uint32_t) > S.getSize())
3381 outs() << listname << " list pointer extends past end of (" << SegName
3382 << "," << SectName << ") section\n";
Kevin Enderbycf261312015-04-06 22:33:43 +00003383 uint32_t Address = S.getAddress() + i;
3384 outs() << format("%08" PRIx32, Address) << " ";
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003385
3386 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3387 sys::swapByteOrder(p);
3388 outs() << format("0x%" PRIx32, p);
3389
3390 const char *name = get_symbol_32(i, S, info, p);
3391 if (name != nullptr)
3392 outs() << " " << name;
3393 outs() << "\n";
3394
3395 if (func)
3396 func(p, info);
3397 }
3398}
3399
3400static void print_layout_map(const char *layout_map, uint32_t left) {
Kevin Enderbya59824a2015-10-06 22:27:08 +00003401 if (layout_map == nullptr)
3402 return;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003403 outs() << " layout map: ";
3404 do {
3405 outs() << format("0x%02" PRIx32, (*layout_map) & 0xff) << " ";
3406 left--;
3407 layout_map++;
3408 } while (*layout_map != '\0' && left != 0);
3409 outs() << "\n";
3410}
3411
Kevin Enderby0fc11822015-04-01 20:57:01 +00003412static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) {
3413 uint32_t offset, left;
3414 SectionRef S;
3415 const char *layout_map;
3416
3417 if (p == 0)
3418 return;
3419 layout_map = get_pointer_64(p, offset, left, S, info);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003420 print_layout_map(layout_map, left);
3421}
3422
3423static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) {
3424 uint32_t offset, left;
3425 SectionRef S;
3426 const char *layout_map;
3427
3428 if (p == 0)
3429 return;
3430 layout_map = get_pointer_32(p, offset, left, S, info);
3431 print_layout_map(layout_map, left);
Kevin Enderby0fc11822015-04-01 20:57:01 +00003432}
3433
3434static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info,
3435 const char *indent) {
3436 struct method_list64_t ml;
3437 struct method64_t m;
3438 const char *r;
3439 uint32_t offset, xoffset, left, i;
3440 SectionRef S, xS;
3441 const char *name, *sym_name;
3442 uint64_t n_value;
3443
3444 r = get_pointer_64(p, offset, left, S, info);
3445 if (r == nullptr)
3446 return;
3447 memset(&ml, '\0', sizeof(struct method_list64_t));
3448 if (left < sizeof(struct method_list64_t)) {
3449 memcpy(&ml, r, left);
3450 outs() << " (method_list_t entends past the end of the section)\n";
3451 } else
3452 memcpy(&ml, r, sizeof(struct method_list64_t));
3453 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3454 swapStruct(ml);
3455 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3456 outs() << indent << "\t\t count " << ml.count << "\n";
3457
3458 p += sizeof(struct method_list64_t);
3459 offset += sizeof(struct method_list64_t);
3460 for (i = 0; i < ml.count; i++) {
3461 r = get_pointer_64(p, offset, left, S, info);
3462 if (r == nullptr)
3463 return;
3464 memset(&m, '\0', sizeof(struct method64_t));
3465 if (left < sizeof(struct method64_t)) {
Kevin Enderbya59824a2015-10-06 22:27:08 +00003466 memcpy(&m, r, left);
3467 outs() << indent << " (method_t extends past the end of the section)\n";
Kevin Enderby0fc11822015-04-01 20:57:01 +00003468 } else
3469 memcpy(&m, r, sizeof(struct method64_t));
3470 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3471 swapStruct(m);
3472
3473 outs() << indent << "\t\t name ";
3474 sym_name = get_symbol_64(offset + offsetof(struct method64_t, name), S,
3475 info, n_value, m.name);
3476 if (n_value != 0) {
3477 if (info->verbose && sym_name != nullptr)
3478 outs() << sym_name;
3479 else
3480 outs() << format("0x%" PRIx64, n_value);
3481 if (m.name != 0)
3482 outs() << " + " << format("0x%" PRIx64, m.name);
3483 } else
3484 outs() << format("0x%" PRIx64, m.name);
3485 name = get_pointer_64(m.name + n_value, xoffset, left, xS, info);
3486 if (name != nullptr)
3487 outs() << format(" %.*s", left, name);
3488 outs() << "\n";
3489
3490 outs() << indent << "\t\t types ";
3491 sym_name = get_symbol_64(offset + offsetof(struct method64_t, types), S,
3492 info, n_value, m.types);
3493 if (n_value != 0) {
3494 if (info->verbose && sym_name != nullptr)
3495 outs() << sym_name;
3496 else
3497 outs() << format("0x%" PRIx64, n_value);
3498 if (m.types != 0)
3499 outs() << " + " << format("0x%" PRIx64, m.types);
3500 } else
3501 outs() << format("0x%" PRIx64, m.types);
3502 name = get_pointer_64(m.types + n_value, xoffset, left, xS, info);
3503 if (name != nullptr)
3504 outs() << format(" %.*s", left, name);
3505 outs() << "\n";
3506
3507 outs() << indent << "\t\t imp ";
3508 name = get_symbol_64(offset + offsetof(struct method64_t, imp), S, info,
3509 n_value, m.imp);
3510 if (info->verbose && name == nullptr) {
3511 if (n_value != 0) {
3512 outs() << format("0x%" PRIx64, n_value) << " ";
3513 if (m.imp != 0)
3514 outs() << "+ " << format("0x%" PRIx64, m.imp) << " ";
3515 } else
3516 outs() << format("0x%" PRIx64, m.imp) << " ";
3517 }
3518 if (name != nullptr)
3519 outs() << name;
3520 outs() << "\n";
3521
3522 p += sizeof(struct method64_t);
3523 offset += sizeof(struct method64_t);
3524 }
3525}
3526
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003527static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info,
3528 const char *indent) {
3529 struct method_list32_t ml;
3530 struct method32_t m;
Kevin Enderby846c0002015-04-16 17:19:59 +00003531 const char *r, *name;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003532 uint32_t offset, xoffset, left, i;
3533 SectionRef S, xS;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003534
3535 r = get_pointer_32(p, offset, left, S, info);
3536 if (r == nullptr)
3537 return;
3538 memset(&ml, '\0', sizeof(struct method_list32_t));
3539 if (left < sizeof(struct method_list32_t)) {
3540 memcpy(&ml, r, left);
3541 outs() << " (method_list_t entends past the end of the section)\n";
3542 } else
3543 memcpy(&ml, r, sizeof(struct method_list32_t));
3544 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3545 swapStruct(ml);
3546 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3547 outs() << indent << "\t\t count " << ml.count << "\n";
3548
3549 p += sizeof(struct method_list32_t);
3550 offset += sizeof(struct method_list32_t);
3551 for (i = 0; i < ml.count; i++) {
3552 r = get_pointer_32(p, offset, left, S, info);
3553 if (r == nullptr)
3554 return;
3555 memset(&m, '\0', sizeof(struct method32_t));
3556 if (left < sizeof(struct method32_t)) {
3557 memcpy(&ml, r, left);
3558 outs() << indent << " (method_t entends past the end of the section)\n";
3559 } else
3560 memcpy(&m, r, sizeof(struct method32_t));
3561 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3562 swapStruct(m);
3563
3564 outs() << indent << "\t\t name " << format("0x%" PRIx32, m.name);
3565 name = get_pointer_32(m.name, xoffset, left, xS, info);
3566 if (name != nullptr)
3567 outs() << format(" %.*s", left, name);
3568 outs() << "\n";
3569
3570 outs() << indent << "\t\t types " << format("0x%" PRIx32, m.types);
3571 name = get_pointer_32(m.types, xoffset, left, xS, info);
3572 if (name != nullptr)
3573 outs() << format(" %.*s", left, name);
3574 outs() << "\n";
3575
3576 outs() << indent << "\t\t imp " << format("0x%" PRIx32, m.imp);
3577 name = get_symbol_32(offset + offsetof(struct method32_t, imp), S, info,
3578 m.imp);
3579 if (name != nullptr)
3580 outs() << " " << name;
3581 outs() << "\n";
3582
3583 p += sizeof(struct method32_t);
3584 offset += sizeof(struct method32_t);
3585 }
3586}
3587
Kevin Enderby846c0002015-04-16 17:19:59 +00003588static bool print_method_list(uint32_t p, struct DisassembleInfo *info) {
3589 uint32_t offset, left, xleft;
3590 SectionRef S;
3591 struct objc_method_list_t method_list;
3592 struct objc_method_t method;
3593 const char *r, *methods, *name, *SymbolName;
3594 int32_t i;
3595
3596 r = get_pointer_32(p, offset, left, S, info, true);
3597 if (r == nullptr)
3598 return true;
3599
3600 outs() << "\n";
3601 if (left > sizeof(struct objc_method_list_t)) {
3602 memcpy(&method_list, r, sizeof(struct objc_method_list_t));
3603 } else {
3604 outs() << "\t\t objc_method_list extends past end of the section\n";
3605 memset(&method_list, '\0', sizeof(struct objc_method_list_t));
3606 memcpy(&method_list, r, left);
3607 }
3608 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3609 swapStruct(method_list);
3610
3611 outs() << "\t\t obsolete "
3612 << format("0x%08" PRIx32, method_list.obsolete) << "\n";
3613 outs() << "\t\t method_count " << method_list.method_count << "\n";
3614
3615 methods = r + sizeof(struct objc_method_list_t);
3616 for (i = 0; i < method_list.method_count; i++) {
3617 if ((i + 1) * sizeof(struct objc_method_t) > left) {
3618 outs() << "\t\t remaining method's extend past the of the section\n";
3619 break;
3620 }
3621 memcpy(&method, methods + i * sizeof(struct objc_method_t),
3622 sizeof(struct objc_method_t));
3623 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3624 swapStruct(method);
3625
3626 outs() << "\t\t method_name "
3627 << format("0x%08" PRIx32, method.method_name);
3628 if (info->verbose) {
3629 name = get_pointer_32(method.method_name, offset, xleft, S, info, true);
3630 if (name != nullptr)
3631 outs() << format(" %.*s", xleft, name);
3632 else
3633 outs() << " (not in an __OBJC section)";
3634 }
3635 outs() << "\n";
3636
3637 outs() << "\t\t method_types "
3638 << format("0x%08" PRIx32, method.method_types);
3639 if (info->verbose) {
3640 name = get_pointer_32(method.method_types, offset, xleft, S, info, true);
3641 if (name != nullptr)
3642 outs() << format(" %.*s", xleft, name);
3643 else
3644 outs() << " (not in an __OBJC section)";
3645 }
3646 outs() << "\n";
3647
3648 outs() << "\t\t method_imp "
3649 << format("0x%08" PRIx32, method.method_imp) << " ";
3650 if (info->verbose) {
3651 SymbolName = GuessSymbolName(method.method_imp, info->AddrMap);
3652 if (SymbolName != nullptr)
3653 outs() << SymbolName;
3654 }
3655 outs() << "\n";
3656 }
3657 return false;
3658}
3659
Kevin Enderby0fc11822015-04-01 20:57:01 +00003660static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) {
3661 struct protocol_list64_t pl;
3662 uint64_t q, n_value;
3663 struct protocol64_t pc;
3664 const char *r;
3665 uint32_t offset, xoffset, left, i;
3666 SectionRef S, xS;
3667 const char *name, *sym_name;
3668
3669 r = get_pointer_64(p, offset, left, S, info);
3670 if (r == nullptr)
3671 return;
3672 memset(&pl, '\0', sizeof(struct protocol_list64_t));
3673 if (left < sizeof(struct protocol_list64_t)) {
3674 memcpy(&pl, r, left);
3675 outs() << " (protocol_list_t entends past the end of the section)\n";
3676 } else
3677 memcpy(&pl, r, sizeof(struct protocol_list64_t));
3678 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3679 swapStruct(pl);
3680 outs() << " count " << pl.count << "\n";
3681
3682 p += sizeof(struct protocol_list64_t);
3683 offset += sizeof(struct protocol_list64_t);
3684 for (i = 0; i < pl.count; i++) {
3685 r = get_pointer_64(p, offset, left, S, info);
3686 if (r == nullptr)
3687 return;
3688 q = 0;
3689 if (left < sizeof(uint64_t)) {
3690 memcpy(&q, r, left);
3691 outs() << " (protocol_t * entends past the end of the section)\n";
3692 } else
3693 memcpy(&q, r, sizeof(uint64_t));
3694 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3695 sys::swapByteOrder(q);
3696
3697 outs() << "\t\t list[" << i << "] ";
3698 sym_name = get_symbol_64(offset, S, info, n_value, q);
3699 if (n_value != 0) {
3700 if (info->verbose && sym_name != nullptr)
3701 outs() << sym_name;
3702 else
3703 outs() << format("0x%" PRIx64, n_value);
3704 if (q != 0)
3705 outs() << " + " << format("0x%" PRIx64, q);
3706 } else
3707 outs() << format("0x%" PRIx64, q);
3708 outs() << " (struct protocol_t *)\n";
3709
3710 r = get_pointer_64(q + n_value, offset, left, S, info);
3711 if (r == nullptr)
3712 return;
3713 memset(&pc, '\0', sizeof(struct protocol64_t));
3714 if (left < sizeof(struct protocol64_t)) {
3715 memcpy(&pc, r, left);
3716 outs() << " (protocol_t entends past the end of the section)\n";
3717 } else
3718 memcpy(&pc, r, sizeof(struct protocol64_t));
3719 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3720 swapStruct(pc);
3721
3722 outs() << "\t\t\t isa " << format("0x%" PRIx64, pc.isa) << "\n";
3723
3724 outs() << "\t\t\t name ";
3725 sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name), S,
3726 info, n_value, pc.name);
3727 if (n_value != 0) {
3728 if (info->verbose && sym_name != nullptr)
3729 outs() << sym_name;
3730 else
3731 outs() << format("0x%" PRIx64, n_value);
3732 if (pc.name != 0)
3733 outs() << " + " << format("0x%" PRIx64, pc.name);
3734 } else
3735 outs() << format("0x%" PRIx64, pc.name);
3736 name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info);
3737 if (name != nullptr)
3738 outs() << format(" %.*s", left, name);
3739 outs() << "\n";
3740
3741 outs() << "\t\t\tprotocols " << format("0x%" PRIx64, pc.protocols) << "\n";
3742
3743 outs() << "\t\t instanceMethods ";
3744 sym_name =
3745 get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods),
3746 S, info, n_value, pc.instanceMethods);
3747 if (n_value != 0) {
3748 if (info->verbose && sym_name != nullptr)
3749 outs() << sym_name;
3750 else
3751 outs() << format("0x%" PRIx64, n_value);
3752 if (pc.instanceMethods != 0)
3753 outs() << " + " << format("0x%" PRIx64, pc.instanceMethods);
3754 } else
3755 outs() << format("0x%" PRIx64, pc.instanceMethods);
3756 outs() << " (struct method_list_t *)\n";
3757 if (pc.instanceMethods + n_value != 0)
3758 print_method_list64_t(pc.instanceMethods + n_value, info, "\t");
3759
3760 outs() << "\t\t classMethods ";
3761 sym_name =
3762 get_symbol_64(offset + offsetof(struct protocol64_t, classMethods), S,
3763 info, n_value, pc.classMethods);
3764 if (n_value != 0) {
3765 if (info->verbose && sym_name != nullptr)
3766 outs() << sym_name;
3767 else
3768 outs() << format("0x%" PRIx64, n_value);
3769 if (pc.classMethods != 0)
3770 outs() << " + " << format("0x%" PRIx64, pc.classMethods);
3771 } else
3772 outs() << format("0x%" PRIx64, pc.classMethods);
3773 outs() << " (struct method_list_t *)\n";
3774 if (pc.classMethods + n_value != 0)
3775 print_method_list64_t(pc.classMethods + n_value, info, "\t");
3776
3777 outs() << "\t optionalInstanceMethods "
3778 << format("0x%" PRIx64, pc.optionalInstanceMethods) << "\n";
3779 outs() << "\t optionalClassMethods "
3780 << format("0x%" PRIx64, pc.optionalClassMethods) << "\n";
3781 outs() << "\t instanceProperties "
3782 << format("0x%" PRIx64, pc.instanceProperties) << "\n";
3783
3784 p += sizeof(uint64_t);
3785 offset += sizeof(uint64_t);
3786 }
3787}
3788
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003789static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) {
3790 struct protocol_list32_t pl;
3791 uint32_t q;
3792 struct protocol32_t pc;
3793 const char *r;
3794 uint32_t offset, xoffset, left, i;
3795 SectionRef S, xS;
3796 const char *name;
3797
3798 r = get_pointer_32(p, offset, left, S, info);
3799 if (r == nullptr)
3800 return;
3801 memset(&pl, '\0', sizeof(struct protocol_list32_t));
3802 if (left < sizeof(struct protocol_list32_t)) {
3803 memcpy(&pl, r, left);
3804 outs() << " (protocol_list_t entends past the end of the section)\n";
3805 } else
3806 memcpy(&pl, r, sizeof(struct protocol_list32_t));
3807 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3808 swapStruct(pl);
3809 outs() << " count " << pl.count << "\n";
3810
3811 p += sizeof(struct protocol_list32_t);
3812 offset += sizeof(struct protocol_list32_t);
3813 for (i = 0; i < pl.count; i++) {
3814 r = get_pointer_32(p, offset, left, S, info);
3815 if (r == nullptr)
3816 return;
3817 q = 0;
3818 if (left < sizeof(uint32_t)) {
3819 memcpy(&q, r, left);
3820 outs() << " (protocol_t * entends past the end of the section)\n";
3821 } else
3822 memcpy(&q, r, sizeof(uint32_t));
3823 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3824 sys::swapByteOrder(q);
3825 outs() << "\t\t list[" << i << "] " << format("0x%" PRIx32, q)
3826 << " (struct protocol_t *)\n";
3827 r = get_pointer_32(q, offset, left, S, info);
3828 if (r == nullptr)
3829 return;
3830 memset(&pc, '\0', sizeof(struct protocol32_t));
3831 if (left < sizeof(struct protocol32_t)) {
3832 memcpy(&pc, r, left);
3833 outs() << " (protocol_t entends past the end of the section)\n";
3834 } else
3835 memcpy(&pc, r, sizeof(struct protocol32_t));
3836 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3837 swapStruct(pc);
3838 outs() << "\t\t\t isa " << format("0x%" PRIx32, pc.isa) << "\n";
3839 outs() << "\t\t\t name " << format("0x%" PRIx32, pc.name);
3840 name = get_pointer_32(pc.name, xoffset, left, xS, info);
3841 if (name != nullptr)
3842 outs() << format(" %.*s", left, name);
3843 outs() << "\n";
3844 outs() << "\t\t\tprotocols " << format("0x%" PRIx32, pc.protocols) << "\n";
3845 outs() << "\t\t instanceMethods "
3846 << format("0x%" PRIx32, pc.instanceMethods)
3847 << " (struct method_list_t *)\n";
3848 if (pc.instanceMethods != 0)
3849 print_method_list32_t(pc.instanceMethods, info, "\t");
3850 outs() << "\t\t classMethods " << format("0x%" PRIx32, pc.classMethods)
3851 << " (struct method_list_t *)\n";
3852 if (pc.classMethods != 0)
3853 print_method_list32_t(pc.classMethods, info, "\t");
3854 outs() << "\t optionalInstanceMethods "
3855 << format("0x%" PRIx32, pc.optionalInstanceMethods) << "\n";
3856 outs() << "\t optionalClassMethods "
3857 << format("0x%" PRIx32, pc.optionalClassMethods) << "\n";
3858 outs() << "\t instanceProperties "
3859 << format("0x%" PRIx32, pc.instanceProperties) << "\n";
3860 p += sizeof(uint32_t);
3861 offset += sizeof(uint32_t);
3862 }
3863}
3864
Kevin Enderby846c0002015-04-16 17:19:59 +00003865static void print_indent(uint32_t indent) {
3866 for (uint32_t i = 0; i < indent;) {
3867 if (indent - i >= 8) {
3868 outs() << "\t";
3869 i += 8;
3870 } else {
3871 for (uint32_t j = i; j < indent; j++)
3872 outs() << " ";
3873 return;
3874 }
3875 }
3876}
3877
3878static bool print_method_description_list(uint32_t p, uint32_t indent,
3879 struct DisassembleInfo *info) {
3880 uint32_t offset, left, xleft;
3881 SectionRef S;
3882 struct objc_method_description_list_t mdl;
3883 struct objc_method_description_t md;
3884 const char *r, *list, *name;
3885 int32_t i;
3886
3887 r = get_pointer_32(p, offset, left, S, info, true);
3888 if (r == nullptr)
3889 return true;
3890
3891 outs() << "\n";
3892 if (left > sizeof(struct objc_method_description_list_t)) {
3893 memcpy(&mdl, r, sizeof(struct objc_method_description_list_t));
3894 } else {
3895 print_indent(indent);
3896 outs() << " objc_method_description_list extends past end of the section\n";
3897 memset(&mdl, '\0', sizeof(struct objc_method_description_list_t));
3898 memcpy(&mdl, r, left);
3899 }
3900 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3901 swapStruct(mdl);
3902
3903 print_indent(indent);
3904 outs() << " count " << mdl.count << "\n";
3905
3906 list = r + sizeof(struct objc_method_description_list_t);
3907 for (i = 0; i < mdl.count; i++) {
3908 if ((i + 1) * sizeof(struct objc_method_description_t) > left) {
3909 print_indent(indent);
3910 outs() << " remaining list entries extend past the of the section\n";
3911 break;
3912 }
3913 print_indent(indent);
3914 outs() << " list[" << i << "]\n";
3915 memcpy(&md, list + i * sizeof(struct objc_method_description_t),
3916 sizeof(struct objc_method_description_t));
3917 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3918 swapStruct(md);
3919
3920 print_indent(indent);
3921 outs() << " name " << format("0x%08" PRIx32, md.name);
3922 if (info->verbose) {
3923 name = get_pointer_32(md.name, offset, xleft, S, info, true);
3924 if (name != nullptr)
3925 outs() << format(" %.*s", xleft, name);
3926 else
3927 outs() << " (not in an __OBJC section)";
3928 }
3929 outs() << "\n";
3930
3931 print_indent(indent);
3932 outs() << " types " << format("0x%08" PRIx32, md.types);
3933 if (info->verbose) {
3934 name = get_pointer_32(md.types, offset, xleft, S, info, true);
3935 if (name != nullptr)
3936 outs() << format(" %.*s", xleft, name);
3937 else
3938 outs() << " (not in an __OBJC section)";
3939 }
3940 outs() << "\n";
3941 }
3942 return false;
3943}
3944
3945static bool print_protocol_list(uint32_t p, uint32_t indent,
3946 struct DisassembleInfo *info);
3947
3948static bool print_protocol(uint32_t p, uint32_t indent,
3949 struct DisassembleInfo *info) {
3950 uint32_t offset, left;
3951 SectionRef S;
3952 struct objc_protocol_t protocol;
3953 const char *r, *name;
3954
3955 r = get_pointer_32(p, offset, left, S, info, true);
3956 if (r == nullptr)
3957 return true;
3958
3959 outs() << "\n";
3960 if (left >= sizeof(struct objc_protocol_t)) {
3961 memcpy(&protocol, r, sizeof(struct objc_protocol_t));
3962 } else {
3963 print_indent(indent);
3964 outs() << " Protocol extends past end of the section\n";
3965 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
3966 memcpy(&protocol, r, left);
3967 }
3968 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3969 swapStruct(protocol);
3970
3971 print_indent(indent);
3972 outs() << " isa " << format("0x%08" PRIx32, protocol.isa)
3973 << "\n";
3974
3975 print_indent(indent);
3976 outs() << " protocol_name "
3977 << format("0x%08" PRIx32, protocol.protocol_name);
3978 if (info->verbose) {
3979 name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true);
3980 if (name != nullptr)
3981 outs() << format(" %.*s", left, name);
3982 else
3983 outs() << " (not in an __OBJC section)";
3984 }
3985 outs() << "\n";
3986
3987 print_indent(indent);
3988 outs() << " protocol_list "
3989 << format("0x%08" PRIx32, protocol.protocol_list);
3990 if (print_protocol_list(protocol.protocol_list, indent + 4, info))
3991 outs() << " (not in an __OBJC section)\n";
3992
3993 print_indent(indent);
3994 outs() << " instance_methods "
3995 << format("0x%08" PRIx32, protocol.instance_methods);
3996 if (print_method_description_list(protocol.instance_methods, indent, info))
3997 outs() << " (not in an __OBJC section)\n";
3998
3999 print_indent(indent);
4000 outs() << " class_methods "
4001 << format("0x%08" PRIx32, protocol.class_methods);
4002 if (print_method_description_list(protocol.class_methods, indent, info))
4003 outs() << " (not in an __OBJC section)\n";
4004
4005 return false;
4006}
4007
4008static bool print_protocol_list(uint32_t p, uint32_t indent,
4009 struct DisassembleInfo *info) {
4010 uint32_t offset, left, l;
4011 SectionRef S;
4012 struct objc_protocol_list_t protocol_list;
4013 const char *r, *list;
4014 int32_t i;
4015
4016 r = get_pointer_32(p, offset, left, S, info, true);
4017 if (r == nullptr)
4018 return true;
4019
4020 outs() << "\n";
4021 if (left > sizeof(struct objc_protocol_list_t)) {
4022 memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t));
4023 } else {
4024 outs() << "\t\t objc_protocol_list_t extends past end of the section\n";
4025 memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t));
4026 memcpy(&protocol_list, r, left);
4027 }
4028 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4029 swapStruct(protocol_list);
4030
4031 print_indent(indent);
4032 outs() << " next " << format("0x%08" PRIx32, protocol_list.next)
4033 << "\n";
4034 print_indent(indent);
4035 outs() << " count " << protocol_list.count << "\n";
4036
4037 list = r + sizeof(struct objc_protocol_list_t);
4038 for (i = 0; i < protocol_list.count; i++) {
4039 if ((i + 1) * sizeof(uint32_t) > left) {
4040 outs() << "\t\t remaining list entries extend past the of the section\n";
4041 break;
4042 }
4043 memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t));
4044 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4045 sys::swapByteOrder(l);
4046
4047 print_indent(indent);
4048 outs() << " list[" << i << "] " << format("0x%08" PRIx32, l);
4049 if (print_protocol(l, indent, info))
4050 outs() << "(not in an __OBJC section)\n";
4051 }
4052 return false;
4053}
4054
Kevin Enderby0fc11822015-04-01 20:57:01 +00004055static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) {
4056 struct ivar_list64_t il;
4057 struct ivar64_t i;
4058 const char *r;
4059 uint32_t offset, xoffset, left, j;
4060 SectionRef S, xS;
4061 const char *name, *sym_name, *ivar_offset_p;
4062 uint64_t ivar_offset, n_value;
4063
4064 r = get_pointer_64(p, offset, left, S, info);
4065 if (r == nullptr)
4066 return;
4067 memset(&il, '\0', sizeof(struct ivar_list64_t));
4068 if (left < sizeof(struct ivar_list64_t)) {
4069 memcpy(&il, r, left);
4070 outs() << " (ivar_list_t entends past the end of the section)\n";
4071 } else
4072 memcpy(&il, r, sizeof(struct ivar_list64_t));
4073 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4074 swapStruct(il);
4075 outs() << " entsize " << il.entsize << "\n";
4076 outs() << " count " << il.count << "\n";
4077
4078 p += sizeof(struct ivar_list64_t);
4079 offset += sizeof(struct ivar_list64_t);
4080 for (j = 0; j < il.count; j++) {
4081 r = get_pointer_64(p, offset, left, S, info);
4082 if (r == nullptr)
4083 return;
4084 memset(&i, '\0', sizeof(struct ivar64_t));
4085 if (left < sizeof(struct ivar64_t)) {
4086 memcpy(&i, r, left);
4087 outs() << " (ivar_t entends past the end of the section)\n";
4088 } else
4089 memcpy(&i, r, sizeof(struct ivar64_t));
4090 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4091 swapStruct(i);
4092
4093 outs() << "\t\t\t offset ";
4094 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset), S,
4095 info, n_value, i.offset);
4096 if (n_value != 0) {
4097 if (info->verbose && sym_name != nullptr)
4098 outs() << sym_name;
4099 else
4100 outs() << format("0x%" PRIx64, n_value);
4101 if (i.offset != 0)
4102 outs() << " + " << format("0x%" PRIx64, i.offset);
4103 } else
4104 outs() << format("0x%" PRIx64, i.offset);
4105 ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info);
4106 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4107 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4108 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4109 sys::swapByteOrder(ivar_offset);
4110 outs() << " " << ivar_offset << "\n";
4111 } else
4112 outs() << "\n";
4113
4114 outs() << "\t\t\t name ";
4115 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name), S, info,
4116 n_value, i.name);
4117 if (n_value != 0) {
4118 if (info->verbose && sym_name != nullptr)
4119 outs() << sym_name;
4120 else
4121 outs() << format("0x%" PRIx64, n_value);
4122 if (i.name != 0)
4123 outs() << " + " << format("0x%" PRIx64, i.name);
4124 } else
4125 outs() << format("0x%" PRIx64, i.name);
4126 name = get_pointer_64(i.name + n_value, xoffset, left, xS, info);
4127 if (name != nullptr)
4128 outs() << format(" %.*s", left, name);
4129 outs() << "\n";
4130
4131 outs() << "\t\t\t type ";
4132 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type), S, info,
4133 n_value, i.name);
4134 name = get_pointer_64(i.type + n_value, xoffset, left, xS, info);
4135 if (n_value != 0) {
4136 if (info->verbose && sym_name != nullptr)
4137 outs() << sym_name;
4138 else
4139 outs() << format("0x%" PRIx64, n_value);
4140 if (i.type != 0)
4141 outs() << " + " << format("0x%" PRIx64, i.type);
4142 } else
4143 outs() << format("0x%" PRIx64, i.type);
4144 if (name != nullptr)
4145 outs() << format(" %.*s", left, name);
4146 outs() << "\n";
4147
4148 outs() << "\t\t\talignment " << i.alignment << "\n";
4149 outs() << "\t\t\t size " << i.size << "\n";
4150
4151 p += sizeof(struct ivar64_t);
4152 offset += sizeof(struct ivar64_t);
4153 }
4154}
4155
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004156static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) {
4157 struct ivar_list32_t il;
4158 struct ivar32_t i;
4159 const char *r;
4160 uint32_t offset, xoffset, left, j;
4161 SectionRef S, xS;
4162 const char *name, *ivar_offset_p;
4163 uint32_t ivar_offset;
4164
4165 r = get_pointer_32(p, offset, left, S, info);
4166 if (r == nullptr)
4167 return;
4168 memset(&il, '\0', sizeof(struct ivar_list32_t));
4169 if (left < sizeof(struct ivar_list32_t)) {
4170 memcpy(&il, r, left);
4171 outs() << " (ivar_list_t entends past the end of the section)\n";
4172 } else
4173 memcpy(&il, r, sizeof(struct ivar_list32_t));
4174 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4175 swapStruct(il);
4176 outs() << " entsize " << il.entsize << "\n";
4177 outs() << " count " << il.count << "\n";
4178
4179 p += sizeof(struct ivar_list32_t);
4180 offset += sizeof(struct ivar_list32_t);
4181 for (j = 0; j < il.count; j++) {
4182 r = get_pointer_32(p, offset, left, S, info);
4183 if (r == nullptr)
4184 return;
4185 memset(&i, '\0', sizeof(struct ivar32_t));
4186 if (left < sizeof(struct ivar32_t)) {
4187 memcpy(&i, r, left);
4188 outs() << " (ivar_t entends past the end of the section)\n";
4189 } else
4190 memcpy(&i, r, sizeof(struct ivar32_t));
4191 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4192 swapStruct(i);
4193
4194 outs() << "\t\t\t offset " << format("0x%" PRIx32, i.offset);
4195 ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info);
4196 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4197 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4198 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4199 sys::swapByteOrder(ivar_offset);
4200 outs() << " " << ivar_offset << "\n";
4201 } else
4202 outs() << "\n";
4203
4204 outs() << "\t\t\t name " << format("0x%" PRIx32, i.name);
4205 name = get_pointer_32(i.name, xoffset, left, xS, info);
4206 if (name != nullptr)
4207 outs() << format(" %.*s", left, name);
4208 outs() << "\n";
4209
4210 outs() << "\t\t\t type " << format("0x%" PRIx32, i.type);
4211 name = get_pointer_32(i.type, xoffset, left, xS, info);
4212 if (name != nullptr)
4213 outs() << format(" %.*s", left, name);
4214 outs() << "\n";
4215
4216 outs() << "\t\t\talignment " << i.alignment << "\n";
4217 outs() << "\t\t\t size " << i.size << "\n";
4218
4219 p += sizeof(struct ivar32_t);
4220 offset += sizeof(struct ivar32_t);
4221 }
4222}
4223
Kevin Enderby0fc11822015-04-01 20:57:01 +00004224static void print_objc_property_list64(uint64_t p,
4225 struct DisassembleInfo *info) {
4226 struct objc_property_list64 opl;
4227 struct objc_property64 op;
4228 const char *r;
4229 uint32_t offset, xoffset, left, j;
4230 SectionRef S, xS;
4231 const char *name, *sym_name;
4232 uint64_t n_value;
4233
4234 r = get_pointer_64(p, offset, left, S, info);
4235 if (r == nullptr)
4236 return;
4237 memset(&opl, '\0', sizeof(struct objc_property_list64));
4238 if (left < sizeof(struct objc_property_list64)) {
4239 memcpy(&opl, r, left);
4240 outs() << " (objc_property_list entends past the end of the section)\n";
4241 } else
4242 memcpy(&opl, r, sizeof(struct objc_property_list64));
4243 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4244 swapStruct(opl);
4245 outs() << " entsize " << opl.entsize << "\n";
4246 outs() << " count " << opl.count << "\n";
4247
4248 p += sizeof(struct objc_property_list64);
4249 offset += sizeof(struct objc_property_list64);
4250 for (j = 0; j < opl.count; j++) {
4251 r = get_pointer_64(p, offset, left, S, info);
4252 if (r == nullptr)
4253 return;
4254 memset(&op, '\0', sizeof(struct objc_property64));
4255 if (left < sizeof(struct objc_property64)) {
4256 memcpy(&op, r, left);
4257 outs() << " (objc_property entends past the end of the section)\n";
4258 } else
4259 memcpy(&op, r, sizeof(struct objc_property64));
4260 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4261 swapStruct(op);
4262
4263 outs() << "\t\t\t name ";
4264 sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name), S,
4265 info, n_value, op.name);
4266 if (n_value != 0) {
4267 if (info->verbose && sym_name != nullptr)
4268 outs() << sym_name;
4269 else
4270 outs() << format("0x%" PRIx64, n_value);
4271 if (op.name != 0)
4272 outs() << " + " << format("0x%" PRIx64, op.name);
4273 } else
4274 outs() << format("0x%" PRIx64, op.name);
4275 name = get_pointer_64(op.name + n_value, xoffset, left, xS, info);
4276 if (name != nullptr)
4277 outs() << format(" %.*s", left, name);
4278 outs() << "\n";
4279
4280 outs() << "\t\t\tattributes ";
4281 sym_name =
4282 get_symbol_64(offset + offsetof(struct objc_property64, attributes), S,
4283 info, n_value, op.attributes);
4284 if (n_value != 0) {
4285 if (info->verbose && sym_name != nullptr)
4286 outs() << sym_name;
4287 else
4288 outs() << format("0x%" PRIx64, n_value);
4289 if (op.attributes != 0)
4290 outs() << " + " << format("0x%" PRIx64, op.attributes);
4291 } else
4292 outs() << format("0x%" PRIx64, op.attributes);
4293 name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info);
4294 if (name != nullptr)
4295 outs() << format(" %.*s", left, name);
4296 outs() << "\n";
4297
4298 p += sizeof(struct objc_property64);
4299 offset += sizeof(struct objc_property64);
4300 }
4301}
4302
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004303static void print_objc_property_list32(uint32_t p,
4304 struct DisassembleInfo *info) {
4305 struct objc_property_list32 opl;
4306 struct objc_property32 op;
4307 const char *r;
4308 uint32_t offset, xoffset, left, j;
4309 SectionRef S, xS;
4310 const char *name;
4311
4312 r = get_pointer_32(p, offset, left, S, info);
4313 if (r == nullptr)
4314 return;
4315 memset(&opl, '\0', sizeof(struct objc_property_list32));
4316 if (left < sizeof(struct objc_property_list32)) {
4317 memcpy(&opl, r, left);
4318 outs() << " (objc_property_list entends past the end of the section)\n";
4319 } else
4320 memcpy(&opl, r, sizeof(struct objc_property_list32));
4321 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4322 swapStruct(opl);
4323 outs() << " entsize " << opl.entsize << "\n";
4324 outs() << " count " << opl.count << "\n";
4325
4326 p += sizeof(struct objc_property_list32);
4327 offset += sizeof(struct objc_property_list32);
4328 for (j = 0; j < opl.count; j++) {
4329 r = get_pointer_32(p, offset, left, S, info);
4330 if (r == nullptr)
4331 return;
4332 memset(&op, '\0', sizeof(struct objc_property32));
4333 if (left < sizeof(struct objc_property32)) {
4334 memcpy(&op, r, left);
4335 outs() << " (objc_property entends past the end of the section)\n";
4336 } else
4337 memcpy(&op, r, sizeof(struct objc_property32));
4338 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4339 swapStruct(op);
4340
4341 outs() << "\t\t\t name " << format("0x%" PRIx32, op.name);
4342 name = get_pointer_32(op.name, xoffset, left, xS, info);
4343 if (name != nullptr)
4344 outs() << format(" %.*s", left, name);
4345 outs() << "\n";
4346
4347 outs() << "\t\t\tattributes " << format("0x%" PRIx32, op.attributes);
4348 name = get_pointer_32(op.attributes, xoffset, left, xS, info);
4349 if (name != nullptr)
4350 outs() << format(" %.*s", left, name);
4351 outs() << "\n";
4352
4353 p += sizeof(struct objc_property32);
4354 offset += sizeof(struct objc_property32);
4355 }
4356}
4357
Richard Smith81ff44d2015-10-09 22:09:56 +00004358static bool print_class_ro64_t(uint64_t p, struct DisassembleInfo *info,
Kevin Enderby0fc11822015-04-01 20:57:01 +00004359 bool &is_meta_class) {
4360 struct class_ro64_t cro;
4361 const char *r;
4362 uint32_t offset, xoffset, left;
4363 SectionRef S, xS;
4364 const char *name, *sym_name;
4365 uint64_t n_value;
4366
4367 r = get_pointer_64(p, offset, left, S, info);
4368 if (r == nullptr || left < sizeof(struct class_ro64_t))
Richard Smith81ff44d2015-10-09 22:09:56 +00004369 return false;
Kevin Enderby0fc11822015-04-01 20:57:01 +00004370 memset(&cro, '\0', sizeof(struct class_ro64_t));
4371 if (left < sizeof(struct class_ro64_t)) {
4372 memcpy(&cro, r, left);
4373 outs() << " (class_ro_t entends past the end of the section)\n";
4374 } else
4375 memcpy(&cro, r, sizeof(struct class_ro64_t));
4376 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4377 swapStruct(cro);
4378 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4379 if (cro.flags & RO_META)
4380 outs() << " RO_META";
4381 if (cro.flags & RO_ROOT)
4382 outs() << " RO_ROOT";
4383 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4384 outs() << " RO_HAS_CXX_STRUCTORS";
4385 outs() << "\n";
4386 outs() << " instanceStart " << cro.instanceStart << "\n";
4387 outs() << " instanceSize " << cro.instanceSize << "\n";
4388 outs() << " reserved " << format("0x%" PRIx32, cro.reserved)
4389 << "\n";
4390 outs() << " ivarLayout " << format("0x%" PRIx64, cro.ivarLayout)
4391 << "\n";
4392 print_layout_map64(cro.ivarLayout, info);
4393
4394 outs() << " name ";
4395 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name), S,
4396 info, n_value, cro.name);
4397 if (n_value != 0) {
4398 if (info->verbose && sym_name != nullptr)
4399 outs() << sym_name;
4400 else
4401 outs() << format("0x%" PRIx64, n_value);
4402 if (cro.name != 0)
4403 outs() << " + " << format("0x%" PRIx64, cro.name);
4404 } else
4405 outs() << format("0x%" PRIx64, cro.name);
4406 name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info);
4407 if (name != nullptr)
4408 outs() << format(" %.*s", left, name);
4409 outs() << "\n";
4410
4411 outs() << " baseMethods ";
4412 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods),
4413 S, info, n_value, cro.baseMethods);
4414 if (n_value != 0) {
4415 if (info->verbose && sym_name != nullptr)
4416 outs() << sym_name;
4417 else
4418 outs() << format("0x%" PRIx64, n_value);
4419 if (cro.baseMethods != 0)
4420 outs() << " + " << format("0x%" PRIx64, cro.baseMethods);
4421 } else
4422 outs() << format("0x%" PRIx64, cro.baseMethods);
4423 outs() << " (struct method_list_t *)\n";
4424 if (cro.baseMethods + n_value != 0)
4425 print_method_list64_t(cro.baseMethods + n_value, info, "");
4426
4427 outs() << " baseProtocols ";
4428 sym_name =
4429 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols), S,
4430 info, n_value, cro.baseProtocols);
4431 if (n_value != 0) {
4432 if (info->verbose && sym_name != nullptr)
4433 outs() << sym_name;
4434 else
4435 outs() << format("0x%" PRIx64, n_value);
4436 if (cro.baseProtocols != 0)
4437 outs() << " + " << format("0x%" PRIx64, cro.baseProtocols);
4438 } else
4439 outs() << format("0x%" PRIx64, cro.baseProtocols);
4440 outs() << "\n";
4441 if (cro.baseProtocols + n_value != 0)
4442 print_protocol_list64_t(cro.baseProtocols + n_value, info);
4443
4444 outs() << " ivars ";
4445 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars), S,
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004446 info, n_value, cro.ivars);
Kevin Enderby0fc11822015-04-01 20:57:01 +00004447 if (n_value != 0) {
4448 if (info->verbose && sym_name != nullptr)
4449 outs() << sym_name;
4450 else
4451 outs() << format("0x%" PRIx64, n_value);
4452 if (cro.ivars != 0)
4453 outs() << " + " << format("0x%" PRIx64, cro.ivars);
4454 } else
4455 outs() << format("0x%" PRIx64, cro.ivars);
4456 outs() << "\n";
4457 if (cro.ivars + n_value != 0)
4458 print_ivar_list64_t(cro.ivars + n_value, info);
4459
4460 outs() << " weakIvarLayout ";
4461 sym_name =
4462 get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout), S,
4463 info, n_value, cro.weakIvarLayout);
4464 if (n_value != 0) {
4465 if (info->verbose && sym_name != nullptr)
4466 outs() << sym_name;
4467 else
4468 outs() << format("0x%" PRIx64, n_value);
4469 if (cro.weakIvarLayout != 0)
4470 outs() << " + " << format("0x%" PRIx64, cro.weakIvarLayout);
4471 } else
4472 outs() << format("0x%" PRIx64, cro.weakIvarLayout);
4473 outs() << "\n";
4474 print_layout_map64(cro.weakIvarLayout + n_value, info);
4475
4476 outs() << " baseProperties ";
4477 sym_name =
4478 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties), S,
4479 info, n_value, cro.baseProperties);
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.baseProperties != 0)
4486 outs() << " + " << format("0x%" PRIx64, cro.baseProperties);
4487 } else
4488 outs() << format("0x%" PRIx64, cro.baseProperties);
4489 outs() << "\n";
4490 if (cro.baseProperties + n_value != 0)
4491 print_objc_property_list64(cro.baseProperties + n_value, info);
4492
Rafael Espindolab9091322015-10-24 23:19:10 +00004493 is_meta_class = (cro.flags & RO_META) != 0;
Richard Smith81ff44d2015-10-09 22:09:56 +00004494 return true;
Kevin Enderby0fc11822015-04-01 20:57:01 +00004495}
4496
Richard Smith81ff44d2015-10-09 22:09:56 +00004497static bool print_class_ro32_t(uint32_t p, struct DisassembleInfo *info,
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004498 bool &is_meta_class) {
4499 struct class_ro32_t cro;
4500 const char *r;
4501 uint32_t offset, xoffset, left;
4502 SectionRef S, xS;
4503 const char *name;
4504
4505 r = get_pointer_32(p, offset, left, S, info);
4506 if (r == nullptr)
Richard Smith81ff44d2015-10-09 22:09:56 +00004507 return false;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004508 memset(&cro, '\0', sizeof(struct class_ro32_t));
4509 if (left < sizeof(struct class_ro32_t)) {
4510 memcpy(&cro, r, left);
4511 outs() << " (class_ro_t entends past the end of the section)\n";
4512 } else
4513 memcpy(&cro, r, sizeof(struct class_ro32_t));
4514 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4515 swapStruct(cro);
4516 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4517 if (cro.flags & RO_META)
4518 outs() << " RO_META";
4519 if (cro.flags & RO_ROOT)
4520 outs() << " RO_ROOT";
4521 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4522 outs() << " RO_HAS_CXX_STRUCTORS";
4523 outs() << "\n";
4524 outs() << " instanceStart " << cro.instanceStart << "\n";
4525 outs() << " instanceSize " << cro.instanceSize << "\n";
4526 outs() << " ivarLayout " << format("0x%" PRIx32, cro.ivarLayout)
4527 << "\n";
4528 print_layout_map32(cro.ivarLayout, info);
4529
4530 outs() << " name " << format("0x%" PRIx32, cro.name);
4531 name = get_pointer_32(cro.name, xoffset, left, xS, info);
4532 if (name != nullptr)
4533 outs() << format(" %.*s", left, name);
4534 outs() << "\n";
4535
4536 outs() << " baseMethods "
4537 << format("0x%" PRIx32, cro.baseMethods)
4538 << " (struct method_list_t *)\n";
4539 if (cro.baseMethods != 0)
4540 print_method_list32_t(cro.baseMethods, info, "");
4541
4542 outs() << " baseProtocols "
4543 << format("0x%" PRIx32, cro.baseProtocols) << "\n";
4544 if (cro.baseProtocols != 0)
4545 print_protocol_list32_t(cro.baseProtocols, info);
4546 outs() << " ivars " << format("0x%" PRIx32, cro.ivars)
4547 << "\n";
4548 if (cro.ivars != 0)
4549 print_ivar_list32_t(cro.ivars, info);
4550 outs() << " weakIvarLayout "
4551 << format("0x%" PRIx32, cro.weakIvarLayout) << "\n";
4552 print_layout_map32(cro.weakIvarLayout, info);
4553 outs() << " baseProperties "
4554 << format("0x%" PRIx32, cro.baseProperties) << "\n";
4555 if (cro.baseProperties != 0)
4556 print_objc_property_list32(cro.baseProperties, info);
Rafael Espindolab9091322015-10-24 23:19:10 +00004557 is_meta_class = (cro.flags & RO_META) != 0;
Richard Smith81ff44d2015-10-09 22:09:56 +00004558 return true;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004559}
4560
Kevin Enderby0fc11822015-04-01 20:57:01 +00004561static void print_class64_t(uint64_t p, struct DisassembleInfo *info) {
4562 struct class64_t c;
4563 const char *r;
4564 uint32_t offset, left;
4565 SectionRef S;
4566 const char *name;
4567 uint64_t isa_n_value, n_value;
4568
4569 r = get_pointer_64(p, offset, left, S, info);
4570 if (r == nullptr || left < sizeof(struct class64_t))
4571 return;
4572 memset(&c, '\0', sizeof(struct class64_t));
4573 if (left < sizeof(struct class64_t)) {
4574 memcpy(&c, r, left);
4575 outs() << " (class_t entends past the end of the section)\n";
4576 } else
4577 memcpy(&c, r, sizeof(struct class64_t));
4578 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4579 swapStruct(c);
4580
4581 outs() << " isa " << format("0x%" PRIx64, c.isa);
4582 name = get_symbol_64(offset + offsetof(struct class64_t, isa), S, info,
4583 isa_n_value, c.isa);
4584 if (name != nullptr)
4585 outs() << " " << name;
4586 outs() << "\n";
4587
4588 outs() << " superclass " << format("0x%" PRIx64, c.superclass);
4589 name = get_symbol_64(offset + offsetof(struct class64_t, superclass), S, info,
4590 n_value, c.superclass);
4591 if (name != nullptr)
4592 outs() << " " << name;
4593 outs() << "\n";
4594
4595 outs() << " cache " << format("0x%" PRIx64, c.cache);
4596 name = get_symbol_64(offset + offsetof(struct class64_t, cache), S, info,
4597 n_value, c.cache);
4598 if (name != nullptr)
4599 outs() << " " << name;
4600 outs() << "\n";
4601
4602 outs() << " vtable " << format("0x%" PRIx64, c.vtable);
4603 name = get_symbol_64(offset + offsetof(struct class64_t, vtable), S, info,
4604 n_value, c.vtable);
4605 if (name != nullptr)
4606 outs() << " " << name;
4607 outs() << "\n";
4608
4609 name = get_symbol_64(offset + offsetof(struct class64_t, data), S, info,
4610 n_value, c.data);
4611 outs() << " data ";
4612 if (n_value != 0) {
4613 if (info->verbose && name != nullptr)
4614 outs() << name;
4615 else
4616 outs() << format("0x%" PRIx64, n_value);
4617 if (c.data != 0)
4618 outs() << " + " << format("0x%" PRIx64, c.data);
4619 } else
4620 outs() << format("0x%" PRIx64, c.data);
4621 outs() << " (struct class_ro_t *)";
4622
4623 // This is a Swift class if some of the low bits of the pointer are set.
4624 if ((c.data + n_value) & 0x7)
4625 outs() << " Swift class";
4626 outs() << "\n";
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004627 bool is_meta_class;
Richard Smith81ff44d2015-10-09 22:09:56 +00004628 if (!print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class))
4629 return;
Kevin Enderby0fc11822015-04-01 20:57:01 +00004630
Kevin Enderbyaac75382015-10-08 16:56:35 +00004631 if (!is_meta_class &&
4632 c.isa + isa_n_value != p &&
4633 c.isa + isa_n_value != 0 &&
4634 info->depth < 100) {
4635 info->depth++;
4636 outs() << "Meta Class\n";
4637 print_class64_t(c.isa + isa_n_value, info);
Kevin Enderby0fc11822015-04-01 20:57:01 +00004638 }
4639}
4640
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004641static void print_class32_t(uint32_t p, struct DisassembleInfo *info) {
4642 struct class32_t c;
4643 const char *r;
4644 uint32_t offset, left;
4645 SectionRef S;
4646 const char *name;
4647
4648 r = get_pointer_32(p, offset, left, S, info);
4649 if (r == nullptr)
4650 return;
4651 memset(&c, '\0', sizeof(struct class32_t));
4652 if (left < sizeof(struct class32_t)) {
4653 memcpy(&c, r, left);
4654 outs() << " (class_t entends past the end of the section)\n";
4655 } else
4656 memcpy(&c, r, sizeof(struct class32_t));
4657 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4658 swapStruct(c);
4659
4660 outs() << " isa " << format("0x%" PRIx32, c.isa);
4661 name =
4662 get_symbol_32(offset + offsetof(struct class32_t, isa), S, info, c.isa);
4663 if (name != nullptr)
4664 outs() << " " << name;
4665 outs() << "\n";
4666
4667 outs() << " superclass " << format("0x%" PRIx32, c.superclass);
4668 name = get_symbol_32(offset + offsetof(struct class32_t, superclass), S, info,
4669 c.superclass);
4670 if (name != nullptr)
4671 outs() << " " << name;
4672 outs() << "\n";
4673
4674 outs() << " cache " << format("0x%" PRIx32, c.cache);
4675 name = get_symbol_32(offset + offsetof(struct class32_t, cache), S, info,
4676 c.cache);
4677 if (name != nullptr)
4678 outs() << " " << name;
4679 outs() << "\n";
4680
4681 outs() << " vtable " << format("0x%" PRIx32, c.vtable);
4682 name = get_symbol_32(offset + offsetof(struct class32_t, vtable), S, info,
4683 c.vtable);
4684 if (name != nullptr)
4685 outs() << " " << name;
4686 outs() << "\n";
4687
4688 name =
4689 get_symbol_32(offset + offsetof(struct class32_t, data), S, info, c.data);
4690 outs() << " data " << format("0x%" PRIx32, c.data)
4691 << " (struct class_ro_t *)";
4692
4693 // This is a Swift class if some of the low bits of the pointer are set.
4694 if (c.data & 0x3)
4695 outs() << " Swift class";
4696 outs() << "\n";
4697 bool is_meta_class;
Richard Smith81ff44d2015-10-09 22:09:56 +00004698 if (!print_class_ro32_t(c.data & ~0x3, info, is_meta_class))
4699 return;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004700
Hans Wennborgcc9deb42015-09-29 18:02:48 +00004701 if (!is_meta_class) {
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004702 outs() << "Meta Class\n";
4703 print_class32_t(c.isa, info);
4704 }
4705}
4706
Kevin Enderby846c0002015-04-16 17:19:59 +00004707static void print_objc_class_t(struct objc_class_t *objc_class,
4708 struct DisassembleInfo *info) {
4709 uint32_t offset, left, xleft;
4710 const char *name, *p, *ivar_list;
4711 SectionRef S;
4712 int32_t i;
4713 struct objc_ivar_list_t objc_ivar_list;
4714 struct objc_ivar_t ivar;
4715
4716 outs() << "\t\t isa " << format("0x%08" PRIx32, objc_class->isa);
4717 if (info->verbose && CLS_GETINFO(objc_class, CLS_META)) {
4718 name = get_pointer_32(objc_class->isa, offset, left, S, info, true);
4719 if (name != nullptr)
4720 outs() << format(" %.*s", left, name);
4721 else
4722 outs() << " (not in an __OBJC section)";
4723 }
4724 outs() << "\n";
4725
4726 outs() << "\t super_class "
4727 << format("0x%08" PRIx32, objc_class->super_class);
4728 if (info->verbose) {
4729 name = get_pointer_32(objc_class->super_class, offset, left, S, info, true);
4730 if (name != nullptr)
4731 outs() << format(" %.*s", left, name);
4732 else
4733 outs() << " (not in an __OBJC section)";
4734 }
4735 outs() << "\n";
4736
4737 outs() << "\t\t name " << format("0x%08" PRIx32, objc_class->name);
4738 if (info->verbose) {
4739 name = get_pointer_32(objc_class->name, offset, left, S, info, true);
4740 if (name != nullptr)
4741 outs() << format(" %.*s", left, name);
4742 else
4743 outs() << " (not in an __OBJC section)";
4744 }
4745 outs() << "\n";
4746
4747 outs() << "\t\t version " << format("0x%08" PRIx32, objc_class->version)
4748 << "\n";
4749
4750 outs() << "\t\t info " << format("0x%08" PRIx32, objc_class->info);
4751 if (info->verbose) {
4752 if (CLS_GETINFO(objc_class, CLS_CLASS))
4753 outs() << " CLS_CLASS";
4754 else if (CLS_GETINFO(objc_class, CLS_META))
4755 outs() << " CLS_META";
4756 }
4757 outs() << "\n";
4758
4759 outs() << "\t instance_size "
4760 << format("0x%08" PRIx32, objc_class->instance_size) << "\n";
4761
4762 p = get_pointer_32(objc_class->ivars, offset, left, S, info, true);
4763 outs() << "\t\t ivars " << format("0x%08" PRIx32, objc_class->ivars);
4764 if (p != nullptr) {
4765 if (left > sizeof(struct objc_ivar_list_t)) {
4766 outs() << "\n";
4767 memcpy(&objc_ivar_list, p, sizeof(struct objc_ivar_list_t));
4768 } else {
4769 outs() << " (entends past the end of the section)\n";
4770 memset(&objc_ivar_list, '\0', sizeof(struct objc_ivar_list_t));
4771 memcpy(&objc_ivar_list, p, left);
4772 }
4773 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4774 swapStruct(objc_ivar_list);
4775 outs() << "\t\t ivar_count " << objc_ivar_list.ivar_count << "\n";
4776 ivar_list = p + sizeof(struct objc_ivar_list_t);
4777 for (i = 0; i < objc_ivar_list.ivar_count; i++) {
4778 if ((i + 1) * sizeof(struct objc_ivar_t) > left) {
4779 outs() << "\t\t remaining ivar's extend past the of the section\n";
4780 break;
4781 }
4782 memcpy(&ivar, ivar_list + i * sizeof(struct objc_ivar_t),
4783 sizeof(struct objc_ivar_t));
4784 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4785 swapStruct(ivar);
4786
4787 outs() << "\t\t\tivar_name " << format("0x%08" PRIx32, ivar.ivar_name);
4788 if (info->verbose) {
4789 name = get_pointer_32(ivar.ivar_name, offset, xleft, S, info, true);
4790 if (name != nullptr)
4791 outs() << format(" %.*s", xleft, name);
4792 else
4793 outs() << " (not in an __OBJC section)";
4794 }
4795 outs() << "\n";
4796
4797 outs() << "\t\t\tivar_type " << format("0x%08" PRIx32, ivar.ivar_type);
4798 if (info->verbose) {
4799 name = get_pointer_32(ivar.ivar_type, offset, xleft, S, info, true);
4800 if (name != nullptr)
4801 outs() << format(" %.*s", xleft, name);
4802 else
4803 outs() << " (not in an __OBJC section)";
4804 }
4805 outs() << "\n";
4806
4807 outs() << "\t\t ivar_offset "
4808 << format("0x%08" PRIx32, ivar.ivar_offset) << "\n";
4809 }
4810 } else {
4811 outs() << " (not in an __OBJC section)\n";
4812 }
4813
4814 outs() << "\t\t methods " << format("0x%08" PRIx32, objc_class->methodLists);
4815 if (print_method_list(objc_class->methodLists, info))
4816 outs() << " (not in an __OBJC section)\n";
4817
4818 outs() << "\t\t cache " << format("0x%08" PRIx32, objc_class->cache)
4819 << "\n";
4820
4821 outs() << "\t\tprotocols " << format("0x%08" PRIx32, objc_class->protocols);
4822 if (print_protocol_list(objc_class->protocols, 16, info))
4823 outs() << " (not in an __OBJC section)\n";
4824}
4825
4826static void print_objc_objc_category_t(struct objc_category_t *objc_category,
4827 struct DisassembleInfo *info) {
4828 uint32_t offset, left;
4829 const char *name;
4830 SectionRef S;
4831
4832 outs() << "\t category name "
4833 << format("0x%08" PRIx32, objc_category->category_name);
4834 if (info->verbose) {
4835 name = get_pointer_32(objc_category->category_name, offset, left, S, info,
4836 true);
4837 if (name != nullptr)
4838 outs() << format(" %.*s", left, name);
4839 else
4840 outs() << " (not in an __OBJC section)";
4841 }
4842 outs() << "\n";
4843
4844 outs() << "\t\t class name "
4845 << format("0x%08" PRIx32, objc_category->class_name);
4846 if (info->verbose) {
4847 name =
4848 get_pointer_32(objc_category->class_name, offset, left, S, info, true);
4849 if (name != nullptr)
4850 outs() << format(" %.*s", left, name);
4851 else
4852 outs() << " (not in an __OBJC section)";
4853 }
4854 outs() << "\n";
4855
4856 outs() << "\t instance methods "
4857 << format("0x%08" PRIx32, objc_category->instance_methods);
4858 if (print_method_list(objc_category->instance_methods, info))
4859 outs() << " (not in an __OBJC section)\n";
4860
4861 outs() << "\t class methods "
4862 << format("0x%08" PRIx32, objc_category->class_methods);
4863 if (print_method_list(objc_category->class_methods, info))
4864 outs() << " (not in an __OBJC section)\n";
4865}
4866
Kevin Enderby0fc11822015-04-01 20:57:01 +00004867static void print_category64_t(uint64_t p, struct DisassembleInfo *info) {
4868 struct category64_t c;
4869 const char *r;
4870 uint32_t offset, xoffset, left;
4871 SectionRef S, xS;
4872 const char *name, *sym_name;
4873 uint64_t n_value;
4874
4875 r = get_pointer_64(p, offset, left, S, info);
4876 if (r == nullptr)
4877 return;
4878 memset(&c, '\0', sizeof(struct category64_t));
4879 if (left < sizeof(struct category64_t)) {
4880 memcpy(&c, r, left);
4881 outs() << " (category_t entends past the end of the section)\n";
4882 } else
4883 memcpy(&c, r, sizeof(struct category64_t));
4884 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4885 swapStruct(c);
4886
4887 outs() << " name ";
4888 sym_name = get_symbol_64(offset + offsetof(struct category64_t, name), S,
4889 info, n_value, c.name);
4890 if (n_value != 0) {
4891 if (info->verbose && sym_name != nullptr)
4892 outs() << sym_name;
4893 else
4894 outs() << format("0x%" PRIx64, n_value);
4895 if (c.name != 0)
4896 outs() << " + " << format("0x%" PRIx64, c.name);
4897 } else
4898 outs() << format("0x%" PRIx64, c.name);
4899 name = get_pointer_64(c.name + n_value, xoffset, left, xS, info);
4900 if (name != nullptr)
4901 outs() << format(" %.*s", left, name);
4902 outs() << "\n";
4903
4904 outs() << " cls ";
4905 sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls), S, info,
4906 n_value, c.cls);
4907 if (n_value != 0) {
4908 if (info->verbose && sym_name != nullptr)
4909 outs() << sym_name;
4910 else
4911 outs() << format("0x%" PRIx64, n_value);
4912 if (c.cls != 0)
4913 outs() << " + " << format("0x%" PRIx64, c.cls);
4914 } else
4915 outs() << format("0x%" PRIx64, c.cls);
4916 outs() << "\n";
4917 if (c.cls + n_value != 0)
4918 print_class64_t(c.cls + n_value, info);
4919
4920 outs() << " instanceMethods ";
4921 sym_name =
4922 get_symbol_64(offset + offsetof(struct category64_t, instanceMethods), S,
4923 info, n_value, c.instanceMethods);
4924 if (n_value != 0) {
4925 if (info->verbose && sym_name != nullptr)
4926 outs() << sym_name;
4927 else
4928 outs() << format("0x%" PRIx64, n_value);
4929 if (c.instanceMethods != 0)
4930 outs() << " + " << format("0x%" PRIx64, c.instanceMethods);
4931 } else
4932 outs() << format("0x%" PRIx64, c.instanceMethods);
4933 outs() << "\n";
4934 if (c.instanceMethods + n_value != 0)
4935 print_method_list64_t(c.instanceMethods + n_value, info, "");
4936
4937 outs() << " classMethods ";
4938 sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods),
4939 S, info, n_value, c.classMethods);
4940 if (n_value != 0) {
4941 if (info->verbose && sym_name != nullptr)
4942 outs() << sym_name;
4943 else
4944 outs() << format("0x%" PRIx64, n_value);
4945 if (c.classMethods != 0)
4946 outs() << " + " << format("0x%" PRIx64, c.classMethods);
4947 } else
4948 outs() << format("0x%" PRIx64, c.classMethods);
4949 outs() << "\n";
4950 if (c.classMethods + n_value != 0)
4951 print_method_list64_t(c.classMethods + n_value, info, "");
4952
4953 outs() << " protocols ";
4954 sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols), S,
4955 info, n_value, c.protocols);
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.protocols != 0)
4962 outs() << " + " << format("0x%" PRIx64, c.protocols);
4963 } else
4964 outs() << format("0x%" PRIx64, c.protocols);
4965 outs() << "\n";
4966 if (c.protocols + n_value != 0)
4967 print_protocol_list64_t(c.protocols + n_value, info);
4968
4969 outs() << "instanceProperties ";
4970 sym_name =
4971 get_symbol_64(offset + offsetof(struct category64_t, instanceProperties),
4972 S, info, n_value, c.instanceProperties);
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.instanceProperties != 0)
4979 outs() << " + " << format("0x%" PRIx64, c.instanceProperties);
4980 } else
4981 outs() << format("0x%" PRIx64, c.instanceProperties);
4982 outs() << "\n";
4983 if (c.instanceProperties + n_value != 0)
4984 print_objc_property_list64(c.instanceProperties + n_value, info);
4985}
4986
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004987static void print_category32_t(uint32_t p, struct DisassembleInfo *info) {
4988 struct category32_t c;
4989 const char *r;
4990 uint32_t offset, left;
4991 SectionRef S, xS;
4992 const char *name;
4993
4994 r = get_pointer_32(p, offset, left, S, info);
4995 if (r == nullptr)
4996 return;
4997 memset(&c, '\0', sizeof(struct category32_t));
4998 if (left < sizeof(struct category32_t)) {
4999 memcpy(&c, r, left);
5000 outs() << " (category_t entends past the end of the section)\n";
5001 } else
5002 memcpy(&c, r, sizeof(struct category32_t));
5003 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5004 swapStruct(c);
5005
5006 outs() << " name " << format("0x%" PRIx32, c.name);
5007 name = get_symbol_32(offset + offsetof(struct category32_t, name), S, info,
5008 c.name);
Hans Wennborgcc9deb42015-09-29 18:02:48 +00005009 if (name)
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005010 outs() << " " << name;
5011 outs() << "\n";
5012
5013 outs() << " cls " << format("0x%" PRIx32, c.cls) << "\n";
5014 if (c.cls != 0)
5015 print_class32_t(c.cls, info);
5016 outs() << " instanceMethods " << format("0x%" PRIx32, c.instanceMethods)
5017 << "\n";
5018 if (c.instanceMethods != 0)
5019 print_method_list32_t(c.instanceMethods, info, "");
5020 outs() << " classMethods " << format("0x%" PRIx32, c.classMethods)
5021 << "\n";
5022 if (c.classMethods != 0)
5023 print_method_list32_t(c.classMethods, info, "");
5024 outs() << " protocols " << format("0x%" PRIx32, c.protocols) << "\n";
5025 if (c.protocols != 0)
5026 print_protocol_list32_t(c.protocols, info);
5027 outs() << "instanceProperties " << format("0x%" PRIx32, c.instanceProperties)
5028 << "\n";
5029 if (c.instanceProperties != 0)
5030 print_objc_property_list32(c.instanceProperties, info);
5031}
5032
Kevin Enderby0fc11822015-04-01 20:57:01 +00005033static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) {
5034 uint32_t i, left, offset, xoffset;
5035 uint64_t p, n_value;
5036 struct message_ref64 mr;
5037 const char *name, *sym_name;
5038 const char *r;
5039 SectionRef xS;
5040
5041 if (S == SectionRef())
5042 return;
5043
5044 StringRef SectName;
5045 S.getName(SectName);
5046 DataRefImpl Ref = S.getRawDataRefImpl();
5047 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5048 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5049 offset = 0;
5050 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
5051 p = S.getAddress() + i;
5052 r = get_pointer_64(p, offset, left, S, info);
5053 if (r == nullptr)
5054 return;
5055 memset(&mr, '\0', sizeof(struct message_ref64));
5056 if (left < sizeof(struct message_ref64)) {
5057 memcpy(&mr, r, left);
5058 outs() << " (message_ref entends past the end of the section)\n";
5059 } else
5060 memcpy(&mr, r, sizeof(struct message_ref64));
5061 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5062 swapStruct(mr);
5063
5064 outs() << " imp ";
5065 name = get_symbol_64(offset + offsetof(struct message_ref64, imp), S, info,
5066 n_value, mr.imp);
5067 if (n_value != 0) {
5068 outs() << format("0x%" PRIx64, n_value) << " ";
5069 if (mr.imp != 0)
5070 outs() << "+ " << format("0x%" PRIx64, mr.imp) << " ";
5071 } else
5072 outs() << format("0x%" PRIx64, mr.imp) << " ";
5073 if (name != nullptr)
5074 outs() << " " << name;
5075 outs() << "\n";
5076
5077 outs() << " sel ";
5078 sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel), S,
5079 info, n_value, mr.sel);
5080 if (n_value != 0) {
5081 if (info->verbose && sym_name != nullptr)
5082 outs() << sym_name;
5083 else
5084 outs() << format("0x%" PRIx64, n_value);
5085 if (mr.sel != 0)
5086 outs() << " + " << format("0x%" PRIx64, mr.sel);
5087 } else
5088 outs() << format("0x%" PRIx64, mr.sel);
5089 name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info);
5090 if (name != nullptr)
5091 outs() << format(" %.*s", left, name);
5092 outs() << "\n";
5093
5094 offset += sizeof(struct message_ref64);
5095 }
5096}
5097
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005098static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) {
5099 uint32_t i, left, offset, xoffset, p;
5100 struct message_ref32 mr;
5101 const char *name, *r;
5102 SectionRef xS;
5103
5104 if (S == SectionRef())
5105 return;
5106
5107 StringRef SectName;
5108 S.getName(SectName);
5109 DataRefImpl Ref = S.getRawDataRefImpl();
5110 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5111 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5112 offset = 0;
5113 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
5114 p = S.getAddress() + i;
5115 r = get_pointer_32(p, offset, left, S, info);
5116 if (r == nullptr)
5117 return;
5118 memset(&mr, '\0', sizeof(struct message_ref32));
5119 if (left < sizeof(struct message_ref32)) {
5120 memcpy(&mr, r, left);
5121 outs() << " (message_ref entends past the end of the section)\n";
5122 } else
5123 memcpy(&mr, r, sizeof(struct message_ref32));
5124 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5125 swapStruct(mr);
5126
5127 outs() << " imp " << format("0x%" PRIx32, mr.imp);
5128 name = get_symbol_32(offset + offsetof(struct message_ref32, imp), S, info,
5129 mr.imp);
5130 if (name != nullptr)
5131 outs() << " " << name;
5132 outs() << "\n";
5133
5134 outs() << " sel " << format("0x%" PRIx32, mr.sel);
5135 name = get_pointer_32(mr.sel, xoffset, left, xS, info);
5136 if (name != nullptr)
5137 outs() << " " << name;
5138 outs() << "\n";
5139
5140 offset += sizeof(struct message_ref32);
5141 }
5142}
5143
Kevin Enderby0fc11822015-04-01 20:57:01 +00005144static void print_image_info64(SectionRef S, struct DisassembleInfo *info) {
5145 uint32_t left, offset, swift_version;
5146 uint64_t p;
5147 struct objc_image_info64 o;
5148 const char *r;
5149
Kevin Enderbyaf7c9d02015-10-09 16:48:44 +00005150 if (S == SectionRef())
5151 return;
5152
Kevin Enderby0fc11822015-04-01 20:57:01 +00005153 StringRef SectName;
5154 S.getName(SectName);
5155 DataRefImpl Ref = S.getRawDataRefImpl();
5156 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5157 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5158 p = S.getAddress();
5159 r = get_pointer_64(p, offset, left, S, info);
5160 if (r == nullptr)
5161 return;
5162 memset(&o, '\0', sizeof(struct objc_image_info64));
5163 if (left < sizeof(struct objc_image_info64)) {
5164 memcpy(&o, r, left);
5165 outs() << " (objc_image_info entends past the end of the section)\n";
5166 } else
5167 memcpy(&o, r, sizeof(struct objc_image_info64));
5168 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5169 swapStruct(o);
5170 outs() << " version " << o.version << "\n";
5171 outs() << " flags " << format("0x%" PRIx32, o.flags);
5172 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5173 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5174 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5175 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5176 swift_version = (o.flags >> 8) & 0xff;
5177 if (swift_version != 0) {
5178 if (swift_version == 1)
5179 outs() << " Swift 1.0";
5180 else if (swift_version == 2)
5181 outs() << " Swift 1.1";
5182 else
5183 outs() << " unknown future Swift version (" << swift_version << ")";
5184 }
5185 outs() << "\n";
5186}
5187
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005188static void print_image_info32(SectionRef S, struct DisassembleInfo *info) {
5189 uint32_t left, offset, swift_version, p;
5190 struct objc_image_info32 o;
5191 const char *r;
5192
Kevin Enderby19be2512016-04-21 19:49:29 +00005193 if (S == SectionRef())
5194 return;
5195
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005196 StringRef SectName;
5197 S.getName(SectName);
5198 DataRefImpl Ref = S.getRawDataRefImpl();
5199 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5200 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5201 p = S.getAddress();
5202 r = get_pointer_32(p, offset, left, S, info);
5203 if (r == nullptr)
5204 return;
5205 memset(&o, '\0', sizeof(struct objc_image_info32));
5206 if (left < sizeof(struct objc_image_info32)) {
5207 memcpy(&o, r, left);
5208 outs() << " (objc_image_info entends past the end of the section)\n";
5209 } else
5210 memcpy(&o, r, sizeof(struct objc_image_info32));
5211 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5212 swapStruct(o);
5213 outs() << " version " << o.version << "\n";
5214 outs() << " flags " << format("0x%" PRIx32, o.flags);
5215 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5216 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5217 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5218 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5219 swift_version = (o.flags >> 8) & 0xff;
5220 if (swift_version != 0) {
5221 if (swift_version == 1)
5222 outs() << " Swift 1.0";
5223 else if (swift_version == 2)
5224 outs() << " Swift 1.1";
5225 else
5226 outs() << " unknown future Swift version (" << swift_version << ")";
5227 }
5228 outs() << "\n";
5229}
5230
Kevin Enderby846c0002015-04-16 17:19:59 +00005231static void print_image_info(SectionRef S, struct DisassembleInfo *info) {
5232 uint32_t left, offset, p;
5233 struct imageInfo_t o;
5234 const char *r;
5235
5236 StringRef SectName;
5237 S.getName(SectName);
5238 DataRefImpl Ref = S.getRawDataRefImpl();
5239 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5240 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5241 p = S.getAddress();
5242 r = get_pointer_32(p, offset, left, S, info);
5243 if (r == nullptr)
5244 return;
5245 memset(&o, '\0', sizeof(struct imageInfo_t));
5246 if (left < sizeof(struct imageInfo_t)) {
5247 memcpy(&o, r, left);
5248 outs() << " (imageInfo entends past the end of the section)\n";
5249 } else
5250 memcpy(&o, r, sizeof(struct imageInfo_t));
5251 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5252 swapStruct(o);
5253 outs() << " version " << o.version << "\n";
5254 outs() << " flags " << format("0x%" PRIx32, o.flags);
5255 if (o.flags & 0x1)
5256 outs() << " F&C";
5257 if (o.flags & 0x2)
5258 outs() << " GC";
5259 if (o.flags & 0x4)
5260 outs() << " GC-only";
5261 else
5262 outs() << " RR";
5263 outs() << "\n";
5264}
5265
Kevin Enderby0fc11822015-04-01 20:57:01 +00005266static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) {
5267 SymbolAddressMap AddrMap;
5268 if (verbose)
5269 CreateSymbolAddressMap(O, &AddrMap);
5270
5271 std::vector<SectionRef> Sections;
5272 for (const SectionRef &Section : O->sections()) {
5273 StringRef SectName;
5274 Section.getName(SectName);
5275 Sections.push_back(Section);
5276 }
5277
5278 struct DisassembleInfo info;
5279 // Set up the block of info used by the Symbolizer call backs.
5280 info.verbose = verbose;
5281 info.O = O;
5282 info.AddrMap = &AddrMap;
5283 info.Sections = &Sections;
5284 info.class_name = nullptr;
5285 info.selector_name = nullptr;
5286 info.method = nullptr;
5287 info.demangled_name = nullptr;
5288 info.bindtable = nullptr;
5289 info.adrp_addr = 0;
5290 info.adrp_inst = 0;
5291
Kevin Enderbyaac75382015-10-08 16:56:35 +00005292 info.depth = 0;
Davide Italiano62507042015-12-11 22:27:59 +00005293 SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5294 if (CL == SectionRef())
5295 CL = get_section(O, "__DATA", "__objc_classlist");
5296 info.S = CL;
5297 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005298
Davide Italiano62507042015-12-11 22:27:59 +00005299 SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5300 if (CR == SectionRef())
5301 CR = get_section(O, "__DATA", "__objc_classrefs");
5302 info.S = CR;
5303 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005304
Davide Italiano62507042015-12-11 22:27:59 +00005305 SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5306 if (SR == SectionRef())
5307 SR = get_section(O, "__DATA", "__objc_superrefs");
5308 info.S = SR;
5309 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005310
Davide Italiano62507042015-12-11 22:27:59 +00005311 SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5312 if (CA == SectionRef())
5313 CA = get_section(O, "__DATA", "__objc_catlist");
5314 info.S = CA;
5315 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005316
Davide Italiano62507042015-12-11 22:27:59 +00005317 SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5318 if (PL == SectionRef())
5319 PL = get_section(O, "__DATA", "__objc_protolist");
5320 info.S = PL;
5321 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005322
Davide Italiano62507042015-12-11 22:27:59 +00005323 SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5324 if (MR == SectionRef())
5325 MR = get_section(O, "__DATA", "__objc_msgrefs");
5326 info.S = MR;
5327 print_message_refs64(MR, &info);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005328
Davide Italiano62507042015-12-11 22:27:59 +00005329 SectionRef II = get_section(O, "__OBJC2", "__image_info");
5330 if (II == SectionRef())
5331 II = get_section(O, "__DATA", "__objc_imageinfo");
5332 info.S = II;
5333 print_image_info64(II, &info);
Kevin Enderby0bc6ed42015-04-01 21:50:45 +00005334
5335 if (info.bindtable != nullptr)
5336 delete info.bindtable;
Kevin Enderby0fc11822015-04-01 20:57:01 +00005337}
5338
5339static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) {
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005340 SymbolAddressMap AddrMap;
5341 if (verbose)
5342 CreateSymbolAddressMap(O, &AddrMap);
5343
5344 std::vector<SectionRef> Sections;
5345 for (const SectionRef &Section : O->sections()) {
5346 StringRef SectName;
5347 Section.getName(SectName);
5348 Sections.push_back(Section);
5349 }
5350
5351 struct DisassembleInfo info;
5352 // Set up the block of info used by the Symbolizer call backs.
5353 info.verbose = verbose;
5354 info.O = O;
5355 info.AddrMap = &AddrMap;
5356 info.Sections = &Sections;
5357 info.class_name = nullptr;
5358 info.selector_name = nullptr;
5359 info.method = nullptr;
5360 info.demangled_name = nullptr;
5361 info.bindtable = nullptr;
5362 info.adrp_addr = 0;
5363 info.adrp_inst = 0;
5364
5365 const SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5366 if (CL != SectionRef()) {
5367 info.S = CL;
5368 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5369 } else {
5370 const SectionRef CL = get_section(O, "__DATA", "__objc_classlist");
5371 info.S = CL;
5372 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5373 }
5374
5375 const SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5376 if (CR != SectionRef()) {
5377 info.S = CR;
5378 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5379 } else {
5380 const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs");
5381 info.S = CR;
5382 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5383 }
5384
5385 const SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5386 if (SR != SectionRef()) {
5387 info.S = SR;
5388 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5389 } else {
5390 const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs");
5391 info.S = SR;
5392 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5393 }
5394
5395 const SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5396 if (CA != SectionRef()) {
5397 info.S = CA;
5398 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5399 } else {
5400 const SectionRef CA = get_section(O, "__DATA", "__objc_catlist");
5401 info.S = CA;
5402 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5403 }
5404
5405 const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5406 if (PL != SectionRef()) {
5407 info.S = PL;
5408 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5409 } else {
5410 const SectionRef PL = get_section(O, "__DATA", "__objc_protolist");
5411 info.S = PL;
5412 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5413 }
5414
5415 const SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5416 if (MR != SectionRef()) {
5417 info.S = MR;
5418 print_message_refs32(MR, &info);
5419 } else {
5420 const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs");
5421 info.S = MR;
5422 print_message_refs32(MR, &info);
5423 }
5424
5425 const SectionRef II = get_section(O, "__OBJC2", "__image_info");
5426 if (II != SectionRef()) {
5427 info.S = II;
5428 print_image_info32(II, &info);
5429 } else {
5430 const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo");
5431 info.S = II;
5432 print_image_info32(II, &info);
5433 }
Kevin Enderby0fc11822015-04-01 20:57:01 +00005434}
5435
5436static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) {
Kevin Enderby846c0002015-04-16 17:19:59 +00005437 uint32_t i, j, p, offset, xoffset, left, defs_left, def;
5438 const char *r, *name, *defs;
5439 struct objc_module_t module;
5440 SectionRef S, xS;
5441 struct objc_symtab_t symtab;
5442 struct objc_class_t objc_class;
5443 struct objc_category_t objc_category;
5444
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005445 outs() << "Objective-C segment\n";
Kevin Enderby846c0002015-04-16 17:19:59 +00005446 S = get_section(O, "__OBJC", "__module_info");
5447 if (S == SectionRef())
5448 return false;
5449
5450 SymbolAddressMap AddrMap;
5451 if (verbose)
5452 CreateSymbolAddressMap(O, &AddrMap);
5453
5454 std::vector<SectionRef> Sections;
5455 for (const SectionRef &Section : O->sections()) {
5456 StringRef SectName;
5457 Section.getName(SectName);
5458 Sections.push_back(Section);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005459 }
Kevin Enderby846c0002015-04-16 17:19:59 +00005460
5461 struct DisassembleInfo info;
5462 // Set up the block of info used by the Symbolizer call backs.
5463 info.verbose = verbose;
5464 info.O = O;
5465 info.AddrMap = &AddrMap;
5466 info.Sections = &Sections;
5467 info.class_name = nullptr;
5468 info.selector_name = nullptr;
5469 info.method = nullptr;
5470 info.demangled_name = nullptr;
5471 info.bindtable = nullptr;
5472 info.adrp_addr = 0;
5473 info.adrp_inst = 0;
5474
5475 for (i = 0; i < S.getSize(); i += sizeof(struct objc_module_t)) {
5476 p = S.getAddress() + i;
5477 r = get_pointer_32(p, offset, left, S, &info, true);
5478 if (r == nullptr)
5479 return true;
5480 memset(&module, '\0', sizeof(struct objc_module_t));
5481 if (left < sizeof(struct objc_module_t)) {
5482 memcpy(&module, r, left);
5483 outs() << " (module extends past end of __module_info section)\n";
5484 } else
5485 memcpy(&module, r, sizeof(struct objc_module_t));
5486 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5487 swapStruct(module);
5488
5489 outs() << "Module " << format("0x%" PRIx32, p) << "\n";
5490 outs() << " version " << module.version << "\n";
5491 outs() << " size " << module.size << "\n";
5492 outs() << " name ";
5493 name = get_pointer_32(module.name, xoffset, left, xS, &info, true);
5494 if (name != nullptr)
5495 outs() << format("%.*s", left, name);
5496 else
5497 outs() << format("0x%08" PRIx32, module.name)
5498 << "(not in an __OBJC section)";
5499 outs() << "\n";
5500
5501 r = get_pointer_32(module.symtab, xoffset, left, xS, &info, true);
5502 if (module.symtab == 0 || r == nullptr) {
5503 outs() << " symtab " << format("0x%08" PRIx32, module.symtab)
5504 << " (not in an __OBJC section)\n";
5505 continue;
5506 }
5507 outs() << " symtab " << format("0x%08" PRIx32, module.symtab) << "\n";
5508 memset(&symtab, '\0', sizeof(struct objc_symtab_t));
5509 defs_left = 0;
5510 defs = nullptr;
5511 if (left < sizeof(struct objc_symtab_t)) {
5512 memcpy(&symtab, r, left);
5513 outs() << "\tsymtab extends past end of an __OBJC section)\n";
5514 } else {
5515 memcpy(&symtab, r, sizeof(struct objc_symtab_t));
5516 if (left > sizeof(struct objc_symtab_t)) {
5517 defs_left = left - sizeof(struct objc_symtab_t);
5518 defs = r + sizeof(struct objc_symtab_t);
5519 }
5520 }
5521 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5522 swapStruct(symtab);
5523
5524 outs() << "\tsel_ref_cnt " << symtab.sel_ref_cnt << "\n";
5525 r = get_pointer_32(symtab.refs, xoffset, left, xS, &info, true);
5526 outs() << "\trefs " << format("0x%08" PRIx32, symtab.refs);
5527 if (r == nullptr)
5528 outs() << " (not in an __OBJC section)";
5529 outs() << "\n";
5530 outs() << "\tcls_def_cnt " << symtab.cls_def_cnt << "\n";
5531 outs() << "\tcat_def_cnt " << symtab.cat_def_cnt << "\n";
5532 if (symtab.cls_def_cnt > 0)
5533 outs() << "\tClass Definitions\n";
5534 for (j = 0; j < symtab.cls_def_cnt; j++) {
5535 if ((j + 1) * sizeof(uint32_t) > defs_left) {
5536 outs() << "\t(remaining class defs entries entends past the end of the "
5537 << "section)\n";
5538 break;
5539 }
5540 memcpy(&def, defs + j * sizeof(uint32_t), sizeof(uint32_t));
5541 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5542 sys::swapByteOrder(def);
5543
5544 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5545 outs() << "\tdefs[" << j << "] " << format("0x%08" PRIx32, def);
5546 if (r != nullptr) {
5547 if (left > sizeof(struct objc_class_t)) {
5548 outs() << "\n";
5549 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5550 } else {
5551 outs() << " (entends past the end of the section)\n";
5552 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5553 memcpy(&objc_class, r, left);
5554 }
5555 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5556 swapStruct(objc_class);
5557 print_objc_class_t(&objc_class, &info);
5558 } else {
5559 outs() << "(not in an __OBJC section)\n";
5560 }
5561
5562 if (CLS_GETINFO(&objc_class, CLS_CLASS)) {
5563 outs() << "\tMeta Class";
5564 r = get_pointer_32(objc_class.isa, xoffset, left, xS, &info, true);
5565 if (r != nullptr) {
5566 if (left > sizeof(struct objc_class_t)) {
5567 outs() << "\n";
5568 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5569 } else {
5570 outs() << " (entends past the end of the section)\n";
5571 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5572 memcpy(&objc_class, r, left);
5573 }
5574 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5575 swapStruct(objc_class);
5576 print_objc_class_t(&objc_class, &info);
5577 } else {
5578 outs() << "(not in an __OBJC section)\n";
5579 }
5580 }
5581 }
5582 if (symtab.cat_def_cnt > 0)
5583 outs() << "\tCategory Definitions\n";
5584 for (j = 0; j < symtab.cat_def_cnt; j++) {
5585 if ((j + symtab.cls_def_cnt + 1) * sizeof(uint32_t) > defs_left) {
5586 outs() << "\t(remaining category defs entries entends past the end of "
5587 << "the section)\n";
5588 break;
5589 }
5590 memcpy(&def, defs + (j + symtab.cls_def_cnt) * sizeof(uint32_t),
5591 sizeof(uint32_t));
5592 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5593 sys::swapByteOrder(def);
5594
5595 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5596 outs() << "\tdefs[" << j + symtab.cls_def_cnt << "] "
5597 << format("0x%08" PRIx32, def);
5598 if (r != nullptr) {
5599 if (left > sizeof(struct objc_category_t)) {
5600 outs() << "\n";
5601 memcpy(&objc_category, r, sizeof(struct objc_category_t));
5602 } else {
5603 outs() << " (entends past the end of the section)\n";
5604 memset(&objc_category, '\0', sizeof(struct objc_category_t));
5605 memcpy(&objc_category, r, left);
5606 }
5607 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5608 swapStruct(objc_category);
5609 print_objc_objc_category_t(&objc_category, &info);
5610 } else {
5611 outs() << "(not in an __OBJC section)\n";
5612 }
5613 }
5614 }
5615 const SectionRef II = get_section(O, "__OBJC", "__image_info");
5616 if (II != SectionRef())
5617 print_image_info(II, &info);
5618
5619 return true;
Kevin Enderby0fc11822015-04-01 20:57:01 +00005620}
5621
Kevin Enderby4ad9bde2015-04-16 22:33:20 +00005622static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
5623 uint32_t size, uint32_t addr) {
5624 SymbolAddressMap AddrMap;
5625 CreateSymbolAddressMap(O, &AddrMap);
5626
5627 std::vector<SectionRef> Sections;
5628 for (const SectionRef &Section : O->sections()) {
5629 StringRef SectName;
5630 Section.getName(SectName);
5631 Sections.push_back(Section);
5632 }
5633
5634 struct DisassembleInfo info;
5635 // Set up the block of info used by the Symbolizer call backs.
5636 info.verbose = true;
5637 info.O = O;
5638 info.AddrMap = &AddrMap;
5639 info.Sections = &Sections;
5640 info.class_name = nullptr;
5641 info.selector_name = nullptr;
5642 info.method = nullptr;
5643 info.demangled_name = nullptr;
5644 info.bindtable = nullptr;
5645 info.adrp_addr = 0;
5646 info.adrp_inst = 0;
5647
5648 const char *p;
5649 struct objc_protocol_t protocol;
5650 uint32_t left, paddr;
5651 for (p = sect; p < sect + size; p += sizeof(struct objc_protocol_t)) {
5652 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
5653 left = size - (p - sect);
5654 if (left < sizeof(struct objc_protocol_t)) {
5655 outs() << "Protocol extends past end of __protocol section\n";
5656 memcpy(&protocol, p, left);
5657 } else
5658 memcpy(&protocol, p, sizeof(struct objc_protocol_t));
5659 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5660 swapStruct(protocol);
5661 paddr = addr + (p - sect);
5662 outs() << "Protocol " << format("0x%" PRIx32, paddr);
5663 if (print_protocol(paddr, 0, &info))
5664 outs() << "(not in an __OBJC section)\n";
5665 }
5666}
5667
Kevin Enderby9873e2c2016-05-23 21:34:12 +00005668#ifdef HAVE_LIBXAR
5669inline void swapStruct(struct xar_header &xar) {
5670 sys::swapByteOrder(xar.magic);
5671 sys::swapByteOrder(xar.size);
5672 sys::swapByteOrder(xar.version);
5673 sys::swapByteOrder(xar.toc_length_compressed);
5674 sys::swapByteOrder(xar.toc_length_uncompressed);
5675 sys::swapByteOrder(xar.cksum_alg);
5676}
5677
5678static void PrintModeVerbose(uint32_t mode) {
5679 switch(mode & S_IFMT){
5680 case S_IFDIR:
5681 outs() << "d";
5682 break;
5683 case S_IFCHR:
5684 outs() << "c";
5685 break;
5686 case S_IFBLK:
5687 outs() << "b";
5688 break;
5689 case S_IFREG:
5690 outs() << "-";
5691 break;
5692 case S_IFLNK:
5693 outs() << "l";
5694 break;
5695 case S_IFSOCK:
5696 outs() << "s";
5697 break;
5698 default:
5699 outs() << "?";
5700 break;
5701 }
5702
5703 /* owner permissions */
5704 if(mode & S_IREAD)
5705 outs() << "r";
5706 else
5707 outs() << "-";
5708 if(mode & S_IWRITE)
5709 outs() << "w";
5710 else
5711 outs() << "-";
5712 if(mode & S_ISUID)
5713 outs() << "s";
5714 else if(mode & S_IEXEC)
5715 outs() << "x";
5716 else
5717 outs() << "-";
5718
5719 /* group permissions */
5720 if(mode & (S_IREAD >> 3))
5721 outs() << "r";
5722 else
5723 outs() << "-";
5724 if(mode & (S_IWRITE >> 3))
5725 outs() << "w";
5726 else
5727 outs() << "-";
5728 if(mode & S_ISGID)
5729 outs() << "s";
5730 else if(mode & (S_IEXEC >> 3))
5731 outs() << "x";
5732 else
5733 outs() << "-";
5734
5735 /* other permissions */
5736 if(mode & (S_IREAD >> 6))
5737 outs() << "r";
5738 else
5739 outs() << "-";
5740 if(mode & (S_IWRITE >> 6))
5741 outs() << "w";
5742 else
5743 outs() << "-";
5744 if(mode & S_ISVTX)
5745 outs() << "t";
5746 else if(mode & (S_IEXEC >> 6))
5747 outs() << "x";
5748 else
5749 outs() << "-";
5750}
5751
5752static void PrintXarFilesSummary(const char *XarFilename, xar_t xar) {
5753 xar_iter_t xi;
5754 xar_file_t xf;
5755 xar_iter_t xp;
5756 const char *key, *type, *mode, *user, *group, *size, *mtime, *name, *m;
5757 char *endp;
5758 uint32_t mode_value;
5759
5760 xi = xar_iter_new();
5761 if (!xi) {
5762 errs() << "Can't obtain an xar iterator for xar archive "
5763 << XarFilename << "\n";
5764 return;
5765 }
5766
5767 // Go through the xar's files.
5768 for (xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)) {
5769 xp = xar_iter_new();
5770 if(!xp){
5771 errs() << "Can't obtain an xar iterator for xar archive "
5772 << XarFilename << "\n";
5773 return;
5774 }
5775 type = nullptr;
5776 mode = nullptr;
5777 user = nullptr;
5778 group = nullptr;
5779 size = nullptr;
5780 mtime = nullptr;
5781 name = nullptr;
5782 for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){
5783 const char *val = nullptr;
5784 xar_prop_get(xf, key, &val);
5785#if 0 // Useful for debugging.
5786 outs() << "key: " << key << " value: " << val << "\n";
5787#endif
5788 if(strcmp(key, "type") == 0)
5789 type = val;
5790 if(strcmp(key, "mode") == 0)
5791 mode = val;
5792 if(strcmp(key, "user") == 0)
5793 user = val;
5794 if(strcmp(key, "group") == 0)
5795 group = val;
5796 if(strcmp(key, "data/size") == 0)
5797 size = val;
5798 if(strcmp(key, "mtime") == 0)
5799 mtime = val;
5800 if(strcmp(key, "name") == 0)
5801 name = val;
5802 }
5803 if(mode != nullptr){
5804 mode_value = strtoul(mode, &endp, 8);
5805 if(*endp != '\0')
5806 outs() << "(mode: \"" << mode << "\" contains non-octal chars) ";
5807 if(strcmp(type, "file") == 0)
5808 mode_value |= S_IFREG;
5809 PrintModeVerbose(mode_value);
5810 outs() << " ";
5811 }
5812 if(user != nullptr)
5813 outs() << format("%10s/", user);
5814 if(group != nullptr)
5815 outs() << format("%-10s ", group);
5816 if(size != nullptr)
5817 outs() << format("%7s ", size);
5818 if(mtime != nullptr){
5819 for(m = mtime; *m != 'T' && *m != '\0'; m++)
5820 outs() << *m;
5821 if(*m == 'T')
5822 m++;
5823 outs() << " ";
5824 for( ; *m != 'Z' && *m != '\0'; m++)
5825 outs() << *m;
5826 outs() << " ";
5827 }
5828 if(name != nullptr)
5829 outs() << name;
5830 outs() << "\n";
5831 }
5832}
5833
5834static void DumpBitcodeSection(MachOObjectFile *O, const char *sect,
5835 uint32_t size, bool verbose,
5836 bool PrintXarHeader, bool PrintXarFileHeaders,
5837 std::string XarMemberName) {
5838 if(size < sizeof(struct xar_header)) {
5839 outs() << "size of (__LLVM,__bundle) section too small (smaller than size "
5840 "of struct xar_header)\n";
5841 return;
5842 }
5843 struct xar_header XarHeader;
5844 memcpy(&XarHeader, sect, sizeof(struct xar_header));
5845 if (sys::IsLittleEndianHost)
5846 swapStruct(XarHeader);
5847 if (PrintXarHeader) {
5848 if (!XarMemberName.empty())
5849 outs() << "In xar member " << XarMemberName << ": ";
5850 else
5851 outs() << "For (__LLVM,__bundle) section: ";
5852 outs() << "xar header\n";
5853 if (XarHeader.magic == XAR_HEADER_MAGIC)
5854 outs() << " magic XAR_HEADER_MAGIC\n";
5855 else
5856 outs() << " magic "
5857 << format_hex(XarHeader.magic, 10, true)
5858 << " (not XAR_HEADER_MAGIC)\n";
5859 outs() << " size " << XarHeader.size << "\n";
5860 outs() << " version " << XarHeader.version << "\n";
5861 outs() << " toc_length_compressed " << XarHeader.toc_length_compressed
5862 << "\n";
5863 outs() << "toc_length_uncompressed " << XarHeader.toc_length_uncompressed
5864 << "\n";
5865 outs() << " cksum_alg ";
5866 switch (XarHeader.cksum_alg) {
5867 case XAR_CKSUM_NONE:
5868 outs() << "XAR_CKSUM_NONE\n";
5869 break;
5870 case XAR_CKSUM_SHA1:
5871 outs() << "XAR_CKSUM_SHA1\n";
5872 break;
5873 case XAR_CKSUM_MD5:
5874 outs() << "XAR_CKSUM_MD5\n";
5875 break;
5876 case XAR_CKSUM_SHA256:
5877 outs() << "XAR_CKSUM_SHA256\n";
5878 break;
5879 case XAR_CKSUM_SHA512:
5880 outs() << "XAR_CKSUM_SHA512\n";
5881 break;
5882 default:
5883 outs() << XarHeader.cksum_alg << "\n";
5884 }
5885 }
5886
5887 SmallString<128> XarFilename;
5888 int FD;
5889 std::error_code XarEC =
5890 sys::fs::createTemporaryFile("llvm-objdump", "xar", FD, XarFilename);
5891 if (XarEC) {
5892 errs() << XarEC.message() << "\n";
5893 return;
5894 }
5895 tool_output_file XarFile(XarFilename, FD);
5896 raw_fd_ostream &XarOut = XarFile.os();
5897 StringRef XarContents(sect, size);
5898 XarOut << XarContents;
5899 XarOut.close();
5900 if (XarOut.has_error())
5901 return;
5902
5903 xar_t xar = xar_open(XarFilename.c_str(), READ);
5904 if (!xar) {
5905 errs() << "Can't create temporary xar archive " << XarFilename << "\n";
5906 return;
5907 }
5908
5909 SmallString<128> TocFilename;
5910 std::error_code TocEC =
5911 sys::fs::createTemporaryFile("llvm-objdump", "toc", TocFilename);
5912 if (TocEC) {
5913 errs() << TocEC.message() << "\n";
5914 return;
5915 }
5916 xar_serialize(xar, TocFilename.c_str());
5917
5918 if (PrintXarFileHeaders) {
5919 if (!XarMemberName.empty())
5920 outs() << "In xar member " << XarMemberName << ": ";
5921 else
5922 outs() << "For (__LLVM,__bundle) section: ";
5923 outs() << "xar archive files:\n";
5924 PrintXarFilesSummary(XarFilename.c_str(), xar);
5925 }
5926
5927 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
5928 MemoryBuffer::getFileOrSTDIN(TocFilename.c_str());
5929 if (std::error_code EC = FileOrErr.getError()) {
5930 errs() << EC.message() << "\n";
5931 return;
5932 }
5933 std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
5934
5935 if (!XarMemberName.empty())
5936 outs() << "In xar member " << XarMemberName << ": ";
5937 else
5938 outs() << "For (__LLVM,__bundle) section: ";
5939 outs() << "xar table of contents:\n";
5940 outs() << Buffer->getBuffer() << "\n";
5941
5942 // TODO: Go through the xar's files.
5943 xar_iter_t xi = xar_iter_new();
5944 if(!xi){
5945 errs() << "Can't obtain an xar iterator for xar archive "
5946 << XarFilename.c_str() << "\n";
5947 xar_close(xar);
5948 return;
5949 }
5950 for(xar_file_t xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)){
5951 const char *key;
5952 xar_iter_t xp;
5953 const char *member_name, *member_type, *member_size_string;
5954 size_t member_size;
5955
5956 xp = xar_iter_new();
5957 if(!xp){
5958 errs() << "Can't obtain an xar iterator for xar archive "
5959 << XarFilename.c_str() << "\n";
5960 xar_close(xar);
5961 return;
5962 }
5963 member_name = NULL;
5964 member_type = NULL;
5965 member_size_string = NULL;
5966 for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){
5967 const char *val = nullptr;
5968 xar_prop_get(xf, key, &val);
5969#if 0 // Useful for debugging.
5970 outs() << "key: " << key << " value: " << val << "\n";
5971#endif
5972 if(strcmp(key, "name") == 0)
5973 member_name = val;
5974 if(strcmp(key, "type") == 0)
5975 member_type = val;
5976 if(strcmp(key, "data/size") == 0)
5977 member_size_string = val;
5978 }
5979 /*
5980 * If we find a file with a name, date/size and type properties
5981 * and with the type being "file" see if that is a xar file.
5982 */
5983 if (member_name != NULL && member_type != NULL &&
5984 strcmp(member_type, "file") == 0 &&
5985 member_size_string != NULL){
5986 // Extract the file into a buffer.
5987 char *endptr;
5988 member_size = strtoul(member_size_string, &endptr, 10);
5989 if (*endptr == '\0' && member_size != 0) {
5990 char *buffer = (char *) ::operator new (member_size);
5991 if (xar_extract_tobuffersz(xar, xf, &buffer, &member_size) == 0) {
5992#if 0 // Useful for debugging.
5993 outs() << "xar member: " << member_name << " extracted\n";
5994#endif
5995 // Set the XarMemberName we want to see printed in the header.
5996 std::string OldXarMemberName;
5997 // If XarMemberName is already set this is nested. So
5998 // save the old name and create the nested name.
5999 if (!XarMemberName.empty()) {
6000 OldXarMemberName = XarMemberName;
6001 XarMemberName =
6002 (Twine("[") + XarMemberName + "]" + member_name).str();
6003 } else {
6004 OldXarMemberName = "";
6005 XarMemberName = member_name;
6006 }
6007 // See if this is could be a xar file (nested).
6008 if (member_size >= sizeof(struct xar_header)) {
6009#if 0 // Useful for debugging.
6010 outs() << "could be a xar file: " << member_name << "\n";
6011#endif
6012 memcpy((char *)&XarHeader, buffer, sizeof(struct xar_header));
6013 if (sys::IsLittleEndianHost)
6014 swapStruct(XarHeader);
6015 if(XarHeader.magic == XAR_HEADER_MAGIC)
6016 DumpBitcodeSection(O, buffer, member_size, verbose,
6017 PrintXarHeader, PrintXarFileHeaders,
6018 XarMemberName);
6019 }
6020 XarMemberName = OldXarMemberName;
6021 }
6022 delete buffer;
6023 }
6024 }
6025 xar_iter_free(xp);
6026 }
6027 xar_close(xar);
6028}
6029#endif // defined(HAVE_LIBXAR)
6030
Kevin Enderby0fc11822015-04-01 20:57:01 +00006031static void printObjcMetaData(MachOObjectFile *O, bool verbose) {
6032 if (O->is64Bit())
6033 printObjc2_64bit_MetaData(O, verbose);
6034 else {
6035 MachO::mach_header H;
6036 H = O->getHeader();
6037 if (H.cputype == MachO::CPU_TYPE_ARM)
6038 printObjc2_32bit_MetaData(O, verbose);
6039 else {
6040 // This is the 32-bit non-arm cputype case. Which is normally
6041 // the first Objective-C ABI. But it may be the case of a
6042 // binary for the iOS simulator which is the second Objective-C
6043 // ABI. In that case printObjc1_32bit_MetaData() will determine that
6044 // and return false.
Hans Wennborgcc9deb42015-09-29 18:02:48 +00006045 if (!printObjc1_32bit_MetaData(O, verbose))
Kevin Enderby0fc11822015-04-01 20:57:01 +00006046 printObjc2_32bit_MetaData(O, verbose);
6047 }
6048 }
6049}
6050
Kevin Enderbybf246f52014-09-24 23:08:22 +00006051// GuessLiteralPointer returns a string which for the item in the Mach-O file
6052// for the address passed in as ReferenceValue for printing as a comment with
6053// the instruction and also returns the corresponding type of that item
6054// indirectly through ReferenceType.
6055//
6056// If ReferenceValue is an address of literal cstring then a pointer to the
6057// cstring is returned and ReferenceType is set to
6058// LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr .
6059//
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006060// If ReferenceValue is an address of an Objective-C CFString, Selector ref or
6061// Class ref that name is returned and the ReferenceType is set accordingly.
6062//
6063// Lastly, literals which are Symbol address in a literal pool are looked for
6064// and if found the symbol name is returned and ReferenceType is set to
6065// LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr .
6066//
6067// If there is no item in the Mach-O file for the address passed in as
6068// ReferenceValue nullptr is returned and ReferenceType is unchanged.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00006069static const char *GuessLiteralPointer(uint64_t ReferenceValue,
6070 uint64_t ReferencePC,
6071 uint64_t *ReferenceType,
6072 struct DisassembleInfo *info) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00006073 // First see if there is an external relocation entry at the ReferencePC.
Kevin Enderbyd90a4172015-10-10 00:05:01 +00006074 if (info->O->getHeader().filetype == MachO::MH_OBJECT) {
6075 uint64_t sect_addr = info->S.getAddress();
6076 uint64_t sect_offset = ReferencePC - sect_addr;
6077 bool reloc_found = false;
6078 DataRefImpl Rel;
6079 MachO::any_relocation_info RE;
6080 bool isExtern = false;
6081 SymbolRef Symbol;
6082 for (const RelocationRef &Reloc : info->S.relocations()) {
6083 uint64_t RelocOffset = Reloc.getOffset();
6084 if (RelocOffset == sect_offset) {
6085 Rel = Reloc.getRawDataRefImpl();
6086 RE = info->O->getRelocation(Rel);
6087 if (info->O->isRelocationScattered(RE))
6088 continue;
6089 isExtern = info->O->getPlainRelocationExternal(RE);
6090 if (isExtern) {
6091 symbol_iterator RelocSym = Reloc.getSymbol();
6092 Symbol = *RelocSym;
6093 }
6094 reloc_found = true;
6095 break;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006096 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006097 }
Kevin Enderbyd90a4172015-10-10 00:05:01 +00006098 // If there is an external relocation entry for a symbol in a section
6099 // then used that symbol's value for the value of the reference.
6100 if (reloc_found && isExtern) {
6101 if (info->O->getAnyRelocationPCRel(RE)) {
6102 unsigned Type = info->O->getAnyRelocationType(RE);
6103 if (Type == MachO::X86_64_RELOC_SIGNED) {
6104 ReferenceValue = Symbol.getValue();
6105 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006106 }
6107 }
6108 }
6109
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006110 // Look for literals such as Objective-C CFStrings refs, Selector refs,
6111 // Message refs and Class refs.
6112 bool classref, selref, msgref, cfstring;
6113 uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref,
6114 selref, msgref, cfstring);
David Blaikie33dd45d02015-03-23 18:39:02 +00006115 if (classref && pointer_value == 0) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006116 // Note the ReferenceValue is a pointer into the __objc_classrefs section.
6117 // And the pointer_value in that section is typically zero as it will be
6118 // set by dyld as part of the "bind information".
6119 const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info);
6120 if (name != nullptr) {
6121 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
Hans Wennborgdb53e302014-10-23 21:59:17 +00006122 const char *class_name = strrchr(name, '$');
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006123 if (class_name != nullptr && class_name[1] == '_' &&
6124 class_name[2] != '\0') {
6125 info->class_name = class_name + 2;
6126 return name;
6127 }
6128 }
6129 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006130
David Blaikie33dd45d02015-03-23 18:39:02 +00006131 if (classref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006132 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
6133 const char *name =
6134 get_objc2_64bit_class_name(pointer_value, ReferenceValue, info);
6135 if (name != nullptr)
6136 info->class_name = name;
6137 else
6138 name = "bad class ref";
Kevin Enderbybf246f52014-09-24 23:08:22 +00006139 return name;
6140 }
6141
David Blaikie33dd45d02015-03-23 18:39:02 +00006142 if (cfstring) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006143 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref;
6144 const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info);
6145 return name;
6146 }
6147
David Blaikie33dd45d02015-03-23 18:39:02 +00006148 if (selref && pointer_value == 0)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006149 pointer_value = get_objc2_64bit_selref(ReferenceValue, info);
6150
6151 if (pointer_value != 0)
6152 ReferenceValue = pointer_value;
6153
6154 const char *name = GuessCstringPointer(ReferenceValue, info);
6155 if (name) {
David Blaikie33dd45d02015-03-23 18:39:02 +00006156 if (pointer_value != 0 && selref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006157 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref;
6158 info->selector_name = name;
David Blaikie33dd45d02015-03-23 18:39:02 +00006159 } else if (pointer_value != 0 && msgref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006160 info->class_name = nullptr;
6161 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref;
6162 info->selector_name = name;
6163 } else
6164 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr;
6165 return name;
6166 }
6167
6168 // Lastly look for an indirect symbol with this ReferenceValue which is in
6169 // a literal pool. If found return that symbol name.
6170 name = GuessIndirectSymbol(ReferenceValue, info);
6171 if (name) {
6172 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr;
6173 return name;
6174 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006175
6176 return nullptr;
6177}
6178
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006179// SymbolizerSymbolLookUp is the symbol lookup function passed when creating
Kevin Enderbybf246f52014-09-24 23:08:22 +00006180// the Symbolizer. It looks up the ReferenceValue using the info passed via the
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006181// pointer to the struct DisassembleInfo that was passed when MCSymbolizer
6182// is created and returns the symbol name that matches the ReferenceValue or
6183// nullptr if none. The ReferenceType is passed in for the IN type of
6184// reference the instruction is making from the values in defined in the header
6185// "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific
6186// Out type and the ReferenceName will also be set which is added as a comment
6187// to the disassembled instruction.
6188//
Kevin Enderby04bf6932014-10-28 23:39:46 +00006189#if HAVE_CXXABI_H
6190// If the symbol name is a C++ mangled name then the demangled name is
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006191// returned through ReferenceName and ReferenceType is set to
6192// LLVMDisassembler_ReferenceType_DeMangled_Name .
Kevin Enderby04bf6932014-10-28 23:39:46 +00006193#endif
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006194//
6195// When this is called to get a symbol name for a branch target then the
6196// ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
6197// SymbolValue will be looked for in the indirect symbol table to determine if
6198// it is an address for a symbol stub. If so then the symbol name for that
6199// stub is returned indirectly through ReferenceName and then ReferenceType is
6200// set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
6201//
Kevin Enderbybf246f52014-09-24 23:08:22 +00006202// When this is called with an value loaded via a PC relative load then
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006203// ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
6204// SymbolValue is checked to be an address of literal pointer, symbol pointer,
6205// or an Objective-C meta data reference. If so the output ReferenceType is
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006206// set to correspond to that as well as setting the ReferenceName.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00006207static const char *SymbolizerSymbolLookUp(void *DisInfo,
6208 uint64_t ReferenceValue,
6209 uint64_t *ReferenceType,
6210 uint64_t ReferencePC,
6211 const char **ReferenceName) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006212 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006213 // If no verbose symbolic information is wanted then just return nullptr.
David Blaikie33dd45d02015-03-23 18:39:02 +00006214 if (!info->verbose) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00006215 *ReferenceName = nullptr;
6216 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006217 return nullptr;
6218 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006219
Kevin Enderbyf6d25852015-01-31 00:37:11 +00006220 const char *SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
Kevin Enderbybf246f52014-09-24 23:08:22 +00006221
Kevin Enderby85974882014-09-26 22:20:44 +00006222 if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) {
6223 *ReferenceName = GuessIndirectSymbol(ReferenceValue, info);
Kevin Enderby04bf6932014-10-28 23:39:46 +00006224 if (*ReferenceName != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006225 method_reference(info, ReferenceType, ReferenceName);
6226 if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message)
6227 *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub;
6228 } else
Kevin Enderby04bf6932014-10-28 23:39:46 +00006229#if HAVE_CXXABI_H
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006230 if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
Kevin Enderby04bf6932014-10-28 23:39:46 +00006231 if (info->demangled_name != nullptr)
6232 free(info->demangled_name);
6233 int status;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006234 info->demangled_name =
6235 abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status);
Kevin Enderby04bf6932014-10-28 23:39:46 +00006236 if (info->demangled_name != nullptr) {
6237 *ReferenceName = info->demangled_name;
6238 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
6239 } else
6240 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6241 } else
6242#endif
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006243 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6244 } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) {
6245 *ReferenceName =
6246 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
Kevin Enderby85974882014-09-26 22:20:44 +00006247 if (*ReferenceName)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006248 method_reference(info, ReferenceType, ReferenceName);
Kevin Enderby85974882014-09-26 22:20:44 +00006249 else
6250 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006251 // If this is arm64 and the reference is an adrp instruction save the
6252 // instruction, passed in ReferenceValue and the address of the instruction
6253 // for use later if we see and add immediate instruction.
6254 } else if (info->O->getArch() == Triple::aarch64 &&
6255 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) {
6256 info->adrp_inst = ReferenceValue;
6257 info->adrp_addr = ReferencePC;
6258 SymbolName = nullptr;
6259 *ReferenceName = nullptr;
6260 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6261 // If this is arm64 and reference is an add immediate instruction and we
6262 // have
6263 // seen an adrp instruction just before it and the adrp's Xd register
6264 // matches
6265 // this add's Xn register reconstruct the value being referenced and look to
6266 // see if it is a literal pointer. Note the add immediate instruction is
6267 // passed in ReferenceValue.
6268 } else if (info->O->getArch() == Triple::aarch64 &&
6269 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri &&
6270 ReferencePC - 4 == info->adrp_addr &&
6271 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
6272 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
6273 uint32_t addxri_inst;
6274 uint64_t adrp_imm, addxri_imm;
6275
6276 adrp_imm =
6277 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
6278 if (info->adrp_inst & 0x0200000)
6279 adrp_imm |= 0xfffffffffc000000LL;
6280
6281 addxri_inst = ReferenceValue;
6282 addxri_imm = (addxri_inst >> 10) & 0xfff;
6283 if (((addxri_inst >> 22) & 0x3) == 1)
6284 addxri_imm <<= 12;
6285
6286 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
6287 (adrp_imm << 12) + addxri_imm;
6288
6289 *ReferenceName =
6290 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6291 if (*ReferenceName == nullptr)
6292 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6293 // If this is arm64 and the reference is a load register instruction and we
6294 // have seen an adrp instruction just before it and the adrp's Xd register
6295 // matches this add's Xn register reconstruct the value being referenced and
6296 // look to see if it is a literal pointer. Note the load register
6297 // instruction is passed in ReferenceValue.
6298 } else if (info->O->getArch() == Triple::aarch64 &&
6299 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui &&
6300 ReferencePC - 4 == info->adrp_addr &&
6301 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
6302 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
6303 uint32_t ldrxui_inst;
6304 uint64_t adrp_imm, ldrxui_imm;
6305
6306 adrp_imm =
6307 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
6308 if (info->adrp_inst & 0x0200000)
6309 adrp_imm |= 0xfffffffffc000000LL;
6310
6311 ldrxui_inst = ReferenceValue;
6312 ldrxui_imm = (ldrxui_inst >> 10) & 0xfff;
6313
6314 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
6315 (adrp_imm << 12) + (ldrxui_imm << 3);
6316
6317 *ReferenceName =
6318 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6319 if (*ReferenceName == nullptr)
6320 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6321 }
6322 // If this arm64 and is an load register (PC-relative) instruction the
6323 // ReferenceValue is the PC plus the immediate value.
6324 else if (info->O->getArch() == Triple::aarch64 &&
6325 (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl ||
6326 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR)) {
6327 *ReferenceName =
6328 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6329 if (*ReferenceName == nullptr)
6330 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Kevin Enderby85974882014-09-26 22:20:44 +00006331 }
Kevin Enderby04bf6932014-10-28 23:39:46 +00006332#if HAVE_CXXABI_H
6333 else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
6334 if (info->demangled_name != nullptr)
6335 free(info->demangled_name);
6336 int status;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006337 info->demangled_name =
6338 abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status);
Kevin Enderby04bf6932014-10-28 23:39:46 +00006339 if (info->demangled_name != nullptr) {
6340 *ReferenceName = info->demangled_name;
6341 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
6342 }
6343 }
6344#endif
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006345 else {
Kevin Enderbybf246f52014-09-24 23:08:22 +00006346 *ReferenceName = nullptr;
6347 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6348 }
6349
6350 return SymbolName;
6351}
6352
Kevin Enderbybf246f52014-09-24 23:08:22 +00006353/// \brief Emits the comments that are stored in the CommentStream.
6354/// Each comment in the CommentStream must end with a newline.
6355static void emitComments(raw_svector_ostream &CommentStream,
6356 SmallString<128> &CommentsToEmit,
6357 formatted_raw_ostream &FormattedOS,
6358 const MCAsmInfo &MAI) {
6359 // Flush the stream before taking its content.
Kevin Enderbybf246f52014-09-24 23:08:22 +00006360 StringRef Comments = CommentsToEmit.str();
6361 // Get the default information for printing a comment.
6362 const char *CommentBegin = MAI.getCommentString();
6363 unsigned CommentColumn = MAI.getCommentColumn();
6364 bool IsFirst = true;
6365 while (!Comments.empty()) {
6366 if (!IsFirst)
6367 FormattedOS << '\n';
6368 // Emit a line of comments.
6369 FormattedOS.PadToColumn(CommentColumn);
6370 size_t Position = Comments.find('\n');
6371 FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
6372 // Move after the newline character.
6373 Comments = Comments.substr(Position + 1);
6374 IsFirst = false;
6375 }
6376 FormattedOS.flush();
6377
6378 // Tell the comment stream that the vector changed underneath it.
6379 CommentsToEmit.clear();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006380}
6381
Kevin Enderby95df54c2015-02-04 01:01:38 +00006382static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
6383 StringRef DisSegName, StringRef DisSectName) {
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006384 const char *McpuDefault = nullptr;
6385 const Target *ThumbTarget = nullptr;
6386 const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006387 if (!TheTarget) {
6388 // GetTarget prints out stuff.
6389 return;
6390 }
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006391 if (MCPU.empty() && McpuDefault)
6392 MCPU = McpuDefault;
6393
Ahmed Charles56440fd2014-03-06 05:51:42 +00006394 std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006395 std::unique_ptr<const MCInstrInfo> ThumbInstrInfo;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006396 if (ThumbTarget)
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006397 ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo());
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006398
Kevin Enderbyc9595622014-08-06 23:24:41 +00006399 // Package up features to be passed to target/subtarget
6400 std::string FeaturesStr;
6401 if (MAttrs.size()) {
6402 SubtargetFeatures Features;
6403 for (unsigned i = 0; i != MAttrs.size(); ++i)
6404 Features.AddFeature(MAttrs[i]);
6405 FeaturesStr = Features.getString();
6406 }
6407
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006408 // Set up disassembler.
Ahmed Charles56440fd2014-03-06 05:51:42 +00006409 std::unique_ptr<const MCRegisterInfo> MRI(
6410 TheTarget->createMCRegInfo(TripleName));
6411 std::unique_ptr<const MCAsmInfo> AsmInfo(
Rafael Espindola227144c2013-05-13 01:16:13 +00006412 TheTarget->createMCAsmInfo(*MRI, TripleName));
Ahmed Charles56440fd2014-03-06 05:51:42 +00006413 std::unique_ptr<const MCSubtargetInfo> STI(
Kevin Enderbyc9595622014-08-06 23:24:41 +00006414 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
Craig Toppere6cb63e2014-04-25 04:24:47 +00006415 MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr);
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006416 std::unique_ptr<MCDisassembler> DisAsm(
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006417 TheTarget->createMCDisassembler(*STI, Ctx));
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006418 std::unique_ptr<MCSymbolizer> Symbolizer;
6419 struct DisassembleInfo SymbolizerInfo;
6420 std::unique_ptr<MCRelocationInfo> RelInfo(
6421 TheTarget->createMCRelocationInfo(TripleName, Ctx));
6422 if (RelInfo) {
6423 Symbolizer.reset(TheTarget->createMCSymbolizer(
6424 TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
David Blaikie186db432015-01-18 20:45:48 +00006425 &SymbolizerInfo, &Ctx, std::move(RelInfo)));
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006426 DisAsm->setSymbolizer(std::move(Symbolizer));
6427 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006428 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
Ahmed Charles56440fd2014-03-06 05:51:42 +00006429 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
Daniel Sanders50f17232015-09-15 16:17:27 +00006430 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI));
Kevin Enderbybf246f52014-09-24 23:08:22 +00006431 // Set the display preference for hex vs. decimal immediates.
6432 IP->setPrintImmHex(PrintImmHex);
6433 // Comment stream and backing vector.
6434 SmallString<128> CommentsToEmit;
6435 raw_svector_ostream CommentStream(CommentsToEmit);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00006436 // FIXME: Setting the CommentStream in the InstPrinter is problematic in that
6437 // if it is done then arm64 comments for string literals don't get printed
6438 // and some constant get printed instead and not setting it causes intel
6439 // (32-bit and 64-bit) comments printed with different spacing before the
6440 // comment causing different diffs with the 'C' disassembler library API.
6441 // IP->setCommentStream(CommentStream);
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006442
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006443 if (!AsmInfo || !STI || !DisAsm || !IP) {
Michael J. Spencerc1363cf2011-10-07 19:25:47 +00006444 errs() << "error: couldn't initialize disassembler for target "
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006445 << TripleName << '\n';
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006446 return;
6447 }
6448
Tim Northover09ca33e2016-04-22 23:23:31 +00006449 // Set up separate thumb disassembler if needed.
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006450 std::unique_ptr<const MCRegisterInfo> ThumbMRI;
6451 std::unique_ptr<const MCAsmInfo> ThumbAsmInfo;
6452 std::unique_ptr<const MCSubtargetInfo> ThumbSTI;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006453 std::unique_ptr<MCDisassembler> ThumbDisAsm;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006454 std::unique_ptr<MCInstPrinter> ThumbIP;
6455 std::unique_ptr<MCContext> ThumbCtx;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006456 std::unique_ptr<MCSymbolizer> ThumbSymbolizer;
6457 struct DisassembleInfo ThumbSymbolizerInfo;
6458 std::unique_ptr<MCRelocationInfo> ThumbRelInfo;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006459 if (ThumbTarget) {
6460 ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName));
6461 ThumbAsmInfo.reset(
6462 ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName));
6463 ThumbSTI.reset(
6464 ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MCPU, FeaturesStr));
6465 ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr));
6466 ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx));
Kevin Enderby930fdc72014-11-06 19:00:13 +00006467 MCContext *PtrThumbCtx = ThumbCtx.get();
6468 ThumbRelInfo.reset(
6469 ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx));
6470 if (ThumbRelInfo) {
6471 ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer(
6472 ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
David Blaikie186db432015-01-18 20:45:48 +00006473 &ThumbSymbolizerInfo, PtrThumbCtx, std::move(ThumbRelInfo)));
Kevin Enderby930fdc72014-11-06 19:00:13 +00006474 ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer));
6475 }
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006476 int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect();
6477 ThumbIP.reset(ThumbTarget->createMCInstPrinter(
Daniel Sanders50f17232015-09-15 16:17:27 +00006478 Triple(ThumbTripleName), ThumbAsmPrinterVariant, *ThumbAsmInfo,
6479 *ThumbInstrInfo, *ThumbMRI));
Kevin Enderbybf246f52014-09-24 23:08:22 +00006480 // Set the display preference for hex vs. decimal immediates.
6481 ThumbIP->setPrintImmHex(PrintImmHex);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006482 }
6483
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006484 if (ThumbTarget && (!ThumbAsmInfo || !ThumbSTI || !ThumbDisAsm || !ThumbIP)) {
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006485 errs() << "error: couldn't initialize disassembler for target "
6486 << ThumbTripleName << '\n';
6487 return;
6488 }
6489
Charles Davis8bdfafd2013-09-01 04:28:48 +00006490 MachO::mach_header Header = MachOOF->getHeader();
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006491
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006492 // FIXME: Using the -cfg command line option, this code used to be able to
6493 // annotate relocations with the referenced symbol's name, and if this was
6494 // inside a __[cf]string section, the data it points to. This is now replaced
6495 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
Owen Andersond9243c42011-10-17 21:37:35 +00006496 std::vector<SectionRef> Sections;
6497 std::vector<SymbolRef> Symbols;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006498 SmallVector<uint64_t, 8> FoundFns;
Kevin Enderby273ae012013-06-06 17:20:50 +00006499 uint64_t BaseSegmentAddress;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006500
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00006501 getSectionsAndSymbols(MachOOF, Sections, Symbols, FoundFns,
Kevin Enderby273ae012013-06-06 17:20:50 +00006502 BaseSegmentAddress);
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006503
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006504 // Sort the symbols by address, just in case they didn't come in that way.
Owen Andersond9243c42011-10-17 21:37:35 +00006505 std::sort(Symbols.begin(), Symbols.end(), SymbolSorter());
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006506
Kevin Enderby273ae012013-06-06 17:20:50 +00006507 // Build a data in code table that is sorted on by the address of each entry.
6508 uint64_t BaseAddress = 0;
Charles Davis8bdfafd2013-09-01 04:28:48 +00006509 if (Header.filetype == MachO::MH_OBJECT)
Rafael Espindola80291272014-10-08 15:28:58 +00006510 BaseAddress = Sections[0].getAddress();
Kevin Enderby273ae012013-06-06 17:20:50 +00006511 else
6512 BaseAddress = BaseSegmentAddress;
6513 DiceTable Dices;
Kevin Enderby273ae012013-06-06 17:20:50 +00006514 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
Rafael Espindola5e812af2014-01-30 02:49:50 +00006515 DI != DE; ++DI) {
Kevin Enderby273ae012013-06-06 17:20:50 +00006516 uint32_t Offset;
6517 DI->getOffset(Offset);
6518 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
6519 }
6520 array_pod_sort(Dices.begin(), Dices.end());
6521
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006522#ifndef NDEBUG
6523 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
6524#else
6525 raw_ostream &DebugOut = nulls();
6526#endif
6527
Ahmed Charles56440fd2014-03-06 05:51:42 +00006528 std::unique_ptr<DIContext> diContext;
Rafael Espindola9b709252013-04-13 01:45:40 +00006529 ObjectFile *DbgObj = MachOOF;
Benjamin Kramer699128e2011-09-21 01:13:19 +00006530 // Try to find debug info and set up the DIContext for it.
6531 if (UseDbg) {
Benjamin Kramer699128e2011-09-21 01:13:19 +00006532 // A separate DSym file path was specified, parse it as a macho file,
6533 // get the sections and supply it to the section name parsing machinery.
6534 if (!DSYMFile.empty()) {
Rafael Espindola48af1c22014-08-19 18:44:46 +00006535 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
Rafael Espindolaadf21f22014-07-06 17:43:13 +00006536 MemoryBuffer::getFileOrSTDIN(DSYMFile);
Rafael Espindola48af1c22014-08-19 18:44:46 +00006537 if (std::error_code EC = BufOrErr.getError()) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +00006538 errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n';
Benjamin Kramer699128e2011-09-21 01:13:19 +00006539 return;
6540 }
Rafael Espindola48af1c22014-08-19 18:44:46 +00006541 DbgObj =
6542 ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef())
6543 .get()
6544 .release();
Benjamin Kramer699128e2011-09-21 01:13:19 +00006545 }
6546
Eric Christopher7370b552012-11-12 21:40:38 +00006547 // Setup the DIContext
Zachary Turner6489d7b2015-04-23 17:37:47 +00006548 diContext.reset(new DWARFContextInMemory(*DbgObj));
Benjamin Kramer699128e2011-09-21 01:13:19 +00006549 }
6550
Colin LeMahieufcc32762015-07-29 19:08:10 +00006551 if (FilterSections.size() == 0)
Kevin Enderby95df54c2015-02-04 01:01:38 +00006552 outs() << "(" << DisSegName << "," << DisSectName << ") section\n";
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00006553
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006554 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
Owen Andersond9243c42011-10-17 21:37:35 +00006555 StringRef SectName;
Kevin Enderby95df54c2015-02-04 01:01:38 +00006556 if (Sections[SectIdx].getName(SectName) || SectName != DisSectName)
6557 continue;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006558
Rafael Espindolaa9f810b2012-12-21 03:47:03 +00006559 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006560
Rafael Espindolab0f76a42013-04-05 15:15:22 +00006561 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
Kevin Enderby95df54c2015-02-04 01:01:38 +00006562 if (SegmentName != DisSegName)
Rafael Espindolaa9f810b2012-12-21 03:47:03 +00006563 continue;
6564
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006565 StringRef BytesStr;
6566 Sections[SectIdx].getContents(BytesStr);
Aaron Ballman106fd7b2014-11-12 14:01:17 +00006567 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
6568 BytesStr.size());
Rafael Espindola80291272014-10-08 15:28:58 +00006569 uint64_t SectAddress = Sections[SectIdx].getAddress();
Rafael Espindolabd604f22014-11-07 00:52:15 +00006570
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006571 bool symbolTableWorked = false;
6572
Kevin Enderbybf246f52014-09-24 23:08:22 +00006573 // Create a map of symbol addresses to symbol names for use by
6574 // the SymbolizerSymbolLookUp() routine.
6575 SymbolAddressMap AddrMap;
Kevin Enderby6a221752015-03-17 17:10:57 +00006576 bool DisSymNameFound = false;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006577 for (const SymbolRef &Symbol : MachOOF->symbols()) {
Kevin Enderby7bd8d992016-05-02 20:28:12 +00006578 Expected<SymbolRef::Type> STOrErr = Symbol.getType();
6579 if (!STOrErr) {
6580 std::string Buf;
6581 raw_string_ostream OS(Buf);
6582 logAllUnhandledErrors(STOrErr.takeError(), OS, "");
6583 OS.flush();
6584 report_fatal_error(Buf);
6585 }
Kevin Enderby5afbc1c2016-03-23 20:27:00 +00006586 SymbolRef::Type ST = *STOrErr;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006587 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
6588 ST == SymbolRef::ST_Other) {
Rafael Espindoladea00162015-07-03 17:44:18 +00006589 uint64_t Address = Symbol.getValue();
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00006590 Expected<StringRef> SymNameOrErr = Symbol.getName();
6591 if (!SymNameOrErr) {
6592 std::string Buf;
6593 raw_string_ostream OS(Buf);
6594 logAllUnhandledErrors(SymNameOrErr.takeError(), OS, "");
6595 OS.flush();
6596 report_fatal_error(Buf);
6597 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00006598 StringRef SymName = *SymNameOrErr;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006599 AddrMap[Address] = SymName;
Kevin Enderby6a221752015-03-17 17:10:57 +00006600 if (!DisSymName.empty() && DisSymName == SymName)
6601 DisSymNameFound = true;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006602 }
6603 }
David Blaikie33dd45d02015-03-23 18:39:02 +00006604 if (!DisSymName.empty() && !DisSymNameFound) {
Kevin Enderby6a221752015-03-17 17:10:57 +00006605 outs() << "Can't find -dis-symname: " << DisSymName << "\n";
6606 return;
6607 }
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006608 // Set up the block of info used by the Symbolizer call backs.
Kevin Enderby8e29ec92015-03-17 22:26:11 +00006609 SymbolizerInfo.verbose = !NoSymbolicOperands;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006610 SymbolizerInfo.O = MachOOF;
6611 SymbolizerInfo.S = Sections[SectIdx];
Kevin Enderbybf246f52014-09-24 23:08:22 +00006612 SymbolizerInfo.AddrMap = &AddrMap;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006613 SymbolizerInfo.Sections = &Sections;
6614 SymbolizerInfo.class_name = nullptr;
6615 SymbolizerInfo.selector_name = nullptr;
6616 SymbolizerInfo.method = nullptr;
Kevin Enderby04bf6932014-10-28 23:39:46 +00006617 SymbolizerInfo.demangled_name = nullptr;
Kevin Enderby078be602014-10-23 19:53:12 +00006618 SymbolizerInfo.bindtable = nullptr;
Kevin Enderby10738222014-11-19 20:20:16 +00006619 SymbolizerInfo.adrp_addr = 0;
6620 SymbolizerInfo.adrp_inst = 0;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006621 // Same for the ThumbSymbolizer
Kevin Enderby8e29ec92015-03-17 22:26:11 +00006622 ThumbSymbolizerInfo.verbose = !NoSymbolicOperands;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006623 ThumbSymbolizerInfo.O = MachOOF;
6624 ThumbSymbolizerInfo.S = Sections[SectIdx];
6625 ThumbSymbolizerInfo.AddrMap = &AddrMap;
6626 ThumbSymbolizerInfo.Sections = &Sections;
6627 ThumbSymbolizerInfo.class_name = nullptr;
6628 ThumbSymbolizerInfo.selector_name = nullptr;
6629 ThumbSymbolizerInfo.method = nullptr;
6630 ThumbSymbolizerInfo.demangled_name = nullptr;
6631 ThumbSymbolizerInfo.bindtable = nullptr;
Kevin Enderby10738222014-11-19 20:20:16 +00006632 ThumbSymbolizerInfo.adrp_addr = 0;
6633 ThumbSymbolizerInfo.adrp_inst = 0;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006634
Kevin Enderby4b627be2016-04-28 20:14:13 +00006635 unsigned int Arch = MachOOF->getArch();
6636
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006637 // Disassemble symbol by symbol.
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006638 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00006639 Expected<StringRef> SymNameOrErr = Symbols[SymIdx].getName();
6640 if (!SymNameOrErr) {
6641 std::string Buf;
6642 raw_string_ostream OS(Buf);
6643 logAllUnhandledErrors(SymNameOrErr.takeError(), OS, "");
6644 OS.flush();
6645 report_fatal_error(Buf);
6646 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00006647 StringRef SymName = *SymNameOrErr;
Owen Andersond9243c42011-10-17 21:37:35 +00006648
Kevin Enderby7bd8d992016-05-02 20:28:12 +00006649 Expected<SymbolRef::Type> STOrErr = Symbols[SymIdx].getType();
6650 if (!STOrErr) {
6651 std::string Buf;
6652 raw_string_ostream OS(Buf);
6653 logAllUnhandledErrors(STOrErr.takeError(), OS, "");
6654 OS.flush();
6655 report_fatal_error(Buf);
6656 }
Kevin Enderby5afbc1c2016-03-23 20:27:00 +00006657 SymbolRef::Type ST = *STOrErr;
Kuba Breckade833222015-11-12 09:40:29 +00006658 if (ST != SymbolRef::ST_Function && ST != SymbolRef::ST_Data)
Owen Andersond9243c42011-10-17 21:37:35 +00006659 continue;
6660
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006661 // Make sure the symbol is defined in this section.
Rafael Espindola80291272014-10-08 15:28:58 +00006662 bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]);
Owen Andersond9243c42011-10-17 21:37:35 +00006663 if (!containsSym)
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006664 continue;
6665
Kevin Enderby6a221752015-03-17 17:10:57 +00006666 // If we are only disassembling one symbol see if this is that symbol.
6667 if (!DisSymName.empty() && DisSymName != SymName)
6668 continue;
6669
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006670 // Start at the address of the symbol relative to the section's address.
Rafael Espindoladea00162015-07-03 17:44:18 +00006671 uint64_t Start = Symbols[SymIdx].getValue();
Rafael Espindola80291272014-10-08 15:28:58 +00006672 uint64_t SectionAddress = Sections[SectIdx].getAddress();
Cameron Zwarich54478a52012-02-03 05:42:17 +00006673 Start -= SectionAddress;
Owen Andersond9243c42011-10-17 21:37:35 +00006674
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006675 // Stop disassembling either at the beginning of the next symbol or at
6676 // the end of the section.
Kevin Enderbyedd58722012-05-15 18:57:14 +00006677 bool containsNextSym = false;
Owen Andersond9243c42011-10-17 21:37:35 +00006678 uint64_t NextSym = 0;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006679 uint64_t NextSymIdx = SymIdx + 1;
Owen Andersond9243c42011-10-17 21:37:35 +00006680 while (Symbols.size() > NextSymIdx) {
Kevin Enderby7bd8d992016-05-02 20:28:12 +00006681 Expected<SymbolRef::Type> STOrErr = Symbols[NextSymIdx].getType();
6682 if (!STOrErr) {
6683 std::string Buf;
6684 raw_string_ostream OS(Buf);
6685 logAllUnhandledErrors(STOrErr.takeError(), OS, "");
6686 OS.flush();
6687 report_fatal_error(Buf);
6688 }
Kevin Enderby5afbc1c2016-03-23 20:27:00 +00006689 SymbolRef::Type NextSymType = *STOrErr;
Owen Andersond9243c42011-10-17 21:37:35 +00006690 if (NextSymType == SymbolRef::ST_Function) {
Rafael Espindola80291272014-10-08 15:28:58 +00006691 containsNextSym =
6692 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
Rafael Espindoladea00162015-07-03 17:44:18 +00006693 NextSym = Symbols[NextSymIdx].getValue();
Cameron Zwarich54478a52012-02-03 05:42:17 +00006694 NextSym -= SectionAddress;
Owen Andersond9243c42011-10-17 21:37:35 +00006695 break;
6696 }
6697 ++NextSymIdx;
6698 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006699
Rafael Espindola80291272014-10-08 15:28:58 +00006700 uint64_t SectSize = Sections[SectIdx].getSize();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006701 uint64_t End = containsNextSym ? NextSym : SectSize;
Owen Andersond9243c42011-10-17 21:37:35 +00006702 uint64_t Size;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006703
6704 symbolTableWorked = true;
Rafael Espindolabd604f22014-11-07 00:52:15 +00006705
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006706 DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
Tim Northover09ca33e2016-04-22 23:23:31 +00006707 bool IsThumb = MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb;
6708
6709 // We only need the dedicated Thumb target if there's a real choice
6710 // (i.e. we're not targeting M-class) and the function is Thumb.
6711 bool UseThumbTarget = IsThumb && ThumbTarget;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006712
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006713 outs() << SymName << ":\n";
6714 DILineInfo lastLine;
6715 for (uint64_t Index = Start; Index < End; Index += Size) {
6716 MCInst Inst;
Owen Andersond9243c42011-10-17 21:37:35 +00006717
Kevin Enderbybf246f52014-09-24 23:08:22 +00006718 uint64_t PC = SectAddress + Index;
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00006719 if (!NoLeadingAddr) {
6720 if (FullLeadingAddr) {
6721 if (MachOOF->is64Bit())
6722 outs() << format("%016" PRIx64, PC);
6723 else
6724 outs() << format("%08" PRIx64, PC);
6725 } else {
6726 outs() << format("%8" PRIx64 ":", PC);
6727 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006728 }
Kevin Enderby4b627be2016-04-28 20:14:13 +00006729 if (!NoShowRawInsn || Arch == Triple::arm)
Kevin Enderbybf246f52014-09-24 23:08:22 +00006730 outs() << "\t";
Kevin Enderby273ae012013-06-06 17:20:50 +00006731
6732 // Check the data in code table here to see if this is data not an
6733 // instruction to be disassembled.
6734 DiceTable Dice;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006735 Dice.push_back(std::make_pair(PC, DiceRef()));
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006736 dice_table_iterator DTI =
6737 std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(),
6738 compareDiceTableEntries);
6739 if (DTI != Dices.end()) {
Kevin Enderby273ae012013-06-06 17:20:50 +00006740 uint16_t Length;
6741 DTI->second.getLength(Length);
Kevin Enderby273ae012013-06-06 17:20:50 +00006742 uint16_t Kind;
6743 DTI->second.getKind(Kind);
Colin LeMahieufc32b1b2015-03-18 19:27:31 +00006744 Size = DumpDataInCode(Bytes.data() + Index, Length, Kind);
Kevin Enderby930fdc72014-11-06 19:00:13 +00006745 if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) &&
6746 (PC == (DTI->first + Length - 1)) && (Length & 1))
6747 Size++;
Kevin Enderby273ae012013-06-06 17:20:50 +00006748 continue;
6749 }
6750
Kevin Enderbybf246f52014-09-24 23:08:22 +00006751 SmallVector<char, 64> AnnotationsBytes;
6752 raw_svector_ostream Annotations(AnnotationsBytes);
6753
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006754 bool gotInst;
Tim Northover09ca33e2016-04-22 23:23:31 +00006755 if (UseThumbTarget)
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006756 gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006757 PC, DebugOut, Annotations);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006758 else
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006759 gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC,
Kevin Enderbybf246f52014-09-24 23:08:22 +00006760 DebugOut, Annotations);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006761 if (gotInst) {
Kevin Enderby4b627be2016-04-28 20:14:13 +00006762 if (!NoShowRawInsn || Arch == Triple::arm) {
Craig Topper0013be12015-09-21 05:32:41 +00006763 dumpBytes(makeArrayRef(Bytes.data() + Index, Size), outs());
Kevin Enderbybf246f52014-09-24 23:08:22 +00006764 }
6765 formatted_raw_ostream FormattedOS(outs());
Kevin Enderbybf246f52014-09-24 23:08:22 +00006766 StringRef AnnotationsStr = Annotations.str();
Tim Northover09ca33e2016-04-22 23:23:31 +00006767 if (UseThumbTarget)
Akira Hatanakab46d0232015-03-27 20:36:02 +00006768 ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr, *ThumbSTI);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006769 else
Akira Hatanaka1d079942015-03-28 20:44:05 +00006770 IP->printInst(&Inst, FormattedOS, AnnotationsStr, *STI);
Kevin Enderbybf246f52014-09-24 23:08:22 +00006771 emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo);
Owen Andersond9243c42011-10-17 21:37:35 +00006772
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006773 // Print debug info.
6774 if (diContext) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006775 DILineInfo dli = diContext->getLineInfoForAddress(PC);
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006776 // Print valid line info if it changed.
Alexey Samsonovd0109992014-04-18 21:36:39 +00006777 if (dli != lastLine && dli.Line != 0)
6778 outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
6779 << dli.Column;
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006780 lastLine = dli;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006781 }
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006782 outs() << "\n";
6783 } else {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006784 unsigned int Arch = MachOOF->getArch();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006785 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006786 outs() << format("\t.byte 0x%02x #bad opcode\n",
6787 *(Bytes.data() + Index) & 0xff);
6788 Size = 1; // skip exactly one illegible byte and move on.
Tim Northover09ca33e2016-04-22 23:23:31 +00006789 } else if (Arch == Triple::aarch64 ||
6790 (Arch == Triple::arm && !IsThumb)) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006791 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
6792 (*(Bytes.data() + Index + 1) & 0xff) << 8 |
6793 (*(Bytes.data() + Index + 2) & 0xff) << 16 |
6794 (*(Bytes.data() + Index + 3) & 0xff) << 24;
6795 outs() << format("\t.long\t0x%08x\n", opcode);
6796 Size = 4;
Tim Northover09ca33e2016-04-22 23:23:31 +00006797 } else if (Arch == Triple::arm) {
6798 assert(IsThumb && "ARM mode should have been dealt with above");
6799 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
6800 (*(Bytes.data() + Index + 1) & 0xff) << 8;
6801 outs() << format("\t.short\t0x%04x\n", opcode);
6802 Size = 2;
6803 } else{
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006804 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6805 if (Size == 0)
6806 Size = 1; // skip illegible bytes
6807 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006808 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006809 }
6810 }
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006811 if (!symbolTableWorked) {
Rafael Espindola80291272014-10-08 15:28:58 +00006812 // Reading the symbol table didn't work, disassemble the whole section.
6813 uint64_t SectAddress = Sections[SectIdx].getAddress();
6814 uint64_t SectSize = Sections[SectIdx].getSize();
Kevin Enderbybadd1002012-05-18 00:13:56 +00006815 uint64_t InstSize;
6816 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
Bill Wendling4e68e062012-07-19 00:17:40 +00006817 MCInst Inst;
Kevin Enderbybadd1002012-05-18 00:13:56 +00006818
Kevin Enderbybf246f52014-09-24 23:08:22 +00006819 uint64_t PC = SectAddress + Index;
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006820 if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC,
6821 DebugOut, nulls())) {
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00006822 if (!NoLeadingAddr) {
6823 if (FullLeadingAddr) {
6824 if (MachOOF->is64Bit())
6825 outs() << format("%016" PRIx64, PC);
6826 else
6827 outs() << format("%08" PRIx64, PC);
6828 } else {
6829 outs() << format("%8" PRIx64 ":", PC);
6830 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006831 }
Kevin Enderby4b627be2016-04-28 20:14:13 +00006832 if (!NoShowRawInsn || Arch == Triple::arm) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00006833 outs() << "\t";
Craig Topper0013be12015-09-21 05:32:41 +00006834 dumpBytes(makeArrayRef(Bytes.data() + Index, InstSize), outs());
Kevin Enderbybf246f52014-09-24 23:08:22 +00006835 }
Akira Hatanaka1d079942015-03-28 20:44:05 +00006836 IP->printInst(&Inst, outs(), "", *STI);
Bill Wendling4e68e062012-07-19 00:17:40 +00006837 outs() << "\n";
6838 } else {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006839 unsigned int Arch = MachOOF->getArch();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006840 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006841 outs() << format("\t.byte 0x%02x #bad opcode\n",
6842 *(Bytes.data() + Index) & 0xff);
6843 InstSize = 1; // skip exactly one illegible byte and move on.
6844 } else {
6845 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6846 if (InstSize == 0)
6847 InstSize = 1; // skip illegible bytes
6848 }
Bill Wendling4e68e062012-07-19 00:17:40 +00006849 }
Kevin Enderbybadd1002012-05-18 00:13:56 +00006850 }
6851 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00006852 // The TripleName's need to be reset if we are called again for a different
6853 // archtecture.
6854 TripleName = "";
6855 ThumbTripleName = "";
6856
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006857 if (SymbolizerInfo.method != nullptr)
6858 free(SymbolizerInfo.method);
Kevin Enderby04bf6932014-10-28 23:39:46 +00006859 if (SymbolizerInfo.demangled_name != nullptr)
6860 free(SymbolizerInfo.demangled_name);
Kevin Enderby078be602014-10-23 19:53:12 +00006861 if (SymbolizerInfo.bindtable != nullptr)
6862 delete SymbolizerInfo.bindtable;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006863 if (ThumbSymbolizerInfo.method != nullptr)
6864 free(ThumbSymbolizerInfo.method);
6865 if (ThumbSymbolizerInfo.demangled_name != nullptr)
6866 free(ThumbSymbolizerInfo.demangled_name);
6867 if (ThumbSymbolizerInfo.bindtable != nullptr)
6868 delete ThumbSymbolizerInfo.bindtable;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006869 }
6870}
Tim Northover4bd286a2014-08-01 13:07:19 +00006871
Tim Northover39c70bb2014-08-12 11:52:59 +00006872//===----------------------------------------------------------------------===//
6873// __compact_unwind section dumping
6874//===----------------------------------------------------------------------===//
6875
Tim Northover4bd286a2014-08-01 13:07:19 +00006876namespace {
Tim Northover39c70bb2014-08-12 11:52:59 +00006877
6878template <typename T> static uint64_t readNext(const char *&Buf) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006879 using llvm::support::little;
6880 using llvm::support::unaligned;
Tim Northover39c70bb2014-08-12 11:52:59 +00006881
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006882 uint64_t Val = support::endian::read<T, little, unaligned>(Buf);
6883 Buf += sizeof(T);
6884 return Val;
6885}
Tim Northover39c70bb2014-08-12 11:52:59 +00006886
Tim Northover4bd286a2014-08-01 13:07:19 +00006887struct CompactUnwindEntry {
6888 uint32_t OffsetInSection;
6889
6890 uint64_t FunctionAddr;
6891 uint32_t Length;
6892 uint32_t CompactEncoding;
6893 uint64_t PersonalityAddr;
6894 uint64_t LSDAAddr;
6895
6896 RelocationRef FunctionReloc;
6897 RelocationRef PersonalityReloc;
6898 RelocationRef LSDAReloc;
6899
6900 CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64)
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006901 : OffsetInSection(Offset) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006902 if (Is64)
6903 read<uint64_t>(Contents.data() + Offset);
6904 else
6905 read<uint32_t>(Contents.data() + Offset);
6906 }
6907
6908private:
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006909 template <typename UIntPtr> void read(const char *Buf) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006910 FunctionAddr = readNext<UIntPtr>(Buf);
6911 Length = readNext<uint32_t>(Buf);
6912 CompactEncoding = readNext<uint32_t>(Buf);
6913 PersonalityAddr = readNext<UIntPtr>(Buf);
6914 LSDAAddr = readNext<UIntPtr>(Buf);
6915 }
6916};
6917}
6918
6919/// Given a relocation from __compact_unwind, consisting of the RelocationRef
6920/// and data being relocated, determine the best base Name and Addend to use for
6921/// display purposes.
6922///
6923/// 1. An Extern relocation will directly reference a symbol (and the data is
6924/// then already an addend), so use that.
6925/// 2. Otherwise the data is an offset in the object file's layout; try to find
6926// a symbol before it in the same section, and use the offset from there.
6927/// 3. Finally, if all that fails, fall back to an offset from the start of the
6928/// referenced section.
6929static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
6930 std::map<uint64_t, SymbolRef> &Symbols,
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006931 const RelocationRef &Reloc, uint64_t Addr,
Tim Northover4bd286a2014-08-01 13:07:19 +00006932 StringRef &Name, uint64_t &Addend) {
6933 if (Reloc.getSymbol() != Obj->symbol_end()) {
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00006934 Expected<StringRef> NameOrErr = Reloc.getSymbol()->getName();
6935 if (!NameOrErr) {
6936 std::string Buf;
6937 raw_string_ostream OS(Buf);
6938 logAllUnhandledErrors(NameOrErr.takeError(), OS, "");
6939 OS.flush();
6940 report_fatal_error(Buf);
6941 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00006942 Name = *NameOrErr;
Tim Northover4bd286a2014-08-01 13:07:19 +00006943 Addend = Addr;
6944 return;
6945 }
6946
6947 auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
Keno Fischerc780e8e2015-05-21 21:24:32 +00006948 SectionRef RelocSection = Obj->getAnyRelocationSection(RE);
Tim Northover4bd286a2014-08-01 13:07:19 +00006949
Rafael Espindola80291272014-10-08 15:28:58 +00006950 uint64_t SectionAddr = RelocSection.getAddress();
Tim Northover4bd286a2014-08-01 13:07:19 +00006951
6952 auto Sym = Symbols.upper_bound(Addr);
6953 if (Sym == Symbols.begin()) {
6954 // The first symbol in the object is after this reference, the best we can
6955 // do is section-relative notation.
6956 RelocSection.getName(Name);
6957 Addend = Addr - SectionAddr;
6958 return;
6959 }
6960
6961 // Go back one so that SymbolAddress <= Addr.
6962 --Sym;
6963
Kevin Enderby7bd8d992016-05-02 20:28:12 +00006964 auto SectOrErr = Sym->second.getSection();
6965 if (!SectOrErr) {
6966 std::string Buf;
6967 raw_string_ostream OS(Buf);
6968 logAllUnhandledErrors(SectOrErr.takeError(), OS, "");
6969 OS.flush();
6970 report_fatal_error(Buf);
6971 }
6972 section_iterator SymSection = *SectOrErr;
Tim Northover4bd286a2014-08-01 13:07:19 +00006973 if (RelocSection == *SymSection) {
6974 // There's a valid symbol in the same section before this reference.
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00006975 Expected<StringRef> NameOrErr = Sym->second.getName();
6976 if (!NameOrErr) {
6977 std::string Buf;
6978 raw_string_ostream OS(Buf);
6979 logAllUnhandledErrors(NameOrErr.takeError(), OS, "");
6980 OS.flush();
6981 report_fatal_error(Buf);
6982 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00006983 Name = *NameOrErr;
Tim Northover4bd286a2014-08-01 13:07:19 +00006984 Addend = Addr - Sym->first;
6985 return;
6986 }
6987
6988 // There is a symbol before this reference, but it's in a different
6989 // section. Probably not helpful to mention it, so use the section name.
6990 RelocSection.getName(Name);
6991 Addend = Addr - SectionAddr;
6992}
6993
6994static void printUnwindRelocDest(const MachOObjectFile *Obj,
6995 std::map<uint64_t, SymbolRef> &Symbols,
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006996 const RelocationRef &Reloc, uint64_t Addr) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006997 StringRef Name;
6998 uint64_t Addend;
6999
Rafael Espindola854038e2015-06-26 14:51:16 +00007000 if (!Reloc.getObject())
Tim Northover0b0add52014-09-09 10:45:06 +00007001 return;
7002
Tim Northover4bd286a2014-08-01 13:07:19 +00007003 findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend);
7004
7005 outs() << Name;
7006 if (Addend)
Tim Northover63a25622014-08-11 09:14:06 +00007007 outs() << " + " << format("0x%" PRIx64, Addend);
Tim Northover4bd286a2014-08-01 13:07:19 +00007008}
7009
7010static void
7011printMachOCompactUnwindSection(const MachOObjectFile *Obj,
7012 std::map<uint64_t, SymbolRef> &Symbols,
7013 const SectionRef &CompactUnwind) {
7014
7015 assert(Obj->isLittleEndian() &&
7016 "There should not be a big-endian .o with __compact_unwind");
7017
7018 bool Is64 = Obj->is64Bit();
7019 uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
7020 uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
7021
7022 StringRef Contents;
7023 CompactUnwind.getContents(Contents);
7024
7025 SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
7026
7027 // First populate the initial raw offsets, encodings and so on from the entry.
7028 for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) {
7029 CompactUnwindEntry Entry(Contents.data(), Offset, Is64);
7030 CompactUnwinds.push_back(Entry);
7031 }
7032
7033 // Next we need to look at the relocations to find out what objects are
7034 // actually being referred to.
7035 for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00007036 uint64_t RelocAddress = Reloc.getOffset();
Tim Northover4bd286a2014-08-01 13:07:19 +00007037
7038 uint32_t EntryIdx = RelocAddress / EntrySize;
7039 uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
7040 CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx];
7041
7042 if (OffsetInEntry == 0)
7043 Entry.FunctionReloc = Reloc;
7044 else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t))
7045 Entry.PersonalityReloc = Reloc;
7046 else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t))
7047 Entry.LSDAReloc = Reloc;
7048 else
7049 llvm_unreachable("Unexpected relocation in __compact_unwind section");
7050 }
7051
7052 // Finally, we're ready to print the data we've gathered.
7053 outs() << "Contents of __compact_unwind section:\n";
7054 for (auto &Entry : CompactUnwinds) {
Tim Northover06af2602014-08-08 12:08:51 +00007055 outs() << " Entry at offset "
7056 << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n";
Tim Northover4bd286a2014-08-01 13:07:19 +00007057
7058 // 1. Start of the region this entry applies to.
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007059 outs() << " start: " << format("0x%" PRIx64,
7060 Entry.FunctionAddr) << ' ';
7061 printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr);
Tim Northover4bd286a2014-08-01 13:07:19 +00007062 outs() << '\n';
7063
7064 // 2. Length of the region this entry applies to.
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007065 outs() << " length: " << format("0x%" PRIx32, Entry.Length)
7066 << '\n';
Tim Northover4bd286a2014-08-01 13:07:19 +00007067 // 3. The 32-bit compact encoding.
7068 outs() << " compact encoding: "
Tim Northoverb911bf82014-08-08 12:00:09 +00007069 << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n';
Tim Northover4bd286a2014-08-01 13:07:19 +00007070
7071 // 4. The personality function, if present.
Rafael Espindola854038e2015-06-26 14:51:16 +00007072 if (Entry.PersonalityReloc.getObject()) {
Tim Northover4bd286a2014-08-01 13:07:19 +00007073 outs() << " personality function: "
Tim Northoverb911bf82014-08-08 12:00:09 +00007074 << format("0x%" PRIx64, Entry.PersonalityAddr) << ' ';
Tim Northover4bd286a2014-08-01 13:07:19 +00007075 printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc,
7076 Entry.PersonalityAddr);
7077 outs() << '\n';
7078 }
7079
7080 // 5. This entry's language-specific data area.
Rafael Espindola854038e2015-06-26 14:51:16 +00007081 if (Entry.LSDAReloc.getObject()) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007082 outs() << " LSDA: " << format("0x%" PRIx64,
7083 Entry.LSDAAddr) << ' ';
Tim Northover4bd286a2014-08-01 13:07:19 +00007084 printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr);
7085 outs() << '\n';
7086 }
7087 }
7088}
7089
Tim Northover39c70bb2014-08-12 11:52:59 +00007090//===----------------------------------------------------------------------===//
7091// __unwind_info section dumping
7092//===----------------------------------------------------------------------===//
7093
7094static void printRegularSecondLevelUnwindPage(const char *PageStart) {
7095 const char *Pos = PageStart;
7096 uint32_t Kind = readNext<uint32_t>(Pos);
7097 (void)Kind;
7098 assert(Kind == 2 && "kind for a regular 2nd level index should be 2");
7099
7100 uint16_t EntriesStart = readNext<uint16_t>(Pos);
7101 uint16_t NumEntries = readNext<uint16_t>(Pos);
7102
7103 Pos = PageStart + EntriesStart;
7104 for (unsigned i = 0; i < NumEntries; ++i) {
7105 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
7106 uint32_t Encoding = readNext<uint32_t>(Pos);
7107
7108 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007109 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
7110 << ", "
7111 << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00007112 }
7113}
7114
7115static void printCompressedSecondLevelUnwindPage(
7116 const char *PageStart, uint32_t FunctionBase,
7117 const SmallVectorImpl<uint32_t> &CommonEncodings) {
7118 const char *Pos = PageStart;
7119 uint32_t Kind = readNext<uint32_t>(Pos);
7120 (void)Kind;
7121 assert(Kind == 3 && "kind for a compressed 2nd level index should be 3");
7122
7123 uint16_t EntriesStart = readNext<uint16_t>(Pos);
7124 uint16_t NumEntries = readNext<uint16_t>(Pos);
7125
7126 uint16_t EncodingsStart = readNext<uint16_t>(Pos);
7127 readNext<uint16_t>(Pos);
Aaron Ballman80930af2014-08-14 13:53:19 +00007128 const auto *PageEncodings = reinterpret_cast<const support::ulittle32_t *>(
7129 PageStart + EncodingsStart);
Tim Northover39c70bb2014-08-12 11:52:59 +00007130
7131 Pos = PageStart + EntriesStart;
7132 for (unsigned i = 0; i < NumEntries; ++i) {
7133 uint32_t Entry = readNext<uint32_t>(Pos);
7134 uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff);
7135 uint32_t EncodingIdx = Entry >> 24;
7136
7137 uint32_t Encoding;
7138 if (EncodingIdx < CommonEncodings.size())
7139 Encoding = CommonEncodings[EncodingIdx];
7140 else
7141 Encoding = PageEncodings[EncodingIdx - CommonEncodings.size()];
7142
7143 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007144 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
7145 << ", "
7146 << "encoding[" << EncodingIdx
7147 << "]=" << format("0x%08" PRIx32, Encoding) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00007148 }
7149}
7150
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007151static void printMachOUnwindInfoSection(const MachOObjectFile *Obj,
7152 std::map<uint64_t, SymbolRef> &Symbols,
7153 const SectionRef &UnwindInfo) {
Tim Northover39c70bb2014-08-12 11:52:59 +00007154
7155 assert(Obj->isLittleEndian() &&
7156 "There should not be a big-endian .o with __unwind_info");
7157
7158 outs() << "Contents of __unwind_info section:\n";
7159
7160 StringRef Contents;
7161 UnwindInfo.getContents(Contents);
7162 const char *Pos = Contents.data();
7163
7164 //===----------------------------------
7165 // Section header
7166 //===----------------------------------
7167
7168 uint32_t Version = readNext<uint32_t>(Pos);
7169 outs() << " Version: "
7170 << format("0x%" PRIx32, Version) << '\n';
7171 assert(Version == 1 && "only understand version 1");
7172
7173 uint32_t CommonEncodingsStart = readNext<uint32_t>(Pos);
7174 outs() << " Common encodings array section offset: "
7175 << format("0x%" PRIx32, CommonEncodingsStart) << '\n';
7176 uint32_t NumCommonEncodings = readNext<uint32_t>(Pos);
7177 outs() << " Number of common encodings in array: "
7178 << format("0x%" PRIx32, NumCommonEncodings) << '\n';
7179
7180 uint32_t PersonalitiesStart = readNext<uint32_t>(Pos);
7181 outs() << " Personality function array section offset: "
7182 << format("0x%" PRIx32, PersonalitiesStart) << '\n';
7183 uint32_t NumPersonalities = readNext<uint32_t>(Pos);
7184 outs() << " Number of personality functions in array: "
7185 << format("0x%" PRIx32, NumPersonalities) << '\n';
7186
7187 uint32_t IndicesStart = readNext<uint32_t>(Pos);
7188 outs() << " Index array section offset: "
7189 << format("0x%" PRIx32, IndicesStart) << '\n';
7190 uint32_t NumIndices = readNext<uint32_t>(Pos);
7191 outs() << " Number of indices in array: "
7192 << format("0x%" PRIx32, NumIndices) << '\n';
7193
7194 //===----------------------------------
7195 // A shared list of common encodings
7196 //===----------------------------------
7197
7198 // These occupy indices in the range [0, N] whenever an encoding is referenced
7199 // from a compressed 2nd level index table. In practice the linker only
7200 // creates ~128 of these, so that indices are available to embed encodings in
7201 // the 2nd level index.
7202
7203 SmallVector<uint32_t, 64> CommonEncodings;
7204 outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n";
7205 Pos = Contents.data() + CommonEncodingsStart;
7206 for (unsigned i = 0; i < NumCommonEncodings; ++i) {
7207 uint32_t Encoding = readNext<uint32_t>(Pos);
7208 CommonEncodings.push_back(Encoding);
7209
7210 outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding)
7211 << '\n';
7212 }
7213
Tim Northover39c70bb2014-08-12 11:52:59 +00007214 //===----------------------------------
7215 // Personality functions used in this executable
7216 //===----------------------------------
7217
7218 // There should be only a handful of these (one per source language,
7219 // roughly). Particularly since they only get 2 bits in the compact encoding.
7220
7221 outs() << " Personality functions: (count = " << NumPersonalities << ")\n";
7222 Pos = Contents.data() + PersonalitiesStart;
7223 for (unsigned i = 0; i < NumPersonalities; ++i) {
7224 uint32_t PersonalityFn = readNext<uint32_t>(Pos);
7225 outs() << " personality[" << i + 1
7226 << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n';
7227 }
7228
7229 //===----------------------------------
7230 // The level 1 index entries
7231 //===----------------------------------
7232
7233 // These specify an approximate place to start searching for the more detailed
7234 // information, sorted by PC.
7235
7236 struct IndexEntry {
7237 uint32_t FunctionOffset;
7238 uint32_t SecondLevelPageStart;
7239 uint32_t LSDAStart;
7240 };
7241
7242 SmallVector<IndexEntry, 4> IndexEntries;
7243
7244 outs() << " Top level indices: (count = " << NumIndices << ")\n";
7245 Pos = Contents.data() + IndicesStart;
7246 for (unsigned i = 0; i < NumIndices; ++i) {
7247 IndexEntry Entry;
7248
7249 Entry.FunctionOffset = readNext<uint32_t>(Pos);
7250 Entry.SecondLevelPageStart = readNext<uint32_t>(Pos);
7251 Entry.LSDAStart = readNext<uint32_t>(Pos);
7252 IndexEntries.push_back(Entry);
7253
7254 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007255 << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset)
7256 << ", "
Tim Northover39c70bb2014-08-12 11:52:59 +00007257 << "2nd level page offset="
7258 << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007259 << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00007260 }
7261
Tim Northover39c70bb2014-08-12 11:52:59 +00007262 //===----------------------------------
7263 // Next come the LSDA tables
7264 //===----------------------------------
7265
7266 // The LSDA layout is rather implicit: it's a contiguous array of entries from
7267 // the first top-level index's LSDAOffset to the last (sentinel).
7268
7269 outs() << " LSDA descriptors:\n";
7270 Pos = Contents.data() + IndexEntries[0].LSDAStart;
7271 int NumLSDAs = (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) /
7272 (2 * sizeof(uint32_t));
7273 for (int i = 0; i < NumLSDAs; ++i) {
7274 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
7275 uint32_t LSDAOffset = readNext<uint32_t>(Pos);
7276 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007277 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
7278 << ", "
7279 << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00007280 }
7281
7282 //===----------------------------------
7283 // Finally, the 2nd level indices
7284 //===----------------------------------
7285
7286 // Generally these are 4K in size, and have 2 possible forms:
7287 // + Regular stores up to 511 entries with disparate encodings
7288 // + Compressed stores up to 1021 entries if few enough compact encoding
7289 // values are used.
7290 outs() << " Second level indices:\n";
7291 for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) {
7292 // The final sentinel top-level index has no associated 2nd level page
7293 if (IndexEntries[i].SecondLevelPageStart == 0)
7294 break;
7295
7296 outs() << " Second level index[" << i << "]: "
7297 << "offset in section="
7298 << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart)
7299 << ", "
7300 << "base function offset="
7301 << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n';
7302
7303 Pos = Contents.data() + IndexEntries[i].SecondLevelPageStart;
Aaron Ballman80930af2014-08-14 13:53:19 +00007304 uint32_t Kind = *reinterpret_cast<const support::ulittle32_t *>(Pos);
Tim Northover39c70bb2014-08-12 11:52:59 +00007305 if (Kind == 2)
7306 printRegularSecondLevelUnwindPage(Pos);
7307 else if (Kind == 3)
7308 printCompressedSecondLevelUnwindPage(Pos, IndexEntries[i].FunctionOffset,
7309 CommonEncodings);
7310 else
7311 llvm_unreachable("Do not know how to print this kind of 2nd level page");
Tim Northover39c70bb2014-08-12 11:52:59 +00007312 }
7313}
7314
Tim Northover4bd286a2014-08-01 13:07:19 +00007315void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) {
7316 std::map<uint64_t, SymbolRef> Symbols;
7317 for (const SymbolRef &SymRef : Obj->symbols()) {
7318 // Discard any undefined or absolute symbols. They're not going to take part
7319 // in the convenience lookup for unwind info and just take up resources.
Kevin Enderby7bd8d992016-05-02 20:28:12 +00007320 auto SectOrErr = SymRef.getSection();
7321 if (!SectOrErr) {
7322 // TODO: Actually report errors helpfully.
7323 consumeError(SectOrErr.takeError());
7324 continue;
7325 }
7326 section_iterator Section = *SectOrErr;
Tim Northover4bd286a2014-08-01 13:07:19 +00007327 if (Section == Obj->section_end())
7328 continue;
7329
Rafael Espindoladea00162015-07-03 17:44:18 +00007330 uint64_t Addr = SymRef.getValue();
Tim Northover4bd286a2014-08-01 13:07:19 +00007331 Symbols.insert(std::make_pair(Addr, SymRef));
7332 }
7333
7334 for (const SectionRef &Section : Obj->sections()) {
7335 StringRef SectName;
7336 Section.getName(SectName);
7337 if (SectName == "__compact_unwind")
7338 printMachOCompactUnwindSection(Obj, Symbols, Section);
7339 else if (SectName == "__unwind_info")
Tim Northover39c70bb2014-08-12 11:52:59 +00007340 printMachOUnwindInfoSection(Obj, Symbols, Section);
Tim Northover4bd286a2014-08-01 13:07:19 +00007341 }
7342}
Kevin Enderbyb76d3862014-08-22 20:35:18 +00007343
7344static void PrintMachHeader(uint32_t magic, uint32_t cputype,
7345 uint32_t cpusubtype, uint32_t filetype,
7346 uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
7347 bool verbose) {
7348 outs() << "Mach header\n";
7349 outs() << " magic cputype cpusubtype caps filetype ncmds "
7350 "sizeofcmds flags\n";
7351 if (verbose) {
7352 if (magic == MachO::MH_MAGIC)
7353 outs() << " MH_MAGIC";
7354 else if (magic == MachO::MH_MAGIC_64)
7355 outs() << "MH_MAGIC_64";
7356 else
7357 outs() << format(" 0x%08" PRIx32, magic);
7358 switch (cputype) {
7359 case MachO::CPU_TYPE_I386:
7360 outs() << " I386";
7361 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7362 case MachO::CPU_SUBTYPE_I386_ALL:
7363 outs() << " ALL";
7364 break;
7365 default:
7366 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7367 break;
7368 }
7369 break;
7370 case MachO::CPU_TYPE_X86_64:
7371 outs() << " X86_64";
Kevin Enderby131d1772015-01-09 19:22:37 +00007372 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7373 case MachO::CPU_SUBTYPE_X86_64_ALL:
7374 outs() << " ALL";
7375 break;
7376 case MachO::CPU_SUBTYPE_X86_64_H:
7377 outs() << " Haswell";
7378 break;
7379 default:
7380 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7381 break;
7382 }
Kevin Enderbyb76d3862014-08-22 20:35:18 +00007383 break;
7384 case MachO::CPU_TYPE_ARM:
7385 outs() << " ARM";
7386 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7387 case MachO::CPU_SUBTYPE_ARM_ALL:
7388 outs() << " ALL";
7389 break;
7390 case MachO::CPU_SUBTYPE_ARM_V4T:
7391 outs() << " V4T";
7392 break;
7393 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
7394 outs() << " V5TEJ";
7395 break;
7396 case MachO::CPU_SUBTYPE_ARM_XSCALE:
7397 outs() << " XSCALE";
7398 break;
7399 case MachO::CPU_SUBTYPE_ARM_V6:
7400 outs() << " V6";
7401 break;
7402 case MachO::CPU_SUBTYPE_ARM_V6M:
7403 outs() << " V6M";
7404 break;
7405 case MachO::CPU_SUBTYPE_ARM_V7:
7406 outs() << " V7";
7407 break;
7408 case MachO::CPU_SUBTYPE_ARM_V7EM:
7409 outs() << " V7EM";
7410 break;
7411 case MachO::CPU_SUBTYPE_ARM_V7K:
7412 outs() << " V7K";
7413 break;
7414 case MachO::CPU_SUBTYPE_ARM_V7M:
7415 outs() << " V7M";
7416 break;
7417 case MachO::CPU_SUBTYPE_ARM_V7S:
7418 outs() << " V7S";
7419 break;
7420 default:
7421 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7422 break;
7423 }
7424 break;
7425 case MachO::CPU_TYPE_ARM64:
7426 outs() << " ARM64";
7427 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7428 case MachO::CPU_SUBTYPE_ARM64_ALL:
7429 outs() << " ALL";
7430 break;
7431 default:
7432 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7433 break;
7434 }
7435 break;
7436 case MachO::CPU_TYPE_POWERPC:
7437 outs() << " PPC";
7438 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7439 case MachO::CPU_SUBTYPE_POWERPC_ALL:
7440 outs() << " ALL";
7441 break;
7442 default:
7443 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7444 break;
7445 }
7446 break;
7447 case MachO::CPU_TYPE_POWERPC64:
7448 outs() << " PPC64";
7449 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7450 case MachO::CPU_SUBTYPE_POWERPC_ALL:
7451 outs() << " ALL";
7452 break;
7453 default:
7454 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7455 break;
7456 }
7457 break;
Kevin Enderby40fdbf82016-01-26 18:20:49 +00007458 default:
7459 outs() << format(" %7d", cputype);
7460 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7461 break;
Kevin Enderbyb76d3862014-08-22 20:35:18 +00007462 }
7463 if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007464 outs() << " LIB64";
Kevin Enderbyb76d3862014-08-22 20:35:18 +00007465 } else {
7466 outs() << format(" 0x%02" PRIx32,
7467 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
7468 }
7469 switch (filetype) {
7470 case MachO::MH_OBJECT:
7471 outs() << " OBJECT";
7472 break;
7473 case MachO::MH_EXECUTE:
7474 outs() << " EXECUTE";
7475 break;
7476 case MachO::MH_FVMLIB:
7477 outs() << " FVMLIB";
7478 break;
7479 case MachO::MH_CORE:
7480 outs() << " CORE";
7481 break;
7482 case MachO::MH_PRELOAD:
7483 outs() << " PRELOAD";
7484 break;
7485 case MachO::MH_DYLIB:
7486 outs() << " DYLIB";
7487 break;
7488 case MachO::MH_DYLIB_STUB:
7489 outs() << " DYLIB_STUB";
7490 break;
7491 case MachO::MH_DYLINKER:
7492 outs() << " DYLINKER";
7493 break;
7494 case MachO::MH_BUNDLE:
7495 outs() << " BUNDLE";
7496 break;
7497 case MachO::MH_DSYM:
7498 outs() << " DSYM";
7499 break;
7500 case MachO::MH_KEXT_BUNDLE:
7501 outs() << " KEXTBUNDLE";
7502 break;
7503 default:
7504 outs() << format(" %10u", filetype);
7505 break;
7506 }
7507 outs() << format(" %5u", ncmds);
7508 outs() << format(" %10u", sizeofcmds);
7509 uint32_t f = flags;
7510 if (f & MachO::MH_NOUNDEFS) {
7511 outs() << " NOUNDEFS";
7512 f &= ~MachO::MH_NOUNDEFS;
7513 }
7514 if (f & MachO::MH_INCRLINK) {
7515 outs() << " INCRLINK";
7516 f &= ~MachO::MH_INCRLINK;
7517 }
7518 if (f & MachO::MH_DYLDLINK) {
7519 outs() << " DYLDLINK";
7520 f &= ~MachO::MH_DYLDLINK;
7521 }
7522 if (f & MachO::MH_BINDATLOAD) {
7523 outs() << " BINDATLOAD";
7524 f &= ~MachO::MH_BINDATLOAD;
7525 }
7526 if (f & MachO::MH_PREBOUND) {
7527 outs() << " PREBOUND";
7528 f &= ~MachO::MH_PREBOUND;
7529 }
7530 if (f & MachO::MH_SPLIT_SEGS) {
7531 outs() << " SPLIT_SEGS";
7532 f &= ~MachO::MH_SPLIT_SEGS;
7533 }
7534 if (f & MachO::MH_LAZY_INIT) {
7535 outs() << " LAZY_INIT";
7536 f &= ~MachO::MH_LAZY_INIT;
7537 }
7538 if (f & MachO::MH_TWOLEVEL) {
7539 outs() << " TWOLEVEL";
7540 f &= ~MachO::MH_TWOLEVEL;
7541 }
7542 if (f & MachO::MH_FORCE_FLAT) {
7543 outs() << " FORCE_FLAT";
7544 f &= ~MachO::MH_FORCE_FLAT;
7545 }
7546 if (f & MachO::MH_NOMULTIDEFS) {
7547 outs() << " NOMULTIDEFS";
7548 f &= ~MachO::MH_NOMULTIDEFS;
7549 }
7550 if (f & MachO::MH_NOFIXPREBINDING) {
7551 outs() << " NOFIXPREBINDING";
7552 f &= ~MachO::MH_NOFIXPREBINDING;
7553 }
7554 if (f & MachO::MH_PREBINDABLE) {
7555 outs() << " PREBINDABLE";
7556 f &= ~MachO::MH_PREBINDABLE;
7557 }
7558 if (f & MachO::MH_ALLMODSBOUND) {
7559 outs() << " ALLMODSBOUND";
7560 f &= ~MachO::MH_ALLMODSBOUND;
7561 }
7562 if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
7563 outs() << " SUBSECTIONS_VIA_SYMBOLS";
7564 f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
7565 }
7566 if (f & MachO::MH_CANONICAL) {
7567 outs() << " CANONICAL";
7568 f &= ~MachO::MH_CANONICAL;
7569 }
7570 if (f & MachO::MH_WEAK_DEFINES) {
7571 outs() << " WEAK_DEFINES";
7572 f &= ~MachO::MH_WEAK_DEFINES;
7573 }
7574 if (f & MachO::MH_BINDS_TO_WEAK) {
7575 outs() << " BINDS_TO_WEAK";
7576 f &= ~MachO::MH_BINDS_TO_WEAK;
7577 }
7578 if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
7579 outs() << " ALLOW_STACK_EXECUTION";
7580 f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
7581 }
7582 if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
7583 outs() << " DEAD_STRIPPABLE_DYLIB";
7584 f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
7585 }
7586 if (f & MachO::MH_PIE) {
7587 outs() << " PIE";
7588 f &= ~MachO::MH_PIE;
7589 }
7590 if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
7591 outs() << " NO_REEXPORTED_DYLIBS";
7592 f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
7593 }
7594 if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
7595 outs() << " MH_HAS_TLV_DESCRIPTORS";
7596 f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
7597 }
7598 if (f & MachO::MH_NO_HEAP_EXECUTION) {
7599 outs() << " MH_NO_HEAP_EXECUTION";
7600 f &= ~MachO::MH_NO_HEAP_EXECUTION;
7601 }
7602 if (f & MachO::MH_APP_EXTENSION_SAFE) {
7603 outs() << " APP_EXTENSION_SAFE";
7604 f &= ~MachO::MH_APP_EXTENSION_SAFE;
7605 }
7606 if (f != 0 || flags == 0)
7607 outs() << format(" 0x%08" PRIx32, f);
7608 } else {
7609 outs() << format(" 0x%08" PRIx32, magic);
7610 outs() << format(" %7d", cputype);
7611 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7612 outs() << format(" 0x%02" PRIx32,
7613 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
7614 outs() << format(" %10u", filetype);
7615 outs() << format(" %5u", ncmds);
7616 outs() << format(" %10u", sizeofcmds);
7617 outs() << format(" 0x%08" PRIx32, flags);
7618 }
7619 outs() << "\n";
7620}
7621
Kevin Enderby956366c2014-08-29 22:30:52 +00007622static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
7623 StringRef SegName, uint64_t vmaddr,
7624 uint64_t vmsize, uint64_t fileoff,
7625 uint64_t filesize, uint32_t maxprot,
7626 uint32_t initprot, uint32_t nsects,
7627 uint32_t flags, uint32_t object_size,
7628 bool verbose) {
7629 uint64_t expected_cmdsize;
7630 if (cmd == MachO::LC_SEGMENT) {
7631 outs() << " cmd LC_SEGMENT\n";
7632 expected_cmdsize = nsects;
7633 expected_cmdsize *= sizeof(struct MachO::section);
7634 expected_cmdsize += sizeof(struct MachO::segment_command);
7635 } else {
7636 outs() << " cmd LC_SEGMENT_64\n";
7637 expected_cmdsize = nsects;
7638 expected_cmdsize *= sizeof(struct MachO::section_64);
7639 expected_cmdsize += sizeof(struct MachO::segment_command_64);
7640 }
7641 outs() << " cmdsize " << cmdsize;
7642 if (cmdsize != expected_cmdsize)
7643 outs() << " Inconsistent size\n";
7644 else
7645 outs() << "\n";
7646 outs() << " segname " << SegName << "\n";
7647 if (cmd == MachO::LC_SEGMENT_64) {
7648 outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n";
7649 outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n";
7650 } else {
Kevin Enderbyadb7c432014-12-16 18:58:11 +00007651 outs() << " vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n";
7652 outs() << " vmsize " << format("0x%08" PRIx64, vmsize) << "\n";
Kevin Enderby956366c2014-08-29 22:30:52 +00007653 }
7654 outs() << " fileoff " << fileoff;
7655 if (fileoff > object_size)
7656 outs() << " (past end of file)\n";
7657 else
7658 outs() << "\n";
7659 outs() << " filesize " << filesize;
7660 if (fileoff + filesize > object_size)
7661 outs() << " (past end of file)\n";
7662 else
7663 outs() << "\n";
7664 if (verbose) {
7665 if ((maxprot &
7666 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7667 MachO::VM_PROT_EXECUTE)) != 0)
7668 outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n";
7669 else {
Davide Italiano37ff06a2015-09-02 16:53:25 +00007670 outs() << " maxprot ";
7671 outs() << ((maxprot & MachO::VM_PROT_READ) ? "r" : "-");
7672 outs() << ((maxprot & MachO::VM_PROT_WRITE) ? "w" : "-");
7673 outs() << ((maxprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
Kevin Enderby956366c2014-08-29 22:30:52 +00007674 }
7675 if ((initprot &
7676 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7677 MachO::VM_PROT_EXECUTE)) != 0)
7678 outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n";
7679 else {
Davide Italiano37ff06a2015-09-02 16:53:25 +00007680 outs() << " initprot ";
7681 outs() << ((initprot & MachO::VM_PROT_READ) ? "r" : "-");
7682 outs() << ((initprot & MachO::VM_PROT_WRITE) ? "w" : "-");
7683 outs() << ((initprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
Kevin Enderby956366c2014-08-29 22:30:52 +00007684 }
7685 } else {
7686 outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n";
7687 outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n";
7688 }
7689 outs() << " nsects " << nsects << "\n";
7690 if (verbose) {
7691 outs() << " flags";
7692 if (flags == 0)
7693 outs() << " (none)\n";
7694 else {
7695 if (flags & MachO::SG_HIGHVM) {
7696 outs() << " HIGHVM";
7697 flags &= ~MachO::SG_HIGHVM;
7698 }
7699 if (flags & MachO::SG_FVMLIB) {
7700 outs() << " FVMLIB";
7701 flags &= ~MachO::SG_FVMLIB;
7702 }
7703 if (flags & MachO::SG_NORELOC) {
7704 outs() << " NORELOC";
7705 flags &= ~MachO::SG_NORELOC;
7706 }
7707 if (flags & MachO::SG_PROTECTED_VERSION_1) {
7708 outs() << " PROTECTED_VERSION_1";
7709 flags &= ~MachO::SG_PROTECTED_VERSION_1;
7710 }
7711 if (flags)
7712 outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n";
7713 else
7714 outs() << "\n";
7715 }
7716 } else {
7717 outs() << " flags " << format("0x%" PRIx32, flags) << "\n";
7718 }
7719}
7720
7721static void PrintSection(const char *sectname, const char *segname,
7722 uint64_t addr, uint64_t size, uint32_t offset,
7723 uint32_t align, uint32_t reloff, uint32_t nreloc,
7724 uint32_t flags, uint32_t reserved1, uint32_t reserved2,
7725 uint32_t cmd, const char *sg_segname,
7726 uint32_t filetype, uint32_t object_size,
7727 bool verbose) {
7728 outs() << "Section\n";
7729 outs() << " sectname " << format("%.16s\n", sectname);
7730 outs() << " segname " << format("%.16s", segname);
7731 if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0)
7732 outs() << " (does not match segment)\n";
7733 else
7734 outs() << "\n";
7735 if (cmd == MachO::LC_SEGMENT_64) {
7736 outs() << " addr " << format("0x%016" PRIx64, addr) << "\n";
7737 outs() << " size " << format("0x%016" PRIx64, size);
7738 } else {
Kevin Enderby75594b62014-12-16 21:00:25 +00007739 outs() << " addr " << format("0x%08" PRIx64, addr) << "\n";
7740 outs() << " size " << format("0x%08" PRIx64, size);
Kevin Enderby956366c2014-08-29 22:30:52 +00007741 }
7742 if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size)
7743 outs() << " (past end of file)\n";
7744 else
7745 outs() << "\n";
7746 outs() << " offset " << offset;
7747 if (offset > object_size)
7748 outs() << " (past end of file)\n";
7749 else
7750 outs() << "\n";
7751 uint32_t align_shifted = 1 << align;
7752 outs() << " align 2^" << align << " (" << align_shifted << ")\n";
7753 outs() << " reloff " << reloff;
7754 if (reloff > object_size)
7755 outs() << " (past end of file)\n";
7756 else
7757 outs() << "\n";
7758 outs() << " nreloc " << nreloc;
7759 if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size)
7760 outs() << " (past end of file)\n";
7761 else
7762 outs() << "\n";
7763 uint32_t section_type = flags & MachO::SECTION_TYPE;
7764 if (verbose) {
7765 outs() << " type";
7766 if (section_type == MachO::S_REGULAR)
7767 outs() << " S_REGULAR\n";
7768 else if (section_type == MachO::S_ZEROFILL)
7769 outs() << " S_ZEROFILL\n";
7770 else if (section_type == MachO::S_CSTRING_LITERALS)
7771 outs() << " S_CSTRING_LITERALS\n";
7772 else if (section_type == MachO::S_4BYTE_LITERALS)
7773 outs() << " S_4BYTE_LITERALS\n";
7774 else if (section_type == MachO::S_8BYTE_LITERALS)
7775 outs() << " S_8BYTE_LITERALS\n";
7776 else if (section_type == MachO::S_16BYTE_LITERALS)
7777 outs() << " S_16BYTE_LITERALS\n";
7778 else if (section_type == MachO::S_LITERAL_POINTERS)
7779 outs() << " S_LITERAL_POINTERS\n";
7780 else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS)
7781 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
7782 else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS)
7783 outs() << " S_LAZY_SYMBOL_POINTERS\n";
7784 else if (section_type == MachO::S_SYMBOL_STUBS)
7785 outs() << " S_SYMBOL_STUBS\n";
7786 else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS)
7787 outs() << " S_MOD_INIT_FUNC_POINTERS\n";
7788 else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS)
7789 outs() << " S_MOD_TERM_FUNC_POINTERS\n";
7790 else if (section_type == MachO::S_COALESCED)
7791 outs() << " S_COALESCED\n";
7792 else if (section_type == MachO::S_INTERPOSING)
7793 outs() << " S_INTERPOSING\n";
7794 else if (section_type == MachO::S_DTRACE_DOF)
7795 outs() << " S_DTRACE_DOF\n";
7796 else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS)
7797 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
7798 else if (section_type == MachO::S_THREAD_LOCAL_REGULAR)
7799 outs() << " S_THREAD_LOCAL_REGULAR\n";
7800 else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL)
7801 outs() << " S_THREAD_LOCAL_ZEROFILL\n";
7802 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES)
7803 outs() << " S_THREAD_LOCAL_VARIABLES\n";
7804 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7805 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
7806 else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS)
7807 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
7808 else
7809 outs() << format("0x%08" PRIx32, section_type) << "\n";
7810 outs() << "attributes";
7811 uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES;
7812 if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS)
7813 outs() << " PURE_INSTRUCTIONS";
7814 if (section_attributes & MachO::S_ATTR_NO_TOC)
7815 outs() << " NO_TOC";
7816 if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS)
7817 outs() << " STRIP_STATIC_SYMS";
7818 if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP)
7819 outs() << " NO_DEAD_STRIP";
7820 if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT)
7821 outs() << " LIVE_SUPPORT";
7822 if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE)
7823 outs() << " SELF_MODIFYING_CODE";
7824 if (section_attributes & MachO::S_ATTR_DEBUG)
7825 outs() << " DEBUG";
7826 if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS)
7827 outs() << " SOME_INSTRUCTIONS";
7828 if (section_attributes & MachO::S_ATTR_EXT_RELOC)
7829 outs() << " EXT_RELOC";
7830 if (section_attributes & MachO::S_ATTR_LOC_RELOC)
7831 outs() << " LOC_RELOC";
7832 if (section_attributes == 0)
7833 outs() << " (none)";
7834 outs() << "\n";
7835 } else
7836 outs() << " flags " << format("0x%08" PRIx32, flags) << "\n";
7837 outs() << " reserved1 " << reserved1;
7838 if (section_type == MachO::S_SYMBOL_STUBS ||
7839 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
7840 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
7841 section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
7842 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7843 outs() << " (index into indirect symbol table)\n";
7844 else
7845 outs() << "\n";
7846 outs() << " reserved2 " << reserved2;
7847 if (section_type == MachO::S_SYMBOL_STUBS)
7848 outs() << " (size of stubs)\n";
7849 else
7850 outs() << "\n";
7851}
7852
David Majnemer73cc6ff2014-11-13 19:48:56 +00007853static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit,
Kevin Enderby956366c2014-08-29 22:30:52 +00007854 uint32_t object_size) {
7855 outs() << " cmd LC_SYMTAB\n";
7856 outs() << " cmdsize " << st.cmdsize;
7857 if (st.cmdsize != sizeof(struct MachO::symtab_command))
7858 outs() << " Incorrect size\n";
7859 else
7860 outs() << "\n";
7861 outs() << " symoff " << st.symoff;
7862 if (st.symoff > object_size)
7863 outs() << " (past end of file)\n";
7864 else
7865 outs() << "\n";
7866 outs() << " nsyms " << st.nsyms;
7867 uint64_t big_size;
David Majnemer73cc6ff2014-11-13 19:48:56 +00007868 if (Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00007869 big_size = st.nsyms;
7870 big_size *= sizeof(struct MachO::nlist_64);
7871 big_size += st.symoff;
7872 if (big_size > object_size)
7873 outs() << " (past end of file)\n";
7874 else
7875 outs() << "\n";
7876 } else {
7877 big_size = st.nsyms;
7878 big_size *= sizeof(struct MachO::nlist);
7879 big_size += st.symoff;
7880 if (big_size > object_size)
7881 outs() << " (past end of file)\n";
7882 else
7883 outs() << "\n";
7884 }
7885 outs() << " stroff " << st.stroff;
7886 if (st.stroff > object_size)
7887 outs() << " (past end of file)\n";
7888 else
7889 outs() << "\n";
7890 outs() << " strsize " << st.strsize;
7891 big_size = st.stroff;
7892 big_size += st.strsize;
7893 if (big_size > object_size)
7894 outs() << " (past end of file)\n";
7895 else
7896 outs() << "\n";
7897}
7898
7899static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
7900 uint32_t nsyms, uint32_t object_size,
David Majnemer73cc6ff2014-11-13 19:48:56 +00007901 bool Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00007902 outs() << " cmd LC_DYSYMTAB\n";
7903 outs() << " cmdsize " << dyst.cmdsize;
7904 if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
7905 outs() << " Incorrect size\n";
7906 else
7907 outs() << "\n";
7908 outs() << " ilocalsym " << dyst.ilocalsym;
7909 if (dyst.ilocalsym > nsyms)
7910 outs() << " (greater than the number of symbols)\n";
7911 else
7912 outs() << "\n";
7913 outs() << " nlocalsym " << dyst.nlocalsym;
7914 uint64_t big_size;
7915 big_size = dyst.ilocalsym;
7916 big_size += dyst.nlocalsym;
7917 if (big_size > nsyms)
7918 outs() << " (past the end of the symbol table)\n";
7919 else
7920 outs() << "\n";
7921 outs() << " iextdefsym " << dyst.iextdefsym;
7922 if (dyst.iextdefsym > nsyms)
7923 outs() << " (greater than the number of symbols)\n";
7924 else
7925 outs() << "\n";
7926 outs() << " nextdefsym " << dyst.nextdefsym;
7927 big_size = dyst.iextdefsym;
7928 big_size += dyst.nextdefsym;
7929 if (big_size > nsyms)
7930 outs() << " (past the end of the symbol table)\n";
7931 else
7932 outs() << "\n";
7933 outs() << " iundefsym " << dyst.iundefsym;
7934 if (dyst.iundefsym > nsyms)
7935 outs() << " (greater than the number of symbols)\n";
7936 else
7937 outs() << "\n";
7938 outs() << " nundefsym " << dyst.nundefsym;
7939 big_size = dyst.iundefsym;
7940 big_size += dyst.nundefsym;
7941 if (big_size > nsyms)
7942 outs() << " (past the end of the symbol table)\n";
7943 else
7944 outs() << "\n";
7945 outs() << " tocoff " << dyst.tocoff;
7946 if (dyst.tocoff > object_size)
7947 outs() << " (past end of file)\n";
7948 else
7949 outs() << "\n";
7950 outs() << " ntoc " << dyst.ntoc;
7951 big_size = dyst.ntoc;
7952 big_size *= sizeof(struct MachO::dylib_table_of_contents);
7953 big_size += dyst.tocoff;
7954 if (big_size > object_size)
7955 outs() << " (past end of file)\n";
7956 else
7957 outs() << "\n";
7958 outs() << " modtaboff " << dyst.modtaboff;
7959 if (dyst.modtaboff > object_size)
7960 outs() << " (past end of file)\n";
7961 else
7962 outs() << "\n";
7963 outs() << " nmodtab " << dyst.nmodtab;
7964 uint64_t modtabend;
David Majnemer73cc6ff2014-11-13 19:48:56 +00007965 if (Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00007966 modtabend = dyst.nmodtab;
7967 modtabend *= sizeof(struct MachO::dylib_module_64);
7968 modtabend += dyst.modtaboff;
7969 } else {
7970 modtabend = dyst.nmodtab;
7971 modtabend *= sizeof(struct MachO::dylib_module);
7972 modtabend += dyst.modtaboff;
7973 }
7974 if (modtabend > object_size)
7975 outs() << " (past end of file)\n";
7976 else
7977 outs() << "\n";
7978 outs() << " extrefsymoff " << dyst.extrefsymoff;
7979 if (dyst.extrefsymoff > object_size)
7980 outs() << " (past end of file)\n";
7981 else
7982 outs() << "\n";
7983 outs() << " nextrefsyms " << dyst.nextrefsyms;
7984 big_size = dyst.nextrefsyms;
7985 big_size *= sizeof(struct MachO::dylib_reference);
7986 big_size += dyst.extrefsymoff;
7987 if (big_size > object_size)
7988 outs() << " (past end of file)\n";
7989 else
7990 outs() << "\n";
7991 outs() << " indirectsymoff " << dyst.indirectsymoff;
7992 if (dyst.indirectsymoff > object_size)
7993 outs() << " (past end of file)\n";
7994 else
7995 outs() << "\n";
7996 outs() << " nindirectsyms " << dyst.nindirectsyms;
7997 big_size = dyst.nindirectsyms;
7998 big_size *= sizeof(uint32_t);
7999 big_size += dyst.indirectsymoff;
8000 if (big_size > object_size)
8001 outs() << " (past end of file)\n";
8002 else
8003 outs() << "\n";
8004 outs() << " extreloff " << dyst.extreloff;
8005 if (dyst.extreloff > object_size)
8006 outs() << " (past end of file)\n";
8007 else
8008 outs() << "\n";
8009 outs() << " nextrel " << dyst.nextrel;
8010 big_size = dyst.nextrel;
8011 big_size *= sizeof(struct MachO::relocation_info);
8012 big_size += dyst.extreloff;
8013 if (big_size > object_size)
8014 outs() << " (past end of file)\n";
8015 else
8016 outs() << "\n";
8017 outs() << " locreloff " << dyst.locreloff;
8018 if (dyst.locreloff > object_size)
8019 outs() << " (past end of file)\n";
8020 else
8021 outs() << "\n";
8022 outs() << " nlocrel " << dyst.nlocrel;
8023 big_size = dyst.nlocrel;
8024 big_size *= sizeof(struct MachO::relocation_info);
8025 big_size += dyst.locreloff;
8026 if (big_size > object_size)
8027 outs() << " (past end of file)\n";
8028 else
8029 outs() << "\n";
8030}
8031
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008032static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc,
8033 uint32_t object_size) {
8034 if (dc.cmd == MachO::LC_DYLD_INFO)
8035 outs() << " cmd LC_DYLD_INFO\n";
8036 else
8037 outs() << " cmd LC_DYLD_INFO_ONLY\n";
8038 outs() << " cmdsize " << dc.cmdsize;
8039 if (dc.cmdsize != sizeof(struct MachO::dyld_info_command))
8040 outs() << " Incorrect size\n";
8041 else
8042 outs() << "\n";
8043 outs() << " rebase_off " << dc.rebase_off;
8044 if (dc.rebase_off > object_size)
8045 outs() << " (past end of file)\n";
8046 else
8047 outs() << "\n";
8048 outs() << " rebase_size " << dc.rebase_size;
8049 uint64_t big_size;
8050 big_size = dc.rebase_off;
8051 big_size += dc.rebase_size;
8052 if (big_size > object_size)
8053 outs() << " (past end of file)\n";
8054 else
8055 outs() << "\n";
8056 outs() << " bind_off " << dc.bind_off;
8057 if (dc.bind_off > object_size)
8058 outs() << " (past end of file)\n";
8059 else
8060 outs() << "\n";
8061 outs() << " bind_size " << dc.bind_size;
8062 big_size = dc.bind_off;
8063 big_size += dc.bind_size;
8064 if (big_size > object_size)
8065 outs() << " (past end of file)\n";
8066 else
8067 outs() << "\n";
8068 outs() << " weak_bind_off " << dc.weak_bind_off;
8069 if (dc.weak_bind_off > object_size)
8070 outs() << " (past end of file)\n";
8071 else
8072 outs() << "\n";
8073 outs() << " weak_bind_size " << dc.weak_bind_size;
8074 big_size = dc.weak_bind_off;
8075 big_size += dc.weak_bind_size;
8076 if (big_size > object_size)
8077 outs() << " (past end of file)\n";
8078 else
8079 outs() << "\n";
8080 outs() << " lazy_bind_off " << dc.lazy_bind_off;
8081 if (dc.lazy_bind_off > object_size)
8082 outs() << " (past end of file)\n";
8083 else
8084 outs() << "\n";
8085 outs() << " lazy_bind_size " << dc.lazy_bind_size;
8086 big_size = dc.lazy_bind_off;
8087 big_size += dc.lazy_bind_size;
8088 if (big_size > object_size)
8089 outs() << " (past end of file)\n";
8090 else
8091 outs() << "\n";
8092 outs() << " export_off " << dc.export_off;
8093 if (dc.export_off > object_size)
8094 outs() << " (past end of file)\n";
8095 else
8096 outs() << "\n";
8097 outs() << " export_size " << dc.export_size;
8098 big_size = dc.export_off;
8099 big_size += dc.export_size;
8100 if (big_size > object_size)
8101 outs() << " (past end of file)\n";
8102 else
8103 outs() << "\n";
8104}
8105
8106static void PrintDyldLoadCommand(MachO::dylinker_command dyld,
8107 const char *Ptr) {
8108 if (dyld.cmd == MachO::LC_ID_DYLINKER)
8109 outs() << " cmd LC_ID_DYLINKER\n";
8110 else if (dyld.cmd == MachO::LC_LOAD_DYLINKER)
8111 outs() << " cmd LC_LOAD_DYLINKER\n";
8112 else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT)
8113 outs() << " cmd LC_DYLD_ENVIRONMENT\n";
8114 else
8115 outs() << " cmd ?(" << dyld.cmd << ")\n";
8116 outs() << " cmdsize " << dyld.cmdsize;
8117 if (dyld.cmdsize < sizeof(struct MachO::dylinker_command))
8118 outs() << " Incorrect size\n";
8119 else
8120 outs() << "\n";
8121 if (dyld.name >= dyld.cmdsize)
8122 outs() << " name ?(bad offset " << dyld.name << ")\n";
8123 else {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008124 const char *P = (const char *)(Ptr) + dyld.name;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008125 outs() << " name " << P << " (offset " << dyld.name << ")\n";
8126 }
8127}
8128
8129static void PrintUuidLoadCommand(MachO::uuid_command uuid) {
8130 outs() << " cmd LC_UUID\n";
8131 outs() << " cmdsize " << uuid.cmdsize;
8132 if (uuid.cmdsize != sizeof(struct MachO::uuid_command))
8133 outs() << " Incorrect size\n";
8134 else
8135 outs() << "\n";
8136 outs() << " uuid ";
Davide Italianoc74277a2015-12-07 00:03:28 +00008137 for (int i = 0; i < 16; ++i) {
8138 outs() << format("%02" PRIX32, uuid.uuid[i]);
8139 if (i == 3 || i == 5 || i == 7 || i == 9)
8140 outs() << "-";
8141 }
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008142 outs() << "\n";
8143}
8144
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008145static void PrintRpathLoadCommand(MachO::rpath_command rpath, const char *Ptr) {
Jean-Daniel Dupas00cc1f52014-12-04 07:37:02 +00008146 outs() << " cmd LC_RPATH\n";
8147 outs() << " cmdsize " << rpath.cmdsize;
8148 if (rpath.cmdsize < sizeof(struct MachO::rpath_command))
8149 outs() << " Incorrect size\n";
8150 else
8151 outs() << "\n";
8152 if (rpath.path >= rpath.cmdsize)
8153 outs() << " path ?(bad offset " << rpath.path << ")\n";
8154 else {
8155 const char *P = (const char *)(Ptr) + rpath.path;
8156 outs() << " path " << P << " (offset " << rpath.path << ")\n";
8157 }
8158}
8159
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008160static void PrintVersionMinLoadCommand(MachO::version_min_command vd) {
Tim Northoverbfbfb122015-11-02 21:26:58 +00008161 StringRef LoadCmdName;
8162 switch (vd.cmd) {
8163 case MachO::LC_VERSION_MIN_MACOSX:
8164 LoadCmdName = "LC_VERSION_MIN_MACOSX";
8165 break;
8166 case MachO::LC_VERSION_MIN_IPHONEOS:
8167 LoadCmdName = "LC_VERSION_MIN_IPHONEOS";
8168 break;
8169 case MachO::LC_VERSION_MIN_TVOS:
8170 LoadCmdName = "LC_VERSION_MIN_TVOS";
8171 break;
8172 case MachO::LC_VERSION_MIN_WATCHOS:
8173 LoadCmdName = "LC_VERSION_MIN_WATCHOS";
8174 break;
8175 default:
8176 llvm_unreachable("Unknown version min load command");
8177 }
8178
8179 outs() << " cmd " << LoadCmdName << '\n';
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008180 outs() << " cmdsize " << vd.cmdsize;
8181 if (vd.cmdsize != sizeof(struct MachO::version_min_command))
8182 outs() << " Incorrect size\n";
8183 else
8184 outs() << "\n";
Davide Italiano56baef32015-08-26 12:26:11 +00008185 outs() << " version "
8186 << MachOObjectFile::getVersionMinMajor(vd, false) << "."
8187 << MachOObjectFile::getVersionMinMinor(vd, false);
8188 uint32_t Update = MachOObjectFile::getVersionMinUpdate(vd, false);
8189 if (Update != 0)
8190 outs() << "." << Update;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008191 outs() << "\n";
8192 if (vd.sdk == 0)
Kevin Enderby57538292014-12-17 01:01:30 +00008193 outs() << " sdk n/a";
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008194 else {
Davide Italiano56baef32015-08-26 12:26:11 +00008195 outs() << " sdk "
8196 << MachOObjectFile::getVersionMinMajor(vd, true) << "."
8197 << MachOObjectFile::getVersionMinMinor(vd, true);
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008198 }
Davide Italiano56baef32015-08-26 12:26:11 +00008199 Update = MachOObjectFile::getVersionMinUpdate(vd, true);
8200 if (Update != 0)
8201 outs() << "." << Update;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008202 outs() << "\n";
8203}
8204
8205static void PrintSourceVersionCommand(MachO::source_version_command sd) {
8206 outs() << " cmd LC_SOURCE_VERSION\n";
8207 outs() << " cmdsize " << sd.cmdsize;
8208 if (sd.cmdsize != sizeof(struct MachO::source_version_command))
8209 outs() << " Incorrect size\n";
8210 else
8211 outs() << "\n";
8212 uint64_t a = (sd.version >> 40) & 0xffffff;
8213 uint64_t b = (sd.version >> 30) & 0x3ff;
8214 uint64_t c = (sd.version >> 20) & 0x3ff;
8215 uint64_t d = (sd.version >> 10) & 0x3ff;
8216 uint64_t e = sd.version & 0x3ff;
8217 outs() << " version " << a << "." << b;
8218 if (e != 0)
8219 outs() << "." << c << "." << d << "." << e;
8220 else if (d != 0)
8221 outs() << "." << c << "." << d;
8222 else if (c != 0)
8223 outs() << "." << c;
8224 outs() << "\n";
8225}
8226
8227static void PrintEntryPointCommand(MachO::entry_point_command ep) {
8228 outs() << " cmd LC_MAIN\n";
8229 outs() << " cmdsize " << ep.cmdsize;
8230 if (ep.cmdsize != sizeof(struct MachO::entry_point_command))
8231 outs() << " Incorrect size\n";
8232 else
8233 outs() << "\n";
8234 outs() << " entryoff " << ep.entryoff << "\n";
8235 outs() << " stacksize " << ep.stacksize << "\n";
8236}
8237
Kevin Enderby0804f4672014-12-16 23:25:52 +00008238static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec,
8239 uint32_t object_size) {
8240 outs() << " cmd LC_ENCRYPTION_INFO\n";
8241 outs() << " cmdsize " << ec.cmdsize;
8242 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command))
8243 outs() << " Incorrect size\n";
8244 else
8245 outs() << "\n";
8246 outs() << " cryptoff " << ec.cryptoff;
8247 if (ec.cryptoff > object_size)
8248 outs() << " (past end of file)\n";
8249 else
8250 outs() << "\n";
8251 outs() << " cryptsize " << ec.cryptsize;
8252 if (ec.cryptsize > object_size)
8253 outs() << " (past end of file)\n";
8254 else
8255 outs() << "\n";
8256 outs() << " cryptid " << ec.cryptid << "\n";
8257}
8258
Kevin Enderby57538292014-12-17 01:01:30 +00008259static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec,
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008260 uint32_t object_size) {
Kevin Enderby57538292014-12-17 01:01:30 +00008261 outs() << " cmd LC_ENCRYPTION_INFO_64\n";
8262 outs() << " cmdsize " << ec.cmdsize;
8263 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64))
8264 outs() << " Incorrect size\n";
8265 else
8266 outs() << "\n";
8267 outs() << " cryptoff " << ec.cryptoff;
8268 if (ec.cryptoff > object_size)
8269 outs() << " (past end of file)\n";
8270 else
8271 outs() << "\n";
8272 outs() << " cryptsize " << ec.cryptsize;
8273 if (ec.cryptsize > object_size)
8274 outs() << " (past end of file)\n";
8275 else
8276 outs() << "\n";
8277 outs() << " cryptid " << ec.cryptid << "\n";
8278 outs() << " pad " << ec.pad << "\n";
8279}
8280
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00008281static void PrintLinkerOptionCommand(MachO::linker_option_command lo,
8282 const char *Ptr) {
8283 outs() << " cmd LC_LINKER_OPTION\n";
8284 outs() << " cmdsize " << lo.cmdsize;
8285 if (lo.cmdsize < sizeof(struct MachO::linker_option_command))
8286 outs() << " Incorrect size\n";
8287 else
8288 outs() << "\n";
8289 outs() << " count " << lo.count << "\n";
8290 const char *string = Ptr + sizeof(struct MachO::linker_option_command);
8291 uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command);
8292 uint32_t i = 0;
8293 while (left > 0) {
8294 while (*string == '\0' && left > 0) {
8295 string++;
8296 left--;
8297 }
8298 if (left > 0) {
8299 i++;
8300 outs() << " string #" << i << " " << format("%.*s\n", left, string);
David Majnemerd4449ed2014-12-20 08:24:43 +00008301 uint32_t NullPos = StringRef(string, left).find('\0');
8302 uint32_t len = std::min(NullPos, left) + 1;
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00008303 string += len;
8304 left -= len;
8305 }
8306 }
8307 if (lo.count != i)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008308 outs() << " count " << lo.count << " does not match number of strings "
8309 << i << "\n";
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00008310}
8311
Kevin Enderbyb4b79312014-12-18 19:24:35 +00008312static void PrintSubFrameworkCommand(MachO::sub_framework_command sub,
8313 const char *Ptr) {
8314 outs() << " cmd LC_SUB_FRAMEWORK\n";
8315 outs() << " cmdsize " << sub.cmdsize;
8316 if (sub.cmdsize < sizeof(struct MachO::sub_framework_command))
8317 outs() << " Incorrect size\n";
8318 else
8319 outs() << "\n";
8320 if (sub.umbrella < sub.cmdsize) {
8321 const char *P = Ptr + sub.umbrella;
8322 outs() << " umbrella " << P << " (offset " << sub.umbrella << ")\n";
8323 } else {
8324 outs() << " umbrella ?(bad offset " << sub.umbrella << ")\n";
8325 }
8326}
8327
Kevin Enderbya2bd8d92014-12-18 23:13:26 +00008328static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub,
8329 const char *Ptr) {
8330 outs() << " cmd LC_SUB_UMBRELLA\n";
8331 outs() << " cmdsize " << sub.cmdsize;
8332 if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command))
8333 outs() << " Incorrect size\n";
8334 else
8335 outs() << "\n";
8336 if (sub.sub_umbrella < sub.cmdsize) {
8337 const char *P = Ptr + sub.sub_umbrella;
8338 outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n";
8339 } else {
8340 outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n";
8341 }
8342}
8343
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00008344static void PrintSubLibraryCommand(MachO::sub_library_command sub,
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008345 const char *Ptr) {
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00008346 outs() << " cmd LC_SUB_LIBRARY\n";
8347 outs() << " cmdsize " << sub.cmdsize;
8348 if (sub.cmdsize < sizeof(struct MachO::sub_library_command))
8349 outs() << " Incorrect size\n";
8350 else
8351 outs() << "\n";
8352 if (sub.sub_library < sub.cmdsize) {
8353 const char *P = Ptr + sub.sub_library;
8354 outs() << " sub_library " << P << " (offset " << sub.sub_library << ")\n";
8355 } else {
8356 outs() << " sub_library ?(bad offset " << sub.sub_library << ")\n";
8357 }
8358}
8359
Kevin Enderby186eac32014-12-19 21:06:24 +00008360static void PrintSubClientCommand(MachO::sub_client_command sub,
8361 const char *Ptr) {
8362 outs() << " cmd LC_SUB_CLIENT\n";
8363 outs() << " cmdsize " << sub.cmdsize;
8364 if (sub.cmdsize < sizeof(struct MachO::sub_client_command))
8365 outs() << " Incorrect size\n";
8366 else
8367 outs() << "\n";
8368 if (sub.client < sub.cmdsize) {
8369 const char *P = Ptr + sub.client;
8370 outs() << " client " << P << " (offset " << sub.client << ")\n";
8371 } else {
8372 outs() << " client ?(bad offset " << sub.client << ")\n";
8373 }
8374}
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00008375
Kevin Enderby52e4ce42014-12-19 22:25:22 +00008376static void PrintRoutinesCommand(MachO::routines_command r) {
8377 outs() << " cmd LC_ROUTINES\n";
8378 outs() << " cmdsize " << r.cmdsize;
8379 if (r.cmdsize != sizeof(struct MachO::routines_command))
8380 outs() << " Incorrect size\n";
8381 else
8382 outs() << "\n";
8383 outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n";
8384 outs() << " init_module " << r.init_module << "\n";
8385 outs() << " reserved1 " << r.reserved1 << "\n";
8386 outs() << " reserved2 " << r.reserved2 << "\n";
8387 outs() << " reserved3 " << r.reserved3 << "\n";
8388 outs() << " reserved4 " << r.reserved4 << "\n";
8389 outs() << " reserved5 " << r.reserved5 << "\n";
8390 outs() << " reserved6 " << r.reserved6 << "\n";
8391}
8392
8393static void PrintRoutinesCommand64(MachO::routines_command_64 r) {
8394 outs() << " cmd LC_ROUTINES_64\n";
8395 outs() << " cmdsize " << r.cmdsize;
8396 if (r.cmdsize != sizeof(struct MachO::routines_command_64))
8397 outs() << " Incorrect size\n";
8398 else
8399 outs() << "\n";
8400 outs() << " init_address " << format("0x%016" PRIx64, r.init_address) << "\n";
8401 outs() << " init_module " << r.init_module << "\n";
8402 outs() << " reserved1 " << r.reserved1 << "\n";
8403 outs() << " reserved2 " << r.reserved2 << "\n";
8404 outs() << " reserved3 " << r.reserved3 << "\n";
8405 outs() << " reserved4 " << r.reserved4 << "\n";
8406 outs() << " reserved5 " << r.reserved5 << "\n";
8407 outs() << " reserved6 " << r.reserved6 << "\n";
8408}
8409
Kevin Enderby48ef5342014-12-23 22:56:39 +00008410static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) {
8411 outs() << " rax " << format("0x%016" PRIx64, cpu64.rax);
8412 outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx);
8413 outs() << " rcx " << format("0x%016" PRIx64, cpu64.rcx) << "\n";
8414 outs() << " rdx " << format("0x%016" PRIx64, cpu64.rdx);
8415 outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi);
8416 outs() << " rsi " << format("0x%016" PRIx64, cpu64.rsi) << "\n";
8417 outs() << " rbp " << format("0x%016" PRIx64, cpu64.rbp);
8418 outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp);
8419 outs() << " r8 " << format("0x%016" PRIx64, cpu64.r8) << "\n";
8420 outs() << " r9 " << format("0x%016" PRIx64, cpu64.r9);
8421 outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10);
8422 outs() << " r11 " << format("0x%016" PRIx64, cpu64.r11) << "\n";
8423 outs() << " r12 " << format("0x%016" PRIx64, cpu64.r12);
8424 outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13);
8425 outs() << " r14 " << format("0x%016" PRIx64, cpu64.r14) << "\n";
8426 outs() << " r15 " << format("0x%016" PRIx64, cpu64.r15);
8427 outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n";
8428 outs() << "rflags " << format("0x%016" PRIx64, cpu64.rflags);
8429 outs() << " cs " << format("0x%016" PRIx64, cpu64.cs);
8430 outs() << " fs " << format("0x%016" PRIx64, cpu64.fs) << "\n";
8431 outs() << " gs " << format("0x%016" PRIx64, cpu64.gs) << "\n";
8432}
8433
Kevin Enderby227df342014-12-23 23:43:59 +00008434static void Print_mmst_reg(MachO::mmst_reg_t &r) {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008435 uint32_t f;
8436 outs() << "\t mmst_reg ";
8437 for (f = 0; f < 10; f++)
8438 outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " ";
8439 outs() << "\n";
8440 outs() << "\t mmst_rsrv ";
8441 for (f = 0; f < 6; f++)
8442 outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " ";
8443 outs() << "\n";
8444}
8445
Kevin Enderbyaefb0032014-12-24 00:16:51 +00008446static void Print_xmm_reg(MachO::xmm_reg_t &r) {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008447 uint32_t f;
8448 outs() << "\t xmm_reg ";
8449 for (f = 0; f < 16; f++)
8450 outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " ";
8451 outs() << "\n";
8452}
8453
8454static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) {
8455 outs() << "\t fpu_reserved[0] " << fpu.fpu_reserved[0];
8456 outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n";
8457 outs() << "\t control: invalid " << fpu.fpu_fcw.invalid;
8458 outs() << " denorm " << fpu.fpu_fcw.denorm;
8459 outs() << " zdiv " << fpu.fpu_fcw.zdiv;
8460 outs() << " ovrfl " << fpu.fpu_fcw.ovrfl;
8461 outs() << " undfl " << fpu.fpu_fcw.undfl;
8462 outs() << " precis " << fpu.fpu_fcw.precis << "\n";
8463 outs() << "\t\t pc ";
8464 if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B)
8465 outs() << "FP_PREC_24B ";
8466 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B)
8467 outs() << "FP_PREC_53B ";
8468 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B)
8469 outs() << "FP_PREC_64B ";
8470 else
8471 outs() << fpu.fpu_fcw.pc << " ";
8472 outs() << "rc ";
8473 if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR)
8474 outs() << "FP_RND_NEAR ";
8475 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN)
8476 outs() << "FP_RND_DOWN ";
8477 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP)
8478 outs() << "FP_RND_UP ";
8479 else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008480 outs() << "FP_CHOP ";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008481 outs() << "\n";
8482 outs() << "\t status: invalid " << fpu.fpu_fsw.invalid;
8483 outs() << " denorm " << fpu.fpu_fsw.denorm;
8484 outs() << " zdiv " << fpu.fpu_fsw.zdiv;
8485 outs() << " ovrfl " << fpu.fpu_fsw.ovrfl;
8486 outs() << " undfl " << fpu.fpu_fsw.undfl;
8487 outs() << " precis " << fpu.fpu_fsw.precis;
8488 outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n";
8489 outs() << "\t errsumm " << fpu.fpu_fsw.errsumm;
8490 outs() << " c0 " << fpu.fpu_fsw.c0;
8491 outs() << " c1 " << fpu.fpu_fsw.c1;
8492 outs() << " c2 " << fpu.fpu_fsw.c2;
8493 outs() << " tos " << fpu.fpu_fsw.tos;
8494 outs() << " c3 " << fpu.fpu_fsw.c3;
8495 outs() << " busy " << fpu.fpu_fsw.busy << "\n";
8496 outs() << "\t fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw);
8497 outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1);
8498 outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop);
8499 outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n";
8500 outs() << "\t fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs);
8501 outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2);
8502 outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp);
8503 outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n";
8504 outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3);
8505 outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr);
8506 outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask);
8507 outs() << "\n";
8508 outs() << "\t fpu_stmm0:\n";
8509 Print_mmst_reg(fpu.fpu_stmm0);
8510 outs() << "\t fpu_stmm1:\n";
8511 Print_mmst_reg(fpu.fpu_stmm1);
8512 outs() << "\t fpu_stmm2:\n";
8513 Print_mmst_reg(fpu.fpu_stmm2);
8514 outs() << "\t fpu_stmm3:\n";
8515 Print_mmst_reg(fpu.fpu_stmm3);
8516 outs() << "\t fpu_stmm4:\n";
8517 Print_mmst_reg(fpu.fpu_stmm4);
8518 outs() << "\t fpu_stmm5:\n";
8519 Print_mmst_reg(fpu.fpu_stmm5);
8520 outs() << "\t fpu_stmm6:\n";
8521 Print_mmst_reg(fpu.fpu_stmm6);
8522 outs() << "\t fpu_stmm7:\n";
8523 Print_mmst_reg(fpu.fpu_stmm7);
8524 outs() << "\t fpu_xmm0:\n";
8525 Print_xmm_reg(fpu.fpu_xmm0);
8526 outs() << "\t fpu_xmm1:\n";
8527 Print_xmm_reg(fpu.fpu_xmm1);
8528 outs() << "\t fpu_xmm2:\n";
8529 Print_xmm_reg(fpu.fpu_xmm2);
8530 outs() << "\t fpu_xmm3:\n";
8531 Print_xmm_reg(fpu.fpu_xmm3);
8532 outs() << "\t fpu_xmm4:\n";
8533 Print_xmm_reg(fpu.fpu_xmm4);
8534 outs() << "\t fpu_xmm5:\n";
8535 Print_xmm_reg(fpu.fpu_xmm5);
8536 outs() << "\t fpu_xmm6:\n";
8537 Print_xmm_reg(fpu.fpu_xmm6);
8538 outs() << "\t fpu_xmm7:\n";
8539 Print_xmm_reg(fpu.fpu_xmm7);
8540 outs() << "\t fpu_xmm8:\n";
8541 Print_xmm_reg(fpu.fpu_xmm8);
8542 outs() << "\t fpu_xmm9:\n";
8543 Print_xmm_reg(fpu.fpu_xmm9);
8544 outs() << "\t fpu_xmm10:\n";
8545 Print_xmm_reg(fpu.fpu_xmm10);
8546 outs() << "\t fpu_xmm11:\n";
8547 Print_xmm_reg(fpu.fpu_xmm11);
8548 outs() << "\t fpu_xmm12:\n";
8549 Print_xmm_reg(fpu.fpu_xmm12);
8550 outs() << "\t fpu_xmm13:\n";
8551 Print_xmm_reg(fpu.fpu_xmm13);
8552 outs() << "\t fpu_xmm14:\n";
8553 Print_xmm_reg(fpu.fpu_xmm14);
8554 outs() << "\t fpu_xmm15:\n";
8555 Print_xmm_reg(fpu.fpu_xmm15);
8556 outs() << "\t fpu_rsrv4:\n";
8557 for (uint32_t f = 0; f < 6; f++) {
8558 outs() << "\t ";
8559 for (uint32_t g = 0; g < 16; g++)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008560 outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f * g]) << " ";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008561 outs() << "\n";
8562 }
8563 outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1);
8564 outs() << "\n";
8565}
8566
8567static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) {
8568 outs() << "\t trapno " << format("0x%08" PRIx32, exc64.trapno);
8569 outs() << " err " << format("0x%08" PRIx32, exc64.err);
8570 outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n";
8571}
8572
8573static void PrintThreadCommand(MachO::thread_command t, const char *Ptr,
8574 bool isLittleEndian, uint32_t cputype) {
8575 if (t.cmd == MachO::LC_THREAD)
8576 outs() << " cmd LC_THREAD\n";
8577 else if (t.cmd == MachO::LC_UNIXTHREAD)
8578 outs() << " cmd LC_UNIXTHREAD\n";
8579 else
8580 outs() << " cmd " << t.cmd << " (unknown)\n";
8581 outs() << " cmdsize " << t.cmdsize;
8582 if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t))
8583 outs() << " Incorrect size\n";
8584 else
8585 outs() << "\n";
8586
8587 const char *begin = Ptr + sizeof(struct MachO::thread_command);
8588 const char *end = Ptr + t.cmdsize;
8589 uint32_t flavor, count, left;
8590 if (cputype == MachO::CPU_TYPE_X86_64) {
8591 while (begin < end) {
8592 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8593 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8594 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008595 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008596 flavor = 0;
8597 begin = end;
8598 }
8599 if (isLittleEndian != sys::IsLittleEndianHost)
8600 sys::swapByteOrder(flavor);
8601 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8602 memcpy((char *)&count, begin, sizeof(uint32_t));
8603 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008604 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008605 count = 0;
8606 begin = end;
8607 }
8608 if (isLittleEndian != sys::IsLittleEndianHost)
8609 sys::swapByteOrder(count);
8610 if (flavor == MachO::x86_THREAD_STATE64) {
8611 outs() << " flavor x86_THREAD_STATE64\n";
8612 if (count == MachO::x86_THREAD_STATE64_COUNT)
8613 outs() << " count x86_THREAD_STATE64_COUNT\n";
8614 else
8615 outs() << " count " << count
8616 << " (not x86_THREAD_STATE64_COUNT)\n";
8617 MachO::x86_thread_state64_t cpu64;
8618 left = end - begin;
8619 if (left >= sizeof(MachO::x86_thread_state64_t)) {
8620 memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t));
8621 begin += sizeof(MachO::x86_thread_state64_t);
8622 } else {
8623 memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t));
8624 memcpy(&cpu64, begin, left);
8625 begin += left;
8626 }
8627 if (isLittleEndian != sys::IsLittleEndianHost)
8628 swapStruct(cpu64);
8629 Print_x86_thread_state64_t(cpu64);
8630 } else if (flavor == MachO::x86_THREAD_STATE) {
8631 outs() << " flavor x86_THREAD_STATE\n";
8632 if (count == MachO::x86_THREAD_STATE_COUNT)
8633 outs() << " count x86_THREAD_STATE_COUNT\n";
8634 else
8635 outs() << " count " << count
8636 << " (not x86_THREAD_STATE_COUNT)\n";
8637 struct MachO::x86_thread_state_t ts;
8638 left = end - begin;
8639 if (left >= sizeof(MachO::x86_thread_state_t)) {
8640 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
8641 begin += sizeof(MachO::x86_thread_state_t);
8642 } else {
8643 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
8644 memcpy(&ts, begin, left);
8645 begin += left;
8646 }
8647 if (isLittleEndian != sys::IsLittleEndianHost)
8648 swapStruct(ts);
8649 if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) {
8650 outs() << "\t tsh.flavor x86_THREAD_STATE64 ";
8651 if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT)
8652 outs() << "tsh.count x86_THREAD_STATE64_COUNT\n";
8653 else
8654 outs() << "tsh.count " << ts.tsh.count
8655 << " (not x86_THREAD_STATE64_COUNT\n";
8656 Print_x86_thread_state64_t(ts.uts.ts64);
8657 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008658 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
8659 << ts.tsh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008660 }
8661 } else if (flavor == MachO::x86_FLOAT_STATE) {
8662 outs() << " flavor x86_FLOAT_STATE\n";
8663 if (count == MachO::x86_FLOAT_STATE_COUNT)
8664 outs() << " count x86_FLOAT_STATE_COUNT\n";
8665 else
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008666 outs() << " count " << count << " (not x86_FLOAT_STATE_COUNT)\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008667 struct MachO::x86_float_state_t fs;
8668 left = end - begin;
8669 if (left >= sizeof(MachO::x86_float_state_t)) {
8670 memcpy(&fs, begin, sizeof(MachO::x86_float_state_t));
8671 begin += sizeof(MachO::x86_float_state_t);
8672 } else {
8673 memset(&fs, '\0', sizeof(MachO::x86_float_state_t));
8674 memcpy(&fs, begin, left);
8675 begin += left;
8676 }
8677 if (isLittleEndian != sys::IsLittleEndianHost)
8678 swapStruct(fs);
8679 if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) {
8680 outs() << "\t fsh.flavor x86_FLOAT_STATE64 ";
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008681 if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT)
Kevin Enderby48ef5342014-12-23 22:56:39 +00008682 outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n";
8683 else
8684 outs() << "fsh.count " << fs.fsh.count
8685 << " (not x86_FLOAT_STATE64_COUNT\n";
8686 Print_x86_float_state_t(fs.ufs.fs64);
8687 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008688 outs() << "\t fsh.flavor " << fs.fsh.flavor << " fsh.count "
8689 << fs.fsh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008690 }
8691 } else if (flavor == MachO::x86_EXCEPTION_STATE) {
8692 outs() << " flavor x86_EXCEPTION_STATE\n";
8693 if (count == MachO::x86_EXCEPTION_STATE_COUNT)
8694 outs() << " count x86_EXCEPTION_STATE_COUNT\n";
8695 else
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008696 outs() << " count " << count
Kevin Enderby48ef5342014-12-23 22:56:39 +00008697 << " (not x86_EXCEPTION_STATE_COUNT)\n";
8698 struct MachO::x86_exception_state_t es;
8699 left = end - begin;
8700 if (left >= sizeof(MachO::x86_exception_state_t)) {
8701 memcpy(&es, begin, sizeof(MachO::x86_exception_state_t));
8702 begin += sizeof(MachO::x86_exception_state_t);
8703 } else {
8704 memset(&es, '\0', sizeof(MachO::x86_exception_state_t));
8705 memcpy(&es, begin, left);
8706 begin += left;
8707 }
8708 if (isLittleEndian != sys::IsLittleEndianHost)
8709 swapStruct(es);
8710 if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) {
8711 outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n";
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008712 if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT)
Kevin Enderby48ef5342014-12-23 22:56:39 +00008713 outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n";
8714 else
8715 outs() << "\t esh.count " << es.esh.count
8716 << " (not x86_EXCEPTION_STATE64_COUNT\n";
8717 Print_x86_exception_state_t(es.ues.es64);
8718 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008719 outs() << "\t esh.flavor " << es.esh.flavor << " esh.count "
8720 << es.esh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008721 }
8722 } else {
8723 outs() << " flavor " << flavor << " (unknown)\n";
8724 outs() << " count " << count << "\n";
8725 outs() << " state (unknown)\n";
8726 begin += count * sizeof(uint32_t);
8727 }
8728 }
8729 } else {
8730 while (begin < end) {
8731 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8732 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8733 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008734 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008735 flavor = 0;
8736 begin = end;
8737 }
8738 if (isLittleEndian != sys::IsLittleEndianHost)
8739 sys::swapByteOrder(flavor);
8740 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8741 memcpy((char *)&count, begin, sizeof(uint32_t));
8742 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008743 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008744 count = 0;
8745 begin = end;
8746 }
8747 if (isLittleEndian != sys::IsLittleEndianHost)
8748 sys::swapByteOrder(count);
8749 outs() << " flavor " << flavor << "\n";
8750 outs() << " count " << count << "\n";
8751 outs() << " state (Unknown cputype/cpusubtype)\n";
8752 begin += count * sizeof(uint32_t);
8753 }
8754 }
8755}
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008756
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008757static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) {
8758 if (dl.cmd == MachO::LC_ID_DYLIB)
8759 outs() << " cmd LC_ID_DYLIB\n";
8760 else if (dl.cmd == MachO::LC_LOAD_DYLIB)
8761 outs() << " cmd LC_LOAD_DYLIB\n";
8762 else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB)
8763 outs() << " cmd LC_LOAD_WEAK_DYLIB\n";
8764 else if (dl.cmd == MachO::LC_REEXPORT_DYLIB)
8765 outs() << " cmd LC_REEXPORT_DYLIB\n";
8766 else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB)
8767 outs() << " cmd LC_LAZY_LOAD_DYLIB\n";
8768 else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
8769 outs() << " cmd LC_LOAD_UPWARD_DYLIB\n";
8770 else
8771 outs() << " cmd " << dl.cmd << " (unknown)\n";
8772 outs() << " cmdsize " << dl.cmdsize;
8773 if (dl.cmdsize < sizeof(struct MachO::dylib_command))
8774 outs() << " Incorrect size\n";
8775 else
8776 outs() << "\n";
8777 if (dl.dylib.name < dl.cmdsize) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008778 const char *P = (const char *)(Ptr) + dl.dylib.name;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008779 outs() << " name " << P << " (offset " << dl.dylib.name << ")\n";
8780 } else {
8781 outs() << " name ?(bad offset " << dl.dylib.name << ")\n";
8782 }
8783 outs() << " time stamp " << dl.dylib.timestamp << " ";
8784 time_t t = dl.dylib.timestamp;
8785 outs() << ctime(&t);
8786 outs() << " current version ";
8787 if (dl.dylib.current_version == 0xffffffff)
8788 outs() << "n/a\n";
8789 else
8790 outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "."
8791 << ((dl.dylib.current_version >> 8) & 0xff) << "."
8792 << (dl.dylib.current_version & 0xff) << "\n";
8793 outs() << "compatibility version ";
8794 if (dl.dylib.compatibility_version == 0xffffffff)
8795 outs() << "n/a\n";
8796 else
8797 outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
8798 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
8799 << (dl.dylib.compatibility_version & 0xff) << "\n";
8800}
8801
8802static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
8803 uint32_t object_size) {
8804 if (ld.cmd == MachO::LC_CODE_SIGNATURE)
Kevin Enderby1be37a32016-04-28 21:07:20 +00008805 outs() << " cmd LC_CODE_SIGNATURE\n";
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008806 else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO)
8807 outs() << " cmd LC_SEGMENT_SPLIT_INFO\n";
8808 else if (ld.cmd == MachO::LC_FUNCTION_STARTS)
8809 outs() << " cmd LC_FUNCTION_STARTS\n";
8810 else if (ld.cmd == MachO::LC_DATA_IN_CODE)
8811 outs() << " cmd LC_DATA_IN_CODE\n";
8812 else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS)
8813 outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n";
8814 else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT)
8815 outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n";
8816 else
8817 outs() << " cmd " << ld.cmd << " (?)\n";
8818 outs() << " cmdsize " << ld.cmdsize;
8819 if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command))
8820 outs() << " Incorrect size\n";
8821 else
8822 outs() << "\n";
8823 outs() << " dataoff " << ld.dataoff;
8824 if (ld.dataoff > object_size)
8825 outs() << " (past end of file)\n";
8826 else
8827 outs() << "\n";
8828 outs() << " datasize " << ld.datasize;
8829 uint64_t big_size = ld.dataoff;
8830 big_size += ld.datasize;
8831 if (big_size > object_size)
8832 outs() << " (past end of file)\n";
8833 else
8834 outs() << "\n";
8835}
8836
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00008837static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t filetype,
8838 uint32_t cputype, bool verbose) {
Kevin Enderby956366c2014-08-29 22:30:52 +00008839 StringRef Buf = Obj->getData();
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00008840 unsigned Index = 0;
8841 for (const auto &Command : Obj->load_commands()) {
8842 outs() << "Load command " << Index++ << "\n";
Kevin Enderby956366c2014-08-29 22:30:52 +00008843 if (Command.C.cmd == MachO::LC_SEGMENT) {
8844 MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
8845 const char *sg_segname = SLC.segname;
8846 PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr,
8847 SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot,
8848 SLC.initprot, SLC.nsects, SLC.flags, Buf.size(),
8849 verbose);
8850 for (unsigned j = 0; j < SLC.nsects; j++) {
Kevin Enderbyc9713382014-12-16 01:14:45 +00008851 MachO::section S = Obj->getSection(Command, j);
Kevin Enderby956366c2014-08-29 22:30:52 +00008852 PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align,
8853 S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2,
8854 SLC.cmd, sg_segname, filetype, Buf.size(), verbose);
8855 }
8856 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
8857 MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command);
8858 const char *sg_segname = SLC_64.segname;
8859 PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname,
8860 SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff,
8861 SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot,
8862 SLC_64.nsects, SLC_64.flags, Buf.size(), verbose);
8863 for (unsigned j = 0; j < SLC_64.nsects; j++) {
8864 MachO::section_64 S_64 = Obj->getSection64(Command, j);
8865 PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size,
8866 S_64.offset, S_64.align, S_64.reloff, S_64.nreloc,
8867 S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd,
8868 sg_segname, filetype, Buf.size(), verbose);
8869 }
8870 } else if (Command.C.cmd == MachO::LC_SYMTAB) {
8871 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
David Majnemer73cc6ff2014-11-13 19:48:56 +00008872 PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size());
Kevin Enderby956366c2014-08-29 22:30:52 +00008873 } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
8874 MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
8875 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
David Majnemer73cc6ff2014-11-13 19:48:56 +00008876 PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(),
8877 Obj->is64Bit());
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008878 } else if (Command.C.cmd == MachO::LC_DYLD_INFO ||
8879 Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
8880 MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command);
8881 PrintDyldInfoLoadCommand(DyldInfo, Buf.size());
8882 } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER ||
8883 Command.C.cmd == MachO::LC_ID_DYLINKER ||
8884 Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
8885 MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command);
8886 PrintDyldLoadCommand(Dyld, Command.Ptr);
8887 } else if (Command.C.cmd == MachO::LC_UUID) {
8888 MachO::uuid_command Uuid = Obj->getUuidCommand(Command);
8889 PrintUuidLoadCommand(Uuid);
Jean-Daniel Dupas00cc1f52014-12-04 07:37:02 +00008890 } else if (Command.C.cmd == MachO::LC_RPATH) {
8891 MachO::rpath_command Rpath = Obj->getRpathCommand(Command);
8892 PrintRpathLoadCommand(Rpath, Command.Ptr);
Kevin Enderby1ff0ecc2014-12-16 21:48:27 +00008893 } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX ||
Tim Northoverbfbfb122015-11-02 21:26:58 +00008894 Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS ||
8895 Command.C.cmd == MachO::LC_VERSION_MIN_TVOS ||
8896 Command.C.cmd == MachO::LC_VERSION_MIN_WATCHOS) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008897 MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command);
8898 PrintVersionMinLoadCommand(Vd);
8899 } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) {
8900 MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command);
8901 PrintSourceVersionCommand(Sd);
8902 } else if (Command.C.cmd == MachO::LC_MAIN) {
8903 MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command);
8904 PrintEntryPointCommand(Ep);
Kevin Enderby0804f4672014-12-16 23:25:52 +00008905 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008906 MachO::encryption_info_command Ei =
8907 Obj->getEncryptionInfoCommand(Command);
Kevin Enderby0804f4672014-12-16 23:25:52 +00008908 PrintEncryptionInfoCommand(Ei, Buf.size());
Kevin Enderby57538292014-12-17 01:01:30 +00008909 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008910 MachO::encryption_info_command_64 Ei =
8911 Obj->getEncryptionInfoCommand64(Command);
Kevin Enderby57538292014-12-17 01:01:30 +00008912 PrintEncryptionInfoCommand64(Ei, Buf.size());
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00008913 } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008914 MachO::linker_option_command Lo =
8915 Obj->getLinkerOptionLoadCommand(Command);
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00008916 PrintLinkerOptionCommand(Lo, Command.Ptr);
Kevin Enderbyb4b79312014-12-18 19:24:35 +00008917 } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) {
8918 MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command);
8919 PrintSubFrameworkCommand(Sf, Command.Ptr);
Kevin Enderbya2bd8d92014-12-18 23:13:26 +00008920 } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) {
8921 MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command);
8922 PrintSubUmbrellaCommand(Sf, Command.Ptr);
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00008923 } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) {
8924 MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command);
8925 PrintSubLibraryCommand(Sl, Command.Ptr);
Kevin Enderby186eac32014-12-19 21:06:24 +00008926 } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) {
8927 MachO::sub_client_command Sc = Obj->getSubClientCommand(Command);
8928 PrintSubClientCommand(Sc, Command.Ptr);
Kevin Enderby52e4ce42014-12-19 22:25:22 +00008929 } else if (Command.C.cmd == MachO::LC_ROUTINES) {
8930 MachO::routines_command Rc = Obj->getRoutinesCommand(Command);
8931 PrintRoutinesCommand(Rc);
8932 } else if (Command.C.cmd == MachO::LC_ROUTINES_64) {
8933 MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command);
8934 PrintRoutinesCommand64(Rc);
Kevin Enderby48ef5342014-12-23 22:56:39 +00008935 } else if (Command.C.cmd == MachO::LC_THREAD ||
8936 Command.C.cmd == MachO::LC_UNIXTHREAD) {
8937 MachO::thread_command Tc = Obj->getThreadCommand(Command);
8938 PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype);
Nick Kledzik15558912014-10-16 18:58:20 +00008939 } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
8940 Command.C.cmd == MachO::LC_ID_DYLIB ||
8941 Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
8942 Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
8943 Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
8944 Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008945 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
8946 PrintDylibCommand(Dl, Command.Ptr);
8947 } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE ||
8948 Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO ||
8949 Command.C.cmd == MachO::LC_FUNCTION_STARTS ||
8950 Command.C.cmd == MachO::LC_DATA_IN_CODE ||
8951 Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS ||
8952 Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
8953 MachO::linkedit_data_command Ld =
8954 Obj->getLinkeditDataLoadCommand(Command);
8955 PrintLinkEditDataCommand(Ld, Buf.size());
Kevin Enderby956366c2014-08-29 22:30:52 +00008956 } else {
8957 outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd)
8958 << ")\n";
8959 outs() << " cmdsize " << Command.C.cmdsize << "\n";
8960 // TODO: get and print the raw bytes of the load command.
8961 }
8962 // TODO: print all the other kinds of load commands.
Kevin Enderby956366c2014-08-29 22:30:52 +00008963 }
8964}
8965
Kevin Enderby0ae163f2016-01-13 00:25:36 +00008966static void PrintMachHeader(const MachOObjectFile *Obj, bool verbose) {
Kevin Enderbyb76d3862014-08-22 20:35:18 +00008967 if (Obj->is64Bit()) {
8968 MachO::mach_header_64 H_64;
8969 H_64 = Obj->getHeader64();
8970 PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
8971 H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
8972 } else {
8973 MachO::mach_header H;
8974 H = Obj->getHeader();
8975 PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
8976 H.sizeofcmds, H.flags, verbose);
8977 }
8978}
8979
8980void llvm::printMachOFileHeader(const object::ObjectFile *Obj) {
8981 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
Kevin Enderby0ae163f2016-01-13 00:25:36 +00008982 PrintMachHeader(file, !NonVerbose);
8983}
8984
8985void llvm::printMachOLoadCommands(const object::ObjectFile *Obj) {
8986 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
Kevin Enderby956366c2014-08-29 22:30:52 +00008987 uint32_t filetype = 0;
8988 uint32_t cputype = 0;
Kevin Enderby0ae163f2016-01-13 00:25:36 +00008989 if (file->is64Bit()) {
8990 MachO::mach_header_64 H_64;
8991 H_64 = file->getHeader64();
8992 filetype = H_64.filetype;
8993 cputype = H_64.cputype;
8994 } else {
8995 MachO::mach_header H;
8996 H = file->getHeader();
8997 filetype = H.filetype;
8998 cputype = H.cputype;
8999 }
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00009000 PrintLoadCommands(file, filetype, cputype, !NonVerbose);
Kevin Enderbyb76d3862014-08-22 20:35:18 +00009001}
Nick Kledzikd04bc352014-08-30 00:20:14 +00009002
9003//===----------------------------------------------------------------------===//
9004// export trie dumping
9005//===----------------------------------------------------------------------===//
9006
9007void llvm::printMachOExportsTrie(const object::MachOObjectFile *Obj) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009008 for (const llvm::object::ExportEntry &Entry : Obj->exports()) {
9009 uint64_t Flags = Entry.flags();
Nick Kledzikd04bc352014-08-30 00:20:14 +00009010 bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
9011 bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
9012 bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
9013 MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL);
9014 bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
9015 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
9016 bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
9017 if (ReExport)
9018 outs() << "[re-export] ";
9019 else
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009020 outs() << format("0x%08llX ",
9021 Entry.address()); // FIXME:add in base address
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009022 outs() << Entry.name();
Nick Kledzikd04bc352014-08-30 00:20:14 +00009023 if (WeakDef || ThreadLocal || Resolver || Abs) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009024 bool NeedsComma = false;
Nick Kledzik1d1ac4b2014-09-03 01:12:52 +00009025 outs() << " [";
Nick Kledzikd04bc352014-08-30 00:20:14 +00009026 if (WeakDef) {
9027 outs() << "weak_def";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009028 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00009029 }
9030 if (ThreadLocal) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009031 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00009032 outs() << ", ";
9033 outs() << "per-thread";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009034 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00009035 }
9036 if (Abs) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009037 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00009038 outs() << ", ";
9039 outs() << "absolute";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009040 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00009041 }
9042 if (Resolver) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009043 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00009044 outs() << ", ";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009045 outs() << format("resolver=0x%08llX", Entry.other());
9046 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00009047 }
9048 outs() << "]";
9049 }
9050 if (ReExport) {
9051 StringRef DylibName = "unknown";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009052 int Ordinal = Entry.other() - 1;
9053 Obj->getLibraryShortNameByIndex(Ordinal, DylibName);
9054 if (Entry.otherName().empty())
Nick Kledzikd04bc352014-08-30 00:20:14 +00009055 outs() << " (from " << DylibName << ")";
9056 else
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009057 outs() << " (" << Entry.otherName() << " from " << DylibName << ")";
Nick Kledzikd04bc352014-08-30 00:20:14 +00009058 }
9059 outs() << "\n";
9060 }
9061}
Nick Kledzikac431442014-09-12 21:34:15 +00009062
Nick Kledzikac431442014-09-12 21:34:15 +00009063//===----------------------------------------------------------------------===//
9064// rebase table dumping
9065//===----------------------------------------------------------------------===//
9066
9067namespace {
9068class SegInfo {
9069public:
9070 SegInfo(const object::MachOObjectFile *Obj);
9071
9072 StringRef segmentName(uint32_t SegIndex);
9073 StringRef sectionName(uint32_t SegIndex, uint64_t SegOffset);
9074 uint64_t address(uint32_t SegIndex, uint64_t SegOffset);
Kevin Enderbyaf7c9d02015-10-09 16:48:44 +00009075 bool isValidSegIndexAndOffset(uint32_t SegIndex, uint64_t SegOffset);
Nick Kledzikac431442014-09-12 21:34:15 +00009076
9077private:
9078 struct SectionInfo {
9079 uint64_t Address;
9080 uint64_t Size;
9081 StringRef SectionName;
9082 StringRef SegmentName;
9083 uint64_t OffsetInSegment;
9084 uint64_t SegmentStartAddress;
9085 uint32_t SegmentIndex;
9086 };
9087 const SectionInfo &findSection(uint32_t SegIndex, uint64_t SegOffset);
9088 SmallVector<SectionInfo, 32> Sections;
9089};
9090}
9091
9092SegInfo::SegInfo(const object::MachOObjectFile *Obj) {
9093 // Build table of sections so segIndex/offset pairs can be translated.
Nick Kledzik56ebef42014-09-16 01:41:51 +00009094 uint32_t CurSegIndex = Obj->hasPageZeroSegment() ? 1 : 0;
Nick Kledzikac431442014-09-12 21:34:15 +00009095 StringRef CurSegName;
9096 uint64_t CurSegAddress;
9097 for (const SectionRef &Section : Obj->sections()) {
9098 SectionInfo Info;
Davide Italianoccd53fe2015-08-05 07:18:31 +00009099 error(Section.getName(Info.SectionName));
Rafael Espindola80291272014-10-08 15:28:58 +00009100 Info.Address = Section.getAddress();
9101 Info.Size = Section.getSize();
Nick Kledzikac431442014-09-12 21:34:15 +00009102 Info.SegmentName =
9103 Obj->getSectionFinalSegmentName(Section.getRawDataRefImpl());
9104 if (!Info.SegmentName.equals(CurSegName)) {
9105 ++CurSegIndex;
9106 CurSegName = Info.SegmentName;
9107 CurSegAddress = Info.Address;
9108 }
9109 Info.SegmentIndex = CurSegIndex - 1;
9110 Info.OffsetInSegment = Info.Address - CurSegAddress;
9111 Info.SegmentStartAddress = CurSegAddress;
9112 Sections.push_back(Info);
9113 }
9114}
9115
9116StringRef SegInfo::segmentName(uint32_t SegIndex) {
9117 for (const SectionInfo &SI : Sections) {
9118 if (SI.SegmentIndex == SegIndex)
9119 return SI.SegmentName;
9120 }
9121 llvm_unreachable("invalid segIndex");
9122}
9123
Kevin Enderbyaf7c9d02015-10-09 16:48:44 +00009124bool SegInfo::isValidSegIndexAndOffset(uint32_t SegIndex,
9125 uint64_t OffsetInSeg) {
9126 for (const SectionInfo &SI : Sections) {
9127 if (SI.SegmentIndex != SegIndex)
9128 continue;
9129 if (SI.OffsetInSegment > OffsetInSeg)
9130 continue;
9131 if (OffsetInSeg >= (SI.OffsetInSegment + SI.Size))
9132 continue;
9133 return true;
9134 }
9135 return false;
9136}
9137
Nick Kledzikac431442014-09-12 21:34:15 +00009138const SegInfo::SectionInfo &SegInfo::findSection(uint32_t SegIndex,
9139 uint64_t OffsetInSeg) {
9140 for (const SectionInfo &SI : Sections) {
9141 if (SI.SegmentIndex != SegIndex)
9142 continue;
9143 if (SI.OffsetInSegment > OffsetInSeg)
9144 continue;
9145 if (OffsetInSeg >= (SI.OffsetInSegment + SI.Size))
9146 continue;
9147 return SI;
9148 }
9149 llvm_unreachable("segIndex and offset not in any section");
9150}
9151
9152StringRef SegInfo::sectionName(uint32_t SegIndex, uint64_t OffsetInSeg) {
9153 return findSection(SegIndex, OffsetInSeg).SectionName;
9154}
9155
9156uint64_t SegInfo::address(uint32_t SegIndex, uint64_t OffsetInSeg) {
9157 const SectionInfo &SI = findSection(SegIndex, OffsetInSeg);
9158 return SI.SegmentStartAddress + OffsetInSeg;
9159}
9160
9161void llvm::printMachORebaseTable(const object::MachOObjectFile *Obj) {
9162 // Build table of sections so names can used in final output.
9163 SegInfo sectionTable(Obj);
9164
9165 outs() << "segment section address type\n";
9166 for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable()) {
9167 uint32_t SegIndex = Entry.segmentIndex();
9168 uint64_t OffsetInSeg = Entry.segmentOffset();
9169 StringRef SegmentName = sectionTable.segmentName(SegIndex);
9170 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
9171 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
9172
9173 // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009174 outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n",
9175 SegmentName.str().c_str(), SectionName.str().c_str(),
9176 Address, Entry.typeName().str().c_str());
Nick Kledzikac431442014-09-12 21:34:15 +00009177 }
9178}
Nick Kledzik56ebef42014-09-16 01:41:51 +00009179
9180static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
9181 StringRef DylibName;
9182 switch (Ordinal) {
9183 case MachO::BIND_SPECIAL_DYLIB_SELF:
9184 return "this-image";
9185 case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
9186 return "main-executable";
9187 case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
9188 return "flat-namespace";
9189 default:
Nick Kledzikabd29872014-09-16 22:03:13 +00009190 if (Ordinal > 0) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009191 std::error_code EC =
9192 Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName);
Nick Kledzikabd29872014-09-16 22:03:13 +00009193 if (EC)
Nick Kledzik51d2c2b2014-10-14 23:29:38 +00009194 return "<<bad library ordinal>>";
Nick Kledzikabd29872014-09-16 22:03:13 +00009195 return DylibName;
9196 }
Nick Kledzik56ebef42014-09-16 01:41:51 +00009197 }
Nick Kledzikabd29872014-09-16 22:03:13 +00009198 return "<<unknown special ordinal>>";
Nick Kledzik56ebef42014-09-16 01:41:51 +00009199}
9200
9201//===----------------------------------------------------------------------===//
9202// bind table dumping
9203//===----------------------------------------------------------------------===//
9204
9205void llvm::printMachOBindTable(const object::MachOObjectFile *Obj) {
9206 // Build table of sections so names can used in final output.
9207 SegInfo sectionTable(Obj);
9208
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009209 outs() << "segment section address type "
9210 "addend dylib symbol\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00009211 for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable()) {
9212 uint32_t SegIndex = Entry.segmentIndex();
9213 uint64_t OffsetInSeg = Entry.segmentOffset();
9214 StringRef SegmentName = sectionTable.segmentName(SegIndex);
9215 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
9216 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
9217
9218 // Table lines look like:
9219 // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009220 StringRef Attr;
Nick Kledzik56ebef42014-09-16 01:41:51 +00009221 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT)
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009222 Attr = " (weak_import)";
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009223 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009224 << left_justify(SectionName, 18) << " "
9225 << format_hex(Address, 10, true) << " "
9226 << left_justify(Entry.typeName(), 8) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009227 << format_decimal(Entry.addend(), 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009228 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009229 << Entry.symbolName() << Attr << "\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00009230 }
9231}
9232
9233//===----------------------------------------------------------------------===//
9234// lazy bind table dumping
9235//===----------------------------------------------------------------------===//
9236
9237void llvm::printMachOLazyBindTable(const object::MachOObjectFile *Obj) {
9238 // Build table of sections so names can used in final output.
9239 SegInfo sectionTable(Obj);
9240
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009241 outs() << "segment section address "
9242 "dylib symbol\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00009243 for (const llvm::object::MachOBindEntry &Entry : Obj->lazyBindTable()) {
9244 uint32_t SegIndex = Entry.segmentIndex();
9245 uint64_t OffsetInSeg = Entry.segmentOffset();
9246 StringRef SegmentName = sectionTable.segmentName(SegIndex);
9247 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
9248 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
9249
9250 // Table lines look like:
9251 // __DATA __got 0x00012010 libSystem ___stack_chk_guard
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009252 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009253 << left_justify(SectionName, 18) << " "
9254 << format_hex(Address, 10, true) << " "
9255 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
Nick Kledzik56ebef42014-09-16 01:41:51 +00009256 << Entry.symbolName() << "\n";
9257 }
9258}
9259
Nick Kledzik56ebef42014-09-16 01:41:51 +00009260//===----------------------------------------------------------------------===//
9261// weak bind table dumping
9262//===----------------------------------------------------------------------===//
9263
9264void llvm::printMachOWeakBindTable(const object::MachOObjectFile *Obj) {
9265 // Build table of sections so names can used in final output.
9266 SegInfo sectionTable(Obj);
9267
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009268 outs() << "segment section address "
9269 "type addend symbol\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00009270 for (const llvm::object::MachOBindEntry &Entry : Obj->weakBindTable()) {
9271 // Strong symbols don't have a location to update.
9272 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009273 outs() << " strong "
Nick Kledzik56ebef42014-09-16 01:41:51 +00009274 << Entry.symbolName() << "\n";
9275 continue;
9276 }
9277 uint32_t SegIndex = Entry.segmentIndex();
9278 uint64_t OffsetInSeg = Entry.segmentOffset();
9279 StringRef SegmentName = sectionTable.segmentName(SegIndex);
9280 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
9281 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
9282
9283 // Table lines look like:
9284 // __DATA __data 0x00001000 pointer 0 _foo
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009285 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009286 << left_justify(SectionName, 18) << " "
9287 << format_hex(Address, 10, true) << " "
9288 << left_justify(Entry.typeName(), 8) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009289 << format_decimal(Entry.addend(), 8) << " " << Entry.symbolName()
9290 << "\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00009291 }
9292}
9293
Kevin Enderby6f326ce2014-10-23 19:37:31 +00009294// get_dyld_bind_info_symbolname() is used for disassembly and passed an
9295// address, ReferenceValue, in the Mach-O file and looks in the dyld bind
9296// information for that address. If the address is found its binding symbol
9297// name is returned. If not nullptr is returned.
9298static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
9299 struct DisassembleInfo *info) {
Kevin Enderby078be602014-10-23 19:53:12 +00009300 if (info->bindtable == nullptr) {
9301 info->bindtable = new (BindTable);
Kevin Enderby6f326ce2014-10-23 19:37:31 +00009302 SegInfo sectionTable(info->O);
9303 for (const llvm::object::MachOBindEntry &Entry : info->O->bindTable()) {
9304 uint32_t SegIndex = Entry.segmentIndex();
9305 uint64_t OffsetInSeg = Entry.segmentOffset();
Kevin Enderbyaf7c9d02015-10-09 16:48:44 +00009306 if (!sectionTable.isValidSegIndexAndOffset(SegIndex, OffsetInSeg))
9307 continue;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00009308 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
9309 const char *SymbolName = nullptr;
9310 StringRef name = Entry.symbolName();
9311 if (!name.empty())
9312 SymbolName = name.data();
Kevin Enderby078be602014-10-23 19:53:12 +00009313 info->bindtable->push_back(std::make_pair(Address, SymbolName));
Kevin Enderby6f326ce2014-10-23 19:37:31 +00009314 }
9315 }
Kevin Enderby078be602014-10-23 19:53:12 +00009316 for (bind_table_iterator BI = info->bindtable->begin(),
9317 BE = info->bindtable->end();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00009318 BI != BE; ++BI) {
9319 uint64_t Address = BI->first;
9320 if (ReferenceValue == Address) {
9321 const char *SymbolName = BI->second;
9322 return SymbolName;
9323 }
9324 }
9325 return nullptr;
9326}