blob: 006ba5765e79ba19b3e280ba1eaddb1287e74f1d [file] [log] [blame]
Benjamin Kramer43a772e2011-09-19 17:56:04 +00001//===-- MachODump.cpp - Object file dumping utility for llvm --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the MachO-specific dumper for llvm-objdump.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm-objdump.h"
Kevin Enderby98c9acc2014-09-16 18:00:57 +000015#include "llvm-c/Disassembler.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000016#include "llvm/ADT/STLExtras.h"
Ahmed Bougachaaa790682013-05-24 01:07:04 +000017#include "llvm/ADT/StringExtras.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000018#include "llvm/ADT/Triple.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000019#include "llvm/BinaryFormat/MachO.h"
Kevin Enderby04bf6932014-10-28 23:39:46 +000020#include "llvm/Config/config.h"
Zachary Turner6489d7b2015-04-23 17:37:47 +000021#include "llvm/DebugInfo/DIContext.h"
22#include "llvm/DebugInfo/DWARF/DWARFContext.h"
Rafael Espindolab940b662016-09-06 19:16:48 +000023#include "llvm/Demangle/Demangle.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000024#include "llvm/MC/MCAsmInfo.h"
Lang Hamesa1bc0f52014-04-15 04:40:56 +000025#include "llvm/MC/MCContext.h"
Benjamin Kramerf57c1972016-01-26 16:44:37 +000026#include "llvm/MC/MCDisassembler/MCDisassembler.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000027#include "llvm/MC/MCInst.h"
28#include "llvm/MC/MCInstPrinter.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000029#include "llvm/MC/MCInstrDesc.h"
30#include "llvm/MC/MCInstrInfo.h"
Jim Grosbachfd93a592012-03-05 19:33:20 +000031#include "llvm/MC/MCRegisterInfo.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000032#include "llvm/MC/MCSubtargetInfo.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000033#include "llvm/Object/MachO.h"
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +000034#include "llvm/Object/MachOUniversal.h"
Rafael Espindola9b709252013-04-13 01:45:40 +000035#include "llvm/Support/Casting.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000036#include "llvm/Support/CommandLine.h"
37#include "llvm/Support/Debug.h"
Tim Northover4bd286a2014-08-01 13:07:19 +000038#include "llvm/Support/Endian.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000039#include "llvm/Support/Format.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000040#include "llvm/Support/FormattedStream.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000041#include "llvm/Support/GraphWriter.h"
Kevin Enderby9a509442015-01-27 21:28:24 +000042#include "llvm/Support/LEB128.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000043#include "llvm/Support/MemoryBuffer.h"
44#include "llvm/Support/TargetRegistry.h"
45#include "llvm/Support/TargetSelect.h"
Kevin Enderby9873e2c2016-05-23 21:34:12 +000046#include "llvm/Support/ToolOutputFile.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000047#include "llvm/Support/raw_ostream.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000048#include <algorithm>
49#include <cstring>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000050#include <system_error>
Kevin Enderby04bf6932014-10-28 23:39:46 +000051
Kevin Enderby9873e2c2016-05-23 21:34:12 +000052#ifdef HAVE_LIBXAR
53extern "C" {
54#include <xar/xar.h>
55}
56#endif
57
Benjamin Kramer43a772e2011-09-19 17:56:04 +000058using namespace llvm;
59using namespace object;
60
61static cl::opt<bool>
Kevin Enderbyb28ed012014-10-29 21:28:24 +000062 UseDbg("g",
63 cl::desc("Print line information from debug info if available"));
Benjamin Kramer699128e2011-09-21 01:13:19 +000064
Kevin Enderbyb28ed012014-10-29 21:28:24 +000065static cl::opt<std::string> DSYMFile("dsym",
66 cl::desc("Use .dSYM file for debug info"));
Benjamin Kramer699128e2011-09-21 01:13:19 +000067
Kevin Enderbyb28ed012014-10-29 21:28:24 +000068static cl::opt<bool> FullLeadingAddr("full-leading-addr",
69 cl::desc("Print full leading address"));
Kevin Enderbybf246f52014-09-24 23:08:22 +000070
Kevin Enderbyf9d60f02016-11-29 21:43:40 +000071static cl::opt<bool> NoLeadingHeaders("no-leading-headers",
72 cl::desc("Print no leading headers"));
73
Kevin Enderby13023a12015-01-15 23:19:11 +000074cl::opt<bool> llvm::UniversalHeaders("universal-headers",
75 cl::desc("Print Mach-O universal headers "
76 "(requires -macho)"));
77
Kevin Enderby131d1772015-01-09 19:22:37 +000078cl::opt<bool>
Kevin Enderby13023a12015-01-15 23:19:11 +000079 llvm::ArchiveHeaders("archive-headers",
80 cl::desc("Print archive headers for Mach-O archives "
81 "(requires -macho)"));
Kevin Enderby131d1772015-01-09 19:22:37 +000082
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +000083cl::opt<bool>
Kevin Enderby8972e482015-04-30 20:30:42 +000084 ArchiveMemberOffsets("archive-member-offsets",
85 cl::desc("Print the offset to each archive member for "
86 "Mach-O archives (requires -macho and "
87 "-archive-headers)"));
88
89cl::opt<bool>
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +000090 llvm::IndirectSymbols("indirect-symbols",
91 cl::desc("Print indirect symbol table for Mach-O "
92 "objects (requires -macho)"));
93
Kevin Enderby69fe98d2015-01-23 18:52:17 +000094cl::opt<bool>
95 llvm::DataInCode("data-in-code",
96 cl::desc("Print the data in code table for Mach-O objects "
97 "(requires -macho)"));
98
Kevin Enderby9a509442015-01-27 21:28:24 +000099cl::opt<bool>
100 llvm::LinkOptHints("link-opt-hints",
101 cl::desc("Print the linker optimization hints for "
102 "Mach-O objects (requires -macho)"));
103
Kevin Enderbycd66be52015-03-11 22:06:32 +0000104cl::opt<bool>
105 llvm::InfoPlist("info-plist",
106 cl::desc("Print the info plist section as strings for "
107 "Mach-O objects (requires -macho)"));
108
Kevin Enderbyf0640752015-03-13 17:56:32 +0000109cl::opt<bool>
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000110 llvm::DylibsUsed("dylibs-used",
111 cl::desc("Print the shared libraries used for linked "
112 "Mach-O files (requires -macho)"));
113
114cl::opt<bool>
115 llvm::DylibId("dylib-id",
116 cl::desc("Print the shared library's id for the dylib Mach-O "
117 "file (requires -macho)"));
118
119cl::opt<bool>
Kevin Enderbyf0640752015-03-13 17:56:32 +0000120 llvm::NonVerbose("non-verbose",
121 cl::desc("Print the info for Mach-O objects in "
122 "non-verbose or numeric form (requires -macho)"));
123
Kevin Enderby0fc11822015-04-01 20:57:01 +0000124cl::opt<bool>
125 llvm::ObjcMetaData("objc-meta-data",
126 cl::desc("Print the Objective-C runtime meta data for "
127 "Mach-O files (requires -macho)"));
128
Kevin Enderby6a221752015-03-17 17:10:57 +0000129cl::opt<std::string> llvm::DisSymName(
130 "dis-symname",
Saleem Abdulrasoolec6a7742016-09-08 23:17:34 +0000131 cl::desc("disassemble just this symbol's instructions (requires -macho)"));
Kevin Enderby6a221752015-03-17 17:10:57 +0000132
Kevin Enderby8e29ec92015-03-17 22:26:11 +0000133static cl::opt<bool> NoSymbolicOperands(
134 "no-symbolic-operands",
135 cl::desc("do not symbolic operands when disassembling (requires -macho)"));
136
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000137static cl::list<std::string>
138 ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
139 cl::ZeroOrMore);
Hans Wennborgcc9deb42015-09-29 18:02:48 +0000140
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000141bool ArchAll = false;
142
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000143static std::string ThumbTripleName;
144
145static const Target *GetTarget(const MachOObjectFile *MachOObj,
146 const char **McpuDefault,
147 const Target **ThumbTarget) {
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000148 // Figure out the target triple.
Tim Northover9e8eb412016-04-22 23:21:13 +0000149 llvm::Triple TT(TripleName);
Cameron Zwarich88cc16a2012-02-03 06:35:22 +0000150 if (TripleName.empty()) {
Tim Northover9e8eb412016-04-22 23:21:13 +0000151 TT = MachOObj->getArchTriple(McpuDefault);
Cameron Zwarich88cc16a2012-02-03 06:35:22 +0000152 TripleName = TT.str();
Tim Northover9e8eb412016-04-22 23:21:13 +0000153 }
154
155 if (TT.getArch() == Triple::arm) {
156 // We've inferred a 32-bit ARM target from the object file. All MachO CPUs
157 // that support ARM are also capable of Thumb mode.
158 llvm::Triple ThumbTriple = TT;
159 std::string ThumbName = (Twine("thumb") + TT.getArchName().substr(3)).str();
160 ThumbTriple.setArchName(ThumbName);
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000161 ThumbTripleName = ThumbTriple.str();
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000162 }
163
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000164 // Get the target specific parser.
165 std::string Error;
166 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000167 if (TheTarget && ThumbTripleName.empty())
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000168 return TheTarget;
169
Kevin Enderbyec5ca032014-08-18 20:21:02 +0000170 *ThumbTarget = TargetRegistry::lookupTarget(ThumbTripleName, Error);
171 if (*ThumbTarget)
172 return TheTarget;
173
174 errs() << "llvm-objdump: error: unable to get target for '";
175 if (!TheTarget)
176 errs() << TripleName;
177 else
178 errs() << ThumbTripleName;
179 errs() << "', see --version and --triple.\n";
Craig Toppere6cb63e2014-04-25 04:24:47 +0000180 return nullptr;
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000181}
182
Owen Andersond9243c42011-10-17 21:37:35 +0000183struct SymbolSorter {
184 bool operator()(const SymbolRef &A, const SymbolRef &B) {
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000185 Expected<SymbolRef::Type> ATypeOrErr = A.getType();
Kevin Enderby844c4ac2016-11-15 23:07:41 +0000186 if (!ATypeOrErr)
187 report_error(A.getObject()->getFileName(), ATypeOrErr.takeError());
Kevin Enderby5afbc1c2016-03-23 20:27:00 +0000188 SymbolRef::Type AType = *ATypeOrErr;
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000189 Expected<SymbolRef::Type> BTypeOrErr = B.getType();
Kevin Enderby844c4ac2016-11-15 23:07:41 +0000190 if (!BTypeOrErr)
191 report_error(B.getObject()->getFileName(), BTypeOrErr.takeError());
Kevin Enderby74f58d42016-03-23 21:45:21 +0000192 SymbolRef::Type BType = *BTypeOrErr;
Kevin Enderby5afbc1c2016-03-23 20:27:00 +0000193 uint64_t AAddr = (AType != SymbolRef::ST_Function) ? 0 : A.getValue();
194 uint64_t BAddr = (BType != SymbolRef::ST_Function) ? 0 : B.getValue();
Owen Andersond9243c42011-10-17 21:37:35 +0000195 return AAddr < BAddr;
196 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000197};
198
Kevin Enderby273ae012013-06-06 17:20:50 +0000199// Types for the storted data in code table that is built before disassembly
200// and the predicate function to sort them.
201typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
202typedef std::vector<DiceTableEntry> DiceTable;
203typedef DiceTable::iterator dice_table_iterator;
204
Kevin Enderby930fdc72014-11-06 19:00:13 +0000205// This is used to search for a data in code table entry for the PC being
206// disassembled. The j parameter has the PC in j.first. A single data in code
207// table entry can cover many bytes for each of its Kind's. So if the offset,
208// aka the i.first value, of the data in code table entry plus its Length
209// covers the PC being searched for this will return true. If not it will
210// return false.
David Majnemerea9b8ee2014-11-04 08:41:48 +0000211static bool compareDiceTableEntries(const DiceTableEntry &i,
212 const DiceTableEntry &j) {
Kevin Enderby930fdc72014-11-06 19:00:13 +0000213 uint16_t Length;
214 i.second.getLength(Length);
215
216 return j.first >= i.first && j.first < i.first + Length;
Kevin Enderby273ae012013-06-06 17:20:50 +0000217}
218
Colin LeMahieufc32b1b2015-03-18 19:27:31 +0000219static uint64_t DumpDataInCode(const uint8_t *bytes, uint64_t Length,
Kevin Enderby930fdc72014-11-06 19:00:13 +0000220 unsigned short Kind) {
221 uint32_t Value, Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000222
223 switch (Kind) {
Kevin Enderby930fdc72014-11-06 19:00:13 +0000224 default:
Charles Davis8bdfafd2013-09-01 04:28:48 +0000225 case MachO::DICE_KIND_DATA:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000226 if (Length >= 4) {
227 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000228 dumpBytes(makeArrayRef(bytes, 4), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000229 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
Kevin Enderby273ae012013-06-06 17:20:50 +0000230 outs() << "\t.long " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000231 Size = 4;
232 } else if (Length >= 2) {
233 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000234 dumpBytes(makeArrayRef(bytes, 2), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000235 Value = bytes[1] << 8 | bytes[0];
Kevin Enderby273ae012013-06-06 17:20:50 +0000236 outs() << "\t.short " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000237 Size = 2;
238 } else {
239 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000240 dumpBytes(makeArrayRef(bytes, 2), outs());
Kevin Enderby273ae012013-06-06 17:20:50 +0000241 Value = bytes[0];
242 outs() << "\t.byte " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000243 Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000244 }
Kevin Enderby930fdc72014-11-06 19:00:13 +0000245 if (Kind == MachO::DICE_KIND_DATA)
246 outs() << "\t@ KIND_DATA\n";
247 else
248 outs() << "\t@ data in code kind = " << Kind << "\n";
Kevin Enderby273ae012013-06-06 17:20:50 +0000249 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000250 case MachO::DICE_KIND_JUMP_TABLE8:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000251 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000252 dumpBytes(makeArrayRef(bytes, 1), outs());
Kevin Enderby273ae012013-06-06 17:20:50 +0000253 Value = bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000254 outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n";
255 Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000256 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000257 case MachO::DICE_KIND_JUMP_TABLE16:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000258 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000259 dumpBytes(makeArrayRef(bytes, 2), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000260 Value = bytes[1] << 8 | bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000261 outs() << "\t.short " << format("%5u", Value & 0xffff)
262 << "\t@ KIND_JUMP_TABLE16\n";
263 Size = 2;
Kevin Enderby273ae012013-06-06 17:20:50 +0000264 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000265 case MachO::DICE_KIND_JUMP_TABLE32:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000266 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
267 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000268 dumpBytes(makeArrayRef(bytes, 4), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000269 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000270 outs() << "\t.long " << Value;
271 if (Kind == MachO::DICE_KIND_JUMP_TABLE32)
272 outs() << "\t@ KIND_JUMP_TABLE32\n";
273 else
274 outs() << "\t@ KIND_ABS_JUMP_TABLE32\n";
275 Size = 4;
Kevin Enderby273ae012013-06-06 17:20:50 +0000276 break;
277 }
Kevin Enderby930fdc72014-11-06 19:00:13 +0000278 return Size;
Kevin Enderby273ae012013-06-06 17:20:50 +0000279}
280
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000281static void getSectionsAndSymbols(MachOObjectFile *MachOObj,
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000282 std::vector<SectionRef> &Sections,
283 std::vector<SymbolRef> &Symbols,
284 SmallVectorImpl<uint64_t> &FoundFns,
285 uint64_t &BaseSegmentAddress) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000286 for (const SymbolRef &Symbol : MachOObj->symbols()) {
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000287 Expected<StringRef> SymName = Symbol.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +0000288 if (!SymName)
289 report_error(MachOObj->getFileName(), SymName.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000290 if (!SymName->startswith("ltmp"))
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000291 Symbols.push_back(Symbol);
292 }
Owen Andersond9243c42011-10-17 21:37:35 +0000293
Alexey Samsonov48803e52014-03-13 14:37:36 +0000294 for (const SectionRef &Section : MachOObj->sections()) {
Owen Andersond9243c42011-10-17 21:37:35 +0000295 StringRef SectName;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000296 Section.getName(SectName);
297 Sections.push_back(Section);
Owen Andersond9243c42011-10-17 21:37:35 +0000298 }
299
Kevin Enderby273ae012013-06-06 17:20:50 +0000300 bool BaseSegmentAddressSet = false;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000301 for (const auto &Command : MachOObj->load_commands()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000302 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
Benjamin Kramer699128e2011-09-21 01:13:19 +0000303 // We found a function starts segment, parse the addresses for later
304 // consumption.
Charles Davis8bdfafd2013-09-01 04:28:48 +0000305 MachO::linkedit_data_command LLC =
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000306 MachOObj->getLinkeditDataLoadCommand(Command);
Benjamin Kramer699128e2011-09-21 01:13:19 +0000307
Charles Davis8bdfafd2013-09-01 04:28:48 +0000308 MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000309 } else if (Command.C.cmd == MachO::LC_SEGMENT) {
310 MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000311 StringRef SegName = SLC.segname;
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000312 if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
Kevin Enderby273ae012013-06-06 17:20:50 +0000313 BaseSegmentAddressSet = true;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000314 BaseSegmentAddress = SLC.vmaddr;
Kevin Enderby273ae012013-06-06 17:20:50 +0000315 }
316 }
Benjamin Kramer8a529dc2011-09-21 22:16:43 +0000317 }
Benjamin Kramer699128e2011-09-21 01:13:19 +0000318}
319
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000320static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose,
321 uint32_t n, uint32_t count,
322 uint32_t stride, uint64_t addr) {
323 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
324 uint32_t nindirectsyms = Dysymtab.nindirectsyms;
325 if (n > nindirectsyms)
326 outs() << " (entries start past the end of the indirect symbol "
327 "table) (reserved1 field greater than the table size)";
328 else if (n + count > nindirectsyms)
329 outs() << " (entries extends past the end of the indirect symbol "
330 "table)";
331 outs() << "\n";
332 uint32_t cputype = O->getHeader().cputype;
333 if (cputype & MachO::CPU_ARCH_ABI64)
334 outs() << "address index";
335 else
336 outs() << "address index";
337 if (verbose)
338 outs() << " name\n";
339 else
340 outs() << "\n";
341 for (uint32_t j = 0; j < count && n + j < nindirectsyms; j++) {
342 if (cputype & MachO::CPU_ARCH_ABI64)
343 outs() << format("0x%016" PRIx64, addr + j * stride) << " ";
344 else
Tim Northover43978372016-04-26 18:29:16 +0000345 outs() << format("0x%08" PRIx32, (uint32_t)addr + j * stride) << " ";
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000346 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
347 uint32_t indirect_symbol = O->getIndirectSymbolTableEntry(Dysymtab, n + j);
348 if (indirect_symbol == MachO::INDIRECT_SYMBOL_LOCAL) {
349 outs() << "LOCAL\n";
350 continue;
351 }
352 if (indirect_symbol ==
353 (MachO::INDIRECT_SYMBOL_LOCAL | MachO::INDIRECT_SYMBOL_ABS)) {
354 outs() << "LOCAL ABSOLUTE\n";
355 continue;
356 }
357 if (indirect_symbol == MachO::INDIRECT_SYMBOL_ABS) {
358 outs() << "ABSOLUTE\n";
359 continue;
360 }
361 outs() << format("%5u ", indirect_symbol);
Kevin Enderbyf0640752015-03-13 17:56:32 +0000362 if (verbose) {
363 MachO::symtab_command Symtab = O->getSymtabLoadCommand();
364 if (indirect_symbol < Symtab.nsyms) {
365 symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol);
366 SymbolRef Symbol = *Sym;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000367 Expected<StringRef> SymName = Symbol.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +0000368 if (!SymName)
369 report_error(O->getFileName(), SymName.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000370 outs() << *SymName;
Kevin Enderbyf0640752015-03-13 17:56:32 +0000371 } else {
372 outs() << "?";
373 }
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000374 }
375 outs() << "\n";
376 }
377}
378
379static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) {
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000380 for (const auto &Load : O->load_commands()) {
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000381 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
382 MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
383 for (unsigned J = 0; J < Seg.nsects; ++J) {
384 MachO::section_64 Sec = O->getSection64(Load, J);
385 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
386 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
387 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
388 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
389 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
390 section_type == MachO::S_SYMBOL_STUBS) {
391 uint32_t stride;
392 if (section_type == MachO::S_SYMBOL_STUBS)
393 stride = Sec.reserved2;
394 else
395 stride = 8;
396 if (stride == 0) {
397 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
398 << Sec.sectname << ") "
399 << "(size of stubs in reserved2 field is zero)\n";
400 continue;
401 }
402 uint32_t count = Sec.size / stride;
403 outs() << "Indirect symbols for (" << Sec.segname << ","
404 << Sec.sectname << ") " << count << " entries";
405 uint32_t n = Sec.reserved1;
406 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
407 }
408 }
409 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
410 MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
411 for (unsigned J = 0; J < Seg.nsects; ++J) {
412 MachO::section Sec = O->getSection(Load, J);
413 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
414 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
415 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
416 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
417 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
418 section_type == MachO::S_SYMBOL_STUBS) {
419 uint32_t stride;
420 if (section_type == MachO::S_SYMBOL_STUBS)
421 stride = Sec.reserved2;
422 else
423 stride = 4;
424 if (stride == 0) {
425 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
426 << Sec.sectname << ") "
427 << "(size of stubs in reserved2 field is zero)\n";
428 continue;
429 }
430 uint32_t count = Sec.size / stride;
431 outs() << "Indirect symbols for (" << Sec.segname << ","
432 << Sec.sectname << ") " << count << " entries";
433 uint32_t n = Sec.reserved1;
434 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
435 }
436 }
437 }
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000438 }
439}
440
Kevin Enderby69fe98d2015-01-23 18:52:17 +0000441static void PrintDataInCodeTable(MachOObjectFile *O, bool verbose) {
442 MachO::linkedit_data_command DIC = O->getDataInCodeLoadCommand();
443 uint32_t nentries = DIC.datasize / sizeof(struct MachO::data_in_code_entry);
444 outs() << "Data in code table (" << nentries << " entries)\n";
445 outs() << "offset length kind\n";
446 for (dice_iterator DI = O->begin_dices(), DE = O->end_dices(); DI != DE;
447 ++DI) {
448 uint32_t Offset;
449 DI->getOffset(Offset);
450 outs() << format("0x%08" PRIx32, Offset) << " ";
451 uint16_t Length;
452 DI->getLength(Length);
453 outs() << format("%6u", Length) << " ";
454 uint16_t Kind;
455 DI->getKind(Kind);
456 if (verbose) {
457 switch (Kind) {
458 case MachO::DICE_KIND_DATA:
459 outs() << "DATA";
460 break;
461 case MachO::DICE_KIND_JUMP_TABLE8:
462 outs() << "JUMP_TABLE8";
463 break;
464 case MachO::DICE_KIND_JUMP_TABLE16:
465 outs() << "JUMP_TABLE16";
466 break;
467 case MachO::DICE_KIND_JUMP_TABLE32:
468 outs() << "JUMP_TABLE32";
469 break;
470 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
471 outs() << "ABS_JUMP_TABLE32";
472 break;
473 default:
474 outs() << format("0x%04" PRIx32, Kind);
475 break;
476 }
477 } else
478 outs() << format("0x%04" PRIx32, Kind);
479 outs() << "\n";
480 }
481}
482
Kevin Enderby9a509442015-01-27 21:28:24 +0000483static void PrintLinkOptHints(MachOObjectFile *O) {
484 MachO::linkedit_data_command LohLC = O->getLinkOptHintsLoadCommand();
485 const char *loh = O->getData().substr(LohLC.dataoff, 1).data();
486 uint32_t nloh = LohLC.datasize;
487 outs() << "Linker optimiztion hints (" << nloh << " total bytes)\n";
488 for (uint32_t i = 0; i < nloh;) {
489 unsigned n;
490 uint64_t identifier = decodeULEB128((const uint8_t *)(loh + i), &n);
491 i += n;
492 outs() << " identifier " << identifier << " ";
493 if (i >= nloh)
494 return;
495 switch (identifier) {
496 case 1:
497 outs() << "AdrpAdrp\n";
498 break;
499 case 2:
500 outs() << "AdrpLdr\n";
501 break;
502 case 3:
503 outs() << "AdrpAddLdr\n";
504 break;
505 case 4:
506 outs() << "AdrpLdrGotLdr\n";
507 break;
508 case 5:
509 outs() << "AdrpAddStr\n";
510 break;
511 case 6:
512 outs() << "AdrpLdrGotStr\n";
513 break;
514 case 7:
515 outs() << "AdrpAdd\n";
516 break;
517 case 8:
518 outs() << "AdrpLdrGot\n";
519 break;
520 default:
521 outs() << "Unknown identifier value\n";
522 break;
523 }
524 uint64_t narguments = decodeULEB128((const uint8_t *)(loh + i), &n);
525 i += n;
526 outs() << " narguments " << narguments << "\n";
527 if (i >= nloh)
528 return;
529
530 for (uint32_t j = 0; j < narguments; j++) {
531 uint64_t value = decodeULEB128((const uint8_t *)(loh + i), &n);
532 i += n;
533 outs() << "\tvalue " << format("0x%" PRIx64, value) << "\n";
534 if (i >= nloh)
535 return;
536 }
537 }
538}
539
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000540static void PrintDylibs(MachOObjectFile *O, bool JustId) {
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000541 unsigned Index = 0;
542 for (const auto &Load : O->load_commands()) {
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000543 if ((JustId && Load.C.cmd == MachO::LC_ID_DYLIB) ||
544 (!JustId && (Load.C.cmd == MachO::LC_ID_DYLIB ||
545 Load.C.cmd == MachO::LC_LOAD_DYLIB ||
546 Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
547 Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
548 Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
549 Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB))) {
550 MachO::dylib_command dl = O->getDylibIDLoadCommand(Load);
551 if (dl.dylib.name < dl.cmdsize) {
552 const char *p = (const char *)(Load.Ptr) + dl.dylib.name;
553 if (JustId)
554 outs() << p << "\n";
555 else {
556 outs() << "\t" << p;
557 outs() << " (compatibility version "
558 << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
559 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
560 << (dl.dylib.compatibility_version & 0xff) << ",";
561 outs() << " current version "
562 << ((dl.dylib.current_version >> 16) & 0xffff) << "."
563 << ((dl.dylib.current_version >> 8) & 0xff) << "."
564 << (dl.dylib.current_version & 0xff) << ")\n";
565 }
566 } else {
567 outs() << "\tBad offset (" << dl.dylib.name << ") for name of ";
568 if (Load.C.cmd == MachO::LC_ID_DYLIB)
569 outs() << "LC_ID_DYLIB ";
570 else if (Load.C.cmd == MachO::LC_LOAD_DYLIB)
571 outs() << "LC_LOAD_DYLIB ";
572 else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB)
573 outs() << "LC_LOAD_WEAK_DYLIB ";
574 else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB)
575 outs() << "LC_LAZY_LOAD_DYLIB ";
576 else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB)
577 outs() << "LC_REEXPORT_DYLIB ";
578 else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
579 outs() << "LC_LOAD_UPWARD_DYLIB ";
580 else
581 outs() << "LC_??? ";
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000582 outs() << "command " << Index++ << "\n";
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000583 }
584 }
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000585 }
586}
587
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000588typedef DenseMap<uint64_t, StringRef> SymbolAddressMap;
589
590static void CreateSymbolAddressMap(MachOObjectFile *O,
591 SymbolAddressMap *AddrMap) {
592 // Create a map of symbol addresses to symbol names.
593 for (const SymbolRef &Symbol : O->symbols()) {
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000594 Expected<SymbolRef::Type> STOrErr = Symbol.getType();
Kevin Enderby844c4ac2016-11-15 23:07:41 +0000595 if (!STOrErr)
596 report_error(O->getFileName(), STOrErr.takeError());
Kevin Enderby5afbc1c2016-03-23 20:27:00 +0000597 SymbolRef::Type ST = *STOrErr;
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000598 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
599 ST == SymbolRef::ST_Other) {
Rafael Espindoladea00162015-07-03 17:44:18 +0000600 uint64_t Address = Symbol.getValue();
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000601 Expected<StringRef> SymNameOrErr = Symbol.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +0000602 if (!SymNameOrErr)
603 report_error(O->getFileName(), SymNameOrErr.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000604 StringRef SymName = *SymNameOrErr;
Kevin Enderby846c0002015-04-16 17:19:59 +0000605 if (!SymName.startswith(".objc"))
606 (*AddrMap)[Address] = SymName;
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000607 }
608 }
609}
610
611// GuessSymbolName is passed the address of what might be a symbol and a
612// pointer to the SymbolAddressMap. It returns the name of a symbol
613// with that address or nullptr if no symbol is found with that address.
614static const char *GuessSymbolName(uint64_t value, SymbolAddressMap *AddrMap) {
615 const char *SymbolName = nullptr;
616 // A DenseMap can't lookup up some values.
617 if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) {
618 StringRef name = AddrMap->lookup(value);
619 if (!name.empty())
620 SymbolName = name.data();
621 }
622 return SymbolName;
623}
624
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000625static void DumpCstringChar(const char c) {
626 char p[2];
627 p[0] = c;
628 p[1] = '\0';
629 outs().write_escaped(p);
630}
631
Kevin Enderby10ba0412015-02-04 21:38:42 +0000632static void DumpCstringSection(MachOObjectFile *O, const char *sect,
633 uint32_t sect_size, uint64_t sect_addr,
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000634 bool print_addresses) {
Kevin Enderby10ba0412015-02-04 21:38:42 +0000635 for (uint32_t i = 0; i < sect_size; i++) {
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000636 if (print_addresses) {
637 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000638 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000639 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000640 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000641 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000642 for (; i < sect_size && sect[i] != '\0'; i++)
643 DumpCstringChar(sect[i]);
Kevin Enderby10ba0412015-02-04 21:38:42 +0000644 if (i < sect_size && sect[i] == '\0')
645 outs() << "\n";
646 }
647}
648
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000649static void DumpLiteral4(uint32_t l, float f) {
650 outs() << format("0x%08" PRIx32, l);
651 if ((l & 0x7f800000) != 0x7f800000)
652 outs() << format(" (%.16e)\n", f);
653 else {
654 if (l == 0x7f800000)
655 outs() << " (+Infinity)\n";
656 else if (l == 0xff800000)
657 outs() << " (-Infinity)\n";
658 else if ((l & 0x00400000) == 0x00400000)
659 outs() << " (non-signaling Not-a-Number)\n";
660 else
661 outs() << " (signaling Not-a-Number)\n";
662 }
663}
664
665static void DumpLiteral4Section(MachOObjectFile *O, const char *sect,
666 uint32_t sect_size, uint64_t sect_addr,
667 bool print_addresses) {
668 for (uint32_t i = 0; i < sect_size; i += sizeof(float)) {
669 if (print_addresses) {
670 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000671 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000672 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000673 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000674 }
675 float f;
676 memcpy(&f, sect + i, sizeof(float));
677 if (O->isLittleEndian() != sys::IsLittleEndianHost)
678 sys::swapByteOrder(f);
679 uint32_t l;
680 memcpy(&l, sect + i, sizeof(uint32_t));
681 if (O->isLittleEndian() != sys::IsLittleEndianHost)
682 sys::swapByteOrder(l);
683 DumpLiteral4(l, f);
684 }
685}
686
687static void DumpLiteral8(MachOObjectFile *O, uint32_t l0, uint32_t l1,
688 double d) {
689 outs() << format("0x%08" PRIx32, l0) << " " << format("0x%08" PRIx32, l1);
690 uint32_t Hi, Lo;
Davide Italianob627d9f2015-12-12 21:50:11 +0000691 Hi = (O->isLittleEndian()) ? l1 : l0;
692 Lo = (O->isLittleEndian()) ? l0 : l1;
693
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000694 // Hi is the high word, so this is equivalent to if(isfinite(d))
695 if ((Hi & 0x7ff00000) != 0x7ff00000)
696 outs() << format(" (%.16e)\n", d);
697 else {
698 if (Hi == 0x7ff00000 && Lo == 0)
699 outs() << " (+Infinity)\n";
700 else if (Hi == 0xfff00000 && Lo == 0)
701 outs() << " (-Infinity)\n";
702 else if ((Hi & 0x00080000) == 0x00080000)
703 outs() << " (non-signaling Not-a-Number)\n";
704 else
705 outs() << " (signaling Not-a-Number)\n";
706 }
707}
708
709static void DumpLiteral8Section(MachOObjectFile *O, const char *sect,
710 uint32_t sect_size, uint64_t sect_addr,
711 bool print_addresses) {
712 for (uint32_t i = 0; i < sect_size; i += sizeof(double)) {
713 if (print_addresses) {
714 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000715 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000716 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000717 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000718 }
719 double d;
720 memcpy(&d, sect + i, sizeof(double));
721 if (O->isLittleEndian() != sys::IsLittleEndianHost)
722 sys::swapByteOrder(d);
723 uint32_t l0, l1;
724 memcpy(&l0, sect + i, sizeof(uint32_t));
725 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
726 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
727 sys::swapByteOrder(l0);
728 sys::swapByteOrder(l1);
729 }
730 DumpLiteral8(O, l0, l1, d);
731 }
732}
733
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000734static void DumpLiteral16(uint32_t l0, uint32_t l1, uint32_t l2, uint32_t l3) {
735 outs() << format("0x%08" PRIx32, l0) << " ";
736 outs() << format("0x%08" PRIx32, l1) << " ";
737 outs() << format("0x%08" PRIx32, l2) << " ";
738 outs() << format("0x%08" PRIx32, l3) << "\n";
739}
740
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000741static void DumpLiteral16Section(MachOObjectFile *O, const char *sect,
742 uint32_t sect_size, uint64_t sect_addr,
743 bool print_addresses) {
744 for (uint32_t i = 0; i < sect_size; i += 16) {
745 if (print_addresses) {
746 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000747 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000748 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000749 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000750 }
751 uint32_t l0, l1, l2, l3;
752 memcpy(&l0, sect + i, sizeof(uint32_t));
753 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
754 memcpy(&l2, sect + i + 2 * sizeof(uint32_t), sizeof(uint32_t));
755 memcpy(&l3, sect + i + 3 * sizeof(uint32_t), sizeof(uint32_t));
756 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
757 sys::swapByteOrder(l0);
758 sys::swapByteOrder(l1);
759 sys::swapByteOrder(l2);
760 sys::swapByteOrder(l3);
761 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000762 DumpLiteral16(l0, l1, l2, l3);
763 }
764}
765
766static void DumpLiteralPointerSection(MachOObjectFile *O,
767 const SectionRef &Section,
768 const char *sect, uint32_t sect_size,
769 uint64_t sect_addr,
770 bool print_addresses) {
771 // Collect the literal sections in this Mach-O file.
772 std::vector<SectionRef> LiteralSections;
773 for (const SectionRef &Section : O->sections()) {
774 DataRefImpl Ref = Section.getRawDataRefImpl();
775 uint32_t section_type;
776 if (O->is64Bit()) {
777 const MachO::section_64 Sec = O->getSection64(Ref);
778 section_type = Sec.flags & MachO::SECTION_TYPE;
779 } else {
780 const MachO::section Sec = O->getSection(Ref);
781 section_type = Sec.flags & MachO::SECTION_TYPE;
782 }
783 if (section_type == MachO::S_CSTRING_LITERALS ||
784 section_type == MachO::S_4BYTE_LITERALS ||
785 section_type == MachO::S_8BYTE_LITERALS ||
786 section_type == MachO::S_16BYTE_LITERALS)
787 LiteralSections.push_back(Section);
788 }
789
790 // Set the size of the literal pointer.
791 uint32_t lp_size = O->is64Bit() ? 8 : 4;
792
Eric Christopher572e03a2015-06-19 01:53:21 +0000793 // Collect the external relocation symbols for the literal pointers.
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000794 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
795 for (const RelocationRef &Reloc : Section.relocations()) {
796 DataRefImpl Rel;
797 MachO::any_relocation_info RE;
798 bool isExtern = false;
799 Rel = Reloc.getRawDataRefImpl();
800 RE = O->getRelocation(Rel);
801 isExtern = O->getPlainRelocationExternal(RE);
802 if (isExtern) {
Rafael Espindola96d071c2015-06-29 23:29:12 +0000803 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000804 symbol_iterator RelocSym = Reloc.getSymbol();
805 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
806 }
807 }
808 array_pod_sort(Relocs.begin(), Relocs.end());
809
810 // Dump each literal pointer.
811 for (uint32_t i = 0; i < sect_size; i += lp_size) {
812 if (print_addresses) {
813 if (O->is64Bit())
814 outs() << format("%016" PRIx64, sect_addr + i) << " ";
815 else
816 outs() << format("%08" PRIx64, sect_addr + i) << " ";
817 }
818 uint64_t lp;
819 if (O->is64Bit()) {
820 memcpy(&lp, sect + i, sizeof(uint64_t));
821 if (O->isLittleEndian() != sys::IsLittleEndianHost)
822 sys::swapByteOrder(lp);
823 } else {
824 uint32_t li;
825 memcpy(&li, sect + i, sizeof(uint32_t));
826 if (O->isLittleEndian() != sys::IsLittleEndianHost)
827 sys::swapByteOrder(li);
828 lp = li;
829 }
830
831 // First look for an external relocation entry for this literal pointer.
David Majnemer42531262016-08-12 03:55:06 +0000832 auto Reloc = find_if(Relocs, [&](const std::pair<uint64_t, SymbolRef> &P) {
833 return P.first == i;
834 });
David Blaikie33dd45d02015-03-23 18:39:02 +0000835 if (Reloc != Relocs.end()) {
836 symbol_iterator RelocSym = Reloc->second;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000837 Expected<StringRef> SymName = RelocSym->getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +0000838 if (!SymName)
839 report_error(O->getFileName(), SymName.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000840 outs() << "external relocation entry for symbol:" << *SymName << "\n";
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000841 continue;
David Blaikie33dd45d02015-03-23 18:39:02 +0000842 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000843
844 // For local references see what the section the literal pointer points to.
David Majnemer562e8292016-08-12 00:18:03 +0000845 auto Sect = find_if(LiteralSections, [&](const SectionRef &R) {
846 return lp >= R.getAddress() && lp < R.getAddress() + R.getSize();
847 });
David Blaikie33dd45d02015-03-23 18:39:02 +0000848 if (Sect == LiteralSections.end()) {
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000849 outs() << format("0x%" PRIx64, lp) << " (not in a literal section)\n";
David Blaikie33dd45d02015-03-23 18:39:02 +0000850 continue;
851 }
852
853 uint64_t SectAddress = Sect->getAddress();
854 uint64_t SectSize = Sect->getSize();
855
856 StringRef SectName;
857 Sect->getName(SectName);
858 DataRefImpl Ref = Sect->getRawDataRefImpl();
859 StringRef SegmentName = O->getSectionFinalSegmentName(Ref);
860 outs() << SegmentName << ":" << SectName << ":";
861
862 uint32_t section_type;
863 if (O->is64Bit()) {
864 const MachO::section_64 Sec = O->getSection64(Ref);
865 section_type = Sec.flags & MachO::SECTION_TYPE;
866 } else {
867 const MachO::section Sec = O->getSection(Ref);
868 section_type = Sec.flags & MachO::SECTION_TYPE;
869 }
870
871 StringRef BytesStr;
872 Sect->getContents(BytesStr);
873 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
874
875 switch (section_type) {
876 case MachO::S_CSTRING_LITERALS:
877 for (uint64_t i = lp - SectAddress; i < SectSize && Contents[i] != '\0';
878 i++) {
879 DumpCstringChar(Contents[i]);
880 }
881 outs() << "\n";
882 break;
883 case MachO::S_4BYTE_LITERALS:
884 float f;
885 memcpy(&f, Contents + (lp - SectAddress), sizeof(float));
886 uint32_t l;
887 memcpy(&l, Contents + (lp - SectAddress), sizeof(uint32_t));
888 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
889 sys::swapByteOrder(f);
890 sys::swapByteOrder(l);
891 }
892 DumpLiteral4(l, f);
893 break;
894 case MachO::S_8BYTE_LITERALS: {
895 double d;
896 memcpy(&d, Contents + (lp - SectAddress), sizeof(double));
897 uint32_t l0, l1;
898 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
899 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
900 sizeof(uint32_t));
901 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
902 sys::swapByteOrder(f);
903 sys::swapByteOrder(l0);
904 sys::swapByteOrder(l1);
905 }
906 DumpLiteral8(O, l0, l1, d);
907 break;
908 }
909 case MachO::S_16BYTE_LITERALS: {
910 uint32_t l0, l1, l2, l3;
911 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
912 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
913 sizeof(uint32_t));
914 memcpy(&l2, Contents + (lp - SectAddress) + 2 * sizeof(uint32_t),
915 sizeof(uint32_t));
916 memcpy(&l3, Contents + (lp - SectAddress) + 3 * sizeof(uint32_t),
917 sizeof(uint32_t));
918 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
919 sys::swapByteOrder(l0);
920 sys::swapByteOrder(l1);
921 sys::swapByteOrder(l2);
922 sys::swapByteOrder(l3);
923 }
924 DumpLiteral16(l0, l1, l2, l3);
925 break;
926 }
927 }
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000928 }
929}
930
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000931static void DumpInitTermPointerSection(MachOObjectFile *O, const char *sect,
932 uint32_t sect_size, uint64_t sect_addr,
933 SymbolAddressMap *AddrMap,
934 bool verbose) {
935 uint32_t stride;
Davide Italiano3eb47e22015-12-15 23:14:21 +0000936 stride = (O->is64Bit()) ? sizeof(uint64_t) : sizeof(uint32_t);
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000937 for (uint32_t i = 0; i < sect_size; i += stride) {
938 const char *SymbolName = nullptr;
939 if (O->is64Bit()) {
940 outs() << format("0x%016" PRIx64, sect_addr + i * stride) << " ";
941 uint64_t pointer_value;
942 memcpy(&pointer_value, sect + i, stride);
943 if (O->isLittleEndian() != sys::IsLittleEndianHost)
944 sys::swapByteOrder(pointer_value);
945 outs() << format("0x%016" PRIx64, pointer_value);
946 if (verbose)
947 SymbolName = GuessSymbolName(pointer_value, AddrMap);
948 } else {
949 outs() << format("0x%08" PRIx64, sect_addr + i * stride) << " ";
950 uint32_t pointer_value;
951 memcpy(&pointer_value, sect + i, stride);
952 if (O->isLittleEndian() != sys::IsLittleEndianHost)
953 sys::swapByteOrder(pointer_value);
954 outs() << format("0x%08" PRIx32, pointer_value);
955 if (verbose)
956 SymbolName = GuessSymbolName(pointer_value, AddrMap);
957 }
958 if (SymbolName)
959 outs() << " " << SymbolName;
960 outs() << "\n";
961 }
962}
963
964static void DumpRawSectionContents(MachOObjectFile *O, const char *sect,
965 uint32_t size, uint64_t addr) {
966 uint32_t cputype = O->getHeader().cputype;
967 if (cputype == MachO::CPU_TYPE_I386 || cputype == MachO::CPU_TYPE_X86_64) {
968 uint32_t j;
969 for (uint32_t i = 0; i < size; i += j, addr += j) {
970 if (O->is64Bit())
971 outs() << format("%016" PRIx64, addr) << "\t";
972 else
Kevin Enderbyf0640752015-03-13 17:56:32 +0000973 outs() << format("%08" PRIx64, addr) << "\t";
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000974 for (j = 0; j < 16 && i + j < size; j++) {
975 uint8_t byte_word = *(sect + i + j);
976 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
977 }
978 outs() << "\n";
979 }
980 } else {
981 uint32_t j;
982 for (uint32_t i = 0; i < size; i += j, addr += j) {
983 if (O->is64Bit())
984 outs() << format("%016" PRIx64, addr) << "\t";
985 else
Kevin Enderbyc4930852016-04-27 22:36:18 +0000986 outs() << format("%08" PRIx64, addr) << "\t";
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000987 for (j = 0; j < 4 * sizeof(int32_t) && i + j < size;
988 j += sizeof(int32_t)) {
Kevin Enderby8eccdad2016-04-27 23:43:00 +0000989 if (i + j + sizeof(int32_t) <= size) {
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000990 uint32_t long_word;
991 memcpy(&long_word, sect + i + j, sizeof(int32_t));
992 if (O->isLittleEndian() != sys::IsLittleEndianHost)
993 sys::swapByteOrder(long_word);
994 outs() << format("%08" PRIx32, long_word) << " ";
995 } else {
996 for (uint32_t k = 0; i + j + k < size; k++) {
Kevin Enderby8eccdad2016-04-27 23:43:00 +0000997 uint8_t byte_word = *(sect + i + j + k);
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000998 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
999 }
1000 }
1001 }
1002 outs() << "\n";
1003 }
1004 }
1005}
1006
Kevin Enderby95df54c2015-02-04 01:01:38 +00001007static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
1008 StringRef DisSegName, StringRef DisSectName);
Kevin Enderby4ad9bde2015-04-16 22:33:20 +00001009static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
1010 uint32_t size, uint32_t addr);
Kevin Enderby9873e2c2016-05-23 21:34:12 +00001011#ifdef HAVE_LIBXAR
1012static void DumpBitcodeSection(MachOObjectFile *O, const char *sect,
1013 uint32_t size, bool verbose,
1014 bool PrintXarHeader, bool PrintXarFileHeaders,
1015 std::string XarMemberName);
1016#endif // defined(HAVE_LIBXAR)
Kevin Enderby95df54c2015-02-04 01:01:38 +00001017
1018static void DumpSectionContents(StringRef Filename, MachOObjectFile *O,
1019 bool verbose) {
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001020 SymbolAddressMap AddrMap;
1021 if (verbose)
1022 CreateSymbolAddressMap(O, &AddrMap);
1023
Colin LeMahieufcc32762015-07-29 19:08:10 +00001024 for (unsigned i = 0; i < FilterSections.size(); ++i) {
1025 StringRef DumpSection = FilterSections[i];
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001026 std::pair<StringRef, StringRef> DumpSegSectName;
1027 DumpSegSectName = DumpSection.split(',');
1028 StringRef DumpSegName, DumpSectName;
1029 if (DumpSegSectName.second.size()) {
1030 DumpSegName = DumpSegSectName.first;
1031 DumpSectName = DumpSegSectName.second;
1032 } else {
1033 DumpSegName = "";
1034 DumpSectName = DumpSegSectName.first;
1035 }
1036 for (const SectionRef &Section : O->sections()) {
1037 StringRef SectName;
1038 Section.getName(SectName);
1039 DataRefImpl Ref = Section.getRawDataRefImpl();
1040 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1041 if ((DumpSegName.empty() || SegName == DumpSegName) &&
1042 (SectName == DumpSectName)) {
Adrian Prantlc2401dd2015-03-27 17:31:15 +00001043
Kevin Enderby95df54c2015-02-04 01:01:38 +00001044 uint32_t section_flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001045 if (O->is64Bit()) {
1046 const MachO::section_64 Sec = O->getSection64(Ref);
Kevin Enderby95df54c2015-02-04 01:01:38 +00001047 section_flags = Sec.flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001048
1049 } else {
1050 const MachO::section Sec = O->getSection(Ref);
Kevin Enderby95df54c2015-02-04 01:01:38 +00001051 section_flags = Sec.flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001052 }
Kevin Enderby95df54c2015-02-04 01:01:38 +00001053 uint32_t section_type = section_flags & MachO::SECTION_TYPE;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001054
1055 StringRef BytesStr;
1056 Section.getContents(BytesStr);
1057 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1058 uint32_t sect_size = BytesStr.size();
1059 uint64_t sect_addr = Section.getAddress();
1060
Adrian Prantlc2401dd2015-03-27 17:31:15 +00001061 outs() << "Contents of (" << SegName << "," << SectName
1062 << ") section\n";
1063
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001064 if (verbose) {
Kevin Enderby95df54c2015-02-04 01:01:38 +00001065 if ((section_flags & MachO::S_ATTR_PURE_INSTRUCTIONS) ||
1066 (section_flags & MachO::S_ATTR_SOME_INSTRUCTIONS)) {
1067 DisassembleMachO(Filename, O, SegName, SectName);
1068 continue;
1069 }
Kevin Enderbycd66be52015-03-11 22:06:32 +00001070 if (SegName == "__TEXT" && SectName == "__info_plist") {
1071 outs() << sect;
1072 continue;
1073 }
Kevin Enderby4ad9bde2015-04-16 22:33:20 +00001074 if (SegName == "__OBJC" && SectName == "__protocol") {
1075 DumpProtocolSection(O, sect, sect_size, sect_addr);
1076 continue;
1077 }
Kevin Enderby9873e2c2016-05-23 21:34:12 +00001078#ifdef HAVE_LIBXAR
1079 if (SegName == "__LLVM" && SectName == "__bundle") {
1080 DumpBitcodeSection(O, sect, sect_size, verbose, !NoSymbolicOperands,
1081 ArchiveHeaders, "");
1082 continue;
1083 }
1084#endif // defined(HAVE_LIBXAR)
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001085 switch (section_type) {
1086 case MachO::S_REGULAR:
1087 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1088 break;
1089 case MachO::S_ZEROFILL:
1090 outs() << "zerofill section and has no contents in the file\n";
1091 break;
Kevin Enderby10ba0412015-02-04 21:38:42 +00001092 case MachO::S_CSTRING_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001093 DumpCstringSection(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby10ba0412015-02-04 21:38:42 +00001094 break;
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001095 case MachO::S_4BYTE_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001096 DumpLiteral4Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001097 break;
1098 case MachO::S_8BYTE_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001099 DumpLiteral8Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001100 break;
1101 case MachO::S_16BYTE_LITERALS:
Kevin Enderby0fc11822015-04-01 20:57:01 +00001102 DumpLiteral16Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1103 break;
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001104 case MachO::S_LITERAL_POINTERS:
1105 DumpLiteralPointerSection(O, Section, sect, sect_size, sect_addr,
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001106 !NoLeadingAddr);
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001107 break;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001108 case MachO::S_MOD_INIT_FUNC_POINTERS:
1109 case MachO::S_MOD_TERM_FUNC_POINTERS:
1110 DumpInitTermPointerSection(O, sect, sect_size, sect_addr, &AddrMap,
1111 verbose);
1112 break;
1113 default:
1114 outs() << "Unknown section type ("
1115 << format("0x%08" PRIx32, section_type) << ")\n";
1116 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1117 break;
1118 }
1119 } else {
1120 if (section_type == MachO::S_ZEROFILL)
1121 outs() << "zerofill section and has no contents in the file\n";
1122 else
1123 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1124 }
1125 }
1126 }
1127 }
1128}
1129
Kevin Enderbycd66be52015-03-11 22:06:32 +00001130static void DumpInfoPlistSectionContents(StringRef Filename,
1131 MachOObjectFile *O) {
1132 for (const SectionRef &Section : O->sections()) {
1133 StringRef SectName;
1134 Section.getName(SectName);
1135 DataRefImpl Ref = Section.getRawDataRefImpl();
1136 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1137 if (SegName == "__TEXT" && SectName == "__info_plist") {
Kevin Enderby30cf2e82017-06-20 21:00:25 +00001138 if (!NoLeadingHeaders)
1139 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
Kevin Enderbycd66be52015-03-11 22:06:32 +00001140 StringRef BytesStr;
1141 Section.getContents(BytesStr);
1142 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
Kevin Enderby418659f2017-02-06 21:01:08 +00001143 outs() << format("%.*s", BytesStr.size(), sect) << "\n";
Kevin Enderbycd66be52015-03-11 22:06:32 +00001144 return;
1145 }
1146 }
1147}
1148
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001149// checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file
1150// and if it is and there is a list of architecture flags is specified then
1151// check to make sure this Mach-O file is one of those architectures or all
1152// architectures were specified. If not then an error is generated and this
1153// routine returns false. Else it returns true.
1154static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
David Majnemer91160d82016-10-31 17:11:31 +00001155 auto *MachO = dyn_cast<MachOObjectFile>(O);
1156
1157 if (!MachO || ArchAll || ArchFlags.empty())
1158 return true;
1159
1160 MachO::mach_header H;
1161 MachO::mach_header_64 H_64;
1162 Triple T;
Kevin Enderby59343a92016-12-16 22:54:02 +00001163 const char *McpuDefault, *ArchFlag;
David Majnemer91160d82016-10-31 17:11:31 +00001164 if (MachO->is64Bit()) {
1165 H_64 = MachO->MachOObjectFile::getHeader64();
Kevin Enderby59343a92016-12-16 22:54:02 +00001166 T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype,
1167 &McpuDefault, &ArchFlag);
David Majnemer91160d82016-10-31 17:11:31 +00001168 } else {
1169 H = MachO->MachOObjectFile::getHeader();
Kevin Enderby59343a92016-12-16 22:54:02 +00001170 T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype,
1171 &McpuDefault, &ArchFlag);
David Majnemer91160d82016-10-31 17:11:31 +00001172 }
Kevin Enderby59343a92016-12-16 22:54:02 +00001173 const std::string ArchFlagName(ArchFlag);
David Majnemer91160d82016-10-31 17:11:31 +00001174 if (none_of(ArchFlags, [&](const std::string &Name) {
Kevin Enderby59343a92016-12-16 22:54:02 +00001175 return Name == ArchFlagName;
David Majnemer91160d82016-10-31 17:11:31 +00001176 })) {
1177 errs() << "llvm-objdump: " + Filename + ": No architecture specified.\n";
1178 return false;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001179 }
1180 return true;
1181}
1182
Kevin Enderby0fc11822015-04-01 20:57:01 +00001183static void printObjcMetaData(MachOObjectFile *O, bool verbose);
1184
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001185// ProcessMachO() is passed a single opened Mach-O file, which may be an
1186// archive member and or in a slice of a universal file. It prints the
1187// the file name and header info and then processes it according to the
1188// command line options.
Kevin Enderby844c4ac2016-11-15 23:07:41 +00001189static void ProcessMachO(StringRef Name, MachOObjectFile *MachOOF,
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001190 StringRef ArchiveMemberName = StringRef(),
1191 StringRef ArchitectureName = StringRef()) {
Kevin Enderby131d1772015-01-09 19:22:37 +00001192 // If we are doing some processing here on the Mach-O file print the header
1193 // info. And don't print it otherwise like in the case of printing the
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +00001194 // UniversalHeaders or ArchiveHeaders.
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001195 if (Disassemble || PrivateHeaders || ExportsTrie || Rebase || Bind || SymbolTable ||
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001196 LazyBind || WeakBind || IndirectSymbols || DataInCode || LinkOptHints ||
Colin LeMahieufcc32762015-07-29 19:08:10 +00001197 DylibsUsed || DylibId || ObjcMetaData || (FilterSections.size() != 0)) {
Kevin Enderbyf9d60f02016-11-29 21:43:40 +00001198 if (!NoLeadingHeaders) {
1199 outs() << Name;
1200 if (!ArchiveMemberName.empty())
1201 outs() << '(' << ArchiveMemberName << ')';
1202 if (!ArchitectureName.empty())
1203 outs() << " (architecture " << ArchitectureName << ")";
1204 outs() << ":\n";
1205 }
Kevin Enderby131d1772015-01-09 19:22:37 +00001206 }
Kevin Enderby844c4ac2016-11-15 23:07:41 +00001207 // To use the report_error() form with an ArchiveName and FileName set
1208 // these up based on what is passed for Name and ArchiveMemberName.
1209 StringRef ArchiveName;
1210 StringRef FileName;
1211 if (!ArchiveMemberName.empty()) {
1212 ArchiveName = Name;
1213 FileName = ArchiveMemberName;
1214 } else {
1215 ArchiveName = StringRef();
1216 FileName = Name;
1217 }
1218
1219 // If we need the symbol table to do the operation then check it here to
1220 // produce a good error message as to where the Mach-O file comes from in
1221 // the error message.
1222 if (Disassemble || IndirectSymbols || FilterSections.size() != 0 ||
1223 UnwindInfo)
1224 if (Error Err = MachOOF->checkSymbolTable())
1225 report_error(ArchiveName, FileName, std::move(Err), ArchitectureName);
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001226
Kevin Enderbyaf2999a2017-06-22 19:50:56 +00001227 if (Disassemble) {
1228 if (MachOOF->getHeader().filetype == MachO::MH_KEXT_BUNDLE &&
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +00001229 MachOOF->getHeader().cputype == MachO::CPU_TYPE_ARM64)
Kevin Enderbyaf2999a2017-06-22 19:50:56 +00001230 DisassembleMachO(FileName, MachOOF, "__TEXT_EXEC", "__text");
1231 else
1232 DisassembleMachO(FileName, MachOOF, "__TEXT", "__text");
1233 }
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +00001234 if (IndirectSymbols)
Kevin Enderbyf0640752015-03-13 17:56:32 +00001235 PrintIndirectSymbols(MachOOF, !NonVerbose);
Kevin Enderby69fe98d2015-01-23 18:52:17 +00001236 if (DataInCode)
Kevin Enderbyf0640752015-03-13 17:56:32 +00001237 PrintDataInCodeTable(MachOOF, !NonVerbose);
Kevin Enderby9a509442015-01-27 21:28:24 +00001238 if (LinkOptHints)
1239 PrintLinkOptHints(MachOOF);
Kevin Enderby98da6132015-01-20 21:47:46 +00001240 if (Relocations)
1241 PrintRelocations(MachOOF);
1242 if (SectionHeaders)
1243 PrintSectionHeaders(MachOOF);
1244 if (SectionContents)
1245 PrintSectionContents(MachOOF);
Colin LeMahieufcc32762015-07-29 19:08:10 +00001246 if (FilterSections.size() != 0)
Kevin Enderby844c4ac2016-11-15 23:07:41 +00001247 DumpSectionContents(FileName, MachOOF, !NonVerbose);
Kevin Enderbycd66be52015-03-11 22:06:32 +00001248 if (InfoPlist)
Kevin Enderby844c4ac2016-11-15 23:07:41 +00001249 DumpInfoPlistSectionContents(FileName, MachOOF);
Kevin Enderbybc847fa2015-03-16 20:08:09 +00001250 if (DylibsUsed)
1251 PrintDylibs(MachOOF, false);
1252 if (DylibId)
1253 PrintDylibs(MachOOF, true);
Kevin Enderby844c4ac2016-11-15 23:07:41 +00001254 if (SymbolTable)
Kevin Enderby9acb1092016-05-31 20:35:34 +00001255 PrintSymbolTable(MachOOF, ArchiveName, ArchitectureName);
Kevin Enderby98da6132015-01-20 21:47:46 +00001256 if (UnwindInfo)
1257 printMachOUnwindInfo(MachOOF);
Kevin Enderby0ae163f2016-01-13 00:25:36 +00001258 if (PrivateHeaders) {
1259 printMachOFileHeader(MachOOF);
1260 printMachOLoadCommands(MachOOF);
1261 }
1262 if (FirstPrivateHeader)
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001263 printMachOFileHeader(MachOOF);
Kevin Enderby0fc11822015-04-01 20:57:01 +00001264 if (ObjcMetaData)
1265 printObjcMetaData(MachOOF, !NonVerbose);
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001266 if (ExportsTrie)
1267 printExportsTrie(MachOOF);
1268 if (Rebase)
1269 printRebaseTable(MachOOF);
1270 if (Bind)
1271 printBindTable(MachOOF);
1272 if (LazyBind)
1273 printLazyBindTable(MachOOF);
1274 if (WeakBind)
1275 printWeakBindTable(MachOOF);
Igor Laevsky03a670c2016-01-26 15:09:42 +00001276
1277 if (DwarfDumpType != DIDT_Null) {
Rafael Espindolac398e672017-07-19 22:27:28 +00001278 std::unique_ptr<DIContext> DICtx = DWARFContext::create(*MachOOF);
Igor Laevsky03a670c2016-01-26 15:09:42 +00001279 // Dump the complete DWARF structure.
Adrian Prantlf4bc1f72017-06-01 18:18:23 +00001280 DIDumpOptions DumpOpts;
1281 DumpOpts.DumpType = DwarfDumpType;
Adrian Prantlf4bc1f72017-06-01 18:18:23 +00001282 DICtx->dump(outs(), DumpOpts);
Igor Laevsky03a670c2016-01-26 15:09:42 +00001283 }
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001284}
1285
Kevin Enderby131d1772015-01-09 19:22:37 +00001286// printUnknownCPUType() helps print_fat_headers for unknown CPU's.
1287static void printUnknownCPUType(uint32_t cputype, uint32_t cpusubtype) {
1288 outs() << " cputype (" << cputype << ")\n";
1289 outs() << " cpusubtype (" << cpusubtype << ")\n";
1290}
1291
1292// printCPUType() helps print_fat_headers by printing the cputype and
1293// pusubtype (symbolically for the one's it knows about).
1294static void printCPUType(uint32_t cputype, uint32_t cpusubtype) {
1295 switch (cputype) {
1296 case MachO::CPU_TYPE_I386:
1297 switch (cpusubtype) {
1298 case MachO::CPU_SUBTYPE_I386_ALL:
1299 outs() << " cputype CPU_TYPE_I386\n";
1300 outs() << " cpusubtype CPU_SUBTYPE_I386_ALL\n";
1301 break;
1302 default:
1303 printUnknownCPUType(cputype, cpusubtype);
1304 break;
1305 }
1306 break;
1307 case MachO::CPU_TYPE_X86_64:
1308 switch (cpusubtype) {
1309 case MachO::CPU_SUBTYPE_X86_64_ALL:
1310 outs() << " cputype CPU_TYPE_X86_64\n";
1311 outs() << " cpusubtype CPU_SUBTYPE_X86_64_ALL\n";
1312 break;
1313 case MachO::CPU_SUBTYPE_X86_64_H:
1314 outs() << " cputype CPU_TYPE_X86_64\n";
1315 outs() << " cpusubtype CPU_SUBTYPE_X86_64_H\n";
1316 break;
1317 default:
1318 printUnknownCPUType(cputype, cpusubtype);
1319 break;
1320 }
1321 break;
1322 case MachO::CPU_TYPE_ARM:
1323 switch (cpusubtype) {
1324 case MachO::CPU_SUBTYPE_ARM_ALL:
1325 outs() << " cputype CPU_TYPE_ARM\n";
1326 outs() << " cpusubtype CPU_SUBTYPE_ARM_ALL\n";
1327 break;
1328 case MachO::CPU_SUBTYPE_ARM_V4T:
1329 outs() << " cputype CPU_TYPE_ARM\n";
1330 outs() << " cpusubtype CPU_SUBTYPE_ARM_V4T\n";
1331 break;
1332 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
1333 outs() << " cputype CPU_TYPE_ARM\n";
1334 outs() << " cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n";
1335 break;
1336 case MachO::CPU_SUBTYPE_ARM_XSCALE:
1337 outs() << " cputype CPU_TYPE_ARM\n";
1338 outs() << " cpusubtype CPU_SUBTYPE_ARM_XSCALE\n";
1339 break;
1340 case MachO::CPU_SUBTYPE_ARM_V6:
1341 outs() << " cputype CPU_TYPE_ARM\n";
1342 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6\n";
1343 break;
1344 case MachO::CPU_SUBTYPE_ARM_V6M:
1345 outs() << " cputype CPU_TYPE_ARM\n";
1346 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6M\n";
1347 break;
1348 case MachO::CPU_SUBTYPE_ARM_V7:
1349 outs() << " cputype CPU_TYPE_ARM\n";
1350 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7\n";
1351 break;
1352 case MachO::CPU_SUBTYPE_ARM_V7EM:
1353 outs() << " cputype CPU_TYPE_ARM\n";
1354 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7EM\n";
1355 break;
1356 case MachO::CPU_SUBTYPE_ARM_V7K:
1357 outs() << " cputype CPU_TYPE_ARM\n";
1358 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7K\n";
1359 break;
1360 case MachO::CPU_SUBTYPE_ARM_V7M:
1361 outs() << " cputype CPU_TYPE_ARM\n";
1362 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7M\n";
1363 break;
1364 case MachO::CPU_SUBTYPE_ARM_V7S:
1365 outs() << " cputype CPU_TYPE_ARM\n";
1366 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7S\n";
1367 break;
1368 default:
1369 printUnknownCPUType(cputype, cpusubtype);
1370 break;
1371 }
1372 break;
1373 case MachO::CPU_TYPE_ARM64:
1374 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1375 case MachO::CPU_SUBTYPE_ARM64_ALL:
1376 outs() << " cputype CPU_TYPE_ARM64\n";
1377 outs() << " cpusubtype CPU_SUBTYPE_ARM64_ALL\n";
1378 break;
1379 default:
1380 printUnknownCPUType(cputype, cpusubtype);
1381 break;
1382 }
1383 break;
1384 default:
1385 printUnknownCPUType(cputype, cpusubtype);
1386 break;
1387 }
1388}
1389
1390static void printMachOUniversalHeaders(const object::MachOUniversalBinary *UB,
1391 bool verbose) {
1392 outs() << "Fat headers\n";
Kevin Enderby606a3382016-06-21 21:55:01 +00001393 if (verbose) {
1394 if (UB->getMagic() == MachO::FAT_MAGIC)
1395 outs() << "fat_magic FAT_MAGIC\n";
1396 else // UB->getMagic() == MachO::FAT_MAGIC_64
1397 outs() << "fat_magic FAT_MAGIC_64\n";
1398 } else
Kevin Enderby131d1772015-01-09 19:22:37 +00001399 outs() << "fat_magic " << format("0x%" PRIx32, MachO::FAT_MAGIC) << "\n";
1400
1401 uint32_t nfat_arch = UB->getNumberOfObjects();
1402 StringRef Buf = UB->getData();
1403 uint64_t size = Buf.size();
1404 uint64_t big_size = sizeof(struct MachO::fat_header) +
1405 nfat_arch * sizeof(struct MachO::fat_arch);
1406 outs() << "nfat_arch " << UB->getNumberOfObjects();
1407 if (nfat_arch == 0)
1408 outs() << " (malformed, contains zero architecture types)\n";
1409 else if (big_size > size)
1410 outs() << " (malformed, architectures past end of file)\n";
1411 else
1412 outs() << "\n";
1413
1414 for (uint32_t i = 0; i < nfat_arch; ++i) {
1415 MachOUniversalBinary::ObjectForArch OFA(UB, i);
1416 uint32_t cputype = OFA.getCPUType();
1417 uint32_t cpusubtype = OFA.getCPUSubType();
1418 outs() << "architecture ";
1419 for (uint32_t j = 0; i != 0 && j <= i - 1; j++) {
1420 MachOUniversalBinary::ObjectForArch other_OFA(UB, j);
1421 uint32_t other_cputype = other_OFA.getCPUType();
1422 uint32_t other_cpusubtype = other_OFA.getCPUSubType();
Kevin Enderby0512bd72015-01-09 21:55:03 +00001423 if (cputype != 0 && cpusubtype != 0 && cputype == other_cputype &&
Kevin Enderby131d1772015-01-09 19:22:37 +00001424 (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) ==
Kevin Enderby0512bd72015-01-09 21:55:03 +00001425 (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK)) {
Kevin Enderby131d1772015-01-09 19:22:37 +00001426 outs() << "(illegal duplicate architecture) ";
1427 break;
Kevin Enderby0512bd72015-01-09 21:55:03 +00001428 }
Kevin Enderby131d1772015-01-09 19:22:37 +00001429 }
1430 if (verbose) {
Kevin Enderby59343a92016-12-16 22:54:02 +00001431 outs() << OFA.getArchFlagName() << "\n";
Kevin Enderby131d1772015-01-09 19:22:37 +00001432 printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1433 } else {
1434 outs() << i << "\n";
1435 outs() << " cputype " << cputype << "\n";
1436 outs() << " cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK)
1437 << "\n";
1438 }
1439 if (verbose &&
1440 (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64)
1441 outs() << " capabilities CPU_SUBTYPE_LIB64\n";
1442 else
1443 outs() << " capabilities "
1444 << format("0x%" PRIx32,
1445 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n";
1446 outs() << " offset " << OFA.getOffset();
1447 if (OFA.getOffset() > size)
1448 outs() << " (past end of file)";
1449 if (OFA.getOffset() % (1 << OFA.getAlign()) != 0)
1450 outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")";
1451 outs() << "\n";
1452 outs() << " size " << OFA.getSize();
1453 big_size = OFA.getOffset() + OFA.getSize();
1454 if (big_size > size)
1455 outs() << " (past end of file)";
1456 outs() << "\n";
1457 outs() << " align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign())
1458 << ")\n";
1459 }
1460}
1461
Kevin Enderby6524bd82016-07-19 20:47:07 +00001462static void printArchiveChild(StringRef Filename, const Archive::Child &C,
1463 bool verbose, bool print_offset,
1464 StringRef ArchitectureName = StringRef()) {
Kevin Enderby13023a12015-01-15 23:19:11 +00001465 if (print_offset)
1466 outs() << C.getChildOffset() << "\t";
Vedant Kumar4031d9f2016-08-03 19:02:50 +00001467 Expected<sys::fs::perms> ModeOrErr = C.getAccessMode();
1468 if (!ModeOrErr)
1469 report_error(Filename, C, ModeOrErr.takeError(), ArchitectureName);
1470 sys::fs::perms Mode = ModeOrErr.get();
Kevin Enderby13023a12015-01-15 23:19:11 +00001471 if (verbose) {
1472 // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
1473 // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
1474 outs() << "-";
Davide Italianobb9a6cc2015-09-07 20:47:03 +00001475 outs() << ((Mode & sys::fs::owner_read) ? "r" : "-");
1476 outs() << ((Mode & sys::fs::owner_write) ? "w" : "-");
1477 outs() << ((Mode & sys::fs::owner_exe) ? "x" : "-");
1478 outs() << ((Mode & sys::fs::group_read) ? "r" : "-");
1479 outs() << ((Mode & sys::fs::group_write) ? "w" : "-");
1480 outs() << ((Mode & sys::fs::group_exe) ? "x" : "-");
1481 outs() << ((Mode & sys::fs::others_read) ? "r" : "-");
1482 outs() << ((Mode & sys::fs::others_write) ? "w" : "-");
1483 outs() << ((Mode & sys::fs::others_exe) ? "x" : "-");
Kevin Enderby13023a12015-01-15 23:19:11 +00001484 } else {
1485 outs() << format("0%o ", Mode);
1486 }
1487
Vedant Kumar4031d9f2016-08-03 19:02:50 +00001488 Expected<unsigned> UIDOrErr = C.getUID();
1489 if (!UIDOrErr)
1490 report_error(Filename, C, UIDOrErr.takeError(), ArchitectureName);
1491 unsigned UID = UIDOrErr.get();
Kevin Enderby13023a12015-01-15 23:19:11 +00001492 outs() << format("%3d/", UID);
Vedant Kumar4031d9f2016-08-03 19:02:50 +00001493 Expected<unsigned> GIDOrErr = C.getGID();
1494 if (!GIDOrErr)
1495 report_error(Filename, C, GIDOrErr.takeError(), ArchitectureName);
1496 unsigned GID = GIDOrErr.get();
Kevin Enderby13023a12015-01-15 23:19:11 +00001497 outs() << format("%-3d ", GID);
Kevin Enderby6524bd82016-07-19 20:47:07 +00001498 Expected<uint64_t> Size = C.getRawSize();
1499 if (!Size)
1500 report_error(Filename, C, Size.takeError(), ArchitectureName);
Kevin Enderby7a969422015-11-05 19:24:56 +00001501 outs() << format("%5" PRId64, Size.get()) << " ";
Kevin Enderby13023a12015-01-15 23:19:11 +00001502
1503 StringRef RawLastModified = C.getRawLastModified();
1504 if (verbose) {
1505 unsigned Seconds;
1506 if (RawLastModified.getAsInteger(10, Seconds))
Vedant Kumar4031d9f2016-08-03 19:02:50 +00001507 outs() << "(date: \"" << RawLastModified
1508 << "\" contains non-decimal chars) ";
Kevin Enderby13023a12015-01-15 23:19:11 +00001509 else {
1510 // Since cime(3) returns a 26 character string of the form:
1511 // "Sun Sep 16 01:03:52 1973\n\0"
1512 // just print 24 characters.
1513 time_t t = Seconds;
1514 outs() << format("%.24s ", ctime(&t));
1515 }
1516 } else {
1517 outs() << RawLastModified << " ";
1518 }
1519
1520 if (verbose) {
Kevin Enderbyf4586032016-07-29 17:44:13 +00001521 Expected<StringRef> NameOrErr = C.getName();
1522 if (!NameOrErr) {
1523 consumeError(NameOrErr.takeError());
1524 Expected<StringRef> NameOrErr = C.getRawName();
1525 if (!NameOrErr)
1526 report_error(Filename, C, NameOrErr.takeError(), ArchitectureName);
1527 StringRef RawName = NameOrErr.get();
Kevin Enderby13023a12015-01-15 23:19:11 +00001528 outs() << RawName << "\n";
1529 } else {
1530 StringRef Name = NameOrErr.get();
1531 outs() << Name << "\n";
1532 }
1533 } else {
Kevin Enderbyf4586032016-07-29 17:44:13 +00001534 Expected<StringRef> NameOrErr = C.getRawName();
1535 if (!NameOrErr)
1536 report_error(Filename, C, NameOrErr.takeError(), ArchitectureName);
1537 StringRef RawName = NameOrErr.get();
Kevin Enderby13023a12015-01-15 23:19:11 +00001538 outs() << RawName << "\n";
1539 }
1540}
1541
Kevin Enderby6524bd82016-07-19 20:47:07 +00001542static void printArchiveHeaders(StringRef Filename, Archive *A, bool verbose,
1543 bool print_offset,
1544 StringRef ArchitectureName = StringRef()) {
Mehdi Amini41af4302016-11-11 04:28:40 +00001545 Error Err = Error::success();
1546 ;
Lang Hamesfc209622016-07-14 02:24:01 +00001547 for (const auto &C : A->children(Err, false))
Kevin Enderby6524bd82016-07-19 20:47:07 +00001548 printArchiveChild(Filename, C, verbose, print_offset, ArchitectureName);
1549
Lang Hamesfc209622016-07-14 02:24:01 +00001550 if (Err)
Kevin Enderby844c4ac2016-11-15 23:07:41 +00001551 report_error(StringRef(), Filename, std::move(Err), ArchitectureName);
Kevin Enderby13023a12015-01-15 23:19:11 +00001552}
1553
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001554// ParseInputMachO() parses the named Mach-O file in Filename and handles the
1555// -arch flags selecting just those slices as specified by them and also parses
1556// archive files. Then for each individual Mach-O file ProcessMachO() is
1557// called to process the file based on the command line options.
1558void llvm::ParseInputMachO(StringRef Filename) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001559 // Check for -arch all and verifiy the -arch flags are valid.
1560 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1561 if (ArchFlags[i] == "all") {
1562 ArchAll = true;
1563 } else {
1564 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
1565 errs() << "llvm-objdump: Unknown architecture named '" + ArchFlags[i] +
1566 "'for the -arch option\n";
1567 return;
1568 }
1569 }
1570 }
1571
1572 // Attempt to open the binary.
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +00001573 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename);
Kevin Enderby98898f22017-01-30 20:53:17 +00001574 if (!BinaryOrErr) {
1575 if (auto E = isNotObjectErrorInvalidFileType(BinaryOrErr.takeError()))
1576 report_error(Filename, std::move(E));
1577 else
1578 outs() << Filename << ": is not an object file\n";
1579 return;
1580 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001581 Binary &Bin = *BinaryOrErr.get().getBinary();
Kevin Enderby3f0ffab2014-12-03 22:29:40 +00001582
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001583 if (Archive *A = dyn_cast<Archive>(&Bin)) {
1584 outs() << "Archive : " << Filename << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001585 if (ArchiveHeaders)
Kevin Enderby6524bd82016-07-19 20:47:07 +00001586 printArchiveHeaders(Filename, A, !NonVerbose, ArchiveMemberOffsets);
1587
Mehdi Amini41af4302016-11-11 04:28:40 +00001588 Error Err = Error::success();
Lang Hamesfc209622016-07-14 02:24:01 +00001589 for (auto &C : A->children(Err)) {
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001590 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1591 if (!ChildOrErr) {
1592 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
1593 report_error(Filename, C, std::move(E));
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001594 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001595 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001596 if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1597 if (!checkMachOAndArchFlags(O, Filename))
1598 return;
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001599 ProcessMachO(Filename, O, O->getFileName());
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001600 }
1601 }
Lang Hamesfc209622016-07-14 02:24:01 +00001602 if (Err)
1603 report_error(Filename, std::move(Err));
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001604 return;
1605 }
Kevin Enderby131d1772015-01-09 19:22:37 +00001606 if (UniversalHeaders) {
1607 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin))
Kevin Enderbyf0640752015-03-13 17:56:32 +00001608 printMachOUniversalHeaders(UB, !NonVerbose);
Kevin Enderby131d1772015-01-09 19:22:37 +00001609 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001610 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
1611 // If we have a list of architecture flags specified dump only those.
1612 if (!ArchAll && ArchFlags.size() != 0) {
1613 // Look for a slice in the universal binary that matches each ArchFlag.
1614 bool ArchFound;
1615 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1616 ArchFound = false;
1617 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1618 E = UB->end_objects();
1619 I != E; ++I) {
Kevin Enderby59343a92016-12-16 22:54:02 +00001620 if (ArchFlags[i] == I->getArchFlagName()) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001621 ArchFound = true;
Kevin Enderby9acb1092016-05-31 20:35:34 +00001622 Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001623 I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001624 std::string ArchitectureName = "";
1625 if (ArchFlags.size() > 1)
Kevin Enderby59343a92016-12-16 22:54:02 +00001626 ArchitectureName = I->getArchFlagName();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001627 if (ObjOrErr) {
1628 ObjectFile &O = *ObjOrErr.get();
1629 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001630 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
Kevin Enderby9acb1092016-05-31 20:35:34 +00001631 } else if (auto E = isNotObjectErrorInvalidFileType(
1632 ObjOrErr.takeError())) {
1633 report_error(Filename, StringRef(), std::move(E),
1634 ArchitectureName);
1635 continue;
Kevin Enderby42398052016-06-28 23:16:13 +00001636 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001637 I->getAsArchive()) {
1638 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001639 outs() << "Archive : " << Filename;
1640 if (!ArchitectureName.empty())
1641 outs() << " (architecture " << ArchitectureName << ")";
1642 outs() << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001643 if (ArchiveHeaders)
Kevin Enderby6524bd82016-07-19 20:47:07 +00001644 printArchiveHeaders(Filename, A.get(), !NonVerbose,
1645 ArchiveMemberOffsets, ArchitectureName);
Mehdi Amini41af4302016-11-11 04:28:40 +00001646 Error Err = Error::success();
Lang Hamesfc209622016-07-14 02:24:01 +00001647 for (auto &C : A->children(Err)) {
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001648 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1649 if (!ChildOrErr) {
1650 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
Kevin Enderby9acb1092016-05-31 20:35:34 +00001651 report_error(Filename, C, std::move(E), ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001652 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001653 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001654 if (MachOObjectFile *O =
1655 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001656 ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001657 }
Lang Hamesfc209622016-07-14 02:24:01 +00001658 if (Err)
1659 report_error(Filename, std::move(Err));
Kevin Enderby42398052016-06-28 23:16:13 +00001660 } else {
1661 consumeError(AOrErr.takeError());
1662 error("Mach-O universal file: " + Filename + " for " +
Kevin Enderby59343a92016-12-16 22:54:02 +00001663 "architecture " + StringRef(I->getArchFlagName()) +
Kevin Enderby42398052016-06-28 23:16:13 +00001664 " is not a Mach-O file or an archive file");
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001665 }
1666 }
1667 }
1668 if (!ArchFound) {
1669 errs() << "llvm-objdump: file: " + Filename + " does not contain "
1670 << "architecture: " + ArchFlags[i] + "\n";
1671 return;
1672 }
1673 }
1674 return;
1675 }
1676 // No architecture flags were specified so if this contains a slice that
1677 // matches the host architecture dump only that.
1678 if (!ArchAll) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001679 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1680 E = UB->end_objects();
1681 I != E; ++I) {
Kevin Enderby0512bd72015-01-09 21:55:03 +00001682 if (MachOObjectFile::getHostArch().getArchName() ==
Kevin Enderby59343a92016-12-16 22:54:02 +00001683 I->getArchFlagName()) {
Kevin Enderby9acb1092016-05-31 20:35:34 +00001684 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001685 std::string ArchiveName;
1686 ArchiveName.clear();
1687 if (ObjOrErr) {
1688 ObjectFile &O = *ObjOrErr.get();
1689 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001690 ProcessMachO(Filename, MachOOF);
Kevin Enderby9acb1092016-05-31 20:35:34 +00001691 } else if (auto E = isNotObjectErrorInvalidFileType(
1692 ObjOrErr.takeError())) {
1693 report_error(Filename, std::move(E));
1694 continue;
Kevin Enderby42398052016-06-28 23:16:13 +00001695 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001696 I->getAsArchive()) {
1697 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001698 outs() << "Archive : " << Filename << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001699 if (ArchiveHeaders)
Kevin Enderby6524bd82016-07-19 20:47:07 +00001700 printArchiveHeaders(Filename, A.get(), !NonVerbose,
1701 ArchiveMemberOffsets);
Mehdi Amini41af4302016-11-11 04:28:40 +00001702 Error Err = Error::success();
Lang Hamesfc209622016-07-14 02:24:01 +00001703 for (auto &C : A->children(Err)) {
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001704 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1705 if (!ChildOrErr) {
1706 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
1707 report_error(Filename, C, std::move(E));
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001708 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001709 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001710 if (MachOObjectFile *O =
1711 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001712 ProcessMachO(Filename, O, O->getFileName());
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001713 }
Lang Hamesfc209622016-07-14 02:24:01 +00001714 if (Err)
1715 report_error(Filename, std::move(Err));
Kevin Enderby42398052016-06-28 23:16:13 +00001716 } else {
1717 consumeError(AOrErr.takeError());
1718 error("Mach-O universal file: " + Filename + " for architecture " +
Kevin Enderby59343a92016-12-16 22:54:02 +00001719 StringRef(I->getArchFlagName()) +
Kevin Enderby42398052016-06-28 23:16:13 +00001720 " is not a Mach-O file or an archive file");
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001721 }
1722 return;
1723 }
1724 }
1725 }
1726 // Either all architectures have been specified or none have been specified
1727 // and this does not contain the host architecture so dump all the slices.
1728 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
1729 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
1730 E = UB->end_objects();
1731 I != E; ++I) {
Kevin Enderby9acb1092016-05-31 20:35:34 +00001732 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001733 std::string ArchitectureName = "";
1734 if (moreThanOneArch)
Kevin Enderby59343a92016-12-16 22:54:02 +00001735 ArchitectureName = I->getArchFlagName();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001736 if (ObjOrErr) {
1737 ObjectFile &Obj = *ObjOrErr.get();
1738 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001739 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
Kevin Enderby9acb1092016-05-31 20:35:34 +00001740 } else if (auto E = isNotObjectErrorInvalidFileType(
1741 ObjOrErr.takeError())) {
1742 report_error(StringRef(), Filename, std::move(E), ArchitectureName);
1743 continue;
Kevin Enderby42398052016-06-28 23:16:13 +00001744 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
1745 I->getAsArchive()) {
Rafael Espindola0bfe8282014-12-09 21:05:36 +00001746 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001747 outs() << "Archive : " << Filename;
1748 if (!ArchitectureName.empty())
1749 outs() << " (architecture " << ArchitectureName << ")";
1750 outs() << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001751 if (ArchiveHeaders)
Kevin Enderby6524bd82016-07-19 20:47:07 +00001752 printArchiveHeaders(Filename, A.get(), !NonVerbose,
1753 ArchiveMemberOffsets, ArchitectureName);
Mehdi Amini41af4302016-11-11 04:28:40 +00001754 Error Err = Error::success();
Lang Hamesfc209622016-07-14 02:24:01 +00001755 for (auto &C : A->children(Err)) {
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001756 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1757 if (!ChildOrErr) {
1758 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
Kevin Enderby9acb1092016-05-31 20:35:34 +00001759 report_error(Filename, C, std::move(E), ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001760 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001761 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001762 if (MachOObjectFile *O =
1763 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1764 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001765 ProcessMachO(Filename, MachOOF, MachOOF->getFileName(),
1766 ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001767 }
1768 }
Lang Hamesfc209622016-07-14 02:24:01 +00001769 if (Err)
1770 report_error(Filename, std::move(Err));
Kevin Enderby42398052016-06-28 23:16:13 +00001771 } else {
1772 consumeError(AOrErr.takeError());
1773 error("Mach-O universal file: " + Filename + " for architecture " +
Kevin Enderby59343a92016-12-16 22:54:02 +00001774 StringRef(I->getArchFlagName()) +
Kevin Enderby42398052016-06-28 23:16:13 +00001775 " is not a Mach-O file or an archive file");
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001776 }
1777 }
1778 return;
1779 }
1780 if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) {
1781 if (!checkMachOAndArchFlags(O, Filename))
1782 return;
1783 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O)) {
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001784 ProcessMachO(Filename, MachOOF);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001785 } else
1786 errs() << "llvm-objdump: '" << Filename << "': "
1787 << "Object is not a Mach-O file type.\n";
Davide Italiano25d84582016-01-13 04:11:36 +00001788 return;
1789 }
1790 llvm_unreachable("Input object can't be invalid at this point");
Rafael Espindola9b709252013-04-13 01:45:40 +00001791}
1792
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001793// The block of info used by the Symbolizer call backs.
1794struct DisassembleInfo {
1795 bool verbose;
1796 MachOObjectFile *O;
1797 SectionRef S;
Kevin Enderbybf246f52014-09-24 23:08:22 +00001798 SymbolAddressMap *AddrMap;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00001799 std::vector<SectionRef> *Sections;
1800 const char *class_name;
1801 const char *selector_name;
1802 char *method;
Kevin Enderby04bf6932014-10-28 23:39:46 +00001803 char *demangled_name;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00001804 uint64_t adrp_addr;
1805 uint32_t adrp_inst;
Saleem Abdulrasool1d84d9a2017-01-08 19:14:15 +00001806 std::unique_ptr<SymbolAddressMap> bindtable;
Kevin Enderbyaac75382015-10-08 16:56:35 +00001807 uint32_t depth;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001808};
1809
1810// SymbolizerGetOpInfo() is the operand information call back function.
1811// This is called to get the symbolic information for operand(s) of an
1812// instruction when it is being done. This routine does this from
1813// the relocation information, symbol table, etc. That block of information
1814// is a pointer to the struct DisassembleInfo that was passed when the
1815// disassembler context was created and passed to back to here when
1816// called back by the disassembler for instruction operands that could have
1817// relocation information. The address of the instruction containing operand is
1818// at the Pc parameter. The immediate value the operand has is passed in
1819// op_info->Value and is at Offset past the start of the instruction and has a
1820// byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
1821// LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
1822// names and addends of the symbolic expression to add for the operand. The
1823// value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
1824// information is returned then this function returns 1 else it returns 0.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00001825static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
1826 uint64_t Size, int TagType, void *TagBuf) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001827 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
1828 struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00001829 uint64_t value = op_info->Value;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001830
1831 // Make sure all fields returned are zero if we don't set them.
1832 memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1));
1833 op_info->Value = value;
1834
1835 // If the TagType is not the value 1 which it code knows about or if no
1836 // verbose symbolic information is wanted then just return 0, indicating no
1837 // information is being returned.
David Blaikie33dd45d02015-03-23 18:39:02 +00001838 if (TagType != 1 || !info->verbose)
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001839 return 0;
1840
1841 unsigned int Arch = info->O->getArch();
1842 if (Arch == Triple::x86) {
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001843 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1844 return 0;
Kevin Enderbyd90a4172015-10-10 00:05:01 +00001845 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
1846 // TODO:
1847 // Search the external relocation entries of a fully linked image
1848 // (if any) for an entry that matches this segment offset.
1849 // uint32_t seg_offset = (Pc + Offset);
1850 return 0;
1851 }
1852 // In MH_OBJECT filetypes search the section's relocation entries (if any)
1853 // for an entry for this section offset.
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001854 uint32_t sect_addr = info->S.getAddress();
1855 uint32_t sect_offset = (Pc + Offset) - sect_addr;
1856 bool reloc_found = false;
1857 DataRefImpl Rel;
1858 MachO::any_relocation_info RE;
1859 bool isExtern = false;
1860 SymbolRef Symbol;
1861 bool r_scattered = false;
1862 uint32_t r_value, pair_r_value, r_type;
1863 for (const RelocationRef &Reloc : info->S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00001864 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001865 if (RelocOffset == sect_offset) {
1866 Rel = Reloc.getRawDataRefImpl();
1867 RE = info->O->getRelocation(Rel);
Kevin Enderby3eb73e12014-11-11 19:16:45 +00001868 r_type = info->O->getAnyRelocationType(RE);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001869 r_scattered = info->O->isRelocationScattered(RE);
1870 if (r_scattered) {
1871 r_value = info->O->getScatteredRelocationValue(RE);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001872 if (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1873 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
1874 DataRefImpl RelNext = Rel;
1875 info->O->moveRelocationNext(RelNext);
1876 MachO::any_relocation_info RENext;
1877 RENext = info->O->getRelocation(RelNext);
1878 if (info->O->isRelocationScattered(RENext))
Kevin Enderby930fdc72014-11-06 19:00:13 +00001879 pair_r_value = info->O->getScatteredRelocationValue(RENext);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001880 else
1881 return 0;
1882 }
1883 } else {
1884 isExtern = info->O->getPlainRelocationExternal(RE);
1885 if (isExtern) {
1886 symbol_iterator RelocSym = Reloc.getSymbol();
1887 Symbol = *RelocSym;
1888 }
1889 }
1890 reloc_found = true;
1891 break;
1892 }
1893 }
1894 if (reloc_found && isExtern) {
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00001895 Expected<StringRef> SymName = Symbol.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00001896 if (!SymName)
1897 report_error(info->O->getFileName(), SymName.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001898 const char *name = SymName->data();
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001899 op_info->AddSymbol.Present = 1;
1900 op_info->AddSymbol.Name = name;
1901 // For i386 extern relocation entries the value in the instruction is
1902 // the offset from the symbol, and value is already set in op_info->Value.
1903 return 1;
1904 }
1905 if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
1906 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) {
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001907 const char *add = GuessSymbolName(r_value, info->AddrMap);
1908 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00001909 uint32_t offset = value - (r_value - pair_r_value);
1910 op_info->AddSymbol.Present = 1;
1911 if (add != nullptr)
1912 op_info->AddSymbol.Name = add;
1913 else
1914 op_info->AddSymbol.Value = r_value;
1915 op_info->SubtractSymbol.Present = 1;
1916 if (sub != nullptr)
1917 op_info->SubtractSymbol.Name = sub;
1918 else
1919 op_info->SubtractSymbol.Value = pair_r_value;
1920 op_info->Value = offset;
1921 return 1;
1922 }
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001923 return 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00001924 }
1925 if (Arch == Triple::x86_64) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001926 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
1927 return 0;
Kevin Enderbyabf10f22017-06-22 17:41:22 +00001928 // For non MH_OBJECT types, like MH_KEXT_BUNDLE, Search the external
1929 // relocation entries of a linked image (if any) for an entry that matches
1930 // this segment offset.
Kevin Enderbyd90a4172015-10-10 00:05:01 +00001931 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
Kevin Enderbyabf10f22017-06-22 17:41:22 +00001932 uint64_t seg_offset = Pc + Offset;
1933 bool reloc_found = false;
1934 DataRefImpl Rel;
1935 MachO::any_relocation_info RE;
1936 bool isExtern = false;
1937 SymbolRef Symbol;
1938 for (const RelocationRef &Reloc : info->O->external_relocations()) {
1939 uint64_t RelocOffset = Reloc.getOffset();
1940 if (RelocOffset == seg_offset) {
1941 Rel = Reloc.getRawDataRefImpl();
1942 RE = info->O->getRelocation(Rel);
1943 // external relocation entries should always be external.
1944 isExtern = info->O->getPlainRelocationExternal(RE);
1945 if (isExtern) {
1946 symbol_iterator RelocSym = Reloc.getSymbol();
1947 Symbol = *RelocSym;
1948 }
1949 reloc_found = true;
1950 break;
1951 }
1952 }
1953 if (reloc_found && isExtern) {
1954 // The Value passed in will be adjusted by the Pc if the instruction
1955 // adds the Pc. But for x86_64 external relocation entries the Value
1956 // is the offset from the external symbol.
1957 if (info->O->getAnyRelocationPCRel(RE))
1958 op_info->Value -= Pc + Offset + Size;
1959 Expected<StringRef> SymName = Symbol.getName();
1960 if (!SymName)
1961 report_error(info->O->getFileName(), SymName.takeError());
1962 const char *name = SymName->data();
1963 op_info->AddSymbol.Present = 1;
1964 op_info->AddSymbol.Name = name;
1965 return 1;
1966 }
Kevin Enderbyd90a4172015-10-10 00:05:01 +00001967 return 0;
1968 }
1969 // In MH_OBJECT filetypes search the section's relocation entries (if any)
1970 // for an entry for this section offset.
Rafael Espindola80291272014-10-08 15:28:58 +00001971 uint64_t sect_addr = info->S.getAddress();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001972 uint64_t sect_offset = (Pc + Offset) - sect_addr;
1973 bool reloc_found = false;
1974 DataRefImpl Rel;
1975 MachO::any_relocation_info RE;
1976 bool isExtern = false;
1977 SymbolRef Symbol;
1978 for (const RelocationRef &Reloc : info->S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00001979 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00001980 if (RelocOffset == sect_offset) {
1981 Rel = Reloc.getRawDataRefImpl();
1982 RE = info->O->getRelocation(Rel);
1983 // NOTE: Scattered relocations don't exist on x86_64.
1984 isExtern = info->O->getPlainRelocationExternal(RE);
1985 if (isExtern) {
1986 symbol_iterator RelocSym = Reloc.getSymbol();
1987 Symbol = *RelocSym;
1988 }
1989 reloc_found = true;
1990 break;
1991 }
1992 }
1993 if (reloc_found && isExtern) {
1994 // The Value passed in will be adjusted by the Pc if the instruction
1995 // adds the Pc. But for x86_64 external relocation entries the Value
1996 // is the offset from the external symbol.
1997 if (info->O->getAnyRelocationPCRel(RE))
1998 op_info->Value -= Pc + Offset + Size;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00001999 Expected<StringRef> SymName = Symbol.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00002000 if (!SymName)
2001 report_error(info->O->getFileName(), SymName.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002002 const char *name = SymName->data();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002003 unsigned Type = info->O->getAnyRelocationType(RE);
2004 if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
2005 DataRefImpl RelNext = Rel;
2006 info->O->moveRelocationNext(RelNext);
2007 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2008 unsigned TypeNext = info->O->getAnyRelocationType(RENext);
2009 bool isExternNext = info->O->getPlainRelocationExternal(RENext);
2010 unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext);
2011 if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) {
2012 op_info->SubtractSymbol.Present = 1;
2013 op_info->SubtractSymbol.Name = name;
2014 symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
2015 Symbol = *RelocSymNext;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002016 Expected<StringRef> SymNameNext = Symbol.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00002017 if (!SymNameNext)
2018 report_error(info->O->getFileName(), SymNameNext.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002019 name = SymNameNext->data();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002020 }
2021 }
2022 // TODO: add the VariantKinds to op_info->VariantKind for relocation types
2023 // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
2024 op_info->AddSymbol.Present = 1;
2025 op_info->AddSymbol.Name = name;
2026 return 1;
2027 }
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002028 return 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00002029 }
2030 if (Arch == Triple::arm) {
Kevin Enderby930fdc72014-11-06 19:00:13 +00002031 if (Offset != 0 || (Size != 4 && Size != 2))
2032 return 0;
Kevin Enderbyd90a4172015-10-10 00:05:01 +00002033 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2034 // TODO:
2035 // Search the external relocation entries of a fully linked image
2036 // (if any) for an entry that matches this segment offset.
2037 // uint32_t seg_offset = (Pc + Offset);
2038 return 0;
2039 }
2040 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2041 // for an entry for this section offset.
Kevin Enderby930fdc72014-11-06 19:00:13 +00002042 uint32_t sect_addr = info->S.getAddress();
2043 uint32_t sect_offset = (Pc + Offset) - sect_addr;
Kevin Enderby930fdc72014-11-06 19:00:13 +00002044 DataRefImpl Rel;
2045 MachO::any_relocation_info RE;
2046 bool isExtern = false;
2047 SymbolRef Symbol;
2048 bool r_scattered = false;
2049 uint32_t r_value, pair_r_value, r_type, r_length, other_half;
David Blaikie33dd45d02015-03-23 18:39:02 +00002050 auto Reloc =
David Majnemer562e8292016-08-12 00:18:03 +00002051 find_if(info->S.relocations(), [&](const RelocationRef &Reloc) {
2052 uint64_t RelocOffset = Reloc.getOffset();
2053 return RelocOffset == sect_offset;
2054 });
David Blaikie33dd45d02015-03-23 18:39:02 +00002055
2056 if (Reloc == info->S.relocations().end())
2057 return 0;
2058
2059 Rel = Reloc->getRawDataRefImpl();
2060 RE = info->O->getRelocation(Rel);
2061 r_length = info->O->getAnyRelocationLength(RE);
2062 r_scattered = info->O->isRelocationScattered(RE);
2063 if (r_scattered) {
2064 r_value = info->O->getScatteredRelocationValue(RE);
2065 r_type = info->O->getScatteredRelocationType(RE);
2066 } else {
2067 r_type = info->O->getAnyRelocationType(RE);
2068 isExtern = info->O->getPlainRelocationExternal(RE);
2069 if (isExtern) {
2070 symbol_iterator RelocSym = Reloc->getSymbol();
2071 Symbol = *RelocSym;
Kevin Enderby930fdc72014-11-06 19:00:13 +00002072 }
2073 }
David Blaikie33dd45d02015-03-23 18:39:02 +00002074 if (r_type == MachO::ARM_RELOC_HALF ||
2075 r_type == MachO::ARM_RELOC_SECTDIFF ||
2076 r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
2077 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2078 DataRefImpl RelNext = Rel;
2079 info->O->moveRelocationNext(RelNext);
2080 MachO::any_relocation_info RENext;
2081 RENext = info->O->getRelocation(RelNext);
2082 other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff;
2083 if (info->O->isRelocationScattered(RENext))
2084 pair_r_value = info->O->getScatteredRelocationValue(RENext);
2085 }
2086
2087 if (isExtern) {
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002088 Expected<StringRef> SymName = Symbol.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00002089 if (!SymName)
2090 report_error(info->O->getFileName(), SymName.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002091 const char *name = SymName->data();
Kevin Enderby930fdc72014-11-06 19:00:13 +00002092 op_info->AddSymbol.Present = 1;
2093 op_info->AddSymbol.Name = name;
Sylvestre Ledru648cced2015-02-05 17:00:23 +00002094 switch (r_type) {
2095 case MachO::ARM_RELOC_HALF:
2096 if ((r_length & 0x1) == 1) {
2097 op_info->Value = value << 16 | other_half;
2098 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2099 } else {
2100 op_info->Value = other_half << 16 | value;
2101 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Sylvestre Ledrufe0c7ad2015-02-05 16:35:44 +00002102 }
Sylvestre Ledru648cced2015-02-05 17:00:23 +00002103 break;
2104 default:
2105 break;
Kevin Enderby930fdc72014-11-06 19:00:13 +00002106 }
2107 return 1;
2108 }
2109 // If we have a branch that is not an external relocation entry then
2110 // return 0 so the code in tryAddingSymbolicOperand() can use the
2111 // SymbolLookUp call back with the branch target address to look up the
Simon Pilgrimdae11f72016-11-20 13:31:13 +00002112 // symbol and possibility add an annotation for a symbol stub.
David Blaikie33dd45d02015-03-23 18:39:02 +00002113 if (isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 ||
2114 r_type == MachO::ARM_THUMB_RELOC_BR22))
Kevin Enderby930fdc72014-11-06 19:00:13 +00002115 return 0;
2116
2117 uint32_t offset = 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00002118 if (r_type == MachO::ARM_RELOC_HALF ||
2119 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2120 if ((r_length & 0x1) == 1)
2121 value = value << 16 | other_half;
2122 else
2123 value = other_half << 16 | value;
2124 }
2125 if (r_scattered && (r_type != MachO::ARM_RELOC_HALF &&
2126 r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) {
2127 offset = value - r_value;
2128 value = r_value;
Kevin Enderby930fdc72014-11-06 19:00:13 +00002129 }
2130
David Blaikie33dd45d02015-03-23 18:39:02 +00002131 if (r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
Kevin Enderby930fdc72014-11-06 19:00:13 +00002132 if ((r_length & 0x1) == 1)
2133 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2134 else
2135 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00002136 const char *add = GuessSymbolName(r_value, info->AddrMap);
2137 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
Kevin Enderby930fdc72014-11-06 19:00:13 +00002138 int32_t offset = value - (r_value - pair_r_value);
2139 op_info->AddSymbol.Present = 1;
2140 if (add != nullptr)
2141 op_info->AddSymbol.Name = add;
2142 else
2143 op_info->AddSymbol.Value = r_value;
2144 op_info->SubtractSymbol.Present = 1;
2145 if (sub != nullptr)
2146 op_info->SubtractSymbol.Name = sub;
2147 else
2148 op_info->SubtractSymbol.Value = pair_r_value;
2149 op_info->Value = offset;
2150 return 1;
2151 }
2152
Kevin Enderby930fdc72014-11-06 19:00:13 +00002153 op_info->AddSymbol.Present = 1;
2154 op_info->Value = offset;
David Blaikie33dd45d02015-03-23 18:39:02 +00002155 if (r_type == MachO::ARM_RELOC_HALF) {
2156 if ((r_length & 0x1) == 1)
2157 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2158 else
2159 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Kevin Enderby930fdc72014-11-06 19:00:13 +00002160 }
Kevin Enderbyf6d25852015-01-31 00:37:11 +00002161 const char *add = GuessSymbolName(value, info->AddrMap);
Kevin Enderby930fdc72014-11-06 19:00:13 +00002162 if (add != nullptr) {
2163 op_info->AddSymbol.Name = add;
2164 return 1;
2165 }
2166 op_info->AddSymbol.Value = value;
2167 return 1;
David Blaikie33dd45d02015-03-23 18:39:02 +00002168 }
2169 if (Arch == Triple::aarch64) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002170 if (Offset != 0 || Size != 4)
2171 return 0;
Kevin Enderbyd90a4172015-10-10 00:05:01 +00002172 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2173 // TODO:
2174 // Search the external relocation entries of a fully linked image
2175 // (if any) for an entry that matches this segment offset.
2176 // uint64_t seg_offset = (Pc + Offset);
2177 return 0;
2178 }
2179 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2180 // for an entry for this section offset.
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002181 uint64_t sect_addr = info->S.getAddress();
2182 uint64_t sect_offset = (Pc + Offset) - sect_addr;
David Blaikie33dd45d02015-03-23 18:39:02 +00002183 auto Reloc =
David Majnemer562e8292016-08-12 00:18:03 +00002184 find_if(info->S.relocations(), [&](const RelocationRef &Reloc) {
2185 uint64_t RelocOffset = Reloc.getOffset();
2186 return RelocOffset == sect_offset;
2187 });
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002188
David Blaikie33dd45d02015-03-23 18:39:02 +00002189 if (Reloc == info->S.relocations().end())
2190 return 0;
2191
2192 DataRefImpl Rel = Reloc->getRawDataRefImpl();
2193 MachO::any_relocation_info RE = info->O->getRelocation(Rel);
2194 uint32_t r_type = info->O->getAnyRelocationType(RE);
2195 if (r_type == MachO::ARM64_RELOC_ADDEND) {
2196 DataRefImpl RelNext = Rel;
2197 info->O->moveRelocationNext(RelNext);
2198 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2199 if (value == 0) {
2200 value = info->O->getPlainRelocationSymbolNum(RENext);
2201 op_info->Value = value;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002202 }
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002203 }
David Blaikie33dd45d02015-03-23 18:39:02 +00002204 // NOTE: Scattered relocations don't exist on arm64.
2205 if (!info->O->getPlainRelocationExternal(RE))
2206 return 0;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002207 Expected<StringRef> SymName = Reloc->getSymbol()->getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00002208 if (!SymName)
2209 report_error(info->O->getFileName(), SymName.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002210 const char *name = SymName->data();
David Blaikie33dd45d02015-03-23 18:39:02 +00002211 op_info->AddSymbol.Present = 1;
2212 op_info->AddSymbol.Name = name;
2213
2214 switch (r_type) {
2215 case MachO::ARM64_RELOC_PAGE21:
2216 /* @page */
2217 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE;
2218 break;
2219 case MachO::ARM64_RELOC_PAGEOFF12:
2220 /* @pageoff */
2221 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF;
2222 break;
2223 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
2224 /* @gotpage */
2225 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE;
2226 break;
2227 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
2228 /* @gotpageoff */
2229 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF;
2230 break;
2231 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
2232 /* @tvlppage is not implemented in llvm-mc */
2233 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP;
2234 break;
2235 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
2236 /* @tvlppageoff is not implemented in llvm-mc */
2237 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF;
2238 break;
2239 default:
2240 case MachO::ARM64_RELOC_BRANCH26:
2241 op_info->VariantKind = LLVMDisassembler_VariantKind_None;
2242 break;
2243 }
2244 return 1;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002245 }
David Blaikie33dd45d02015-03-23 18:39:02 +00002246 return 0;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002247}
2248
Kevin Enderbybf246f52014-09-24 23:08:22 +00002249// GuessCstringPointer is passed the address of what might be a pointer to a
2250// literal string in a cstring section. If that address is in a cstring section
2251// it returns a pointer to that string. Else it returns nullptr.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00002252static const char *GuessCstringPointer(uint64_t ReferenceValue,
2253 struct DisassembleInfo *info) {
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002254 for (const auto &Load : info->O->load_commands()) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00002255 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2256 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2257 for (unsigned J = 0; J < Seg.nsects; ++J) {
2258 MachO::section_64 Sec = info->O->getSection64(Load, J);
2259 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2260 if (section_type == MachO::S_CSTRING_LITERALS &&
2261 ReferenceValue >= Sec.addr &&
2262 ReferenceValue < Sec.addr + Sec.size) {
2263 uint64_t sect_offset = ReferenceValue - Sec.addr;
2264 uint64_t object_offset = Sec.offset + sect_offset;
2265 StringRef MachOContents = info->O->getData();
2266 uint64_t object_size = MachOContents.size();
2267 const char *object_addr = (const char *)MachOContents.data();
2268 if (object_offset < object_size) {
2269 const char *name = object_addr + object_offset;
2270 return name;
2271 } else {
2272 return nullptr;
2273 }
2274 }
2275 }
2276 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2277 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2278 for (unsigned J = 0; J < Seg.nsects; ++J) {
2279 MachO::section Sec = info->O->getSection(Load, J);
2280 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2281 if (section_type == MachO::S_CSTRING_LITERALS &&
2282 ReferenceValue >= Sec.addr &&
2283 ReferenceValue < Sec.addr + Sec.size) {
2284 uint64_t sect_offset = ReferenceValue - Sec.addr;
2285 uint64_t object_offset = Sec.offset + sect_offset;
2286 StringRef MachOContents = info->O->getData();
2287 uint64_t object_size = MachOContents.size();
2288 const char *object_addr = (const char *)MachOContents.data();
2289 if (object_offset < object_size) {
2290 const char *name = object_addr + object_offset;
2291 return name;
2292 } else {
2293 return nullptr;
2294 }
2295 }
2296 }
2297 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00002298 }
2299 return nullptr;
2300}
2301
Kevin Enderby85974882014-09-26 22:20:44 +00002302// GuessIndirectSymbol returns the name of the indirect symbol for the
2303// ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe
2304// an address of a symbol stub or a lazy or non-lazy pointer to associate the
2305// symbol name being referenced by the stub or pointer.
2306static const char *GuessIndirectSymbol(uint64_t ReferenceValue,
2307 struct DisassembleInfo *info) {
Kevin Enderby85974882014-09-26 22:20:44 +00002308 MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand();
2309 MachO::symtab_command Symtab = info->O->getSymtabLoadCommand();
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002310 for (const auto &Load : info->O->load_commands()) {
Kevin Enderby85974882014-09-26 22:20:44 +00002311 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2312 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2313 for (unsigned J = 0; J < Seg.nsects; ++J) {
2314 MachO::section_64 Sec = info->O->getSection64(Load, J);
2315 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2316 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2317 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2318 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2319 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2320 section_type == MachO::S_SYMBOL_STUBS) &&
2321 ReferenceValue >= Sec.addr &&
2322 ReferenceValue < Sec.addr + Sec.size) {
2323 uint32_t stride;
2324 if (section_type == MachO::S_SYMBOL_STUBS)
2325 stride = Sec.reserved2;
2326 else
2327 stride = 8;
2328 if (stride == 0)
2329 return nullptr;
2330 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2331 if (index < Dysymtab.nindirectsyms) {
2332 uint32_t indirect_symbol =
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002333 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
Kevin Enderby85974882014-09-26 22:20:44 +00002334 if (indirect_symbol < Symtab.nsyms) {
2335 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2336 SymbolRef Symbol = *Sym;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002337 Expected<StringRef> SymName = Symbol.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00002338 if (!SymName)
2339 report_error(info->O->getFileName(), SymName.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002340 const char *name = SymName->data();
Kevin Enderby85974882014-09-26 22:20:44 +00002341 return name;
2342 }
2343 }
2344 }
2345 }
2346 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2347 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2348 for (unsigned J = 0; J < Seg.nsects; ++J) {
2349 MachO::section Sec = info->O->getSection(Load, J);
2350 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2351 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2352 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2353 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2354 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2355 section_type == MachO::S_SYMBOL_STUBS) &&
2356 ReferenceValue >= Sec.addr &&
2357 ReferenceValue < Sec.addr + Sec.size) {
2358 uint32_t stride;
2359 if (section_type == MachO::S_SYMBOL_STUBS)
2360 stride = Sec.reserved2;
2361 else
2362 stride = 4;
2363 if (stride == 0)
2364 return nullptr;
2365 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2366 if (index < Dysymtab.nindirectsyms) {
2367 uint32_t indirect_symbol =
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002368 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
Kevin Enderby85974882014-09-26 22:20:44 +00002369 if (indirect_symbol < Symtab.nsyms) {
2370 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2371 SymbolRef Symbol = *Sym;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002372 Expected<StringRef> SymName = Symbol.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00002373 if (!SymName)
2374 report_error(info->O->getFileName(), SymName.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002375 const char *name = SymName->data();
Kevin Enderby85974882014-09-26 22:20:44 +00002376 return name;
2377 }
2378 }
2379 }
2380 }
2381 }
Kevin Enderby85974882014-09-26 22:20:44 +00002382 }
2383 return nullptr;
2384}
2385
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002386// method_reference() is called passing it the ReferenceName that might be
2387// a reference it to an Objective-C method call. If so then it allocates and
2388// assembles a method call string with the values last seen and saved in
2389// the DisassembleInfo's class_name and selector_name fields. This is saved
2390// into the method field of the info and any previous string is free'ed.
2391// Then the class_name field in the info is set to nullptr. The method call
2392// string is set into ReferenceName and ReferenceType is set to
2393// LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call
2394// then both ReferenceType and ReferenceName are left unchanged.
2395static void method_reference(struct DisassembleInfo *info,
2396 uint64_t *ReferenceType,
2397 const char **ReferenceName) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002398 unsigned int Arch = info->O->getArch();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002399 if (*ReferenceName != nullptr) {
2400 if (strcmp(*ReferenceName, "_objc_msgSend") == 0) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002401 if (info->selector_name != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002402 if (info->method != nullptr)
2403 free(info->method);
2404 if (info->class_name != nullptr) {
2405 info->method = (char *)malloc(5 + strlen(info->class_name) +
2406 strlen(info->selector_name));
2407 if (info->method != nullptr) {
2408 strcpy(info->method, "+[");
2409 strcat(info->method, info->class_name);
2410 strcat(info->method, " ");
2411 strcat(info->method, info->selector_name);
2412 strcat(info->method, "]");
2413 *ReferenceName = info->method;
2414 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2415 }
2416 } else {
2417 info->method = (char *)malloc(9 + strlen(info->selector_name));
2418 if (info->method != nullptr) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002419 if (Arch == Triple::x86_64)
2420 strcpy(info->method, "-[%rdi ");
2421 else if (Arch == Triple::aarch64)
2422 strcpy(info->method, "-[x0 ");
2423 else
2424 strcpy(info->method, "-[r? ");
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002425 strcat(info->method, info->selector_name);
2426 strcat(info->method, "]");
2427 *ReferenceName = info->method;
2428 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2429 }
2430 }
2431 info->class_name = nullptr;
2432 }
2433 } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002434 if (info->selector_name != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002435 if (info->method != nullptr)
2436 free(info->method);
2437 info->method = (char *)malloc(17 + strlen(info->selector_name));
2438 if (info->method != nullptr) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002439 if (Arch == Triple::x86_64)
2440 strcpy(info->method, "-[[%rdi super] ");
2441 else if (Arch == Triple::aarch64)
2442 strcpy(info->method, "-[[x0 super] ");
2443 else
2444 strcpy(info->method, "-[[r? super] ");
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002445 strcat(info->method, info->selector_name);
2446 strcat(info->method, "]");
2447 *ReferenceName = info->method;
2448 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2449 }
2450 info->class_name = nullptr;
2451 }
2452 }
2453 }
2454}
2455
2456// GuessPointerPointer() is passed the address of what might be a pointer to
2457// a reference to an Objective-C class, selector, message ref or cfstring.
2458// If so the value of the pointer is returned and one of the booleans are set
2459// to true. If not zero is returned and all the booleans are set to false.
2460static uint64_t GuessPointerPointer(uint64_t ReferenceValue,
2461 struct DisassembleInfo *info,
2462 bool &classref, bool &selref, bool &msgref,
2463 bool &cfstring) {
2464 classref = false;
2465 selref = false;
2466 msgref = false;
2467 cfstring = false;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002468 for (const auto &Load : info->O->load_commands()) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002469 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2470 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2471 for (unsigned J = 0; J < Seg.nsects; ++J) {
2472 MachO::section_64 Sec = info->O->getSection64(Load, J);
2473 if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 ||
2474 strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2475 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 ||
2476 strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 ||
2477 strncmp(Sec.sectname, "__cfstring", 16) == 0) &&
2478 ReferenceValue >= Sec.addr &&
2479 ReferenceValue < Sec.addr + Sec.size) {
2480 uint64_t sect_offset = ReferenceValue - Sec.addr;
2481 uint64_t object_offset = Sec.offset + sect_offset;
2482 StringRef MachOContents = info->O->getData();
2483 uint64_t object_size = MachOContents.size();
2484 const char *object_addr = (const char *)MachOContents.data();
2485 if (object_offset < object_size) {
2486 uint64_t pointer_value;
2487 memcpy(&pointer_value, object_addr + object_offset,
2488 sizeof(uint64_t));
2489 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2490 sys::swapByteOrder(pointer_value);
2491 if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0)
2492 selref = true;
2493 else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2494 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0)
2495 classref = true;
2496 else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 &&
2497 ReferenceValue + 8 < Sec.addr + Sec.size) {
2498 msgref = true;
2499 memcpy(&pointer_value, object_addr + object_offset + 8,
2500 sizeof(uint64_t));
2501 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2502 sys::swapByteOrder(pointer_value);
2503 } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0)
2504 cfstring = true;
2505 return pointer_value;
2506 } else {
2507 return 0;
2508 }
2509 }
2510 }
2511 }
2512 // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002513 }
2514 return 0;
2515}
2516
2517// get_pointer_64 returns a pointer to the bytes in the object file at the
2518// Address from a section in the Mach-O file. And indirectly returns the
2519// offset into the section, number of bytes left in the section past the offset
2520// and which section is was being referenced. If the Address is not in a
2521// section nullptr is returned.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00002522static const char *get_pointer_64(uint64_t Address, uint32_t &offset,
2523 uint32_t &left, SectionRef &S,
Kevin Enderby846c0002015-04-16 17:19:59 +00002524 DisassembleInfo *info,
2525 bool objc_only = false) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002526 offset = 0;
2527 left = 0;
2528 S = SectionRef();
2529 for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) {
2530 uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress();
2531 uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize();
Kevin Enderby46e642f2015-10-08 22:50:55 +00002532 if (SectSize == 0)
2533 continue;
Kevin Enderby846c0002015-04-16 17:19:59 +00002534 if (objc_only) {
2535 StringRef SectName;
2536 ((*(info->Sections))[SectIdx]).getName(SectName);
2537 DataRefImpl Ref = ((*(info->Sections))[SectIdx]).getRawDataRefImpl();
2538 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
2539 if (SegName != "__OBJC" && SectName != "__cstring")
2540 continue;
2541 }
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002542 if (Address >= SectAddress && Address < SectAddress + SectSize) {
2543 S = (*(info->Sections))[SectIdx];
2544 offset = Address - SectAddress;
2545 left = SectSize - offset;
2546 StringRef SectContents;
2547 ((*(info->Sections))[SectIdx]).getContents(SectContents);
2548 return SectContents.data() + offset;
2549 }
2550 }
2551 return nullptr;
2552}
2553
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002554static const char *get_pointer_32(uint32_t Address, uint32_t &offset,
2555 uint32_t &left, SectionRef &S,
Kevin Enderby846c0002015-04-16 17:19:59 +00002556 DisassembleInfo *info,
2557 bool objc_only = false) {
2558 return get_pointer_64(Address, offset, left, S, info, objc_only);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002559}
2560
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002561// get_symbol_64() returns the name of a symbol (or nullptr) and the address of
2562// the symbol indirectly through n_value. Based on the relocation information
2563// for the specified section offset in the specified section reference.
Kevin Enderby0fc11822015-04-01 20:57:01 +00002564// If no relocation information is found and a non-zero ReferenceValue for the
2565// symbol is passed, look up that address in the info's AddrMap.
Rafael Espindolad7a32ea2015-06-24 10:20:30 +00002566static const char *get_symbol_64(uint32_t sect_offset, SectionRef S,
2567 DisassembleInfo *info, uint64_t &n_value,
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +00002568 uint64_t ReferenceValue = 0) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002569 n_value = 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00002570 if (!info->verbose)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002571 return nullptr;
2572
2573 // See if there is an external relocation entry at the sect_offset.
2574 bool reloc_found = false;
2575 DataRefImpl Rel;
2576 MachO::any_relocation_info RE;
2577 bool isExtern = false;
2578 SymbolRef Symbol;
2579 for (const RelocationRef &Reloc : S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00002580 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002581 if (RelocOffset == sect_offset) {
2582 Rel = Reloc.getRawDataRefImpl();
2583 RE = info->O->getRelocation(Rel);
2584 if (info->O->isRelocationScattered(RE))
2585 continue;
2586 isExtern = info->O->getPlainRelocationExternal(RE);
2587 if (isExtern) {
2588 symbol_iterator RelocSym = Reloc.getSymbol();
2589 Symbol = *RelocSym;
2590 }
2591 reloc_found = true;
2592 break;
2593 }
2594 }
2595 // If there is an external relocation entry for a symbol in this section
2596 // at this section_offset then use that symbol's value for the n_value
2597 // and return its name.
2598 const char *SymbolName = nullptr;
2599 if (reloc_found && isExtern) {
Rafael Espindoladea00162015-07-03 17:44:18 +00002600 n_value = Symbol.getValue();
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002601 Expected<StringRef> NameOrError = Symbol.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00002602 if (!NameOrError)
2603 report_error(info->O->getFileName(), NameOrError.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002604 StringRef Name = *NameOrError;
2605 if (!Name.empty()) {
2606 SymbolName = Name.data();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002607 return SymbolName;
2608 }
2609 }
2610
2611 // TODO: For fully linked images, look through the external relocation
2612 // entries off the dynamic symtab command. For these the r_offset is from the
2613 // start of the first writeable segment in the Mach-O file. So the offset
2614 // to this section from that segment is passed to this routine by the caller,
2615 // as the database_offset. Which is the difference of the section's starting
2616 // address and the first writable segment.
2617 //
2618 // NOTE: need add passing the database_offset to this routine.
2619
Kevin Enderby0fc11822015-04-01 20:57:01 +00002620 // We did not find an external relocation entry so look up the ReferenceValue
2621 // as an address of a symbol and if found return that symbol's name.
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +00002622 SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002623
2624 return SymbolName;
2625}
2626
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002627static const char *get_symbol_32(uint32_t sect_offset, SectionRef S,
2628 DisassembleInfo *info,
2629 uint32_t ReferenceValue) {
2630 uint64_t n_value64;
2631 return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue);
2632}
2633
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002634// These are structs in the Objective-C meta data and read to produce the
2635// comments for disassembly. While these are part of the ABI they are no
Zachary Turner264b5d92017-06-07 03:48:56 +00002636// public defintions. So the are here not in include/llvm/BinaryFormat/MachO.h
2637// .
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002638
2639// The cfstring object in a 64-bit Mach-O file.
2640struct cfstring64_t {
2641 uint64_t isa; // class64_t * (64-bit pointer)
2642 uint64_t flags; // flag bits
2643 uint64_t characters; // char * (64-bit pointer)
2644 uint64_t length; // number of non-NULL characters in above
2645};
2646
2647// The class object in a 64-bit Mach-O file.
2648struct class64_t {
2649 uint64_t isa; // class64_t * (64-bit pointer)
2650 uint64_t superclass; // class64_t * (64-bit pointer)
2651 uint64_t cache; // Cache (64-bit pointer)
2652 uint64_t vtable; // IMP * (64-bit pointer)
2653 uint64_t data; // class_ro64_t * (64-bit pointer)
2654};
2655
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002656struct class32_t {
2657 uint32_t isa; /* class32_t * (32-bit pointer) */
2658 uint32_t superclass; /* class32_t * (32-bit pointer) */
2659 uint32_t cache; /* Cache (32-bit pointer) */
2660 uint32_t vtable; /* IMP * (32-bit pointer) */
2661 uint32_t data; /* class_ro32_t * (32-bit pointer) */
2662};
2663
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002664struct class_ro64_t {
2665 uint32_t flags;
2666 uint32_t instanceStart;
2667 uint32_t instanceSize;
2668 uint32_t reserved;
2669 uint64_t ivarLayout; // const uint8_t * (64-bit pointer)
2670 uint64_t name; // const char * (64-bit pointer)
2671 uint64_t baseMethods; // const method_list_t * (64-bit pointer)
2672 uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer)
2673 uint64_t ivars; // const ivar_list_t * (64-bit pointer)
2674 uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer)
2675 uint64_t baseProperties; // const struct objc_property_list (64-bit pointer)
2676};
2677
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002678struct class_ro32_t {
2679 uint32_t flags;
2680 uint32_t instanceStart;
2681 uint32_t instanceSize;
2682 uint32_t ivarLayout; /* const uint8_t * (32-bit pointer) */
2683 uint32_t name; /* const char * (32-bit pointer) */
2684 uint32_t baseMethods; /* const method_list_t * (32-bit pointer) */
2685 uint32_t baseProtocols; /* const protocol_list_t * (32-bit pointer) */
2686 uint32_t ivars; /* const ivar_list_t * (32-bit pointer) */
2687 uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */
2688 uint32_t baseProperties; /* const struct objc_property_list *
2689 (32-bit pointer) */
2690};
2691
2692/* Values for class_ro{64,32}_t->flags */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002693#define RO_META (1 << 0)
2694#define RO_ROOT (1 << 1)
2695#define RO_HAS_CXX_STRUCTORS (1 << 2)
2696
2697struct method_list64_t {
2698 uint32_t entsize;
2699 uint32_t count;
2700 /* struct method64_t first; These structures follow inline */
2701};
2702
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002703struct method_list32_t {
2704 uint32_t entsize;
2705 uint32_t count;
2706 /* struct method32_t first; These structures follow inline */
2707};
2708
Kevin Enderby0fc11822015-04-01 20:57:01 +00002709struct method64_t {
2710 uint64_t name; /* SEL (64-bit pointer) */
2711 uint64_t types; /* const char * (64-bit pointer) */
2712 uint64_t imp; /* IMP (64-bit pointer) */
2713};
2714
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002715struct method32_t {
2716 uint32_t name; /* SEL (32-bit pointer) */
2717 uint32_t types; /* const char * (32-bit pointer) */
2718 uint32_t imp; /* IMP (32-bit pointer) */
2719};
2720
Kevin Enderby0fc11822015-04-01 20:57:01 +00002721struct protocol_list64_t {
2722 uint64_t count; /* uintptr_t (a 64-bit value) */
2723 /* struct protocol64_t * list[0]; These pointers follow inline */
2724};
2725
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002726struct protocol_list32_t {
2727 uint32_t count; /* uintptr_t (a 32-bit value) */
2728 /* struct protocol32_t * list[0]; These pointers follow inline */
2729};
2730
Kevin Enderby0fc11822015-04-01 20:57:01 +00002731struct protocol64_t {
2732 uint64_t isa; /* id * (64-bit pointer) */
2733 uint64_t name; /* const char * (64-bit pointer) */
2734 uint64_t protocols; /* struct protocol_list64_t *
2735 (64-bit pointer) */
2736 uint64_t instanceMethods; /* method_list_t * (64-bit pointer) */
2737 uint64_t classMethods; /* method_list_t * (64-bit pointer) */
2738 uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */
2739 uint64_t optionalClassMethods; /* method_list_t * (64-bit pointer) */
2740 uint64_t instanceProperties; /* struct objc_property_list *
2741 (64-bit pointer) */
2742};
2743
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002744struct protocol32_t {
2745 uint32_t isa; /* id * (32-bit pointer) */
2746 uint32_t name; /* const char * (32-bit pointer) */
2747 uint32_t protocols; /* struct protocol_list_t *
2748 (32-bit pointer) */
2749 uint32_t instanceMethods; /* method_list_t * (32-bit pointer) */
2750 uint32_t classMethods; /* method_list_t * (32-bit pointer) */
2751 uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */
2752 uint32_t optionalClassMethods; /* method_list_t * (32-bit pointer) */
2753 uint32_t instanceProperties; /* struct objc_property_list *
2754 (32-bit pointer) */
2755};
2756
Kevin Enderby0fc11822015-04-01 20:57:01 +00002757struct ivar_list64_t {
2758 uint32_t entsize;
2759 uint32_t count;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002760 /* struct ivar64_t first; These structures follow inline */
2761};
2762
2763struct ivar_list32_t {
2764 uint32_t entsize;
2765 uint32_t count;
2766 /* struct ivar32_t first; These structures follow inline */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002767};
2768
2769struct ivar64_t {
2770 uint64_t offset; /* uintptr_t * (64-bit pointer) */
2771 uint64_t name; /* const char * (64-bit pointer) */
2772 uint64_t type; /* const char * (64-bit pointer) */
2773 uint32_t alignment;
2774 uint32_t size;
2775};
2776
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002777struct ivar32_t {
2778 uint32_t offset; /* uintptr_t * (32-bit pointer) */
2779 uint32_t name; /* const char * (32-bit pointer) */
2780 uint32_t type; /* const char * (32-bit pointer) */
2781 uint32_t alignment;
2782 uint32_t size;
2783};
2784
Kevin Enderby0fc11822015-04-01 20:57:01 +00002785struct objc_property_list64 {
2786 uint32_t entsize;
2787 uint32_t count;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002788 /* struct objc_property64 first; These structures follow inline */
2789};
2790
2791struct objc_property_list32 {
2792 uint32_t entsize;
2793 uint32_t count;
2794 /* struct objc_property32 first; These structures follow inline */
Kevin Enderby0fc11822015-04-01 20:57:01 +00002795};
2796
2797struct objc_property64 {
2798 uint64_t name; /* const char * (64-bit pointer) */
2799 uint64_t attributes; /* const char * (64-bit pointer) */
2800};
2801
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002802struct objc_property32 {
2803 uint32_t name; /* const char * (32-bit pointer) */
2804 uint32_t attributes; /* const char * (32-bit pointer) */
2805};
2806
Kevin Enderby0fc11822015-04-01 20:57:01 +00002807struct category64_t {
2808 uint64_t name; /* const char * (64-bit pointer) */
2809 uint64_t cls; /* struct class_t * (64-bit pointer) */
2810 uint64_t instanceMethods; /* struct method_list_t * (64-bit pointer) */
2811 uint64_t classMethods; /* struct method_list_t * (64-bit pointer) */
2812 uint64_t protocols; /* struct protocol_list_t * (64-bit pointer) */
2813 uint64_t instanceProperties; /* struct objc_property_list *
2814 (64-bit pointer) */
2815};
2816
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002817struct category32_t {
2818 uint32_t name; /* const char * (32-bit pointer) */
2819 uint32_t cls; /* struct class_t * (32-bit pointer) */
2820 uint32_t instanceMethods; /* struct method_list_t * (32-bit pointer) */
2821 uint32_t classMethods; /* struct method_list_t * (32-bit pointer) */
2822 uint32_t protocols; /* struct protocol_list_t * (32-bit pointer) */
2823 uint32_t instanceProperties; /* struct objc_property_list *
2824 (32-bit pointer) */
2825};
2826
Kevin Enderby0fc11822015-04-01 20:57:01 +00002827struct objc_image_info64 {
2828 uint32_t version;
2829 uint32_t flags;
2830};
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002831struct objc_image_info32 {
2832 uint32_t version;
2833 uint32_t flags;
2834};
Kevin Enderby846c0002015-04-16 17:19:59 +00002835struct imageInfo_t {
2836 uint32_t version;
2837 uint32_t flags;
2838};
Kevin Enderby0fc11822015-04-01 20:57:01 +00002839/* masks for objc_image_info.flags */
2840#define OBJC_IMAGE_IS_REPLACEMENT (1 << 0)
2841#define OBJC_IMAGE_SUPPORTS_GC (1 << 1)
2842
2843struct message_ref64 {
2844 uint64_t imp; /* IMP (64-bit pointer) */
2845 uint64_t sel; /* SEL (64-bit pointer) */
2846};
2847
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002848struct message_ref32 {
2849 uint32_t imp; /* IMP (32-bit pointer) */
2850 uint32_t sel; /* SEL (32-bit pointer) */
2851};
2852
Kevin Enderby846c0002015-04-16 17:19:59 +00002853// Objective-C 1 (32-bit only) meta data structs.
2854
2855struct objc_module_t {
2856 uint32_t version;
2857 uint32_t size;
2858 uint32_t name; /* char * (32-bit pointer) */
2859 uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */
2860};
2861
2862struct objc_symtab_t {
2863 uint32_t sel_ref_cnt;
2864 uint32_t refs; /* SEL * (32-bit pointer) */
2865 uint16_t cls_def_cnt;
2866 uint16_t cat_def_cnt;
2867 // uint32_t defs[1]; /* void * (32-bit pointer) variable size */
2868};
2869
2870struct objc_class_t {
2871 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2872 uint32_t super_class; /* struct objc_class * (32-bit pointer) */
2873 uint32_t name; /* const char * (32-bit pointer) */
2874 int32_t version;
2875 int32_t info;
2876 int32_t instance_size;
2877 uint32_t ivars; /* struct objc_ivar_list * (32-bit pointer) */
2878 uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */
2879 uint32_t cache; /* struct objc_cache * (32-bit pointer) */
2880 uint32_t protocols; /* struct objc_protocol_list * (32-bit pointer) */
2881};
2882
2883#define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask))
2884// class is not a metaclass
2885#define CLS_CLASS 0x1
2886// class is a metaclass
2887#define CLS_META 0x2
2888
2889struct objc_category_t {
2890 uint32_t category_name; /* char * (32-bit pointer) */
2891 uint32_t class_name; /* char * (32-bit pointer) */
2892 uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */
2893 uint32_t class_methods; /* struct objc_method_list * (32-bit pointer) */
2894 uint32_t protocols; /* struct objc_protocol_list * (32-bit ptr) */
2895};
2896
2897struct objc_ivar_t {
2898 uint32_t ivar_name; /* char * (32-bit pointer) */
2899 uint32_t ivar_type; /* char * (32-bit pointer) */
2900 int32_t ivar_offset;
2901};
2902
2903struct objc_ivar_list_t {
2904 int32_t ivar_count;
2905 // struct objc_ivar_t ivar_list[1]; /* variable length structure */
2906};
2907
2908struct objc_method_list_t {
2909 uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */
2910 int32_t method_count;
2911 // struct objc_method_t method_list[1]; /* variable length structure */
2912};
2913
2914struct objc_method_t {
2915 uint32_t method_name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2916 uint32_t method_types; /* char * (32-bit pointer) */
2917 uint32_t method_imp; /* IMP, aka function pointer, (*IMP)(id, SEL, ...)
2918 (32-bit pointer) */
2919};
2920
2921struct objc_protocol_list_t {
2922 uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */
2923 int32_t count;
2924 // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t *
2925 // (32-bit pointer) */
2926};
2927
2928struct objc_protocol_t {
2929 uint32_t isa; /* struct objc_class * (32-bit pointer) */
2930 uint32_t protocol_name; /* char * (32-bit pointer) */
2931 uint32_t protocol_list; /* struct objc_protocol_list * (32-bit pointer) */
2932 uint32_t instance_methods; /* struct objc_method_description_list *
2933 (32-bit pointer) */
2934 uint32_t class_methods; /* struct objc_method_description_list *
2935 (32-bit pointer) */
2936};
2937
2938struct objc_method_description_list_t {
2939 int32_t count;
2940 // struct objc_method_description_t list[1];
2941};
2942
2943struct objc_method_description_t {
2944 uint32_t name; /* SEL, aka struct objc_selector * (32-bit pointer) */
2945 uint32_t types; /* char * (32-bit pointer) */
2946};
2947
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002948inline void swapStruct(struct cfstring64_t &cfs) {
2949 sys::swapByteOrder(cfs.isa);
2950 sys::swapByteOrder(cfs.flags);
2951 sys::swapByteOrder(cfs.characters);
2952 sys::swapByteOrder(cfs.length);
2953}
2954
2955inline void swapStruct(struct class64_t &c) {
2956 sys::swapByteOrder(c.isa);
2957 sys::swapByteOrder(c.superclass);
2958 sys::swapByteOrder(c.cache);
2959 sys::swapByteOrder(c.vtable);
2960 sys::swapByteOrder(c.data);
2961}
2962
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002963inline void swapStruct(struct class32_t &c) {
2964 sys::swapByteOrder(c.isa);
2965 sys::swapByteOrder(c.superclass);
2966 sys::swapByteOrder(c.cache);
2967 sys::swapByteOrder(c.vtable);
2968 sys::swapByteOrder(c.data);
2969}
2970
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002971inline void swapStruct(struct class_ro64_t &cro) {
2972 sys::swapByteOrder(cro.flags);
2973 sys::swapByteOrder(cro.instanceStart);
2974 sys::swapByteOrder(cro.instanceSize);
2975 sys::swapByteOrder(cro.reserved);
2976 sys::swapByteOrder(cro.ivarLayout);
2977 sys::swapByteOrder(cro.name);
2978 sys::swapByteOrder(cro.baseMethods);
2979 sys::swapByteOrder(cro.baseProtocols);
2980 sys::swapByteOrder(cro.ivars);
2981 sys::swapByteOrder(cro.weakIvarLayout);
2982 sys::swapByteOrder(cro.baseProperties);
2983}
2984
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002985inline void swapStruct(struct class_ro32_t &cro) {
2986 sys::swapByteOrder(cro.flags);
2987 sys::swapByteOrder(cro.instanceStart);
2988 sys::swapByteOrder(cro.instanceSize);
2989 sys::swapByteOrder(cro.ivarLayout);
2990 sys::swapByteOrder(cro.name);
2991 sys::swapByteOrder(cro.baseMethods);
2992 sys::swapByteOrder(cro.baseProtocols);
2993 sys::swapByteOrder(cro.ivars);
2994 sys::swapByteOrder(cro.weakIvarLayout);
2995 sys::swapByteOrder(cro.baseProperties);
2996}
2997
Kevin Enderby0fc11822015-04-01 20:57:01 +00002998inline void swapStruct(struct method_list64_t &ml) {
2999 sys::swapByteOrder(ml.entsize);
3000 sys::swapByteOrder(ml.count);
3001}
3002
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003003inline void swapStruct(struct method_list32_t &ml) {
3004 sys::swapByteOrder(ml.entsize);
3005 sys::swapByteOrder(ml.count);
3006}
3007
Kevin Enderby0fc11822015-04-01 20:57:01 +00003008inline void swapStruct(struct method64_t &m) {
3009 sys::swapByteOrder(m.name);
3010 sys::swapByteOrder(m.types);
3011 sys::swapByteOrder(m.imp);
3012}
3013
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003014inline void swapStruct(struct method32_t &m) {
3015 sys::swapByteOrder(m.name);
3016 sys::swapByteOrder(m.types);
3017 sys::swapByteOrder(m.imp);
3018}
3019
Kevin Enderby0fc11822015-04-01 20:57:01 +00003020inline void swapStruct(struct protocol_list64_t &pl) {
3021 sys::swapByteOrder(pl.count);
3022}
3023
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003024inline void swapStruct(struct protocol_list32_t &pl) {
3025 sys::swapByteOrder(pl.count);
3026}
3027
Kevin Enderby0fc11822015-04-01 20:57:01 +00003028inline void swapStruct(struct protocol64_t &p) {
3029 sys::swapByteOrder(p.isa);
3030 sys::swapByteOrder(p.name);
3031 sys::swapByteOrder(p.protocols);
3032 sys::swapByteOrder(p.instanceMethods);
3033 sys::swapByteOrder(p.classMethods);
3034 sys::swapByteOrder(p.optionalInstanceMethods);
3035 sys::swapByteOrder(p.optionalClassMethods);
3036 sys::swapByteOrder(p.instanceProperties);
3037}
3038
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003039inline void swapStruct(struct protocol32_t &p) {
3040 sys::swapByteOrder(p.isa);
3041 sys::swapByteOrder(p.name);
3042 sys::swapByteOrder(p.protocols);
3043 sys::swapByteOrder(p.instanceMethods);
3044 sys::swapByteOrder(p.classMethods);
3045 sys::swapByteOrder(p.optionalInstanceMethods);
3046 sys::swapByteOrder(p.optionalClassMethods);
3047 sys::swapByteOrder(p.instanceProperties);
3048}
3049
Kevin Enderby0fc11822015-04-01 20:57:01 +00003050inline void swapStruct(struct ivar_list64_t &il) {
3051 sys::swapByteOrder(il.entsize);
3052 sys::swapByteOrder(il.count);
3053}
3054
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003055inline void swapStruct(struct ivar_list32_t &il) {
3056 sys::swapByteOrder(il.entsize);
3057 sys::swapByteOrder(il.count);
3058}
3059
Kevin Enderby0fc11822015-04-01 20:57:01 +00003060inline void swapStruct(struct ivar64_t &i) {
3061 sys::swapByteOrder(i.offset);
3062 sys::swapByteOrder(i.name);
3063 sys::swapByteOrder(i.type);
3064 sys::swapByteOrder(i.alignment);
3065 sys::swapByteOrder(i.size);
3066}
3067
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003068inline void swapStruct(struct ivar32_t &i) {
3069 sys::swapByteOrder(i.offset);
3070 sys::swapByteOrder(i.name);
3071 sys::swapByteOrder(i.type);
3072 sys::swapByteOrder(i.alignment);
3073 sys::swapByteOrder(i.size);
3074}
3075
Kevin Enderby0fc11822015-04-01 20:57:01 +00003076inline void swapStruct(struct objc_property_list64 &pl) {
3077 sys::swapByteOrder(pl.entsize);
3078 sys::swapByteOrder(pl.count);
3079}
3080
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003081inline void swapStruct(struct objc_property_list32 &pl) {
3082 sys::swapByteOrder(pl.entsize);
3083 sys::swapByteOrder(pl.count);
3084}
3085
Kevin Enderby0fc11822015-04-01 20:57:01 +00003086inline void swapStruct(struct objc_property64 &op) {
3087 sys::swapByteOrder(op.name);
3088 sys::swapByteOrder(op.attributes);
3089}
3090
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003091inline void swapStruct(struct objc_property32 &op) {
3092 sys::swapByteOrder(op.name);
3093 sys::swapByteOrder(op.attributes);
3094}
3095
Kevin Enderby0fc11822015-04-01 20:57:01 +00003096inline void swapStruct(struct category64_t &c) {
3097 sys::swapByteOrder(c.name);
3098 sys::swapByteOrder(c.cls);
3099 sys::swapByteOrder(c.instanceMethods);
3100 sys::swapByteOrder(c.classMethods);
3101 sys::swapByteOrder(c.protocols);
3102 sys::swapByteOrder(c.instanceProperties);
3103}
3104
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003105inline void swapStruct(struct category32_t &c) {
3106 sys::swapByteOrder(c.name);
3107 sys::swapByteOrder(c.cls);
3108 sys::swapByteOrder(c.instanceMethods);
3109 sys::swapByteOrder(c.classMethods);
3110 sys::swapByteOrder(c.protocols);
3111 sys::swapByteOrder(c.instanceProperties);
3112}
3113
Kevin Enderby0fc11822015-04-01 20:57:01 +00003114inline void swapStruct(struct objc_image_info64 &o) {
3115 sys::swapByteOrder(o.version);
3116 sys::swapByteOrder(o.flags);
3117}
3118
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003119inline void swapStruct(struct objc_image_info32 &o) {
3120 sys::swapByteOrder(o.version);
3121 sys::swapByteOrder(o.flags);
3122}
3123
Kevin Enderby846c0002015-04-16 17:19:59 +00003124inline void swapStruct(struct imageInfo_t &o) {
3125 sys::swapByteOrder(o.version);
3126 sys::swapByteOrder(o.flags);
3127}
3128
Kevin Enderby0fc11822015-04-01 20:57:01 +00003129inline void swapStruct(struct message_ref64 &mr) {
3130 sys::swapByteOrder(mr.imp);
3131 sys::swapByteOrder(mr.sel);
3132}
3133
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003134inline void swapStruct(struct message_ref32 &mr) {
3135 sys::swapByteOrder(mr.imp);
3136 sys::swapByteOrder(mr.sel);
3137}
3138
Kevin Enderby846c0002015-04-16 17:19:59 +00003139inline void swapStruct(struct objc_module_t &module) {
3140 sys::swapByteOrder(module.version);
3141 sys::swapByteOrder(module.size);
3142 sys::swapByteOrder(module.name);
3143 sys::swapByteOrder(module.symtab);
Jingyue Wufedecc42015-04-16 18:43:44 +00003144}
Kevin Enderby846c0002015-04-16 17:19:59 +00003145
3146inline void swapStruct(struct objc_symtab_t &symtab) {
3147 sys::swapByteOrder(symtab.sel_ref_cnt);
3148 sys::swapByteOrder(symtab.refs);
3149 sys::swapByteOrder(symtab.cls_def_cnt);
3150 sys::swapByteOrder(symtab.cat_def_cnt);
Jingyue Wufedecc42015-04-16 18:43:44 +00003151}
Kevin Enderby846c0002015-04-16 17:19:59 +00003152
3153inline void swapStruct(struct objc_class_t &objc_class) {
3154 sys::swapByteOrder(objc_class.isa);
3155 sys::swapByteOrder(objc_class.super_class);
3156 sys::swapByteOrder(objc_class.name);
3157 sys::swapByteOrder(objc_class.version);
3158 sys::swapByteOrder(objc_class.info);
3159 sys::swapByteOrder(objc_class.instance_size);
3160 sys::swapByteOrder(objc_class.ivars);
3161 sys::swapByteOrder(objc_class.methodLists);
3162 sys::swapByteOrder(objc_class.cache);
3163 sys::swapByteOrder(objc_class.protocols);
Jingyue Wufedecc42015-04-16 18:43:44 +00003164}
Kevin Enderby846c0002015-04-16 17:19:59 +00003165
3166inline void swapStruct(struct objc_category_t &objc_category) {
3167 sys::swapByteOrder(objc_category.category_name);
3168 sys::swapByteOrder(objc_category.class_name);
3169 sys::swapByteOrder(objc_category.instance_methods);
3170 sys::swapByteOrder(objc_category.class_methods);
3171 sys::swapByteOrder(objc_category.protocols);
3172}
3173
3174inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) {
3175 sys::swapByteOrder(objc_ivar_list.ivar_count);
3176}
3177
3178inline void swapStruct(struct objc_ivar_t &objc_ivar) {
3179 sys::swapByteOrder(objc_ivar.ivar_name);
3180 sys::swapByteOrder(objc_ivar.ivar_type);
3181 sys::swapByteOrder(objc_ivar.ivar_offset);
Jingyue Wufedecc42015-04-16 18:43:44 +00003182}
Kevin Enderby846c0002015-04-16 17:19:59 +00003183
3184inline void swapStruct(struct objc_method_list_t &method_list) {
3185 sys::swapByteOrder(method_list.obsolete);
3186 sys::swapByteOrder(method_list.method_count);
3187}
3188
3189inline void swapStruct(struct objc_method_t &method) {
3190 sys::swapByteOrder(method.method_name);
3191 sys::swapByteOrder(method.method_types);
3192 sys::swapByteOrder(method.method_imp);
3193}
3194
3195inline void swapStruct(struct objc_protocol_list_t &protocol_list) {
3196 sys::swapByteOrder(protocol_list.next);
3197 sys::swapByteOrder(protocol_list.count);
3198}
3199
3200inline void swapStruct(struct objc_protocol_t &protocol) {
3201 sys::swapByteOrder(protocol.isa);
3202 sys::swapByteOrder(protocol.protocol_name);
3203 sys::swapByteOrder(protocol.protocol_list);
3204 sys::swapByteOrder(protocol.instance_methods);
3205 sys::swapByteOrder(protocol.class_methods);
3206}
3207
3208inline void swapStruct(struct objc_method_description_list_t &mdl) {
3209 sys::swapByteOrder(mdl.count);
3210}
3211
3212inline void swapStruct(struct objc_method_description_t &md) {
3213 sys::swapByteOrder(md.name);
3214 sys::swapByteOrder(md.types);
3215}
3216
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003217static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
3218 struct DisassembleInfo *info);
3219
3220// get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
3221// to an Objective-C class and returns the class name. It is also passed the
3222// address of the pointer, so when the pointer is zero as it can be in an .o
3223// file, that is used to look for an external relocation entry with a symbol
3224// name.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003225static const char *get_objc2_64bit_class_name(uint64_t pointer_value,
3226 uint64_t ReferenceValue,
3227 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003228 const char *r;
3229 uint32_t offset, left;
3230 SectionRef S;
3231
3232 // The pointer_value can be 0 in an object file and have a relocation
3233 // entry for the class symbol at the ReferenceValue (the address of the
3234 // pointer).
3235 if (pointer_value == 0) {
3236 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3237 if (r == nullptr || left < sizeof(uint64_t))
3238 return nullptr;
3239 uint64_t n_value;
3240 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3241 if (symbol_name == nullptr)
3242 return nullptr;
Hans Wennborgdb53e302014-10-23 21:59:17 +00003243 const char *class_name = strrchr(symbol_name, '$');
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003244 if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0')
3245 return class_name + 2;
3246 else
3247 return nullptr;
3248 }
3249
3250 // The case were the pointer_value is non-zero and points to a class defined
3251 // in this Mach-O file.
3252 r = get_pointer_64(pointer_value, offset, left, S, info);
3253 if (r == nullptr || left < sizeof(struct class64_t))
3254 return nullptr;
3255 struct class64_t c;
3256 memcpy(&c, r, sizeof(struct class64_t));
3257 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3258 swapStruct(c);
3259 if (c.data == 0)
3260 return nullptr;
3261 r = get_pointer_64(c.data, offset, left, S, info);
3262 if (r == nullptr || left < sizeof(struct class_ro64_t))
3263 return nullptr;
3264 struct class_ro64_t cro;
3265 memcpy(&cro, r, sizeof(struct class_ro64_t));
3266 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3267 swapStruct(cro);
3268 if (cro.name == 0)
3269 return nullptr;
3270 const char *name = get_pointer_64(cro.name, offset, left, S, info);
3271 return name;
3272}
3273
3274// get_objc2_64bit_cfstring_name is used for disassembly and is passed a
3275// pointer to a cfstring and returns its name or nullptr.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003276static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue,
3277 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003278 const char *r, *name;
3279 uint32_t offset, left;
3280 SectionRef S;
3281 struct cfstring64_t cfs;
3282 uint64_t cfs_characters;
3283
3284 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3285 if (r == nullptr || left < sizeof(struct cfstring64_t))
3286 return nullptr;
3287 memcpy(&cfs, r, sizeof(struct cfstring64_t));
3288 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3289 swapStruct(cfs);
3290 if (cfs.characters == 0) {
3291 uint64_t n_value;
3292 const char *symbol_name = get_symbol_64(
3293 offset + offsetof(struct cfstring64_t, characters), S, info, n_value);
3294 if (symbol_name == nullptr)
3295 return nullptr;
3296 cfs_characters = n_value;
3297 } else
3298 cfs_characters = cfs.characters;
3299 name = get_pointer_64(cfs_characters, offset, left, S, info);
3300
3301 return name;
3302}
3303
3304// get_objc2_64bit_selref() is used for disassembly and is passed a the address
3305// of a pointer to an Objective-C selector reference when the pointer value is
3306// zero as in a .o file and is likely to have a external relocation entry with
3307// who's symbol's n_value is the real pointer to the selector name. If that is
3308// the case the real pointer to the selector name is returned else 0 is
3309// returned
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003310static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue,
3311 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003312 uint32_t offset, left;
3313 SectionRef S;
3314
3315 const char *r = get_pointer_64(ReferenceValue, offset, left, S, info);
3316 if (r == nullptr || left < sizeof(uint64_t))
3317 return 0;
3318 uint64_t n_value;
3319 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3320 if (symbol_name == nullptr)
3321 return 0;
3322 return n_value;
3323}
3324
Kevin Enderby0fc11822015-04-01 20:57:01 +00003325static const SectionRef get_section(MachOObjectFile *O, const char *segname,
3326 const char *sectname) {
3327 for (const SectionRef &Section : O->sections()) {
3328 StringRef SectName;
3329 Section.getName(SectName);
3330 DataRefImpl Ref = Section.getRawDataRefImpl();
3331 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3332 if (SegName == segname && SectName == sectname)
3333 return Section;
3334 }
3335 return SectionRef();
3336}
3337
3338static void
3339walk_pointer_list_64(const char *listname, const SectionRef S,
3340 MachOObjectFile *O, struct DisassembleInfo *info,
3341 void (*func)(uint64_t, struct DisassembleInfo *info)) {
3342 if (S == SectionRef())
3343 return;
3344
3345 StringRef SectName;
3346 S.getName(SectName);
3347 DataRefImpl Ref = S.getRawDataRefImpl();
3348 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3349 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3350
3351 StringRef BytesStr;
3352 S.getContents(BytesStr);
3353 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3354
3355 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) {
3356 uint32_t left = S.getSize() - i;
3357 uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t);
3358 uint64_t p = 0;
3359 memcpy(&p, Contents + i, size);
3360 if (i + sizeof(uint64_t) > S.getSize())
3361 outs() << listname << " list pointer extends past end of (" << SegName
3362 << "," << SectName << ") section\n";
3363 outs() << format("%016" PRIx64, S.getAddress() + i) << " ";
3364
3365 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3366 sys::swapByteOrder(p);
3367
3368 uint64_t n_value = 0;
3369 const char *name = get_symbol_64(i, S, info, n_value, p);
3370 if (name == nullptr)
3371 name = get_dyld_bind_info_symbolname(S.getAddress() + i, info);
3372
3373 if (n_value != 0) {
3374 outs() << format("0x%" PRIx64, n_value);
3375 if (p != 0)
3376 outs() << " + " << format("0x%" PRIx64, p);
3377 } else
3378 outs() << format("0x%" PRIx64, p);
3379 if (name != nullptr)
3380 outs() << " " << name;
3381 outs() << "\n";
3382
3383 p += n_value;
3384 if (func)
3385 func(p, info);
3386 }
3387}
3388
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003389static void
3390walk_pointer_list_32(const char *listname, const SectionRef S,
3391 MachOObjectFile *O, struct DisassembleInfo *info,
3392 void (*func)(uint32_t, struct DisassembleInfo *info)) {
3393 if (S == SectionRef())
3394 return;
3395
3396 StringRef SectName;
3397 S.getName(SectName);
3398 DataRefImpl Ref = S.getRawDataRefImpl();
3399 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3400 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3401
3402 StringRef BytesStr;
3403 S.getContents(BytesStr);
3404 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3405
3406 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) {
3407 uint32_t left = S.getSize() - i;
3408 uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t);
3409 uint32_t p = 0;
3410 memcpy(&p, Contents + i, size);
3411 if (i + sizeof(uint32_t) > S.getSize())
3412 outs() << listname << " list pointer extends past end of (" << SegName
3413 << "," << SectName << ") section\n";
Kevin Enderbycf261312015-04-06 22:33:43 +00003414 uint32_t Address = S.getAddress() + i;
3415 outs() << format("%08" PRIx32, Address) << " ";
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003416
3417 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3418 sys::swapByteOrder(p);
3419 outs() << format("0x%" PRIx32, p);
3420
3421 const char *name = get_symbol_32(i, S, info, p);
3422 if (name != nullptr)
3423 outs() << " " << name;
3424 outs() << "\n";
3425
3426 if (func)
3427 func(p, info);
3428 }
3429}
3430
3431static void print_layout_map(const char *layout_map, uint32_t left) {
Kevin Enderbya59824a2015-10-06 22:27:08 +00003432 if (layout_map == nullptr)
3433 return;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003434 outs() << " layout map: ";
3435 do {
3436 outs() << format("0x%02" PRIx32, (*layout_map) & 0xff) << " ";
3437 left--;
3438 layout_map++;
3439 } while (*layout_map != '\0' && left != 0);
3440 outs() << "\n";
3441}
3442
Kevin Enderby0fc11822015-04-01 20:57:01 +00003443static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) {
3444 uint32_t offset, left;
3445 SectionRef S;
3446 const char *layout_map;
3447
3448 if (p == 0)
3449 return;
3450 layout_map = get_pointer_64(p, offset, left, S, info);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003451 print_layout_map(layout_map, left);
3452}
3453
3454static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) {
3455 uint32_t offset, left;
3456 SectionRef S;
3457 const char *layout_map;
3458
3459 if (p == 0)
3460 return;
3461 layout_map = get_pointer_32(p, offset, left, S, info);
3462 print_layout_map(layout_map, left);
Kevin Enderby0fc11822015-04-01 20:57:01 +00003463}
3464
3465static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info,
3466 const char *indent) {
3467 struct method_list64_t ml;
3468 struct method64_t m;
3469 const char *r;
3470 uint32_t offset, xoffset, left, i;
3471 SectionRef S, xS;
3472 const char *name, *sym_name;
3473 uint64_t n_value;
3474
3475 r = get_pointer_64(p, offset, left, S, info);
3476 if (r == nullptr)
3477 return;
3478 memset(&ml, '\0', sizeof(struct method_list64_t));
3479 if (left < sizeof(struct method_list64_t)) {
3480 memcpy(&ml, r, left);
3481 outs() << " (method_list_t entends past the end of the section)\n";
3482 } else
3483 memcpy(&ml, r, sizeof(struct method_list64_t));
3484 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3485 swapStruct(ml);
3486 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3487 outs() << indent << "\t\t count " << ml.count << "\n";
3488
3489 p += sizeof(struct method_list64_t);
3490 offset += sizeof(struct method_list64_t);
3491 for (i = 0; i < ml.count; i++) {
3492 r = get_pointer_64(p, offset, left, S, info);
3493 if (r == nullptr)
3494 return;
3495 memset(&m, '\0', sizeof(struct method64_t));
3496 if (left < sizeof(struct method64_t)) {
Kevin Enderbya59824a2015-10-06 22:27:08 +00003497 memcpy(&m, r, left);
3498 outs() << indent << " (method_t extends past the end of the section)\n";
Kevin Enderby0fc11822015-04-01 20:57:01 +00003499 } else
3500 memcpy(&m, r, sizeof(struct method64_t));
3501 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3502 swapStruct(m);
3503
3504 outs() << indent << "\t\t name ";
3505 sym_name = get_symbol_64(offset + offsetof(struct method64_t, name), S,
3506 info, n_value, m.name);
3507 if (n_value != 0) {
3508 if (info->verbose && sym_name != nullptr)
3509 outs() << sym_name;
3510 else
3511 outs() << format("0x%" PRIx64, n_value);
3512 if (m.name != 0)
3513 outs() << " + " << format("0x%" PRIx64, m.name);
3514 } else
3515 outs() << format("0x%" PRIx64, m.name);
3516 name = get_pointer_64(m.name + n_value, xoffset, left, xS, info);
3517 if (name != nullptr)
3518 outs() << format(" %.*s", left, name);
3519 outs() << "\n";
3520
3521 outs() << indent << "\t\t types ";
3522 sym_name = get_symbol_64(offset + offsetof(struct method64_t, types), S,
3523 info, n_value, m.types);
3524 if (n_value != 0) {
3525 if (info->verbose && sym_name != nullptr)
3526 outs() << sym_name;
3527 else
3528 outs() << format("0x%" PRIx64, n_value);
3529 if (m.types != 0)
3530 outs() << " + " << format("0x%" PRIx64, m.types);
3531 } else
3532 outs() << format("0x%" PRIx64, m.types);
3533 name = get_pointer_64(m.types + n_value, xoffset, left, xS, info);
3534 if (name != nullptr)
3535 outs() << format(" %.*s", left, name);
3536 outs() << "\n";
3537
3538 outs() << indent << "\t\t imp ";
3539 name = get_symbol_64(offset + offsetof(struct method64_t, imp), S, info,
3540 n_value, m.imp);
3541 if (info->verbose && name == nullptr) {
3542 if (n_value != 0) {
3543 outs() << format("0x%" PRIx64, n_value) << " ";
3544 if (m.imp != 0)
3545 outs() << "+ " << format("0x%" PRIx64, m.imp) << " ";
3546 } else
3547 outs() << format("0x%" PRIx64, m.imp) << " ";
3548 }
3549 if (name != nullptr)
3550 outs() << name;
3551 outs() << "\n";
3552
3553 p += sizeof(struct method64_t);
3554 offset += sizeof(struct method64_t);
3555 }
3556}
3557
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003558static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info,
3559 const char *indent) {
3560 struct method_list32_t ml;
3561 struct method32_t m;
Kevin Enderby846c0002015-04-16 17:19:59 +00003562 const char *r, *name;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003563 uint32_t offset, xoffset, left, i;
3564 SectionRef S, xS;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003565
3566 r = get_pointer_32(p, offset, left, S, info);
3567 if (r == nullptr)
3568 return;
3569 memset(&ml, '\0', sizeof(struct method_list32_t));
3570 if (left < sizeof(struct method_list32_t)) {
3571 memcpy(&ml, r, left);
3572 outs() << " (method_list_t entends past the end of the section)\n";
3573 } else
3574 memcpy(&ml, r, sizeof(struct method_list32_t));
3575 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3576 swapStruct(ml);
3577 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3578 outs() << indent << "\t\t count " << ml.count << "\n";
3579
3580 p += sizeof(struct method_list32_t);
3581 offset += sizeof(struct method_list32_t);
3582 for (i = 0; i < ml.count; i++) {
3583 r = get_pointer_32(p, offset, left, S, info);
3584 if (r == nullptr)
3585 return;
3586 memset(&m, '\0', sizeof(struct method32_t));
3587 if (left < sizeof(struct method32_t)) {
3588 memcpy(&ml, r, left);
3589 outs() << indent << " (method_t entends past the end of the section)\n";
3590 } else
3591 memcpy(&m, r, sizeof(struct method32_t));
3592 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3593 swapStruct(m);
3594
3595 outs() << indent << "\t\t name " << format("0x%" PRIx32, m.name);
3596 name = get_pointer_32(m.name, xoffset, left, xS, info);
3597 if (name != nullptr)
3598 outs() << format(" %.*s", left, name);
3599 outs() << "\n";
3600
3601 outs() << indent << "\t\t types " << format("0x%" PRIx32, m.types);
3602 name = get_pointer_32(m.types, xoffset, left, xS, info);
3603 if (name != nullptr)
3604 outs() << format(" %.*s", left, name);
3605 outs() << "\n";
3606
3607 outs() << indent << "\t\t imp " << format("0x%" PRIx32, m.imp);
3608 name = get_symbol_32(offset + offsetof(struct method32_t, imp), S, info,
3609 m.imp);
3610 if (name != nullptr)
3611 outs() << " " << name;
3612 outs() << "\n";
3613
3614 p += sizeof(struct method32_t);
3615 offset += sizeof(struct method32_t);
3616 }
3617}
3618
Kevin Enderby846c0002015-04-16 17:19:59 +00003619static bool print_method_list(uint32_t p, struct DisassembleInfo *info) {
3620 uint32_t offset, left, xleft;
3621 SectionRef S;
3622 struct objc_method_list_t method_list;
3623 struct objc_method_t method;
3624 const char *r, *methods, *name, *SymbolName;
3625 int32_t i;
3626
3627 r = get_pointer_32(p, offset, left, S, info, true);
3628 if (r == nullptr)
3629 return true;
3630
3631 outs() << "\n";
3632 if (left > sizeof(struct objc_method_list_t)) {
3633 memcpy(&method_list, r, sizeof(struct objc_method_list_t));
3634 } else {
3635 outs() << "\t\t objc_method_list extends past end of the section\n";
3636 memset(&method_list, '\0', sizeof(struct objc_method_list_t));
3637 memcpy(&method_list, r, left);
3638 }
3639 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3640 swapStruct(method_list);
3641
3642 outs() << "\t\t obsolete "
3643 << format("0x%08" PRIx32, method_list.obsolete) << "\n";
3644 outs() << "\t\t method_count " << method_list.method_count << "\n";
3645
3646 methods = r + sizeof(struct objc_method_list_t);
3647 for (i = 0; i < method_list.method_count; i++) {
3648 if ((i + 1) * sizeof(struct objc_method_t) > left) {
3649 outs() << "\t\t remaining method's extend past the of the section\n";
3650 break;
3651 }
3652 memcpy(&method, methods + i * sizeof(struct objc_method_t),
3653 sizeof(struct objc_method_t));
3654 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3655 swapStruct(method);
3656
3657 outs() << "\t\t method_name "
3658 << format("0x%08" PRIx32, method.method_name);
3659 if (info->verbose) {
3660 name = get_pointer_32(method.method_name, offset, xleft, S, info, true);
3661 if (name != nullptr)
3662 outs() << format(" %.*s", xleft, name);
3663 else
3664 outs() << " (not in an __OBJC section)";
3665 }
3666 outs() << "\n";
3667
3668 outs() << "\t\t method_types "
3669 << format("0x%08" PRIx32, method.method_types);
3670 if (info->verbose) {
3671 name = get_pointer_32(method.method_types, offset, xleft, S, info, true);
3672 if (name != nullptr)
3673 outs() << format(" %.*s", xleft, name);
3674 else
3675 outs() << " (not in an __OBJC section)";
3676 }
3677 outs() << "\n";
3678
3679 outs() << "\t\t method_imp "
3680 << format("0x%08" PRIx32, method.method_imp) << " ";
3681 if (info->verbose) {
3682 SymbolName = GuessSymbolName(method.method_imp, info->AddrMap);
3683 if (SymbolName != nullptr)
3684 outs() << SymbolName;
3685 }
3686 outs() << "\n";
3687 }
3688 return false;
3689}
3690
Kevin Enderby0fc11822015-04-01 20:57:01 +00003691static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) {
3692 struct protocol_list64_t pl;
3693 uint64_t q, n_value;
3694 struct protocol64_t pc;
3695 const char *r;
3696 uint32_t offset, xoffset, left, i;
3697 SectionRef S, xS;
3698 const char *name, *sym_name;
3699
3700 r = get_pointer_64(p, offset, left, S, info);
3701 if (r == nullptr)
3702 return;
3703 memset(&pl, '\0', sizeof(struct protocol_list64_t));
3704 if (left < sizeof(struct protocol_list64_t)) {
3705 memcpy(&pl, r, left);
3706 outs() << " (protocol_list_t entends past the end of the section)\n";
3707 } else
3708 memcpy(&pl, r, sizeof(struct protocol_list64_t));
3709 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3710 swapStruct(pl);
3711 outs() << " count " << pl.count << "\n";
3712
3713 p += sizeof(struct protocol_list64_t);
3714 offset += sizeof(struct protocol_list64_t);
3715 for (i = 0; i < pl.count; i++) {
3716 r = get_pointer_64(p, offset, left, S, info);
3717 if (r == nullptr)
3718 return;
3719 q = 0;
3720 if (left < sizeof(uint64_t)) {
3721 memcpy(&q, r, left);
3722 outs() << " (protocol_t * entends past the end of the section)\n";
3723 } else
3724 memcpy(&q, r, sizeof(uint64_t));
3725 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3726 sys::swapByteOrder(q);
3727
3728 outs() << "\t\t list[" << i << "] ";
3729 sym_name = get_symbol_64(offset, S, info, n_value, q);
3730 if (n_value != 0) {
3731 if (info->verbose && sym_name != nullptr)
3732 outs() << sym_name;
3733 else
3734 outs() << format("0x%" PRIx64, n_value);
3735 if (q != 0)
3736 outs() << " + " << format("0x%" PRIx64, q);
3737 } else
3738 outs() << format("0x%" PRIx64, q);
3739 outs() << " (struct protocol_t *)\n";
3740
3741 r = get_pointer_64(q + n_value, offset, left, S, info);
3742 if (r == nullptr)
3743 return;
3744 memset(&pc, '\0', sizeof(struct protocol64_t));
3745 if (left < sizeof(struct protocol64_t)) {
3746 memcpy(&pc, r, left);
3747 outs() << " (protocol_t entends past the end of the section)\n";
3748 } else
3749 memcpy(&pc, r, sizeof(struct protocol64_t));
3750 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3751 swapStruct(pc);
3752
3753 outs() << "\t\t\t isa " << format("0x%" PRIx64, pc.isa) << "\n";
3754
3755 outs() << "\t\t\t name ";
3756 sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name), S,
3757 info, n_value, pc.name);
3758 if (n_value != 0) {
3759 if (info->verbose && sym_name != nullptr)
3760 outs() << sym_name;
3761 else
3762 outs() << format("0x%" PRIx64, n_value);
3763 if (pc.name != 0)
3764 outs() << " + " << format("0x%" PRIx64, pc.name);
3765 } else
3766 outs() << format("0x%" PRIx64, pc.name);
3767 name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info);
3768 if (name != nullptr)
3769 outs() << format(" %.*s", left, name);
3770 outs() << "\n";
3771
3772 outs() << "\t\t\tprotocols " << format("0x%" PRIx64, pc.protocols) << "\n";
3773
3774 outs() << "\t\t instanceMethods ";
3775 sym_name =
3776 get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods),
3777 S, info, n_value, pc.instanceMethods);
3778 if (n_value != 0) {
3779 if (info->verbose && sym_name != nullptr)
3780 outs() << sym_name;
3781 else
3782 outs() << format("0x%" PRIx64, n_value);
3783 if (pc.instanceMethods != 0)
3784 outs() << " + " << format("0x%" PRIx64, pc.instanceMethods);
3785 } else
3786 outs() << format("0x%" PRIx64, pc.instanceMethods);
3787 outs() << " (struct method_list_t *)\n";
3788 if (pc.instanceMethods + n_value != 0)
3789 print_method_list64_t(pc.instanceMethods + n_value, info, "\t");
3790
3791 outs() << "\t\t classMethods ";
3792 sym_name =
3793 get_symbol_64(offset + offsetof(struct protocol64_t, classMethods), S,
3794 info, n_value, pc.classMethods);
3795 if (n_value != 0) {
3796 if (info->verbose && sym_name != nullptr)
3797 outs() << sym_name;
3798 else
3799 outs() << format("0x%" PRIx64, n_value);
3800 if (pc.classMethods != 0)
3801 outs() << " + " << format("0x%" PRIx64, pc.classMethods);
3802 } else
3803 outs() << format("0x%" PRIx64, pc.classMethods);
3804 outs() << " (struct method_list_t *)\n";
3805 if (pc.classMethods + n_value != 0)
3806 print_method_list64_t(pc.classMethods + n_value, info, "\t");
3807
3808 outs() << "\t optionalInstanceMethods "
3809 << format("0x%" PRIx64, pc.optionalInstanceMethods) << "\n";
3810 outs() << "\t optionalClassMethods "
3811 << format("0x%" PRIx64, pc.optionalClassMethods) << "\n";
3812 outs() << "\t instanceProperties "
3813 << format("0x%" PRIx64, pc.instanceProperties) << "\n";
3814
3815 p += sizeof(uint64_t);
3816 offset += sizeof(uint64_t);
3817 }
3818}
3819
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003820static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) {
3821 struct protocol_list32_t pl;
3822 uint32_t q;
3823 struct protocol32_t pc;
3824 const char *r;
3825 uint32_t offset, xoffset, left, i;
3826 SectionRef S, xS;
3827 const char *name;
3828
3829 r = get_pointer_32(p, offset, left, S, info);
3830 if (r == nullptr)
3831 return;
3832 memset(&pl, '\0', sizeof(struct protocol_list32_t));
3833 if (left < sizeof(struct protocol_list32_t)) {
3834 memcpy(&pl, r, left);
3835 outs() << " (protocol_list_t entends past the end of the section)\n";
3836 } else
3837 memcpy(&pl, r, sizeof(struct protocol_list32_t));
3838 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3839 swapStruct(pl);
3840 outs() << " count " << pl.count << "\n";
3841
3842 p += sizeof(struct protocol_list32_t);
3843 offset += sizeof(struct protocol_list32_t);
3844 for (i = 0; i < pl.count; i++) {
3845 r = get_pointer_32(p, offset, left, S, info);
3846 if (r == nullptr)
3847 return;
3848 q = 0;
3849 if (left < sizeof(uint32_t)) {
3850 memcpy(&q, r, left);
3851 outs() << " (protocol_t * entends past the end of the section)\n";
3852 } else
3853 memcpy(&q, r, sizeof(uint32_t));
3854 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3855 sys::swapByteOrder(q);
3856 outs() << "\t\t list[" << i << "] " << format("0x%" PRIx32, q)
3857 << " (struct protocol_t *)\n";
3858 r = get_pointer_32(q, offset, left, S, info);
3859 if (r == nullptr)
3860 return;
3861 memset(&pc, '\0', sizeof(struct protocol32_t));
3862 if (left < sizeof(struct protocol32_t)) {
3863 memcpy(&pc, r, left);
3864 outs() << " (protocol_t entends past the end of the section)\n";
3865 } else
3866 memcpy(&pc, r, sizeof(struct protocol32_t));
3867 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3868 swapStruct(pc);
3869 outs() << "\t\t\t isa " << format("0x%" PRIx32, pc.isa) << "\n";
3870 outs() << "\t\t\t name " << format("0x%" PRIx32, pc.name);
3871 name = get_pointer_32(pc.name, xoffset, left, xS, info);
3872 if (name != nullptr)
3873 outs() << format(" %.*s", left, name);
3874 outs() << "\n";
3875 outs() << "\t\t\tprotocols " << format("0x%" PRIx32, pc.protocols) << "\n";
3876 outs() << "\t\t instanceMethods "
3877 << format("0x%" PRIx32, pc.instanceMethods)
3878 << " (struct method_list_t *)\n";
3879 if (pc.instanceMethods != 0)
3880 print_method_list32_t(pc.instanceMethods, info, "\t");
3881 outs() << "\t\t classMethods " << format("0x%" PRIx32, pc.classMethods)
3882 << " (struct method_list_t *)\n";
3883 if (pc.classMethods != 0)
3884 print_method_list32_t(pc.classMethods, info, "\t");
3885 outs() << "\t optionalInstanceMethods "
3886 << format("0x%" PRIx32, pc.optionalInstanceMethods) << "\n";
3887 outs() << "\t optionalClassMethods "
3888 << format("0x%" PRIx32, pc.optionalClassMethods) << "\n";
3889 outs() << "\t instanceProperties "
3890 << format("0x%" PRIx32, pc.instanceProperties) << "\n";
3891 p += sizeof(uint32_t);
3892 offset += sizeof(uint32_t);
3893 }
3894}
3895
Kevin Enderby846c0002015-04-16 17:19:59 +00003896static void print_indent(uint32_t indent) {
3897 for (uint32_t i = 0; i < indent;) {
3898 if (indent - i >= 8) {
3899 outs() << "\t";
3900 i += 8;
3901 } else {
3902 for (uint32_t j = i; j < indent; j++)
3903 outs() << " ";
3904 return;
3905 }
3906 }
3907}
3908
3909static bool print_method_description_list(uint32_t p, uint32_t indent,
3910 struct DisassembleInfo *info) {
3911 uint32_t offset, left, xleft;
3912 SectionRef S;
3913 struct objc_method_description_list_t mdl;
3914 struct objc_method_description_t md;
3915 const char *r, *list, *name;
3916 int32_t i;
3917
3918 r = get_pointer_32(p, offset, left, S, info, true);
3919 if (r == nullptr)
3920 return true;
3921
3922 outs() << "\n";
3923 if (left > sizeof(struct objc_method_description_list_t)) {
3924 memcpy(&mdl, r, sizeof(struct objc_method_description_list_t));
3925 } else {
3926 print_indent(indent);
3927 outs() << " objc_method_description_list extends past end of the section\n";
3928 memset(&mdl, '\0', sizeof(struct objc_method_description_list_t));
3929 memcpy(&mdl, r, left);
3930 }
3931 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3932 swapStruct(mdl);
3933
3934 print_indent(indent);
3935 outs() << " count " << mdl.count << "\n";
3936
3937 list = r + sizeof(struct objc_method_description_list_t);
3938 for (i = 0; i < mdl.count; i++) {
3939 if ((i + 1) * sizeof(struct objc_method_description_t) > left) {
3940 print_indent(indent);
3941 outs() << " remaining list entries extend past the of the section\n";
3942 break;
3943 }
3944 print_indent(indent);
3945 outs() << " list[" << i << "]\n";
3946 memcpy(&md, list + i * sizeof(struct objc_method_description_t),
3947 sizeof(struct objc_method_description_t));
3948 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3949 swapStruct(md);
3950
3951 print_indent(indent);
3952 outs() << " name " << format("0x%08" PRIx32, md.name);
3953 if (info->verbose) {
3954 name = get_pointer_32(md.name, offset, xleft, S, info, true);
3955 if (name != nullptr)
3956 outs() << format(" %.*s", xleft, name);
3957 else
3958 outs() << " (not in an __OBJC section)";
3959 }
3960 outs() << "\n";
3961
3962 print_indent(indent);
3963 outs() << " types " << format("0x%08" PRIx32, md.types);
3964 if (info->verbose) {
3965 name = get_pointer_32(md.types, offset, xleft, S, info, true);
3966 if (name != nullptr)
3967 outs() << format(" %.*s", xleft, name);
3968 else
3969 outs() << " (not in an __OBJC section)";
3970 }
3971 outs() << "\n";
3972 }
3973 return false;
3974}
3975
3976static bool print_protocol_list(uint32_t p, uint32_t indent,
3977 struct DisassembleInfo *info);
3978
3979static bool print_protocol(uint32_t p, uint32_t indent,
3980 struct DisassembleInfo *info) {
3981 uint32_t offset, left;
3982 SectionRef S;
3983 struct objc_protocol_t protocol;
3984 const char *r, *name;
3985
3986 r = get_pointer_32(p, offset, left, S, info, true);
3987 if (r == nullptr)
3988 return true;
3989
3990 outs() << "\n";
3991 if (left >= sizeof(struct objc_protocol_t)) {
3992 memcpy(&protocol, r, sizeof(struct objc_protocol_t));
3993 } else {
3994 print_indent(indent);
3995 outs() << " Protocol extends past end of the section\n";
3996 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
3997 memcpy(&protocol, r, left);
3998 }
3999 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4000 swapStruct(protocol);
4001
4002 print_indent(indent);
4003 outs() << " isa " << format("0x%08" PRIx32, protocol.isa)
4004 << "\n";
4005
4006 print_indent(indent);
4007 outs() << " protocol_name "
4008 << format("0x%08" PRIx32, protocol.protocol_name);
4009 if (info->verbose) {
4010 name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true);
4011 if (name != nullptr)
4012 outs() << format(" %.*s", left, name);
4013 else
4014 outs() << " (not in an __OBJC section)";
4015 }
4016 outs() << "\n";
4017
4018 print_indent(indent);
4019 outs() << " protocol_list "
4020 << format("0x%08" PRIx32, protocol.protocol_list);
4021 if (print_protocol_list(protocol.protocol_list, indent + 4, info))
4022 outs() << " (not in an __OBJC section)\n";
4023
4024 print_indent(indent);
4025 outs() << " instance_methods "
4026 << format("0x%08" PRIx32, protocol.instance_methods);
4027 if (print_method_description_list(protocol.instance_methods, indent, info))
4028 outs() << " (not in an __OBJC section)\n";
4029
4030 print_indent(indent);
4031 outs() << " class_methods "
4032 << format("0x%08" PRIx32, protocol.class_methods);
4033 if (print_method_description_list(protocol.class_methods, indent, info))
4034 outs() << " (not in an __OBJC section)\n";
4035
4036 return false;
4037}
4038
4039static bool print_protocol_list(uint32_t p, uint32_t indent,
4040 struct DisassembleInfo *info) {
4041 uint32_t offset, left, l;
4042 SectionRef S;
4043 struct objc_protocol_list_t protocol_list;
4044 const char *r, *list;
4045 int32_t i;
4046
4047 r = get_pointer_32(p, offset, left, S, info, true);
4048 if (r == nullptr)
4049 return true;
4050
4051 outs() << "\n";
4052 if (left > sizeof(struct objc_protocol_list_t)) {
4053 memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t));
4054 } else {
4055 outs() << "\t\t objc_protocol_list_t extends past end of the section\n";
4056 memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t));
4057 memcpy(&protocol_list, r, left);
4058 }
4059 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4060 swapStruct(protocol_list);
4061
4062 print_indent(indent);
4063 outs() << " next " << format("0x%08" PRIx32, protocol_list.next)
4064 << "\n";
4065 print_indent(indent);
4066 outs() << " count " << protocol_list.count << "\n";
4067
4068 list = r + sizeof(struct objc_protocol_list_t);
4069 for (i = 0; i < protocol_list.count; i++) {
4070 if ((i + 1) * sizeof(uint32_t) > left) {
4071 outs() << "\t\t remaining list entries extend past the of the section\n";
4072 break;
4073 }
4074 memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t));
4075 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4076 sys::swapByteOrder(l);
4077
4078 print_indent(indent);
4079 outs() << " list[" << i << "] " << format("0x%08" PRIx32, l);
4080 if (print_protocol(l, indent, info))
4081 outs() << "(not in an __OBJC section)\n";
4082 }
4083 return false;
4084}
4085
Kevin Enderby0fc11822015-04-01 20:57:01 +00004086static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) {
4087 struct ivar_list64_t il;
4088 struct ivar64_t i;
4089 const char *r;
4090 uint32_t offset, xoffset, left, j;
4091 SectionRef S, xS;
4092 const char *name, *sym_name, *ivar_offset_p;
4093 uint64_t ivar_offset, n_value;
4094
4095 r = get_pointer_64(p, offset, left, S, info);
4096 if (r == nullptr)
4097 return;
4098 memset(&il, '\0', sizeof(struct ivar_list64_t));
4099 if (left < sizeof(struct ivar_list64_t)) {
4100 memcpy(&il, r, left);
4101 outs() << " (ivar_list_t entends past the end of the section)\n";
4102 } else
4103 memcpy(&il, r, sizeof(struct ivar_list64_t));
4104 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4105 swapStruct(il);
4106 outs() << " entsize " << il.entsize << "\n";
4107 outs() << " count " << il.count << "\n";
4108
4109 p += sizeof(struct ivar_list64_t);
4110 offset += sizeof(struct ivar_list64_t);
4111 for (j = 0; j < il.count; j++) {
4112 r = get_pointer_64(p, offset, left, S, info);
4113 if (r == nullptr)
4114 return;
4115 memset(&i, '\0', sizeof(struct ivar64_t));
4116 if (left < sizeof(struct ivar64_t)) {
4117 memcpy(&i, r, left);
4118 outs() << " (ivar_t entends past the end of the section)\n";
4119 } else
4120 memcpy(&i, r, sizeof(struct ivar64_t));
4121 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4122 swapStruct(i);
4123
4124 outs() << "\t\t\t offset ";
4125 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset), S,
4126 info, n_value, i.offset);
4127 if (n_value != 0) {
4128 if (info->verbose && sym_name != nullptr)
4129 outs() << sym_name;
4130 else
4131 outs() << format("0x%" PRIx64, n_value);
4132 if (i.offset != 0)
4133 outs() << " + " << format("0x%" PRIx64, i.offset);
4134 } else
4135 outs() << format("0x%" PRIx64, i.offset);
4136 ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info);
4137 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4138 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4139 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4140 sys::swapByteOrder(ivar_offset);
4141 outs() << " " << ivar_offset << "\n";
4142 } else
4143 outs() << "\n";
4144
4145 outs() << "\t\t\t name ";
4146 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name), S, info,
4147 n_value, i.name);
4148 if (n_value != 0) {
4149 if (info->verbose && sym_name != nullptr)
4150 outs() << sym_name;
4151 else
4152 outs() << format("0x%" PRIx64, n_value);
4153 if (i.name != 0)
4154 outs() << " + " << format("0x%" PRIx64, i.name);
4155 } else
4156 outs() << format("0x%" PRIx64, i.name);
4157 name = get_pointer_64(i.name + n_value, xoffset, left, xS, info);
4158 if (name != nullptr)
4159 outs() << format(" %.*s", left, name);
4160 outs() << "\n";
4161
4162 outs() << "\t\t\t type ";
4163 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type), S, info,
4164 n_value, i.name);
4165 name = get_pointer_64(i.type + n_value, xoffset, left, xS, info);
4166 if (n_value != 0) {
4167 if (info->verbose && sym_name != nullptr)
4168 outs() << sym_name;
4169 else
4170 outs() << format("0x%" PRIx64, n_value);
4171 if (i.type != 0)
4172 outs() << " + " << format("0x%" PRIx64, i.type);
4173 } else
4174 outs() << format("0x%" PRIx64, i.type);
4175 if (name != nullptr)
4176 outs() << format(" %.*s", left, name);
4177 outs() << "\n";
4178
4179 outs() << "\t\t\talignment " << i.alignment << "\n";
4180 outs() << "\t\t\t size " << i.size << "\n";
4181
4182 p += sizeof(struct ivar64_t);
4183 offset += sizeof(struct ivar64_t);
4184 }
4185}
4186
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004187static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) {
4188 struct ivar_list32_t il;
4189 struct ivar32_t i;
4190 const char *r;
4191 uint32_t offset, xoffset, left, j;
4192 SectionRef S, xS;
4193 const char *name, *ivar_offset_p;
4194 uint32_t ivar_offset;
4195
4196 r = get_pointer_32(p, offset, left, S, info);
4197 if (r == nullptr)
4198 return;
4199 memset(&il, '\0', sizeof(struct ivar_list32_t));
4200 if (left < sizeof(struct ivar_list32_t)) {
4201 memcpy(&il, r, left);
4202 outs() << " (ivar_list_t entends past the end of the section)\n";
4203 } else
4204 memcpy(&il, r, sizeof(struct ivar_list32_t));
4205 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4206 swapStruct(il);
4207 outs() << " entsize " << il.entsize << "\n";
4208 outs() << " count " << il.count << "\n";
4209
4210 p += sizeof(struct ivar_list32_t);
4211 offset += sizeof(struct ivar_list32_t);
4212 for (j = 0; j < il.count; j++) {
4213 r = get_pointer_32(p, offset, left, S, info);
4214 if (r == nullptr)
4215 return;
4216 memset(&i, '\0', sizeof(struct ivar32_t));
4217 if (left < sizeof(struct ivar32_t)) {
4218 memcpy(&i, r, left);
4219 outs() << " (ivar_t entends past the end of the section)\n";
4220 } else
4221 memcpy(&i, r, sizeof(struct ivar32_t));
4222 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4223 swapStruct(i);
4224
4225 outs() << "\t\t\t offset " << format("0x%" PRIx32, i.offset);
4226 ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info);
4227 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4228 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4229 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4230 sys::swapByteOrder(ivar_offset);
4231 outs() << " " << ivar_offset << "\n";
4232 } else
4233 outs() << "\n";
4234
4235 outs() << "\t\t\t name " << format("0x%" PRIx32, i.name);
4236 name = get_pointer_32(i.name, xoffset, left, xS, info);
4237 if (name != nullptr)
4238 outs() << format(" %.*s", left, name);
4239 outs() << "\n";
4240
4241 outs() << "\t\t\t type " << format("0x%" PRIx32, i.type);
4242 name = get_pointer_32(i.type, xoffset, left, xS, info);
4243 if (name != nullptr)
4244 outs() << format(" %.*s", left, name);
4245 outs() << "\n";
4246
4247 outs() << "\t\t\talignment " << i.alignment << "\n";
4248 outs() << "\t\t\t size " << i.size << "\n";
4249
4250 p += sizeof(struct ivar32_t);
4251 offset += sizeof(struct ivar32_t);
4252 }
4253}
4254
Kevin Enderby0fc11822015-04-01 20:57:01 +00004255static void print_objc_property_list64(uint64_t p,
4256 struct DisassembleInfo *info) {
4257 struct objc_property_list64 opl;
4258 struct objc_property64 op;
4259 const char *r;
4260 uint32_t offset, xoffset, left, j;
4261 SectionRef S, xS;
4262 const char *name, *sym_name;
4263 uint64_t n_value;
4264
4265 r = get_pointer_64(p, offset, left, S, info);
4266 if (r == nullptr)
4267 return;
4268 memset(&opl, '\0', sizeof(struct objc_property_list64));
4269 if (left < sizeof(struct objc_property_list64)) {
4270 memcpy(&opl, r, left);
4271 outs() << " (objc_property_list entends past the end of the section)\n";
4272 } else
4273 memcpy(&opl, r, sizeof(struct objc_property_list64));
4274 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4275 swapStruct(opl);
4276 outs() << " entsize " << opl.entsize << "\n";
4277 outs() << " count " << opl.count << "\n";
4278
4279 p += sizeof(struct objc_property_list64);
4280 offset += sizeof(struct objc_property_list64);
4281 for (j = 0; j < opl.count; j++) {
4282 r = get_pointer_64(p, offset, left, S, info);
4283 if (r == nullptr)
4284 return;
4285 memset(&op, '\0', sizeof(struct objc_property64));
4286 if (left < sizeof(struct objc_property64)) {
4287 memcpy(&op, r, left);
4288 outs() << " (objc_property entends past the end of the section)\n";
4289 } else
4290 memcpy(&op, r, sizeof(struct objc_property64));
4291 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4292 swapStruct(op);
4293
4294 outs() << "\t\t\t name ";
4295 sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name), S,
4296 info, n_value, op.name);
4297 if (n_value != 0) {
4298 if (info->verbose && sym_name != nullptr)
4299 outs() << sym_name;
4300 else
4301 outs() << format("0x%" PRIx64, n_value);
4302 if (op.name != 0)
4303 outs() << " + " << format("0x%" PRIx64, op.name);
4304 } else
4305 outs() << format("0x%" PRIx64, op.name);
4306 name = get_pointer_64(op.name + n_value, xoffset, left, xS, info);
4307 if (name != nullptr)
4308 outs() << format(" %.*s", left, name);
4309 outs() << "\n";
4310
4311 outs() << "\t\t\tattributes ";
4312 sym_name =
4313 get_symbol_64(offset + offsetof(struct objc_property64, attributes), S,
4314 info, n_value, op.attributes);
4315 if (n_value != 0) {
4316 if (info->verbose && sym_name != nullptr)
4317 outs() << sym_name;
4318 else
4319 outs() << format("0x%" PRIx64, n_value);
4320 if (op.attributes != 0)
4321 outs() << " + " << format("0x%" PRIx64, op.attributes);
4322 } else
4323 outs() << format("0x%" PRIx64, op.attributes);
4324 name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info);
4325 if (name != nullptr)
4326 outs() << format(" %.*s", left, name);
4327 outs() << "\n";
4328
4329 p += sizeof(struct objc_property64);
4330 offset += sizeof(struct objc_property64);
4331 }
4332}
4333
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004334static void print_objc_property_list32(uint32_t p,
4335 struct DisassembleInfo *info) {
4336 struct objc_property_list32 opl;
4337 struct objc_property32 op;
4338 const char *r;
4339 uint32_t offset, xoffset, left, j;
4340 SectionRef S, xS;
4341 const char *name;
4342
4343 r = get_pointer_32(p, offset, left, S, info);
4344 if (r == nullptr)
4345 return;
4346 memset(&opl, '\0', sizeof(struct objc_property_list32));
4347 if (left < sizeof(struct objc_property_list32)) {
4348 memcpy(&opl, r, left);
4349 outs() << " (objc_property_list entends past the end of the section)\n";
4350 } else
4351 memcpy(&opl, r, sizeof(struct objc_property_list32));
4352 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4353 swapStruct(opl);
4354 outs() << " entsize " << opl.entsize << "\n";
4355 outs() << " count " << opl.count << "\n";
4356
4357 p += sizeof(struct objc_property_list32);
4358 offset += sizeof(struct objc_property_list32);
4359 for (j = 0; j < opl.count; j++) {
4360 r = get_pointer_32(p, offset, left, S, info);
4361 if (r == nullptr)
4362 return;
4363 memset(&op, '\0', sizeof(struct objc_property32));
4364 if (left < sizeof(struct objc_property32)) {
4365 memcpy(&op, r, left);
4366 outs() << " (objc_property entends past the end of the section)\n";
4367 } else
4368 memcpy(&op, r, sizeof(struct objc_property32));
4369 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4370 swapStruct(op);
4371
4372 outs() << "\t\t\t name " << format("0x%" PRIx32, op.name);
4373 name = get_pointer_32(op.name, xoffset, left, xS, info);
4374 if (name != nullptr)
4375 outs() << format(" %.*s", left, name);
4376 outs() << "\n";
4377
4378 outs() << "\t\t\tattributes " << format("0x%" PRIx32, op.attributes);
4379 name = get_pointer_32(op.attributes, xoffset, left, xS, info);
4380 if (name != nullptr)
4381 outs() << format(" %.*s", left, name);
4382 outs() << "\n";
4383
4384 p += sizeof(struct objc_property32);
4385 offset += sizeof(struct objc_property32);
4386 }
4387}
4388
Richard Smith81ff44d2015-10-09 22:09:56 +00004389static bool print_class_ro64_t(uint64_t p, struct DisassembleInfo *info,
Kevin Enderby0fc11822015-04-01 20:57:01 +00004390 bool &is_meta_class) {
4391 struct class_ro64_t cro;
4392 const char *r;
4393 uint32_t offset, xoffset, left;
4394 SectionRef S, xS;
4395 const char *name, *sym_name;
4396 uint64_t n_value;
4397
4398 r = get_pointer_64(p, offset, left, S, info);
4399 if (r == nullptr || left < sizeof(struct class_ro64_t))
Richard Smith81ff44d2015-10-09 22:09:56 +00004400 return false;
Chandler Carruth33e58902016-11-04 07:10:24 +00004401 memcpy(&cro, r, sizeof(struct class_ro64_t));
Kevin Enderby0fc11822015-04-01 20:57:01 +00004402 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4403 swapStruct(cro);
4404 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4405 if (cro.flags & RO_META)
4406 outs() << " RO_META";
4407 if (cro.flags & RO_ROOT)
4408 outs() << " RO_ROOT";
4409 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4410 outs() << " RO_HAS_CXX_STRUCTORS";
4411 outs() << "\n";
4412 outs() << " instanceStart " << cro.instanceStart << "\n";
4413 outs() << " instanceSize " << cro.instanceSize << "\n";
4414 outs() << " reserved " << format("0x%" PRIx32, cro.reserved)
4415 << "\n";
4416 outs() << " ivarLayout " << format("0x%" PRIx64, cro.ivarLayout)
4417 << "\n";
4418 print_layout_map64(cro.ivarLayout, info);
4419
4420 outs() << " name ";
4421 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name), S,
4422 info, n_value, cro.name);
4423 if (n_value != 0) {
4424 if (info->verbose && sym_name != nullptr)
4425 outs() << sym_name;
4426 else
4427 outs() << format("0x%" PRIx64, n_value);
4428 if (cro.name != 0)
4429 outs() << " + " << format("0x%" PRIx64, cro.name);
4430 } else
4431 outs() << format("0x%" PRIx64, cro.name);
4432 name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info);
4433 if (name != nullptr)
4434 outs() << format(" %.*s", left, name);
4435 outs() << "\n";
4436
4437 outs() << " baseMethods ";
4438 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods),
4439 S, info, n_value, cro.baseMethods);
4440 if (n_value != 0) {
4441 if (info->verbose && sym_name != nullptr)
4442 outs() << sym_name;
4443 else
4444 outs() << format("0x%" PRIx64, n_value);
4445 if (cro.baseMethods != 0)
4446 outs() << " + " << format("0x%" PRIx64, cro.baseMethods);
4447 } else
4448 outs() << format("0x%" PRIx64, cro.baseMethods);
4449 outs() << " (struct method_list_t *)\n";
4450 if (cro.baseMethods + n_value != 0)
4451 print_method_list64_t(cro.baseMethods + n_value, info, "");
4452
4453 outs() << " baseProtocols ";
4454 sym_name =
4455 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols), S,
4456 info, n_value, cro.baseProtocols);
4457 if (n_value != 0) {
4458 if (info->verbose && sym_name != nullptr)
4459 outs() << sym_name;
4460 else
4461 outs() << format("0x%" PRIx64, n_value);
4462 if (cro.baseProtocols != 0)
4463 outs() << " + " << format("0x%" PRIx64, cro.baseProtocols);
4464 } else
4465 outs() << format("0x%" PRIx64, cro.baseProtocols);
4466 outs() << "\n";
4467 if (cro.baseProtocols + n_value != 0)
4468 print_protocol_list64_t(cro.baseProtocols + n_value, info);
4469
4470 outs() << " ivars ";
4471 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars), S,
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004472 info, n_value, cro.ivars);
Kevin Enderby0fc11822015-04-01 20:57:01 +00004473 if (n_value != 0) {
4474 if (info->verbose && sym_name != nullptr)
4475 outs() << sym_name;
4476 else
4477 outs() << format("0x%" PRIx64, n_value);
4478 if (cro.ivars != 0)
4479 outs() << " + " << format("0x%" PRIx64, cro.ivars);
4480 } else
4481 outs() << format("0x%" PRIx64, cro.ivars);
4482 outs() << "\n";
4483 if (cro.ivars + n_value != 0)
4484 print_ivar_list64_t(cro.ivars + n_value, info);
4485
4486 outs() << " weakIvarLayout ";
4487 sym_name =
4488 get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout), S,
4489 info, n_value, cro.weakIvarLayout);
4490 if (n_value != 0) {
4491 if (info->verbose && sym_name != nullptr)
4492 outs() << sym_name;
4493 else
4494 outs() << format("0x%" PRIx64, n_value);
4495 if (cro.weakIvarLayout != 0)
4496 outs() << " + " << format("0x%" PRIx64, cro.weakIvarLayout);
4497 } else
4498 outs() << format("0x%" PRIx64, cro.weakIvarLayout);
4499 outs() << "\n";
4500 print_layout_map64(cro.weakIvarLayout + n_value, info);
4501
4502 outs() << " baseProperties ";
4503 sym_name =
4504 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties), S,
4505 info, n_value, cro.baseProperties);
4506 if (n_value != 0) {
4507 if (info->verbose && sym_name != nullptr)
4508 outs() << sym_name;
4509 else
4510 outs() << format("0x%" PRIx64, n_value);
4511 if (cro.baseProperties != 0)
4512 outs() << " + " << format("0x%" PRIx64, cro.baseProperties);
4513 } else
4514 outs() << format("0x%" PRIx64, cro.baseProperties);
4515 outs() << "\n";
4516 if (cro.baseProperties + n_value != 0)
4517 print_objc_property_list64(cro.baseProperties + n_value, info);
4518
Rafael Espindolab9091322015-10-24 23:19:10 +00004519 is_meta_class = (cro.flags & RO_META) != 0;
Richard Smith81ff44d2015-10-09 22:09:56 +00004520 return true;
Kevin Enderby0fc11822015-04-01 20:57:01 +00004521}
4522
Richard Smith81ff44d2015-10-09 22:09:56 +00004523static bool print_class_ro32_t(uint32_t p, struct DisassembleInfo *info,
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004524 bool &is_meta_class) {
4525 struct class_ro32_t cro;
4526 const char *r;
4527 uint32_t offset, xoffset, left;
4528 SectionRef S, xS;
4529 const char *name;
4530
4531 r = get_pointer_32(p, offset, left, S, info);
4532 if (r == nullptr)
Richard Smith81ff44d2015-10-09 22:09:56 +00004533 return false;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004534 memset(&cro, '\0', sizeof(struct class_ro32_t));
4535 if (left < sizeof(struct class_ro32_t)) {
4536 memcpy(&cro, r, left);
4537 outs() << " (class_ro_t entends past the end of the section)\n";
4538 } else
4539 memcpy(&cro, r, sizeof(struct class_ro32_t));
4540 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4541 swapStruct(cro);
4542 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4543 if (cro.flags & RO_META)
4544 outs() << " RO_META";
4545 if (cro.flags & RO_ROOT)
4546 outs() << " RO_ROOT";
4547 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4548 outs() << " RO_HAS_CXX_STRUCTORS";
4549 outs() << "\n";
4550 outs() << " instanceStart " << cro.instanceStart << "\n";
4551 outs() << " instanceSize " << cro.instanceSize << "\n";
4552 outs() << " ivarLayout " << format("0x%" PRIx32, cro.ivarLayout)
4553 << "\n";
4554 print_layout_map32(cro.ivarLayout, info);
4555
4556 outs() << " name " << format("0x%" PRIx32, cro.name);
4557 name = get_pointer_32(cro.name, xoffset, left, xS, info);
4558 if (name != nullptr)
4559 outs() << format(" %.*s", left, name);
4560 outs() << "\n";
4561
4562 outs() << " baseMethods "
4563 << format("0x%" PRIx32, cro.baseMethods)
4564 << " (struct method_list_t *)\n";
4565 if (cro.baseMethods != 0)
4566 print_method_list32_t(cro.baseMethods, info, "");
4567
4568 outs() << " baseProtocols "
4569 << format("0x%" PRIx32, cro.baseProtocols) << "\n";
4570 if (cro.baseProtocols != 0)
4571 print_protocol_list32_t(cro.baseProtocols, info);
4572 outs() << " ivars " << format("0x%" PRIx32, cro.ivars)
4573 << "\n";
4574 if (cro.ivars != 0)
4575 print_ivar_list32_t(cro.ivars, info);
4576 outs() << " weakIvarLayout "
4577 << format("0x%" PRIx32, cro.weakIvarLayout) << "\n";
4578 print_layout_map32(cro.weakIvarLayout, info);
4579 outs() << " baseProperties "
4580 << format("0x%" PRIx32, cro.baseProperties) << "\n";
4581 if (cro.baseProperties != 0)
4582 print_objc_property_list32(cro.baseProperties, info);
Rafael Espindolab9091322015-10-24 23:19:10 +00004583 is_meta_class = (cro.flags & RO_META) != 0;
Richard Smith81ff44d2015-10-09 22:09:56 +00004584 return true;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004585}
4586
Kevin Enderby0fc11822015-04-01 20:57:01 +00004587static void print_class64_t(uint64_t p, struct DisassembleInfo *info) {
4588 struct class64_t c;
4589 const char *r;
4590 uint32_t offset, left;
4591 SectionRef S;
4592 const char *name;
4593 uint64_t isa_n_value, n_value;
4594
4595 r = get_pointer_64(p, offset, left, S, info);
4596 if (r == nullptr || left < sizeof(struct class64_t))
4597 return;
Chandler Carruth33e58902016-11-04 07:10:24 +00004598 memcpy(&c, r, sizeof(struct class64_t));
Kevin Enderby0fc11822015-04-01 20:57:01 +00004599 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4600 swapStruct(c);
4601
4602 outs() << " isa " << format("0x%" PRIx64, c.isa);
4603 name = get_symbol_64(offset + offsetof(struct class64_t, isa), S, info,
4604 isa_n_value, c.isa);
4605 if (name != nullptr)
4606 outs() << " " << name;
4607 outs() << "\n";
4608
4609 outs() << " superclass " << format("0x%" PRIx64, c.superclass);
4610 name = get_symbol_64(offset + offsetof(struct class64_t, superclass), S, info,
4611 n_value, c.superclass);
4612 if (name != nullptr)
4613 outs() << " " << name;
Kevin Enderby1ce38582017-06-20 22:55:11 +00004614 else {
4615 name = get_dyld_bind_info_symbolname(S.getAddress() +
4616 offset + offsetof(struct class64_t, superclass), info);
4617 if (name != nullptr)
4618 outs() << " " << name;
4619 }
Kevin Enderby0fc11822015-04-01 20:57:01 +00004620 outs() << "\n";
4621
4622 outs() << " cache " << format("0x%" PRIx64, c.cache);
4623 name = get_symbol_64(offset + offsetof(struct class64_t, cache), S, info,
4624 n_value, c.cache);
4625 if (name != nullptr)
4626 outs() << " " << name;
4627 outs() << "\n";
4628
4629 outs() << " vtable " << format("0x%" PRIx64, c.vtable);
4630 name = get_symbol_64(offset + offsetof(struct class64_t, vtable), S, info,
4631 n_value, c.vtable);
4632 if (name != nullptr)
4633 outs() << " " << name;
4634 outs() << "\n";
4635
4636 name = get_symbol_64(offset + offsetof(struct class64_t, data), S, info,
4637 n_value, c.data);
4638 outs() << " data ";
4639 if (n_value != 0) {
4640 if (info->verbose && name != nullptr)
4641 outs() << name;
4642 else
4643 outs() << format("0x%" PRIx64, n_value);
4644 if (c.data != 0)
4645 outs() << " + " << format("0x%" PRIx64, c.data);
4646 } else
4647 outs() << format("0x%" PRIx64, c.data);
4648 outs() << " (struct class_ro_t *)";
4649
4650 // This is a Swift class if some of the low bits of the pointer are set.
4651 if ((c.data + n_value) & 0x7)
4652 outs() << " Swift class";
4653 outs() << "\n";
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004654 bool is_meta_class;
Richard Smith81ff44d2015-10-09 22:09:56 +00004655 if (!print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class))
4656 return;
Kevin Enderby0fc11822015-04-01 20:57:01 +00004657
Kevin Enderbyaac75382015-10-08 16:56:35 +00004658 if (!is_meta_class &&
4659 c.isa + isa_n_value != p &&
4660 c.isa + isa_n_value != 0 &&
4661 info->depth < 100) {
4662 info->depth++;
4663 outs() << "Meta Class\n";
4664 print_class64_t(c.isa + isa_n_value, info);
Kevin Enderby0fc11822015-04-01 20:57:01 +00004665 }
4666}
4667
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004668static void print_class32_t(uint32_t p, struct DisassembleInfo *info) {
4669 struct class32_t c;
4670 const char *r;
4671 uint32_t offset, left;
4672 SectionRef S;
4673 const char *name;
4674
4675 r = get_pointer_32(p, offset, left, S, info);
4676 if (r == nullptr)
4677 return;
4678 memset(&c, '\0', sizeof(struct class32_t));
4679 if (left < sizeof(struct class32_t)) {
4680 memcpy(&c, r, left);
4681 outs() << " (class_t entends past the end of the section)\n";
4682 } else
4683 memcpy(&c, r, sizeof(struct class32_t));
4684 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4685 swapStruct(c);
4686
4687 outs() << " isa " << format("0x%" PRIx32, c.isa);
4688 name =
4689 get_symbol_32(offset + offsetof(struct class32_t, isa), S, info, c.isa);
4690 if (name != nullptr)
4691 outs() << " " << name;
4692 outs() << "\n";
4693
4694 outs() << " superclass " << format("0x%" PRIx32, c.superclass);
4695 name = get_symbol_32(offset + offsetof(struct class32_t, superclass), S, info,
4696 c.superclass);
4697 if (name != nullptr)
4698 outs() << " " << name;
4699 outs() << "\n";
4700
4701 outs() << " cache " << format("0x%" PRIx32, c.cache);
4702 name = get_symbol_32(offset + offsetof(struct class32_t, cache), S, info,
4703 c.cache);
4704 if (name != nullptr)
4705 outs() << " " << name;
4706 outs() << "\n";
4707
4708 outs() << " vtable " << format("0x%" PRIx32, c.vtable);
4709 name = get_symbol_32(offset + offsetof(struct class32_t, vtable), S, info,
4710 c.vtable);
4711 if (name != nullptr)
4712 outs() << " " << name;
4713 outs() << "\n";
4714
4715 name =
4716 get_symbol_32(offset + offsetof(struct class32_t, data), S, info, c.data);
4717 outs() << " data " << format("0x%" PRIx32, c.data)
4718 << " (struct class_ro_t *)";
4719
4720 // This is a Swift class if some of the low bits of the pointer are set.
4721 if (c.data & 0x3)
4722 outs() << " Swift class";
4723 outs() << "\n";
4724 bool is_meta_class;
Richard Smith81ff44d2015-10-09 22:09:56 +00004725 if (!print_class_ro32_t(c.data & ~0x3, info, is_meta_class))
4726 return;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004727
Hans Wennborgcc9deb42015-09-29 18:02:48 +00004728 if (!is_meta_class) {
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004729 outs() << "Meta Class\n";
4730 print_class32_t(c.isa, info);
4731 }
4732}
4733
Kevin Enderby846c0002015-04-16 17:19:59 +00004734static void print_objc_class_t(struct objc_class_t *objc_class,
4735 struct DisassembleInfo *info) {
4736 uint32_t offset, left, xleft;
4737 const char *name, *p, *ivar_list;
4738 SectionRef S;
4739 int32_t i;
4740 struct objc_ivar_list_t objc_ivar_list;
4741 struct objc_ivar_t ivar;
4742
4743 outs() << "\t\t isa " << format("0x%08" PRIx32, objc_class->isa);
4744 if (info->verbose && CLS_GETINFO(objc_class, CLS_META)) {
4745 name = get_pointer_32(objc_class->isa, offset, left, S, info, true);
4746 if (name != nullptr)
4747 outs() << format(" %.*s", left, name);
4748 else
4749 outs() << " (not in an __OBJC section)";
4750 }
4751 outs() << "\n";
4752
4753 outs() << "\t super_class "
4754 << format("0x%08" PRIx32, objc_class->super_class);
4755 if (info->verbose) {
4756 name = get_pointer_32(objc_class->super_class, 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\t name " << format("0x%08" PRIx32, objc_class->name);
4765 if (info->verbose) {
4766 name = get_pointer_32(objc_class->name, offset, left, S, info, true);
4767 if (name != nullptr)
4768 outs() << format(" %.*s", left, name);
4769 else
4770 outs() << " (not in an __OBJC section)";
4771 }
4772 outs() << "\n";
4773
4774 outs() << "\t\t version " << format("0x%08" PRIx32, objc_class->version)
4775 << "\n";
4776
4777 outs() << "\t\t info " << format("0x%08" PRIx32, objc_class->info);
4778 if (info->verbose) {
4779 if (CLS_GETINFO(objc_class, CLS_CLASS))
4780 outs() << " CLS_CLASS";
4781 else if (CLS_GETINFO(objc_class, CLS_META))
4782 outs() << " CLS_META";
4783 }
4784 outs() << "\n";
4785
4786 outs() << "\t instance_size "
4787 << format("0x%08" PRIx32, objc_class->instance_size) << "\n";
4788
4789 p = get_pointer_32(objc_class->ivars, offset, left, S, info, true);
4790 outs() << "\t\t ivars " << format("0x%08" PRIx32, objc_class->ivars);
4791 if (p != nullptr) {
4792 if (left > sizeof(struct objc_ivar_list_t)) {
4793 outs() << "\n";
4794 memcpy(&objc_ivar_list, p, sizeof(struct objc_ivar_list_t));
4795 } else {
4796 outs() << " (entends past the end of the section)\n";
4797 memset(&objc_ivar_list, '\0', sizeof(struct objc_ivar_list_t));
4798 memcpy(&objc_ivar_list, p, left);
4799 }
4800 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4801 swapStruct(objc_ivar_list);
4802 outs() << "\t\t ivar_count " << objc_ivar_list.ivar_count << "\n";
4803 ivar_list = p + sizeof(struct objc_ivar_list_t);
4804 for (i = 0; i < objc_ivar_list.ivar_count; i++) {
4805 if ((i + 1) * sizeof(struct objc_ivar_t) > left) {
4806 outs() << "\t\t remaining ivar's extend past the of the section\n";
4807 break;
4808 }
4809 memcpy(&ivar, ivar_list + i * sizeof(struct objc_ivar_t),
4810 sizeof(struct objc_ivar_t));
4811 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4812 swapStruct(ivar);
4813
4814 outs() << "\t\t\tivar_name " << format("0x%08" PRIx32, ivar.ivar_name);
4815 if (info->verbose) {
4816 name = get_pointer_32(ivar.ivar_name, offset, xleft, S, info, true);
4817 if (name != nullptr)
4818 outs() << format(" %.*s", xleft, name);
4819 else
4820 outs() << " (not in an __OBJC section)";
4821 }
4822 outs() << "\n";
4823
4824 outs() << "\t\t\tivar_type " << format("0x%08" PRIx32, ivar.ivar_type);
4825 if (info->verbose) {
4826 name = get_pointer_32(ivar.ivar_type, offset, xleft, S, info, true);
4827 if (name != nullptr)
4828 outs() << format(" %.*s", xleft, name);
4829 else
4830 outs() << " (not in an __OBJC section)";
4831 }
4832 outs() << "\n";
4833
4834 outs() << "\t\t ivar_offset "
4835 << format("0x%08" PRIx32, ivar.ivar_offset) << "\n";
4836 }
4837 } else {
4838 outs() << " (not in an __OBJC section)\n";
4839 }
4840
4841 outs() << "\t\t methods " << format("0x%08" PRIx32, objc_class->methodLists);
4842 if (print_method_list(objc_class->methodLists, info))
4843 outs() << " (not in an __OBJC section)\n";
4844
4845 outs() << "\t\t cache " << format("0x%08" PRIx32, objc_class->cache)
4846 << "\n";
4847
4848 outs() << "\t\tprotocols " << format("0x%08" PRIx32, objc_class->protocols);
4849 if (print_protocol_list(objc_class->protocols, 16, info))
4850 outs() << " (not in an __OBJC section)\n";
4851}
4852
4853static void print_objc_objc_category_t(struct objc_category_t *objc_category,
4854 struct DisassembleInfo *info) {
4855 uint32_t offset, left;
4856 const char *name;
4857 SectionRef S;
4858
4859 outs() << "\t category name "
4860 << format("0x%08" PRIx32, objc_category->category_name);
4861 if (info->verbose) {
4862 name = get_pointer_32(objc_category->category_name, offset, left, S, info,
4863 true);
4864 if (name != nullptr)
4865 outs() << format(" %.*s", left, name);
4866 else
4867 outs() << " (not in an __OBJC section)";
4868 }
4869 outs() << "\n";
4870
4871 outs() << "\t\t class name "
4872 << format("0x%08" PRIx32, objc_category->class_name);
4873 if (info->verbose) {
4874 name =
4875 get_pointer_32(objc_category->class_name, offset, left, S, info, true);
4876 if (name != nullptr)
4877 outs() << format(" %.*s", left, name);
4878 else
4879 outs() << " (not in an __OBJC section)";
4880 }
4881 outs() << "\n";
4882
4883 outs() << "\t instance methods "
4884 << format("0x%08" PRIx32, objc_category->instance_methods);
4885 if (print_method_list(objc_category->instance_methods, info))
4886 outs() << " (not in an __OBJC section)\n";
4887
4888 outs() << "\t class methods "
4889 << format("0x%08" PRIx32, objc_category->class_methods);
4890 if (print_method_list(objc_category->class_methods, info))
4891 outs() << " (not in an __OBJC section)\n";
4892}
4893
Kevin Enderby0fc11822015-04-01 20:57:01 +00004894static void print_category64_t(uint64_t p, struct DisassembleInfo *info) {
4895 struct category64_t c;
4896 const char *r;
4897 uint32_t offset, xoffset, left;
4898 SectionRef S, xS;
4899 const char *name, *sym_name;
4900 uint64_t n_value;
4901
4902 r = get_pointer_64(p, offset, left, S, info);
4903 if (r == nullptr)
4904 return;
4905 memset(&c, '\0', sizeof(struct category64_t));
4906 if (left < sizeof(struct category64_t)) {
4907 memcpy(&c, r, left);
4908 outs() << " (category_t entends past the end of the section)\n";
4909 } else
4910 memcpy(&c, r, sizeof(struct category64_t));
4911 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4912 swapStruct(c);
4913
4914 outs() << " name ";
4915 sym_name = get_symbol_64(offset + offsetof(struct category64_t, name), S,
4916 info, n_value, c.name);
4917 if (n_value != 0) {
4918 if (info->verbose && sym_name != nullptr)
4919 outs() << sym_name;
4920 else
4921 outs() << format("0x%" PRIx64, n_value);
4922 if (c.name != 0)
4923 outs() << " + " << format("0x%" PRIx64, c.name);
4924 } else
4925 outs() << format("0x%" PRIx64, c.name);
4926 name = get_pointer_64(c.name + n_value, xoffset, left, xS, info);
4927 if (name != nullptr)
4928 outs() << format(" %.*s", left, name);
4929 outs() << "\n";
4930
4931 outs() << " cls ";
4932 sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls), S, info,
4933 n_value, c.cls);
4934 if (n_value != 0) {
4935 if (info->verbose && sym_name != nullptr)
4936 outs() << sym_name;
4937 else
4938 outs() << format("0x%" PRIx64, n_value);
4939 if (c.cls != 0)
4940 outs() << " + " << format("0x%" PRIx64, c.cls);
4941 } else
4942 outs() << format("0x%" PRIx64, c.cls);
4943 outs() << "\n";
4944 if (c.cls + n_value != 0)
4945 print_class64_t(c.cls + n_value, info);
4946
4947 outs() << " instanceMethods ";
4948 sym_name =
4949 get_symbol_64(offset + offsetof(struct category64_t, instanceMethods), S,
4950 info, n_value, c.instanceMethods);
4951 if (n_value != 0) {
4952 if (info->verbose && sym_name != nullptr)
4953 outs() << sym_name;
4954 else
4955 outs() << format("0x%" PRIx64, n_value);
4956 if (c.instanceMethods != 0)
4957 outs() << " + " << format("0x%" PRIx64, c.instanceMethods);
4958 } else
4959 outs() << format("0x%" PRIx64, c.instanceMethods);
4960 outs() << "\n";
4961 if (c.instanceMethods + n_value != 0)
4962 print_method_list64_t(c.instanceMethods + n_value, info, "");
4963
4964 outs() << " classMethods ";
4965 sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods),
4966 S, info, n_value, c.classMethods);
4967 if (n_value != 0) {
4968 if (info->verbose && sym_name != nullptr)
4969 outs() << sym_name;
4970 else
4971 outs() << format("0x%" PRIx64, n_value);
4972 if (c.classMethods != 0)
4973 outs() << " + " << format("0x%" PRIx64, c.classMethods);
4974 } else
4975 outs() << format("0x%" PRIx64, c.classMethods);
4976 outs() << "\n";
4977 if (c.classMethods + n_value != 0)
4978 print_method_list64_t(c.classMethods + n_value, info, "");
4979
4980 outs() << " protocols ";
4981 sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols), S,
4982 info, n_value, c.protocols);
4983 if (n_value != 0) {
4984 if (info->verbose && sym_name != nullptr)
4985 outs() << sym_name;
4986 else
4987 outs() << format("0x%" PRIx64, n_value);
4988 if (c.protocols != 0)
4989 outs() << " + " << format("0x%" PRIx64, c.protocols);
4990 } else
4991 outs() << format("0x%" PRIx64, c.protocols);
4992 outs() << "\n";
4993 if (c.protocols + n_value != 0)
4994 print_protocol_list64_t(c.protocols + n_value, info);
4995
4996 outs() << "instanceProperties ";
4997 sym_name =
4998 get_symbol_64(offset + offsetof(struct category64_t, instanceProperties),
4999 S, info, n_value, c.instanceProperties);
5000 if (n_value != 0) {
5001 if (info->verbose && sym_name != nullptr)
5002 outs() << sym_name;
5003 else
5004 outs() << format("0x%" PRIx64, n_value);
5005 if (c.instanceProperties != 0)
5006 outs() << " + " << format("0x%" PRIx64, c.instanceProperties);
5007 } else
5008 outs() << format("0x%" PRIx64, c.instanceProperties);
5009 outs() << "\n";
5010 if (c.instanceProperties + n_value != 0)
5011 print_objc_property_list64(c.instanceProperties + n_value, info);
5012}
5013
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005014static void print_category32_t(uint32_t p, struct DisassembleInfo *info) {
5015 struct category32_t c;
5016 const char *r;
5017 uint32_t offset, left;
5018 SectionRef S, xS;
5019 const char *name;
5020
5021 r = get_pointer_32(p, offset, left, S, info);
5022 if (r == nullptr)
5023 return;
5024 memset(&c, '\0', sizeof(struct category32_t));
5025 if (left < sizeof(struct category32_t)) {
5026 memcpy(&c, r, left);
5027 outs() << " (category_t entends past the end of the section)\n";
5028 } else
5029 memcpy(&c, r, sizeof(struct category32_t));
5030 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5031 swapStruct(c);
5032
5033 outs() << " name " << format("0x%" PRIx32, c.name);
5034 name = get_symbol_32(offset + offsetof(struct category32_t, name), S, info,
5035 c.name);
Hans Wennborgcc9deb42015-09-29 18:02:48 +00005036 if (name)
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005037 outs() << " " << name;
5038 outs() << "\n";
5039
5040 outs() << " cls " << format("0x%" PRIx32, c.cls) << "\n";
5041 if (c.cls != 0)
5042 print_class32_t(c.cls, info);
5043 outs() << " instanceMethods " << format("0x%" PRIx32, c.instanceMethods)
5044 << "\n";
5045 if (c.instanceMethods != 0)
5046 print_method_list32_t(c.instanceMethods, info, "");
5047 outs() << " classMethods " << format("0x%" PRIx32, c.classMethods)
5048 << "\n";
5049 if (c.classMethods != 0)
5050 print_method_list32_t(c.classMethods, info, "");
5051 outs() << " protocols " << format("0x%" PRIx32, c.protocols) << "\n";
5052 if (c.protocols != 0)
5053 print_protocol_list32_t(c.protocols, info);
5054 outs() << "instanceProperties " << format("0x%" PRIx32, c.instanceProperties)
5055 << "\n";
5056 if (c.instanceProperties != 0)
5057 print_objc_property_list32(c.instanceProperties, info);
5058}
5059
Kevin Enderby0fc11822015-04-01 20:57:01 +00005060static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) {
5061 uint32_t i, left, offset, xoffset;
5062 uint64_t p, n_value;
5063 struct message_ref64 mr;
5064 const char *name, *sym_name;
5065 const char *r;
5066 SectionRef xS;
5067
5068 if (S == SectionRef())
5069 return;
5070
5071 StringRef SectName;
5072 S.getName(SectName);
5073 DataRefImpl Ref = S.getRawDataRefImpl();
5074 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5075 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5076 offset = 0;
5077 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
5078 p = S.getAddress() + i;
5079 r = get_pointer_64(p, offset, left, S, info);
5080 if (r == nullptr)
5081 return;
5082 memset(&mr, '\0', sizeof(struct message_ref64));
5083 if (left < sizeof(struct message_ref64)) {
5084 memcpy(&mr, r, left);
5085 outs() << " (message_ref entends past the end of the section)\n";
5086 } else
5087 memcpy(&mr, r, sizeof(struct message_ref64));
5088 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5089 swapStruct(mr);
5090
5091 outs() << " imp ";
5092 name = get_symbol_64(offset + offsetof(struct message_ref64, imp), S, info,
5093 n_value, mr.imp);
5094 if (n_value != 0) {
5095 outs() << format("0x%" PRIx64, n_value) << " ";
5096 if (mr.imp != 0)
5097 outs() << "+ " << format("0x%" PRIx64, mr.imp) << " ";
5098 } else
5099 outs() << format("0x%" PRIx64, mr.imp) << " ";
5100 if (name != nullptr)
5101 outs() << " " << name;
5102 outs() << "\n";
5103
5104 outs() << " sel ";
5105 sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel), S,
5106 info, n_value, mr.sel);
5107 if (n_value != 0) {
5108 if (info->verbose && sym_name != nullptr)
5109 outs() << sym_name;
5110 else
5111 outs() << format("0x%" PRIx64, n_value);
5112 if (mr.sel != 0)
5113 outs() << " + " << format("0x%" PRIx64, mr.sel);
5114 } else
5115 outs() << format("0x%" PRIx64, mr.sel);
5116 name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info);
5117 if (name != nullptr)
5118 outs() << format(" %.*s", left, name);
5119 outs() << "\n";
5120
5121 offset += sizeof(struct message_ref64);
5122 }
5123}
5124
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005125static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) {
5126 uint32_t i, left, offset, xoffset, p;
5127 struct message_ref32 mr;
5128 const char *name, *r;
5129 SectionRef xS;
5130
5131 if (S == SectionRef())
5132 return;
5133
5134 StringRef SectName;
5135 S.getName(SectName);
5136 DataRefImpl Ref = S.getRawDataRefImpl();
5137 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5138 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5139 offset = 0;
5140 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
5141 p = S.getAddress() + i;
5142 r = get_pointer_32(p, offset, left, S, info);
5143 if (r == nullptr)
5144 return;
5145 memset(&mr, '\0', sizeof(struct message_ref32));
5146 if (left < sizeof(struct message_ref32)) {
5147 memcpy(&mr, r, left);
5148 outs() << " (message_ref entends past the end of the section)\n";
5149 } else
5150 memcpy(&mr, r, sizeof(struct message_ref32));
5151 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5152 swapStruct(mr);
5153
5154 outs() << " imp " << format("0x%" PRIx32, mr.imp);
5155 name = get_symbol_32(offset + offsetof(struct message_ref32, imp), S, info,
5156 mr.imp);
5157 if (name != nullptr)
5158 outs() << " " << name;
5159 outs() << "\n";
5160
5161 outs() << " sel " << format("0x%" PRIx32, mr.sel);
5162 name = get_pointer_32(mr.sel, xoffset, left, xS, info);
5163 if (name != nullptr)
5164 outs() << " " << name;
5165 outs() << "\n";
5166
5167 offset += sizeof(struct message_ref32);
5168 }
5169}
5170
Kevin Enderby0fc11822015-04-01 20:57:01 +00005171static void print_image_info64(SectionRef S, struct DisassembleInfo *info) {
5172 uint32_t left, offset, swift_version;
5173 uint64_t p;
5174 struct objc_image_info64 o;
5175 const char *r;
5176
Kevin Enderbyaf7c9d02015-10-09 16:48:44 +00005177 if (S == SectionRef())
5178 return;
5179
Kevin Enderby0fc11822015-04-01 20:57:01 +00005180 StringRef SectName;
5181 S.getName(SectName);
5182 DataRefImpl Ref = S.getRawDataRefImpl();
5183 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5184 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5185 p = S.getAddress();
5186 r = get_pointer_64(p, offset, left, S, info);
5187 if (r == nullptr)
5188 return;
5189 memset(&o, '\0', sizeof(struct objc_image_info64));
5190 if (left < sizeof(struct objc_image_info64)) {
5191 memcpy(&o, r, left);
5192 outs() << " (objc_image_info entends past the end of the section)\n";
5193 } else
5194 memcpy(&o, r, sizeof(struct objc_image_info64));
5195 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5196 swapStruct(o);
5197 outs() << " version " << o.version << "\n";
5198 outs() << " flags " << format("0x%" PRIx32, o.flags);
5199 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5200 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5201 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5202 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5203 swift_version = (o.flags >> 8) & 0xff;
5204 if (swift_version != 0) {
5205 if (swift_version == 1)
5206 outs() << " Swift 1.0";
5207 else if (swift_version == 2)
5208 outs() << " Swift 1.1";
5209 else
5210 outs() << " unknown future Swift version (" << swift_version << ")";
5211 }
5212 outs() << "\n";
5213}
5214
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005215static void print_image_info32(SectionRef S, struct DisassembleInfo *info) {
5216 uint32_t left, offset, swift_version, p;
5217 struct objc_image_info32 o;
5218 const char *r;
5219
Kevin Enderby19be2512016-04-21 19:49:29 +00005220 if (S == SectionRef())
5221 return;
5222
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005223 StringRef SectName;
5224 S.getName(SectName);
5225 DataRefImpl Ref = S.getRawDataRefImpl();
5226 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5227 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5228 p = S.getAddress();
5229 r = get_pointer_32(p, offset, left, S, info);
5230 if (r == nullptr)
5231 return;
5232 memset(&o, '\0', sizeof(struct objc_image_info32));
5233 if (left < sizeof(struct objc_image_info32)) {
5234 memcpy(&o, r, left);
5235 outs() << " (objc_image_info entends past the end of the section)\n";
5236 } else
5237 memcpy(&o, r, sizeof(struct objc_image_info32));
5238 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5239 swapStruct(o);
5240 outs() << " version " << o.version << "\n";
5241 outs() << " flags " << format("0x%" PRIx32, o.flags);
5242 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5243 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5244 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5245 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5246 swift_version = (o.flags >> 8) & 0xff;
5247 if (swift_version != 0) {
5248 if (swift_version == 1)
5249 outs() << " Swift 1.0";
5250 else if (swift_version == 2)
5251 outs() << " Swift 1.1";
5252 else
5253 outs() << " unknown future Swift version (" << swift_version << ")";
5254 }
5255 outs() << "\n";
5256}
5257
Kevin Enderby846c0002015-04-16 17:19:59 +00005258static void print_image_info(SectionRef S, struct DisassembleInfo *info) {
5259 uint32_t left, offset, p;
5260 struct imageInfo_t o;
5261 const char *r;
5262
5263 StringRef SectName;
5264 S.getName(SectName);
5265 DataRefImpl Ref = S.getRawDataRefImpl();
5266 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5267 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5268 p = S.getAddress();
5269 r = get_pointer_32(p, offset, left, S, info);
5270 if (r == nullptr)
5271 return;
5272 memset(&o, '\0', sizeof(struct imageInfo_t));
5273 if (left < sizeof(struct imageInfo_t)) {
5274 memcpy(&o, r, left);
5275 outs() << " (imageInfo entends past the end of the section)\n";
5276 } else
5277 memcpy(&o, r, sizeof(struct imageInfo_t));
5278 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5279 swapStruct(o);
5280 outs() << " version " << o.version << "\n";
5281 outs() << " flags " << format("0x%" PRIx32, o.flags);
5282 if (o.flags & 0x1)
5283 outs() << " F&C";
5284 if (o.flags & 0x2)
5285 outs() << " GC";
5286 if (o.flags & 0x4)
5287 outs() << " GC-only";
5288 else
5289 outs() << " RR";
5290 outs() << "\n";
5291}
5292
Kevin Enderby0fc11822015-04-01 20:57:01 +00005293static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) {
5294 SymbolAddressMap AddrMap;
5295 if (verbose)
5296 CreateSymbolAddressMap(O, &AddrMap);
5297
5298 std::vector<SectionRef> Sections;
5299 for (const SectionRef &Section : O->sections()) {
5300 StringRef SectName;
5301 Section.getName(SectName);
5302 Sections.push_back(Section);
5303 }
5304
5305 struct DisassembleInfo info;
5306 // Set up the block of info used by the Symbolizer call backs.
5307 info.verbose = verbose;
5308 info.O = O;
5309 info.AddrMap = &AddrMap;
5310 info.Sections = &Sections;
5311 info.class_name = nullptr;
5312 info.selector_name = nullptr;
5313 info.method = nullptr;
5314 info.demangled_name = nullptr;
5315 info.bindtable = nullptr;
5316 info.adrp_addr = 0;
5317 info.adrp_inst = 0;
5318
Kevin Enderbyaac75382015-10-08 16:56:35 +00005319 info.depth = 0;
Davide Italiano62507042015-12-11 22:27:59 +00005320 SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5321 if (CL == SectionRef())
5322 CL = get_section(O, "__DATA", "__objc_classlist");
Kevin Enderby5879a482017-02-09 17:56:26 +00005323 if (CL == SectionRef())
5324 CL = get_section(O, "__DATA_CONST", "__objc_classlist");
5325 if (CL == SectionRef())
5326 CL = get_section(O, "__DATA_DIRTY", "__objc_classlist");
Davide Italiano62507042015-12-11 22:27:59 +00005327 info.S = CL;
5328 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005329
Davide Italiano62507042015-12-11 22:27:59 +00005330 SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5331 if (CR == SectionRef())
5332 CR = get_section(O, "__DATA", "__objc_classrefs");
Kevin Enderby5879a482017-02-09 17:56:26 +00005333 if (CR == SectionRef())
5334 CR = get_section(O, "__DATA_CONST", "__objc_classrefs");
5335 if (CR == SectionRef())
5336 CR = get_section(O, "__DATA_DIRTY", "__objc_classrefs");
Davide Italiano62507042015-12-11 22:27:59 +00005337 info.S = CR;
5338 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005339
Davide Italiano62507042015-12-11 22:27:59 +00005340 SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5341 if (SR == SectionRef())
5342 SR = get_section(O, "__DATA", "__objc_superrefs");
Kevin Enderby5879a482017-02-09 17:56:26 +00005343 if (SR == SectionRef())
5344 SR = get_section(O, "__DATA_CONST", "__objc_superrefs");
5345 if (SR == SectionRef())
5346 SR = get_section(O, "__DATA_DIRTY", "__objc_superrefs");
Davide Italiano62507042015-12-11 22:27:59 +00005347 info.S = SR;
5348 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005349
Davide Italiano62507042015-12-11 22:27:59 +00005350 SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5351 if (CA == SectionRef())
5352 CA = get_section(O, "__DATA", "__objc_catlist");
Kevin Enderby5879a482017-02-09 17:56:26 +00005353 if (CA == SectionRef())
5354 CA = get_section(O, "__DATA_CONST", "__objc_catlist");
5355 if (CA == SectionRef())
5356 CA = get_section(O, "__DATA_DIRTY", "__objc_catlist");
Davide Italiano62507042015-12-11 22:27:59 +00005357 info.S = CA;
5358 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005359
Davide Italiano62507042015-12-11 22:27:59 +00005360 SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5361 if (PL == SectionRef())
5362 PL = get_section(O, "__DATA", "__objc_protolist");
Kevin Enderby5879a482017-02-09 17:56:26 +00005363 if (PL == SectionRef())
5364 PL = get_section(O, "__DATA_CONST", "__objc_protolist");
5365 if (PL == SectionRef())
5366 PL = get_section(O, "__DATA_DIRTY", "__objc_protolist");
Davide Italiano62507042015-12-11 22:27:59 +00005367 info.S = PL;
5368 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005369
Davide Italiano62507042015-12-11 22:27:59 +00005370 SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5371 if (MR == SectionRef())
5372 MR = get_section(O, "__DATA", "__objc_msgrefs");
Kevin Enderby5879a482017-02-09 17:56:26 +00005373 if (MR == SectionRef())
5374 MR = get_section(O, "__DATA_CONST", "__objc_msgrefs");
5375 if (MR == SectionRef())
5376 MR = get_section(O, "__DATA_DIRTY", "__objc_msgrefs");
Davide Italiano62507042015-12-11 22:27:59 +00005377 info.S = MR;
5378 print_message_refs64(MR, &info);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005379
Davide Italiano62507042015-12-11 22:27:59 +00005380 SectionRef II = get_section(O, "__OBJC2", "__image_info");
5381 if (II == SectionRef())
5382 II = get_section(O, "__DATA", "__objc_imageinfo");
Kevin Enderby5879a482017-02-09 17:56:26 +00005383 if (II == SectionRef())
5384 II = get_section(O, "__DATA_CONST", "__objc_imageinfo");
5385 if (II == SectionRef())
5386 II = get_section(O, "__DATA_DIRTY", "__objc_imageinfo");
Davide Italiano62507042015-12-11 22:27:59 +00005387 info.S = II;
5388 print_image_info64(II, &info);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005389}
5390
5391static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) {
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005392 SymbolAddressMap AddrMap;
5393 if (verbose)
5394 CreateSymbolAddressMap(O, &AddrMap);
5395
5396 std::vector<SectionRef> Sections;
5397 for (const SectionRef &Section : O->sections()) {
5398 StringRef SectName;
5399 Section.getName(SectName);
5400 Sections.push_back(Section);
5401 }
5402
5403 struct DisassembleInfo info;
5404 // Set up the block of info used by the Symbolizer call backs.
5405 info.verbose = verbose;
5406 info.O = O;
5407 info.AddrMap = &AddrMap;
5408 info.Sections = &Sections;
5409 info.class_name = nullptr;
5410 info.selector_name = nullptr;
5411 info.method = nullptr;
5412 info.demangled_name = nullptr;
5413 info.bindtable = nullptr;
5414 info.adrp_addr = 0;
5415 info.adrp_inst = 0;
5416
Kevin Enderby5879a482017-02-09 17:56:26 +00005417 SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5418 if (CL == SectionRef())
5419 CL = get_section(O, "__DATA", "__objc_classlist");
5420 if (CL == SectionRef())
5421 CL = get_section(O, "__DATA_CONST", "__objc_classlist");
5422 if (CL == SectionRef())
5423 CL = get_section(O, "__DATA_DIRTY", "__objc_classlist");
5424 info.S = CL;
5425 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005426
Kevin Enderby5879a482017-02-09 17:56:26 +00005427 SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5428 if (CR == SectionRef())
5429 CR = get_section(O, "__DATA", "__objc_classrefs");
5430 if (CR == SectionRef())
5431 CR = get_section(O, "__DATA_CONST", "__objc_classrefs");
5432 if (CR == SectionRef())
5433 CR = get_section(O, "__DATA_DIRTY", "__objc_classrefs");
5434 info.S = CR;
5435 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005436
Kevin Enderby5879a482017-02-09 17:56:26 +00005437 SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5438 if (SR == SectionRef())
5439 SR = get_section(O, "__DATA", "__objc_superrefs");
5440 if (SR == SectionRef())
5441 SR = get_section(O, "__DATA_CONST", "__objc_superrefs");
5442 if (SR == SectionRef())
5443 SR = get_section(O, "__DATA_DIRTY", "__objc_superrefs");
5444 info.S = SR;
5445 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005446
Kevin Enderby5879a482017-02-09 17:56:26 +00005447 SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5448 if (CA == SectionRef())
5449 CA = get_section(O, "__DATA", "__objc_catlist");
5450 if (CA == SectionRef())
5451 CA = get_section(O, "__DATA_CONST", "__objc_catlist");
5452 if (CA == SectionRef())
5453 CA = get_section(O, "__DATA_DIRTY", "__objc_catlist");
5454 info.S = CA;
5455 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005456
Kevin Enderby5879a482017-02-09 17:56:26 +00005457 SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5458 if (PL == SectionRef())
5459 PL = get_section(O, "__DATA", "__objc_protolist");
5460 if (PL == SectionRef())
5461 PL = get_section(O, "__DATA_CONST", "__objc_protolist");
5462 if (PL == SectionRef())
5463 PL = get_section(O, "__DATA_DIRTY", "__objc_protolist");
5464 info.S = PL;
5465 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005466
Kevin Enderby5879a482017-02-09 17:56:26 +00005467 SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5468 if (MR == SectionRef())
5469 MR = get_section(O, "__DATA", "__objc_msgrefs");
5470 if (MR == SectionRef())
5471 MR = get_section(O, "__DATA_CONST", "__objc_msgrefs");
5472 if (MR == SectionRef())
5473 MR = get_section(O, "__DATA_DIRTY", "__objc_msgrefs");
5474 info.S = MR;
5475 print_message_refs32(MR, &info);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005476
Kevin Enderby5879a482017-02-09 17:56:26 +00005477 SectionRef II = get_section(O, "__OBJC2", "__image_info");
5478 if (II == SectionRef())
5479 II = get_section(O, "__DATA", "__objc_imageinfo");
5480 if (II == SectionRef())
5481 II = get_section(O, "__DATA_CONST", "__objc_imageinfo");
5482 if (II == SectionRef())
5483 II = get_section(O, "__DATA_DIRTY", "__objc_imageinfo");
5484 info.S = II;
5485 print_image_info32(II, &info);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005486}
5487
5488static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) {
Kevin Enderby846c0002015-04-16 17:19:59 +00005489 uint32_t i, j, p, offset, xoffset, left, defs_left, def;
5490 const char *r, *name, *defs;
5491 struct objc_module_t module;
5492 SectionRef S, xS;
5493 struct objc_symtab_t symtab;
5494 struct objc_class_t objc_class;
5495 struct objc_category_t objc_category;
5496
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005497 outs() << "Objective-C segment\n";
Kevin Enderby846c0002015-04-16 17:19:59 +00005498 S = get_section(O, "__OBJC", "__module_info");
5499 if (S == SectionRef())
5500 return false;
5501
5502 SymbolAddressMap AddrMap;
5503 if (verbose)
5504 CreateSymbolAddressMap(O, &AddrMap);
5505
5506 std::vector<SectionRef> Sections;
5507 for (const SectionRef &Section : O->sections()) {
5508 StringRef SectName;
5509 Section.getName(SectName);
5510 Sections.push_back(Section);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005511 }
Kevin Enderby846c0002015-04-16 17:19:59 +00005512
5513 struct DisassembleInfo info;
5514 // Set up the block of info used by the Symbolizer call backs.
5515 info.verbose = verbose;
5516 info.O = O;
5517 info.AddrMap = &AddrMap;
5518 info.Sections = &Sections;
5519 info.class_name = nullptr;
5520 info.selector_name = nullptr;
5521 info.method = nullptr;
5522 info.demangled_name = nullptr;
5523 info.bindtable = nullptr;
5524 info.adrp_addr = 0;
5525 info.adrp_inst = 0;
5526
5527 for (i = 0; i < S.getSize(); i += sizeof(struct objc_module_t)) {
5528 p = S.getAddress() + i;
5529 r = get_pointer_32(p, offset, left, S, &info, true);
5530 if (r == nullptr)
5531 return true;
5532 memset(&module, '\0', sizeof(struct objc_module_t));
5533 if (left < sizeof(struct objc_module_t)) {
5534 memcpy(&module, r, left);
5535 outs() << " (module extends past end of __module_info section)\n";
5536 } else
5537 memcpy(&module, r, sizeof(struct objc_module_t));
5538 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5539 swapStruct(module);
5540
5541 outs() << "Module " << format("0x%" PRIx32, p) << "\n";
5542 outs() << " version " << module.version << "\n";
5543 outs() << " size " << module.size << "\n";
5544 outs() << " name ";
5545 name = get_pointer_32(module.name, xoffset, left, xS, &info, true);
5546 if (name != nullptr)
5547 outs() << format("%.*s", left, name);
5548 else
5549 outs() << format("0x%08" PRIx32, module.name)
5550 << "(not in an __OBJC section)";
5551 outs() << "\n";
5552
5553 r = get_pointer_32(module.symtab, xoffset, left, xS, &info, true);
5554 if (module.symtab == 0 || r == nullptr) {
5555 outs() << " symtab " << format("0x%08" PRIx32, module.symtab)
5556 << " (not in an __OBJC section)\n";
5557 continue;
5558 }
5559 outs() << " symtab " << format("0x%08" PRIx32, module.symtab) << "\n";
5560 memset(&symtab, '\0', sizeof(struct objc_symtab_t));
5561 defs_left = 0;
5562 defs = nullptr;
5563 if (left < sizeof(struct objc_symtab_t)) {
5564 memcpy(&symtab, r, left);
5565 outs() << "\tsymtab extends past end of an __OBJC section)\n";
5566 } else {
5567 memcpy(&symtab, r, sizeof(struct objc_symtab_t));
5568 if (left > sizeof(struct objc_symtab_t)) {
5569 defs_left = left - sizeof(struct objc_symtab_t);
5570 defs = r + sizeof(struct objc_symtab_t);
5571 }
5572 }
5573 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5574 swapStruct(symtab);
5575
5576 outs() << "\tsel_ref_cnt " << symtab.sel_ref_cnt << "\n";
5577 r = get_pointer_32(symtab.refs, xoffset, left, xS, &info, true);
5578 outs() << "\trefs " << format("0x%08" PRIx32, symtab.refs);
5579 if (r == nullptr)
5580 outs() << " (not in an __OBJC section)";
5581 outs() << "\n";
5582 outs() << "\tcls_def_cnt " << symtab.cls_def_cnt << "\n";
5583 outs() << "\tcat_def_cnt " << symtab.cat_def_cnt << "\n";
5584 if (symtab.cls_def_cnt > 0)
5585 outs() << "\tClass Definitions\n";
5586 for (j = 0; j < symtab.cls_def_cnt; j++) {
5587 if ((j + 1) * sizeof(uint32_t) > defs_left) {
5588 outs() << "\t(remaining class defs entries entends past the end of the "
5589 << "section)\n";
5590 break;
5591 }
5592 memcpy(&def, defs + j * sizeof(uint32_t), sizeof(uint32_t));
5593 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5594 sys::swapByteOrder(def);
5595
5596 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5597 outs() << "\tdefs[" << j << "] " << format("0x%08" PRIx32, def);
5598 if (r != nullptr) {
5599 if (left > sizeof(struct objc_class_t)) {
5600 outs() << "\n";
5601 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5602 } else {
5603 outs() << " (entends past the end of the section)\n";
5604 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5605 memcpy(&objc_class, r, left);
5606 }
5607 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5608 swapStruct(objc_class);
5609 print_objc_class_t(&objc_class, &info);
5610 } else {
5611 outs() << "(not in an __OBJC section)\n";
5612 }
5613
5614 if (CLS_GETINFO(&objc_class, CLS_CLASS)) {
5615 outs() << "\tMeta Class";
5616 r = get_pointer_32(objc_class.isa, xoffset, left, xS, &info, true);
5617 if (r != nullptr) {
5618 if (left > sizeof(struct objc_class_t)) {
5619 outs() << "\n";
5620 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5621 } else {
5622 outs() << " (entends past the end of the section)\n";
5623 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5624 memcpy(&objc_class, r, left);
5625 }
5626 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5627 swapStruct(objc_class);
5628 print_objc_class_t(&objc_class, &info);
5629 } else {
5630 outs() << "(not in an __OBJC section)\n";
5631 }
5632 }
5633 }
5634 if (symtab.cat_def_cnt > 0)
5635 outs() << "\tCategory Definitions\n";
5636 for (j = 0; j < symtab.cat_def_cnt; j++) {
5637 if ((j + symtab.cls_def_cnt + 1) * sizeof(uint32_t) > defs_left) {
5638 outs() << "\t(remaining category defs entries entends past the end of "
5639 << "the section)\n";
5640 break;
5641 }
5642 memcpy(&def, defs + (j + symtab.cls_def_cnt) * sizeof(uint32_t),
5643 sizeof(uint32_t));
5644 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5645 sys::swapByteOrder(def);
5646
5647 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5648 outs() << "\tdefs[" << j + symtab.cls_def_cnt << "] "
5649 << format("0x%08" PRIx32, def);
5650 if (r != nullptr) {
5651 if (left > sizeof(struct objc_category_t)) {
5652 outs() << "\n";
5653 memcpy(&objc_category, r, sizeof(struct objc_category_t));
5654 } else {
5655 outs() << " (entends past the end of the section)\n";
5656 memset(&objc_category, '\0', sizeof(struct objc_category_t));
5657 memcpy(&objc_category, r, left);
5658 }
5659 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5660 swapStruct(objc_category);
5661 print_objc_objc_category_t(&objc_category, &info);
5662 } else {
5663 outs() << "(not in an __OBJC section)\n";
5664 }
5665 }
5666 }
5667 const SectionRef II = get_section(O, "__OBJC", "__image_info");
5668 if (II != SectionRef())
5669 print_image_info(II, &info);
5670
5671 return true;
Kevin Enderby0fc11822015-04-01 20:57:01 +00005672}
5673
Kevin Enderby4ad9bde2015-04-16 22:33:20 +00005674static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
5675 uint32_t size, uint32_t addr) {
5676 SymbolAddressMap AddrMap;
5677 CreateSymbolAddressMap(O, &AddrMap);
5678
5679 std::vector<SectionRef> Sections;
5680 for (const SectionRef &Section : O->sections()) {
5681 StringRef SectName;
5682 Section.getName(SectName);
5683 Sections.push_back(Section);
5684 }
5685
5686 struct DisassembleInfo info;
5687 // Set up the block of info used by the Symbolizer call backs.
5688 info.verbose = true;
5689 info.O = O;
5690 info.AddrMap = &AddrMap;
5691 info.Sections = &Sections;
5692 info.class_name = nullptr;
5693 info.selector_name = nullptr;
5694 info.method = nullptr;
5695 info.demangled_name = nullptr;
5696 info.bindtable = nullptr;
5697 info.adrp_addr = 0;
5698 info.adrp_inst = 0;
5699
5700 const char *p;
5701 struct objc_protocol_t protocol;
5702 uint32_t left, paddr;
5703 for (p = sect; p < sect + size; p += sizeof(struct objc_protocol_t)) {
5704 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
5705 left = size - (p - sect);
5706 if (left < sizeof(struct objc_protocol_t)) {
5707 outs() << "Protocol extends past end of __protocol section\n";
5708 memcpy(&protocol, p, left);
5709 } else
5710 memcpy(&protocol, p, sizeof(struct objc_protocol_t));
5711 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5712 swapStruct(protocol);
5713 paddr = addr + (p - sect);
5714 outs() << "Protocol " << format("0x%" PRIx32, paddr);
5715 if (print_protocol(paddr, 0, &info))
5716 outs() << "(not in an __OBJC section)\n";
5717 }
5718}
5719
Kevin Enderby9873e2c2016-05-23 21:34:12 +00005720#ifdef HAVE_LIBXAR
5721inline void swapStruct(struct xar_header &xar) {
5722 sys::swapByteOrder(xar.magic);
5723 sys::swapByteOrder(xar.size);
5724 sys::swapByteOrder(xar.version);
5725 sys::swapByteOrder(xar.toc_length_compressed);
5726 sys::swapByteOrder(xar.toc_length_uncompressed);
5727 sys::swapByteOrder(xar.cksum_alg);
5728}
5729
5730static void PrintModeVerbose(uint32_t mode) {
5731 switch(mode & S_IFMT){
5732 case S_IFDIR:
5733 outs() << "d";
5734 break;
5735 case S_IFCHR:
5736 outs() << "c";
5737 break;
5738 case S_IFBLK:
5739 outs() << "b";
5740 break;
5741 case S_IFREG:
5742 outs() << "-";
5743 break;
5744 case S_IFLNK:
5745 outs() << "l";
5746 break;
5747 case S_IFSOCK:
5748 outs() << "s";
5749 break;
5750 default:
5751 outs() << "?";
5752 break;
5753 }
5754
5755 /* owner permissions */
5756 if(mode & S_IREAD)
5757 outs() << "r";
5758 else
5759 outs() << "-";
5760 if(mode & S_IWRITE)
5761 outs() << "w";
5762 else
5763 outs() << "-";
5764 if(mode & S_ISUID)
5765 outs() << "s";
5766 else if(mode & S_IEXEC)
5767 outs() << "x";
5768 else
5769 outs() << "-";
5770
5771 /* group permissions */
5772 if(mode & (S_IREAD >> 3))
5773 outs() << "r";
5774 else
5775 outs() << "-";
5776 if(mode & (S_IWRITE >> 3))
5777 outs() << "w";
5778 else
5779 outs() << "-";
5780 if(mode & S_ISGID)
5781 outs() << "s";
5782 else if(mode & (S_IEXEC >> 3))
5783 outs() << "x";
5784 else
5785 outs() << "-";
5786
5787 /* other permissions */
5788 if(mode & (S_IREAD >> 6))
5789 outs() << "r";
5790 else
5791 outs() << "-";
5792 if(mode & (S_IWRITE >> 6))
5793 outs() << "w";
5794 else
5795 outs() << "-";
5796 if(mode & S_ISVTX)
5797 outs() << "t";
5798 else if(mode & (S_IEXEC >> 6))
5799 outs() << "x";
5800 else
5801 outs() << "-";
5802}
5803
5804static void PrintXarFilesSummary(const char *XarFilename, xar_t xar) {
5805 xar_iter_t xi;
5806 xar_file_t xf;
5807 xar_iter_t xp;
5808 const char *key, *type, *mode, *user, *group, *size, *mtime, *name, *m;
5809 char *endp;
5810 uint32_t mode_value;
5811
5812 xi = xar_iter_new();
5813 if (!xi) {
5814 errs() << "Can't obtain an xar iterator for xar archive "
5815 << XarFilename << "\n";
5816 return;
5817 }
5818
5819 // Go through the xar's files.
5820 for (xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)) {
5821 xp = xar_iter_new();
5822 if(!xp){
5823 errs() << "Can't obtain an xar iterator for xar archive "
5824 << XarFilename << "\n";
5825 return;
5826 }
5827 type = nullptr;
5828 mode = nullptr;
5829 user = nullptr;
5830 group = nullptr;
5831 size = nullptr;
5832 mtime = nullptr;
5833 name = nullptr;
5834 for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){
Jonas Devliegherec0a758d2017-09-18 14:15:57 +00005835 const char *val = nullptr;
Kevin Enderby9873e2c2016-05-23 21:34:12 +00005836 xar_prop_get(xf, key, &val);
5837#if 0 // Useful for debugging.
5838 outs() << "key: " << key << " value: " << val << "\n";
5839#endif
5840 if(strcmp(key, "type") == 0)
5841 type = val;
5842 if(strcmp(key, "mode") == 0)
5843 mode = val;
5844 if(strcmp(key, "user") == 0)
5845 user = val;
5846 if(strcmp(key, "group") == 0)
5847 group = val;
5848 if(strcmp(key, "data/size") == 0)
5849 size = val;
5850 if(strcmp(key, "mtime") == 0)
5851 mtime = val;
5852 if(strcmp(key, "name") == 0)
5853 name = val;
5854 }
5855 if(mode != nullptr){
5856 mode_value = strtoul(mode, &endp, 8);
5857 if(*endp != '\0')
5858 outs() << "(mode: \"" << mode << "\" contains non-octal chars) ";
5859 if(strcmp(type, "file") == 0)
5860 mode_value |= S_IFREG;
5861 PrintModeVerbose(mode_value);
5862 outs() << " ";
5863 }
5864 if(user != nullptr)
5865 outs() << format("%10s/", user);
5866 if(group != nullptr)
5867 outs() << format("%-10s ", group);
5868 if(size != nullptr)
5869 outs() << format("%7s ", size);
5870 if(mtime != nullptr){
5871 for(m = mtime; *m != 'T' && *m != '\0'; m++)
5872 outs() << *m;
5873 if(*m == 'T')
5874 m++;
5875 outs() << " ";
5876 for( ; *m != 'Z' && *m != '\0'; m++)
5877 outs() << *m;
5878 outs() << " ";
5879 }
5880 if(name != nullptr)
5881 outs() << name;
5882 outs() << "\n";
Francis Ricci1bae0ac2017-09-13 13:57:45 +00005883 xar_iter_free(xp);
Kevin Enderby9873e2c2016-05-23 21:34:12 +00005884 }
Francis Ricci1bae0ac2017-09-13 13:57:45 +00005885 xar_iter_free(xi);
Kevin Enderby9873e2c2016-05-23 21:34:12 +00005886}
5887
5888static void DumpBitcodeSection(MachOObjectFile *O, const char *sect,
5889 uint32_t size, bool verbose,
5890 bool PrintXarHeader, bool PrintXarFileHeaders,
5891 std::string XarMemberName) {
5892 if(size < sizeof(struct xar_header)) {
5893 outs() << "size of (__LLVM,__bundle) section too small (smaller than size "
5894 "of struct xar_header)\n";
5895 return;
5896 }
5897 struct xar_header XarHeader;
5898 memcpy(&XarHeader, sect, sizeof(struct xar_header));
5899 if (sys::IsLittleEndianHost)
5900 swapStruct(XarHeader);
5901 if (PrintXarHeader) {
5902 if (!XarMemberName.empty())
5903 outs() << "In xar member " << XarMemberName << ": ";
5904 else
5905 outs() << "For (__LLVM,__bundle) section: ";
5906 outs() << "xar header\n";
5907 if (XarHeader.magic == XAR_HEADER_MAGIC)
5908 outs() << " magic XAR_HEADER_MAGIC\n";
5909 else
5910 outs() << " magic "
5911 << format_hex(XarHeader.magic, 10, true)
5912 << " (not XAR_HEADER_MAGIC)\n";
5913 outs() << " size " << XarHeader.size << "\n";
5914 outs() << " version " << XarHeader.version << "\n";
5915 outs() << " toc_length_compressed " << XarHeader.toc_length_compressed
5916 << "\n";
5917 outs() << "toc_length_uncompressed " << XarHeader.toc_length_uncompressed
5918 << "\n";
5919 outs() << " cksum_alg ";
5920 switch (XarHeader.cksum_alg) {
5921 case XAR_CKSUM_NONE:
5922 outs() << "XAR_CKSUM_NONE\n";
5923 break;
5924 case XAR_CKSUM_SHA1:
5925 outs() << "XAR_CKSUM_SHA1\n";
5926 break;
5927 case XAR_CKSUM_MD5:
5928 outs() << "XAR_CKSUM_MD5\n";
5929 break;
Kevin Enderby42882282016-05-23 22:18:59 +00005930#ifdef XAR_CKSUM_SHA256
Kevin Enderby9873e2c2016-05-23 21:34:12 +00005931 case XAR_CKSUM_SHA256:
5932 outs() << "XAR_CKSUM_SHA256\n";
5933 break;
Kevin Enderby42882282016-05-23 22:18:59 +00005934#endif
5935#ifdef XAR_CKSUM_SHA512
Kevin Enderby9873e2c2016-05-23 21:34:12 +00005936 case XAR_CKSUM_SHA512:
5937 outs() << "XAR_CKSUM_SHA512\n";
5938 break;
Kevin Enderby42882282016-05-23 22:18:59 +00005939#endif
Kevin Enderby9873e2c2016-05-23 21:34:12 +00005940 default:
5941 outs() << XarHeader.cksum_alg << "\n";
5942 }
5943 }
5944
5945 SmallString<128> XarFilename;
5946 int FD;
5947 std::error_code XarEC =
5948 sys::fs::createTemporaryFile("llvm-objdump", "xar", FD, XarFilename);
5949 if (XarEC) {
5950 errs() << XarEC.message() << "\n";
5951 return;
5952 }
5953 tool_output_file XarFile(XarFilename, FD);
5954 raw_fd_ostream &XarOut = XarFile.os();
5955 StringRef XarContents(sect, size);
5956 XarOut << XarContents;
5957 XarOut.close();
5958 if (XarOut.has_error())
5959 return;
5960
5961 xar_t xar = xar_open(XarFilename.c_str(), READ);
5962 if (!xar) {
5963 errs() << "Can't create temporary xar archive " << XarFilename << "\n";
5964 return;
5965 }
5966
5967 SmallString<128> TocFilename;
5968 std::error_code TocEC =
5969 sys::fs::createTemporaryFile("llvm-objdump", "toc", TocFilename);
5970 if (TocEC) {
5971 errs() << TocEC.message() << "\n";
5972 return;
5973 }
5974 xar_serialize(xar, TocFilename.c_str());
5975
5976 if (PrintXarFileHeaders) {
5977 if (!XarMemberName.empty())
5978 outs() << "In xar member " << XarMemberName << ": ";
5979 else
5980 outs() << "For (__LLVM,__bundle) section: ";
5981 outs() << "xar archive files:\n";
5982 PrintXarFilesSummary(XarFilename.c_str(), xar);
5983 }
5984
5985 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
5986 MemoryBuffer::getFileOrSTDIN(TocFilename.c_str());
5987 if (std::error_code EC = FileOrErr.getError()) {
5988 errs() << EC.message() << "\n";
5989 return;
5990 }
5991 std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
5992
5993 if (!XarMemberName.empty())
5994 outs() << "In xar member " << XarMemberName << ": ";
5995 else
5996 outs() << "For (__LLVM,__bundle) section: ";
5997 outs() << "xar table of contents:\n";
5998 outs() << Buffer->getBuffer() << "\n";
5999
6000 // TODO: Go through the xar's files.
6001 xar_iter_t xi = xar_iter_new();
6002 if(!xi){
6003 errs() << "Can't obtain an xar iterator for xar archive "
6004 << XarFilename.c_str() << "\n";
6005 xar_close(xar);
6006 return;
6007 }
6008 for(xar_file_t xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)){
6009 const char *key;
6010 xar_iter_t xp;
6011 const char *member_name, *member_type, *member_size_string;
6012 size_t member_size;
6013
6014 xp = xar_iter_new();
6015 if(!xp){
6016 errs() << "Can't obtain an xar iterator for xar archive "
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +00006017 << XarFilename.c_str() << "\n";
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006018 xar_close(xar);
6019 return;
6020 }
6021 member_name = NULL;
6022 member_type = NULL;
6023 member_size_string = NULL;
6024 for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){
Jonas Devliegherec0a758d2017-09-18 14:15:57 +00006025 const char *val = nullptr;
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006026 xar_prop_get(xf, key, &val);
6027#if 0 // Useful for debugging.
6028 outs() << "key: " << key << " value: " << val << "\n";
6029#endif
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +00006030 if (strcmp(key, "name") == 0)
6031 member_name = val;
6032 if (strcmp(key, "type") == 0)
6033 member_type = val;
6034 if (strcmp(key, "data/size") == 0)
6035 member_size_string = val;
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006036 }
6037 /*
6038 * If we find a file with a name, date/size and type properties
6039 * and with the type being "file" see if that is a xar file.
6040 */
6041 if (member_name != NULL && member_type != NULL &&
6042 strcmp(member_type, "file") == 0 &&
6043 member_size_string != NULL){
6044 // Extract the file into a buffer.
6045 char *endptr;
6046 member_size = strtoul(member_size_string, &endptr, 10);
6047 if (*endptr == '\0' && member_size != 0) {
Francis Ricci1bae0ac2017-09-13 13:57:45 +00006048 char *buffer;
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +00006049 if (xar_extract_tobuffersz(xar, xf, &buffer, &member_size) == 0) {
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006050#if 0 // Useful for debugging.
6051 outs() << "xar member: " << member_name << " extracted\n";
6052#endif
6053 // Set the XarMemberName we want to see printed in the header.
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +00006054 std::string OldXarMemberName;
6055 // If XarMemberName is already set this is nested. So
6056 // save the old name and create the nested name.
6057 if (!XarMemberName.empty()) {
6058 OldXarMemberName = XarMemberName;
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006059 XarMemberName =
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +00006060 (Twine("[") + XarMemberName + "]" + member_name).str();
6061 } else {
6062 OldXarMemberName = "";
6063 XarMemberName = member_name;
6064 }
6065 // See if this is could be a xar file (nested).
6066 if (member_size >= sizeof(struct xar_header)) {
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006067#if 0 // Useful for debugging.
6068 outs() << "could be a xar file: " << member_name << "\n";
6069#endif
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +00006070 memcpy((char *)&XarHeader, buffer, sizeof(struct xar_header));
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006071 if (sys::IsLittleEndianHost)
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +00006072 swapStruct(XarHeader);
6073 if (XarHeader.magic == XAR_HEADER_MAGIC)
6074 DumpBitcodeSection(O, buffer, member_size, verbose,
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006075 PrintXarHeader, PrintXarFileHeaders,
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +00006076 XarMemberName);
6077 }
6078 XarMemberName = OldXarMemberName;
Francis Ricci1bae0ac2017-09-13 13:57:45 +00006079 delete buffer;
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +00006080 }
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006081 }
6082 }
6083 xar_iter_free(xp);
6084 }
Francis Ricci1bae0ac2017-09-13 13:57:45 +00006085 xar_iter_free(xi);
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006086 xar_close(xar);
6087}
6088#endif // defined(HAVE_LIBXAR)
6089
Kevin Enderby0fc11822015-04-01 20:57:01 +00006090static void printObjcMetaData(MachOObjectFile *O, bool verbose) {
6091 if (O->is64Bit())
6092 printObjc2_64bit_MetaData(O, verbose);
6093 else {
6094 MachO::mach_header H;
6095 H = O->getHeader();
6096 if (H.cputype == MachO::CPU_TYPE_ARM)
6097 printObjc2_32bit_MetaData(O, verbose);
6098 else {
6099 // This is the 32-bit non-arm cputype case. Which is normally
6100 // the first Objective-C ABI. But it may be the case of a
6101 // binary for the iOS simulator which is the second Objective-C
6102 // ABI. In that case printObjc1_32bit_MetaData() will determine that
6103 // and return false.
Hans Wennborgcc9deb42015-09-29 18:02:48 +00006104 if (!printObjc1_32bit_MetaData(O, verbose))
Kevin Enderby0fc11822015-04-01 20:57:01 +00006105 printObjc2_32bit_MetaData(O, verbose);
6106 }
6107 }
6108}
6109
Kevin Enderbybf246f52014-09-24 23:08:22 +00006110// GuessLiteralPointer returns a string which for the item in the Mach-O file
6111// for the address passed in as ReferenceValue for printing as a comment with
6112// the instruction and also returns the corresponding type of that item
6113// indirectly through ReferenceType.
6114//
6115// If ReferenceValue is an address of literal cstring then a pointer to the
6116// cstring is returned and ReferenceType is set to
6117// LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr .
6118//
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006119// If ReferenceValue is an address of an Objective-C CFString, Selector ref or
6120// Class ref that name is returned and the ReferenceType is set accordingly.
6121//
6122// Lastly, literals which are Symbol address in a literal pool are looked for
6123// and if found the symbol name is returned and ReferenceType is set to
6124// LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr .
6125//
6126// If there is no item in the Mach-O file for the address passed in as
6127// ReferenceValue nullptr is returned and ReferenceType is unchanged.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00006128static const char *GuessLiteralPointer(uint64_t ReferenceValue,
6129 uint64_t ReferencePC,
6130 uint64_t *ReferenceType,
6131 struct DisassembleInfo *info) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00006132 // First see if there is an external relocation entry at the ReferencePC.
Kevin Enderbyd90a4172015-10-10 00:05:01 +00006133 if (info->O->getHeader().filetype == MachO::MH_OBJECT) {
6134 uint64_t sect_addr = info->S.getAddress();
6135 uint64_t sect_offset = ReferencePC - sect_addr;
6136 bool reloc_found = false;
6137 DataRefImpl Rel;
6138 MachO::any_relocation_info RE;
6139 bool isExtern = false;
6140 SymbolRef Symbol;
6141 for (const RelocationRef &Reloc : info->S.relocations()) {
6142 uint64_t RelocOffset = Reloc.getOffset();
6143 if (RelocOffset == sect_offset) {
6144 Rel = Reloc.getRawDataRefImpl();
6145 RE = info->O->getRelocation(Rel);
6146 if (info->O->isRelocationScattered(RE))
6147 continue;
6148 isExtern = info->O->getPlainRelocationExternal(RE);
6149 if (isExtern) {
6150 symbol_iterator RelocSym = Reloc.getSymbol();
6151 Symbol = *RelocSym;
6152 }
6153 reloc_found = true;
6154 break;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006155 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006156 }
Kevin Enderbyd90a4172015-10-10 00:05:01 +00006157 // If there is an external relocation entry for a symbol in a section
6158 // then used that symbol's value for the value of the reference.
6159 if (reloc_found && isExtern) {
6160 if (info->O->getAnyRelocationPCRel(RE)) {
6161 unsigned Type = info->O->getAnyRelocationType(RE);
6162 if (Type == MachO::X86_64_RELOC_SIGNED) {
6163 ReferenceValue = Symbol.getValue();
6164 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006165 }
6166 }
6167 }
6168
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006169 // Look for literals such as Objective-C CFStrings refs, Selector refs,
6170 // Message refs and Class refs.
6171 bool classref, selref, msgref, cfstring;
6172 uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref,
6173 selref, msgref, cfstring);
David Blaikie33dd45d02015-03-23 18:39:02 +00006174 if (classref && pointer_value == 0) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006175 // Note the ReferenceValue is a pointer into the __objc_classrefs section.
6176 // And the pointer_value in that section is typically zero as it will be
6177 // set by dyld as part of the "bind information".
6178 const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info);
6179 if (name != nullptr) {
6180 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
Hans Wennborgdb53e302014-10-23 21:59:17 +00006181 const char *class_name = strrchr(name, '$');
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006182 if (class_name != nullptr && class_name[1] == '_' &&
6183 class_name[2] != '\0') {
6184 info->class_name = class_name + 2;
6185 return name;
6186 }
6187 }
6188 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006189
David Blaikie33dd45d02015-03-23 18:39:02 +00006190 if (classref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006191 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
6192 const char *name =
6193 get_objc2_64bit_class_name(pointer_value, ReferenceValue, info);
6194 if (name != nullptr)
6195 info->class_name = name;
6196 else
6197 name = "bad class ref";
Kevin Enderbybf246f52014-09-24 23:08:22 +00006198 return name;
6199 }
6200
David Blaikie33dd45d02015-03-23 18:39:02 +00006201 if (cfstring) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006202 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref;
6203 const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info);
6204 return name;
6205 }
6206
David Blaikie33dd45d02015-03-23 18:39:02 +00006207 if (selref && pointer_value == 0)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006208 pointer_value = get_objc2_64bit_selref(ReferenceValue, info);
6209
6210 if (pointer_value != 0)
6211 ReferenceValue = pointer_value;
6212
6213 const char *name = GuessCstringPointer(ReferenceValue, info);
6214 if (name) {
David Blaikie33dd45d02015-03-23 18:39:02 +00006215 if (pointer_value != 0 && selref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006216 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref;
6217 info->selector_name = name;
David Blaikie33dd45d02015-03-23 18:39:02 +00006218 } else if (pointer_value != 0 && msgref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006219 info->class_name = nullptr;
6220 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref;
6221 info->selector_name = name;
6222 } else
6223 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr;
6224 return name;
6225 }
6226
6227 // Lastly look for an indirect symbol with this ReferenceValue which is in
6228 // a literal pool. If found return that symbol name.
6229 name = GuessIndirectSymbol(ReferenceValue, info);
6230 if (name) {
6231 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr;
6232 return name;
6233 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006234
6235 return nullptr;
6236}
6237
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006238// SymbolizerSymbolLookUp is the symbol lookup function passed when creating
Kevin Enderbybf246f52014-09-24 23:08:22 +00006239// the Symbolizer. It looks up the ReferenceValue using the info passed via the
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006240// pointer to the struct DisassembleInfo that was passed when MCSymbolizer
6241// is created and returns the symbol name that matches the ReferenceValue or
6242// nullptr if none. The ReferenceType is passed in for the IN type of
6243// reference the instruction is making from the values in defined in the header
6244// "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific
6245// Out type and the ReferenceName will also be set which is added as a comment
6246// to the disassembled instruction.
6247//
Kevin Enderby04bf6932014-10-28 23:39:46 +00006248// If the symbol name is a C++ mangled name then the demangled name is
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006249// returned through ReferenceName and ReferenceType is set to
6250// LLVMDisassembler_ReferenceType_DeMangled_Name .
6251//
6252// When this is called to get a symbol name for a branch target then the
6253// ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
6254// SymbolValue will be looked for in the indirect symbol table to determine if
6255// it is an address for a symbol stub. If so then the symbol name for that
6256// stub is returned indirectly through ReferenceName and then ReferenceType is
6257// set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
6258//
Kevin Enderbybf246f52014-09-24 23:08:22 +00006259// When this is called with an value loaded via a PC relative load then
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006260// ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
6261// SymbolValue is checked to be an address of literal pointer, symbol pointer,
6262// or an Objective-C meta data reference. If so the output ReferenceType is
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006263// set to correspond to that as well as setting the ReferenceName.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00006264static const char *SymbolizerSymbolLookUp(void *DisInfo,
6265 uint64_t ReferenceValue,
6266 uint64_t *ReferenceType,
6267 uint64_t ReferencePC,
6268 const char **ReferenceName) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006269 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006270 // If no verbose symbolic information is wanted then just return nullptr.
David Blaikie33dd45d02015-03-23 18:39:02 +00006271 if (!info->verbose) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00006272 *ReferenceName = nullptr;
6273 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006274 return nullptr;
6275 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006276
Kevin Enderbyf6d25852015-01-31 00:37:11 +00006277 const char *SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
Kevin Enderbybf246f52014-09-24 23:08:22 +00006278
Kevin Enderby85974882014-09-26 22:20:44 +00006279 if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) {
6280 *ReferenceName = GuessIndirectSymbol(ReferenceValue, info);
Kevin Enderby04bf6932014-10-28 23:39:46 +00006281 if (*ReferenceName != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006282 method_reference(info, ReferenceType, ReferenceName);
6283 if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message)
6284 *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub;
Rafael Espindolab940b662016-09-06 19:16:48 +00006285 } else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
Kevin Enderby04bf6932014-10-28 23:39:46 +00006286 if (info->demangled_name != nullptr)
6287 free(info->demangled_name);
6288 int status;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006289 info->demangled_name =
Rafael Espindolab940b662016-09-06 19:16:48 +00006290 itaniumDemangle(SymbolName + 1, nullptr, nullptr, &status);
Kevin Enderby04bf6932014-10-28 23:39:46 +00006291 if (info->demangled_name != nullptr) {
6292 *ReferenceName = info->demangled_name;
6293 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
6294 } else
6295 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6296 } else
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006297 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6298 } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) {
6299 *ReferenceName =
6300 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
Kevin Enderby85974882014-09-26 22:20:44 +00006301 if (*ReferenceName)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006302 method_reference(info, ReferenceType, ReferenceName);
Kevin Enderby85974882014-09-26 22:20:44 +00006303 else
6304 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006305 // If this is arm64 and the reference is an adrp instruction save the
6306 // instruction, passed in ReferenceValue and the address of the instruction
6307 // for use later if we see and add immediate instruction.
6308 } else if (info->O->getArch() == Triple::aarch64 &&
6309 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) {
6310 info->adrp_inst = ReferenceValue;
6311 info->adrp_addr = ReferencePC;
6312 SymbolName = nullptr;
6313 *ReferenceName = nullptr;
6314 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6315 // If this is arm64 and reference is an add immediate instruction and we
6316 // have
6317 // seen an adrp instruction just before it and the adrp's Xd register
6318 // matches
6319 // this add's Xn register reconstruct the value being referenced and look to
6320 // see if it is a literal pointer. Note the add immediate instruction is
6321 // passed in ReferenceValue.
6322 } else if (info->O->getArch() == Triple::aarch64 &&
6323 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri &&
6324 ReferencePC - 4 == info->adrp_addr &&
6325 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
6326 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
6327 uint32_t addxri_inst;
6328 uint64_t adrp_imm, addxri_imm;
6329
6330 adrp_imm =
6331 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
6332 if (info->adrp_inst & 0x0200000)
6333 adrp_imm |= 0xfffffffffc000000LL;
6334
6335 addxri_inst = ReferenceValue;
6336 addxri_imm = (addxri_inst >> 10) & 0xfff;
6337 if (((addxri_inst >> 22) & 0x3) == 1)
6338 addxri_imm <<= 12;
6339
6340 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
6341 (adrp_imm << 12) + addxri_imm;
6342
6343 *ReferenceName =
6344 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6345 if (*ReferenceName == nullptr)
6346 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6347 // If this is arm64 and the reference is a load register instruction and we
6348 // have seen an adrp instruction just before it and the adrp's Xd register
6349 // matches this add's Xn register reconstruct the value being referenced and
6350 // look to see if it is a literal pointer. Note the load register
6351 // instruction is passed in ReferenceValue.
6352 } else if (info->O->getArch() == Triple::aarch64 &&
6353 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui &&
6354 ReferencePC - 4 == info->adrp_addr &&
6355 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
6356 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
6357 uint32_t ldrxui_inst;
6358 uint64_t adrp_imm, ldrxui_imm;
6359
6360 adrp_imm =
6361 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
6362 if (info->adrp_inst & 0x0200000)
6363 adrp_imm |= 0xfffffffffc000000LL;
6364
6365 ldrxui_inst = ReferenceValue;
6366 ldrxui_imm = (ldrxui_inst >> 10) & 0xfff;
6367
6368 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
6369 (adrp_imm << 12) + (ldrxui_imm << 3);
6370
6371 *ReferenceName =
6372 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6373 if (*ReferenceName == nullptr)
6374 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6375 }
6376 // If this arm64 and is an load register (PC-relative) instruction the
6377 // ReferenceValue is the PC plus the immediate value.
6378 else if (info->O->getArch() == Triple::aarch64 &&
6379 (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl ||
6380 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR)) {
6381 *ReferenceName =
6382 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6383 if (*ReferenceName == nullptr)
6384 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Rafael Espindolab940b662016-09-06 19:16:48 +00006385 } else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
Kevin Enderby04bf6932014-10-28 23:39:46 +00006386 if (info->demangled_name != nullptr)
6387 free(info->demangled_name);
6388 int status;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006389 info->demangled_name =
Rafael Espindolab940b662016-09-06 19:16:48 +00006390 itaniumDemangle(SymbolName + 1, nullptr, nullptr, &status);
Kevin Enderby04bf6932014-10-28 23:39:46 +00006391 if (info->demangled_name != nullptr) {
6392 *ReferenceName = info->demangled_name;
6393 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
6394 }
6395 }
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006396 else {
Kevin Enderbybf246f52014-09-24 23:08:22 +00006397 *ReferenceName = nullptr;
6398 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6399 }
6400
6401 return SymbolName;
6402}
6403
Kevin Enderbybf246f52014-09-24 23:08:22 +00006404/// \brief Emits the comments that are stored in the CommentStream.
6405/// Each comment in the CommentStream must end with a newline.
6406static void emitComments(raw_svector_ostream &CommentStream,
6407 SmallString<128> &CommentsToEmit,
6408 formatted_raw_ostream &FormattedOS,
6409 const MCAsmInfo &MAI) {
6410 // Flush the stream before taking its content.
Kevin Enderbybf246f52014-09-24 23:08:22 +00006411 StringRef Comments = CommentsToEmit.str();
6412 // Get the default information for printing a comment.
Mehdi Amini36d33fc2016-10-01 06:46:33 +00006413 StringRef CommentBegin = MAI.getCommentString();
Kevin Enderbybf246f52014-09-24 23:08:22 +00006414 unsigned CommentColumn = MAI.getCommentColumn();
6415 bool IsFirst = true;
6416 while (!Comments.empty()) {
6417 if (!IsFirst)
6418 FormattedOS << '\n';
6419 // Emit a line of comments.
6420 FormattedOS.PadToColumn(CommentColumn);
6421 size_t Position = Comments.find('\n');
6422 FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
6423 // Move after the newline character.
6424 Comments = Comments.substr(Position + 1);
6425 IsFirst = false;
6426 }
6427 FormattedOS.flush();
6428
6429 // Tell the comment stream that the vector changed underneath it.
6430 CommentsToEmit.clear();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006431}
6432
Kevin Enderby95df54c2015-02-04 01:01:38 +00006433static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
6434 StringRef DisSegName, StringRef DisSectName) {
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006435 const char *McpuDefault = nullptr;
6436 const Target *ThumbTarget = nullptr;
6437 const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006438 if (!TheTarget) {
6439 // GetTarget prints out stuff.
6440 return;
6441 }
Kevin Enderbyf310e622017-09-21 21:45:02 +00006442 std::string MachOMCPU;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006443 if (MCPU.empty() && McpuDefault)
Kevin Enderbyf310e622017-09-21 21:45:02 +00006444 MachOMCPU = McpuDefault;
6445 else
6446 MachOMCPU = MCPU;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006447
Ahmed Charles56440fd2014-03-06 05:51:42 +00006448 std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006449 std::unique_ptr<const MCInstrInfo> ThumbInstrInfo;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006450 if (ThumbTarget)
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006451 ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo());
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006452
Kevin Enderbyc9595622014-08-06 23:24:41 +00006453 // Package up features to be passed to target/subtarget
6454 std::string FeaturesStr;
6455 if (MAttrs.size()) {
6456 SubtargetFeatures Features;
6457 for (unsigned i = 0; i != MAttrs.size(); ++i)
6458 Features.AddFeature(MAttrs[i]);
6459 FeaturesStr = Features.getString();
6460 }
6461
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006462 // Set up disassembler.
Ahmed Charles56440fd2014-03-06 05:51:42 +00006463 std::unique_ptr<const MCRegisterInfo> MRI(
6464 TheTarget->createMCRegInfo(TripleName));
6465 std::unique_ptr<const MCAsmInfo> AsmInfo(
Rafael Espindola227144c2013-05-13 01:16:13 +00006466 TheTarget->createMCAsmInfo(*MRI, TripleName));
Ahmed Charles56440fd2014-03-06 05:51:42 +00006467 std::unique_ptr<const MCSubtargetInfo> STI(
Kevin Enderbyf310e622017-09-21 21:45:02 +00006468 TheTarget->createMCSubtargetInfo(TripleName, MachOMCPU, FeaturesStr));
Craig Toppere6cb63e2014-04-25 04:24:47 +00006469 MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr);
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006470 std::unique_ptr<MCDisassembler> DisAsm(
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006471 TheTarget->createMCDisassembler(*STI, Ctx));
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006472 std::unique_ptr<MCSymbolizer> Symbolizer;
6473 struct DisassembleInfo SymbolizerInfo;
6474 std::unique_ptr<MCRelocationInfo> RelInfo(
6475 TheTarget->createMCRelocationInfo(TripleName, Ctx));
6476 if (RelInfo) {
6477 Symbolizer.reset(TheTarget->createMCSymbolizer(
6478 TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
David Blaikie186db432015-01-18 20:45:48 +00006479 &SymbolizerInfo, &Ctx, std::move(RelInfo)));
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006480 DisAsm->setSymbolizer(std::move(Symbolizer));
6481 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006482 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
Ahmed Charles56440fd2014-03-06 05:51:42 +00006483 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
Daniel Sanders50f17232015-09-15 16:17:27 +00006484 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI));
Kevin Enderbybf246f52014-09-24 23:08:22 +00006485 // Set the display preference for hex vs. decimal immediates.
6486 IP->setPrintImmHex(PrintImmHex);
6487 // Comment stream and backing vector.
6488 SmallString<128> CommentsToEmit;
6489 raw_svector_ostream CommentStream(CommentsToEmit);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00006490 // FIXME: Setting the CommentStream in the InstPrinter is problematic in that
6491 // if it is done then arm64 comments for string literals don't get printed
6492 // and some constant get printed instead and not setting it causes intel
6493 // (32-bit and 64-bit) comments printed with different spacing before the
6494 // comment causing different diffs with the 'C' disassembler library API.
6495 // IP->setCommentStream(CommentStream);
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006496
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006497 if (!AsmInfo || !STI || !DisAsm || !IP) {
Michael J. Spencerc1363cf2011-10-07 19:25:47 +00006498 errs() << "error: couldn't initialize disassembler for target "
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006499 << TripleName << '\n';
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006500 return;
6501 }
6502
Tim Northover09ca33e2016-04-22 23:23:31 +00006503 // Set up separate thumb disassembler if needed.
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006504 std::unique_ptr<const MCRegisterInfo> ThumbMRI;
6505 std::unique_ptr<const MCAsmInfo> ThumbAsmInfo;
6506 std::unique_ptr<const MCSubtargetInfo> ThumbSTI;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006507 std::unique_ptr<MCDisassembler> ThumbDisAsm;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006508 std::unique_ptr<MCInstPrinter> ThumbIP;
6509 std::unique_ptr<MCContext> ThumbCtx;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006510 std::unique_ptr<MCSymbolizer> ThumbSymbolizer;
6511 struct DisassembleInfo ThumbSymbolizerInfo;
6512 std::unique_ptr<MCRelocationInfo> ThumbRelInfo;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006513 if (ThumbTarget) {
6514 ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName));
6515 ThumbAsmInfo.reset(
6516 ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName));
6517 ThumbSTI.reset(
Kevin Enderbyf310e622017-09-21 21:45:02 +00006518 ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MachOMCPU,
6519 FeaturesStr));
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006520 ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr));
6521 ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx));
Kevin Enderby930fdc72014-11-06 19:00:13 +00006522 MCContext *PtrThumbCtx = ThumbCtx.get();
6523 ThumbRelInfo.reset(
6524 ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx));
6525 if (ThumbRelInfo) {
6526 ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer(
6527 ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
David Blaikie186db432015-01-18 20:45:48 +00006528 &ThumbSymbolizerInfo, PtrThumbCtx, std::move(ThumbRelInfo)));
Kevin Enderby930fdc72014-11-06 19:00:13 +00006529 ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer));
6530 }
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006531 int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect();
6532 ThumbIP.reset(ThumbTarget->createMCInstPrinter(
Daniel Sanders50f17232015-09-15 16:17:27 +00006533 Triple(ThumbTripleName), ThumbAsmPrinterVariant, *ThumbAsmInfo,
6534 *ThumbInstrInfo, *ThumbMRI));
Kevin Enderbybf246f52014-09-24 23:08:22 +00006535 // Set the display preference for hex vs. decimal immediates.
6536 ThumbIP->setPrintImmHex(PrintImmHex);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006537 }
6538
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006539 if (ThumbTarget && (!ThumbAsmInfo || !ThumbSTI || !ThumbDisAsm || !ThumbIP)) {
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006540 errs() << "error: couldn't initialize disassembler for target "
6541 << ThumbTripleName << '\n';
6542 return;
6543 }
6544
Charles Davis8bdfafd2013-09-01 04:28:48 +00006545 MachO::mach_header Header = MachOOF->getHeader();
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006546
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006547 // FIXME: Using the -cfg command line option, this code used to be able to
6548 // annotate relocations with the referenced symbol's name, and if this was
6549 // inside a __[cf]string section, the data it points to. This is now replaced
6550 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
Owen Andersond9243c42011-10-17 21:37:35 +00006551 std::vector<SectionRef> Sections;
6552 std::vector<SymbolRef> Symbols;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006553 SmallVector<uint64_t, 8> FoundFns;
Kevin Enderby273ae012013-06-06 17:20:50 +00006554 uint64_t BaseSegmentAddress;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006555
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00006556 getSectionsAndSymbols(MachOOF, Sections, Symbols, FoundFns,
Kevin Enderby273ae012013-06-06 17:20:50 +00006557 BaseSegmentAddress);
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006558
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006559 // Sort the symbols by address, just in case they didn't come in that way.
Owen Andersond9243c42011-10-17 21:37:35 +00006560 std::sort(Symbols.begin(), Symbols.end(), SymbolSorter());
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006561
Kevin Enderby273ae012013-06-06 17:20:50 +00006562 // Build a data in code table that is sorted on by the address of each entry.
6563 uint64_t BaseAddress = 0;
Charles Davis8bdfafd2013-09-01 04:28:48 +00006564 if (Header.filetype == MachO::MH_OBJECT)
Rafael Espindola80291272014-10-08 15:28:58 +00006565 BaseAddress = Sections[0].getAddress();
Kevin Enderby273ae012013-06-06 17:20:50 +00006566 else
6567 BaseAddress = BaseSegmentAddress;
6568 DiceTable Dices;
Kevin Enderby273ae012013-06-06 17:20:50 +00006569 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
Rafael Espindola5e812af2014-01-30 02:49:50 +00006570 DI != DE; ++DI) {
Kevin Enderby273ae012013-06-06 17:20:50 +00006571 uint32_t Offset;
6572 DI->getOffset(Offset);
6573 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
6574 }
6575 array_pod_sort(Dices.begin(), Dices.end());
6576
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006577#ifndef NDEBUG
6578 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
6579#else
6580 raw_ostream &DebugOut = nulls();
6581#endif
6582
Ahmed Charles56440fd2014-03-06 05:51:42 +00006583 std::unique_ptr<DIContext> diContext;
Rafael Espindola9b709252013-04-13 01:45:40 +00006584 ObjectFile *DbgObj = MachOOF;
Benjamin Kramer699128e2011-09-21 01:13:19 +00006585 // Try to find debug info and set up the DIContext for it.
6586 if (UseDbg) {
Benjamin Kramer699128e2011-09-21 01:13:19 +00006587 // A separate DSym file path was specified, parse it as a macho file,
6588 // get the sections and supply it to the section name parsing machinery.
6589 if (!DSYMFile.empty()) {
Rafael Espindola48af1c22014-08-19 18:44:46 +00006590 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
Rafael Espindolaadf21f22014-07-06 17:43:13 +00006591 MemoryBuffer::getFileOrSTDIN(DSYMFile);
Rafael Espindola48af1c22014-08-19 18:44:46 +00006592 if (std::error_code EC = BufOrErr.getError()) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +00006593 errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n';
Benjamin Kramer699128e2011-09-21 01:13:19 +00006594 return;
6595 }
Rafael Espindola48af1c22014-08-19 18:44:46 +00006596 DbgObj =
6597 ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef())
6598 .get()
6599 .release();
Benjamin Kramer699128e2011-09-21 01:13:19 +00006600 }
6601
Eric Christopher7370b552012-11-12 21:40:38 +00006602 // Setup the DIContext
Rafael Espindolac398e672017-07-19 22:27:28 +00006603 diContext = DWARFContext::create(*DbgObj);
Benjamin Kramer699128e2011-09-21 01:13:19 +00006604 }
6605
Colin LeMahieufcc32762015-07-29 19:08:10 +00006606 if (FilterSections.size() == 0)
Kevin Enderby95df54c2015-02-04 01:01:38 +00006607 outs() << "(" << DisSegName << "," << DisSectName << ") section\n";
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00006608
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006609 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
Owen Andersond9243c42011-10-17 21:37:35 +00006610 StringRef SectName;
Kevin Enderby95df54c2015-02-04 01:01:38 +00006611 if (Sections[SectIdx].getName(SectName) || SectName != DisSectName)
6612 continue;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006613
Rafael Espindolaa9f810b2012-12-21 03:47:03 +00006614 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006615
Rafael Espindolab0f76a42013-04-05 15:15:22 +00006616 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
Kevin Enderby95df54c2015-02-04 01:01:38 +00006617 if (SegmentName != DisSegName)
Rafael Espindolaa9f810b2012-12-21 03:47:03 +00006618 continue;
6619
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006620 StringRef BytesStr;
6621 Sections[SectIdx].getContents(BytesStr);
Aaron Ballman106fd7b2014-11-12 14:01:17 +00006622 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
6623 BytesStr.size());
Rafael Espindola80291272014-10-08 15:28:58 +00006624 uint64_t SectAddress = Sections[SectIdx].getAddress();
Rafael Espindolabd604f22014-11-07 00:52:15 +00006625
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006626 bool symbolTableWorked = false;
6627
Kevin Enderbybf246f52014-09-24 23:08:22 +00006628 // Create a map of symbol addresses to symbol names for use by
6629 // the SymbolizerSymbolLookUp() routine.
6630 SymbolAddressMap AddrMap;
Kevin Enderby6a221752015-03-17 17:10:57 +00006631 bool DisSymNameFound = false;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006632 for (const SymbolRef &Symbol : MachOOF->symbols()) {
Kevin Enderby7bd8d992016-05-02 20:28:12 +00006633 Expected<SymbolRef::Type> STOrErr = Symbol.getType();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00006634 if (!STOrErr)
6635 report_error(MachOOF->getFileName(), STOrErr.takeError());
Kevin Enderby5afbc1c2016-03-23 20:27:00 +00006636 SymbolRef::Type ST = *STOrErr;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006637 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
6638 ST == SymbolRef::ST_Other) {
Rafael Espindoladea00162015-07-03 17:44:18 +00006639 uint64_t Address = Symbol.getValue();
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00006640 Expected<StringRef> SymNameOrErr = Symbol.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00006641 if (!SymNameOrErr)
6642 report_error(MachOOF->getFileName(), SymNameOrErr.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00006643 StringRef SymName = *SymNameOrErr;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006644 AddrMap[Address] = SymName;
Kevin Enderby6a221752015-03-17 17:10:57 +00006645 if (!DisSymName.empty() && DisSymName == SymName)
6646 DisSymNameFound = true;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006647 }
6648 }
David Blaikie33dd45d02015-03-23 18:39:02 +00006649 if (!DisSymName.empty() && !DisSymNameFound) {
Kevin Enderby6a221752015-03-17 17:10:57 +00006650 outs() << "Can't find -dis-symname: " << DisSymName << "\n";
6651 return;
6652 }
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006653 // Set up the block of info used by the Symbolizer call backs.
Kevin Enderby8e29ec92015-03-17 22:26:11 +00006654 SymbolizerInfo.verbose = !NoSymbolicOperands;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006655 SymbolizerInfo.O = MachOOF;
6656 SymbolizerInfo.S = Sections[SectIdx];
Kevin Enderbybf246f52014-09-24 23:08:22 +00006657 SymbolizerInfo.AddrMap = &AddrMap;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006658 SymbolizerInfo.Sections = &Sections;
6659 SymbolizerInfo.class_name = nullptr;
6660 SymbolizerInfo.selector_name = nullptr;
6661 SymbolizerInfo.method = nullptr;
Kevin Enderby04bf6932014-10-28 23:39:46 +00006662 SymbolizerInfo.demangled_name = nullptr;
Kevin Enderby078be602014-10-23 19:53:12 +00006663 SymbolizerInfo.bindtable = nullptr;
Kevin Enderby10738222014-11-19 20:20:16 +00006664 SymbolizerInfo.adrp_addr = 0;
6665 SymbolizerInfo.adrp_inst = 0;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006666 // Same for the ThumbSymbolizer
Kevin Enderby8e29ec92015-03-17 22:26:11 +00006667 ThumbSymbolizerInfo.verbose = !NoSymbolicOperands;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006668 ThumbSymbolizerInfo.O = MachOOF;
6669 ThumbSymbolizerInfo.S = Sections[SectIdx];
6670 ThumbSymbolizerInfo.AddrMap = &AddrMap;
6671 ThumbSymbolizerInfo.Sections = &Sections;
6672 ThumbSymbolizerInfo.class_name = nullptr;
6673 ThumbSymbolizerInfo.selector_name = nullptr;
6674 ThumbSymbolizerInfo.method = nullptr;
6675 ThumbSymbolizerInfo.demangled_name = nullptr;
6676 ThumbSymbolizerInfo.bindtable = nullptr;
Kevin Enderby10738222014-11-19 20:20:16 +00006677 ThumbSymbolizerInfo.adrp_addr = 0;
6678 ThumbSymbolizerInfo.adrp_inst = 0;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006679
Kevin Enderby4b627be2016-04-28 20:14:13 +00006680 unsigned int Arch = MachOOF->getArch();
6681
Tim Northoverf203ab52016-07-14 22:13:32 +00006682 // Skip all symbols if this is a stubs file.
6683 if (Bytes.size() == 0)
6684 return;
6685
Kevin Enderbyc138da32017-02-06 18:43:18 +00006686 // If the section has symbols but no symbol at the start of the section
6687 // these are used to make sure the bytes before the first symbol are
6688 // disassembled.
6689 bool FirstSymbol = true;
6690 bool FirstSymbolAtSectionStart = true;
6691
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006692 // Disassemble symbol by symbol.
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006693 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00006694 Expected<StringRef> SymNameOrErr = Symbols[SymIdx].getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00006695 if (!SymNameOrErr)
6696 report_error(MachOOF->getFileName(), SymNameOrErr.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00006697 StringRef SymName = *SymNameOrErr;
Owen Andersond9243c42011-10-17 21:37:35 +00006698
Kevin Enderby7bd8d992016-05-02 20:28:12 +00006699 Expected<SymbolRef::Type> STOrErr = Symbols[SymIdx].getType();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00006700 if (!STOrErr)
6701 report_error(MachOOF->getFileName(), STOrErr.takeError());
Kevin Enderby5afbc1c2016-03-23 20:27:00 +00006702 SymbolRef::Type ST = *STOrErr;
Kuba Breckade833222015-11-12 09:40:29 +00006703 if (ST != SymbolRef::ST_Function && ST != SymbolRef::ST_Data)
Owen Andersond9243c42011-10-17 21:37:35 +00006704 continue;
6705
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006706 // Make sure the symbol is defined in this section.
Rafael Espindola80291272014-10-08 15:28:58 +00006707 bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]);
Kevin Enderbyd8a6e832016-06-15 21:14:01 +00006708 if (!containsSym) {
6709 if (!DisSymName.empty() && DisSymName == SymName) {
6710 outs() << "-dis-symname: " << DisSymName << " not in the section\n";
6711 return;
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +00006712 }
Kevin Enderbyd8a6e832016-06-15 21:14:01 +00006713 continue;
6714 }
6715 // The __mh_execute_header is special and we need to deal with that fact
6716 // this symbol is before the start of the (__TEXT,__text) section and at the
6717 // address of the start of the __TEXT segment. This is because this symbol
6718 // is an N_SECT symbol in the (__TEXT,__text) but its address is before the
6719 // start of the section in a standard MH_EXECUTE filetype.
6720 if (!DisSymName.empty() && DisSymName == "__mh_execute_header") {
6721 outs() << "-dis-symname: __mh_execute_header not in any section\n";
6722 return;
6723 }
Tim Northoverfbefee32016-07-14 23:13:03 +00006724 // When this code is trying to disassemble a symbol at a time and in the
6725 // case there is only the __mh_execute_header symbol left as in a stripped
6726 // executable, we need to deal with this by ignoring this symbol so the
6727 // whole section is disassembled and this symbol is then not displayed.
6728 if (SymName == "__mh_execute_header" || SymName == "__mh_dylib_header" ||
6729 SymName == "__mh_bundle_header" || SymName == "__mh_object_header" ||
6730 SymName == "__mh_preload_header" || SymName == "__mh_dylinker_header")
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006731 continue;
6732
Kevin Enderby6a221752015-03-17 17:10:57 +00006733 // If we are only disassembling one symbol see if this is that symbol.
6734 if (!DisSymName.empty() && DisSymName != SymName)
6735 continue;
6736
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006737 // Start at the address of the symbol relative to the section's address.
Tim Northoverf203ab52016-07-14 22:13:32 +00006738 uint64_t SectSize = Sections[SectIdx].getSize();
Rafael Espindoladea00162015-07-03 17:44:18 +00006739 uint64_t Start = Symbols[SymIdx].getValue();
Rafael Espindola80291272014-10-08 15:28:58 +00006740 uint64_t SectionAddress = Sections[SectIdx].getAddress();
Cameron Zwarich54478a52012-02-03 05:42:17 +00006741 Start -= SectionAddress;
Owen Andersond9243c42011-10-17 21:37:35 +00006742
Tim Northoverf203ab52016-07-14 22:13:32 +00006743 if (Start > SectSize) {
6744 outs() << "section data ends, " << SymName
6745 << " lies outside valid range\n";
6746 return;
6747 }
6748
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006749 // Stop disassembling either at the beginning of the next symbol or at
6750 // the end of the section.
Kevin Enderbyedd58722012-05-15 18:57:14 +00006751 bool containsNextSym = false;
Owen Andersond9243c42011-10-17 21:37:35 +00006752 uint64_t NextSym = 0;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006753 uint64_t NextSymIdx = SymIdx + 1;
Owen Andersond9243c42011-10-17 21:37:35 +00006754 while (Symbols.size() > NextSymIdx) {
Kevin Enderby7bd8d992016-05-02 20:28:12 +00006755 Expected<SymbolRef::Type> STOrErr = Symbols[NextSymIdx].getType();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00006756 if (!STOrErr)
6757 report_error(MachOOF->getFileName(), STOrErr.takeError());
Kevin Enderby5afbc1c2016-03-23 20:27:00 +00006758 SymbolRef::Type NextSymType = *STOrErr;
Owen Andersond9243c42011-10-17 21:37:35 +00006759 if (NextSymType == SymbolRef::ST_Function) {
Rafael Espindola80291272014-10-08 15:28:58 +00006760 containsNextSym =
6761 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
Rafael Espindoladea00162015-07-03 17:44:18 +00006762 NextSym = Symbols[NextSymIdx].getValue();
Cameron Zwarich54478a52012-02-03 05:42:17 +00006763 NextSym -= SectionAddress;
Owen Andersond9243c42011-10-17 21:37:35 +00006764 break;
6765 }
6766 ++NextSymIdx;
6767 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006768
Tim Northoverf203ab52016-07-14 22:13:32 +00006769 uint64_t End = containsNextSym ? std::min(NextSym, SectSize) : SectSize;
Owen Andersond9243c42011-10-17 21:37:35 +00006770 uint64_t Size;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006771
6772 symbolTableWorked = true;
Rafael Espindolabd604f22014-11-07 00:52:15 +00006773
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006774 DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
Tim Northover09ca33e2016-04-22 23:23:31 +00006775 bool IsThumb = MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb;
6776
6777 // We only need the dedicated Thumb target if there's a real choice
6778 // (i.e. we're not targeting M-class) and the function is Thumb.
6779 bool UseThumbTarget = IsThumb && ThumbTarget;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006780
Kevin Enderbyc138da32017-02-06 18:43:18 +00006781 // If we are not specifying a symbol to start disassembly with and this
6782 // is the first symbol in the section but not at the start of the section
6783 // then move the disassembly index to the start of the section and
6784 // don't print the symbol name just yet. This is so the bytes before the
6785 // first symbol are disassembled.
6786 uint64_t SymbolStart = Start;
6787 if (DisSymName.empty() && FirstSymbol && Start != 0) {
6788 FirstSymbolAtSectionStart = false;
6789 Start = 0;
6790 }
6791 else
6792 outs() << SymName << ":\n";
6793
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006794 DILineInfo lastLine;
6795 for (uint64_t Index = Start; Index < End; Index += Size) {
6796 MCInst Inst;
Owen Andersond9243c42011-10-17 21:37:35 +00006797
Kevin Enderbyc138da32017-02-06 18:43:18 +00006798 // If this is the first symbol in the section and it was not at the
6799 // start of the section, see if we are at its Index now and if so print
6800 // the symbol name.
6801 if (FirstSymbol && !FirstSymbolAtSectionStart && Index == SymbolStart)
6802 outs() << SymName << ":\n";
6803
Kevin Enderbybf246f52014-09-24 23:08:22 +00006804 uint64_t PC = SectAddress + Index;
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00006805 if (!NoLeadingAddr) {
6806 if (FullLeadingAddr) {
6807 if (MachOOF->is64Bit())
6808 outs() << format("%016" PRIx64, PC);
6809 else
6810 outs() << format("%08" PRIx64, PC);
6811 } else {
6812 outs() << format("%8" PRIx64 ":", PC);
6813 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006814 }
Kevin Enderby4b627be2016-04-28 20:14:13 +00006815 if (!NoShowRawInsn || Arch == Triple::arm)
Kevin Enderbybf246f52014-09-24 23:08:22 +00006816 outs() << "\t";
Kevin Enderby273ae012013-06-06 17:20:50 +00006817
6818 // Check the data in code table here to see if this is data not an
6819 // instruction to be disassembled.
6820 DiceTable Dice;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006821 Dice.push_back(std::make_pair(PC, DiceRef()));
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006822 dice_table_iterator DTI =
6823 std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(),
6824 compareDiceTableEntries);
6825 if (DTI != Dices.end()) {
Kevin Enderby273ae012013-06-06 17:20:50 +00006826 uint16_t Length;
6827 DTI->second.getLength(Length);
Kevin Enderby273ae012013-06-06 17:20:50 +00006828 uint16_t Kind;
6829 DTI->second.getKind(Kind);
Colin LeMahieufc32b1b2015-03-18 19:27:31 +00006830 Size = DumpDataInCode(Bytes.data() + Index, Length, Kind);
Kevin Enderby930fdc72014-11-06 19:00:13 +00006831 if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) &&
6832 (PC == (DTI->first + Length - 1)) && (Length & 1))
6833 Size++;
Kevin Enderby273ae012013-06-06 17:20:50 +00006834 continue;
6835 }
6836
Kevin Enderbybf246f52014-09-24 23:08:22 +00006837 SmallVector<char, 64> AnnotationsBytes;
6838 raw_svector_ostream Annotations(AnnotationsBytes);
6839
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006840 bool gotInst;
Tim Northover09ca33e2016-04-22 23:23:31 +00006841 if (UseThumbTarget)
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006842 gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006843 PC, DebugOut, Annotations);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006844 else
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006845 gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC,
Kevin Enderbybf246f52014-09-24 23:08:22 +00006846 DebugOut, Annotations);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006847 if (gotInst) {
Kevin Enderby4b627be2016-04-28 20:14:13 +00006848 if (!NoShowRawInsn || Arch == Triple::arm) {
Craig Topper0013be12015-09-21 05:32:41 +00006849 dumpBytes(makeArrayRef(Bytes.data() + Index, Size), outs());
Kevin Enderbybf246f52014-09-24 23:08:22 +00006850 }
6851 formatted_raw_ostream FormattedOS(outs());
Kevin Enderbybf246f52014-09-24 23:08:22 +00006852 StringRef AnnotationsStr = Annotations.str();
Tim Northover09ca33e2016-04-22 23:23:31 +00006853 if (UseThumbTarget)
Akira Hatanakab46d0232015-03-27 20:36:02 +00006854 ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr, *ThumbSTI);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006855 else
Akira Hatanaka1d079942015-03-28 20:44:05 +00006856 IP->printInst(&Inst, FormattedOS, AnnotationsStr, *STI);
Kevin Enderbybf246f52014-09-24 23:08:22 +00006857 emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo);
Owen Andersond9243c42011-10-17 21:37:35 +00006858
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006859 // Print debug info.
6860 if (diContext) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006861 DILineInfo dli = diContext->getLineInfoForAddress(PC);
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006862 // Print valid line info if it changed.
Alexey Samsonovd0109992014-04-18 21:36:39 +00006863 if (dli != lastLine && dli.Line != 0)
6864 outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
6865 << dli.Column;
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006866 lastLine = dli;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006867 }
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006868 outs() << "\n";
6869 } else {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006870 unsigned int Arch = MachOOF->getArch();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006871 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006872 outs() << format("\t.byte 0x%02x #bad opcode\n",
6873 *(Bytes.data() + Index) & 0xff);
6874 Size = 1; // skip exactly one illegible byte and move on.
Tim Northover09ca33e2016-04-22 23:23:31 +00006875 } else if (Arch == Triple::aarch64 ||
6876 (Arch == Triple::arm && !IsThumb)) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006877 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
6878 (*(Bytes.data() + Index + 1) & 0xff) << 8 |
6879 (*(Bytes.data() + Index + 2) & 0xff) << 16 |
6880 (*(Bytes.data() + Index + 3) & 0xff) << 24;
6881 outs() << format("\t.long\t0x%08x\n", opcode);
6882 Size = 4;
Tim Northover09ca33e2016-04-22 23:23:31 +00006883 } else if (Arch == Triple::arm) {
6884 assert(IsThumb && "ARM mode should have been dealt with above");
6885 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
6886 (*(Bytes.data() + Index + 1) & 0xff) << 8;
6887 outs() << format("\t.short\t0x%04x\n", opcode);
6888 Size = 2;
6889 } else{
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006890 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6891 if (Size == 0)
6892 Size = 1; // skip illegible bytes
6893 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006894 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006895 }
Kevin Enderbyc138da32017-02-06 18:43:18 +00006896 // Now that we are done disassembled the first symbol set the bool that
6897 // were doing this to false.
6898 FirstSymbol = false;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006899 }
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006900 if (!symbolTableWorked) {
Rafael Espindola80291272014-10-08 15:28:58 +00006901 // Reading the symbol table didn't work, disassemble the whole section.
6902 uint64_t SectAddress = Sections[SectIdx].getAddress();
6903 uint64_t SectSize = Sections[SectIdx].getSize();
Kevin Enderbybadd1002012-05-18 00:13:56 +00006904 uint64_t InstSize;
6905 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
Bill Wendling4e68e062012-07-19 00:17:40 +00006906 MCInst Inst;
Kevin Enderbybadd1002012-05-18 00:13:56 +00006907
Kevin Enderbybf246f52014-09-24 23:08:22 +00006908 uint64_t PC = SectAddress + Index;
Kevin Enderby02d3a372017-01-31 18:09:10 +00006909 SmallVector<char, 64> AnnotationsBytes;
6910 raw_svector_ostream Annotations(AnnotationsBytes);
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006911 if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC,
Kevin Enderby02d3a372017-01-31 18:09:10 +00006912 DebugOut, Annotations)) {
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00006913 if (!NoLeadingAddr) {
6914 if (FullLeadingAddr) {
6915 if (MachOOF->is64Bit())
6916 outs() << format("%016" PRIx64, PC);
6917 else
6918 outs() << format("%08" PRIx64, PC);
6919 } else {
6920 outs() << format("%8" PRIx64 ":", PC);
6921 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006922 }
Kevin Enderby4b627be2016-04-28 20:14:13 +00006923 if (!NoShowRawInsn || Arch == Triple::arm) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00006924 outs() << "\t";
Craig Topper0013be12015-09-21 05:32:41 +00006925 dumpBytes(makeArrayRef(Bytes.data() + Index, InstSize), outs());
Kevin Enderbybf246f52014-09-24 23:08:22 +00006926 }
Kevin Enderby02d3a372017-01-31 18:09:10 +00006927 StringRef AnnotationsStr = Annotations.str();
6928 IP->printInst(&Inst, outs(), AnnotationsStr, *STI);
Bill Wendling4e68e062012-07-19 00:17:40 +00006929 outs() << "\n";
6930 } else {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006931 unsigned int Arch = MachOOF->getArch();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006932 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006933 outs() << format("\t.byte 0x%02x #bad opcode\n",
6934 *(Bytes.data() + Index) & 0xff);
6935 InstSize = 1; // skip exactly one illegible byte and move on.
6936 } else {
6937 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
6938 if (InstSize == 0)
6939 InstSize = 1; // skip illegible bytes
6940 }
Bill Wendling4e68e062012-07-19 00:17:40 +00006941 }
Kevin Enderbybadd1002012-05-18 00:13:56 +00006942 }
6943 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00006944 // The TripleName's need to be reset if we are called again for a different
6945 // archtecture.
6946 TripleName = "";
6947 ThumbTripleName = "";
6948
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006949 if (SymbolizerInfo.method != nullptr)
6950 free(SymbolizerInfo.method);
Kevin Enderby04bf6932014-10-28 23:39:46 +00006951 if (SymbolizerInfo.demangled_name != nullptr)
6952 free(SymbolizerInfo.demangled_name);
Kevin Enderby930fdc72014-11-06 19:00:13 +00006953 if (ThumbSymbolizerInfo.method != nullptr)
6954 free(ThumbSymbolizerInfo.method);
6955 if (ThumbSymbolizerInfo.demangled_name != nullptr)
6956 free(ThumbSymbolizerInfo.demangled_name);
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006957 }
6958}
Tim Northover4bd286a2014-08-01 13:07:19 +00006959
Tim Northover39c70bb2014-08-12 11:52:59 +00006960//===----------------------------------------------------------------------===//
6961// __compact_unwind section dumping
6962//===----------------------------------------------------------------------===//
6963
Tim Northover4bd286a2014-08-01 13:07:19 +00006964namespace {
Tim Northover39c70bb2014-08-12 11:52:59 +00006965
6966template <typename T> static uint64_t readNext(const char *&Buf) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006967 using llvm::support::little;
6968 using llvm::support::unaligned;
Tim Northover39c70bb2014-08-12 11:52:59 +00006969
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006970 uint64_t Val = support::endian::read<T, little, unaligned>(Buf);
6971 Buf += sizeof(T);
6972 return Val;
6973}
Tim Northover39c70bb2014-08-12 11:52:59 +00006974
Tim Northover4bd286a2014-08-01 13:07:19 +00006975struct CompactUnwindEntry {
6976 uint32_t OffsetInSection;
6977
6978 uint64_t FunctionAddr;
6979 uint32_t Length;
6980 uint32_t CompactEncoding;
6981 uint64_t PersonalityAddr;
6982 uint64_t LSDAAddr;
6983
6984 RelocationRef FunctionReloc;
6985 RelocationRef PersonalityReloc;
6986 RelocationRef LSDAReloc;
6987
6988 CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64)
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006989 : OffsetInSection(Offset) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006990 if (Is64)
6991 read<uint64_t>(Contents.data() + Offset);
6992 else
6993 read<uint32_t>(Contents.data() + Offset);
6994 }
6995
6996private:
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006997 template <typename UIntPtr> void read(const char *Buf) {
Tim Northover4bd286a2014-08-01 13:07:19 +00006998 FunctionAddr = readNext<UIntPtr>(Buf);
6999 Length = readNext<uint32_t>(Buf);
7000 CompactEncoding = readNext<uint32_t>(Buf);
7001 PersonalityAddr = readNext<UIntPtr>(Buf);
7002 LSDAAddr = readNext<UIntPtr>(Buf);
7003 }
7004};
7005}
7006
7007/// Given a relocation from __compact_unwind, consisting of the RelocationRef
7008/// and data being relocated, determine the best base Name and Addend to use for
7009/// display purposes.
7010///
7011/// 1. An Extern relocation will directly reference a symbol (and the data is
7012/// then already an addend), so use that.
7013/// 2. Otherwise the data is an offset in the object file's layout; try to find
7014// a symbol before it in the same section, and use the offset from there.
7015/// 3. Finally, if all that fails, fall back to an offset from the start of the
7016/// referenced section.
7017static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
7018 std::map<uint64_t, SymbolRef> &Symbols,
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007019 const RelocationRef &Reloc, uint64_t Addr,
Tim Northover4bd286a2014-08-01 13:07:19 +00007020 StringRef &Name, uint64_t &Addend) {
7021 if (Reloc.getSymbol() != Obj->symbol_end()) {
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00007022 Expected<StringRef> NameOrErr = Reloc.getSymbol()->getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00007023 if (!NameOrErr)
7024 report_error(Obj->getFileName(), NameOrErr.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00007025 Name = *NameOrErr;
Tim Northover4bd286a2014-08-01 13:07:19 +00007026 Addend = Addr;
7027 return;
7028 }
7029
7030 auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
Keno Fischerc780e8e2015-05-21 21:24:32 +00007031 SectionRef RelocSection = Obj->getAnyRelocationSection(RE);
Tim Northover4bd286a2014-08-01 13:07:19 +00007032
Rafael Espindola80291272014-10-08 15:28:58 +00007033 uint64_t SectionAddr = RelocSection.getAddress();
Tim Northover4bd286a2014-08-01 13:07:19 +00007034
7035 auto Sym = Symbols.upper_bound(Addr);
7036 if (Sym == Symbols.begin()) {
7037 // The first symbol in the object is after this reference, the best we can
7038 // do is section-relative notation.
7039 RelocSection.getName(Name);
7040 Addend = Addr - SectionAddr;
7041 return;
7042 }
7043
7044 // Go back one so that SymbolAddress <= Addr.
7045 --Sym;
7046
Kevin Enderby7bd8d992016-05-02 20:28:12 +00007047 auto SectOrErr = Sym->second.getSection();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00007048 if (!SectOrErr)
7049 report_error(Obj->getFileName(), SectOrErr.takeError());
Kevin Enderby7bd8d992016-05-02 20:28:12 +00007050 section_iterator SymSection = *SectOrErr;
Tim Northover4bd286a2014-08-01 13:07:19 +00007051 if (RelocSection == *SymSection) {
7052 // There's a valid symbol in the same section before this reference.
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00007053 Expected<StringRef> NameOrErr = Sym->second.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00007054 if (!NameOrErr)
7055 report_error(Obj->getFileName(), NameOrErr.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00007056 Name = *NameOrErr;
Tim Northover4bd286a2014-08-01 13:07:19 +00007057 Addend = Addr - Sym->first;
7058 return;
7059 }
7060
7061 // There is a symbol before this reference, but it's in a different
7062 // section. Probably not helpful to mention it, so use the section name.
7063 RelocSection.getName(Name);
7064 Addend = Addr - SectionAddr;
7065}
7066
7067static void printUnwindRelocDest(const MachOObjectFile *Obj,
7068 std::map<uint64_t, SymbolRef> &Symbols,
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007069 const RelocationRef &Reloc, uint64_t Addr) {
Tim Northover4bd286a2014-08-01 13:07:19 +00007070 StringRef Name;
7071 uint64_t Addend;
7072
Rafael Espindola854038e2015-06-26 14:51:16 +00007073 if (!Reloc.getObject())
Tim Northover0b0add52014-09-09 10:45:06 +00007074 return;
7075
Tim Northover4bd286a2014-08-01 13:07:19 +00007076 findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend);
7077
7078 outs() << Name;
7079 if (Addend)
Tim Northover63a25622014-08-11 09:14:06 +00007080 outs() << " + " << format("0x%" PRIx64, Addend);
Tim Northover4bd286a2014-08-01 13:07:19 +00007081}
7082
7083static void
7084printMachOCompactUnwindSection(const MachOObjectFile *Obj,
7085 std::map<uint64_t, SymbolRef> &Symbols,
7086 const SectionRef &CompactUnwind) {
7087
Tim Northoverbf55f7e2016-11-15 20:26:01 +00007088 if (!Obj->isLittleEndian()) {
7089 outs() << "Skipping big-endian __compact_unwind section\n";
7090 return;
7091 }
Tim Northover4bd286a2014-08-01 13:07:19 +00007092
7093 bool Is64 = Obj->is64Bit();
7094 uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
7095 uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
7096
7097 StringRef Contents;
7098 CompactUnwind.getContents(Contents);
7099
7100 SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
7101
7102 // First populate the initial raw offsets, encodings and so on from the entry.
7103 for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) {
7104 CompactUnwindEntry Entry(Contents.data(), Offset, Is64);
7105 CompactUnwinds.push_back(Entry);
7106 }
7107
7108 // Next we need to look at the relocations to find out what objects are
7109 // actually being referred to.
7110 for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00007111 uint64_t RelocAddress = Reloc.getOffset();
Tim Northover4bd286a2014-08-01 13:07:19 +00007112
7113 uint32_t EntryIdx = RelocAddress / EntrySize;
7114 uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
7115 CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx];
7116
7117 if (OffsetInEntry == 0)
7118 Entry.FunctionReloc = Reloc;
7119 else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t))
7120 Entry.PersonalityReloc = Reloc;
7121 else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t))
7122 Entry.LSDAReloc = Reloc;
Tim Northoverbf55f7e2016-11-15 20:26:01 +00007123 else {
7124 outs() << "Invalid relocation in __compact_unwind section\n";
7125 return;
7126 }
Tim Northover4bd286a2014-08-01 13:07:19 +00007127 }
7128
7129 // Finally, we're ready to print the data we've gathered.
7130 outs() << "Contents of __compact_unwind section:\n";
7131 for (auto &Entry : CompactUnwinds) {
Tim Northover06af2602014-08-08 12:08:51 +00007132 outs() << " Entry at offset "
7133 << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n";
Tim Northover4bd286a2014-08-01 13:07:19 +00007134
7135 // 1. Start of the region this entry applies to.
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007136 outs() << " start: " << format("0x%" PRIx64,
7137 Entry.FunctionAddr) << ' ';
7138 printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr);
Tim Northover4bd286a2014-08-01 13:07:19 +00007139 outs() << '\n';
7140
7141 // 2. Length of the region this entry applies to.
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007142 outs() << " length: " << format("0x%" PRIx32, Entry.Length)
7143 << '\n';
Tim Northover4bd286a2014-08-01 13:07:19 +00007144 // 3. The 32-bit compact encoding.
7145 outs() << " compact encoding: "
Tim Northoverb911bf82014-08-08 12:00:09 +00007146 << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n';
Tim Northover4bd286a2014-08-01 13:07:19 +00007147
7148 // 4. The personality function, if present.
Rafael Espindola854038e2015-06-26 14:51:16 +00007149 if (Entry.PersonalityReloc.getObject()) {
Tim Northover4bd286a2014-08-01 13:07:19 +00007150 outs() << " personality function: "
Tim Northoverb911bf82014-08-08 12:00:09 +00007151 << format("0x%" PRIx64, Entry.PersonalityAddr) << ' ';
Tim Northover4bd286a2014-08-01 13:07:19 +00007152 printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc,
7153 Entry.PersonalityAddr);
7154 outs() << '\n';
7155 }
7156
7157 // 5. This entry's language-specific data area.
Rafael Espindola854038e2015-06-26 14:51:16 +00007158 if (Entry.LSDAReloc.getObject()) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007159 outs() << " LSDA: " << format("0x%" PRIx64,
7160 Entry.LSDAAddr) << ' ';
Tim Northover4bd286a2014-08-01 13:07:19 +00007161 printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr);
7162 outs() << '\n';
7163 }
7164 }
7165}
7166
Tim Northover39c70bb2014-08-12 11:52:59 +00007167//===----------------------------------------------------------------------===//
7168// __unwind_info section dumping
7169//===----------------------------------------------------------------------===//
7170
7171static void printRegularSecondLevelUnwindPage(const char *PageStart) {
7172 const char *Pos = PageStart;
7173 uint32_t Kind = readNext<uint32_t>(Pos);
7174 (void)Kind;
7175 assert(Kind == 2 && "kind for a regular 2nd level index should be 2");
7176
7177 uint16_t EntriesStart = readNext<uint16_t>(Pos);
7178 uint16_t NumEntries = readNext<uint16_t>(Pos);
7179
7180 Pos = PageStart + EntriesStart;
7181 for (unsigned i = 0; i < NumEntries; ++i) {
7182 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
7183 uint32_t Encoding = readNext<uint32_t>(Pos);
7184
7185 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007186 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
7187 << ", "
7188 << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00007189 }
7190}
7191
7192static void printCompressedSecondLevelUnwindPage(
7193 const char *PageStart, uint32_t FunctionBase,
7194 const SmallVectorImpl<uint32_t> &CommonEncodings) {
7195 const char *Pos = PageStart;
7196 uint32_t Kind = readNext<uint32_t>(Pos);
7197 (void)Kind;
7198 assert(Kind == 3 && "kind for a compressed 2nd level index should be 3");
7199
7200 uint16_t EntriesStart = readNext<uint16_t>(Pos);
7201 uint16_t NumEntries = readNext<uint16_t>(Pos);
7202
7203 uint16_t EncodingsStart = readNext<uint16_t>(Pos);
7204 readNext<uint16_t>(Pos);
Aaron Ballman80930af2014-08-14 13:53:19 +00007205 const auto *PageEncodings = reinterpret_cast<const support::ulittle32_t *>(
7206 PageStart + EncodingsStart);
Tim Northover39c70bb2014-08-12 11:52:59 +00007207
7208 Pos = PageStart + EntriesStart;
7209 for (unsigned i = 0; i < NumEntries; ++i) {
7210 uint32_t Entry = readNext<uint32_t>(Pos);
7211 uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff);
7212 uint32_t EncodingIdx = Entry >> 24;
7213
7214 uint32_t Encoding;
7215 if (EncodingIdx < CommonEncodings.size())
7216 Encoding = CommonEncodings[EncodingIdx];
7217 else
7218 Encoding = PageEncodings[EncodingIdx - CommonEncodings.size()];
7219
7220 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007221 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
7222 << ", "
7223 << "encoding[" << EncodingIdx
7224 << "]=" << format("0x%08" PRIx32, Encoding) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00007225 }
7226}
7227
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007228static void printMachOUnwindInfoSection(const MachOObjectFile *Obj,
7229 std::map<uint64_t, SymbolRef> &Symbols,
7230 const SectionRef &UnwindInfo) {
Tim Northover39c70bb2014-08-12 11:52:59 +00007231
Tim Northoverbf55f7e2016-11-15 20:26:01 +00007232 if (!Obj->isLittleEndian()) {
7233 outs() << "Skipping big-endian __unwind_info section\n";
7234 return;
7235 }
Tim Northover39c70bb2014-08-12 11:52:59 +00007236
7237 outs() << "Contents of __unwind_info section:\n";
7238
7239 StringRef Contents;
7240 UnwindInfo.getContents(Contents);
7241 const char *Pos = Contents.data();
7242
7243 //===----------------------------------
7244 // Section header
7245 //===----------------------------------
7246
7247 uint32_t Version = readNext<uint32_t>(Pos);
7248 outs() << " Version: "
7249 << format("0x%" PRIx32, Version) << '\n';
Tim Northoverbf55f7e2016-11-15 20:26:01 +00007250 if (Version != 1) {
7251 outs() << " Skipping section with unknown version\n";
7252 return;
7253 }
Tim Northover39c70bb2014-08-12 11:52:59 +00007254
7255 uint32_t CommonEncodingsStart = readNext<uint32_t>(Pos);
7256 outs() << " Common encodings array section offset: "
7257 << format("0x%" PRIx32, CommonEncodingsStart) << '\n';
7258 uint32_t NumCommonEncodings = readNext<uint32_t>(Pos);
7259 outs() << " Number of common encodings in array: "
7260 << format("0x%" PRIx32, NumCommonEncodings) << '\n';
7261
7262 uint32_t PersonalitiesStart = readNext<uint32_t>(Pos);
7263 outs() << " Personality function array section offset: "
7264 << format("0x%" PRIx32, PersonalitiesStart) << '\n';
7265 uint32_t NumPersonalities = readNext<uint32_t>(Pos);
7266 outs() << " Number of personality functions in array: "
7267 << format("0x%" PRIx32, NumPersonalities) << '\n';
7268
7269 uint32_t IndicesStart = readNext<uint32_t>(Pos);
7270 outs() << " Index array section offset: "
7271 << format("0x%" PRIx32, IndicesStart) << '\n';
7272 uint32_t NumIndices = readNext<uint32_t>(Pos);
7273 outs() << " Number of indices in array: "
7274 << format("0x%" PRIx32, NumIndices) << '\n';
7275
7276 //===----------------------------------
7277 // A shared list of common encodings
7278 //===----------------------------------
7279
7280 // These occupy indices in the range [0, N] whenever an encoding is referenced
7281 // from a compressed 2nd level index table. In practice the linker only
7282 // creates ~128 of these, so that indices are available to embed encodings in
7283 // the 2nd level index.
7284
7285 SmallVector<uint32_t, 64> CommonEncodings;
7286 outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n";
7287 Pos = Contents.data() + CommonEncodingsStart;
7288 for (unsigned i = 0; i < NumCommonEncodings; ++i) {
7289 uint32_t Encoding = readNext<uint32_t>(Pos);
7290 CommonEncodings.push_back(Encoding);
7291
7292 outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding)
7293 << '\n';
7294 }
7295
Tim Northover39c70bb2014-08-12 11:52:59 +00007296 //===----------------------------------
7297 // Personality functions used in this executable
7298 //===----------------------------------
7299
7300 // There should be only a handful of these (one per source language,
7301 // roughly). Particularly since they only get 2 bits in the compact encoding.
7302
7303 outs() << " Personality functions: (count = " << NumPersonalities << ")\n";
7304 Pos = Contents.data() + PersonalitiesStart;
7305 for (unsigned i = 0; i < NumPersonalities; ++i) {
7306 uint32_t PersonalityFn = readNext<uint32_t>(Pos);
7307 outs() << " personality[" << i + 1
7308 << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n';
7309 }
7310
7311 //===----------------------------------
7312 // The level 1 index entries
7313 //===----------------------------------
7314
7315 // These specify an approximate place to start searching for the more detailed
7316 // information, sorted by PC.
7317
7318 struct IndexEntry {
7319 uint32_t FunctionOffset;
7320 uint32_t SecondLevelPageStart;
7321 uint32_t LSDAStart;
7322 };
7323
7324 SmallVector<IndexEntry, 4> IndexEntries;
7325
7326 outs() << " Top level indices: (count = " << NumIndices << ")\n";
7327 Pos = Contents.data() + IndicesStart;
7328 for (unsigned i = 0; i < NumIndices; ++i) {
7329 IndexEntry Entry;
7330
7331 Entry.FunctionOffset = readNext<uint32_t>(Pos);
7332 Entry.SecondLevelPageStart = readNext<uint32_t>(Pos);
7333 Entry.LSDAStart = readNext<uint32_t>(Pos);
7334 IndexEntries.push_back(Entry);
7335
7336 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007337 << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset)
7338 << ", "
Tim Northover39c70bb2014-08-12 11:52:59 +00007339 << "2nd level page offset="
7340 << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007341 << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00007342 }
7343
Tim Northover39c70bb2014-08-12 11:52:59 +00007344 //===----------------------------------
7345 // Next come the LSDA tables
7346 //===----------------------------------
7347
7348 // The LSDA layout is rather implicit: it's a contiguous array of entries from
7349 // the first top-level index's LSDAOffset to the last (sentinel).
7350
7351 outs() << " LSDA descriptors:\n";
7352 Pos = Contents.data() + IndexEntries[0].LSDAStart;
7353 int NumLSDAs = (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) /
7354 (2 * sizeof(uint32_t));
7355 for (int i = 0; i < NumLSDAs; ++i) {
7356 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
7357 uint32_t LSDAOffset = readNext<uint32_t>(Pos);
7358 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007359 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
7360 << ", "
7361 << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00007362 }
7363
7364 //===----------------------------------
7365 // Finally, the 2nd level indices
7366 //===----------------------------------
7367
7368 // Generally these are 4K in size, and have 2 possible forms:
7369 // + Regular stores up to 511 entries with disparate encodings
7370 // + Compressed stores up to 1021 entries if few enough compact encoding
7371 // values are used.
7372 outs() << " Second level indices:\n";
7373 for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) {
7374 // The final sentinel top-level index has no associated 2nd level page
7375 if (IndexEntries[i].SecondLevelPageStart == 0)
7376 break;
7377
7378 outs() << " Second level index[" << i << "]: "
7379 << "offset in section="
7380 << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart)
7381 << ", "
7382 << "base function offset="
7383 << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n';
7384
7385 Pos = Contents.data() + IndexEntries[i].SecondLevelPageStart;
Aaron Ballman80930af2014-08-14 13:53:19 +00007386 uint32_t Kind = *reinterpret_cast<const support::ulittle32_t *>(Pos);
Tim Northover39c70bb2014-08-12 11:52:59 +00007387 if (Kind == 2)
7388 printRegularSecondLevelUnwindPage(Pos);
7389 else if (Kind == 3)
7390 printCompressedSecondLevelUnwindPage(Pos, IndexEntries[i].FunctionOffset,
7391 CommonEncodings);
7392 else
Tim Northoverbf55f7e2016-11-15 20:26:01 +00007393 outs() << " Skipping 2nd level page with unknown kind " << Kind
7394 << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00007395 }
7396}
7397
Tim Northover4bd286a2014-08-01 13:07:19 +00007398void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) {
7399 std::map<uint64_t, SymbolRef> Symbols;
7400 for (const SymbolRef &SymRef : Obj->symbols()) {
7401 // Discard any undefined or absolute symbols. They're not going to take part
7402 // in the convenience lookup for unwind info and just take up resources.
Kevin Enderby7bd8d992016-05-02 20:28:12 +00007403 auto SectOrErr = SymRef.getSection();
7404 if (!SectOrErr) {
7405 // TODO: Actually report errors helpfully.
7406 consumeError(SectOrErr.takeError());
7407 continue;
7408 }
7409 section_iterator Section = *SectOrErr;
Tim Northover4bd286a2014-08-01 13:07:19 +00007410 if (Section == Obj->section_end())
7411 continue;
7412
Rafael Espindoladea00162015-07-03 17:44:18 +00007413 uint64_t Addr = SymRef.getValue();
Tim Northover4bd286a2014-08-01 13:07:19 +00007414 Symbols.insert(std::make_pair(Addr, SymRef));
7415 }
7416
7417 for (const SectionRef &Section : Obj->sections()) {
7418 StringRef SectName;
7419 Section.getName(SectName);
7420 if (SectName == "__compact_unwind")
7421 printMachOCompactUnwindSection(Obj, Symbols, Section);
7422 else if (SectName == "__unwind_info")
Tim Northover39c70bb2014-08-12 11:52:59 +00007423 printMachOUnwindInfoSection(Obj, Symbols, Section);
Tim Northover4bd286a2014-08-01 13:07:19 +00007424 }
7425}
Kevin Enderbyb76d3862014-08-22 20:35:18 +00007426
7427static void PrintMachHeader(uint32_t magic, uint32_t cputype,
7428 uint32_t cpusubtype, uint32_t filetype,
7429 uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
7430 bool verbose) {
7431 outs() << "Mach header\n";
7432 outs() << " magic cputype cpusubtype caps filetype ncmds "
7433 "sizeofcmds flags\n";
7434 if (verbose) {
7435 if (magic == MachO::MH_MAGIC)
7436 outs() << " MH_MAGIC";
7437 else if (magic == MachO::MH_MAGIC_64)
7438 outs() << "MH_MAGIC_64";
7439 else
7440 outs() << format(" 0x%08" PRIx32, magic);
7441 switch (cputype) {
7442 case MachO::CPU_TYPE_I386:
7443 outs() << " I386";
7444 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7445 case MachO::CPU_SUBTYPE_I386_ALL:
7446 outs() << " ALL";
7447 break;
7448 default:
7449 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7450 break;
7451 }
7452 break;
7453 case MachO::CPU_TYPE_X86_64:
7454 outs() << " X86_64";
Kevin Enderby131d1772015-01-09 19:22:37 +00007455 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7456 case MachO::CPU_SUBTYPE_X86_64_ALL:
7457 outs() << " ALL";
7458 break;
7459 case MachO::CPU_SUBTYPE_X86_64_H:
7460 outs() << " Haswell";
7461 break;
7462 default:
7463 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7464 break;
7465 }
Kevin Enderbyb76d3862014-08-22 20:35:18 +00007466 break;
7467 case MachO::CPU_TYPE_ARM:
7468 outs() << " ARM";
7469 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7470 case MachO::CPU_SUBTYPE_ARM_ALL:
7471 outs() << " ALL";
7472 break;
7473 case MachO::CPU_SUBTYPE_ARM_V4T:
7474 outs() << " V4T";
7475 break;
7476 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
7477 outs() << " V5TEJ";
7478 break;
7479 case MachO::CPU_SUBTYPE_ARM_XSCALE:
7480 outs() << " XSCALE";
7481 break;
7482 case MachO::CPU_SUBTYPE_ARM_V6:
7483 outs() << " V6";
7484 break;
7485 case MachO::CPU_SUBTYPE_ARM_V6M:
7486 outs() << " V6M";
7487 break;
7488 case MachO::CPU_SUBTYPE_ARM_V7:
7489 outs() << " V7";
7490 break;
7491 case MachO::CPU_SUBTYPE_ARM_V7EM:
7492 outs() << " V7EM";
7493 break;
7494 case MachO::CPU_SUBTYPE_ARM_V7K:
7495 outs() << " V7K";
7496 break;
7497 case MachO::CPU_SUBTYPE_ARM_V7M:
7498 outs() << " V7M";
7499 break;
7500 case MachO::CPU_SUBTYPE_ARM_V7S:
7501 outs() << " V7S";
7502 break;
7503 default:
7504 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7505 break;
7506 }
7507 break;
7508 case MachO::CPU_TYPE_ARM64:
7509 outs() << " ARM64";
7510 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7511 case MachO::CPU_SUBTYPE_ARM64_ALL:
7512 outs() << " ALL";
7513 break;
7514 default:
7515 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7516 break;
7517 }
7518 break;
7519 case MachO::CPU_TYPE_POWERPC:
7520 outs() << " PPC";
7521 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7522 case MachO::CPU_SUBTYPE_POWERPC_ALL:
7523 outs() << " ALL";
7524 break;
7525 default:
7526 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7527 break;
7528 }
7529 break;
7530 case MachO::CPU_TYPE_POWERPC64:
7531 outs() << " PPC64";
7532 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7533 case MachO::CPU_SUBTYPE_POWERPC_ALL:
7534 outs() << " ALL";
7535 break;
7536 default:
7537 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7538 break;
7539 }
7540 break;
Kevin Enderby40fdbf82016-01-26 18:20:49 +00007541 default:
7542 outs() << format(" %7d", cputype);
7543 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7544 break;
Kevin Enderbyb76d3862014-08-22 20:35:18 +00007545 }
7546 if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007547 outs() << " LIB64";
Kevin Enderbyb76d3862014-08-22 20:35:18 +00007548 } else {
7549 outs() << format(" 0x%02" PRIx32,
7550 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
7551 }
7552 switch (filetype) {
7553 case MachO::MH_OBJECT:
7554 outs() << " OBJECT";
7555 break;
7556 case MachO::MH_EXECUTE:
7557 outs() << " EXECUTE";
7558 break;
7559 case MachO::MH_FVMLIB:
7560 outs() << " FVMLIB";
7561 break;
7562 case MachO::MH_CORE:
7563 outs() << " CORE";
7564 break;
7565 case MachO::MH_PRELOAD:
7566 outs() << " PRELOAD";
7567 break;
7568 case MachO::MH_DYLIB:
7569 outs() << " DYLIB";
7570 break;
7571 case MachO::MH_DYLIB_STUB:
7572 outs() << " DYLIB_STUB";
7573 break;
7574 case MachO::MH_DYLINKER:
7575 outs() << " DYLINKER";
7576 break;
7577 case MachO::MH_BUNDLE:
7578 outs() << " BUNDLE";
7579 break;
7580 case MachO::MH_DSYM:
7581 outs() << " DSYM";
7582 break;
7583 case MachO::MH_KEXT_BUNDLE:
7584 outs() << " KEXTBUNDLE";
7585 break;
7586 default:
7587 outs() << format(" %10u", filetype);
7588 break;
7589 }
7590 outs() << format(" %5u", ncmds);
7591 outs() << format(" %10u", sizeofcmds);
7592 uint32_t f = flags;
7593 if (f & MachO::MH_NOUNDEFS) {
7594 outs() << " NOUNDEFS";
7595 f &= ~MachO::MH_NOUNDEFS;
7596 }
7597 if (f & MachO::MH_INCRLINK) {
7598 outs() << " INCRLINK";
7599 f &= ~MachO::MH_INCRLINK;
7600 }
7601 if (f & MachO::MH_DYLDLINK) {
7602 outs() << " DYLDLINK";
7603 f &= ~MachO::MH_DYLDLINK;
7604 }
7605 if (f & MachO::MH_BINDATLOAD) {
7606 outs() << " BINDATLOAD";
7607 f &= ~MachO::MH_BINDATLOAD;
7608 }
7609 if (f & MachO::MH_PREBOUND) {
7610 outs() << " PREBOUND";
7611 f &= ~MachO::MH_PREBOUND;
7612 }
7613 if (f & MachO::MH_SPLIT_SEGS) {
7614 outs() << " SPLIT_SEGS";
7615 f &= ~MachO::MH_SPLIT_SEGS;
7616 }
7617 if (f & MachO::MH_LAZY_INIT) {
7618 outs() << " LAZY_INIT";
7619 f &= ~MachO::MH_LAZY_INIT;
7620 }
7621 if (f & MachO::MH_TWOLEVEL) {
7622 outs() << " TWOLEVEL";
7623 f &= ~MachO::MH_TWOLEVEL;
7624 }
7625 if (f & MachO::MH_FORCE_FLAT) {
7626 outs() << " FORCE_FLAT";
7627 f &= ~MachO::MH_FORCE_FLAT;
7628 }
7629 if (f & MachO::MH_NOMULTIDEFS) {
7630 outs() << " NOMULTIDEFS";
7631 f &= ~MachO::MH_NOMULTIDEFS;
7632 }
7633 if (f & MachO::MH_NOFIXPREBINDING) {
7634 outs() << " NOFIXPREBINDING";
7635 f &= ~MachO::MH_NOFIXPREBINDING;
7636 }
7637 if (f & MachO::MH_PREBINDABLE) {
7638 outs() << " PREBINDABLE";
7639 f &= ~MachO::MH_PREBINDABLE;
7640 }
7641 if (f & MachO::MH_ALLMODSBOUND) {
7642 outs() << " ALLMODSBOUND";
7643 f &= ~MachO::MH_ALLMODSBOUND;
7644 }
7645 if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
7646 outs() << " SUBSECTIONS_VIA_SYMBOLS";
7647 f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
7648 }
7649 if (f & MachO::MH_CANONICAL) {
7650 outs() << " CANONICAL";
7651 f &= ~MachO::MH_CANONICAL;
7652 }
7653 if (f & MachO::MH_WEAK_DEFINES) {
7654 outs() << " WEAK_DEFINES";
7655 f &= ~MachO::MH_WEAK_DEFINES;
7656 }
7657 if (f & MachO::MH_BINDS_TO_WEAK) {
7658 outs() << " BINDS_TO_WEAK";
7659 f &= ~MachO::MH_BINDS_TO_WEAK;
7660 }
7661 if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
7662 outs() << " ALLOW_STACK_EXECUTION";
7663 f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
7664 }
7665 if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
7666 outs() << " DEAD_STRIPPABLE_DYLIB";
7667 f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
7668 }
7669 if (f & MachO::MH_PIE) {
7670 outs() << " PIE";
7671 f &= ~MachO::MH_PIE;
7672 }
7673 if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
7674 outs() << " NO_REEXPORTED_DYLIBS";
7675 f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
7676 }
7677 if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
7678 outs() << " MH_HAS_TLV_DESCRIPTORS";
7679 f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
7680 }
7681 if (f & MachO::MH_NO_HEAP_EXECUTION) {
7682 outs() << " MH_NO_HEAP_EXECUTION";
7683 f &= ~MachO::MH_NO_HEAP_EXECUTION;
7684 }
7685 if (f & MachO::MH_APP_EXTENSION_SAFE) {
7686 outs() << " APP_EXTENSION_SAFE";
7687 f &= ~MachO::MH_APP_EXTENSION_SAFE;
7688 }
Kevin Enderbydf0d6da2017-06-19 19:38:22 +00007689 if (f & MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO) {
7690 outs() << " NLIST_OUTOFSYNC_WITH_DYLDINFO";
7691 f &= ~MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO;
7692 }
Kevin Enderbyb76d3862014-08-22 20:35:18 +00007693 if (f != 0 || flags == 0)
7694 outs() << format(" 0x%08" PRIx32, f);
7695 } else {
7696 outs() << format(" 0x%08" PRIx32, magic);
7697 outs() << format(" %7d", cputype);
7698 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7699 outs() << format(" 0x%02" PRIx32,
7700 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
7701 outs() << format(" %10u", filetype);
7702 outs() << format(" %5u", ncmds);
7703 outs() << format(" %10u", sizeofcmds);
7704 outs() << format(" 0x%08" PRIx32, flags);
7705 }
7706 outs() << "\n";
7707}
7708
Kevin Enderby956366c2014-08-29 22:30:52 +00007709static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
7710 StringRef SegName, uint64_t vmaddr,
7711 uint64_t vmsize, uint64_t fileoff,
7712 uint64_t filesize, uint32_t maxprot,
7713 uint32_t initprot, uint32_t nsects,
7714 uint32_t flags, uint32_t object_size,
7715 bool verbose) {
7716 uint64_t expected_cmdsize;
7717 if (cmd == MachO::LC_SEGMENT) {
7718 outs() << " cmd LC_SEGMENT\n";
7719 expected_cmdsize = nsects;
7720 expected_cmdsize *= sizeof(struct MachO::section);
7721 expected_cmdsize += sizeof(struct MachO::segment_command);
7722 } else {
7723 outs() << " cmd LC_SEGMENT_64\n";
7724 expected_cmdsize = nsects;
7725 expected_cmdsize *= sizeof(struct MachO::section_64);
7726 expected_cmdsize += sizeof(struct MachO::segment_command_64);
7727 }
7728 outs() << " cmdsize " << cmdsize;
7729 if (cmdsize != expected_cmdsize)
7730 outs() << " Inconsistent size\n";
7731 else
7732 outs() << "\n";
7733 outs() << " segname " << SegName << "\n";
7734 if (cmd == MachO::LC_SEGMENT_64) {
7735 outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n";
7736 outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n";
7737 } else {
Kevin Enderbyadb7c432014-12-16 18:58:11 +00007738 outs() << " vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n";
7739 outs() << " vmsize " << format("0x%08" PRIx64, vmsize) << "\n";
Kevin Enderby956366c2014-08-29 22:30:52 +00007740 }
7741 outs() << " fileoff " << fileoff;
7742 if (fileoff > object_size)
7743 outs() << " (past end of file)\n";
7744 else
7745 outs() << "\n";
7746 outs() << " filesize " << filesize;
7747 if (fileoff + filesize > object_size)
7748 outs() << " (past end of file)\n";
7749 else
7750 outs() << "\n";
7751 if (verbose) {
7752 if ((maxprot &
7753 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7754 MachO::VM_PROT_EXECUTE)) != 0)
7755 outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n";
7756 else {
Davide Italiano37ff06a2015-09-02 16:53:25 +00007757 outs() << " maxprot ";
7758 outs() << ((maxprot & MachO::VM_PROT_READ) ? "r" : "-");
7759 outs() << ((maxprot & MachO::VM_PROT_WRITE) ? "w" : "-");
7760 outs() << ((maxprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
Kevin Enderby956366c2014-08-29 22:30:52 +00007761 }
7762 if ((initprot &
7763 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
7764 MachO::VM_PROT_EXECUTE)) != 0)
Kevin Enderby41c9c002016-10-21 18:22:35 +00007765 outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n";
Kevin Enderby956366c2014-08-29 22:30:52 +00007766 else {
Kevin Enderby41c9c002016-10-21 18:22:35 +00007767 outs() << " initprot ";
Davide Italiano37ff06a2015-09-02 16:53:25 +00007768 outs() << ((initprot & MachO::VM_PROT_READ) ? "r" : "-");
7769 outs() << ((initprot & MachO::VM_PROT_WRITE) ? "w" : "-");
7770 outs() << ((initprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
Kevin Enderby956366c2014-08-29 22:30:52 +00007771 }
7772 } else {
7773 outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n";
7774 outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n";
7775 }
7776 outs() << " nsects " << nsects << "\n";
7777 if (verbose) {
7778 outs() << " flags";
7779 if (flags == 0)
7780 outs() << " (none)\n";
7781 else {
7782 if (flags & MachO::SG_HIGHVM) {
7783 outs() << " HIGHVM";
7784 flags &= ~MachO::SG_HIGHVM;
7785 }
7786 if (flags & MachO::SG_FVMLIB) {
7787 outs() << " FVMLIB";
7788 flags &= ~MachO::SG_FVMLIB;
7789 }
7790 if (flags & MachO::SG_NORELOC) {
7791 outs() << " NORELOC";
7792 flags &= ~MachO::SG_NORELOC;
7793 }
7794 if (flags & MachO::SG_PROTECTED_VERSION_1) {
7795 outs() << " PROTECTED_VERSION_1";
7796 flags &= ~MachO::SG_PROTECTED_VERSION_1;
7797 }
7798 if (flags)
7799 outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n";
7800 else
7801 outs() << "\n";
7802 }
7803 } else {
7804 outs() << " flags " << format("0x%" PRIx32, flags) << "\n";
7805 }
7806}
7807
7808static void PrintSection(const char *sectname, const char *segname,
7809 uint64_t addr, uint64_t size, uint32_t offset,
7810 uint32_t align, uint32_t reloff, uint32_t nreloc,
7811 uint32_t flags, uint32_t reserved1, uint32_t reserved2,
7812 uint32_t cmd, const char *sg_segname,
7813 uint32_t filetype, uint32_t object_size,
7814 bool verbose) {
7815 outs() << "Section\n";
7816 outs() << " sectname " << format("%.16s\n", sectname);
7817 outs() << " segname " << format("%.16s", segname);
7818 if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0)
7819 outs() << " (does not match segment)\n";
7820 else
7821 outs() << "\n";
7822 if (cmd == MachO::LC_SEGMENT_64) {
7823 outs() << " addr " << format("0x%016" PRIx64, addr) << "\n";
7824 outs() << " size " << format("0x%016" PRIx64, size);
7825 } else {
Kevin Enderby75594b62014-12-16 21:00:25 +00007826 outs() << " addr " << format("0x%08" PRIx64, addr) << "\n";
7827 outs() << " size " << format("0x%08" PRIx64, size);
Kevin Enderby956366c2014-08-29 22:30:52 +00007828 }
7829 if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size)
7830 outs() << " (past end of file)\n";
7831 else
7832 outs() << "\n";
7833 outs() << " offset " << offset;
7834 if (offset > object_size)
7835 outs() << " (past end of file)\n";
7836 else
7837 outs() << "\n";
7838 uint32_t align_shifted = 1 << align;
7839 outs() << " align 2^" << align << " (" << align_shifted << ")\n";
7840 outs() << " reloff " << reloff;
7841 if (reloff > object_size)
7842 outs() << " (past end of file)\n";
7843 else
7844 outs() << "\n";
7845 outs() << " nreloc " << nreloc;
7846 if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size)
7847 outs() << " (past end of file)\n";
7848 else
7849 outs() << "\n";
7850 uint32_t section_type = flags & MachO::SECTION_TYPE;
7851 if (verbose) {
7852 outs() << " type";
7853 if (section_type == MachO::S_REGULAR)
7854 outs() << " S_REGULAR\n";
7855 else if (section_type == MachO::S_ZEROFILL)
7856 outs() << " S_ZEROFILL\n";
7857 else if (section_type == MachO::S_CSTRING_LITERALS)
7858 outs() << " S_CSTRING_LITERALS\n";
7859 else if (section_type == MachO::S_4BYTE_LITERALS)
7860 outs() << " S_4BYTE_LITERALS\n";
7861 else if (section_type == MachO::S_8BYTE_LITERALS)
7862 outs() << " S_8BYTE_LITERALS\n";
7863 else if (section_type == MachO::S_16BYTE_LITERALS)
7864 outs() << " S_16BYTE_LITERALS\n";
7865 else if (section_type == MachO::S_LITERAL_POINTERS)
7866 outs() << " S_LITERAL_POINTERS\n";
7867 else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS)
7868 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
7869 else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS)
7870 outs() << " S_LAZY_SYMBOL_POINTERS\n";
7871 else if (section_type == MachO::S_SYMBOL_STUBS)
7872 outs() << " S_SYMBOL_STUBS\n";
7873 else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS)
7874 outs() << " S_MOD_INIT_FUNC_POINTERS\n";
7875 else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS)
7876 outs() << " S_MOD_TERM_FUNC_POINTERS\n";
7877 else if (section_type == MachO::S_COALESCED)
7878 outs() << " S_COALESCED\n";
7879 else if (section_type == MachO::S_INTERPOSING)
7880 outs() << " S_INTERPOSING\n";
7881 else if (section_type == MachO::S_DTRACE_DOF)
7882 outs() << " S_DTRACE_DOF\n";
7883 else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS)
7884 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
7885 else if (section_type == MachO::S_THREAD_LOCAL_REGULAR)
7886 outs() << " S_THREAD_LOCAL_REGULAR\n";
7887 else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL)
7888 outs() << " S_THREAD_LOCAL_ZEROFILL\n";
7889 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES)
7890 outs() << " S_THREAD_LOCAL_VARIABLES\n";
7891 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7892 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
7893 else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS)
7894 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
7895 else
7896 outs() << format("0x%08" PRIx32, section_type) << "\n";
7897 outs() << "attributes";
7898 uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES;
7899 if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS)
7900 outs() << " PURE_INSTRUCTIONS";
7901 if (section_attributes & MachO::S_ATTR_NO_TOC)
7902 outs() << " NO_TOC";
7903 if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS)
7904 outs() << " STRIP_STATIC_SYMS";
7905 if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP)
7906 outs() << " NO_DEAD_STRIP";
7907 if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT)
7908 outs() << " LIVE_SUPPORT";
7909 if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE)
7910 outs() << " SELF_MODIFYING_CODE";
7911 if (section_attributes & MachO::S_ATTR_DEBUG)
7912 outs() << " DEBUG";
7913 if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS)
7914 outs() << " SOME_INSTRUCTIONS";
7915 if (section_attributes & MachO::S_ATTR_EXT_RELOC)
7916 outs() << " EXT_RELOC";
7917 if (section_attributes & MachO::S_ATTR_LOC_RELOC)
7918 outs() << " LOC_RELOC";
7919 if (section_attributes == 0)
7920 outs() << " (none)";
7921 outs() << "\n";
7922 } else
7923 outs() << " flags " << format("0x%08" PRIx32, flags) << "\n";
7924 outs() << " reserved1 " << reserved1;
7925 if (section_type == MachO::S_SYMBOL_STUBS ||
7926 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
7927 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
7928 section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
7929 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
7930 outs() << " (index into indirect symbol table)\n";
7931 else
7932 outs() << "\n";
7933 outs() << " reserved2 " << reserved2;
7934 if (section_type == MachO::S_SYMBOL_STUBS)
7935 outs() << " (size of stubs)\n";
7936 else
7937 outs() << "\n";
7938}
7939
David Majnemer73cc6ff2014-11-13 19:48:56 +00007940static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit,
Kevin Enderby956366c2014-08-29 22:30:52 +00007941 uint32_t object_size) {
7942 outs() << " cmd LC_SYMTAB\n";
7943 outs() << " cmdsize " << st.cmdsize;
7944 if (st.cmdsize != sizeof(struct MachO::symtab_command))
7945 outs() << " Incorrect size\n";
7946 else
7947 outs() << "\n";
7948 outs() << " symoff " << st.symoff;
7949 if (st.symoff > object_size)
7950 outs() << " (past end of file)\n";
7951 else
7952 outs() << "\n";
7953 outs() << " nsyms " << st.nsyms;
7954 uint64_t big_size;
David Majnemer73cc6ff2014-11-13 19:48:56 +00007955 if (Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00007956 big_size = st.nsyms;
7957 big_size *= sizeof(struct MachO::nlist_64);
7958 big_size += st.symoff;
7959 if (big_size > object_size)
7960 outs() << " (past end of file)\n";
7961 else
7962 outs() << "\n";
7963 } else {
7964 big_size = st.nsyms;
7965 big_size *= sizeof(struct MachO::nlist);
7966 big_size += st.symoff;
7967 if (big_size > object_size)
7968 outs() << " (past end of file)\n";
7969 else
7970 outs() << "\n";
7971 }
7972 outs() << " stroff " << st.stroff;
7973 if (st.stroff > object_size)
7974 outs() << " (past end of file)\n";
7975 else
7976 outs() << "\n";
7977 outs() << " strsize " << st.strsize;
7978 big_size = st.stroff;
7979 big_size += st.strsize;
7980 if (big_size > object_size)
7981 outs() << " (past end of file)\n";
7982 else
7983 outs() << "\n";
7984}
7985
7986static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
7987 uint32_t nsyms, uint32_t object_size,
David Majnemer73cc6ff2014-11-13 19:48:56 +00007988 bool Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00007989 outs() << " cmd LC_DYSYMTAB\n";
7990 outs() << " cmdsize " << dyst.cmdsize;
7991 if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
7992 outs() << " Incorrect size\n";
7993 else
7994 outs() << "\n";
7995 outs() << " ilocalsym " << dyst.ilocalsym;
7996 if (dyst.ilocalsym > nsyms)
7997 outs() << " (greater than the number of symbols)\n";
7998 else
7999 outs() << "\n";
8000 outs() << " nlocalsym " << dyst.nlocalsym;
8001 uint64_t big_size;
8002 big_size = dyst.ilocalsym;
8003 big_size += dyst.nlocalsym;
8004 if (big_size > nsyms)
8005 outs() << " (past the end of the symbol table)\n";
8006 else
8007 outs() << "\n";
8008 outs() << " iextdefsym " << dyst.iextdefsym;
8009 if (dyst.iextdefsym > nsyms)
8010 outs() << " (greater than the number of symbols)\n";
8011 else
8012 outs() << "\n";
8013 outs() << " nextdefsym " << dyst.nextdefsym;
8014 big_size = dyst.iextdefsym;
8015 big_size += dyst.nextdefsym;
8016 if (big_size > nsyms)
8017 outs() << " (past the end of the symbol table)\n";
8018 else
8019 outs() << "\n";
8020 outs() << " iundefsym " << dyst.iundefsym;
8021 if (dyst.iundefsym > nsyms)
8022 outs() << " (greater than the number of symbols)\n";
8023 else
8024 outs() << "\n";
8025 outs() << " nundefsym " << dyst.nundefsym;
8026 big_size = dyst.iundefsym;
8027 big_size += dyst.nundefsym;
8028 if (big_size > nsyms)
8029 outs() << " (past the end of the symbol table)\n";
8030 else
8031 outs() << "\n";
8032 outs() << " tocoff " << dyst.tocoff;
8033 if (dyst.tocoff > object_size)
8034 outs() << " (past end of file)\n";
8035 else
8036 outs() << "\n";
8037 outs() << " ntoc " << dyst.ntoc;
8038 big_size = dyst.ntoc;
8039 big_size *= sizeof(struct MachO::dylib_table_of_contents);
8040 big_size += dyst.tocoff;
8041 if (big_size > object_size)
8042 outs() << " (past end of file)\n";
8043 else
8044 outs() << "\n";
8045 outs() << " modtaboff " << dyst.modtaboff;
8046 if (dyst.modtaboff > object_size)
8047 outs() << " (past end of file)\n";
8048 else
8049 outs() << "\n";
8050 outs() << " nmodtab " << dyst.nmodtab;
8051 uint64_t modtabend;
David Majnemer73cc6ff2014-11-13 19:48:56 +00008052 if (Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00008053 modtabend = dyst.nmodtab;
8054 modtabend *= sizeof(struct MachO::dylib_module_64);
8055 modtabend += dyst.modtaboff;
8056 } else {
8057 modtabend = dyst.nmodtab;
8058 modtabend *= sizeof(struct MachO::dylib_module);
8059 modtabend += dyst.modtaboff;
8060 }
8061 if (modtabend > object_size)
8062 outs() << " (past end of file)\n";
8063 else
8064 outs() << "\n";
8065 outs() << " extrefsymoff " << dyst.extrefsymoff;
8066 if (dyst.extrefsymoff > object_size)
8067 outs() << " (past end of file)\n";
8068 else
8069 outs() << "\n";
8070 outs() << " nextrefsyms " << dyst.nextrefsyms;
8071 big_size = dyst.nextrefsyms;
8072 big_size *= sizeof(struct MachO::dylib_reference);
8073 big_size += dyst.extrefsymoff;
8074 if (big_size > object_size)
8075 outs() << " (past end of file)\n";
8076 else
8077 outs() << "\n";
8078 outs() << " indirectsymoff " << dyst.indirectsymoff;
8079 if (dyst.indirectsymoff > object_size)
8080 outs() << " (past end of file)\n";
8081 else
8082 outs() << "\n";
8083 outs() << " nindirectsyms " << dyst.nindirectsyms;
8084 big_size = dyst.nindirectsyms;
8085 big_size *= sizeof(uint32_t);
8086 big_size += dyst.indirectsymoff;
8087 if (big_size > object_size)
8088 outs() << " (past end of file)\n";
8089 else
8090 outs() << "\n";
8091 outs() << " extreloff " << dyst.extreloff;
8092 if (dyst.extreloff > object_size)
8093 outs() << " (past end of file)\n";
8094 else
8095 outs() << "\n";
8096 outs() << " nextrel " << dyst.nextrel;
8097 big_size = dyst.nextrel;
8098 big_size *= sizeof(struct MachO::relocation_info);
8099 big_size += dyst.extreloff;
8100 if (big_size > object_size)
8101 outs() << " (past end of file)\n";
8102 else
8103 outs() << "\n";
8104 outs() << " locreloff " << dyst.locreloff;
8105 if (dyst.locreloff > object_size)
8106 outs() << " (past end of file)\n";
8107 else
8108 outs() << "\n";
8109 outs() << " nlocrel " << dyst.nlocrel;
8110 big_size = dyst.nlocrel;
8111 big_size *= sizeof(struct MachO::relocation_info);
8112 big_size += dyst.locreloff;
8113 if (big_size > object_size)
8114 outs() << " (past end of file)\n";
8115 else
8116 outs() << "\n";
8117}
8118
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008119static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc,
8120 uint32_t object_size) {
8121 if (dc.cmd == MachO::LC_DYLD_INFO)
8122 outs() << " cmd LC_DYLD_INFO\n";
8123 else
8124 outs() << " cmd LC_DYLD_INFO_ONLY\n";
8125 outs() << " cmdsize " << dc.cmdsize;
8126 if (dc.cmdsize != sizeof(struct MachO::dyld_info_command))
8127 outs() << " Incorrect size\n";
8128 else
8129 outs() << "\n";
8130 outs() << " rebase_off " << dc.rebase_off;
8131 if (dc.rebase_off > object_size)
8132 outs() << " (past end of file)\n";
8133 else
8134 outs() << "\n";
8135 outs() << " rebase_size " << dc.rebase_size;
8136 uint64_t big_size;
8137 big_size = dc.rebase_off;
8138 big_size += dc.rebase_size;
8139 if (big_size > object_size)
8140 outs() << " (past end of file)\n";
8141 else
8142 outs() << "\n";
8143 outs() << " bind_off " << dc.bind_off;
8144 if (dc.bind_off > object_size)
8145 outs() << " (past end of file)\n";
8146 else
8147 outs() << "\n";
8148 outs() << " bind_size " << dc.bind_size;
8149 big_size = dc.bind_off;
8150 big_size += dc.bind_size;
8151 if (big_size > object_size)
8152 outs() << " (past end of file)\n";
8153 else
8154 outs() << "\n";
8155 outs() << " weak_bind_off " << dc.weak_bind_off;
8156 if (dc.weak_bind_off > object_size)
8157 outs() << " (past end of file)\n";
8158 else
8159 outs() << "\n";
8160 outs() << " weak_bind_size " << dc.weak_bind_size;
8161 big_size = dc.weak_bind_off;
8162 big_size += dc.weak_bind_size;
8163 if (big_size > object_size)
8164 outs() << " (past end of file)\n";
8165 else
8166 outs() << "\n";
8167 outs() << " lazy_bind_off " << dc.lazy_bind_off;
8168 if (dc.lazy_bind_off > object_size)
8169 outs() << " (past end of file)\n";
8170 else
8171 outs() << "\n";
8172 outs() << " lazy_bind_size " << dc.lazy_bind_size;
8173 big_size = dc.lazy_bind_off;
8174 big_size += dc.lazy_bind_size;
8175 if (big_size > object_size)
8176 outs() << " (past end of file)\n";
8177 else
8178 outs() << "\n";
8179 outs() << " export_off " << dc.export_off;
8180 if (dc.export_off > object_size)
8181 outs() << " (past end of file)\n";
8182 else
8183 outs() << "\n";
8184 outs() << " export_size " << dc.export_size;
8185 big_size = dc.export_off;
8186 big_size += dc.export_size;
8187 if (big_size > object_size)
8188 outs() << " (past end of file)\n";
8189 else
8190 outs() << "\n";
8191}
8192
8193static void PrintDyldLoadCommand(MachO::dylinker_command dyld,
8194 const char *Ptr) {
8195 if (dyld.cmd == MachO::LC_ID_DYLINKER)
8196 outs() << " cmd LC_ID_DYLINKER\n";
8197 else if (dyld.cmd == MachO::LC_LOAD_DYLINKER)
8198 outs() << " cmd LC_LOAD_DYLINKER\n";
8199 else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT)
8200 outs() << " cmd LC_DYLD_ENVIRONMENT\n";
8201 else
8202 outs() << " cmd ?(" << dyld.cmd << ")\n";
8203 outs() << " cmdsize " << dyld.cmdsize;
8204 if (dyld.cmdsize < sizeof(struct MachO::dylinker_command))
8205 outs() << " Incorrect size\n";
8206 else
8207 outs() << "\n";
8208 if (dyld.name >= dyld.cmdsize)
8209 outs() << " name ?(bad offset " << dyld.name << ")\n";
8210 else {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008211 const char *P = (const char *)(Ptr) + dyld.name;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008212 outs() << " name " << P << " (offset " << dyld.name << ")\n";
8213 }
8214}
8215
8216static void PrintUuidLoadCommand(MachO::uuid_command uuid) {
8217 outs() << " cmd LC_UUID\n";
8218 outs() << " cmdsize " << uuid.cmdsize;
8219 if (uuid.cmdsize != sizeof(struct MachO::uuid_command))
8220 outs() << " Incorrect size\n";
8221 else
8222 outs() << "\n";
8223 outs() << " uuid ";
Davide Italianoc74277a2015-12-07 00:03:28 +00008224 for (int i = 0; i < 16; ++i) {
8225 outs() << format("%02" PRIX32, uuid.uuid[i]);
8226 if (i == 3 || i == 5 || i == 7 || i == 9)
8227 outs() << "-";
8228 }
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008229 outs() << "\n";
8230}
8231
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008232static void PrintRpathLoadCommand(MachO::rpath_command rpath, const char *Ptr) {
Jean-Daniel Dupas00cc1f52014-12-04 07:37:02 +00008233 outs() << " cmd LC_RPATH\n";
8234 outs() << " cmdsize " << rpath.cmdsize;
8235 if (rpath.cmdsize < sizeof(struct MachO::rpath_command))
8236 outs() << " Incorrect size\n";
8237 else
8238 outs() << "\n";
8239 if (rpath.path >= rpath.cmdsize)
8240 outs() << " path ?(bad offset " << rpath.path << ")\n";
8241 else {
8242 const char *P = (const char *)(Ptr) + rpath.path;
8243 outs() << " path " << P << " (offset " << rpath.path << ")\n";
8244 }
8245}
8246
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008247static void PrintVersionMinLoadCommand(MachO::version_min_command vd) {
Tim Northoverbfbfb122015-11-02 21:26:58 +00008248 StringRef LoadCmdName;
8249 switch (vd.cmd) {
8250 case MachO::LC_VERSION_MIN_MACOSX:
8251 LoadCmdName = "LC_VERSION_MIN_MACOSX";
8252 break;
8253 case MachO::LC_VERSION_MIN_IPHONEOS:
8254 LoadCmdName = "LC_VERSION_MIN_IPHONEOS";
8255 break;
8256 case MachO::LC_VERSION_MIN_TVOS:
8257 LoadCmdName = "LC_VERSION_MIN_TVOS";
8258 break;
8259 case MachO::LC_VERSION_MIN_WATCHOS:
8260 LoadCmdName = "LC_VERSION_MIN_WATCHOS";
8261 break;
8262 default:
8263 llvm_unreachable("Unknown version min load command");
8264 }
8265
8266 outs() << " cmd " << LoadCmdName << '\n';
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008267 outs() << " cmdsize " << vd.cmdsize;
8268 if (vd.cmdsize != sizeof(struct MachO::version_min_command))
8269 outs() << " Incorrect size\n";
8270 else
8271 outs() << "\n";
Davide Italiano56baef32015-08-26 12:26:11 +00008272 outs() << " version "
8273 << MachOObjectFile::getVersionMinMajor(vd, false) << "."
8274 << MachOObjectFile::getVersionMinMinor(vd, false);
8275 uint32_t Update = MachOObjectFile::getVersionMinUpdate(vd, false);
8276 if (Update != 0)
8277 outs() << "." << Update;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008278 outs() << "\n";
8279 if (vd.sdk == 0)
Kevin Enderby57538292014-12-17 01:01:30 +00008280 outs() << " sdk n/a";
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008281 else {
Davide Italiano56baef32015-08-26 12:26:11 +00008282 outs() << " sdk "
8283 << MachOObjectFile::getVersionMinMajor(vd, true) << "."
8284 << MachOObjectFile::getVersionMinMinor(vd, true);
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008285 }
Davide Italiano56baef32015-08-26 12:26:11 +00008286 Update = MachOObjectFile::getVersionMinUpdate(vd, true);
8287 if (Update != 0)
8288 outs() << "." << Update;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008289 outs() << "\n";
8290}
8291
Kevin Enderbya4579c42017-01-19 17:36:31 +00008292static void PrintNoteLoadCommand(MachO::note_command Nt) {
8293 outs() << " cmd LC_NOTE\n";
8294 outs() << " cmdsize " << Nt.cmdsize;
8295 if (Nt.cmdsize != sizeof(struct MachO::note_command))
8296 outs() << " Incorrect size\n";
8297 else
8298 outs() << "\n";
8299 const char *d = Nt.data_owner;
8300 outs() << "data_owner " << format("%.16s\n", d);
8301 outs() << " offset " << Nt.offset << "\n";
8302 outs() << " size " << Nt.size << "\n";
8303}
8304
Steven Wu5b54a422017-01-23 20:07:55 +00008305static void PrintBuildToolVersion(MachO::build_tool_version bv) {
8306 outs() << " tool " << MachOObjectFile::getBuildTool(bv.tool) << "\n";
8307 outs() << " version " << MachOObjectFile::getVersionString(bv.version)
8308 << "\n";
8309}
8310
8311static void PrintBuildVersionLoadCommand(const MachOObjectFile *obj,
8312 MachO::build_version_command bd) {
8313 outs() << " cmd LC_BUILD_VERSION\n";
8314 outs() << " cmdsize " << bd.cmdsize;
8315 if (bd.cmdsize !=
8316 sizeof(struct MachO::build_version_command) +
8317 bd.ntools * sizeof(struct MachO::build_tool_version))
8318 outs() << " Incorrect size\n";
8319 else
8320 outs() << "\n";
8321 outs() << " platform " << MachOObjectFile::getBuildPlatform(bd.platform)
8322 << "\n";
8323 if (bd.sdk)
8324 outs() << " sdk " << MachOObjectFile::getVersionString(bd.sdk)
8325 << "\n";
8326 else
8327 outs() << " sdk n/a\n";
8328 outs() << " minos " << MachOObjectFile::getVersionString(bd.minos)
8329 << "\n";
8330 outs() << " ntools " << bd.ntools << "\n";
8331 for (unsigned i = 0; i < bd.ntools; ++i) {
8332 MachO::build_tool_version bv = obj->getBuildToolVersion(i);
8333 PrintBuildToolVersion(bv);
8334 }
8335}
8336
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008337static void PrintSourceVersionCommand(MachO::source_version_command sd) {
8338 outs() << " cmd LC_SOURCE_VERSION\n";
8339 outs() << " cmdsize " << sd.cmdsize;
8340 if (sd.cmdsize != sizeof(struct MachO::source_version_command))
8341 outs() << " Incorrect size\n";
8342 else
8343 outs() << "\n";
8344 uint64_t a = (sd.version >> 40) & 0xffffff;
8345 uint64_t b = (sd.version >> 30) & 0x3ff;
8346 uint64_t c = (sd.version >> 20) & 0x3ff;
8347 uint64_t d = (sd.version >> 10) & 0x3ff;
8348 uint64_t e = sd.version & 0x3ff;
8349 outs() << " version " << a << "." << b;
8350 if (e != 0)
8351 outs() << "." << c << "." << d << "." << e;
8352 else if (d != 0)
8353 outs() << "." << c << "." << d;
8354 else if (c != 0)
8355 outs() << "." << c;
8356 outs() << "\n";
8357}
8358
8359static void PrintEntryPointCommand(MachO::entry_point_command ep) {
8360 outs() << " cmd LC_MAIN\n";
8361 outs() << " cmdsize " << ep.cmdsize;
8362 if (ep.cmdsize != sizeof(struct MachO::entry_point_command))
8363 outs() << " Incorrect size\n";
8364 else
8365 outs() << "\n";
8366 outs() << " entryoff " << ep.entryoff << "\n";
8367 outs() << " stacksize " << ep.stacksize << "\n";
8368}
8369
Kevin Enderby0804f4672014-12-16 23:25:52 +00008370static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec,
8371 uint32_t object_size) {
8372 outs() << " cmd LC_ENCRYPTION_INFO\n";
8373 outs() << " cmdsize " << ec.cmdsize;
8374 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command))
8375 outs() << " Incorrect size\n";
8376 else
8377 outs() << "\n";
8378 outs() << " cryptoff " << ec.cryptoff;
8379 if (ec.cryptoff > object_size)
8380 outs() << " (past end of file)\n";
8381 else
8382 outs() << "\n";
8383 outs() << " cryptsize " << ec.cryptsize;
8384 if (ec.cryptsize > object_size)
8385 outs() << " (past end of file)\n";
8386 else
8387 outs() << "\n";
8388 outs() << " cryptid " << ec.cryptid << "\n";
8389}
8390
Kevin Enderby57538292014-12-17 01:01:30 +00008391static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec,
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008392 uint32_t object_size) {
Kevin Enderby57538292014-12-17 01:01:30 +00008393 outs() << " cmd LC_ENCRYPTION_INFO_64\n";
8394 outs() << " cmdsize " << ec.cmdsize;
8395 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64))
8396 outs() << " Incorrect size\n";
8397 else
8398 outs() << "\n";
8399 outs() << " cryptoff " << ec.cryptoff;
8400 if (ec.cryptoff > object_size)
8401 outs() << " (past end of file)\n";
8402 else
8403 outs() << "\n";
8404 outs() << " cryptsize " << ec.cryptsize;
8405 if (ec.cryptsize > object_size)
8406 outs() << " (past end of file)\n";
8407 else
8408 outs() << "\n";
8409 outs() << " cryptid " << ec.cryptid << "\n";
8410 outs() << " pad " << ec.pad << "\n";
8411}
8412
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00008413static void PrintLinkerOptionCommand(MachO::linker_option_command lo,
8414 const char *Ptr) {
8415 outs() << " cmd LC_LINKER_OPTION\n";
8416 outs() << " cmdsize " << lo.cmdsize;
8417 if (lo.cmdsize < sizeof(struct MachO::linker_option_command))
8418 outs() << " Incorrect size\n";
8419 else
8420 outs() << "\n";
8421 outs() << " count " << lo.count << "\n";
8422 const char *string = Ptr + sizeof(struct MachO::linker_option_command);
8423 uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command);
8424 uint32_t i = 0;
8425 while (left > 0) {
8426 while (*string == '\0' && left > 0) {
8427 string++;
8428 left--;
8429 }
8430 if (left > 0) {
8431 i++;
8432 outs() << " string #" << i << " " << format("%.*s\n", left, string);
David Majnemerd4449ed2014-12-20 08:24:43 +00008433 uint32_t NullPos = StringRef(string, left).find('\0');
8434 uint32_t len = std::min(NullPos, left) + 1;
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00008435 string += len;
8436 left -= len;
8437 }
8438 }
8439 if (lo.count != i)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008440 outs() << " count " << lo.count << " does not match number of strings "
8441 << i << "\n";
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00008442}
8443
Kevin Enderbyb4b79312014-12-18 19:24:35 +00008444static void PrintSubFrameworkCommand(MachO::sub_framework_command sub,
8445 const char *Ptr) {
8446 outs() << " cmd LC_SUB_FRAMEWORK\n";
8447 outs() << " cmdsize " << sub.cmdsize;
8448 if (sub.cmdsize < sizeof(struct MachO::sub_framework_command))
8449 outs() << " Incorrect size\n";
8450 else
8451 outs() << "\n";
8452 if (sub.umbrella < sub.cmdsize) {
8453 const char *P = Ptr + sub.umbrella;
8454 outs() << " umbrella " << P << " (offset " << sub.umbrella << ")\n";
8455 } else {
8456 outs() << " umbrella ?(bad offset " << sub.umbrella << ")\n";
8457 }
8458}
8459
Kevin Enderbya2bd8d92014-12-18 23:13:26 +00008460static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub,
8461 const char *Ptr) {
8462 outs() << " cmd LC_SUB_UMBRELLA\n";
8463 outs() << " cmdsize " << sub.cmdsize;
8464 if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command))
8465 outs() << " Incorrect size\n";
8466 else
8467 outs() << "\n";
8468 if (sub.sub_umbrella < sub.cmdsize) {
8469 const char *P = Ptr + sub.sub_umbrella;
8470 outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n";
8471 } else {
8472 outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n";
8473 }
8474}
8475
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00008476static void PrintSubLibraryCommand(MachO::sub_library_command sub,
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008477 const char *Ptr) {
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00008478 outs() << " cmd LC_SUB_LIBRARY\n";
8479 outs() << " cmdsize " << sub.cmdsize;
8480 if (sub.cmdsize < sizeof(struct MachO::sub_library_command))
8481 outs() << " Incorrect size\n";
8482 else
8483 outs() << "\n";
8484 if (sub.sub_library < sub.cmdsize) {
8485 const char *P = Ptr + sub.sub_library;
8486 outs() << " sub_library " << P << " (offset " << sub.sub_library << ")\n";
8487 } else {
8488 outs() << " sub_library ?(bad offset " << sub.sub_library << ")\n";
8489 }
8490}
8491
Kevin Enderby186eac32014-12-19 21:06:24 +00008492static void PrintSubClientCommand(MachO::sub_client_command sub,
8493 const char *Ptr) {
8494 outs() << " cmd LC_SUB_CLIENT\n";
8495 outs() << " cmdsize " << sub.cmdsize;
8496 if (sub.cmdsize < sizeof(struct MachO::sub_client_command))
8497 outs() << " Incorrect size\n";
8498 else
8499 outs() << "\n";
8500 if (sub.client < sub.cmdsize) {
8501 const char *P = Ptr + sub.client;
8502 outs() << " client " << P << " (offset " << sub.client << ")\n";
8503 } else {
8504 outs() << " client ?(bad offset " << sub.client << ")\n";
8505 }
8506}
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00008507
Kevin Enderby52e4ce42014-12-19 22:25:22 +00008508static void PrintRoutinesCommand(MachO::routines_command r) {
8509 outs() << " cmd LC_ROUTINES\n";
8510 outs() << " cmdsize " << r.cmdsize;
8511 if (r.cmdsize != sizeof(struct MachO::routines_command))
8512 outs() << " Incorrect size\n";
8513 else
8514 outs() << "\n";
8515 outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n";
8516 outs() << " init_module " << r.init_module << "\n";
8517 outs() << " reserved1 " << r.reserved1 << "\n";
8518 outs() << " reserved2 " << r.reserved2 << "\n";
8519 outs() << " reserved3 " << r.reserved3 << "\n";
8520 outs() << " reserved4 " << r.reserved4 << "\n";
8521 outs() << " reserved5 " << r.reserved5 << "\n";
8522 outs() << " reserved6 " << r.reserved6 << "\n";
8523}
8524
8525static void PrintRoutinesCommand64(MachO::routines_command_64 r) {
8526 outs() << " cmd LC_ROUTINES_64\n";
8527 outs() << " cmdsize " << r.cmdsize;
8528 if (r.cmdsize != sizeof(struct MachO::routines_command_64))
8529 outs() << " Incorrect size\n";
8530 else
8531 outs() << "\n";
8532 outs() << " init_address " << format("0x%016" PRIx64, r.init_address) << "\n";
8533 outs() << " init_module " << r.init_module << "\n";
8534 outs() << " reserved1 " << r.reserved1 << "\n";
8535 outs() << " reserved2 " << r.reserved2 << "\n";
8536 outs() << " reserved3 " << r.reserved3 << "\n";
8537 outs() << " reserved4 " << r.reserved4 << "\n";
8538 outs() << " reserved5 " << r.reserved5 << "\n";
8539 outs() << " reserved6 " << r.reserved6 << "\n";
8540}
8541
Kevin Enderbyc3a035d2017-01-23 21:13:29 +00008542static void Print_x86_thread_state32_t(MachO::x86_thread_state32_t &cpu32) {
8543 outs() << "\t eax " << format("0x%08" PRIx32, cpu32.eax);
8544 outs() << " ebx " << format("0x%08" PRIx32, cpu32.ebx);
8545 outs() << " ecx " << format("0x%08" PRIx32, cpu32.ecx);
8546 outs() << " edx " << format("0x%08" PRIx32, cpu32.edx) << "\n";
8547 outs() << "\t edi " << format("0x%08" PRIx32, cpu32.edi);
8548 outs() << " esi " << format("0x%08" PRIx32, cpu32.esi);
8549 outs() << " ebp " << format("0x%08" PRIx32, cpu32.ebp);
8550 outs() << " esp " << format("0x%08" PRIx32, cpu32.esp) << "\n";
8551 outs() << "\t ss " << format("0x%08" PRIx32, cpu32.ss);
8552 outs() << " eflags " << format("0x%08" PRIx32, cpu32.eflags);
8553 outs() << " eip " << format("0x%08" PRIx32, cpu32.eip);
8554 outs() << " cs " << format("0x%08" PRIx32, cpu32.cs) << "\n";
8555 outs() << "\t ds " << format("0x%08" PRIx32, cpu32.ds);
8556 outs() << " es " << format("0x%08" PRIx32, cpu32.es);
8557 outs() << " fs " << format("0x%08" PRIx32, cpu32.fs);
8558 outs() << " gs " << format("0x%08" PRIx32, cpu32.gs) << "\n";
8559}
8560
Kevin Enderby48ef5342014-12-23 22:56:39 +00008561static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) {
8562 outs() << " rax " << format("0x%016" PRIx64, cpu64.rax);
8563 outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx);
8564 outs() << " rcx " << format("0x%016" PRIx64, cpu64.rcx) << "\n";
8565 outs() << " rdx " << format("0x%016" PRIx64, cpu64.rdx);
8566 outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi);
8567 outs() << " rsi " << format("0x%016" PRIx64, cpu64.rsi) << "\n";
8568 outs() << " rbp " << format("0x%016" PRIx64, cpu64.rbp);
8569 outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp);
8570 outs() << " r8 " << format("0x%016" PRIx64, cpu64.r8) << "\n";
8571 outs() << " r9 " << format("0x%016" PRIx64, cpu64.r9);
8572 outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10);
8573 outs() << " r11 " << format("0x%016" PRIx64, cpu64.r11) << "\n";
8574 outs() << " r12 " << format("0x%016" PRIx64, cpu64.r12);
8575 outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13);
8576 outs() << " r14 " << format("0x%016" PRIx64, cpu64.r14) << "\n";
8577 outs() << " r15 " << format("0x%016" PRIx64, cpu64.r15);
8578 outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n";
8579 outs() << "rflags " << format("0x%016" PRIx64, cpu64.rflags);
8580 outs() << " cs " << format("0x%016" PRIx64, cpu64.cs);
8581 outs() << " fs " << format("0x%016" PRIx64, cpu64.fs) << "\n";
8582 outs() << " gs " << format("0x%016" PRIx64, cpu64.gs) << "\n";
8583}
8584
Kevin Enderby227df342014-12-23 23:43:59 +00008585static void Print_mmst_reg(MachO::mmst_reg_t &r) {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008586 uint32_t f;
8587 outs() << "\t mmst_reg ";
8588 for (f = 0; f < 10; f++)
8589 outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " ";
8590 outs() << "\n";
8591 outs() << "\t mmst_rsrv ";
8592 for (f = 0; f < 6; f++)
8593 outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " ";
8594 outs() << "\n";
8595}
8596
Kevin Enderbyaefb0032014-12-24 00:16:51 +00008597static void Print_xmm_reg(MachO::xmm_reg_t &r) {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008598 uint32_t f;
8599 outs() << "\t xmm_reg ";
8600 for (f = 0; f < 16; f++)
8601 outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " ";
8602 outs() << "\n";
8603}
8604
8605static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) {
8606 outs() << "\t fpu_reserved[0] " << fpu.fpu_reserved[0];
8607 outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n";
8608 outs() << "\t control: invalid " << fpu.fpu_fcw.invalid;
8609 outs() << " denorm " << fpu.fpu_fcw.denorm;
8610 outs() << " zdiv " << fpu.fpu_fcw.zdiv;
8611 outs() << " ovrfl " << fpu.fpu_fcw.ovrfl;
8612 outs() << " undfl " << fpu.fpu_fcw.undfl;
8613 outs() << " precis " << fpu.fpu_fcw.precis << "\n";
8614 outs() << "\t\t pc ";
8615 if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B)
8616 outs() << "FP_PREC_24B ";
8617 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B)
8618 outs() << "FP_PREC_53B ";
8619 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B)
8620 outs() << "FP_PREC_64B ";
8621 else
8622 outs() << fpu.fpu_fcw.pc << " ";
8623 outs() << "rc ";
8624 if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR)
8625 outs() << "FP_RND_NEAR ";
8626 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN)
8627 outs() << "FP_RND_DOWN ";
8628 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP)
8629 outs() << "FP_RND_UP ";
8630 else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008631 outs() << "FP_CHOP ";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008632 outs() << "\n";
8633 outs() << "\t status: invalid " << fpu.fpu_fsw.invalid;
8634 outs() << " denorm " << fpu.fpu_fsw.denorm;
8635 outs() << " zdiv " << fpu.fpu_fsw.zdiv;
8636 outs() << " ovrfl " << fpu.fpu_fsw.ovrfl;
8637 outs() << " undfl " << fpu.fpu_fsw.undfl;
8638 outs() << " precis " << fpu.fpu_fsw.precis;
8639 outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n";
8640 outs() << "\t errsumm " << fpu.fpu_fsw.errsumm;
8641 outs() << " c0 " << fpu.fpu_fsw.c0;
8642 outs() << " c1 " << fpu.fpu_fsw.c1;
8643 outs() << " c2 " << fpu.fpu_fsw.c2;
8644 outs() << " tos " << fpu.fpu_fsw.tos;
8645 outs() << " c3 " << fpu.fpu_fsw.c3;
8646 outs() << " busy " << fpu.fpu_fsw.busy << "\n";
8647 outs() << "\t fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw);
8648 outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1);
8649 outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop);
8650 outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n";
8651 outs() << "\t fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs);
8652 outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2);
8653 outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp);
8654 outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n";
8655 outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3);
8656 outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr);
8657 outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask);
8658 outs() << "\n";
8659 outs() << "\t fpu_stmm0:\n";
8660 Print_mmst_reg(fpu.fpu_stmm0);
8661 outs() << "\t fpu_stmm1:\n";
8662 Print_mmst_reg(fpu.fpu_stmm1);
8663 outs() << "\t fpu_stmm2:\n";
8664 Print_mmst_reg(fpu.fpu_stmm2);
8665 outs() << "\t fpu_stmm3:\n";
8666 Print_mmst_reg(fpu.fpu_stmm3);
8667 outs() << "\t fpu_stmm4:\n";
8668 Print_mmst_reg(fpu.fpu_stmm4);
8669 outs() << "\t fpu_stmm5:\n";
8670 Print_mmst_reg(fpu.fpu_stmm5);
8671 outs() << "\t fpu_stmm6:\n";
8672 Print_mmst_reg(fpu.fpu_stmm6);
8673 outs() << "\t fpu_stmm7:\n";
8674 Print_mmst_reg(fpu.fpu_stmm7);
8675 outs() << "\t fpu_xmm0:\n";
8676 Print_xmm_reg(fpu.fpu_xmm0);
8677 outs() << "\t fpu_xmm1:\n";
8678 Print_xmm_reg(fpu.fpu_xmm1);
8679 outs() << "\t fpu_xmm2:\n";
8680 Print_xmm_reg(fpu.fpu_xmm2);
8681 outs() << "\t fpu_xmm3:\n";
8682 Print_xmm_reg(fpu.fpu_xmm3);
8683 outs() << "\t fpu_xmm4:\n";
8684 Print_xmm_reg(fpu.fpu_xmm4);
8685 outs() << "\t fpu_xmm5:\n";
8686 Print_xmm_reg(fpu.fpu_xmm5);
8687 outs() << "\t fpu_xmm6:\n";
8688 Print_xmm_reg(fpu.fpu_xmm6);
8689 outs() << "\t fpu_xmm7:\n";
8690 Print_xmm_reg(fpu.fpu_xmm7);
8691 outs() << "\t fpu_xmm8:\n";
8692 Print_xmm_reg(fpu.fpu_xmm8);
8693 outs() << "\t fpu_xmm9:\n";
8694 Print_xmm_reg(fpu.fpu_xmm9);
8695 outs() << "\t fpu_xmm10:\n";
8696 Print_xmm_reg(fpu.fpu_xmm10);
8697 outs() << "\t fpu_xmm11:\n";
8698 Print_xmm_reg(fpu.fpu_xmm11);
8699 outs() << "\t fpu_xmm12:\n";
8700 Print_xmm_reg(fpu.fpu_xmm12);
8701 outs() << "\t fpu_xmm13:\n";
8702 Print_xmm_reg(fpu.fpu_xmm13);
8703 outs() << "\t fpu_xmm14:\n";
8704 Print_xmm_reg(fpu.fpu_xmm14);
8705 outs() << "\t fpu_xmm15:\n";
8706 Print_xmm_reg(fpu.fpu_xmm15);
8707 outs() << "\t fpu_rsrv4:\n";
8708 for (uint32_t f = 0; f < 6; f++) {
8709 outs() << "\t ";
8710 for (uint32_t g = 0; g < 16; g++)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008711 outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f * g]) << " ";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008712 outs() << "\n";
8713 }
8714 outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1);
8715 outs() << "\n";
8716}
8717
8718static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) {
8719 outs() << "\t trapno " << format("0x%08" PRIx32, exc64.trapno);
8720 outs() << " err " << format("0x%08" PRIx32, exc64.err);
8721 outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n";
8722}
8723
Kevin Enderby41c9c002016-10-21 18:22:35 +00008724static void Print_arm_thread_state32_t(MachO::arm_thread_state32_t &cpu32) {
8725 outs() << "\t r0 " << format("0x%08" PRIx32, cpu32.r[0]);
8726 outs() << " r1 " << format("0x%08" PRIx32, cpu32.r[1]);
8727 outs() << " r2 " << format("0x%08" PRIx32, cpu32.r[2]);
8728 outs() << " r3 " << format("0x%08" PRIx32, cpu32.r[3]) << "\n";
8729 outs() << "\t r4 " << format("0x%08" PRIx32, cpu32.r[4]);
8730 outs() << " r5 " << format("0x%08" PRIx32, cpu32.r[5]);
8731 outs() << " r6 " << format("0x%08" PRIx32, cpu32.r[6]);
8732 outs() << " r7 " << format("0x%08" PRIx32, cpu32.r[7]) << "\n";
8733 outs() << "\t r8 " << format("0x%08" PRIx32, cpu32.r[8]);
8734 outs() << " r9 " << format("0x%08" PRIx32, cpu32.r[9]);
8735 outs() << " r10 " << format("0x%08" PRIx32, cpu32.r[10]);
8736 outs() << " r11 " << format("0x%08" PRIx32, cpu32.r[11]) << "\n";
8737 outs() << "\t r12 " << format("0x%08" PRIx32, cpu32.r[12]);
8738 outs() << " sp " << format("0x%08" PRIx32, cpu32.sp);
8739 outs() << " lr " << format("0x%08" PRIx32, cpu32.lr);
8740 outs() << " pc " << format("0x%08" PRIx32, cpu32.pc) << "\n";
8741 outs() << "\t cpsr " << format("0x%08" PRIx32, cpu32.cpsr) << "\n";
8742}
8743
Kevin Enderby7747cb52016-11-03 20:51:28 +00008744static void Print_arm_thread_state64_t(MachO::arm_thread_state64_t &cpu64) {
8745 outs() << "\t x0 " << format("0x%016" PRIx64, cpu64.x[0]);
8746 outs() << " x1 " << format("0x%016" PRIx64, cpu64.x[1]);
8747 outs() << " x2 " << format("0x%016" PRIx64, cpu64.x[2]) << "\n";
8748 outs() << "\t x3 " << format("0x%016" PRIx64, cpu64.x[3]);
8749 outs() << " x4 " << format("0x%016" PRIx64, cpu64.x[4]);
8750 outs() << " x5 " << format("0x%016" PRIx64, cpu64.x[5]) << "\n";
8751 outs() << "\t x6 " << format("0x%016" PRIx64, cpu64.x[6]);
8752 outs() << " x7 " << format("0x%016" PRIx64, cpu64.x[7]);
8753 outs() << " x8 " << format("0x%016" PRIx64, cpu64.x[8]) << "\n";
8754 outs() << "\t x9 " << format("0x%016" PRIx64, cpu64.x[9]);
8755 outs() << " x10 " << format("0x%016" PRIx64, cpu64.x[10]);
8756 outs() << " x11 " << format("0x%016" PRIx64, cpu64.x[11]) << "\n";
8757 outs() << "\t x12 " << format("0x%016" PRIx64, cpu64.x[12]);
8758 outs() << " x13 " << format("0x%016" PRIx64, cpu64.x[13]);
8759 outs() << " x14 " << format("0x%016" PRIx64, cpu64.x[14]) << "\n";
8760 outs() << "\t x15 " << format("0x%016" PRIx64, cpu64.x[15]);
8761 outs() << " x16 " << format("0x%016" PRIx64, cpu64.x[16]);
8762 outs() << " x17 " << format("0x%016" PRIx64, cpu64.x[17]) << "\n";
8763 outs() << "\t x18 " << format("0x%016" PRIx64, cpu64.x[18]);
8764 outs() << " x19 " << format("0x%016" PRIx64, cpu64.x[19]);
8765 outs() << " x20 " << format("0x%016" PRIx64, cpu64.x[20]) << "\n";
8766 outs() << "\t x21 " << format("0x%016" PRIx64, cpu64.x[21]);
8767 outs() << " x22 " << format("0x%016" PRIx64, cpu64.x[22]);
8768 outs() << " x23 " << format("0x%016" PRIx64, cpu64.x[23]) << "\n";
8769 outs() << "\t x24 " << format("0x%016" PRIx64, cpu64.x[24]);
8770 outs() << " x25 " << format("0x%016" PRIx64, cpu64.x[25]);
8771 outs() << " x26 " << format("0x%016" PRIx64, cpu64.x[26]) << "\n";
8772 outs() << "\t x27 " << format("0x%016" PRIx64, cpu64.x[27]);
8773 outs() << " x28 " << format("0x%016" PRIx64, cpu64.x[28]);
8774 outs() << " fp " << format("0x%016" PRIx64, cpu64.fp) << "\n";
8775 outs() << "\t lr " << format("0x%016" PRIx64, cpu64.lr);
8776 outs() << " sp " << format("0x%016" PRIx64, cpu64.sp);
8777 outs() << " pc " << format("0x%016" PRIx64, cpu64.pc) << "\n";
8778 outs() << "\t cpsr " << format("0x%08" PRIx32, cpu64.cpsr) << "\n";
8779}
8780
Kevin Enderby48ef5342014-12-23 22:56:39 +00008781static void PrintThreadCommand(MachO::thread_command t, const char *Ptr,
8782 bool isLittleEndian, uint32_t cputype) {
8783 if (t.cmd == MachO::LC_THREAD)
8784 outs() << " cmd LC_THREAD\n";
8785 else if (t.cmd == MachO::LC_UNIXTHREAD)
8786 outs() << " cmd LC_UNIXTHREAD\n";
8787 else
8788 outs() << " cmd " << t.cmd << " (unknown)\n";
8789 outs() << " cmdsize " << t.cmdsize;
8790 if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t))
8791 outs() << " Incorrect size\n";
8792 else
8793 outs() << "\n";
8794
8795 const char *begin = Ptr + sizeof(struct MachO::thread_command);
8796 const char *end = Ptr + t.cmdsize;
8797 uint32_t flavor, count, left;
Kevin Enderbyc3a035d2017-01-23 21:13:29 +00008798 if (cputype == MachO::CPU_TYPE_I386) {
8799 while (begin < end) {
8800 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8801 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8802 begin += sizeof(uint32_t);
8803 } else {
8804 flavor = 0;
8805 begin = end;
8806 }
8807 if (isLittleEndian != sys::IsLittleEndianHost)
8808 sys::swapByteOrder(flavor);
8809 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8810 memcpy((char *)&count, begin, sizeof(uint32_t));
8811 begin += sizeof(uint32_t);
8812 } else {
8813 count = 0;
8814 begin = end;
8815 }
8816 if (isLittleEndian != sys::IsLittleEndianHost)
8817 sys::swapByteOrder(count);
8818 if (flavor == MachO::x86_THREAD_STATE32) {
8819 outs() << " flavor i386_THREAD_STATE\n";
8820 if (count == MachO::x86_THREAD_STATE32_COUNT)
8821 outs() << " count i386_THREAD_STATE_COUNT\n";
8822 else
8823 outs() << " count " << count
8824 << " (not x86_THREAD_STATE32_COUNT)\n";
8825 MachO::x86_thread_state32_t cpu32;
8826 left = end - begin;
8827 if (left >= sizeof(MachO::x86_thread_state32_t)) {
8828 memcpy(&cpu32, begin, sizeof(MachO::x86_thread_state32_t));
8829 begin += sizeof(MachO::x86_thread_state32_t);
8830 } else {
8831 memset(&cpu32, '\0', sizeof(MachO::x86_thread_state32_t));
8832 memcpy(&cpu32, begin, left);
8833 begin += left;
8834 }
8835 if (isLittleEndian != sys::IsLittleEndianHost)
8836 swapStruct(cpu32);
8837 Print_x86_thread_state32_t(cpu32);
8838 } else if (flavor == MachO::x86_THREAD_STATE) {
8839 outs() << " flavor x86_THREAD_STATE\n";
8840 if (count == MachO::x86_THREAD_STATE_COUNT)
8841 outs() << " count x86_THREAD_STATE_COUNT\n";
8842 else
8843 outs() << " count " << count
8844 << " (not x86_THREAD_STATE_COUNT)\n";
8845 struct MachO::x86_thread_state_t ts;
8846 left = end - begin;
8847 if (left >= sizeof(MachO::x86_thread_state_t)) {
8848 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
8849 begin += sizeof(MachO::x86_thread_state_t);
8850 } else {
8851 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
8852 memcpy(&ts, begin, left);
8853 begin += left;
8854 }
8855 if (isLittleEndian != sys::IsLittleEndianHost)
8856 swapStruct(ts);
8857 if (ts.tsh.flavor == MachO::x86_THREAD_STATE32) {
8858 outs() << "\t tsh.flavor x86_THREAD_STATE32 ";
8859 if (ts.tsh.count == MachO::x86_THREAD_STATE32_COUNT)
8860 outs() << "tsh.count x86_THREAD_STATE32_COUNT\n";
8861 else
8862 outs() << "tsh.count " << ts.tsh.count
8863 << " (not x86_THREAD_STATE32_COUNT\n";
8864 Print_x86_thread_state32_t(ts.uts.ts32);
8865 } else {
8866 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
8867 << ts.tsh.count << "\n";
8868 }
8869 } else {
8870 outs() << " flavor " << flavor << " (unknown)\n";
8871 outs() << " count " << count << "\n";
8872 outs() << " state (unknown)\n";
8873 begin += count * sizeof(uint32_t);
8874 }
8875 }
8876 } else if (cputype == MachO::CPU_TYPE_X86_64) {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008877 while (begin < end) {
8878 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8879 memcpy((char *)&flavor, begin, sizeof(uint32_t));
8880 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008881 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008882 flavor = 0;
8883 begin = end;
8884 }
8885 if (isLittleEndian != sys::IsLittleEndianHost)
8886 sys::swapByteOrder(flavor);
8887 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
8888 memcpy((char *)&count, begin, sizeof(uint32_t));
8889 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008890 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008891 count = 0;
8892 begin = end;
8893 }
8894 if (isLittleEndian != sys::IsLittleEndianHost)
8895 sys::swapByteOrder(count);
8896 if (flavor == MachO::x86_THREAD_STATE64) {
8897 outs() << " flavor x86_THREAD_STATE64\n";
8898 if (count == MachO::x86_THREAD_STATE64_COUNT)
8899 outs() << " count x86_THREAD_STATE64_COUNT\n";
8900 else
8901 outs() << " count " << count
8902 << " (not x86_THREAD_STATE64_COUNT)\n";
8903 MachO::x86_thread_state64_t cpu64;
8904 left = end - begin;
8905 if (left >= sizeof(MachO::x86_thread_state64_t)) {
8906 memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t));
8907 begin += sizeof(MachO::x86_thread_state64_t);
8908 } else {
8909 memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t));
8910 memcpy(&cpu64, begin, left);
8911 begin += left;
8912 }
8913 if (isLittleEndian != sys::IsLittleEndianHost)
8914 swapStruct(cpu64);
8915 Print_x86_thread_state64_t(cpu64);
8916 } else if (flavor == MachO::x86_THREAD_STATE) {
8917 outs() << " flavor x86_THREAD_STATE\n";
8918 if (count == MachO::x86_THREAD_STATE_COUNT)
8919 outs() << " count x86_THREAD_STATE_COUNT\n";
8920 else
8921 outs() << " count " << count
8922 << " (not x86_THREAD_STATE_COUNT)\n";
8923 struct MachO::x86_thread_state_t ts;
8924 left = end - begin;
8925 if (left >= sizeof(MachO::x86_thread_state_t)) {
8926 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
8927 begin += sizeof(MachO::x86_thread_state_t);
8928 } else {
8929 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
8930 memcpy(&ts, begin, left);
8931 begin += left;
8932 }
8933 if (isLittleEndian != sys::IsLittleEndianHost)
8934 swapStruct(ts);
8935 if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) {
8936 outs() << "\t tsh.flavor x86_THREAD_STATE64 ";
8937 if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT)
8938 outs() << "tsh.count x86_THREAD_STATE64_COUNT\n";
8939 else
8940 outs() << "tsh.count " << ts.tsh.count
8941 << " (not x86_THREAD_STATE64_COUNT\n";
8942 Print_x86_thread_state64_t(ts.uts.ts64);
8943 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008944 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
8945 << ts.tsh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008946 }
8947 } else if (flavor == MachO::x86_FLOAT_STATE) {
8948 outs() << " flavor x86_FLOAT_STATE\n";
8949 if (count == MachO::x86_FLOAT_STATE_COUNT)
8950 outs() << " count x86_FLOAT_STATE_COUNT\n";
8951 else
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008952 outs() << " count " << count << " (not x86_FLOAT_STATE_COUNT)\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008953 struct MachO::x86_float_state_t fs;
8954 left = end - begin;
8955 if (left >= sizeof(MachO::x86_float_state_t)) {
8956 memcpy(&fs, begin, sizeof(MachO::x86_float_state_t));
8957 begin += sizeof(MachO::x86_float_state_t);
8958 } else {
8959 memset(&fs, '\0', sizeof(MachO::x86_float_state_t));
8960 memcpy(&fs, begin, left);
8961 begin += left;
8962 }
8963 if (isLittleEndian != sys::IsLittleEndianHost)
8964 swapStruct(fs);
8965 if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) {
8966 outs() << "\t fsh.flavor x86_FLOAT_STATE64 ";
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008967 if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT)
Kevin Enderby48ef5342014-12-23 22:56:39 +00008968 outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n";
8969 else
8970 outs() << "fsh.count " << fs.fsh.count
8971 << " (not x86_FLOAT_STATE64_COUNT\n";
8972 Print_x86_float_state_t(fs.ufs.fs64);
8973 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008974 outs() << "\t fsh.flavor " << fs.fsh.flavor << " fsh.count "
8975 << fs.fsh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008976 }
8977 } else if (flavor == MachO::x86_EXCEPTION_STATE) {
8978 outs() << " flavor x86_EXCEPTION_STATE\n";
8979 if (count == MachO::x86_EXCEPTION_STATE_COUNT)
8980 outs() << " count x86_EXCEPTION_STATE_COUNT\n";
8981 else
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008982 outs() << " count " << count
Kevin Enderby48ef5342014-12-23 22:56:39 +00008983 << " (not x86_EXCEPTION_STATE_COUNT)\n";
8984 struct MachO::x86_exception_state_t es;
8985 left = end - begin;
8986 if (left >= sizeof(MachO::x86_exception_state_t)) {
8987 memcpy(&es, begin, sizeof(MachO::x86_exception_state_t));
8988 begin += sizeof(MachO::x86_exception_state_t);
8989 } else {
8990 memset(&es, '\0', sizeof(MachO::x86_exception_state_t));
8991 memcpy(&es, begin, left);
8992 begin += left;
8993 }
8994 if (isLittleEndian != sys::IsLittleEndianHost)
8995 swapStruct(es);
8996 if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) {
8997 outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n";
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008998 if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT)
Kevin Enderby48ef5342014-12-23 22:56:39 +00008999 outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n";
9000 else
9001 outs() << "\t esh.count " << es.esh.count
9002 << " (not x86_EXCEPTION_STATE64_COUNT\n";
9003 Print_x86_exception_state_t(es.ues.es64);
9004 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00009005 outs() << "\t esh.flavor " << es.esh.flavor << " esh.count "
9006 << es.esh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00009007 }
9008 } else {
9009 outs() << " flavor " << flavor << " (unknown)\n";
9010 outs() << " count " << count << "\n";
9011 outs() << " state (unknown)\n";
9012 begin += count * sizeof(uint32_t);
9013 }
9014 }
Kevin Enderby41c9c002016-10-21 18:22:35 +00009015 } else if (cputype == MachO::CPU_TYPE_ARM) {
9016 while (begin < end) {
9017 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9018 memcpy((char *)&flavor, begin, sizeof(uint32_t));
9019 begin += sizeof(uint32_t);
9020 } else {
9021 flavor = 0;
9022 begin = end;
9023 }
9024 if (isLittleEndian != sys::IsLittleEndianHost)
9025 sys::swapByteOrder(flavor);
9026 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9027 memcpy((char *)&count, begin, sizeof(uint32_t));
9028 begin += sizeof(uint32_t);
9029 } else {
9030 count = 0;
9031 begin = end;
9032 }
9033 if (isLittleEndian != sys::IsLittleEndianHost)
9034 sys::swapByteOrder(count);
9035 if (flavor == MachO::ARM_THREAD_STATE) {
9036 outs() << " flavor ARM_THREAD_STATE\n";
9037 if (count == MachO::ARM_THREAD_STATE_COUNT)
9038 outs() << " count ARM_THREAD_STATE_COUNT\n";
9039 else
9040 outs() << " count " << count
9041 << " (not ARM_THREAD_STATE_COUNT)\n";
9042 MachO::arm_thread_state32_t cpu32;
9043 left = end - begin;
9044 if (left >= sizeof(MachO::arm_thread_state32_t)) {
9045 memcpy(&cpu32, begin, sizeof(MachO::arm_thread_state32_t));
9046 begin += sizeof(MachO::arm_thread_state32_t);
9047 } else {
9048 memset(&cpu32, '\0', sizeof(MachO::arm_thread_state32_t));
9049 memcpy(&cpu32, begin, left);
9050 begin += left;
9051 }
9052 if (isLittleEndian != sys::IsLittleEndianHost)
9053 swapStruct(cpu32);
9054 Print_arm_thread_state32_t(cpu32);
9055 } else {
9056 outs() << " flavor " << flavor << " (unknown)\n";
9057 outs() << " count " << count << "\n";
9058 outs() << " state (unknown)\n";
9059 begin += count * sizeof(uint32_t);
9060 }
9061 }
Kevin Enderby7747cb52016-11-03 20:51:28 +00009062 } else if (cputype == MachO::CPU_TYPE_ARM64) {
9063 while (begin < end) {
9064 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9065 memcpy((char *)&flavor, begin, sizeof(uint32_t));
9066 begin += sizeof(uint32_t);
9067 } else {
9068 flavor = 0;
9069 begin = end;
9070 }
9071 if (isLittleEndian != sys::IsLittleEndianHost)
9072 sys::swapByteOrder(flavor);
9073 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9074 memcpy((char *)&count, begin, sizeof(uint32_t));
9075 begin += sizeof(uint32_t);
9076 } else {
9077 count = 0;
9078 begin = end;
9079 }
9080 if (isLittleEndian != sys::IsLittleEndianHost)
9081 sys::swapByteOrder(count);
9082 if (flavor == MachO::ARM_THREAD_STATE64) {
9083 outs() << " flavor ARM_THREAD_STATE64\n";
9084 if (count == MachO::ARM_THREAD_STATE64_COUNT)
9085 outs() << " count ARM_THREAD_STATE64_COUNT\n";
9086 else
9087 outs() << " count " << count
9088 << " (not ARM_THREAD_STATE64_COUNT)\n";
9089 MachO::arm_thread_state64_t cpu64;
9090 left = end - begin;
9091 if (left >= sizeof(MachO::arm_thread_state64_t)) {
9092 memcpy(&cpu64, begin, sizeof(MachO::arm_thread_state64_t));
9093 begin += sizeof(MachO::arm_thread_state64_t);
9094 } else {
9095 memset(&cpu64, '\0', sizeof(MachO::arm_thread_state64_t));
9096 memcpy(&cpu64, begin, left);
9097 begin += left;
9098 }
9099 if (isLittleEndian != sys::IsLittleEndianHost)
9100 swapStruct(cpu64);
9101 Print_arm_thread_state64_t(cpu64);
9102 } else {
9103 outs() << " flavor " << flavor << " (unknown)\n";
9104 outs() << " count " << count << "\n";
9105 outs() << " state (unknown)\n";
9106 begin += count * sizeof(uint32_t);
9107 }
9108 }
Kevin Enderby48ef5342014-12-23 22:56:39 +00009109 } else {
9110 while (begin < end) {
9111 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9112 memcpy((char *)&flavor, begin, sizeof(uint32_t));
9113 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00009114 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00009115 flavor = 0;
9116 begin = end;
9117 }
9118 if (isLittleEndian != sys::IsLittleEndianHost)
9119 sys::swapByteOrder(flavor);
9120 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9121 memcpy((char *)&count, begin, sizeof(uint32_t));
9122 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00009123 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00009124 count = 0;
9125 begin = end;
9126 }
9127 if (isLittleEndian != sys::IsLittleEndianHost)
9128 sys::swapByteOrder(count);
9129 outs() << " flavor " << flavor << "\n";
9130 outs() << " count " << count << "\n";
9131 outs() << " state (Unknown cputype/cpusubtype)\n";
9132 begin += count * sizeof(uint32_t);
9133 }
9134 }
9135}
Kevin Enderby66d51fc2015-01-08 00:25:24 +00009136
Kevin Enderby8ae63c12014-09-04 16:54:47 +00009137static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) {
9138 if (dl.cmd == MachO::LC_ID_DYLIB)
9139 outs() << " cmd LC_ID_DYLIB\n";
9140 else if (dl.cmd == MachO::LC_LOAD_DYLIB)
9141 outs() << " cmd LC_LOAD_DYLIB\n";
9142 else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB)
9143 outs() << " cmd LC_LOAD_WEAK_DYLIB\n";
9144 else if (dl.cmd == MachO::LC_REEXPORT_DYLIB)
9145 outs() << " cmd LC_REEXPORT_DYLIB\n";
9146 else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB)
9147 outs() << " cmd LC_LAZY_LOAD_DYLIB\n";
9148 else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
9149 outs() << " cmd LC_LOAD_UPWARD_DYLIB\n";
9150 else
9151 outs() << " cmd " << dl.cmd << " (unknown)\n";
9152 outs() << " cmdsize " << dl.cmdsize;
9153 if (dl.cmdsize < sizeof(struct MachO::dylib_command))
9154 outs() << " Incorrect size\n";
9155 else
9156 outs() << "\n";
9157 if (dl.dylib.name < dl.cmdsize) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009158 const char *P = (const char *)(Ptr) + dl.dylib.name;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00009159 outs() << " name " << P << " (offset " << dl.dylib.name << ")\n";
9160 } else {
9161 outs() << " name ?(bad offset " << dl.dylib.name << ")\n";
9162 }
9163 outs() << " time stamp " << dl.dylib.timestamp << " ";
9164 time_t t = dl.dylib.timestamp;
9165 outs() << ctime(&t);
9166 outs() << " current version ";
9167 if (dl.dylib.current_version == 0xffffffff)
9168 outs() << "n/a\n";
9169 else
9170 outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "."
9171 << ((dl.dylib.current_version >> 8) & 0xff) << "."
9172 << (dl.dylib.current_version & 0xff) << "\n";
9173 outs() << "compatibility version ";
9174 if (dl.dylib.compatibility_version == 0xffffffff)
9175 outs() << "n/a\n";
9176 else
9177 outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
9178 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
9179 << (dl.dylib.compatibility_version & 0xff) << "\n";
9180}
9181
9182static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
9183 uint32_t object_size) {
9184 if (ld.cmd == MachO::LC_CODE_SIGNATURE)
Kevin Enderby1be37a32016-04-28 21:07:20 +00009185 outs() << " cmd LC_CODE_SIGNATURE\n";
Kevin Enderby8ae63c12014-09-04 16:54:47 +00009186 else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO)
9187 outs() << " cmd LC_SEGMENT_SPLIT_INFO\n";
9188 else if (ld.cmd == MachO::LC_FUNCTION_STARTS)
9189 outs() << " cmd LC_FUNCTION_STARTS\n";
9190 else if (ld.cmd == MachO::LC_DATA_IN_CODE)
9191 outs() << " cmd LC_DATA_IN_CODE\n";
9192 else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS)
9193 outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n";
9194 else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT)
9195 outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n";
9196 else
9197 outs() << " cmd " << ld.cmd << " (?)\n";
9198 outs() << " cmdsize " << ld.cmdsize;
9199 if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command))
9200 outs() << " Incorrect size\n";
9201 else
9202 outs() << "\n";
9203 outs() << " dataoff " << ld.dataoff;
9204 if (ld.dataoff > object_size)
9205 outs() << " (past end of file)\n";
9206 else
9207 outs() << "\n";
9208 outs() << " datasize " << ld.datasize;
9209 uint64_t big_size = ld.dataoff;
9210 big_size += ld.datasize;
9211 if (big_size > object_size)
9212 outs() << " (past end of file)\n";
9213 else
9214 outs() << "\n";
9215}
9216
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00009217static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t filetype,
9218 uint32_t cputype, bool verbose) {
Kevin Enderby956366c2014-08-29 22:30:52 +00009219 StringRef Buf = Obj->getData();
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00009220 unsigned Index = 0;
9221 for (const auto &Command : Obj->load_commands()) {
9222 outs() << "Load command " << Index++ << "\n";
Kevin Enderby956366c2014-08-29 22:30:52 +00009223 if (Command.C.cmd == MachO::LC_SEGMENT) {
9224 MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
9225 const char *sg_segname = SLC.segname;
9226 PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr,
9227 SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot,
9228 SLC.initprot, SLC.nsects, SLC.flags, Buf.size(),
9229 verbose);
9230 for (unsigned j = 0; j < SLC.nsects; j++) {
Kevin Enderbyc9713382014-12-16 01:14:45 +00009231 MachO::section S = Obj->getSection(Command, j);
Kevin Enderby956366c2014-08-29 22:30:52 +00009232 PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align,
9233 S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2,
9234 SLC.cmd, sg_segname, filetype, Buf.size(), verbose);
9235 }
9236 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
9237 MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command);
9238 const char *sg_segname = SLC_64.segname;
9239 PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname,
9240 SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff,
9241 SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot,
9242 SLC_64.nsects, SLC_64.flags, Buf.size(), verbose);
9243 for (unsigned j = 0; j < SLC_64.nsects; j++) {
9244 MachO::section_64 S_64 = Obj->getSection64(Command, j);
9245 PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size,
9246 S_64.offset, S_64.align, S_64.reloff, S_64.nreloc,
9247 S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd,
9248 sg_segname, filetype, Buf.size(), verbose);
9249 }
9250 } else if (Command.C.cmd == MachO::LC_SYMTAB) {
9251 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
David Majnemer73cc6ff2014-11-13 19:48:56 +00009252 PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size());
Kevin Enderby956366c2014-08-29 22:30:52 +00009253 } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
9254 MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
9255 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
David Majnemer73cc6ff2014-11-13 19:48:56 +00009256 PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(),
9257 Obj->is64Bit());
Kevin Enderby8ae63c12014-09-04 16:54:47 +00009258 } else if (Command.C.cmd == MachO::LC_DYLD_INFO ||
9259 Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
9260 MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command);
9261 PrintDyldInfoLoadCommand(DyldInfo, Buf.size());
9262 } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER ||
9263 Command.C.cmd == MachO::LC_ID_DYLINKER ||
9264 Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
9265 MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command);
9266 PrintDyldLoadCommand(Dyld, Command.Ptr);
9267 } else if (Command.C.cmd == MachO::LC_UUID) {
9268 MachO::uuid_command Uuid = Obj->getUuidCommand(Command);
9269 PrintUuidLoadCommand(Uuid);
Jean-Daniel Dupas00cc1f52014-12-04 07:37:02 +00009270 } else if (Command.C.cmd == MachO::LC_RPATH) {
9271 MachO::rpath_command Rpath = Obj->getRpathCommand(Command);
9272 PrintRpathLoadCommand(Rpath, Command.Ptr);
Kevin Enderby1ff0ecc2014-12-16 21:48:27 +00009273 } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX ||
Tim Northoverbfbfb122015-11-02 21:26:58 +00009274 Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS ||
9275 Command.C.cmd == MachO::LC_VERSION_MIN_TVOS ||
9276 Command.C.cmd == MachO::LC_VERSION_MIN_WATCHOS) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00009277 MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command);
9278 PrintVersionMinLoadCommand(Vd);
Kevin Enderbya4579c42017-01-19 17:36:31 +00009279 } else if (Command.C.cmd == MachO::LC_NOTE) {
9280 MachO::note_command Nt = Obj->getNoteLoadCommand(Command);
9281 PrintNoteLoadCommand(Nt);
Steven Wu5b54a422017-01-23 20:07:55 +00009282 } else if (Command.C.cmd == MachO::LC_BUILD_VERSION) {
9283 MachO::build_version_command Bv =
9284 Obj->getBuildVersionLoadCommand(Command);
9285 PrintBuildVersionLoadCommand(Obj, Bv);
Kevin Enderby8ae63c12014-09-04 16:54:47 +00009286 } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) {
9287 MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command);
9288 PrintSourceVersionCommand(Sd);
9289 } else if (Command.C.cmd == MachO::LC_MAIN) {
9290 MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command);
9291 PrintEntryPointCommand(Ep);
Kevin Enderby0804f4672014-12-16 23:25:52 +00009292 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00009293 MachO::encryption_info_command Ei =
9294 Obj->getEncryptionInfoCommand(Command);
Kevin Enderby0804f4672014-12-16 23:25:52 +00009295 PrintEncryptionInfoCommand(Ei, Buf.size());
Kevin Enderby57538292014-12-17 01:01:30 +00009296 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00009297 MachO::encryption_info_command_64 Ei =
9298 Obj->getEncryptionInfoCommand64(Command);
Kevin Enderby57538292014-12-17 01:01:30 +00009299 PrintEncryptionInfoCommand64(Ei, Buf.size());
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00009300 } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00009301 MachO::linker_option_command Lo =
9302 Obj->getLinkerOptionLoadCommand(Command);
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00009303 PrintLinkerOptionCommand(Lo, Command.Ptr);
Kevin Enderbyb4b79312014-12-18 19:24:35 +00009304 } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) {
9305 MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command);
9306 PrintSubFrameworkCommand(Sf, Command.Ptr);
Kevin Enderbya2bd8d92014-12-18 23:13:26 +00009307 } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) {
9308 MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command);
9309 PrintSubUmbrellaCommand(Sf, Command.Ptr);
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00009310 } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) {
9311 MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command);
9312 PrintSubLibraryCommand(Sl, Command.Ptr);
Kevin Enderby186eac32014-12-19 21:06:24 +00009313 } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) {
9314 MachO::sub_client_command Sc = Obj->getSubClientCommand(Command);
9315 PrintSubClientCommand(Sc, Command.Ptr);
Kevin Enderby52e4ce42014-12-19 22:25:22 +00009316 } else if (Command.C.cmd == MachO::LC_ROUTINES) {
9317 MachO::routines_command Rc = Obj->getRoutinesCommand(Command);
9318 PrintRoutinesCommand(Rc);
9319 } else if (Command.C.cmd == MachO::LC_ROUTINES_64) {
9320 MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command);
9321 PrintRoutinesCommand64(Rc);
Kevin Enderby48ef5342014-12-23 22:56:39 +00009322 } else if (Command.C.cmd == MachO::LC_THREAD ||
9323 Command.C.cmd == MachO::LC_UNIXTHREAD) {
9324 MachO::thread_command Tc = Obj->getThreadCommand(Command);
9325 PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype);
Nick Kledzik15558912014-10-16 18:58:20 +00009326 } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
9327 Command.C.cmd == MachO::LC_ID_DYLIB ||
9328 Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
9329 Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
9330 Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
9331 Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00009332 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
9333 PrintDylibCommand(Dl, Command.Ptr);
9334 } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE ||
9335 Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO ||
9336 Command.C.cmd == MachO::LC_FUNCTION_STARTS ||
9337 Command.C.cmd == MachO::LC_DATA_IN_CODE ||
9338 Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS ||
9339 Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
9340 MachO::linkedit_data_command Ld =
9341 Obj->getLinkeditDataLoadCommand(Command);
9342 PrintLinkEditDataCommand(Ld, Buf.size());
Kevin Enderby956366c2014-08-29 22:30:52 +00009343 } else {
9344 outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd)
9345 << ")\n";
9346 outs() << " cmdsize " << Command.C.cmdsize << "\n";
9347 // TODO: get and print the raw bytes of the load command.
9348 }
9349 // TODO: print all the other kinds of load commands.
Kevin Enderby956366c2014-08-29 22:30:52 +00009350 }
9351}
9352
Kevin Enderby0ae163f2016-01-13 00:25:36 +00009353static void PrintMachHeader(const MachOObjectFile *Obj, bool verbose) {
Kevin Enderbyb76d3862014-08-22 20:35:18 +00009354 if (Obj->is64Bit()) {
9355 MachO::mach_header_64 H_64;
9356 H_64 = Obj->getHeader64();
9357 PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
9358 H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
9359 } else {
9360 MachO::mach_header H;
9361 H = Obj->getHeader();
9362 PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
9363 H.sizeofcmds, H.flags, verbose);
9364 }
9365}
9366
9367void llvm::printMachOFileHeader(const object::ObjectFile *Obj) {
9368 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
Kevin Enderby0ae163f2016-01-13 00:25:36 +00009369 PrintMachHeader(file, !NonVerbose);
9370}
9371
9372void llvm::printMachOLoadCommands(const object::ObjectFile *Obj) {
9373 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
Kevin Enderby956366c2014-08-29 22:30:52 +00009374 uint32_t filetype = 0;
9375 uint32_t cputype = 0;
Kevin Enderby0ae163f2016-01-13 00:25:36 +00009376 if (file->is64Bit()) {
9377 MachO::mach_header_64 H_64;
9378 H_64 = file->getHeader64();
9379 filetype = H_64.filetype;
9380 cputype = H_64.cputype;
9381 } else {
9382 MachO::mach_header H;
9383 H = file->getHeader();
9384 filetype = H.filetype;
9385 cputype = H.cputype;
9386 }
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00009387 PrintLoadCommands(file, filetype, cputype, !NonVerbose);
Kevin Enderbyb76d3862014-08-22 20:35:18 +00009388}
Nick Kledzikd04bc352014-08-30 00:20:14 +00009389
9390//===----------------------------------------------------------------------===//
9391// export trie dumping
9392//===----------------------------------------------------------------------===//
9393
9394void llvm::printMachOExportsTrie(const object::MachOObjectFile *Obj) {
Kevin Enderby0d5ec112017-06-19 21:23:07 +00009395 uint64_t BaseSegmentAddress = 0;
9396 for (const auto &Command : Obj->load_commands()) {
9397 if (Command.C.cmd == MachO::LC_SEGMENT) {
9398 MachO::segment_command Seg = Obj->getSegmentLoadCommand(Command);
9399 if (Seg.fileoff == 0 && Seg.filesize != 0) {
9400 BaseSegmentAddress = Seg.vmaddr;
9401 break;
9402 }
9403 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
9404 MachO::segment_command_64 Seg = Obj->getSegment64LoadCommand(Command);
9405 if (Seg.fileoff == 0 && Seg.filesize != 0) {
9406 BaseSegmentAddress = Seg.vmaddr;
9407 break;
9408 }
9409 }
9410 }
Kevin Enderby3e95bd22017-07-20 23:08:41 +00009411 Error Err = Error::success();
Alexander Shaposhnikove5740342017-07-29 00:30:45 +00009412 for (const llvm::object::ExportEntry &Entry : Obj->exports(Err)) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009413 uint64_t Flags = Entry.flags();
Nick Kledzikd04bc352014-08-30 00:20:14 +00009414 bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
9415 bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
9416 bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
9417 MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL);
9418 bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
9419 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
9420 bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
9421 if (ReExport)
9422 outs() << "[re-export] ";
9423 else
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009424 outs() << format("0x%08llX ",
Kevin Enderby0d5ec112017-06-19 21:23:07 +00009425 Entry.address() + BaseSegmentAddress);
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009426 outs() << Entry.name();
Nick Kledzikd04bc352014-08-30 00:20:14 +00009427 if (WeakDef || ThreadLocal || Resolver || Abs) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009428 bool NeedsComma = false;
Nick Kledzik1d1ac4b2014-09-03 01:12:52 +00009429 outs() << " [";
Nick Kledzikd04bc352014-08-30 00:20:14 +00009430 if (WeakDef) {
9431 outs() << "weak_def";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009432 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00009433 }
9434 if (ThreadLocal) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009435 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00009436 outs() << ", ";
9437 outs() << "per-thread";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009438 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00009439 }
9440 if (Abs) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009441 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00009442 outs() << ", ";
9443 outs() << "absolute";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009444 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00009445 }
9446 if (Resolver) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009447 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00009448 outs() << ", ";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009449 outs() << format("resolver=0x%08llX", Entry.other());
9450 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00009451 }
9452 outs() << "]";
9453 }
9454 if (ReExport) {
9455 StringRef DylibName = "unknown";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009456 int Ordinal = Entry.other() - 1;
9457 Obj->getLibraryShortNameByIndex(Ordinal, DylibName);
9458 if (Entry.otherName().empty())
Nick Kledzikd04bc352014-08-30 00:20:14 +00009459 outs() << " (from " << DylibName << ")";
9460 else
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009461 outs() << " (" << Entry.otherName() << " from " << DylibName << ")";
Nick Kledzikd04bc352014-08-30 00:20:14 +00009462 }
9463 outs() << "\n";
9464 }
Kevin Enderby3e95bd22017-07-20 23:08:41 +00009465 if (Err)
9466 report_error(Obj->getFileName(), std::move(Err));
Nick Kledzikd04bc352014-08-30 00:20:14 +00009467}
Nick Kledzikac431442014-09-12 21:34:15 +00009468
Nick Kledzikac431442014-09-12 21:34:15 +00009469//===----------------------------------------------------------------------===//
9470// rebase table dumping
9471//===----------------------------------------------------------------------===//
9472
Kevin Enderbya8d256c2017-03-20 19:46:55 +00009473void llvm::printMachORebaseTable(object::MachOObjectFile *Obj) {
Nick Kledzikac431442014-09-12 21:34:15 +00009474 outs() << "segment section address type\n";
Kevin Enderbya8d256c2017-03-20 19:46:55 +00009475 Error Err = Error::success();
9476 for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable(Err)) {
9477 StringRef SegmentName = Entry.segmentName();
9478 StringRef SectionName = Entry.sectionName();
9479 uint64_t Address = Entry.address();
Nick Kledzikac431442014-09-12 21:34:15 +00009480
9481 // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009482 outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n",
9483 SegmentName.str().c_str(), SectionName.str().c_str(),
9484 Address, Entry.typeName().str().c_str());
Nick Kledzikac431442014-09-12 21:34:15 +00009485 }
Kevin Enderbya8d256c2017-03-20 19:46:55 +00009486 if (Err)
9487 report_error(Obj->getFileName(), std::move(Err));
Nick Kledzikac431442014-09-12 21:34:15 +00009488}
Nick Kledzik56ebef42014-09-16 01:41:51 +00009489
9490static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
9491 StringRef DylibName;
9492 switch (Ordinal) {
9493 case MachO::BIND_SPECIAL_DYLIB_SELF:
9494 return "this-image";
9495 case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
9496 return "main-executable";
9497 case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
9498 return "flat-namespace";
9499 default:
Nick Kledzikabd29872014-09-16 22:03:13 +00009500 if (Ordinal > 0) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009501 std::error_code EC =
9502 Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName);
Nick Kledzikabd29872014-09-16 22:03:13 +00009503 if (EC)
Nick Kledzik51d2c2b2014-10-14 23:29:38 +00009504 return "<<bad library ordinal>>";
Nick Kledzikabd29872014-09-16 22:03:13 +00009505 return DylibName;
9506 }
Nick Kledzik56ebef42014-09-16 01:41:51 +00009507 }
Nick Kledzikabd29872014-09-16 22:03:13 +00009508 return "<<unknown special ordinal>>";
Nick Kledzik56ebef42014-09-16 01:41:51 +00009509}
9510
9511//===----------------------------------------------------------------------===//
9512// bind table dumping
9513//===----------------------------------------------------------------------===//
9514
Kevin Enderbya8d256c2017-03-20 19:46:55 +00009515void llvm::printMachOBindTable(object::MachOObjectFile *Obj) {
Nick Kledzik56ebef42014-09-16 01:41:51 +00009516 // Build table of sections so names can used in final output.
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009517 outs() << "segment section address type "
9518 "addend dylib symbol\n";
Kevin Enderbyfeb63b92017-02-28 21:47:07 +00009519 Error Err = Error::success();
9520 for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable(Err)) {
Kevin Enderbya8d256c2017-03-20 19:46:55 +00009521 StringRef SegmentName = Entry.segmentName();
9522 StringRef SectionName = Entry.sectionName();
9523 uint64_t Address = Entry.address();
Nick Kledzik56ebef42014-09-16 01:41:51 +00009524
9525 // Table lines look like:
9526 // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009527 StringRef Attr;
Nick Kledzik56ebef42014-09-16 01:41:51 +00009528 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT)
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009529 Attr = " (weak_import)";
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009530 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009531 << left_justify(SectionName, 18) << " "
9532 << format_hex(Address, 10, true) << " "
9533 << left_justify(Entry.typeName(), 8) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009534 << format_decimal(Entry.addend(), 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009535 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009536 << Entry.symbolName() << Attr << "\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00009537 }
Kevin Enderbyfeb63b92017-02-28 21:47:07 +00009538 if (Err)
9539 report_error(Obj->getFileName(), std::move(Err));
Nick Kledzik56ebef42014-09-16 01:41:51 +00009540}
9541
9542//===----------------------------------------------------------------------===//
9543// lazy bind table dumping
9544//===----------------------------------------------------------------------===//
9545
Kevin Enderbya8d256c2017-03-20 19:46:55 +00009546void llvm::printMachOLazyBindTable(object::MachOObjectFile *Obj) {
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009547 outs() << "segment section address "
9548 "dylib symbol\n";
Kevin Enderbyfeb63b92017-02-28 21:47:07 +00009549 Error Err = Error::success();
9550 for (const llvm::object::MachOBindEntry &Entry : Obj->lazyBindTable(Err)) {
Kevin Enderbya8d256c2017-03-20 19:46:55 +00009551 StringRef SegmentName = Entry.segmentName();
9552 StringRef SectionName = Entry.sectionName();
9553 uint64_t Address = Entry.address();
Nick Kledzik56ebef42014-09-16 01:41:51 +00009554
9555 // Table lines look like:
9556 // __DATA __got 0x00012010 libSystem ___stack_chk_guard
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009557 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009558 << left_justify(SectionName, 18) << " "
9559 << format_hex(Address, 10, true) << " "
9560 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
Nick Kledzik56ebef42014-09-16 01:41:51 +00009561 << Entry.symbolName() << "\n";
9562 }
Kevin Enderbyfeb63b92017-02-28 21:47:07 +00009563 if (Err)
9564 report_error(Obj->getFileName(), std::move(Err));
Nick Kledzik56ebef42014-09-16 01:41:51 +00009565}
9566
Nick Kledzik56ebef42014-09-16 01:41:51 +00009567//===----------------------------------------------------------------------===//
9568// weak bind table dumping
9569//===----------------------------------------------------------------------===//
9570
Kevin Enderbya8d256c2017-03-20 19:46:55 +00009571void llvm::printMachOWeakBindTable(object::MachOObjectFile *Obj) {
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009572 outs() << "segment section address "
9573 "type addend symbol\n";
Kevin Enderbyfeb63b92017-02-28 21:47:07 +00009574 Error Err = Error::success();
9575 for (const llvm::object::MachOBindEntry &Entry : Obj->weakBindTable(Err)) {
Nick Kledzik56ebef42014-09-16 01:41:51 +00009576 // Strong symbols don't have a location to update.
9577 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009578 outs() << " strong "
Nick Kledzik56ebef42014-09-16 01:41:51 +00009579 << Entry.symbolName() << "\n";
9580 continue;
9581 }
Kevin Enderbya8d256c2017-03-20 19:46:55 +00009582 StringRef SegmentName = Entry.segmentName();
9583 StringRef SectionName = Entry.sectionName();
9584 uint64_t Address = Entry.address();
Nick Kledzik56ebef42014-09-16 01:41:51 +00009585
9586 // Table lines look like:
9587 // __DATA __data 0x00001000 pointer 0 _foo
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009588 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009589 << left_justify(SectionName, 18) << " "
9590 << format_hex(Address, 10, true) << " "
9591 << left_justify(Entry.typeName(), 8) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009592 << format_decimal(Entry.addend(), 8) << " " << Entry.symbolName()
9593 << "\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00009594 }
Kevin Enderbyfeb63b92017-02-28 21:47:07 +00009595 if (Err)
9596 report_error(Obj->getFileName(), std::move(Err));
Nick Kledzik56ebef42014-09-16 01:41:51 +00009597}
9598
Kevin Enderby6f326ce2014-10-23 19:37:31 +00009599// get_dyld_bind_info_symbolname() is used for disassembly and passed an
9600// address, ReferenceValue, in the Mach-O file and looks in the dyld bind
9601// information for that address. If the address is found its binding symbol
9602// name is returned. If not nullptr is returned.
9603static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
9604 struct DisassembleInfo *info) {
Kevin Enderby078be602014-10-23 19:53:12 +00009605 if (info->bindtable == nullptr) {
Saleem Abdulrasool1d84d9a2017-01-08 19:14:15 +00009606 info->bindtable = llvm::make_unique<SymbolAddressMap>();
Kevin Enderbyfeb63b92017-02-28 21:47:07 +00009607 Error Err = Error::success();
9608 for (const llvm::object::MachOBindEntry &Entry : info->O->bindTable(Err)) {
Kevin Enderbya8d256c2017-03-20 19:46:55 +00009609 uint64_t Address = Entry.address();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00009610 StringRef name = Entry.symbolName();
9611 if (!name.empty())
Saleem Abdulrasool1d84d9a2017-01-08 19:14:15 +00009612 (*info->bindtable)[Address] = name;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00009613 }
Kevin Enderbyfeb63b92017-02-28 21:47:07 +00009614 if (Err)
9615 report_error(info->O->getFileName(), std::move(Err));
Kevin Enderby6f326ce2014-10-23 19:37:31 +00009616 }
Saleem Abdulrasool1d84d9a2017-01-08 19:14:15 +00009617 auto name = info->bindtable->lookup(ReferenceValue);
9618 return !name.empty() ? name.data() : nullptr;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00009619}