blob: 854f465fdd56b7ce2e116cfb9ff508b3e61b2a7c [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"
Rafael Espindolab940b662016-09-06 19:16:48 +000023#include "llvm/Demangle/Demangle.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000024#include "llvm/MC/MCAsmInfo.h"
Lang Hamesa1bc0f52014-04-15 04:40:56 +000025#include "llvm/MC/MCContext.h"
Benjamin Kramerf57c1972016-01-26 16:44:37 +000026#include "llvm/MC/MCDisassembler/MCDisassembler.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000027#include "llvm/MC/MCInst.h"
28#include "llvm/MC/MCInstPrinter.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000029#include "llvm/MC/MCInstrDesc.h"
30#include "llvm/MC/MCInstrInfo.h"
Jim Grosbachfd93a592012-03-05 19:33:20 +000031#include "llvm/MC/MCRegisterInfo.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000032#include "llvm/MC/MCSubtargetInfo.h"
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +000033#include "llvm/Object/MachOUniversal.h"
Rafael Espindola9b709252013-04-13 01:45:40 +000034#include "llvm/Support/Casting.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000035#include "llvm/Support/CommandLine.h"
36#include "llvm/Support/Debug.h"
Tim Northover4bd286a2014-08-01 13:07:19 +000037#include "llvm/Support/Endian.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000038#include "llvm/Support/Format.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000039#include "llvm/Support/FormattedStream.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000040#include "llvm/Support/GraphWriter.h"
Kevin Enderby9a509442015-01-27 21:28:24 +000041#include "llvm/Support/LEB128.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000042#include "llvm/Support/MachO.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000043#include "llvm/Support/MemoryBuffer.h"
44#include "llvm/Support/TargetRegistry.h"
45#include "llvm/Support/TargetSelect.h"
Kevin Enderby9873e2c2016-05-23 21:34:12 +000046#include "llvm/Support/ToolOutputFile.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000047#include "llvm/Support/raw_ostream.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000048#include <algorithm>
49#include <cstring>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000050#include <system_error>
Kevin Enderby04bf6932014-10-28 23:39:46 +000051
Kevin Enderby9873e2c2016-05-23 21:34:12 +000052#ifdef HAVE_LIBXAR
53extern "C" {
54#include <xar/xar.h>
55}
56#endif
57
Benjamin Kramer43a772e2011-09-19 17:56:04 +000058using namespace llvm;
59using namespace object;
60
61static cl::opt<bool>
Kevin Enderbyb28ed012014-10-29 21:28:24 +000062 UseDbg("g",
63 cl::desc("Print line information from debug info if available"));
Benjamin Kramer699128e2011-09-21 01:13:19 +000064
Kevin Enderbyb28ed012014-10-29 21:28:24 +000065static cl::opt<std::string> DSYMFile("dsym",
66 cl::desc("Use .dSYM file for debug info"));
Benjamin Kramer699128e2011-09-21 01:13:19 +000067
Kevin Enderbyb28ed012014-10-29 21:28:24 +000068static cl::opt<bool> FullLeadingAddr("full-leading-addr",
69 cl::desc("Print full leading address"));
Kevin Enderbybf246f52014-09-24 23:08:22 +000070
Kevin Enderbyab5e6c92015-03-17 21:07:39 +000071static cl::opt<bool> NoLeadingAddr("no-leading-addr",
72 cl::desc("Print no leading address"));
73
Kevin Enderby13023a12015-01-15 23:19:11 +000074cl::opt<bool> llvm::UniversalHeaders("universal-headers",
75 cl::desc("Print Mach-O universal headers "
76 "(requires -macho)"));
77
Kevin Enderby131d1772015-01-09 19:22:37 +000078cl::opt<bool>
Kevin Enderby13023a12015-01-15 23:19:11 +000079 llvm::ArchiveHeaders("archive-headers",
80 cl::desc("Print archive headers for Mach-O archives "
81 "(requires -macho)"));
Kevin Enderby131d1772015-01-09 19:22:37 +000082
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +000083cl::opt<bool>
Kevin Enderby8972e482015-04-30 20:30:42 +000084 ArchiveMemberOffsets("archive-member-offsets",
85 cl::desc("Print the offset to each archive member for "
86 "Mach-O archives (requires -macho and "
87 "-archive-headers)"));
88
89cl::opt<bool>
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +000090 llvm::IndirectSymbols("indirect-symbols",
91 cl::desc("Print indirect symbol table for Mach-O "
92 "objects (requires -macho)"));
93
Kevin Enderby69fe98d2015-01-23 18:52:17 +000094cl::opt<bool>
95 llvm::DataInCode("data-in-code",
96 cl::desc("Print the data in code table for Mach-O objects "
97 "(requires -macho)"));
98
Kevin Enderby9a509442015-01-27 21:28:24 +000099cl::opt<bool>
100 llvm::LinkOptHints("link-opt-hints",
101 cl::desc("Print the linker optimization hints for "
102 "Mach-O objects (requires -macho)"));
103
Kevin Enderbycd66be52015-03-11 22:06:32 +0000104cl::opt<bool>
105 llvm::InfoPlist("info-plist",
106 cl::desc("Print the info plist section as strings for "
107 "Mach-O objects (requires -macho)"));
108
Kevin Enderbyf0640752015-03-13 17:56:32 +0000109cl::opt<bool>
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000110 llvm::DylibsUsed("dylibs-used",
111 cl::desc("Print the shared libraries used for linked "
112 "Mach-O files (requires -macho)"));
113
114cl::opt<bool>
115 llvm::DylibId("dylib-id",
116 cl::desc("Print the shared library's id for the dylib Mach-O "
117 "file (requires -macho)"));
118
119cl::opt<bool>
Kevin Enderbyf0640752015-03-13 17:56:32 +0000120 llvm::NonVerbose("non-verbose",
121 cl::desc("Print the info for Mach-O objects in "
122 "non-verbose or numeric form (requires -macho)"));
123
Kevin Enderby0fc11822015-04-01 20:57:01 +0000124cl::opt<bool>
125 llvm::ObjcMetaData("objc-meta-data",
126 cl::desc("Print the Objective-C runtime meta data for "
127 "Mach-O files (requires -macho)"));
128
Kevin Enderby6a221752015-03-17 17:10:57 +0000129cl::opt<std::string> llvm::DisSymName(
130 "dis-symname",
Saleem Abdulrasoolec6a7742016-09-08 23:17:34 +0000131 cl::desc("disassemble just this symbol's instructions (requires -macho)"));
Kevin Enderby6a221752015-03-17 17:10:57 +0000132
Kevin Enderby8e29ec92015-03-17 22:26:11 +0000133static cl::opt<bool> NoSymbolicOperands(
134 "no-symbolic-operands",
135 cl::desc("do not symbolic operands when disassembling (requires -macho)"));
136
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000137static cl::list<std::string>
138 ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
139 cl::ZeroOrMore);
Hans Wennborgcc9deb42015-09-29 18:02:48 +0000140
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000141bool ArchAll = false;
142
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000143static std::string ThumbTripleName;
144
145static const Target *GetTarget(const MachOObjectFile *MachOObj,
146 const char **McpuDefault,
147 const Target **ThumbTarget) {
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000148 // Figure out the target triple.
Tim Northover9e8eb412016-04-22 23:21:13 +0000149 llvm::Triple TT(TripleName);
Cameron Zwarich88cc16a2012-02-03 06:35:22 +0000150 if (TripleName.empty()) {
Tim Northover9e8eb412016-04-22 23:21:13 +0000151 TT = MachOObj->getArchTriple(McpuDefault);
Cameron Zwarich88cc16a2012-02-03 06:35:22 +0000152 TripleName = TT.str();
Tim Northover9e8eb412016-04-22 23:21:13 +0000153 }
154
155 if (TT.getArch() == Triple::arm) {
156 // We've inferred a 32-bit ARM target from the object file. All MachO CPUs
157 // that support ARM are also capable of Thumb mode.
158 llvm::Triple ThumbTriple = TT;
159 std::string ThumbName = (Twine("thumb") + TT.getArchName().substr(3)).str();
160 ThumbTriple.setArchName(ThumbName);
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000161 ThumbTripleName = ThumbTriple.str();
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000162 }
163
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000164 // Get the target specific parser.
165 std::string Error;
166 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000167 if (TheTarget && ThumbTripleName.empty())
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000168 return TheTarget;
169
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000170 *ThumbTarget = TargetRegistry::lookupTarget(ThumbTripleName, Error);
171 if (*ThumbTarget)
172 return TheTarget;
173
174 errs() << "llvm-objdump: error: unable to get target for '";
175 if (!TheTarget)
176 errs() << TripleName;
177 else
178 errs() << ThumbTripleName;
179 errs() << "', see --version and --triple.\n";
Craig Toppere6cb63e2014-04-25 04:24:47 +0000180 return nullptr;
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000181}
182
Owen Andersond9243c42011-10-17 21:37:35 +0000183struct SymbolSorter {
184 bool operator()(const SymbolRef &A, const SymbolRef &B) {
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000185 Expected<SymbolRef::Type> ATypeOrErr = A.getType();
186 if (!ATypeOrErr) {
187 std::string Buf;
188 raw_string_ostream OS(Buf);
189 logAllUnhandledErrors(ATypeOrErr.takeError(), OS, "");
190 OS.flush();
191 report_fatal_error(Buf);
192 }
Kevin Enderby5afbc1c2016-03-23 20:27:00 +0000193 SymbolRef::Type AType = *ATypeOrErr;
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000194 Expected<SymbolRef::Type> BTypeOrErr = B.getType();
195 if (!BTypeOrErr) {
196 std::string Buf;
197 raw_string_ostream OS(Buf);
198 logAllUnhandledErrors(BTypeOrErr.takeError(), OS, "");
199 OS.flush();
200 report_fatal_error(Buf);
201 }
Kevin Enderby74f58d42016-03-23 21:45:21 +0000202 SymbolRef::Type BType = *BTypeOrErr;
Kevin Enderby5afbc1c2016-03-23 20:27:00 +0000203 uint64_t AAddr = (AType != SymbolRef::ST_Function) ? 0 : A.getValue();
204 uint64_t BAddr = (BType != SymbolRef::ST_Function) ? 0 : B.getValue();
Owen Andersond9243c42011-10-17 21:37:35 +0000205 return AAddr < BAddr;
206 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000207};
208
Kevin Enderby273ae012013-06-06 17:20:50 +0000209// Types for the storted data in code table that is built before disassembly
210// and the predicate function to sort them.
211typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
212typedef std::vector<DiceTableEntry> DiceTable;
213typedef DiceTable::iterator dice_table_iterator;
214
Kevin Enderby930fdc72014-11-06 19:00:13 +0000215// This is used to search for a data in code table entry for the PC being
216// disassembled. The j parameter has the PC in j.first. A single data in code
217// table entry can cover many bytes for each of its Kind's. So if the offset,
218// aka the i.first value, of the data in code table entry plus its Length
219// covers the PC being searched for this will return true. If not it will
220// return false.
David Majnemerea9b8ee2014-11-04 08:41:48 +0000221static bool compareDiceTableEntries(const DiceTableEntry &i,
222 const DiceTableEntry &j) {
Kevin Enderby930fdc72014-11-06 19:00:13 +0000223 uint16_t Length;
224 i.second.getLength(Length);
225
226 return j.first >= i.first && j.first < i.first + Length;
Kevin Enderby273ae012013-06-06 17:20:50 +0000227}
228
Colin LeMahieufc32b1b2015-03-18 19:27:31 +0000229static uint64_t DumpDataInCode(const uint8_t *bytes, uint64_t Length,
Kevin Enderby930fdc72014-11-06 19:00:13 +0000230 unsigned short Kind) {
231 uint32_t Value, Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000232
233 switch (Kind) {
Kevin Enderby930fdc72014-11-06 19:00:13 +0000234 default:
Charles Davis8bdfafd2013-09-01 04:28:48 +0000235 case MachO::DICE_KIND_DATA:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000236 if (Length >= 4) {
237 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000238 dumpBytes(makeArrayRef(bytes, 4), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000239 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
Kevin Enderby273ae012013-06-06 17:20:50 +0000240 outs() << "\t.long " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000241 Size = 4;
242 } else if (Length >= 2) {
243 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000244 dumpBytes(makeArrayRef(bytes, 2), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000245 Value = bytes[1] << 8 | bytes[0];
Kevin Enderby273ae012013-06-06 17:20:50 +0000246 outs() << "\t.short " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000247 Size = 2;
248 } else {
249 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000250 dumpBytes(makeArrayRef(bytes, 2), outs());
Kevin Enderby273ae012013-06-06 17:20:50 +0000251 Value = bytes[0];
252 outs() << "\t.byte " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000253 Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000254 }
Kevin Enderby930fdc72014-11-06 19:00:13 +0000255 if (Kind == MachO::DICE_KIND_DATA)
256 outs() << "\t@ KIND_DATA\n";
257 else
258 outs() << "\t@ data in code kind = " << Kind << "\n";
Kevin Enderby273ae012013-06-06 17:20:50 +0000259 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000260 case MachO::DICE_KIND_JUMP_TABLE8:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000261 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000262 dumpBytes(makeArrayRef(bytes, 1), outs());
Kevin Enderby273ae012013-06-06 17:20:50 +0000263 Value = bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000264 outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n";
265 Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000266 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000267 case MachO::DICE_KIND_JUMP_TABLE16:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000268 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000269 dumpBytes(makeArrayRef(bytes, 2), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000270 Value = bytes[1] << 8 | bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000271 outs() << "\t.short " << format("%5u", Value & 0xffff)
272 << "\t@ KIND_JUMP_TABLE16\n";
273 Size = 2;
Kevin Enderby273ae012013-06-06 17:20:50 +0000274 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000275 case MachO::DICE_KIND_JUMP_TABLE32:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000276 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
277 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000278 dumpBytes(makeArrayRef(bytes, 4), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000279 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000280 outs() << "\t.long " << Value;
281 if (Kind == MachO::DICE_KIND_JUMP_TABLE32)
282 outs() << "\t@ KIND_JUMP_TABLE32\n";
283 else
284 outs() << "\t@ KIND_ABS_JUMP_TABLE32\n";
285 Size = 4;
Kevin Enderby273ae012013-06-06 17:20:50 +0000286 break;
287 }
Kevin Enderby930fdc72014-11-06 19:00:13 +0000288 return Size;
Kevin Enderby273ae012013-06-06 17:20:50 +0000289}
290
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000291static void getSectionsAndSymbols(MachOObjectFile *MachOObj,
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000292 std::vector<SectionRef> &Sections,
293 std::vector<SymbolRef> &Symbols,
294 SmallVectorImpl<uint64_t> &FoundFns,
295 uint64_t &BaseSegmentAddress) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000296 for (const SymbolRef &Symbol : MachOObj->symbols()) {
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000297 Expected<StringRef> SymName = Symbol.getName();
298 if (!SymName) {
299 std::string Buf;
300 raw_string_ostream OS(Buf);
301 logAllUnhandledErrors(SymName.takeError(), OS, "");
302 OS.flush();
303 report_fatal_error(Buf);
304 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000305 if (!SymName->startswith("ltmp"))
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000306 Symbols.push_back(Symbol);
307 }
Owen Andersond9243c42011-10-17 21:37:35 +0000308
Alexey Samsonov48803e52014-03-13 14:37:36 +0000309 for (const SectionRef &Section : MachOObj->sections()) {
Owen Andersond9243c42011-10-17 21:37:35 +0000310 StringRef SectName;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000311 Section.getName(SectName);
312 Sections.push_back(Section);
Owen Andersond9243c42011-10-17 21:37:35 +0000313 }
314
Kevin Enderby273ae012013-06-06 17:20:50 +0000315 bool BaseSegmentAddressSet = false;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000316 for (const auto &Command : MachOObj->load_commands()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000317 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
Benjamin Kramer699128e2011-09-21 01:13:19 +0000318 // We found a function starts segment, parse the addresses for later
319 // consumption.
Charles Davis8bdfafd2013-09-01 04:28:48 +0000320 MachO::linkedit_data_command LLC =
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000321 MachOObj->getLinkeditDataLoadCommand(Command);
Benjamin Kramer699128e2011-09-21 01:13:19 +0000322
Charles Davis8bdfafd2013-09-01 04:28:48 +0000323 MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000324 } else if (Command.C.cmd == MachO::LC_SEGMENT) {
325 MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000326 StringRef SegName = SLC.segname;
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000327 if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
Kevin Enderby273ae012013-06-06 17:20:50 +0000328 BaseSegmentAddressSet = true;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000329 BaseSegmentAddress = SLC.vmaddr;
Kevin Enderby273ae012013-06-06 17:20:50 +0000330 }
331 }
Benjamin Kramer8a529dc2011-09-21 22:16:43 +0000332 }
Benjamin Kramer699128e2011-09-21 01:13:19 +0000333}
334
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000335static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose,
336 uint32_t n, uint32_t count,
337 uint32_t stride, uint64_t addr) {
338 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
339 uint32_t nindirectsyms = Dysymtab.nindirectsyms;
340 if (n > nindirectsyms)
341 outs() << " (entries start past the end of the indirect symbol "
342 "table) (reserved1 field greater than the table size)";
343 else if (n + count > nindirectsyms)
344 outs() << " (entries extends past the end of the indirect symbol "
345 "table)";
346 outs() << "\n";
347 uint32_t cputype = O->getHeader().cputype;
348 if (cputype & MachO::CPU_ARCH_ABI64)
349 outs() << "address index";
350 else
351 outs() << "address index";
352 if (verbose)
353 outs() << " name\n";
354 else
355 outs() << "\n";
356 for (uint32_t j = 0; j < count && n + j < nindirectsyms; j++) {
357 if (cputype & MachO::CPU_ARCH_ABI64)
358 outs() << format("0x%016" PRIx64, addr + j * stride) << " ";
359 else
Tim Northover43978372016-04-26 18:29:16 +0000360 outs() << format("0x%08" PRIx32, (uint32_t)addr + j * stride) << " ";
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000361 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
362 uint32_t indirect_symbol = O->getIndirectSymbolTableEntry(Dysymtab, n + j);
363 if (indirect_symbol == MachO::INDIRECT_SYMBOL_LOCAL) {
364 outs() << "LOCAL\n";
365 continue;
366 }
367 if (indirect_symbol ==
368 (MachO::INDIRECT_SYMBOL_LOCAL | MachO::INDIRECT_SYMBOL_ABS)) {
369 outs() << "LOCAL ABSOLUTE\n";
370 continue;
371 }
372 if (indirect_symbol == MachO::INDIRECT_SYMBOL_ABS) {
373 outs() << "ABSOLUTE\n";
374 continue;
375 }
376 outs() << format("%5u ", indirect_symbol);
Kevin Enderbyf0640752015-03-13 17:56:32 +0000377 if (verbose) {
378 MachO::symtab_command Symtab = O->getSymtabLoadCommand();
379 if (indirect_symbol < Symtab.nsyms) {
380 symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol);
381 SymbolRef Symbol = *Sym;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000382 Expected<StringRef> SymName = Symbol.getName();
383 if (!SymName) {
384 std::string Buf;
385 raw_string_ostream OS(Buf);
386 logAllUnhandledErrors(SymName.takeError(), OS, "");
387 OS.flush();
388 report_fatal_error(Buf);
389 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000390 outs() << *SymName;
Kevin Enderbyf0640752015-03-13 17:56:32 +0000391 } else {
392 outs() << "?";
393 }
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000394 }
395 outs() << "\n";
396 }
397}
398
399static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) {
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000400 for (const auto &Load : O->load_commands()) {
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000401 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
402 MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
403 for (unsigned J = 0; J < Seg.nsects; ++J) {
404 MachO::section_64 Sec = O->getSection64(Load, J);
405 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
406 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
407 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
408 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
409 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
410 section_type == MachO::S_SYMBOL_STUBS) {
411 uint32_t stride;
412 if (section_type == MachO::S_SYMBOL_STUBS)
413 stride = Sec.reserved2;
414 else
415 stride = 8;
416 if (stride == 0) {
417 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
418 << Sec.sectname << ") "
419 << "(size of stubs in reserved2 field is zero)\n";
420 continue;
421 }
422 uint32_t count = Sec.size / stride;
423 outs() << "Indirect symbols for (" << Sec.segname << ","
424 << Sec.sectname << ") " << count << " entries";
425 uint32_t n = Sec.reserved1;
426 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
427 }
428 }
429 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
430 MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
431 for (unsigned J = 0; J < Seg.nsects; ++J) {
432 MachO::section Sec = O->getSection(Load, J);
433 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
434 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
435 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
436 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
437 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
438 section_type == MachO::S_SYMBOL_STUBS) {
439 uint32_t stride;
440 if (section_type == MachO::S_SYMBOL_STUBS)
441 stride = Sec.reserved2;
442 else
443 stride = 4;
444 if (stride == 0) {
445 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
446 << Sec.sectname << ") "
447 << "(size of stubs in reserved2 field is zero)\n";
448 continue;
449 }
450 uint32_t count = Sec.size / stride;
451 outs() << "Indirect symbols for (" << Sec.segname << ","
452 << Sec.sectname << ") " << count << " entries";
453 uint32_t n = Sec.reserved1;
454 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
455 }
456 }
457 }
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000458 }
459}
460
Kevin Enderby69fe98d2015-01-23 18:52:17 +0000461static void PrintDataInCodeTable(MachOObjectFile *O, bool verbose) {
462 MachO::linkedit_data_command DIC = O->getDataInCodeLoadCommand();
463 uint32_t nentries = DIC.datasize / sizeof(struct MachO::data_in_code_entry);
464 outs() << "Data in code table (" << nentries << " entries)\n";
465 outs() << "offset length kind\n";
466 for (dice_iterator DI = O->begin_dices(), DE = O->end_dices(); DI != DE;
467 ++DI) {
468 uint32_t Offset;
469 DI->getOffset(Offset);
470 outs() << format("0x%08" PRIx32, Offset) << " ";
471 uint16_t Length;
472 DI->getLength(Length);
473 outs() << format("%6u", Length) << " ";
474 uint16_t Kind;
475 DI->getKind(Kind);
476 if (verbose) {
477 switch (Kind) {
478 case MachO::DICE_KIND_DATA:
479 outs() << "DATA";
480 break;
481 case MachO::DICE_KIND_JUMP_TABLE8:
482 outs() << "JUMP_TABLE8";
483 break;
484 case MachO::DICE_KIND_JUMP_TABLE16:
485 outs() << "JUMP_TABLE16";
486 break;
487 case MachO::DICE_KIND_JUMP_TABLE32:
488 outs() << "JUMP_TABLE32";
489 break;
490 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
491 outs() << "ABS_JUMP_TABLE32";
492 break;
493 default:
494 outs() << format("0x%04" PRIx32, Kind);
495 break;
496 }
497 } else
498 outs() << format("0x%04" PRIx32, Kind);
499 outs() << "\n";
500 }
501}
502
Kevin Enderby9a509442015-01-27 21:28:24 +0000503static void PrintLinkOptHints(MachOObjectFile *O) {
504 MachO::linkedit_data_command LohLC = O->getLinkOptHintsLoadCommand();
505 const char *loh = O->getData().substr(LohLC.dataoff, 1).data();
506 uint32_t nloh = LohLC.datasize;
507 outs() << "Linker optimiztion hints (" << nloh << " total bytes)\n";
508 for (uint32_t i = 0; i < nloh;) {
509 unsigned n;
510 uint64_t identifier = decodeULEB128((const uint8_t *)(loh + i), &n);
511 i += n;
512 outs() << " identifier " << identifier << " ";
513 if (i >= nloh)
514 return;
515 switch (identifier) {
516 case 1:
517 outs() << "AdrpAdrp\n";
518 break;
519 case 2:
520 outs() << "AdrpLdr\n";
521 break;
522 case 3:
523 outs() << "AdrpAddLdr\n";
524 break;
525 case 4:
526 outs() << "AdrpLdrGotLdr\n";
527 break;
528 case 5:
529 outs() << "AdrpAddStr\n";
530 break;
531 case 6:
532 outs() << "AdrpLdrGotStr\n";
533 break;
534 case 7:
535 outs() << "AdrpAdd\n";
536 break;
537 case 8:
538 outs() << "AdrpLdrGot\n";
539 break;
540 default:
541 outs() << "Unknown identifier value\n";
542 break;
543 }
544 uint64_t narguments = decodeULEB128((const uint8_t *)(loh + i), &n);
545 i += n;
546 outs() << " narguments " << narguments << "\n";
547 if (i >= nloh)
548 return;
549
550 for (uint32_t j = 0; j < narguments; j++) {
551 uint64_t value = decodeULEB128((const uint8_t *)(loh + i), &n);
552 i += n;
553 outs() << "\tvalue " << format("0x%" PRIx64, value) << "\n";
554 if (i >= nloh)
555 return;
556 }
557 }
558}
559
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000560static void PrintDylibs(MachOObjectFile *O, bool JustId) {
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000561 unsigned Index = 0;
562 for (const auto &Load : O->load_commands()) {
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000563 if ((JustId && Load.C.cmd == MachO::LC_ID_DYLIB) ||
564 (!JustId && (Load.C.cmd == MachO::LC_ID_DYLIB ||
565 Load.C.cmd == MachO::LC_LOAD_DYLIB ||
566 Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
567 Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
568 Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
569 Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB))) {
570 MachO::dylib_command dl = O->getDylibIDLoadCommand(Load);
571 if (dl.dylib.name < dl.cmdsize) {
572 const char *p = (const char *)(Load.Ptr) + dl.dylib.name;
573 if (JustId)
574 outs() << p << "\n";
575 else {
576 outs() << "\t" << p;
577 outs() << " (compatibility version "
578 << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
579 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
580 << (dl.dylib.compatibility_version & 0xff) << ",";
581 outs() << " current version "
582 << ((dl.dylib.current_version >> 16) & 0xffff) << "."
583 << ((dl.dylib.current_version >> 8) & 0xff) << "."
584 << (dl.dylib.current_version & 0xff) << ")\n";
585 }
586 } else {
587 outs() << "\tBad offset (" << dl.dylib.name << ") for name of ";
588 if (Load.C.cmd == MachO::LC_ID_DYLIB)
589 outs() << "LC_ID_DYLIB ";
590 else if (Load.C.cmd == MachO::LC_LOAD_DYLIB)
591 outs() << "LC_LOAD_DYLIB ";
592 else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB)
593 outs() << "LC_LOAD_WEAK_DYLIB ";
594 else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB)
595 outs() << "LC_LAZY_LOAD_DYLIB ";
596 else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB)
597 outs() << "LC_REEXPORT_DYLIB ";
598 else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
599 outs() << "LC_LOAD_UPWARD_DYLIB ";
600 else
601 outs() << "LC_??? ";
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000602 outs() << "command " << Index++ << "\n";
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000603 }
604 }
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000605 }
606}
607
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000608typedef DenseMap<uint64_t, StringRef> SymbolAddressMap;
609
610static void CreateSymbolAddressMap(MachOObjectFile *O,
611 SymbolAddressMap *AddrMap) {
612 // Create a map of symbol addresses to symbol names.
613 for (const SymbolRef &Symbol : O->symbols()) {
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000614 Expected<SymbolRef::Type> STOrErr = Symbol.getType();
615 if (!STOrErr) {
616 std::string Buf;
617 raw_string_ostream OS(Buf);
618 logAllUnhandledErrors(STOrErr.takeError(), OS, "");
619 OS.flush();
620 report_fatal_error(Buf);
621 }
Kevin Enderby5afbc1c2016-03-23 20:27:00 +0000622 SymbolRef::Type ST = *STOrErr;
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000623 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
624 ST == SymbolRef::ST_Other) {
Rafael Espindoladea00162015-07-03 17:44:18 +0000625 uint64_t Address = Symbol.getValue();
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000626 Expected<StringRef> SymNameOrErr = Symbol.getName();
627 if (!SymNameOrErr) {
628 std::string Buf;
629 raw_string_ostream OS(Buf);
630 logAllUnhandledErrors(SymNameOrErr.takeError(), OS, "");
631 OS.flush();
632 report_fatal_error(Buf);
633 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000634 StringRef SymName = *SymNameOrErr;
Kevin Enderby846c0002015-04-16 17:19:59 +0000635 if (!SymName.startswith(".objc"))
636 (*AddrMap)[Address] = SymName;
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000637 }
638 }
639}
640
641// GuessSymbolName is passed the address of what might be a symbol and a
642// pointer to the SymbolAddressMap. It returns the name of a symbol
643// with that address or nullptr if no symbol is found with that address.
644static const char *GuessSymbolName(uint64_t value, SymbolAddressMap *AddrMap) {
645 const char *SymbolName = nullptr;
646 // A DenseMap can't lookup up some values.
647 if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) {
648 StringRef name = AddrMap->lookup(value);
649 if (!name.empty())
650 SymbolName = name.data();
651 }
652 return SymbolName;
653}
654
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000655static void DumpCstringChar(const char c) {
656 char p[2];
657 p[0] = c;
658 p[1] = '\0';
659 outs().write_escaped(p);
660}
661
Kevin Enderby10ba0412015-02-04 21:38:42 +0000662static void DumpCstringSection(MachOObjectFile *O, const char *sect,
663 uint32_t sect_size, uint64_t sect_addr,
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000664 bool print_addresses) {
Kevin Enderby10ba0412015-02-04 21:38:42 +0000665 for (uint32_t i = 0; i < sect_size; i++) {
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000666 if (print_addresses) {
667 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000668 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000669 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000670 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000671 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000672 for (; i < sect_size && sect[i] != '\0'; i++)
673 DumpCstringChar(sect[i]);
Kevin Enderby10ba0412015-02-04 21:38:42 +0000674 if (i < sect_size && sect[i] == '\0')
675 outs() << "\n";
676 }
677}
678
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000679static void DumpLiteral4(uint32_t l, float f) {
680 outs() << format("0x%08" PRIx32, l);
681 if ((l & 0x7f800000) != 0x7f800000)
682 outs() << format(" (%.16e)\n", f);
683 else {
684 if (l == 0x7f800000)
685 outs() << " (+Infinity)\n";
686 else if (l == 0xff800000)
687 outs() << " (-Infinity)\n";
688 else if ((l & 0x00400000) == 0x00400000)
689 outs() << " (non-signaling Not-a-Number)\n";
690 else
691 outs() << " (signaling Not-a-Number)\n";
692 }
693}
694
695static void DumpLiteral4Section(MachOObjectFile *O, const char *sect,
696 uint32_t sect_size, uint64_t sect_addr,
697 bool print_addresses) {
698 for (uint32_t i = 0; i < sect_size; i += sizeof(float)) {
699 if (print_addresses) {
700 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000701 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000702 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000703 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000704 }
705 float f;
706 memcpy(&f, sect + i, sizeof(float));
707 if (O->isLittleEndian() != sys::IsLittleEndianHost)
708 sys::swapByteOrder(f);
709 uint32_t l;
710 memcpy(&l, sect + i, sizeof(uint32_t));
711 if (O->isLittleEndian() != sys::IsLittleEndianHost)
712 sys::swapByteOrder(l);
713 DumpLiteral4(l, f);
714 }
715}
716
717static void DumpLiteral8(MachOObjectFile *O, uint32_t l0, uint32_t l1,
718 double d) {
719 outs() << format("0x%08" PRIx32, l0) << " " << format("0x%08" PRIx32, l1);
720 uint32_t Hi, Lo;
Davide Italianob627d9f2015-12-12 21:50:11 +0000721 Hi = (O->isLittleEndian()) ? l1 : l0;
722 Lo = (O->isLittleEndian()) ? l0 : l1;
723
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000724 // Hi is the high word, so this is equivalent to if(isfinite(d))
725 if ((Hi & 0x7ff00000) != 0x7ff00000)
726 outs() << format(" (%.16e)\n", d);
727 else {
728 if (Hi == 0x7ff00000 && Lo == 0)
729 outs() << " (+Infinity)\n";
730 else if (Hi == 0xfff00000 && Lo == 0)
731 outs() << " (-Infinity)\n";
732 else if ((Hi & 0x00080000) == 0x00080000)
733 outs() << " (non-signaling Not-a-Number)\n";
734 else
735 outs() << " (signaling Not-a-Number)\n";
736 }
737}
738
739static void DumpLiteral8Section(MachOObjectFile *O, const char *sect,
740 uint32_t sect_size, uint64_t sect_addr,
741 bool print_addresses) {
742 for (uint32_t i = 0; i < sect_size; i += sizeof(double)) {
743 if (print_addresses) {
744 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000745 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000746 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000747 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000748 }
749 double d;
750 memcpy(&d, sect + i, sizeof(double));
751 if (O->isLittleEndian() != sys::IsLittleEndianHost)
752 sys::swapByteOrder(d);
753 uint32_t l0, l1;
754 memcpy(&l0, sect + i, sizeof(uint32_t));
755 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
756 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
757 sys::swapByteOrder(l0);
758 sys::swapByteOrder(l1);
759 }
760 DumpLiteral8(O, l0, l1, d);
761 }
762}
763
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000764static void DumpLiteral16(uint32_t l0, uint32_t l1, uint32_t l2, uint32_t l3) {
765 outs() << format("0x%08" PRIx32, l0) << " ";
766 outs() << format("0x%08" PRIx32, l1) << " ";
767 outs() << format("0x%08" PRIx32, l2) << " ";
768 outs() << format("0x%08" PRIx32, l3) << "\n";
769}
770
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000771static void DumpLiteral16Section(MachOObjectFile *O, const char *sect,
772 uint32_t sect_size, uint64_t sect_addr,
773 bool print_addresses) {
774 for (uint32_t i = 0; i < sect_size; i += 16) {
775 if (print_addresses) {
776 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000777 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000778 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000779 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000780 }
781 uint32_t l0, l1, l2, l3;
782 memcpy(&l0, sect + i, sizeof(uint32_t));
783 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
784 memcpy(&l2, sect + i + 2 * sizeof(uint32_t), sizeof(uint32_t));
785 memcpy(&l3, sect + i + 3 * sizeof(uint32_t), sizeof(uint32_t));
786 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
787 sys::swapByteOrder(l0);
788 sys::swapByteOrder(l1);
789 sys::swapByteOrder(l2);
790 sys::swapByteOrder(l3);
791 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000792 DumpLiteral16(l0, l1, l2, l3);
793 }
794}
795
796static void DumpLiteralPointerSection(MachOObjectFile *O,
797 const SectionRef &Section,
798 const char *sect, uint32_t sect_size,
799 uint64_t sect_addr,
800 bool print_addresses) {
801 // Collect the literal sections in this Mach-O file.
802 std::vector<SectionRef> LiteralSections;
803 for (const SectionRef &Section : O->sections()) {
804 DataRefImpl Ref = Section.getRawDataRefImpl();
805 uint32_t section_type;
806 if (O->is64Bit()) {
807 const MachO::section_64 Sec = O->getSection64(Ref);
808 section_type = Sec.flags & MachO::SECTION_TYPE;
809 } else {
810 const MachO::section Sec = O->getSection(Ref);
811 section_type = Sec.flags & MachO::SECTION_TYPE;
812 }
813 if (section_type == MachO::S_CSTRING_LITERALS ||
814 section_type == MachO::S_4BYTE_LITERALS ||
815 section_type == MachO::S_8BYTE_LITERALS ||
816 section_type == MachO::S_16BYTE_LITERALS)
817 LiteralSections.push_back(Section);
818 }
819
820 // Set the size of the literal pointer.
821 uint32_t lp_size = O->is64Bit() ? 8 : 4;
822
Eric Christopher572e03a2015-06-19 01:53:21 +0000823 // Collect the external relocation symbols for the literal pointers.
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000824 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
825 for (const RelocationRef &Reloc : Section.relocations()) {
826 DataRefImpl Rel;
827 MachO::any_relocation_info RE;
828 bool isExtern = false;
829 Rel = Reloc.getRawDataRefImpl();
830 RE = O->getRelocation(Rel);
831 isExtern = O->getPlainRelocationExternal(RE);
832 if (isExtern) {
Rafael Espindola96d071c2015-06-29 23:29:12 +0000833 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000834 symbol_iterator RelocSym = Reloc.getSymbol();
835 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
836 }
837 }
838 array_pod_sort(Relocs.begin(), Relocs.end());
839
840 // Dump each literal pointer.
841 for (uint32_t i = 0; i < sect_size; i += lp_size) {
842 if (print_addresses) {
843 if (O->is64Bit())
844 outs() << format("%016" PRIx64, sect_addr + i) << " ";
845 else
846 outs() << format("%08" PRIx64, sect_addr + i) << " ";
847 }
848 uint64_t lp;
849 if (O->is64Bit()) {
850 memcpy(&lp, sect + i, sizeof(uint64_t));
851 if (O->isLittleEndian() != sys::IsLittleEndianHost)
852 sys::swapByteOrder(lp);
853 } else {
854 uint32_t li;
855 memcpy(&li, sect + i, sizeof(uint32_t));
856 if (O->isLittleEndian() != sys::IsLittleEndianHost)
857 sys::swapByteOrder(li);
858 lp = li;
859 }
860
861 // First look for an external relocation entry for this literal pointer.
David Majnemer42531262016-08-12 03:55:06 +0000862 auto Reloc = find_if(Relocs, [&](const std::pair<uint64_t, SymbolRef> &P) {
863 return P.first == i;
864 });
David Blaikie33dd45d02015-03-23 18:39:02 +0000865 if (Reloc != Relocs.end()) {
866 symbol_iterator RelocSym = Reloc->second;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000867 Expected<StringRef> SymName = RelocSym->getName();
868 if (!SymName) {
869 std::string Buf;
870 raw_string_ostream OS(Buf);
871 logAllUnhandledErrors(SymName.takeError(), OS, "");
872 OS.flush();
873 report_fatal_error(Buf);
874 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000875 outs() << "external relocation entry for symbol:" << *SymName << "\n";
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000876 continue;
David Blaikie33dd45d02015-03-23 18:39:02 +0000877 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000878
879 // For local references see what the section the literal pointer points to.
David Majnemer562e8292016-08-12 00:18:03 +0000880 auto Sect = find_if(LiteralSections, [&](const SectionRef &R) {
881 return lp >= R.getAddress() && lp < R.getAddress() + R.getSize();
882 });
David Blaikie33dd45d02015-03-23 18:39:02 +0000883 if (Sect == LiteralSections.end()) {
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000884 outs() << format("0x%" PRIx64, lp) << " (not in a literal section)\n";
David Blaikie33dd45d02015-03-23 18:39:02 +0000885 continue;
886 }
887
888 uint64_t SectAddress = Sect->getAddress();
889 uint64_t SectSize = Sect->getSize();
890
891 StringRef SectName;
892 Sect->getName(SectName);
893 DataRefImpl Ref = Sect->getRawDataRefImpl();
894 StringRef SegmentName = O->getSectionFinalSegmentName(Ref);
895 outs() << SegmentName << ":" << SectName << ":";
896
897 uint32_t section_type;
898 if (O->is64Bit()) {
899 const MachO::section_64 Sec = O->getSection64(Ref);
900 section_type = Sec.flags & MachO::SECTION_TYPE;
901 } else {
902 const MachO::section Sec = O->getSection(Ref);
903 section_type = Sec.flags & MachO::SECTION_TYPE;
904 }
905
906 StringRef BytesStr;
907 Sect->getContents(BytesStr);
908 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
909
910 switch (section_type) {
911 case MachO::S_CSTRING_LITERALS:
912 for (uint64_t i = lp - SectAddress; i < SectSize && Contents[i] != '\0';
913 i++) {
914 DumpCstringChar(Contents[i]);
915 }
916 outs() << "\n";
917 break;
918 case MachO::S_4BYTE_LITERALS:
919 float f;
920 memcpy(&f, Contents + (lp - SectAddress), sizeof(float));
921 uint32_t l;
922 memcpy(&l, Contents + (lp - SectAddress), sizeof(uint32_t));
923 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
924 sys::swapByteOrder(f);
925 sys::swapByteOrder(l);
926 }
927 DumpLiteral4(l, f);
928 break;
929 case MachO::S_8BYTE_LITERALS: {
930 double d;
931 memcpy(&d, Contents + (lp - SectAddress), sizeof(double));
932 uint32_t l0, l1;
933 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
934 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
935 sizeof(uint32_t));
936 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
937 sys::swapByteOrder(f);
938 sys::swapByteOrder(l0);
939 sys::swapByteOrder(l1);
940 }
941 DumpLiteral8(O, l0, l1, d);
942 break;
943 }
944 case MachO::S_16BYTE_LITERALS: {
945 uint32_t l0, l1, l2, l3;
946 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
947 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
948 sizeof(uint32_t));
949 memcpy(&l2, Contents + (lp - SectAddress) + 2 * sizeof(uint32_t),
950 sizeof(uint32_t));
951 memcpy(&l3, Contents + (lp - SectAddress) + 3 * sizeof(uint32_t),
952 sizeof(uint32_t));
953 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
954 sys::swapByteOrder(l0);
955 sys::swapByteOrder(l1);
956 sys::swapByteOrder(l2);
957 sys::swapByteOrder(l3);
958 }
959 DumpLiteral16(l0, l1, l2, l3);
960 break;
961 }
962 }
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000963 }
964}
965
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000966static void DumpInitTermPointerSection(MachOObjectFile *O, const char *sect,
967 uint32_t sect_size, uint64_t sect_addr,
968 SymbolAddressMap *AddrMap,
969 bool verbose) {
970 uint32_t stride;
Davide Italiano3eb47e22015-12-15 23:14:21 +0000971 stride = (O->is64Bit()) ? sizeof(uint64_t) : sizeof(uint32_t);
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000972 for (uint32_t i = 0; i < sect_size; i += stride) {
973 const char *SymbolName = nullptr;
974 if (O->is64Bit()) {
975 outs() << format("0x%016" PRIx64, sect_addr + i * stride) << " ";
976 uint64_t pointer_value;
977 memcpy(&pointer_value, sect + i, stride);
978 if (O->isLittleEndian() != sys::IsLittleEndianHost)
979 sys::swapByteOrder(pointer_value);
980 outs() << format("0x%016" PRIx64, pointer_value);
981 if (verbose)
982 SymbolName = GuessSymbolName(pointer_value, AddrMap);
983 } else {
984 outs() << format("0x%08" PRIx64, sect_addr + i * stride) << " ";
985 uint32_t pointer_value;
986 memcpy(&pointer_value, sect + i, stride);
987 if (O->isLittleEndian() != sys::IsLittleEndianHost)
988 sys::swapByteOrder(pointer_value);
989 outs() << format("0x%08" PRIx32, pointer_value);
990 if (verbose)
991 SymbolName = GuessSymbolName(pointer_value, AddrMap);
992 }
993 if (SymbolName)
994 outs() << " " << SymbolName;
995 outs() << "\n";
996 }
997}
998
999static void DumpRawSectionContents(MachOObjectFile *O, const char *sect,
1000 uint32_t size, uint64_t addr) {
1001 uint32_t cputype = O->getHeader().cputype;
1002 if (cputype == MachO::CPU_TYPE_I386 || cputype == MachO::CPU_TYPE_X86_64) {
1003 uint32_t j;
1004 for (uint32_t i = 0; i < size; i += j, addr += j) {
1005 if (O->is64Bit())
1006 outs() << format("%016" PRIx64, addr) << "\t";
1007 else
Kevin Enderbyf0640752015-03-13 17:56:32 +00001008 outs() << format("%08" PRIx64, addr) << "\t";
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001009 for (j = 0; j < 16 && i + j < size; j++) {
1010 uint8_t byte_word = *(sect + i + j);
1011 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
1012 }
1013 outs() << "\n";
1014 }
1015 } else {
1016 uint32_t j;
1017 for (uint32_t i = 0; i < size; i += j, addr += j) {
1018 if (O->is64Bit())
1019 outs() << format("%016" PRIx64, addr) << "\t";
1020 else
Kevin Enderbyc4930852016-04-27 22:36:18 +00001021 outs() << format("%08" PRIx64, addr) << "\t";
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001022 for (j = 0; j < 4 * sizeof(int32_t) && i + j < size;
1023 j += sizeof(int32_t)) {
Kevin Enderby8eccdad2016-04-27 23:43:00 +00001024 if (i + j + sizeof(int32_t) <= size) {
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001025 uint32_t long_word;
1026 memcpy(&long_word, sect + i + j, sizeof(int32_t));
1027 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1028 sys::swapByteOrder(long_word);
1029 outs() << format("%08" PRIx32, long_word) << " ";
1030 } else {
1031 for (uint32_t k = 0; i + j + k < size; k++) {
Kevin Enderby8eccdad2016-04-27 23:43:00 +00001032 uint8_t byte_word = *(sect + i + j + k);
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001033 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
1034 }
1035 }
1036 }
1037 outs() << "\n";
1038 }
1039 }
1040}
1041
Kevin Enderby95df54c2015-02-04 01:01:38 +00001042static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
1043 StringRef DisSegName, StringRef DisSectName);
Kevin Enderby4ad9bde2015-04-16 22:33:20 +00001044static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
1045 uint32_t size, uint32_t addr);
Kevin Enderby9873e2c2016-05-23 21:34:12 +00001046#ifdef HAVE_LIBXAR
1047static void DumpBitcodeSection(MachOObjectFile *O, const char *sect,
1048 uint32_t size, bool verbose,
1049 bool PrintXarHeader, bool PrintXarFileHeaders,
1050 std::string XarMemberName);
1051#endif // defined(HAVE_LIBXAR)
Kevin Enderby95df54c2015-02-04 01:01:38 +00001052
1053static void DumpSectionContents(StringRef Filename, MachOObjectFile *O,
1054 bool verbose) {
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001055 SymbolAddressMap AddrMap;
1056 if (verbose)
1057 CreateSymbolAddressMap(O, &AddrMap);
1058
Colin LeMahieufcc32762015-07-29 19:08:10 +00001059 for (unsigned i = 0; i < FilterSections.size(); ++i) {
1060 StringRef DumpSection = FilterSections[i];
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001061 std::pair<StringRef, StringRef> DumpSegSectName;
1062 DumpSegSectName = DumpSection.split(',');
1063 StringRef DumpSegName, DumpSectName;
1064 if (DumpSegSectName.second.size()) {
1065 DumpSegName = DumpSegSectName.first;
1066 DumpSectName = DumpSegSectName.second;
1067 } else {
1068 DumpSegName = "";
1069 DumpSectName = DumpSegSectName.first;
1070 }
1071 for (const SectionRef &Section : O->sections()) {
1072 StringRef SectName;
1073 Section.getName(SectName);
1074 DataRefImpl Ref = Section.getRawDataRefImpl();
1075 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1076 if ((DumpSegName.empty() || SegName == DumpSegName) &&
1077 (SectName == DumpSectName)) {
Adrian Prantlc2401dd2015-03-27 17:31:15 +00001078
Kevin Enderby95df54c2015-02-04 01:01:38 +00001079 uint32_t section_flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001080 if (O->is64Bit()) {
1081 const MachO::section_64 Sec = O->getSection64(Ref);
Kevin Enderby95df54c2015-02-04 01:01:38 +00001082 section_flags = Sec.flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001083
1084 } else {
1085 const MachO::section Sec = O->getSection(Ref);
Kevin Enderby95df54c2015-02-04 01:01:38 +00001086 section_flags = Sec.flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001087 }
Kevin Enderby95df54c2015-02-04 01:01:38 +00001088 uint32_t section_type = section_flags & MachO::SECTION_TYPE;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001089
1090 StringRef BytesStr;
1091 Section.getContents(BytesStr);
1092 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1093 uint32_t sect_size = BytesStr.size();
1094 uint64_t sect_addr = Section.getAddress();
1095
Adrian Prantlc2401dd2015-03-27 17:31:15 +00001096 outs() << "Contents of (" << SegName << "," << SectName
1097 << ") section\n";
1098
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001099 if (verbose) {
Kevin Enderby95df54c2015-02-04 01:01:38 +00001100 if ((section_flags & MachO::S_ATTR_PURE_INSTRUCTIONS) ||
1101 (section_flags & MachO::S_ATTR_SOME_INSTRUCTIONS)) {
1102 DisassembleMachO(Filename, O, SegName, SectName);
1103 continue;
1104 }
Kevin Enderbycd66be52015-03-11 22:06:32 +00001105 if (SegName == "__TEXT" && SectName == "__info_plist") {
1106 outs() << sect;
1107 continue;
1108 }
Kevin Enderby4ad9bde2015-04-16 22:33:20 +00001109 if (SegName == "__OBJC" && SectName == "__protocol") {
1110 DumpProtocolSection(O, sect, sect_size, sect_addr);
1111 continue;
1112 }
Kevin Enderby9873e2c2016-05-23 21:34:12 +00001113#ifdef HAVE_LIBXAR
1114 if (SegName == "__LLVM" && SectName == "__bundle") {
1115 DumpBitcodeSection(O, sect, sect_size, verbose, !NoSymbolicOperands,
1116 ArchiveHeaders, "");
1117 continue;
1118 }
1119#endif // defined(HAVE_LIBXAR)
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001120 switch (section_type) {
1121 case MachO::S_REGULAR:
1122 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1123 break;
1124 case MachO::S_ZEROFILL:
1125 outs() << "zerofill section and has no contents in the file\n";
1126 break;
Kevin Enderby10ba0412015-02-04 21:38:42 +00001127 case MachO::S_CSTRING_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001128 DumpCstringSection(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby10ba0412015-02-04 21:38:42 +00001129 break;
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001130 case MachO::S_4BYTE_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001131 DumpLiteral4Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001132 break;
1133 case MachO::S_8BYTE_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001134 DumpLiteral8Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001135 break;
1136 case MachO::S_16BYTE_LITERALS:
Kevin Enderby0fc11822015-04-01 20:57:01 +00001137 DumpLiteral16Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1138 break;
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001139 case MachO::S_LITERAL_POINTERS:
1140 DumpLiteralPointerSection(O, Section, sect, sect_size, sect_addr,
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001141 !NoLeadingAddr);
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001142 break;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001143 case MachO::S_MOD_INIT_FUNC_POINTERS:
1144 case MachO::S_MOD_TERM_FUNC_POINTERS:
1145 DumpInitTermPointerSection(O, sect, sect_size, sect_addr, &AddrMap,
1146 verbose);
1147 break;
1148 default:
1149 outs() << "Unknown section type ("
1150 << format("0x%08" PRIx32, section_type) << ")\n";
1151 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1152 break;
1153 }
1154 } else {
1155 if (section_type == MachO::S_ZEROFILL)
1156 outs() << "zerofill section and has no contents in the file\n";
1157 else
1158 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1159 }
1160 }
1161 }
1162 }
1163}
1164
Kevin Enderbycd66be52015-03-11 22:06:32 +00001165static void DumpInfoPlistSectionContents(StringRef Filename,
1166 MachOObjectFile *O) {
1167 for (const SectionRef &Section : O->sections()) {
1168 StringRef SectName;
1169 Section.getName(SectName);
1170 DataRefImpl Ref = Section.getRawDataRefImpl();
1171 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1172 if (SegName == "__TEXT" && SectName == "__info_plist") {
1173 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
1174 StringRef BytesStr;
1175 Section.getContents(BytesStr);
1176 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1177 outs() << sect;
1178 return;
1179 }
1180 }
1181}
1182
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001183// checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file
1184// and if it is and there is a list of architecture flags is specified then
1185// check to make sure this Mach-O file is one of those architectures or all
1186// architectures were specified. If not then an error is generated and this
1187// routine returns false. Else it returns true.
1188static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
David Majnemer91160d82016-10-31 17:11:31 +00001189 auto *MachO = dyn_cast<MachOObjectFile>(O);
1190
1191 if (!MachO || ArchAll || ArchFlags.empty())
1192 return true;
1193
1194 MachO::mach_header H;
1195 MachO::mach_header_64 H_64;
1196 Triple T;
1197 if (MachO->is64Bit()) {
1198 H_64 = MachO->MachOObjectFile::getHeader64();
1199 T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype);
1200 } else {
1201 H = MachO->MachOObjectFile::getHeader();
1202 T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype);
1203 }
1204 if (none_of(ArchFlags, [&](const std::string &Name) {
1205 return Name == T.getArchName();
1206 })) {
1207 errs() << "llvm-objdump: " + Filename + ": No architecture specified.\n";
1208 return false;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001209 }
1210 return true;
1211}
1212
Kevin Enderby0fc11822015-04-01 20:57:01 +00001213static void printObjcMetaData(MachOObjectFile *O, bool verbose);
1214
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001215// ProcessMachO() is passed a single opened Mach-O file, which may be an
1216// archive member and or in a slice of a universal file. It prints the
1217// the file name and header info and then processes it according to the
1218// command line options.
1219static void ProcessMachO(StringRef Filename, MachOObjectFile *MachOOF,
1220 StringRef ArchiveMemberName = StringRef(),
1221 StringRef ArchitectureName = StringRef()) {
Kevin Enderby131d1772015-01-09 19:22:37 +00001222 // If we are doing some processing here on the Mach-O file print the header
1223 // info. And don't print it otherwise like in the case of printing the
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +00001224 // UniversalHeaders or ArchiveHeaders.
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001225 if (Disassemble || PrivateHeaders || ExportsTrie || Rebase || Bind || SymbolTable ||
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001226 LazyBind || WeakBind || IndirectSymbols || DataInCode || LinkOptHints ||
Colin LeMahieufcc32762015-07-29 19:08:10 +00001227 DylibsUsed || DylibId || ObjcMetaData || (FilterSections.size() != 0)) {
Kevin Enderby131d1772015-01-09 19:22:37 +00001228 outs() << Filename;
1229 if (!ArchiveMemberName.empty())
1230 outs() << '(' << ArchiveMemberName << ')';
1231 if (!ArchitectureName.empty())
1232 outs() << " (architecture " << ArchitectureName << ")";
1233 outs() << ":\n";
1234 }
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001235
1236 if (Disassemble)
Kevin Enderby95df54c2015-02-04 01:01:38 +00001237 DisassembleMachO(Filename, MachOOF, "__TEXT", "__text");
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +00001238 if (IndirectSymbols)
Kevin Enderbyf0640752015-03-13 17:56:32 +00001239 PrintIndirectSymbols(MachOOF, !NonVerbose);
Kevin Enderby69fe98d2015-01-23 18:52:17 +00001240 if (DataInCode)
Kevin Enderbyf0640752015-03-13 17:56:32 +00001241 PrintDataInCodeTable(MachOOF, !NonVerbose);
Kevin Enderby9a509442015-01-27 21:28:24 +00001242 if (LinkOptHints)
1243 PrintLinkOptHints(MachOOF);
Kevin Enderby98da6132015-01-20 21:47:46 +00001244 if (Relocations)
1245 PrintRelocations(MachOOF);
1246 if (SectionHeaders)
1247 PrintSectionHeaders(MachOOF);
1248 if (SectionContents)
1249 PrintSectionContents(MachOOF);
Colin LeMahieufcc32762015-07-29 19:08:10 +00001250 if (FilterSections.size() != 0)
Kevin Enderbyf0640752015-03-13 17:56:32 +00001251 DumpSectionContents(Filename, MachOOF, !NonVerbose);
Kevin Enderbycd66be52015-03-11 22:06:32 +00001252 if (InfoPlist)
1253 DumpInfoPlistSectionContents(Filename, MachOOF);
Kevin Enderbybc847fa2015-03-16 20:08:09 +00001254 if (DylibsUsed)
1255 PrintDylibs(MachOOF, false);
1256 if (DylibId)
1257 PrintDylibs(MachOOF, true);
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001258 if (SymbolTable) {
1259 StringRef ArchiveName = ArchiveMemberName == StringRef() ? "" : Filename;
Kevin Enderby9acb1092016-05-31 20:35:34 +00001260 PrintSymbolTable(MachOOF, ArchiveName, ArchitectureName);
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001261 }
Kevin Enderby98da6132015-01-20 21:47:46 +00001262 if (UnwindInfo)
1263 printMachOUnwindInfo(MachOOF);
Kevin Enderby0ae163f2016-01-13 00:25:36 +00001264 if (PrivateHeaders) {
1265 printMachOFileHeader(MachOOF);
1266 printMachOLoadCommands(MachOOF);
1267 }
1268 if (FirstPrivateHeader)
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001269 printMachOFileHeader(MachOOF);
Kevin Enderby0fc11822015-04-01 20:57:01 +00001270 if (ObjcMetaData)
1271 printObjcMetaData(MachOOF, !NonVerbose);
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001272 if (ExportsTrie)
1273 printExportsTrie(MachOOF);
1274 if (Rebase)
1275 printRebaseTable(MachOOF);
1276 if (Bind)
1277 printBindTable(MachOOF);
1278 if (LazyBind)
1279 printLazyBindTable(MachOOF);
1280 if (WeakBind)
1281 printWeakBindTable(MachOOF);
Igor Laevsky03a670c2016-01-26 15:09:42 +00001282
1283 if (DwarfDumpType != DIDT_Null) {
1284 std::unique_ptr<DIContext> DICtx(new DWARFContextInMemory(*MachOOF));
1285 // Dump the complete DWARF structure.
1286 DICtx->dump(outs(), DwarfDumpType, true /* DumpEH */);
1287 }
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001288}
1289
Kevin Enderby131d1772015-01-09 19:22:37 +00001290// printUnknownCPUType() helps print_fat_headers for unknown CPU's.
1291static void printUnknownCPUType(uint32_t cputype, uint32_t cpusubtype) {
1292 outs() << " cputype (" << cputype << ")\n";
1293 outs() << " cpusubtype (" << cpusubtype << ")\n";
1294}
1295
1296// printCPUType() helps print_fat_headers by printing the cputype and
1297// pusubtype (symbolically for the one's it knows about).
1298static void printCPUType(uint32_t cputype, uint32_t cpusubtype) {
1299 switch (cputype) {
1300 case MachO::CPU_TYPE_I386:
1301 switch (cpusubtype) {
1302 case MachO::CPU_SUBTYPE_I386_ALL:
1303 outs() << " cputype CPU_TYPE_I386\n";
1304 outs() << " cpusubtype CPU_SUBTYPE_I386_ALL\n";
1305 break;
1306 default:
1307 printUnknownCPUType(cputype, cpusubtype);
1308 break;
1309 }
1310 break;
1311 case MachO::CPU_TYPE_X86_64:
1312 switch (cpusubtype) {
1313 case MachO::CPU_SUBTYPE_X86_64_ALL:
1314 outs() << " cputype CPU_TYPE_X86_64\n";
1315 outs() << " cpusubtype CPU_SUBTYPE_X86_64_ALL\n";
1316 break;
1317 case MachO::CPU_SUBTYPE_X86_64_H:
1318 outs() << " cputype CPU_TYPE_X86_64\n";
1319 outs() << " cpusubtype CPU_SUBTYPE_X86_64_H\n";
1320 break;
1321 default:
1322 printUnknownCPUType(cputype, cpusubtype);
1323 break;
1324 }
1325 break;
1326 case MachO::CPU_TYPE_ARM:
1327 switch (cpusubtype) {
1328 case MachO::CPU_SUBTYPE_ARM_ALL:
1329 outs() << " cputype CPU_TYPE_ARM\n";
1330 outs() << " cpusubtype CPU_SUBTYPE_ARM_ALL\n";
1331 break;
1332 case MachO::CPU_SUBTYPE_ARM_V4T:
1333 outs() << " cputype CPU_TYPE_ARM\n";
1334 outs() << " cpusubtype CPU_SUBTYPE_ARM_V4T\n";
1335 break;
1336 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
1337 outs() << " cputype CPU_TYPE_ARM\n";
1338 outs() << " cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n";
1339 break;
1340 case MachO::CPU_SUBTYPE_ARM_XSCALE:
1341 outs() << " cputype CPU_TYPE_ARM\n";
1342 outs() << " cpusubtype CPU_SUBTYPE_ARM_XSCALE\n";
1343 break;
1344 case MachO::CPU_SUBTYPE_ARM_V6:
1345 outs() << " cputype CPU_TYPE_ARM\n";
1346 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6\n";
1347 break;
1348 case MachO::CPU_SUBTYPE_ARM_V6M:
1349 outs() << " cputype CPU_TYPE_ARM\n";
1350 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6M\n";
1351 break;
1352 case MachO::CPU_SUBTYPE_ARM_V7:
1353 outs() << " cputype CPU_TYPE_ARM\n";
1354 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7\n";
1355 break;
1356 case MachO::CPU_SUBTYPE_ARM_V7EM:
1357 outs() << " cputype CPU_TYPE_ARM\n";
1358 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7EM\n";
1359 break;
1360 case MachO::CPU_SUBTYPE_ARM_V7K:
1361 outs() << " cputype CPU_TYPE_ARM\n";
1362 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7K\n";
1363 break;
1364 case MachO::CPU_SUBTYPE_ARM_V7M:
1365 outs() << " cputype CPU_TYPE_ARM\n";
1366 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7M\n";
1367 break;
1368 case MachO::CPU_SUBTYPE_ARM_V7S:
1369 outs() << " cputype CPU_TYPE_ARM\n";
1370 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7S\n";
1371 break;
1372 default:
1373 printUnknownCPUType(cputype, cpusubtype);
1374 break;
1375 }
1376 break;
1377 case MachO::CPU_TYPE_ARM64:
1378 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1379 case MachO::CPU_SUBTYPE_ARM64_ALL:
1380 outs() << " cputype CPU_TYPE_ARM64\n";
1381 outs() << " cpusubtype CPU_SUBTYPE_ARM64_ALL\n";
1382 break;
1383 default:
1384 printUnknownCPUType(cputype, cpusubtype);
1385 break;
1386 }
1387 break;
1388 default:
1389 printUnknownCPUType(cputype, cpusubtype);
1390 break;
1391 }
1392}
1393
1394static void printMachOUniversalHeaders(const object::MachOUniversalBinary *UB,
1395 bool verbose) {
1396 outs() << "Fat headers\n";
Kevin Enderby606a3382016-06-21 21:55:01 +00001397 if (verbose) {
1398 if (UB->getMagic() == MachO::FAT_MAGIC)
1399 outs() << "fat_magic FAT_MAGIC\n";
1400 else // UB->getMagic() == MachO::FAT_MAGIC_64
1401 outs() << "fat_magic FAT_MAGIC_64\n";
1402 } else
Kevin Enderby131d1772015-01-09 19:22:37 +00001403 outs() << "fat_magic " << format("0x%" PRIx32, MachO::FAT_MAGIC) << "\n";
1404
1405 uint32_t nfat_arch = UB->getNumberOfObjects();
1406 StringRef Buf = UB->getData();
1407 uint64_t size = Buf.size();
1408 uint64_t big_size = sizeof(struct MachO::fat_header) +
1409 nfat_arch * sizeof(struct MachO::fat_arch);
1410 outs() << "nfat_arch " << UB->getNumberOfObjects();
1411 if (nfat_arch == 0)
1412 outs() << " (malformed, contains zero architecture types)\n";
1413 else if (big_size > size)
1414 outs() << " (malformed, architectures past end of file)\n";
1415 else
1416 outs() << "\n";
1417
1418 for (uint32_t i = 0; i < nfat_arch; ++i) {
1419 MachOUniversalBinary::ObjectForArch OFA(UB, i);
1420 uint32_t cputype = OFA.getCPUType();
1421 uint32_t cpusubtype = OFA.getCPUSubType();
1422 outs() << "architecture ";
1423 for (uint32_t j = 0; i != 0 && j <= i - 1; j++) {
1424 MachOUniversalBinary::ObjectForArch other_OFA(UB, j);
1425 uint32_t other_cputype = other_OFA.getCPUType();
1426 uint32_t other_cpusubtype = other_OFA.getCPUSubType();
Kevin Enderby0512bd72015-01-09 21:55:03 +00001427 if (cputype != 0 && cpusubtype != 0 && cputype == other_cputype &&
Kevin Enderby131d1772015-01-09 19:22:37 +00001428 (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) ==
Kevin Enderby0512bd72015-01-09 21:55:03 +00001429 (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK)) {
Kevin Enderby131d1772015-01-09 19:22:37 +00001430 outs() << "(illegal duplicate architecture) ";
1431 break;
Kevin Enderby0512bd72015-01-09 21:55:03 +00001432 }
Kevin Enderby131d1772015-01-09 19:22:37 +00001433 }
1434 if (verbose) {
1435 outs() << OFA.getArchTypeName() << "\n";
1436 printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1437 } else {
1438 outs() << i << "\n";
1439 outs() << " cputype " << cputype << "\n";
1440 outs() << " cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK)
1441 << "\n";
1442 }
1443 if (verbose &&
1444 (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64)
1445 outs() << " capabilities CPU_SUBTYPE_LIB64\n";
1446 else
1447 outs() << " capabilities "
1448 << format("0x%" PRIx32,
1449 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n";
1450 outs() << " offset " << OFA.getOffset();
1451 if (OFA.getOffset() > size)
1452 outs() << " (past end of file)";
1453 if (OFA.getOffset() % (1 << OFA.getAlign()) != 0)
1454 outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")";
1455 outs() << "\n";
1456 outs() << " size " << OFA.getSize();
1457 big_size = OFA.getOffset() + OFA.getSize();
1458 if (big_size > size)
1459 outs() << " (past end of file)";
1460 outs() << "\n";
1461 outs() << " align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign())
1462 << ")\n";
1463 }
1464}
1465
Kevin Enderby6524bd82016-07-19 20:47:07 +00001466static void printArchiveChild(StringRef Filename, const Archive::Child &C,
1467 bool verbose, bool print_offset,
1468 StringRef ArchitectureName = StringRef()) {
Kevin Enderby13023a12015-01-15 23:19:11 +00001469 if (print_offset)
1470 outs() << C.getChildOffset() << "\t";
Vedant Kumar4031d9f2016-08-03 19:02:50 +00001471 Expected<sys::fs::perms> ModeOrErr = C.getAccessMode();
1472 if (!ModeOrErr)
1473 report_error(Filename, C, ModeOrErr.takeError(), ArchitectureName);
1474 sys::fs::perms Mode = ModeOrErr.get();
Kevin Enderby13023a12015-01-15 23:19:11 +00001475 if (verbose) {
1476 // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
1477 // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
1478 outs() << "-";
Davide Italianobb9a6cc2015-09-07 20:47:03 +00001479 outs() << ((Mode & sys::fs::owner_read) ? "r" : "-");
1480 outs() << ((Mode & sys::fs::owner_write) ? "w" : "-");
1481 outs() << ((Mode & sys::fs::owner_exe) ? "x" : "-");
1482 outs() << ((Mode & sys::fs::group_read) ? "r" : "-");
1483 outs() << ((Mode & sys::fs::group_write) ? "w" : "-");
1484 outs() << ((Mode & sys::fs::group_exe) ? "x" : "-");
1485 outs() << ((Mode & sys::fs::others_read) ? "r" : "-");
1486 outs() << ((Mode & sys::fs::others_write) ? "w" : "-");
1487 outs() << ((Mode & sys::fs::others_exe) ? "x" : "-");
Kevin Enderby13023a12015-01-15 23:19:11 +00001488 } else {
1489 outs() << format("0%o ", Mode);
1490 }
1491
Vedant Kumar4031d9f2016-08-03 19:02:50 +00001492 Expected<unsigned> UIDOrErr = C.getUID();
1493 if (!UIDOrErr)
1494 report_error(Filename, C, UIDOrErr.takeError(), ArchitectureName);
1495 unsigned UID = UIDOrErr.get();
Kevin Enderby13023a12015-01-15 23:19:11 +00001496 outs() << format("%3d/", UID);
Vedant Kumar4031d9f2016-08-03 19:02:50 +00001497 Expected<unsigned> GIDOrErr = C.getGID();
1498 if (!GIDOrErr)
1499 report_error(Filename, C, GIDOrErr.takeError(), ArchitectureName);
1500 unsigned GID = GIDOrErr.get();
Kevin Enderby13023a12015-01-15 23:19:11 +00001501 outs() << format("%-3d ", GID);
Kevin Enderby6524bd82016-07-19 20:47:07 +00001502 Expected<uint64_t> Size = C.getRawSize();
1503 if (!Size)
1504 report_error(Filename, C, Size.takeError(), ArchitectureName);
Kevin Enderby7a969422015-11-05 19:24:56 +00001505 outs() << format("%5" PRId64, Size.get()) << " ";
Kevin Enderby13023a12015-01-15 23:19:11 +00001506
1507 StringRef RawLastModified = C.getRawLastModified();
1508 if (verbose) {
1509 unsigned Seconds;
1510 if (RawLastModified.getAsInteger(10, Seconds))
Vedant Kumar4031d9f2016-08-03 19:02:50 +00001511 outs() << "(date: \"" << RawLastModified
1512 << "\" contains non-decimal chars) ";
Kevin Enderby13023a12015-01-15 23:19:11 +00001513 else {
1514 // Since cime(3) returns a 26 character string of the form:
1515 // "Sun Sep 16 01:03:52 1973\n\0"
1516 // just print 24 characters.
1517 time_t t = Seconds;
1518 outs() << format("%.24s ", ctime(&t));
1519 }
1520 } else {
1521 outs() << RawLastModified << " ";
1522 }
1523
1524 if (verbose) {
Kevin Enderbyf4586032016-07-29 17:44:13 +00001525 Expected<StringRef> NameOrErr = C.getName();
1526 if (!NameOrErr) {
1527 consumeError(NameOrErr.takeError());
1528 Expected<StringRef> NameOrErr = C.getRawName();
1529 if (!NameOrErr)
1530 report_error(Filename, C, NameOrErr.takeError(), ArchitectureName);
1531 StringRef RawName = NameOrErr.get();
Kevin Enderby13023a12015-01-15 23:19:11 +00001532 outs() << RawName << "\n";
1533 } else {
1534 StringRef Name = NameOrErr.get();
1535 outs() << Name << "\n";
1536 }
1537 } else {
Kevin Enderbyf4586032016-07-29 17:44:13 +00001538 Expected<StringRef> NameOrErr = C.getRawName();
1539 if (!NameOrErr)
1540 report_error(Filename, C, NameOrErr.takeError(), ArchitectureName);
1541 StringRef RawName = NameOrErr.get();
Kevin Enderby13023a12015-01-15 23:19:11 +00001542 outs() << RawName << "\n";
1543 }
1544}
1545
Kevin Enderby6524bd82016-07-19 20:47:07 +00001546static void printArchiveHeaders(StringRef Filename, Archive *A, bool verbose,
1547 bool print_offset,
1548 StringRef ArchitectureName = StringRef()) {
Lang Hamesfc209622016-07-14 02:24:01 +00001549 Error Err;
1550 for (const auto &C : A->children(Err, false))
Kevin Enderby6524bd82016-07-19 20:47:07 +00001551 printArchiveChild(Filename, C, verbose, print_offset, ArchitectureName);
1552
Lang Hamesfc209622016-07-14 02:24:01 +00001553 if (Err)
Kevin Enderby6524bd82016-07-19 20:47:07 +00001554 report_error(Filename, std::move(Err));
Kevin Enderby13023a12015-01-15 23:19:11 +00001555}
1556
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001557// ParseInputMachO() parses the named Mach-O file in Filename and handles the
1558// -arch flags selecting just those slices as specified by them and also parses
1559// archive files. Then for each individual Mach-O file ProcessMachO() is
1560// called to process the file based on the command line options.
1561void llvm::ParseInputMachO(StringRef Filename) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001562 // Check for -arch all and verifiy the -arch flags are valid.
1563 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1564 if (ArchFlags[i] == "all") {
1565 ArchAll = true;
1566 } else {
1567 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
1568 errs() << "llvm-objdump: Unknown architecture named '" + ArchFlags[i] +
1569 "'for the -arch option\n";
1570 return;
1571 }
1572 }
1573 }
1574
1575 // Attempt to open the binary.
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +00001576 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename);
1577 if (!BinaryOrErr)
1578 report_error(Filename, BinaryOrErr.takeError());
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001579 Binary &Bin = *BinaryOrErr.get().getBinary();
Kevin Enderby3f0ffab2014-12-03 22:29:40 +00001580
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001581 if (Archive *A = dyn_cast<Archive>(&Bin)) {
1582 outs() << "Archive : " << Filename << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001583 if (ArchiveHeaders)
Kevin Enderby6524bd82016-07-19 20:47:07 +00001584 printArchiveHeaders(Filename, A, !NonVerbose, ArchiveMemberOffsets);
1585
Lang Hamesfc209622016-07-14 02:24:01 +00001586 Error Err;
1587 for (auto &C : A->children(Err)) {
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001588 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1589 if (!ChildOrErr) {
1590 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
1591 report_error(Filename, C, std::move(E));
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001592 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001593 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001594 if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1595 if (!checkMachOAndArchFlags(O, Filename))
1596 return;
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001597 ProcessMachO(Filename, O, O->getFileName());
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001598 }
1599 }
Lang Hamesfc209622016-07-14 02:24:01 +00001600 if (Err)
1601 report_error(Filename, std::move(Err));
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001602 return;
1603 }
Kevin Enderby131d1772015-01-09 19:22:37 +00001604 if (UniversalHeaders) {
1605 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin))
Kevin Enderbyf0640752015-03-13 17:56:32 +00001606 printMachOUniversalHeaders(UB, !NonVerbose);
Kevin Enderby131d1772015-01-09 19:22:37 +00001607 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001608 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
1609 // If we have a list of architecture flags specified dump only those.
1610 if (!ArchAll && ArchFlags.size() != 0) {
1611 // Look for a slice in the universal binary that matches each ArchFlag.
1612 bool ArchFound;
1613 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1614 ArchFound = false;
1615 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1616 E = UB->end_objects();
1617 I != E; ++I) {
1618 if (ArchFlags[i] == I->getArchTypeName()) {
1619 ArchFound = true;
Kevin Enderby9acb1092016-05-31 20:35:34 +00001620 Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001621 I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001622 std::string ArchitectureName = "";
1623 if (ArchFlags.size() > 1)
1624 ArchitectureName = I->getArchTypeName();
1625 if (ObjOrErr) {
1626 ObjectFile &O = *ObjOrErr.get();
1627 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001628 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
Kevin Enderby9acb1092016-05-31 20:35:34 +00001629 } else if (auto E = isNotObjectErrorInvalidFileType(
1630 ObjOrErr.takeError())) {
1631 report_error(Filename, StringRef(), std::move(E),
1632 ArchitectureName);
1633 continue;
Kevin Enderby42398052016-06-28 23:16:13 +00001634 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001635 I->getAsArchive()) {
1636 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001637 outs() << "Archive : " << Filename;
1638 if (!ArchitectureName.empty())
1639 outs() << " (architecture " << ArchitectureName << ")";
1640 outs() << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001641 if (ArchiveHeaders)
Kevin Enderby6524bd82016-07-19 20:47:07 +00001642 printArchiveHeaders(Filename, A.get(), !NonVerbose,
1643 ArchiveMemberOffsets, ArchitectureName);
Lang Hamesfc209622016-07-14 02:24:01 +00001644 Error Err;
1645 for (auto &C : A->children(Err)) {
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001646 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1647 if (!ChildOrErr) {
1648 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
Kevin Enderby9acb1092016-05-31 20:35:34 +00001649 report_error(Filename, C, std::move(E), ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001650 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001651 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001652 if (MachOObjectFile *O =
1653 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001654 ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001655 }
Lang Hamesfc209622016-07-14 02:24:01 +00001656 if (Err)
1657 report_error(Filename, std::move(Err));
Kevin Enderby42398052016-06-28 23:16:13 +00001658 } else {
1659 consumeError(AOrErr.takeError());
1660 error("Mach-O universal file: " + Filename + " for " +
1661 "architecture " + StringRef(I->getArchTypeName()) +
1662 " is not a Mach-O file or an archive file");
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001663 }
1664 }
1665 }
1666 if (!ArchFound) {
1667 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1668 << "architecture: " + ArchFlags[i] + "\n";
1669 return;
1670 }
1671 }
1672 return;
1673 }
1674 // No architecture flags were specified so if this contains a slice that
1675 // matches the host architecture dump only that.
1676 if (!ArchAll) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001677 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1678 E = UB->end_objects();
1679 I != E; ++I) {
Kevin Enderby0512bd72015-01-09 21:55:03 +00001680 if (MachOObjectFile::getHostArch().getArchName() ==
1681 I->getArchTypeName()) {
Kevin Enderby9acb1092016-05-31 20:35:34 +00001682 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001683 std::string ArchiveName;
1684 ArchiveName.clear();
1685 if (ObjOrErr) {
1686 ObjectFile &O = *ObjOrErr.get();
1687 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001688 ProcessMachO(Filename, MachOOF);
Kevin Enderby9acb1092016-05-31 20:35:34 +00001689 } else if (auto E = isNotObjectErrorInvalidFileType(
1690 ObjOrErr.takeError())) {
1691 report_error(Filename, std::move(E));
1692 continue;
Kevin Enderby42398052016-06-28 23:16:13 +00001693 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001694 I->getAsArchive()) {
1695 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001696 outs() << "Archive : " << Filename << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001697 if (ArchiveHeaders)
Kevin Enderby6524bd82016-07-19 20:47:07 +00001698 printArchiveHeaders(Filename, A.get(), !NonVerbose,
1699 ArchiveMemberOffsets);
Lang Hamesfc209622016-07-14 02:24:01 +00001700 Error Err;
1701 for (auto &C : A->children(Err)) {
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001702 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1703 if (!ChildOrErr) {
1704 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
1705 report_error(Filename, C, std::move(E));
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001706 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001707 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001708 if (MachOObjectFile *O =
1709 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001710 ProcessMachO(Filename, O, O->getFileName());
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001711 }
Lang Hamesfc209622016-07-14 02:24:01 +00001712 if (Err)
1713 report_error(Filename, std::move(Err));
Kevin Enderby42398052016-06-28 23:16:13 +00001714 } else {
1715 consumeError(AOrErr.takeError());
1716 error("Mach-O universal file: " + Filename + " for architecture " +
1717 StringRef(I->getArchTypeName()) +
1718 " is not a Mach-O file or an archive file");
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001719 }
1720 return;
1721 }
1722 }
1723 }
1724 // Either all architectures have been specified or none have been specified
1725 // and this does not contain the host architecture so dump all the slices.
1726 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
1727 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1728 E = UB->end_objects();
1729 I != E; ++I) {
Kevin Enderby9acb1092016-05-31 20:35:34 +00001730 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001731 std::string ArchitectureName = "";
1732 if (moreThanOneArch)
1733 ArchitectureName = I->getArchTypeName();
1734 if (ObjOrErr) {
1735 ObjectFile &Obj = *ObjOrErr.get();
1736 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001737 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
Kevin Enderby9acb1092016-05-31 20:35:34 +00001738 } else if (auto E = isNotObjectErrorInvalidFileType(
1739 ObjOrErr.takeError())) {
1740 report_error(StringRef(), Filename, std::move(E), ArchitectureName);
1741 continue;
Kevin Enderby42398052016-06-28 23:16:13 +00001742 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
1743 I->getAsArchive()) {
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001744 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001745 outs() << "Archive : " << Filename;
1746 if (!ArchitectureName.empty())
1747 outs() << " (architecture " << ArchitectureName << ")";
1748 outs() << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001749 if (ArchiveHeaders)
Kevin Enderby6524bd82016-07-19 20:47:07 +00001750 printArchiveHeaders(Filename, A.get(), !NonVerbose,
1751 ArchiveMemberOffsets, ArchitectureName);
Lang Hamesfc209622016-07-14 02:24:01 +00001752 Error Err;
1753 for (auto &C : A->children(Err)) {
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001754 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1755 if (!ChildOrErr) {
1756 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
Kevin Enderby9acb1092016-05-31 20:35:34 +00001757 report_error(Filename, C, std::move(E), ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001758 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001759 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001760 if (MachOObjectFile *O =
1761 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1762 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001763 ProcessMachO(Filename, MachOOF, MachOOF->getFileName(),
1764 ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001765 }
1766 }
Lang Hamesfc209622016-07-14 02:24:01 +00001767 if (Err)
1768 report_error(Filename, std::move(Err));
Kevin Enderby42398052016-06-28 23:16:13 +00001769 } else {
1770 consumeError(AOrErr.takeError());
1771 error("Mach-O universal file: " + Filename + " for architecture " +
1772 StringRef(I->getArchTypeName()) +
1773 " is not a Mach-O file or an archive file");
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001774 }
1775 }
1776 return;
1777 }
1778 if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) {
1779 if (!checkMachOAndArchFlags(O, Filename))
1780 return;
1781 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O)) {
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001782 ProcessMachO(Filename, MachOOF);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001783 } else
1784 errs() << "llvm-objdump: '" << Filename << "': "
1785 << "Object is not a Mach-O file type.\n";
Davide Italiano25d84582016-01-13 04:11:36 +00001786 return;
1787 }
1788 llvm_unreachable("Input object can't be invalid at this point");
Rafael Espindola9b709252013-04-13 01:45:40 +00001789}
1790
Kevin Enderby6f326ce2014-10-23 19:37:31 +00001791typedef std::pair<uint64_t, const char *> BindInfoEntry;
1792typedef std::vector<BindInfoEntry> BindTable;
1793typedef BindTable::iterator bind_table_iterator;
Kevin Enderbybf246f52014-09-24 23:08:22 +00001794
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001795// The block of info used by the Symbolizer call backs.
1796struct DisassembleInfo {
1797 bool verbose;
1798 MachOObjectFile *O;
1799 SectionRef S;
Kevin Enderbybf246f52014-09-24 23:08:22 +00001800 SymbolAddressMap *AddrMap;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00001801 std::vector<SectionRef> *Sections;
1802 const char *class_name;
1803 const char *selector_name;
1804 char *method;
Kevin Enderby04bf6932014-10-28 23:39:46 +00001805 char *demangled_name;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00001806 uint64_t adrp_addr;
1807 uint32_t adrp_inst;
Kevin Enderby078be602014-10-23 19:53:12 +00001808 BindTable *bindtable;
Kevin Enderbyaac75382015-10-08 16:56:35 +00001809 uint32_t depth;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001810};
1811
1812// SymbolizerGetOpInfo() is the operand information call back function.
1813// This is called to get the symbolic information for operand(s) of an
1814// instruction when it is being done. This routine does this from
1815// the relocation information, symbol table, etc. That block of information
1816// is a pointer to the struct DisassembleInfo that was passed when the
1817// disassembler context was created and passed to back to here when
1818// called back by the disassembler for instruction operands that could have
1819// relocation information. The address of the instruction containing operand is
1820// at the Pc parameter. The immediate value the operand has is passed in
1821// op_info->Value and is at Offset past the start of the instruction and has a
1822// byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
1823// LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
1824// names and addends of the symbolic expression to add for the operand. The
1825// value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
1826// information is returned then this function returns 1 else it returns 0.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00001827static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
1828 uint64_t Size, int TagType, void *TagBuf) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001829 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
1830 struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00001831 uint64_t value = op_info->Value;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001832
1833 // Make sure all fields returned are zero if we don't set them.
1834 memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1));
1835 op_info->Value = value;
1836
1837 // If the TagType is not the value 1 which it code knows about or if no
1838 // verbose symbolic information is wanted then just return 0, indicating no
1839 // information is being returned.
David Blaikie33dd45d02015-03-23 18:39:02 +00001840 if (TagType != 1 || !info->verbose)
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001841 return 0;
1842
1843 unsigned int Arch = info->O->getArch();
1844 if (Arch == Triple::x86) {
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001845 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1846 return 0;
Kevin Enderbyd90a4172015-10-10 00:05:01 +00001847 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
1848 // TODO:
1849 // Search the external relocation entries of a fully linked image
1850 // (if any) for an entry that matches this segment offset.
1851 // uint32_t seg_offset = (Pc + Offset);
1852 return 0;
1853 }
1854 // In MH_OBJECT filetypes search the section's relocation entries (if any)
1855 // for an entry for this section offset.
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001856 uint32_t sect_addr = info->S.getAddress();
1857 uint32_t sect_offset = (Pc + Offset) - sect_addr;
1858 bool reloc_found = false;
1859 DataRefImpl Rel;
1860 MachO::any_relocation_info RE;
1861 bool isExtern = false;
1862 SymbolRef Symbol;
1863 bool r_scattered = false;
1864 uint32_t r_value, pair_r_value, r_type;
1865 for (const RelocationRef &Reloc : info->S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00001866 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001867 if (RelocOffset == sect_offset) {
1868 Rel = Reloc.getRawDataRefImpl();
1869 RE = info->O->getRelocation(Rel);
Kevin Enderby3eb73e12014-11-11 19:16:45 +00001870 r_type = info->O->getAnyRelocationType(RE);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001871 r_scattered = info->O->isRelocationScattered(RE);
1872 if (r_scattered) {
1873 r_value = info->O->getScatteredRelocationValue(RE);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001874 if (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1875 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
1876 DataRefImpl RelNext = Rel;
1877 info->O->moveRelocationNext(RelNext);
1878 MachO::any_relocation_info RENext;
1879 RENext = info->O->getRelocation(RelNext);
1880 if (info->O->isRelocationScattered(RENext))
Kevin Enderby930fdc72014-11-06 19:00:13 +00001881 pair_r_value = info->O->getScatteredRelocationValue(RENext);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001882 else
1883 return 0;
1884 }
1885 } else {
1886 isExtern = info->O->getPlainRelocationExternal(RE);
1887 if (isExtern) {
1888 symbol_iterator RelocSym = Reloc.getSymbol();
1889 Symbol = *RelocSym;
1890 }
1891 }
1892 reloc_found = true;
1893 break;
1894 }
1895 }
1896 if (reloc_found && isExtern) {
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00001897 Expected<StringRef> SymName = Symbol.getName();
1898 if (!SymName) {
1899 std::string Buf;
1900 raw_string_ostream OS(Buf);
1901 logAllUnhandledErrors(SymName.takeError(), OS, "");
1902 OS.flush();
1903 report_fatal_error(Buf);
1904 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001905 const char *name = SymName->data();
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001906 op_info->AddSymbol.Present = 1;
1907 op_info->AddSymbol.Name = name;
1908 // For i386 extern relocation entries the value in the instruction is
1909 // the offset from the symbol, and value is already set in op_info->Value.
1910 return 1;
1911 }
1912 if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1913 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) {
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001914 const char *add = GuessSymbolName(r_value, info->AddrMap);
1915 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001916 uint32_t offset = value - (r_value - pair_r_value);
1917 op_info->AddSymbol.Present = 1;
1918 if (add != nullptr)
1919 op_info->AddSymbol.Name = add;
1920 else
1921 op_info->AddSymbol.Value = r_value;
1922 op_info->SubtractSymbol.Present = 1;
1923 if (sub != nullptr)
1924 op_info->SubtractSymbol.Name = sub;
1925 else
1926 op_info->SubtractSymbol.Value = pair_r_value;
1927 op_info->Value = offset;
1928 return 1;
1929 }
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001930 return 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00001931 }
1932 if (Arch == Triple::x86_64) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001933 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1934 return 0;
Kevin Enderbyd90a4172015-10-10 00:05:01 +00001935 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
1936 // TODO:
1937 // Search the external relocation entries of a fully linked image
1938 // (if any) for an entry that matches this segment offset.
1939 // uint64_t seg_offset = (Pc + Offset);
1940 return 0;
1941 }
1942 // In MH_OBJECT filetypes search the section's relocation entries (if any)
1943 // for an entry for this section offset.
Rafael Espindola80291272014-10-08 15:28:58 +00001944 uint64_t sect_addr = info->S.getAddress();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001945 uint64_t sect_offset = (Pc + Offset) - sect_addr;
1946 bool reloc_found = false;
1947 DataRefImpl Rel;
1948 MachO::any_relocation_info RE;
1949 bool isExtern = false;
1950 SymbolRef Symbol;
1951 for (const RelocationRef &Reloc : info->S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00001952 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001953 if (RelocOffset == sect_offset) {
1954 Rel = Reloc.getRawDataRefImpl();
1955 RE = info->O->getRelocation(Rel);
1956 // NOTE: Scattered relocations don't exist on x86_64.
1957 isExtern = info->O->getPlainRelocationExternal(RE);
1958 if (isExtern) {
1959 symbol_iterator RelocSym = Reloc.getSymbol();
1960 Symbol = *RelocSym;
1961 }
1962 reloc_found = true;
1963 break;
1964 }
1965 }
1966 if (reloc_found && isExtern) {
1967 // The Value passed in will be adjusted by the Pc if the instruction
1968 // adds the Pc. But for x86_64 external relocation entries the Value
1969 // is the offset from the external symbol.
1970 if (info->O->getAnyRelocationPCRel(RE))
1971 op_info->Value -= Pc + Offset + Size;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00001972 Expected<StringRef> SymName = Symbol.getName();
1973 if (!SymName) {
1974 std::string Buf;
1975 raw_string_ostream OS(Buf);
1976 logAllUnhandledErrors(SymName.takeError(), OS, "");
1977 OS.flush();
1978 report_fatal_error(Buf);
1979 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001980 const char *name = SymName->data();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001981 unsigned Type = info->O->getAnyRelocationType(RE);
1982 if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
1983 DataRefImpl RelNext = Rel;
1984 info->O->moveRelocationNext(RelNext);
1985 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
1986 unsigned TypeNext = info->O->getAnyRelocationType(RENext);
1987 bool isExternNext = info->O->getPlainRelocationExternal(RENext);
1988 unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext);
1989 if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) {
1990 op_info->SubtractSymbol.Present = 1;
1991 op_info->SubtractSymbol.Name = name;
1992 symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
1993 Symbol = *RelocSymNext;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00001994 Expected<StringRef> SymNameNext = Symbol.getName();
1995 if (!SymNameNext) {
1996 std::string Buf;
1997 raw_string_ostream OS(Buf);
1998 logAllUnhandledErrors(SymNameNext.takeError(), OS, "");
1999 OS.flush();
2000 report_fatal_error(Buf);
2001 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002002 name = SymNameNext->data();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002003 }
2004 }
2005 // TODO: add the VariantKinds to op_info->VariantKind for relocation types
2006 // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
2007 op_info->AddSymbol.Present = 1;
2008 op_info->AddSymbol.Name = name;
2009 return 1;
2010 }
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002011 return 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00002012 }
2013 if (Arch == Triple::arm) {
Kevin Enderby930fdc72014-11-06 19:00:13 +00002014 if (Offset != 0 || (Size != 4 && Size != 2))
2015 return 0;
Kevin Enderbyd90a4172015-10-10 00:05:01 +00002016 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2017 // TODO:
2018 // Search the external relocation entries of a fully linked image
2019 // (if any) for an entry that matches this segment offset.
2020 // uint32_t seg_offset = (Pc + Offset);
2021 return 0;
2022 }
2023 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2024 // for an entry for this section offset.
Kevin Enderby930fdc72014-11-06 19:00:13 +00002025 uint32_t sect_addr = info->S.getAddress();
2026 uint32_t sect_offset = (Pc + Offset) - sect_addr;
Kevin Enderby930fdc72014-11-06 19:00:13 +00002027 DataRefImpl Rel;
2028 MachO::any_relocation_info RE;
2029 bool isExtern = false;
2030 SymbolRef Symbol;
2031 bool r_scattered = false;
2032 uint32_t r_value, pair_r_value, r_type, r_length, other_half;
David Blaikie33dd45d02015-03-23 18:39:02 +00002033 auto Reloc =
David Majnemer562e8292016-08-12 00:18:03 +00002034 find_if(info->S.relocations(), [&](const RelocationRef &Reloc) {
2035 uint64_t RelocOffset = Reloc.getOffset();
2036 return RelocOffset == sect_offset;
2037 });
David Blaikie33dd45d02015-03-23 18:39:02 +00002038
2039 if (Reloc == info->S.relocations().end())
2040 return 0;
2041
2042 Rel = Reloc->getRawDataRefImpl();
2043 RE = info->O->getRelocation(Rel);
2044 r_length = info->O->getAnyRelocationLength(RE);
2045 r_scattered = info->O->isRelocationScattered(RE);
2046 if (r_scattered) {
2047 r_value = info->O->getScatteredRelocationValue(RE);
2048 r_type = info->O->getScatteredRelocationType(RE);
2049 } else {
2050 r_type = info->O->getAnyRelocationType(RE);
2051 isExtern = info->O->getPlainRelocationExternal(RE);
2052 if (isExtern) {
2053 symbol_iterator RelocSym = Reloc->getSymbol();
2054 Symbol = *RelocSym;
Kevin Enderby930fdc72014-11-06 19:00:13 +00002055 }
2056 }
David Blaikie33dd45d02015-03-23 18:39:02 +00002057 if (r_type == MachO::ARM_RELOC_HALF ||
2058 r_type == MachO::ARM_RELOC_SECTDIFF ||
2059 r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
2060 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2061 DataRefImpl RelNext = Rel;
2062 info->O->moveRelocationNext(RelNext);
2063 MachO::any_relocation_info RENext;
2064 RENext = info->O->getRelocation(RelNext);
2065 other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff;
2066 if (info->O->isRelocationScattered(RENext))
2067 pair_r_value = info->O->getScatteredRelocationValue(RENext);
2068 }
2069
2070 if (isExtern) {
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002071 Expected<StringRef> SymName = Symbol.getName();
2072 if (!SymName) {
2073 std::string Buf;
2074 raw_string_ostream OS(Buf);
2075 logAllUnhandledErrors(SymName.takeError(), OS, "");
2076 OS.flush();
2077 report_fatal_error(Buf);
2078 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002079 const char *name = SymName->data();
Kevin Enderby930fdc72014-11-06 19:00:13 +00002080 op_info->AddSymbol.Present = 1;
2081 op_info->AddSymbol.Name = name;
Sylvestre Ledru648cced2015-02-05 17:00:23 +00002082 switch (r_type) {
2083 case MachO::ARM_RELOC_HALF:
2084 if ((r_length & 0x1) == 1) {
2085 op_info->Value = value << 16 | other_half;
2086 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2087 } else {
2088 op_info->Value = other_half << 16 | value;
2089 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Sylvestre Ledrufe0c7ad2015-02-05 16:35:44 +00002090 }
Sylvestre Ledru648cced2015-02-05 17:00:23 +00002091 break;
2092 default:
2093 break;
Kevin Enderby930fdc72014-11-06 19:00:13 +00002094 }
2095 return 1;
2096 }
2097 // If we have a branch that is not an external relocation entry then
2098 // return 0 so the code in tryAddingSymbolicOperand() can use the
2099 // SymbolLookUp call back with the branch target address to look up the
2100 // symbol and possiblity add an annotation for a symbol stub.
David Blaikie33dd45d02015-03-23 18:39:02 +00002101 if (isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 ||
2102 r_type == MachO::ARM_THUMB_RELOC_BR22))
Kevin Enderby930fdc72014-11-06 19:00:13 +00002103 return 0;
2104
2105 uint32_t offset = 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00002106 if (r_type == MachO::ARM_RELOC_HALF ||
2107 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2108 if ((r_length & 0x1) == 1)
2109 value = value << 16 | other_half;
2110 else
2111 value = other_half << 16 | value;
2112 }
2113 if (r_scattered && (r_type != MachO::ARM_RELOC_HALF &&
2114 r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) {
2115 offset = value - r_value;
2116 value = r_value;
Kevin Enderby930fdc72014-11-06 19:00:13 +00002117 }
2118
David Blaikie33dd45d02015-03-23 18:39:02 +00002119 if (r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
Kevin Enderby930fdc72014-11-06 19:00:13 +00002120 if ((r_length & 0x1) == 1)
2121 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2122 else
2123 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00002124 const char *add = GuessSymbolName(r_value, info->AddrMap);
2125 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
Kevin Enderby930fdc72014-11-06 19:00:13 +00002126 int32_t offset = value - (r_value - pair_r_value);
2127 op_info->AddSymbol.Present = 1;
2128 if (add != nullptr)
2129 op_info->AddSymbol.Name = add;
2130 else
2131 op_info->AddSymbol.Value = r_value;
2132 op_info->SubtractSymbol.Present = 1;
2133 if (sub != nullptr)
2134 op_info->SubtractSymbol.Name = sub;
2135 else
2136 op_info->SubtractSymbol.Value = pair_r_value;
2137 op_info->Value = offset;
2138 return 1;
2139 }
2140
Kevin Enderby930fdc72014-11-06 19:00:13 +00002141 op_info->AddSymbol.Present = 1;
2142 op_info->Value = offset;
David Blaikie33dd45d02015-03-23 18:39:02 +00002143 if (r_type == MachO::ARM_RELOC_HALF) {
2144 if ((r_length & 0x1) == 1)
2145 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2146 else
2147 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Kevin Enderby930fdc72014-11-06 19:00:13 +00002148 }
Kevin Enderbyf6d25852015-01-31 00:37:11 +00002149 const char *add = GuessSymbolName(value, info->AddrMap);
Kevin Enderby930fdc72014-11-06 19:00:13 +00002150 if (add != nullptr) {
2151 op_info->AddSymbol.Name = add;
2152 return 1;
2153 }
2154 op_info->AddSymbol.Value = value;
2155 return 1;
David Blaikie33dd45d02015-03-23 18:39:02 +00002156 }
2157 if (Arch == Triple::aarch64) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002158 if (Offset != 0 || Size != 4)
2159 return 0;
Kevin Enderbyd90a4172015-10-10 00:05:01 +00002160 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2161 // TODO:
2162 // Search the external relocation entries of a fully linked image
2163 // (if any) for an entry that matches this segment offset.
2164 // uint64_t seg_offset = (Pc + Offset);
2165 return 0;
2166 }
2167 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2168 // for an entry for this section offset.
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002169 uint64_t sect_addr = info->S.getAddress();
2170 uint64_t sect_offset = (Pc + Offset) - sect_addr;
David Blaikie33dd45d02015-03-23 18:39:02 +00002171 auto Reloc =
David Majnemer562e8292016-08-12 00:18:03 +00002172 find_if(info->S.relocations(), [&](const RelocationRef &Reloc) {
2173 uint64_t RelocOffset = Reloc.getOffset();
2174 return RelocOffset == sect_offset;
2175 });
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002176
David Blaikie33dd45d02015-03-23 18:39:02 +00002177 if (Reloc == info->S.relocations().end())
2178 return 0;
2179
2180 DataRefImpl Rel = Reloc->getRawDataRefImpl();
2181 MachO::any_relocation_info RE = info->O->getRelocation(Rel);
2182 uint32_t r_type = info->O->getAnyRelocationType(RE);
2183 if (r_type == MachO::ARM64_RELOC_ADDEND) {
2184 DataRefImpl RelNext = Rel;
2185 info->O->moveRelocationNext(RelNext);
2186 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2187 if (value == 0) {
2188 value = info->O->getPlainRelocationSymbolNum(RENext);
2189 op_info->Value = value;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002190 }
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002191 }
David Blaikie33dd45d02015-03-23 18:39:02 +00002192 // NOTE: Scattered relocations don't exist on arm64.
2193 if (!info->O->getPlainRelocationExternal(RE))
2194 return 0;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002195 Expected<StringRef> SymName = Reloc->getSymbol()->getName();
2196 if (!SymName) {
2197 std::string Buf;
2198 raw_string_ostream OS(Buf);
2199 logAllUnhandledErrors(SymName.takeError(), OS, "");
2200 OS.flush();
2201 report_fatal_error(Buf);
2202 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002203 const char *name = SymName->data();
David Blaikie33dd45d02015-03-23 18:39:02 +00002204 op_info->AddSymbol.Present = 1;
2205 op_info->AddSymbol.Name = name;
2206
2207 switch (r_type) {
2208 case MachO::ARM64_RELOC_PAGE21:
2209 /* @page */
2210 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE;
2211 break;
2212 case MachO::ARM64_RELOC_PAGEOFF12:
2213 /* @pageoff */
2214 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF;
2215 break;
2216 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
2217 /* @gotpage */
2218 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE;
2219 break;
2220 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
2221 /* @gotpageoff */
2222 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF;
2223 break;
2224 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
2225 /* @tvlppage is not implemented in llvm-mc */
2226 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP;
2227 break;
2228 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
2229 /* @tvlppageoff is not implemented in llvm-mc */
2230 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF;
2231 break;
2232 default:
2233 case MachO::ARM64_RELOC_BRANCH26:
2234 op_info->VariantKind = LLVMDisassembler_VariantKind_None;
2235 break;
2236 }
2237 return 1;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002238 }
David Blaikie33dd45d02015-03-23 18:39:02 +00002239 return 0;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002240}
2241
Kevin Enderbybf246f52014-09-24 23:08:22 +00002242// GuessCstringPointer is passed the address of what might be a pointer to a
2243// literal string in a cstring section. If that address is in a cstring section
2244// it returns a pointer to that string. Else it returns nullptr.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00002245static const char *GuessCstringPointer(uint64_t ReferenceValue,
2246 struct DisassembleInfo *info) {
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002247 for (const auto &Load : info->O->load_commands()) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00002248 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2249 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2250 for (unsigned J = 0; J < Seg.nsects; ++J) {
2251 MachO::section_64 Sec = info->O->getSection64(Load, J);
2252 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2253 if (section_type == MachO::S_CSTRING_LITERALS &&
2254 ReferenceValue >= Sec.addr &&
2255 ReferenceValue < Sec.addr + Sec.size) {
2256 uint64_t sect_offset = ReferenceValue - Sec.addr;
2257 uint64_t object_offset = Sec.offset + sect_offset;
2258 StringRef MachOContents = info->O->getData();
2259 uint64_t object_size = MachOContents.size();
2260 const char *object_addr = (const char *)MachOContents.data();
2261 if (object_offset < object_size) {
2262 const char *name = object_addr + object_offset;
2263 return name;
2264 } else {
2265 return nullptr;
2266 }
2267 }
2268 }
2269 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2270 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2271 for (unsigned J = 0; J < Seg.nsects; ++J) {
2272 MachO::section Sec = info->O->getSection(Load, J);
2273 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2274 if (section_type == MachO::S_CSTRING_LITERALS &&
2275 ReferenceValue >= Sec.addr &&
2276 ReferenceValue < Sec.addr + Sec.size) {
2277 uint64_t sect_offset = ReferenceValue - Sec.addr;
2278 uint64_t object_offset = Sec.offset + sect_offset;
2279 StringRef MachOContents = info->O->getData();
2280 uint64_t object_size = MachOContents.size();
2281 const char *object_addr = (const char *)MachOContents.data();
2282 if (object_offset < object_size) {
2283 const char *name = object_addr + object_offset;
2284 return name;
2285 } else {
2286 return nullptr;
2287 }
2288 }
2289 }
2290 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00002291 }
2292 return nullptr;
2293}
2294
Kevin Enderby85974882014-09-26 22:20:44 +00002295// GuessIndirectSymbol returns the name of the indirect symbol for the
2296// ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe
2297// an address of a symbol stub or a lazy or non-lazy pointer to associate the
2298// symbol name being referenced by the stub or pointer.
2299static const char *GuessIndirectSymbol(uint64_t ReferenceValue,
2300 struct DisassembleInfo *info) {
Kevin Enderby85974882014-09-26 22:20:44 +00002301 MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand();
2302 MachO::symtab_command Symtab = info->O->getSymtabLoadCommand();
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002303 for (const auto &Load : info->O->load_commands()) {
Kevin Enderby85974882014-09-26 22:20:44 +00002304 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2305 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2306 for (unsigned J = 0; J < Seg.nsects; ++J) {
2307 MachO::section_64 Sec = info->O->getSection64(Load, J);
2308 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2309 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2310 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2311 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2312 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2313 section_type == MachO::S_SYMBOL_STUBS) &&
2314 ReferenceValue >= Sec.addr &&
2315 ReferenceValue < Sec.addr + Sec.size) {
2316 uint32_t stride;
2317 if (section_type == MachO::S_SYMBOL_STUBS)
2318 stride = Sec.reserved2;
2319 else
2320 stride = 8;
2321 if (stride == 0)
2322 return nullptr;
2323 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2324 if (index < Dysymtab.nindirectsyms) {
2325 uint32_t indirect_symbol =
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002326 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
Kevin Enderby85974882014-09-26 22:20:44 +00002327 if (indirect_symbol < Symtab.nsyms) {
2328 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2329 SymbolRef Symbol = *Sym;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002330 Expected<StringRef> SymName = Symbol.getName();
2331 if (!SymName) {
2332 std::string Buf;
2333 raw_string_ostream OS(Buf);
2334 logAllUnhandledErrors(SymName.takeError(), OS, "");
2335 OS.flush();
2336 report_fatal_error(Buf);
2337 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002338 const char *name = SymName->data();
Kevin Enderby85974882014-09-26 22:20:44 +00002339 return name;
2340 }
2341 }
2342 }
2343 }
2344 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2345 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2346 for (unsigned J = 0; J < Seg.nsects; ++J) {
2347 MachO::section Sec = info->O->getSection(Load, J);
2348 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2349 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2350 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2351 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2352 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2353 section_type == MachO::S_SYMBOL_STUBS) &&
2354 ReferenceValue >= Sec.addr &&
2355 ReferenceValue < Sec.addr + Sec.size) {
2356 uint32_t stride;
2357 if (section_type == MachO::S_SYMBOL_STUBS)
2358 stride = Sec.reserved2;
2359 else
2360 stride = 4;
2361 if (stride == 0)
2362 return nullptr;
2363 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2364 if (index < Dysymtab.nindirectsyms) {
2365 uint32_t indirect_symbol =
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002366 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
Kevin Enderby85974882014-09-26 22:20:44 +00002367 if (indirect_symbol < Symtab.nsyms) {
2368 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2369 SymbolRef Symbol = *Sym;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002370 Expected<StringRef> SymName = Symbol.getName();
2371 if (!SymName) {
2372 std::string Buf;
2373 raw_string_ostream OS(Buf);
2374 logAllUnhandledErrors(SymName.takeError(), OS, "");
2375 OS.flush();
2376 report_fatal_error(Buf);
2377 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002378 const char *name = SymName->data();
Kevin Enderby85974882014-09-26 22:20:44 +00002379 return name;
2380 }
2381 }
2382 }
2383 }
2384 }
Kevin Enderby85974882014-09-26 22:20:44 +00002385 }
2386 return nullptr;
2387}
2388
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002389// method_reference() is called passing it the ReferenceName that might be
2390// a reference it to an Objective-C method call. If so then it allocates and
2391// assembles a method call string with the values last seen and saved in
2392// the DisassembleInfo's class_name and selector_name fields. This is saved
2393// into the method field of the info and any previous string is free'ed.
2394// Then the class_name field in the info is set to nullptr. The method call
2395// string is set into ReferenceName and ReferenceType is set to
2396// LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call
2397// then both ReferenceType and ReferenceName are left unchanged.
2398static void method_reference(struct DisassembleInfo *info,
2399 uint64_t *ReferenceType,
2400 const char **ReferenceName) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002401 unsigned int Arch = info->O->getArch();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002402 if (*ReferenceName != nullptr) {
2403 if (strcmp(*ReferenceName, "_objc_msgSend") == 0) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002404 if (info->selector_name != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002405 if (info->method != nullptr)
2406 free(info->method);
2407 if (info->class_name != nullptr) {
2408 info->method = (char *)malloc(5 + strlen(info->class_name) +
2409 strlen(info->selector_name));
2410 if (info->method != nullptr) {
2411 strcpy(info->method, "+[");
2412 strcat(info->method, info->class_name);
2413 strcat(info->method, " ");
2414 strcat(info->method, info->selector_name);
2415 strcat(info->method, "]");
2416 *ReferenceName = info->method;
2417 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2418 }
2419 } else {
2420 info->method = (char *)malloc(9 + strlen(info->selector_name));
2421 if (info->method != nullptr) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002422 if (Arch == Triple::x86_64)
2423 strcpy(info->method, "-[%rdi ");
2424 else if (Arch == Triple::aarch64)
2425 strcpy(info->method, "-[x0 ");
2426 else
2427 strcpy(info->method, "-[r? ");
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002428 strcat(info->method, info->selector_name);
2429 strcat(info->method, "]");
2430 *ReferenceName = info->method;
2431 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2432 }
2433 }
2434 info->class_name = nullptr;
2435 }
2436 } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002437 if (info->selector_name != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002438 if (info->method != nullptr)
2439 free(info->method);
2440 info->method = (char *)malloc(17 + strlen(info->selector_name));
2441 if (info->method != nullptr) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002442 if (Arch == Triple::x86_64)
2443 strcpy(info->method, "-[[%rdi super] ");
2444 else if (Arch == Triple::aarch64)
2445 strcpy(info->method, "-[[x0 super] ");
2446 else
2447 strcpy(info->method, "-[[r? super] ");
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002448 strcat(info->method, info->selector_name);
2449 strcat(info->method, "]");
2450 *ReferenceName = info->method;
2451 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2452 }
2453 info->class_name = nullptr;
2454 }
2455 }
2456 }
2457}
2458
2459// GuessPointerPointer() is passed the address of what might be a pointer to
2460// a reference to an Objective-C class, selector, message ref or cfstring.
2461// If so the value of the pointer is returned and one of the booleans are set
2462// to true. If not zero is returned and all the booleans are set to false.
2463static uint64_t GuessPointerPointer(uint64_t ReferenceValue,
2464 struct DisassembleInfo *info,
2465 bool &classref, bool &selref, bool &msgref,
2466 bool &cfstring) {
2467 classref = false;
2468 selref = false;
2469 msgref = false;
2470 cfstring = false;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002471 for (const auto &Load : info->O->load_commands()) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002472 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2473 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2474 for (unsigned J = 0; J < Seg.nsects; ++J) {
2475 MachO::section_64 Sec = info->O->getSection64(Load, J);
2476 if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 ||
2477 strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2478 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 ||
2479 strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 ||
2480 strncmp(Sec.sectname, "__cfstring", 16) == 0) &&
2481 ReferenceValue >= Sec.addr &&
2482 ReferenceValue < Sec.addr + Sec.size) {
2483 uint64_t sect_offset = ReferenceValue - Sec.addr;
2484 uint64_t object_offset = Sec.offset + sect_offset;
2485 StringRef MachOContents = info->O->getData();
2486 uint64_t object_size = MachOContents.size();
2487 const char *object_addr = (const char *)MachOContents.data();
2488 if (object_offset < object_size) {
2489 uint64_t pointer_value;
2490 memcpy(&pointer_value, object_addr + object_offset,
2491 sizeof(uint64_t));
2492 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2493 sys::swapByteOrder(pointer_value);
2494 if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0)
2495 selref = true;
2496 else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2497 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0)
2498 classref = true;
2499 else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 &&
2500 ReferenceValue + 8 < Sec.addr + Sec.size) {
2501 msgref = true;
2502 memcpy(&pointer_value, object_addr + object_offset + 8,
2503 sizeof(uint64_t));
2504 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2505 sys::swapByteOrder(pointer_value);
2506 } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0)
2507 cfstring = true;
2508 return pointer_value;
2509 } else {
2510 return 0;
2511 }
2512 }
2513 }
2514 }
2515 // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002516 }
2517 return 0;
2518}
2519
2520// get_pointer_64 returns a pointer to the bytes in the object file at the
2521// Address from a section in the Mach-O file. And indirectly returns the
2522// offset into the section, number of bytes left in the section past the offset
2523// and which section is was being referenced. If the Address is not in a
2524// section nullptr is returned.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00002525static const char *get_pointer_64(uint64_t Address, uint32_t &offset,
2526 uint32_t &left, SectionRef &S,
Kevin Enderby846c0002015-04-16 17:19:59 +00002527 DisassembleInfo *info,
2528 bool objc_only = false) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002529 offset = 0;
2530 left = 0;
2531 S = SectionRef();
2532 for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) {
2533 uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress();
2534 uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize();
Kevin Enderby46e642f2015-10-08 22:50:55 +00002535 if (SectSize == 0)
2536 continue;
Kevin Enderby846c0002015-04-16 17:19:59 +00002537 if (objc_only) {
2538 StringRef SectName;
2539 ((*(info->Sections))[SectIdx]).getName(SectName);
2540 DataRefImpl Ref = ((*(info->Sections))[SectIdx]).getRawDataRefImpl();
2541 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
2542 if (SegName != "__OBJC" && SectName != "__cstring")
2543 continue;
2544 }
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002545 if (Address >= SectAddress && Address < SectAddress + SectSize) {
2546 S = (*(info->Sections))[SectIdx];
2547 offset = Address - SectAddress;
2548 left = SectSize - offset;
2549 StringRef SectContents;
2550 ((*(info->Sections))[SectIdx]).getContents(SectContents);
2551 return SectContents.data() + offset;
2552 }
2553 }
2554 return nullptr;
2555}
2556
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002557static const char *get_pointer_32(uint32_t Address, uint32_t &offset,
2558 uint32_t &left, SectionRef &S,
Kevin Enderby846c0002015-04-16 17:19:59 +00002559 DisassembleInfo *info,
2560 bool objc_only = false) {
2561 return get_pointer_64(Address, offset, left, S, info, objc_only);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002562}
2563
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002564// get_symbol_64() returns the name of a symbol (or nullptr) and the address of
2565// the symbol indirectly through n_value. Based on the relocation information
2566// for the specified section offset in the specified section reference.
Kevin Enderby0fc11822015-04-01 20:57:01 +00002567// If no relocation information is found and a non-zero ReferenceValue for the
2568// symbol is passed, look up that address in the info's AddrMap.
Rafael Espindolad7a32ea2015-06-24 10:20:30 +00002569static const char *get_symbol_64(uint32_t sect_offset, SectionRef S,
2570 DisassembleInfo *info, uint64_t &n_value,
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +00002571 uint64_t ReferenceValue = 0) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002572 n_value = 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00002573 if (!info->verbose)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002574 return nullptr;
2575
2576 // See if there is an external relocation entry at the sect_offset.
2577 bool reloc_found = false;
2578 DataRefImpl Rel;
2579 MachO::any_relocation_info RE;
2580 bool isExtern = false;
2581 SymbolRef Symbol;
2582 for (const RelocationRef &Reloc : S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00002583 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002584 if (RelocOffset == sect_offset) {
2585 Rel = Reloc.getRawDataRefImpl();
2586 RE = info->O->getRelocation(Rel);
2587 if (info->O->isRelocationScattered(RE))
2588 continue;
2589 isExtern = info->O->getPlainRelocationExternal(RE);
2590 if (isExtern) {
2591 symbol_iterator RelocSym = Reloc.getSymbol();
2592 Symbol = *RelocSym;
2593 }
2594 reloc_found = true;
2595 break;
2596 }
2597 }
2598 // If there is an external relocation entry for a symbol in this section
2599 // at this section_offset then use that symbol's value for the n_value
2600 // and return its name.
2601 const char *SymbolName = nullptr;
2602 if (reloc_found && isExtern) {
Rafael Espindoladea00162015-07-03 17:44:18 +00002603 n_value = Symbol.getValue();
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002604 Expected<StringRef> NameOrError = Symbol.getName();
2605 if (!NameOrError) {
2606 std::string Buf;
2607 raw_string_ostream OS(Buf);
2608 logAllUnhandledErrors(NameOrError.takeError(), OS, "");
2609 OS.flush();
2610 report_fatal_error(Buf);
2611 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002612 StringRef Name = *NameOrError;
2613 if (!Name.empty()) {
2614 SymbolName = Name.data();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002615 return SymbolName;
2616 }
2617 }
2618
2619 // TODO: For fully linked images, look through the external relocation
2620 // entries off the dynamic symtab command. For these the r_offset is from the
2621 // start of the first writeable segment in the Mach-O file. So the offset
2622 // to this section from that segment is passed to this routine by the caller,
2623 // as the database_offset. Which is the difference of the section's starting
2624 // address and the first writable segment.
2625 //
2626 // NOTE: need add passing the database_offset to this routine.
2627
Kevin Enderby0fc11822015-04-01 20:57:01 +00002628 // We did not find an external relocation entry so look up the ReferenceValue
2629 // as an address of a symbol and if found return that symbol's name.
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +00002630 SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002631
2632 return SymbolName;
2633}
2634
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002635static const char *get_symbol_32(uint32_t sect_offset, SectionRef S,
2636 DisassembleInfo *info,
2637 uint32_t ReferenceValue) {
2638 uint64_t n_value64;
2639 return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue);
2640}
2641
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002642// These are structs in the Objective-C meta data and read to produce the
2643// comments for disassembly. While these are part of the ABI they are no
2644// public defintions. So the are here not in include/llvm/Support/MachO.h .
2645
2646// The cfstring object in a 64-bit Mach-O file.
2647struct cfstring64_t {
2648 uint64_t isa; // class64_t * (64-bit pointer)
2649 uint64_t flags; // flag bits
2650 uint64_t characters; // char * (64-bit pointer)
2651 uint64_t length; // number of non-NULL characters in above
2652};
2653
2654// The class object in a 64-bit Mach-O file.
2655struct class64_t {
2656 uint64_t isa; // class64_t * (64-bit pointer)
2657 uint64_t superclass; // class64_t * (64-bit pointer)
2658 uint64_t cache; // Cache (64-bit pointer)
2659 uint64_t vtable; // IMP * (64-bit pointer)
2660 uint64_t data; // class_ro64_t * (64-bit pointer)
2661};
2662
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002663struct class32_t {
2664 uint32_t isa; /* class32_t * (32-bit pointer) */
2665 uint32_t superclass; /* class32_t * (32-bit pointer) */
2666 uint32_t cache; /* Cache (32-bit pointer) */
2667 uint32_t vtable; /* IMP * (32-bit pointer) */
2668 uint32_t data; /* class_ro32_t * (32-bit pointer) */
2669};
2670
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002671struct class_ro64_t {
2672 uint32_t flags;
2673 uint32_t instanceStart;
2674 uint32_t instanceSize;
2675 uint32_t reserved;
2676 uint64_t ivarLayout; // const uint8_t * (64-bit pointer)
2677 uint64_t name; // const char * (64-bit pointer)
2678 uint64_t baseMethods; // const method_list_t * (64-bit pointer)
2679 uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer)
2680 uint64_t ivars; // const ivar_list_t * (64-bit pointer)
2681 uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer)
2682 uint64_t baseProperties; // const struct objc_property_list (64-bit pointer)
2683};
2684
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002685struct class_ro32_t {
2686 uint32_t flags;
2687 uint32_t instanceStart;
2688 uint32_t instanceSize;
2689 uint32_t ivarLayout; /* const uint8_t * (32-bit pointer) */
2690 uint32_t name; /* const char * (32-bit pointer) */
2691 uint32_t baseMethods; /* const method_list_t * (32-bit pointer) */
2692 uint32_t baseProtocols; /* const protocol_list_t * (32-bit pointer) */
2693 uint32_t ivars; /* const ivar_list_t * (32-bit pointer) */
2694 uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */
2695 uint32_t baseProperties; /* const struct objc_property_list *
2696 (32-bit pointer) */
2697};
2698
2699/* Values for class_ro{64,32}_t->flags */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002700#define RO_META (1 << 0)
2701#define RO_ROOT (1 << 1)
2702#define RO_HAS_CXX_STRUCTORS (1 << 2)
2703
2704struct method_list64_t {
2705 uint32_t entsize;
2706 uint32_t count;
2707 /* struct method64_t first; These structures follow inline */
2708};
2709
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002710struct method_list32_t {
2711 uint32_t entsize;
2712 uint32_t count;
2713 /* struct method32_t first; These structures follow inline */
2714};
2715
Kevin Enderby0fc11822015-04-01 20:57:01 +00002716struct method64_t {
2717 uint64_t name; /* SEL (64-bit pointer) */
2718 uint64_t types; /* const char * (64-bit pointer) */
2719 uint64_t imp; /* IMP (64-bit pointer) */
2720};
2721
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002722struct method32_t {
2723 uint32_t name; /* SEL (32-bit pointer) */
2724 uint32_t types; /* const char * (32-bit pointer) */
2725 uint32_t imp; /* IMP (32-bit pointer) */
2726};
2727
Kevin Enderby0fc11822015-04-01 20:57:01 +00002728struct protocol_list64_t {
2729 uint64_t count; /* uintptr_t (a 64-bit value) */
2730 /* struct protocol64_t * list[0]; These pointers follow inline */
2731};
2732
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002733struct protocol_list32_t {
2734 uint32_t count; /* uintptr_t (a 32-bit value) */
2735 /* struct protocol32_t * list[0]; These pointers follow inline */
2736};
2737
Kevin Enderby0fc11822015-04-01 20:57:01 +00002738struct protocol64_t {
2739 uint64_t isa; /* id * (64-bit pointer) */
2740 uint64_t name; /* const char * (64-bit pointer) */
2741 uint64_t protocols; /* struct protocol_list64_t *
2742 (64-bit pointer) */
2743 uint64_t instanceMethods; /* method_list_t * (64-bit pointer) */
2744 uint64_t classMethods; /* method_list_t * (64-bit pointer) */
2745 uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */
2746 uint64_t optionalClassMethods; /* method_list_t * (64-bit pointer) */
2747 uint64_t instanceProperties; /* struct objc_property_list *
2748 (64-bit pointer) */
2749};
2750
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002751struct protocol32_t {
2752 uint32_t isa; /* id * (32-bit pointer) */
2753 uint32_t name; /* const char * (32-bit pointer) */
2754 uint32_t protocols; /* struct protocol_list_t *
2755 (32-bit pointer) */
2756 uint32_t instanceMethods; /* method_list_t * (32-bit pointer) */
2757 uint32_t classMethods; /* method_list_t * (32-bit pointer) */
2758 uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */
2759 uint32_t optionalClassMethods; /* method_list_t * (32-bit pointer) */
2760 uint32_t instanceProperties; /* struct objc_property_list *
2761 (32-bit pointer) */
2762};
2763
Kevin Enderby0fc11822015-04-01 20:57:01 +00002764struct ivar_list64_t {
2765 uint32_t entsize;
2766 uint32_t count;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002767 /* struct ivar64_t first; These structures follow inline */
2768};
2769
2770struct ivar_list32_t {
2771 uint32_t entsize;
2772 uint32_t count;
2773 /* struct ivar32_t first; These structures follow inline */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002774};
2775
2776struct ivar64_t {
2777 uint64_t offset; /* uintptr_t * (64-bit pointer) */
2778 uint64_t name; /* const char * (64-bit pointer) */
2779 uint64_t type; /* const char * (64-bit pointer) */
2780 uint32_t alignment;
2781 uint32_t size;
2782};
2783
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002784struct ivar32_t {
2785 uint32_t offset; /* uintptr_t * (32-bit pointer) */
2786 uint32_t name; /* const char * (32-bit pointer) */
2787 uint32_t type; /* const char * (32-bit pointer) */
2788 uint32_t alignment;
2789 uint32_t size;
2790};
2791
Kevin Enderby0fc11822015-04-01 20:57:01 +00002792struct objc_property_list64 {
2793 uint32_t entsize;
2794 uint32_t count;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002795 /* struct objc_property64 first; These structures follow inline */
2796};
2797
2798struct objc_property_list32 {
2799 uint32_t entsize;
2800 uint32_t count;
2801 /* struct objc_property32 first; These structures follow inline */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002802};
2803
2804struct objc_property64 {
2805 uint64_t name; /* const char * (64-bit pointer) */
2806 uint64_t attributes; /* const char * (64-bit pointer) */
2807};
2808
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002809struct objc_property32 {
2810 uint32_t name; /* const char * (32-bit pointer) */
2811 uint32_t attributes; /* const char * (32-bit pointer) */
2812};
2813
Kevin Enderby0fc11822015-04-01 20:57:01 +00002814struct category64_t {
2815 uint64_t name; /* const char * (64-bit pointer) */
2816 uint64_t cls; /* struct class_t * (64-bit pointer) */
2817 uint64_t instanceMethods; /* struct method_list_t * (64-bit pointer) */
2818 uint64_t classMethods; /* struct method_list_t * (64-bit pointer) */
2819 uint64_t protocols; /* struct protocol_list_t * (64-bit pointer) */
2820 uint64_t instanceProperties; /* struct objc_property_list *
2821 (64-bit pointer) */
2822};
2823
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002824struct category32_t {
2825 uint32_t name; /* const char * (32-bit pointer) */
2826 uint32_t cls; /* struct class_t * (32-bit pointer) */
2827 uint32_t instanceMethods; /* struct method_list_t * (32-bit pointer) */
2828 uint32_t classMethods; /* struct method_list_t * (32-bit pointer) */
2829 uint32_t protocols; /* struct protocol_list_t * (32-bit pointer) */
2830 uint32_t instanceProperties; /* struct objc_property_list *
2831 (32-bit pointer) */
2832};
2833
Kevin Enderby0fc11822015-04-01 20:57:01 +00002834struct objc_image_info64 {
2835 uint32_t version;
2836 uint32_t flags;
2837};
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002838struct objc_image_info32 {
2839 uint32_t version;
2840 uint32_t flags;
2841};
Kevin Enderby846c0002015-04-16 17:19:59 +00002842struct imageInfo_t {
2843 uint32_t version;
2844 uint32_t flags;
2845};
Kevin Enderby0fc11822015-04-01 20:57:01 +00002846/* masks for objc_image_info.flags */
2847#define OBJC_IMAGE_IS_REPLACEMENT (1 << 0)
2848#define OBJC_IMAGE_SUPPORTS_GC (1 << 1)
2849
2850struct message_ref64 {
2851 uint64_t imp; /* IMP (64-bit pointer) */
2852 uint64_t sel; /* SEL (64-bit pointer) */
2853};
2854
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002855struct message_ref32 {
2856 uint32_t imp; /* IMP (32-bit pointer) */
2857 uint32_t sel; /* SEL (32-bit pointer) */
2858};
2859
Kevin Enderby846c0002015-04-16 17:19:59 +00002860// Objective-C 1 (32-bit only) meta data structs.
2861
2862struct objc_module_t {
2863 uint32_t version;
2864 uint32_t size;
2865 uint32_t name; /* char * (32-bit pointer) */
2866 uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */
2867};
2868
2869struct objc_symtab_t {
2870 uint32_t sel_ref_cnt;
2871 uint32_t refs; /* SEL * (32-bit pointer) */
2872 uint16_t cls_def_cnt;
2873 uint16_t cat_def_cnt;
2874 // uint32_t defs[1]; /* void * (32-bit pointer) variable size */
2875};
2876
2877struct objc_class_t {
2878 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2879 uint32_t super_class; /* struct objc_class * (32-bit pointer) */
2880 uint32_t name; /* const char * (32-bit pointer) */
2881 int32_t version;
2882 int32_t info;
2883 int32_t instance_size;
2884 uint32_t ivars; /* struct objc_ivar_list * (32-bit pointer) */
2885 uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */
2886 uint32_t cache; /* struct objc_cache * (32-bit pointer) */
2887 uint32_t protocols; /* struct objc_protocol_list * (32-bit pointer) */
2888};
2889
2890#define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask))
2891// class is not a metaclass
2892#define CLS_CLASS 0x1
2893// class is a metaclass
2894#define CLS_META 0x2
2895
2896struct objc_category_t {
2897 uint32_t category_name; /* char * (32-bit pointer) */
2898 uint32_t class_name; /* char * (32-bit pointer) */
2899 uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */
2900 uint32_t class_methods; /* struct objc_method_list * (32-bit pointer) */
2901 uint32_t protocols; /* struct objc_protocol_list * (32-bit ptr) */
2902};
2903
2904struct objc_ivar_t {
2905 uint32_t ivar_name; /* char * (32-bit pointer) */
2906 uint32_t ivar_type; /* char * (32-bit pointer) */
2907 int32_t ivar_offset;
2908};
2909
2910struct objc_ivar_list_t {
2911 int32_t ivar_count;
2912 // struct objc_ivar_t ivar_list[1]; /* variable length structure */
2913};
2914
2915struct objc_method_list_t {
2916 uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */
2917 int32_t method_count;
2918 // struct objc_method_t method_list[1]; /* variable length structure */
2919};
2920
2921struct objc_method_t {
2922 uint32_t method_name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2923 uint32_t method_types; /* char * (32-bit pointer) */
2924 uint32_t method_imp; /* IMP, aka function pointer, (*IMP)(id, SEL, ...)
2925 (32-bit pointer) */
2926};
2927
2928struct objc_protocol_list_t {
2929 uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */
2930 int32_t count;
2931 // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t *
2932 // (32-bit pointer) */
2933};
2934
2935struct objc_protocol_t {
2936 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2937 uint32_t protocol_name; /* char * (32-bit pointer) */
2938 uint32_t protocol_list; /* struct objc_protocol_list * (32-bit pointer) */
2939 uint32_t instance_methods; /* struct objc_method_description_list *
2940 (32-bit pointer) */
2941 uint32_t class_methods; /* struct objc_method_description_list *
2942 (32-bit pointer) */
2943};
2944
2945struct objc_method_description_list_t {
2946 int32_t count;
2947 // struct objc_method_description_t list[1];
2948};
2949
2950struct objc_method_description_t {
2951 uint32_t name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2952 uint32_t types; /* char * (32-bit pointer) */
2953};
2954
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002955inline void swapStruct(struct cfstring64_t &cfs) {
2956 sys::swapByteOrder(cfs.isa);
2957 sys::swapByteOrder(cfs.flags);
2958 sys::swapByteOrder(cfs.characters);
2959 sys::swapByteOrder(cfs.length);
2960}
2961
2962inline void swapStruct(struct class64_t &c) {
2963 sys::swapByteOrder(c.isa);
2964 sys::swapByteOrder(c.superclass);
2965 sys::swapByteOrder(c.cache);
2966 sys::swapByteOrder(c.vtable);
2967 sys::swapByteOrder(c.data);
2968}
2969
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002970inline void swapStruct(struct class32_t &c) {
2971 sys::swapByteOrder(c.isa);
2972 sys::swapByteOrder(c.superclass);
2973 sys::swapByteOrder(c.cache);
2974 sys::swapByteOrder(c.vtable);
2975 sys::swapByteOrder(c.data);
2976}
2977
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002978inline void swapStruct(struct class_ro64_t &cro) {
2979 sys::swapByteOrder(cro.flags);
2980 sys::swapByteOrder(cro.instanceStart);
2981 sys::swapByteOrder(cro.instanceSize);
2982 sys::swapByteOrder(cro.reserved);
2983 sys::swapByteOrder(cro.ivarLayout);
2984 sys::swapByteOrder(cro.name);
2985 sys::swapByteOrder(cro.baseMethods);
2986 sys::swapByteOrder(cro.baseProtocols);
2987 sys::swapByteOrder(cro.ivars);
2988 sys::swapByteOrder(cro.weakIvarLayout);
2989 sys::swapByteOrder(cro.baseProperties);
2990}
2991
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002992inline void swapStruct(struct class_ro32_t &cro) {
2993 sys::swapByteOrder(cro.flags);
2994 sys::swapByteOrder(cro.instanceStart);
2995 sys::swapByteOrder(cro.instanceSize);
2996 sys::swapByteOrder(cro.ivarLayout);
2997 sys::swapByteOrder(cro.name);
2998 sys::swapByteOrder(cro.baseMethods);
2999 sys::swapByteOrder(cro.baseProtocols);
3000 sys::swapByteOrder(cro.ivars);
3001 sys::swapByteOrder(cro.weakIvarLayout);
3002 sys::swapByteOrder(cro.baseProperties);
3003}
3004
Kevin Enderby0fc11822015-04-01 20:57:01 +00003005inline void swapStruct(struct method_list64_t &ml) {
3006 sys::swapByteOrder(ml.entsize);
3007 sys::swapByteOrder(ml.count);
3008}
3009
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003010inline void swapStruct(struct method_list32_t &ml) {
3011 sys::swapByteOrder(ml.entsize);
3012 sys::swapByteOrder(ml.count);
3013}
3014
Kevin Enderby0fc11822015-04-01 20:57:01 +00003015inline void swapStruct(struct method64_t &m) {
3016 sys::swapByteOrder(m.name);
3017 sys::swapByteOrder(m.types);
3018 sys::swapByteOrder(m.imp);
3019}
3020
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003021inline void swapStruct(struct method32_t &m) {
3022 sys::swapByteOrder(m.name);
3023 sys::swapByteOrder(m.types);
3024 sys::swapByteOrder(m.imp);
3025}
3026
Kevin Enderby0fc11822015-04-01 20:57:01 +00003027inline void swapStruct(struct protocol_list64_t &pl) {
3028 sys::swapByteOrder(pl.count);
3029}
3030
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003031inline void swapStruct(struct protocol_list32_t &pl) {
3032 sys::swapByteOrder(pl.count);
3033}
3034
Kevin Enderby0fc11822015-04-01 20:57:01 +00003035inline void swapStruct(struct protocol64_t &p) {
3036 sys::swapByteOrder(p.isa);
3037 sys::swapByteOrder(p.name);
3038 sys::swapByteOrder(p.protocols);
3039 sys::swapByteOrder(p.instanceMethods);
3040 sys::swapByteOrder(p.classMethods);
3041 sys::swapByteOrder(p.optionalInstanceMethods);
3042 sys::swapByteOrder(p.optionalClassMethods);
3043 sys::swapByteOrder(p.instanceProperties);
3044}
3045
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003046inline void swapStruct(struct protocol32_t &p) {
3047 sys::swapByteOrder(p.isa);
3048 sys::swapByteOrder(p.name);
3049 sys::swapByteOrder(p.protocols);
3050 sys::swapByteOrder(p.instanceMethods);
3051 sys::swapByteOrder(p.classMethods);
3052 sys::swapByteOrder(p.optionalInstanceMethods);
3053 sys::swapByteOrder(p.optionalClassMethods);
3054 sys::swapByteOrder(p.instanceProperties);
3055}
3056
Kevin Enderby0fc11822015-04-01 20:57:01 +00003057inline void swapStruct(struct ivar_list64_t &il) {
3058 sys::swapByteOrder(il.entsize);
3059 sys::swapByteOrder(il.count);
3060}
3061
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003062inline void swapStruct(struct ivar_list32_t &il) {
3063 sys::swapByteOrder(il.entsize);
3064 sys::swapByteOrder(il.count);
3065}
3066
Kevin Enderby0fc11822015-04-01 20:57:01 +00003067inline void swapStruct(struct ivar64_t &i) {
3068 sys::swapByteOrder(i.offset);
3069 sys::swapByteOrder(i.name);
3070 sys::swapByteOrder(i.type);
3071 sys::swapByteOrder(i.alignment);
3072 sys::swapByteOrder(i.size);
3073}
3074
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003075inline void swapStruct(struct ivar32_t &i) {
3076 sys::swapByteOrder(i.offset);
3077 sys::swapByteOrder(i.name);
3078 sys::swapByteOrder(i.type);
3079 sys::swapByteOrder(i.alignment);
3080 sys::swapByteOrder(i.size);
3081}
3082
Kevin Enderby0fc11822015-04-01 20:57:01 +00003083inline void swapStruct(struct objc_property_list64 &pl) {
3084 sys::swapByteOrder(pl.entsize);
3085 sys::swapByteOrder(pl.count);
3086}
3087
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003088inline void swapStruct(struct objc_property_list32 &pl) {
3089 sys::swapByteOrder(pl.entsize);
3090 sys::swapByteOrder(pl.count);
3091}
3092
Kevin Enderby0fc11822015-04-01 20:57:01 +00003093inline void swapStruct(struct objc_property64 &op) {
3094 sys::swapByteOrder(op.name);
3095 sys::swapByteOrder(op.attributes);
3096}
3097
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003098inline void swapStruct(struct objc_property32 &op) {
3099 sys::swapByteOrder(op.name);
3100 sys::swapByteOrder(op.attributes);
3101}
3102
Kevin Enderby0fc11822015-04-01 20:57:01 +00003103inline void swapStruct(struct category64_t &c) {
3104 sys::swapByteOrder(c.name);
3105 sys::swapByteOrder(c.cls);
3106 sys::swapByteOrder(c.instanceMethods);
3107 sys::swapByteOrder(c.classMethods);
3108 sys::swapByteOrder(c.protocols);
3109 sys::swapByteOrder(c.instanceProperties);
3110}
3111
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003112inline void swapStruct(struct category32_t &c) {
3113 sys::swapByteOrder(c.name);
3114 sys::swapByteOrder(c.cls);
3115 sys::swapByteOrder(c.instanceMethods);
3116 sys::swapByteOrder(c.classMethods);
3117 sys::swapByteOrder(c.protocols);
3118 sys::swapByteOrder(c.instanceProperties);
3119}
3120
Kevin Enderby0fc11822015-04-01 20:57:01 +00003121inline void swapStruct(struct objc_image_info64 &o) {
3122 sys::swapByteOrder(o.version);
3123 sys::swapByteOrder(o.flags);
3124}
3125
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003126inline void swapStruct(struct objc_image_info32 &o) {
3127 sys::swapByteOrder(o.version);
3128 sys::swapByteOrder(o.flags);
3129}
3130
Kevin Enderby846c0002015-04-16 17:19:59 +00003131inline void swapStruct(struct imageInfo_t &o) {
3132 sys::swapByteOrder(o.version);
3133 sys::swapByteOrder(o.flags);
3134}
3135
Kevin Enderby0fc11822015-04-01 20:57:01 +00003136inline void swapStruct(struct message_ref64 &mr) {
3137 sys::swapByteOrder(mr.imp);
3138 sys::swapByteOrder(mr.sel);
3139}
3140
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003141inline void swapStruct(struct message_ref32 &mr) {
3142 sys::swapByteOrder(mr.imp);
3143 sys::swapByteOrder(mr.sel);
3144}
3145
Kevin Enderby846c0002015-04-16 17:19:59 +00003146inline void swapStruct(struct objc_module_t &module) {
3147 sys::swapByteOrder(module.version);
3148 sys::swapByteOrder(module.size);
3149 sys::swapByteOrder(module.name);
3150 sys::swapByteOrder(module.symtab);
Jingyue Wufedecc42015-04-16 18:43:44 +00003151}
Kevin Enderby846c0002015-04-16 17:19:59 +00003152
3153inline void swapStruct(struct objc_symtab_t &symtab) {
3154 sys::swapByteOrder(symtab.sel_ref_cnt);
3155 sys::swapByteOrder(symtab.refs);
3156 sys::swapByteOrder(symtab.cls_def_cnt);
3157 sys::swapByteOrder(symtab.cat_def_cnt);
Jingyue Wufedecc42015-04-16 18:43:44 +00003158}
Kevin Enderby846c0002015-04-16 17:19:59 +00003159
3160inline void swapStruct(struct objc_class_t &objc_class) {
3161 sys::swapByteOrder(objc_class.isa);
3162 sys::swapByteOrder(objc_class.super_class);
3163 sys::swapByteOrder(objc_class.name);
3164 sys::swapByteOrder(objc_class.version);
3165 sys::swapByteOrder(objc_class.info);
3166 sys::swapByteOrder(objc_class.instance_size);
3167 sys::swapByteOrder(objc_class.ivars);
3168 sys::swapByteOrder(objc_class.methodLists);
3169 sys::swapByteOrder(objc_class.cache);
3170 sys::swapByteOrder(objc_class.protocols);
Jingyue Wufedecc42015-04-16 18:43:44 +00003171}
Kevin Enderby846c0002015-04-16 17:19:59 +00003172
3173inline void swapStruct(struct objc_category_t &objc_category) {
3174 sys::swapByteOrder(objc_category.category_name);
3175 sys::swapByteOrder(objc_category.class_name);
3176 sys::swapByteOrder(objc_category.instance_methods);
3177 sys::swapByteOrder(objc_category.class_methods);
3178 sys::swapByteOrder(objc_category.protocols);
3179}
3180
3181inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) {
3182 sys::swapByteOrder(objc_ivar_list.ivar_count);
3183}
3184
3185inline void swapStruct(struct objc_ivar_t &objc_ivar) {
3186 sys::swapByteOrder(objc_ivar.ivar_name);
3187 sys::swapByteOrder(objc_ivar.ivar_type);
3188 sys::swapByteOrder(objc_ivar.ivar_offset);
Jingyue Wufedecc42015-04-16 18:43:44 +00003189}
Kevin Enderby846c0002015-04-16 17:19:59 +00003190
3191inline void swapStruct(struct objc_method_list_t &method_list) {
3192 sys::swapByteOrder(method_list.obsolete);
3193 sys::swapByteOrder(method_list.method_count);
3194}
3195
3196inline void swapStruct(struct objc_method_t &method) {
3197 sys::swapByteOrder(method.method_name);
3198 sys::swapByteOrder(method.method_types);
3199 sys::swapByteOrder(method.method_imp);
3200}
3201
3202inline void swapStruct(struct objc_protocol_list_t &protocol_list) {
3203 sys::swapByteOrder(protocol_list.next);
3204 sys::swapByteOrder(protocol_list.count);
3205}
3206
3207inline void swapStruct(struct objc_protocol_t &protocol) {
3208 sys::swapByteOrder(protocol.isa);
3209 sys::swapByteOrder(protocol.protocol_name);
3210 sys::swapByteOrder(protocol.protocol_list);
3211 sys::swapByteOrder(protocol.instance_methods);
3212 sys::swapByteOrder(protocol.class_methods);
3213}
3214
3215inline void swapStruct(struct objc_method_description_list_t &mdl) {
3216 sys::swapByteOrder(mdl.count);
3217}
3218
3219inline void swapStruct(struct objc_method_description_t &md) {
3220 sys::swapByteOrder(md.name);
3221 sys::swapByteOrder(md.types);
3222}
3223
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003224static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
3225 struct DisassembleInfo *info);
3226
3227// get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
3228// to an Objective-C class and returns the class name. It is also passed the
3229// address of the pointer, so when the pointer is zero as it can be in an .o
3230// file, that is used to look for an external relocation entry with a symbol
3231// name.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003232static const char *get_objc2_64bit_class_name(uint64_t pointer_value,
3233 uint64_t ReferenceValue,
3234 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003235 const char *r;
3236 uint32_t offset, left;
3237 SectionRef S;
3238
3239 // The pointer_value can be 0 in an object file and have a relocation
3240 // entry for the class symbol at the ReferenceValue (the address of the
3241 // pointer).
3242 if (pointer_value == 0) {
3243 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3244 if (r == nullptr || left < sizeof(uint64_t))
3245 return nullptr;
3246 uint64_t n_value;
3247 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3248 if (symbol_name == nullptr)
3249 return nullptr;
Hans Wennborgdb53e302014-10-23 21:59:17 +00003250 const char *class_name = strrchr(symbol_name, '$');
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003251 if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0')
3252 return class_name + 2;
3253 else
3254 return nullptr;
3255 }
3256
3257 // The case were the pointer_value is non-zero and points to a class defined
3258 // in this Mach-O file.
3259 r = get_pointer_64(pointer_value, offset, left, S, info);
3260 if (r == nullptr || left < sizeof(struct class64_t))
3261 return nullptr;
3262 struct class64_t c;
3263 memcpy(&c, r, sizeof(struct class64_t));
3264 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3265 swapStruct(c);
3266 if (c.data == 0)
3267 return nullptr;
3268 r = get_pointer_64(c.data, offset, left, S, info);
3269 if (r == nullptr || left < sizeof(struct class_ro64_t))
3270 return nullptr;
3271 struct class_ro64_t cro;
3272 memcpy(&cro, r, sizeof(struct class_ro64_t));
3273 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3274 swapStruct(cro);
3275 if (cro.name == 0)
3276 return nullptr;
3277 const char *name = get_pointer_64(cro.name, offset, left, S, info);
3278 return name;
3279}
3280
3281// get_objc2_64bit_cfstring_name is used for disassembly and is passed a
3282// pointer to a cfstring and returns its name or nullptr.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003283static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue,
3284 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003285 const char *r, *name;
3286 uint32_t offset, left;
3287 SectionRef S;
3288 struct cfstring64_t cfs;
3289 uint64_t cfs_characters;
3290
3291 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3292 if (r == nullptr || left < sizeof(struct cfstring64_t))
3293 return nullptr;
3294 memcpy(&cfs, r, sizeof(struct cfstring64_t));
3295 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3296 swapStruct(cfs);
3297 if (cfs.characters == 0) {
3298 uint64_t n_value;
3299 const char *symbol_name = get_symbol_64(
3300 offset + offsetof(struct cfstring64_t, characters), S, info, n_value);
3301 if (symbol_name == nullptr)
3302 return nullptr;
3303 cfs_characters = n_value;
3304 } else
3305 cfs_characters = cfs.characters;
3306 name = get_pointer_64(cfs_characters, offset, left, S, info);
3307
3308 return name;
3309}
3310
3311// get_objc2_64bit_selref() is used for disassembly and is passed a the address
3312// of a pointer to an Objective-C selector reference when the pointer value is
3313// zero as in a .o file and is likely to have a external relocation entry with
3314// who's symbol's n_value is the real pointer to the selector name. If that is
3315// the case the real pointer to the selector name is returned else 0 is
3316// returned
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003317static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue,
3318 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003319 uint32_t offset, left;
3320 SectionRef S;
3321
3322 const char *r = get_pointer_64(ReferenceValue, offset, left, S, info);
3323 if (r == nullptr || left < sizeof(uint64_t))
3324 return 0;
3325 uint64_t n_value;
3326 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3327 if (symbol_name == nullptr)
3328 return 0;
3329 return n_value;
3330}
3331
Kevin Enderby0fc11822015-04-01 20:57:01 +00003332static const SectionRef get_section(MachOObjectFile *O, const char *segname,
3333 const char *sectname) {
3334 for (const SectionRef &Section : O->sections()) {
3335 StringRef SectName;
3336 Section.getName(SectName);
3337 DataRefImpl Ref = Section.getRawDataRefImpl();
3338 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3339 if (SegName == segname && SectName == sectname)
3340 return Section;
3341 }
3342 return SectionRef();
3343}
3344
3345static void
3346walk_pointer_list_64(const char *listname, const SectionRef S,
3347 MachOObjectFile *O, struct DisassembleInfo *info,
3348 void (*func)(uint64_t, struct DisassembleInfo *info)) {
3349 if (S == SectionRef())
3350 return;
3351
3352 StringRef SectName;
3353 S.getName(SectName);
3354 DataRefImpl Ref = S.getRawDataRefImpl();
3355 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3356 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3357
3358 StringRef BytesStr;
3359 S.getContents(BytesStr);
3360 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3361
3362 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) {
3363 uint32_t left = S.getSize() - i;
3364 uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t);
3365 uint64_t p = 0;
3366 memcpy(&p, Contents + i, size);
3367 if (i + sizeof(uint64_t) > S.getSize())
3368 outs() << listname << " list pointer extends past end of (" << SegName
3369 << "," << SectName << ") section\n";
3370 outs() << format("%016" PRIx64, S.getAddress() + i) << " ";
3371
3372 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3373 sys::swapByteOrder(p);
3374
3375 uint64_t n_value = 0;
3376 const char *name = get_symbol_64(i, S, info, n_value, p);
3377 if (name == nullptr)
3378 name = get_dyld_bind_info_symbolname(S.getAddress() + i, info);
3379
3380 if (n_value != 0) {
3381 outs() << format("0x%" PRIx64, n_value);
3382 if (p != 0)
3383 outs() << " + " << format("0x%" PRIx64, p);
3384 } else
3385 outs() << format("0x%" PRIx64, p);
3386 if (name != nullptr)
3387 outs() << " " << name;
3388 outs() << "\n";
3389
3390 p += n_value;
3391 if (func)
3392 func(p, info);
3393 }
3394}
3395
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003396static void
3397walk_pointer_list_32(const char *listname, const SectionRef S,
3398 MachOObjectFile *O, struct DisassembleInfo *info,
3399 void (*func)(uint32_t, struct DisassembleInfo *info)) {
3400 if (S == SectionRef())
3401 return;
3402
3403 StringRef SectName;
3404 S.getName(SectName);
3405 DataRefImpl Ref = S.getRawDataRefImpl();
3406 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3407 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3408
3409 StringRef BytesStr;
3410 S.getContents(BytesStr);
3411 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3412
3413 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) {
3414 uint32_t left = S.getSize() - i;
3415 uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t);
3416 uint32_t p = 0;
3417 memcpy(&p, Contents + i, size);
3418 if (i + sizeof(uint32_t) > S.getSize())
3419 outs() << listname << " list pointer extends past end of (" << SegName
3420 << "," << SectName << ") section\n";
Kevin Enderbycf261312015-04-06 22:33:43 +00003421 uint32_t Address = S.getAddress() + i;
3422 outs() << format("%08" PRIx32, Address) << " ";
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003423
3424 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3425 sys::swapByteOrder(p);
3426 outs() << format("0x%" PRIx32, p);
3427
3428 const char *name = get_symbol_32(i, S, info, p);
3429 if (name != nullptr)
3430 outs() << " " << name;
3431 outs() << "\n";
3432
3433 if (func)
3434 func(p, info);
3435 }
3436}
3437
3438static void print_layout_map(const char *layout_map, uint32_t left) {
Kevin Enderbya59824a2015-10-06 22:27:08 +00003439 if (layout_map == nullptr)
3440 return;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003441 outs() << " layout map: ";
3442 do {
3443 outs() << format("0x%02" PRIx32, (*layout_map) & 0xff) << " ";
3444 left--;
3445 layout_map++;
3446 } while (*layout_map != '\0' && left != 0);
3447 outs() << "\n";
3448}
3449
Kevin Enderby0fc11822015-04-01 20:57:01 +00003450static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) {
3451 uint32_t offset, left;
3452 SectionRef S;
3453 const char *layout_map;
3454
3455 if (p == 0)
3456 return;
3457 layout_map = get_pointer_64(p, offset, left, S, info);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003458 print_layout_map(layout_map, left);
3459}
3460
3461static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) {
3462 uint32_t offset, left;
3463 SectionRef S;
3464 const char *layout_map;
3465
3466 if (p == 0)
3467 return;
3468 layout_map = get_pointer_32(p, offset, left, S, info);
3469 print_layout_map(layout_map, left);
Kevin Enderby0fc11822015-04-01 20:57:01 +00003470}
3471
3472static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info,
3473 const char *indent) {
3474 struct method_list64_t ml;
3475 struct method64_t m;
3476 const char *r;
3477 uint32_t offset, xoffset, left, i;
3478 SectionRef S, xS;
3479 const char *name, *sym_name;
3480 uint64_t n_value;
3481
3482 r = get_pointer_64(p, offset, left, S, info);
3483 if (r == nullptr)
3484 return;
3485 memset(&ml, '\0', sizeof(struct method_list64_t));
3486 if (left < sizeof(struct method_list64_t)) {
3487 memcpy(&ml, r, left);
3488 outs() << " (method_list_t entends past the end of the section)\n";
3489 } else
3490 memcpy(&ml, r, sizeof(struct method_list64_t));
3491 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3492 swapStruct(ml);
3493 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3494 outs() << indent << "\t\t count " << ml.count << "\n";
3495
3496 p += sizeof(struct method_list64_t);
3497 offset += sizeof(struct method_list64_t);
3498 for (i = 0; i < ml.count; i++) {
3499 r = get_pointer_64(p, offset, left, S, info);
3500 if (r == nullptr)
3501 return;
3502 memset(&m, '\0', sizeof(struct method64_t));
3503 if (left < sizeof(struct method64_t)) {
Kevin Enderbya59824a2015-10-06 22:27:08 +00003504 memcpy(&m, r, left);
3505 outs() << indent << " (method_t extends past the end of the section)\n";
Kevin Enderby0fc11822015-04-01 20:57:01 +00003506 } else
3507 memcpy(&m, r, sizeof(struct method64_t));
3508 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3509 swapStruct(m);
3510
3511 outs() << indent << "\t\t name ";
3512 sym_name = get_symbol_64(offset + offsetof(struct method64_t, name), S,
3513 info, n_value, m.name);
3514 if (n_value != 0) {
3515 if (info->verbose && sym_name != nullptr)
3516 outs() << sym_name;
3517 else
3518 outs() << format("0x%" PRIx64, n_value);
3519 if (m.name != 0)
3520 outs() << " + " << format("0x%" PRIx64, m.name);
3521 } else
3522 outs() << format("0x%" PRIx64, m.name);
3523 name = get_pointer_64(m.name + n_value, xoffset, left, xS, info);
3524 if (name != nullptr)
3525 outs() << format(" %.*s", left, name);
3526 outs() << "\n";
3527
3528 outs() << indent << "\t\t types ";
3529 sym_name = get_symbol_64(offset + offsetof(struct method64_t, types), S,
3530 info, n_value, m.types);
3531 if (n_value != 0) {
3532 if (info->verbose && sym_name != nullptr)
3533 outs() << sym_name;
3534 else
3535 outs() << format("0x%" PRIx64, n_value);
3536 if (m.types != 0)
3537 outs() << " + " << format("0x%" PRIx64, m.types);
3538 } else
3539 outs() << format("0x%" PRIx64, m.types);
3540 name = get_pointer_64(m.types + n_value, xoffset, left, xS, info);
3541 if (name != nullptr)
3542 outs() << format(" %.*s", left, name);
3543 outs() << "\n";
3544
3545 outs() << indent << "\t\t imp ";
3546 name = get_symbol_64(offset + offsetof(struct method64_t, imp), S, info,
3547 n_value, m.imp);
3548 if (info->verbose && name == nullptr) {
3549 if (n_value != 0) {
3550 outs() << format("0x%" PRIx64, n_value) << " ";
3551 if (m.imp != 0)
3552 outs() << "+ " << format("0x%" PRIx64, m.imp) << " ";
3553 } else
3554 outs() << format("0x%" PRIx64, m.imp) << " ";
3555 }
3556 if (name != nullptr)
3557 outs() << name;
3558 outs() << "\n";
3559
3560 p += sizeof(struct method64_t);
3561 offset += sizeof(struct method64_t);
3562 }
3563}
3564
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003565static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info,
3566 const char *indent) {
3567 struct method_list32_t ml;
3568 struct method32_t m;
Kevin Enderby846c0002015-04-16 17:19:59 +00003569 const char *r, *name;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003570 uint32_t offset, xoffset, left, i;
3571 SectionRef S, xS;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003572
3573 r = get_pointer_32(p, offset, left, S, info);
3574 if (r == nullptr)
3575 return;
3576 memset(&ml, '\0', sizeof(struct method_list32_t));
3577 if (left < sizeof(struct method_list32_t)) {
3578 memcpy(&ml, r, left);
3579 outs() << " (method_list_t entends past the end of the section)\n";
3580 } else
3581 memcpy(&ml, r, sizeof(struct method_list32_t));
3582 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3583 swapStruct(ml);
3584 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3585 outs() << indent << "\t\t count " << ml.count << "\n";
3586
3587 p += sizeof(struct method_list32_t);
3588 offset += sizeof(struct method_list32_t);
3589 for (i = 0; i < ml.count; i++) {
3590 r = get_pointer_32(p, offset, left, S, info);
3591 if (r == nullptr)
3592 return;
3593 memset(&m, '\0', sizeof(struct method32_t));
3594 if (left < sizeof(struct method32_t)) {
3595 memcpy(&ml, r, left);
3596 outs() << indent << " (method_t entends past the end of the section)\n";
3597 } else
3598 memcpy(&m, r, sizeof(struct method32_t));
3599 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3600 swapStruct(m);
3601
3602 outs() << indent << "\t\t name " << format("0x%" PRIx32, m.name);
3603 name = get_pointer_32(m.name, xoffset, left, xS, info);
3604 if (name != nullptr)
3605 outs() << format(" %.*s", left, name);
3606 outs() << "\n";
3607
3608 outs() << indent << "\t\t types " << format("0x%" PRIx32, m.types);
3609 name = get_pointer_32(m.types, xoffset, left, xS, info);
3610 if (name != nullptr)
3611 outs() << format(" %.*s", left, name);
3612 outs() << "\n";
3613
3614 outs() << indent << "\t\t imp " << format("0x%" PRIx32, m.imp);
3615 name = get_symbol_32(offset + offsetof(struct method32_t, imp), S, info,
3616 m.imp);
3617 if (name != nullptr)
3618 outs() << " " << name;
3619 outs() << "\n";
3620
3621 p += sizeof(struct method32_t);
3622 offset += sizeof(struct method32_t);
3623 }
3624}
3625
Kevin Enderby846c0002015-04-16 17:19:59 +00003626static bool print_method_list(uint32_t p, struct DisassembleInfo *info) {
3627 uint32_t offset, left, xleft;
3628 SectionRef S;
3629 struct objc_method_list_t method_list;
3630 struct objc_method_t method;
3631 const char *r, *methods, *name, *SymbolName;
3632 int32_t i;
3633
3634 r = get_pointer_32(p, offset, left, S, info, true);
3635 if (r == nullptr)
3636 return true;
3637
3638 outs() << "\n";
3639 if (left > sizeof(struct objc_method_list_t)) {
3640 memcpy(&method_list, r, sizeof(struct objc_method_list_t));
3641 } else {
3642 outs() << "\t\t objc_method_list extends past end of the section\n";
3643 memset(&method_list, '\0', sizeof(struct objc_method_list_t));
3644 memcpy(&method_list, r, left);
3645 }
3646 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3647 swapStruct(method_list);
3648
3649 outs() << "\t\t obsolete "
3650 << format("0x%08" PRIx32, method_list.obsolete) << "\n";
3651 outs() << "\t\t method_count " << method_list.method_count << "\n";
3652
3653 methods = r + sizeof(struct objc_method_list_t);
3654 for (i = 0; i < method_list.method_count; i++) {
3655 if ((i + 1) * sizeof(struct objc_method_t) > left) {
3656 outs() << "\t\t remaining method's extend past the of the section\n";
3657 break;
3658 }
3659 memcpy(&method, methods + i * sizeof(struct objc_method_t),
3660 sizeof(struct objc_method_t));
3661 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3662 swapStruct(method);
3663
3664 outs() << "\t\t method_name "
3665 << format("0x%08" PRIx32, method.method_name);
3666 if (info->verbose) {
3667 name = get_pointer_32(method.method_name, offset, xleft, S, info, true);
3668 if (name != nullptr)
3669 outs() << format(" %.*s", xleft, name);
3670 else
3671 outs() << " (not in an __OBJC section)";
3672 }
3673 outs() << "\n";
3674
3675 outs() << "\t\t method_types "
3676 << format("0x%08" PRIx32, method.method_types);
3677 if (info->verbose) {
3678 name = get_pointer_32(method.method_types, offset, xleft, S, info, true);
3679 if (name != nullptr)
3680 outs() << format(" %.*s", xleft, name);
3681 else
3682 outs() << " (not in an __OBJC section)";
3683 }
3684 outs() << "\n";
3685
3686 outs() << "\t\t method_imp "
3687 << format("0x%08" PRIx32, method.method_imp) << " ";
3688 if (info->verbose) {
3689 SymbolName = GuessSymbolName(method.method_imp, info->AddrMap);
3690 if (SymbolName != nullptr)
3691 outs() << SymbolName;
3692 }
3693 outs() << "\n";
3694 }
3695 return false;
3696}
3697
Kevin Enderby0fc11822015-04-01 20:57:01 +00003698static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) {
3699 struct protocol_list64_t pl;
3700 uint64_t q, n_value;
3701 struct protocol64_t pc;
3702 const char *r;
3703 uint32_t offset, xoffset, left, i;
3704 SectionRef S, xS;
3705 const char *name, *sym_name;
3706
3707 r = get_pointer_64(p, offset, left, S, info);
3708 if (r == nullptr)
3709 return;
3710 memset(&pl, '\0', sizeof(struct protocol_list64_t));
3711 if (left < sizeof(struct protocol_list64_t)) {
3712 memcpy(&pl, r, left);
3713 outs() << " (protocol_list_t entends past the end of the section)\n";
3714 } else
3715 memcpy(&pl, r, sizeof(struct protocol_list64_t));
3716 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3717 swapStruct(pl);
3718 outs() << " count " << pl.count << "\n";
3719
3720 p += sizeof(struct protocol_list64_t);
3721 offset += sizeof(struct protocol_list64_t);
3722 for (i = 0; i < pl.count; i++) {
3723 r = get_pointer_64(p, offset, left, S, info);
3724 if (r == nullptr)
3725 return;
3726 q = 0;
3727 if (left < sizeof(uint64_t)) {
3728 memcpy(&q, r, left);
3729 outs() << " (protocol_t * entends past the end of the section)\n";
3730 } else
3731 memcpy(&q, r, sizeof(uint64_t));
3732 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3733 sys::swapByteOrder(q);
3734
3735 outs() << "\t\t list[" << i << "] ";
3736 sym_name = get_symbol_64(offset, S, info, n_value, q);
3737 if (n_value != 0) {
3738 if (info->verbose && sym_name != nullptr)
3739 outs() << sym_name;
3740 else
3741 outs() << format("0x%" PRIx64, n_value);
3742 if (q != 0)
3743 outs() << " + " << format("0x%" PRIx64, q);
3744 } else
3745 outs() << format("0x%" PRIx64, q);
3746 outs() << " (struct protocol_t *)\n";
3747
3748 r = get_pointer_64(q + n_value, offset, left, S, info);
3749 if (r == nullptr)
3750 return;
3751 memset(&pc, '\0', sizeof(struct protocol64_t));
3752 if (left < sizeof(struct protocol64_t)) {
3753 memcpy(&pc, r, left);
3754 outs() << " (protocol_t entends past the end of the section)\n";
3755 } else
3756 memcpy(&pc, r, sizeof(struct protocol64_t));
3757 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3758 swapStruct(pc);
3759
3760 outs() << "\t\t\t isa " << format("0x%" PRIx64, pc.isa) << "\n";
3761
3762 outs() << "\t\t\t name ";
3763 sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name), S,
3764 info, n_value, pc.name);
3765 if (n_value != 0) {
3766 if (info->verbose && sym_name != nullptr)
3767 outs() << sym_name;
3768 else
3769 outs() << format("0x%" PRIx64, n_value);
3770 if (pc.name != 0)
3771 outs() << " + " << format("0x%" PRIx64, pc.name);
3772 } else
3773 outs() << format("0x%" PRIx64, pc.name);
3774 name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info);
3775 if (name != nullptr)
3776 outs() << format(" %.*s", left, name);
3777 outs() << "\n";
3778
3779 outs() << "\t\t\tprotocols " << format("0x%" PRIx64, pc.protocols) << "\n";
3780
3781 outs() << "\t\t instanceMethods ";
3782 sym_name =
3783 get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods),
3784 S, info, n_value, pc.instanceMethods);
3785 if (n_value != 0) {
3786 if (info->verbose && sym_name != nullptr)
3787 outs() << sym_name;
3788 else
3789 outs() << format("0x%" PRIx64, n_value);
3790 if (pc.instanceMethods != 0)
3791 outs() << " + " << format("0x%" PRIx64, pc.instanceMethods);
3792 } else
3793 outs() << format("0x%" PRIx64, pc.instanceMethods);
3794 outs() << " (struct method_list_t *)\n";
3795 if (pc.instanceMethods + n_value != 0)
3796 print_method_list64_t(pc.instanceMethods + n_value, info, "\t");
3797
3798 outs() << "\t\t classMethods ";
3799 sym_name =
3800 get_symbol_64(offset + offsetof(struct protocol64_t, classMethods), S,
3801 info, n_value, pc.classMethods);
3802 if (n_value != 0) {
3803 if (info->verbose && sym_name != nullptr)
3804 outs() << sym_name;
3805 else
3806 outs() << format("0x%" PRIx64, n_value);
3807 if (pc.classMethods != 0)
3808 outs() << " + " << format("0x%" PRIx64, pc.classMethods);
3809 } else
3810 outs() << format("0x%" PRIx64, pc.classMethods);
3811 outs() << " (struct method_list_t *)\n";
3812 if (pc.classMethods + n_value != 0)
3813 print_method_list64_t(pc.classMethods + n_value, info, "\t");
3814
3815 outs() << "\t optionalInstanceMethods "
3816 << format("0x%" PRIx64, pc.optionalInstanceMethods) << "\n";
3817 outs() << "\t optionalClassMethods "
3818 << format("0x%" PRIx64, pc.optionalClassMethods) << "\n";
3819 outs() << "\t instanceProperties "
3820 << format("0x%" PRIx64, pc.instanceProperties) << "\n";
3821
3822 p += sizeof(uint64_t);
3823 offset += sizeof(uint64_t);
3824 }
3825}
3826
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003827static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) {
3828 struct protocol_list32_t pl;
3829 uint32_t q;
3830 struct protocol32_t pc;
3831 const char *r;
3832 uint32_t offset, xoffset, left, i;
3833 SectionRef S, xS;
3834 const char *name;
3835
3836 r = get_pointer_32(p, offset, left, S, info);
3837 if (r == nullptr)
3838 return;
3839 memset(&pl, '\0', sizeof(struct protocol_list32_t));
3840 if (left < sizeof(struct protocol_list32_t)) {
3841 memcpy(&pl, r, left);
3842 outs() << " (protocol_list_t entends past the end of the section)\n";
3843 } else
3844 memcpy(&pl, r, sizeof(struct protocol_list32_t));
3845 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3846 swapStruct(pl);
3847 outs() << " count " << pl.count << "\n";
3848
3849 p += sizeof(struct protocol_list32_t);
3850 offset += sizeof(struct protocol_list32_t);
3851 for (i = 0; i < pl.count; i++) {
3852 r = get_pointer_32(p, offset, left, S, info);
3853 if (r == nullptr)
3854 return;
3855 q = 0;
3856 if (left < sizeof(uint32_t)) {
3857 memcpy(&q, r, left);
3858 outs() << " (protocol_t * entends past the end of the section)\n";
3859 } else
3860 memcpy(&q, r, sizeof(uint32_t));
3861 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3862 sys::swapByteOrder(q);
3863 outs() << "\t\t list[" << i << "] " << format("0x%" PRIx32, q)
3864 << " (struct protocol_t *)\n";
3865 r = get_pointer_32(q, offset, left, S, info);
3866 if (r == nullptr)
3867 return;
3868 memset(&pc, '\0', sizeof(struct protocol32_t));
3869 if (left < sizeof(struct protocol32_t)) {
3870 memcpy(&pc, r, left);
3871 outs() << " (protocol_t entends past the end of the section)\n";
3872 } else
3873 memcpy(&pc, r, sizeof(struct protocol32_t));
3874 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3875 swapStruct(pc);
3876 outs() << "\t\t\t isa " << format("0x%" PRIx32, pc.isa) << "\n";
3877 outs() << "\t\t\t name " << format("0x%" PRIx32, pc.name);
3878 name = get_pointer_32(pc.name, xoffset, left, xS, info);
3879 if (name != nullptr)
3880 outs() << format(" %.*s", left, name);
3881 outs() << "\n";
3882 outs() << "\t\t\tprotocols " << format("0x%" PRIx32, pc.protocols) << "\n";
3883 outs() << "\t\t instanceMethods "
3884 << format("0x%" PRIx32, pc.instanceMethods)
3885 << " (struct method_list_t *)\n";
3886 if (pc.instanceMethods != 0)
3887 print_method_list32_t(pc.instanceMethods, info, "\t");
3888 outs() << "\t\t classMethods " << format("0x%" PRIx32, pc.classMethods)
3889 << " (struct method_list_t *)\n";
3890 if (pc.classMethods != 0)
3891 print_method_list32_t(pc.classMethods, info, "\t");
3892 outs() << "\t optionalInstanceMethods "
3893 << format("0x%" PRIx32, pc.optionalInstanceMethods) << "\n";
3894 outs() << "\t optionalClassMethods "
3895 << format("0x%" PRIx32, pc.optionalClassMethods) << "\n";
3896 outs() << "\t instanceProperties "
3897 << format("0x%" PRIx32, pc.instanceProperties) << "\n";
3898 p += sizeof(uint32_t);
3899 offset += sizeof(uint32_t);
3900 }
3901}
3902
Kevin Enderby846c0002015-04-16 17:19:59 +00003903static void print_indent(uint32_t indent) {
3904 for (uint32_t i = 0; i < indent;) {
3905 if (indent - i >= 8) {
3906 outs() << "\t";
3907 i += 8;
3908 } else {
3909 for (uint32_t j = i; j < indent; j++)
3910 outs() << " ";
3911 return;
3912 }
3913 }
3914}
3915
3916static bool print_method_description_list(uint32_t p, uint32_t indent,
3917 struct DisassembleInfo *info) {
3918 uint32_t offset, left, xleft;
3919 SectionRef S;
3920 struct objc_method_description_list_t mdl;
3921 struct objc_method_description_t md;
3922 const char *r, *list, *name;
3923 int32_t i;
3924
3925 r = get_pointer_32(p, offset, left, S, info, true);
3926 if (r == nullptr)
3927 return true;
3928
3929 outs() << "\n";
3930 if (left > sizeof(struct objc_method_description_list_t)) {
3931 memcpy(&mdl, r, sizeof(struct objc_method_description_list_t));
3932 } else {
3933 print_indent(indent);
3934 outs() << " objc_method_description_list extends past end of the section\n";
3935 memset(&mdl, '\0', sizeof(struct objc_method_description_list_t));
3936 memcpy(&mdl, r, left);
3937 }
3938 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3939 swapStruct(mdl);
3940
3941 print_indent(indent);
3942 outs() << " count " << mdl.count << "\n";
3943
3944 list = r + sizeof(struct objc_method_description_list_t);
3945 for (i = 0; i < mdl.count; i++) {
3946 if ((i + 1) * sizeof(struct objc_method_description_t) > left) {
3947 print_indent(indent);
3948 outs() << " remaining list entries extend past the of the section\n";
3949 break;
3950 }
3951 print_indent(indent);
3952 outs() << " list[" << i << "]\n";
3953 memcpy(&md, list + i * sizeof(struct objc_method_description_t),
3954 sizeof(struct objc_method_description_t));
3955 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3956 swapStruct(md);
3957
3958 print_indent(indent);
3959 outs() << " name " << format("0x%08" PRIx32, md.name);
3960 if (info->verbose) {
3961 name = get_pointer_32(md.name, offset, xleft, S, info, true);
3962 if (name != nullptr)
3963 outs() << format(" %.*s", xleft, name);
3964 else
3965 outs() << " (not in an __OBJC section)";
3966 }
3967 outs() << "\n";
3968
3969 print_indent(indent);
3970 outs() << " types " << format("0x%08" PRIx32, md.types);
3971 if (info->verbose) {
3972 name = get_pointer_32(md.types, offset, xleft, S, info, true);
3973 if (name != nullptr)
3974 outs() << format(" %.*s", xleft, name);
3975 else
3976 outs() << " (not in an __OBJC section)";
3977 }
3978 outs() << "\n";
3979 }
3980 return false;
3981}
3982
3983static bool print_protocol_list(uint32_t p, uint32_t indent,
3984 struct DisassembleInfo *info);
3985
3986static bool print_protocol(uint32_t p, uint32_t indent,
3987 struct DisassembleInfo *info) {
3988 uint32_t offset, left;
3989 SectionRef S;
3990 struct objc_protocol_t protocol;
3991 const char *r, *name;
3992
3993 r = get_pointer_32(p, offset, left, S, info, true);
3994 if (r == nullptr)
3995 return true;
3996
3997 outs() << "\n";
3998 if (left >= sizeof(struct objc_protocol_t)) {
3999 memcpy(&protocol, r, sizeof(struct objc_protocol_t));
4000 } else {
4001 print_indent(indent);
4002 outs() << " Protocol extends past end of the section\n";
4003 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
4004 memcpy(&protocol, r, left);
4005 }
4006 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4007 swapStruct(protocol);
4008
4009 print_indent(indent);
4010 outs() << " isa " << format("0x%08" PRIx32, protocol.isa)
4011 << "\n";
4012
4013 print_indent(indent);
4014 outs() << " protocol_name "
4015 << format("0x%08" PRIx32, protocol.protocol_name);
4016 if (info->verbose) {
4017 name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true);
4018 if (name != nullptr)
4019 outs() << format(" %.*s", left, name);
4020 else
4021 outs() << " (not in an __OBJC section)";
4022 }
4023 outs() << "\n";
4024
4025 print_indent(indent);
4026 outs() << " protocol_list "
4027 << format("0x%08" PRIx32, protocol.protocol_list);
4028 if (print_protocol_list(protocol.protocol_list, indent + 4, info))
4029 outs() << " (not in an __OBJC section)\n";
4030
4031 print_indent(indent);
4032 outs() << " instance_methods "
4033 << format("0x%08" PRIx32, protocol.instance_methods);
4034 if (print_method_description_list(protocol.instance_methods, indent, info))
4035 outs() << " (not in an __OBJC section)\n";
4036
4037 print_indent(indent);
4038 outs() << " class_methods "
4039 << format("0x%08" PRIx32, protocol.class_methods);
4040 if (print_method_description_list(protocol.class_methods, indent, info))
4041 outs() << " (not in an __OBJC section)\n";
4042
4043 return false;
4044}
4045
4046static bool print_protocol_list(uint32_t p, uint32_t indent,
4047 struct DisassembleInfo *info) {
4048 uint32_t offset, left, l;
4049 SectionRef S;
4050 struct objc_protocol_list_t protocol_list;
4051 const char *r, *list;
4052 int32_t i;
4053
4054 r = get_pointer_32(p, offset, left, S, info, true);
4055 if (r == nullptr)
4056 return true;
4057
4058 outs() << "\n";
4059 if (left > sizeof(struct objc_protocol_list_t)) {
4060 memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t));
4061 } else {
4062 outs() << "\t\t objc_protocol_list_t extends past end of the section\n";
4063 memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t));
4064 memcpy(&protocol_list, r, left);
4065 }
4066 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4067 swapStruct(protocol_list);
4068
4069 print_indent(indent);
4070 outs() << " next " << format("0x%08" PRIx32, protocol_list.next)
4071 << "\n";
4072 print_indent(indent);
4073 outs() << " count " << protocol_list.count << "\n";
4074
4075 list = r + sizeof(struct objc_protocol_list_t);
4076 for (i = 0; i < protocol_list.count; i++) {
4077 if ((i + 1) * sizeof(uint32_t) > left) {
4078 outs() << "\t\t remaining list entries extend past the of the section\n";
4079 break;
4080 }
4081 memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t));
4082 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4083 sys::swapByteOrder(l);
4084
4085 print_indent(indent);
4086 outs() << " list[" << i << "] " << format("0x%08" PRIx32, l);
4087 if (print_protocol(l, indent, info))
4088 outs() << "(not in an __OBJC section)\n";
4089 }
4090 return false;
4091}
4092
Kevin Enderby0fc11822015-04-01 20:57:01 +00004093static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) {
4094 struct ivar_list64_t il;
4095 struct ivar64_t i;
4096 const char *r;
4097 uint32_t offset, xoffset, left, j;
4098 SectionRef S, xS;
4099 const char *name, *sym_name, *ivar_offset_p;
4100 uint64_t ivar_offset, n_value;
4101
4102 r = get_pointer_64(p, offset, left, S, info);
4103 if (r == nullptr)
4104 return;
4105 memset(&il, '\0', sizeof(struct ivar_list64_t));
4106 if (left < sizeof(struct ivar_list64_t)) {
4107 memcpy(&il, r, left);
4108 outs() << " (ivar_list_t entends past the end of the section)\n";
4109 } else
4110 memcpy(&il, r, sizeof(struct ivar_list64_t));
4111 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4112 swapStruct(il);
4113 outs() << " entsize " << il.entsize << "\n";
4114 outs() << " count " << il.count << "\n";
4115
4116 p += sizeof(struct ivar_list64_t);
4117 offset += sizeof(struct ivar_list64_t);
4118 for (j = 0; j < il.count; j++) {
4119 r = get_pointer_64(p, offset, left, S, info);
4120 if (r == nullptr)
4121 return;
4122 memset(&i, '\0', sizeof(struct ivar64_t));
4123 if (left < sizeof(struct ivar64_t)) {
4124 memcpy(&i, r, left);
4125 outs() << " (ivar_t entends past the end of the section)\n";
4126 } else
4127 memcpy(&i, r, sizeof(struct ivar64_t));
4128 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4129 swapStruct(i);
4130
4131 outs() << "\t\t\t offset ";
4132 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset), S,
4133 info, n_value, i.offset);
4134 if (n_value != 0) {
4135 if (info->verbose && sym_name != nullptr)
4136 outs() << sym_name;
4137 else
4138 outs() << format("0x%" PRIx64, n_value);
4139 if (i.offset != 0)
4140 outs() << " + " << format("0x%" PRIx64, i.offset);
4141 } else
4142 outs() << format("0x%" PRIx64, i.offset);
4143 ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info);
4144 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4145 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4146 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4147 sys::swapByteOrder(ivar_offset);
4148 outs() << " " << ivar_offset << "\n";
4149 } else
4150 outs() << "\n";
4151
4152 outs() << "\t\t\t name ";
4153 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name), S, info,
4154 n_value, i.name);
4155 if (n_value != 0) {
4156 if (info->verbose && sym_name != nullptr)
4157 outs() << sym_name;
4158 else
4159 outs() << format("0x%" PRIx64, n_value);
4160 if (i.name != 0)
4161 outs() << " + " << format("0x%" PRIx64, i.name);
4162 } else
4163 outs() << format("0x%" PRIx64, i.name);
4164 name = get_pointer_64(i.name + n_value, xoffset, left, xS, info);
4165 if (name != nullptr)
4166 outs() << format(" %.*s", left, name);
4167 outs() << "\n";
4168
4169 outs() << "\t\t\t type ";
4170 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type), S, info,
4171 n_value, i.name);
4172 name = get_pointer_64(i.type + n_value, xoffset, left, xS, info);
4173 if (n_value != 0) {
4174 if (info->verbose && sym_name != nullptr)
4175 outs() << sym_name;
4176 else
4177 outs() << format("0x%" PRIx64, n_value);
4178 if (i.type != 0)
4179 outs() << " + " << format("0x%" PRIx64, i.type);
4180 } else
4181 outs() << format("0x%" PRIx64, i.type);
4182 if (name != nullptr)
4183 outs() << format(" %.*s", left, name);
4184 outs() << "\n";
4185
4186 outs() << "\t\t\talignment " << i.alignment << "\n";
4187 outs() << "\t\t\t size " << i.size << "\n";
4188
4189 p += sizeof(struct ivar64_t);
4190 offset += sizeof(struct ivar64_t);
4191 }
4192}
4193
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004194static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) {
4195 struct ivar_list32_t il;
4196 struct ivar32_t i;
4197 const char *r;
4198 uint32_t offset, xoffset, left, j;
4199 SectionRef S, xS;
4200 const char *name, *ivar_offset_p;
4201 uint32_t ivar_offset;
4202
4203 r = get_pointer_32(p, offset, left, S, info);
4204 if (r == nullptr)
4205 return;
4206 memset(&il, '\0', sizeof(struct ivar_list32_t));
4207 if (left < sizeof(struct ivar_list32_t)) {
4208 memcpy(&il, r, left);
4209 outs() << " (ivar_list_t entends past the end of the section)\n";
4210 } else
4211 memcpy(&il, r, sizeof(struct ivar_list32_t));
4212 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4213 swapStruct(il);
4214 outs() << " entsize " << il.entsize << "\n";
4215 outs() << " count " << il.count << "\n";
4216
4217 p += sizeof(struct ivar_list32_t);
4218 offset += sizeof(struct ivar_list32_t);
4219 for (j = 0; j < il.count; j++) {
4220 r = get_pointer_32(p, offset, left, S, info);
4221 if (r == nullptr)
4222 return;
4223 memset(&i, '\0', sizeof(struct ivar32_t));
4224 if (left < sizeof(struct ivar32_t)) {
4225 memcpy(&i, r, left);
4226 outs() << " (ivar_t entends past the end of the section)\n";
4227 } else
4228 memcpy(&i, r, sizeof(struct ivar32_t));
4229 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4230 swapStruct(i);
4231
4232 outs() << "\t\t\t offset " << format("0x%" PRIx32, i.offset);
4233 ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info);
4234 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4235 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4236 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4237 sys::swapByteOrder(ivar_offset);
4238 outs() << " " << ivar_offset << "\n";
4239 } else
4240 outs() << "\n";
4241
4242 outs() << "\t\t\t name " << format("0x%" PRIx32, i.name);
4243 name = get_pointer_32(i.name, xoffset, left, xS, info);
4244 if (name != nullptr)
4245 outs() << format(" %.*s", left, name);
4246 outs() << "\n";
4247
4248 outs() << "\t\t\t type " << format("0x%" PRIx32, i.type);
4249 name = get_pointer_32(i.type, xoffset, left, xS, info);
4250 if (name != nullptr)
4251 outs() << format(" %.*s", left, name);
4252 outs() << "\n";
4253
4254 outs() << "\t\t\talignment " << i.alignment << "\n";
4255 outs() << "\t\t\t size " << i.size << "\n";
4256
4257 p += sizeof(struct ivar32_t);
4258 offset += sizeof(struct ivar32_t);
4259 }
4260}
4261
Kevin Enderby0fc11822015-04-01 20:57:01 +00004262static void print_objc_property_list64(uint64_t p,
4263 struct DisassembleInfo *info) {
4264 struct objc_property_list64 opl;
4265 struct objc_property64 op;
4266 const char *r;
4267 uint32_t offset, xoffset, left, j;
4268 SectionRef S, xS;
4269 const char *name, *sym_name;
4270 uint64_t n_value;
4271
4272 r = get_pointer_64(p, offset, left, S, info);
4273 if (r == nullptr)
4274 return;
4275 memset(&opl, '\0', sizeof(struct objc_property_list64));
4276 if (left < sizeof(struct objc_property_list64)) {
4277 memcpy(&opl, r, left);
4278 outs() << " (objc_property_list entends past the end of the section)\n";
4279 } else
4280 memcpy(&opl, r, sizeof(struct objc_property_list64));
4281 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4282 swapStruct(opl);
4283 outs() << " entsize " << opl.entsize << "\n";
4284 outs() << " count " << opl.count << "\n";
4285
4286 p += sizeof(struct objc_property_list64);
4287 offset += sizeof(struct objc_property_list64);
4288 for (j = 0; j < opl.count; j++) {
4289 r = get_pointer_64(p, offset, left, S, info);
4290 if (r == nullptr)
4291 return;
4292 memset(&op, '\0', sizeof(struct objc_property64));
4293 if (left < sizeof(struct objc_property64)) {
4294 memcpy(&op, r, left);
4295 outs() << " (objc_property entends past the end of the section)\n";
4296 } else
4297 memcpy(&op, r, sizeof(struct objc_property64));
4298 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4299 swapStruct(op);
4300
4301 outs() << "\t\t\t name ";
4302 sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name), S,
4303 info, n_value, op.name);
4304 if (n_value != 0) {
4305 if (info->verbose && sym_name != nullptr)
4306 outs() << sym_name;
4307 else
4308 outs() << format("0x%" PRIx64, n_value);
4309 if (op.name != 0)
4310 outs() << " + " << format("0x%" PRIx64, op.name);
4311 } else
4312 outs() << format("0x%" PRIx64, op.name);
4313 name = get_pointer_64(op.name + n_value, xoffset, left, xS, info);
4314 if (name != nullptr)
4315 outs() << format(" %.*s", left, name);
4316 outs() << "\n";
4317
4318 outs() << "\t\t\tattributes ";
4319 sym_name =
4320 get_symbol_64(offset + offsetof(struct objc_property64, attributes), S,
4321 info, n_value, op.attributes);
4322 if (n_value != 0) {
4323 if (info->verbose && sym_name != nullptr)
4324 outs() << sym_name;
4325 else
4326 outs() << format("0x%" PRIx64, n_value);
4327 if (op.attributes != 0)
4328 outs() << " + " << format("0x%" PRIx64, op.attributes);
4329 } else
4330 outs() << format("0x%" PRIx64, op.attributes);
4331 name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info);
4332 if (name != nullptr)
4333 outs() << format(" %.*s", left, name);
4334 outs() << "\n";
4335
4336 p += sizeof(struct objc_property64);
4337 offset += sizeof(struct objc_property64);
4338 }
4339}
4340
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004341static void print_objc_property_list32(uint32_t p,
4342 struct DisassembleInfo *info) {
4343 struct objc_property_list32 opl;
4344 struct objc_property32 op;
4345 const char *r;
4346 uint32_t offset, xoffset, left, j;
4347 SectionRef S, xS;
4348 const char *name;
4349
4350 r = get_pointer_32(p, offset, left, S, info);
4351 if (r == nullptr)
4352 return;
4353 memset(&opl, '\0', sizeof(struct objc_property_list32));
4354 if (left < sizeof(struct objc_property_list32)) {
4355 memcpy(&opl, r, left);
4356 outs() << " (objc_property_list entends past the end of the section)\n";
4357 } else
4358 memcpy(&opl, r, sizeof(struct objc_property_list32));
4359 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4360 swapStruct(opl);
4361 outs() << " entsize " << opl.entsize << "\n";
4362 outs() << " count " << opl.count << "\n";
4363
4364 p += sizeof(struct objc_property_list32);
4365 offset += sizeof(struct objc_property_list32);
4366 for (j = 0; j < opl.count; j++) {
4367 r = get_pointer_32(p, offset, left, S, info);
4368 if (r == nullptr)
4369 return;
4370 memset(&op, '\0', sizeof(struct objc_property32));
4371 if (left < sizeof(struct objc_property32)) {
4372 memcpy(&op, r, left);
4373 outs() << " (objc_property entends past the end of the section)\n";
4374 } else
4375 memcpy(&op, r, sizeof(struct objc_property32));
4376 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4377 swapStruct(op);
4378
4379 outs() << "\t\t\t name " << format("0x%" PRIx32, op.name);
4380 name = get_pointer_32(op.name, xoffset, left, xS, info);
4381 if (name != nullptr)
4382 outs() << format(" %.*s", left, name);
4383 outs() << "\n";
4384
4385 outs() << "\t\t\tattributes " << format("0x%" PRIx32, op.attributes);
4386 name = get_pointer_32(op.attributes, xoffset, left, xS, info);
4387 if (name != nullptr)
4388 outs() << format(" %.*s", left, name);
4389 outs() << "\n";
4390
4391 p += sizeof(struct objc_property32);
4392 offset += sizeof(struct objc_property32);
4393 }
4394}
4395
Richard Smith81ff44d2015-10-09 22:09:56 +00004396static bool print_class_ro64_t(uint64_t p, struct DisassembleInfo *info,
Kevin Enderby0fc11822015-04-01 20:57:01 +00004397 bool &is_meta_class) {
4398 struct class_ro64_t cro;
4399 const char *r;
4400 uint32_t offset, xoffset, left;
4401 SectionRef S, xS;
4402 const char *name, *sym_name;
4403 uint64_t n_value;
4404
4405 r = get_pointer_64(p, offset, left, S, info);
4406 if (r == nullptr || left < sizeof(struct class_ro64_t))
Richard Smith81ff44d2015-10-09 22:09:56 +00004407 return false;
Kevin Enderby0fc11822015-04-01 20:57:01 +00004408 memset(&cro, '\0', sizeof(struct class_ro64_t));
4409 if (left < sizeof(struct class_ro64_t)) {
4410 memcpy(&cro, r, left);
4411 outs() << " (class_ro_t entends past the end of the section)\n";
4412 } else
4413 memcpy(&cro, r, sizeof(struct class_ro64_t));
4414 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4415 swapStruct(cro);
4416 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4417 if (cro.flags & RO_META)
4418 outs() << " RO_META";
4419 if (cro.flags & RO_ROOT)
4420 outs() << " RO_ROOT";
4421 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4422 outs() << " RO_HAS_CXX_STRUCTORS";
4423 outs() << "\n";
4424 outs() << " instanceStart " << cro.instanceStart << "\n";
4425 outs() << " instanceSize " << cro.instanceSize << "\n";
4426 outs() << " reserved " << format("0x%" PRIx32, cro.reserved)
4427 << "\n";
4428 outs() << " ivarLayout " << format("0x%" PRIx64, cro.ivarLayout)
4429 << "\n";
4430 print_layout_map64(cro.ivarLayout, info);
4431
4432 outs() << " name ";
4433 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name), S,
4434 info, n_value, cro.name);
4435 if (n_value != 0) {
4436 if (info->verbose && sym_name != nullptr)
4437 outs() << sym_name;
4438 else
4439 outs() << format("0x%" PRIx64, n_value);
4440 if (cro.name != 0)
4441 outs() << " + " << format("0x%" PRIx64, cro.name);
4442 } else
4443 outs() << format("0x%" PRIx64, cro.name);
4444 name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info);
4445 if (name != nullptr)
4446 outs() << format(" %.*s", left, name);
4447 outs() << "\n";
4448
4449 outs() << " baseMethods ";
4450 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods),
4451 S, info, n_value, cro.baseMethods);
4452 if (n_value != 0) {
4453 if (info->verbose && sym_name != nullptr)
4454 outs() << sym_name;
4455 else
4456 outs() << format("0x%" PRIx64, n_value);
4457 if (cro.baseMethods != 0)
4458 outs() << " + " << format("0x%" PRIx64, cro.baseMethods);
4459 } else
4460 outs() << format("0x%" PRIx64, cro.baseMethods);
4461 outs() << " (struct method_list_t *)\n";
4462 if (cro.baseMethods + n_value != 0)
4463 print_method_list64_t(cro.baseMethods + n_value, info, "");
4464
4465 outs() << " baseProtocols ";
4466 sym_name =
4467 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols), S,
4468 info, n_value, cro.baseProtocols);
4469 if (n_value != 0) {
4470 if (info->verbose && sym_name != nullptr)
4471 outs() << sym_name;
4472 else
4473 outs() << format("0x%" PRIx64, n_value);
4474 if (cro.baseProtocols != 0)
4475 outs() << " + " << format("0x%" PRIx64, cro.baseProtocols);
4476 } else
4477 outs() << format("0x%" PRIx64, cro.baseProtocols);
4478 outs() << "\n";
4479 if (cro.baseProtocols + n_value != 0)
4480 print_protocol_list64_t(cro.baseProtocols + n_value, info);
4481
4482 outs() << " ivars ";
4483 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars), S,
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004484 info, n_value, cro.ivars);
Kevin Enderby0fc11822015-04-01 20:57:01 +00004485 if (n_value != 0) {
4486 if (info->verbose && sym_name != nullptr)
4487 outs() << sym_name;
4488 else
4489 outs() << format("0x%" PRIx64, n_value);
4490 if (cro.ivars != 0)
4491 outs() << " + " << format("0x%" PRIx64, cro.ivars);
4492 } else
4493 outs() << format("0x%" PRIx64, cro.ivars);
4494 outs() << "\n";
4495 if (cro.ivars + n_value != 0)
4496 print_ivar_list64_t(cro.ivars + n_value, info);
4497
4498 outs() << " weakIvarLayout ";
4499 sym_name =
4500 get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout), S,
4501 info, n_value, cro.weakIvarLayout);
4502 if (n_value != 0) {
4503 if (info->verbose && sym_name != nullptr)
4504 outs() << sym_name;
4505 else
4506 outs() << format("0x%" PRIx64, n_value);
4507 if (cro.weakIvarLayout != 0)
4508 outs() << " + " << format("0x%" PRIx64, cro.weakIvarLayout);
4509 } else
4510 outs() << format("0x%" PRIx64, cro.weakIvarLayout);
4511 outs() << "\n";
4512 print_layout_map64(cro.weakIvarLayout + n_value, info);
4513
4514 outs() << " baseProperties ";
4515 sym_name =
4516 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties), S,
4517 info, n_value, cro.baseProperties);
4518 if (n_value != 0) {
4519 if (info->verbose && sym_name != nullptr)
4520 outs() << sym_name;
4521 else
4522 outs() << format("0x%" PRIx64, n_value);
4523 if (cro.baseProperties != 0)
4524 outs() << " + " << format("0x%" PRIx64, cro.baseProperties);
4525 } else
4526 outs() << format("0x%" PRIx64, cro.baseProperties);
4527 outs() << "\n";
4528 if (cro.baseProperties + n_value != 0)
4529 print_objc_property_list64(cro.baseProperties + n_value, info);
4530
Rafael Espindolab9091322015-10-24 23:19:10 +00004531 is_meta_class = (cro.flags & RO_META) != 0;
Richard Smith81ff44d2015-10-09 22:09:56 +00004532 return true;
Kevin Enderby0fc11822015-04-01 20:57:01 +00004533}
4534
Richard Smith81ff44d2015-10-09 22:09:56 +00004535static bool print_class_ro32_t(uint32_t p, struct DisassembleInfo *info,
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004536 bool &is_meta_class) {
4537 struct class_ro32_t cro;
4538 const char *r;
4539 uint32_t offset, xoffset, left;
4540 SectionRef S, xS;
4541 const char *name;
4542
4543 r = get_pointer_32(p, offset, left, S, info);
4544 if (r == nullptr)
Richard Smith81ff44d2015-10-09 22:09:56 +00004545 return false;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004546 memset(&cro, '\0', sizeof(struct class_ro32_t));
4547 if (left < sizeof(struct class_ro32_t)) {
4548 memcpy(&cro, r, left);
4549 outs() << " (class_ro_t entends past the end of the section)\n";
4550 } else
4551 memcpy(&cro, r, sizeof(struct class_ro32_t));
4552 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4553 swapStruct(cro);
4554 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4555 if (cro.flags & RO_META)
4556 outs() << " RO_META";
4557 if (cro.flags & RO_ROOT)
4558 outs() << " RO_ROOT";
4559 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4560 outs() << " RO_HAS_CXX_STRUCTORS";
4561 outs() << "\n";
4562 outs() << " instanceStart " << cro.instanceStart << "\n";
4563 outs() << " instanceSize " << cro.instanceSize << "\n";
4564 outs() << " ivarLayout " << format("0x%" PRIx32, cro.ivarLayout)
4565 << "\n";
4566 print_layout_map32(cro.ivarLayout, info);
4567
4568 outs() << " name " << format("0x%" PRIx32, cro.name);
4569 name = get_pointer_32(cro.name, xoffset, left, xS, info);
4570 if (name != nullptr)
4571 outs() << format(" %.*s", left, name);
4572 outs() << "\n";
4573
4574 outs() << " baseMethods "
4575 << format("0x%" PRIx32, cro.baseMethods)
4576 << " (struct method_list_t *)\n";
4577 if (cro.baseMethods != 0)
4578 print_method_list32_t(cro.baseMethods, info, "");
4579
4580 outs() << " baseProtocols "
4581 << format("0x%" PRIx32, cro.baseProtocols) << "\n";
4582 if (cro.baseProtocols != 0)
4583 print_protocol_list32_t(cro.baseProtocols, info);
4584 outs() << " ivars " << format("0x%" PRIx32, cro.ivars)
4585 << "\n";
4586 if (cro.ivars != 0)
4587 print_ivar_list32_t(cro.ivars, info);
4588 outs() << " weakIvarLayout "
4589 << format("0x%" PRIx32, cro.weakIvarLayout) << "\n";
4590 print_layout_map32(cro.weakIvarLayout, info);
4591 outs() << " baseProperties "
4592 << format("0x%" PRIx32, cro.baseProperties) << "\n";
4593 if (cro.baseProperties != 0)
4594 print_objc_property_list32(cro.baseProperties, info);
Rafael Espindolab9091322015-10-24 23:19:10 +00004595 is_meta_class = (cro.flags & RO_META) != 0;
Richard Smith81ff44d2015-10-09 22:09:56 +00004596 return true;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004597}
4598
Kevin Enderby0fc11822015-04-01 20:57:01 +00004599static void print_class64_t(uint64_t p, struct DisassembleInfo *info) {
4600 struct class64_t c;
4601 const char *r;
4602 uint32_t offset, left;
4603 SectionRef S;
4604 const char *name;
4605 uint64_t isa_n_value, n_value;
4606
4607 r = get_pointer_64(p, offset, left, S, info);
4608 if (r == nullptr || left < sizeof(struct class64_t))
4609 return;
4610 memset(&c, '\0', sizeof(struct class64_t));
4611 if (left < sizeof(struct class64_t)) {
4612 memcpy(&c, r, left);
4613 outs() << " (class_t entends past the end of the section)\n";
4614 } else
4615 memcpy(&c, r, sizeof(struct class64_t));
4616 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4617 swapStruct(c);
4618
4619 outs() << " isa " << format("0x%" PRIx64, c.isa);
4620 name = get_symbol_64(offset + offsetof(struct class64_t, isa), S, info,
4621 isa_n_value, c.isa);
4622 if (name != nullptr)
4623 outs() << " " << name;
4624 outs() << "\n";
4625
4626 outs() << " superclass " << format("0x%" PRIx64, c.superclass);
4627 name = get_symbol_64(offset + offsetof(struct class64_t, superclass), S, info,
4628 n_value, c.superclass);
4629 if (name != nullptr)
4630 outs() << " " << name;
4631 outs() << "\n";
4632
4633 outs() << " cache " << format("0x%" PRIx64, c.cache);
4634 name = get_symbol_64(offset + offsetof(struct class64_t, cache), S, info,
4635 n_value, c.cache);
4636 if (name != nullptr)
4637 outs() << " " << name;
4638 outs() << "\n";
4639
4640 outs() << " vtable " << format("0x%" PRIx64, c.vtable);
4641 name = get_symbol_64(offset + offsetof(struct class64_t, vtable), S, info,
4642 n_value, c.vtable);
4643 if (name != nullptr)
4644 outs() << " " << name;
4645 outs() << "\n";
4646
4647 name = get_symbol_64(offset + offsetof(struct class64_t, data), S, info,
4648 n_value, c.data);
4649 outs() << " data ";
4650 if (n_value != 0) {
4651 if (info->verbose && name != nullptr)
4652 outs() << name;
4653 else
4654 outs() << format("0x%" PRIx64, n_value);
4655 if (c.data != 0)
4656 outs() << " + " << format("0x%" PRIx64, c.data);
4657 } else
4658 outs() << format("0x%" PRIx64, c.data);
4659 outs() << " (struct class_ro_t *)";
4660
4661 // This is a Swift class if some of the low bits of the pointer are set.
4662 if ((c.data + n_value) & 0x7)
4663 outs() << " Swift class";
4664 outs() << "\n";
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004665 bool is_meta_class;
Richard Smith81ff44d2015-10-09 22:09:56 +00004666 if (!print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class))
4667 return;
Kevin Enderby0fc11822015-04-01 20:57:01 +00004668
Kevin Enderbyaac75382015-10-08 16:56:35 +00004669 if (!is_meta_class &&
4670 c.isa + isa_n_value != p &&
4671 c.isa + isa_n_value != 0 &&
4672 info->depth < 100) {
4673 info->depth++;
4674 outs() << "Meta Class\n";
4675 print_class64_t(c.isa + isa_n_value, info);
Kevin Enderby0fc11822015-04-01 20:57:01 +00004676 }
4677}
4678
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004679static void print_class32_t(uint32_t p, struct DisassembleInfo *info) {
4680 struct class32_t c;
4681 const char *r;
4682 uint32_t offset, left;
4683 SectionRef S;
4684 const char *name;
4685
4686 r = get_pointer_32(p, offset, left, S, info);
4687 if (r == nullptr)
4688 return;
4689 memset(&c, '\0', sizeof(struct class32_t));
4690 if (left < sizeof(struct class32_t)) {
4691 memcpy(&c, r, left);
4692 outs() << " (class_t entends past the end of the section)\n";
4693 } else
4694 memcpy(&c, r, sizeof(struct class32_t));
4695 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4696 swapStruct(c);
4697
4698 outs() << " isa " << format("0x%" PRIx32, c.isa);
4699 name =
4700 get_symbol_32(offset + offsetof(struct class32_t, isa), S, info, c.isa);
4701 if (name != nullptr)
4702 outs() << " " << name;
4703 outs() << "\n";
4704
4705 outs() << " superclass " << format("0x%" PRIx32, c.superclass);
4706 name = get_symbol_32(offset + offsetof(struct class32_t, superclass), S, info,
4707 c.superclass);
4708 if (name != nullptr)
4709 outs() << " " << name;
4710 outs() << "\n";
4711
4712 outs() << " cache " << format("0x%" PRIx32, c.cache);
4713 name = get_symbol_32(offset + offsetof(struct class32_t, cache), S, info,
4714 c.cache);
4715 if (name != nullptr)
4716 outs() << " " << name;
4717 outs() << "\n";
4718
4719 outs() << " vtable " << format("0x%" PRIx32, c.vtable);
4720 name = get_symbol_32(offset + offsetof(struct class32_t, vtable), S, info,
4721 c.vtable);
4722 if (name != nullptr)
4723 outs() << " " << name;
4724 outs() << "\n";
4725
4726 name =
4727 get_symbol_32(offset + offsetof(struct class32_t, data), S, info, c.data);
4728 outs() << " data " << format("0x%" PRIx32, c.data)
4729 << " (struct class_ro_t *)";
4730
4731 // This is a Swift class if some of the low bits of the pointer are set.
4732 if (c.data & 0x3)
4733 outs() << " Swift class";
4734 outs() << "\n";
4735 bool is_meta_class;
Richard Smith81ff44d2015-10-09 22:09:56 +00004736 if (!print_class_ro32_t(c.data & ~0x3, info, is_meta_class))
4737 return;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004738
Hans Wennborgcc9deb42015-09-29 18:02:48 +00004739 if (!is_meta_class) {
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004740 outs() << "Meta Class\n";
4741 print_class32_t(c.isa, info);
4742 }
4743}
4744
Kevin Enderby846c0002015-04-16 17:19:59 +00004745static void print_objc_class_t(struct objc_class_t *objc_class,
4746 struct DisassembleInfo *info) {
4747 uint32_t offset, left, xleft;
4748 const char *name, *p, *ivar_list;
4749 SectionRef S;
4750 int32_t i;
4751 struct objc_ivar_list_t objc_ivar_list;
4752 struct objc_ivar_t ivar;
4753
4754 outs() << "\t\t isa " << format("0x%08" PRIx32, objc_class->isa);
4755 if (info->verbose && CLS_GETINFO(objc_class, CLS_META)) {
4756 name = get_pointer_32(objc_class->isa, offset, left, S, info, true);
4757 if (name != nullptr)
4758 outs() << format(" %.*s", left, name);
4759 else
4760 outs() << " (not in an __OBJC section)";
4761 }
4762 outs() << "\n";
4763
4764 outs() << "\t super_class "
4765 << format("0x%08" PRIx32, objc_class->super_class);
4766 if (info->verbose) {
4767 name = get_pointer_32(objc_class->super_class, offset, left, S, info, true);
4768 if (name != nullptr)
4769 outs() << format(" %.*s", left, name);
4770 else
4771 outs() << " (not in an __OBJC section)";
4772 }
4773 outs() << "\n";
4774
4775 outs() << "\t\t name " << format("0x%08" PRIx32, objc_class->name);
4776 if (info->verbose) {
4777 name = get_pointer_32(objc_class->name, offset, left, S, info, true);
4778 if (name != nullptr)
4779 outs() << format(" %.*s", left, name);
4780 else
4781 outs() << " (not in an __OBJC section)";
4782 }
4783 outs() << "\n";
4784
4785 outs() << "\t\t version " << format("0x%08" PRIx32, objc_class->version)
4786 << "\n";
4787
4788 outs() << "\t\t info " << format("0x%08" PRIx32, objc_class->info);
4789 if (info->verbose) {
4790 if (CLS_GETINFO(objc_class, CLS_CLASS))
4791 outs() << " CLS_CLASS";
4792 else if (CLS_GETINFO(objc_class, CLS_META))
4793 outs() << " CLS_META";
4794 }
4795 outs() << "\n";
4796
4797 outs() << "\t instance_size "
4798 << format("0x%08" PRIx32, objc_class->instance_size) << "\n";
4799
4800 p = get_pointer_32(objc_class->ivars, offset, left, S, info, true);
4801 outs() << "\t\t ivars " << format("0x%08" PRIx32, objc_class->ivars);
4802 if (p != nullptr) {
4803 if (left > sizeof(struct objc_ivar_list_t)) {
4804 outs() << "\n";
4805 memcpy(&objc_ivar_list, p, sizeof(struct objc_ivar_list_t));
4806 } else {
4807 outs() << " (entends past the end of the section)\n";
4808 memset(&objc_ivar_list, '\0', sizeof(struct objc_ivar_list_t));
4809 memcpy(&objc_ivar_list, p, left);
4810 }
4811 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4812 swapStruct(objc_ivar_list);
4813 outs() << "\t\t ivar_count " << objc_ivar_list.ivar_count << "\n";
4814 ivar_list = p + sizeof(struct objc_ivar_list_t);
4815 for (i = 0; i < objc_ivar_list.ivar_count; i++) {
4816 if ((i + 1) * sizeof(struct objc_ivar_t) > left) {
4817 outs() << "\t\t remaining ivar's extend past the of the section\n";
4818 break;
4819 }
4820 memcpy(&ivar, ivar_list + i * sizeof(struct objc_ivar_t),
4821 sizeof(struct objc_ivar_t));
4822 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4823 swapStruct(ivar);
4824
4825 outs() << "\t\t\tivar_name " << format("0x%08" PRIx32, ivar.ivar_name);
4826 if (info->verbose) {
4827 name = get_pointer_32(ivar.ivar_name, offset, xleft, S, info, true);
4828 if (name != nullptr)
4829 outs() << format(" %.*s", xleft, name);
4830 else
4831 outs() << " (not in an __OBJC section)";
4832 }
4833 outs() << "\n";
4834
4835 outs() << "\t\t\tivar_type " << format("0x%08" PRIx32, ivar.ivar_type);
4836 if (info->verbose) {
4837 name = get_pointer_32(ivar.ivar_type, offset, xleft, S, info, true);
4838 if (name != nullptr)
4839 outs() << format(" %.*s", xleft, name);
4840 else
4841 outs() << " (not in an __OBJC section)";
4842 }
4843 outs() << "\n";
4844
4845 outs() << "\t\t ivar_offset "
4846 << format("0x%08" PRIx32, ivar.ivar_offset) << "\n";
4847 }
4848 } else {
4849 outs() << " (not in an __OBJC section)\n";
4850 }
4851
4852 outs() << "\t\t methods " << format("0x%08" PRIx32, objc_class->methodLists);
4853 if (print_method_list(objc_class->methodLists, info))
4854 outs() << " (not in an __OBJC section)\n";
4855
4856 outs() << "\t\t cache " << format("0x%08" PRIx32, objc_class->cache)
4857 << "\n";
4858
4859 outs() << "\t\tprotocols " << format("0x%08" PRIx32, objc_class->protocols);
4860 if (print_protocol_list(objc_class->protocols, 16, info))
4861 outs() << " (not in an __OBJC section)\n";
4862}
4863
4864static void print_objc_objc_category_t(struct objc_category_t *objc_category,
4865 struct DisassembleInfo *info) {
4866 uint32_t offset, left;
4867 const char *name;
4868 SectionRef S;
4869
4870 outs() << "\t category name "
4871 << format("0x%08" PRIx32, objc_category->category_name);
4872 if (info->verbose) {
4873 name = get_pointer_32(objc_category->category_name, offset, left, S, info,
4874 true);
4875 if (name != nullptr)
4876 outs() << format(" %.*s", left, name);
4877 else
4878 outs() << " (not in an __OBJC section)";
4879 }
4880 outs() << "\n";
4881
4882 outs() << "\t\t class name "
4883 << format("0x%08" PRIx32, objc_category->class_name);
4884 if (info->verbose) {
4885 name =
4886 get_pointer_32(objc_category->class_name, offset, left, S, info, true);
4887 if (name != nullptr)
4888 outs() << format(" %.*s", left, name);
4889 else
4890 outs() << " (not in an __OBJC section)";
4891 }
4892 outs() << "\n";
4893
4894 outs() << "\t instance methods "
4895 << format("0x%08" PRIx32, objc_category->instance_methods);
4896 if (print_method_list(objc_category->instance_methods, info))
4897 outs() << " (not in an __OBJC section)\n";
4898
4899 outs() << "\t class methods "
4900 << format("0x%08" PRIx32, objc_category->class_methods);
4901 if (print_method_list(objc_category->class_methods, info))
4902 outs() << " (not in an __OBJC section)\n";
4903}
4904
Kevin Enderby0fc11822015-04-01 20:57:01 +00004905static void print_category64_t(uint64_t p, struct DisassembleInfo *info) {
4906 struct category64_t c;
4907 const char *r;
4908 uint32_t offset, xoffset, left;
4909 SectionRef S, xS;
4910 const char *name, *sym_name;
4911 uint64_t n_value;
4912
4913 r = get_pointer_64(p, offset, left, S, info);
4914 if (r == nullptr)
4915 return;
4916 memset(&c, '\0', sizeof(struct category64_t));
4917 if (left < sizeof(struct category64_t)) {
4918 memcpy(&c, r, left);
4919 outs() << " (category_t entends past the end of the section)\n";
4920 } else
4921 memcpy(&c, r, sizeof(struct category64_t));
4922 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4923 swapStruct(c);
4924
4925 outs() << " name ";
4926 sym_name = get_symbol_64(offset + offsetof(struct category64_t, name), S,
4927 info, n_value, c.name);
4928 if (n_value != 0) {
4929 if (info->verbose && sym_name != nullptr)
4930 outs() << sym_name;
4931 else
4932 outs() << format("0x%" PRIx64, n_value);
4933 if (c.name != 0)
4934 outs() << " + " << format("0x%" PRIx64, c.name);
4935 } else
4936 outs() << format("0x%" PRIx64, c.name);
4937 name = get_pointer_64(c.name + n_value, xoffset, left, xS, info);
4938 if (name != nullptr)
4939 outs() << format(" %.*s", left, name);
4940 outs() << "\n";
4941
4942 outs() << " cls ";
4943 sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls), S, info,
4944 n_value, c.cls);
4945 if (n_value != 0) {
4946 if (info->verbose && sym_name != nullptr)
4947 outs() << sym_name;
4948 else
4949 outs() << format("0x%" PRIx64, n_value);
4950 if (c.cls != 0)
4951 outs() << " + " << format("0x%" PRIx64, c.cls);
4952 } else
4953 outs() << format("0x%" PRIx64, c.cls);
4954 outs() << "\n";
4955 if (c.cls + n_value != 0)
4956 print_class64_t(c.cls + n_value, info);
4957
4958 outs() << " instanceMethods ";
4959 sym_name =
4960 get_symbol_64(offset + offsetof(struct category64_t, instanceMethods), S,
4961 info, n_value, c.instanceMethods);
4962 if (n_value != 0) {
4963 if (info->verbose && sym_name != nullptr)
4964 outs() << sym_name;
4965 else
4966 outs() << format("0x%" PRIx64, n_value);
4967 if (c.instanceMethods != 0)
4968 outs() << " + " << format("0x%" PRIx64, c.instanceMethods);
4969 } else
4970 outs() << format("0x%" PRIx64, c.instanceMethods);
4971 outs() << "\n";
4972 if (c.instanceMethods + n_value != 0)
4973 print_method_list64_t(c.instanceMethods + n_value, info, "");
4974
4975 outs() << " classMethods ";
4976 sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods),
4977 S, info, n_value, c.classMethods);
4978 if (n_value != 0) {
4979 if (info->verbose && sym_name != nullptr)
4980 outs() << sym_name;
4981 else
4982 outs() << format("0x%" PRIx64, n_value);
4983 if (c.classMethods != 0)
4984 outs() << " + " << format("0x%" PRIx64, c.classMethods);
4985 } else
4986 outs() << format("0x%" PRIx64, c.classMethods);
4987 outs() << "\n";
4988 if (c.classMethods + n_value != 0)
4989 print_method_list64_t(c.classMethods + n_value, info, "");
4990
4991 outs() << " protocols ";
4992 sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols), S,
4993 info, n_value, c.protocols);
4994 if (n_value != 0) {
4995 if (info->verbose && sym_name != nullptr)
4996 outs() << sym_name;
4997 else
4998 outs() << format("0x%" PRIx64, n_value);
4999 if (c.protocols != 0)
5000 outs() << " + " << format("0x%" PRIx64, c.protocols);
5001 } else
5002 outs() << format("0x%" PRIx64, c.protocols);
5003 outs() << "\n";
5004 if (c.protocols + n_value != 0)
5005 print_protocol_list64_t(c.protocols + n_value, info);
5006
5007 outs() << "instanceProperties ";
5008 sym_name =
5009 get_symbol_64(offset + offsetof(struct category64_t, instanceProperties),
5010 S, info, n_value, c.instanceProperties);
5011 if (n_value != 0) {
5012 if (info->verbose && sym_name != nullptr)
5013 outs() << sym_name;
5014 else
5015 outs() << format("0x%" PRIx64, n_value);
5016 if (c.instanceProperties != 0)
5017 outs() << " + " << format("0x%" PRIx64, c.instanceProperties);
5018 } else
5019 outs() << format("0x%" PRIx64, c.instanceProperties);
5020 outs() << "\n";
5021 if (c.instanceProperties + n_value != 0)
5022 print_objc_property_list64(c.instanceProperties + n_value, info);
5023}
5024
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005025static void print_category32_t(uint32_t p, struct DisassembleInfo *info) {
5026 struct category32_t c;
5027 const char *r;
5028 uint32_t offset, left;
5029 SectionRef S, xS;
5030 const char *name;
5031
5032 r = get_pointer_32(p, offset, left, S, info);
5033 if (r == nullptr)
5034 return;
5035 memset(&c, '\0', sizeof(struct category32_t));
5036 if (left < sizeof(struct category32_t)) {
5037 memcpy(&c, r, left);
5038 outs() << " (category_t entends past the end of the section)\n";
5039 } else
5040 memcpy(&c, r, sizeof(struct category32_t));
5041 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5042 swapStruct(c);
5043
5044 outs() << " name " << format("0x%" PRIx32, c.name);
5045 name = get_symbol_32(offset + offsetof(struct category32_t, name), S, info,
5046 c.name);
Hans Wennborgcc9deb42015-09-29 18:02:48 +00005047 if (name)
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005048 outs() << " " << name;
5049 outs() << "\n";
5050
5051 outs() << " cls " << format("0x%" PRIx32, c.cls) << "\n";
5052 if (c.cls != 0)
5053 print_class32_t(c.cls, info);
5054 outs() << " instanceMethods " << format("0x%" PRIx32, c.instanceMethods)
5055 << "\n";
5056 if (c.instanceMethods != 0)
5057 print_method_list32_t(c.instanceMethods, info, "");
5058 outs() << " classMethods " << format("0x%" PRIx32, c.classMethods)
5059 << "\n";
5060 if (c.classMethods != 0)
5061 print_method_list32_t(c.classMethods, info, "");
5062 outs() << " protocols " << format("0x%" PRIx32, c.protocols) << "\n";
5063 if (c.protocols != 0)
5064 print_protocol_list32_t(c.protocols, info);
5065 outs() << "instanceProperties " << format("0x%" PRIx32, c.instanceProperties)
5066 << "\n";
5067 if (c.instanceProperties != 0)
5068 print_objc_property_list32(c.instanceProperties, info);
5069}
5070
Kevin Enderby0fc11822015-04-01 20:57:01 +00005071static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) {
5072 uint32_t i, left, offset, xoffset;
5073 uint64_t p, n_value;
5074 struct message_ref64 mr;
5075 const char *name, *sym_name;
5076 const char *r;
5077 SectionRef xS;
5078
5079 if (S == SectionRef())
5080 return;
5081
5082 StringRef SectName;
5083 S.getName(SectName);
5084 DataRefImpl Ref = S.getRawDataRefImpl();
5085 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5086 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5087 offset = 0;
5088 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
5089 p = S.getAddress() + i;
5090 r = get_pointer_64(p, offset, left, S, info);
5091 if (r == nullptr)
5092 return;
5093 memset(&mr, '\0', sizeof(struct message_ref64));
5094 if (left < sizeof(struct message_ref64)) {
5095 memcpy(&mr, r, left);
5096 outs() << " (message_ref entends past the end of the section)\n";
5097 } else
5098 memcpy(&mr, r, sizeof(struct message_ref64));
5099 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5100 swapStruct(mr);
5101
5102 outs() << " imp ";
5103 name = get_symbol_64(offset + offsetof(struct message_ref64, imp), S, info,
5104 n_value, mr.imp);
5105 if (n_value != 0) {
5106 outs() << format("0x%" PRIx64, n_value) << " ";
5107 if (mr.imp != 0)
5108 outs() << "+ " << format("0x%" PRIx64, mr.imp) << " ";
5109 } else
5110 outs() << format("0x%" PRIx64, mr.imp) << " ";
5111 if (name != nullptr)
5112 outs() << " " << name;
5113 outs() << "\n";
5114
5115 outs() << " sel ";
5116 sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel), S,
5117 info, n_value, mr.sel);
5118 if (n_value != 0) {
5119 if (info->verbose && sym_name != nullptr)
5120 outs() << sym_name;
5121 else
5122 outs() << format("0x%" PRIx64, n_value);
5123 if (mr.sel != 0)
5124 outs() << " + " << format("0x%" PRIx64, mr.sel);
5125 } else
5126 outs() << format("0x%" PRIx64, mr.sel);
5127 name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info);
5128 if (name != nullptr)
5129 outs() << format(" %.*s", left, name);
5130 outs() << "\n";
5131
5132 offset += sizeof(struct message_ref64);
5133 }
5134}
5135
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005136static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) {
5137 uint32_t i, left, offset, xoffset, p;
5138 struct message_ref32 mr;
5139 const char *name, *r;
5140 SectionRef xS;
5141
5142 if (S == SectionRef())
5143 return;
5144
5145 StringRef SectName;
5146 S.getName(SectName);
5147 DataRefImpl Ref = S.getRawDataRefImpl();
5148 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5149 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5150 offset = 0;
5151 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
5152 p = S.getAddress() + i;
5153 r = get_pointer_32(p, offset, left, S, info);
5154 if (r == nullptr)
5155 return;
5156 memset(&mr, '\0', sizeof(struct message_ref32));
5157 if (left < sizeof(struct message_ref32)) {
5158 memcpy(&mr, r, left);
5159 outs() << " (message_ref entends past the end of the section)\n";
5160 } else
5161 memcpy(&mr, r, sizeof(struct message_ref32));
5162 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5163 swapStruct(mr);
5164
5165 outs() << " imp " << format("0x%" PRIx32, mr.imp);
5166 name = get_symbol_32(offset + offsetof(struct message_ref32, imp), S, info,
5167 mr.imp);
5168 if (name != nullptr)
5169 outs() << " " << name;
5170 outs() << "\n";
5171
5172 outs() << " sel " << format("0x%" PRIx32, mr.sel);
5173 name = get_pointer_32(mr.sel, xoffset, left, xS, info);
5174 if (name != nullptr)
5175 outs() << " " << name;
5176 outs() << "\n";
5177
5178 offset += sizeof(struct message_ref32);
5179 }
5180}
5181
Kevin Enderby0fc11822015-04-01 20:57:01 +00005182static void print_image_info64(SectionRef S, struct DisassembleInfo *info) {
5183 uint32_t left, offset, swift_version;
5184 uint64_t p;
5185 struct objc_image_info64 o;
5186 const char *r;
5187
Kevin Enderbyaf7c9d02015-10-09 16:48:44 +00005188 if (S == SectionRef())
5189 return;
5190
Kevin Enderby0fc11822015-04-01 20:57:01 +00005191 StringRef SectName;
5192 S.getName(SectName);
5193 DataRefImpl Ref = S.getRawDataRefImpl();
5194 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5195 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5196 p = S.getAddress();
5197 r = get_pointer_64(p, offset, left, S, info);
5198 if (r == nullptr)
5199 return;
5200 memset(&o, '\0', sizeof(struct objc_image_info64));
5201 if (left < sizeof(struct objc_image_info64)) {
5202 memcpy(&o, r, left);
5203 outs() << " (objc_image_info entends past the end of the section)\n";
5204 } else
5205 memcpy(&o, r, sizeof(struct objc_image_info64));
5206 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5207 swapStruct(o);
5208 outs() << " version " << o.version << "\n";
5209 outs() << " flags " << format("0x%" PRIx32, o.flags);
5210 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5211 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5212 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5213 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5214 swift_version = (o.flags >> 8) & 0xff;
5215 if (swift_version != 0) {
5216 if (swift_version == 1)
5217 outs() << " Swift 1.0";
5218 else if (swift_version == 2)
5219 outs() << " Swift 1.1";
5220 else
5221 outs() << " unknown future Swift version (" << swift_version << ")";
5222 }
5223 outs() << "\n";
5224}
5225
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005226static void print_image_info32(SectionRef S, struct DisassembleInfo *info) {
5227 uint32_t left, offset, swift_version, p;
5228 struct objc_image_info32 o;
5229 const char *r;
5230
Kevin Enderby19be2512016-04-21 19:49:29 +00005231 if (S == SectionRef())
5232 return;
5233
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005234 StringRef SectName;
5235 S.getName(SectName);
5236 DataRefImpl Ref = S.getRawDataRefImpl();
5237 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5238 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5239 p = S.getAddress();
5240 r = get_pointer_32(p, offset, left, S, info);
5241 if (r == nullptr)
5242 return;
5243 memset(&o, '\0', sizeof(struct objc_image_info32));
5244 if (left < sizeof(struct objc_image_info32)) {
5245 memcpy(&o, r, left);
5246 outs() << " (objc_image_info entends past the end of the section)\n";
5247 } else
5248 memcpy(&o, r, sizeof(struct objc_image_info32));
5249 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5250 swapStruct(o);
5251 outs() << " version " << o.version << "\n";
5252 outs() << " flags " << format("0x%" PRIx32, o.flags);
5253 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5254 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5255 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5256 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5257 swift_version = (o.flags >> 8) & 0xff;
5258 if (swift_version != 0) {
5259 if (swift_version == 1)
5260 outs() << " Swift 1.0";
5261 else if (swift_version == 2)
5262 outs() << " Swift 1.1";
5263 else
5264 outs() << " unknown future Swift version (" << swift_version << ")";
5265 }
5266 outs() << "\n";
5267}
5268
Kevin Enderby846c0002015-04-16 17:19:59 +00005269static void print_image_info(SectionRef S, struct DisassembleInfo *info) {
5270 uint32_t left, offset, p;
5271 struct imageInfo_t o;
5272 const char *r;
5273
5274 StringRef SectName;
5275 S.getName(SectName);
5276 DataRefImpl Ref = S.getRawDataRefImpl();
5277 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5278 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5279 p = S.getAddress();
5280 r = get_pointer_32(p, offset, left, S, info);
5281 if (r == nullptr)
5282 return;
5283 memset(&o, '\0', sizeof(struct imageInfo_t));
5284 if (left < sizeof(struct imageInfo_t)) {
5285 memcpy(&o, r, left);
5286 outs() << " (imageInfo entends past the end of the section)\n";
5287 } else
5288 memcpy(&o, r, sizeof(struct imageInfo_t));
5289 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5290 swapStruct(o);
5291 outs() << " version " << o.version << "\n";
5292 outs() << " flags " << format("0x%" PRIx32, o.flags);
5293 if (o.flags & 0x1)
5294 outs() << " F&C";
5295 if (o.flags & 0x2)
5296 outs() << " GC";
5297 if (o.flags & 0x4)
5298 outs() << " GC-only";
5299 else
5300 outs() << " RR";
5301 outs() << "\n";
5302}
5303
Kevin Enderby0fc11822015-04-01 20:57:01 +00005304static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) {
5305 SymbolAddressMap AddrMap;
5306 if (verbose)
5307 CreateSymbolAddressMap(O, &AddrMap);
5308
5309 std::vector<SectionRef> Sections;
5310 for (const SectionRef &Section : O->sections()) {
5311 StringRef SectName;
5312 Section.getName(SectName);
5313 Sections.push_back(Section);
5314 }
5315
5316 struct DisassembleInfo info;
5317 // Set up the block of info used by the Symbolizer call backs.
5318 info.verbose = verbose;
5319 info.O = O;
5320 info.AddrMap = &AddrMap;
5321 info.Sections = &Sections;
5322 info.class_name = nullptr;
5323 info.selector_name = nullptr;
5324 info.method = nullptr;
5325 info.demangled_name = nullptr;
5326 info.bindtable = nullptr;
5327 info.adrp_addr = 0;
5328 info.adrp_inst = 0;
5329
Kevin Enderbyaac75382015-10-08 16:56:35 +00005330 info.depth = 0;
Davide Italiano62507042015-12-11 22:27:59 +00005331 SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5332 if (CL == SectionRef())
5333 CL = get_section(O, "__DATA", "__objc_classlist");
5334 info.S = CL;
5335 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005336
Davide Italiano62507042015-12-11 22:27:59 +00005337 SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5338 if (CR == SectionRef())
5339 CR = get_section(O, "__DATA", "__objc_classrefs");
5340 info.S = CR;
5341 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005342
Davide Italiano62507042015-12-11 22:27:59 +00005343 SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5344 if (SR == SectionRef())
5345 SR = get_section(O, "__DATA", "__objc_superrefs");
5346 info.S = SR;
5347 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005348
Davide Italiano62507042015-12-11 22:27:59 +00005349 SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5350 if (CA == SectionRef())
5351 CA = get_section(O, "__DATA", "__objc_catlist");
5352 info.S = CA;
5353 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005354
Davide Italiano62507042015-12-11 22:27:59 +00005355 SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5356 if (PL == SectionRef())
5357 PL = get_section(O, "__DATA", "__objc_protolist");
5358 info.S = PL;
5359 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005360
Davide Italiano62507042015-12-11 22:27:59 +00005361 SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5362 if (MR == SectionRef())
5363 MR = get_section(O, "__DATA", "__objc_msgrefs");
5364 info.S = MR;
5365 print_message_refs64(MR, &info);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005366
Davide Italiano62507042015-12-11 22:27:59 +00005367 SectionRef II = get_section(O, "__OBJC2", "__image_info");
5368 if (II == SectionRef())
5369 II = get_section(O, "__DATA", "__objc_imageinfo");
5370 info.S = II;
5371 print_image_info64(II, &info);
Kevin Enderby0bc6ed42015-04-01 21:50:45 +00005372
5373 if (info.bindtable != nullptr)
5374 delete info.bindtable;
Kevin Enderby0fc11822015-04-01 20:57:01 +00005375}
5376
5377static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) {
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005378 SymbolAddressMap AddrMap;
5379 if (verbose)
5380 CreateSymbolAddressMap(O, &AddrMap);
5381
5382 std::vector<SectionRef> Sections;
5383 for (const SectionRef &Section : O->sections()) {
5384 StringRef SectName;
5385 Section.getName(SectName);
5386 Sections.push_back(Section);
5387 }
5388
5389 struct DisassembleInfo info;
5390 // Set up the block of info used by the Symbolizer call backs.
5391 info.verbose = verbose;
5392 info.O = O;
5393 info.AddrMap = &AddrMap;
5394 info.Sections = &Sections;
5395 info.class_name = nullptr;
5396 info.selector_name = nullptr;
5397 info.method = nullptr;
5398 info.demangled_name = nullptr;
5399 info.bindtable = nullptr;
5400 info.adrp_addr = 0;
5401 info.adrp_inst = 0;
5402
5403 const SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5404 if (CL != SectionRef()) {
5405 info.S = CL;
5406 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5407 } else {
5408 const SectionRef CL = get_section(O, "__DATA", "__objc_classlist");
5409 info.S = CL;
5410 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
5411 }
5412
5413 const SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5414 if (CR != SectionRef()) {
5415 info.S = CR;
5416 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5417 } else {
5418 const SectionRef CR = get_section(O, "__DATA", "__objc_classrefs");
5419 info.S = CR;
5420 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
5421 }
5422
5423 const SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5424 if (SR != SectionRef()) {
5425 info.S = SR;
5426 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5427 } else {
5428 const SectionRef SR = get_section(O, "__DATA", "__objc_superrefs");
5429 info.S = SR;
5430 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
5431 }
5432
5433 const SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5434 if (CA != SectionRef()) {
5435 info.S = CA;
5436 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5437 } else {
5438 const SectionRef CA = get_section(O, "__DATA", "__objc_catlist");
5439 info.S = CA;
5440 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
5441 }
5442
5443 const SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5444 if (PL != SectionRef()) {
5445 info.S = PL;
5446 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5447 } else {
5448 const SectionRef PL = get_section(O, "__DATA", "__objc_protolist");
5449 info.S = PL;
5450 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
5451 }
5452
5453 const SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5454 if (MR != SectionRef()) {
5455 info.S = MR;
5456 print_message_refs32(MR, &info);
5457 } else {
5458 const SectionRef MR = get_section(O, "__DATA", "__objc_msgrefs");
5459 info.S = MR;
5460 print_message_refs32(MR, &info);
5461 }
5462
5463 const SectionRef II = get_section(O, "__OBJC2", "__image_info");
5464 if (II != SectionRef()) {
5465 info.S = II;
5466 print_image_info32(II, &info);
5467 } else {
5468 const SectionRef II = get_section(O, "__DATA", "__objc_imageinfo");
5469 info.S = II;
5470 print_image_info32(II, &info);
5471 }
Kevin Enderby0fc11822015-04-01 20:57:01 +00005472}
5473
5474static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) {
Kevin Enderby846c0002015-04-16 17:19:59 +00005475 uint32_t i, j, p, offset, xoffset, left, defs_left, def;
5476 const char *r, *name, *defs;
5477 struct objc_module_t module;
5478 SectionRef S, xS;
5479 struct objc_symtab_t symtab;
5480 struct objc_class_t objc_class;
5481 struct objc_category_t objc_category;
5482
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005483 outs() << "Objective-C segment\n";
Kevin Enderby846c0002015-04-16 17:19:59 +00005484 S = get_section(O, "__OBJC", "__module_info");
5485 if (S == SectionRef())
5486 return false;
5487
5488 SymbolAddressMap AddrMap;
5489 if (verbose)
5490 CreateSymbolAddressMap(O, &AddrMap);
5491
5492 std::vector<SectionRef> Sections;
5493 for (const SectionRef &Section : O->sections()) {
5494 StringRef SectName;
5495 Section.getName(SectName);
5496 Sections.push_back(Section);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005497 }
Kevin Enderby846c0002015-04-16 17:19:59 +00005498
5499 struct DisassembleInfo info;
5500 // Set up the block of info used by the Symbolizer call backs.
5501 info.verbose = verbose;
5502 info.O = O;
5503 info.AddrMap = &AddrMap;
5504 info.Sections = &Sections;
5505 info.class_name = nullptr;
5506 info.selector_name = nullptr;
5507 info.method = nullptr;
5508 info.demangled_name = nullptr;
5509 info.bindtable = nullptr;
5510 info.adrp_addr = 0;
5511 info.adrp_inst = 0;
5512
5513 for (i = 0; i < S.getSize(); i += sizeof(struct objc_module_t)) {
5514 p = S.getAddress() + i;
5515 r = get_pointer_32(p, offset, left, S, &info, true);
5516 if (r == nullptr)
5517 return true;
5518 memset(&module, '\0', sizeof(struct objc_module_t));
5519 if (left < sizeof(struct objc_module_t)) {
5520 memcpy(&module, r, left);
5521 outs() << " (module extends past end of __module_info section)\n";
5522 } else
5523 memcpy(&module, r, sizeof(struct objc_module_t));
5524 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5525 swapStruct(module);
5526
5527 outs() << "Module " << format("0x%" PRIx32, p) << "\n";
5528 outs() << " version " << module.version << "\n";
5529 outs() << " size " << module.size << "\n";
5530 outs() << " name ";
5531 name = get_pointer_32(module.name, xoffset, left, xS, &info, true);
5532 if (name != nullptr)
5533 outs() << format("%.*s", left, name);
5534 else
5535 outs() << format("0x%08" PRIx32, module.name)
5536 << "(not in an __OBJC section)";
5537 outs() << "\n";
5538
5539 r = get_pointer_32(module.symtab, xoffset, left, xS, &info, true);
5540 if (module.symtab == 0 || r == nullptr) {
5541 outs() << " symtab " << format("0x%08" PRIx32, module.symtab)
5542 << " (not in an __OBJC section)\n";
5543 continue;
5544 }
5545 outs() << " symtab " << format("0x%08" PRIx32, module.symtab) << "\n";
5546 memset(&symtab, '\0', sizeof(struct objc_symtab_t));
5547 defs_left = 0;
5548 defs = nullptr;
5549 if (left < sizeof(struct objc_symtab_t)) {
5550 memcpy(&symtab, r, left);
5551 outs() << "\tsymtab extends past end of an __OBJC section)\n";
5552 } else {
5553 memcpy(&symtab, r, sizeof(struct objc_symtab_t));
5554 if (left > sizeof(struct objc_symtab_t)) {
5555 defs_left = left - sizeof(struct objc_symtab_t);
5556 defs = r + sizeof(struct objc_symtab_t);
5557 }
5558 }
5559 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5560 swapStruct(symtab);
5561
5562 outs() << "\tsel_ref_cnt " << symtab.sel_ref_cnt << "\n";
5563 r = get_pointer_32(symtab.refs, xoffset, left, xS, &info, true);
5564 outs() << "\trefs " << format("0x%08" PRIx32, symtab.refs);
5565 if (r == nullptr)
5566 outs() << " (not in an __OBJC section)";
5567 outs() << "\n";
5568 outs() << "\tcls_def_cnt " << symtab.cls_def_cnt << "\n";
5569 outs() << "\tcat_def_cnt " << symtab.cat_def_cnt << "\n";
5570 if (symtab.cls_def_cnt > 0)
5571 outs() << "\tClass Definitions\n";
5572 for (j = 0; j < symtab.cls_def_cnt; j++) {
5573 if ((j + 1) * sizeof(uint32_t) > defs_left) {
5574 outs() << "\t(remaining class defs entries entends past the end of the "
5575 << "section)\n";
5576 break;
5577 }
5578 memcpy(&def, defs + j * sizeof(uint32_t), sizeof(uint32_t));
5579 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5580 sys::swapByteOrder(def);
5581
5582 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5583 outs() << "\tdefs[" << j << "] " << format("0x%08" PRIx32, def);
5584 if (r != nullptr) {
5585 if (left > sizeof(struct objc_class_t)) {
5586 outs() << "\n";
5587 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5588 } else {
5589 outs() << " (entends past the end of the section)\n";
5590 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5591 memcpy(&objc_class, r, left);
5592 }
5593 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5594 swapStruct(objc_class);
5595 print_objc_class_t(&objc_class, &info);
5596 } else {
5597 outs() << "(not in an __OBJC section)\n";
5598 }
5599
5600 if (CLS_GETINFO(&objc_class, CLS_CLASS)) {
5601 outs() << "\tMeta Class";
5602 r = get_pointer_32(objc_class.isa, xoffset, left, xS, &info, true);
5603 if (r != nullptr) {
5604 if (left > sizeof(struct objc_class_t)) {
5605 outs() << "\n";
5606 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5607 } else {
5608 outs() << " (entends past the end of the section)\n";
5609 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5610 memcpy(&objc_class, r, left);
5611 }
5612 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5613 swapStruct(objc_class);
5614 print_objc_class_t(&objc_class, &info);
5615 } else {
5616 outs() << "(not in an __OBJC section)\n";
5617 }
5618 }
5619 }
5620 if (symtab.cat_def_cnt > 0)
5621 outs() << "\tCategory Definitions\n";
5622 for (j = 0; j < symtab.cat_def_cnt; j++) {
5623 if ((j + symtab.cls_def_cnt + 1) * sizeof(uint32_t) > defs_left) {
5624 outs() << "\t(remaining category defs entries entends past the end of "
5625 << "the section)\n";
5626 break;
5627 }
5628 memcpy(&def, defs + (j + symtab.cls_def_cnt) * sizeof(uint32_t),
5629 sizeof(uint32_t));
5630 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5631 sys::swapByteOrder(def);
5632
5633 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5634 outs() << "\tdefs[" << j + symtab.cls_def_cnt << "] "
5635 << format("0x%08" PRIx32, def);
5636 if (r != nullptr) {
5637 if (left > sizeof(struct objc_category_t)) {
5638 outs() << "\n";
5639 memcpy(&objc_category, r, sizeof(struct objc_category_t));
5640 } else {
5641 outs() << " (entends past the end of the section)\n";
5642 memset(&objc_category, '\0', sizeof(struct objc_category_t));
5643 memcpy(&objc_category, r, left);
5644 }
5645 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5646 swapStruct(objc_category);
5647 print_objc_objc_category_t(&objc_category, &info);
5648 } else {
5649 outs() << "(not in an __OBJC section)\n";
5650 }
5651 }
5652 }
5653 const SectionRef II = get_section(O, "__OBJC", "__image_info");
5654 if (II != SectionRef())
5655 print_image_info(II, &info);
5656
5657 return true;
Kevin Enderby0fc11822015-04-01 20:57:01 +00005658}
5659
Kevin Enderby4ad9bde2015-04-16 22:33:20 +00005660static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
5661 uint32_t size, uint32_t addr) {
5662 SymbolAddressMap AddrMap;
5663 CreateSymbolAddressMap(O, &AddrMap);
5664
5665 std::vector<SectionRef> Sections;
5666 for (const SectionRef &Section : O->sections()) {
5667 StringRef SectName;
5668 Section.getName(SectName);
5669 Sections.push_back(Section);
5670 }
5671
5672 struct DisassembleInfo info;
5673 // Set up the block of info used by the Symbolizer call backs.
5674 info.verbose = true;
5675 info.O = O;
5676 info.AddrMap = &AddrMap;
5677 info.Sections = &Sections;
5678 info.class_name = nullptr;
5679 info.selector_name = nullptr;
5680 info.method = nullptr;
5681 info.demangled_name = nullptr;
5682 info.bindtable = nullptr;
5683 info.adrp_addr = 0;
5684 info.adrp_inst = 0;
5685
5686 const char *p;
5687 struct objc_protocol_t protocol;
5688 uint32_t left, paddr;
5689 for (p = sect; p < sect + size; p += sizeof(struct objc_protocol_t)) {
5690 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
5691 left = size - (p - sect);
5692 if (left < sizeof(struct objc_protocol_t)) {
5693 outs() << "Protocol extends past end of __protocol section\n";
5694 memcpy(&protocol, p, left);
5695 } else
5696 memcpy(&protocol, p, sizeof(struct objc_protocol_t));
5697 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5698 swapStruct(protocol);
5699 paddr = addr + (p - sect);
5700 outs() << "Protocol " << format("0x%" PRIx32, paddr);
5701 if (print_protocol(paddr, 0, &info))
5702 outs() << "(not in an __OBJC section)\n";
5703 }
5704}
5705
Kevin Enderby9873e2c2016-05-23 21:34:12 +00005706#ifdef HAVE_LIBXAR
5707inline void swapStruct(struct xar_header &xar) {
5708 sys::swapByteOrder(xar.magic);
5709 sys::swapByteOrder(xar.size);
5710 sys::swapByteOrder(xar.version);
5711 sys::swapByteOrder(xar.toc_length_compressed);
5712 sys::swapByteOrder(xar.toc_length_uncompressed);
5713 sys::swapByteOrder(xar.cksum_alg);
5714}
5715
5716static void PrintModeVerbose(uint32_t mode) {
5717 switch(mode & S_IFMT){
5718 case S_IFDIR:
5719 outs() << "d";
5720 break;
5721 case S_IFCHR:
5722 outs() << "c";
5723 break;
5724 case S_IFBLK:
5725 outs() << "b";
5726 break;
5727 case S_IFREG:
5728 outs() << "-";
5729 break;
5730 case S_IFLNK:
5731 outs() << "l";
5732 break;
5733 case S_IFSOCK:
5734 outs() << "s";
5735 break;
5736 default:
5737 outs() << "?";
5738 break;
5739 }
5740
5741 /* owner permissions */
5742 if(mode & S_IREAD)
5743 outs() << "r";
5744 else
5745 outs() << "-";
5746 if(mode & S_IWRITE)
5747 outs() << "w";
5748 else
5749 outs() << "-";
5750 if(mode & S_ISUID)
5751 outs() << "s";
5752 else if(mode & S_IEXEC)
5753 outs() << "x";
5754 else
5755 outs() << "-";
5756
5757 /* group permissions */
5758 if(mode & (S_IREAD >> 3))
5759 outs() << "r";
5760 else
5761 outs() << "-";
5762 if(mode & (S_IWRITE >> 3))
5763 outs() << "w";
5764 else
5765 outs() << "-";
5766 if(mode & S_ISGID)
5767 outs() << "s";
5768 else if(mode & (S_IEXEC >> 3))
5769 outs() << "x";
5770 else
5771 outs() << "-";
5772
5773 /* other permissions */
5774 if(mode & (S_IREAD >> 6))
5775 outs() << "r";
5776 else
5777 outs() << "-";
5778 if(mode & (S_IWRITE >> 6))
5779 outs() << "w";
5780 else
5781 outs() << "-";
5782 if(mode & S_ISVTX)
5783 outs() << "t";
5784 else if(mode & (S_IEXEC >> 6))
5785 outs() << "x";
5786 else
5787 outs() << "-";
5788}
5789
5790static void PrintXarFilesSummary(const char *XarFilename, xar_t xar) {
5791 xar_iter_t xi;
5792 xar_file_t xf;
5793 xar_iter_t xp;
5794 const char *key, *type, *mode, *user, *group, *size, *mtime, *name, *m;
5795 char *endp;
5796 uint32_t mode_value;
5797
5798 xi = xar_iter_new();
5799 if (!xi) {
5800 errs() << "Can't obtain an xar iterator for xar archive "
5801 << XarFilename << "\n";
5802 return;
5803 }
5804
5805 // Go through the xar's files.
5806 for (xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)) {
5807 xp = xar_iter_new();
5808 if(!xp){
5809 errs() << "Can't obtain an xar iterator for xar archive "
5810 << XarFilename << "\n";
5811 return;
5812 }
5813 type = nullptr;
5814 mode = nullptr;
5815 user = nullptr;
5816 group = nullptr;
5817 size = nullptr;
5818 mtime = nullptr;
5819 name = nullptr;
5820 for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){
5821 const char *val = nullptr;
5822 xar_prop_get(xf, key, &val);
5823#if 0 // Useful for debugging.
5824 outs() << "key: " << key << " value: " << val << "\n";
5825#endif
5826 if(strcmp(key, "type") == 0)
5827 type = val;
5828 if(strcmp(key, "mode") == 0)
5829 mode = val;
5830 if(strcmp(key, "user") == 0)
5831 user = val;
5832 if(strcmp(key, "group") == 0)
5833 group = val;
5834 if(strcmp(key, "data/size") == 0)
5835 size = val;
5836 if(strcmp(key, "mtime") == 0)
5837 mtime = val;
5838 if(strcmp(key, "name") == 0)
5839 name = val;
5840 }
5841 if(mode != nullptr){
5842 mode_value = strtoul(mode, &endp, 8);
5843 if(*endp != '\0')
5844 outs() << "(mode: \"" << mode << "\" contains non-octal chars) ";
5845 if(strcmp(type, "file") == 0)
5846 mode_value |= S_IFREG;
5847 PrintModeVerbose(mode_value);
5848 outs() << " ";
5849 }
5850 if(user != nullptr)
5851 outs() << format("%10s/", user);
5852 if(group != nullptr)
5853 outs() << format("%-10s ", group);
5854 if(size != nullptr)
5855 outs() << format("%7s ", size);
5856 if(mtime != nullptr){
5857 for(m = mtime; *m != 'T' && *m != '\0'; m++)
5858 outs() << *m;
5859 if(*m == 'T')
5860 m++;
5861 outs() << " ";
5862 for( ; *m != 'Z' && *m != '\0'; m++)
5863 outs() << *m;
5864 outs() << " ";
5865 }
5866 if(name != nullptr)
5867 outs() << name;
5868 outs() << "\n";
5869 }
5870}
5871
5872static void DumpBitcodeSection(MachOObjectFile *O, const char *sect,
5873 uint32_t size, bool verbose,
5874 bool PrintXarHeader, bool PrintXarFileHeaders,
5875 std::string XarMemberName) {
5876 if(size < sizeof(struct xar_header)) {
5877 outs() << "size of (__LLVM,__bundle) section too small (smaller than size "
5878 "of struct xar_header)\n";
5879 return;
5880 }
5881 struct xar_header XarHeader;
5882 memcpy(&XarHeader, sect, sizeof(struct xar_header));
5883 if (sys::IsLittleEndianHost)
5884 swapStruct(XarHeader);
5885 if (PrintXarHeader) {
5886 if (!XarMemberName.empty())
5887 outs() << "In xar member " << XarMemberName << ": ";
5888 else
5889 outs() << "For (__LLVM,__bundle) section: ";
5890 outs() << "xar header\n";
5891 if (XarHeader.magic == XAR_HEADER_MAGIC)
5892 outs() << " magic XAR_HEADER_MAGIC\n";
5893 else
5894 outs() << " magic "
5895 << format_hex(XarHeader.magic, 10, true)
5896 << " (not XAR_HEADER_MAGIC)\n";
5897 outs() << " size " << XarHeader.size << "\n";
5898 outs() << " version " << XarHeader.version << "\n";
5899 outs() << " toc_length_compressed " << XarHeader.toc_length_compressed
5900 << "\n";
5901 outs() << "toc_length_uncompressed " << XarHeader.toc_length_uncompressed
5902 << "\n";
5903 outs() << " cksum_alg ";
5904 switch (XarHeader.cksum_alg) {
5905 case XAR_CKSUM_NONE:
5906 outs() << "XAR_CKSUM_NONE\n";
5907 break;
5908 case XAR_CKSUM_SHA1:
5909 outs() << "XAR_CKSUM_SHA1\n";
5910 break;
5911 case XAR_CKSUM_MD5:
5912 outs() << "XAR_CKSUM_MD5\n";
5913 break;
Kevin Enderby42882282016-05-23 22:18:59 +00005914#ifdef XAR_CKSUM_SHA256
Kevin Enderby9873e2c2016-05-23 21:34:12 +00005915 case XAR_CKSUM_SHA256:
5916 outs() << "XAR_CKSUM_SHA256\n";
5917 break;
Kevin Enderby42882282016-05-23 22:18:59 +00005918#endif
5919#ifdef XAR_CKSUM_SHA512
Kevin Enderby9873e2c2016-05-23 21:34:12 +00005920 case XAR_CKSUM_SHA512:
5921 outs() << "XAR_CKSUM_SHA512\n";
5922 break;
Kevin Enderby42882282016-05-23 22:18:59 +00005923#endif
Kevin Enderby9873e2c2016-05-23 21:34:12 +00005924 default:
5925 outs() << XarHeader.cksum_alg << "\n";
5926 }
5927 }
5928
5929 SmallString<128> XarFilename;
5930 int FD;
5931 std::error_code XarEC =
5932 sys::fs::createTemporaryFile("llvm-objdump", "xar", FD, XarFilename);
5933 if (XarEC) {
5934 errs() << XarEC.message() << "\n";
5935 return;
5936 }
5937 tool_output_file XarFile(XarFilename, FD);
5938 raw_fd_ostream &XarOut = XarFile.os();
5939 StringRef XarContents(sect, size);
5940 XarOut << XarContents;
5941 XarOut.close();
5942 if (XarOut.has_error())
5943 return;
5944
5945 xar_t xar = xar_open(XarFilename.c_str(), READ);
5946 if (!xar) {
5947 errs() << "Can't create temporary xar archive " << XarFilename << "\n";
5948 return;
5949 }
5950
5951 SmallString<128> TocFilename;
5952 std::error_code TocEC =
5953 sys::fs::createTemporaryFile("llvm-objdump", "toc", TocFilename);
5954 if (TocEC) {
5955 errs() << TocEC.message() << "\n";
5956 return;
5957 }
5958 xar_serialize(xar, TocFilename.c_str());
5959
5960 if (PrintXarFileHeaders) {
5961 if (!XarMemberName.empty())
5962 outs() << "In xar member " << XarMemberName << ": ";
5963 else
5964 outs() << "For (__LLVM,__bundle) section: ";
5965 outs() << "xar archive files:\n";
5966 PrintXarFilesSummary(XarFilename.c_str(), xar);
5967 }
5968
5969 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
5970 MemoryBuffer::getFileOrSTDIN(TocFilename.c_str());
5971 if (std::error_code EC = FileOrErr.getError()) {
5972 errs() << EC.message() << "\n";
5973 return;
5974 }
5975 std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
5976
5977 if (!XarMemberName.empty())
5978 outs() << "In xar member " << XarMemberName << ": ";
5979 else
5980 outs() << "For (__LLVM,__bundle) section: ";
5981 outs() << "xar table of contents:\n";
5982 outs() << Buffer->getBuffer() << "\n";
5983
5984 // TODO: Go through the xar's files.
5985 xar_iter_t xi = xar_iter_new();
5986 if(!xi){
5987 errs() << "Can't obtain an xar iterator for xar archive "
5988 << XarFilename.c_str() << "\n";
5989 xar_close(xar);
5990 return;
5991 }
5992 for(xar_file_t xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)){
5993 const char *key;
5994 xar_iter_t xp;
5995 const char *member_name, *member_type, *member_size_string;
5996 size_t member_size;
5997
5998 xp = xar_iter_new();
5999 if(!xp){
6000 errs() << "Can't obtain an xar iterator for xar archive "
6001 << XarFilename.c_str() << "\n";
6002 xar_close(xar);
6003 return;
6004 }
6005 member_name = NULL;
6006 member_type = NULL;
6007 member_size_string = NULL;
6008 for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){
6009 const char *val = nullptr;
6010 xar_prop_get(xf, key, &val);
6011#if 0 // Useful for debugging.
6012 outs() << "key: " << key << " value: " << val << "\n";
6013#endif
6014 if(strcmp(key, "name") == 0)
6015 member_name = val;
6016 if(strcmp(key, "type") == 0)
6017 member_type = val;
6018 if(strcmp(key, "data/size") == 0)
6019 member_size_string = val;
6020 }
6021 /*
6022 * If we find a file with a name, date/size and type properties
6023 * and with the type being "file" see if that is a xar file.
6024 */
6025 if (member_name != NULL && member_type != NULL &&
6026 strcmp(member_type, "file") == 0 &&
6027 member_size_string != NULL){
6028 // Extract the file into a buffer.
6029 char *endptr;
6030 member_size = strtoul(member_size_string, &endptr, 10);
6031 if (*endptr == '\0' && member_size != 0) {
6032 char *buffer = (char *) ::operator new (member_size);
6033 if (xar_extract_tobuffersz(xar, xf, &buffer, &member_size) == 0) {
6034#if 0 // Useful for debugging.
6035 outs() << "xar member: " << member_name << " extracted\n";
6036#endif
6037 // Set the XarMemberName we want to see printed in the header.
6038 std::string OldXarMemberName;
6039 // If XarMemberName is already set this is nested. So
6040 // save the old name and create the nested name.
6041 if (!XarMemberName.empty()) {
6042 OldXarMemberName = XarMemberName;
6043 XarMemberName =
6044 (Twine("[") + XarMemberName + "]" + member_name).str();
6045 } else {
6046 OldXarMemberName = "";
6047 XarMemberName = member_name;
6048 }
6049 // See if this is could be a xar file (nested).
6050 if (member_size >= sizeof(struct xar_header)) {
6051#if 0 // Useful for debugging.
6052 outs() << "could be a xar file: " << member_name << "\n";
6053#endif
6054 memcpy((char *)&XarHeader, buffer, sizeof(struct xar_header));
6055 if (sys::IsLittleEndianHost)
6056 swapStruct(XarHeader);
6057 if(XarHeader.magic == XAR_HEADER_MAGIC)
6058 DumpBitcodeSection(O, buffer, member_size, verbose,
6059 PrintXarHeader, PrintXarFileHeaders,
6060 XarMemberName);
6061 }
6062 XarMemberName = OldXarMemberName;
6063 }
6064 delete buffer;
6065 }
6066 }
6067 xar_iter_free(xp);
6068 }
6069 xar_close(xar);
6070}
6071#endif // defined(HAVE_LIBXAR)
6072
Kevin Enderby0fc11822015-04-01 20:57:01 +00006073static void printObjcMetaData(MachOObjectFile *O, bool verbose) {
6074 if (O->is64Bit())
6075 printObjc2_64bit_MetaData(O, verbose);
6076 else {
6077 MachO::mach_header H;
6078 H = O->getHeader();
6079 if (H.cputype == MachO::CPU_TYPE_ARM)
6080 printObjc2_32bit_MetaData(O, verbose);
6081 else {
6082 // This is the 32-bit non-arm cputype case. Which is normally
6083 // the first Objective-C ABI. But it may be the case of a
6084 // binary for the iOS simulator which is the second Objective-C
6085 // ABI. In that case printObjc1_32bit_MetaData() will determine that
6086 // and return false.
Hans Wennborgcc9deb42015-09-29 18:02:48 +00006087 if (!printObjc1_32bit_MetaData(O, verbose))
Kevin Enderby0fc11822015-04-01 20:57:01 +00006088 printObjc2_32bit_MetaData(O, verbose);
6089 }
6090 }
6091}
6092
Kevin Enderbybf246f52014-09-24 23:08:22 +00006093// GuessLiteralPointer returns a string which for the item in the Mach-O file
6094// for the address passed in as ReferenceValue for printing as a comment with
6095// the instruction and also returns the corresponding type of that item
6096// indirectly through ReferenceType.
6097//
6098// If ReferenceValue is an address of literal cstring then a pointer to the
6099// cstring is returned and ReferenceType is set to
6100// LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr .
6101//
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006102// If ReferenceValue is an address of an Objective-C CFString, Selector ref or
6103// Class ref that name is returned and the ReferenceType is set accordingly.
6104//
6105// Lastly, literals which are Symbol address in a literal pool are looked for
6106// and if found the symbol name is returned and ReferenceType is set to
6107// LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr .
6108//
6109// If there is no item in the Mach-O file for the address passed in as
6110// ReferenceValue nullptr is returned and ReferenceType is unchanged.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00006111static const char *GuessLiteralPointer(uint64_t ReferenceValue,
6112 uint64_t ReferencePC,
6113 uint64_t *ReferenceType,
6114 struct DisassembleInfo *info) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00006115 // First see if there is an external relocation entry at the ReferencePC.
Kevin Enderbyd90a4172015-10-10 00:05:01 +00006116 if (info->O->getHeader().filetype == MachO::MH_OBJECT) {
6117 uint64_t sect_addr = info->S.getAddress();
6118 uint64_t sect_offset = ReferencePC - sect_addr;
6119 bool reloc_found = false;
6120 DataRefImpl Rel;
6121 MachO::any_relocation_info RE;
6122 bool isExtern = false;
6123 SymbolRef Symbol;
6124 for (const RelocationRef &Reloc : info->S.relocations()) {
6125 uint64_t RelocOffset = Reloc.getOffset();
6126 if (RelocOffset == sect_offset) {
6127 Rel = Reloc.getRawDataRefImpl();
6128 RE = info->O->getRelocation(Rel);
6129 if (info->O->isRelocationScattered(RE))
6130 continue;
6131 isExtern = info->O->getPlainRelocationExternal(RE);
6132 if (isExtern) {
6133 symbol_iterator RelocSym = Reloc.getSymbol();
6134 Symbol = *RelocSym;
6135 }
6136 reloc_found = true;
6137 break;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006138 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006139 }
Kevin Enderbyd90a4172015-10-10 00:05:01 +00006140 // If there is an external relocation entry for a symbol in a section
6141 // then used that symbol's value for the value of the reference.
6142 if (reloc_found && isExtern) {
6143 if (info->O->getAnyRelocationPCRel(RE)) {
6144 unsigned Type = info->O->getAnyRelocationType(RE);
6145 if (Type == MachO::X86_64_RELOC_SIGNED) {
6146 ReferenceValue = Symbol.getValue();
6147 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006148 }
6149 }
6150 }
6151
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006152 // Look for literals such as Objective-C CFStrings refs, Selector refs,
6153 // Message refs and Class refs.
6154 bool classref, selref, msgref, cfstring;
6155 uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref,
6156 selref, msgref, cfstring);
David Blaikie33dd45d02015-03-23 18:39:02 +00006157 if (classref && pointer_value == 0) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006158 // Note the ReferenceValue is a pointer into the __objc_classrefs section.
6159 // And the pointer_value in that section is typically zero as it will be
6160 // set by dyld as part of the "bind information".
6161 const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info);
6162 if (name != nullptr) {
6163 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
Hans Wennborgdb53e302014-10-23 21:59:17 +00006164 const char *class_name = strrchr(name, '$');
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006165 if (class_name != nullptr && class_name[1] == '_' &&
6166 class_name[2] != '\0') {
6167 info->class_name = class_name + 2;
6168 return name;
6169 }
6170 }
6171 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006172
David Blaikie33dd45d02015-03-23 18:39:02 +00006173 if (classref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006174 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
6175 const char *name =
6176 get_objc2_64bit_class_name(pointer_value, ReferenceValue, info);
6177 if (name != nullptr)
6178 info->class_name = name;
6179 else
6180 name = "bad class ref";
Kevin Enderbybf246f52014-09-24 23:08:22 +00006181 return name;
6182 }
6183
David Blaikie33dd45d02015-03-23 18:39:02 +00006184 if (cfstring) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006185 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref;
6186 const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info);
6187 return name;
6188 }
6189
David Blaikie33dd45d02015-03-23 18:39:02 +00006190 if (selref && pointer_value == 0)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006191 pointer_value = get_objc2_64bit_selref(ReferenceValue, info);
6192
6193 if (pointer_value != 0)
6194 ReferenceValue = pointer_value;
6195
6196 const char *name = GuessCstringPointer(ReferenceValue, info);
6197 if (name) {
David Blaikie33dd45d02015-03-23 18:39:02 +00006198 if (pointer_value != 0 && selref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006199 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref;
6200 info->selector_name = name;
David Blaikie33dd45d02015-03-23 18:39:02 +00006201 } else if (pointer_value != 0 && msgref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006202 info->class_name = nullptr;
6203 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref;
6204 info->selector_name = name;
6205 } else
6206 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr;
6207 return name;
6208 }
6209
6210 // Lastly look for an indirect symbol with this ReferenceValue which is in
6211 // a literal pool. If found return that symbol name.
6212 name = GuessIndirectSymbol(ReferenceValue, info);
6213 if (name) {
6214 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr;
6215 return name;
6216 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006217
6218 return nullptr;
6219}
6220
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006221// SymbolizerSymbolLookUp is the symbol lookup function passed when creating
Kevin Enderbybf246f52014-09-24 23:08:22 +00006222// the Symbolizer. It looks up the ReferenceValue using the info passed via the
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006223// pointer to the struct DisassembleInfo that was passed when MCSymbolizer
6224// is created and returns the symbol name that matches the ReferenceValue or
6225// nullptr if none. The ReferenceType is passed in for the IN type of
6226// reference the instruction is making from the values in defined in the header
6227// "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific
6228// Out type and the ReferenceName will also be set which is added as a comment
6229// to the disassembled instruction.
6230//
Kevin Enderby04bf6932014-10-28 23:39:46 +00006231// If the symbol name is a C++ mangled name then the demangled name is
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006232// returned through ReferenceName and ReferenceType is set to
6233// LLVMDisassembler_ReferenceType_DeMangled_Name .
6234//
6235// When this is called to get a symbol name for a branch target then the
6236// ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
6237// SymbolValue will be looked for in the indirect symbol table to determine if
6238// it is an address for a symbol stub. If so then the symbol name for that
6239// stub is returned indirectly through ReferenceName and then ReferenceType is
6240// set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
6241//
Kevin Enderbybf246f52014-09-24 23:08:22 +00006242// When this is called with an value loaded via a PC relative load then
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006243// ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
6244// SymbolValue is checked to be an address of literal pointer, symbol pointer,
6245// or an Objective-C meta data reference. If so the output ReferenceType is
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006246// set to correspond to that as well as setting the ReferenceName.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00006247static const char *SymbolizerSymbolLookUp(void *DisInfo,
6248 uint64_t ReferenceValue,
6249 uint64_t *ReferenceType,
6250 uint64_t ReferencePC,
6251 const char **ReferenceName) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006252 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006253 // If no verbose symbolic information is wanted then just return nullptr.
David Blaikie33dd45d02015-03-23 18:39:02 +00006254 if (!info->verbose) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00006255 *ReferenceName = nullptr;
6256 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006257 return nullptr;
6258 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006259
Kevin Enderbyf6d25852015-01-31 00:37:11 +00006260 const char *SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
Kevin Enderbybf246f52014-09-24 23:08:22 +00006261
Kevin Enderby85974882014-09-26 22:20:44 +00006262 if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) {
6263 *ReferenceName = GuessIndirectSymbol(ReferenceValue, info);
Kevin Enderby04bf6932014-10-28 23:39:46 +00006264 if (*ReferenceName != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006265 method_reference(info, ReferenceType, ReferenceName);
6266 if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message)
6267 *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub;
Rafael Espindolab940b662016-09-06 19:16:48 +00006268 } else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
Kevin Enderby04bf6932014-10-28 23:39:46 +00006269 if (info->demangled_name != nullptr)
6270 free(info->demangled_name);
6271 int status;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006272 info->demangled_name =
Rafael Espindolab940b662016-09-06 19:16:48 +00006273 itaniumDemangle(SymbolName + 1, nullptr, nullptr, &status);
Kevin Enderby04bf6932014-10-28 23:39:46 +00006274 if (info->demangled_name != nullptr) {
6275 *ReferenceName = info->demangled_name;
6276 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
6277 } else
6278 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6279 } else
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006280 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6281 } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) {
6282 *ReferenceName =
6283 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
Kevin Enderby85974882014-09-26 22:20:44 +00006284 if (*ReferenceName)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006285 method_reference(info, ReferenceType, ReferenceName);
Kevin Enderby85974882014-09-26 22:20:44 +00006286 else
6287 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006288 // If this is arm64 and the reference is an adrp instruction save the
6289 // instruction, passed in ReferenceValue and the address of the instruction
6290 // for use later if we see and add immediate instruction.
6291 } else if (info->O->getArch() == Triple::aarch64 &&
6292 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) {
6293 info->adrp_inst = ReferenceValue;
6294 info->adrp_addr = ReferencePC;
6295 SymbolName = nullptr;
6296 *ReferenceName = nullptr;
6297 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6298 // If this is arm64 and reference is an add immediate instruction and we
6299 // have
6300 // seen an adrp instruction just before it and the adrp's Xd register
6301 // matches
6302 // this add's Xn register reconstruct the value being referenced and look to
6303 // see if it is a literal pointer. Note the add immediate instruction is
6304 // passed in ReferenceValue.
6305 } else if (info->O->getArch() == Triple::aarch64 &&
6306 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri &&
6307 ReferencePC - 4 == info->adrp_addr &&
6308 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
6309 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
6310 uint32_t addxri_inst;
6311 uint64_t adrp_imm, addxri_imm;
6312
6313 adrp_imm =
6314 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
6315 if (info->adrp_inst & 0x0200000)
6316 adrp_imm |= 0xfffffffffc000000LL;
6317
6318 addxri_inst = ReferenceValue;
6319 addxri_imm = (addxri_inst >> 10) & 0xfff;
6320 if (((addxri_inst >> 22) & 0x3) == 1)
6321 addxri_imm <<= 12;
6322
6323 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
6324 (adrp_imm << 12) + addxri_imm;
6325
6326 *ReferenceName =
6327 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6328 if (*ReferenceName == nullptr)
6329 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6330 // If this is arm64 and the reference is a load register instruction and we
6331 // have seen an adrp instruction just before it and the adrp's Xd register
6332 // matches this add's Xn register reconstruct the value being referenced and
6333 // look to see if it is a literal pointer. Note the load register
6334 // instruction is passed in ReferenceValue.
6335 } else if (info->O->getArch() == Triple::aarch64 &&
6336 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui &&
6337 ReferencePC - 4 == info->adrp_addr &&
6338 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
6339 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
6340 uint32_t ldrxui_inst;
6341 uint64_t adrp_imm, ldrxui_imm;
6342
6343 adrp_imm =
6344 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
6345 if (info->adrp_inst & 0x0200000)
6346 adrp_imm |= 0xfffffffffc000000LL;
6347
6348 ldrxui_inst = ReferenceValue;
6349 ldrxui_imm = (ldrxui_inst >> 10) & 0xfff;
6350
6351 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
6352 (adrp_imm << 12) + (ldrxui_imm << 3);
6353
6354 *ReferenceName =
6355 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6356 if (*ReferenceName == nullptr)
6357 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6358 }
6359 // If this arm64 and is an load register (PC-relative) instruction the
6360 // ReferenceValue is the PC plus the immediate value.
6361 else if (info->O->getArch() == Triple::aarch64 &&
6362 (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl ||
6363 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR)) {
6364 *ReferenceName =
6365 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6366 if (*ReferenceName == nullptr)
6367 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Rafael Espindolab940b662016-09-06 19:16:48 +00006368 } else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
Kevin Enderby04bf6932014-10-28 23:39:46 +00006369 if (info->demangled_name != nullptr)
6370 free(info->demangled_name);
6371 int status;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006372 info->demangled_name =
Rafael Espindolab940b662016-09-06 19:16:48 +00006373 itaniumDemangle(SymbolName + 1, nullptr, nullptr, &status);
Kevin Enderby04bf6932014-10-28 23:39:46 +00006374 if (info->demangled_name != nullptr) {
6375 *ReferenceName = info->demangled_name;
6376 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
6377 }
6378 }
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006379 else {
Kevin Enderbybf246f52014-09-24 23:08:22 +00006380 *ReferenceName = nullptr;
6381 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6382 }
6383
6384 return SymbolName;
6385}
6386
Kevin Enderbybf246f52014-09-24 23:08:22 +00006387/// \brief Emits the comments that are stored in the CommentStream.
6388/// Each comment in the CommentStream must end with a newline.
6389static void emitComments(raw_svector_ostream &CommentStream,
6390 SmallString<128> &CommentsToEmit,
6391 formatted_raw_ostream &FormattedOS,
6392 const MCAsmInfo &MAI) {
6393 // Flush the stream before taking its content.
Kevin Enderbybf246f52014-09-24 23:08:22 +00006394 StringRef Comments = CommentsToEmit.str();
6395 // Get the default information for printing a comment.
Mehdi Amini36d33fc2016-10-01 06:46:33 +00006396 StringRef CommentBegin = MAI.getCommentString();
Kevin Enderbybf246f52014-09-24 23:08:22 +00006397 unsigned CommentColumn = MAI.getCommentColumn();
6398 bool IsFirst = true;
6399 while (!Comments.empty()) {
6400 if (!IsFirst)
6401 FormattedOS << '\n';
6402 // Emit a line of comments.
6403 FormattedOS.PadToColumn(CommentColumn);
6404 size_t Position = Comments.find('\n');
6405 FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
6406 // Move after the newline character.
6407 Comments = Comments.substr(Position + 1);
6408 IsFirst = false;
6409 }
6410 FormattedOS.flush();
6411
6412 // Tell the comment stream that the vector changed underneath it.
6413 CommentsToEmit.clear();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006414}
6415
Kevin Enderby95df54c2015-02-04 01:01:38 +00006416static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
6417 StringRef DisSegName, StringRef DisSectName) {
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006418 const char *McpuDefault = nullptr;
6419 const Target *ThumbTarget = nullptr;
6420 const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006421 if (!TheTarget) {
6422 // GetTarget prints out stuff.
6423 return;
6424 }
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006425 if (MCPU.empty() && McpuDefault)
6426 MCPU = McpuDefault;
6427
Ahmed Charles56440fd2014-03-06 05:51:42 +00006428 std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006429 std::unique_ptr<const MCInstrInfo> ThumbInstrInfo;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006430 if (ThumbTarget)
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006431 ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo());
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006432
Kevin Enderbyc9595622014-08-06 23:24:41 +00006433 // Package up features to be passed to target/subtarget
6434 std::string FeaturesStr;
6435 if (MAttrs.size()) {
6436 SubtargetFeatures Features;
6437 for (unsigned i = 0; i != MAttrs.size(); ++i)
6438 Features.AddFeature(MAttrs[i]);
6439 FeaturesStr = Features.getString();
6440 }
6441
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006442 // Set up disassembler.
Ahmed Charles56440fd2014-03-06 05:51:42 +00006443 std::unique_ptr<const MCRegisterInfo> MRI(
6444 TheTarget->createMCRegInfo(TripleName));
6445 std::unique_ptr<const MCAsmInfo> AsmInfo(
Rafael Espindola227144c2013-05-13 01:16:13 +00006446 TheTarget->createMCAsmInfo(*MRI, TripleName));
Ahmed Charles56440fd2014-03-06 05:51:42 +00006447 std::unique_ptr<const MCSubtargetInfo> STI(
Kevin Enderbyc9595622014-08-06 23:24:41 +00006448 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
Craig Toppere6cb63e2014-04-25 04:24:47 +00006449 MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr);
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006450 std::unique_ptr<MCDisassembler> DisAsm(
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006451 TheTarget->createMCDisassembler(*STI, Ctx));
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006452 std::unique_ptr<MCSymbolizer> Symbolizer;
6453 struct DisassembleInfo SymbolizerInfo;
6454 std::unique_ptr<MCRelocationInfo> RelInfo(
6455 TheTarget->createMCRelocationInfo(TripleName, Ctx));
6456 if (RelInfo) {
6457 Symbolizer.reset(TheTarget->createMCSymbolizer(
6458 TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
David Blaikie186db432015-01-18 20:45:48 +00006459 &SymbolizerInfo, &Ctx, std::move(RelInfo)));
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006460 DisAsm->setSymbolizer(std::move(Symbolizer));
6461 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006462 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
Ahmed Charles56440fd2014-03-06 05:51:42 +00006463 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
Daniel Sanders50f17232015-09-15 16:17:27 +00006464 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI));
Kevin Enderbybf246f52014-09-24 23:08:22 +00006465 // Set the display preference for hex vs. decimal immediates.
6466 IP->setPrintImmHex(PrintImmHex);
6467 // Comment stream and backing vector.
6468 SmallString<128> CommentsToEmit;
6469 raw_svector_ostream CommentStream(CommentsToEmit);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00006470 // FIXME: Setting the CommentStream in the InstPrinter is problematic in that
6471 // if it is done then arm64 comments for string literals don't get printed
6472 // and some constant get printed instead and not setting it causes intel
6473 // (32-bit and 64-bit) comments printed with different spacing before the
6474 // comment causing different diffs with the 'C' disassembler library API.
6475 // IP->setCommentStream(CommentStream);
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006476
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006477 if (!AsmInfo || !STI || !DisAsm || !IP) {
Michael J. Spencerc1363cf2011-10-07 19:25:47 +00006478 errs() << "error: couldn't initialize disassembler for target "
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006479 << TripleName << '\n';
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006480 return;
6481 }
6482
Tim Northover09ca33e2016-04-22 23:23:31 +00006483 // Set up separate thumb disassembler if needed.
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006484 std::unique_ptr<const MCRegisterInfo> ThumbMRI;
6485 std::unique_ptr<const MCAsmInfo> ThumbAsmInfo;
6486 std::unique_ptr<const MCSubtargetInfo> ThumbSTI;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006487 std::unique_ptr<MCDisassembler> ThumbDisAsm;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006488 std::unique_ptr<MCInstPrinter> ThumbIP;
6489 std::unique_ptr<MCContext> ThumbCtx;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006490 std::unique_ptr<MCSymbolizer> ThumbSymbolizer;
6491 struct DisassembleInfo ThumbSymbolizerInfo;
6492 std::unique_ptr<MCRelocationInfo> ThumbRelInfo;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006493 if (ThumbTarget) {
6494 ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName));
6495 ThumbAsmInfo.reset(
6496 ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName));
6497 ThumbSTI.reset(
6498 ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MCPU, FeaturesStr));
6499 ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr));
6500 ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx));
Kevin Enderby930fdc72014-11-06 19:00:13 +00006501 MCContext *PtrThumbCtx = ThumbCtx.get();
6502 ThumbRelInfo.reset(
6503 ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx));
6504 if (ThumbRelInfo) {
6505 ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer(
6506 ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
David Blaikie186db432015-01-18 20:45:48 +00006507 &ThumbSymbolizerInfo, PtrThumbCtx, std::move(ThumbRelInfo)));
Kevin Enderby930fdc72014-11-06 19:00:13 +00006508 ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer));
6509 }
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006510 int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect();
6511 ThumbIP.reset(ThumbTarget->createMCInstPrinter(
Daniel Sanders50f17232015-09-15 16:17:27 +00006512 Triple(ThumbTripleName), ThumbAsmPrinterVariant, *ThumbAsmInfo,
6513 *ThumbInstrInfo, *ThumbMRI));
Kevin Enderbybf246f52014-09-24 23:08:22 +00006514 // Set the display preference for hex vs. decimal immediates.
6515 ThumbIP->setPrintImmHex(PrintImmHex);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006516 }
6517
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006518 if (ThumbTarget && (!ThumbAsmInfo || !ThumbSTI || !ThumbDisAsm || !ThumbIP)) {
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006519 errs() << "error: couldn't initialize disassembler for target "
6520 << ThumbTripleName << '\n';
6521 return;
6522 }
6523
Charles Davis8bdfafd2013-09-01 04:28:48 +00006524 MachO::mach_header Header = MachOOF->getHeader();
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006525
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006526 // FIXME: Using the -cfg command line option, this code used to be able to
6527 // annotate relocations with the referenced symbol's name, and if this was
6528 // inside a __[cf]string section, the data it points to. This is now replaced
6529 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
Owen Andersond9243c42011-10-17 21:37:35 +00006530 std::vector<SectionRef> Sections;
6531 std::vector<SymbolRef> Symbols;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006532 SmallVector<uint64_t, 8> FoundFns;
Kevin Enderby273ae012013-06-06 17:20:50 +00006533 uint64_t BaseSegmentAddress;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006534
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00006535 getSectionsAndSymbols(MachOOF, Sections, Symbols, FoundFns,
Kevin Enderby273ae012013-06-06 17:20:50 +00006536 BaseSegmentAddress);
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006537
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006538 // Sort the symbols by address, just in case they didn't come in that way.
Owen Andersond9243c42011-10-17 21:37:35 +00006539 std::sort(Symbols.begin(), Symbols.end(), SymbolSorter());
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006540
Kevin Enderby273ae012013-06-06 17:20:50 +00006541 // Build a data in code table that is sorted on by the address of each entry.
6542 uint64_t BaseAddress = 0;
Charles Davis8bdfafd2013-09-01 04:28:48 +00006543 if (Header.filetype == MachO::MH_OBJECT)
Rafael Espindola80291272014-10-08 15:28:58 +00006544 BaseAddress = Sections[0].getAddress();
Kevin Enderby273ae012013-06-06 17:20:50 +00006545 else
6546 BaseAddress = BaseSegmentAddress;
6547 DiceTable Dices;
Kevin Enderby273ae012013-06-06 17:20:50 +00006548 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
Rafael Espindola5e812af2014-01-30 02:49:50 +00006549 DI != DE; ++DI) {
Kevin Enderby273ae012013-06-06 17:20:50 +00006550 uint32_t Offset;
6551 DI->getOffset(Offset);
6552 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
6553 }
6554 array_pod_sort(Dices.begin(), Dices.end());
6555
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006556#ifndef NDEBUG
6557 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
6558#else
6559 raw_ostream &DebugOut = nulls();
6560#endif
6561
Ahmed Charles56440fd2014-03-06 05:51:42 +00006562 std::unique_ptr<DIContext> diContext;
Rafael Espindola9b709252013-04-13 01:45:40 +00006563 ObjectFile *DbgObj = MachOOF;
Benjamin Kramer699128e2011-09-21 01:13:19 +00006564 // Try to find debug info and set up the DIContext for it.
6565 if (UseDbg) {
Benjamin Kramer699128e2011-09-21 01:13:19 +00006566 // A separate DSym file path was specified, parse it as a macho file,
6567 // get the sections and supply it to the section name parsing machinery.
6568 if (!DSYMFile.empty()) {
Rafael Espindola48af1c22014-08-19 18:44:46 +00006569 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
Rafael Espindolaadf21f22014-07-06 17:43:13 +00006570 MemoryBuffer::getFileOrSTDIN(DSYMFile);
Rafael Espindola48af1c22014-08-19 18:44:46 +00006571 if (std::error_code EC = BufOrErr.getError()) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +00006572 errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n';
Benjamin Kramer699128e2011-09-21 01:13:19 +00006573 return;
6574 }
Rafael Espindola48af1c22014-08-19 18:44:46 +00006575 DbgObj =
6576 ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef())
6577 .get()
6578 .release();
Benjamin Kramer699128e2011-09-21 01:13:19 +00006579 }
6580
Eric Christopher7370b552012-11-12 21:40:38 +00006581 // Setup the DIContext
Zachary Turner6489d7b2015-04-23 17:37:47 +00006582 diContext.reset(new DWARFContextInMemory(*DbgObj));
Benjamin Kramer699128e2011-09-21 01:13:19 +00006583 }
6584
Colin LeMahieufcc32762015-07-29 19:08:10 +00006585 if (FilterSections.size() == 0)
Kevin Enderby95df54c2015-02-04 01:01:38 +00006586 outs() << "(" << DisSegName << "," << DisSectName << ") section\n";
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00006587
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006588 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
Owen Andersond9243c42011-10-17 21:37:35 +00006589 StringRef SectName;
Kevin Enderby95df54c2015-02-04 01:01:38 +00006590 if (Sections[SectIdx].getName(SectName) || SectName != DisSectName)
6591 continue;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006592
Rafael Espindolaa9f810b2012-12-21 03:47:03 +00006593 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006594
Rafael Espindolab0f76a42013-04-05 15:15:22 +00006595 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
Kevin Enderby95df54c2015-02-04 01:01:38 +00006596 if (SegmentName != DisSegName)
Rafael Espindolaa9f810b2012-12-21 03:47:03 +00006597 continue;
6598
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006599 StringRef BytesStr;
6600 Sections[SectIdx].getContents(BytesStr);
Aaron Ballman106fd7b2014-11-12 14:01:17 +00006601 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
6602 BytesStr.size());
Rafael Espindola80291272014-10-08 15:28:58 +00006603 uint64_t SectAddress = Sections[SectIdx].getAddress();
Rafael Espindolabd604f22014-11-07 00:52:15 +00006604
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006605 bool symbolTableWorked = false;
6606
Kevin Enderbybf246f52014-09-24 23:08:22 +00006607 // Create a map of symbol addresses to symbol names for use by
6608 // the SymbolizerSymbolLookUp() routine.
6609 SymbolAddressMap AddrMap;
Kevin Enderby6a221752015-03-17 17:10:57 +00006610 bool DisSymNameFound = false;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006611 for (const SymbolRef &Symbol : MachOOF->symbols()) {
Kevin Enderby7bd8d992016-05-02 20:28:12 +00006612 Expected<SymbolRef::Type> STOrErr = Symbol.getType();
6613 if (!STOrErr) {
6614 std::string Buf;
6615 raw_string_ostream OS(Buf);
6616 logAllUnhandledErrors(STOrErr.takeError(), OS, "");
6617 OS.flush();
6618 report_fatal_error(Buf);
6619 }
Kevin Enderby5afbc1c2016-03-23 20:27:00 +00006620 SymbolRef::Type ST = *STOrErr;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006621 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
6622 ST == SymbolRef::ST_Other) {
Rafael Espindoladea00162015-07-03 17:44:18 +00006623 uint64_t Address = Symbol.getValue();
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00006624 Expected<StringRef> SymNameOrErr = Symbol.getName();
6625 if (!SymNameOrErr) {
6626 std::string Buf;
6627 raw_string_ostream OS(Buf);
6628 logAllUnhandledErrors(SymNameOrErr.takeError(), OS, "");
6629 OS.flush();
6630 report_fatal_error(Buf);
6631 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00006632 StringRef SymName = *SymNameOrErr;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006633 AddrMap[Address] = SymName;
Kevin Enderby6a221752015-03-17 17:10:57 +00006634 if (!DisSymName.empty() && DisSymName == SymName)
6635 DisSymNameFound = true;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006636 }
6637 }
David Blaikie33dd45d02015-03-23 18:39:02 +00006638 if (!DisSymName.empty() && !DisSymNameFound) {
Kevin Enderby6a221752015-03-17 17:10:57 +00006639 outs() << "Can't find -dis-symname: " << DisSymName << "\n";
6640 return;
6641 }
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006642 // Set up the block of info used by the Symbolizer call backs.
Kevin Enderby8e29ec92015-03-17 22:26:11 +00006643 SymbolizerInfo.verbose = !NoSymbolicOperands;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006644 SymbolizerInfo.O = MachOOF;
6645 SymbolizerInfo.S = Sections[SectIdx];
Kevin Enderbybf246f52014-09-24 23:08:22 +00006646 SymbolizerInfo.AddrMap = &AddrMap;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006647 SymbolizerInfo.Sections = &Sections;
6648 SymbolizerInfo.class_name = nullptr;
6649 SymbolizerInfo.selector_name = nullptr;
6650 SymbolizerInfo.method = nullptr;
Kevin Enderby04bf6932014-10-28 23:39:46 +00006651 SymbolizerInfo.demangled_name = nullptr;
Kevin Enderby078be602014-10-23 19:53:12 +00006652 SymbolizerInfo.bindtable = nullptr;
Kevin Enderby10738222014-11-19 20:20:16 +00006653 SymbolizerInfo.adrp_addr = 0;
6654 SymbolizerInfo.adrp_inst = 0;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006655 // Same for the ThumbSymbolizer
Kevin Enderby8e29ec92015-03-17 22:26:11 +00006656 ThumbSymbolizerInfo.verbose = !NoSymbolicOperands;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006657 ThumbSymbolizerInfo.O = MachOOF;
6658 ThumbSymbolizerInfo.S = Sections[SectIdx];
6659 ThumbSymbolizerInfo.AddrMap = &AddrMap;
6660 ThumbSymbolizerInfo.Sections = &Sections;
6661 ThumbSymbolizerInfo.class_name = nullptr;
6662 ThumbSymbolizerInfo.selector_name = nullptr;
6663 ThumbSymbolizerInfo.method = nullptr;
6664 ThumbSymbolizerInfo.demangled_name = nullptr;
6665 ThumbSymbolizerInfo.bindtable = nullptr;
Kevin Enderby10738222014-11-19 20:20:16 +00006666 ThumbSymbolizerInfo.adrp_addr = 0;
6667 ThumbSymbolizerInfo.adrp_inst = 0;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006668
Kevin Enderby4b627be2016-04-28 20:14:13 +00006669 unsigned int Arch = MachOOF->getArch();
6670
Tim Northoverf203ab52016-07-14 22:13:32 +00006671 // Skip all symbols if this is a stubs file.
6672 if (Bytes.size() == 0)
6673 return;
6674
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006675 // Disassemble symbol by symbol.
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006676 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00006677 Expected<StringRef> SymNameOrErr = Symbols[SymIdx].getName();
6678 if (!SymNameOrErr) {
6679 std::string Buf;
6680 raw_string_ostream OS(Buf);
6681 logAllUnhandledErrors(SymNameOrErr.takeError(), OS, "");
6682 OS.flush();
6683 report_fatal_error(Buf);
6684 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00006685 StringRef SymName = *SymNameOrErr;
Owen Andersond9243c42011-10-17 21:37:35 +00006686
Kevin Enderby7bd8d992016-05-02 20:28:12 +00006687 Expected<SymbolRef::Type> STOrErr = Symbols[SymIdx].getType();
6688 if (!STOrErr) {
6689 std::string Buf;
6690 raw_string_ostream OS(Buf);
6691 logAllUnhandledErrors(STOrErr.takeError(), OS, "");
6692 OS.flush();
6693 report_fatal_error(Buf);
6694 }
Kevin Enderby5afbc1c2016-03-23 20:27:00 +00006695 SymbolRef::Type ST = *STOrErr;
Kuba Breckade833222015-11-12 09:40:29 +00006696 if (ST != SymbolRef::ST_Function && ST != SymbolRef::ST_Data)
Owen Andersond9243c42011-10-17 21:37:35 +00006697 continue;
6698
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006699 // Make sure the symbol is defined in this section.
Rafael Espindola80291272014-10-08 15:28:58 +00006700 bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]);
Kevin Enderbyd8a6e832016-06-15 21:14:01 +00006701 if (!containsSym) {
6702 if (!DisSymName.empty() && DisSymName == SymName) {
6703 outs() << "-dis-symname: " << DisSymName << " not in the section\n";
6704 return;
6705 }
6706 continue;
6707 }
6708 // The __mh_execute_header is special and we need to deal with that fact
6709 // this symbol is before the start of the (__TEXT,__text) section and at the
6710 // address of the start of the __TEXT segment. This is because this symbol
6711 // is an N_SECT symbol in the (__TEXT,__text) but its address is before the
6712 // start of the section in a standard MH_EXECUTE filetype.
6713 if (!DisSymName.empty() && DisSymName == "__mh_execute_header") {
6714 outs() << "-dis-symname: __mh_execute_header not in any section\n";
6715 return;
6716 }
Tim Northoverfbefee32016-07-14 23:13:03 +00006717 // When this code is trying to disassemble a symbol at a time and in the
6718 // case there is only the __mh_execute_header symbol left as in a stripped
6719 // executable, we need to deal with this by ignoring this symbol so the
6720 // whole section is disassembled and this symbol is then not displayed.
6721 if (SymName == "__mh_execute_header" || SymName == "__mh_dylib_header" ||
6722 SymName == "__mh_bundle_header" || SymName == "__mh_object_header" ||
6723 SymName == "__mh_preload_header" || SymName == "__mh_dylinker_header")
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006724 continue;
6725
Kevin Enderby6a221752015-03-17 17:10:57 +00006726 // If we are only disassembling one symbol see if this is that symbol.
6727 if (!DisSymName.empty() && DisSymName != SymName)
6728 continue;
6729
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006730 // Start at the address of the symbol relative to the section's address.
Tim Northoverf203ab52016-07-14 22:13:32 +00006731 uint64_t SectSize = Sections[SectIdx].getSize();
Rafael Espindoladea00162015-07-03 17:44:18 +00006732 uint64_t Start = Symbols[SymIdx].getValue();
Rafael Espindola80291272014-10-08 15:28:58 +00006733 uint64_t SectionAddress = Sections[SectIdx].getAddress();
Cameron Zwarich54478a52012-02-03 05:42:17 +00006734 Start -= SectionAddress;
Owen Andersond9243c42011-10-17 21:37:35 +00006735
Tim Northoverf203ab52016-07-14 22:13:32 +00006736 if (Start > SectSize) {
6737 outs() << "section data ends, " << SymName
6738 << " lies outside valid range\n";
6739 return;
6740 }
6741
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006742 // Stop disassembling either at the beginning of the next symbol or at
6743 // the end of the section.
Kevin Enderbyedd58722012-05-15 18:57:14 +00006744 bool containsNextSym = false;
Owen Andersond9243c42011-10-17 21:37:35 +00006745 uint64_t NextSym = 0;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006746 uint64_t NextSymIdx = SymIdx + 1;
Owen Andersond9243c42011-10-17 21:37:35 +00006747 while (Symbols.size() > NextSymIdx) {
Kevin Enderby7bd8d992016-05-02 20:28:12 +00006748 Expected<SymbolRef::Type> STOrErr = Symbols[NextSymIdx].getType();
6749 if (!STOrErr) {
6750 std::string Buf;
6751 raw_string_ostream OS(Buf);
6752 logAllUnhandledErrors(STOrErr.takeError(), OS, "");
6753 OS.flush();
6754 report_fatal_error(Buf);
6755 }
Kevin Enderby5afbc1c2016-03-23 20:27:00 +00006756 SymbolRef::Type NextSymType = *STOrErr;
Owen Andersond9243c42011-10-17 21:37:35 +00006757 if (NextSymType == SymbolRef::ST_Function) {
Rafael Espindola80291272014-10-08 15:28:58 +00006758 containsNextSym =
6759 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
Rafael Espindoladea00162015-07-03 17:44:18 +00006760 NextSym = Symbols[NextSymIdx].getValue();
Cameron Zwarich54478a52012-02-03 05:42:17 +00006761 NextSym -= SectionAddress;
Owen Andersond9243c42011-10-17 21:37:35 +00006762 break;
6763 }
6764 ++NextSymIdx;
6765 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006766
Tim Northoverf203ab52016-07-14 22:13:32 +00006767 uint64_t End = containsNextSym ? std::min(NextSym, SectSize) : SectSize;
Owen Andersond9243c42011-10-17 21:37:35 +00006768 uint64_t Size;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006769
6770 symbolTableWorked = true;
Rafael Espindolabd604f22014-11-07 00:52:15 +00006771
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006772 DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
Tim Northover09ca33e2016-04-22 23:23:31 +00006773 bool IsThumb = MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb;
6774
6775 // We only need the dedicated Thumb target if there's a real choice
6776 // (i.e. we're not targeting M-class) and the function is Thumb.
6777 bool UseThumbTarget = IsThumb && ThumbTarget;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006778
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006779 outs() << SymName << ":\n";
6780 DILineInfo lastLine;
6781 for (uint64_t Index = Start; Index < End; Index += Size) {
6782 MCInst Inst;
Owen Andersond9243c42011-10-17 21:37:35 +00006783
Kevin Enderbybf246f52014-09-24 23:08:22 +00006784 uint64_t PC = SectAddress + Index;
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00006785 if (!NoLeadingAddr) {
6786 if (FullLeadingAddr) {
6787 if (MachOOF->is64Bit())
6788 outs() << format("%016" PRIx64, PC);
6789 else
6790 outs() << format("%08" PRIx64, PC);
6791 } else {
6792 outs() << format("%8" PRIx64 ":", PC);
6793 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006794 }
Kevin Enderby4b627be2016-04-28 20:14:13 +00006795 if (!NoShowRawInsn || Arch == Triple::arm)
Kevin Enderbybf246f52014-09-24 23:08:22 +00006796 outs() << "\t";
Kevin Enderby273ae012013-06-06 17:20:50 +00006797
6798 // Check the data in code table here to see if this is data not an
6799 // instruction to be disassembled.
6800 DiceTable Dice;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006801 Dice.push_back(std::make_pair(PC, DiceRef()));
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006802 dice_table_iterator DTI =
6803 std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(),
6804 compareDiceTableEntries);
6805 if (DTI != Dices.end()) {
Kevin Enderby273ae012013-06-06 17:20:50 +00006806 uint16_t Length;
6807 DTI->second.getLength(Length);
Kevin Enderby273ae012013-06-06 17:20:50 +00006808 uint16_t Kind;
6809 DTI->second.getKind(Kind);
Colin LeMahieufc32b1b2015-03-18 19:27:31 +00006810 Size = DumpDataInCode(Bytes.data() + Index, Length, Kind);
Kevin Enderby930fdc72014-11-06 19:00:13 +00006811 if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) &&
6812 (PC == (DTI->first + Length - 1)) && (Length & 1))
6813 Size++;
Kevin Enderby273ae012013-06-06 17:20:50 +00006814 continue;
6815 }
6816
Kevin Enderbybf246f52014-09-24 23:08:22 +00006817 SmallVector<char, 64> AnnotationsBytes;
6818 raw_svector_ostream Annotations(AnnotationsBytes);
6819
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006820 bool gotInst;
Tim Northover09ca33e2016-04-22 23:23:31 +00006821 if (UseThumbTarget)
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006822 gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006823 PC, DebugOut, Annotations);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006824 else
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006825 gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC,
Kevin Enderbybf246f52014-09-24 23:08:22 +00006826 DebugOut, Annotations);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006827 if (gotInst) {
Kevin Enderby4b627be2016-04-28 20:14:13 +00006828 if (!NoShowRawInsn || Arch == Triple::arm) {
Craig Topper0013be12015-09-21 05:32:41 +00006829 dumpBytes(makeArrayRef(Bytes.data() + Index, Size), outs());
Kevin Enderbybf246f52014-09-24 23:08:22 +00006830 }
6831 formatted_raw_ostream FormattedOS(outs());
Kevin Enderbybf246f52014-09-24 23:08:22 +00006832 StringRef AnnotationsStr = Annotations.str();
Tim Northover09ca33e2016-04-22 23:23:31 +00006833 if (UseThumbTarget)
Akira Hatanakab46d0232015-03-27 20:36:02 +00006834 ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr, *ThumbSTI);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006835 else
Akira Hatanaka1d079942015-03-28 20:44:05 +00006836 IP->printInst(&Inst, FormattedOS, AnnotationsStr, *STI);
Kevin Enderbybf246f52014-09-24 23:08:22 +00006837 emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo);
Owen Andersond9243c42011-10-17 21:37:35 +00006838
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006839 // Print debug info.
6840 if (diContext) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006841 DILineInfo dli = diContext->getLineInfoForAddress(PC);
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006842 // Print valid line info if it changed.
Alexey Samsonovd0109992014-04-18 21:36:39 +00006843 if (dli != lastLine && dli.Line != 0)
6844 outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
6845 << dli.Column;
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006846 lastLine = dli;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006847 }
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006848 outs() << "\n";
6849 } else {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006850 unsigned int Arch = MachOOF->getArch();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006851 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006852 outs() << format("\t.byte 0x%02x #bad opcode\n",
6853 *(Bytes.data() + Index) & 0xff);
6854 Size = 1; // skip exactly one illegible byte and move on.
Tim Northover09ca33e2016-04-22 23:23:31 +00006855 } else if (Arch == Triple::aarch64 ||
6856 (Arch == Triple::arm && !IsThumb)) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006857 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
6858 (*(Bytes.data() + Index + 1) & 0xff) << 8 |
6859 (*(Bytes.data() + Index + 2) & 0xff) << 16 |
6860 (*(Bytes.data() + Index + 3) & 0xff) << 24;
6861 outs() << format("\t.long\t0x%08x\n", opcode);
6862 Size = 4;
Tim Northover09ca33e2016-04-22 23:23:31 +00006863 } else if (Arch == Triple::arm) {
6864 assert(IsThumb && "ARM mode should have been dealt with above");
6865 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
6866 (*(Bytes.data() + Index + 1) & 0xff) << 8;
6867 outs() << format("\t.short\t0x%04x\n", opcode);
6868 Size = 2;
6869 } else{
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006870 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6871 if (Size == 0)
6872 Size = 1; // skip illegible bytes
6873 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006874 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006875 }
6876 }
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006877 if (!symbolTableWorked) {
Rafael Espindola80291272014-10-08 15:28:58 +00006878 // Reading the symbol table didn't work, disassemble the whole section.
6879 uint64_t SectAddress = Sections[SectIdx].getAddress();
6880 uint64_t SectSize = Sections[SectIdx].getSize();
Kevin Enderbybadd1002012-05-18 00:13:56 +00006881 uint64_t InstSize;
6882 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
Bill Wendling4e68e062012-07-19 00:17:40 +00006883 MCInst Inst;
Kevin Enderbybadd1002012-05-18 00:13:56 +00006884
Kevin Enderbybf246f52014-09-24 23:08:22 +00006885 uint64_t PC = SectAddress + Index;
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006886 if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC,
6887 DebugOut, nulls())) {
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00006888 if (!NoLeadingAddr) {
6889 if (FullLeadingAddr) {
6890 if (MachOOF->is64Bit())
6891 outs() << format("%016" PRIx64, PC);
6892 else
6893 outs() << format("%08" PRIx64, PC);
6894 } else {
6895 outs() << format("%8" PRIx64 ":", PC);
6896 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006897 }
Kevin Enderby4b627be2016-04-28 20:14:13 +00006898 if (!NoShowRawInsn || Arch == Triple::arm) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00006899 outs() << "\t";
Craig Topper0013be12015-09-21 05:32:41 +00006900 dumpBytes(makeArrayRef(Bytes.data() + Index, InstSize), outs());
Kevin Enderbybf246f52014-09-24 23:08:22 +00006901 }
Akira Hatanaka1d079942015-03-28 20:44:05 +00006902 IP->printInst(&Inst, outs(), "", *STI);
Bill Wendling4e68e062012-07-19 00:17:40 +00006903 outs() << "\n";
6904 } else {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006905 unsigned int Arch = MachOOF->getArch();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006906 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006907 outs() << format("\t.byte 0x%02x #bad opcode\n",
6908 *(Bytes.data() + Index) & 0xff);
6909 InstSize = 1; // skip exactly one illegible byte and move on.
6910 } else {
6911 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6912 if (InstSize == 0)
6913 InstSize = 1; // skip illegible bytes
6914 }
Bill Wendling4e68e062012-07-19 00:17:40 +00006915 }
Kevin Enderbybadd1002012-05-18 00:13:56 +00006916 }
6917 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00006918 // The TripleName's need to be reset if we are called again for a different
6919 // archtecture.
6920 TripleName = "";
6921 ThumbTripleName = "";
6922
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006923 if (SymbolizerInfo.method != nullptr)
6924 free(SymbolizerInfo.method);
Kevin Enderby04bf6932014-10-28 23:39:46 +00006925 if (SymbolizerInfo.demangled_name != nullptr)
6926 free(SymbolizerInfo.demangled_name);
Kevin Enderby078be602014-10-23 19:53:12 +00006927 if (SymbolizerInfo.bindtable != nullptr)
6928 delete SymbolizerInfo.bindtable;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006929 if (ThumbSymbolizerInfo.method != nullptr)
6930 free(ThumbSymbolizerInfo.method);
6931 if (ThumbSymbolizerInfo.demangled_name != nullptr)
6932 free(ThumbSymbolizerInfo.demangled_name);
6933 if (ThumbSymbolizerInfo.bindtable != nullptr)
6934 delete ThumbSymbolizerInfo.bindtable;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006935 }
6936}
Tim Northover4bd286a2014-08-01 13:07:19 +00006937
Tim Northover39c70bb2014-08-12 11:52:59 +00006938//===----------------------------------------------------------------------===//
6939// __compact_unwind section dumping
6940//===----------------------------------------------------------------------===//
6941
Tim Northover4bd286a2014-08-01 13:07:19 +00006942namespace {
Tim Northover39c70bb2014-08-12 11:52:59 +00006943
6944template <typename T> static uint64_t readNext(const char *&Buf) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006945 using llvm::support::little;
6946 using llvm::support::unaligned;
Tim Northover39c70bb2014-08-12 11:52:59 +00006947
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006948 uint64_t Val = support::endian::read<T, little, unaligned>(Buf);
6949 Buf += sizeof(T);
6950 return Val;
6951}
Tim Northover39c70bb2014-08-12 11:52:59 +00006952
Tim Northover4bd286a2014-08-01 13:07:19 +00006953struct CompactUnwindEntry {
6954 uint32_t OffsetInSection;
6955
6956 uint64_t FunctionAddr;
6957 uint32_t Length;
6958 uint32_t CompactEncoding;
6959 uint64_t PersonalityAddr;
6960 uint64_t LSDAAddr;
6961
6962 RelocationRef FunctionReloc;
6963 RelocationRef PersonalityReloc;
6964 RelocationRef LSDAReloc;
6965
6966 CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64)
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006967 : OffsetInSection(Offset) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006968 if (Is64)
6969 read<uint64_t>(Contents.data() + Offset);
6970 else
6971 read<uint32_t>(Contents.data() + Offset);
6972 }
6973
6974private:
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006975 template <typename UIntPtr> void read(const char *Buf) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006976 FunctionAddr = readNext<UIntPtr>(Buf);
6977 Length = readNext<uint32_t>(Buf);
6978 CompactEncoding = readNext<uint32_t>(Buf);
6979 PersonalityAddr = readNext<UIntPtr>(Buf);
6980 LSDAAddr = readNext<UIntPtr>(Buf);
6981 }
6982};
6983}
6984
6985/// Given a relocation from __compact_unwind, consisting of the RelocationRef
6986/// and data being relocated, determine the best base Name and Addend to use for
6987/// display purposes.
6988///
6989/// 1. An Extern relocation will directly reference a symbol (and the data is
6990/// then already an addend), so use that.
6991/// 2. Otherwise the data is an offset in the object file's layout; try to find
6992// a symbol before it in the same section, and use the offset from there.
6993/// 3. Finally, if all that fails, fall back to an offset from the start of the
6994/// referenced section.
6995static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
6996 std::map<uint64_t, SymbolRef> &Symbols,
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006997 const RelocationRef &Reloc, uint64_t Addr,
Tim Northover4bd286a2014-08-01 13:07:19 +00006998 StringRef &Name, uint64_t &Addend) {
6999 if (Reloc.getSymbol() != Obj->symbol_end()) {
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00007000 Expected<StringRef> NameOrErr = Reloc.getSymbol()->getName();
7001 if (!NameOrErr) {
7002 std::string Buf;
7003 raw_string_ostream OS(Buf);
7004 logAllUnhandledErrors(NameOrErr.takeError(), OS, "");
7005 OS.flush();
7006 report_fatal_error(Buf);
7007 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00007008 Name = *NameOrErr;
Tim Northover4bd286a2014-08-01 13:07:19 +00007009 Addend = Addr;
7010 return;
7011 }
7012
7013 auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
Keno Fischerc780e8e2015-05-21 21:24:32 +00007014 SectionRef RelocSection = Obj->getAnyRelocationSection(RE);
Tim Northover4bd286a2014-08-01 13:07:19 +00007015
Rafael Espindola80291272014-10-08 15:28:58 +00007016 uint64_t SectionAddr = RelocSection.getAddress();
Tim Northover4bd286a2014-08-01 13:07:19 +00007017
7018 auto Sym = Symbols.upper_bound(Addr);
7019 if (Sym == Symbols.begin()) {
7020 // The first symbol in the object is after this reference, the best we can
7021 // do is section-relative notation.
7022 RelocSection.getName(Name);
7023 Addend = Addr - SectionAddr;
7024 return;
7025 }
7026
7027 // Go back one so that SymbolAddress <= Addr.
7028 --Sym;
7029
Kevin Enderby7bd8d992016-05-02 20:28:12 +00007030 auto SectOrErr = Sym->second.getSection();
7031 if (!SectOrErr) {
7032 std::string Buf;
7033 raw_string_ostream OS(Buf);
7034 logAllUnhandledErrors(SectOrErr.takeError(), OS, "");
7035 OS.flush();
7036 report_fatal_error(Buf);
7037 }
7038 section_iterator SymSection = *SectOrErr;
Tim Northover4bd286a2014-08-01 13:07:19 +00007039 if (RelocSection == *SymSection) {
7040 // There's a valid symbol in the same section before this reference.
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00007041 Expected<StringRef> NameOrErr = Sym->second.getName();
7042 if (!NameOrErr) {
7043 std::string Buf;
7044 raw_string_ostream OS(Buf);
7045 logAllUnhandledErrors(NameOrErr.takeError(), OS, "");
7046 OS.flush();
7047 report_fatal_error(Buf);
7048 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00007049 Name = *NameOrErr;
Tim Northover4bd286a2014-08-01 13:07:19 +00007050 Addend = Addr - Sym->first;
7051 return;
7052 }
7053
7054 // There is a symbol before this reference, but it's in a different
7055 // section. Probably not helpful to mention it, so use the section name.
7056 RelocSection.getName(Name);
7057 Addend = Addr - SectionAddr;
7058}
7059
7060static void printUnwindRelocDest(const MachOObjectFile *Obj,
7061 std::map<uint64_t, SymbolRef> &Symbols,
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007062 const RelocationRef &Reloc, uint64_t Addr) {
Tim Northover4bd286a2014-08-01 13:07:19 +00007063 StringRef Name;
7064 uint64_t Addend;
7065
Rafael Espindola854038e2015-06-26 14:51:16 +00007066 if (!Reloc.getObject())
Tim Northover0b0add52014-09-09 10:45:06 +00007067 return;
7068
Tim Northover4bd286a2014-08-01 13:07:19 +00007069 findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend);
7070
7071 outs() << Name;
7072 if (Addend)
Tim Northover63a25622014-08-11 09:14:06 +00007073 outs() << " + " << format("0x%" PRIx64, Addend);
Tim Northover4bd286a2014-08-01 13:07:19 +00007074}
7075
7076static void
7077printMachOCompactUnwindSection(const MachOObjectFile *Obj,
7078 std::map<uint64_t, SymbolRef> &Symbols,
7079 const SectionRef &CompactUnwind) {
7080
7081 assert(Obj->isLittleEndian() &&
7082 "There should not be a big-endian .o with __compact_unwind");
7083
7084 bool Is64 = Obj->is64Bit();
7085 uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
7086 uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
7087
7088 StringRef Contents;
7089 CompactUnwind.getContents(Contents);
7090
7091 SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
7092
7093 // First populate the initial raw offsets, encodings and so on from the entry.
7094 for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) {
7095 CompactUnwindEntry Entry(Contents.data(), Offset, Is64);
7096 CompactUnwinds.push_back(Entry);
7097 }
7098
7099 // Next we need to look at the relocations to find out what objects are
7100 // actually being referred to.
7101 for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00007102 uint64_t RelocAddress = Reloc.getOffset();
Tim Northover4bd286a2014-08-01 13:07:19 +00007103
7104 uint32_t EntryIdx = RelocAddress / EntrySize;
7105 uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
7106 CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx];
7107
7108 if (OffsetInEntry == 0)
7109 Entry.FunctionReloc = Reloc;
7110 else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t))
7111 Entry.PersonalityReloc = Reloc;
7112 else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t))
7113 Entry.LSDAReloc = Reloc;
7114 else
7115 llvm_unreachable("Unexpected relocation in __compact_unwind section");
7116 }
7117
7118 // Finally, we're ready to print the data we've gathered.
7119 outs() << "Contents of __compact_unwind section:\n";
7120 for (auto &Entry : CompactUnwinds) {
Tim Northover06af2602014-08-08 12:08:51 +00007121 outs() << " Entry at offset "
7122 << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n";
Tim Northover4bd286a2014-08-01 13:07:19 +00007123
7124 // 1. Start of the region this entry applies to.
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007125 outs() << " start: " << format("0x%" PRIx64,
7126 Entry.FunctionAddr) << ' ';
7127 printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr);
Tim Northover4bd286a2014-08-01 13:07:19 +00007128 outs() << '\n';
7129
7130 // 2. Length of the region this entry applies to.
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007131 outs() << " length: " << format("0x%" PRIx32, Entry.Length)
7132 << '\n';
Tim Northover4bd286a2014-08-01 13:07:19 +00007133 // 3. The 32-bit compact encoding.
7134 outs() << " compact encoding: "
Tim Northoverb911bf82014-08-08 12:00:09 +00007135 << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n';
Tim Northover4bd286a2014-08-01 13:07:19 +00007136
7137 // 4. The personality function, if present.
Rafael Espindola854038e2015-06-26 14:51:16 +00007138 if (Entry.PersonalityReloc.getObject()) {
Tim Northover4bd286a2014-08-01 13:07:19 +00007139 outs() << " personality function: "
Tim Northoverb911bf82014-08-08 12:00:09 +00007140 << format("0x%" PRIx64, Entry.PersonalityAddr) << ' ';
Tim Northover4bd286a2014-08-01 13:07:19 +00007141 printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc,
7142 Entry.PersonalityAddr);
7143 outs() << '\n';
7144 }
7145
7146 // 5. This entry's language-specific data area.
Rafael Espindola854038e2015-06-26 14:51:16 +00007147 if (Entry.LSDAReloc.getObject()) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007148 outs() << " LSDA: " << format("0x%" PRIx64,
7149 Entry.LSDAAddr) << ' ';
Tim Northover4bd286a2014-08-01 13:07:19 +00007150 printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr);
7151 outs() << '\n';
7152 }
7153 }
7154}
7155
Tim Northover39c70bb2014-08-12 11:52:59 +00007156//===----------------------------------------------------------------------===//
7157// __unwind_info section dumping
7158//===----------------------------------------------------------------------===//
7159
7160static void printRegularSecondLevelUnwindPage(const char *PageStart) {
7161 const char *Pos = PageStart;
7162 uint32_t Kind = readNext<uint32_t>(Pos);
7163 (void)Kind;
7164 assert(Kind == 2 && "kind for a regular 2nd level index should be 2");
7165
7166 uint16_t EntriesStart = readNext<uint16_t>(Pos);
7167 uint16_t NumEntries = readNext<uint16_t>(Pos);
7168
7169 Pos = PageStart + EntriesStart;
7170 for (unsigned i = 0; i < NumEntries; ++i) {
7171 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
7172 uint32_t Encoding = readNext<uint32_t>(Pos);
7173
7174 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007175 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
7176 << ", "
7177 << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00007178 }
7179}
7180
7181static void printCompressedSecondLevelUnwindPage(
7182 const char *PageStart, uint32_t FunctionBase,
7183 const SmallVectorImpl<uint32_t> &CommonEncodings) {
7184 const char *Pos = PageStart;
7185 uint32_t Kind = readNext<uint32_t>(Pos);
7186 (void)Kind;
7187 assert(Kind == 3 && "kind for a compressed 2nd level index should be 3");
7188
7189 uint16_t EntriesStart = readNext<uint16_t>(Pos);
7190 uint16_t NumEntries = readNext<uint16_t>(Pos);
7191
7192 uint16_t EncodingsStart = readNext<uint16_t>(Pos);
7193 readNext<uint16_t>(Pos);
Aaron Ballman80930af2014-08-14 13:53:19 +00007194 const auto *PageEncodings = reinterpret_cast<const support::ulittle32_t *>(
7195 PageStart + EncodingsStart);
Tim Northover39c70bb2014-08-12 11:52:59 +00007196
7197 Pos = PageStart + EntriesStart;
7198 for (unsigned i = 0; i < NumEntries; ++i) {
7199 uint32_t Entry = readNext<uint32_t>(Pos);
7200 uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff);
7201 uint32_t EncodingIdx = Entry >> 24;
7202
7203 uint32_t Encoding;
7204 if (EncodingIdx < CommonEncodings.size())
7205 Encoding = CommonEncodings[EncodingIdx];
7206 else
7207 Encoding = PageEncodings[EncodingIdx - CommonEncodings.size()];
7208
7209 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007210 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
7211 << ", "
7212 << "encoding[" << EncodingIdx
7213 << "]=" << format("0x%08" PRIx32, Encoding) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00007214 }
7215}
7216
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007217static void printMachOUnwindInfoSection(const MachOObjectFile *Obj,
7218 std::map<uint64_t, SymbolRef> &Symbols,
7219 const SectionRef &UnwindInfo) {
Tim Northover39c70bb2014-08-12 11:52:59 +00007220
7221 assert(Obj->isLittleEndian() &&
7222 "There should not be a big-endian .o with __unwind_info");
7223
7224 outs() << "Contents of __unwind_info section:\n";
7225
7226 StringRef Contents;
7227 UnwindInfo.getContents(Contents);
7228 const char *Pos = Contents.data();
7229
7230 //===----------------------------------
7231 // Section header
7232 //===----------------------------------
7233
7234 uint32_t Version = readNext<uint32_t>(Pos);
7235 outs() << " Version: "
7236 << format("0x%" PRIx32, Version) << '\n';
7237 assert(Version == 1 && "only understand version 1");
7238
7239 uint32_t CommonEncodingsStart = readNext<uint32_t>(Pos);
7240 outs() << " Common encodings array section offset: "
7241 << format("0x%" PRIx32, CommonEncodingsStart) << '\n';
7242 uint32_t NumCommonEncodings = readNext<uint32_t>(Pos);
7243 outs() << " Number of common encodings in array: "
7244 << format("0x%" PRIx32, NumCommonEncodings) << '\n';
7245
7246 uint32_t PersonalitiesStart = readNext<uint32_t>(Pos);
7247 outs() << " Personality function array section offset: "
7248 << format("0x%" PRIx32, PersonalitiesStart) << '\n';
7249 uint32_t NumPersonalities = readNext<uint32_t>(Pos);
7250 outs() << " Number of personality functions in array: "
7251 << format("0x%" PRIx32, NumPersonalities) << '\n';
7252
7253 uint32_t IndicesStart = readNext<uint32_t>(Pos);
7254 outs() << " Index array section offset: "
7255 << format("0x%" PRIx32, IndicesStart) << '\n';
7256 uint32_t NumIndices = readNext<uint32_t>(Pos);
7257 outs() << " Number of indices in array: "
7258 << format("0x%" PRIx32, NumIndices) << '\n';
7259
7260 //===----------------------------------
7261 // A shared list of common encodings
7262 //===----------------------------------
7263
7264 // These occupy indices in the range [0, N] whenever an encoding is referenced
7265 // from a compressed 2nd level index table. In practice the linker only
7266 // creates ~128 of these, so that indices are available to embed encodings in
7267 // the 2nd level index.
7268
7269 SmallVector<uint32_t, 64> CommonEncodings;
7270 outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n";
7271 Pos = Contents.data() + CommonEncodingsStart;
7272 for (unsigned i = 0; i < NumCommonEncodings; ++i) {
7273 uint32_t Encoding = readNext<uint32_t>(Pos);
7274 CommonEncodings.push_back(Encoding);
7275
7276 outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding)
7277 << '\n';
7278 }
7279
Tim Northover39c70bb2014-08-12 11:52:59 +00007280 //===----------------------------------
7281 // Personality functions used in this executable
7282 //===----------------------------------
7283
7284 // There should be only a handful of these (one per source language,
7285 // roughly). Particularly since they only get 2 bits in the compact encoding.
7286
7287 outs() << " Personality functions: (count = " << NumPersonalities << ")\n";
7288 Pos = Contents.data() + PersonalitiesStart;
7289 for (unsigned i = 0; i < NumPersonalities; ++i) {
7290 uint32_t PersonalityFn = readNext<uint32_t>(Pos);
7291 outs() << " personality[" << i + 1
7292 << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n';
7293 }
7294
7295 //===----------------------------------
7296 // The level 1 index entries
7297 //===----------------------------------
7298
7299 // These specify an approximate place to start searching for the more detailed
7300 // information, sorted by PC.
7301
7302 struct IndexEntry {
7303 uint32_t FunctionOffset;
7304 uint32_t SecondLevelPageStart;
7305 uint32_t LSDAStart;
7306 };
7307
7308 SmallVector<IndexEntry, 4> IndexEntries;
7309
7310 outs() << " Top level indices: (count = " << NumIndices << ")\n";
7311 Pos = Contents.data() + IndicesStart;
7312 for (unsigned i = 0; i < NumIndices; ++i) {
7313 IndexEntry Entry;
7314
7315 Entry.FunctionOffset = readNext<uint32_t>(Pos);
7316 Entry.SecondLevelPageStart = readNext<uint32_t>(Pos);
7317 Entry.LSDAStart = readNext<uint32_t>(Pos);
7318 IndexEntries.push_back(Entry);
7319
7320 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007321 << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset)
7322 << ", "
Tim Northover39c70bb2014-08-12 11:52:59 +00007323 << "2nd level page offset="
7324 << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007325 << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00007326 }
7327
Tim Northover39c70bb2014-08-12 11:52:59 +00007328 //===----------------------------------
7329 // Next come the LSDA tables
7330 //===----------------------------------
7331
7332 // The LSDA layout is rather implicit: it's a contiguous array of entries from
7333 // the first top-level index's LSDAOffset to the last (sentinel).
7334
7335 outs() << " LSDA descriptors:\n";
7336 Pos = Contents.data() + IndexEntries[0].LSDAStart;
7337 int NumLSDAs = (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) /
7338 (2 * sizeof(uint32_t));
7339 for (int i = 0; i < NumLSDAs; ++i) {
7340 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
7341 uint32_t LSDAOffset = readNext<uint32_t>(Pos);
7342 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007343 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
7344 << ", "
7345 << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00007346 }
7347
7348 //===----------------------------------
7349 // Finally, the 2nd level indices
7350 //===----------------------------------
7351
7352 // Generally these are 4K in size, and have 2 possible forms:
7353 // + Regular stores up to 511 entries with disparate encodings
7354 // + Compressed stores up to 1021 entries if few enough compact encoding
7355 // values are used.
7356 outs() << " Second level indices:\n";
7357 for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) {
7358 // The final sentinel top-level index has no associated 2nd level page
7359 if (IndexEntries[i].SecondLevelPageStart == 0)
7360 break;
7361
7362 outs() << " Second level index[" << i << "]: "
7363 << "offset in section="
7364 << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart)
7365 << ", "
7366 << "base function offset="
7367 << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n';
7368
7369 Pos = Contents.data() + IndexEntries[i].SecondLevelPageStart;
Aaron Ballman80930af2014-08-14 13:53:19 +00007370 uint32_t Kind = *reinterpret_cast<const support::ulittle32_t *>(Pos);
Tim Northover39c70bb2014-08-12 11:52:59 +00007371 if (Kind == 2)
7372 printRegularSecondLevelUnwindPage(Pos);
7373 else if (Kind == 3)
7374 printCompressedSecondLevelUnwindPage(Pos, IndexEntries[i].FunctionOffset,
7375 CommonEncodings);
7376 else
7377 llvm_unreachable("Do not know how to print this kind of 2nd level page");
Tim Northover39c70bb2014-08-12 11:52:59 +00007378 }
7379}
7380
Tim Northover4bd286a2014-08-01 13:07:19 +00007381void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) {
7382 std::map<uint64_t, SymbolRef> Symbols;
7383 for (const SymbolRef &SymRef : Obj->symbols()) {
7384 // Discard any undefined or absolute symbols. They're not going to take part
7385 // in the convenience lookup for unwind info and just take up resources.
Kevin Enderby7bd8d992016-05-02 20:28:12 +00007386 auto SectOrErr = SymRef.getSection();
7387 if (!SectOrErr) {
7388 // TODO: Actually report errors helpfully.
7389 consumeError(SectOrErr.takeError());
7390 continue;
7391 }
7392 section_iterator Section = *SectOrErr;
Tim Northover4bd286a2014-08-01 13:07:19 +00007393 if (Section == Obj->section_end())
7394 continue;
7395
Rafael Espindoladea00162015-07-03 17:44:18 +00007396 uint64_t Addr = SymRef.getValue();
Tim Northover4bd286a2014-08-01 13:07:19 +00007397 Symbols.insert(std::make_pair(Addr, SymRef));
7398 }
7399
7400 for (const SectionRef &Section : Obj->sections()) {
7401 StringRef SectName;
7402 Section.getName(SectName);
7403 if (SectName == "__compact_unwind")
7404 printMachOCompactUnwindSection(Obj, Symbols, Section);
7405 else if (SectName == "__unwind_info")
Tim Northover39c70bb2014-08-12 11:52:59 +00007406 printMachOUnwindInfoSection(Obj, Symbols, Section);
Tim Northover4bd286a2014-08-01 13:07:19 +00007407 }
7408}
Kevin Enderbyb76d3862014-08-22 20:35:18 +00007409
7410static void PrintMachHeader(uint32_t magic, uint32_t cputype,
7411 uint32_t cpusubtype, uint32_t filetype,
7412 uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
7413 bool verbose) {
7414 outs() << "Mach header\n";
7415 outs() << " magic cputype cpusubtype caps filetype ncmds "
7416 "sizeofcmds flags\n";
7417 if (verbose) {
7418 if (magic == MachO::MH_MAGIC)
7419 outs() << " MH_MAGIC";
7420 else if (magic == MachO::MH_MAGIC_64)
7421 outs() << "MH_MAGIC_64";
7422 else
7423 outs() << format(" 0x%08" PRIx32, magic);
7424 switch (cputype) {
7425 case MachO::CPU_TYPE_I386:
7426 outs() << " I386";
7427 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7428 case MachO::CPU_SUBTYPE_I386_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_X86_64:
7437 outs() << " X86_64";
Kevin Enderby131d1772015-01-09 19:22:37 +00007438 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7439 case MachO::CPU_SUBTYPE_X86_64_ALL:
7440 outs() << " ALL";
7441 break;
7442 case MachO::CPU_SUBTYPE_X86_64_H:
7443 outs() << " Haswell";
7444 break;
7445 default:
7446 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7447 break;
7448 }
Kevin Enderbyb76d3862014-08-22 20:35:18 +00007449 break;
7450 case MachO::CPU_TYPE_ARM:
7451 outs() << " ARM";
7452 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7453 case MachO::CPU_SUBTYPE_ARM_ALL:
7454 outs() << " ALL";
7455 break;
7456 case MachO::CPU_SUBTYPE_ARM_V4T:
7457 outs() << " V4T";
7458 break;
7459 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
7460 outs() << " V5TEJ";
7461 break;
7462 case MachO::CPU_SUBTYPE_ARM_XSCALE:
7463 outs() << " XSCALE";
7464 break;
7465 case MachO::CPU_SUBTYPE_ARM_V6:
7466 outs() << " V6";
7467 break;
7468 case MachO::CPU_SUBTYPE_ARM_V6M:
7469 outs() << " V6M";
7470 break;
7471 case MachO::CPU_SUBTYPE_ARM_V7:
7472 outs() << " V7";
7473 break;
7474 case MachO::CPU_SUBTYPE_ARM_V7EM:
7475 outs() << " V7EM";
7476 break;
7477 case MachO::CPU_SUBTYPE_ARM_V7K:
7478 outs() << " V7K";
7479 break;
7480 case MachO::CPU_SUBTYPE_ARM_V7M:
7481 outs() << " V7M";
7482 break;
7483 case MachO::CPU_SUBTYPE_ARM_V7S:
7484 outs() << " V7S";
7485 break;
7486 default:
7487 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7488 break;
7489 }
7490 break;
7491 case MachO::CPU_TYPE_ARM64:
7492 outs() << " ARM64";
7493 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7494 case MachO::CPU_SUBTYPE_ARM64_ALL:
7495 outs() << " ALL";
7496 break;
7497 default:
7498 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7499 break;
7500 }
7501 break;
7502 case MachO::CPU_TYPE_POWERPC:
7503 outs() << " PPC";
7504 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7505 case MachO::CPU_SUBTYPE_POWERPC_ALL:
7506 outs() << " ALL";
7507 break;
7508 default:
7509 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7510 break;
7511 }
7512 break;
7513 case MachO::CPU_TYPE_POWERPC64:
7514 outs() << " PPC64";
7515 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7516 case MachO::CPU_SUBTYPE_POWERPC_ALL:
7517 outs() << " ALL";
7518 break;
7519 default:
7520 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7521 break;
7522 }
7523 break;
Kevin Enderby40fdbf82016-01-26 18:20:49 +00007524 default:
7525 outs() << format(" %7d", cputype);
7526 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7527 break;
Kevin Enderbyb76d3862014-08-22 20:35:18 +00007528 }
7529 if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007530 outs() << " LIB64";
Kevin Enderbyb76d3862014-08-22 20:35:18 +00007531 } else {
7532 outs() << format(" 0x%02" PRIx32,
7533 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
7534 }
7535 switch (filetype) {
7536 case MachO::MH_OBJECT:
7537 outs() << " OBJECT";
7538 break;
7539 case MachO::MH_EXECUTE:
7540 outs() << " EXECUTE";
7541 break;
7542 case MachO::MH_FVMLIB:
7543 outs() << " FVMLIB";
7544 break;
7545 case MachO::MH_CORE:
7546 outs() << " CORE";
7547 break;
7548 case MachO::MH_PRELOAD:
7549 outs() << " PRELOAD";
7550 break;
7551 case MachO::MH_DYLIB:
7552 outs() << " DYLIB";
7553 break;
7554 case MachO::MH_DYLIB_STUB:
7555 outs() << " DYLIB_STUB";
7556 break;
7557 case MachO::MH_DYLINKER:
7558 outs() << " DYLINKER";
7559 break;
7560 case MachO::MH_BUNDLE:
7561 outs() << " BUNDLE";
7562 break;
7563 case MachO::MH_DSYM:
7564 outs() << " DSYM";
7565 break;
7566 case MachO::MH_KEXT_BUNDLE:
7567 outs() << " KEXTBUNDLE";
7568 break;
7569 default:
7570 outs() << format(" %10u", filetype);
7571 break;
7572 }
7573 outs() << format(" %5u", ncmds);
7574 outs() << format(" %10u", sizeofcmds);
7575 uint32_t f = flags;
7576 if (f & MachO::MH_NOUNDEFS) {
7577 outs() << " NOUNDEFS";
7578 f &= ~MachO::MH_NOUNDEFS;
7579 }
7580 if (f & MachO::MH_INCRLINK) {
7581 outs() << " INCRLINK";
7582 f &= ~MachO::MH_INCRLINK;
7583 }
7584 if (f & MachO::MH_DYLDLINK) {
7585 outs() << " DYLDLINK";
7586 f &= ~MachO::MH_DYLDLINK;
7587 }
7588 if (f & MachO::MH_BINDATLOAD) {
7589 outs() << " BINDATLOAD";
7590 f &= ~MachO::MH_BINDATLOAD;
7591 }
7592 if (f & MachO::MH_PREBOUND) {
7593 outs() << " PREBOUND";
7594 f &= ~MachO::MH_PREBOUND;
7595 }
7596 if (f & MachO::MH_SPLIT_SEGS) {
7597 outs() << " SPLIT_SEGS";
7598 f &= ~MachO::MH_SPLIT_SEGS;
7599 }
7600 if (f & MachO::MH_LAZY_INIT) {
7601 outs() << " LAZY_INIT";
7602 f &= ~MachO::MH_LAZY_INIT;
7603 }
7604 if (f & MachO::MH_TWOLEVEL) {
7605 outs() << " TWOLEVEL";
7606 f &= ~MachO::MH_TWOLEVEL;
7607 }
7608 if (f & MachO::MH_FORCE_FLAT) {
7609 outs() << " FORCE_FLAT";
7610 f &= ~MachO::MH_FORCE_FLAT;
7611 }
7612 if (f & MachO::MH_NOMULTIDEFS) {
7613 outs() << " NOMULTIDEFS";
7614 f &= ~MachO::MH_NOMULTIDEFS;
7615 }
7616 if (f & MachO::MH_NOFIXPREBINDING) {
7617 outs() << " NOFIXPREBINDING";
7618 f &= ~MachO::MH_NOFIXPREBINDING;
7619 }
7620 if (f & MachO::MH_PREBINDABLE) {
7621 outs() << " PREBINDABLE";
7622 f &= ~MachO::MH_PREBINDABLE;
7623 }
7624 if (f & MachO::MH_ALLMODSBOUND) {
7625 outs() << " ALLMODSBOUND";
7626 f &= ~MachO::MH_ALLMODSBOUND;
7627 }
7628 if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
7629 outs() << " SUBSECTIONS_VIA_SYMBOLS";
7630 f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
7631 }
7632 if (f & MachO::MH_CANONICAL) {
7633 outs() << " CANONICAL";
7634 f &= ~MachO::MH_CANONICAL;
7635 }
7636 if (f & MachO::MH_WEAK_DEFINES) {
7637 outs() << " WEAK_DEFINES";
7638 f &= ~MachO::MH_WEAK_DEFINES;
7639 }
7640 if (f & MachO::MH_BINDS_TO_WEAK) {
7641 outs() << " BINDS_TO_WEAK";
7642 f &= ~MachO::MH_BINDS_TO_WEAK;
7643 }
7644 if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
7645 outs() << " ALLOW_STACK_EXECUTION";
7646 f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
7647 }
7648 if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
7649 outs() << " DEAD_STRIPPABLE_DYLIB";
7650 f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
7651 }
7652 if (f & MachO::MH_PIE) {
7653 outs() << " PIE";
7654 f &= ~MachO::MH_PIE;
7655 }
7656 if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
7657 outs() << " NO_REEXPORTED_DYLIBS";
7658 f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
7659 }
7660 if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
7661 outs() << " MH_HAS_TLV_DESCRIPTORS";
7662 f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
7663 }
7664 if (f & MachO::MH_NO_HEAP_EXECUTION) {
7665 outs() << " MH_NO_HEAP_EXECUTION";
7666 f &= ~MachO::MH_NO_HEAP_EXECUTION;
7667 }
7668 if (f & MachO::MH_APP_EXTENSION_SAFE) {
7669 outs() << " APP_EXTENSION_SAFE";
7670 f &= ~MachO::MH_APP_EXTENSION_SAFE;
7671 }
7672 if (f != 0 || flags == 0)
7673 outs() << format(" 0x%08" PRIx32, f);
7674 } else {
7675 outs() << format(" 0x%08" PRIx32, magic);
7676 outs() << format(" %7d", cputype);
7677 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7678 outs() << format(" 0x%02" PRIx32,
7679 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
7680 outs() << format(" %10u", filetype);
7681 outs() << format(" %5u", ncmds);
7682 outs() << format(" %10u", sizeofcmds);
7683 outs() << format(" 0x%08" PRIx32, flags);
7684 }
7685 outs() << "\n";
7686}
7687
Kevin Enderby956366c2014-08-29 22:30:52 +00007688static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
7689 StringRef SegName, uint64_t vmaddr,
7690 uint64_t vmsize, uint64_t fileoff,
7691 uint64_t filesize, uint32_t maxprot,
7692 uint32_t initprot, uint32_t nsects,
7693 uint32_t flags, uint32_t object_size,
7694 bool verbose) {
7695 uint64_t expected_cmdsize;
7696 if (cmd == MachO::LC_SEGMENT) {
7697 outs() << " cmd LC_SEGMENT\n";
7698 expected_cmdsize = nsects;
7699 expected_cmdsize *= sizeof(struct MachO::section);
7700 expected_cmdsize += sizeof(struct MachO::segment_command);
7701 } else {
7702 outs() << " cmd LC_SEGMENT_64\n";
7703 expected_cmdsize = nsects;
7704 expected_cmdsize *= sizeof(struct MachO::section_64);
7705 expected_cmdsize += sizeof(struct MachO::segment_command_64);
7706 }
7707 outs() << " cmdsize " << cmdsize;
7708 if (cmdsize != expected_cmdsize)
7709 outs() << " Inconsistent size\n";
7710 else
7711 outs() << "\n";
7712 outs() << " segname " << SegName << "\n";
7713 if (cmd == MachO::LC_SEGMENT_64) {
7714 outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n";
7715 outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n";
7716 } else {
Kevin Enderbyadb7c432014-12-16 18:58:11 +00007717 outs() << " vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n";
7718 outs() << " vmsize " << format("0x%08" PRIx64, vmsize) << "\n";
Kevin Enderby956366c2014-08-29 22:30:52 +00007719 }
7720 outs() << " fileoff " << fileoff;
7721 if (fileoff > object_size)
7722 outs() << " (past end of file)\n";
7723 else
7724 outs() << "\n";
7725 outs() << " filesize " << filesize;
7726 if (fileoff + filesize > object_size)
7727 outs() << " (past end of file)\n";
7728 else
7729 outs() << "\n";
7730 if (verbose) {
7731 if ((maxprot &
7732 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7733 MachO::VM_PROT_EXECUTE)) != 0)
7734 outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n";
7735 else {
Davide Italiano37ff06a2015-09-02 16:53:25 +00007736 outs() << " maxprot ";
7737 outs() << ((maxprot & MachO::VM_PROT_READ) ? "r" : "-");
7738 outs() << ((maxprot & MachO::VM_PROT_WRITE) ? "w" : "-");
7739 outs() << ((maxprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
Kevin Enderby956366c2014-08-29 22:30:52 +00007740 }
7741 if ((initprot &
7742 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7743 MachO::VM_PROT_EXECUTE)) != 0)
Kevin Enderby41c9c002016-10-21 18:22:35 +00007744 outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n";
Kevin Enderby956366c2014-08-29 22:30:52 +00007745 else {
Kevin Enderby41c9c002016-10-21 18:22:35 +00007746 outs() << " initprot ";
Davide Italiano37ff06a2015-09-02 16:53:25 +00007747 outs() << ((initprot & MachO::VM_PROT_READ) ? "r" : "-");
7748 outs() << ((initprot & MachO::VM_PROT_WRITE) ? "w" : "-");
7749 outs() << ((initprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
Kevin Enderby956366c2014-08-29 22:30:52 +00007750 }
7751 } else {
7752 outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n";
7753 outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n";
7754 }
7755 outs() << " nsects " << nsects << "\n";
7756 if (verbose) {
7757 outs() << " flags";
7758 if (flags == 0)
7759 outs() << " (none)\n";
7760 else {
7761 if (flags & MachO::SG_HIGHVM) {
7762 outs() << " HIGHVM";
7763 flags &= ~MachO::SG_HIGHVM;
7764 }
7765 if (flags & MachO::SG_FVMLIB) {
7766 outs() << " FVMLIB";
7767 flags &= ~MachO::SG_FVMLIB;
7768 }
7769 if (flags & MachO::SG_NORELOC) {
7770 outs() << " NORELOC";
7771 flags &= ~MachO::SG_NORELOC;
7772 }
7773 if (flags & MachO::SG_PROTECTED_VERSION_1) {
7774 outs() << " PROTECTED_VERSION_1";
7775 flags &= ~MachO::SG_PROTECTED_VERSION_1;
7776 }
7777 if (flags)
7778 outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n";
7779 else
7780 outs() << "\n";
7781 }
7782 } else {
7783 outs() << " flags " << format("0x%" PRIx32, flags) << "\n";
7784 }
7785}
7786
7787static void PrintSection(const char *sectname, const char *segname,
7788 uint64_t addr, uint64_t size, uint32_t offset,
7789 uint32_t align, uint32_t reloff, uint32_t nreloc,
7790 uint32_t flags, uint32_t reserved1, uint32_t reserved2,
7791 uint32_t cmd, const char *sg_segname,
7792 uint32_t filetype, uint32_t object_size,
7793 bool verbose) {
7794 outs() << "Section\n";
7795 outs() << " sectname " << format("%.16s\n", sectname);
7796 outs() << " segname " << format("%.16s", segname);
7797 if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0)
7798 outs() << " (does not match segment)\n";
7799 else
7800 outs() << "\n";
7801 if (cmd == MachO::LC_SEGMENT_64) {
7802 outs() << " addr " << format("0x%016" PRIx64, addr) << "\n";
7803 outs() << " size " << format("0x%016" PRIx64, size);
7804 } else {
Kevin Enderby75594b62014-12-16 21:00:25 +00007805 outs() << " addr " << format("0x%08" PRIx64, addr) << "\n";
7806 outs() << " size " << format("0x%08" PRIx64, size);
Kevin Enderby956366c2014-08-29 22:30:52 +00007807 }
7808 if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size)
7809 outs() << " (past end of file)\n";
7810 else
7811 outs() << "\n";
7812 outs() << " offset " << offset;
7813 if (offset > object_size)
7814 outs() << " (past end of file)\n";
7815 else
7816 outs() << "\n";
7817 uint32_t align_shifted = 1 << align;
7818 outs() << " align 2^" << align << " (" << align_shifted << ")\n";
7819 outs() << " reloff " << reloff;
7820 if (reloff > object_size)
7821 outs() << " (past end of file)\n";
7822 else
7823 outs() << "\n";
7824 outs() << " nreloc " << nreloc;
7825 if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size)
7826 outs() << " (past end of file)\n";
7827 else
7828 outs() << "\n";
7829 uint32_t section_type = flags & MachO::SECTION_TYPE;
7830 if (verbose) {
7831 outs() << " type";
7832 if (section_type == MachO::S_REGULAR)
7833 outs() << " S_REGULAR\n";
7834 else if (section_type == MachO::S_ZEROFILL)
7835 outs() << " S_ZEROFILL\n";
7836 else if (section_type == MachO::S_CSTRING_LITERALS)
7837 outs() << " S_CSTRING_LITERALS\n";
7838 else if (section_type == MachO::S_4BYTE_LITERALS)
7839 outs() << " S_4BYTE_LITERALS\n";
7840 else if (section_type == MachO::S_8BYTE_LITERALS)
7841 outs() << " S_8BYTE_LITERALS\n";
7842 else if (section_type == MachO::S_16BYTE_LITERALS)
7843 outs() << " S_16BYTE_LITERALS\n";
7844 else if (section_type == MachO::S_LITERAL_POINTERS)
7845 outs() << " S_LITERAL_POINTERS\n";
7846 else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS)
7847 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
7848 else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS)
7849 outs() << " S_LAZY_SYMBOL_POINTERS\n";
7850 else if (section_type == MachO::S_SYMBOL_STUBS)
7851 outs() << " S_SYMBOL_STUBS\n";
7852 else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS)
7853 outs() << " S_MOD_INIT_FUNC_POINTERS\n";
7854 else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS)
7855 outs() << " S_MOD_TERM_FUNC_POINTERS\n";
7856 else if (section_type == MachO::S_COALESCED)
7857 outs() << " S_COALESCED\n";
7858 else if (section_type == MachO::S_INTERPOSING)
7859 outs() << " S_INTERPOSING\n";
7860 else if (section_type == MachO::S_DTRACE_DOF)
7861 outs() << " S_DTRACE_DOF\n";
7862 else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS)
7863 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
7864 else if (section_type == MachO::S_THREAD_LOCAL_REGULAR)
7865 outs() << " S_THREAD_LOCAL_REGULAR\n";
7866 else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL)
7867 outs() << " S_THREAD_LOCAL_ZEROFILL\n";
7868 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES)
7869 outs() << " S_THREAD_LOCAL_VARIABLES\n";
7870 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7871 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
7872 else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS)
7873 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
7874 else
7875 outs() << format("0x%08" PRIx32, section_type) << "\n";
7876 outs() << "attributes";
7877 uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES;
7878 if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS)
7879 outs() << " PURE_INSTRUCTIONS";
7880 if (section_attributes & MachO::S_ATTR_NO_TOC)
7881 outs() << " NO_TOC";
7882 if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS)
7883 outs() << " STRIP_STATIC_SYMS";
7884 if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP)
7885 outs() << " NO_DEAD_STRIP";
7886 if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT)
7887 outs() << " LIVE_SUPPORT";
7888 if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE)
7889 outs() << " SELF_MODIFYING_CODE";
7890 if (section_attributes & MachO::S_ATTR_DEBUG)
7891 outs() << " DEBUG";
7892 if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS)
7893 outs() << " SOME_INSTRUCTIONS";
7894 if (section_attributes & MachO::S_ATTR_EXT_RELOC)
7895 outs() << " EXT_RELOC";
7896 if (section_attributes & MachO::S_ATTR_LOC_RELOC)
7897 outs() << " LOC_RELOC";
7898 if (section_attributes == 0)
7899 outs() << " (none)";
7900 outs() << "\n";
7901 } else
7902 outs() << " flags " << format("0x%08" PRIx32, flags) << "\n";
7903 outs() << " reserved1 " << reserved1;
7904 if (section_type == MachO::S_SYMBOL_STUBS ||
7905 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
7906 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
7907 section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
7908 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7909 outs() << " (index into indirect symbol table)\n";
7910 else
7911 outs() << "\n";
7912 outs() << " reserved2 " << reserved2;
7913 if (section_type == MachO::S_SYMBOL_STUBS)
7914 outs() << " (size of stubs)\n";
7915 else
7916 outs() << "\n";
7917}
7918
David Majnemer73cc6ff2014-11-13 19:48:56 +00007919static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit,
Kevin Enderby956366c2014-08-29 22:30:52 +00007920 uint32_t object_size) {
7921 outs() << " cmd LC_SYMTAB\n";
7922 outs() << " cmdsize " << st.cmdsize;
7923 if (st.cmdsize != sizeof(struct MachO::symtab_command))
7924 outs() << " Incorrect size\n";
7925 else
7926 outs() << "\n";
7927 outs() << " symoff " << st.symoff;
7928 if (st.symoff > object_size)
7929 outs() << " (past end of file)\n";
7930 else
7931 outs() << "\n";
7932 outs() << " nsyms " << st.nsyms;
7933 uint64_t big_size;
David Majnemer73cc6ff2014-11-13 19:48:56 +00007934 if (Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00007935 big_size = st.nsyms;
7936 big_size *= sizeof(struct MachO::nlist_64);
7937 big_size += st.symoff;
7938 if (big_size > object_size)
7939 outs() << " (past end of file)\n";
7940 else
7941 outs() << "\n";
7942 } else {
7943 big_size = st.nsyms;
7944 big_size *= sizeof(struct MachO::nlist);
7945 big_size += st.symoff;
7946 if (big_size > object_size)
7947 outs() << " (past end of file)\n";
7948 else
7949 outs() << "\n";
7950 }
7951 outs() << " stroff " << st.stroff;
7952 if (st.stroff > object_size)
7953 outs() << " (past end of file)\n";
7954 else
7955 outs() << "\n";
7956 outs() << " strsize " << st.strsize;
7957 big_size = st.stroff;
7958 big_size += st.strsize;
7959 if (big_size > object_size)
7960 outs() << " (past end of file)\n";
7961 else
7962 outs() << "\n";
7963}
7964
7965static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
7966 uint32_t nsyms, uint32_t object_size,
David Majnemer73cc6ff2014-11-13 19:48:56 +00007967 bool Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00007968 outs() << " cmd LC_DYSYMTAB\n";
7969 outs() << " cmdsize " << dyst.cmdsize;
7970 if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
7971 outs() << " Incorrect size\n";
7972 else
7973 outs() << "\n";
7974 outs() << " ilocalsym " << dyst.ilocalsym;
7975 if (dyst.ilocalsym > nsyms)
7976 outs() << " (greater than the number of symbols)\n";
7977 else
7978 outs() << "\n";
7979 outs() << " nlocalsym " << dyst.nlocalsym;
7980 uint64_t big_size;
7981 big_size = dyst.ilocalsym;
7982 big_size += dyst.nlocalsym;
7983 if (big_size > nsyms)
7984 outs() << " (past the end of the symbol table)\n";
7985 else
7986 outs() << "\n";
7987 outs() << " iextdefsym " << dyst.iextdefsym;
7988 if (dyst.iextdefsym > nsyms)
7989 outs() << " (greater than the number of symbols)\n";
7990 else
7991 outs() << "\n";
7992 outs() << " nextdefsym " << dyst.nextdefsym;
7993 big_size = dyst.iextdefsym;
7994 big_size += dyst.nextdefsym;
7995 if (big_size > nsyms)
7996 outs() << " (past the end of the symbol table)\n";
7997 else
7998 outs() << "\n";
7999 outs() << " iundefsym " << dyst.iundefsym;
8000 if (dyst.iundefsym > nsyms)
8001 outs() << " (greater than the number of symbols)\n";
8002 else
8003 outs() << "\n";
8004 outs() << " nundefsym " << dyst.nundefsym;
8005 big_size = dyst.iundefsym;
8006 big_size += dyst.nundefsym;
8007 if (big_size > nsyms)
8008 outs() << " (past the end of the symbol table)\n";
8009 else
8010 outs() << "\n";
8011 outs() << " tocoff " << dyst.tocoff;
8012 if (dyst.tocoff > object_size)
8013 outs() << " (past end of file)\n";
8014 else
8015 outs() << "\n";
8016 outs() << " ntoc " << dyst.ntoc;
8017 big_size = dyst.ntoc;
8018 big_size *= sizeof(struct MachO::dylib_table_of_contents);
8019 big_size += dyst.tocoff;
8020 if (big_size > object_size)
8021 outs() << " (past end of file)\n";
8022 else
8023 outs() << "\n";
8024 outs() << " modtaboff " << dyst.modtaboff;
8025 if (dyst.modtaboff > object_size)
8026 outs() << " (past end of file)\n";
8027 else
8028 outs() << "\n";
8029 outs() << " nmodtab " << dyst.nmodtab;
8030 uint64_t modtabend;
David Majnemer73cc6ff2014-11-13 19:48:56 +00008031 if (Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00008032 modtabend = dyst.nmodtab;
8033 modtabend *= sizeof(struct MachO::dylib_module_64);
8034 modtabend += dyst.modtaboff;
8035 } else {
8036 modtabend = dyst.nmodtab;
8037 modtabend *= sizeof(struct MachO::dylib_module);
8038 modtabend += dyst.modtaboff;
8039 }
8040 if (modtabend > object_size)
8041 outs() << " (past end of file)\n";
8042 else
8043 outs() << "\n";
8044 outs() << " extrefsymoff " << dyst.extrefsymoff;
8045 if (dyst.extrefsymoff > object_size)
8046 outs() << " (past end of file)\n";
8047 else
8048 outs() << "\n";
8049 outs() << " nextrefsyms " << dyst.nextrefsyms;
8050 big_size = dyst.nextrefsyms;
8051 big_size *= sizeof(struct MachO::dylib_reference);
8052 big_size += dyst.extrefsymoff;
8053 if (big_size > object_size)
8054 outs() << " (past end of file)\n";
8055 else
8056 outs() << "\n";
8057 outs() << " indirectsymoff " << dyst.indirectsymoff;
8058 if (dyst.indirectsymoff > object_size)
8059 outs() << " (past end of file)\n";
8060 else
8061 outs() << "\n";
8062 outs() << " nindirectsyms " << dyst.nindirectsyms;
8063 big_size = dyst.nindirectsyms;
8064 big_size *= sizeof(uint32_t);
8065 big_size += dyst.indirectsymoff;
8066 if (big_size > object_size)
8067 outs() << " (past end of file)\n";
8068 else
8069 outs() << "\n";
8070 outs() << " extreloff " << dyst.extreloff;
8071 if (dyst.extreloff > object_size)
8072 outs() << " (past end of file)\n";
8073 else
8074 outs() << "\n";
8075 outs() << " nextrel " << dyst.nextrel;
8076 big_size = dyst.nextrel;
8077 big_size *= sizeof(struct MachO::relocation_info);
8078 big_size += dyst.extreloff;
8079 if (big_size > object_size)
8080 outs() << " (past end of file)\n";
8081 else
8082 outs() << "\n";
8083 outs() << " locreloff " << dyst.locreloff;
8084 if (dyst.locreloff > object_size)
8085 outs() << " (past end of file)\n";
8086 else
8087 outs() << "\n";
8088 outs() << " nlocrel " << dyst.nlocrel;
8089 big_size = dyst.nlocrel;
8090 big_size *= sizeof(struct MachO::relocation_info);
8091 big_size += dyst.locreloff;
8092 if (big_size > object_size)
8093 outs() << " (past end of file)\n";
8094 else
8095 outs() << "\n";
8096}
8097
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008098static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc,
8099 uint32_t object_size) {
8100 if (dc.cmd == MachO::LC_DYLD_INFO)
8101 outs() << " cmd LC_DYLD_INFO\n";
8102 else
8103 outs() << " cmd LC_DYLD_INFO_ONLY\n";
8104 outs() << " cmdsize " << dc.cmdsize;
8105 if (dc.cmdsize != sizeof(struct MachO::dyld_info_command))
8106 outs() << " Incorrect size\n";
8107 else
8108 outs() << "\n";
8109 outs() << " rebase_off " << dc.rebase_off;
8110 if (dc.rebase_off > object_size)
8111 outs() << " (past end of file)\n";
8112 else
8113 outs() << "\n";
8114 outs() << " rebase_size " << dc.rebase_size;
8115 uint64_t big_size;
8116 big_size = dc.rebase_off;
8117 big_size += dc.rebase_size;
8118 if (big_size > object_size)
8119 outs() << " (past end of file)\n";
8120 else
8121 outs() << "\n";
8122 outs() << " bind_off " << dc.bind_off;
8123 if (dc.bind_off > object_size)
8124 outs() << " (past end of file)\n";
8125 else
8126 outs() << "\n";
8127 outs() << " bind_size " << dc.bind_size;
8128 big_size = dc.bind_off;
8129 big_size += dc.bind_size;
8130 if (big_size > object_size)
8131 outs() << " (past end of file)\n";
8132 else
8133 outs() << "\n";
8134 outs() << " weak_bind_off " << dc.weak_bind_off;
8135 if (dc.weak_bind_off > object_size)
8136 outs() << " (past end of file)\n";
8137 else
8138 outs() << "\n";
8139 outs() << " weak_bind_size " << dc.weak_bind_size;
8140 big_size = dc.weak_bind_off;
8141 big_size += dc.weak_bind_size;
8142 if (big_size > object_size)
8143 outs() << " (past end of file)\n";
8144 else
8145 outs() << "\n";
8146 outs() << " lazy_bind_off " << dc.lazy_bind_off;
8147 if (dc.lazy_bind_off > object_size)
8148 outs() << " (past end of file)\n";
8149 else
8150 outs() << "\n";
8151 outs() << " lazy_bind_size " << dc.lazy_bind_size;
8152 big_size = dc.lazy_bind_off;
8153 big_size += dc.lazy_bind_size;
8154 if (big_size > object_size)
8155 outs() << " (past end of file)\n";
8156 else
8157 outs() << "\n";
8158 outs() << " export_off " << dc.export_off;
8159 if (dc.export_off > object_size)
8160 outs() << " (past end of file)\n";
8161 else
8162 outs() << "\n";
8163 outs() << " export_size " << dc.export_size;
8164 big_size = dc.export_off;
8165 big_size += dc.export_size;
8166 if (big_size > object_size)
8167 outs() << " (past end of file)\n";
8168 else
8169 outs() << "\n";
8170}
8171
8172static void PrintDyldLoadCommand(MachO::dylinker_command dyld,
8173 const char *Ptr) {
8174 if (dyld.cmd == MachO::LC_ID_DYLINKER)
8175 outs() << " cmd LC_ID_DYLINKER\n";
8176 else if (dyld.cmd == MachO::LC_LOAD_DYLINKER)
8177 outs() << " cmd LC_LOAD_DYLINKER\n";
8178 else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT)
8179 outs() << " cmd LC_DYLD_ENVIRONMENT\n";
8180 else
8181 outs() << " cmd ?(" << dyld.cmd << ")\n";
8182 outs() << " cmdsize " << dyld.cmdsize;
8183 if (dyld.cmdsize < sizeof(struct MachO::dylinker_command))
8184 outs() << " Incorrect size\n";
8185 else
8186 outs() << "\n";
8187 if (dyld.name >= dyld.cmdsize)
8188 outs() << " name ?(bad offset " << dyld.name << ")\n";
8189 else {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008190 const char *P = (const char *)(Ptr) + dyld.name;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008191 outs() << " name " << P << " (offset " << dyld.name << ")\n";
8192 }
8193}
8194
8195static void PrintUuidLoadCommand(MachO::uuid_command uuid) {
8196 outs() << " cmd LC_UUID\n";
8197 outs() << " cmdsize " << uuid.cmdsize;
8198 if (uuid.cmdsize != sizeof(struct MachO::uuid_command))
8199 outs() << " Incorrect size\n";
8200 else
8201 outs() << "\n";
8202 outs() << " uuid ";
Davide Italianoc74277a2015-12-07 00:03:28 +00008203 for (int i = 0; i < 16; ++i) {
8204 outs() << format("%02" PRIX32, uuid.uuid[i]);
8205 if (i == 3 || i == 5 || i == 7 || i == 9)
8206 outs() << "-";
8207 }
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008208 outs() << "\n";
8209}
8210
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008211static void PrintRpathLoadCommand(MachO::rpath_command rpath, const char *Ptr) {
Jean-Daniel Dupas00cc1f52014-12-04 07:37:02 +00008212 outs() << " cmd LC_RPATH\n";
8213 outs() << " cmdsize " << rpath.cmdsize;
8214 if (rpath.cmdsize < sizeof(struct MachO::rpath_command))
8215 outs() << " Incorrect size\n";
8216 else
8217 outs() << "\n";
8218 if (rpath.path >= rpath.cmdsize)
8219 outs() << " path ?(bad offset " << rpath.path << ")\n";
8220 else {
8221 const char *P = (const char *)(Ptr) + rpath.path;
8222 outs() << " path " << P << " (offset " << rpath.path << ")\n";
8223 }
8224}
8225
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008226static void PrintVersionMinLoadCommand(MachO::version_min_command vd) {
Tim Northoverbfbfb122015-11-02 21:26:58 +00008227 StringRef LoadCmdName;
8228 switch (vd.cmd) {
8229 case MachO::LC_VERSION_MIN_MACOSX:
8230 LoadCmdName = "LC_VERSION_MIN_MACOSX";
8231 break;
8232 case MachO::LC_VERSION_MIN_IPHONEOS:
8233 LoadCmdName = "LC_VERSION_MIN_IPHONEOS";
8234 break;
8235 case MachO::LC_VERSION_MIN_TVOS:
8236 LoadCmdName = "LC_VERSION_MIN_TVOS";
8237 break;
8238 case MachO::LC_VERSION_MIN_WATCHOS:
8239 LoadCmdName = "LC_VERSION_MIN_WATCHOS";
8240 break;
8241 default:
8242 llvm_unreachable("Unknown version min load command");
8243 }
8244
8245 outs() << " cmd " << LoadCmdName << '\n';
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008246 outs() << " cmdsize " << vd.cmdsize;
8247 if (vd.cmdsize != sizeof(struct MachO::version_min_command))
8248 outs() << " Incorrect size\n";
8249 else
8250 outs() << "\n";
Davide Italiano56baef32015-08-26 12:26:11 +00008251 outs() << " version "
8252 << MachOObjectFile::getVersionMinMajor(vd, false) << "."
8253 << MachOObjectFile::getVersionMinMinor(vd, false);
8254 uint32_t Update = MachOObjectFile::getVersionMinUpdate(vd, false);
8255 if (Update != 0)
8256 outs() << "." << Update;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008257 outs() << "\n";
8258 if (vd.sdk == 0)
Kevin Enderby57538292014-12-17 01:01:30 +00008259 outs() << " sdk n/a";
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008260 else {
Davide Italiano56baef32015-08-26 12:26:11 +00008261 outs() << " sdk "
8262 << MachOObjectFile::getVersionMinMajor(vd, true) << "."
8263 << MachOObjectFile::getVersionMinMinor(vd, true);
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008264 }
Davide Italiano56baef32015-08-26 12:26:11 +00008265 Update = MachOObjectFile::getVersionMinUpdate(vd, true);
8266 if (Update != 0)
8267 outs() << "." << Update;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008268 outs() << "\n";
8269}
8270
8271static void PrintSourceVersionCommand(MachO::source_version_command sd) {
8272 outs() << " cmd LC_SOURCE_VERSION\n";
8273 outs() << " cmdsize " << sd.cmdsize;
8274 if (sd.cmdsize != sizeof(struct MachO::source_version_command))
8275 outs() << " Incorrect size\n";
8276 else
8277 outs() << "\n";
8278 uint64_t a = (sd.version >> 40) & 0xffffff;
8279 uint64_t b = (sd.version >> 30) & 0x3ff;
8280 uint64_t c = (sd.version >> 20) & 0x3ff;
8281 uint64_t d = (sd.version >> 10) & 0x3ff;
8282 uint64_t e = sd.version & 0x3ff;
8283 outs() << " version " << a << "." << b;
8284 if (e != 0)
8285 outs() << "." << c << "." << d << "." << e;
8286 else if (d != 0)
8287 outs() << "." << c << "." << d;
8288 else if (c != 0)
8289 outs() << "." << c;
8290 outs() << "\n";
8291}
8292
8293static void PrintEntryPointCommand(MachO::entry_point_command ep) {
8294 outs() << " cmd LC_MAIN\n";
8295 outs() << " cmdsize " << ep.cmdsize;
8296 if (ep.cmdsize != sizeof(struct MachO::entry_point_command))
8297 outs() << " Incorrect size\n";
8298 else
8299 outs() << "\n";
8300 outs() << " entryoff " << ep.entryoff << "\n";
8301 outs() << " stacksize " << ep.stacksize << "\n";
8302}
8303
Kevin Enderby0804f4672014-12-16 23:25:52 +00008304static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec,
8305 uint32_t object_size) {
8306 outs() << " cmd LC_ENCRYPTION_INFO\n";
8307 outs() << " cmdsize " << ec.cmdsize;
8308 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command))
8309 outs() << " Incorrect size\n";
8310 else
8311 outs() << "\n";
8312 outs() << " cryptoff " << ec.cryptoff;
8313 if (ec.cryptoff > object_size)
8314 outs() << " (past end of file)\n";
8315 else
8316 outs() << "\n";
8317 outs() << " cryptsize " << ec.cryptsize;
8318 if (ec.cryptsize > object_size)
8319 outs() << " (past end of file)\n";
8320 else
8321 outs() << "\n";
8322 outs() << " cryptid " << ec.cryptid << "\n";
8323}
8324
Kevin Enderby57538292014-12-17 01:01:30 +00008325static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec,
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008326 uint32_t object_size) {
Kevin Enderby57538292014-12-17 01:01:30 +00008327 outs() << " cmd LC_ENCRYPTION_INFO_64\n";
8328 outs() << " cmdsize " << ec.cmdsize;
8329 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64))
8330 outs() << " Incorrect size\n";
8331 else
8332 outs() << "\n";
8333 outs() << " cryptoff " << ec.cryptoff;
8334 if (ec.cryptoff > object_size)
8335 outs() << " (past end of file)\n";
8336 else
8337 outs() << "\n";
8338 outs() << " cryptsize " << ec.cryptsize;
8339 if (ec.cryptsize > object_size)
8340 outs() << " (past end of file)\n";
8341 else
8342 outs() << "\n";
8343 outs() << " cryptid " << ec.cryptid << "\n";
8344 outs() << " pad " << ec.pad << "\n";
8345}
8346
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00008347static void PrintLinkerOptionCommand(MachO::linker_option_command lo,
8348 const char *Ptr) {
8349 outs() << " cmd LC_LINKER_OPTION\n";
8350 outs() << " cmdsize " << lo.cmdsize;
8351 if (lo.cmdsize < sizeof(struct MachO::linker_option_command))
8352 outs() << " Incorrect size\n";
8353 else
8354 outs() << "\n";
8355 outs() << " count " << lo.count << "\n";
8356 const char *string = Ptr + sizeof(struct MachO::linker_option_command);
8357 uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command);
8358 uint32_t i = 0;
8359 while (left > 0) {
8360 while (*string == '\0' && left > 0) {
8361 string++;
8362 left--;
8363 }
8364 if (left > 0) {
8365 i++;
8366 outs() << " string #" << i << " " << format("%.*s\n", left, string);
David Majnemerd4449ed2014-12-20 08:24:43 +00008367 uint32_t NullPos = StringRef(string, left).find('\0');
8368 uint32_t len = std::min(NullPos, left) + 1;
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00008369 string += len;
8370 left -= len;
8371 }
8372 }
8373 if (lo.count != i)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008374 outs() << " count " << lo.count << " does not match number of strings "
8375 << i << "\n";
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00008376}
8377
Kevin Enderbyb4b79312014-12-18 19:24:35 +00008378static void PrintSubFrameworkCommand(MachO::sub_framework_command sub,
8379 const char *Ptr) {
8380 outs() << " cmd LC_SUB_FRAMEWORK\n";
8381 outs() << " cmdsize " << sub.cmdsize;
8382 if (sub.cmdsize < sizeof(struct MachO::sub_framework_command))
8383 outs() << " Incorrect size\n";
8384 else
8385 outs() << "\n";
8386 if (sub.umbrella < sub.cmdsize) {
8387 const char *P = Ptr + sub.umbrella;
8388 outs() << " umbrella " << P << " (offset " << sub.umbrella << ")\n";
8389 } else {
8390 outs() << " umbrella ?(bad offset " << sub.umbrella << ")\n";
8391 }
8392}
8393
Kevin Enderbya2bd8d92014-12-18 23:13:26 +00008394static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub,
8395 const char *Ptr) {
8396 outs() << " cmd LC_SUB_UMBRELLA\n";
8397 outs() << " cmdsize " << sub.cmdsize;
8398 if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command))
8399 outs() << " Incorrect size\n";
8400 else
8401 outs() << "\n";
8402 if (sub.sub_umbrella < sub.cmdsize) {
8403 const char *P = Ptr + sub.sub_umbrella;
8404 outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n";
8405 } else {
8406 outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n";
8407 }
8408}
8409
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00008410static void PrintSubLibraryCommand(MachO::sub_library_command sub,
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008411 const char *Ptr) {
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00008412 outs() << " cmd LC_SUB_LIBRARY\n";
8413 outs() << " cmdsize " << sub.cmdsize;
8414 if (sub.cmdsize < sizeof(struct MachO::sub_library_command))
8415 outs() << " Incorrect size\n";
8416 else
8417 outs() << "\n";
8418 if (sub.sub_library < sub.cmdsize) {
8419 const char *P = Ptr + sub.sub_library;
8420 outs() << " sub_library " << P << " (offset " << sub.sub_library << ")\n";
8421 } else {
8422 outs() << " sub_library ?(bad offset " << sub.sub_library << ")\n";
8423 }
8424}
8425
Kevin Enderby186eac32014-12-19 21:06:24 +00008426static void PrintSubClientCommand(MachO::sub_client_command sub,
8427 const char *Ptr) {
8428 outs() << " cmd LC_SUB_CLIENT\n";
8429 outs() << " cmdsize " << sub.cmdsize;
8430 if (sub.cmdsize < sizeof(struct MachO::sub_client_command))
8431 outs() << " Incorrect size\n";
8432 else
8433 outs() << "\n";
8434 if (sub.client < sub.cmdsize) {
8435 const char *P = Ptr + sub.client;
8436 outs() << " client " << P << " (offset " << sub.client << ")\n";
8437 } else {
8438 outs() << " client ?(bad offset " << sub.client << ")\n";
8439 }
8440}
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00008441
Kevin Enderby52e4ce42014-12-19 22:25:22 +00008442static void PrintRoutinesCommand(MachO::routines_command r) {
8443 outs() << " cmd LC_ROUTINES\n";
8444 outs() << " cmdsize " << r.cmdsize;
8445 if (r.cmdsize != sizeof(struct MachO::routines_command))
8446 outs() << " Incorrect size\n";
8447 else
8448 outs() << "\n";
8449 outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n";
8450 outs() << " init_module " << r.init_module << "\n";
8451 outs() << " reserved1 " << r.reserved1 << "\n";
8452 outs() << " reserved2 " << r.reserved2 << "\n";
8453 outs() << " reserved3 " << r.reserved3 << "\n";
8454 outs() << " reserved4 " << r.reserved4 << "\n";
8455 outs() << " reserved5 " << r.reserved5 << "\n";
8456 outs() << " reserved6 " << r.reserved6 << "\n";
8457}
8458
8459static void PrintRoutinesCommand64(MachO::routines_command_64 r) {
8460 outs() << " cmd LC_ROUTINES_64\n";
8461 outs() << " cmdsize " << r.cmdsize;
8462 if (r.cmdsize != sizeof(struct MachO::routines_command_64))
8463 outs() << " Incorrect size\n";
8464 else
8465 outs() << "\n";
8466 outs() << " init_address " << format("0x%016" PRIx64, r.init_address) << "\n";
8467 outs() << " init_module " << r.init_module << "\n";
8468 outs() << " reserved1 " << r.reserved1 << "\n";
8469 outs() << " reserved2 " << r.reserved2 << "\n";
8470 outs() << " reserved3 " << r.reserved3 << "\n";
8471 outs() << " reserved4 " << r.reserved4 << "\n";
8472 outs() << " reserved5 " << r.reserved5 << "\n";
8473 outs() << " reserved6 " << r.reserved6 << "\n";
8474}
8475
Kevin Enderby48ef5342014-12-23 22:56:39 +00008476static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) {
8477 outs() << " rax " << format("0x%016" PRIx64, cpu64.rax);
8478 outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx);
8479 outs() << " rcx " << format("0x%016" PRIx64, cpu64.rcx) << "\n";
8480 outs() << " rdx " << format("0x%016" PRIx64, cpu64.rdx);
8481 outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi);
8482 outs() << " rsi " << format("0x%016" PRIx64, cpu64.rsi) << "\n";
8483 outs() << " rbp " << format("0x%016" PRIx64, cpu64.rbp);
8484 outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp);
8485 outs() << " r8 " << format("0x%016" PRIx64, cpu64.r8) << "\n";
8486 outs() << " r9 " << format("0x%016" PRIx64, cpu64.r9);
8487 outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10);
8488 outs() << " r11 " << format("0x%016" PRIx64, cpu64.r11) << "\n";
8489 outs() << " r12 " << format("0x%016" PRIx64, cpu64.r12);
8490 outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13);
8491 outs() << " r14 " << format("0x%016" PRIx64, cpu64.r14) << "\n";
8492 outs() << " r15 " << format("0x%016" PRIx64, cpu64.r15);
8493 outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n";
8494 outs() << "rflags " << format("0x%016" PRIx64, cpu64.rflags);
8495 outs() << " cs " << format("0x%016" PRIx64, cpu64.cs);
8496 outs() << " fs " << format("0x%016" PRIx64, cpu64.fs) << "\n";
8497 outs() << " gs " << format("0x%016" PRIx64, cpu64.gs) << "\n";
8498}
8499
Kevin Enderby227df342014-12-23 23:43:59 +00008500static void Print_mmst_reg(MachO::mmst_reg_t &r) {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008501 uint32_t f;
8502 outs() << "\t mmst_reg ";
8503 for (f = 0; f < 10; f++)
8504 outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " ";
8505 outs() << "\n";
8506 outs() << "\t mmst_rsrv ";
8507 for (f = 0; f < 6; f++)
8508 outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " ";
8509 outs() << "\n";
8510}
8511
Kevin Enderbyaefb0032014-12-24 00:16:51 +00008512static void Print_xmm_reg(MachO::xmm_reg_t &r) {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008513 uint32_t f;
8514 outs() << "\t xmm_reg ";
8515 for (f = 0; f < 16; f++)
8516 outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " ";
8517 outs() << "\n";
8518}
8519
8520static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) {
8521 outs() << "\t fpu_reserved[0] " << fpu.fpu_reserved[0];
8522 outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n";
8523 outs() << "\t control: invalid " << fpu.fpu_fcw.invalid;
8524 outs() << " denorm " << fpu.fpu_fcw.denorm;
8525 outs() << " zdiv " << fpu.fpu_fcw.zdiv;
8526 outs() << " ovrfl " << fpu.fpu_fcw.ovrfl;
8527 outs() << " undfl " << fpu.fpu_fcw.undfl;
8528 outs() << " precis " << fpu.fpu_fcw.precis << "\n";
8529 outs() << "\t\t pc ";
8530 if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B)
8531 outs() << "FP_PREC_24B ";
8532 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B)
8533 outs() << "FP_PREC_53B ";
8534 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B)
8535 outs() << "FP_PREC_64B ";
8536 else
8537 outs() << fpu.fpu_fcw.pc << " ";
8538 outs() << "rc ";
8539 if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR)
8540 outs() << "FP_RND_NEAR ";
8541 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN)
8542 outs() << "FP_RND_DOWN ";
8543 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP)
8544 outs() << "FP_RND_UP ";
8545 else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008546 outs() << "FP_CHOP ";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008547 outs() << "\n";
8548 outs() << "\t status: invalid " << fpu.fpu_fsw.invalid;
8549 outs() << " denorm " << fpu.fpu_fsw.denorm;
8550 outs() << " zdiv " << fpu.fpu_fsw.zdiv;
8551 outs() << " ovrfl " << fpu.fpu_fsw.ovrfl;
8552 outs() << " undfl " << fpu.fpu_fsw.undfl;
8553 outs() << " precis " << fpu.fpu_fsw.precis;
8554 outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n";
8555 outs() << "\t errsumm " << fpu.fpu_fsw.errsumm;
8556 outs() << " c0 " << fpu.fpu_fsw.c0;
8557 outs() << " c1 " << fpu.fpu_fsw.c1;
8558 outs() << " c2 " << fpu.fpu_fsw.c2;
8559 outs() << " tos " << fpu.fpu_fsw.tos;
8560 outs() << " c3 " << fpu.fpu_fsw.c3;
8561 outs() << " busy " << fpu.fpu_fsw.busy << "\n";
8562 outs() << "\t fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw);
8563 outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1);
8564 outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop);
8565 outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n";
8566 outs() << "\t fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs);
8567 outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2);
8568 outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp);
8569 outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n";
8570 outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3);
8571 outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr);
8572 outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask);
8573 outs() << "\n";
8574 outs() << "\t fpu_stmm0:\n";
8575 Print_mmst_reg(fpu.fpu_stmm0);
8576 outs() << "\t fpu_stmm1:\n";
8577 Print_mmst_reg(fpu.fpu_stmm1);
8578 outs() << "\t fpu_stmm2:\n";
8579 Print_mmst_reg(fpu.fpu_stmm2);
8580 outs() << "\t fpu_stmm3:\n";
8581 Print_mmst_reg(fpu.fpu_stmm3);
8582 outs() << "\t fpu_stmm4:\n";
8583 Print_mmst_reg(fpu.fpu_stmm4);
8584 outs() << "\t fpu_stmm5:\n";
8585 Print_mmst_reg(fpu.fpu_stmm5);
8586 outs() << "\t fpu_stmm6:\n";
8587 Print_mmst_reg(fpu.fpu_stmm6);
8588 outs() << "\t fpu_stmm7:\n";
8589 Print_mmst_reg(fpu.fpu_stmm7);
8590 outs() << "\t fpu_xmm0:\n";
8591 Print_xmm_reg(fpu.fpu_xmm0);
8592 outs() << "\t fpu_xmm1:\n";
8593 Print_xmm_reg(fpu.fpu_xmm1);
8594 outs() << "\t fpu_xmm2:\n";
8595 Print_xmm_reg(fpu.fpu_xmm2);
8596 outs() << "\t fpu_xmm3:\n";
8597 Print_xmm_reg(fpu.fpu_xmm3);
8598 outs() << "\t fpu_xmm4:\n";
8599 Print_xmm_reg(fpu.fpu_xmm4);
8600 outs() << "\t fpu_xmm5:\n";
8601 Print_xmm_reg(fpu.fpu_xmm5);
8602 outs() << "\t fpu_xmm6:\n";
8603 Print_xmm_reg(fpu.fpu_xmm6);
8604 outs() << "\t fpu_xmm7:\n";
8605 Print_xmm_reg(fpu.fpu_xmm7);
8606 outs() << "\t fpu_xmm8:\n";
8607 Print_xmm_reg(fpu.fpu_xmm8);
8608 outs() << "\t fpu_xmm9:\n";
8609 Print_xmm_reg(fpu.fpu_xmm9);
8610 outs() << "\t fpu_xmm10:\n";
8611 Print_xmm_reg(fpu.fpu_xmm10);
8612 outs() << "\t fpu_xmm11:\n";
8613 Print_xmm_reg(fpu.fpu_xmm11);
8614 outs() << "\t fpu_xmm12:\n";
8615 Print_xmm_reg(fpu.fpu_xmm12);
8616 outs() << "\t fpu_xmm13:\n";
8617 Print_xmm_reg(fpu.fpu_xmm13);
8618 outs() << "\t fpu_xmm14:\n";
8619 Print_xmm_reg(fpu.fpu_xmm14);
8620 outs() << "\t fpu_xmm15:\n";
8621 Print_xmm_reg(fpu.fpu_xmm15);
8622 outs() << "\t fpu_rsrv4:\n";
8623 for (uint32_t f = 0; f < 6; f++) {
8624 outs() << "\t ";
8625 for (uint32_t g = 0; g < 16; g++)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008626 outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f * g]) << " ";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008627 outs() << "\n";
8628 }
8629 outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1);
8630 outs() << "\n";
8631}
8632
8633static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) {
8634 outs() << "\t trapno " << format("0x%08" PRIx32, exc64.trapno);
8635 outs() << " err " << format("0x%08" PRIx32, exc64.err);
8636 outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n";
8637}
8638
Kevin Enderby41c9c002016-10-21 18:22:35 +00008639static void Print_arm_thread_state32_t(MachO::arm_thread_state32_t &cpu32) {
8640 outs() << "\t r0 " << format("0x%08" PRIx32, cpu32.r[0]);
8641 outs() << " r1 " << format("0x%08" PRIx32, cpu32.r[1]);
8642 outs() << " r2 " << format("0x%08" PRIx32, cpu32.r[2]);
8643 outs() << " r3 " << format("0x%08" PRIx32, cpu32.r[3]) << "\n";
8644 outs() << "\t r4 " << format("0x%08" PRIx32, cpu32.r[4]);
8645 outs() << " r5 " << format("0x%08" PRIx32, cpu32.r[5]);
8646 outs() << " r6 " << format("0x%08" PRIx32, cpu32.r[6]);
8647 outs() << " r7 " << format("0x%08" PRIx32, cpu32.r[7]) << "\n";
8648 outs() << "\t r8 " << format("0x%08" PRIx32, cpu32.r[8]);
8649 outs() << " r9 " << format("0x%08" PRIx32, cpu32.r[9]);
8650 outs() << " r10 " << format("0x%08" PRIx32, cpu32.r[10]);
8651 outs() << " r11 " << format("0x%08" PRIx32, cpu32.r[11]) << "\n";
8652 outs() << "\t r12 " << format("0x%08" PRIx32, cpu32.r[12]);
8653 outs() << " sp " << format("0x%08" PRIx32, cpu32.sp);
8654 outs() << " lr " << format("0x%08" PRIx32, cpu32.lr);
8655 outs() << " pc " << format("0x%08" PRIx32, cpu32.pc) << "\n";
8656 outs() << "\t cpsr " << format("0x%08" PRIx32, cpu32.cpsr) << "\n";
8657}
8658
Kevin Enderby48ef5342014-12-23 22:56:39 +00008659static void PrintThreadCommand(MachO::thread_command t, const char *Ptr,
8660 bool isLittleEndian, uint32_t cputype) {
8661 if (t.cmd == MachO::LC_THREAD)
8662 outs() << " cmd LC_THREAD\n";
8663 else if (t.cmd == MachO::LC_UNIXTHREAD)
8664 outs() << " cmd LC_UNIXTHREAD\n";
8665 else
8666 outs() << " cmd " << t.cmd << " (unknown)\n";
8667 outs() << " cmdsize " << t.cmdsize;
8668 if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t))
8669 outs() << " Incorrect size\n";
8670 else
8671 outs() << "\n";
8672
8673 const char *begin = Ptr + sizeof(struct MachO::thread_command);
8674 const char *end = Ptr + t.cmdsize;
8675 uint32_t flavor, count, left;
8676 if (cputype == MachO::CPU_TYPE_X86_64) {
8677 while (begin < end) {
8678 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8679 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8680 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008681 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008682 flavor = 0;
8683 begin = end;
8684 }
8685 if (isLittleEndian != sys::IsLittleEndianHost)
8686 sys::swapByteOrder(flavor);
8687 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8688 memcpy((char *)&count, begin, sizeof(uint32_t));
8689 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008690 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008691 count = 0;
8692 begin = end;
8693 }
8694 if (isLittleEndian != sys::IsLittleEndianHost)
8695 sys::swapByteOrder(count);
8696 if (flavor == MachO::x86_THREAD_STATE64) {
8697 outs() << " flavor x86_THREAD_STATE64\n";
8698 if (count == MachO::x86_THREAD_STATE64_COUNT)
8699 outs() << " count x86_THREAD_STATE64_COUNT\n";
8700 else
8701 outs() << " count " << count
8702 << " (not x86_THREAD_STATE64_COUNT)\n";
8703 MachO::x86_thread_state64_t cpu64;
8704 left = end - begin;
8705 if (left >= sizeof(MachO::x86_thread_state64_t)) {
8706 memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t));
8707 begin += sizeof(MachO::x86_thread_state64_t);
8708 } else {
8709 memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t));
8710 memcpy(&cpu64, begin, left);
8711 begin += left;
8712 }
8713 if (isLittleEndian != sys::IsLittleEndianHost)
8714 swapStruct(cpu64);
8715 Print_x86_thread_state64_t(cpu64);
8716 } else if (flavor == MachO::x86_THREAD_STATE) {
8717 outs() << " flavor x86_THREAD_STATE\n";
8718 if (count == MachO::x86_THREAD_STATE_COUNT)
8719 outs() << " count x86_THREAD_STATE_COUNT\n";
8720 else
8721 outs() << " count " << count
8722 << " (not x86_THREAD_STATE_COUNT)\n";
8723 struct MachO::x86_thread_state_t ts;
8724 left = end - begin;
8725 if (left >= sizeof(MachO::x86_thread_state_t)) {
8726 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
8727 begin += sizeof(MachO::x86_thread_state_t);
8728 } else {
8729 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
8730 memcpy(&ts, begin, left);
8731 begin += left;
8732 }
8733 if (isLittleEndian != sys::IsLittleEndianHost)
8734 swapStruct(ts);
8735 if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) {
8736 outs() << "\t tsh.flavor x86_THREAD_STATE64 ";
8737 if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT)
8738 outs() << "tsh.count x86_THREAD_STATE64_COUNT\n";
8739 else
8740 outs() << "tsh.count " << ts.tsh.count
8741 << " (not x86_THREAD_STATE64_COUNT\n";
8742 Print_x86_thread_state64_t(ts.uts.ts64);
8743 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008744 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
8745 << ts.tsh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008746 }
8747 } else if (flavor == MachO::x86_FLOAT_STATE) {
8748 outs() << " flavor x86_FLOAT_STATE\n";
8749 if (count == MachO::x86_FLOAT_STATE_COUNT)
8750 outs() << " count x86_FLOAT_STATE_COUNT\n";
8751 else
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008752 outs() << " count " << count << " (not x86_FLOAT_STATE_COUNT)\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008753 struct MachO::x86_float_state_t fs;
8754 left = end - begin;
8755 if (left >= sizeof(MachO::x86_float_state_t)) {
8756 memcpy(&fs, begin, sizeof(MachO::x86_float_state_t));
8757 begin += sizeof(MachO::x86_float_state_t);
8758 } else {
8759 memset(&fs, '\0', sizeof(MachO::x86_float_state_t));
8760 memcpy(&fs, begin, left);
8761 begin += left;
8762 }
8763 if (isLittleEndian != sys::IsLittleEndianHost)
8764 swapStruct(fs);
8765 if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) {
8766 outs() << "\t fsh.flavor x86_FLOAT_STATE64 ";
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008767 if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT)
Kevin Enderby48ef5342014-12-23 22:56:39 +00008768 outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n";
8769 else
8770 outs() << "fsh.count " << fs.fsh.count
8771 << " (not x86_FLOAT_STATE64_COUNT\n";
8772 Print_x86_float_state_t(fs.ufs.fs64);
8773 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008774 outs() << "\t fsh.flavor " << fs.fsh.flavor << " fsh.count "
8775 << fs.fsh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008776 }
8777 } else if (flavor == MachO::x86_EXCEPTION_STATE) {
8778 outs() << " flavor x86_EXCEPTION_STATE\n";
8779 if (count == MachO::x86_EXCEPTION_STATE_COUNT)
8780 outs() << " count x86_EXCEPTION_STATE_COUNT\n";
8781 else
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008782 outs() << " count " << count
Kevin Enderby48ef5342014-12-23 22:56:39 +00008783 << " (not x86_EXCEPTION_STATE_COUNT)\n";
8784 struct MachO::x86_exception_state_t es;
8785 left = end - begin;
8786 if (left >= sizeof(MachO::x86_exception_state_t)) {
8787 memcpy(&es, begin, sizeof(MachO::x86_exception_state_t));
8788 begin += sizeof(MachO::x86_exception_state_t);
8789 } else {
8790 memset(&es, '\0', sizeof(MachO::x86_exception_state_t));
8791 memcpy(&es, begin, left);
8792 begin += left;
8793 }
8794 if (isLittleEndian != sys::IsLittleEndianHost)
8795 swapStruct(es);
8796 if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) {
8797 outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n";
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008798 if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT)
Kevin Enderby48ef5342014-12-23 22:56:39 +00008799 outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n";
8800 else
8801 outs() << "\t esh.count " << es.esh.count
8802 << " (not x86_EXCEPTION_STATE64_COUNT\n";
8803 Print_x86_exception_state_t(es.ues.es64);
8804 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008805 outs() << "\t esh.flavor " << es.esh.flavor << " esh.count "
8806 << es.esh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008807 }
8808 } else {
8809 outs() << " flavor " << flavor << " (unknown)\n";
8810 outs() << " count " << count << "\n";
8811 outs() << " state (unknown)\n";
8812 begin += count * sizeof(uint32_t);
8813 }
8814 }
Kevin Enderby41c9c002016-10-21 18:22:35 +00008815 } else if (cputype == MachO::CPU_TYPE_ARM) {
8816 while (begin < end) {
8817 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8818 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8819 begin += sizeof(uint32_t);
8820 } else {
8821 flavor = 0;
8822 begin = end;
8823 }
8824 if (isLittleEndian != sys::IsLittleEndianHost)
8825 sys::swapByteOrder(flavor);
8826 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8827 memcpy((char *)&count, begin, sizeof(uint32_t));
8828 begin += sizeof(uint32_t);
8829 } else {
8830 count = 0;
8831 begin = end;
8832 }
8833 if (isLittleEndian != sys::IsLittleEndianHost)
8834 sys::swapByteOrder(count);
8835 if (flavor == MachO::ARM_THREAD_STATE) {
8836 outs() << " flavor ARM_THREAD_STATE\n";
8837 if (count == MachO::ARM_THREAD_STATE_COUNT)
8838 outs() << " count ARM_THREAD_STATE_COUNT\n";
8839 else
8840 outs() << " count " << count
8841 << " (not ARM_THREAD_STATE_COUNT)\n";
8842 MachO::arm_thread_state32_t cpu32;
8843 left = end - begin;
8844 if (left >= sizeof(MachO::arm_thread_state32_t)) {
8845 memcpy(&cpu32, begin, sizeof(MachO::arm_thread_state32_t));
8846 begin += sizeof(MachO::arm_thread_state32_t);
8847 } else {
8848 memset(&cpu32, '\0', sizeof(MachO::arm_thread_state32_t));
8849 memcpy(&cpu32, begin, left);
8850 begin += left;
8851 }
8852 if (isLittleEndian != sys::IsLittleEndianHost)
8853 swapStruct(cpu32);
8854 Print_arm_thread_state32_t(cpu32);
8855 } else {
8856 outs() << " flavor " << flavor << " (unknown)\n";
8857 outs() << " count " << count << "\n";
8858 outs() << " state (unknown)\n";
8859 begin += count * sizeof(uint32_t);
8860 }
8861 }
Kevin Enderby48ef5342014-12-23 22:56:39 +00008862 } else {
8863 while (begin < end) {
8864 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8865 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8866 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008867 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008868 flavor = 0;
8869 begin = end;
8870 }
8871 if (isLittleEndian != sys::IsLittleEndianHost)
8872 sys::swapByteOrder(flavor);
8873 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8874 memcpy((char *)&count, begin, sizeof(uint32_t));
8875 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008876 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008877 count = 0;
8878 begin = end;
8879 }
8880 if (isLittleEndian != sys::IsLittleEndianHost)
8881 sys::swapByteOrder(count);
8882 outs() << " flavor " << flavor << "\n";
8883 outs() << " count " << count << "\n";
8884 outs() << " state (Unknown cputype/cpusubtype)\n";
8885 begin += count * sizeof(uint32_t);
8886 }
8887 }
8888}
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008889
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008890static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) {
8891 if (dl.cmd == MachO::LC_ID_DYLIB)
8892 outs() << " cmd LC_ID_DYLIB\n";
8893 else if (dl.cmd == MachO::LC_LOAD_DYLIB)
8894 outs() << " cmd LC_LOAD_DYLIB\n";
8895 else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB)
8896 outs() << " cmd LC_LOAD_WEAK_DYLIB\n";
8897 else if (dl.cmd == MachO::LC_REEXPORT_DYLIB)
8898 outs() << " cmd LC_REEXPORT_DYLIB\n";
8899 else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB)
8900 outs() << " cmd LC_LAZY_LOAD_DYLIB\n";
8901 else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
8902 outs() << " cmd LC_LOAD_UPWARD_DYLIB\n";
8903 else
8904 outs() << " cmd " << dl.cmd << " (unknown)\n";
8905 outs() << " cmdsize " << dl.cmdsize;
8906 if (dl.cmdsize < sizeof(struct MachO::dylib_command))
8907 outs() << " Incorrect size\n";
8908 else
8909 outs() << "\n";
8910 if (dl.dylib.name < dl.cmdsize) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008911 const char *P = (const char *)(Ptr) + dl.dylib.name;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008912 outs() << " name " << P << " (offset " << dl.dylib.name << ")\n";
8913 } else {
8914 outs() << " name ?(bad offset " << dl.dylib.name << ")\n";
8915 }
8916 outs() << " time stamp " << dl.dylib.timestamp << " ";
8917 time_t t = dl.dylib.timestamp;
8918 outs() << ctime(&t);
8919 outs() << " current version ";
8920 if (dl.dylib.current_version == 0xffffffff)
8921 outs() << "n/a\n";
8922 else
8923 outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "."
8924 << ((dl.dylib.current_version >> 8) & 0xff) << "."
8925 << (dl.dylib.current_version & 0xff) << "\n";
8926 outs() << "compatibility version ";
8927 if (dl.dylib.compatibility_version == 0xffffffff)
8928 outs() << "n/a\n";
8929 else
8930 outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
8931 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
8932 << (dl.dylib.compatibility_version & 0xff) << "\n";
8933}
8934
8935static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
8936 uint32_t object_size) {
8937 if (ld.cmd == MachO::LC_CODE_SIGNATURE)
Kevin Enderby1be37a32016-04-28 21:07:20 +00008938 outs() << " cmd LC_CODE_SIGNATURE\n";
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008939 else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO)
8940 outs() << " cmd LC_SEGMENT_SPLIT_INFO\n";
8941 else if (ld.cmd == MachO::LC_FUNCTION_STARTS)
8942 outs() << " cmd LC_FUNCTION_STARTS\n";
8943 else if (ld.cmd == MachO::LC_DATA_IN_CODE)
8944 outs() << " cmd LC_DATA_IN_CODE\n";
8945 else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS)
8946 outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n";
8947 else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT)
8948 outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n";
8949 else
8950 outs() << " cmd " << ld.cmd << " (?)\n";
8951 outs() << " cmdsize " << ld.cmdsize;
8952 if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command))
8953 outs() << " Incorrect size\n";
8954 else
8955 outs() << "\n";
8956 outs() << " dataoff " << ld.dataoff;
8957 if (ld.dataoff > object_size)
8958 outs() << " (past end of file)\n";
8959 else
8960 outs() << "\n";
8961 outs() << " datasize " << ld.datasize;
8962 uint64_t big_size = ld.dataoff;
8963 big_size += ld.datasize;
8964 if (big_size > object_size)
8965 outs() << " (past end of file)\n";
8966 else
8967 outs() << "\n";
8968}
8969
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00008970static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t filetype,
8971 uint32_t cputype, bool verbose) {
Kevin Enderby956366c2014-08-29 22:30:52 +00008972 StringRef Buf = Obj->getData();
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00008973 unsigned Index = 0;
8974 for (const auto &Command : Obj->load_commands()) {
8975 outs() << "Load command " << Index++ << "\n";
Kevin Enderby956366c2014-08-29 22:30:52 +00008976 if (Command.C.cmd == MachO::LC_SEGMENT) {
8977 MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
8978 const char *sg_segname = SLC.segname;
8979 PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr,
8980 SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot,
8981 SLC.initprot, SLC.nsects, SLC.flags, Buf.size(),
8982 verbose);
8983 for (unsigned j = 0; j < SLC.nsects; j++) {
Kevin Enderbyc9713382014-12-16 01:14:45 +00008984 MachO::section S = Obj->getSection(Command, j);
Kevin Enderby956366c2014-08-29 22:30:52 +00008985 PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align,
8986 S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2,
8987 SLC.cmd, sg_segname, filetype, Buf.size(), verbose);
8988 }
8989 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
8990 MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command);
8991 const char *sg_segname = SLC_64.segname;
8992 PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname,
8993 SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff,
8994 SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot,
8995 SLC_64.nsects, SLC_64.flags, Buf.size(), verbose);
8996 for (unsigned j = 0; j < SLC_64.nsects; j++) {
8997 MachO::section_64 S_64 = Obj->getSection64(Command, j);
8998 PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size,
8999 S_64.offset, S_64.align, S_64.reloff, S_64.nreloc,
9000 S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd,
9001 sg_segname, filetype, Buf.size(), verbose);
9002 }
9003 } else if (Command.C.cmd == MachO::LC_SYMTAB) {
9004 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
David Majnemer73cc6ff2014-11-13 19:48:56 +00009005 PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size());
Kevin Enderby956366c2014-08-29 22:30:52 +00009006 } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
9007 MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
9008 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
David Majnemer73cc6ff2014-11-13 19:48:56 +00009009 PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(),
9010 Obj->is64Bit());
Kevin Enderby8ae63c12014-09-04 16:54:47 +00009011 } else if (Command.C.cmd == MachO::LC_DYLD_INFO ||
9012 Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
9013 MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command);
9014 PrintDyldInfoLoadCommand(DyldInfo, Buf.size());
9015 } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER ||
9016 Command.C.cmd == MachO::LC_ID_DYLINKER ||
9017 Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
9018 MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command);
9019 PrintDyldLoadCommand(Dyld, Command.Ptr);
9020 } else if (Command.C.cmd == MachO::LC_UUID) {
9021 MachO::uuid_command Uuid = Obj->getUuidCommand(Command);
9022 PrintUuidLoadCommand(Uuid);
Jean-Daniel Dupas00cc1f52014-12-04 07:37:02 +00009023 } else if (Command.C.cmd == MachO::LC_RPATH) {
9024 MachO::rpath_command Rpath = Obj->getRpathCommand(Command);
9025 PrintRpathLoadCommand(Rpath, Command.Ptr);
Kevin Enderby1ff0ecc2014-12-16 21:48:27 +00009026 } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX ||
Tim Northoverbfbfb122015-11-02 21:26:58 +00009027 Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS ||
9028 Command.C.cmd == MachO::LC_VERSION_MIN_TVOS ||
9029 Command.C.cmd == MachO::LC_VERSION_MIN_WATCHOS) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00009030 MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command);
9031 PrintVersionMinLoadCommand(Vd);
9032 } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) {
9033 MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command);
9034 PrintSourceVersionCommand(Sd);
9035 } else if (Command.C.cmd == MachO::LC_MAIN) {
9036 MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command);
9037 PrintEntryPointCommand(Ep);
Kevin Enderby0804f4672014-12-16 23:25:52 +00009038 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00009039 MachO::encryption_info_command Ei =
9040 Obj->getEncryptionInfoCommand(Command);
Kevin Enderby0804f4672014-12-16 23:25:52 +00009041 PrintEncryptionInfoCommand(Ei, Buf.size());
Kevin Enderby57538292014-12-17 01:01:30 +00009042 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00009043 MachO::encryption_info_command_64 Ei =
9044 Obj->getEncryptionInfoCommand64(Command);
Kevin Enderby57538292014-12-17 01:01:30 +00009045 PrintEncryptionInfoCommand64(Ei, Buf.size());
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00009046 } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00009047 MachO::linker_option_command Lo =
9048 Obj->getLinkerOptionLoadCommand(Command);
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00009049 PrintLinkerOptionCommand(Lo, Command.Ptr);
Kevin Enderbyb4b79312014-12-18 19:24:35 +00009050 } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) {
9051 MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command);
9052 PrintSubFrameworkCommand(Sf, Command.Ptr);
Kevin Enderbya2bd8d92014-12-18 23:13:26 +00009053 } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) {
9054 MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command);
9055 PrintSubUmbrellaCommand(Sf, Command.Ptr);
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00009056 } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) {
9057 MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command);
9058 PrintSubLibraryCommand(Sl, Command.Ptr);
Kevin Enderby186eac32014-12-19 21:06:24 +00009059 } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) {
9060 MachO::sub_client_command Sc = Obj->getSubClientCommand(Command);
9061 PrintSubClientCommand(Sc, Command.Ptr);
Kevin Enderby52e4ce42014-12-19 22:25:22 +00009062 } else if (Command.C.cmd == MachO::LC_ROUTINES) {
9063 MachO::routines_command Rc = Obj->getRoutinesCommand(Command);
9064 PrintRoutinesCommand(Rc);
9065 } else if (Command.C.cmd == MachO::LC_ROUTINES_64) {
9066 MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command);
9067 PrintRoutinesCommand64(Rc);
Kevin Enderby48ef5342014-12-23 22:56:39 +00009068 } else if (Command.C.cmd == MachO::LC_THREAD ||
9069 Command.C.cmd == MachO::LC_UNIXTHREAD) {
9070 MachO::thread_command Tc = Obj->getThreadCommand(Command);
9071 PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype);
Nick Kledzik15558912014-10-16 18:58:20 +00009072 } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
9073 Command.C.cmd == MachO::LC_ID_DYLIB ||
9074 Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
9075 Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
9076 Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
9077 Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00009078 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
9079 PrintDylibCommand(Dl, Command.Ptr);
9080 } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE ||
9081 Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO ||
9082 Command.C.cmd == MachO::LC_FUNCTION_STARTS ||
9083 Command.C.cmd == MachO::LC_DATA_IN_CODE ||
9084 Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS ||
9085 Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
9086 MachO::linkedit_data_command Ld =
9087 Obj->getLinkeditDataLoadCommand(Command);
9088 PrintLinkEditDataCommand(Ld, Buf.size());
Kevin Enderby956366c2014-08-29 22:30:52 +00009089 } else {
9090 outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd)
9091 << ")\n";
9092 outs() << " cmdsize " << Command.C.cmdsize << "\n";
9093 // TODO: get and print the raw bytes of the load command.
9094 }
9095 // TODO: print all the other kinds of load commands.
Kevin Enderby956366c2014-08-29 22:30:52 +00009096 }
9097}
9098
Kevin Enderby0ae163f2016-01-13 00:25:36 +00009099static void PrintMachHeader(const MachOObjectFile *Obj, bool verbose) {
Kevin Enderbyb76d3862014-08-22 20:35:18 +00009100 if (Obj->is64Bit()) {
9101 MachO::mach_header_64 H_64;
9102 H_64 = Obj->getHeader64();
9103 PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
9104 H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
9105 } else {
9106 MachO::mach_header H;
9107 H = Obj->getHeader();
9108 PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
9109 H.sizeofcmds, H.flags, verbose);
9110 }
9111}
9112
9113void llvm::printMachOFileHeader(const object::ObjectFile *Obj) {
9114 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
Kevin Enderby0ae163f2016-01-13 00:25:36 +00009115 PrintMachHeader(file, !NonVerbose);
9116}
9117
9118void llvm::printMachOLoadCommands(const object::ObjectFile *Obj) {
9119 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
Kevin Enderby956366c2014-08-29 22:30:52 +00009120 uint32_t filetype = 0;
9121 uint32_t cputype = 0;
Kevin Enderby0ae163f2016-01-13 00:25:36 +00009122 if (file->is64Bit()) {
9123 MachO::mach_header_64 H_64;
9124 H_64 = file->getHeader64();
9125 filetype = H_64.filetype;
9126 cputype = H_64.cputype;
9127 } else {
9128 MachO::mach_header H;
9129 H = file->getHeader();
9130 filetype = H.filetype;
9131 cputype = H.cputype;
9132 }
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00009133 PrintLoadCommands(file, filetype, cputype, !NonVerbose);
Kevin Enderbyb76d3862014-08-22 20:35:18 +00009134}
Nick Kledzikd04bc352014-08-30 00:20:14 +00009135
9136//===----------------------------------------------------------------------===//
9137// export trie dumping
9138//===----------------------------------------------------------------------===//
9139
9140void llvm::printMachOExportsTrie(const object::MachOObjectFile *Obj) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009141 for (const llvm::object::ExportEntry &Entry : Obj->exports()) {
9142 uint64_t Flags = Entry.flags();
Nick Kledzikd04bc352014-08-30 00:20:14 +00009143 bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
9144 bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
9145 bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
9146 MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL);
9147 bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
9148 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
9149 bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
9150 if (ReExport)
9151 outs() << "[re-export] ";
9152 else
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009153 outs() << format("0x%08llX ",
9154 Entry.address()); // FIXME:add in base address
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009155 outs() << Entry.name();
Nick Kledzikd04bc352014-08-30 00:20:14 +00009156 if (WeakDef || ThreadLocal || Resolver || Abs) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009157 bool NeedsComma = false;
Nick Kledzik1d1ac4b2014-09-03 01:12:52 +00009158 outs() << " [";
Nick Kledzikd04bc352014-08-30 00:20:14 +00009159 if (WeakDef) {
9160 outs() << "weak_def";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009161 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00009162 }
9163 if (ThreadLocal) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009164 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00009165 outs() << ", ";
9166 outs() << "per-thread";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009167 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00009168 }
9169 if (Abs) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009170 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00009171 outs() << ", ";
9172 outs() << "absolute";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009173 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00009174 }
9175 if (Resolver) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009176 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00009177 outs() << ", ";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009178 outs() << format("resolver=0x%08llX", Entry.other());
9179 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00009180 }
9181 outs() << "]";
9182 }
9183 if (ReExport) {
9184 StringRef DylibName = "unknown";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009185 int Ordinal = Entry.other() - 1;
9186 Obj->getLibraryShortNameByIndex(Ordinal, DylibName);
9187 if (Entry.otherName().empty())
Nick Kledzikd04bc352014-08-30 00:20:14 +00009188 outs() << " (from " << DylibName << ")";
9189 else
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009190 outs() << " (" << Entry.otherName() << " from " << DylibName << ")";
Nick Kledzikd04bc352014-08-30 00:20:14 +00009191 }
9192 outs() << "\n";
9193 }
9194}
Nick Kledzikac431442014-09-12 21:34:15 +00009195
Nick Kledzikac431442014-09-12 21:34:15 +00009196//===----------------------------------------------------------------------===//
9197// rebase table dumping
9198//===----------------------------------------------------------------------===//
9199
9200namespace {
9201class SegInfo {
9202public:
9203 SegInfo(const object::MachOObjectFile *Obj);
9204
9205 StringRef segmentName(uint32_t SegIndex);
9206 StringRef sectionName(uint32_t SegIndex, uint64_t SegOffset);
9207 uint64_t address(uint32_t SegIndex, uint64_t SegOffset);
Kevin Enderbyaf7c9d02015-10-09 16:48:44 +00009208 bool isValidSegIndexAndOffset(uint32_t SegIndex, uint64_t SegOffset);
Nick Kledzikac431442014-09-12 21:34:15 +00009209
9210private:
9211 struct SectionInfo {
9212 uint64_t Address;
9213 uint64_t Size;
9214 StringRef SectionName;
9215 StringRef SegmentName;
9216 uint64_t OffsetInSegment;
9217 uint64_t SegmentStartAddress;
9218 uint32_t SegmentIndex;
9219 };
9220 const SectionInfo &findSection(uint32_t SegIndex, uint64_t SegOffset);
9221 SmallVector<SectionInfo, 32> Sections;
9222};
9223}
9224
9225SegInfo::SegInfo(const object::MachOObjectFile *Obj) {
9226 // Build table of sections so segIndex/offset pairs can be translated.
Nick Kledzik56ebef42014-09-16 01:41:51 +00009227 uint32_t CurSegIndex = Obj->hasPageZeroSegment() ? 1 : 0;
Nick Kledzikac431442014-09-12 21:34:15 +00009228 StringRef CurSegName;
9229 uint64_t CurSegAddress;
9230 for (const SectionRef &Section : Obj->sections()) {
9231 SectionInfo Info;
Davide Italianoccd53fe2015-08-05 07:18:31 +00009232 error(Section.getName(Info.SectionName));
Rafael Espindola80291272014-10-08 15:28:58 +00009233 Info.Address = Section.getAddress();
9234 Info.Size = Section.getSize();
Nick Kledzikac431442014-09-12 21:34:15 +00009235 Info.SegmentName =
9236 Obj->getSectionFinalSegmentName(Section.getRawDataRefImpl());
9237 if (!Info.SegmentName.equals(CurSegName)) {
9238 ++CurSegIndex;
9239 CurSegName = Info.SegmentName;
9240 CurSegAddress = Info.Address;
9241 }
9242 Info.SegmentIndex = CurSegIndex - 1;
9243 Info.OffsetInSegment = Info.Address - CurSegAddress;
9244 Info.SegmentStartAddress = CurSegAddress;
9245 Sections.push_back(Info);
9246 }
9247}
9248
9249StringRef SegInfo::segmentName(uint32_t SegIndex) {
9250 for (const SectionInfo &SI : Sections) {
9251 if (SI.SegmentIndex == SegIndex)
9252 return SI.SegmentName;
9253 }
9254 llvm_unreachable("invalid segIndex");
9255}
9256
Kevin Enderbyaf7c9d02015-10-09 16:48:44 +00009257bool SegInfo::isValidSegIndexAndOffset(uint32_t SegIndex,
9258 uint64_t OffsetInSeg) {
9259 for (const SectionInfo &SI : Sections) {
9260 if (SI.SegmentIndex != SegIndex)
9261 continue;
9262 if (SI.OffsetInSegment > OffsetInSeg)
9263 continue;
9264 if (OffsetInSeg >= (SI.OffsetInSegment + SI.Size))
9265 continue;
9266 return true;
9267 }
9268 return false;
9269}
9270
Nick Kledzikac431442014-09-12 21:34:15 +00009271const SegInfo::SectionInfo &SegInfo::findSection(uint32_t SegIndex,
9272 uint64_t OffsetInSeg) {
9273 for (const SectionInfo &SI : Sections) {
9274 if (SI.SegmentIndex != SegIndex)
9275 continue;
9276 if (SI.OffsetInSegment > OffsetInSeg)
9277 continue;
9278 if (OffsetInSeg >= (SI.OffsetInSegment + SI.Size))
9279 continue;
9280 return SI;
9281 }
9282 llvm_unreachable("segIndex and offset not in any section");
9283}
9284
9285StringRef SegInfo::sectionName(uint32_t SegIndex, uint64_t OffsetInSeg) {
9286 return findSection(SegIndex, OffsetInSeg).SectionName;
9287}
9288
9289uint64_t SegInfo::address(uint32_t SegIndex, uint64_t OffsetInSeg) {
9290 const SectionInfo &SI = findSection(SegIndex, OffsetInSeg);
9291 return SI.SegmentStartAddress + OffsetInSeg;
9292}
9293
9294void llvm::printMachORebaseTable(const object::MachOObjectFile *Obj) {
9295 // Build table of sections so names can used in final output.
9296 SegInfo sectionTable(Obj);
9297
9298 outs() << "segment section address type\n";
9299 for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable()) {
9300 uint32_t SegIndex = Entry.segmentIndex();
9301 uint64_t OffsetInSeg = Entry.segmentOffset();
9302 StringRef SegmentName = sectionTable.segmentName(SegIndex);
9303 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
9304 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
9305
9306 // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009307 outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n",
9308 SegmentName.str().c_str(), SectionName.str().c_str(),
9309 Address, Entry.typeName().str().c_str());
Nick Kledzikac431442014-09-12 21:34:15 +00009310 }
9311}
Nick Kledzik56ebef42014-09-16 01:41:51 +00009312
9313static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
9314 StringRef DylibName;
9315 switch (Ordinal) {
9316 case MachO::BIND_SPECIAL_DYLIB_SELF:
9317 return "this-image";
9318 case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
9319 return "main-executable";
9320 case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
9321 return "flat-namespace";
9322 default:
Nick Kledzikabd29872014-09-16 22:03:13 +00009323 if (Ordinal > 0) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009324 std::error_code EC =
9325 Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName);
Nick Kledzikabd29872014-09-16 22:03:13 +00009326 if (EC)
Nick Kledzik51d2c2b2014-10-14 23:29:38 +00009327 return "<<bad library ordinal>>";
Nick Kledzikabd29872014-09-16 22:03:13 +00009328 return DylibName;
9329 }
Nick Kledzik56ebef42014-09-16 01:41:51 +00009330 }
Nick Kledzikabd29872014-09-16 22:03:13 +00009331 return "<<unknown special ordinal>>";
Nick Kledzik56ebef42014-09-16 01:41:51 +00009332}
9333
9334//===----------------------------------------------------------------------===//
9335// bind table dumping
9336//===----------------------------------------------------------------------===//
9337
9338void llvm::printMachOBindTable(const object::MachOObjectFile *Obj) {
9339 // Build table of sections so names can used in final output.
9340 SegInfo sectionTable(Obj);
9341
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009342 outs() << "segment section address type "
9343 "addend dylib symbol\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00009344 for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable()) {
9345 uint32_t SegIndex = Entry.segmentIndex();
9346 uint64_t OffsetInSeg = Entry.segmentOffset();
9347 StringRef SegmentName = sectionTable.segmentName(SegIndex);
9348 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
9349 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
9350
9351 // Table lines look like:
9352 // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009353 StringRef Attr;
Nick Kledzik56ebef42014-09-16 01:41:51 +00009354 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT)
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009355 Attr = " (weak_import)";
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009356 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009357 << left_justify(SectionName, 18) << " "
9358 << format_hex(Address, 10, true) << " "
9359 << left_justify(Entry.typeName(), 8) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009360 << format_decimal(Entry.addend(), 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009361 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009362 << Entry.symbolName() << Attr << "\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00009363 }
9364}
9365
9366//===----------------------------------------------------------------------===//
9367// lazy bind table dumping
9368//===----------------------------------------------------------------------===//
9369
9370void llvm::printMachOLazyBindTable(const object::MachOObjectFile *Obj) {
9371 // Build table of sections so names can used in final output.
9372 SegInfo sectionTable(Obj);
9373
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009374 outs() << "segment section address "
9375 "dylib symbol\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00009376 for (const llvm::object::MachOBindEntry &Entry : Obj->lazyBindTable()) {
9377 uint32_t SegIndex = Entry.segmentIndex();
9378 uint64_t OffsetInSeg = Entry.segmentOffset();
9379 StringRef SegmentName = sectionTable.segmentName(SegIndex);
9380 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
9381 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
9382
9383 // Table lines look like:
9384 // __DATA __got 0x00012010 libSystem ___stack_chk_guard
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009385 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009386 << left_justify(SectionName, 18) << " "
9387 << format_hex(Address, 10, true) << " "
9388 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
Nick Kledzik56ebef42014-09-16 01:41:51 +00009389 << Entry.symbolName() << "\n";
9390 }
9391}
9392
Nick Kledzik56ebef42014-09-16 01:41:51 +00009393//===----------------------------------------------------------------------===//
9394// weak bind table dumping
9395//===----------------------------------------------------------------------===//
9396
9397void llvm::printMachOWeakBindTable(const object::MachOObjectFile *Obj) {
9398 // Build table of sections so names can used in final output.
9399 SegInfo sectionTable(Obj);
9400
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009401 outs() << "segment section address "
9402 "type addend symbol\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00009403 for (const llvm::object::MachOBindEntry &Entry : Obj->weakBindTable()) {
9404 // Strong symbols don't have a location to update.
9405 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009406 outs() << " strong "
Nick Kledzik56ebef42014-09-16 01:41:51 +00009407 << Entry.symbolName() << "\n";
9408 continue;
9409 }
9410 uint32_t SegIndex = Entry.segmentIndex();
9411 uint64_t OffsetInSeg = Entry.segmentOffset();
9412 StringRef SegmentName = sectionTable.segmentName(SegIndex);
9413 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
9414 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
9415
9416 // Table lines look like:
9417 // __DATA __data 0x00001000 pointer 0 _foo
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009418 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009419 << left_justify(SectionName, 18) << " "
9420 << format_hex(Address, 10, true) << " "
9421 << left_justify(Entry.typeName(), 8) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009422 << format_decimal(Entry.addend(), 8) << " " << Entry.symbolName()
9423 << "\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00009424 }
9425}
9426
Kevin Enderby6f326ce2014-10-23 19:37:31 +00009427// get_dyld_bind_info_symbolname() is used for disassembly and passed an
9428// address, ReferenceValue, in the Mach-O file and looks in the dyld bind
9429// information for that address. If the address is found its binding symbol
9430// name is returned. If not nullptr is returned.
9431static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
9432 struct DisassembleInfo *info) {
Kevin Enderby078be602014-10-23 19:53:12 +00009433 if (info->bindtable == nullptr) {
9434 info->bindtable = new (BindTable);
Kevin Enderby6f326ce2014-10-23 19:37:31 +00009435 SegInfo sectionTable(info->O);
9436 for (const llvm::object::MachOBindEntry &Entry : info->O->bindTable()) {
9437 uint32_t SegIndex = Entry.segmentIndex();
9438 uint64_t OffsetInSeg = Entry.segmentOffset();
Kevin Enderbyaf7c9d02015-10-09 16:48:44 +00009439 if (!sectionTable.isValidSegIndexAndOffset(SegIndex, OffsetInSeg))
9440 continue;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00009441 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
9442 const char *SymbolName = nullptr;
9443 StringRef name = Entry.symbolName();
9444 if (!name.empty())
9445 SymbolName = name.data();
Kevin Enderby078be602014-10-23 19:53:12 +00009446 info->bindtable->push_back(std::make_pair(Address, SymbolName));
Kevin Enderby6f326ce2014-10-23 19:37:31 +00009447 }
9448 }
Kevin Enderby078be602014-10-23 19:53:12 +00009449 for (bind_table_iterator BI = info->bindtable->begin(),
9450 BE = info->bindtable->end();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00009451 BI != BE; ++BI) {
9452 uint64_t Address = BI->first;
9453 if (ReferenceValue == Address) {
9454 const char *SymbolName = BI->second;
9455 return SymbolName;
9456 }
9457 }
9458 return nullptr;
9459}