blob: 69e95b4a29e751db2a2156de1e8f38efcdbeb0f0 [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
Francis Ricci195a66f2017-10-06 15:54:20 +0000205#ifdef HAVE_LIBXAR
Francis Ricci6f942972017-10-06 15:33:28 +0000206namespace {
207struct ScopedXarFile {
208 xar_t xar;
Francis Ricci57d84522017-10-09 20:27:14 +0000209 ScopedXarFile(const char *filename, int32_t flags)
210 : xar(xar_open(filename, flags)) {}
Francis Ricci6f942972017-10-06 15:33:28 +0000211 ~ScopedXarFile() {
212 if (xar)
213 xar_close(xar);
214 }
215 ScopedXarFile(const ScopedXarFile &) = delete;
216 ScopedXarFile &operator=(const ScopedXarFile &) = delete;
217 operator xar_t() { return xar; }
218};
219
220struct ScopedXarIter {
221 xar_iter_t iter;
Francis Ricci57d84522017-10-09 20:27:14 +0000222 ScopedXarIter() : iter(xar_iter_new()) {}
Francis Ricci6f942972017-10-06 15:33:28 +0000223 ~ScopedXarIter() {
224 if (iter)
225 xar_iter_free(iter);
226 }
227 ScopedXarIter(const ScopedXarIter &) = delete;
228 ScopedXarIter &operator=(const ScopedXarIter &) = delete;
229 operator xar_iter_t() { return iter; }
230};
231} // namespace
Francis Ricci195a66f2017-10-06 15:54:20 +0000232#endif // defined(HAVE_LIBXAR)
Francis Ricci6f942972017-10-06 15:33:28 +0000233
Kevin Enderby930fdc72014-11-06 19:00:13 +0000234// This is used to search for a data in code table entry for the PC being
235// disassembled. The j parameter has the PC in j.first. A single data in code
236// table entry can cover many bytes for each of its Kind's. So if the offset,
237// aka the i.first value, of the data in code table entry plus its Length
238// covers the PC being searched for this will return true. If not it will
239// return false.
David Majnemerea9b8ee2014-11-04 08:41:48 +0000240static bool compareDiceTableEntries(const DiceTableEntry &i,
241 const DiceTableEntry &j) {
Kevin Enderby930fdc72014-11-06 19:00:13 +0000242 uint16_t Length;
243 i.second.getLength(Length);
244
245 return j.first >= i.first && j.first < i.first + Length;
Kevin Enderby273ae012013-06-06 17:20:50 +0000246}
247
Colin LeMahieufc32b1b2015-03-18 19:27:31 +0000248static uint64_t DumpDataInCode(const uint8_t *bytes, uint64_t Length,
Kevin Enderby930fdc72014-11-06 19:00:13 +0000249 unsigned short Kind) {
250 uint32_t Value, Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000251
252 switch (Kind) {
Kevin Enderby930fdc72014-11-06 19:00:13 +0000253 default:
Charles Davis8bdfafd2013-09-01 04:28:48 +0000254 case MachO::DICE_KIND_DATA:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000255 if (Length >= 4) {
256 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000257 dumpBytes(makeArrayRef(bytes, 4), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000258 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
Kevin Enderby273ae012013-06-06 17:20:50 +0000259 outs() << "\t.long " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000260 Size = 4;
261 } else if (Length >= 2) {
262 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000263 dumpBytes(makeArrayRef(bytes, 2), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000264 Value = bytes[1] << 8 | bytes[0];
Kevin Enderby273ae012013-06-06 17:20:50 +0000265 outs() << "\t.short " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000266 Size = 2;
267 } else {
268 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000269 dumpBytes(makeArrayRef(bytes, 2), outs());
Kevin Enderby273ae012013-06-06 17:20:50 +0000270 Value = bytes[0];
271 outs() << "\t.byte " << Value;
Kevin Enderby930fdc72014-11-06 19:00:13 +0000272 Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000273 }
Kevin Enderby930fdc72014-11-06 19:00:13 +0000274 if (Kind == MachO::DICE_KIND_DATA)
275 outs() << "\t@ KIND_DATA\n";
276 else
277 outs() << "\t@ data in code kind = " << Kind << "\n";
Kevin Enderby273ae012013-06-06 17:20:50 +0000278 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000279 case MachO::DICE_KIND_JUMP_TABLE8:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000280 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000281 dumpBytes(makeArrayRef(bytes, 1), outs());
Kevin Enderby273ae012013-06-06 17:20:50 +0000282 Value = bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000283 outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n";
284 Size = 1;
Kevin Enderby273ae012013-06-06 17:20:50 +0000285 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000286 case MachO::DICE_KIND_JUMP_TABLE16:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000287 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000288 dumpBytes(makeArrayRef(bytes, 2), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000289 Value = bytes[1] << 8 | bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000290 outs() << "\t.short " << format("%5u", Value & 0xffff)
291 << "\t@ KIND_JUMP_TABLE16\n";
292 Size = 2;
Kevin Enderby273ae012013-06-06 17:20:50 +0000293 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000294 case MachO::DICE_KIND_JUMP_TABLE32:
Kevin Enderby930fdc72014-11-06 19:00:13 +0000295 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
296 if (!NoShowRawInsn)
Craig Topper0013be12015-09-21 05:32:41 +0000297 dumpBytes(makeArrayRef(bytes, 4), outs());
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000298 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
Kevin Enderby930fdc72014-11-06 19:00:13 +0000299 outs() << "\t.long " << Value;
300 if (Kind == MachO::DICE_KIND_JUMP_TABLE32)
301 outs() << "\t@ KIND_JUMP_TABLE32\n";
302 else
303 outs() << "\t@ KIND_ABS_JUMP_TABLE32\n";
304 Size = 4;
Kevin Enderby273ae012013-06-06 17:20:50 +0000305 break;
306 }
Kevin Enderby930fdc72014-11-06 19:00:13 +0000307 return Size;
Kevin Enderby273ae012013-06-06 17:20:50 +0000308}
309
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000310static void getSectionsAndSymbols(MachOObjectFile *MachOObj,
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000311 std::vector<SectionRef> &Sections,
312 std::vector<SymbolRef> &Symbols,
313 SmallVectorImpl<uint64_t> &FoundFns,
314 uint64_t &BaseSegmentAddress) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000315 for (const SymbolRef &Symbol : MachOObj->symbols()) {
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000316 Expected<StringRef> SymName = Symbol.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +0000317 if (!SymName)
318 report_error(MachOObj->getFileName(), SymName.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000319 if (!SymName->startswith("ltmp"))
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000320 Symbols.push_back(Symbol);
321 }
Owen Andersond9243c42011-10-17 21:37:35 +0000322
Alexey Samsonov48803e52014-03-13 14:37:36 +0000323 for (const SectionRef &Section : MachOObj->sections()) {
Owen Andersond9243c42011-10-17 21:37:35 +0000324 StringRef SectName;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000325 Section.getName(SectName);
326 Sections.push_back(Section);
Owen Andersond9243c42011-10-17 21:37:35 +0000327 }
328
Kevin Enderby273ae012013-06-06 17:20:50 +0000329 bool BaseSegmentAddressSet = false;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000330 for (const auto &Command : MachOObj->load_commands()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000331 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
Benjamin Kramer699128e2011-09-21 01:13:19 +0000332 // We found a function starts segment, parse the addresses for later
333 // consumption.
Charles Davis8bdfafd2013-09-01 04:28:48 +0000334 MachO::linkedit_data_command LLC =
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000335 MachOObj->getLinkeditDataLoadCommand(Command);
Benjamin Kramer699128e2011-09-21 01:13:19 +0000336
Charles Davis8bdfafd2013-09-01 04:28:48 +0000337 MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000338 } else if (Command.C.cmd == MachO::LC_SEGMENT) {
339 MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000340 StringRef SegName = SLC.segname;
Kevin Enderbyb28ed012014-10-29 21:28:24 +0000341 if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
Kevin Enderby273ae012013-06-06 17:20:50 +0000342 BaseSegmentAddressSet = true;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000343 BaseSegmentAddress = SLC.vmaddr;
Kevin Enderby273ae012013-06-06 17:20:50 +0000344 }
345 }
Benjamin Kramer8a529dc2011-09-21 22:16:43 +0000346 }
Benjamin Kramer699128e2011-09-21 01:13:19 +0000347}
348
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000349static void PrintIndirectSymbolTable(MachOObjectFile *O, bool verbose,
350 uint32_t n, uint32_t count,
351 uint32_t stride, uint64_t addr) {
352 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
353 uint32_t nindirectsyms = Dysymtab.nindirectsyms;
354 if (n > nindirectsyms)
355 outs() << " (entries start past the end of the indirect symbol "
356 "table) (reserved1 field greater than the table size)";
357 else if (n + count > nindirectsyms)
358 outs() << " (entries extends past the end of the indirect symbol "
359 "table)";
360 outs() << "\n";
361 uint32_t cputype = O->getHeader().cputype;
362 if (cputype & MachO::CPU_ARCH_ABI64)
363 outs() << "address index";
364 else
365 outs() << "address index";
366 if (verbose)
367 outs() << " name\n";
368 else
369 outs() << "\n";
370 for (uint32_t j = 0; j < count && n + j < nindirectsyms; j++) {
371 if (cputype & MachO::CPU_ARCH_ABI64)
372 outs() << format("0x%016" PRIx64, addr + j * stride) << " ";
373 else
Tim Northover43978372016-04-26 18:29:16 +0000374 outs() << format("0x%08" PRIx32, (uint32_t)addr + j * stride) << " ";
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000375 MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
376 uint32_t indirect_symbol = O->getIndirectSymbolTableEntry(Dysymtab, n + j);
377 if (indirect_symbol == MachO::INDIRECT_SYMBOL_LOCAL) {
378 outs() << "LOCAL\n";
379 continue;
380 }
381 if (indirect_symbol ==
382 (MachO::INDIRECT_SYMBOL_LOCAL | MachO::INDIRECT_SYMBOL_ABS)) {
383 outs() << "LOCAL ABSOLUTE\n";
384 continue;
385 }
386 if (indirect_symbol == MachO::INDIRECT_SYMBOL_ABS) {
387 outs() << "ABSOLUTE\n";
388 continue;
389 }
390 outs() << format("%5u ", indirect_symbol);
Kevin Enderbyf0640752015-03-13 17:56:32 +0000391 if (verbose) {
392 MachO::symtab_command Symtab = O->getSymtabLoadCommand();
393 if (indirect_symbol < Symtab.nsyms) {
394 symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol);
395 SymbolRef Symbol = *Sym;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000396 Expected<StringRef> SymName = Symbol.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +0000397 if (!SymName)
398 report_error(O->getFileName(), SymName.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000399 outs() << *SymName;
Kevin Enderbyf0640752015-03-13 17:56:32 +0000400 } else {
401 outs() << "?";
402 }
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000403 }
404 outs() << "\n";
405 }
406}
407
408static void PrintIndirectSymbols(MachOObjectFile *O, bool verbose) {
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000409 for (const auto &Load : O->load_commands()) {
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000410 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
411 MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
412 for (unsigned J = 0; J < Seg.nsects; ++J) {
413 MachO::section_64 Sec = O->getSection64(Load, J);
414 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
415 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
416 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
417 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
418 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
419 section_type == MachO::S_SYMBOL_STUBS) {
420 uint32_t stride;
421 if (section_type == MachO::S_SYMBOL_STUBS)
422 stride = Sec.reserved2;
423 else
424 stride = 8;
425 if (stride == 0) {
426 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
427 << Sec.sectname << ") "
428 << "(size of stubs in reserved2 field is zero)\n";
429 continue;
430 }
431 uint32_t count = Sec.size / stride;
432 outs() << "Indirect symbols for (" << Sec.segname << ","
433 << Sec.sectname << ") " << count << " entries";
434 uint32_t n = Sec.reserved1;
435 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
436 }
437 }
438 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
439 MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
440 for (unsigned J = 0; J < Seg.nsects; ++J) {
441 MachO::section Sec = O->getSection(Load, J);
442 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
443 if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
444 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
445 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
446 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
447 section_type == MachO::S_SYMBOL_STUBS) {
448 uint32_t stride;
449 if (section_type == MachO::S_SYMBOL_STUBS)
450 stride = Sec.reserved2;
451 else
452 stride = 4;
453 if (stride == 0) {
454 outs() << "Can't print indirect symbols for (" << Sec.segname << ","
455 << Sec.sectname << ") "
456 << "(size of stubs in reserved2 field is zero)\n";
457 continue;
458 }
459 uint32_t count = Sec.size / stride;
460 outs() << "Indirect symbols for (" << Sec.segname << ","
461 << Sec.sectname << ") " << count << " entries";
462 uint32_t n = Sec.reserved1;
463 PrintIndirectSymbolTable(O, verbose, n, count, stride, Sec.addr);
464 }
465 }
466 }
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000467 }
468}
469
Michael Trenta1703b12017-12-15 17:57:40 +0000470static void PrintRType(const uint64_t cputype, const unsigned r_type) {
471 static char const *generic_r_types[] = {
472 "VANILLA ", "PAIR ", "SECTDIF ", "PBLAPTR ", "LOCSDIF ", "TLV ",
473 " 6 (?) ", " 7 (?) ", " 8 (?) ", " 9 (?) ", " 10 (?) ", " 11 (?) ",
474 " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
475 };
476 static char const *x86_64_r_types[] = {
477 "UNSIGND ", "SIGNED ", "BRANCH ", "GOT_LD ", "GOT ", "SUB ",
478 "SIGNED1 ", "SIGNED2 ", "SIGNED4 ", "TLV ", " 10 (?) ", " 11 (?) ",
479 " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
480 };
481 static char const *arm_r_types[] = {
482 "VANILLA ", "PAIR ", "SECTDIFF", "LOCSDIF ", "PBLAPTR ",
483 "BR24 ", "T_BR22 ", "T_BR32 ", "HALF ", "HALFDIF ",
484 " 10 (?) ", " 11 (?) ", " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
485 };
486 static char const *arm64_r_types[] = {
487 "UNSIGND ", "SUB ", "BR26 ", "PAGE21 ", "PAGOF12 ",
488 "GOTLDP ", "GOTLDPOF", "PTRTGOT ", "TLVLDP ", "TLVLDPOF",
489 "ADDEND ", " 11 (?) ", " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) "
490 };
491
492 if (r_type > 0xf){
493 outs() << format("%-7u", r_type) << " ";
494 return;
495 }
496 switch (cputype) {
497 case MachO::CPU_TYPE_I386:
498 outs() << generic_r_types[r_type];
499 break;
500 case MachO::CPU_TYPE_X86_64:
501 outs() << x86_64_r_types[r_type];
502 break;
503 case MachO::CPU_TYPE_ARM:
504 outs() << arm_r_types[r_type];
505 break;
506 case MachO::CPU_TYPE_ARM64:
507 outs() << arm64_r_types[r_type];
508 break;
509 default:
510 outs() << format("%-7u ", r_type);
511 }
512}
513
514static void PrintRLength(const uint64_t cputype, const unsigned r_type,
515 const unsigned r_length, const bool previous_arm_half){
516 if (cputype == MachO::CPU_TYPE_ARM &&
517 (r_type == llvm::MachO::ARM_RELOC_HALF ||
518 r_type == llvm::MachO::ARM_RELOC_HALF_SECTDIFF ||
519 previous_arm_half == true)) {
520 if ((r_length & 0x1) == 0)
521 outs() << "lo/";
522 else
523 outs() << "hi/";
524 if ((r_length & 0x1) == 0)
525 outs() << "arm ";
526 else
527 outs() << "thm ";
528 } else {
529 switch (r_length) {
530 case 0:
531 outs() << "byte ";
532 break;
533 case 1:
534 outs() << "word ";
535 break;
536 case 2:
537 outs() << "long ";
538 break;
539 case 3:
540 if (cputype == MachO::CPU_TYPE_X86_64)
541 outs() << "quad ";
542 else
543 outs() << format("?(%2d) ", r_length);
544 break;
545 default:
546 outs() << format("?(%2d) ", r_length);
547 }
548 }
549}
550
551static void PrintRelocationEntries(const MachOObjectFile *O,
552 const relocation_iterator Begin,
553 const relocation_iterator End,
554 const uint64_t cputype,
555 const bool verbose) {
556 const MachO::symtab_command Symtab = O->getSymtabLoadCommand();
557 bool previous_arm_half = false;
558 bool previous_sectdiff = false;
559 uint32_t sectdiff_r_type = 0;
560
561 for (relocation_iterator Reloc = Begin; Reloc != End; ++Reloc) {
562 const DataRefImpl Rel = Reloc->getRawDataRefImpl();
563 const MachO::any_relocation_info RE = O->getRelocation(Rel);
564 const unsigned r_type = O->getAnyRelocationType(RE);
565 const bool r_scattered = O->isRelocationScattered(RE);
566 const unsigned r_pcrel = O->getAnyRelocationPCRel(RE);
567 const unsigned r_length = O->getAnyRelocationLength(RE);
568 const unsigned r_address = O->getAnyRelocationAddress(RE);
569 const bool r_extern = (r_scattered ? false :
570 O->getPlainRelocationExternal(RE));
571 const uint32_t r_value = (r_scattered ?
572 O->getScatteredRelocationValue(RE) : 0);
573 const unsigned r_symbolnum = (r_scattered ? 0 :
574 O->getPlainRelocationSymbolNum(RE));
575
576 if (r_scattered && cputype != MachO::CPU_TYPE_X86_64) {
577 if (verbose) {
578 // scattered: address
579 if ((cputype == MachO::CPU_TYPE_I386 &&
580 r_type == llvm::MachO::GENERIC_RELOC_PAIR) ||
581 (cputype == MachO::CPU_TYPE_ARM &&
582 r_type == llvm::MachO::ARM_RELOC_PAIR))
583 outs() << " ";
584 else
585 outs() << format("%08x ", (unsigned int)r_address);
586
587 // scattered: pcrel
588 if (r_pcrel)
589 outs() << "True ";
590 else
591 outs() << "False ";
592
593 // scattered: length
594 PrintRLength(cputype, r_type, r_length, previous_arm_half);
595
596 // scattered: extern & type
597 outs() << "n/a ";
598 PrintRType(cputype, r_type);
599
600 // scattered: scattered & value
601 outs() << format("True 0x%08x", (unsigned int)r_value);
602 if (previous_sectdiff == false) {
603 if ((cputype == MachO::CPU_TYPE_ARM &&
604 r_type == llvm::MachO::ARM_RELOC_PAIR))
605 outs() << format(" half = 0x%04x ", (unsigned int)r_address);
606 }
607 else if (cputype == MachO::CPU_TYPE_ARM &&
608 sectdiff_r_type == llvm::MachO::ARM_RELOC_HALF_SECTDIFF)
609 outs() << format(" other_half = 0x%04x ", (unsigned int)r_address);
610 if ((cputype == MachO::CPU_TYPE_I386 &&
611 (r_type == llvm::MachO::GENERIC_RELOC_SECTDIFF ||
612 r_type == llvm::MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) ||
613 (cputype == MachO::CPU_TYPE_ARM &&
614 (sectdiff_r_type == llvm::MachO::ARM_RELOC_SECTDIFF ||
615 sectdiff_r_type == llvm::MachO::ARM_RELOC_LOCAL_SECTDIFF ||
616 sectdiff_r_type == llvm::MachO::ARM_RELOC_HALF_SECTDIFF))) {
617 previous_sectdiff = true;
618 sectdiff_r_type = r_type;
619 }
620 else {
621 previous_sectdiff = false;
622 sectdiff_r_type = 0;
623 }
624 if (cputype == MachO::CPU_TYPE_ARM &&
625 (r_type == llvm::MachO::ARM_RELOC_HALF ||
626 r_type == llvm::MachO::ARM_RELOC_HALF_SECTDIFF))
627 previous_arm_half = true;
628 else
629 previous_arm_half = false;
630 outs() << "\n";
631 }
632 else {
633 // scattered: address pcrel length extern type scattered value
634 outs() << format("%08x %1d %-2d n/a %-7d 1 0x%08x\n",
635 (unsigned int)r_address, r_pcrel, r_length, r_type,
636 (unsigned int)r_value);
637 }
638 }
639 else {
640 if (verbose) {
641 // plain: address
642 if (cputype == MachO::CPU_TYPE_ARM &&
643 r_type == llvm::MachO::ARM_RELOC_PAIR)
644 outs() << " ";
645 else
646 outs() << format("%08x ", (unsigned int)r_address);
647
648 // plain: pcrel
649 if (r_pcrel)
650 outs() << "True ";
651 else
652 outs() << "False ";
653
654 // plain: length
655 PrintRLength(cputype, r_type, r_length, previous_arm_half);
656
657 if (r_extern) {
658 // plain: extern & type & scattered
659 outs() << "True ";
660 PrintRType(cputype, r_type);
661 outs() << "False ";
662
663 // plain: symbolnum/value
664 if (r_symbolnum > Symtab.nsyms)
665 outs() << format("?(%d)\n", r_symbolnum);
666 else {
667 SymbolRef Symbol = *O->getSymbolByIndex(r_symbolnum);
668 Expected<StringRef> SymNameNext = Symbol.getName();
669 const char *name = NULL;
670 if (SymNameNext)
671 name = SymNameNext->data();
672 if (name == NULL)
673 outs() << format("?(%d)\n", r_symbolnum);
674 else
675 outs() << name << "\n";
676 }
677 }
678 else {
679 // plain: extern & type & scattered
680 outs() << "False ";
681 PrintRType(cputype, r_type);
682 outs() << "False ";
683
684 // plain: symbolnum/value
685 if (cputype == MachO::CPU_TYPE_ARM &&
686 r_type == llvm::MachO::ARM_RELOC_PAIR)
687 outs() << format("other_half = 0x%04x\n", (unsigned int)r_address);
688 else if (cputype == MachO::CPU_TYPE_ARM64 &&
689 r_type == llvm::MachO::ARM64_RELOC_ADDEND)
690 outs() << format("addend = 0x%06x\n", (unsigned int)r_symbolnum);
691 else {
692 outs() << format("%d ", r_symbolnum);
693 if (r_symbolnum == llvm::MachO::R_ABS)
694 outs() << "R_ABS\n";
695 else {
696 // in this case, r_symbolnum is actually a 1-based section number
697 uint32_t nsects = O->section_end()->getRawDataRefImpl().d.a;
698 if (r_symbolnum > 0 && r_symbolnum <= nsects) {
699 llvm::object::DataRefImpl DRI;
700 DRI.d.a = r_symbolnum-1;
701 StringRef SegName = O->getSectionFinalSegmentName(DRI);
702 StringRef SectName;
703 if (O->getSectionName(DRI, SectName))
704 outs() << "(?,?)\n";
705 else
706 outs() << "(" << SegName << "," << SectName << ")\n";
707 }
708 else {
709 outs() << "(?,?)\n";
710 }
711 }
712 }
713 }
714 if (cputype == MachO::CPU_TYPE_ARM &&
715 (r_type == llvm::MachO::ARM_RELOC_HALF ||
716 r_type == llvm::MachO::ARM_RELOC_HALF_SECTDIFF))
717 previous_arm_half = true;
718 else
719 previous_arm_half = false;
720 }
721 else {
722 // plain: address pcrel length extern type scattered symbolnum/section
723 outs() << format("%08x %1d %-2d %1d %-7d 0 %d\n",
724 (unsigned int)r_address, r_pcrel, r_length, r_extern,
725 r_type, r_symbolnum);
726 }
727 }
728 }
729}
730
731static void PrintRelocations(const MachOObjectFile *O, const bool verbose) {
732 const uint64_t cputype = O->getHeader().cputype;
733 const MachO::dysymtab_command Dysymtab = O->getDysymtabLoadCommand();
734 if (Dysymtab.nextrel != 0) {
735 outs() << "External relocation information " << Dysymtab.nextrel
736 << " entries";
737 outs() << "\naddress pcrel length extern type scattered "
738 "symbolnum/value\n";
739 PrintRelocationEntries(O, O->extrel_begin(), O->extrel_end(), cputype,
740 verbose);
741 }
742 if (Dysymtab.nlocrel != 0) {
743 outs() << format("Local relocation information %u entries",
744 Dysymtab.nlocrel);
745 outs() << "\naddress pcrel length extern type scattered "
746 "symbolnum/value\n";
747 PrintRelocationEntries(O, O->locrel_begin(), O->locrel_end(), cputype,
748 verbose);
749 }
750 for (const auto &Load : O->load_commands()) {
751 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
752 const MachO::segment_command_64 Seg = O->getSegment64LoadCommand(Load);
753 for (unsigned J = 0; J < Seg.nsects; ++J) {
754 const MachO::section_64 Sec = O->getSection64(Load, J);
755 if (Sec.nreloc != 0) {
756 DataRefImpl DRI;
757 DRI.d.a = J;
758 const StringRef SegName = O->getSectionFinalSegmentName(DRI);
759 StringRef SectName;
760 if (O->getSectionName(DRI, SectName))
761 outs() << "Relocation information (" << SegName << ",?) "
762 << format("%u entries", Sec.nreloc);
763 else
764 outs() << "Relocation information (" << SegName << ","
765 << SectName << format(") %u entries", Sec.nreloc);
766 outs() << "\naddress pcrel length extern type scattered "
767 "symbolnum/value\n";
768 PrintRelocationEntries(O, O->section_rel_begin(DRI),
769 O->section_rel_end(DRI), cputype, verbose);
770 }
771 }
772 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
773 const MachO::segment_command Seg = O->getSegmentLoadCommand(Load);
774 for (unsigned J = 0; J < Seg.nsects; ++J) {
775 const MachO::section Sec = O->getSection(Load, J);
776 if (Sec.nreloc != 0) {
777 DataRefImpl DRI;
778 DRI.d.a = J;
779 const StringRef SegName = O->getSectionFinalSegmentName(DRI);
780 StringRef SectName;
781 if (O->getSectionName(DRI, SectName))
782 outs() << "Relocation information (" << SegName << ",?) "
783 << format("%u entries", Sec.nreloc);
784 else
785 outs() << "Relocation information (" << SegName << ","
786 << SectName << format(") %u entries", Sec.nreloc);
787 outs() << "\naddress pcrel length extern type scattered "
788 "symbolnum/value\n";
789 PrintRelocationEntries(O, O->section_rel_begin(DRI),
790 O->section_rel_end(DRI), cputype, verbose);
791 }
792 }
793 }
794 }
795}
796
Kevin Enderby69fe98d2015-01-23 18:52:17 +0000797static void PrintDataInCodeTable(MachOObjectFile *O, bool verbose) {
798 MachO::linkedit_data_command DIC = O->getDataInCodeLoadCommand();
799 uint32_t nentries = DIC.datasize / sizeof(struct MachO::data_in_code_entry);
800 outs() << "Data in code table (" << nentries << " entries)\n";
801 outs() << "offset length kind\n";
802 for (dice_iterator DI = O->begin_dices(), DE = O->end_dices(); DI != DE;
803 ++DI) {
804 uint32_t Offset;
805 DI->getOffset(Offset);
806 outs() << format("0x%08" PRIx32, Offset) << " ";
807 uint16_t Length;
808 DI->getLength(Length);
809 outs() << format("%6u", Length) << " ";
810 uint16_t Kind;
811 DI->getKind(Kind);
812 if (verbose) {
813 switch (Kind) {
814 case MachO::DICE_KIND_DATA:
815 outs() << "DATA";
816 break;
817 case MachO::DICE_KIND_JUMP_TABLE8:
818 outs() << "JUMP_TABLE8";
819 break;
820 case MachO::DICE_KIND_JUMP_TABLE16:
821 outs() << "JUMP_TABLE16";
822 break;
823 case MachO::DICE_KIND_JUMP_TABLE32:
824 outs() << "JUMP_TABLE32";
825 break;
826 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
827 outs() << "ABS_JUMP_TABLE32";
828 break;
829 default:
830 outs() << format("0x%04" PRIx32, Kind);
831 break;
832 }
833 } else
834 outs() << format("0x%04" PRIx32, Kind);
835 outs() << "\n";
836 }
837}
838
Kevin Enderby9a509442015-01-27 21:28:24 +0000839static void PrintLinkOptHints(MachOObjectFile *O) {
840 MachO::linkedit_data_command LohLC = O->getLinkOptHintsLoadCommand();
841 const char *loh = O->getData().substr(LohLC.dataoff, 1).data();
842 uint32_t nloh = LohLC.datasize;
843 outs() << "Linker optimiztion hints (" << nloh << " total bytes)\n";
844 for (uint32_t i = 0; i < nloh;) {
845 unsigned n;
846 uint64_t identifier = decodeULEB128((const uint8_t *)(loh + i), &n);
847 i += n;
848 outs() << " identifier " << identifier << " ";
849 if (i >= nloh)
850 return;
851 switch (identifier) {
852 case 1:
853 outs() << "AdrpAdrp\n";
854 break;
855 case 2:
856 outs() << "AdrpLdr\n";
857 break;
858 case 3:
859 outs() << "AdrpAddLdr\n";
860 break;
861 case 4:
862 outs() << "AdrpLdrGotLdr\n";
863 break;
864 case 5:
865 outs() << "AdrpAddStr\n";
866 break;
867 case 6:
868 outs() << "AdrpLdrGotStr\n";
869 break;
870 case 7:
871 outs() << "AdrpAdd\n";
872 break;
873 case 8:
874 outs() << "AdrpLdrGot\n";
875 break;
876 default:
877 outs() << "Unknown identifier value\n";
878 break;
879 }
880 uint64_t narguments = decodeULEB128((const uint8_t *)(loh + i), &n);
881 i += n;
882 outs() << " narguments " << narguments << "\n";
883 if (i >= nloh)
884 return;
885
886 for (uint32_t j = 0; j < narguments; j++) {
887 uint64_t value = decodeULEB128((const uint8_t *)(loh + i), &n);
888 i += n;
889 outs() << "\tvalue " << format("0x%" PRIx64, value) << "\n";
890 if (i >= nloh)
891 return;
892 }
893 }
894}
895
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000896static void PrintDylibs(MachOObjectFile *O, bool JustId) {
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000897 unsigned Index = 0;
898 for (const auto &Load : O->load_commands()) {
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000899 if ((JustId && Load.C.cmd == MachO::LC_ID_DYLIB) ||
900 (!JustId && (Load.C.cmd == MachO::LC_ID_DYLIB ||
901 Load.C.cmd == MachO::LC_LOAD_DYLIB ||
902 Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
903 Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
904 Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
905 Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB))) {
906 MachO::dylib_command dl = O->getDylibIDLoadCommand(Load);
907 if (dl.dylib.name < dl.cmdsize) {
908 const char *p = (const char *)(Load.Ptr) + dl.dylib.name;
909 if (JustId)
910 outs() << p << "\n";
911 else {
912 outs() << "\t" << p;
913 outs() << " (compatibility version "
914 << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
915 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
916 << (dl.dylib.compatibility_version & 0xff) << ",";
917 outs() << " current version "
918 << ((dl.dylib.current_version >> 16) & 0xffff) << "."
919 << ((dl.dylib.current_version >> 8) & 0xff) << "."
920 << (dl.dylib.current_version & 0xff) << ")\n";
921 }
922 } else {
923 outs() << "\tBad offset (" << dl.dylib.name << ") for name of ";
924 if (Load.C.cmd == MachO::LC_ID_DYLIB)
925 outs() << "LC_ID_DYLIB ";
926 else if (Load.C.cmd == MachO::LC_LOAD_DYLIB)
927 outs() << "LC_LOAD_DYLIB ";
928 else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB)
929 outs() << "LC_LOAD_WEAK_DYLIB ";
930 else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB)
931 outs() << "LC_LAZY_LOAD_DYLIB ";
932 else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB)
933 outs() << "LC_REEXPORT_DYLIB ";
934 else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
935 outs() << "LC_LOAD_UPWARD_DYLIB ";
936 else
937 outs() << "LC_??? ";
Alexey Samsonovd319c4f2015-06-03 22:19:36 +0000938 outs() << "command " << Index++ << "\n";
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000939 }
940 }
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000941 }
942}
943
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000944typedef DenseMap<uint64_t, StringRef> SymbolAddressMap;
945
946static void CreateSymbolAddressMap(MachOObjectFile *O,
947 SymbolAddressMap *AddrMap) {
948 // Create a map of symbol addresses to symbol names.
949 for (const SymbolRef &Symbol : O->symbols()) {
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000950 Expected<SymbolRef::Type> STOrErr = Symbol.getType();
Kevin Enderby844c4ac2016-11-15 23:07:41 +0000951 if (!STOrErr)
952 report_error(O->getFileName(), STOrErr.takeError());
Kevin Enderby5afbc1c2016-03-23 20:27:00 +0000953 SymbolRef::Type ST = *STOrErr;
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000954 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
955 ST == SymbolRef::ST_Other) {
Rafael Espindoladea00162015-07-03 17:44:18 +0000956 uint64_t Address = Symbol.getValue();
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000957 Expected<StringRef> SymNameOrErr = Symbol.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +0000958 if (!SymNameOrErr)
959 report_error(O->getFileName(), SymNameOrErr.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000960 StringRef SymName = *SymNameOrErr;
Kevin Enderby846c0002015-04-16 17:19:59 +0000961 if (!SymName.startswith(".objc"))
962 (*AddrMap)[Address] = SymName;
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000963 }
964 }
965}
966
967// GuessSymbolName is passed the address of what might be a symbol and a
968// pointer to the SymbolAddressMap. It returns the name of a symbol
969// with that address or nullptr if no symbol is found with that address.
970static const char *GuessSymbolName(uint64_t value, SymbolAddressMap *AddrMap) {
971 const char *SymbolName = nullptr;
972 // A DenseMap can't lookup up some values.
973 if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) {
974 StringRef name = AddrMap->lookup(value);
975 if (!name.empty())
976 SymbolName = name.data();
977 }
978 return SymbolName;
979}
980
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000981static void DumpCstringChar(const char c) {
982 char p[2];
983 p[0] = c;
984 p[1] = '\0';
985 outs().write_escaped(p);
986}
987
Kevin Enderby10ba0412015-02-04 21:38:42 +0000988static void DumpCstringSection(MachOObjectFile *O, const char *sect,
989 uint32_t sect_size, uint64_t sect_addr,
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000990 bool print_addresses) {
Kevin Enderby10ba0412015-02-04 21:38:42 +0000991 for (uint32_t i = 0; i < sect_size; i++) {
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000992 if (print_addresses) {
993 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000994 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000995 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000996 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +0000997 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +0000998 for (; i < sect_size && sect[i] != '\0'; i++)
999 DumpCstringChar(sect[i]);
Kevin Enderby10ba0412015-02-04 21:38:42 +00001000 if (i < sect_size && sect[i] == '\0')
1001 outs() << "\n";
1002 }
1003}
1004
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001005static void DumpLiteral4(uint32_t l, float f) {
1006 outs() << format("0x%08" PRIx32, l);
1007 if ((l & 0x7f800000) != 0x7f800000)
1008 outs() << format(" (%.16e)\n", f);
1009 else {
1010 if (l == 0x7f800000)
1011 outs() << " (+Infinity)\n";
1012 else if (l == 0xff800000)
1013 outs() << " (-Infinity)\n";
1014 else if ((l & 0x00400000) == 0x00400000)
1015 outs() << " (non-signaling Not-a-Number)\n";
1016 else
1017 outs() << " (signaling Not-a-Number)\n";
1018 }
1019}
1020
1021static void DumpLiteral4Section(MachOObjectFile *O, const char *sect,
1022 uint32_t sect_size, uint64_t sect_addr,
1023 bool print_addresses) {
1024 for (uint32_t i = 0; i < sect_size; i += sizeof(float)) {
1025 if (print_addresses) {
1026 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001027 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001028 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001029 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001030 }
1031 float f;
1032 memcpy(&f, sect + i, sizeof(float));
1033 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1034 sys::swapByteOrder(f);
1035 uint32_t l;
1036 memcpy(&l, sect + i, sizeof(uint32_t));
1037 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1038 sys::swapByteOrder(l);
1039 DumpLiteral4(l, f);
1040 }
1041}
1042
1043static void DumpLiteral8(MachOObjectFile *O, uint32_t l0, uint32_t l1,
1044 double d) {
1045 outs() << format("0x%08" PRIx32, l0) << " " << format("0x%08" PRIx32, l1);
1046 uint32_t Hi, Lo;
Davide Italianob627d9f2015-12-12 21:50:11 +00001047 Hi = (O->isLittleEndian()) ? l1 : l0;
1048 Lo = (O->isLittleEndian()) ? l0 : l1;
1049
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001050 // Hi is the high word, so this is equivalent to if(isfinite(d))
1051 if ((Hi & 0x7ff00000) != 0x7ff00000)
1052 outs() << format(" (%.16e)\n", d);
1053 else {
1054 if (Hi == 0x7ff00000 && Lo == 0)
1055 outs() << " (+Infinity)\n";
1056 else if (Hi == 0xfff00000 && Lo == 0)
1057 outs() << " (-Infinity)\n";
1058 else if ((Hi & 0x00080000) == 0x00080000)
1059 outs() << " (non-signaling Not-a-Number)\n";
1060 else
1061 outs() << " (signaling Not-a-Number)\n";
1062 }
1063}
1064
1065static void DumpLiteral8Section(MachOObjectFile *O, const char *sect,
1066 uint32_t sect_size, uint64_t sect_addr,
1067 bool print_addresses) {
1068 for (uint32_t i = 0; i < sect_size; i += sizeof(double)) {
1069 if (print_addresses) {
1070 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001071 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001072 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001073 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001074 }
1075 double d;
1076 memcpy(&d, sect + i, sizeof(double));
1077 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1078 sys::swapByteOrder(d);
1079 uint32_t l0, l1;
1080 memcpy(&l0, sect + i, sizeof(uint32_t));
1081 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
1082 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
1083 sys::swapByteOrder(l0);
1084 sys::swapByteOrder(l1);
1085 }
1086 DumpLiteral8(O, l0, l1, d);
1087 }
1088}
1089
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001090static void DumpLiteral16(uint32_t l0, uint32_t l1, uint32_t l2, uint32_t l3) {
1091 outs() << format("0x%08" PRIx32, l0) << " ";
1092 outs() << format("0x%08" PRIx32, l1) << " ";
1093 outs() << format("0x%08" PRIx32, l2) << " ";
1094 outs() << format("0x%08" PRIx32, l3) << "\n";
1095}
1096
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001097static void DumpLiteral16Section(MachOObjectFile *O, const char *sect,
1098 uint32_t sect_size, uint64_t sect_addr,
1099 bool print_addresses) {
1100 for (uint32_t i = 0; i < sect_size; i += 16) {
1101 if (print_addresses) {
1102 if (O->is64Bit())
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001103 outs() << format("%016" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001104 else
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001105 outs() << format("%08" PRIx64, sect_addr + i) << " ";
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001106 }
1107 uint32_t l0, l1, l2, l3;
1108 memcpy(&l0, sect + i, sizeof(uint32_t));
1109 memcpy(&l1, sect + i + sizeof(uint32_t), sizeof(uint32_t));
1110 memcpy(&l2, sect + i + 2 * sizeof(uint32_t), sizeof(uint32_t));
1111 memcpy(&l3, sect + i + 3 * sizeof(uint32_t), sizeof(uint32_t));
1112 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
1113 sys::swapByteOrder(l0);
1114 sys::swapByteOrder(l1);
1115 sys::swapByteOrder(l2);
1116 sys::swapByteOrder(l3);
1117 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001118 DumpLiteral16(l0, l1, l2, l3);
1119 }
1120}
1121
1122static void DumpLiteralPointerSection(MachOObjectFile *O,
1123 const SectionRef &Section,
1124 const char *sect, uint32_t sect_size,
1125 uint64_t sect_addr,
1126 bool print_addresses) {
1127 // Collect the literal sections in this Mach-O file.
1128 std::vector<SectionRef> LiteralSections;
1129 for (const SectionRef &Section : O->sections()) {
1130 DataRefImpl Ref = Section.getRawDataRefImpl();
1131 uint32_t section_type;
1132 if (O->is64Bit()) {
1133 const MachO::section_64 Sec = O->getSection64(Ref);
1134 section_type = Sec.flags & MachO::SECTION_TYPE;
1135 } else {
1136 const MachO::section Sec = O->getSection(Ref);
1137 section_type = Sec.flags & MachO::SECTION_TYPE;
1138 }
1139 if (section_type == MachO::S_CSTRING_LITERALS ||
1140 section_type == MachO::S_4BYTE_LITERALS ||
1141 section_type == MachO::S_8BYTE_LITERALS ||
1142 section_type == MachO::S_16BYTE_LITERALS)
1143 LiteralSections.push_back(Section);
1144 }
1145
1146 // Set the size of the literal pointer.
1147 uint32_t lp_size = O->is64Bit() ? 8 : 4;
1148
Eric Christopher572e03a2015-06-19 01:53:21 +00001149 // Collect the external relocation symbols for the literal pointers.
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001150 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
1151 for (const RelocationRef &Reloc : Section.relocations()) {
1152 DataRefImpl Rel;
1153 MachO::any_relocation_info RE;
1154 bool isExtern = false;
1155 Rel = Reloc.getRawDataRefImpl();
1156 RE = O->getRelocation(Rel);
1157 isExtern = O->getPlainRelocationExternal(RE);
1158 if (isExtern) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00001159 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001160 symbol_iterator RelocSym = Reloc.getSymbol();
1161 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
1162 }
1163 }
1164 array_pod_sort(Relocs.begin(), Relocs.end());
1165
1166 // Dump each literal pointer.
1167 for (uint32_t i = 0; i < sect_size; i += lp_size) {
1168 if (print_addresses) {
1169 if (O->is64Bit())
1170 outs() << format("%016" PRIx64, sect_addr + i) << " ";
1171 else
1172 outs() << format("%08" PRIx64, sect_addr + i) << " ";
1173 }
1174 uint64_t lp;
1175 if (O->is64Bit()) {
1176 memcpy(&lp, sect + i, sizeof(uint64_t));
1177 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1178 sys::swapByteOrder(lp);
1179 } else {
1180 uint32_t li;
1181 memcpy(&li, sect + i, sizeof(uint32_t));
1182 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1183 sys::swapByteOrder(li);
1184 lp = li;
1185 }
1186
1187 // First look for an external relocation entry for this literal pointer.
David Majnemer42531262016-08-12 03:55:06 +00001188 auto Reloc = find_if(Relocs, [&](const std::pair<uint64_t, SymbolRef> &P) {
1189 return P.first == i;
1190 });
David Blaikie33dd45d02015-03-23 18:39:02 +00001191 if (Reloc != Relocs.end()) {
1192 symbol_iterator RelocSym = Reloc->second;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00001193 Expected<StringRef> SymName = RelocSym->getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00001194 if (!SymName)
1195 report_error(O->getFileName(), SymName.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001196 outs() << "external relocation entry for symbol:" << *SymName << "\n";
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001197 continue;
David Blaikie33dd45d02015-03-23 18:39:02 +00001198 }
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001199
1200 // For local references see what the section the literal pointer points to.
David Majnemer562e8292016-08-12 00:18:03 +00001201 auto Sect = find_if(LiteralSections, [&](const SectionRef &R) {
1202 return lp >= R.getAddress() && lp < R.getAddress() + R.getSize();
1203 });
David Blaikie33dd45d02015-03-23 18:39:02 +00001204 if (Sect == LiteralSections.end()) {
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001205 outs() << format("0x%" PRIx64, lp) << " (not in a literal section)\n";
David Blaikie33dd45d02015-03-23 18:39:02 +00001206 continue;
1207 }
1208
1209 uint64_t SectAddress = Sect->getAddress();
1210 uint64_t SectSize = Sect->getSize();
1211
1212 StringRef SectName;
1213 Sect->getName(SectName);
1214 DataRefImpl Ref = Sect->getRawDataRefImpl();
1215 StringRef SegmentName = O->getSectionFinalSegmentName(Ref);
1216 outs() << SegmentName << ":" << SectName << ":";
1217
1218 uint32_t section_type;
1219 if (O->is64Bit()) {
1220 const MachO::section_64 Sec = O->getSection64(Ref);
1221 section_type = Sec.flags & MachO::SECTION_TYPE;
1222 } else {
1223 const MachO::section Sec = O->getSection(Ref);
1224 section_type = Sec.flags & MachO::SECTION_TYPE;
1225 }
1226
1227 StringRef BytesStr;
1228 Sect->getContents(BytesStr);
1229 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
1230
1231 switch (section_type) {
1232 case MachO::S_CSTRING_LITERALS:
1233 for (uint64_t i = lp - SectAddress; i < SectSize && Contents[i] != '\0';
1234 i++) {
1235 DumpCstringChar(Contents[i]);
1236 }
1237 outs() << "\n";
1238 break;
1239 case MachO::S_4BYTE_LITERALS:
1240 float f;
1241 memcpy(&f, Contents + (lp - SectAddress), sizeof(float));
1242 uint32_t l;
1243 memcpy(&l, Contents + (lp - SectAddress), sizeof(uint32_t));
1244 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
1245 sys::swapByteOrder(f);
1246 sys::swapByteOrder(l);
1247 }
1248 DumpLiteral4(l, f);
1249 break;
1250 case MachO::S_8BYTE_LITERALS: {
1251 double d;
1252 memcpy(&d, Contents + (lp - SectAddress), sizeof(double));
1253 uint32_t l0, l1;
1254 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
1255 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
1256 sizeof(uint32_t));
1257 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
1258 sys::swapByteOrder(f);
1259 sys::swapByteOrder(l0);
1260 sys::swapByteOrder(l1);
1261 }
1262 DumpLiteral8(O, l0, l1, d);
1263 break;
1264 }
1265 case MachO::S_16BYTE_LITERALS: {
1266 uint32_t l0, l1, l2, l3;
1267 memcpy(&l0, Contents + (lp - SectAddress), sizeof(uint32_t));
1268 memcpy(&l1, Contents + (lp - SectAddress) + sizeof(uint32_t),
1269 sizeof(uint32_t));
1270 memcpy(&l2, Contents + (lp - SectAddress) + 2 * sizeof(uint32_t),
1271 sizeof(uint32_t));
1272 memcpy(&l3, Contents + (lp - SectAddress) + 3 * sizeof(uint32_t),
1273 sizeof(uint32_t));
1274 if (O->isLittleEndian() != sys::IsLittleEndianHost) {
1275 sys::swapByteOrder(l0);
1276 sys::swapByteOrder(l1);
1277 sys::swapByteOrder(l2);
1278 sys::swapByteOrder(l3);
1279 }
1280 DumpLiteral16(l0, l1, l2, l3);
1281 break;
1282 }
1283 }
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001284 }
1285}
1286
Kevin Enderby85b7a662018-03-20 20:29:52 +00001287static void DumpInitTermPointerSection(MachOObjectFile *O,
1288 const SectionRef &Section,
1289 const char *sect,
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001290 uint32_t sect_size, uint64_t sect_addr,
1291 SymbolAddressMap *AddrMap,
1292 bool verbose) {
1293 uint32_t stride;
Davide Italiano3eb47e22015-12-15 23:14:21 +00001294 stride = (O->is64Bit()) ? sizeof(uint64_t) : sizeof(uint32_t);
Kevin Enderby85b7a662018-03-20 20:29:52 +00001295
1296 // Collect the external relocation symbols for the pointers.
1297 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
1298 for (const RelocationRef &Reloc : Section.relocations()) {
1299 DataRefImpl Rel;
1300 MachO::any_relocation_info RE;
1301 bool isExtern = false;
1302 Rel = Reloc.getRawDataRefImpl();
1303 RE = O->getRelocation(Rel);
1304 isExtern = O->getPlainRelocationExternal(RE);
1305 if (isExtern) {
1306 uint64_t RelocOffset = Reloc.getOffset();
1307 symbol_iterator RelocSym = Reloc.getSymbol();
1308 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
1309 }
1310 }
1311 array_pod_sort(Relocs.begin(), Relocs.end());
1312
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001313 for (uint32_t i = 0; i < sect_size; i += stride) {
1314 const char *SymbolName = nullptr;
Kevin Enderby85b7a662018-03-20 20:29:52 +00001315 uint64_t p;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001316 if (O->is64Bit()) {
1317 outs() << format("0x%016" PRIx64, sect_addr + i * stride) << " ";
1318 uint64_t pointer_value;
1319 memcpy(&pointer_value, sect + i, stride);
1320 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1321 sys::swapByteOrder(pointer_value);
1322 outs() << format("0x%016" PRIx64, pointer_value);
Kevin Enderby85b7a662018-03-20 20:29:52 +00001323 p = pointer_value;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001324 } else {
1325 outs() << format("0x%08" PRIx64, sect_addr + i * stride) << " ";
1326 uint32_t pointer_value;
1327 memcpy(&pointer_value, sect + i, stride);
1328 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1329 sys::swapByteOrder(pointer_value);
1330 outs() << format("0x%08" PRIx32, pointer_value);
Kevin Enderby85b7a662018-03-20 20:29:52 +00001331 p = pointer_value;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001332 }
Kevin Enderby85b7a662018-03-20 20:29:52 +00001333 if (verbose) {
1334 // First look for an external relocation entry for this pointer.
1335 auto Reloc = find_if(Relocs, [&](const std::pair<uint64_t, SymbolRef> &P) {
1336 return P.first == i;
1337 });
1338 if (Reloc != Relocs.end()) {
1339 symbol_iterator RelocSym = Reloc->second;
1340 Expected<StringRef> SymName = RelocSym->getName();
1341 if (!SymName)
1342 report_error(O->getFileName(), SymName.takeError());
1343 outs() << " " << *SymName;
1344 } else {
1345 SymbolName = GuessSymbolName(p, AddrMap);
1346 if (SymbolName)
1347 outs() << " " << SymbolName;
1348 }
1349 }
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001350 outs() << "\n";
1351 }
1352}
1353
1354static void DumpRawSectionContents(MachOObjectFile *O, const char *sect,
1355 uint32_t size, uint64_t addr) {
1356 uint32_t cputype = O->getHeader().cputype;
1357 if (cputype == MachO::CPU_TYPE_I386 || cputype == MachO::CPU_TYPE_X86_64) {
1358 uint32_t j;
1359 for (uint32_t i = 0; i < size; i += j, addr += j) {
1360 if (O->is64Bit())
1361 outs() << format("%016" PRIx64, addr) << "\t";
1362 else
Kevin Enderbyf0640752015-03-13 17:56:32 +00001363 outs() << format("%08" PRIx64, addr) << "\t";
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001364 for (j = 0; j < 16 && i + j < size; j++) {
1365 uint8_t byte_word = *(sect + i + j);
1366 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
1367 }
1368 outs() << "\n";
1369 }
1370 } else {
1371 uint32_t j;
1372 for (uint32_t i = 0; i < size; i += j, addr += j) {
1373 if (O->is64Bit())
1374 outs() << format("%016" PRIx64, addr) << "\t";
1375 else
Kevin Enderbyc4930852016-04-27 22:36:18 +00001376 outs() << format("%08" PRIx64, addr) << "\t";
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001377 for (j = 0; j < 4 * sizeof(int32_t) && i + j < size;
1378 j += sizeof(int32_t)) {
Kevin Enderby8eccdad2016-04-27 23:43:00 +00001379 if (i + j + sizeof(int32_t) <= size) {
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001380 uint32_t long_word;
1381 memcpy(&long_word, sect + i + j, sizeof(int32_t));
1382 if (O->isLittleEndian() != sys::IsLittleEndianHost)
1383 sys::swapByteOrder(long_word);
1384 outs() << format("%08" PRIx32, long_word) << " ";
1385 } else {
1386 for (uint32_t k = 0; i + j + k < size; k++) {
Kevin Enderby8eccdad2016-04-27 23:43:00 +00001387 uint8_t byte_word = *(sect + i + j + k);
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001388 outs() << format("%02" PRIx32, (uint32_t)byte_word) << " ";
1389 }
1390 }
1391 }
1392 outs() << "\n";
1393 }
1394 }
1395}
1396
Kevin Enderby95df54c2015-02-04 01:01:38 +00001397static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
1398 StringRef DisSegName, StringRef DisSectName);
Kevin Enderby4ad9bde2015-04-16 22:33:20 +00001399static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
1400 uint32_t size, uint32_t addr);
Kevin Enderby9873e2c2016-05-23 21:34:12 +00001401#ifdef HAVE_LIBXAR
1402static void DumpBitcodeSection(MachOObjectFile *O, const char *sect,
1403 uint32_t size, bool verbose,
1404 bool PrintXarHeader, bool PrintXarFileHeaders,
1405 std::string XarMemberName);
1406#endif // defined(HAVE_LIBXAR)
Kevin Enderby95df54c2015-02-04 01:01:38 +00001407
1408static void DumpSectionContents(StringRef Filename, MachOObjectFile *O,
1409 bool verbose) {
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001410 SymbolAddressMap AddrMap;
1411 if (verbose)
1412 CreateSymbolAddressMap(O, &AddrMap);
1413
Colin LeMahieufcc32762015-07-29 19:08:10 +00001414 for (unsigned i = 0; i < FilterSections.size(); ++i) {
1415 StringRef DumpSection = FilterSections[i];
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001416 std::pair<StringRef, StringRef> DumpSegSectName;
1417 DumpSegSectName = DumpSection.split(',');
1418 StringRef DumpSegName, DumpSectName;
1419 if (DumpSegSectName.second.size()) {
1420 DumpSegName = DumpSegSectName.first;
1421 DumpSectName = DumpSegSectName.second;
1422 } else {
1423 DumpSegName = "";
1424 DumpSectName = DumpSegSectName.first;
1425 }
1426 for (const SectionRef &Section : O->sections()) {
1427 StringRef SectName;
1428 Section.getName(SectName);
1429 DataRefImpl Ref = Section.getRawDataRefImpl();
1430 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1431 if ((DumpSegName.empty() || SegName == DumpSegName) &&
1432 (SectName == DumpSectName)) {
Adrian Prantlc2401dd2015-03-27 17:31:15 +00001433
Kevin Enderby95df54c2015-02-04 01:01:38 +00001434 uint32_t section_flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001435 if (O->is64Bit()) {
1436 const MachO::section_64 Sec = O->getSection64(Ref);
Kevin Enderby95df54c2015-02-04 01:01:38 +00001437 section_flags = Sec.flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001438
1439 } else {
1440 const MachO::section Sec = O->getSection(Ref);
Kevin Enderby95df54c2015-02-04 01:01:38 +00001441 section_flags = Sec.flags;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001442 }
Kevin Enderby95df54c2015-02-04 01:01:38 +00001443 uint32_t section_type = section_flags & MachO::SECTION_TYPE;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001444
1445 StringRef BytesStr;
1446 Section.getContents(BytesStr);
1447 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
1448 uint32_t sect_size = BytesStr.size();
1449 uint64_t sect_addr = Section.getAddress();
1450
Adrian Prantlc2401dd2015-03-27 17:31:15 +00001451 outs() << "Contents of (" << SegName << "," << SectName
1452 << ") section\n";
1453
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001454 if (verbose) {
Kevin Enderby95df54c2015-02-04 01:01:38 +00001455 if ((section_flags & MachO::S_ATTR_PURE_INSTRUCTIONS) ||
1456 (section_flags & MachO::S_ATTR_SOME_INSTRUCTIONS)) {
1457 DisassembleMachO(Filename, O, SegName, SectName);
1458 continue;
1459 }
Kevin Enderbycd66be52015-03-11 22:06:32 +00001460 if (SegName == "__TEXT" && SectName == "__info_plist") {
1461 outs() << sect;
1462 continue;
1463 }
Kevin Enderby4ad9bde2015-04-16 22:33:20 +00001464 if (SegName == "__OBJC" && SectName == "__protocol") {
1465 DumpProtocolSection(O, sect, sect_size, sect_addr);
1466 continue;
1467 }
Kevin Enderby9873e2c2016-05-23 21:34:12 +00001468#ifdef HAVE_LIBXAR
1469 if (SegName == "__LLVM" && SectName == "__bundle") {
1470 DumpBitcodeSection(O, sect, sect_size, verbose, !NoSymbolicOperands,
1471 ArchiveHeaders, "");
1472 continue;
1473 }
1474#endif // defined(HAVE_LIBXAR)
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001475 switch (section_type) {
1476 case MachO::S_REGULAR:
1477 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1478 break;
1479 case MachO::S_ZEROFILL:
1480 outs() << "zerofill section and has no contents in the file\n";
1481 break;
Kevin Enderby10ba0412015-02-04 21:38:42 +00001482 case MachO::S_CSTRING_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001483 DumpCstringSection(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby10ba0412015-02-04 21:38:42 +00001484 break;
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001485 case MachO::S_4BYTE_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001486 DumpLiteral4Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001487 break;
1488 case MachO::S_8BYTE_LITERALS:
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001489 DumpLiteral8Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
Kevin Enderby74b43cb2015-02-06 23:25:38 +00001490 break;
1491 case MachO::S_16BYTE_LITERALS:
Kevin Enderby0fc11822015-04-01 20:57:01 +00001492 DumpLiteral16Section(O, sect, sect_size, sect_addr, !NoLeadingAddr);
1493 break;
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001494 case MachO::S_LITERAL_POINTERS:
1495 DumpLiteralPointerSection(O, Section, sect, sect_size, sect_addr,
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00001496 !NoLeadingAddr);
Kevin Enderby578fe5a2015-02-17 21:35:48 +00001497 break;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001498 case MachO::S_MOD_INIT_FUNC_POINTERS:
1499 case MachO::S_MOD_TERM_FUNC_POINTERS:
Kevin Enderby85b7a662018-03-20 20:29:52 +00001500 DumpInitTermPointerSection(O, Section, sect, sect_size, sect_addr,
1501 &AddrMap, verbose);
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001502 break;
1503 default:
1504 outs() << "Unknown section type ("
1505 << format("0x%08" PRIx32, section_type) << ")\n";
1506 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1507 break;
1508 }
1509 } else {
1510 if (section_type == MachO::S_ZEROFILL)
1511 outs() << "zerofill section and has no contents in the file\n";
1512 else
1513 DumpRawSectionContents(O, sect, sect_size, sect_addr);
1514 }
1515 }
1516 }
1517 }
1518}
1519
Kevin Enderbycd66be52015-03-11 22:06:32 +00001520static void DumpInfoPlistSectionContents(StringRef Filename,
1521 MachOObjectFile *O) {
1522 for (const SectionRef &Section : O->sections()) {
1523 StringRef SectName;
1524 Section.getName(SectName);
1525 DataRefImpl Ref = Section.getRawDataRefImpl();
1526 StringRef SegName = O->getSectionFinalSegmentName(Ref);
1527 if (SegName == "__TEXT" && SectName == "__info_plist") {
Kevin Enderby30cf2e82017-06-20 21:00:25 +00001528 if (!NoLeadingHeaders)
1529 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
Kevin Enderbycd66be52015-03-11 22:06:32 +00001530 StringRef BytesStr;
1531 Section.getContents(BytesStr);
1532 const char *sect = reinterpret_cast<const char *>(BytesStr.data());
Kevin Enderby418659f2017-02-06 21:01:08 +00001533 outs() << format("%.*s", BytesStr.size(), sect) << "\n";
Kevin Enderbycd66be52015-03-11 22:06:32 +00001534 return;
1535 }
1536 }
1537}
1538
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001539// checkMachOAndArchFlags() checks to see if the ObjectFile is a Mach-O file
1540// and if it is and there is a list of architecture flags is specified then
1541// check to make sure this Mach-O file is one of those architectures or all
1542// architectures were specified. If not then an error is generated and this
1543// routine returns false. Else it returns true.
1544static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
David Majnemer91160d82016-10-31 17:11:31 +00001545 auto *MachO = dyn_cast<MachOObjectFile>(O);
1546
1547 if (!MachO || ArchAll || ArchFlags.empty())
1548 return true;
1549
1550 MachO::mach_header H;
1551 MachO::mach_header_64 H_64;
1552 Triple T;
Kevin Enderby59343a92016-12-16 22:54:02 +00001553 const char *McpuDefault, *ArchFlag;
David Majnemer91160d82016-10-31 17:11:31 +00001554 if (MachO->is64Bit()) {
1555 H_64 = MachO->MachOObjectFile::getHeader64();
Kevin Enderby59343a92016-12-16 22:54:02 +00001556 T = MachOObjectFile::getArchTriple(H_64.cputype, H_64.cpusubtype,
1557 &McpuDefault, &ArchFlag);
David Majnemer91160d82016-10-31 17:11:31 +00001558 } else {
1559 H = MachO->MachOObjectFile::getHeader();
Kevin Enderby59343a92016-12-16 22:54:02 +00001560 T = MachOObjectFile::getArchTriple(H.cputype, H.cpusubtype,
1561 &McpuDefault, &ArchFlag);
David Majnemer91160d82016-10-31 17:11:31 +00001562 }
Kevin Enderby59343a92016-12-16 22:54:02 +00001563 const std::string ArchFlagName(ArchFlag);
David Majnemer91160d82016-10-31 17:11:31 +00001564 if (none_of(ArchFlags, [&](const std::string &Name) {
Kevin Enderby59343a92016-12-16 22:54:02 +00001565 return Name == ArchFlagName;
David Majnemer91160d82016-10-31 17:11:31 +00001566 })) {
1567 errs() << "llvm-objdump: " + Filename + ": No architecture specified.\n";
1568 return false;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001569 }
1570 return true;
1571}
1572
Kevin Enderby0fc11822015-04-01 20:57:01 +00001573static void printObjcMetaData(MachOObjectFile *O, bool verbose);
1574
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001575// ProcessMachO() is passed a single opened Mach-O file, which may be an
1576// archive member and or in a slice of a universal file. It prints the
1577// the file name and header info and then processes it according to the
1578// command line options.
Kevin Enderby844c4ac2016-11-15 23:07:41 +00001579static void ProcessMachO(StringRef Name, MachOObjectFile *MachOOF,
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001580 StringRef ArchiveMemberName = StringRef(),
1581 StringRef ArchitectureName = StringRef()) {
Kevin Enderby131d1772015-01-09 19:22:37 +00001582 // If we are doing some processing here on the Mach-O file print the header
1583 // info. And don't print it otherwise like in the case of printing the
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +00001584 // UniversalHeaders or ArchiveHeaders.
Michael Trenta1703b12017-12-15 17:57:40 +00001585 if (Disassemble || Relocations || PrivateHeaders || ExportsTrie || Rebase ||
1586 Bind || SymbolTable || LazyBind || WeakBind || IndirectSymbols ||
1587 DataInCode || LinkOptHints || DylibsUsed || DylibId || ObjcMetaData ||
1588 (FilterSections.size() != 0)) {
Kevin Enderbyf9d60f02016-11-29 21:43:40 +00001589 if (!NoLeadingHeaders) {
1590 outs() << Name;
1591 if (!ArchiveMemberName.empty())
1592 outs() << '(' << ArchiveMemberName << ')';
1593 if (!ArchitectureName.empty())
1594 outs() << " (architecture " << ArchitectureName << ")";
1595 outs() << ":\n";
1596 }
Kevin Enderby131d1772015-01-09 19:22:37 +00001597 }
Kevin Enderby844c4ac2016-11-15 23:07:41 +00001598 // To use the report_error() form with an ArchiveName and FileName set
1599 // these up based on what is passed for Name and ArchiveMemberName.
1600 StringRef ArchiveName;
1601 StringRef FileName;
1602 if (!ArchiveMemberName.empty()) {
1603 ArchiveName = Name;
1604 FileName = ArchiveMemberName;
1605 } else {
1606 ArchiveName = StringRef();
1607 FileName = Name;
1608 }
1609
1610 // If we need the symbol table to do the operation then check it here to
1611 // produce a good error message as to where the Mach-O file comes from in
1612 // the error message.
1613 if (Disassemble || IndirectSymbols || FilterSections.size() != 0 ||
1614 UnwindInfo)
1615 if (Error Err = MachOOF->checkSymbolTable())
1616 report_error(ArchiveName, FileName, std::move(Err), ArchitectureName);
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001617
Kevin Enderbyaf2999a2017-06-22 19:50:56 +00001618 if (Disassemble) {
1619 if (MachOOF->getHeader().filetype == MachO::MH_KEXT_BUNDLE &&
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +00001620 MachOOF->getHeader().cputype == MachO::CPU_TYPE_ARM64)
Kevin Enderbyaf2999a2017-06-22 19:50:56 +00001621 DisassembleMachO(FileName, MachOOF, "__TEXT_EXEC", "__text");
1622 else
1623 DisassembleMachO(FileName, MachOOF, "__TEXT", "__text");
1624 }
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +00001625 if (IndirectSymbols)
Kevin Enderbyf0640752015-03-13 17:56:32 +00001626 PrintIndirectSymbols(MachOOF, !NonVerbose);
Kevin Enderby69fe98d2015-01-23 18:52:17 +00001627 if (DataInCode)
Kevin Enderbyf0640752015-03-13 17:56:32 +00001628 PrintDataInCodeTable(MachOOF, !NonVerbose);
Kevin Enderby9a509442015-01-27 21:28:24 +00001629 if (LinkOptHints)
1630 PrintLinkOptHints(MachOOF);
Kevin Enderby98da6132015-01-20 21:47:46 +00001631 if (Relocations)
Michael Trenta1703b12017-12-15 17:57:40 +00001632 PrintRelocations(MachOOF, !NonVerbose);
Kevin Enderby98da6132015-01-20 21:47:46 +00001633 if (SectionHeaders)
1634 PrintSectionHeaders(MachOOF);
1635 if (SectionContents)
1636 PrintSectionContents(MachOOF);
Colin LeMahieufcc32762015-07-29 19:08:10 +00001637 if (FilterSections.size() != 0)
Kevin Enderby844c4ac2016-11-15 23:07:41 +00001638 DumpSectionContents(FileName, MachOOF, !NonVerbose);
Kevin Enderbycd66be52015-03-11 22:06:32 +00001639 if (InfoPlist)
Kevin Enderby844c4ac2016-11-15 23:07:41 +00001640 DumpInfoPlistSectionContents(FileName, MachOOF);
Kevin Enderbybc847fa2015-03-16 20:08:09 +00001641 if (DylibsUsed)
1642 PrintDylibs(MachOOF, false);
1643 if (DylibId)
1644 PrintDylibs(MachOOF, true);
Kevin Enderby844c4ac2016-11-15 23:07:41 +00001645 if (SymbolTable)
Kevin Enderby9acb1092016-05-31 20:35:34 +00001646 PrintSymbolTable(MachOOF, ArchiveName, ArchitectureName);
Kevin Enderby98da6132015-01-20 21:47:46 +00001647 if (UnwindInfo)
1648 printMachOUnwindInfo(MachOOF);
Kevin Enderby0ae163f2016-01-13 00:25:36 +00001649 if (PrivateHeaders) {
1650 printMachOFileHeader(MachOOF);
1651 printMachOLoadCommands(MachOOF);
1652 }
1653 if (FirstPrivateHeader)
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001654 printMachOFileHeader(MachOOF);
Kevin Enderby0fc11822015-04-01 20:57:01 +00001655 if (ObjcMetaData)
1656 printObjcMetaData(MachOOF, !NonVerbose);
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001657 if (ExportsTrie)
1658 printExportsTrie(MachOOF);
1659 if (Rebase)
1660 printRebaseTable(MachOOF);
1661 if (Bind)
1662 printBindTable(MachOOF);
1663 if (LazyBind)
1664 printLazyBindTable(MachOOF);
1665 if (WeakBind)
1666 printWeakBindTable(MachOOF);
Igor Laevsky03a670c2016-01-26 15:09:42 +00001667
1668 if (DwarfDumpType != DIDT_Null) {
Rafael Espindolac398e672017-07-19 22:27:28 +00001669 std::unique_ptr<DIContext> DICtx = DWARFContext::create(*MachOOF);
Igor Laevsky03a670c2016-01-26 15:09:42 +00001670 // Dump the complete DWARF structure.
Adrian Prantlf4bc1f72017-06-01 18:18:23 +00001671 DIDumpOptions DumpOpts;
1672 DumpOpts.DumpType = DwarfDumpType;
Adrian Prantlf4bc1f72017-06-01 18:18:23 +00001673 DICtx->dump(outs(), DumpOpts);
Igor Laevsky03a670c2016-01-26 15:09:42 +00001674 }
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001675}
1676
Kevin Enderby131d1772015-01-09 19:22:37 +00001677// printUnknownCPUType() helps print_fat_headers for unknown CPU's.
1678static void printUnknownCPUType(uint32_t cputype, uint32_t cpusubtype) {
1679 outs() << " cputype (" << cputype << ")\n";
1680 outs() << " cpusubtype (" << cpusubtype << ")\n";
1681}
1682
1683// printCPUType() helps print_fat_headers by printing the cputype and
1684// pusubtype (symbolically for the one's it knows about).
1685static void printCPUType(uint32_t cputype, uint32_t cpusubtype) {
1686 switch (cputype) {
1687 case MachO::CPU_TYPE_I386:
1688 switch (cpusubtype) {
1689 case MachO::CPU_SUBTYPE_I386_ALL:
1690 outs() << " cputype CPU_TYPE_I386\n";
1691 outs() << " cpusubtype CPU_SUBTYPE_I386_ALL\n";
1692 break;
1693 default:
1694 printUnknownCPUType(cputype, cpusubtype);
1695 break;
1696 }
1697 break;
1698 case MachO::CPU_TYPE_X86_64:
1699 switch (cpusubtype) {
1700 case MachO::CPU_SUBTYPE_X86_64_ALL:
1701 outs() << " cputype CPU_TYPE_X86_64\n";
1702 outs() << " cpusubtype CPU_SUBTYPE_X86_64_ALL\n";
1703 break;
1704 case MachO::CPU_SUBTYPE_X86_64_H:
1705 outs() << " cputype CPU_TYPE_X86_64\n";
1706 outs() << " cpusubtype CPU_SUBTYPE_X86_64_H\n";
1707 break;
1708 default:
1709 printUnknownCPUType(cputype, cpusubtype);
1710 break;
1711 }
1712 break;
1713 case MachO::CPU_TYPE_ARM:
1714 switch (cpusubtype) {
1715 case MachO::CPU_SUBTYPE_ARM_ALL:
1716 outs() << " cputype CPU_TYPE_ARM\n";
1717 outs() << " cpusubtype CPU_SUBTYPE_ARM_ALL\n";
1718 break;
1719 case MachO::CPU_SUBTYPE_ARM_V4T:
1720 outs() << " cputype CPU_TYPE_ARM\n";
1721 outs() << " cpusubtype CPU_SUBTYPE_ARM_V4T\n";
1722 break;
1723 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
1724 outs() << " cputype CPU_TYPE_ARM\n";
1725 outs() << " cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n";
1726 break;
1727 case MachO::CPU_SUBTYPE_ARM_XSCALE:
1728 outs() << " cputype CPU_TYPE_ARM\n";
1729 outs() << " cpusubtype CPU_SUBTYPE_ARM_XSCALE\n";
1730 break;
1731 case MachO::CPU_SUBTYPE_ARM_V6:
1732 outs() << " cputype CPU_TYPE_ARM\n";
1733 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6\n";
1734 break;
1735 case MachO::CPU_SUBTYPE_ARM_V6M:
1736 outs() << " cputype CPU_TYPE_ARM\n";
1737 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6M\n";
1738 break;
1739 case MachO::CPU_SUBTYPE_ARM_V7:
1740 outs() << " cputype CPU_TYPE_ARM\n";
1741 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7\n";
1742 break;
1743 case MachO::CPU_SUBTYPE_ARM_V7EM:
1744 outs() << " cputype CPU_TYPE_ARM\n";
1745 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7EM\n";
1746 break;
1747 case MachO::CPU_SUBTYPE_ARM_V7K:
1748 outs() << " cputype CPU_TYPE_ARM\n";
1749 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7K\n";
1750 break;
1751 case MachO::CPU_SUBTYPE_ARM_V7M:
1752 outs() << " cputype CPU_TYPE_ARM\n";
1753 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7M\n";
1754 break;
1755 case MachO::CPU_SUBTYPE_ARM_V7S:
1756 outs() << " cputype CPU_TYPE_ARM\n";
1757 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7S\n";
1758 break;
1759 default:
1760 printUnknownCPUType(cputype, cpusubtype);
1761 break;
1762 }
1763 break;
1764 case MachO::CPU_TYPE_ARM64:
1765 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1766 case MachO::CPU_SUBTYPE_ARM64_ALL:
1767 outs() << " cputype CPU_TYPE_ARM64\n";
1768 outs() << " cpusubtype CPU_SUBTYPE_ARM64_ALL\n";
1769 break;
1770 default:
1771 printUnknownCPUType(cputype, cpusubtype);
1772 break;
1773 }
1774 break;
1775 default:
1776 printUnknownCPUType(cputype, cpusubtype);
1777 break;
1778 }
1779}
1780
1781static void printMachOUniversalHeaders(const object::MachOUniversalBinary *UB,
1782 bool verbose) {
1783 outs() << "Fat headers\n";
Kevin Enderby606a3382016-06-21 21:55:01 +00001784 if (verbose) {
1785 if (UB->getMagic() == MachO::FAT_MAGIC)
1786 outs() << "fat_magic FAT_MAGIC\n";
1787 else // UB->getMagic() == MachO::FAT_MAGIC_64
1788 outs() << "fat_magic FAT_MAGIC_64\n";
1789 } else
Kevin Enderby131d1772015-01-09 19:22:37 +00001790 outs() << "fat_magic " << format("0x%" PRIx32, MachO::FAT_MAGIC) << "\n";
1791
1792 uint32_t nfat_arch = UB->getNumberOfObjects();
1793 StringRef Buf = UB->getData();
1794 uint64_t size = Buf.size();
1795 uint64_t big_size = sizeof(struct MachO::fat_header) +
1796 nfat_arch * sizeof(struct MachO::fat_arch);
1797 outs() << "nfat_arch " << UB->getNumberOfObjects();
1798 if (nfat_arch == 0)
1799 outs() << " (malformed, contains zero architecture types)\n";
1800 else if (big_size > size)
1801 outs() << " (malformed, architectures past end of file)\n";
1802 else
1803 outs() << "\n";
1804
1805 for (uint32_t i = 0; i < nfat_arch; ++i) {
1806 MachOUniversalBinary::ObjectForArch OFA(UB, i);
1807 uint32_t cputype = OFA.getCPUType();
1808 uint32_t cpusubtype = OFA.getCPUSubType();
1809 outs() << "architecture ";
1810 for (uint32_t j = 0; i != 0 && j <= i - 1; j++) {
1811 MachOUniversalBinary::ObjectForArch other_OFA(UB, j);
1812 uint32_t other_cputype = other_OFA.getCPUType();
1813 uint32_t other_cpusubtype = other_OFA.getCPUSubType();
Kevin Enderby0512bd72015-01-09 21:55:03 +00001814 if (cputype != 0 && cpusubtype != 0 && cputype == other_cputype &&
Kevin Enderby131d1772015-01-09 19:22:37 +00001815 (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) ==
Kevin Enderby0512bd72015-01-09 21:55:03 +00001816 (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK)) {
Kevin Enderby131d1772015-01-09 19:22:37 +00001817 outs() << "(illegal duplicate architecture) ";
1818 break;
Kevin Enderby0512bd72015-01-09 21:55:03 +00001819 }
Kevin Enderby131d1772015-01-09 19:22:37 +00001820 }
1821 if (verbose) {
Kevin Enderby59343a92016-12-16 22:54:02 +00001822 outs() << OFA.getArchFlagName() << "\n";
Kevin Enderby131d1772015-01-09 19:22:37 +00001823 printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1824 } else {
1825 outs() << i << "\n";
1826 outs() << " cputype " << cputype << "\n";
1827 outs() << " cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK)
1828 << "\n";
1829 }
1830 if (verbose &&
1831 (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64)
1832 outs() << " capabilities CPU_SUBTYPE_LIB64\n";
1833 else
1834 outs() << " capabilities "
1835 << format("0x%" PRIx32,
1836 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n";
1837 outs() << " offset " << OFA.getOffset();
1838 if (OFA.getOffset() > size)
1839 outs() << " (past end of file)";
1840 if (OFA.getOffset() % (1 << OFA.getAlign()) != 0)
1841 outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")";
1842 outs() << "\n";
1843 outs() << " size " << OFA.getSize();
1844 big_size = OFA.getOffset() + OFA.getSize();
1845 if (big_size > size)
1846 outs() << " (past end of file)";
1847 outs() << "\n";
1848 outs() << " align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign())
1849 << ")\n";
1850 }
1851}
1852
Kevin Enderby6524bd82016-07-19 20:47:07 +00001853static void printArchiveChild(StringRef Filename, const Archive::Child &C,
1854 bool verbose, bool print_offset,
1855 StringRef ArchitectureName = StringRef()) {
Kevin Enderby13023a12015-01-15 23:19:11 +00001856 if (print_offset)
1857 outs() << C.getChildOffset() << "\t";
Vedant Kumar4031d9f2016-08-03 19:02:50 +00001858 Expected<sys::fs::perms> ModeOrErr = C.getAccessMode();
1859 if (!ModeOrErr)
1860 report_error(Filename, C, ModeOrErr.takeError(), ArchitectureName);
1861 sys::fs::perms Mode = ModeOrErr.get();
Kevin Enderby13023a12015-01-15 23:19:11 +00001862 if (verbose) {
1863 // FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
1864 // But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
1865 outs() << "-";
Davide Italianobb9a6cc2015-09-07 20:47:03 +00001866 outs() << ((Mode & sys::fs::owner_read) ? "r" : "-");
1867 outs() << ((Mode & sys::fs::owner_write) ? "w" : "-");
1868 outs() << ((Mode & sys::fs::owner_exe) ? "x" : "-");
1869 outs() << ((Mode & sys::fs::group_read) ? "r" : "-");
1870 outs() << ((Mode & sys::fs::group_write) ? "w" : "-");
1871 outs() << ((Mode & sys::fs::group_exe) ? "x" : "-");
1872 outs() << ((Mode & sys::fs::others_read) ? "r" : "-");
1873 outs() << ((Mode & sys::fs::others_write) ? "w" : "-");
1874 outs() << ((Mode & sys::fs::others_exe) ? "x" : "-");
Kevin Enderby13023a12015-01-15 23:19:11 +00001875 } else {
1876 outs() << format("0%o ", Mode);
1877 }
1878
Vedant Kumar4031d9f2016-08-03 19:02:50 +00001879 Expected<unsigned> UIDOrErr = C.getUID();
1880 if (!UIDOrErr)
1881 report_error(Filename, C, UIDOrErr.takeError(), ArchitectureName);
1882 unsigned UID = UIDOrErr.get();
Kevin Enderby13023a12015-01-15 23:19:11 +00001883 outs() << format("%3d/", UID);
Vedant Kumar4031d9f2016-08-03 19:02:50 +00001884 Expected<unsigned> GIDOrErr = C.getGID();
1885 if (!GIDOrErr)
1886 report_error(Filename, C, GIDOrErr.takeError(), ArchitectureName);
1887 unsigned GID = GIDOrErr.get();
Kevin Enderby13023a12015-01-15 23:19:11 +00001888 outs() << format("%-3d ", GID);
Kevin Enderby6524bd82016-07-19 20:47:07 +00001889 Expected<uint64_t> Size = C.getRawSize();
1890 if (!Size)
1891 report_error(Filename, C, Size.takeError(), ArchitectureName);
Kevin Enderby7a969422015-11-05 19:24:56 +00001892 outs() << format("%5" PRId64, Size.get()) << " ";
Kevin Enderby13023a12015-01-15 23:19:11 +00001893
1894 StringRef RawLastModified = C.getRawLastModified();
1895 if (verbose) {
1896 unsigned Seconds;
1897 if (RawLastModified.getAsInteger(10, Seconds))
Vedant Kumar4031d9f2016-08-03 19:02:50 +00001898 outs() << "(date: \"" << RawLastModified
1899 << "\" contains non-decimal chars) ";
Kevin Enderby13023a12015-01-15 23:19:11 +00001900 else {
1901 // Since cime(3) returns a 26 character string of the form:
1902 // "Sun Sep 16 01:03:52 1973\n\0"
1903 // just print 24 characters.
1904 time_t t = Seconds;
1905 outs() << format("%.24s ", ctime(&t));
1906 }
1907 } else {
1908 outs() << RawLastModified << " ";
1909 }
1910
1911 if (verbose) {
Kevin Enderbyf4586032016-07-29 17:44:13 +00001912 Expected<StringRef> NameOrErr = C.getName();
1913 if (!NameOrErr) {
1914 consumeError(NameOrErr.takeError());
1915 Expected<StringRef> NameOrErr = C.getRawName();
1916 if (!NameOrErr)
1917 report_error(Filename, C, NameOrErr.takeError(), ArchitectureName);
1918 StringRef RawName = NameOrErr.get();
Kevin Enderby13023a12015-01-15 23:19:11 +00001919 outs() << RawName << "\n";
1920 } else {
1921 StringRef Name = NameOrErr.get();
1922 outs() << Name << "\n";
1923 }
1924 } else {
Kevin Enderbyf4586032016-07-29 17:44:13 +00001925 Expected<StringRef> NameOrErr = C.getRawName();
1926 if (!NameOrErr)
1927 report_error(Filename, C, NameOrErr.takeError(), ArchitectureName);
1928 StringRef RawName = NameOrErr.get();
Kevin Enderby13023a12015-01-15 23:19:11 +00001929 outs() << RawName << "\n";
1930 }
1931}
1932
Kevin Enderby6524bd82016-07-19 20:47:07 +00001933static void printArchiveHeaders(StringRef Filename, Archive *A, bool verbose,
1934 bool print_offset,
1935 StringRef ArchitectureName = StringRef()) {
Mehdi Amini41af4302016-11-11 04:28:40 +00001936 Error Err = Error::success();
1937 ;
Lang Hamesfc209622016-07-14 02:24:01 +00001938 for (const auto &C : A->children(Err, false))
Kevin Enderby6524bd82016-07-19 20:47:07 +00001939 printArchiveChild(Filename, C, verbose, print_offset, ArchitectureName);
1940
Lang Hamesfc209622016-07-14 02:24:01 +00001941 if (Err)
Kevin Enderby844c4ac2016-11-15 23:07:41 +00001942 report_error(StringRef(), Filename, std::move(Err), ArchitectureName);
Kevin Enderby13023a12015-01-15 23:19:11 +00001943}
1944
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001945// ParseInputMachO() parses the named Mach-O file in Filename and handles the
1946// -arch flags selecting just those slices as specified by them and also parses
1947// archive files. Then for each individual Mach-O file ProcessMachO() is
1948// called to process the file based on the command line options.
1949void llvm::ParseInputMachO(StringRef Filename) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001950 // Check for -arch all and verifiy the -arch flags are valid.
1951 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
1952 if (ArchFlags[i] == "all") {
1953 ArchAll = true;
1954 } else {
1955 if (!MachOObjectFile::isValidArch(ArchFlags[i])) {
1956 errs() << "llvm-objdump: Unknown architecture named '" + ArchFlags[i] +
1957 "'for the -arch option\n";
1958 return;
1959 }
1960 }
1961 }
1962
1963 // Attempt to open the binary.
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +00001964 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename);
Kevin Enderby98898f22017-01-30 20:53:17 +00001965 if (!BinaryOrErr) {
1966 if (auto E = isNotObjectErrorInvalidFileType(BinaryOrErr.takeError()))
1967 report_error(Filename, std::move(E));
1968 else
1969 outs() << Filename << ": is not an object file\n";
1970 return;
1971 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001972 Binary &Bin = *BinaryOrErr.get().getBinary();
Kevin Enderby3f0ffab2014-12-03 22:29:40 +00001973
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001974 if (Archive *A = dyn_cast<Archive>(&Bin)) {
1975 outs() << "Archive : " << Filename << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00001976 if (ArchiveHeaders)
Kevin Enderby6524bd82016-07-19 20:47:07 +00001977 printArchiveHeaders(Filename, A, !NonVerbose, ArchiveMemberOffsets);
1978
Mehdi Amini41af4302016-11-11 04:28:40 +00001979 Error Err = Error::success();
Lang Hamesfc209622016-07-14 02:24:01 +00001980 for (auto &C : A->children(Err)) {
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001981 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1982 if (!ChildOrErr) {
1983 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
1984 report_error(Filename, C, std::move(E));
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001985 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001986 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001987 if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
1988 if (!checkMachOAndArchFlags(O, Filename))
1989 return;
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001990 ProcessMachO(Filename, O, O->getFileName());
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001991 }
1992 }
Lang Hamesfc209622016-07-14 02:24:01 +00001993 if (Err)
1994 report_error(Filename, std::move(Err));
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00001995 return;
1996 }
Kevin Enderby131d1772015-01-09 19:22:37 +00001997 if (UniversalHeaders) {
1998 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin))
Kevin Enderbyf0640752015-03-13 17:56:32 +00001999 printMachOUniversalHeaders(UB, !NonVerbose);
Kevin Enderby131d1772015-01-09 19:22:37 +00002000 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00002001 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
2002 // If we have a list of architecture flags specified dump only those.
2003 if (!ArchAll && ArchFlags.size() != 0) {
2004 // Look for a slice in the universal binary that matches each ArchFlag.
2005 bool ArchFound;
2006 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
2007 ArchFound = false;
2008 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
2009 E = UB->end_objects();
2010 I != E; ++I) {
Kevin Enderby59343a92016-12-16 22:54:02 +00002011 if (ArchFlags[i] == I->getArchFlagName()) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00002012 ArchFound = true;
Kevin Enderby9acb1092016-05-31 20:35:34 +00002013 Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00002014 I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00002015 std::string ArchitectureName = "";
2016 if (ArchFlags.size() > 1)
Kevin Enderby59343a92016-12-16 22:54:02 +00002017 ArchitectureName = I->getArchFlagName();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00002018 if (ObjOrErr) {
2019 ObjectFile &O = *ObjOrErr.get();
2020 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00002021 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
Kevin Enderby9acb1092016-05-31 20:35:34 +00002022 } else if (auto E = isNotObjectErrorInvalidFileType(
2023 ObjOrErr.takeError())) {
2024 report_error(Filename, StringRef(), std::move(E),
2025 ArchitectureName);
2026 continue;
Kevin Enderby42398052016-06-28 23:16:13 +00002027 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
Rafael Espindola0bfe8282014-12-09 21:05:36 +00002028 I->getAsArchive()) {
2029 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00002030 outs() << "Archive : " << Filename;
2031 if (!ArchitectureName.empty())
2032 outs() << " (architecture " << ArchitectureName << ")";
2033 outs() << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00002034 if (ArchiveHeaders)
Kevin Enderby6524bd82016-07-19 20:47:07 +00002035 printArchiveHeaders(Filename, A.get(), !NonVerbose,
2036 ArchiveMemberOffsets, ArchitectureName);
Mehdi Amini41af4302016-11-11 04:28:40 +00002037 Error Err = Error::success();
Lang Hamesfc209622016-07-14 02:24:01 +00002038 for (auto &C : A->children(Err)) {
Kevin Enderbyac9e1552016-05-17 17:10:12 +00002039 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
2040 if (!ChildOrErr) {
2041 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
Kevin Enderby9acb1092016-05-31 20:35:34 +00002042 report_error(Filename, C, std::move(E), ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00002043 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +00002044 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00002045 if (MachOObjectFile *O =
2046 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00002047 ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00002048 }
Lang Hamesfc209622016-07-14 02:24:01 +00002049 if (Err)
2050 report_error(Filename, std::move(Err));
Kevin Enderby42398052016-06-28 23:16:13 +00002051 } else {
2052 consumeError(AOrErr.takeError());
2053 error("Mach-O universal file: " + Filename + " for " +
Kevin Enderby59343a92016-12-16 22:54:02 +00002054 "architecture " + StringRef(I->getArchFlagName()) +
Kevin Enderby42398052016-06-28 23:16:13 +00002055 " is not a Mach-O file or an archive file");
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00002056 }
2057 }
2058 }
2059 if (!ArchFound) {
2060 errs() << "llvm-objdump: file: " + Filename + " does not contain "
2061 << "architecture: " + ArchFlags[i] + "\n";
2062 return;
2063 }
2064 }
2065 return;
2066 }
2067 // No architecture flags were specified so if this contains a slice that
2068 // matches the host architecture dump only that.
2069 if (!ArchAll) {
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00002070 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
2071 E = UB->end_objects();
2072 I != E; ++I) {
Kevin Enderby0512bd72015-01-09 21:55:03 +00002073 if (MachOObjectFile::getHostArch().getArchName() ==
Kevin Enderby59343a92016-12-16 22:54:02 +00002074 I->getArchFlagName()) {
Kevin Enderby9acb1092016-05-31 20:35:34 +00002075 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00002076 std::string ArchiveName;
2077 ArchiveName.clear();
2078 if (ObjOrErr) {
2079 ObjectFile &O = *ObjOrErr.get();
2080 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00002081 ProcessMachO(Filename, MachOOF);
Kevin Enderby9acb1092016-05-31 20:35:34 +00002082 } else if (auto E = isNotObjectErrorInvalidFileType(
2083 ObjOrErr.takeError())) {
2084 report_error(Filename, std::move(E));
2085 continue;
Kevin Enderby42398052016-06-28 23:16:13 +00002086 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
Rafael Espindola0bfe8282014-12-09 21:05:36 +00002087 I->getAsArchive()) {
2088 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00002089 outs() << "Archive : " << Filename << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00002090 if (ArchiveHeaders)
Kevin Enderby6524bd82016-07-19 20:47:07 +00002091 printArchiveHeaders(Filename, A.get(), !NonVerbose,
2092 ArchiveMemberOffsets);
Mehdi Amini41af4302016-11-11 04:28:40 +00002093 Error Err = Error::success();
Lang Hamesfc209622016-07-14 02:24:01 +00002094 for (auto &C : A->children(Err)) {
Kevin Enderbyac9e1552016-05-17 17:10:12 +00002095 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
2096 if (!ChildOrErr) {
2097 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
2098 report_error(Filename, C, std::move(E));
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00002099 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +00002100 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00002101 if (MachOObjectFile *O =
2102 dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00002103 ProcessMachO(Filename, O, O->getFileName());
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00002104 }
Lang Hamesfc209622016-07-14 02:24:01 +00002105 if (Err)
2106 report_error(Filename, std::move(Err));
Kevin Enderby42398052016-06-28 23:16:13 +00002107 } else {
2108 consumeError(AOrErr.takeError());
2109 error("Mach-O universal file: " + Filename + " for architecture " +
Kevin Enderby59343a92016-12-16 22:54:02 +00002110 StringRef(I->getArchFlagName()) +
Kevin Enderby42398052016-06-28 23:16:13 +00002111 " is not a Mach-O file or an archive file");
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00002112 }
2113 return;
2114 }
2115 }
2116 }
2117 // Either all architectures have been specified or none have been specified
2118 // and this does not contain the host architecture so dump all the slices.
2119 bool moreThanOneArch = UB->getNumberOfObjects() > 1;
2120 for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
2121 E = UB->end_objects();
2122 I != E; ++I) {
Kevin Enderby9acb1092016-05-31 20:35:34 +00002123 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00002124 std::string ArchitectureName = "";
2125 if (moreThanOneArch)
Kevin Enderby59343a92016-12-16 22:54:02 +00002126 ArchitectureName = I->getArchFlagName();
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00002127 if (ObjOrErr) {
2128 ObjectFile &Obj = *ObjOrErr.get();
2129 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00002130 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
Kevin Enderby9acb1092016-05-31 20:35:34 +00002131 } else if (auto E = isNotObjectErrorInvalidFileType(
2132 ObjOrErr.takeError())) {
2133 report_error(StringRef(), Filename, std::move(E), ArchitectureName);
2134 continue;
Kevin Enderby42398052016-06-28 23:16:13 +00002135 } else if (Expected<std::unique_ptr<Archive>> AOrErr =
2136 I->getAsArchive()) {
Rafael Espindola0bfe8282014-12-09 21:05:36 +00002137 std::unique_ptr<Archive> &A = *AOrErr;
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00002138 outs() << "Archive : " << Filename;
2139 if (!ArchitectureName.empty())
2140 outs() << " (architecture " << ArchitectureName << ")";
2141 outs() << "\n";
Kevin Enderby13023a12015-01-15 23:19:11 +00002142 if (ArchiveHeaders)
Kevin Enderby6524bd82016-07-19 20:47:07 +00002143 printArchiveHeaders(Filename, A.get(), !NonVerbose,
2144 ArchiveMemberOffsets, ArchitectureName);
Mehdi Amini41af4302016-11-11 04:28:40 +00002145 Error Err = Error::success();
Lang Hamesfc209622016-07-14 02:24:01 +00002146 for (auto &C : A->children(Err)) {
Kevin Enderbyac9e1552016-05-17 17:10:12 +00002147 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
2148 if (!ChildOrErr) {
2149 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
Kevin Enderby9acb1092016-05-31 20:35:34 +00002150 report_error(Filename, C, std::move(E), ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00002151 continue;
Kevin Enderbyac9e1552016-05-17 17:10:12 +00002152 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00002153 if (MachOObjectFile *O =
2154 dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
2155 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O))
Kevin Enderbye2297dd2015-01-07 21:02:18 +00002156 ProcessMachO(Filename, MachOOF, MachOOF->getFileName(),
2157 ArchitectureName);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00002158 }
2159 }
Lang Hamesfc209622016-07-14 02:24:01 +00002160 if (Err)
2161 report_error(Filename, std::move(Err));
Kevin Enderby42398052016-06-28 23:16:13 +00002162 } else {
2163 consumeError(AOrErr.takeError());
2164 error("Mach-O universal file: " + Filename + " for architecture " +
Kevin Enderby59343a92016-12-16 22:54:02 +00002165 StringRef(I->getArchFlagName()) +
Kevin Enderby42398052016-06-28 23:16:13 +00002166 " is not a Mach-O file or an archive file");
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00002167 }
2168 }
2169 return;
2170 }
2171 if (ObjectFile *O = dyn_cast<ObjectFile>(&Bin)) {
2172 if (!checkMachOAndArchFlags(O, Filename))
2173 return;
2174 if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O)) {
Kevin Enderbye2297dd2015-01-07 21:02:18 +00002175 ProcessMachO(Filename, MachOOF);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00002176 } else
2177 errs() << "llvm-objdump: '" << Filename << "': "
2178 << "Object is not a Mach-O file type.\n";
Davide Italiano25d84582016-01-13 04:11:36 +00002179 return;
2180 }
2181 llvm_unreachable("Input object can't be invalid at this point");
Rafael Espindola9b709252013-04-13 01:45:40 +00002182}
2183
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002184// The block of info used by the Symbolizer call backs.
2185struct DisassembleInfo {
Serge Pavlove4e9a1f2018-02-14 03:26:27 +00002186 DisassembleInfo(MachOObjectFile *O, SymbolAddressMap *AddrMap,
2187 std::vector<SectionRef> *Sections, bool verbose)
2188 : verbose(verbose), O(O), AddrMap(AddrMap), Sections(Sections) {}
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002189 bool verbose;
2190 MachOObjectFile *O;
2191 SectionRef S;
Kevin Enderbybf246f52014-09-24 23:08:22 +00002192 SymbolAddressMap *AddrMap;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002193 std::vector<SectionRef> *Sections;
Serge Pavlove4e9a1f2018-02-14 03:26:27 +00002194 const char *class_name = nullptr;
2195 const char *selector_name = nullptr;
David Blaikie0e550682018-02-20 18:48:51 +00002196 std::unique_ptr<char[]> method = nullptr;
Serge Pavlove4e9a1f2018-02-14 03:26:27 +00002197 char *demangled_name = nullptr;
2198 uint64_t adrp_addr = 0;
2199 uint32_t adrp_inst = 0;
Saleem Abdulrasool1d84d9a2017-01-08 19:14:15 +00002200 std::unique_ptr<SymbolAddressMap> bindtable;
Serge Pavlove4e9a1f2018-02-14 03:26:27 +00002201 uint32_t depth = 0;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002202};
2203
2204// SymbolizerGetOpInfo() is the operand information call back function.
2205// This is called to get the symbolic information for operand(s) of an
2206// instruction when it is being done. This routine does this from
2207// the relocation information, symbol table, etc. That block of information
2208// is a pointer to the struct DisassembleInfo that was passed when the
2209// disassembler context was created and passed to back to here when
2210// called back by the disassembler for instruction operands that could have
2211// relocation information. The address of the instruction containing operand is
2212// at the Pc parameter. The immediate value the operand has is passed in
2213// op_info->Value and is at Offset past the start of the instruction and has a
2214// byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
2215// LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
2216// names and addends of the symbolic expression to add for the operand. The
2217// value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
2218// information is returned then this function returns 1 else it returns 0.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00002219static int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
2220 uint64_t Size, int TagType, void *TagBuf) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002221 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
2222 struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002223 uint64_t value = op_info->Value;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002224
2225 // Make sure all fields returned are zero if we don't set them.
2226 memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1));
2227 op_info->Value = value;
2228
2229 // If the TagType is not the value 1 which it code knows about or if no
2230 // verbose symbolic information is wanted then just return 0, indicating no
2231 // information is being returned.
David Blaikie33dd45d02015-03-23 18:39:02 +00002232 if (TagType != 1 || !info->verbose)
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002233 return 0;
2234
2235 unsigned int Arch = info->O->getArch();
2236 if (Arch == Triple::x86) {
Kevin Enderby9907d0a2014-11-04 00:43:16 +00002237 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
2238 return 0;
Kevin Enderbyd90a4172015-10-10 00:05:01 +00002239 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2240 // TODO:
2241 // Search the external relocation entries of a fully linked image
2242 // (if any) for an entry that matches this segment offset.
2243 // uint32_t seg_offset = (Pc + Offset);
2244 return 0;
2245 }
2246 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2247 // for an entry for this section offset.
Kevin Enderby9907d0a2014-11-04 00:43:16 +00002248 uint32_t sect_addr = info->S.getAddress();
2249 uint32_t sect_offset = (Pc + Offset) - sect_addr;
2250 bool reloc_found = false;
2251 DataRefImpl Rel;
2252 MachO::any_relocation_info RE;
2253 bool isExtern = false;
2254 SymbolRef Symbol;
2255 bool r_scattered = false;
2256 uint32_t r_value, pair_r_value, r_type;
2257 for (const RelocationRef &Reloc : info->S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00002258 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby9907d0a2014-11-04 00:43:16 +00002259 if (RelocOffset == sect_offset) {
2260 Rel = Reloc.getRawDataRefImpl();
2261 RE = info->O->getRelocation(Rel);
Kevin Enderby3eb73e12014-11-11 19:16:45 +00002262 r_type = info->O->getAnyRelocationType(RE);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00002263 r_scattered = info->O->isRelocationScattered(RE);
2264 if (r_scattered) {
2265 r_value = info->O->getScatteredRelocationValue(RE);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00002266 if (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
2267 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
2268 DataRefImpl RelNext = Rel;
2269 info->O->moveRelocationNext(RelNext);
2270 MachO::any_relocation_info RENext;
2271 RENext = info->O->getRelocation(RelNext);
2272 if (info->O->isRelocationScattered(RENext))
Kevin Enderby930fdc72014-11-06 19:00:13 +00002273 pair_r_value = info->O->getScatteredRelocationValue(RENext);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00002274 else
2275 return 0;
2276 }
2277 } else {
2278 isExtern = info->O->getPlainRelocationExternal(RE);
2279 if (isExtern) {
2280 symbol_iterator RelocSym = Reloc.getSymbol();
2281 Symbol = *RelocSym;
2282 }
2283 }
2284 reloc_found = true;
2285 break;
2286 }
2287 }
2288 if (reloc_found && isExtern) {
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002289 Expected<StringRef> SymName = Symbol.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00002290 if (!SymName)
2291 report_error(info->O->getFileName(), SymName.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002292 const char *name = SymName->data();
Kevin Enderby9907d0a2014-11-04 00:43:16 +00002293 op_info->AddSymbol.Present = 1;
2294 op_info->AddSymbol.Name = name;
2295 // For i386 extern relocation entries the value in the instruction is
2296 // the offset from the symbol, and value is already set in op_info->Value.
2297 return 1;
2298 }
2299 if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
2300 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) {
Kevin Enderbyf6d25852015-01-31 00:37:11 +00002301 const char *add = GuessSymbolName(r_value, info->AddrMap);
2302 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
Kevin Enderby9907d0a2014-11-04 00:43:16 +00002303 uint32_t offset = value - (r_value - pair_r_value);
2304 op_info->AddSymbol.Present = 1;
2305 if (add != nullptr)
2306 op_info->AddSymbol.Name = add;
2307 else
2308 op_info->AddSymbol.Value = r_value;
2309 op_info->SubtractSymbol.Present = 1;
2310 if (sub != nullptr)
2311 op_info->SubtractSymbol.Name = sub;
2312 else
2313 op_info->SubtractSymbol.Value = pair_r_value;
2314 op_info->Value = offset;
2315 return 1;
2316 }
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002317 return 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00002318 }
2319 if (Arch == Triple::x86_64) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002320 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
2321 return 0;
Kevin Enderbyabf10f22017-06-22 17:41:22 +00002322 // For non MH_OBJECT types, like MH_KEXT_BUNDLE, Search the external
2323 // relocation entries of a linked image (if any) for an entry that matches
2324 // this segment offset.
Kevin Enderbyd90a4172015-10-10 00:05:01 +00002325 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
Kevin Enderbyabf10f22017-06-22 17:41:22 +00002326 uint64_t seg_offset = Pc + Offset;
2327 bool reloc_found = false;
2328 DataRefImpl Rel;
2329 MachO::any_relocation_info RE;
2330 bool isExtern = false;
2331 SymbolRef Symbol;
2332 for (const RelocationRef &Reloc : info->O->external_relocations()) {
2333 uint64_t RelocOffset = Reloc.getOffset();
2334 if (RelocOffset == seg_offset) {
2335 Rel = Reloc.getRawDataRefImpl();
2336 RE = info->O->getRelocation(Rel);
2337 // external relocation entries should always be external.
2338 isExtern = info->O->getPlainRelocationExternal(RE);
2339 if (isExtern) {
2340 symbol_iterator RelocSym = Reloc.getSymbol();
2341 Symbol = *RelocSym;
2342 }
2343 reloc_found = true;
2344 break;
2345 }
2346 }
2347 if (reloc_found && isExtern) {
2348 // The Value passed in will be adjusted by the Pc if the instruction
2349 // adds the Pc. But for x86_64 external relocation entries the Value
2350 // is the offset from the external symbol.
2351 if (info->O->getAnyRelocationPCRel(RE))
2352 op_info->Value -= Pc + Offset + Size;
2353 Expected<StringRef> SymName = Symbol.getName();
2354 if (!SymName)
2355 report_error(info->O->getFileName(), SymName.takeError());
2356 const char *name = SymName->data();
2357 op_info->AddSymbol.Present = 1;
2358 op_info->AddSymbol.Name = name;
2359 return 1;
2360 }
Kevin Enderbyd90a4172015-10-10 00:05:01 +00002361 return 0;
2362 }
2363 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2364 // for an entry for this section offset.
Rafael Espindola80291272014-10-08 15:28:58 +00002365 uint64_t sect_addr = info->S.getAddress();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002366 uint64_t sect_offset = (Pc + Offset) - sect_addr;
2367 bool reloc_found = false;
2368 DataRefImpl Rel;
2369 MachO::any_relocation_info RE;
2370 bool isExtern = false;
2371 SymbolRef Symbol;
2372 for (const RelocationRef &Reloc : info->S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00002373 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002374 if (RelocOffset == sect_offset) {
2375 Rel = Reloc.getRawDataRefImpl();
2376 RE = info->O->getRelocation(Rel);
2377 // NOTE: Scattered relocations don't exist on x86_64.
2378 isExtern = info->O->getPlainRelocationExternal(RE);
2379 if (isExtern) {
2380 symbol_iterator RelocSym = Reloc.getSymbol();
2381 Symbol = *RelocSym;
2382 }
2383 reloc_found = true;
2384 break;
2385 }
2386 }
2387 if (reloc_found && isExtern) {
2388 // The Value passed in will be adjusted by the Pc if the instruction
2389 // adds the Pc. But for x86_64 external relocation entries the Value
2390 // is the offset from the external symbol.
2391 if (info->O->getAnyRelocationPCRel(RE))
2392 op_info->Value -= Pc + Offset + Size;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002393 Expected<StringRef> SymName = Symbol.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00002394 if (!SymName)
2395 report_error(info->O->getFileName(), SymName.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002396 const char *name = SymName->data();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002397 unsigned Type = info->O->getAnyRelocationType(RE);
2398 if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
2399 DataRefImpl RelNext = Rel;
2400 info->O->moveRelocationNext(RelNext);
2401 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2402 unsigned TypeNext = info->O->getAnyRelocationType(RENext);
2403 bool isExternNext = info->O->getPlainRelocationExternal(RENext);
2404 unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext);
2405 if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) {
2406 op_info->SubtractSymbol.Present = 1;
2407 op_info->SubtractSymbol.Name = name;
2408 symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
2409 Symbol = *RelocSymNext;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002410 Expected<StringRef> SymNameNext = Symbol.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00002411 if (!SymNameNext)
2412 report_error(info->O->getFileName(), SymNameNext.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002413 name = SymNameNext->data();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002414 }
2415 }
2416 // TODO: add the VariantKinds to op_info->VariantKind for relocation types
2417 // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
2418 op_info->AddSymbol.Present = 1;
2419 op_info->AddSymbol.Name = name;
2420 return 1;
2421 }
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002422 return 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00002423 }
2424 if (Arch == Triple::arm) {
Kevin Enderby930fdc72014-11-06 19:00:13 +00002425 if (Offset != 0 || (Size != 4 && Size != 2))
2426 return 0;
Kevin Enderbyd90a4172015-10-10 00:05:01 +00002427 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2428 // TODO:
2429 // Search the external relocation entries of a fully linked image
2430 // (if any) for an entry that matches this segment offset.
2431 // uint32_t seg_offset = (Pc + Offset);
2432 return 0;
2433 }
2434 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2435 // for an entry for this section offset.
Kevin Enderby930fdc72014-11-06 19:00:13 +00002436 uint32_t sect_addr = info->S.getAddress();
2437 uint32_t sect_offset = (Pc + Offset) - sect_addr;
Kevin Enderby930fdc72014-11-06 19:00:13 +00002438 DataRefImpl Rel;
2439 MachO::any_relocation_info RE;
2440 bool isExtern = false;
2441 SymbolRef Symbol;
2442 bool r_scattered = false;
2443 uint32_t r_value, pair_r_value, r_type, r_length, other_half;
David Blaikie33dd45d02015-03-23 18:39:02 +00002444 auto Reloc =
David Majnemer562e8292016-08-12 00:18:03 +00002445 find_if(info->S.relocations(), [&](const RelocationRef &Reloc) {
2446 uint64_t RelocOffset = Reloc.getOffset();
2447 return RelocOffset == sect_offset;
2448 });
David Blaikie33dd45d02015-03-23 18:39:02 +00002449
2450 if (Reloc == info->S.relocations().end())
2451 return 0;
2452
2453 Rel = Reloc->getRawDataRefImpl();
2454 RE = info->O->getRelocation(Rel);
2455 r_length = info->O->getAnyRelocationLength(RE);
2456 r_scattered = info->O->isRelocationScattered(RE);
2457 if (r_scattered) {
2458 r_value = info->O->getScatteredRelocationValue(RE);
2459 r_type = info->O->getScatteredRelocationType(RE);
2460 } else {
2461 r_type = info->O->getAnyRelocationType(RE);
2462 isExtern = info->O->getPlainRelocationExternal(RE);
2463 if (isExtern) {
2464 symbol_iterator RelocSym = Reloc->getSymbol();
2465 Symbol = *RelocSym;
Kevin Enderby930fdc72014-11-06 19:00:13 +00002466 }
2467 }
David Blaikie33dd45d02015-03-23 18:39:02 +00002468 if (r_type == MachO::ARM_RELOC_HALF ||
2469 r_type == MachO::ARM_RELOC_SECTDIFF ||
2470 r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
2471 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2472 DataRefImpl RelNext = Rel;
2473 info->O->moveRelocationNext(RelNext);
2474 MachO::any_relocation_info RENext;
2475 RENext = info->O->getRelocation(RelNext);
2476 other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff;
2477 if (info->O->isRelocationScattered(RENext))
2478 pair_r_value = info->O->getScatteredRelocationValue(RENext);
2479 }
2480
2481 if (isExtern) {
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002482 Expected<StringRef> SymName = Symbol.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00002483 if (!SymName)
2484 report_error(info->O->getFileName(), SymName.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002485 const char *name = SymName->data();
Kevin Enderby930fdc72014-11-06 19:00:13 +00002486 op_info->AddSymbol.Present = 1;
2487 op_info->AddSymbol.Name = name;
Sylvestre Ledru648cced2015-02-05 17:00:23 +00002488 switch (r_type) {
2489 case MachO::ARM_RELOC_HALF:
2490 if ((r_length & 0x1) == 1) {
2491 op_info->Value = value << 16 | other_half;
2492 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2493 } else {
2494 op_info->Value = other_half << 16 | value;
2495 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Sylvestre Ledrufe0c7ad2015-02-05 16:35:44 +00002496 }
Sylvestre Ledru648cced2015-02-05 17:00:23 +00002497 break;
2498 default:
2499 break;
Kevin Enderby930fdc72014-11-06 19:00:13 +00002500 }
2501 return 1;
2502 }
2503 // If we have a branch that is not an external relocation entry then
2504 // return 0 so the code in tryAddingSymbolicOperand() can use the
2505 // SymbolLookUp call back with the branch target address to look up the
Simon Pilgrimdae11f72016-11-20 13:31:13 +00002506 // symbol and possibility add an annotation for a symbol stub.
David Blaikie33dd45d02015-03-23 18:39:02 +00002507 if (isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 ||
2508 r_type == MachO::ARM_THUMB_RELOC_BR22))
Kevin Enderby930fdc72014-11-06 19:00:13 +00002509 return 0;
2510
2511 uint32_t offset = 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00002512 if (r_type == MachO::ARM_RELOC_HALF ||
2513 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
2514 if ((r_length & 0x1) == 1)
2515 value = value << 16 | other_half;
2516 else
2517 value = other_half << 16 | value;
2518 }
2519 if (r_scattered && (r_type != MachO::ARM_RELOC_HALF &&
2520 r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) {
2521 offset = value - r_value;
2522 value = r_value;
Kevin Enderby930fdc72014-11-06 19:00:13 +00002523 }
2524
David Blaikie33dd45d02015-03-23 18:39:02 +00002525 if (r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
Kevin Enderby930fdc72014-11-06 19:00:13 +00002526 if ((r_length & 0x1) == 1)
2527 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2528 else
2529 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Kevin Enderbyf6d25852015-01-31 00:37:11 +00002530 const char *add = GuessSymbolName(r_value, info->AddrMap);
2531 const char *sub = GuessSymbolName(pair_r_value, info->AddrMap);
Kevin Enderby930fdc72014-11-06 19:00:13 +00002532 int32_t offset = value - (r_value - pair_r_value);
2533 op_info->AddSymbol.Present = 1;
2534 if (add != nullptr)
2535 op_info->AddSymbol.Name = add;
2536 else
2537 op_info->AddSymbol.Value = r_value;
2538 op_info->SubtractSymbol.Present = 1;
2539 if (sub != nullptr)
2540 op_info->SubtractSymbol.Name = sub;
2541 else
2542 op_info->SubtractSymbol.Value = pair_r_value;
2543 op_info->Value = offset;
2544 return 1;
2545 }
2546
Kevin Enderby930fdc72014-11-06 19:00:13 +00002547 op_info->AddSymbol.Present = 1;
2548 op_info->Value = offset;
David Blaikie33dd45d02015-03-23 18:39:02 +00002549 if (r_type == MachO::ARM_RELOC_HALF) {
2550 if ((r_length & 0x1) == 1)
2551 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
2552 else
2553 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
Kevin Enderby930fdc72014-11-06 19:00:13 +00002554 }
Kevin Enderbyf6d25852015-01-31 00:37:11 +00002555 const char *add = GuessSymbolName(value, info->AddrMap);
Kevin Enderby930fdc72014-11-06 19:00:13 +00002556 if (add != nullptr) {
2557 op_info->AddSymbol.Name = add;
2558 return 1;
2559 }
2560 op_info->AddSymbol.Value = value;
2561 return 1;
David Blaikie33dd45d02015-03-23 18:39:02 +00002562 }
2563 if (Arch == Triple::aarch64) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002564 if (Offset != 0 || Size != 4)
2565 return 0;
Kevin Enderbyd90a4172015-10-10 00:05:01 +00002566 if (info->O->getHeader().filetype != MachO::MH_OBJECT) {
2567 // TODO:
2568 // Search the external relocation entries of a fully linked image
2569 // (if any) for an entry that matches this segment offset.
2570 // uint64_t seg_offset = (Pc + Offset);
2571 return 0;
2572 }
2573 // In MH_OBJECT filetypes search the section's relocation entries (if any)
2574 // for an entry for this section offset.
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002575 uint64_t sect_addr = info->S.getAddress();
2576 uint64_t sect_offset = (Pc + Offset) - sect_addr;
David Blaikie33dd45d02015-03-23 18:39:02 +00002577 auto Reloc =
David Majnemer562e8292016-08-12 00:18:03 +00002578 find_if(info->S.relocations(), [&](const RelocationRef &Reloc) {
2579 uint64_t RelocOffset = Reloc.getOffset();
2580 return RelocOffset == sect_offset;
2581 });
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002582
David Blaikie33dd45d02015-03-23 18:39:02 +00002583 if (Reloc == info->S.relocations().end())
2584 return 0;
2585
2586 DataRefImpl Rel = Reloc->getRawDataRefImpl();
2587 MachO::any_relocation_info RE = info->O->getRelocation(Rel);
2588 uint32_t r_type = info->O->getAnyRelocationType(RE);
2589 if (r_type == MachO::ARM64_RELOC_ADDEND) {
2590 DataRefImpl RelNext = Rel;
2591 info->O->moveRelocationNext(RelNext);
2592 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
2593 if (value == 0) {
2594 value = info->O->getPlainRelocationSymbolNum(RENext);
2595 op_info->Value = value;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002596 }
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002597 }
David Blaikie33dd45d02015-03-23 18:39:02 +00002598 // NOTE: Scattered relocations don't exist on arm64.
2599 if (!info->O->getPlainRelocationExternal(RE))
2600 return 0;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002601 Expected<StringRef> SymName = Reloc->getSymbol()->getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00002602 if (!SymName)
2603 report_error(info->O->getFileName(), SymName.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002604 const char *name = SymName->data();
David Blaikie33dd45d02015-03-23 18:39:02 +00002605 op_info->AddSymbol.Present = 1;
2606 op_info->AddSymbol.Name = name;
2607
2608 switch (r_type) {
2609 case MachO::ARM64_RELOC_PAGE21:
2610 /* @page */
2611 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGE;
2612 break;
2613 case MachO::ARM64_RELOC_PAGEOFF12:
2614 /* @pageoff */
2615 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_PAGEOFF;
2616 break;
2617 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
2618 /* @gotpage */
2619 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGE;
2620 break;
2621 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
2622 /* @gotpageoff */
2623 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF;
2624 break;
2625 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
2626 /* @tvlppage is not implemented in llvm-mc */
2627 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVP;
2628 break;
2629 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
2630 /* @tvlppageoff is not implemented in llvm-mc */
2631 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM64_TLVOFF;
2632 break;
2633 default:
2634 case MachO::ARM64_RELOC_BRANCH26:
2635 op_info->VariantKind = LLVMDisassembler_VariantKind_None;
2636 break;
2637 }
2638 return 1;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002639 }
David Blaikie33dd45d02015-03-23 18:39:02 +00002640 return 0;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00002641}
2642
Kevin Enderbybf246f52014-09-24 23:08:22 +00002643// GuessCstringPointer is passed the address of what might be a pointer to a
2644// literal string in a cstring section. If that address is in a cstring section
2645// it returns a pointer to that string. Else it returns nullptr.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00002646static const char *GuessCstringPointer(uint64_t ReferenceValue,
2647 struct DisassembleInfo *info) {
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002648 for (const auto &Load : info->O->load_commands()) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00002649 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2650 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2651 for (unsigned J = 0; J < Seg.nsects; ++J) {
2652 MachO::section_64 Sec = info->O->getSection64(Load, J);
2653 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2654 if (section_type == MachO::S_CSTRING_LITERALS &&
2655 ReferenceValue >= Sec.addr &&
2656 ReferenceValue < Sec.addr + Sec.size) {
2657 uint64_t sect_offset = ReferenceValue - Sec.addr;
2658 uint64_t object_offset = Sec.offset + sect_offset;
2659 StringRef MachOContents = info->O->getData();
2660 uint64_t object_size = MachOContents.size();
2661 const char *object_addr = (const char *)MachOContents.data();
2662 if (object_offset < object_size) {
2663 const char *name = object_addr + object_offset;
2664 return name;
2665 } else {
2666 return nullptr;
2667 }
2668 }
2669 }
2670 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2671 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2672 for (unsigned J = 0; J < Seg.nsects; ++J) {
2673 MachO::section Sec = info->O->getSection(Load, J);
2674 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2675 if (section_type == MachO::S_CSTRING_LITERALS &&
2676 ReferenceValue >= Sec.addr &&
2677 ReferenceValue < Sec.addr + Sec.size) {
2678 uint64_t sect_offset = ReferenceValue - Sec.addr;
2679 uint64_t object_offset = Sec.offset + sect_offset;
2680 StringRef MachOContents = info->O->getData();
2681 uint64_t object_size = MachOContents.size();
2682 const char *object_addr = (const char *)MachOContents.data();
2683 if (object_offset < object_size) {
2684 const char *name = object_addr + object_offset;
2685 return name;
2686 } else {
2687 return nullptr;
2688 }
2689 }
2690 }
2691 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00002692 }
2693 return nullptr;
2694}
2695
Kevin Enderby85974882014-09-26 22:20:44 +00002696// GuessIndirectSymbol returns the name of the indirect symbol for the
2697// ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe
2698// an address of a symbol stub or a lazy or non-lazy pointer to associate the
2699// symbol name being referenced by the stub or pointer.
2700static const char *GuessIndirectSymbol(uint64_t ReferenceValue,
2701 struct DisassembleInfo *info) {
Kevin Enderby85974882014-09-26 22:20:44 +00002702 MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand();
2703 MachO::symtab_command Symtab = info->O->getSymtabLoadCommand();
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002704 for (const auto &Load : info->O->load_commands()) {
Kevin Enderby85974882014-09-26 22:20:44 +00002705 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2706 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2707 for (unsigned J = 0; J < Seg.nsects; ++J) {
2708 MachO::section_64 Sec = info->O->getSection64(Load, J);
2709 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2710 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2711 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2712 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2713 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2714 section_type == MachO::S_SYMBOL_STUBS) &&
2715 ReferenceValue >= Sec.addr &&
2716 ReferenceValue < Sec.addr + Sec.size) {
2717 uint32_t stride;
2718 if (section_type == MachO::S_SYMBOL_STUBS)
2719 stride = Sec.reserved2;
2720 else
2721 stride = 8;
2722 if (stride == 0)
2723 return nullptr;
2724 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2725 if (index < Dysymtab.nindirectsyms) {
2726 uint32_t indirect_symbol =
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002727 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
Kevin Enderby85974882014-09-26 22:20:44 +00002728 if (indirect_symbol < Symtab.nsyms) {
2729 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2730 SymbolRef Symbol = *Sym;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002731 Expected<StringRef> SymName = Symbol.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00002732 if (!SymName)
2733 report_error(info->O->getFileName(), SymName.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002734 const char *name = SymName->data();
Kevin Enderby85974882014-09-26 22:20:44 +00002735 return name;
2736 }
2737 }
2738 }
2739 }
2740 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
2741 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
2742 for (unsigned J = 0; J < Seg.nsects; ++J) {
2743 MachO::section Sec = info->O->getSection(Load, J);
2744 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
2745 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2746 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2747 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2748 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
2749 section_type == MachO::S_SYMBOL_STUBS) &&
2750 ReferenceValue >= Sec.addr &&
2751 ReferenceValue < Sec.addr + Sec.size) {
2752 uint32_t stride;
2753 if (section_type == MachO::S_SYMBOL_STUBS)
2754 stride = Sec.reserved2;
2755 else
2756 stride = 4;
2757 if (stride == 0)
2758 return nullptr;
2759 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
2760 if (index < Dysymtab.nindirectsyms) {
2761 uint32_t indirect_symbol =
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002762 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
Kevin Enderby85974882014-09-26 22:20:44 +00002763 if (indirect_symbol < Symtab.nsyms) {
2764 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
2765 SymbolRef Symbol = *Sym;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002766 Expected<StringRef> SymName = Symbol.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00002767 if (!SymName)
2768 report_error(info->O->getFileName(), SymName.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002769 const char *name = SymName->data();
Kevin Enderby85974882014-09-26 22:20:44 +00002770 return name;
2771 }
2772 }
2773 }
2774 }
2775 }
Kevin Enderby85974882014-09-26 22:20:44 +00002776 }
2777 return nullptr;
2778}
2779
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002780// method_reference() is called passing it the ReferenceName that might be
2781// a reference it to an Objective-C method call. If so then it allocates and
2782// assembles a method call string with the values last seen and saved in
2783// the DisassembleInfo's class_name and selector_name fields. This is saved
2784// into the method field of the info and any previous string is free'ed.
2785// Then the class_name field in the info is set to nullptr. The method call
2786// string is set into ReferenceName and ReferenceType is set to
2787// LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call
2788// then both ReferenceType and ReferenceName are left unchanged.
2789static void method_reference(struct DisassembleInfo *info,
2790 uint64_t *ReferenceType,
2791 const char **ReferenceName) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002792 unsigned int Arch = info->O->getArch();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002793 if (*ReferenceName != nullptr) {
2794 if (strcmp(*ReferenceName, "_objc_msgSend") == 0) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002795 if (info->selector_name != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002796 if (info->class_name != nullptr) {
David Blaikie0e550682018-02-20 18:48:51 +00002797 info->method = llvm::make_unique<char[]>(
2798 5 + strlen(info->class_name) + strlen(info->selector_name));
2799 char *method = info->method.get();
2800 if (method != nullptr) {
2801 strcpy(method, "+[");
2802 strcat(method, info->class_name);
2803 strcat(method, " ");
2804 strcat(method, info->selector_name);
2805 strcat(method, "]");
2806 *ReferenceName = method;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002807 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2808 }
2809 } else {
David Blaikie0e550682018-02-20 18:48:51 +00002810 info->method =
2811 llvm::make_unique<char[]>(9 + strlen(info->selector_name));
2812 char *method = info->method.get();
2813 if (method != nullptr) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002814 if (Arch == Triple::x86_64)
David Blaikie0e550682018-02-20 18:48:51 +00002815 strcpy(method, "-[%rdi ");
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002816 else if (Arch == Triple::aarch64)
David Blaikie0e550682018-02-20 18:48:51 +00002817 strcpy(method, "-[x0 ");
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002818 else
David Blaikie0e550682018-02-20 18:48:51 +00002819 strcpy(method, "-[r? ");
2820 strcat(method, info->selector_name);
2821 strcat(method, "]");
2822 *ReferenceName = method;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002823 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2824 }
2825 }
2826 info->class_name = nullptr;
2827 }
2828 } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002829 if (info->selector_name != nullptr) {
David Blaikie0e550682018-02-20 18:48:51 +00002830 info->method =
2831 llvm::make_unique<char[]>(17 + strlen(info->selector_name));
2832 char *method = info->method.get();
2833 if (method != nullptr) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002834 if (Arch == Triple::x86_64)
David Blaikie0e550682018-02-20 18:48:51 +00002835 strcpy(method, "-[[%rdi super] ");
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002836 else if (Arch == Triple::aarch64)
David Blaikie0e550682018-02-20 18:48:51 +00002837 strcpy(method, "-[[x0 super] ");
Kevin Enderbyae3c1262014-11-14 21:52:18 +00002838 else
David Blaikie0e550682018-02-20 18:48:51 +00002839 strcpy(method, "-[[r? super] ");
2840 strcat(method, info->selector_name);
2841 strcat(method, "]");
2842 *ReferenceName = method;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002843 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
2844 }
2845 info->class_name = nullptr;
2846 }
2847 }
2848 }
2849}
2850
2851// GuessPointerPointer() is passed the address of what might be a pointer to
2852// a reference to an Objective-C class, selector, message ref or cfstring.
2853// If so the value of the pointer is returned and one of the booleans are set
2854// to true. If not zero is returned and all the booleans are set to false.
2855static uint64_t GuessPointerPointer(uint64_t ReferenceValue,
2856 struct DisassembleInfo *info,
2857 bool &classref, bool &selref, bool &msgref,
2858 bool &cfstring) {
2859 classref = false;
2860 selref = false;
2861 msgref = false;
2862 cfstring = false;
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00002863 for (const auto &Load : info->O->load_commands()) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002864 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
2865 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
2866 for (unsigned J = 0; J < Seg.nsects; ++J) {
2867 MachO::section_64 Sec = info->O->getSection64(Load, J);
2868 if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 ||
2869 strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2870 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 ||
2871 strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 ||
2872 strncmp(Sec.sectname, "__cfstring", 16) == 0) &&
2873 ReferenceValue >= Sec.addr &&
2874 ReferenceValue < Sec.addr + Sec.size) {
2875 uint64_t sect_offset = ReferenceValue - Sec.addr;
2876 uint64_t object_offset = Sec.offset + sect_offset;
2877 StringRef MachOContents = info->O->getData();
2878 uint64_t object_size = MachOContents.size();
2879 const char *object_addr = (const char *)MachOContents.data();
2880 if (object_offset < object_size) {
2881 uint64_t pointer_value;
2882 memcpy(&pointer_value, object_addr + object_offset,
2883 sizeof(uint64_t));
2884 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2885 sys::swapByteOrder(pointer_value);
2886 if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0)
2887 selref = true;
2888 else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
2889 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0)
2890 classref = true;
2891 else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 &&
2892 ReferenceValue + 8 < Sec.addr + Sec.size) {
2893 msgref = true;
2894 memcpy(&pointer_value, object_addr + object_offset + 8,
2895 sizeof(uint64_t));
2896 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
2897 sys::swapByteOrder(pointer_value);
2898 } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0)
2899 cfstring = true;
2900 return pointer_value;
2901 } else {
2902 return 0;
2903 }
2904 }
2905 }
2906 }
2907 // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002908 }
2909 return 0;
2910}
2911
2912// get_pointer_64 returns a pointer to the bytes in the object file at the
2913// Address from a section in the Mach-O file. And indirectly returns the
2914// offset into the section, number of bytes left in the section past the offset
2915// and which section is was being referenced. If the Address is not in a
2916// section nullptr is returned.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00002917static const char *get_pointer_64(uint64_t Address, uint32_t &offset,
2918 uint32_t &left, SectionRef &S,
Kevin Enderby846c0002015-04-16 17:19:59 +00002919 DisassembleInfo *info,
2920 bool objc_only = false) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002921 offset = 0;
2922 left = 0;
2923 S = SectionRef();
2924 for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) {
2925 uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress();
2926 uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize();
Kevin Enderby46e642f2015-10-08 22:50:55 +00002927 if (SectSize == 0)
2928 continue;
Kevin Enderby846c0002015-04-16 17:19:59 +00002929 if (objc_only) {
2930 StringRef SectName;
2931 ((*(info->Sections))[SectIdx]).getName(SectName);
2932 DataRefImpl Ref = ((*(info->Sections))[SectIdx]).getRawDataRefImpl();
2933 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
2934 if (SegName != "__OBJC" && SectName != "__cstring")
2935 continue;
2936 }
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002937 if (Address >= SectAddress && Address < SectAddress + SectSize) {
2938 S = (*(info->Sections))[SectIdx];
2939 offset = Address - SectAddress;
2940 left = SectSize - offset;
2941 StringRef SectContents;
2942 ((*(info->Sections))[SectIdx]).getContents(SectContents);
2943 return SectContents.data() + offset;
2944 }
2945 }
2946 return nullptr;
2947}
2948
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002949static const char *get_pointer_32(uint32_t Address, uint32_t &offset,
2950 uint32_t &left, SectionRef &S,
Kevin Enderby846c0002015-04-16 17:19:59 +00002951 DisassembleInfo *info,
2952 bool objc_only = false) {
2953 return get_pointer_64(Address, offset, left, S, info, objc_only);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00002954}
2955
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002956// get_symbol_64() returns the name of a symbol (or nullptr) and the address of
2957// the symbol indirectly through n_value. Based on the relocation information
2958// for the specified section offset in the specified section reference.
Kevin Enderby0fc11822015-04-01 20:57:01 +00002959// If no relocation information is found and a non-zero ReferenceValue for the
2960// symbol is passed, look up that address in the info's AddrMap.
Rafael Espindolad7a32ea2015-06-24 10:20:30 +00002961static const char *get_symbol_64(uint32_t sect_offset, SectionRef S,
2962 DisassembleInfo *info, uint64_t &n_value,
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +00002963 uint64_t ReferenceValue = 0) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002964 n_value = 0;
David Blaikie33dd45d02015-03-23 18:39:02 +00002965 if (!info->verbose)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002966 return nullptr;
2967
2968 // See if there is an external relocation entry at the sect_offset.
2969 bool reloc_found = false;
2970 DataRefImpl Rel;
2971 MachO::any_relocation_info RE;
2972 bool isExtern = false;
2973 SymbolRef Symbol;
2974 for (const RelocationRef &Reloc : S.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00002975 uint64_t RelocOffset = Reloc.getOffset();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00002976 if (RelocOffset == sect_offset) {
2977 Rel = Reloc.getRawDataRefImpl();
2978 RE = info->O->getRelocation(Rel);
2979 if (info->O->isRelocationScattered(RE))
2980 continue;
2981 isExtern = info->O->getPlainRelocationExternal(RE);
2982 if (isExtern) {
2983 symbol_iterator RelocSym = Reloc.getSymbol();
2984 Symbol = *RelocSym;
2985 }
2986 reloc_found = true;
2987 break;
2988 }
2989 }
2990 // If there is an external relocation entry for a symbol in this section
2991 // at this section_offset then use that symbol's value for the n_value
2992 // and return its name.
2993 const char *SymbolName = nullptr;
2994 if (reloc_found && isExtern) {
Rafael Espindoladea00162015-07-03 17:44:18 +00002995 n_value = Symbol.getValue();
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00002996 Expected<StringRef> NameOrError = Symbol.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00002997 if (!NameOrError)
2998 report_error(info->O->getFileName(), NameOrError.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00002999 StringRef Name = *NameOrError;
3000 if (!Name.empty()) {
3001 SymbolName = Name.data();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003002 return SymbolName;
3003 }
3004 }
3005
3006 // TODO: For fully linked images, look through the external relocation
3007 // entries off the dynamic symtab command. For these the r_offset is from the
3008 // start of the first writeable segment in the Mach-O file. So the offset
3009 // to this section from that segment is passed to this routine by the caller,
3010 // as the database_offset. Which is the difference of the section's starting
3011 // address and the first writable segment.
3012 //
3013 // NOTE: need add passing the database_offset to this routine.
3014
Kevin Enderby0fc11822015-04-01 20:57:01 +00003015 // We did not find an external relocation entry so look up the ReferenceValue
3016 // as an address of a symbol and if found return that symbol's name.
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +00003017 SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003018
3019 return SymbolName;
3020}
3021
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003022static const char *get_symbol_32(uint32_t sect_offset, SectionRef S,
3023 DisassembleInfo *info,
3024 uint32_t ReferenceValue) {
3025 uint64_t n_value64;
3026 return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue);
3027}
3028
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003029// These are structs in the Objective-C meta data and read to produce the
3030// comments for disassembly. While these are part of the ABI they are no
Zachary Turner264b5d92017-06-07 03:48:56 +00003031// public defintions. So the are here not in include/llvm/BinaryFormat/MachO.h
3032// .
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003033
3034// The cfstring object in a 64-bit Mach-O file.
3035struct cfstring64_t {
3036 uint64_t isa; // class64_t * (64-bit pointer)
3037 uint64_t flags; // flag bits
3038 uint64_t characters; // char * (64-bit pointer)
3039 uint64_t length; // number of non-NULL characters in above
3040};
3041
3042// The class object in a 64-bit Mach-O file.
3043struct class64_t {
3044 uint64_t isa; // class64_t * (64-bit pointer)
3045 uint64_t superclass; // class64_t * (64-bit pointer)
3046 uint64_t cache; // Cache (64-bit pointer)
3047 uint64_t vtable; // IMP * (64-bit pointer)
3048 uint64_t data; // class_ro64_t * (64-bit pointer)
3049};
3050
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003051struct class32_t {
3052 uint32_t isa; /* class32_t * (32-bit pointer) */
3053 uint32_t superclass; /* class32_t * (32-bit pointer) */
3054 uint32_t cache; /* Cache (32-bit pointer) */
3055 uint32_t vtable; /* IMP * (32-bit pointer) */
3056 uint32_t data; /* class_ro32_t * (32-bit pointer) */
3057};
3058
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003059struct class_ro64_t {
3060 uint32_t flags;
3061 uint32_t instanceStart;
3062 uint32_t instanceSize;
3063 uint32_t reserved;
3064 uint64_t ivarLayout; // const uint8_t * (64-bit pointer)
3065 uint64_t name; // const char * (64-bit pointer)
3066 uint64_t baseMethods; // const method_list_t * (64-bit pointer)
3067 uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer)
3068 uint64_t ivars; // const ivar_list_t * (64-bit pointer)
3069 uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer)
3070 uint64_t baseProperties; // const struct objc_property_list (64-bit pointer)
3071};
3072
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003073struct class_ro32_t {
3074 uint32_t flags;
3075 uint32_t instanceStart;
3076 uint32_t instanceSize;
3077 uint32_t ivarLayout; /* const uint8_t * (32-bit pointer) */
3078 uint32_t name; /* const char * (32-bit pointer) */
3079 uint32_t baseMethods; /* const method_list_t * (32-bit pointer) */
3080 uint32_t baseProtocols; /* const protocol_list_t * (32-bit pointer) */
3081 uint32_t ivars; /* const ivar_list_t * (32-bit pointer) */
3082 uint32_t weakIvarLayout; /* const uint8_t * (32-bit pointer) */
3083 uint32_t baseProperties; /* const struct objc_property_list *
3084 (32-bit pointer) */
3085};
3086
3087/* Values for class_ro{64,32}_t->flags */
Kevin Enderby0fc11822015-04-01 20:57:01 +00003088#define RO_META (1 << 0)
3089#define RO_ROOT (1 << 1)
3090#define RO_HAS_CXX_STRUCTORS (1 << 2)
3091
3092struct method_list64_t {
3093 uint32_t entsize;
3094 uint32_t count;
3095 /* struct method64_t first; These structures follow inline */
3096};
3097
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003098struct method_list32_t {
3099 uint32_t entsize;
3100 uint32_t count;
3101 /* struct method32_t first; These structures follow inline */
3102};
3103
Kevin Enderby0fc11822015-04-01 20:57:01 +00003104struct method64_t {
3105 uint64_t name; /* SEL (64-bit pointer) */
3106 uint64_t types; /* const char * (64-bit pointer) */
3107 uint64_t imp; /* IMP (64-bit pointer) */
3108};
3109
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003110struct method32_t {
3111 uint32_t name; /* SEL (32-bit pointer) */
3112 uint32_t types; /* const char * (32-bit pointer) */
3113 uint32_t imp; /* IMP (32-bit pointer) */
3114};
3115
Kevin Enderby0fc11822015-04-01 20:57:01 +00003116struct protocol_list64_t {
3117 uint64_t count; /* uintptr_t (a 64-bit value) */
3118 /* struct protocol64_t * list[0]; These pointers follow inline */
3119};
3120
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003121struct protocol_list32_t {
3122 uint32_t count; /* uintptr_t (a 32-bit value) */
3123 /* struct protocol32_t * list[0]; These pointers follow inline */
3124};
3125
Kevin Enderby0fc11822015-04-01 20:57:01 +00003126struct protocol64_t {
3127 uint64_t isa; /* id * (64-bit pointer) */
3128 uint64_t name; /* const char * (64-bit pointer) */
3129 uint64_t protocols; /* struct protocol_list64_t *
3130 (64-bit pointer) */
3131 uint64_t instanceMethods; /* method_list_t * (64-bit pointer) */
3132 uint64_t classMethods; /* method_list_t * (64-bit pointer) */
3133 uint64_t optionalInstanceMethods; /* method_list_t * (64-bit pointer) */
3134 uint64_t optionalClassMethods; /* method_list_t * (64-bit pointer) */
3135 uint64_t instanceProperties; /* struct objc_property_list *
3136 (64-bit pointer) */
3137};
3138
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003139struct protocol32_t {
3140 uint32_t isa; /* id * (32-bit pointer) */
3141 uint32_t name; /* const char * (32-bit pointer) */
3142 uint32_t protocols; /* struct protocol_list_t *
3143 (32-bit pointer) */
3144 uint32_t instanceMethods; /* method_list_t * (32-bit pointer) */
3145 uint32_t classMethods; /* method_list_t * (32-bit pointer) */
3146 uint32_t optionalInstanceMethods; /* method_list_t * (32-bit pointer) */
3147 uint32_t optionalClassMethods; /* method_list_t * (32-bit pointer) */
3148 uint32_t instanceProperties; /* struct objc_property_list *
3149 (32-bit pointer) */
3150};
3151
Kevin Enderby0fc11822015-04-01 20:57:01 +00003152struct ivar_list64_t {
3153 uint32_t entsize;
3154 uint32_t count;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003155 /* struct ivar64_t first; These structures follow inline */
3156};
3157
3158struct ivar_list32_t {
3159 uint32_t entsize;
3160 uint32_t count;
3161 /* struct ivar32_t first; These structures follow inline */
Kevin Enderby0fc11822015-04-01 20:57:01 +00003162};
3163
3164struct ivar64_t {
3165 uint64_t offset; /* uintptr_t * (64-bit pointer) */
3166 uint64_t name; /* const char * (64-bit pointer) */
3167 uint64_t type; /* const char * (64-bit pointer) */
3168 uint32_t alignment;
3169 uint32_t size;
3170};
3171
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003172struct ivar32_t {
3173 uint32_t offset; /* uintptr_t * (32-bit pointer) */
3174 uint32_t name; /* const char * (32-bit pointer) */
3175 uint32_t type; /* const char * (32-bit pointer) */
3176 uint32_t alignment;
3177 uint32_t size;
3178};
3179
Kevin Enderby0fc11822015-04-01 20:57:01 +00003180struct objc_property_list64 {
3181 uint32_t entsize;
3182 uint32_t count;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003183 /* struct objc_property64 first; These structures follow inline */
3184};
3185
3186struct objc_property_list32 {
3187 uint32_t entsize;
3188 uint32_t count;
3189 /* struct objc_property32 first; These structures follow inline */
Kevin Enderby0fc11822015-04-01 20:57:01 +00003190};
3191
3192struct objc_property64 {
3193 uint64_t name; /* const char * (64-bit pointer) */
3194 uint64_t attributes; /* const char * (64-bit pointer) */
3195};
3196
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003197struct objc_property32 {
3198 uint32_t name; /* const char * (32-bit pointer) */
3199 uint32_t attributes; /* const char * (32-bit pointer) */
3200};
3201
Kevin Enderby0fc11822015-04-01 20:57:01 +00003202struct category64_t {
3203 uint64_t name; /* const char * (64-bit pointer) */
3204 uint64_t cls; /* struct class_t * (64-bit pointer) */
3205 uint64_t instanceMethods; /* struct method_list_t * (64-bit pointer) */
3206 uint64_t classMethods; /* struct method_list_t * (64-bit pointer) */
3207 uint64_t protocols; /* struct protocol_list_t * (64-bit pointer) */
3208 uint64_t instanceProperties; /* struct objc_property_list *
3209 (64-bit pointer) */
3210};
3211
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003212struct category32_t {
3213 uint32_t name; /* const char * (32-bit pointer) */
3214 uint32_t cls; /* struct class_t * (32-bit pointer) */
3215 uint32_t instanceMethods; /* struct method_list_t * (32-bit pointer) */
3216 uint32_t classMethods; /* struct method_list_t * (32-bit pointer) */
3217 uint32_t protocols; /* struct protocol_list_t * (32-bit pointer) */
3218 uint32_t instanceProperties; /* struct objc_property_list *
3219 (32-bit pointer) */
3220};
3221
Kevin Enderby0fc11822015-04-01 20:57:01 +00003222struct objc_image_info64 {
3223 uint32_t version;
3224 uint32_t flags;
3225};
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003226struct objc_image_info32 {
3227 uint32_t version;
3228 uint32_t flags;
3229};
Kevin Enderby846c0002015-04-16 17:19:59 +00003230struct imageInfo_t {
3231 uint32_t version;
3232 uint32_t flags;
3233};
Kevin Enderby0fc11822015-04-01 20:57:01 +00003234/* masks for objc_image_info.flags */
3235#define OBJC_IMAGE_IS_REPLACEMENT (1 << 0)
3236#define OBJC_IMAGE_SUPPORTS_GC (1 << 1)
3237
3238struct message_ref64 {
3239 uint64_t imp; /* IMP (64-bit pointer) */
3240 uint64_t sel; /* SEL (64-bit pointer) */
3241};
3242
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003243struct message_ref32 {
3244 uint32_t imp; /* IMP (32-bit pointer) */
3245 uint32_t sel; /* SEL (32-bit pointer) */
3246};
3247
Kevin Enderby846c0002015-04-16 17:19:59 +00003248// Objective-C 1 (32-bit only) meta data structs.
3249
3250struct objc_module_t {
3251 uint32_t version;
3252 uint32_t size;
3253 uint32_t name; /* char * (32-bit pointer) */
3254 uint32_t symtab; /* struct objc_symtab * (32-bit pointer) */
3255};
3256
3257struct objc_symtab_t {
3258 uint32_t sel_ref_cnt;
3259 uint32_t refs; /* SEL * (32-bit pointer) */
3260 uint16_t cls_def_cnt;
3261 uint16_t cat_def_cnt;
3262 // uint32_t defs[1]; /* void * (32-bit pointer) variable size */
3263};
3264
3265struct objc_class_t {
3266 uint32_t isa; /* struct objc_class * (32-bit pointer) */
3267 uint32_t super_class; /* struct objc_class * (32-bit pointer) */
3268 uint32_t name; /* const char * (32-bit pointer) */
3269 int32_t version;
3270 int32_t info;
3271 int32_t instance_size;
3272 uint32_t ivars; /* struct objc_ivar_list * (32-bit pointer) */
3273 uint32_t methodLists; /* struct objc_method_list ** (32-bit pointer) */
3274 uint32_t cache; /* struct objc_cache * (32-bit pointer) */
3275 uint32_t protocols; /* struct objc_protocol_list * (32-bit pointer) */
3276};
3277
3278#define CLS_GETINFO(cls, infomask) ((cls)->info & (infomask))
3279// class is not a metaclass
3280#define CLS_CLASS 0x1
3281// class is a metaclass
3282#define CLS_META 0x2
3283
3284struct objc_category_t {
3285 uint32_t category_name; /* char * (32-bit pointer) */
3286 uint32_t class_name; /* char * (32-bit pointer) */
3287 uint32_t instance_methods; /* struct objc_method_list * (32-bit pointer) */
3288 uint32_t class_methods; /* struct objc_method_list * (32-bit pointer) */
3289 uint32_t protocols; /* struct objc_protocol_list * (32-bit ptr) */
3290};
3291
3292struct objc_ivar_t {
3293 uint32_t ivar_name; /* char * (32-bit pointer) */
3294 uint32_t ivar_type; /* char * (32-bit pointer) */
3295 int32_t ivar_offset;
3296};
3297
3298struct objc_ivar_list_t {
3299 int32_t ivar_count;
3300 // struct objc_ivar_t ivar_list[1]; /* variable length structure */
3301};
3302
3303struct objc_method_list_t {
3304 uint32_t obsolete; /* struct objc_method_list * (32-bit pointer) */
3305 int32_t method_count;
3306 // struct objc_method_t method_list[1]; /* variable length structure */
3307};
3308
3309struct objc_method_t {
3310 uint32_t method_name; /* SEL, aka struct objc_selector * (32-bit pointer) */
3311 uint32_t method_types; /* char * (32-bit pointer) */
3312 uint32_t method_imp; /* IMP, aka function pointer, (*IMP)(id, SEL, ...)
3313 (32-bit pointer) */
3314};
3315
3316struct objc_protocol_list_t {
3317 uint32_t next; /* struct objc_protocol_list * (32-bit pointer) */
3318 int32_t count;
3319 // uint32_t list[1]; /* Protocol *, aka struct objc_protocol_t *
3320 // (32-bit pointer) */
3321};
3322
3323struct objc_protocol_t {
3324 uint32_t isa; /* struct objc_class * (32-bit pointer) */
3325 uint32_t protocol_name; /* char * (32-bit pointer) */
3326 uint32_t protocol_list; /* struct objc_protocol_list * (32-bit pointer) */
3327 uint32_t instance_methods; /* struct objc_method_description_list *
3328 (32-bit pointer) */
3329 uint32_t class_methods; /* struct objc_method_description_list *
3330 (32-bit pointer) */
3331};
3332
3333struct objc_method_description_list_t {
3334 int32_t count;
3335 // struct objc_method_description_t list[1];
3336};
3337
3338struct objc_method_description_t {
3339 uint32_t name; /* SEL, aka struct objc_selector * (32-bit pointer) */
3340 uint32_t types; /* char * (32-bit pointer) */
3341};
3342
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003343inline void swapStruct(struct cfstring64_t &cfs) {
3344 sys::swapByteOrder(cfs.isa);
3345 sys::swapByteOrder(cfs.flags);
3346 sys::swapByteOrder(cfs.characters);
3347 sys::swapByteOrder(cfs.length);
3348}
3349
3350inline void swapStruct(struct class64_t &c) {
3351 sys::swapByteOrder(c.isa);
3352 sys::swapByteOrder(c.superclass);
3353 sys::swapByteOrder(c.cache);
3354 sys::swapByteOrder(c.vtable);
3355 sys::swapByteOrder(c.data);
3356}
3357
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003358inline void swapStruct(struct class32_t &c) {
3359 sys::swapByteOrder(c.isa);
3360 sys::swapByteOrder(c.superclass);
3361 sys::swapByteOrder(c.cache);
3362 sys::swapByteOrder(c.vtable);
3363 sys::swapByteOrder(c.data);
3364}
3365
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003366inline void swapStruct(struct class_ro64_t &cro) {
3367 sys::swapByteOrder(cro.flags);
3368 sys::swapByteOrder(cro.instanceStart);
3369 sys::swapByteOrder(cro.instanceSize);
3370 sys::swapByteOrder(cro.reserved);
3371 sys::swapByteOrder(cro.ivarLayout);
3372 sys::swapByteOrder(cro.name);
3373 sys::swapByteOrder(cro.baseMethods);
3374 sys::swapByteOrder(cro.baseProtocols);
3375 sys::swapByteOrder(cro.ivars);
3376 sys::swapByteOrder(cro.weakIvarLayout);
3377 sys::swapByteOrder(cro.baseProperties);
3378}
3379
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003380inline void swapStruct(struct class_ro32_t &cro) {
3381 sys::swapByteOrder(cro.flags);
3382 sys::swapByteOrder(cro.instanceStart);
3383 sys::swapByteOrder(cro.instanceSize);
3384 sys::swapByteOrder(cro.ivarLayout);
3385 sys::swapByteOrder(cro.name);
3386 sys::swapByteOrder(cro.baseMethods);
3387 sys::swapByteOrder(cro.baseProtocols);
3388 sys::swapByteOrder(cro.ivars);
3389 sys::swapByteOrder(cro.weakIvarLayout);
3390 sys::swapByteOrder(cro.baseProperties);
3391}
3392
Kevin Enderby0fc11822015-04-01 20:57:01 +00003393inline void swapStruct(struct method_list64_t &ml) {
3394 sys::swapByteOrder(ml.entsize);
3395 sys::swapByteOrder(ml.count);
3396}
3397
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003398inline void swapStruct(struct method_list32_t &ml) {
3399 sys::swapByteOrder(ml.entsize);
3400 sys::swapByteOrder(ml.count);
3401}
3402
Kevin Enderby0fc11822015-04-01 20:57:01 +00003403inline void swapStruct(struct method64_t &m) {
3404 sys::swapByteOrder(m.name);
3405 sys::swapByteOrder(m.types);
3406 sys::swapByteOrder(m.imp);
3407}
3408
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003409inline void swapStruct(struct method32_t &m) {
3410 sys::swapByteOrder(m.name);
3411 sys::swapByteOrder(m.types);
3412 sys::swapByteOrder(m.imp);
3413}
3414
Kevin Enderby0fc11822015-04-01 20:57:01 +00003415inline void swapStruct(struct protocol_list64_t &pl) {
3416 sys::swapByteOrder(pl.count);
3417}
3418
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003419inline void swapStruct(struct protocol_list32_t &pl) {
3420 sys::swapByteOrder(pl.count);
3421}
3422
Kevin Enderby0fc11822015-04-01 20:57:01 +00003423inline void swapStruct(struct protocol64_t &p) {
3424 sys::swapByteOrder(p.isa);
3425 sys::swapByteOrder(p.name);
3426 sys::swapByteOrder(p.protocols);
3427 sys::swapByteOrder(p.instanceMethods);
3428 sys::swapByteOrder(p.classMethods);
3429 sys::swapByteOrder(p.optionalInstanceMethods);
3430 sys::swapByteOrder(p.optionalClassMethods);
3431 sys::swapByteOrder(p.instanceProperties);
3432}
3433
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003434inline void swapStruct(struct protocol32_t &p) {
3435 sys::swapByteOrder(p.isa);
3436 sys::swapByteOrder(p.name);
3437 sys::swapByteOrder(p.protocols);
3438 sys::swapByteOrder(p.instanceMethods);
3439 sys::swapByteOrder(p.classMethods);
3440 sys::swapByteOrder(p.optionalInstanceMethods);
3441 sys::swapByteOrder(p.optionalClassMethods);
3442 sys::swapByteOrder(p.instanceProperties);
3443}
3444
Kevin Enderby0fc11822015-04-01 20:57:01 +00003445inline void swapStruct(struct ivar_list64_t &il) {
3446 sys::swapByteOrder(il.entsize);
3447 sys::swapByteOrder(il.count);
3448}
3449
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003450inline void swapStruct(struct ivar_list32_t &il) {
3451 sys::swapByteOrder(il.entsize);
3452 sys::swapByteOrder(il.count);
3453}
3454
Kevin Enderby0fc11822015-04-01 20:57:01 +00003455inline void swapStruct(struct ivar64_t &i) {
3456 sys::swapByteOrder(i.offset);
3457 sys::swapByteOrder(i.name);
3458 sys::swapByteOrder(i.type);
3459 sys::swapByteOrder(i.alignment);
3460 sys::swapByteOrder(i.size);
3461}
3462
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003463inline void swapStruct(struct ivar32_t &i) {
3464 sys::swapByteOrder(i.offset);
3465 sys::swapByteOrder(i.name);
3466 sys::swapByteOrder(i.type);
3467 sys::swapByteOrder(i.alignment);
3468 sys::swapByteOrder(i.size);
3469}
3470
Kevin Enderby0fc11822015-04-01 20:57:01 +00003471inline void swapStruct(struct objc_property_list64 &pl) {
3472 sys::swapByteOrder(pl.entsize);
3473 sys::swapByteOrder(pl.count);
3474}
3475
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003476inline void swapStruct(struct objc_property_list32 &pl) {
3477 sys::swapByteOrder(pl.entsize);
3478 sys::swapByteOrder(pl.count);
3479}
3480
Kevin Enderby0fc11822015-04-01 20:57:01 +00003481inline void swapStruct(struct objc_property64 &op) {
3482 sys::swapByteOrder(op.name);
3483 sys::swapByteOrder(op.attributes);
3484}
3485
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003486inline void swapStruct(struct objc_property32 &op) {
3487 sys::swapByteOrder(op.name);
3488 sys::swapByteOrder(op.attributes);
3489}
3490
Kevin Enderby0fc11822015-04-01 20:57:01 +00003491inline void swapStruct(struct category64_t &c) {
3492 sys::swapByteOrder(c.name);
3493 sys::swapByteOrder(c.cls);
3494 sys::swapByteOrder(c.instanceMethods);
3495 sys::swapByteOrder(c.classMethods);
3496 sys::swapByteOrder(c.protocols);
3497 sys::swapByteOrder(c.instanceProperties);
3498}
3499
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003500inline void swapStruct(struct category32_t &c) {
3501 sys::swapByteOrder(c.name);
3502 sys::swapByteOrder(c.cls);
3503 sys::swapByteOrder(c.instanceMethods);
3504 sys::swapByteOrder(c.classMethods);
3505 sys::swapByteOrder(c.protocols);
3506 sys::swapByteOrder(c.instanceProperties);
3507}
3508
Kevin Enderby0fc11822015-04-01 20:57:01 +00003509inline void swapStruct(struct objc_image_info64 &o) {
3510 sys::swapByteOrder(o.version);
3511 sys::swapByteOrder(o.flags);
3512}
3513
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003514inline void swapStruct(struct objc_image_info32 &o) {
3515 sys::swapByteOrder(o.version);
3516 sys::swapByteOrder(o.flags);
3517}
3518
Kevin Enderby846c0002015-04-16 17:19:59 +00003519inline void swapStruct(struct imageInfo_t &o) {
3520 sys::swapByteOrder(o.version);
3521 sys::swapByteOrder(o.flags);
3522}
3523
Kevin Enderby0fc11822015-04-01 20:57:01 +00003524inline void swapStruct(struct message_ref64 &mr) {
3525 sys::swapByteOrder(mr.imp);
3526 sys::swapByteOrder(mr.sel);
3527}
3528
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003529inline void swapStruct(struct message_ref32 &mr) {
3530 sys::swapByteOrder(mr.imp);
3531 sys::swapByteOrder(mr.sel);
3532}
3533
Kevin Enderby846c0002015-04-16 17:19:59 +00003534inline void swapStruct(struct objc_module_t &module) {
3535 sys::swapByteOrder(module.version);
3536 sys::swapByteOrder(module.size);
3537 sys::swapByteOrder(module.name);
3538 sys::swapByteOrder(module.symtab);
Jingyue Wufedecc42015-04-16 18:43:44 +00003539}
Kevin Enderby846c0002015-04-16 17:19:59 +00003540
3541inline void swapStruct(struct objc_symtab_t &symtab) {
3542 sys::swapByteOrder(symtab.sel_ref_cnt);
3543 sys::swapByteOrder(symtab.refs);
3544 sys::swapByteOrder(symtab.cls_def_cnt);
3545 sys::swapByteOrder(symtab.cat_def_cnt);
Jingyue Wufedecc42015-04-16 18:43:44 +00003546}
Kevin Enderby846c0002015-04-16 17:19:59 +00003547
3548inline void swapStruct(struct objc_class_t &objc_class) {
3549 sys::swapByteOrder(objc_class.isa);
3550 sys::swapByteOrder(objc_class.super_class);
3551 sys::swapByteOrder(objc_class.name);
3552 sys::swapByteOrder(objc_class.version);
3553 sys::swapByteOrder(objc_class.info);
3554 sys::swapByteOrder(objc_class.instance_size);
3555 sys::swapByteOrder(objc_class.ivars);
3556 sys::swapByteOrder(objc_class.methodLists);
3557 sys::swapByteOrder(objc_class.cache);
3558 sys::swapByteOrder(objc_class.protocols);
Jingyue Wufedecc42015-04-16 18:43:44 +00003559}
Kevin Enderby846c0002015-04-16 17:19:59 +00003560
3561inline void swapStruct(struct objc_category_t &objc_category) {
3562 sys::swapByteOrder(objc_category.category_name);
3563 sys::swapByteOrder(objc_category.class_name);
3564 sys::swapByteOrder(objc_category.instance_methods);
3565 sys::swapByteOrder(objc_category.class_methods);
3566 sys::swapByteOrder(objc_category.protocols);
3567}
3568
3569inline void swapStruct(struct objc_ivar_list_t &objc_ivar_list) {
3570 sys::swapByteOrder(objc_ivar_list.ivar_count);
3571}
3572
3573inline void swapStruct(struct objc_ivar_t &objc_ivar) {
3574 sys::swapByteOrder(objc_ivar.ivar_name);
3575 sys::swapByteOrder(objc_ivar.ivar_type);
3576 sys::swapByteOrder(objc_ivar.ivar_offset);
Jingyue Wufedecc42015-04-16 18:43:44 +00003577}
Kevin Enderby846c0002015-04-16 17:19:59 +00003578
3579inline void swapStruct(struct objc_method_list_t &method_list) {
3580 sys::swapByteOrder(method_list.obsolete);
3581 sys::swapByteOrder(method_list.method_count);
3582}
3583
3584inline void swapStruct(struct objc_method_t &method) {
3585 sys::swapByteOrder(method.method_name);
3586 sys::swapByteOrder(method.method_types);
3587 sys::swapByteOrder(method.method_imp);
3588}
3589
3590inline void swapStruct(struct objc_protocol_list_t &protocol_list) {
3591 sys::swapByteOrder(protocol_list.next);
3592 sys::swapByteOrder(protocol_list.count);
3593}
3594
3595inline void swapStruct(struct objc_protocol_t &protocol) {
3596 sys::swapByteOrder(protocol.isa);
3597 sys::swapByteOrder(protocol.protocol_name);
3598 sys::swapByteOrder(protocol.protocol_list);
3599 sys::swapByteOrder(protocol.instance_methods);
3600 sys::swapByteOrder(protocol.class_methods);
3601}
3602
3603inline void swapStruct(struct objc_method_description_list_t &mdl) {
3604 sys::swapByteOrder(mdl.count);
3605}
3606
3607inline void swapStruct(struct objc_method_description_t &md) {
3608 sys::swapByteOrder(md.name);
3609 sys::swapByteOrder(md.types);
3610}
3611
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003612static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
3613 struct DisassembleInfo *info);
3614
3615// get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
3616// to an Objective-C class and returns the class name. It is also passed the
3617// address of the pointer, so when the pointer is zero as it can be in an .o
3618// file, that is used to look for an external relocation entry with a symbol
3619// name.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003620static const char *get_objc2_64bit_class_name(uint64_t pointer_value,
3621 uint64_t ReferenceValue,
3622 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003623 const char *r;
3624 uint32_t offset, left;
3625 SectionRef S;
3626
3627 // The pointer_value can be 0 in an object file and have a relocation
3628 // entry for the class symbol at the ReferenceValue (the address of the
3629 // pointer).
3630 if (pointer_value == 0) {
3631 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3632 if (r == nullptr || left < sizeof(uint64_t))
3633 return nullptr;
3634 uint64_t n_value;
3635 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3636 if (symbol_name == nullptr)
3637 return nullptr;
Hans Wennborgdb53e302014-10-23 21:59:17 +00003638 const char *class_name = strrchr(symbol_name, '$');
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003639 if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0')
3640 return class_name + 2;
3641 else
3642 return nullptr;
3643 }
3644
3645 // The case were the pointer_value is non-zero and points to a class defined
3646 // in this Mach-O file.
3647 r = get_pointer_64(pointer_value, offset, left, S, info);
3648 if (r == nullptr || left < sizeof(struct class64_t))
3649 return nullptr;
3650 struct class64_t c;
3651 memcpy(&c, r, sizeof(struct class64_t));
3652 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3653 swapStruct(c);
3654 if (c.data == 0)
3655 return nullptr;
3656 r = get_pointer_64(c.data, offset, left, S, info);
3657 if (r == nullptr || left < sizeof(struct class_ro64_t))
3658 return nullptr;
3659 struct class_ro64_t cro;
3660 memcpy(&cro, r, sizeof(struct class_ro64_t));
3661 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3662 swapStruct(cro);
3663 if (cro.name == 0)
3664 return nullptr;
3665 const char *name = get_pointer_64(cro.name, offset, left, S, info);
3666 return name;
3667}
3668
3669// get_objc2_64bit_cfstring_name is used for disassembly and is passed a
3670// pointer to a cfstring and returns its name or nullptr.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003671static const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue,
3672 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003673 const char *r, *name;
3674 uint32_t offset, left;
3675 SectionRef S;
3676 struct cfstring64_t cfs;
3677 uint64_t cfs_characters;
3678
3679 r = get_pointer_64(ReferenceValue, offset, left, S, info);
3680 if (r == nullptr || left < sizeof(struct cfstring64_t))
3681 return nullptr;
3682 memcpy(&cfs, r, sizeof(struct cfstring64_t));
3683 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3684 swapStruct(cfs);
3685 if (cfs.characters == 0) {
3686 uint64_t n_value;
3687 const char *symbol_name = get_symbol_64(
3688 offset + offsetof(struct cfstring64_t, characters), S, info, n_value);
3689 if (symbol_name == nullptr)
3690 return nullptr;
3691 cfs_characters = n_value;
3692 } else
3693 cfs_characters = cfs.characters;
3694 name = get_pointer_64(cfs_characters, offset, left, S, info);
3695
3696 return name;
3697}
3698
3699// get_objc2_64bit_selref() is used for disassembly and is passed a the address
3700// of a pointer to an Objective-C selector reference when the pointer value is
3701// zero as in a .o file and is likely to have a external relocation entry with
3702// who's symbol's n_value is the real pointer to the selector name. If that is
3703// the case the real pointer to the selector name is returned else 0 is
3704// returned
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00003705static uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue,
3706 struct DisassembleInfo *info) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00003707 uint32_t offset, left;
3708 SectionRef S;
3709
3710 const char *r = get_pointer_64(ReferenceValue, offset, left, S, info);
3711 if (r == nullptr || left < sizeof(uint64_t))
3712 return 0;
3713 uint64_t n_value;
3714 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
3715 if (symbol_name == nullptr)
3716 return 0;
3717 return n_value;
3718}
3719
Kevin Enderby0fc11822015-04-01 20:57:01 +00003720static const SectionRef get_section(MachOObjectFile *O, const char *segname,
3721 const char *sectname) {
3722 for (const SectionRef &Section : O->sections()) {
3723 StringRef SectName;
3724 Section.getName(SectName);
3725 DataRefImpl Ref = Section.getRawDataRefImpl();
3726 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3727 if (SegName == segname && SectName == sectname)
3728 return Section;
3729 }
3730 return SectionRef();
3731}
3732
3733static void
3734walk_pointer_list_64(const char *listname, const SectionRef S,
3735 MachOObjectFile *O, struct DisassembleInfo *info,
3736 void (*func)(uint64_t, struct DisassembleInfo *info)) {
3737 if (S == SectionRef())
3738 return;
3739
3740 StringRef SectName;
3741 S.getName(SectName);
3742 DataRefImpl Ref = S.getRawDataRefImpl();
3743 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3744 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3745
3746 StringRef BytesStr;
3747 S.getContents(BytesStr);
3748 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3749
3750 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) {
3751 uint32_t left = S.getSize() - i;
3752 uint32_t size = left < sizeof(uint64_t) ? left : sizeof(uint64_t);
3753 uint64_t p = 0;
3754 memcpy(&p, Contents + i, size);
3755 if (i + sizeof(uint64_t) > S.getSize())
3756 outs() << listname << " list pointer extends past end of (" << SegName
3757 << "," << SectName << ") section\n";
3758 outs() << format("%016" PRIx64, S.getAddress() + i) << " ";
3759
3760 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3761 sys::swapByteOrder(p);
3762
3763 uint64_t n_value = 0;
3764 const char *name = get_symbol_64(i, S, info, n_value, p);
3765 if (name == nullptr)
3766 name = get_dyld_bind_info_symbolname(S.getAddress() + i, info);
3767
3768 if (n_value != 0) {
3769 outs() << format("0x%" PRIx64, n_value);
3770 if (p != 0)
3771 outs() << " + " << format("0x%" PRIx64, p);
3772 } else
3773 outs() << format("0x%" PRIx64, p);
3774 if (name != nullptr)
3775 outs() << " " << name;
3776 outs() << "\n";
3777
3778 p += n_value;
3779 if (func)
3780 func(p, info);
3781 }
3782}
3783
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003784static void
3785walk_pointer_list_32(const char *listname, const SectionRef S,
3786 MachOObjectFile *O, struct DisassembleInfo *info,
3787 void (*func)(uint32_t, struct DisassembleInfo *info)) {
3788 if (S == SectionRef())
3789 return;
3790
3791 StringRef SectName;
3792 S.getName(SectName);
3793 DataRefImpl Ref = S.getRawDataRefImpl();
3794 StringRef SegName = O->getSectionFinalSegmentName(Ref);
3795 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
3796
3797 StringRef BytesStr;
3798 S.getContents(BytesStr);
3799 const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
3800
3801 for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) {
3802 uint32_t left = S.getSize() - i;
3803 uint32_t size = left < sizeof(uint32_t) ? left : sizeof(uint32_t);
3804 uint32_t p = 0;
3805 memcpy(&p, Contents + i, size);
3806 if (i + sizeof(uint32_t) > S.getSize())
3807 outs() << listname << " list pointer extends past end of (" << SegName
3808 << "," << SectName << ") section\n";
Kevin Enderbycf261312015-04-06 22:33:43 +00003809 uint32_t Address = S.getAddress() + i;
3810 outs() << format("%08" PRIx32, Address) << " ";
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003811
3812 if (O->isLittleEndian() != sys::IsLittleEndianHost)
3813 sys::swapByteOrder(p);
3814 outs() << format("0x%" PRIx32, p);
3815
3816 const char *name = get_symbol_32(i, S, info, p);
3817 if (name != nullptr)
3818 outs() << " " << name;
3819 outs() << "\n";
3820
3821 if (func)
3822 func(p, info);
3823 }
3824}
3825
3826static void print_layout_map(const char *layout_map, uint32_t left) {
Kevin Enderbya59824a2015-10-06 22:27:08 +00003827 if (layout_map == nullptr)
3828 return;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003829 outs() << " layout map: ";
3830 do {
3831 outs() << format("0x%02" PRIx32, (*layout_map) & 0xff) << " ";
3832 left--;
3833 layout_map++;
3834 } while (*layout_map != '\0' && left != 0);
3835 outs() << "\n";
3836}
3837
Kevin Enderby0fc11822015-04-01 20:57:01 +00003838static void print_layout_map64(uint64_t p, struct DisassembleInfo *info) {
3839 uint32_t offset, left;
3840 SectionRef S;
3841 const char *layout_map;
3842
3843 if (p == 0)
3844 return;
3845 layout_map = get_pointer_64(p, offset, left, S, info);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003846 print_layout_map(layout_map, left);
3847}
3848
3849static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) {
3850 uint32_t offset, left;
3851 SectionRef S;
3852 const char *layout_map;
3853
3854 if (p == 0)
3855 return;
3856 layout_map = get_pointer_32(p, offset, left, S, info);
3857 print_layout_map(layout_map, left);
Kevin Enderby0fc11822015-04-01 20:57:01 +00003858}
3859
3860static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info,
3861 const char *indent) {
3862 struct method_list64_t ml;
3863 struct method64_t m;
3864 const char *r;
3865 uint32_t offset, xoffset, left, i;
3866 SectionRef S, xS;
3867 const char *name, *sym_name;
3868 uint64_t n_value;
3869
3870 r = get_pointer_64(p, offset, left, S, info);
3871 if (r == nullptr)
3872 return;
3873 memset(&ml, '\0', sizeof(struct method_list64_t));
3874 if (left < sizeof(struct method_list64_t)) {
3875 memcpy(&ml, r, left);
3876 outs() << " (method_list_t entends past the end of the section)\n";
3877 } else
3878 memcpy(&ml, r, sizeof(struct method_list64_t));
3879 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3880 swapStruct(ml);
3881 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3882 outs() << indent << "\t\t count " << ml.count << "\n";
3883
3884 p += sizeof(struct method_list64_t);
3885 offset += sizeof(struct method_list64_t);
3886 for (i = 0; i < ml.count; i++) {
3887 r = get_pointer_64(p, offset, left, S, info);
3888 if (r == nullptr)
3889 return;
3890 memset(&m, '\0', sizeof(struct method64_t));
3891 if (left < sizeof(struct method64_t)) {
Kevin Enderbya59824a2015-10-06 22:27:08 +00003892 memcpy(&m, r, left);
3893 outs() << indent << " (method_t extends past the end of the section)\n";
Kevin Enderby0fc11822015-04-01 20:57:01 +00003894 } else
3895 memcpy(&m, r, sizeof(struct method64_t));
3896 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3897 swapStruct(m);
3898
3899 outs() << indent << "\t\t name ";
3900 sym_name = get_symbol_64(offset + offsetof(struct method64_t, name), S,
3901 info, n_value, m.name);
3902 if (n_value != 0) {
3903 if (info->verbose && sym_name != nullptr)
3904 outs() << sym_name;
3905 else
3906 outs() << format("0x%" PRIx64, n_value);
3907 if (m.name != 0)
3908 outs() << " + " << format("0x%" PRIx64, m.name);
3909 } else
3910 outs() << format("0x%" PRIx64, m.name);
3911 name = get_pointer_64(m.name + n_value, xoffset, left, xS, info);
3912 if (name != nullptr)
3913 outs() << format(" %.*s", left, name);
3914 outs() << "\n";
3915
3916 outs() << indent << "\t\t types ";
3917 sym_name = get_symbol_64(offset + offsetof(struct method64_t, types), S,
3918 info, n_value, m.types);
3919 if (n_value != 0) {
3920 if (info->verbose && sym_name != nullptr)
3921 outs() << sym_name;
3922 else
3923 outs() << format("0x%" PRIx64, n_value);
3924 if (m.types != 0)
3925 outs() << " + " << format("0x%" PRIx64, m.types);
3926 } else
3927 outs() << format("0x%" PRIx64, m.types);
3928 name = get_pointer_64(m.types + n_value, xoffset, left, xS, info);
3929 if (name != nullptr)
3930 outs() << format(" %.*s", left, name);
3931 outs() << "\n";
3932
3933 outs() << indent << "\t\t imp ";
3934 name = get_symbol_64(offset + offsetof(struct method64_t, imp), S, info,
3935 n_value, m.imp);
3936 if (info->verbose && name == nullptr) {
3937 if (n_value != 0) {
3938 outs() << format("0x%" PRIx64, n_value) << " ";
3939 if (m.imp != 0)
3940 outs() << "+ " << format("0x%" PRIx64, m.imp) << " ";
3941 } else
3942 outs() << format("0x%" PRIx64, m.imp) << " ";
3943 }
3944 if (name != nullptr)
3945 outs() << name;
3946 outs() << "\n";
3947
3948 p += sizeof(struct method64_t);
3949 offset += sizeof(struct method64_t);
3950 }
3951}
3952
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003953static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info,
3954 const char *indent) {
3955 struct method_list32_t ml;
3956 struct method32_t m;
Kevin Enderby846c0002015-04-16 17:19:59 +00003957 const char *r, *name;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003958 uint32_t offset, xoffset, left, i;
3959 SectionRef S, xS;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00003960
3961 r = get_pointer_32(p, offset, left, S, info);
3962 if (r == nullptr)
3963 return;
3964 memset(&ml, '\0', sizeof(struct method_list32_t));
3965 if (left < sizeof(struct method_list32_t)) {
3966 memcpy(&ml, r, left);
3967 outs() << " (method_list_t entends past the end of the section)\n";
3968 } else
3969 memcpy(&ml, r, sizeof(struct method_list32_t));
3970 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3971 swapStruct(ml);
3972 outs() << indent << "\t\t entsize " << ml.entsize << "\n";
3973 outs() << indent << "\t\t count " << ml.count << "\n";
3974
3975 p += sizeof(struct method_list32_t);
3976 offset += sizeof(struct method_list32_t);
3977 for (i = 0; i < ml.count; i++) {
3978 r = get_pointer_32(p, offset, left, S, info);
3979 if (r == nullptr)
3980 return;
3981 memset(&m, '\0', sizeof(struct method32_t));
3982 if (left < sizeof(struct method32_t)) {
3983 memcpy(&ml, r, left);
3984 outs() << indent << " (method_t entends past the end of the section)\n";
3985 } else
3986 memcpy(&m, r, sizeof(struct method32_t));
3987 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
3988 swapStruct(m);
3989
3990 outs() << indent << "\t\t name " << format("0x%" PRIx32, m.name);
3991 name = get_pointer_32(m.name, xoffset, left, xS, info);
3992 if (name != nullptr)
3993 outs() << format(" %.*s", left, name);
3994 outs() << "\n";
3995
3996 outs() << indent << "\t\t types " << format("0x%" PRIx32, m.types);
3997 name = get_pointer_32(m.types, xoffset, left, xS, info);
3998 if (name != nullptr)
3999 outs() << format(" %.*s", left, name);
4000 outs() << "\n";
4001
4002 outs() << indent << "\t\t imp " << format("0x%" PRIx32, m.imp);
4003 name = get_symbol_32(offset + offsetof(struct method32_t, imp), S, info,
4004 m.imp);
4005 if (name != nullptr)
4006 outs() << " " << name;
4007 outs() << "\n";
4008
4009 p += sizeof(struct method32_t);
4010 offset += sizeof(struct method32_t);
4011 }
4012}
4013
Kevin Enderby846c0002015-04-16 17:19:59 +00004014static bool print_method_list(uint32_t p, struct DisassembleInfo *info) {
4015 uint32_t offset, left, xleft;
4016 SectionRef S;
4017 struct objc_method_list_t method_list;
4018 struct objc_method_t method;
4019 const char *r, *methods, *name, *SymbolName;
4020 int32_t i;
4021
4022 r = get_pointer_32(p, offset, left, S, info, true);
4023 if (r == nullptr)
4024 return true;
4025
4026 outs() << "\n";
4027 if (left > sizeof(struct objc_method_list_t)) {
4028 memcpy(&method_list, r, sizeof(struct objc_method_list_t));
4029 } else {
4030 outs() << "\t\t objc_method_list extends past end of the section\n";
4031 memset(&method_list, '\0', sizeof(struct objc_method_list_t));
4032 memcpy(&method_list, r, left);
4033 }
4034 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4035 swapStruct(method_list);
4036
4037 outs() << "\t\t obsolete "
4038 << format("0x%08" PRIx32, method_list.obsolete) << "\n";
4039 outs() << "\t\t method_count " << method_list.method_count << "\n";
4040
4041 methods = r + sizeof(struct objc_method_list_t);
4042 for (i = 0; i < method_list.method_count; i++) {
4043 if ((i + 1) * sizeof(struct objc_method_t) > left) {
4044 outs() << "\t\t remaining method's extend past the of the section\n";
4045 break;
4046 }
4047 memcpy(&method, methods + i * sizeof(struct objc_method_t),
4048 sizeof(struct objc_method_t));
4049 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4050 swapStruct(method);
4051
4052 outs() << "\t\t method_name "
4053 << format("0x%08" PRIx32, method.method_name);
4054 if (info->verbose) {
4055 name = get_pointer_32(method.method_name, offset, xleft, S, info, true);
4056 if (name != nullptr)
4057 outs() << format(" %.*s", xleft, name);
4058 else
4059 outs() << " (not in an __OBJC section)";
4060 }
4061 outs() << "\n";
4062
4063 outs() << "\t\t method_types "
4064 << format("0x%08" PRIx32, method.method_types);
4065 if (info->verbose) {
4066 name = get_pointer_32(method.method_types, offset, xleft, S, info, true);
4067 if (name != nullptr)
4068 outs() << format(" %.*s", xleft, name);
4069 else
4070 outs() << " (not in an __OBJC section)";
4071 }
4072 outs() << "\n";
4073
4074 outs() << "\t\t method_imp "
4075 << format("0x%08" PRIx32, method.method_imp) << " ";
4076 if (info->verbose) {
4077 SymbolName = GuessSymbolName(method.method_imp, info->AddrMap);
4078 if (SymbolName != nullptr)
4079 outs() << SymbolName;
4080 }
4081 outs() << "\n";
4082 }
4083 return false;
4084}
4085
Kevin Enderby0fc11822015-04-01 20:57:01 +00004086static void print_protocol_list64_t(uint64_t p, struct DisassembleInfo *info) {
4087 struct protocol_list64_t pl;
4088 uint64_t q, n_value;
4089 struct protocol64_t pc;
4090 const char *r;
4091 uint32_t offset, xoffset, left, i;
4092 SectionRef S, xS;
4093 const char *name, *sym_name;
4094
4095 r = get_pointer_64(p, offset, left, S, info);
4096 if (r == nullptr)
4097 return;
4098 memset(&pl, '\0', sizeof(struct protocol_list64_t));
4099 if (left < sizeof(struct protocol_list64_t)) {
4100 memcpy(&pl, r, left);
4101 outs() << " (protocol_list_t entends past the end of the section)\n";
4102 } else
4103 memcpy(&pl, r, sizeof(struct protocol_list64_t));
4104 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4105 swapStruct(pl);
4106 outs() << " count " << pl.count << "\n";
4107
4108 p += sizeof(struct protocol_list64_t);
4109 offset += sizeof(struct protocol_list64_t);
4110 for (i = 0; i < pl.count; i++) {
4111 r = get_pointer_64(p, offset, left, S, info);
4112 if (r == nullptr)
4113 return;
4114 q = 0;
4115 if (left < sizeof(uint64_t)) {
4116 memcpy(&q, r, left);
4117 outs() << " (protocol_t * entends past the end of the section)\n";
4118 } else
4119 memcpy(&q, r, sizeof(uint64_t));
4120 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4121 sys::swapByteOrder(q);
4122
4123 outs() << "\t\t list[" << i << "] ";
4124 sym_name = get_symbol_64(offset, S, info, n_value, q);
4125 if (n_value != 0) {
4126 if (info->verbose && sym_name != nullptr)
4127 outs() << sym_name;
4128 else
4129 outs() << format("0x%" PRIx64, n_value);
4130 if (q != 0)
4131 outs() << " + " << format("0x%" PRIx64, q);
4132 } else
4133 outs() << format("0x%" PRIx64, q);
4134 outs() << " (struct protocol_t *)\n";
4135
4136 r = get_pointer_64(q + n_value, offset, left, S, info);
4137 if (r == nullptr)
4138 return;
4139 memset(&pc, '\0', sizeof(struct protocol64_t));
4140 if (left < sizeof(struct protocol64_t)) {
4141 memcpy(&pc, r, left);
4142 outs() << " (protocol_t entends past the end of the section)\n";
4143 } else
4144 memcpy(&pc, r, sizeof(struct protocol64_t));
4145 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4146 swapStruct(pc);
4147
4148 outs() << "\t\t\t isa " << format("0x%" PRIx64, pc.isa) << "\n";
4149
4150 outs() << "\t\t\t name ";
4151 sym_name = get_symbol_64(offset + offsetof(struct protocol64_t, name), S,
4152 info, n_value, pc.name);
4153 if (n_value != 0) {
4154 if (info->verbose && sym_name != nullptr)
4155 outs() << sym_name;
4156 else
4157 outs() << format("0x%" PRIx64, n_value);
4158 if (pc.name != 0)
4159 outs() << " + " << format("0x%" PRIx64, pc.name);
4160 } else
4161 outs() << format("0x%" PRIx64, pc.name);
4162 name = get_pointer_64(pc.name + n_value, xoffset, left, xS, info);
4163 if (name != nullptr)
4164 outs() << format(" %.*s", left, name);
4165 outs() << "\n";
4166
4167 outs() << "\t\t\tprotocols " << format("0x%" PRIx64, pc.protocols) << "\n";
4168
4169 outs() << "\t\t instanceMethods ";
4170 sym_name =
4171 get_symbol_64(offset + offsetof(struct protocol64_t, instanceMethods),
4172 S, info, n_value, pc.instanceMethods);
4173 if (n_value != 0) {
4174 if (info->verbose && sym_name != nullptr)
4175 outs() << sym_name;
4176 else
4177 outs() << format("0x%" PRIx64, n_value);
4178 if (pc.instanceMethods != 0)
4179 outs() << " + " << format("0x%" PRIx64, pc.instanceMethods);
4180 } else
4181 outs() << format("0x%" PRIx64, pc.instanceMethods);
4182 outs() << " (struct method_list_t *)\n";
4183 if (pc.instanceMethods + n_value != 0)
4184 print_method_list64_t(pc.instanceMethods + n_value, info, "\t");
4185
4186 outs() << "\t\t classMethods ";
4187 sym_name =
4188 get_symbol_64(offset + offsetof(struct protocol64_t, classMethods), S,
4189 info, n_value, pc.classMethods);
4190 if (n_value != 0) {
4191 if (info->verbose && sym_name != nullptr)
4192 outs() << sym_name;
4193 else
4194 outs() << format("0x%" PRIx64, n_value);
4195 if (pc.classMethods != 0)
4196 outs() << " + " << format("0x%" PRIx64, pc.classMethods);
4197 } else
4198 outs() << format("0x%" PRIx64, pc.classMethods);
4199 outs() << " (struct method_list_t *)\n";
4200 if (pc.classMethods + n_value != 0)
4201 print_method_list64_t(pc.classMethods + n_value, info, "\t");
4202
4203 outs() << "\t optionalInstanceMethods "
4204 << format("0x%" PRIx64, pc.optionalInstanceMethods) << "\n";
4205 outs() << "\t optionalClassMethods "
4206 << format("0x%" PRIx64, pc.optionalClassMethods) << "\n";
4207 outs() << "\t instanceProperties "
4208 << format("0x%" PRIx64, pc.instanceProperties) << "\n";
4209
4210 p += sizeof(uint64_t);
4211 offset += sizeof(uint64_t);
4212 }
4213}
4214
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004215static void print_protocol_list32_t(uint32_t p, struct DisassembleInfo *info) {
4216 struct protocol_list32_t pl;
4217 uint32_t q;
4218 struct protocol32_t pc;
4219 const char *r;
4220 uint32_t offset, xoffset, left, i;
4221 SectionRef S, xS;
4222 const char *name;
4223
4224 r = get_pointer_32(p, offset, left, S, info);
4225 if (r == nullptr)
4226 return;
4227 memset(&pl, '\0', sizeof(struct protocol_list32_t));
4228 if (left < sizeof(struct protocol_list32_t)) {
4229 memcpy(&pl, r, left);
4230 outs() << " (protocol_list_t entends past the end of the section)\n";
4231 } else
4232 memcpy(&pl, r, sizeof(struct protocol_list32_t));
4233 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4234 swapStruct(pl);
4235 outs() << " count " << pl.count << "\n";
4236
4237 p += sizeof(struct protocol_list32_t);
4238 offset += sizeof(struct protocol_list32_t);
4239 for (i = 0; i < pl.count; i++) {
4240 r = get_pointer_32(p, offset, left, S, info);
4241 if (r == nullptr)
4242 return;
4243 q = 0;
4244 if (left < sizeof(uint32_t)) {
4245 memcpy(&q, r, left);
4246 outs() << " (protocol_t * entends past the end of the section)\n";
4247 } else
4248 memcpy(&q, r, sizeof(uint32_t));
4249 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4250 sys::swapByteOrder(q);
4251 outs() << "\t\t list[" << i << "] " << format("0x%" PRIx32, q)
4252 << " (struct protocol_t *)\n";
4253 r = get_pointer_32(q, offset, left, S, info);
4254 if (r == nullptr)
4255 return;
4256 memset(&pc, '\0', sizeof(struct protocol32_t));
4257 if (left < sizeof(struct protocol32_t)) {
4258 memcpy(&pc, r, left);
4259 outs() << " (protocol_t entends past the end of the section)\n";
4260 } else
4261 memcpy(&pc, r, sizeof(struct protocol32_t));
4262 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4263 swapStruct(pc);
4264 outs() << "\t\t\t isa " << format("0x%" PRIx32, pc.isa) << "\n";
4265 outs() << "\t\t\t name " << format("0x%" PRIx32, pc.name);
4266 name = get_pointer_32(pc.name, xoffset, left, xS, info);
4267 if (name != nullptr)
4268 outs() << format(" %.*s", left, name);
4269 outs() << "\n";
4270 outs() << "\t\t\tprotocols " << format("0x%" PRIx32, pc.protocols) << "\n";
4271 outs() << "\t\t instanceMethods "
4272 << format("0x%" PRIx32, pc.instanceMethods)
4273 << " (struct method_list_t *)\n";
4274 if (pc.instanceMethods != 0)
4275 print_method_list32_t(pc.instanceMethods, info, "\t");
4276 outs() << "\t\t classMethods " << format("0x%" PRIx32, pc.classMethods)
4277 << " (struct method_list_t *)\n";
4278 if (pc.classMethods != 0)
4279 print_method_list32_t(pc.classMethods, info, "\t");
4280 outs() << "\t optionalInstanceMethods "
4281 << format("0x%" PRIx32, pc.optionalInstanceMethods) << "\n";
4282 outs() << "\t optionalClassMethods "
4283 << format("0x%" PRIx32, pc.optionalClassMethods) << "\n";
4284 outs() << "\t instanceProperties "
4285 << format("0x%" PRIx32, pc.instanceProperties) << "\n";
4286 p += sizeof(uint32_t);
4287 offset += sizeof(uint32_t);
4288 }
4289}
4290
Kevin Enderby846c0002015-04-16 17:19:59 +00004291static void print_indent(uint32_t indent) {
4292 for (uint32_t i = 0; i < indent;) {
4293 if (indent - i >= 8) {
4294 outs() << "\t";
4295 i += 8;
4296 } else {
4297 for (uint32_t j = i; j < indent; j++)
4298 outs() << " ";
4299 return;
4300 }
4301 }
4302}
4303
4304static bool print_method_description_list(uint32_t p, uint32_t indent,
4305 struct DisassembleInfo *info) {
4306 uint32_t offset, left, xleft;
4307 SectionRef S;
4308 struct objc_method_description_list_t mdl;
4309 struct objc_method_description_t md;
4310 const char *r, *list, *name;
4311 int32_t i;
4312
4313 r = get_pointer_32(p, offset, left, S, info, true);
4314 if (r == nullptr)
4315 return true;
4316
4317 outs() << "\n";
4318 if (left > sizeof(struct objc_method_description_list_t)) {
4319 memcpy(&mdl, r, sizeof(struct objc_method_description_list_t));
4320 } else {
4321 print_indent(indent);
4322 outs() << " objc_method_description_list extends past end of the section\n";
4323 memset(&mdl, '\0', sizeof(struct objc_method_description_list_t));
4324 memcpy(&mdl, r, left);
4325 }
4326 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4327 swapStruct(mdl);
4328
4329 print_indent(indent);
4330 outs() << " count " << mdl.count << "\n";
4331
4332 list = r + sizeof(struct objc_method_description_list_t);
4333 for (i = 0; i < mdl.count; i++) {
4334 if ((i + 1) * sizeof(struct objc_method_description_t) > left) {
4335 print_indent(indent);
4336 outs() << " remaining list entries extend past the of the section\n";
4337 break;
4338 }
4339 print_indent(indent);
4340 outs() << " list[" << i << "]\n";
4341 memcpy(&md, list + i * sizeof(struct objc_method_description_t),
4342 sizeof(struct objc_method_description_t));
4343 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4344 swapStruct(md);
4345
4346 print_indent(indent);
4347 outs() << " name " << format("0x%08" PRIx32, md.name);
4348 if (info->verbose) {
4349 name = get_pointer_32(md.name, offset, xleft, S, info, true);
4350 if (name != nullptr)
4351 outs() << format(" %.*s", xleft, name);
4352 else
4353 outs() << " (not in an __OBJC section)";
4354 }
4355 outs() << "\n";
4356
4357 print_indent(indent);
4358 outs() << " types " << format("0x%08" PRIx32, md.types);
4359 if (info->verbose) {
4360 name = get_pointer_32(md.types, offset, xleft, S, info, true);
4361 if (name != nullptr)
4362 outs() << format(" %.*s", xleft, name);
4363 else
4364 outs() << " (not in an __OBJC section)";
4365 }
4366 outs() << "\n";
4367 }
4368 return false;
4369}
4370
4371static bool print_protocol_list(uint32_t p, uint32_t indent,
4372 struct DisassembleInfo *info);
4373
4374static bool print_protocol(uint32_t p, uint32_t indent,
4375 struct DisassembleInfo *info) {
4376 uint32_t offset, left;
4377 SectionRef S;
4378 struct objc_protocol_t protocol;
4379 const char *r, *name;
4380
4381 r = get_pointer_32(p, offset, left, S, info, true);
4382 if (r == nullptr)
4383 return true;
4384
4385 outs() << "\n";
4386 if (left >= sizeof(struct objc_protocol_t)) {
4387 memcpy(&protocol, r, sizeof(struct objc_protocol_t));
4388 } else {
4389 print_indent(indent);
4390 outs() << " Protocol extends past end of the section\n";
4391 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
4392 memcpy(&protocol, r, left);
4393 }
4394 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4395 swapStruct(protocol);
4396
4397 print_indent(indent);
4398 outs() << " isa " << format("0x%08" PRIx32, protocol.isa)
4399 << "\n";
4400
4401 print_indent(indent);
4402 outs() << " protocol_name "
4403 << format("0x%08" PRIx32, protocol.protocol_name);
4404 if (info->verbose) {
4405 name = get_pointer_32(protocol.protocol_name, offset, left, S, info, true);
4406 if (name != nullptr)
4407 outs() << format(" %.*s", left, name);
4408 else
4409 outs() << " (not in an __OBJC section)";
4410 }
4411 outs() << "\n";
4412
4413 print_indent(indent);
4414 outs() << " protocol_list "
4415 << format("0x%08" PRIx32, protocol.protocol_list);
4416 if (print_protocol_list(protocol.protocol_list, indent + 4, info))
4417 outs() << " (not in an __OBJC section)\n";
4418
4419 print_indent(indent);
4420 outs() << " instance_methods "
4421 << format("0x%08" PRIx32, protocol.instance_methods);
4422 if (print_method_description_list(protocol.instance_methods, indent, info))
4423 outs() << " (not in an __OBJC section)\n";
4424
4425 print_indent(indent);
4426 outs() << " class_methods "
4427 << format("0x%08" PRIx32, protocol.class_methods);
4428 if (print_method_description_list(protocol.class_methods, indent, info))
4429 outs() << " (not in an __OBJC section)\n";
4430
4431 return false;
4432}
4433
4434static bool print_protocol_list(uint32_t p, uint32_t indent,
4435 struct DisassembleInfo *info) {
4436 uint32_t offset, left, l;
4437 SectionRef S;
4438 struct objc_protocol_list_t protocol_list;
4439 const char *r, *list;
4440 int32_t i;
4441
4442 r = get_pointer_32(p, offset, left, S, info, true);
4443 if (r == nullptr)
4444 return true;
4445
4446 outs() << "\n";
4447 if (left > sizeof(struct objc_protocol_list_t)) {
4448 memcpy(&protocol_list, r, sizeof(struct objc_protocol_list_t));
4449 } else {
4450 outs() << "\t\t objc_protocol_list_t extends past end of the section\n";
4451 memset(&protocol_list, '\0', sizeof(struct objc_protocol_list_t));
4452 memcpy(&protocol_list, r, left);
4453 }
4454 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4455 swapStruct(protocol_list);
4456
4457 print_indent(indent);
4458 outs() << " next " << format("0x%08" PRIx32, protocol_list.next)
4459 << "\n";
4460 print_indent(indent);
4461 outs() << " count " << protocol_list.count << "\n";
4462
4463 list = r + sizeof(struct objc_protocol_list_t);
4464 for (i = 0; i < protocol_list.count; i++) {
4465 if ((i + 1) * sizeof(uint32_t) > left) {
4466 outs() << "\t\t remaining list entries extend past the of the section\n";
4467 break;
4468 }
4469 memcpy(&l, list + i * sizeof(uint32_t), sizeof(uint32_t));
4470 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4471 sys::swapByteOrder(l);
4472
4473 print_indent(indent);
4474 outs() << " list[" << i << "] " << format("0x%08" PRIx32, l);
4475 if (print_protocol(l, indent, info))
4476 outs() << "(not in an __OBJC section)\n";
4477 }
4478 return false;
4479}
4480
Kevin Enderby0fc11822015-04-01 20:57:01 +00004481static void print_ivar_list64_t(uint64_t p, struct DisassembleInfo *info) {
4482 struct ivar_list64_t il;
4483 struct ivar64_t i;
4484 const char *r;
4485 uint32_t offset, xoffset, left, j;
4486 SectionRef S, xS;
4487 const char *name, *sym_name, *ivar_offset_p;
4488 uint64_t ivar_offset, n_value;
4489
4490 r = get_pointer_64(p, offset, left, S, info);
4491 if (r == nullptr)
4492 return;
4493 memset(&il, '\0', sizeof(struct ivar_list64_t));
4494 if (left < sizeof(struct ivar_list64_t)) {
4495 memcpy(&il, r, left);
4496 outs() << " (ivar_list_t entends past the end of the section)\n";
4497 } else
4498 memcpy(&il, r, sizeof(struct ivar_list64_t));
4499 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4500 swapStruct(il);
4501 outs() << " entsize " << il.entsize << "\n";
4502 outs() << " count " << il.count << "\n";
4503
4504 p += sizeof(struct ivar_list64_t);
4505 offset += sizeof(struct ivar_list64_t);
4506 for (j = 0; j < il.count; j++) {
4507 r = get_pointer_64(p, offset, left, S, info);
4508 if (r == nullptr)
4509 return;
4510 memset(&i, '\0', sizeof(struct ivar64_t));
4511 if (left < sizeof(struct ivar64_t)) {
4512 memcpy(&i, r, left);
4513 outs() << " (ivar_t entends past the end of the section)\n";
4514 } else
4515 memcpy(&i, r, sizeof(struct ivar64_t));
4516 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4517 swapStruct(i);
4518
4519 outs() << "\t\t\t offset ";
4520 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, offset), S,
4521 info, n_value, i.offset);
4522 if (n_value != 0) {
4523 if (info->verbose && sym_name != nullptr)
4524 outs() << sym_name;
4525 else
4526 outs() << format("0x%" PRIx64, n_value);
4527 if (i.offset != 0)
4528 outs() << " + " << format("0x%" PRIx64, i.offset);
4529 } else
4530 outs() << format("0x%" PRIx64, i.offset);
4531 ivar_offset_p = get_pointer_64(i.offset + n_value, xoffset, left, xS, info);
4532 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4533 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4534 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4535 sys::swapByteOrder(ivar_offset);
4536 outs() << " " << ivar_offset << "\n";
4537 } else
4538 outs() << "\n";
4539
4540 outs() << "\t\t\t name ";
4541 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, name), S, info,
4542 n_value, i.name);
4543 if (n_value != 0) {
4544 if (info->verbose && sym_name != nullptr)
4545 outs() << sym_name;
4546 else
4547 outs() << format("0x%" PRIx64, n_value);
4548 if (i.name != 0)
4549 outs() << " + " << format("0x%" PRIx64, i.name);
4550 } else
4551 outs() << format("0x%" PRIx64, i.name);
4552 name = get_pointer_64(i.name + n_value, xoffset, left, xS, info);
4553 if (name != nullptr)
4554 outs() << format(" %.*s", left, name);
4555 outs() << "\n";
4556
4557 outs() << "\t\t\t type ";
4558 sym_name = get_symbol_64(offset + offsetof(struct ivar64_t, type), S, info,
4559 n_value, i.name);
4560 name = get_pointer_64(i.type + n_value, xoffset, left, xS, info);
4561 if (n_value != 0) {
4562 if (info->verbose && sym_name != nullptr)
4563 outs() << sym_name;
4564 else
4565 outs() << format("0x%" PRIx64, n_value);
4566 if (i.type != 0)
4567 outs() << " + " << format("0x%" PRIx64, i.type);
4568 } else
4569 outs() << format("0x%" PRIx64, i.type);
4570 if (name != nullptr)
4571 outs() << format(" %.*s", left, name);
4572 outs() << "\n";
4573
4574 outs() << "\t\t\talignment " << i.alignment << "\n";
4575 outs() << "\t\t\t size " << i.size << "\n";
4576
4577 p += sizeof(struct ivar64_t);
4578 offset += sizeof(struct ivar64_t);
4579 }
4580}
4581
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004582static void print_ivar_list32_t(uint32_t p, struct DisassembleInfo *info) {
4583 struct ivar_list32_t il;
4584 struct ivar32_t i;
4585 const char *r;
4586 uint32_t offset, xoffset, left, j;
4587 SectionRef S, xS;
4588 const char *name, *ivar_offset_p;
4589 uint32_t ivar_offset;
4590
4591 r = get_pointer_32(p, offset, left, S, info);
4592 if (r == nullptr)
4593 return;
4594 memset(&il, '\0', sizeof(struct ivar_list32_t));
4595 if (left < sizeof(struct ivar_list32_t)) {
4596 memcpy(&il, r, left);
4597 outs() << " (ivar_list_t entends past the end of the section)\n";
4598 } else
4599 memcpy(&il, r, sizeof(struct ivar_list32_t));
4600 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4601 swapStruct(il);
4602 outs() << " entsize " << il.entsize << "\n";
4603 outs() << " count " << il.count << "\n";
4604
4605 p += sizeof(struct ivar_list32_t);
4606 offset += sizeof(struct ivar_list32_t);
4607 for (j = 0; j < il.count; j++) {
4608 r = get_pointer_32(p, offset, left, S, info);
4609 if (r == nullptr)
4610 return;
4611 memset(&i, '\0', sizeof(struct ivar32_t));
4612 if (left < sizeof(struct ivar32_t)) {
4613 memcpy(&i, r, left);
4614 outs() << " (ivar_t entends past the end of the section)\n";
4615 } else
4616 memcpy(&i, r, sizeof(struct ivar32_t));
4617 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4618 swapStruct(i);
4619
4620 outs() << "\t\t\t offset " << format("0x%" PRIx32, i.offset);
4621 ivar_offset_p = get_pointer_32(i.offset, xoffset, left, xS, info);
4622 if (ivar_offset_p != nullptr && left >= sizeof(*ivar_offset_p)) {
4623 memcpy(&ivar_offset, ivar_offset_p, sizeof(ivar_offset));
4624 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4625 sys::swapByteOrder(ivar_offset);
4626 outs() << " " << ivar_offset << "\n";
4627 } else
4628 outs() << "\n";
4629
4630 outs() << "\t\t\t name " << format("0x%" PRIx32, i.name);
4631 name = get_pointer_32(i.name, xoffset, left, xS, info);
4632 if (name != nullptr)
4633 outs() << format(" %.*s", left, name);
4634 outs() << "\n";
4635
4636 outs() << "\t\t\t type " << format("0x%" PRIx32, i.type);
4637 name = get_pointer_32(i.type, xoffset, left, xS, info);
4638 if (name != nullptr)
4639 outs() << format(" %.*s", left, name);
4640 outs() << "\n";
4641
4642 outs() << "\t\t\talignment " << i.alignment << "\n";
4643 outs() << "\t\t\t size " << i.size << "\n";
4644
4645 p += sizeof(struct ivar32_t);
4646 offset += sizeof(struct ivar32_t);
4647 }
4648}
4649
Kevin Enderby0fc11822015-04-01 20:57:01 +00004650static void print_objc_property_list64(uint64_t p,
4651 struct DisassembleInfo *info) {
4652 struct objc_property_list64 opl;
4653 struct objc_property64 op;
4654 const char *r;
4655 uint32_t offset, xoffset, left, j;
4656 SectionRef S, xS;
4657 const char *name, *sym_name;
4658 uint64_t n_value;
4659
4660 r = get_pointer_64(p, offset, left, S, info);
4661 if (r == nullptr)
4662 return;
4663 memset(&opl, '\0', sizeof(struct objc_property_list64));
4664 if (left < sizeof(struct objc_property_list64)) {
4665 memcpy(&opl, r, left);
4666 outs() << " (objc_property_list entends past the end of the section)\n";
4667 } else
4668 memcpy(&opl, r, sizeof(struct objc_property_list64));
4669 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4670 swapStruct(opl);
4671 outs() << " entsize " << opl.entsize << "\n";
4672 outs() << " count " << opl.count << "\n";
4673
4674 p += sizeof(struct objc_property_list64);
4675 offset += sizeof(struct objc_property_list64);
4676 for (j = 0; j < opl.count; j++) {
4677 r = get_pointer_64(p, offset, left, S, info);
4678 if (r == nullptr)
4679 return;
4680 memset(&op, '\0', sizeof(struct objc_property64));
4681 if (left < sizeof(struct objc_property64)) {
4682 memcpy(&op, r, left);
4683 outs() << " (objc_property entends past the end of the section)\n";
4684 } else
4685 memcpy(&op, r, sizeof(struct objc_property64));
4686 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4687 swapStruct(op);
4688
4689 outs() << "\t\t\t name ";
4690 sym_name = get_symbol_64(offset + offsetof(struct objc_property64, name), S,
4691 info, n_value, op.name);
4692 if (n_value != 0) {
4693 if (info->verbose && sym_name != nullptr)
4694 outs() << sym_name;
4695 else
4696 outs() << format("0x%" PRIx64, n_value);
4697 if (op.name != 0)
4698 outs() << " + " << format("0x%" PRIx64, op.name);
4699 } else
4700 outs() << format("0x%" PRIx64, op.name);
4701 name = get_pointer_64(op.name + n_value, xoffset, left, xS, info);
4702 if (name != nullptr)
4703 outs() << format(" %.*s", left, name);
4704 outs() << "\n";
4705
4706 outs() << "\t\t\tattributes ";
4707 sym_name =
4708 get_symbol_64(offset + offsetof(struct objc_property64, attributes), S,
4709 info, n_value, op.attributes);
4710 if (n_value != 0) {
4711 if (info->verbose && sym_name != nullptr)
4712 outs() << sym_name;
4713 else
4714 outs() << format("0x%" PRIx64, n_value);
4715 if (op.attributes != 0)
4716 outs() << " + " << format("0x%" PRIx64, op.attributes);
4717 } else
4718 outs() << format("0x%" PRIx64, op.attributes);
4719 name = get_pointer_64(op.attributes + n_value, xoffset, left, xS, info);
4720 if (name != nullptr)
4721 outs() << format(" %.*s", left, name);
4722 outs() << "\n";
4723
4724 p += sizeof(struct objc_property64);
4725 offset += sizeof(struct objc_property64);
4726 }
4727}
4728
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004729static void print_objc_property_list32(uint32_t p,
4730 struct DisassembleInfo *info) {
4731 struct objc_property_list32 opl;
4732 struct objc_property32 op;
4733 const char *r;
4734 uint32_t offset, xoffset, left, j;
4735 SectionRef S, xS;
4736 const char *name;
4737
4738 r = get_pointer_32(p, offset, left, S, info);
4739 if (r == nullptr)
4740 return;
4741 memset(&opl, '\0', sizeof(struct objc_property_list32));
4742 if (left < sizeof(struct objc_property_list32)) {
4743 memcpy(&opl, r, left);
4744 outs() << " (objc_property_list entends past the end of the section)\n";
4745 } else
4746 memcpy(&opl, r, sizeof(struct objc_property_list32));
4747 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4748 swapStruct(opl);
4749 outs() << " entsize " << opl.entsize << "\n";
4750 outs() << " count " << opl.count << "\n";
4751
4752 p += sizeof(struct objc_property_list32);
4753 offset += sizeof(struct objc_property_list32);
4754 for (j = 0; j < opl.count; j++) {
4755 r = get_pointer_32(p, offset, left, S, info);
4756 if (r == nullptr)
4757 return;
4758 memset(&op, '\0', sizeof(struct objc_property32));
4759 if (left < sizeof(struct objc_property32)) {
4760 memcpy(&op, r, left);
4761 outs() << " (objc_property entends past the end of the section)\n";
4762 } else
4763 memcpy(&op, r, sizeof(struct objc_property32));
4764 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4765 swapStruct(op);
4766
4767 outs() << "\t\t\t name " << format("0x%" PRIx32, op.name);
4768 name = get_pointer_32(op.name, xoffset, left, xS, info);
4769 if (name != nullptr)
4770 outs() << format(" %.*s", left, name);
4771 outs() << "\n";
4772
4773 outs() << "\t\t\tattributes " << format("0x%" PRIx32, op.attributes);
4774 name = get_pointer_32(op.attributes, xoffset, left, xS, info);
4775 if (name != nullptr)
4776 outs() << format(" %.*s", left, name);
4777 outs() << "\n";
4778
4779 p += sizeof(struct objc_property32);
4780 offset += sizeof(struct objc_property32);
4781 }
4782}
4783
Richard Smith81ff44d2015-10-09 22:09:56 +00004784static bool print_class_ro64_t(uint64_t p, struct DisassembleInfo *info,
Kevin Enderby0fc11822015-04-01 20:57:01 +00004785 bool &is_meta_class) {
4786 struct class_ro64_t cro;
4787 const char *r;
4788 uint32_t offset, xoffset, left;
4789 SectionRef S, xS;
4790 const char *name, *sym_name;
4791 uint64_t n_value;
4792
4793 r = get_pointer_64(p, offset, left, S, info);
4794 if (r == nullptr || left < sizeof(struct class_ro64_t))
Richard Smith81ff44d2015-10-09 22:09:56 +00004795 return false;
Chandler Carruth33e58902016-11-04 07:10:24 +00004796 memcpy(&cro, r, sizeof(struct class_ro64_t));
Kevin Enderby0fc11822015-04-01 20:57:01 +00004797 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4798 swapStruct(cro);
4799 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4800 if (cro.flags & RO_META)
4801 outs() << " RO_META";
4802 if (cro.flags & RO_ROOT)
4803 outs() << " RO_ROOT";
4804 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4805 outs() << " RO_HAS_CXX_STRUCTORS";
4806 outs() << "\n";
4807 outs() << " instanceStart " << cro.instanceStart << "\n";
4808 outs() << " instanceSize " << cro.instanceSize << "\n";
4809 outs() << " reserved " << format("0x%" PRIx32, cro.reserved)
4810 << "\n";
4811 outs() << " ivarLayout " << format("0x%" PRIx64, cro.ivarLayout)
4812 << "\n";
4813 print_layout_map64(cro.ivarLayout, info);
4814
4815 outs() << " name ";
4816 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, name), S,
4817 info, n_value, cro.name);
4818 if (n_value != 0) {
4819 if (info->verbose && sym_name != nullptr)
4820 outs() << sym_name;
4821 else
4822 outs() << format("0x%" PRIx64, n_value);
4823 if (cro.name != 0)
4824 outs() << " + " << format("0x%" PRIx64, cro.name);
4825 } else
4826 outs() << format("0x%" PRIx64, cro.name);
4827 name = get_pointer_64(cro.name + n_value, xoffset, left, xS, info);
4828 if (name != nullptr)
4829 outs() << format(" %.*s", left, name);
4830 outs() << "\n";
4831
4832 outs() << " baseMethods ";
4833 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, baseMethods),
4834 S, info, n_value, cro.baseMethods);
4835 if (n_value != 0) {
4836 if (info->verbose && sym_name != nullptr)
4837 outs() << sym_name;
4838 else
4839 outs() << format("0x%" PRIx64, n_value);
4840 if (cro.baseMethods != 0)
4841 outs() << " + " << format("0x%" PRIx64, cro.baseMethods);
4842 } else
4843 outs() << format("0x%" PRIx64, cro.baseMethods);
4844 outs() << " (struct method_list_t *)\n";
4845 if (cro.baseMethods + n_value != 0)
4846 print_method_list64_t(cro.baseMethods + n_value, info, "");
4847
4848 outs() << " baseProtocols ";
4849 sym_name =
4850 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProtocols), S,
4851 info, n_value, cro.baseProtocols);
4852 if (n_value != 0) {
4853 if (info->verbose && sym_name != nullptr)
4854 outs() << sym_name;
4855 else
4856 outs() << format("0x%" PRIx64, n_value);
4857 if (cro.baseProtocols != 0)
4858 outs() << " + " << format("0x%" PRIx64, cro.baseProtocols);
4859 } else
4860 outs() << format("0x%" PRIx64, cro.baseProtocols);
4861 outs() << "\n";
4862 if (cro.baseProtocols + n_value != 0)
4863 print_protocol_list64_t(cro.baseProtocols + n_value, info);
4864
4865 outs() << " ivars ";
4866 sym_name = get_symbol_64(offset + offsetof(struct class_ro64_t, ivars), S,
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004867 info, n_value, cro.ivars);
Kevin Enderby0fc11822015-04-01 20:57:01 +00004868 if (n_value != 0) {
4869 if (info->verbose && sym_name != nullptr)
4870 outs() << sym_name;
4871 else
4872 outs() << format("0x%" PRIx64, n_value);
4873 if (cro.ivars != 0)
4874 outs() << " + " << format("0x%" PRIx64, cro.ivars);
4875 } else
4876 outs() << format("0x%" PRIx64, cro.ivars);
4877 outs() << "\n";
4878 if (cro.ivars + n_value != 0)
4879 print_ivar_list64_t(cro.ivars + n_value, info);
4880
4881 outs() << " weakIvarLayout ";
4882 sym_name =
4883 get_symbol_64(offset + offsetof(struct class_ro64_t, weakIvarLayout), S,
4884 info, n_value, cro.weakIvarLayout);
4885 if (n_value != 0) {
4886 if (info->verbose && sym_name != nullptr)
4887 outs() << sym_name;
4888 else
4889 outs() << format("0x%" PRIx64, n_value);
4890 if (cro.weakIvarLayout != 0)
4891 outs() << " + " << format("0x%" PRIx64, cro.weakIvarLayout);
4892 } else
4893 outs() << format("0x%" PRIx64, cro.weakIvarLayout);
4894 outs() << "\n";
4895 print_layout_map64(cro.weakIvarLayout + n_value, info);
4896
4897 outs() << " baseProperties ";
4898 sym_name =
4899 get_symbol_64(offset + offsetof(struct class_ro64_t, baseProperties), S,
4900 info, n_value, cro.baseProperties);
4901 if (n_value != 0) {
4902 if (info->verbose && sym_name != nullptr)
4903 outs() << sym_name;
4904 else
4905 outs() << format("0x%" PRIx64, n_value);
4906 if (cro.baseProperties != 0)
4907 outs() << " + " << format("0x%" PRIx64, cro.baseProperties);
4908 } else
4909 outs() << format("0x%" PRIx64, cro.baseProperties);
4910 outs() << "\n";
4911 if (cro.baseProperties + n_value != 0)
4912 print_objc_property_list64(cro.baseProperties + n_value, info);
4913
Rafael Espindolab9091322015-10-24 23:19:10 +00004914 is_meta_class = (cro.flags & RO_META) != 0;
Richard Smith81ff44d2015-10-09 22:09:56 +00004915 return true;
Kevin Enderby0fc11822015-04-01 20:57:01 +00004916}
4917
Richard Smith81ff44d2015-10-09 22:09:56 +00004918static bool print_class_ro32_t(uint32_t p, struct DisassembleInfo *info,
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004919 bool &is_meta_class) {
4920 struct class_ro32_t cro;
4921 const char *r;
4922 uint32_t offset, xoffset, left;
4923 SectionRef S, xS;
4924 const char *name;
4925
4926 r = get_pointer_32(p, offset, left, S, info);
4927 if (r == nullptr)
Richard Smith81ff44d2015-10-09 22:09:56 +00004928 return false;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004929 memset(&cro, '\0', sizeof(struct class_ro32_t));
4930 if (left < sizeof(struct class_ro32_t)) {
4931 memcpy(&cro, r, left);
4932 outs() << " (class_ro_t entends past the end of the section)\n";
4933 } else
4934 memcpy(&cro, r, sizeof(struct class_ro32_t));
4935 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4936 swapStruct(cro);
4937 outs() << " flags " << format("0x%" PRIx32, cro.flags);
4938 if (cro.flags & RO_META)
4939 outs() << " RO_META";
4940 if (cro.flags & RO_ROOT)
4941 outs() << " RO_ROOT";
4942 if (cro.flags & RO_HAS_CXX_STRUCTORS)
4943 outs() << " RO_HAS_CXX_STRUCTORS";
4944 outs() << "\n";
4945 outs() << " instanceStart " << cro.instanceStart << "\n";
4946 outs() << " instanceSize " << cro.instanceSize << "\n";
4947 outs() << " ivarLayout " << format("0x%" PRIx32, cro.ivarLayout)
4948 << "\n";
4949 print_layout_map32(cro.ivarLayout, info);
4950
4951 outs() << " name " << format("0x%" PRIx32, cro.name);
4952 name = get_pointer_32(cro.name, xoffset, left, xS, info);
4953 if (name != nullptr)
4954 outs() << format(" %.*s", left, name);
4955 outs() << "\n";
4956
4957 outs() << " baseMethods "
4958 << format("0x%" PRIx32, cro.baseMethods)
4959 << " (struct method_list_t *)\n";
4960 if (cro.baseMethods != 0)
4961 print_method_list32_t(cro.baseMethods, info, "");
4962
4963 outs() << " baseProtocols "
4964 << format("0x%" PRIx32, cro.baseProtocols) << "\n";
4965 if (cro.baseProtocols != 0)
4966 print_protocol_list32_t(cro.baseProtocols, info);
4967 outs() << " ivars " << format("0x%" PRIx32, cro.ivars)
4968 << "\n";
4969 if (cro.ivars != 0)
4970 print_ivar_list32_t(cro.ivars, info);
4971 outs() << " weakIvarLayout "
4972 << format("0x%" PRIx32, cro.weakIvarLayout) << "\n";
4973 print_layout_map32(cro.weakIvarLayout, info);
4974 outs() << " baseProperties "
4975 << format("0x%" PRIx32, cro.baseProperties) << "\n";
4976 if (cro.baseProperties != 0)
4977 print_objc_property_list32(cro.baseProperties, info);
Rafael Espindolab9091322015-10-24 23:19:10 +00004978 is_meta_class = (cro.flags & RO_META) != 0;
Richard Smith81ff44d2015-10-09 22:09:56 +00004979 return true;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00004980}
4981
Kevin Enderby0fc11822015-04-01 20:57:01 +00004982static void print_class64_t(uint64_t p, struct DisassembleInfo *info) {
4983 struct class64_t c;
4984 const char *r;
4985 uint32_t offset, left;
4986 SectionRef S;
4987 const char *name;
4988 uint64_t isa_n_value, n_value;
4989
4990 r = get_pointer_64(p, offset, left, S, info);
4991 if (r == nullptr || left < sizeof(struct class64_t))
4992 return;
Chandler Carruth33e58902016-11-04 07:10:24 +00004993 memcpy(&c, r, sizeof(struct class64_t));
Kevin Enderby0fc11822015-04-01 20:57:01 +00004994 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
4995 swapStruct(c);
4996
4997 outs() << " isa " << format("0x%" PRIx64, c.isa);
4998 name = get_symbol_64(offset + offsetof(struct class64_t, isa), S, info,
4999 isa_n_value, c.isa);
5000 if (name != nullptr)
5001 outs() << " " << name;
5002 outs() << "\n";
5003
5004 outs() << " superclass " << format("0x%" PRIx64, c.superclass);
5005 name = get_symbol_64(offset + offsetof(struct class64_t, superclass), S, info,
5006 n_value, c.superclass);
5007 if (name != nullptr)
5008 outs() << " " << name;
Kevin Enderby1ce38582017-06-20 22:55:11 +00005009 else {
5010 name = get_dyld_bind_info_symbolname(S.getAddress() +
5011 offset + offsetof(struct class64_t, superclass), info);
5012 if (name != nullptr)
5013 outs() << " " << name;
5014 }
Kevin Enderby0fc11822015-04-01 20:57:01 +00005015 outs() << "\n";
5016
5017 outs() << " cache " << format("0x%" PRIx64, c.cache);
5018 name = get_symbol_64(offset + offsetof(struct class64_t, cache), S, info,
5019 n_value, c.cache);
5020 if (name != nullptr)
5021 outs() << " " << name;
5022 outs() << "\n";
5023
5024 outs() << " vtable " << format("0x%" PRIx64, c.vtable);
5025 name = get_symbol_64(offset + offsetof(struct class64_t, vtable), S, info,
5026 n_value, c.vtable);
5027 if (name != nullptr)
5028 outs() << " " << name;
5029 outs() << "\n";
5030
5031 name = get_symbol_64(offset + offsetof(struct class64_t, data), S, info,
5032 n_value, c.data);
5033 outs() << " data ";
5034 if (n_value != 0) {
5035 if (info->verbose && name != nullptr)
5036 outs() << name;
5037 else
5038 outs() << format("0x%" PRIx64, n_value);
5039 if (c.data != 0)
5040 outs() << " + " << format("0x%" PRIx64, c.data);
5041 } else
5042 outs() << format("0x%" PRIx64, c.data);
5043 outs() << " (struct class_ro_t *)";
5044
5045 // This is a Swift class if some of the low bits of the pointer are set.
5046 if ((c.data + n_value) & 0x7)
5047 outs() << " Swift class";
5048 outs() << "\n";
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005049 bool is_meta_class;
Richard Smith81ff44d2015-10-09 22:09:56 +00005050 if (!print_class_ro64_t((c.data + n_value) & ~0x7, info, is_meta_class))
5051 return;
Kevin Enderby0fc11822015-04-01 20:57:01 +00005052
Kevin Enderbyaac75382015-10-08 16:56:35 +00005053 if (!is_meta_class &&
5054 c.isa + isa_n_value != p &&
5055 c.isa + isa_n_value != 0 &&
5056 info->depth < 100) {
5057 info->depth++;
5058 outs() << "Meta Class\n";
5059 print_class64_t(c.isa + isa_n_value, info);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005060 }
5061}
5062
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005063static void print_class32_t(uint32_t p, struct DisassembleInfo *info) {
5064 struct class32_t c;
5065 const char *r;
5066 uint32_t offset, left;
5067 SectionRef S;
5068 const char *name;
5069
5070 r = get_pointer_32(p, offset, left, S, info);
5071 if (r == nullptr)
5072 return;
5073 memset(&c, '\0', sizeof(struct class32_t));
5074 if (left < sizeof(struct class32_t)) {
5075 memcpy(&c, r, left);
5076 outs() << " (class_t entends past the end of the section)\n";
5077 } else
5078 memcpy(&c, r, sizeof(struct class32_t));
5079 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5080 swapStruct(c);
5081
5082 outs() << " isa " << format("0x%" PRIx32, c.isa);
5083 name =
5084 get_symbol_32(offset + offsetof(struct class32_t, isa), S, info, c.isa);
5085 if (name != nullptr)
5086 outs() << " " << name;
5087 outs() << "\n";
5088
5089 outs() << " superclass " << format("0x%" PRIx32, c.superclass);
5090 name = get_symbol_32(offset + offsetof(struct class32_t, superclass), S, info,
5091 c.superclass);
5092 if (name != nullptr)
5093 outs() << " " << name;
5094 outs() << "\n";
5095
5096 outs() << " cache " << format("0x%" PRIx32, c.cache);
5097 name = get_symbol_32(offset + offsetof(struct class32_t, cache), S, info,
5098 c.cache);
5099 if (name != nullptr)
5100 outs() << " " << name;
5101 outs() << "\n";
5102
5103 outs() << " vtable " << format("0x%" PRIx32, c.vtable);
5104 name = get_symbol_32(offset + offsetof(struct class32_t, vtable), S, info,
5105 c.vtable);
5106 if (name != nullptr)
5107 outs() << " " << name;
5108 outs() << "\n";
5109
5110 name =
5111 get_symbol_32(offset + offsetof(struct class32_t, data), S, info, c.data);
5112 outs() << " data " << format("0x%" PRIx32, c.data)
5113 << " (struct class_ro_t *)";
5114
5115 // This is a Swift class if some of the low bits of the pointer are set.
5116 if (c.data & 0x3)
5117 outs() << " Swift class";
5118 outs() << "\n";
5119 bool is_meta_class;
Richard Smith81ff44d2015-10-09 22:09:56 +00005120 if (!print_class_ro32_t(c.data & ~0x3, info, is_meta_class))
5121 return;
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005122
Hans Wennborgcc9deb42015-09-29 18:02:48 +00005123 if (!is_meta_class) {
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005124 outs() << "Meta Class\n";
5125 print_class32_t(c.isa, info);
5126 }
5127}
5128
Kevin Enderby846c0002015-04-16 17:19:59 +00005129static void print_objc_class_t(struct objc_class_t *objc_class,
5130 struct DisassembleInfo *info) {
5131 uint32_t offset, left, xleft;
5132 const char *name, *p, *ivar_list;
5133 SectionRef S;
5134 int32_t i;
5135 struct objc_ivar_list_t objc_ivar_list;
5136 struct objc_ivar_t ivar;
5137
5138 outs() << "\t\t isa " << format("0x%08" PRIx32, objc_class->isa);
5139 if (info->verbose && CLS_GETINFO(objc_class, CLS_META)) {
5140 name = get_pointer_32(objc_class->isa, offset, left, S, info, true);
5141 if (name != nullptr)
5142 outs() << format(" %.*s", left, name);
5143 else
5144 outs() << " (not in an __OBJC section)";
5145 }
5146 outs() << "\n";
5147
5148 outs() << "\t super_class "
5149 << format("0x%08" PRIx32, objc_class->super_class);
5150 if (info->verbose) {
5151 name = get_pointer_32(objc_class->super_class, offset, left, S, info, true);
5152 if (name != nullptr)
5153 outs() << format(" %.*s", left, name);
5154 else
5155 outs() << " (not in an __OBJC section)";
5156 }
5157 outs() << "\n";
5158
5159 outs() << "\t\t name " << format("0x%08" PRIx32, objc_class->name);
5160 if (info->verbose) {
5161 name = get_pointer_32(objc_class->name, offset, left, S, info, true);
5162 if (name != nullptr)
5163 outs() << format(" %.*s", left, name);
5164 else
5165 outs() << " (not in an __OBJC section)";
5166 }
5167 outs() << "\n";
5168
5169 outs() << "\t\t version " << format("0x%08" PRIx32, objc_class->version)
5170 << "\n";
5171
5172 outs() << "\t\t info " << format("0x%08" PRIx32, objc_class->info);
5173 if (info->verbose) {
5174 if (CLS_GETINFO(objc_class, CLS_CLASS))
5175 outs() << " CLS_CLASS";
5176 else if (CLS_GETINFO(objc_class, CLS_META))
5177 outs() << " CLS_META";
5178 }
5179 outs() << "\n";
5180
5181 outs() << "\t instance_size "
5182 << format("0x%08" PRIx32, objc_class->instance_size) << "\n";
5183
5184 p = get_pointer_32(objc_class->ivars, offset, left, S, info, true);
5185 outs() << "\t\t ivars " << format("0x%08" PRIx32, objc_class->ivars);
5186 if (p != nullptr) {
5187 if (left > sizeof(struct objc_ivar_list_t)) {
5188 outs() << "\n";
5189 memcpy(&objc_ivar_list, p, sizeof(struct objc_ivar_list_t));
5190 } else {
5191 outs() << " (entends past the end of the section)\n";
5192 memset(&objc_ivar_list, '\0', sizeof(struct objc_ivar_list_t));
5193 memcpy(&objc_ivar_list, p, left);
5194 }
5195 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5196 swapStruct(objc_ivar_list);
5197 outs() << "\t\t ivar_count " << objc_ivar_list.ivar_count << "\n";
5198 ivar_list = p + sizeof(struct objc_ivar_list_t);
5199 for (i = 0; i < objc_ivar_list.ivar_count; i++) {
5200 if ((i + 1) * sizeof(struct objc_ivar_t) > left) {
5201 outs() << "\t\t remaining ivar's extend past the of the section\n";
5202 break;
5203 }
5204 memcpy(&ivar, ivar_list + i * sizeof(struct objc_ivar_t),
5205 sizeof(struct objc_ivar_t));
5206 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5207 swapStruct(ivar);
5208
5209 outs() << "\t\t\tivar_name " << format("0x%08" PRIx32, ivar.ivar_name);
5210 if (info->verbose) {
5211 name = get_pointer_32(ivar.ivar_name, offset, xleft, S, info, true);
5212 if (name != nullptr)
5213 outs() << format(" %.*s", xleft, name);
5214 else
5215 outs() << " (not in an __OBJC section)";
5216 }
5217 outs() << "\n";
5218
5219 outs() << "\t\t\tivar_type " << format("0x%08" PRIx32, ivar.ivar_type);
5220 if (info->verbose) {
5221 name = get_pointer_32(ivar.ivar_type, offset, xleft, S, info, true);
5222 if (name != nullptr)
5223 outs() << format(" %.*s", xleft, name);
5224 else
5225 outs() << " (not in an __OBJC section)";
5226 }
5227 outs() << "\n";
5228
5229 outs() << "\t\t ivar_offset "
5230 << format("0x%08" PRIx32, ivar.ivar_offset) << "\n";
5231 }
5232 } else {
5233 outs() << " (not in an __OBJC section)\n";
5234 }
5235
5236 outs() << "\t\t methods " << format("0x%08" PRIx32, objc_class->methodLists);
5237 if (print_method_list(objc_class->methodLists, info))
5238 outs() << " (not in an __OBJC section)\n";
5239
5240 outs() << "\t\t cache " << format("0x%08" PRIx32, objc_class->cache)
5241 << "\n";
5242
5243 outs() << "\t\tprotocols " << format("0x%08" PRIx32, objc_class->protocols);
5244 if (print_protocol_list(objc_class->protocols, 16, info))
5245 outs() << " (not in an __OBJC section)\n";
5246}
5247
5248static void print_objc_objc_category_t(struct objc_category_t *objc_category,
5249 struct DisassembleInfo *info) {
5250 uint32_t offset, left;
5251 const char *name;
5252 SectionRef S;
5253
5254 outs() << "\t category name "
5255 << format("0x%08" PRIx32, objc_category->category_name);
5256 if (info->verbose) {
5257 name = get_pointer_32(objc_category->category_name, offset, left, S, info,
5258 true);
5259 if (name != nullptr)
5260 outs() << format(" %.*s", left, name);
5261 else
5262 outs() << " (not in an __OBJC section)";
5263 }
5264 outs() << "\n";
5265
5266 outs() << "\t\t class name "
5267 << format("0x%08" PRIx32, objc_category->class_name);
5268 if (info->verbose) {
5269 name =
5270 get_pointer_32(objc_category->class_name, offset, left, S, info, true);
5271 if (name != nullptr)
5272 outs() << format(" %.*s", left, name);
5273 else
5274 outs() << " (not in an __OBJC section)";
5275 }
5276 outs() << "\n";
5277
5278 outs() << "\t instance methods "
5279 << format("0x%08" PRIx32, objc_category->instance_methods);
5280 if (print_method_list(objc_category->instance_methods, info))
5281 outs() << " (not in an __OBJC section)\n";
5282
5283 outs() << "\t class methods "
5284 << format("0x%08" PRIx32, objc_category->class_methods);
5285 if (print_method_list(objc_category->class_methods, info))
5286 outs() << " (not in an __OBJC section)\n";
5287}
5288
Kevin Enderby0fc11822015-04-01 20:57:01 +00005289static void print_category64_t(uint64_t p, struct DisassembleInfo *info) {
5290 struct category64_t c;
5291 const char *r;
5292 uint32_t offset, xoffset, left;
5293 SectionRef S, xS;
5294 const char *name, *sym_name;
5295 uint64_t n_value;
5296
5297 r = get_pointer_64(p, offset, left, S, info);
5298 if (r == nullptr)
5299 return;
5300 memset(&c, '\0', sizeof(struct category64_t));
5301 if (left < sizeof(struct category64_t)) {
5302 memcpy(&c, r, left);
5303 outs() << " (category_t entends past the end of the section)\n";
5304 } else
5305 memcpy(&c, r, sizeof(struct category64_t));
5306 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5307 swapStruct(c);
5308
5309 outs() << " name ";
5310 sym_name = get_symbol_64(offset + offsetof(struct category64_t, name), S,
5311 info, n_value, c.name);
5312 if (n_value != 0) {
5313 if (info->verbose && sym_name != nullptr)
5314 outs() << sym_name;
5315 else
5316 outs() << format("0x%" PRIx64, n_value);
5317 if (c.name != 0)
5318 outs() << " + " << format("0x%" PRIx64, c.name);
5319 } else
5320 outs() << format("0x%" PRIx64, c.name);
5321 name = get_pointer_64(c.name + n_value, xoffset, left, xS, info);
5322 if (name != nullptr)
5323 outs() << format(" %.*s", left, name);
5324 outs() << "\n";
5325
5326 outs() << " cls ";
5327 sym_name = get_symbol_64(offset + offsetof(struct category64_t, cls), S, info,
5328 n_value, c.cls);
5329 if (n_value != 0) {
5330 if (info->verbose && sym_name != nullptr)
5331 outs() << sym_name;
5332 else
5333 outs() << format("0x%" PRIx64, n_value);
5334 if (c.cls != 0)
5335 outs() << " + " << format("0x%" PRIx64, c.cls);
5336 } else
5337 outs() << format("0x%" PRIx64, c.cls);
5338 outs() << "\n";
5339 if (c.cls + n_value != 0)
5340 print_class64_t(c.cls + n_value, info);
5341
5342 outs() << " instanceMethods ";
5343 sym_name =
5344 get_symbol_64(offset + offsetof(struct category64_t, instanceMethods), S,
5345 info, n_value, c.instanceMethods);
5346 if (n_value != 0) {
5347 if (info->verbose && sym_name != nullptr)
5348 outs() << sym_name;
5349 else
5350 outs() << format("0x%" PRIx64, n_value);
5351 if (c.instanceMethods != 0)
5352 outs() << " + " << format("0x%" PRIx64, c.instanceMethods);
5353 } else
5354 outs() << format("0x%" PRIx64, c.instanceMethods);
5355 outs() << "\n";
5356 if (c.instanceMethods + n_value != 0)
5357 print_method_list64_t(c.instanceMethods + n_value, info, "");
5358
5359 outs() << " classMethods ";
5360 sym_name = get_symbol_64(offset + offsetof(struct category64_t, classMethods),
5361 S, info, n_value, c.classMethods);
5362 if (n_value != 0) {
5363 if (info->verbose && sym_name != nullptr)
5364 outs() << sym_name;
5365 else
5366 outs() << format("0x%" PRIx64, n_value);
5367 if (c.classMethods != 0)
5368 outs() << " + " << format("0x%" PRIx64, c.classMethods);
5369 } else
5370 outs() << format("0x%" PRIx64, c.classMethods);
5371 outs() << "\n";
5372 if (c.classMethods + n_value != 0)
5373 print_method_list64_t(c.classMethods + n_value, info, "");
5374
5375 outs() << " protocols ";
5376 sym_name = get_symbol_64(offset + offsetof(struct category64_t, protocols), S,
5377 info, n_value, c.protocols);
5378 if (n_value != 0) {
5379 if (info->verbose && sym_name != nullptr)
5380 outs() << sym_name;
5381 else
5382 outs() << format("0x%" PRIx64, n_value);
5383 if (c.protocols != 0)
5384 outs() << " + " << format("0x%" PRIx64, c.protocols);
5385 } else
5386 outs() << format("0x%" PRIx64, c.protocols);
5387 outs() << "\n";
5388 if (c.protocols + n_value != 0)
5389 print_protocol_list64_t(c.protocols + n_value, info);
5390
5391 outs() << "instanceProperties ";
5392 sym_name =
5393 get_symbol_64(offset + offsetof(struct category64_t, instanceProperties),
5394 S, info, n_value, c.instanceProperties);
5395 if (n_value != 0) {
5396 if (info->verbose && sym_name != nullptr)
5397 outs() << sym_name;
5398 else
5399 outs() << format("0x%" PRIx64, n_value);
5400 if (c.instanceProperties != 0)
5401 outs() << " + " << format("0x%" PRIx64, c.instanceProperties);
5402 } else
5403 outs() << format("0x%" PRIx64, c.instanceProperties);
5404 outs() << "\n";
5405 if (c.instanceProperties + n_value != 0)
5406 print_objc_property_list64(c.instanceProperties + n_value, info);
5407}
5408
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005409static void print_category32_t(uint32_t p, struct DisassembleInfo *info) {
5410 struct category32_t c;
5411 const char *r;
5412 uint32_t offset, left;
5413 SectionRef S, xS;
5414 const char *name;
5415
5416 r = get_pointer_32(p, offset, left, S, info);
5417 if (r == nullptr)
5418 return;
5419 memset(&c, '\0', sizeof(struct category32_t));
5420 if (left < sizeof(struct category32_t)) {
5421 memcpy(&c, r, left);
5422 outs() << " (category_t entends past the end of the section)\n";
5423 } else
5424 memcpy(&c, r, sizeof(struct category32_t));
5425 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5426 swapStruct(c);
5427
5428 outs() << " name " << format("0x%" PRIx32, c.name);
5429 name = get_symbol_32(offset + offsetof(struct category32_t, name), S, info,
5430 c.name);
Hans Wennborgcc9deb42015-09-29 18:02:48 +00005431 if (name)
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005432 outs() << " " << name;
5433 outs() << "\n";
5434
5435 outs() << " cls " << format("0x%" PRIx32, c.cls) << "\n";
5436 if (c.cls != 0)
5437 print_class32_t(c.cls, info);
5438 outs() << " instanceMethods " << format("0x%" PRIx32, c.instanceMethods)
5439 << "\n";
5440 if (c.instanceMethods != 0)
5441 print_method_list32_t(c.instanceMethods, info, "");
5442 outs() << " classMethods " << format("0x%" PRIx32, c.classMethods)
5443 << "\n";
5444 if (c.classMethods != 0)
5445 print_method_list32_t(c.classMethods, info, "");
5446 outs() << " protocols " << format("0x%" PRIx32, c.protocols) << "\n";
5447 if (c.protocols != 0)
5448 print_protocol_list32_t(c.protocols, info);
5449 outs() << "instanceProperties " << format("0x%" PRIx32, c.instanceProperties)
5450 << "\n";
5451 if (c.instanceProperties != 0)
5452 print_objc_property_list32(c.instanceProperties, info);
5453}
5454
Kevin Enderby0fc11822015-04-01 20:57:01 +00005455static void print_message_refs64(SectionRef S, struct DisassembleInfo *info) {
5456 uint32_t i, left, offset, xoffset;
5457 uint64_t p, n_value;
5458 struct message_ref64 mr;
5459 const char *name, *sym_name;
5460 const char *r;
5461 SectionRef xS;
5462
5463 if (S == SectionRef())
5464 return;
5465
5466 StringRef SectName;
5467 S.getName(SectName);
5468 DataRefImpl Ref = S.getRawDataRefImpl();
5469 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5470 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5471 offset = 0;
5472 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
5473 p = S.getAddress() + i;
5474 r = get_pointer_64(p, offset, left, S, info);
5475 if (r == nullptr)
5476 return;
5477 memset(&mr, '\0', sizeof(struct message_ref64));
5478 if (left < sizeof(struct message_ref64)) {
5479 memcpy(&mr, r, left);
5480 outs() << " (message_ref entends past the end of the section)\n";
5481 } else
5482 memcpy(&mr, r, sizeof(struct message_ref64));
5483 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5484 swapStruct(mr);
5485
5486 outs() << " imp ";
5487 name = get_symbol_64(offset + offsetof(struct message_ref64, imp), S, info,
5488 n_value, mr.imp);
5489 if (n_value != 0) {
5490 outs() << format("0x%" PRIx64, n_value) << " ";
5491 if (mr.imp != 0)
5492 outs() << "+ " << format("0x%" PRIx64, mr.imp) << " ";
5493 } else
5494 outs() << format("0x%" PRIx64, mr.imp) << " ";
5495 if (name != nullptr)
5496 outs() << " " << name;
5497 outs() << "\n";
5498
5499 outs() << " sel ";
5500 sym_name = get_symbol_64(offset + offsetof(struct message_ref64, sel), S,
5501 info, n_value, mr.sel);
5502 if (n_value != 0) {
5503 if (info->verbose && sym_name != nullptr)
5504 outs() << sym_name;
5505 else
5506 outs() << format("0x%" PRIx64, n_value);
5507 if (mr.sel != 0)
5508 outs() << " + " << format("0x%" PRIx64, mr.sel);
5509 } else
5510 outs() << format("0x%" PRIx64, mr.sel);
5511 name = get_pointer_64(mr.sel + n_value, xoffset, left, xS, info);
5512 if (name != nullptr)
5513 outs() << format(" %.*s", left, name);
5514 outs() << "\n";
5515
5516 offset += sizeof(struct message_ref64);
5517 }
5518}
5519
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005520static void print_message_refs32(SectionRef S, struct DisassembleInfo *info) {
5521 uint32_t i, left, offset, xoffset, p;
5522 struct message_ref32 mr;
5523 const char *name, *r;
5524 SectionRef xS;
5525
5526 if (S == SectionRef())
5527 return;
5528
5529 StringRef SectName;
5530 S.getName(SectName);
5531 DataRefImpl Ref = S.getRawDataRefImpl();
5532 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5533 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5534 offset = 0;
5535 for (i = 0; i < S.getSize(); i += sizeof(struct message_ref64)) {
5536 p = S.getAddress() + i;
5537 r = get_pointer_32(p, offset, left, S, info);
5538 if (r == nullptr)
5539 return;
5540 memset(&mr, '\0', sizeof(struct message_ref32));
5541 if (left < sizeof(struct message_ref32)) {
5542 memcpy(&mr, r, left);
5543 outs() << " (message_ref entends past the end of the section)\n";
5544 } else
5545 memcpy(&mr, r, sizeof(struct message_ref32));
5546 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5547 swapStruct(mr);
5548
5549 outs() << " imp " << format("0x%" PRIx32, mr.imp);
5550 name = get_symbol_32(offset + offsetof(struct message_ref32, imp), S, info,
5551 mr.imp);
5552 if (name != nullptr)
5553 outs() << " " << name;
5554 outs() << "\n";
5555
5556 outs() << " sel " << format("0x%" PRIx32, mr.sel);
5557 name = get_pointer_32(mr.sel, xoffset, left, xS, info);
5558 if (name != nullptr)
5559 outs() << " " << name;
5560 outs() << "\n";
5561
5562 offset += sizeof(struct message_ref32);
5563 }
5564}
5565
Kevin Enderby0fc11822015-04-01 20:57:01 +00005566static void print_image_info64(SectionRef S, struct DisassembleInfo *info) {
5567 uint32_t left, offset, swift_version;
5568 uint64_t p;
5569 struct objc_image_info64 o;
5570 const char *r;
5571
Kevin Enderbyaf7c9d02015-10-09 16:48:44 +00005572 if (S == SectionRef())
5573 return;
5574
Kevin Enderby0fc11822015-04-01 20:57:01 +00005575 StringRef SectName;
5576 S.getName(SectName);
5577 DataRefImpl Ref = S.getRawDataRefImpl();
5578 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5579 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5580 p = S.getAddress();
5581 r = get_pointer_64(p, offset, left, S, info);
5582 if (r == nullptr)
5583 return;
5584 memset(&o, '\0', sizeof(struct objc_image_info64));
5585 if (left < sizeof(struct objc_image_info64)) {
5586 memcpy(&o, r, left);
5587 outs() << " (objc_image_info entends past the end of the section)\n";
5588 } else
5589 memcpy(&o, r, sizeof(struct objc_image_info64));
5590 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5591 swapStruct(o);
5592 outs() << " version " << o.version << "\n";
5593 outs() << " flags " << format("0x%" PRIx32, o.flags);
5594 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5595 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5596 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5597 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5598 swift_version = (o.flags >> 8) & 0xff;
5599 if (swift_version != 0) {
5600 if (swift_version == 1)
5601 outs() << " Swift 1.0";
5602 else if (swift_version == 2)
5603 outs() << " Swift 1.1";
Kevin Enderbycda2ced2018-02-09 19:31:27 +00005604 else if(swift_version == 3)
5605 outs() << " Swift 2.0";
5606 else if(swift_version == 4)
5607 outs() << " Swift 3.0";
5608 else if(swift_version == 5)
5609 outs() << " Swift 4.0";
5610 else if(swift_version == 6)
5611 outs() << " Swift 4.1";
Kevin Enderby0fc11822015-04-01 20:57:01 +00005612 else
5613 outs() << " unknown future Swift version (" << swift_version << ")";
5614 }
5615 outs() << "\n";
5616}
5617
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005618static void print_image_info32(SectionRef S, struct DisassembleInfo *info) {
5619 uint32_t left, offset, swift_version, p;
5620 struct objc_image_info32 o;
5621 const char *r;
5622
Kevin Enderby19be2512016-04-21 19:49:29 +00005623 if (S == SectionRef())
5624 return;
5625
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005626 StringRef SectName;
5627 S.getName(SectName);
5628 DataRefImpl Ref = S.getRawDataRefImpl();
5629 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5630 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5631 p = S.getAddress();
5632 r = get_pointer_32(p, offset, left, S, info);
5633 if (r == nullptr)
5634 return;
5635 memset(&o, '\0', sizeof(struct objc_image_info32));
5636 if (left < sizeof(struct objc_image_info32)) {
5637 memcpy(&o, r, left);
5638 outs() << " (objc_image_info entends past the end of the section)\n";
5639 } else
5640 memcpy(&o, r, sizeof(struct objc_image_info32));
5641 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5642 swapStruct(o);
5643 outs() << " version " << o.version << "\n";
5644 outs() << " flags " << format("0x%" PRIx32, o.flags);
5645 if (o.flags & OBJC_IMAGE_IS_REPLACEMENT)
5646 outs() << " OBJC_IMAGE_IS_REPLACEMENT";
5647 if (o.flags & OBJC_IMAGE_SUPPORTS_GC)
5648 outs() << " OBJC_IMAGE_SUPPORTS_GC";
5649 swift_version = (o.flags >> 8) & 0xff;
5650 if (swift_version != 0) {
5651 if (swift_version == 1)
5652 outs() << " Swift 1.0";
5653 else if (swift_version == 2)
5654 outs() << " Swift 1.1";
Kevin Enderbycda2ced2018-02-09 19:31:27 +00005655 else if(swift_version == 3)
5656 outs() << " Swift 2.0";
5657 else if(swift_version == 4)
5658 outs() << " Swift 3.0";
5659 else if(swift_version == 5)
5660 outs() << " Swift 4.0";
5661 else if(swift_version == 6)
5662 outs() << " Swift 4.1";
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005663 else
5664 outs() << " unknown future Swift version (" << swift_version << ")";
5665 }
5666 outs() << "\n";
5667}
5668
Kevin Enderby846c0002015-04-16 17:19:59 +00005669static void print_image_info(SectionRef S, struct DisassembleInfo *info) {
5670 uint32_t left, offset, p;
5671 struct imageInfo_t o;
5672 const char *r;
5673
5674 StringRef SectName;
5675 S.getName(SectName);
5676 DataRefImpl Ref = S.getRawDataRefImpl();
5677 StringRef SegName = info->O->getSectionFinalSegmentName(Ref);
5678 outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
5679 p = S.getAddress();
5680 r = get_pointer_32(p, offset, left, S, info);
5681 if (r == nullptr)
5682 return;
5683 memset(&o, '\0', sizeof(struct imageInfo_t));
5684 if (left < sizeof(struct imageInfo_t)) {
5685 memcpy(&o, r, left);
5686 outs() << " (imageInfo entends past the end of the section)\n";
5687 } else
5688 memcpy(&o, r, sizeof(struct imageInfo_t));
5689 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
5690 swapStruct(o);
5691 outs() << " version " << o.version << "\n";
5692 outs() << " flags " << format("0x%" PRIx32, o.flags);
5693 if (o.flags & 0x1)
5694 outs() << " F&C";
5695 if (o.flags & 0x2)
5696 outs() << " GC";
5697 if (o.flags & 0x4)
5698 outs() << " GC-only";
5699 else
5700 outs() << " RR";
5701 outs() << "\n";
5702}
5703
Kevin Enderby0fc11822015-04-01 20:57:01 +00005704static void printObjc2_64bit_MetaData(MachOObjectFile *O, bool verbose) {
5705 SymbolAddressMap AddrMap;
5706 if (verbose)
5707 CreateSymbolAddressMap(O, &AddrMap);
5708
5709 std::vector<SectionRef> Sections;
5710 for (const SectionRef &Section : O->sections()) {
5711 StringRef SectName;
5712 Section.getName(SectName);
5713 Sections.push_back(Section);
5714 }
5715
Serge Pavlove4e9a1f2018-02-14 03:26:27 +00005716 struct DisassembleInfo info(O, &AddrMap, &Sections, verbose);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005717
Davide Italiano62507042015-12-11 22:27:59 +00005718 SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5719 if (CL == SectionRef())
5720 CL = get_section(O, "__DATA", "__objc_classlist");
Kevin Enderby5879a482017-02-09 17:56:26 +00005721 if (CL == SectionRef())
5722 CL = get_section(O, "__DATA_CONST", "__objc_classlist");
5723 if (CL == SectionRef())
5724 CL = get_section(O, "__DATA_DIRTY", "__objc_classlist");
Davide Italiano62507042015-12-11 22:27:59 +00005725 info.S = CL;
5726 walk_pointer_list_64("class", CL, O, &info, print_class64_t);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005727
Davide Italiano62507042015-12-11 22:27:59 +00005728 SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5729 if (CR == SectionRef())
5730 CR = get_section(O, "__DATA", "__objc_classrefs");
Kevin Enderby5879a482017-02-09 17:56:26 +00005731 if (CR == SectionRef())
5732 CR = get_section(O, "__DATA_CONST", "__objc_classrefs");
5733 if (CR == SectionRef())
5734 CR = get_section(O, "__DATA_DIRTY", "__objc_classrefs");
Davide Italiano62507042015-12-11 22:27:59 +00005735 info.S = CR;
5736 walk_pointer_list_64("class refs", CR, O, &info, nullptr);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005737
Davide Italiano62507042015-12-11 22:27:59 +00005738 SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5739 if (SR == SectionRef())
5740 SR = get_section(O, "__DATA", "__objc_superrefs");
Kevin Enderby5879a482017-02-09 17:56:26 +00005741 if (SR == SectionRef())
5742 SR = get_section(O, "__DATA_CONST", "__objc_superrefs");
5743 if (SR == SectionRef())
5744 SR = get_section(O, "__DATA_DIRTY", "__objc_superrefs");
Davide Italiano62507042015-12-11 22:27:59 +00005745 info.S = SR;
5746 walk_pointer_list_64("super refs", SR, O, &info, nullptr);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005747
Davide Italiano62507042015-12-11 22:27:59 +00005748 SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5749 if (CA == SectionRef())
5750 CA = get_section(O, "__DATA", "__objc_catlist");
Kevin Enderby5879a482017-02-09 17:56:26 +00005751 if (CA == SectionRef())
5752 CA = get_section(O, "__DATA_CONST", "__objc_catlist");
5753 if (CA == SectionRef())
5754 CA = get_section(O, "__DATA_DIRTY", "__objc_catlist");
Davide Italiano62507042015-12-11 22:27:59 +00005755 info.S = CA;
5756 walk_pointer_list_64("category", CA, O, &info, print_category64_t);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005757
Davide Italiano62507042015-12-11 22:27:59 +00005758 SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5759 if (PL == SectionRef())
5760 PL = get_section(O, "__DATA", "__objc_protolist");
Kevin Enderby5879a482017-02-09 17:56:26 +00005761 if (PL == SectionRef())
5762 PL = get_section(O, "__DATA_CONST", "__objc_protolist");
5763 if (PL == SectionRef())
5764 PL = get_section(O, "__DATA_DIRTY", "__objc_protolist");
Davide Italiano62507042015-12-11 22:27:59 +00005765 info.S = PL;
5766 walk_pointer_list_64("protocol", PL, O, &info, nullptr);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005767
Davide Italiano62507042015-12-11 22:27:59 +00005768 SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5769 if (MR == SectionRef())
5770 MR = get_section(O, "__DATA", "__objc_msgrefs");
Kevin Enderby5879a482017-02-09 17:56:26 +00005771 if (MR == SectionRef())
5772 MR = get_section(O, "__DATA_CONST", "__objc_msgrefs");
5773 if (MR == SectionRef())
5774 MR = get_section(O, "__DATA_DIRTY", "__objc_msgrefs");
Davide Italiano62507042015-12-11 22:27:59 +00005775 info.S = MR;
5776 print_message_refs64(MR, &info);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005777
Davide Italiano62507042015-12-11 22:27:59 +00005778 SectionRef II = get_section(O, "__OBJC2", "__image_info");
5779 if (II == SectionRef())
5780 II = get_section(O, "__DATA", "__objc_imageinfo");
Kevin Enderby5879a482017-02-09 17:56:26 +00005781 if (II == SectionRef())
5782 II = get_section(O, "__DATA_CONST", "__objc_imageinfo");
5783 if (II == SectionRef())
5784 II = get_section(O, "__DATA_DIRTY", "__objc_imageinfo");
Davide Italiano62507042015-12-11 22:27:59 +00005785 info.S = II;
5786 print_image_info64(II, &info);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005787}
5788
5789static void printObjc2_32bit_MetaData(MachOObjectFile *O, bool verbose) {
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005790 SymbolAddressMap AddrMap;
5791 if (verbose)
5792 CreateSymbolAddressMap(O, &AddrMap);
5793
5794 std::vector<SectionRef> Sections;
5795 for (const SectionRef &Section : O->sections()) {
5796 StringRef SectName;
5797 Section.getName(SectName);
5798 Sections.push_back(Section);
5799 }
5800
Serge Pavlove4e9a1f2018-02-14 03:26:27 +00005801 struct DisassembleInfo info(O, &AddrMap, &Sections, verbose);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005802
Kevin Enderby5879a482017-02-09 17:56:26 +00005803 SectionRef CL = get_section(O, "__OBJC2", "__class_list");
5804 if (CL == SectionRef())
5805 CL = get_section(O, "__DATA", "__objc_classlist");
5806 if (CL == SectionRef())
5807 CL = get_section(O, "__DATA_CONST", "__objc_classlist");
5808 if (CL == SectionRef())
5809 CL = get_section(O, "__DATA_DIRTY", "__objc_classlist");
5810 info.S = CL;
5811 walk_pointer_list_32("class", CL, O, &info, print_class32_t);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005812
Kevin Enderby5879a482017-02-09 17:56:26 +00005813 SectionRef CR = get_section(O, "__OBJC2", "__class_refs");
5814 if (CR == SectionRef())
5815 CR = get_section(O, "__DATA", "__objc_classrefs");
5816 if (CR == SectionRef())
5817 CR = get_section(O, "__DATA_CONST", "__objc_classrefs");
5818 if (CR == SectionRef())
5819 CR = get_section(O, "__DATA_DIRTY", "__objc_classrefs");
5820 info.S = CR;
5821 walk_pointer_list_32("class refs", CR, O, &info, nullptr);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005822
Kevin Enderby5879a482017-02-09 17:56:26 +00005823 SectionRef SR = get_section(O, "__OBJC2", "__super_refs");
5824 if (SR == SectionRef())
5825 SR = get_section(O, "__DATA", "__objc_superrefs");
5826 if (SR == SectionRef())
5827 SR = get_section(O, "__DATA_CONST", "__objc_superrefs");
5828 if (SR == SectionRef())
5829 SR = get_section(O, "__DATA_DIRTY", "__objc_superrefs");
5830 info.S = SR;
5831 walk_pointer_list_32("super refs", SR, O, &info, nullptr);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005832
Kevin Enderby5879a482017-02-09 17:56:26 +00005833 SectionRef CA = get_section(O, "__OBJC2", "__category_list");
5834 if (CA == SectionRef())
5835 CA = get_section(O, "__DATA", "__objc_catlist");
5836 if (CA == SectionRef())
5837 CA = get_section(O, "__DATA_CONST", "__objc_catlist");
5838 if (CA == SectionRef())
5839 CA = get_section(O, "__DATA_DIRTY", "__objc_catlist");
5840 info.S = CA;
5841 walk_pointer_list_32("category", CA, O, &info, print_category32_t);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005842
Kevin Enderby5879a482017-02-09 17:56:26 +00005843 SectionRef PL = get_section(O, "__OBJC2", "__protocol_list");
5844 if (PL == SectionRef())
5845 PL = get_section(O, "__DATA", "__objc_protolist");
5846 if (PL == SectionRef())
5847 PL = get_section(O, "__DATA_CONST", "__objc_protolist");
5848 if (PL == SectionRef())
5849 PL = get_section(O, "__DATA_DIRTY", "__objc_protolist");
5850 info.S = PL;
5851 walk_pointer_list_32("protocol", PL, O, &info, nullptr);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005852
Kevin Enderby5879a482017-02-09 17:56:26 +00005853 SectionRef MR = get_section(O, "__OBJC2", "__message_refs");
5854 if (MR == SectionRef())
5855 MR = get_section(O, "__DATA", "__objc_msgrefs");
5856 if (MR == SectionRef())
5857 MR = get_section(O, "__DATA_CONST", "__objc_msgrefs");
5858 if (MR == SectionRef())
5859 MR = get_section(O, "__DATA_DIRTY", "__objc_msgrefs");
5860 info.S = MR;
5861 print_message_refs32(MR, &info);
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005862
Kevin Enderby5879a482017-02-09 17:56:26 +00005863 SectionRef II = get_section(O, "__OBJC2", "__image_info");
5864 if (II == SectionRef())
5865 II = get_section(O, "__DATA", "__objc_imageinfo");
5866 if (II == SectionRef())
5867 II = get_section(O, "__DATA_CONST", "__objc_imageinfo");
5868 if (II == SectionRef())
5869 II = get_section(O, "__DATA_DIRTY", "__objc_imageinfo");
5870 info.S = II;
5871 print_image_info32(II, &info);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005872}
5873
5874static bool printObjc1_32bit_MetaData(MachOObjectFile *O, bool verbose) {
Kevin Enderby846c0002015-04-16 17:19:59 +00005875 uint32_t i, j, p, offset, xoffset, left, defs_left, def;
5876 const char *r, *name, *defs;
5877 struct objc_module_t module;
5878 SectionRef S, xS;
5879 struct objc_symtab_t symtab;
5880 struct objc_class_t objc_class;
5881 struct objc_category_t objc_category;
5882
Kevin Enderby28c1c1b2015-04-06 17:47:03 +00005883 outs() << "Objective-C segment\n";
Kevin Enderby846c0002015-04-16 17:19:59 +00005884 S = get_section(O, "__OBJC", "__module_info");
5885 if (S == SectionRef())
5886 return false;
5887
5888 SymbolAddressMap AddrMap;
5889 if (verbose)
5890 CreateSymbolAddressMap(O, &AddrMap);
5891
5892 std::vector<SectionRef> Sections;
5893 for (const SectionRef &Section : O->sections()) {
5894 StringRef SectName;
5895 Section.getName(SectName);
5896 Sections.push_back(Section);
Kevin Enderby0fc11822015-04-01 20:57:01 +00005897 }
Kevin Enderby846c0002015-04-16 17:19:59 +00005898
Serge Pavlove4e9a1f2018-02-14 03:26:27 +00005899 struct DisassembleInfo info(O, &AddrMap, &Sections, verbose);
Kevin Enderby846c0002015-04-16 17:19:59 +00005900
5901 for (i = 0; i < S.getSize(); i += sizeof(struct objc_module_t)) {
5902 p = S.getAddress() + i;
5903 r = get_pointer_32(p, offset, left, S, &info, true);
5904 if (r == nullptr)
5905 return true;
5906 memset(&module, '\0', sizeof(struct objc_module_t));
5907 if (left < sizeof(struct objc_module_t)) {
5908 memcpy(&module, r, left);
5909 outs() << " (module extends past end of __module_info section)\n";
5910 } else
5911 memcpy(&module, r, sizeof(struct objc_module_t));
5912 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5913 swapStruct(module);
5914
5915 outs() << "Module " << format("0x%" PRIx32, p) << "\n";
5916 outs() << " version " << module.version << "\n";
5917 outs() << " size " << module.size << "\n";
5918 outs() << " name ";
5919 name = get_pointer_32(module.name, xoffset, left, xS, &info, true);
5920 if (name != nullptr)
5921 outs() << format("%.*s", left, name);
5922 else
5923 outs() << format("0x%08" PRIx32, module.name)
5924 << "(not in an __OBJC section)";
5925 outs() << "\n";
5926
5927 r = get_pointer_32(module.symtab, xoffset, left, xS, &info, true);
5928 if (module.symtab == 0 || r == nullptr) {
5929 outs() << " symtab " << format("0x%08" PRIx32, module.symtab)
5930 << " (not in an __OBJC section)\n";
5931 continue;
5932 }
5933 outs() << " symtab " << format("0x%08" PRIx32, module.symtab) << "\n";
5934 memset(&symtab, '\0', sizeof(struct objc_symtab_t));
5935 defs_left = 0;
5936 defs = nullptr;
5937 if (left < sizeof(struct objc_symtab_t)) {
5938 memcpy(&symtab, r, left);
5939 outs() << "\tsymtab extends past end of an __OBJC section)\n";
5940 } else {
5941 memcpy(&symtab, r, sizeof(struct objc_symtab_t));
5942 if (left > sizeof(struct objc_symtab_t)) {
5943 defs_left = left - sizeof(struct objc_symtab_t);
5944 defs = r + sizeof(struct objc_symtab_t);
5945 }
5946 }
5947 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5948 swapStruct(symtab);
5949
5950 outs() << "\tsel_ref_cnt " << symtab.sel_ref_cnt << "\n";
5951 r = get_pointer_32(symtab.refs, xoffset, left, xS, &info, true);
5952 outs() << "\trefs " << format("0x%08" PRIx32, symtab.refs);
5953 if (r == nullptr)
5954 outs() << " (not in an __OBJC section)";
5955 outs() << "\n";
5956 outs() << "\tcls_def_cnt " << symtab.cls_def_cnt << "\n";
5957 outs() << "\tcat_def_cnt " << symtab.cat_def_cnt << "\n";
5958 if (symtab.cls_def_cnt > 0)
5959 outs() << "\tClass Definitions\n";
5960 for (j = 0; j < symtab.cls_def_cnt; j++) {
5961 if ((j + 1) * sizeof(uint32_t) > defs_left) {
5962 outs() << "\t(remaining class defs entries entends past the end of the "
5963 << "section)\n";
5964 break;
5965 }
5966 memcpy(&def, defs + j * sizeof(uint32_t), sizeof(uint32_t));
5967 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5968 sys::swapByteOrder(def);
5969
5970 r = get_pointer_32(def, xoffset, left, xS, &info, true);
5971 outs() << "\tdefs[" << j << "] " << format("0x%08" PRIx32, def);
5972 if (r != nullptr) {
5973 if (left > sizeof(struct objc_class_t)) {
5974 outs() << "\n";
5975 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5976 } else {
5977 outs() << " (entends past the end of the section)\n";
5978 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5979 memcpy(&objc_class, r, left);
5980 }
5981 if (O->isLittleEndian() != sys::IsLittleEndianHost)
5982 swapStruct(objc_class);
5983 print_objc_class_t(&objc_class, &info);
5984 } else {
5985 outs() << "(not in an __OBJC section)\n";
5986 }
5987
5988 if (CLS_GETINFO(&objc_class, CLS_CLASS)) {
5989 outs() << "\tMeta Class";
5990 r = get_pointer_32(objc_class.isa, xoffset, left, xS, &info, true);
5991 if (r != nullptr) {
5992 if (left > sizeof(struct objc_class_t)) {
5993 outs() << "\n";
5994 memcpy(&objc_class, r, sizeof(struct objc_class_t));
5995 } else {
5996 outs() << " (entends past the end of the section)\n";
5997 memset(&objc_class, '\0', sizeof(struct objc_class_t));
5998 memcpy(&objc_class, r, left);
5999 }
6000 if (O->isLittleEndian() != sys::IsLittleEndianHost)
6001 swapStruct(objc_class);
6002 print_objc_class_t(&objc_class, &info);
6003 } else {
6004 outs() << "(not in an __OBJC section)\n";
6005 }
6006 }
6007 }
6008 if (symtab.cat_def_cnt > 0)
6009 outs() << "\tCategory Definitions\n";
6010 for (j = 0; j < symtab.cat_def_cnt; j++) {
6011 if ((j + symtab.cls_def_cnt + 1) * sizeof(uint32_t) > defs_left) {
6012 outs() << "\t(remaining category defs entries entends past the end of "
6013 << "the section)\n";
6014 break;
6015 }
6016 memcpy(&def, defs + (j + symtab.cls_def_cnt) * sizeof(uint32_t),
6017 sizeof(uint32_t));
6018 if (O->isLittleEndian() != sys::IsLittleEndianHost)
6019 sys::swapByteOrder(def);
6020
6021 r = get_pointer_32(def, xoffset, left, xS, &info, true);
6022 outs() << "\tdefs[" << j + symtab.cls_def_cnt << "] "
6023 << format("0x%08" PRIx32, def);
6024 if (r != nullptr) {
6025 if (left > sizeof(struct objc_category_t)) {
6026 outs() << "\n";
6027 memcpy(&objc_category, r, sizeof(struct objc_category_t));
6028 } else {
6029 outs() << " (entends past the end of the section)\n";
6030 memset(&objc_category, '\0', sizeof(struct objc_category_t));
6031 memcpy(&objc_category, r, left);
6032 }
6033 if (O->isLittleEndian() != sys::IsLittleEndianHost)
6034 swapStruct(objc_category);
6035 print_objc_objc_category_t(&objc_category, &info);
6036 } else {
6037 outs() << "(not in an __OBJC section)\n";
6038 }
6039 }
6040 }
6041 const SectionRef II = get_section(O, "__OBJC", "__image_info");
6042 if (II != SectionRef())
6043 print_image_info(II, &info);
6044
6045 return true;
Kevin Enderby0fc11822015-04-01 20:57:01 +00006046}
6047
Kevin Enderby4ad9bde2015-04-16 22:33:20 +00006048static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
6049 uint32_t size, uint32_t addr) {
6050 SymbolAddressMap AddrMap;
6051 CreateSymbolAddressMap(O, &AddrMap);
6052
6053 std::vector<SectionRef> Sections;
6054 for (const SectionRef &Section : O->sections()) {
6055 StringRef SectName;
6056 Section.getName(SectName);
6057 Sections.push_back(Section);
6058 }
6059
Serge Pavlove4e9a1f2018-02-14 03:26:27 +00006060 struct DisassembleInfo info(O, &AddrMap, &Sections, true);
Kevin Enderby4ad9bde2015-04-16 22:33:20 +00006061
6062 const char *p;
6063 struct objc_protocol_t protocol;
6064 uint32_t left, paddr;
6065 for (p = sect; p < sect + size; p += sizeof(struct objc_protocol_t)) {
6066 memset(&protocol, '\0', sizeof(struct objc_protocol_t));
6067 left = size - (p - sect);
6068 if (left < sizeof(struct objc_protocol_t)) {
6069 outs() << "Protocol extends past end of __protocol section\n";
6070 memcpy(&protocol, p, left);
6071 } else
6072 memcpy(&protocol, p, sizeof(struct objc_protocol_t));
6073 if (O->isLittleEndian() != sys::IsLittleEndianHost)
6074 swapStruct(protocol);
6075 paddr = addr + (p - sect);
6076 outs() << "Protocol " << format("0x%" PRIx32, paddr);
6077 if (print_protocol(paddr, 0, &info))
6078 outs() << "(not in an __OBJC section)\n";
6079 }
6080}
6081
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006082#ifdef HAVE_LIBXAR
6083inline void swapStruct(struct xar_header &xar) {
6084 sys::swapByteOrder(xar.magic);
6085 sys::swapByteOrder(xar.size);
6086 sys::swapByteOrder(xar.version);
6087 sys::swapByteOrder(xar.toc_length_compressed);
6088 sys::swapByteOrder(xar.toc_length_uncompressed);
6089 sys::swapByteOrder(xar.cksum_alg);
6090}
6091
6092static void PrintModeVerbose(uint32_t mode) {
6093 switch(mode & S_IFMT){
6094 case S_IFDIR:
6095 outs() << "d";
6096 break;
6097 case S_IFCHR:
6098 outs() << "c";
6099 break;
6100 case S_IFBLK:
6101 outs() << "b";
6102 break;
6103 case S_IFREG:
6104 outs() << "-";
6105 break;
6106 case S_IFLNK:
6107 outs() << "l";
6108 break;
6109 case S_IFSOCK:
6110 outs() << "s";
6111 break;
6112 default:
6113 outs() << "?";
6114 break;
6115 }
6116
6117 /* owner permissions */
6118 if(mode & S_IREAD)
6119 outs() << "r";
6120 else
6121 outs() << "-";
6122 if(mode & S_IWRITE)
6123 outs() << "w";
6124 else
6125 outs() << "-";
6126 if(mode & S_ISUID)
6127 outs() << "s";
6128 else if(mode & S_IEXEC)
6129 outs() << "x";
6130 else
6131 outs() << "-";
6132
6133 /* group permissions */
6134 if(mode & (S_IREAD >> 3))
6135 outs() << "r";
6136 else
6137 outs() << "-";
6138 if(mode & (S_IWRITE >> 3))
6139 outs() << "w";
6140 else
6141 outs() << "-";
6142 if(mode & S_ISGID)
6143 outs() << "s";
6144 else if(mode & (S_IEXEC >> 3))
6145 outs() << "x";
6146 else
6147 outs() << "-";
6148
6149 /* other permissions */
6150 if(mode & (S_IREAD >> 6))
6151 outs() << "r";
6152 else
6153 outs() << "-";
6154 if(mode & (S_IWRITE >> 6))
6155 outs() << "w";
6156 else
6157 outs() << "-";
6158 if(mode & S_ISVTX)
6159 outs() << "t";
6160 else if(mode & (S_IEXEC >> 6))
6161 outs() << "x";
6162 else
6163 outs() << "-";
6164}
6165
6166static void PrintXarFilesSummary(const char *XarFilename, xar_t xar) {
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006167 xar_file_t xf;
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006168 const char *key, *type, *mode, *user, *group, *size, *mtime, *name, *m;
6169 char *endp;
6170 uint32_t mode_value;
6171
Francis Ricci6f942972017-10-06 15:33:28 +00006172 ScopedXarIter xi;
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006173 if (!xi) {
6174 errs() << "Can't obtain an xar iterator for xar archive "
6175 << XarFilename << "\n";
6176 return;
6177 }
6178
6179 // Go through the xar's files.
6180 for (xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)) {
Francis Ricci6f942972017-10-06 15:33:28 +00006181 ScopedXarIter xp;
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006182 if(!xp){
6183 errs() << "Can't obtain an xar iterator for xar archive "
6184 << XarFilename << "\n";
6185 return;
6186 }
6187 type = nullptr;
6188 mode = nullptr;
6189 user = nullptr;
6190 group = nullptr;
6191 size = nullptr;
6192 mtime = nullptr;
6193 name = nullptr;
6194 for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){
Jonas Devliegherec0a758d2017-09-18 14:15:57 +00006195 const char *val = nullptr;
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006196 xar_prop_get(xf, key, &val);
6197#if 0 // Useful for debugging.
6198 outs() << "key: " << key << " value: " << val << "\n";
6199#endif
6200 if(strcmp(key, "type") == 0)
6201 type = val;
6202 if(strcmp(key, "mode") == 0)
6203 mode = val;
6204 if(strcmp(key, "user") == 0)
6205 user = val;
6206 if(strcmp(key, "group") == 0)
6207 group = val;
6208 if(strcmp(key, "data/size") == 0)
6209 size = val;
6210 if(strcmp(key, "mtime") == 0)
6211 mtime = val;
6212 if(strcmp(key, "name") == 0)
6213 name = val;
6214 }
6215 if(mode != nullptr){
6216 mode_value = strtoul(mode, &endp, 8);
6217 if(*endp != '\0')
6218 outs() << "(mode: \"" << mode << "\" contains non-octal chars) ";
6219 if(strcmp(type, "file") == 0)
6220 mode_value |= S_IFREG;
6221 PrintModeVerbose(mode_value);
6222 outs() << " ";
6223 }
6224 if(user != nullptr)
6225 outs() << format("%10s/", user);
6226 if(group != nullptr)
6227 outs() << format("%-10s ", group);
6228 if(size != nullptr)
6229 outs() << format("%7s ", size);
6230 if(mtime != nullptr){
6231 for(m = mtime; *m != 'T' && *m != '\0'; m++)
6232 outs() << *m;
6233 if(*m == 'T')
6234 m++;
6235 outs() << " ";
6236 for( ; *m != 'Z' && *m != '\0'; m++)
6237 outs() << *m;
6238 outs() << " ";
6239 }
6240 if(name != nullptr)
6241 outs() << name;
6242 outs() << "\n";
6243 }
6244}
6245
6246static void DumpBitcodeSection(MachOObjectFile *O, const char *sect,
6247 uint32_t size, bool verbose,
6248 bool PrintXarHeader, bool PrintXarFileHeaders,
6249 std::string XarMemberName) {
6250 if(size < sizeof(struct xar_header)) {
6251 outs() << "size of (__LLVM,__bundle) section too small (smaller than size "
6252 "of struct xar_header)\n";
6253 return;
6254 }
6255 struct xar_header XarHeader;
6256 memcpy(&XarHeader, sect, sizeof(struct xar_header));
6257 if (sys::IsLittleEndianHost)
6258 swapStruct(XarHeader);
6259 if (PrintXarHeader) {
6260 if (!XarMemberName.empty())
6261 outs() << "In xar member " << XarMemberName << ": ";
6262 else
6263 outs() << "For (__LLVM,__bundle) section: ";
6264 outs() << "xar header\n";
6265 if (XarHeader.magic == XAR_HEADER_MAGIC)
6266 outs() << " magic XAR_HEADER_MAGIC\n";
6267 else
6268 outs() << " magic "
6269 << format_hex(XarHeader.magic, 10, true)
6270 << " (not XAR_HEADER_MAGIC)\n";
6271 outs() << " size " << XarHeader.size << "\n";
6272 outs() << " version " << XarHeader.version << "\n";
6273 outs() << " toc_length_compressed " << XarHeader.toc_length_compressed
6274 << "\n";
6275 outs() << "toc_length_uncompressed " << XarHeader.toc_length_uncompressed
6276 << "\n";
6277 outs() << " cksum_alg ";
6278 switch (XarHeader.cksum_alg) {
6279 case XAR_CKSUM_NONE:
6280 outs() << "XAR_CKSUM_NONE\n";
6281 break;
6282 case XAR_CKSUM_SHA1:
6283 outs() << "XAR_CKSUM_SHA1\n";
6284 break;
6285 case XAR_CKSUM_MD5:
6286 outs() << "XAR_CKSUM_MD5\n";
6287 break;
Kevin Enderby42882282016-05-23 22:18:59 +00006288#ifdef XAR_CKSUM_SHA256
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006289 case XAR_CKSUM_SHA256:
6290 outs() << "XAR_CKSUM_SHA256\n";
6291 break;
Kevin Enderby42882282016-05-23 22:18:59 +00006292#endif
6293#ifdef XAR_CKSUM_SHA512
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006294 case XAR_CKSUM_SHA512:
6295 outs() << "XAR_CKSUM_SHA512\n";
6296 break;
Kevin Enderby42882282016-05-23 22:18:59 +00006297#endif
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006298 default:
6299 outs() << XarHeader.cksum_alg << "\n";
6300 }
6301 }
6302
6303 SmallString<128> XarFilename;
6304 int FD;
6305 std::error_code XarEC =
6306 sys::fs::createTemporaryFile("llvm-objdump", "xar", FD, XarFilename);
6307 if (XarEC) {
6308 errs() << XarEC.message() << "\n";
6309 return;
6310 }
Reid Kleckner3fc649c2017-09-23 01:03:17 +00006311 ToolOutputFile XarFile(XarFilename, FD);
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006312 raw_fd_ostream &XarOut = XarFile.os();
6313 StringRef XarContents(sect, size);
6314 XarOut << XarContents;
6315 XarOut.close();
6316 if (XarOut.has_error())
6317 return;
6318
Francis Ricci6f942972017-10-06 15:33:28 +00006319 ScopedXarFile xar(XarFilename.c_str(), READ);
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006320 if (!xar) {
6321 errs() << "Can't create temporary xar archive " << XarFilename << "\n";
6322 return;
6323 }
6324
6325 SmallString<128> TocFilename;
6326 std::error_code TocEC =
6327 sys::fs::createTemporaryFile("llvm-objdump", "toc", TocFilename);
6328 if (TocEC) {
6329 errs() << TocEC.message() << "\n";
6330 return;
6331 }
6332 xar_serialize(xar, TocFilename.c_str());
6333
6334 if (PrintXarFileHeaders) {
6335 if (!XarMemberName.empty())
6336 outs() << "In xar member " << XarMemberName << ": ";
6337 else
6338 outs() << "For (__LLVM,__bundle) section: ";
6339 outs() << "xar archive files:\n";
6340 PrintXarFilesSummary(XarFilename.c_str(), xar);
6341 }
6342
6343 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
6344 MemoryBuffer::getFileOrSTDIN(TocFilename.c_str());
6345 if (std::error_code EC = FileOrErr.getError()) {
6346 errs() << EC.message() << "\n";
6347 return;
6348 }
6349 std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
6350
6351 if (!XarMemberName.empty())
6352 outs() << "In xar member " << XarMemberName << ": ";
6353 else
6354 outs() << "For (__LLVM,__bundle) section: ";
6355 outs() << "xar table of contents:\n";
6356 outs() << Buffer->getBuffer() << "\n";
6357
6358 // TODO: Go through the xar's files.
Francis Ricci6f942972017-10-06 15:33:28 +00006359 ScopedXarIter xi;
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006360 if(!xi){
6361 errs() << "Can't obtain an xar iterator for xar archive "
6362 << XarFilename.c_str() << "\n";
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006363 return;
6364 }
6365 for(xar_file_t xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)){
6366 const char *key;
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006367 const char *member_name, *member_type, *member_size_string;
6368 size_t member_size;
6369
Francis Ricci6f942972017-10-06 15:33:28 +00006370 ScopedXarIter xp;
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006371 if(!xp){
6372 errs() << "Can't obtain an xar iterator for xar archive "
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +00006373 << XarFilename.c_str() << "\n";
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006374 return;
6375 }
6376 member_name = NULL;
6377 member_type = NULL;
6378 member_size_string = NULL;
6379 for(key = xar_prop_first(xf, xp); key; key = xar_prop_next(xp)){
Jonas Devliegherec0a758d2017-09-18 14:15:57 +00006380 const char *val = nullptr;
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006381 xar_prop_get(xf, key, &val);
6382#if 0 // Useful for debugging.
6383 outs() << "key: " << key << " value: " << val << "\n";
6384#endif
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +00006385 if (strcmp(key, "name") == 0)
6386 member_name = val;
6387 if (strcmp(key, "type") == 0)
6388 member_type = val;
6389 if (strcmp(key, "data/size") == 0)
6390 member_size_string = val;
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006391 }
6392 /*
6393 * If we find a file with a name, date/size and type properties
6394 * and with the type being "file" see if that is a xar file.
6395 */
6396 if (member_name != NULL && member_type != NULL &&
6397 strcmp(member_type, "file") == 0 &&
6398 member_size_string != NULL){
6399 // Extract the file into a buffer.
6400 char *endptr;
6401 member_size = strtoul(member_size_string, &endptr, 10);
6402 if (*endptr == '\0' && member_size != 0) {
Francis Ricci1bae0ac2017-09-13 13:57:45 +00006403 char *buffer;
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +00006404 if (xar_extract_tobuffersz(xar, xf, &buffer, &member_size) == 0) {
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006405#if 0 // Useful for debugging.
NAKAMURA Takumi6f43bd42017-10-18 13:31:28 +00006406 outs() << "xar member: " << member_name << " extracted\n";
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006407#endif
6408 // Set the XarMemberName we want to see printed in the header.
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +00006409 std::string OldXarMemberName;
6410 // If XarMemberName is already set this is nested. So
6411 // save the old name and create the nested name.
6412 if (!XarMemberName.empty()) {
6413 OldXarMemberName = XarMemberName;
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006414 XarMemberName =
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +00006415 (Twine("[") + XarMemberName + "]" + member_name).str();
6416 } else {
6417 OldXarMemberName = "";
6418 XarMemberName = member_name;
6419 }
6420 // See if this is could be a xar file (nested).
6421 if (member_size >= sizeof(struct xar_header)) {
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006422#if 0 // Useful for debugging.
NAKAMURA Takumi6f43bd42017-10-18 13:31:28 +00006423 outs() << "could be a xar file: " << member_name << "\n";
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006424#endif
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +00006425 memcpy((char *)&XarHeader, buffer, sizeof(struct xar_header));
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006426 if (sys::IsLittleEndianHost)
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +00006427 swapStruct(XarHeader);
6428 if (XarHeader.magic == XAR_HEADER_MAGIC)
6429 DumpBitcodeSection(O, buffer, member_size, verbose,
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006430 PrintXarHeader, PrintXarFileHeaders,
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +00006431 XarMemberName);
6432 }
6433 XarMemberName = OldXarMemberName;
Francis Ricci1bae0ac2017-09-13 13:57:45 +00006434 delete buffer;
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +00006435 }
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006436 }
6437 }
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006438 }
Kevin Enderby9873e2c2016-05-23 21:34:12 +00006439}
6440#endif // defined(HAVE_LIBXAR)
6441
Kevin Enderby0fc11822015-04-01 20:57:01 +00006442static void printObjcMetaData(MachOObjectFile *O, bool verbose) {
6443 if (O->is64Bit())
6444 printObjc2_64bit_MetaData(O, verbose);
6445 else {
6446 MachO::mach_header H;
6447 H = O->getHeader();
6448 if (H.cputype == MachO::CPU_TYPE_ARM)
6449 printObjc2_32bit_MetaData(O, verbose);
6450 else {
6451 // This is the 32-bit non-arm cputype case. Which is normally
6452 // the first Objective-C ABI. But it may be the case of a
6453 // binary for the iOS simulator which is the second Objective-C
6454 // ABI. In that case printObjc1_32bit_MetaData() will determine that
6455 // and return false.
Hans Wennborgcc9deb42015-09-29 18:02:48 +00006456 if (!printObjc1_32bit_MetaData(O, verbose))
Kevin Enderby0fc11822015-04-01 20:57:01 +00006457 printObjc2_32bit_MetaData(O, verbose);
6458 }
6459 }
6460}
6461
Kevin Enderbybf246f52014-09-24 23:08:22 +00006462// GuessLiteralPointer returns a string which for the item in the Mach-O file
6463// for the address passed in as ReferenceValue for printing as a comment with
6464// the instruction and also returns the corresponding type of that item
6465// indirectly through ReferenceType.
6466//
6467// If ReferenceValue is an address of literal cstring then a pointer to the
6468// cstring is returned and ReferenceType is set to
6469// LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr .
6470//
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006471// If ReferenceValue is an address of an Objective-C CFString, Selector ref or
6472// Class ref that name is returned and the ReferenceType is set accordingly.
6473//
6474// Lastly, literals which are Symbol address in a literal pool are looked for
6475// and if found the symbol name is returned and ReferenceType is set to
6476// LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr .
6477//
6478// If there is no item in the Mach-O file for the address passed in as
6479// ReferenceValue nullptr is returned and ReferenceType is unchanged.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00006480static const char *GuessLiteralPointer(uint64_t ReferenceValue,
6481 uint64_t ReferencePC,
6482 uint64_t *ReferenceType,
6483 struct DisassembleInfo *info) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00006484 // First see if there is an external relocation entry at the ReferencePC.
Kevin Enderbyd90a4172015-10-10 00:05:01 +00006485 if (info->O->getHeader().filetype == MachO::MH_OBJECT) {
6486 uint64_t sect_addr = info->S.getAddress();
6487 uint64_t sect_offset = ReferencePC - sect_addr;
6488 bool reloc_found = false;
6489 DataRefImpl Rel;
6490 MachO::any_relocation_info RE;
6491 bool isExtern = false;
6492 SymbolRef Symbol;
6493 for (const RelocationRef &Reloc : info->S.relocations()) {
6494 uint64_t RelocOffset = Reloc.getOffset();
6495 if (RelocOffset == sect_offset) {
6496 Rel = Reloc.getRawDataRefImpl();
6497 RE = info->O->getRelocation(Rel);
6498 if (info->O->isRelocationScattered(RE))
6499 continue;
6500 isExtern = info->O->getPlainRelocationExternal(RE);
6501 if (isExtern) {
6502 symbol_iterator RelocSym = Reloc.getSymbol();
6503 Symbol = *RelocSym;
6504 }
6505 reloc_found = true;
6506 break;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006507 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006508 }
Kevin Enderbyd90a4172015-10-10 00:05:01 +00006509 // If there is an external relocation entry for a symbol in a section
6510 // then used that symbol's value for the value of the reference.
6511 if (reloc_found && isExtern) {
6512 if (info->O->getAnyRelocationPCRel(RE)) {
6513 unsigned Type = info->O->getAnyRelocationType(RE);
6514 if (Type == MachO::X86_64_RELOC_SIGNED) {
6515 ReferenceValue = Symbol.getValue();
6516 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006517 }
6518 }
6519 }
6520
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006521 // Look for literals such as Objective-C CFStrings refs, Selector refs,
6522 // Message refs and Class refs.
6523 bool classref, selref, msgref, cfstring;
6524 uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref,
6525 selref, msgref, cfstring);
David Blaikie33dd45d02015-03-23 18:39:02 +00006526 if (classref && pointer_value == 0) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006527 // Note the ReferenceValue is a pointer into the __objc_classrefs section.
6528 // And the pointer_value in that section is typically zero as it will be
6529 // set by dyld as part of the "bind information".
6530 const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info);
6531 if (name != nullptr) {
6532 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
Hans Wennborgdb53e302014-10-23 21:59:17 +00006533 const char *class_name = strrchr(name, '$');
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006534 if (class_name != nullptr && class_name[1] == '_' &&
6535 class_name[2] != '\0') {
6536 info->class_name = class_name + 2;
6537 return name;
6538 }
6539 }
6540 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006541
David Blaikie33dd45d02015-03-23 18:39:02 +00006542 if (classref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006543 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
6544 const char *name =
6545 get_objc2_64bit_class_name(pointer_value, ReferenceValue, info);
6546 if (name != nullptr)
6547 info->class_name = name;
6548 else
6549 name = "bad class ref";
Kevin Enderbybf246f52014-09-24 23:08:22 +00006550 return name;
6551 }
6552
David Blaikie33dd45d02015-03-23 18:39:02 +00006553 if (cfstring) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006554 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref;
6555 const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info);
6556 return name;
6557 }
6558
David Blaikie33dd45d02015-03-23 18:39:02 +00006559 if (selref && pointer_value == 0)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006560 pointer_value = get_objc2_64bit_selref(ReferenceValue, info);
6561
6562 if (pointer_value != 0)
6563 ReferenceValue = pointer_value;
6564
6565 const char *name = GuessCstringPointer(ReferenceValue, info);
6566 if (name) {
David Blaikie33dd45d02015-03-23 18:39:02 +00006567 if (pointer_value != 0 && selref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006568 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref;
6569 info->selector_name = name;
David Blaikie33dd45d02015-03-23 18:39:02 +00006570 } else if (pointer_value != 0 && msgref) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006571 info->class_name = nullptr;
6572 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref;
6573 info->selector_name = name;
6574 } else
6575 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr;
6576 return name;
6577 }
6578
6579 // Lastly look for an indirect symbol with this ReferenceValue which is in
6580 // a literal pool. If found return that symbol name.
6581 name = GuessIndirectSymbol(ReferenceValue, info);
6582 if (name) {
6583 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr;
6584 return name;
6585 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006586
6587 return nullptr;
6588}
6589
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006590// SymbolizerSymbolLookUp is the symbol lookup function passed when creating
Kevin Enderbybf246f52014-09-24 23:08:22 +00006591// the Symbolizer. It looks up the ReferenceValue using the info passed via the
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006592// pointer to the struct DisassembleInfo that was passed when MCSymbolizer
6593// is created and returns the symbol name that matches the ReferenceValue or
6594// nullptr if none. The ReferenceType is passed in for the IN type of
6595// reference the instruction is making from the values in defined in the header
6596// "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific
6597// Out type and the ReferenceName will also be set which is added as a comment
6598// to the disassembled instruction.
6599//
Kevin Enderby04bf6932014-10-28 23:39:46 +00006600// If the symbol name is a C++ mangled name then the demangled name is
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006601// returned through ReferenceName and ReferenceType is set to
6602// LLVMDisassembler_ReferenceType_DeMangled_Name .
6603//
6604// When this is called to get a symbol name for a branch target then the
6605// ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
6606// SymbolValue will be looked for in the indirect symbol table to determine if
6607// it is an address for a symbol stub. If so then the symbol name for that
6608// stub is returned indirectly through ReferenceName and then ReferenceType is
6609// set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
6610//
Kevin Enderbybf246f52014-09-24 23:08:22 +00006611// When this is called with an value loaded via a PC relative load then
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006612// ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
6613// SymbolValue is checked to be an address of literal pointer, symbol pointer,
6614// or an Objective-C meta data reference. If so the output ReferenceType is
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006615// set to correspond to that as well as setting the ReferenceName.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00006616static const char *SymbolizerSymbolLookUp(void *DisInfo,
6617 uint64_t ReferenceValue,
6618 uint64_t *ReferenceType,
6619 uint64_t ReferencePC,
6620 const char **ReferenceName) {
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006621 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006622 // If no verbose symbolic information is wanted then just return nullptr.
David Blaikie33dd45d02015-03-23 18:39:02 +00006623 if (!info->verbose) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00006624 *ReferenceName = nullptr;
6625 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006626 return nullptr;
6627 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00006628
Kevin Enderbyf6d25852015-01-31 00:37:11 +00006629 const char *SymbolName = GuessSymbolName(ReferenceValue, info->AddrMap);
Kevin Enderbybf246f52014-09-24 23:08:22 +00006630
Kevin Enderby85974882014-09-26 22:20:44 +00006631 if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) {
6632 *ReferenceName = GuessIndirectSymbol(ReferenceValue, info);
Kevin Enderby04bf6932014-10-28 23:39:46 +00006633 if (*ReferenceName != nullptr) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006634 method_reference(info, ReferenceType, ReferenceName);
6635 if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message)
6636 *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub;
Rafael Espindolab940b662016-09-06 19:16:48 +00006637 } else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
Kevin Enderby04bf6932014-10-28 23:39:46 +00006638 if (info->demangled_name != nullptr)
6639 free(info->demangled_name);
6640 int status;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006641 info->demangled_name =
Rafael Espindolab940b662016-09-06 19:16:48 +00006642 itaniumDemangle(SymbolName + 1, nullptr, nullptr, &status);
Kevin Enderby04bf6932014-10-28 23:39:46 +00006643 if (info->demangled_name != nullptr) {
6644 *ReferenceName = info->demangled_name;
6645 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
6646 } else
6647 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6648 } else
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006649 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6650 } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) {
6651 *ReferenceName =
6652 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
Kevin Enderby85974882014-09-26 22:20:44 +00006653 if (*ReferenceName)
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006654 method_reference(info, ReferenceType, ReferenceName);
Kevin Enderby85974882014-09-26 22:20:44 +00006655 else
6656 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006657 // If this is arm64 and the reference is an adrp instruction save the
6658 // instruction, passed in ReferenceValue and the address of the instruction
6659 // for use later if we see and add immediate instruction.
6660 } else if (info->O->getArch() == Triple::aarch64 &&
6661 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) {
6662 info->adrp_inst = ReferenceValue;
6663 info->adrp_addr = ReferencePC;
6664 SymbolName = nullptr;
6665 *ReferenceName = nullptr;
6666 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6667 // If this is arm64 and reference is an add immediate instruction and we
6668 // have
6669 // seen an adrp instruction just before it and the adrp's Xd register
6670 // matches
6671 // this add's Xn register reconstruct the value being referenced and look to
6672 // see if it is a literal pointer. Note the add immediate instruction is
6673 // passed in ReferenceValue.
6674 } else if (info->O->getArch() == Triple::aarch64 &&
6675 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri &&
6676 ReferencePC - 4 == info->adrp_addr &&
6677 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
6678 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
6679 uint32_t addxri_inst;
6680 uint64_t adrp_imm, addxri_imm;
6681
6682 adrp_imm =
6683 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
6684 if (info->adrp_inst & 0x0200000)
6685 adrp_imm |= 0xfffffffffc000000LL;
6686
6687 addxri_inst = ReferenceValue;
6688 addxri_imm = (addxri_inst >> 10) & 0xfff;
6689 if (((addxri_inst >> 22) & 0x3) == 1)
6690 addxri_imm <<= 12;
6691
6692 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
6693 (adrp_imm << 12) + addxri_imm;
6694
6695 *ReferenceName =
6696 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6697 if (*ReferenceName == nullptr)
6698 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6699 // If this is arm64 and the reference is a load register instruction and we
6700 // have seen an adrp instruction just before it and the adrp's Xd register
6701 // matches this add's Xn register reconstruct the value being referenced and
6702 // look to see if it is a literal pointer. Note the load register
6703 // instruction is passed in ReferenceValue.
6704 } else if (info->O->getArch() == Triple::aarch64 &&
6705 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXui &&
6706 ReferencePC - 4 == info->adrp_addr &&
6707 (info->adrp_inst & 0x9f000000) == 0x90000000 &&
6708 (info->adrp_inst & 0x1f) == ((ReferenceValue >> 5) & 0x1f)) {
6709 uint32_t ldrxui_inst;
6710 uint64_t adrp_imm, ldrxui_imm;
6711
6712 adrp_imm =
6713 ((info->adrp_inst & 0x00ffffe0) >> 3) | ((info->adrp_inst >> 29) & 0x3);
6714 if (info->adrp_inst & 0x0200000)
6715 adrp_imm |= 0xfffffffffc000000LL;
6716
6717 ldrxui_inst = ReferenceValue;
6718 ldrxui_imm = (ldrxui_inst >> 10) & 0xfff;
6719
6720 ReferenceValue = (info->adrp_addr & 0xfffffffffffff000LL) +
6721 (adrp_imm << 12) + (ldrxui_imm << 3);
6722
6723 *ReferenceName =
6724 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6725 if (*ReferenceName == nullptr)
6726 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6727 }
6728 // If this arm64 and is an load register (PC-relative) instruction the
6729 // ReferenceValue is the PC plus the immediate value.
6730 else if (info->O->getArch() == Triple::aarch64 &&
6731 (*ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_LDRXl ||
6732 *ReferenceType == LLVMDisassembler_ReferenceType_In_ARM64_ADR)) {
6733 *ReferenceName =
6734 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
6735 if (*ReferenceName == nullptr)
6736 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
Rafael Espindolab940b662016-09-06 19:16:48 +00006737 } else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
Kevin Enderby04bf6932014-10-28 23:39:46 +00006738 if (info->demangled_name != nullptr)
6739 free(info->demangled_name);
6740 int status;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00006741 info->demangled_name =
Rafael Espindolab940b662016-09-06 19:16:48 +00006742 itaniumDemangle(SymbolName + 1, nullptr, nullptr, &status);
Kevin Enderby04bf6932014-10-28 23:39:46 +00006743 if (info->demangled_name != nullptr) {
6744 *ReferenceName = info->demangled_name;
6745 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
6746 }
6747 }
Kevin Enderby6f326ce2014-10-23 19:37:31 +00006748 else {
Kevin Enderbybf246f52014-09-24 23:08:22 +00006749 *ReferenceName = nullptr;
6750 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
6751 }
6752
6753 return SymbolName;
6754}
6755
Kevin Enderbybf246f52014-09-24 23:08:22 +00006756/// \brief Emits the comments that are stored in the CommentStream.
6757/// Each comment in the CommentStream must end with a newline.
6758static void emitComments(raw_svector_ostream &CommentStream,
6759 SmallString<128> &CommentsToEmit,
6760 formatted_raw_ostream &FormattedOS,
6761 const MCAsmInfo &MAI) {
6762 // Flush the stream before taking its content.
Kevin Enderbybf246f52014-09-24 23:08:22 +00006763 StringRef Comments = CommentsToEmit.str();
6764 // Get the default information for printing a comment.
Mehdi Amini36d33fc2016-10-01 06:46:33 +00006765 StringRef CommentBegin = MAI.getCommentString();
Kevin Enderbybf246f52014-09-24 23:08:22 +00006766 unsigned CommentColumn = MAI.getCommentColumn();
6767 bool IsFirst = true;
6768 while (!Comments.empty()) {
6769 if (!IsFirst)
6770 FormattedOS << '\n';
6771 // Emit a line of comments.
6772 FormattedOS.PadToColumn(CommentColumn);
6773 size_t Position = Comments.find('\n');
6774 FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
6775 // Move after the newline character.
6776 Comments = Comments.substr(Position + 1);
6777 IsFirst = false;
6778 }
6779 FormattedOS.flush();
6780
6781 // Tell the comment stream that the vector changed underneath it.
6782 CommentsToEmit.clear();
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006783}
6784
Kevin Enderby95df54c2015-02-04 01:01:38 +00006785static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
6786 StringRef DisSegName, StringRef DisSectName) {
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006787 const char *McpuDefault = nullptr;
6788 const Target *ThumbTarget = nullptr;
6789 const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006790 if (!TheTarget) {
6791 // GetTarget prints out stuff.
6792 return;
6793 }
Kevin Enderbyf310e622017-09-21 21:45:02 +00006794 std::string MachOMCPU;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006795 if (MCPU.empty() && McpuDefault)
Kevin Enderbyf310e622017-09-21 21:45:02 +00006796 MachOMCPU = McpuDefault;
6797 else
6798 MachOMCPU = MCPU;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006799
Ahmed Charles56440fd2014-03-06 05:51:42 +00006800 std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006801 std::unique_ptr<const MCInstrInfo> ThumbInstrInfo;
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006802 if (ThumbTarget)
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006803 ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo());
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006804
Kevin Enderbyc9595622014-08-06 23:24:41 +00006805 // Package up features to be passed to target/subtarget
6806 std::string FeaturesStr;
6807 if (MAttrs.size()) {
6808 SubtargetFeatures Features;
6809 for (unsigned i = 0; i != MAttrs.size(); ++i)
6810 Features.AddFeature(MAttrs[i]);
6811 FeaturesStr = Features.getString();
6812 }
6813
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006814 // Set up disassembler.
Ahmed Charles56440fd2014-03-06 05:51:42 +00006815 std::unique_ptr<const MCRegisterInfo> MRI(
6816 TheTarget->createMCRegInfo(TripleName));
6817 std::unique_ptr<const MCAsmInfo> AsmInfo(
Rafael Espindola227144c2013-05-13 01:16:13 +00006818 TheTarget->createMCAsmInfo(*MRI, TripleName));
Ahmed Charles56440fd2014-03-06 05:51:42 +00006819 std::unique_ptr<const MCSubtargetInfo> STI(
Kevin Enderbyf310e622017-09-21 21:45:02 +00006820 TheTarget->createMCSubtargetInfo(TripleName, MachOMCPU, FeaturesStr));
Craig Toppere6cb63e2014-04-25 04:24:47 +00006821 MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr);
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006822 std::unique_ptr<MCDisassembler> DisAsm(
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006823 TheTarget->createMCDisassembler(*STI, Ctx));
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006824 std::unique_ptr<MCSymbolizer> Symbolizer;
Serge Pavlove4e9a1f2018-02-14 03:26:27 +00006825 struct DisassembleInfo SymbolizerInfo(nullptr, nullptr, nullptr, false);
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006826 std::unique_ptr<MCRelocationInfo> RelInfo(
6827 TheTarget->createMCRelocationInfo(TripleName, Ctx));
6828 if (RelInfo) {
6829 Symbolizer.reset(TheTarget->createMCSymbolizer(
6830 TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
David Blaikie186db432015-01-18 20:45:48 +00006831 &SymbolizerInfo, &Ctx, std::move(RelInfo)));
Kevin Enderby98c9acc2014-09-16 18:00:57 +00006832 DisAsm->setSymbolizer(std::move(Symbolizer));
6833 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006834 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
Ahmed Charles56440fd2014-03-06 05:51:42 +00006835 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
Daniel Sanders50f17232015-09-15 16:17:27 +00006836 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI));
Kevin Enderbybf246f52014-09-24 23:08:22 +00006837 // Set the display preference for hex vs. decimal immediates.
6838 IP->setPrintImmHex(PrintImmHex);
6839 // Comment stream and backing vector.
6840 SmallString<128> CommentsToEmit;
6841 raw_svector_ostream CommentStream(CommentsToEmit);
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00006842 // FIXME: Setting the CommentStream in the InstPrinter is problematic in that
6843 // if it is done then arm64 comments for string literals don't get printed
6844 // and some constant get printed instead and not setting it causes intel
6845 // (32-bit and 64-bit) comments printed with different spacing before the
6846 // comment causing different diffs with the 'C' disassembler library API.
6847 // IP->setCommentStream(CommentStream);
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006848
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006849 if (!AsmInfo || !STI || !DisAsm || !IP) {
Michael J. Spencerc1363cf2011-10-07 19:25:47 +00006850 errs() << "error: couldn't initialize disassembler for target "
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00006851 << TripleName << '\n';
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006852 return;
6853 }
6854
Tim Northover09ca33e2016-04-22 23:23:31 +00006855 // Set up separate thumb disassembler if needed.
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006856 std::unique_ptr<const MCRegisterInfo> ThumbMRI;
6857 std::unique_ptr<const MCAsmInfo> ThumbAsmInfo;
6858 std::unique_ptr<const MCSubtargetInfo> ThumbSTI;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006859 std::unique_ptr<MCDisassembler> ThumbDisAsm;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006860 std::unique_ptr<MCInstPrinter> ThumbIP;
6861 std::unique_ptr<MCContext> ThumbCtx;
Kevin Enderby930fdc72014-11-06 19:00:13 +00006862 std::unique_ptr<MCSymbolizer> ThumbSymbolizer;
Serge Pavlove4e9a1f2018-02-14 03:26:27 +00006863 struct DisassembleInfo ThumbSymbolizerInfo(nullptr, nullptr, nullptr, false);
Kevin Enderby930fdc72014-11-06 19:00:13 +00006864 std::unique_ptr<MCRelocationInfo> ThumbRelInfo;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006865 if (ThumbTarget) {
6866 ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName));
6867 ThumbAsmInfo.reset(
6868 ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName));
6869 ThumbSTI.reset(
Kevin Enderbyf310e622017-09-21 21:45:02 +00006870 ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MachOMCPU,
6871 FeaturesStr));
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006872 ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr));
6873 ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx));
Kevin Enderby930fdc72014-11-06 19:00:13 +00006874 MCContext *PtrThumbCtx = ThumbCtx.get();
6875 ThumbRelInfo.reset(
6876 ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx));
6877 if (ThumbRelInfo) {
6878 ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer(
6879 ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
David Blaikie186db432015-01-18 20:45:48 +00006880 &ThumbSymbolizerInfo, PtrThumbCtx, std::move(ThumbRelInfo)));
Kevin Enderby930fdc72014-11-06 19:00:13 +00006881 ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer));
6882 }
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006883 int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect();
6884 ThumbIP.reset(ThumbTarget->createMCInstPrinter(
Daniel Sanders50f17232015-09-15 16:17:27 +00006885 Triple(ThumbTripleName), ThumbAsmPrinterVariant, *ThumbAsmInfo,
6886 *ThumbInstrInfo, *ThumbMRI));
Kevin Enderbybf246f52014-09-24 23:08:22 +00006887 // Set the display preference for hex vs. decimal immediates.
6888 ThumbIP->setPrintImmHex(PrintImmHex);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006889 }
6890
Kevin Enderbyae3c1262014-11-14 21:52:18 +00006891 if (ThumbTarget && (!ThumbAsmInfo || !ThumbSTI || !ThumbDisAsm || !ThumbIP)) {
Kevin Enderbyec5ca032014-08-18 20:21:02 +00006892 errs() << "error: couldn't initialize disassembler for target "
6893 << ThumbTripleName << '\n';
6894 return;
6895 }
6896
Charles Davis8bdfafd2013-09-01 04:28:48 +00006897 MachO::mach_header Header = MachOOF->getHeader();
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006898
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006899 // FIXME: Using the -cfg command line option, this code used to be able to
6900 // annotate relocations with the referenced symbol's name, and if this was
6901 // inside a __[cf]string section, the data it points to. This is now replaced
6902 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
Owen Andersond9243c42011-10-17 21:37:35 +00006903 std::vector<SectionRef> Sections;
6904 std::vector<SymbolRef> Symbols;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006905 SmallVector<uint64_t, 8> FoundFns;
Kevin Enderby273ae012013-06-06 17:20:50 +00006906 uint64_t BaseSegmentAddress;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006907
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00006908 getSectionsAndSymbols(MachOOF, Sections, Symbols, FoundFns,
Kevin Enderby273ae012013-06-06 17:20:50 +00006909 BaseSegmentAddress);
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006910
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006911 // Sort the symbols by address, just in case they didn't come in that way.
Owen Andersond9243c42011-10-17 21:37:35 +00006912 std::sort(Symbols.begin(), Symbols.end(), SymbolSorter());
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006913
Kevin Enderby273ae012013-06-06 17:20:50 +00006914 // Build a data in code table that is sorted on by the address of each entry.
6915 uint64_t BaseAddress = 0;
Charles Davis8bdfafd2013-09-01 04:28:48 +00006916 if (Header.filetype == MachO::MH_OBJECT)
Rafael Espindola80291272014-10-08 15:28:58 +00006917 BaseAddress = Sections[0].getAddress();
Kevin Enderby273ae012013-06-06 17:20:50 +00006918 else
6919 BaseAddress = BaseSegmentAddress;
6920 DiceTable Dices;
Kevin Enderby273ae012013-06-06 17:20:50 +00006921 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
Rafael Espindola5e812af2014-01-30 02:49:50 +00006922 DI != DE; ++DI) {
Kevin Enderby273ae012013-06-06 17:20:50 +00006923 uint32_t Offset;
6924 DI->getOffset(Offset);
6925 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
6926 }
6927 array_pod_sort(Dices.begin(), Dices.end());
6928
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006929#ifndef NDEBUG
6930 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
6931#else
6932 raw_ostream &DebugOut = nulls();
6933#endif
6934
Ahmed Charles56440fd2014-03-06 05:51:42 +00006935 std::unique_ptr<DIContext> diContext;
Rafael Espindola9b709252013-04-13 01:45:40 +00006936 ObjectFile *DbgObj = MachOOF;
Benjamin Kramer699128e2011-09-21 01:13:19 +00006937 // Try to find debug info and set up the DIContext for it.
6938 if (UseDbg) {
Benjamin Kramer699128e2011-09-21 01:13:19 +00006939 // A separate DSym file path was specified, parse it as a macho file,
6940 // get the sections and supply it to the section name parsing machinery.
6941 if (!DSYMFile.empty()) {
Rafael Espindola48af1c22014-08-19 18:44:46 +00006942 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
Rafael Espindolaadf21f22014-07-06 17:43:13 +00006943 MemoryBuffer::getFileOrSTDIN(DSYMFile);
Rafael Espindola48af1c22014-08-19 18:44:46 +00006944 if (std::error_code EC = BufOrErr.getError()) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +00006945 errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n';
Benjamin Kramer699128e2011-09-21 01:13:19 +00006946 return;
6947 }
Rafael Espindola48af1c22014-08-19 18:44:46 +00006948 DbgObj =
6949 ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef())
6950 .get()
6951 .release();
Benjamin Kramer699128e2011-09-21 01:13:19 +00006952 }
6953
Eric Christopher7370b552012-11-12 21:40:38 +00006954 // Setup the DIContext
Rafael Espindolac398e672017-07-19 22:27:28 +00006955 diContext = DWARFContext::create(*DbgObj);
Benjamin Kramer699128e2011-09-21 01:13:19 +00006956 }
6957
Colin LeMahieufcc32762015-07-29 19:08:10 +00006958 if (FilterSections.size() == 0)
Kevin Enderby95df54c2015-02-04 01:01:38 +00006959 outs() << "(" << DisSegName << "," << DisSectName << ") section\n";
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00006960
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006961 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
Owen Andersond9243c42011-10-17 21:37:35 +00006962 StringRef SectName;
Kevin Enderby95df54c2015-02-04 01:01:38 +00006963 if (Sections[SectIdx].getName(SectName) || SectName != DisSectName)
6964 continue;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006965
Rafael Espindolaa9f810b2012-12-21 03:47:03 +00006966 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
Ahmed Bougachaaa790682013-05-24 01:07:04 +00006967
Rafael Espindolab0f76a42013-04-05 15:15:22 +00006968 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
Kevin Enderby95df54c2015-02-04 01:01:38 +00006969 if (SegmentName != DisSegName)
Rafael Espindolaa9f810b2012-12-21 03:47:03 +00006970 continue;
6971
Rafael Espindola7fc5b872014-11-12 02:04:27 +00006972 StringRef BytesStr;
6973 Sections[SectIdx].getContents(BytesStr);
Aaron Ballman106fd7b2014-11-12 14:01:17 +00006974 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
6975 BytesStr.size());
Rafael Espindola80291272014-10-08 15:28:58 +00006976 uint64_t SectAddress = Sections[SectIdx].getAddress();
Rafael Espindolabd604f22014-11-07 00:52:15 +00006977
Benjamin Kramer43a772e2011-09-19 17:56:04 +00006978 bool symbolTableWorked = false;
6979
Kevin Enderbybf246f52014-09-24 23:08:22 +00006980 // Create a map of symbol addresses to symbol names for use by
6981 // the SymbolizerSymbolLookUp() routine.
6982 SymbolAddressMap AddrMap;
Kevin Enderby6a221752015-03-17 17:10:57 +00006983 bool DisSymNameFound = false;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006984 for (const SymbolRef &Symbol : MachOOF->symbols()) {
Kevin Enderby7bd8d992016-05-02 20:28:12 +00006985 Expected<SymbolRef::Type> STOrErr = Symbol.getType();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00006986 if (!STOrErr)
6987 report_error(MachOOF->getFileName(), STOrErr.takeError());
Kevin Enderby5afbc1c2016-03-23 20:27:00 +00006988 SymbolRef::Type ST = *STOrErr;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006989 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
6990 ST == SymbolRef::ST_Other) {
Rafael Espindoladea00162015-07-03 17:44:18 +00006991 uint64_t Address = Symbol.getValue();
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00006992 Expected<StringRef> SymNameOrErr = Symbol.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00006993 if (!SymNameOrErr)
6994 report_error(MachOOF->getFileName(), SymNameOrErr.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00006995 StringRef SymName = *SymNameOrErr;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006996 AddrMap[Address] = SymName;
Kevin Enderby6a221752015-03-17 17:10:57 +00006997 if (!DisSymName.empty() && DisSymName == SymName)
6998 DisSymNameFound = true;
Kevin Enderbybf246f52014-09-24 23:08:22 +00006999 }
7000 }
David Blaikie33dd45d02015-03-23 18:39:02 +00007001 if (!DisSymName.empty() && !DisSymNameFound) {
Kevin Enderby6a221752015-03-17 17:10:57 +00007002 outs() << "Can't find -dis-symname: " << DisSymName << "\n";
7003 return;
7004 }
Kevin Enderby98c9acc2014-09-16 18:00:57 +00007005 // Set up the block of info used by the Symbolizer call backs.
Kevin Enderby8e29ec92015-03-17 22:26:11 +00007006 SymbolizerInfo.verbose = !NoSymbolicOperands;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00007007 SymbolizerInfo.O = MachOOF;
7008 SymbolizerInfo.S = Sections[SectIdx];
Kevin Enderbybf246f52014-09-24 23:08:22 +00007009 SymbolizerInfo.AddrMap = &AddrMap;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00007010 SymbolizerInfo.Sections = &Sections;
Kevin Enderby930fdc72014-11-06 19:00:13 +00007011 // Same for the ThumbSymbolizer
Kevin Enderby8e29ec92015-03-17 22:26:11 +00007012 ThumbSymbolizerInfo.verbose = !NoSymbolicOperands;
Kevin Enderby930fdc72014-11-06 19:00:13 +00007013 ThumbSymbolizerInfo.O = MachOOF;
7014 ThumbSymbolizerInfo.S = Sections[SectIdx];
7015 ThumbSymbolizerInfo.AddrMap = &AddrMap;
7016 ThumbSymbolizerInfo.Sections = &Sections;
Kevin Enderby98c9acc2014-09-16 18:00:57 +00007017
Kevin Enderby4b627be2016-04-28 20:14:13 +00007018 unsigned int Arch = MachOOF->getArch();
7019
Tim Northoverf203ab52016-07-14 22:13:32 +00007020 // Skip all symbols if this is a stubs file.
7021 if (Bytes.size() == 0)
7022 return;
7023
Kevin Enderbyc138da32017-02-06 18:43:18 +00007024 // If the section has symbols but no symbol at the start of the section
7025 // these are used to make sure the bytes before the first symbol are
7026 // disassembled.
7027 bool FirstSymbol = true;
7028 bool FirstSymbolAtSectionStart = true;
7029
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00007030 // Disassemble symbol by symbol.
Benjamin Kramer43a772e2011-09-19 17:56:04 +00007031 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00007032 Expected<StringRef> SymNameOrErr = Symbols[SymIdx].getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00007033 if (!SymNameOrErr)
7034 report_error(MachOOF->getFileName(), SymNameOrErr.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00007035 StringRef SymName = *SymNameOrErr;
Owen Andersond9243c42011-10-17 21:37:35 +00007036
Kevin Enderby7bd8d992016-05-02 20:28:12 +00007037 Expected<SymbolRef::Type> STOrErr = Symbols[SymIdx].getType();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00007038 if (!STOrErr)
7039 report_error(MachOOF->getFileName(), STOrErr.takeError());
Kevin Enderby5afbc1c2016-03-23 20:27:00 +00007040 SymbolRef::Type ST = *STOrErr;
Kuba Breckade833222015-11-12 09:40:29 +00007041 if (ST != SymbolRef::ST_Function && ST != SymbolRef::ST_Data)
Owen Andersond9243c42011-10-17 21:37:35 +00007042 continue;
7043
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00007044 // Make sure the symbol is defined in this section.
Rafael Espindola80291272014-10-08 15:28:58 +00007045 bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]);
Kevin Enderbyd8a6e832016-06-15 21:14:01 +00007046 if (!containsSym) {
7047 if (!DisSymName.empty() && DisSymName == SymName) {
7048 outs() << "-dis-symname: " << DisSymName << " not in the section\n";
7049 return;
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +00007050 }
Kevin Enderbyd8a6e832016-06-15 21:14:01 +00007051 continue;
7052 }
7053 // The __mh_execute_header is special and we need to deal with that fact
7054 // this symbol is before the start of the (__TEXT,__text) section and at the
7055 // address of the start of the __TEXT segment. This is because this symbol
7056 // is an N_SECT symbol in the (__TEXT,__text) but its address is before the
7057 // start of the section in a standard MH_EXECUTE filetype.
7058 if (!DisSymName.empty() && DisSymName == "__mh_execute_header") {
7059 outs() << "-dis-symname: __mh_execute_header not in any section\n";
7060 return;
7061 }
Tim Northoverfbefee32016-07-14 23:13:03 +00007062 // When this code is trying to disassemble a symbol at a time and in the
7063 // case there is only the __mh_execute_header symbol left as in a stripped
7064 // executable, we need to deal with this by ignoring this symbol so the
7065 // whole section is disassembled and this symbol is then not displayed.
7066 if (SymName == "__mh_execute_header" || SymName == "__mh_dylib_header" ||
7067 SymName == "__mh_bundle_header" || SymName == "__mh_object_header" ||
7068 SymName == "__mh_preload_header" || SymName == "__mh_dylinker_header")
Benjamin Kramer43a772e2011-09-19 17:56:04 +00007069 continue;
7070
Kevin Enderby6a221752015-03-17 17:10:57 +00007071 // If we are only disassembling one symbol see if this is that symbol.
7072 if (!DisSymName.empty() && DisSymName != SymName)
7073 continue;
7074
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00007075 // Start at the address of the symbol relative to the section's address.
Tim Northoverf203ab52016-07-14 22:13:32 +00007076 uint64_t SectSize = Sections[SectIdx].getSize();
Rafael Espindoladea00162015-07-03 17:44:18 +00007077 uint64_t Start = Symbols[SymIdx].getValue();
Rafael Espindola80291272014-10-08 15:28:58 +00007078 uint64_t SectionAddress = Sections[SectIdx].getAddress();
Cameron Zwarich54478a52012-02-03 05:42:17 +00007079 Start -= SectionAddress;
Owen Andersond9243c42011-10-17 21:37:35 +00007080
Tim Northoverf203ab52016-07-14 22:13:32 +00007081 if (Start > SectSize) {
7082 outs() << "section data ends, " << SymName
7083 << " lies outside valid range\n";
7084 return;
7085 }
7086
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +00007087 // Stop disassembling either at the beginning of the next symbol or at
7088 // the end of the section.
Kevin Enderbyedd58722012-05-15 18:57:14 +00007089 bool containsNextSym = false;
Owen Andersond9243c42011-10-17 21:37:35 +00007090 uint64_t NextSym = 0;
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007091 uint64_t NextSymIdx = SymIdx + 1;
Owen Andersond9243c42011-10-17 21:37:35 +00007092 while (Symbols.size() > NextSymIdx) {
Kevin Enderby7bd8d992016-05-02 20:28:12 +00007093 Expected<SymbolRef::Type> STOrErr = Symbols[NextSymIdx].getType();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00007094 if (!STOrErr)
7095 report_error(MachOOF->getFileName(), STOrErr.takeError());
Kevin Enderby5afbc1c2016-03-23 20:27:00 +00007096 SymbolRef::Type NextSymType = *STOrErr;
Owen Andersond9243c42011-10-17 21:37:35 +00007097 if (NextSymType == SymbolRef::ST_Function) {
Rafael Espindola80291272014-10-08 15:28:58 +00007098 containsNextSym =
7099 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
Rafael Espindoladea00162015-07-03 17:44:18 +00007100 NextSym = Symbols[NextSymIdx].getValue();
Cameron Zwarich54478a52012-02-03 05:42:17 +00007101 NextSym -= SectionAddress;
Owen Andersond9243c42011-10-17 21:37:35 +00007102 break;
7103 }
7104 ++NextSymIdx;
7105 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00007106
Tim Northoverf203ab52016-07-14 22:13:32 +00007107 uint64_t End = containsNextSym ? std::min(NextSym, SectSize) : SectSize;
Owen Andersond9243c42011-10-17 21:37:35 +00007108 uint64_t Size;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00007109
7110 symbolTableWorked = true;
Rafael Espindolabd604f22014-11-07 00:52:15 +00007111
Kevin Enderbyec5ca032014-08-18 20:21:02 +00007112 DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
Tim Northover09ca33e2016-04-22 23:23:31 +00007113 bool IsThumb = MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb;
7114
7115 // We only need the dedicated Thumb target if there's a real choice
7116 // (i.e. we're not targeting M-class) and the function is Thumb.
7117 bool UseThumbTarget = IsThumb && ThumbTarget;
Kevin Enderbyec5ca032014-08-18 20:21:02 +00007118
Kevin Enderbyc138da32017-02-06 18:43:18 +00007119 // If we are not specifying a symbol to start disassembly with and this
7120 // is the first symbol in the section but not at the start of the section
7121 // then move the disassembly index to the start of the section and
7122 // don't print the symbol name just yet. This is so the bytes before the
7123 // first symbol are disassembled.
7124 uint64_t SymbolStart = Start;
7125 if (DisSymName.empty() && FirstSymbol && Start != 0) {
7126 FirstSymbolAtSectionStart = false;
7127 Start = 0;
7128 }
7129 else
7130 outs() << SymName << ":\n";
7131
Ahmed Bougachaaa790682013-05-24 01:07:04 +00007132 DILineInfo lastLine;
7133 for (uint64_t Index = Start; Index < End; Index += Size) {
7134 MCInst Inst;
Owen Andersond9243c42011-10-17 21:37:35 +00007135
Kevin Enderbyc138da32017-02-06 18:43:18 +00007136 // If this is the first symbol in the section and it was not at the
7137 // start of the section, see if we are at its Index now and if so print
7138 // the symbol name.
7139 if (FirstSymbol && !FirstSymbolAtSectionStart && Index == SymbolStart)
7140 outs() << SymName << ":\n";
7141
Kevin Enderbybf246f52014-09-24 23:08:22 +00007142 uint64_t PC = SectAddress + Index;
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00007143 if (!NoLeadingAddr) {
7144 if (FullLeadingAddr) {
7145 if (MachOOF->is64Bit())
7146 outs() << format("%016" PRIx64, PC);
7147 else
7148 outs() << format("%08" PRIx64, PC);
7149 } else {
7150 outs() << format("%8" PRIx64 ":", PC);
7151 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00007152 }
Kevin Enderby4b627be2016-04-28 20:14:13 +00007153 if (!NoShowRawInsn || Arch == Triple::arm)
Kevin Enderbybf246f52014-09-24 23:08:22 +00007154 outs() << "\t";
Kevin Enderby273ae012013-06-06 17:20:50 +00007155
7156 // Check the data in code table here to see if this is data not an
7157 // instruction to be disassembled.
7158 DiceTable Dice;
Kevin Enderbybf246f52014-09-24 23:08:22 +00007159 Dice.push_back(std::make_pair(PC, DiceRef()));
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007160 dice_table_iterator DTI =
7161 std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(),
7162 compareDiceTableEntries);
7163 if (DTI != Dices.end()) {
Kevin Enderby273ae012013-06-06 17:20:50 +00007164 uint16_t Length;
7165 DTI->second.getLength(Length);
Kevin Enderby273ae012013-06-06 17:20:50 +00007166 uint16_t Kind;
7167 DTI->second.getKind(Kind);
Colin LeMahieufc32b1b2015-03-18 19:27:31 +00007168 Size = DumpDataInCode(Bytes.data() + Index, Length, Kind);
Kevin Enderby930fdc72014-11-06 19:00:13 +00007169 if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) &&
7170 (PC == (DTI->first + Length - 1)) && (Length & 1))
7171 Size++;
Kevin Enderby273ae012013-06-06 17:20:50 +00007172 continue;
7173 }
7174
Kevin Enderbybf246f52014-09-24 23:08:22 +00007175 SmallVector<char, 64> AnnotationsBytes;
7176 raw_svector_ostream Annotations(AnnotationsBytes);
7177
Kevin Enderbyec5ca032014-08-18 20:21:02 +00007178 bool gotInst;
Tim Northover09ca33e2016-04-22 23:23:31 +00007179 if (UseThumbTarget)
Rafael Espindola7fc5b872014-11-12 02:04:27 +00007180 gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
Kevin Enderby6f326ce2014-10-23 19:37:31 +00007181 PC, DebugOut, Annotations);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00007182 else
Rafael Espindola7fc5b872014-11-12 02:04:27 +00007183 gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC,
Kevin Enderbybf246f52014-09-24 23:08:22 +00007184 DebugOut, Annotations);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00007185 if (gotInst) {
Kevin Enderby4b627be2016-04-28 20:14:13 +00007186 if (!NoShowRawInsn || Arch == Triple::arm) {
Craig Topper0013be12015-09-21 05:32:41 +00007187 dumpBytes(makeArrayRef(Bytes.data() + Index, Size), outs());
Kevin Enderbybf246f52014-09-24 23:08:22 +00007188 }
7189 formatted_raw_ostream FormattedOS(outs());
Kevin Enderbybf246f52014-09-24 23:08:22 +00007190 StringRef AnnotationsStr = Annotations.str();
Tim Northover09ca33e2016-04-22 23:23:31 +00007191 if (UseThumbTarget)
Akira Hatanakab46d0232015-03-27 20:36:02 +00007192 ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr, *ThumbSTI);
Kevin Enderbyec5ca032014-08-18 20:21:02 +00007193 else
Akira Hatanaka1d079942015-03-28 20:44:05 +00007194 IP->printInst(&Inst, FormattedOS, AnnotationsStr, *STI);
Kevin Enderbybf246f52014-09-24 23:08:22 +00007195 emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo);
Owen Andersond9243c42011-10-17 21:37:35 +00007196
Ahmed Bougachaaa790682013-05-24 01:07:04 +00007197 // Print debug info.
7198 if (diContext) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007199 DILineInfo dli = diContext->getLineInfoForAddress(PC);
Ahmed Bougachaaa790682013-05-24 01:07:04 +00007200 // Print valid line info if it changed.
Alexey Samsonovd0109992014-04-18 21:36:39 +00007201 if (dli != lastLine && dli.Line != 0)
7202 outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
7203 << dli.Column;
Ahmed Bougachaaa790682013-05-24 01:07:04 +00007204 lastLine = dli;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00007205 }
Ahmed Bougachaaa790682013-05-24 01:07:04 +00007206 outs() << "\n";
7207 } else {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00007208 unsigned int Arch = MachOOF->getArch();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007209 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00007210 outs() << format("\t.byte 0x%02x #bad opcode\n",
7211 *(Bytes.data() + Index) & 0xff);
7212 Size = 1; // skip exactly one illegible byte and move on.
Tim Northover09ca33e2016-04-22 23:23:31 +00007213 } else if (Arch == Triple::aarch64 ||
7214 (Arch == Triple::arm && !IsThumb)) {
Kevin Enderbyae3c1262014-11-14 21:52:18 +00007215 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
7216 (*(Bytes.data() + Index + 1) & 0xff) << 8 |
7217 (*(Bytes.data() + Index + 2) & 0xff) << 16 |
7218 (*(Bytes.data() + Index + 3) & 0xff) << 24;
7219 outs() << format("\t.long\t0x%08x\n", opcode);
7220 Size = 4;
Tim Northover09ca33e2016-04-22 23:23:31 +00007221 } else if (Arch == Triple::arm) {
7222 assert(IsThumb && "ARM mode should have been dealt with above");
7223 uint32_t opcode = (*(Bytes.data() + Index) & 0xff) |
7224 (*(Bytes.data() + Index + 1) & 0xff) << 8;
7225 outs() << format("\t.short\t0x%04x\n", opcode);
7226 Size = 2;
7227 } else{
Kevin Enderby6f326ce2014-10-23 19:37:31 +00007228 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
7229 if (Size == 0)
7230 Size = 1; // skip illegible bytes
7231 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00007232 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00007233 }
Kevin Enderbyc138da32017-02-06 18:43:18 +00007234 // Now that we are done disassembled the first symbol set the bool that
7235 // were doing this to false.
7236 FirstSymbol = false;
Benjamin Kramer43a772e2011-09-19 17:56:04 +00007237 }
Ahmed Bougachaaa790682013-05-24 01:07:04 +00007238 if (!symbolTableWorked) {
Rafael Espindola80291272014-10-08 15:28:58 +00007239 // Reading the symbol table didn't work, disassemble the whole section.
7240 uint64_t SectAddress = Sections[SectIdx].getAddress();
7241 uint64_t SectSize = Sections[SectIdx].getSize();
Kevin Enderbybadd1002012-05-18 00:13:56 +00007242 uint64_t InstSize;
7243 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
Bill Wendling4e68e062012-07-19 00:17:40 +00007244 MCInst Inst;
Kevin Enderbybadd1002012-05-18 00:13:56 +00007245
Kevin Enderbybf246f52014-09-24 23:08:22 +00007246 uint64_t PC = SectAddress + Index;
Kevin Enderby02d3a372017-01-31 18:09:10 +00007247 SmallVector<char, 64> AnnotationsBytes;
7248 raw_svector_ostream Annotations(AnnotationsBytes);
Rafael Espindola7fc5b872014-11-12 02:04:27 +00007249 if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC,
Kevin Enderby02d3a372017-01-31 18:09:10 +00007250 DebugOut, Annotations)) {
Kevin Enderbyab5e6c92015-03-17 21:07:39 +00007251 if (!NoLeadingAddr) {
7252 if (FullLeadingAddr) {
7253 if (MachOOF->is64Bit())
7254 outs() << format("%016" PRIx64, PC);
7255 else
7256 outs() << format("%08" PRIx64, PC);
7257 } else {
7258 outs() << format("%8" PRIx64 ":", PC);
7259 }
Kevin Enderbybf246f52014-09-24 23:08:22 +00007260 }
Kevin Enderby4b627be2016-04-28 20:14:13 +00007261 if (!NoShowRawInsn || Arch == Triple::arm) {
Kevin Enderbybf246f52014-09-24 23:08:22 +00007262 outs() << "\t";
Craig Topper0013be12015-09-21 05:32:41 +00007263 dumpBytes(makeArrayRef(Bytes.data() + Index, InstSize), outs());
Kevin Enderbybf246f52014-09-24 23:08:22 +00007264 }
Kevin Enderby02d3a372017-01-31 18:09:10 +00007265 StringRef AnnotationsStr = Annotations.str();
7266 IP->printInst(&Inst, outs(), AnnotationsStr, *STI);
Bill Wendling4e68e062012-07-19 00:17:40 +00007267 outs() << "\n";
7268 } else {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00007269 unsigned int Arch = MachOOF->getArch();
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007270 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
Kevin Enderby6f326ce2014-10-23 19:37:31 +00007271 outs() << format("\t.byte 0x%02x #bad opcode\n",
7272 *(Bytes.data() + Index) & 0xff);
7273 InstSize = 1; // skip exactly one illegible byte and move on.
7274 } else {
7275 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
7276 if (InstSize == 0)
7277 InstSize = 1; // skip illegible bytes
7278 }
Bill Wendling4e68e062012-07-19 00:17:40 +00007279 }
Kevin Enderbybadd1002012-05-18 00:13:56 +00007280 }
7281 }
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +00007282 // The TripleName's need to be reset if we are called again for a different
7283 // archtecture.
7284 TripleName = "";
7285 ThumbTripleName = "";
7286
Kevin Enderby04bf6932014-10-28 23:39:46 +00007287 if (SymbolizerInfo.demangled_name != nullptr)
7288 free(SymbolizerInfo.demangled_name);
Kevin Enderby930fdc72014-11-06 19:00:13 +00007289 if (ThumbSymbolizerInfo.demangled_name != nullptr)
7290 free(ThumbSymbolizerInfo.demangled_name);
Benjamin Kramer43a772e2011-09-19 17:56:04 +00007291 }
7292}
Tim Northover4bd286a2014-08-01 13:07:19 +00007293
Tim Northover39c70bb2014-08-12 11:52:59 +00007294//===----------------------------------------------------------------------===//
7295// __compact_unwind section dumping
7296//===----------------------------------------------------------------------===//
7297
Tim Northover4bd286a2014-08-01 13:07:19 +00007298namespace {
Tim Northover39c70bb2014-08-12 11:52:59 +00007299
Tim Northover3a4e1c72018-01-23 13:51:57 +00007300template <typename T>
7301static uint64_t read(StringRef Contents, ptrdiff_t Offset) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007302 using llvm::support::little;
7303 using llvm::support::unaligned;
Tim Northover39c70bb2014-08-12 11:52:59 +00007304
Tim Northover3a4e1c72018-01-23 13:51:57 +00007305 if (Offset + sizeof(T) > Contents.size()) {
7306 outs() << "warning: attempt to read past end of buffer\n";
7307 return T();
7308 }
7309
7310 uint64_t Val =
7311 support::endian::read<T, little, unaligned>(Contents.data() + Offset);
7312 return Val;
7313}
7314
7315template <typename T>
7316static uint64_t readNext(StringRef Contents, ptrdiff_t &Offset) {
7317 T Val = read<T>(Contents, Offset);
7318 Offset += sizeof(T);
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007319 return Val;
7320}
Tim Northover39c70bb2014-08-12 11:52:59 +00007321
Tim Northover4bd286a2014-08-01 13:07:19 +00007322struct CompactUnwindEntry {
7323 uint32_t OffsetInSection;
7324
7325 uint64_t FunctionAddr;
7326 uint32_t Length;
7327 uint32_t CompactEncoding;
7328 uint64_t PersonalityAddr;
7329 uint64_t LSDAAddr;
7330
7331 RelocationRef FunctionReloc;
7332 RelocationRef PersonalityReloc;
7333 RelocationRef LSDAReloc;
7334
7335 CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64)
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007336 : OffsetInSection(Offset) {
Tim Northover4bd286a2014-08-01 13:07:19 +00007337 if (Is64)
Tim Northover3a4e1c72018-01-23 13:51:57 +00007338 read<uint64_t>(Contents, Offset);
Tim Northover4bd286a2014-08-01 13:07:19 +00007339 else
Tim Northover3a4e1c72018-01-23 13:51:57 +00007340 read<uint32_t>(Contents, Offset);
Tim Northover4bd286a2014-08-01 13:07:19 +00007341 }
7342
7343private:
Tim Northover3a4e1c72018-01-23 13:51:57 +00007344 template <typename UIntPtr> void read(StringRef Contents, ptrdiff_t Offset) {
7345 FunctionAddr = readNext<UIntPtr>(Contents, Offset);
7346 Length = readNext<uint32_t>(Contents, Offset);
7347 CompactEncoding = readNext<uint32_t>(Contents, Offset);
7348 PersonalityAddr = readNext<UIntPtr>(Contents, Offset);
7349 LSDAAddr = readNext<UIntPtr>(Contents, Offset);
Tim Northover4bd286a2014-08-01 13:07:19 +00007350 }
7351};
7352}
7353
7354/// Given a relocation from __compact_unwind, consisting of the RelocationRef
7355/// and data being relocated, determine the best base Name and Addend to use for
7356/// display purposes.
7357///
7358/// 1. An Extern relocation will directly reference a symbol (and the data is
7359/// then already an addend), so use that.
7360/// 2. Otherwise the data is an offset in the object file's layout; try to find
7361// a symbol before it in the same section, and use the offset from there.
7362/// 3. Finally, if all that fails, fall back to an offset from the start of the
7363/// referenced section.
7364static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
7365 std::map<uint64_t, SymbolRef> &Symbols,
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007366 const RelocationRef &Reloc, uint64_t Addr,
Tim Northover4bd286a2014-08-01 13:07:19 +00007367 StringRef &Name, uint64_t &Addend) {
7368 if (Reloc.getSymbol() != Obj->symbol_end()) {
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00007369 Expected<StringRef> NameOrErr = Reloc.getSymbol()->getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00007370 if (!NameOrErr)
7371 report_error(Obj->getFileName(), NameOrErr.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00007372 Name = *NameOrErr;
Tim Northover4bd286a2014-08-01 13:07:19 +00007373 Addend = Addr;
7374 return;
7375 }
7376
7377 auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
Keno Fischerc780e8e2015-05-21 21:24:32 +00007378 SectionRef RelocSection = Obj->getAnyRelocationSection(RE);
Tim Northover4bd286a2014-08-01 13:07:19 +00007379
Rafael Espindola80291272014-10-08 15:28:58 +00007380 uint64_t SectionAddr = RelocSection.getAddress();
Tim Northover4bd286a2014-08-01 13:07:19 +00007381
7382 auto Sym = Symbols.upper_bound(Addr);
7383 if (Sym == Symbols.begin()) {
7384 // The first symbol in the object is after this reference, the best we can
7385 // do is section-relative notation.
7386 RelocSection.getName(Name);
7387 Addend = Addr - SectionAddr;
7388 return;
7389 }
7390
7391 // Go back one so that SymbolAddress <= Addr.
7392 --Sym;
7393
Kevin Enderby7bd8d992016-05-02 20:28:12 +00007394 auto SectOrErr = Sym->second.getSection();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00007395 if (!SectOrErr)
7396 report_error(Obj->getFileName(), SectOrErr.takeError());
Kevin Enderby7bd8d992016-05-02 20:28:12 +00007397 section_iterator SymSection = *SectOrErr;
Tim Northover4bd286a2014-08-01 13:07:19 +00007398 if (RelocSection == *SymSection) {
7399 // There's a valid symbol in the same section before this reference.
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00007400 Expected<StringRef> NameOrErr = Sym->second.getName();
Kevin Enderby844c4ac2016-11-15 23:07:41 +00007401 if (!NameOrErr)
7402 report_error(Obj->getFileName(), NameOrErr.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00007403 Name = *NameOrErr;
Tim Northover4bd286a2014-08-01 13:07:19 +00007404 Addend = Addr - Sym->first;
7405 return;
7406 }
7407
7408 // There is a symbol before this reference, but it's in a different
7409 // section. Probably not helpful to mention it, so use the section name.
7410 RelocSection.getName(Name);
7411 Addend = Addr - SectionAddr;
7412}
7413
7414static void printUnwindRelocDest(const MachOObjectFile *Obj,
7415 std::map<uint64_t, SymbolRef> &Symbols,
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007416 const RelocationRef &Reloc, uint64_t Addr) {
Tim Northover4bd286a2014-08-01 13:07:19 +00007417 StringRef Name;
7418 uint64_t Addend;
7419
Rafael Espindola854038e2015-06-26 14:51:16 +00007420 if (!Reloc.getObject())
Tim Northover0b0add52014-09-09 10:45:06 +00007421 return;
7422
Tim Northover4bd286a2014-08-01 13:07:19 +00007423 findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend);
7424
7425 outs() << Name;
7426 if (Addend)
Tim Northover63a25622014-08-11 09:14:06 +00007427 outs() << " + " << format("0x%" PRIx64, Addend);
Tim Northover4bd286a2014-08-01 13:07:19 +00007428}
7429
7430static void
7431printMachOCompactUnwindSection(const MachOObjectFile *Obj,
7432 std::map<uint64_t, SymbolRef> &Symbols,
7433 const SectionRef &CompactUnwind) {
7434
Tim Northoverbf55f7e2016-11-15 20:26:01 +00007435 if (!Obj->isLittleEndian()) {
7436 outs() << "Skipping big-endian __compact_unwind section\n";
7437 return;
7438 }
Tim Northover4bd286a2014-08-01 13:07:19 +00007439
7440 bool Is64 = Obj->is64Bit();
7441 uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
7442 uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
7443
7444 StringRef Contents;
7445 CompactUnwind.getContents(Contents);
7446
7447 SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
7448
7449 // First populate the initial raw offsets, encodings and so on from the entry.
7450 for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) {
Tim Northover3a4e1c72018-01-23 13:51:57 +00007451 CompactUnwindEntry Entry(Contents, Offset, Is64);
Tim Northover4bd286a2014-08-01 13:07:19 +00007452 CompactUnwinds.push_back(Entry);
7453 }
7454
7455 // Next we need to look at the relocations to find out what objects are
7456 // actually being referred to.
7457 for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +00007458 uint64_t RelocAddress = Reloc.getOffset();
Tim Northover4bd286a2014-08-01 13:07:19 +00007459
7460 uint32_t EntryIdx = RelocAddress / EntrySize;
7461 uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
7462 CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx];
7463
7464 if (OffsetInEntry == 0)
7465 Entry.FunctionReloc = Reloc;
7466 else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t))
7467 Entry.PersonalityReloc = Reloc;
7468 else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t))
7469 Entry.LSDAReloc = Reloc;
Tim Northoverbf55f7e2016-11-15 20:26:01 +00007470 else {
7471 outs() << "Invalid relocation in __compact_unwind section\n";
7472 return;
7473 }
Tim Northover4bd286a2014-08-01 13:07:19 +00007474 }
7475
7476 // Finally, we're ready to print the data we've gathered.
7477 outs() << "Contents of __compact_unwind section:\n";
7478 for (auto &Entry : CompactUnwinds) {
Tim Northover06af2602014-08-08 12:08:51 +00007479 outs() << " Entry at offset "
7480 << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n";
Tim Northover4bd286a2014-08-01 13:07:19 +00007481
7482 // 1. Start of the region this entry applies to.
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007483 outs() << " start: " << format("0x%" PRIx64,
7484 Entry.FunctionAddr) << ' ';
7485 printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr);
Tim Northover4bd286a2014-08-01 13:07:19 +00007486 outs() << '\n';
7487
7488 // 2. Length of the region this entry applies to.
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007489 outs() << " length: " << format("0x%" PRIx32, Entry.Length)
7490 << '\n';
Tim Northover4bd286a2014-08-01 13:07:19 +00007491 // 3. The 32-bit compact encoding.
7492 outs() << " compact encoding: "
Tim Northoverb911bf82014-08-08 12:00:09 +00007493 << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n';
Tim Northover4bd286a2014-08-01 13:07:19 +00007494
7495 // 4. The personality function, if present.
Rafael Espindola854038e2015-06-26 14:51:16 +00007496 if (Entry.PersonalityReloc.getObject()) {
Tim Northover4bd286a2014-08-01 13:07:19 +00007497 outs() << " personality function: "
Tim Northoverb911bf82014-08-08 12:00:09 +00007498 << format("0x%" PRIx64, Entry.PersonalityAddr) << ' ';
Tim Northover4bd286a2014-08-01 13:07:19 +00007499 printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc,
7500 Entry.PersonalityAddr);
7501 outs() << '\n';
7502 }
7503
7504 // 5. This entry's language-specific data area.
Rafael Espindola854038e2015-06-26 14:51:16 +00007505 if (Entry.LSDAReloc.getObject()) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007506 outs() << " LSDA: " << format("0x%" PRIx64,
7507 Entry.LSDAAddr) << ' ';
Tim Northover4bd286a2014-08-01 13:07:19 +00007508 printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr);
7509 outs() << '\n';
7510 }
7511 }
7512}
7513
Tim Northover39c70bb2014-08-12 11:52:59 +00007514//===----------------------------------------------------------------------===//
7515// __unwind_info section dumping
7516//===----------------------------------------------------------------------===//
7517
Tim Northover3a4e1c72018-01-23 13:51:57 +00007518static void printRegularSecondLevelUnwindPage(StringRef PageData) {
7519 ptrdiff_t Pos = 0;
7520 uint32_t Kind = readNext<uint32_t>(PageData, Pos);
Tim Northover39c70bb2014-08-12 11:52:59 +00007521 (void)Kind;
7522 assert(Kind == 2 && "kind for a regular 2nd level index should be 2");
7523
Tim Northover3a4e1c72018-01-23 13:51:57 +00007524 uint16_t EntriesStart = readNext<uint16_t>(PageData, Pos);
7525 uint16_t NumEntries = readNext<uint16_t>(PageData, Pos);
Tim Northover39c70bb2014-08-12 11:52:59 +00007526
Tim Northover3a4e1c72018-01-23 13:51:57 +00007527 Pos = EntriesStart;
Tim Northover39c70bb2014-08-12 11:52:59 +00007528 for (unsigned i = 0; i < NumEntries; ++i) {
Tim Northover3a4e1c72018-01-23 13:51:57 +00007529 uint32_t FunctionOffset = readNext<uint32_t>(PageData, Pos);
7530 uint32_t Encoding = readNext<uint32_t>(PageData, Pos);
Tim Northover39c70bb2014-08-12 11:52:59 +00007531
7532 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007533 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
7534 << ", "
7535 << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00007536 }
7537}
7538
7539static void printCompressedSecondLevelUnwindPage(
Tim Northover3a4e1c72018-01-23 13:51:57 +00007540 StringRef PageData, uint32_t FunctionBase,
Tim Northover39c70bb2014-08-12 11:52:59 +00007541 const SmallVectorImpl<uint32_t> &CommonEncodings) {
Tim Northover3a4e1c72018-01-23 13:51:57 +00007542 ptrdiff_t Pos = 0;
7543 uint32_t Kind = readNext<uint32_t>(PageData, Pos);
Tim Northover39c70bb2014-08-12 11:52:59 +00007544 (void)Kind;
7545 assert(Kind == 3 && "kind for a compressed 2nd level index should be 3");
7546
Tim Northover3a4e1c72018-01-23 13:51:57 +00007547 uint16_t EntriesStart = readNext<uint16_t>(PageData, Pos);
7548 uint16_t NumEntries = readNext<uint16_t>(PageData, Pos);
Tim Northover39c70bb2014-08-12 11:52:59 +00007549
Tim Northover3a4e1c72018-01-23 13:51:57 +00007550 uint16_t EncodingsStart = readNext<uint16_t>(PageData, Pos);
7551 readNext<uint16_t>(PageData, Pos);
7552 StringRef PageEncodings = PageData.substr(EncodingsStart, StringRef::npos);
Tim Northover39c70bb2014-08-12 11:52:59 +00007553
Tim Northover3a4e1c72018-01-23 13:51:57 +00007554 Pos = EntriesStart;
Tim Northover39c70bb2014-08-12 11:52:59 +00007555 for (unsigned i = 0; i < NumEntries; ++i) {
Tim Northover3a4e1c72018-01-23 13:51:57 +00007556 uint32_t Entry = readNext<uint32_t>(PageData, Pos);
Tim Northover39c70bb2014-08-12 11:52:59 +00007557 uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff);
7558 uint32_t EncodingIdx = Entry >> 24;
7559
7560 uint32_t Encoding;
7561 if (EncodingIdx < CommonEncodings.size())
7562 Encoding = CommonEncodings[EncodingIdx];
7563 else
Tim Northover3a4e1c72018-01-23 13:51:57 +00007564 Encoding = read<uint32_t>(PageEncodings,
7565 sizeof(uint32_t) *
7566 (EncodingIdx - CommonEncodings.size()));
Tim Northover39c70bb2014-08-12 11:52:59 +00007567
7568 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007569 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
7570 << ", "
7571 << "encoding[" << EncodingIdx
7572 << "]=" << format("0x%08" PRIx32, Encoding) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00007573 }
7574}
7575
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007576static void printMachOUnwindInfoSection(const MachOObjectFile *Obj,
7577 std::map<uint64_t, SymbolRef> &Symbols,
7578 const SectionRef &UnwindInfo) {
Tim Northover39c70bb2014-08-12 11:52:59 +00007579
Tim Northoverbf55f7e2016-11-15 20:26:01 +00007580 if (!Obj->isLittleEndian()) {
7581 outs() << "Skipping big-endian __unwind_info section\n";
7582 return;
7583 }
Tim Northover39c70bb2014-08-12 11:52:59 +00007584
7585 outs() << "Contents of __unwind_info section:\n";
7586
7587 StringRef Contents;
7588 UnwindInfo.getContents(Contents);
Tim Northover3a4e1c72018-01-23 13:51:57 +00007589 ptrdiff_t Pos = 0;
Tim Northover39c70bb2014-08-12 11:52:59 +00007590
7591 //===----------------------------------
7592 // Section header
7593 //===----------------------------------
7594
Tim Northover3a4e1c72018-01-23 13:51:57 +00007595 uint32_t Version = readNext<uint32_t>(Contents, Pos);
Tim Northover39c70bb2014-08-12 11:52:59 +00007596 outs() << " Version: "
7597 << format("0x%" PRIx32, Version) << '\n';
Tim Northoverbf55f7e2016-11-15 20:26:01 +00007598 if (Version != 1) {
7599 outs() << " Skipping section with unknown version\n";
7600 return;
7601 }
Tim Northover39c70bb2014-08-12 11:52:59 +00007602
Tim Northover3a4e1c72018-01-23 13:51:57 +00007603 uint32_t CommonEncodingsStart = readNext<uint32_t>(Contents, Pos);
Tim Northover39c70bb2014-08-12 11:52:59 +00007604 outs() << " Common encodings array section offset: "
7605 << format("0x%" PRIx32, CommonEncodingsStart) << '\n';
Tim Northover3a4e1c72018-01-23 13:51:57 +00007606 uint32_t NumCommonEncodings = readNext<uint32_t>(Contents, Pos);
Tim Northover39c70bb2014-08-12 11:52:59 +00007607 outs() << " Number of common encodings in array: "
7608 << format("0x%" PRIx32, NumCommonEncodings) << '\n';
7609
Tim Northover3a4e1c72018-01-23 13:51:57 +00007610 uint32_t PersonalitiesStart = readNext<uint32_t>(Contents, Pos);
Tim Northover39c70bb2014-08-12 11:52:59 +00007611 outs() << " Personality function array section offset: "
7612 << format("0x%" PRIx32, PersonalitiesStart) << '\n';
Tim Northover3a4e1c72018-01-23 13:51:57 +00007613 uint32_t NumPersonalities = readNext<uint32_t>(Contents, Pos);
Tim Northover39c70bb2014-08-12 11:52:59 +00007614 outs() << " Number of personality functions in array: "
7615 << format("0x%" PRIx32, NumPersonalities) << '\n';
7616
Tim Northover3a4e1c72018-01-23 13:51:57 +00007617 uint32_t IndicesStart = readNext<uint32_t>(Contents, Pos);
Tim Northover39c70bb2014-08-12 11:52:59 +00007618 outs() << " Index array section offset: "
7619 << format("0x%" PRIx32, IndicesStart) << '\n';
Tim Northover3a4e1c72018-01-23 13:51:57 +00007620 uint32_t NumIndices = readNext<uint32_t>(Contents, Pos);
Tim Northover39c70bb2014-08-12 11:52:59 +00007621 outs() << " Number of indices in array: "
7622 << format("0x%" PRIx32, NumIndices) << '\n';
7623
7624 //===----------------------------------
7625 // A shared list of common encodings
7626 //===----------------------------------
7627
7628 // These occupy indices in the range [0, N] whenever an encoding is referenced
7629 // from a compressed 2nd level index table. In practice the linker only
7630 // creates ~128 of these, so that indices are available to embed encodings in
7631 // the 2nd level index.
7632
7633 SmallVector<uint32_t, 64> CommonEncodings;
7634 outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n";
Tim Northover3a4e1c72018-01-23 13:51:57 +00007635 Pos = CommonEncodingsStart;
Tim Northover39c70bb2014-08-12 11:52:59 +00007636 for (unsigned i = 0; i < NumCommonEncodings; ++i) {
Tim Northover3a4e1c72018-01-23 13:51:57 +00007637 uint32_t Encoding = readNext<uint32_t>(Contents, Pos);
Tim Northover39c70bb2014-08-12 11:52:59 +00007638 CommonEncodings.push_back(Encoding);
7639
7640 outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding)
7641 << '\n';
7642 }
7643
Tim Northover39c70bb2014-08-12 11:52:59 +00007644 //===----------------------------------
7645 // Personality functions used in this executable
7646 //===----------------------------------
7647
7648 // There should be only a handful of these (one per source language,
7649 // roughly). Particularly since they only get 2 bits in the compact encoding.
7650
7651 outs() << " Personality functions: (count = " << NumPersonalities << ")\n";
Tim Northover3a4e1c72018-01-23 13:51:57 +00007652 Pos = PersonalitiesStart;
Tim Northover39c70bb2014-08-12 11:52:59 +00007653 for (unsigned i = 0; i < NumPersonalities; ++i) {
Tim Northover3a4e1c72018-01-23 13:51:57 +00007654 uint32_t PersonalityFn = readNext<uint32_t>(Contents, Pos);
Tim Northover39c70bb2014-08-12 11:52:59 +00007655 outs() << " personality[" << i + 1
7656 << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n';
7657 }
7658
7659 //===----------------------------------
7660 // The level 1 index entries
7661 //===----------------------------------
7662
7663 // These specify an approximate place to start searching for the more detailed
7664 // information, sorted by PC.
7665
7666 struct IndexEntry {
7667 uint32_t FunctionOffset;
7668 uint32_t SecondLevelPageStart;
7669 uint32_t LSDAStart;
7670 };
7671
7672 SmallVector<IndexEntry, 4> IndexEntries;
7673
7674 outs() << " Top level indices: (count = " << NumIndices << ")\n";
Tim Northover3a4e1c72018-01-23 13:51:57 +00007675 Pos = IndicesStart;
Tim Northover39c70bb2014-08-12 11:52:59 +00007676 for (unsigned i = 0; i < NumIndices; ++i) {
7677 IndexEntry Entry;
7678
Tim Northover3a4e1c72018-01-23 13:51:57 +00007679 Entry.FunctionOffset = readNext<uint32_t>(Contents, Pos);
7680 Entry.SecondLevelPageStart = readNext<uint32_t>(Contents, Pos);
7681 Entry.LSDAStart = readNext<uint32_t>(Contents, Pos);
Tim Northover39c70bb2014-08-12 11:52:59 +00007682 IndexEntries.push_back(Entry);
7683
7684 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007685 << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset)
7686 << ", "
Tim Northover39c70bb2014-08-12 11:52:59 +00007687 << "2nd level page offset="
7688 << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007689 << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00007690 }
7691
Tim Northover39c70bb2014-08-12 11:52:59 +00007692 //===----------------------------------
7693 // Next come the LSDA tables
7694 //===----------------------------------
7695
7696 // The LSDA layout is rather implicit: it's a contiguous array of entries from
7697 // the first top-level index's LSDAOffset to the last (sentinel).
7698
7699 outs() << " LSDA descriptors:\n";
Tim Northover3a4e1c72018-01-23 13:51:57 +00007700 Pos = IndexEntries[0].LSDAStart;
7701 const uint32_t LSDASize = 2 * sizeof(uint32_t);
7702 int NumLSDAs =
7703 (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) / LSDASize;
7704
Tim Northover39c70bb2014-08-12 11:52:59 +00007705 for (int i = 0; i < NumLSDAs; ++i) {
Tim Northover3a4e1c72018-01-23 13:51:57 +00007706 uint32_t FunctionOffset = readNext<uint32_t>(Contents, Pos);
7707 uint32_t LSDAOffset = readNext<uint32_t>(Contents, Pos);
Tim Northover39c70bb2014-08-12 11:52:59 +00007708 outs() << " [" << i << "]: "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00007709 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
7710 << ", "
7711 << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00007712 }
7713
7714 //===----------------------------------
7715 // Finally, the 2nd level indices
7716 //===----------------------------------
7717
7718 // Generally these are 4K in size, and have 2 possible forms:
7719 // + Regular stores up to 511 entries with disparate encodings
7720 // + Compressed stores up to 1021 entries if few enough compact encoding
7721 // values are used.
7722 outs() << " Second level indices:\n";
7723 for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) {
7724 // The final sentinel top-level index has no associated 2nd level page
7725 if (IndexEntries[i].SecondLevelPageStart == 0)
7726 break;
7727
7728 outs() << " Second level index[" << i << "]: "
7729 << "offset in section="
7730 << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart)
7731 << ", "
7732 << "base function offset="
7733 << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n';
7734
Tim Northover3a4e1c72018-01-23 13:51:57 +00007735 Pos = IndexEntries[i].SecondLevelPageStart;
7736 if (Pos + sizeof(uint32_t) > Contents.size()) {
7737 outs() << "warning: invalid offset for second level page: " << Pos << '\n';
7738 continue;
7739 }
7740
7741 uint32_t Kind =
7742 *reinterpret_cast<const support::ulittle32_t *>(Contents.data() + Pos);
Tim Northover39c70bb2014-08-12 11:52:59 +00007743 if (Kind == 2)
Tim Northover3a4e1c72018-01-23 13:51:57 +00007744 printRegularSecondLevelUnwindPage(Contents.substr(Pos, 4096));
Tim Northover39c70bb2014-08-12 11:52:59 +00007745 else if (Kind == 3)
Tim Northover3a4e1c72018-01-23 13:51:57 +00007746 printCompressedSecondLevelUnwindPage(Contents.substr(Pos, 4096),
7747 IndexEntries[i].FunctionOffset,
Tim Northover39c70bb2014-08-12 11:52:59 +00007748 CommonEncodings);
7749 else
Tim Northoverbf55f7e2016-11-15 20:26:01 +00007750 outs() << " Skipping 2nd level page with unknown kind " << Kind
7751 << '\n';
Tim Northover39c70bb2014-08-12 11:52:59 +00007752 }
7753}
7754
Tim Northover4bd286a2014-08-01 13:07:19 +00007755void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) {
7756 std::map<uint64_t, SymbolRef> Symbols;
7757 for (const SymbolRef &SymRef : Obj->symbols()) {
7758 // Discard any undefined or absolute symbols. They're not going to take part
7759 // in the convenience lookup for unwind info and just take up resources.
Kevin Enderby7bd8d992016-05-02 20:28:12 +00007760 auto SectOrErr = SymRef.getSection();
7761 if (!SectOrErr) {
7762 // TODO: Actually report errors helpfully.
7763 consumeError(SectOrErr.takeError());
7764 continue;
7765 }
7766 section_iterator Section = *SectOrErr;
Tim Northover4bd286a2014-08-01 13:07:19 +00007767 if (Section == Obj->section_end())
7768 continue;
7769
Rafael Espindoladea00162015-07-03 17:44:18 +00007770 uint64_t Addr = SymRef.getValue();
Tim Northover4bd286a2014-08-01 13:07:19 +00007771 Symbols.insert(std::make_pair(Addr, SymRef));
7772 }
7773
7774 for (const SectionRef &Section : Obj->sections()) {
7775 StringRef SectName;
7776 Section.getName(SectName);
7777 if (SectName == "__compact_unwind")
7778 printMachOCompactUnwindSection(Obj, Symbols, Section);
7779 else if (SectName == "__unwind_info")
Tim Northover39c70bb2014-08-12 11:52:59 +00007780 printMachOUnwindInfoSection(Obj, Symbols, Section);
Tim Northover4bd286a2014-08-01 13:07:19 +00007781 }
7782}
Kevin Enderbyb76d3862014-08-22 20:35:18 +00007783
7784static void PrintMachHeader(uint32_t magic, uint32_t cputype,
7785 uint32_t cpusubtype, uint32_t filetype,
7786 uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
7787 bool verbose) {
7788 outs() << "Mach header\n";
7789 outs() << " magic cputype cpusubtype caps filetype ncmds "
7790 "sizeofcmds flags\n";
7791 if (verbose) {
7792 if (magic == MachO::MH_MAGIC)
7793 outs() << " MH_MAGIC";
7794 else if (magic == MachO::MH_MAGIC_64)
7795 outs() << "MH_MAGIC_64";
7796 else
7797 outs() << format(" 0x%08" PRIx32, magic);
7798 switch (cputype) {
7799 case MachO::CPU_TYPE_I386:
7800 outs() << " I386";
7801 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7802 case MachO::CPU_SUBTYPE_I386_ALL:
7803 outs() << " ALL";
7804 break;
7805 default:
7806 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7807 break;
7808 }
7809 break;
7810 case MachO::CPU_TYPE_X86_64:
7811 outs() << " X86_64";
Kevin Enderby131d1772015-01-09 19:22:37 +00007812 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7813 case MachO::CPU_SUBTYPE_X86_64_ALL:
7814 outs() << " ALL";
7815 break;
7816 case MachO::CPU_SUBTYPE_X86_64_H:
7817 outs() << " Haswell";
7818 break;
7819 default:
7820 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7821 break;
7822 }
Kevin Enderbyb76d3862014-08-22 20:35:18 +00007823 break;
7824 case MachO::CPU_TYPE_ARM:
7825 outs() << " ARM";
7826 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7827 case MachO::CPU_SUBTYPE_ARM_ALL:
7828 outs() << " ALL";
7829 break;
7830 case MachO::CPU_SUBTYPE_ARM_V4T:
7831 outs() << " V4T";
7832 break;
7833 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
7834 outs() << " V5TEJ";
7835 break;
7836 case MachO::CPU_SUBTYPE_ARM_XSCALE:
7837 outs() << " XSCALE";
7838 break;
7839 case MachO::CPU_SUBTYPE_ARM_V6:
7840 outs() << " V6";
7841 break;
7842 case MachO::CPU_SUBTYPE_ARM_V6M:
7843 outs() << " V6M";
7844 break;
7845 case MachO::CPU_SUBTYPE_ARM_V7:
7846 outs() << " V7";
7847 break;
7848 case MachO::CPU_SUBTYPE_ARM_V7EM:
7849 outs() << " V7EM";
7850 break;
7851 case MachO::CPU_SUBTYPE_ARM_V7K:
7852 outs() << " V7K";
7853 break;
7854 case MachO::CPU_SUBTYPE_ARM_V7M:
7855 outs() << " V7M";
7856 break;
7857 case MachO::CPU_SUBTYPE_ARM_V7S:
7858 outs() << " V7S";
7859 break;
7860 default:
7861 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7862 break;
7863 }
7864 break;
7865 case MachO::CPU_TYPE_ARM64:
7866 outs() << " ARM64";
7867 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7868 case MachO::CPU_SUBTYPE_ARM64_ALL:
7869 outs() << " ALL";
7870 break;
7871 default:
7872 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7873 break;
7874 }
7875 break;
7876 case MachO::CPU_TYPE_POWERPC:
7877 outs() << " PPC";
7878 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7879 case MachO::CPU_SUBTYPE_POWERPC_ALL:
7880 outs() << " ALL";
7881 break;
7882 default:
7883 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7884 break;
7885 }
7886 break;
7887 case MachO::CPU_TYPE_POWERPC64:
7888 outs() << " PPC64";
7889 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
7890 case MachO::CPU_SUBTYPE_POWERPC_ALL:
7891 outs() << " ALL";
7892 break;
7893 default:
7894 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7895 break;
7896 }
7897 break;
Kevin Enderby40fdbf82016-01-26 18:20:49 +00007898 default:
7899 outs() << format(" %7d", cputype);
7900 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
7901 break;
Kevin Enderbyb76d3862014-08-22 20:35:18 +00007902 }
7903 if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00007904 outs() << " LIB64";
Kevin Enderbyb76d3862014-08-22 20:35:18 +00007905 } else {
7906 outs() << format(" 0x%02" PRIx32,
7907 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
7908 }
7909 switch (filetype) {
7910 case MachO::MH_OBJECT:
7911 outs() << " OBJECT";
7912 break;
7913 case MachO::MH_EXECUTE:
7914 outs() << " EXECUTE";
7915 break;
7916 case MachO::MH_FVMLIB:
7917 outs() << " FVMLIB";
7918 break;
7919 case MachO::MH_CORE:
7920 outs() << " CORE";
7921 break;
7922 case MachO::MH_PRELOAD:
7923 outs() << " PRELOAD";
7924 break;
7925 case MachO::MH_DYLIB:
7926 outs() << " DYLIB";
7927 break;
7928 case MachO::MH_DYLIB_STUB:
7929 outs() << " DYLIB_STUB";
7930 break;
7931 case MachO::MH_DYLINKER:
7932 outs() << " DYLINKER";
7933 break;
7934 case MachO::MH_BUNDLE:
7935 outs() << " BUNDLE";
7936 break;
7937 case MachO::MH_DSYM:
7938 outs() << " DSYM";
7939 break;
7940 case MachO::MH_KEXT_BUNDLE:
7941 outs() << " KEXTBUNDLE";
7942 break;
7943 default:
7944 outs() << format(" %10u", filetype);
7945 break;
7946 }
7947 outs() << format(" %5u", ncmds);
7948 outs() << format(" %10u", sizeofcmds);
7949 uint32_t f = flags;
7950 if (f & MachO::MH_NOUNDEFS) {
7951 outs() << " NOUNDEFS";
7952 f &= ~MachO::MH_NOUNDEFS;
7953 }
7954 if (f & MachO::MH_INCRLINK) {
7955 outs() << " INCRLINK";
7956 f &= ~MachO::MH_INCRLINK;
7957 }
7958 if (f & MachO::MH_DYLDLINK) {
7959 outs() << " DYLDLINK";
7960 f &= ~MachO::MH_DYLDLINK;
7961 }
7962 if (f & MachO::MH_BINDATLOAD) {
7963 outs() << " BINDATLOAD";
7964 f &= ~MachO::MH_BINDATLOAD;
7965 }
7966 if (f & MachO::MH_PREBOUND) {
7967 outs() << " PREBOUND";
7968 f &= ~MachO::MH_PREBOUND;
7969 }
7970 if (f & MachO::MH_SPLIT_SEGS) {
7971 outs() << " SPLIT_SEGS";
7972 f &= ~MachO::MH_SPLIT_SEGS;
7973 }
7974 if (f & MachO::MH_LAZY_INIT) {
7975 outs() << " LAZY_INIT";
7976 f &= ~MachO::MH_LAZY_INIT;
7977 }
7978 if (f & MachO::MH_TWOLEVEL) {
7979 outs() << " TWOLEVEL";
7980 f &= ~MachO::MH_TWOLEVEL;
7981 }
7982 if (f & MachO::MH_FORCE_FLAT) {
7983 outs() << " FORCE_FLAT";
7984 f &= ~MachO::MH_FORCE_FLAT;
7985 }
7986 if (f & MachO::MH_NOMULTIDEFS) {
7987 outs() << " NOMULTIDEFS";
7988 f &= ~MachO::MH_NOMULTIDEFS;
7989 }
7990 if (f & MachO::MH_NOFIXPREBINDING) {
7991 outs() << " NOFIXPREBINDING";
7992 f &= ~MachO::MH_NOFIXPREBINDING;
7993 }
7994 if (f & MachO::MH_PREBINDABLE) {
7995 outs() << " PREBINDABLE";
7996 f &= ~MachO::MH_PREBINDABLE;
7997 }
7998 if (f & MachO::MH_ALLMODSBOUND) {
7999 outs() << " ALLMODSBOUND";
8000 f &= ~MachO::MH_ALLMODSBOUND;
8001 }
8002 if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
8003 outs() << " SUBSECTIONS_VIA_SYMBOLS";
8004 f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
8005 }
8006 if (f & MachO::MH_CANONICAL) {
8007 outs() << " CANONICAL";
8008 f &= ~MachO::MH_CANONICAL;
8009 }
8010 if (f & MachO::MH_WEAK_DEFINES) {
8011 outs() << " WEAK_DEFINES";
8012 f &= ~MachO::MH_WEAK_DEFINES;
8013 }
8014 if (f & MachO::MH_BINDS_TO_WEAK) {
8015 outs() << " BINDS_TO_WEAK";
8016 f &= ~MachO::MH_BINDS_TO_WEAK;
8017 }
8018 if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
8019 outs() << " ALLOW_STACK_EXECUTION";
8020 f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
8021 }
8022 if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
8023 outs() << " DEAD_STRIPPABLE_DYLIB";
8024 f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
8025 }
8026 if (f & MachO::MH_PIE) {
8027 outs() << " PIE";
8028 f &= ~MachO::MH_PIE;
8029 }
8030 if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
8031 outs() << " NO_REEXPORTED_DYLIBS";
8032 f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
8033 }
8034 if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
8035 outs() << " MH_HAS_TLV_DESCRIPTORS";
8036 f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
8037 }
8038 if (f & MachO::MH_NO_HEAP_EXECUTION) {
8039 outs() << " MH_NO_HEAP_EXECUTION";
8040 f &= ~MachO::MH_NO_HEAP_EXECUTION;
8041 }
8042 if (f & MachO::MH_APP_EXTENSION_SAFE) {
8043 outs() << " APP_EXTENSION_SAFE";
8044 f &= ~MachO::MH_APP_EXTENSION_SAFE;
8045 }
Kevin Enderbydf0d6da2017-06-19 19:38:22 +00008046 if (f & MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO) {
8047 outs() << " NLIST_OUTOFSYNC_WITH_DYLDINFO";
8048 f &= ~MachO::MH_NLIST_OUTOFSYNC_WITH_DYLDINFO;
8049 }
Kevin Enderbyb76d3862014-08-22 20:35:18 +00008050 if (f != 0 || flags == 0)
8051 outs() << format(" 0x%08" PRIx32, f);
8052 } else {
8053 outs() << format(" 0x%08" PRIx32, magic);
8054 outs() << format(" %7d", cputype);
8055 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
8056 outs() << format(" 0x%02" PRIx32,
8057 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
8058 outs() << format(" %10u", filetype);
8059 outs() << format(" %5u", ncmds);
8060 outs() << format(" %10u", sizeofcmds);
8061 outs() << format(" 0x%08" PRIx32, flags);
8062 }
8063 outs() << "\n";
8064}
8065
Kevin Enderby956366c2014-08-29 22:30:52 +00008066static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
8067 StringRef SegName, uint64_t vmaddr,
8068 uint64_t vmsize, uint64_t fileoff,
8069 uint64_t filesize, uint32_t maxprot,
8070 uint32_t initprot, uint32_t nsects,
8071 uint32_t flags, uint32_t object_size,
8072 bool verbose) {
8073 uint64_t expected_cmdsize;
8074 if (cmd == MachO::LC_SEGMENT) {
8075 outs() << " cmd LC_SEGMENT\n";
8076 expected_cmdsize = nsects;
8077 expected_cmdsize *= sizeof(struct MachO::section);
8078 expected_cmdsize += sizeof(struct MachO::segment_command);
8079 } else {
8080 outs() << " cmd LC_SEGMENT_64\n";
8081 expected_cmdsize = nsects;
8082 expected_cmdsize *= sizeof(struct MachO::section_64);
8083 expected_cmdsize += sizeof(struct MachO::segment_command_64);
8084 }
8085 outs() << " cmdsize " << cmdsize;
8086 if (cmdsize != expected_cmdsize)
8087 outs() << " Inconsistent size\n";
8088 else
8089 outs() << "\n";
8090 outs() << " segname " << SegName << "\n";
8091 if (cmd == MachO::LC_SEGMENT_64) {
8092 outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n";
8093 outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n";
8094 } else {
Kevin Enderbyadb7c432014-12-16 18:58:11 +00008095 outs() << " vmaddr " << format("0x%08" PRIx64, vmaddr) << "\n";
8096 outs() << " vmsize " << format("0x%08" PRIx64, vmsize) << "\n";
Kevin Enderby956366c2014-08-29 22:30:52 +00008097 }
8098 outs() << " fileoff " << fileoff;
8099 if (fileoff > object_size)
8100 outs() << " (past end of file)\n";
8101 else
8102 outs() << "\n";
8103 outs() << " filesize " << filesize;
8104 if (fileoff + filesize > object_size)
8105 outs() << " (past end of file)\n";
8106 else
8107 outs() << "\n";
8108 if (verbose) {
8109 if ((maxprot &
8110 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
8111 MachO::VM_PROT_EXECUTE)) != 0)
8112 outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n";
8113 else {
Davide Italiano37ff06a2015-09-02 16:53:25 +00008114 outs() << " maxprot ";
8115 outs() << ((maxprot & MachO::VM_PROT_READ) ? "r" : "-");
8116 outs() << ((maxprot & MachO::VM_PROT_WRITE) ? "w" : "-");
8117 outs() << ((maxprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
Kevin Enderby956366c2014-08-29 22:30:52 +00008118 }
8119 if ((initprot &
8120 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
8121 MachO::VM_PROT_EXECUTE)) != 0)
Kevin Enderby41c9c002016-10-21 18:22:35 +00008122 outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n";
Kevin Enderby956366c2014-08-29 22:30:52 +00008123 else {
Kevin Enderby41c9c002016-10-21 18:22:35 +00008124 outs() << " initprot ";
Davide Italiano37ff06a2015-09-02 16:53:25 +00008125 outs() << ((initprot & MachO::VM_PROT_READ) ? "r" : "-");
8126 outs() << ((initprot & MachO::VM_PROT_WRITE) ? "w" : "-");
8127 outs() << ((initprot & MachO::VM_PROT_EXECUTE) ? "x\n" : "-\n");
Kevin Enderby956366c2014-08-29 22:30:52 +00008128 }
8129 } else {
8130 outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n";
8131 outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n";
8132 }
8133 outs() << " nsects " << nsects << "\n";
8134 if (verbose) {
8135 outs() << " flags";
8136 if (flags == 0)
8137 outs() << " (none)\n";
8138 else {
8139 if (flags & MachO::SG_HIGHVM) {
8140 outs() << " HIGHVM";
8141 flags &= ~MachO::SG_HIGHVM;
8142 }
8143 if (flags & MachO::SG_FVMLIB) {
8144 outs() << " FVMLIB";
8145 flags &= ~MachO::SG_FVMLIB;
8146 }
8147 if (flags & MachO::SG_NORELOC) {
8148 outs() << " NORELOC";
8149 flags &= ~MachO::SG_NORELOC;
8150 }
8151 if (flags & MachO::SG_PROTECTED_VERSION_1) {
8152 outs() << " PROTECTED_VERSION_1";
8153 flags &= ~MachO::SG_PROTECTED_VERSION_1;
8154 }
8155 if (flags)
8156 outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n";
8157 else
8158 outs() << "\n";
8159 }
8160 } else {
8161 outs() << " flags " << format("0x%" PRIx32, flags) << "\n";
8162 }
8163}
8164
8165static void PrintSection(const char *sectname, const char *segname,
8166 uint64_t addr, uint64_t size, uint32_t offset,
8167 uint32_t align, uint32_t reloff, uint32_t nreloc,
8168 uint32_t flags, uint32_t reserved1, uint32_t reserved2,
8169 uint32_t cmd, const char *sg_segname,
8170 uint32_t filetype, uint32_t object_size,
8171 bool verbose) {
8172 outs() << "Section\n";
8173 outs() << " sectname " << format("%.16s\n", sectname);
8174 outs() << " segname " << format("%.16s", segname);
8175 if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0)
8176 outs() << " (does not match segment)\n";
8177 else
8178 outs() << "\n";
8179 if (cmd == MachO::LC_SEGMENT_64) {
8180 outs() << " addr " << format("0x%016" PRIx64, addr) << "\n";
8181 outs() << " size " << format("0x%016" PRIx64, size);
8182 } else {
Kevin Enderby75594b62014-12-16 21:00:25 +00008183 outs() << " addr " << format("0x%08" PRIx64, addr) << "\n";
8184 outs() << " size " << format("0x%08" PRIx64, size);
Kevin Enderby956366c2014-08-29 22:30:52 +00008185 }
8186 if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size)
8187 outs() << " (past end of file)\n";
8188 else
8189 outs() << "\n";
8190 outs() << " offset " << offset;
8191 if (offset > object_size)
8192 outs() << " (past end of file)\n";
8193 else
8194 outs() << "\n";
8195 uint32_t align_shifted = 1 << align;
8196 outs() << " align 2^" << align << " (" << align_shifted << ")\n";
8197 outs() << " reloff " << reloff;
8198 if (reloff > object_size)
8199 outs() << " (past end of file)\n";
8200 else
8201 outs() << "\n";
8202 outs() << " nreloc " << nreloc;
8203 if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size)
8204 outs() << " (past end of file)\n";
8205 else
8206 outs() << "\n";
8207 uint32_t section_type = flags & MachO::SECTION_TYPE;
8208 if (verbose) {
8209 outs() << " type";
8210 if (section_type == MachO::S_REGULAR)
8211 outs() << " S_REGULAR\n";
8212 else if (section_type == MachO::S_ZEROFILL)
8213 outs() << " S_ZEROFILL\n";
8214 else if (section_type == MachO::S_CSTRING_LITERALS)
8215 outs() << " S_CSTRING_LITERALS\n";
8216 else if (section_type == MachO::S_4BYTE_LITERALS)
8217 outs() << " S_4BYTE_LITERALS\n";
8218 else if (section_type == MachO::S_8BYTE_LITERALS)
8219 outs() << " S_8BYTE_LITERALS\n";
8220 else if (section_type == MachO::S_16BYTE_LITERALS)
8221 outs() << " S_16BYTE_LITERALS\n";
8222 else if (section_type == MachO::S_LITERAL_POINTERS)
8223 outs() << " S_LITERAL_POINTERS\n";
8224 else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS)
8225 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
8226 else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS)
8227 outs() << " S_LAZY_SYMBOL_POINTERS\n";
8228 else if (section_type == MachO::S_SYMBOL_STUBS)
8229 outs() << " S_SYMBOL_STUBS\n";
8230 else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS)
8231 outs() << " S_MOD_INIT_FUNC_POINTERS\n";
8232 else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS)
8233 outs() << " S_MOD_TERM_FUNC_POINTERS\n";
8234 else if (section_type == MachO::S_COALESCED)
8235 outs() << " S_COALESCED\n";
8236 else if (section_type == MachO::S_INTERPOSING)
8237 outs() << " S_INTERPOSING\n";
8238 else if (section_type == MachO::S_DTRACE_DOF)
8239 outs() << " S_DTRACE_DOF\n";
8240 else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS)
8241 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
8242 else if (section_type == MachO::S_THREAD_LOCAL_REGULAR)
8243 outs() << " S_THREAD_LOCAL_REGULAR\n";
8244 else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL)
8245 outs() << " S_THREAD_LOCAL_ZEROFILL\n";
8246 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES)
8247 outs() << " S_THREAD_LOCAL_VARIABLES\n";
8248 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
8249 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
8250 else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS)
8251 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
8252 else
8253 outs() << format("0x%08" PRIx32, section_type) << "\n";
8254 outs() << "attributes";
8255 uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES;
8256 if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS)
8257 outs() << " PURE_INSTRUCTIONS";
8258 if (section_attributes & MachO::S_ATTR_NO_TOC)
8259 outs() << " NO_TOC";
8260 if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS)
8261 outs() << " STRIP_STATIC_SYMS";
8262 if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP)
8263 outs() << " NO_DEAD_STRIP";
8264 if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT)
8265 outs() << " LIVE_SUPPORT";
8266 if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE)
8267 outs() << " SELF_MODIFYING_CODE";
8268 if (section_attributes & MachO::S_ATTR_DEBUG)
8269 outs() << " DEBUG";
8270 if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS)
8271 outs() << " SOME_INSTRUCTIONS";
8272 if (section_attributes & MachO::S_ATTR_EXT_RELOC)
8273 outs() << " EXT_RELOC";
8274 if (section_attributes & MachO::S_ATTR_LOC_RELOC)
8275 outs() << " LOC_RELOC";
8276 if (section_attributes == 0)
8277 outs() << " (none)";
8278 outs() << "\n";
8279 } else
8280 outs() << " flags " << format("0x%08" PRIx32, flags) << "\n";
8281 outs() << " reserved1 " << reserved1;
8282 if (section_type == MachO::S_SYMBOL_STUBS ||
8283 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
8284 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
8285 section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
8286 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
8287 outs() << " (index into indirect symbol table)\n";
8288 else
8289 outs() << "\n";
8290 outs() << " reserved2 " << reserved2;
8291 if (section_type == MachO::S_SYMBOL_STUBS)
8292 outs() << " (size of stubs)\n";
8293 else
8294 outs() << "\n";
8295}
8296
David Majnemer73cc6ff2014-11-13 19:48:56 +00008297static void PrintSymtabLoadCommand(MachO::symtab_command st, bool Is64Bit,
Kevin Enderby956366c2014-08-29 22:30:52 +00008298 uint32_t object_size) {
8299 outs() << " cmd LC_SYMTAB\n";
8300 outs() << " cmdsize " << st.cmdsize;
8301 if (st.cmdsize != sizeof(struct MachO::symtab_command))
8302 outs() << " Incorrect size\n";
8303 else
8304 outs() << "\n";
8305 outs() << " symoff " << st.symoff;
8306 if (st.symoff > object_size)
8307 outs() << " (past end of file)\n";
8308 else
8309 outs() << "\n";
8310 outs() << " nsyms " << st.nsyms;
8311 uint64_t big_size;
David Majnemer73cc6ff2014-11-13 19:48:56 +00008312 if (Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00008313 big_size = st.nsyms;
8314 big_size *= sizeof(struct MachO::nlist_64);
8315 big_size += st.symoff;
8316 if (big_size > object_size)
8317 outs() << " (past end of file)\n";
8318 else
8319 outs() << "\n";
8320 } else {
8321 big_size = st.nsyms;
8322 big_size *= sizeof(struct MachO::nlist);
8323 big_size += st.symoff;
8324 if (big_size > object_size)
8325 outs() << " (past end of file)\n";
8326 else
8327 outs() << "\n";
8328 }
8329 outs() << " stroff " << st.stroff;
8330 if (st.stroff > object_size)
8331 outs() << " (past end of file)\n";
8332 else
8333 outs() << "\n";
8334 outs() << " strsize " << st.strsize;
8335 big_size = st.stroff;
8336 big_size += st.strsize;
8337 if (big_size > object_size)
8338 outs() << " (past end of file)\n";
8339 else
8340 outs() << "\n";
8341}
8342
8343static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
8344 uint32_t nsyms, uint32_t object_size,
David Majnemer73cc6ff2014-11-13 19:48:56 +00008345 bool Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00008346 outs() << " cmd LC_DYSYMTAB\n";
8347 outs() << " cmdsize " << dyst.cmdsize;
8348 if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
8349 outs() << " Incorrect size\n";
8350 else
8351 outs() << "\n";
8352 outs() << " ilocalsym " << dyst.ilocalsym;
8353 if (dyst.ilocalsym > nsyms)
8354 outs() << " (greater than the number of symbols)\n";
8355 else
8356 outs() << "\n";
8357 outs() << " nlocalsym " << dyst.nlocalsym;
8358 uint64_t big_size;
8359 big_size = dyst.ilocalsym;
8360 big_size += dyst.nlocalsym;
8361 if (big_size > nsyms)
8362 outs() << " (past the end of the symbol table)\n";
8363 else
8364 outs() << "\n";
8365 outs() << " iextdefsym " << dyst.iextdefsym;
8366 if (dyst.iextdefsym > nsyms)
8367 outs() << " (greater than the number of symbols)\n";
8368 else
8369 outs() << "\n";
8370 outs() << " nextdefsym " << dyst.nextdefsym;
8371 big_size = dyst.iextdefsym;
8372 big_size += dyst.nextdefsym;
8373 if (big_size > nsyms)
8374 outs() << " (past the end of the symbol table)\n";
8375 else
8376 outs() << "\n";
8377 outs() << " iundefsym " << dyst.iundefsym;
8378 if (dyst.iundefsym > nsyms)
8379 outs() << " (greater than the number of symbols)\n";
8380 else
8381 outs() << "\n";
8382 outs() << " nundefsym " << dyst.nundefsym;
8383 big_size = dyst.iundefsym;
8384 big_size += dyst.nundefsym;
8385 if (big_size > nsyms)
8386 outs() << " (past the end of the symbol table)\n";
8387 else
8388 outs() << "\n";
8389 outs() << " tocoff " << dyst.tocoff;
8390 if (dyst.tocoff > object_size)
8391 outs() << " (past end of file)\n";
8392 else
8393 outs() << "\n";
8394 outs() << " ntoc " << dyst.ntoc;
8395 big_size = dyst.ntoc;
8396 big_size *= sizeof(struct MachO::dylib_table_of_contents);
8397 big_size += dyst.tocoff;
8398 if (big_size > object_size)
8399 outs() << " (past end of file)\n";
8400 else
8401 outs() << "\n";
8402 outs() << " modtaboff " << dyst.modtaboff;
8403 if (dyst.modtaboff > object_size)
8404 outs() << " (past end of file)\n";
8405 else
8406 outs() << "\n";
8407 outs() << " nmodtab " << dyst.nmodtab;
8408 uint64_t modtabend;
David Majnemer73cc6ff2014-11-13 19:48:56 +00008409 if (Is64Bit) {
Kevin Enderby956366c2014-08-29 22:30:52 +00008410 modtabend = dyst.nmodtab;
8411 modtabend *= sizeof(struct MachO::dylib_module_64);
8412 modtabend += dyst.modtaboff;
8413 } else {
8414 modtabend = dyst.nmodtab;
8415 modtabend *= sizeof(struct MachO::dylib_module);
8416 modtabend += dyst.modtaboff;
8417 }
8418 if (modtabend > object_size)
8419 outs() << " (past end of file)\n";
8420 else
8421 outs() << "\n";
8422 outs() << " extrefsymoff " << dyst.extrefsymoff;
8423 if (dyst.extrefsymoff > object_size)
8424 outs() << " (past end of file)\n";
8425 else
8426 outs() << "\n";
8427 outs() << " nextrefsyms " << dyst.nextrefsyms;
8428 big_size = dyst.nextrefsyms;
8429 big_size *= sizeof(struct MachO::dylib_reference);
8430 big_size += dyst.extrefsymoff;
8431 if (big_size > object_size)
8432 outs() << " (past end of file)\n";
8433 else
8434 outs() << "\n";
8435 outs() << " indirectsymoff " << dyst.indirectsymoff;
8436 if (dyst.indirectsymoff > object_size)
8437 outs() << " (past end of file)\n";
8438 else
8439 outs() << "\n";
8440 outs() << " nindirectsyms " << dyst.nindirectsyms;
8441 big_size = dyst.nindirectsyms;
8442 big_size *= sizeof(uint32_t);
8443 big_size += dyst.indirectsymoff;
8444 if (big_size > object_size)
8445 outs() << " (past end of file)\n";
8446 else
8447 outs() << "\n";
8448 outs() << " extreloff " << dyst.extreloff;
8449 if (dyst.extreloff > object_size)
8450 outs() << " (past end of file)\n";
8451 else
8452 outs() << "\n";
8453 outs() << " nextrel " << dyst.nextrel;
8454 big_size = dyst.nextrel;
8455 big_size *= sizeof(struct MachO::relocation_info);
8456 big_size += dyst.extreloff;
8457 if (big_size > object_size)
8458 outs() << " (past end of file)\n";
8459 else
8460 outs() << "\n";
8461 outs() << " locreloff " << dyst.locreloff;
8462 if (dyst.locreloff > object_size)
8463 outs() << " (past end of file)\n";
8464 else
8465 outs() << "\n";
8466 outs() << " nlocrel " << dyst.nlocrel;
8467 big_size = dyst.nlocrel;
8468 big_size *= sizeof(struct MachO::relocation_info);
8469 big_size += dyst.locreloff;
8470 if (big_size > object_size)
8471 outs() << " (past end of file)\n";
8472 else
8473 outs() << "\n";
8474}
8475
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008476static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc,
8477 uint32_t object_size) {
8478 if (dc.cmd == MachO::LC_DYLD_INFO)
8479 outs() << " cmd LC_DYLD_INFO\n";
8480 else
8481 outs() << " cmd LC_DYLD_INFO_ONLY\n";
8482 outs() << " cmdsize " << dc.cmdsize;
8483 if (dc.cmdsize != sizeof(struct MachO::dyld_info_command))
8484 outs() << " Incorrect size\n";
8485 else
8486 outs() << "\n";
8487 outs() << " rebase_off " << dc.rebase_off;
8488 if (dc.rebase_off > object_size)
8489 outs() << " (past end of file)\n";
8490 else
8491 outs() << "\n";
8492 outs() << " rebase_size " << dc.rebase_size;
8493 uint64_t big_size;
8494 big_size = dc.rebase_off;
8495 big_size += dc.rebase_size;
8496 if (big_size > object_size)
8497 outs() << " (past end of file)\n";
8498 else
8499 outs() << "\n";
8500 outs() << " bind_off " << dc.bind_off;
8501 if (dc.bind_off > object_size)
8502 outs() << " (past end of file)\n";
8503 else
8504 outs() << "\n";
8505 outs() << " bind_size " << dc.bind_size;
8506 big_size = dc.bind_off;
8507 big_size += dc.bind_size;
8508 if (big_size > object_size)
8509 outs() << " (past end of file)\n";
8510 else
8511 outs() << "\n";
8512 outs() << " weak_bind_off " << dc.weak_bind_off;
8513 if (dc.weak_bind_off > object_size)
8514 outs() << " (past end of file)\n";
8515 else
8516 outs() << "\n";
8517 outs() << " weak_bind_size " << dc.weak_bind_size;
8518 big_size = dc.weak_bind_off;
8519 big_size += dc.weak_bind_size;
8520 if (big_size > object_size)
8521 outs() << " (past end of file)\n";
8522 else
8523 outs() << "\n";
8524 outs() << " lazy_bind_off " << dc.lazy_bind_off;
8525 if (dc.lazy_bind_off > object_size)
8526 outs() << " (past end of file)\n";
8527 else
8528 outs() << "\n";
8529 outs() << " lazy_bind_size " << dc.lazy_bind_size;
8530 big_size = dc.lazy_bind_off;
8531 big_size += dc.lazy_bind_size;
8532 if (big_size > object_size)
8533 outs() << " (past end of file)\n";
8534 else
8535 outs() << "\n";
8536 outs() << " export_off " << dc.export_off;
8537 if (dc.export_off > object_size)
8538 outs() << " (past end of file)\n";
8539 else
8540 outs() << "\n";
8541 outs() << " export_size " << dc.export_size;
8542 big_size = dc.export_off;
8543 big_size += dc.export_size;
8544 if (big_size > object_size)
8545 outs() << " (past end of file)\n";
8546 else
8547 outs() << "\n";
8548}
8549
8550static void PrintDyldLoadCommand(MachO::dylinker_command dyld,
8551 const char *Ptr) {
8552 if (dyld.cmd == MachO::LC_ID_DYLINKER)
8553 outs() << " cmd LC_ID_DYLINKER\n";
8554 else if (dyld.cmd == MachO::LC_LOAD_DYLINKER)
8555 outs() << " cmd LC_LOAD_DYLINKER\n";
8556 else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT)
8557 outs() << " cmd LC_DYLD_ENVIRONMENT\n";
8558 else
8559 outs() << " cmd ?(" << dyld.cmd << ")\n";
8560 outs() << " cmdsize " << dyld.cmdsize;
8561 if (dyld.cmdsize < sizeof(struct MachO::dylinker_command))
8562 outs() << " Incorrect size\n";
8563 else
8564 outs() << "\n";
8565 if (dyld.name >= dyld.cmdsize)
8566 outs() << " name ?(bad offset " << dyld.name << ")\n";
8567 else {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00008568 const char *P = (const char *)(Ptr) + dyld.name;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008569 outs() << " name " << P << " (offset " << dyld.name << ")\n";
8570 }
8571}
8572
8573static void PrintUuidLoadCommand(MachO::uuid_command uuid) {
8574 outs() << " cmd LC_UUID\n";
8575 outs() << " cmdsize " << uuid.cmdsize;
8576 if (uuid.cmdsize != sizeof(struct MachO::uuid_command))
8577 outs() << " Incorrect size\n";
8578 else
8579 outs() << "\n";
8580 outs() << " uuid ";
Davide Italianoc74277a2015-12-07 00:03:28 +00008581 for (int i = 0; i < 16; ++i) {
8582 outs() << format("%02" PRIX32, uuid.uuid[i]);
8583 if (i == 3 || i == 5 || i == 7 || i == 9)
8584 outs() << "-";
8585 }
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008586 outs() << "\n";
8587}
8588
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008589static void PrintRpathLoadCommand(MachO::rpath_command rpath, const char *Ptr) {
Jean-Daniel Dupas00cc1f52014-12-04 07:37:02 +00008590 outs() << " cmd LC_RPATH\n";
8591 outs() << " cmdsize " << rpath.cmdsize;
8592 if (rpath.cmdsize < sizeof(struct MachO::rpath_command))
8593 outs() << " Incorrect size\n";
8594 else
8595 outs() << "\n";
8596 if (rpath.path >= rpath.cmdsize)
8597 outs() << " path ?(bad offset " << rpath.path << ")\n";
8598 else {
8599 const char *P = (const char *)(Ptr) + rpath.path;
8600 outs() << " path " << P << " (offset " << rpath.path << ")\n";
8601 }
8602}
8603
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008604static void PrintVersionMinLoadCommand(MachO::version_min_command vd) {
Tim Northoverbfbfb122015-11-02 21:26:58 +00008605 StringRef LoadCmdName;
8606 switch (vd.cmd) {
8607 case MachO::LC_VERSION_MIN_MACOSX:
8608 LoadCmdName = "LC_VERSION_MIN_MACOSX";
8609 break;
8610 case MachO::LC_VERSION_MIN_IPHONEOS:
8611 LoadCmdName = "LC_VERSION_MIN_IPHONEOS";
8612 break;
8613 case MachO::LC_VERSION_MIN_TVOS:
8614 LoadCmdName = "LC_VERSION_MIN_TVOS";
8615 break;
8616 case MachO::LC_VERSION_MIN_WATCHOS:
8617 LoadCmdName = "LC_VERSION_MIN_WATCHOS";
8618 break;
8619 default:
8620 llvm_unreachable("Unknown version min load command");
8621 }
8622
8623 outs() << " cmd " << LoadCmdName << '\n';
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008624 outs() << " cmdsize " << vd.cmdsize;
8625 if (vd.cmdsize != sizeof(struct MachO::version_min_command))
8626 outs() << " Incorrect size\n";
8627 else
8628 outs() << "\n";
Davide Italiano56baef32015-08-26 12:26:11 +00008629 outs() << " version "
8630 << MachOObjectFile::getVersionMinMajor(vd, false) << "."
8631 << MachOObjectFile::getVersionMinMinor(vd, false);
8632 uint32_t Update = MachOObjectFile::getVersionMinUpdate(vd, false);
8633 if (Update != 0)
8634 outs() << "." << Update;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008635 outs() << "\n";
8636 if (vd.sdk == 0)
Kevin Enderby57538292014-12-17 01:01:30 +00008637 outs() << " sdk n/a";
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008638 else {
Davide Italiano56baef32015-08-26 12:26:11 +00008639 outs() << " sdk "
8640 << MachOObjectFile::getVersionMinMajor(vd, true) << "."
8641 << MachOObjectFile::getVersionMinMinor(vd, true);
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008642 }
Davide Italiano56baef32015-08-26 12:26:11 +00008643 Update = MachOObjectFile::getVersionMinUpdate(vd, true);
8644 if (Update != 0)
8645 outs() << "." << Update;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008646 outs() << "\n";
8647}
8648
Kevin Enderbya4579c42017-01-19 17:36:31 +00008649static void PrintNoteLoadCommand(MachO::note_command Nt) {
8650 outs() << " cmd LC_NOTE\n";
8651 outs() << " cmdsize " << Nt.cmdsize;
8652 if (Nt.cmdsize != sizeof(struct MachO::note_command))
8653 outs() << " Incorrect size\n";
8654 else
8655 outs() << "\n";
8656 const char *d = Nt.data_owner;
8657 outs() << "data_owner " << format("%.16s\n", d);
8658 outs() << " offset " << Nt.offset << "\n";
8659 outs() << " size " << Nt.size << "\n";
8660}
8661
Steven Wu5b54a422017-01-23 20:07:55 +00008662static void PrintBuildToolVersion(MachO::build_tool_version bv) {
8663 outs() << " tool " << MachOObjectFile::getBuildTool(bv.tool) << "\n";
8664 outs() << " version " << MachOObjectFile::getVersionString(bv.version)
8665 << "\n";
8666}
8667
8668static void PrintBuildVersionLoadCommand(const MachOObjectFile *obj,
8669 MachO::build_version_command bd) {
8670 outs() << " cmd LC_BUILD_VERSION\n";
8671 outs() << " cmdsize " << bd.cmdsize;
8672 if (bd.cmdsize !=
8673 sizeof(struct MachO::build_version_command) +
8674 bd.ntools * sizeof(struct MachO::build_tool_version))
8675 outs() << " Incorrect size\n";
8676 else
8677 outs() << "\n";
8678 outs() << " platform " << MachOObjectFile::getBuildPlatform(bd.platform)
8679 << "\n";
8680 if (bd.sdk)
8681 outs() << " sdk " << MachOObjectFile::getVersionString(bd.sdk)
8682 << "\n";
8683 else
8684 outs() << " sdk n/a\n";
8685 outs() << " minos " << MachOObjectFile::getVersionString(bd.minos)
8686 << "\n";
8687 outs() << " ntools " << bd.ntools << "\n";
8688 for (unsigned i = 0; i < bd.ntools; ++i) {
8689 MachO::build_tool_version bv = obj->getBuildToolVersion(i);
8690 PrintBuildToolVersion(bv);
8691 }
8692}
8693
Kevin Enderby8ae63c12014-09-04 16:54:47 +00008694static void PrintSourceVersionCommand(MachO::source_version_command sd) {
8695 outs() << " cmd LC_SOURCE_VERSION\n";
8696 outs() << " cmdsize " << sd.cmdsize;
8697 if (sd.cmdsize != sizeof(struct MachO::source_version_command))
8698 outs() << " Incorrect size\n";
8699 else
8700 outs() << "\n";
8701 uint64_t a = (sd.version >> 40) & 0xffffff;
8702 uint64_t b = (sd.version >> 30) & 0x3ff;
8703 uint64_t c = (sd.version >> 20) & 0x3ff;
8704 uint64_t d = (sd.version >> 10) & 0x3ff;
8705 uint64_t e = sd.version & 0x3ff;
8706 outs() << " version " << a << "." << b;
8707 if (e != 0)
8708 outs() << "." << c << "." << d << "." << e;
8709 else if (d != 0)
8710 outs() << "." << c << "." << d;
8711 else if (c != 0)
8712 outs() << "." << c;
8713 outs() << "\n";
8714}
8715
8716static void PrintEntryPointCommand(MachO::entry_point_command ep) {
8717 outs() << " cmd LC_MAIN\n";
8718 outs() << " cmdsize " << ep.cmdsize;
8719 if (ep.cmdsize != sizeof(struct MachO::entry_point_command))
8720 outs() << " Incorrect size\n";
8721 else
8722 outs() << "\n";
8723 outs() << " entryoff " << ep.entryoff << "\n";
8724 outs() << " stacksize " << ep.stacksize << "\n";
8725}
8726
Kevin Enderby0804f4672014-12-16 23:25:52 +00008727static void PrintEncryptionInfoCommand(MachO::encryption_info_command ec,
8728 uint32_t object_size) {
8729 outs() << " cmd LC_ENCRYPTION_INFO\n";
8730 outs() << " cmdsize " << ec.cmdsize;
8731 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command))
8732 outs() << " Incorrect size\n";
8733 else
8734 outs() << "\n";
8735 outs() << " cryptoff " << ec.cryptoff;
8736 if (ec.cryptoff > object_size)
8737 outs() << " (past end of file)\n";
8738 else
8739 outs() << "\n";
8740 outs() << " cryptsize " << ec.cryptsize;
8741 if (ec.cryptsize > object_size)
8742 outs() << " (past end of file)\n";
8743 else
8744 outs() << "\n";
8745 outs() << " cryptid " << ec.cryptid << "\n";
8746}
8747
Kevin Enderby57538292014-12-17 01:01:30 +00008748static void PrintEncryptionInfoCommand64(MachO::encryption_info_command_64 ec,
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008749 uint32_t object_size) {
Kevin Enderby57538292014-12-17 01:01:30 +00008750 outs() << " cmd LC_ENCRYPTION_INFO_64\n";
8751 outs() << " cmdsize " << ec.cmdsize;
8752 if (ec.cmdsize != sizeof(struct MachO::encryption_info_command_64))
8753 outs() << " Incorrect size\n";
8754 else
8755 outs() << "\n";
8756 outs() << " cryptoff " << ec.cryptoff;
8757 if (ec.cryptoff > object_size)
8758 outs() << " (past end of file)\n";
8759 else
8760 outs() << "\n";
8761 outs() << " cryptsize " << ec.cryptsize;
8762 if (ec.cryptsize > object_size)
8763 outs() << " (past end of file)\n";
8764 else
8765 outs() << "\n";
8766 outs() << " cryptid " << ec.cryptid << "\n";
8767 outs() << " pad " << ec.pad << "\n";
8768}
8769
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00008770static void PrintLinkerOptionCommand(MachO::linker_option_command lo,
8771 const char *Ptr) {
8772 outs() << " cmd LC_LINKER_OPTION\n";
8773 outs() << " cmdsize " << lo.cmdsize;
8774 if (lo.cmdsize < sizeof(struct MachO::linker_option_command))
8775 outs() << " Incorrect size\n";
8776 else
8777 outs() << "\n";
8778 outs() << " count " << lo.count << "\n";
8779 const char *string = Ptr + sizeof(struct MachO::linker_option_command);
8780 uint32_t left = lo.cmdsize - sizeof(struct MachO::linker_option_command);
8781 uint32_t i = 0;
8782 while (left > 0) {
8783 while (*string == '\0' && left > 0) {
8784 string++;
8785 left--;
8786 }
8787 if (left > 0) {
8788 i++;
8789 outs() << " string #" << i << " " << format("%.*s\n", left, string);
David Majnemerd4449ed2014-12-20 08:24:43 +00008790 uint32_t NullPos = StringRef(string, left).find('\0');
8791 uint32_t len = std::min(NullPos, left) + 1;
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00008792 string += len;
8793 left -= len;
8794 }
8795 }
8796 if (lo.count != i)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008797 outs() << " count " << lo.count << " does not match number of strings "
8798 << i << "\n";
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00008799}
8800
Kevin Enderbyb4b79312014-12-18 19:24:35 +00008801static void PrintSubFrameworkCommand(MachO::sub_framework_command sub,
8802 const char *Ptr) {
8803 outs() << " cmd LC_SUB_FRAMEWORK\n";
8804 outs() << " cmdsize " << sub.cmdsize;
8805 if (sub.cmdsize < sizeof(struct MachO::sub_framework_command))
8806 outs() << " Incorrect size\n";
8807 else
8808 outs() << "\n";
8809 if (sub.umbrella < sub.cmdsize) {
8810 const char *P = Ptr + sub.umbrella;
8811 outs() << " umbrella " << P << " (offset " << sub.umbrella << ")\n";
8812 } else {
8813 outs() << " umbrella ?(bad offset " << sub.umbrella << ")\n";
8814 }
8815}
8816
Kevin Enderbya2bd8d92014-12-18 23:13:26 +00008817static void PrintSubUmbrellaCommand(MachO::sub_umbrella_command sub,
8818 const char *Ptr) {
8819 outs() << " cmd LC_SUB_UMBRELLA\n";
8820 outs() << " cmdsize " << sub.cmdsize;
8821 if (sub.cmdsize < sizeof(struct MachO::sub_umbrella_command))
8822 outs() << " Incorrect size\n";
8823 else
8824 outs() << "\n";
8825 if (sub.sub_umbrella < sub.cmdsize) {
8826 const char *P = Ptr + sub.sub_umbrella;
8827 outs() << " sub_umbrella " << P << " (offset " << sub.sub_umbrella << ")\n";
8828 } else {
8829 outs() << " sub_umbrella ?(bad offset " << sub.sub_umbrella << ")\n";
8830 }
8831}
8832
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00008833static void PrintSubLibraryCommand(MachO::sub_library_command sub,
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008834 const char *Ptr) {
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00008835 outs() << " cmd LC_SUB_LIBRARY\n";
8836 outs() << " cmdsize " << sub.cmdsize;
8837 if (sub.cmdsize < sizeof(struct MachO::sub_library_command))
8838 outs() << " Incorrect size\n";
8839 else
8840 outs() << "\n";
8841 if (sub.sub_library < sub.cmdsize) {
8842 const char *P = Ptr + sub.sub_library;
8843 outs() << " sub_library " << P << " (offset " << sub.sub_library << ")\n";
8844 } else {
8845 outs() << " sub_library ?(bad offset " << sub.sub_library << ")\n";
8846 }
8847}
8848
Kevin Enderby186eac32014-12-19 21:06:24 +00008849static void PrintSubClientCommand(MachO::sub_client_command sub,
8850 const char *Ptr) {
8851 outs() << " cmd LC_SUB_CLIENT\n";
8852 outs() << " cmdsize " << sub.cmdsize;
8853 if (sub.cmdsize < sizeof(struct MachO::sub_client_command))
8854 outs() << " Incorrect size\n";
8855 else
8856 outs() << "\n";
8857 if (sub.client < sub.cmdsize) {
8858 const char *P = Ptr + sub.client;
8859 outs() << " client " << P << " (offset " << sub.client << ")\n";
8860 } else {
8861 outs() << " client ?(bad offset " << sub.client << ")\n";
8862 }
8863}
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00008864
Kevin Enderby52e4ce42014-12-19 22:25:22 +00008865static void PrintRoutinesCommand(MachO::routines_command r) {
8866 outs() << " cmd LC_ROUTINES\n";
8867 outs() << " cmdsize " << r.cmdsize;
8868 if (r.cmdsize != sizeof(struct MachO::routines_command))
8869 outs() << " Incorrect size\n";
8870 else
8871 outs() << "\n";
8872 outs() << " init_address " << format("0x%08" PRIx32, r.init_address) << "\n";
8873 outs() << " init_module " << r.init_module << "\n";
8874 outs() << " reserved1 " << r.reserved1 << "\n";
8875 outs() << " reserved2 " << r.reserved2 << "\n";
8876 outs() << " reserved3 " << r.reserved3 << "\n";
8877 outs() << " reserved4 " << r.reserved4 << "\n";
8878 outs() << " reserved5 " << r.reserved5 << "\n";
8879 outs() << " reserved6 " << r.reserved6 << "\n";
8880}
8881
8882static void PrintRoutinesCommand64(MachO::routines_command_64 r) {
8883 outs() << " cmd LC_ROUTINES_64\n";
8884 outs() << " cmdsize " << r.cmdsize;
8885 if (r.cmdsize != sizeof(struct MachO::routines_command_64))
8886 outs() << " Incorrect size\n";
8887 else
8888 outs() << "\n";
8889 outs() << " init_address " << format("0x%016" PRIx64, r.init_address) << "\n";
8890 outs() << " init_module " << r.init_module << "\n";
8891 outs() << " reserved1 " << r.reserved1 << "\n";
8892 outs() << " reserved2 " << r.reserved2 << "\n";
8893 outs() << " reserved3 " << r.reserved3 << "\n";
8894 outs() << " reserved4 " << r.reserved4 << "\n";
8895 outs() << " reserved5 " << r.reserved5 << "\n";
8896 outs() << " reserved6 " << r.reserved6 << "\n";
8897}
8898
Kevin Enderbyc3a035d2017-01-23 21:13:29 +00008899static void Print_x86_thread_state32_t(MachO::x86_thread_state32_t &cpu32) {
8900 outs() << "\t eax " << format("0x%08" PRIx32, cpu32.eax);
8901 outs() << " ebx " << format("0x%08" PRIx32, cpu32.ebx);
8902 outs() << " ecx " << format("0x%08" PRIx32, cpu32.ecx);
8903 outs() << " edx " << format("0x%08" PRIx32, cpu32.edx) << "\n";
8904 outs() << "\t edi " << format("0x%08" PRIx32, cpu32.edi);
8905 outs() << " esi " << format("0x%08" PRIx32, cpu32.esi);
8906 outs() << " ebp " << format("0x%08" PRIx32, cpu32.ebp);
8907 outs() << " esp " << format("0x%08" PRIx32, cpu32.esp) << "\n";
8908 outs() << "\t ss " << format("0x%08" PRIx32, cpu32.ss);
8909 outs() << " eflags " << format("0x%08" PRIx32, cpu32.eflags);
8910 outs() << " eip " << format("0x%08" PRIx32, cpu32.eip);
8911 outs() << " cs " << format("0x%08" PRIx32, cpu32.cs) << "\n";
8912 outs() << "\t ds " << format("0x%08" PRIx32, cpu32.ds);
8913 outs() << " es " << format("0x%08" PRIx32, cpu32.es);
8914 outs() << " fs " << format("0x%08" PRIx32, cpu32.fs);
8915 outs() << " gs " << format("0x%08" PRIx32, cpu32.gs) << "\n";
8916}
8917
Kevin Enderby48ef5342014-12-23 22:56:39 +00008918static void Print_x86_thread_state64_t(MachO::x86_thread_state64_t &cpu64) {
8919 outs() << " rax " << format("0x%016" PRIx64, cpu64.rax);
8920 outs() << " rbx " << format("0x%016" PRIx64, cpu64.rbx);
8921 outs() << " rcx " << format("0x%016" PRIx64, cpu64.rcx) << "\n";
8922 outs() << " rdx " << format("0x%016" PRIx64, cpu64.rdx);
8923 outs() << " rdi " << format("0x%016" PRIx64, cpu64.rdi);
8924 outs() << " rsi " << format("0x%016" PRIx64, cpu64.rsi) << "\n";
8925 outs() << " rbp " << format("0x%016" PRIx64, cpu64.rbp);
8926 outs() << " rsp " << format("0x%016" PRIx64, cpu64.rsp);
8927 outs() << " r8 " << format("0x%016" PRIx64, cpu64.r8) << "\n";
8928 outs() << " r9 " << format("0x%016" PRIx64, cpu64.r9);
8929 outs() << " r10 " << format("0x%016" PRIx64, cpu64.r10);
8930 outs() << " r11 " << format("0x%016" PRIx64, cpu64.r11) << "\n";
8931 outs() << " r12 " << format("0x%016" PRIx64, cpu64.r12);
8932 outs() << " r13 " << format("0x%016" PRIx64, cpu64.r13);
8933 outs() << " r14 " << format("0x%016" PRIx64, cpu64.r14) << "\n";
8934 outs() << " r15 " << format("0x%016" PRIx64, cpu64.r15);
8935 outs() << " rip " << format("0x%016" PRIx64, cpu64.rip) << "\n";
8936 outs() << "rflags " << format("0x%016" PRIx64, cpu64.rflags);
8937 outs() << " cs " << format("0x%016" PRIx64, cpu64.cs);
8938 outs() << " fs " << format("0x%016" PRIx64, cpu64.fs) << "\n";
8939 outs() << " gs " << format("0x%016" PRIx64, cpu64.gs) << "\n";
8940}
8941
Kevin Enderby227df342014-12-23 23:43:59 +00008942static void Print_mmst_reg(MachO::mmst_reg_t &r) {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008943 uint32_t f;
8944 outs() << "\t mmst_reg ";
8945 for (f = 0; f < 10; f++)
8946 outs() << format("%02" PRIx32, (r.mmst_reg[f] & 0xff)) << " ";
8947 outs() << "\n";
8948 outs() << "\t mmst_rsrv ";
8949 for (f = 0; f < 6; f++)
8950 outs() << format("%02" PRIx32, (r.mmst_rsrv[f] & 0xff)) << " ";
8951 outs() << "\n";
8952}
8953
Kevin Enderbyaefb0032014-12-24 00:16:51 +00008954static void Print_xmm_reg(MachO::xmm_reg_t &r) {
Kevin Enderby48ef5342014-12-23 22:56:39 +00008955 uint32_t f;
8956 outs() << "\t xmm_reg ";
8957 for (f = 0; f < 16; f++)
8958 outs() << format("%02" PRIx32, (r.xmm_reg[f] & 0xff)) << " ";
8959 outs() << "\n";
8960}
8961
8962static void Print_x86_float_state_t(MachO::x86_float_state64_t &fpu) {
8963 outs() << "\t fpu_reserved[0] " << fpu.fpu_reserved[0];
8964 outs() << " fpu_reserved[1] " << fpu.fpu_reserved[1] << "\n";
8965 outs() << "\t control: invalid " << fpu.fpu_fcw.invalid;
8966 outs() << " denorm " << fpu.fpu_fcw.denorm;
8967 outs() << " zdiv " << fpu.fpu_fcw.zdiv;
8968 outs() << " ovrfl " << fpu.fpu_fcw.ovrfl;
8969 outs() << " undfl " << fpu.fpu_fcw.undfl;
8970 outs() << " precis " << fpu.fpu_fcw.precis << "\n";
8971 outs() << "\t\t pc ";
8972 if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_24B)
8973 outs() << "FP_PREC_24B ";
8974 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_53B)
8975 outs() << "FP_PREC_53B ";
8976 else if (fpu.fpu_fcw.pc == MachO::x86_FP_PREC_64B)
8977 outs() << "FP_PREC_64B ";
8978 else
8979 outs() << fpu.fpu_fcw.pc << " ";
8980 outs() << "rc ";
8981 if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_NEAR)
8982 outs() << "FP_RND_NEAR ";
8983 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_DOWN)
8984 outs() << "FP_RND_DOWN ";
8985 else if (fpu.fpu_fcw.rc == MachO::x86_FP_RND_UP)
8986 outs() << "FP_RND_UP ";
8987 else if (fpu.fpu_fcw.rc == MachO::x86_FP_CHOP)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00008988 outs() << "FP_CHOP ";
Kevin Enderby48ef5342014-12-23 22:56:39 +00008989 outs() << "\n";
8990 outs() << "\t status: invalid " << fpu.fpu_fsw.invalid;
8991 outs() << " denorm " << fpu.fpu_fsw.denorm;
8992 outs() << " zdiv " << fpu.fpu_fsw.zdiv;
8993 outs() << " ovrfl " << fpu.fpu_fsw.ovrfl;
8994 outs() << " undfl " << fpu.fpu_fsw.undfl;
8995 outs() << " precis " << fpu.fpu_fsw.precis;
8996 outs() << " stkflt " << fpu.fpu_fsw.stkflt << "\n";
8997 outs() << "\t errsumm " << fpu.fpu_fsw.errsumm;
8998 outs() << " c0 " << fpu.fpu_fsw.c0;
8999 outs() << " c1 " << fpu.fpu_fsw.c1;
9000 outs() << " c2 " << fpu.fpu_fsw.c2;
9001 outs() << " tos " << fpu.fpu_fsw.tos;
9002 outs() << " c3 " << fpu.fpu_fsw.c3;
9003 outs() << " busy " << fpu.fpu_fsw.busy << "\n";
9004 outs() << "\t fpu_ftw " << format("0x%02" PRIx32, fpu.fpu_ftw);
9005 outs() << " fpu_rsrv1 " << format("0x%02" PRIx32, fpu.fpu_rsrv1);
9006 outs() << " fpu_fop " << format("0x%04" PRIx32, fpu.fpu_fop);
9007 outs() << " fpu_ip " << format("0x%08" PRIx32, fpu.fpu_ip) << "\n";
9008 outs() << "\t fpu_cs " << format("0x%04" PRIx32, fpu.fpu_cs);
9009 outs() << " fpu_rsrv2 " << format("0x%04" PRIx32, fpu.fpu_rsrv2);
9010 outs() << " fpu_dp " << format("0x%08" PRIx32, fpu.fpu_dp);
9011 outs() << " fpu_ds " << format("0x%04" PRIx32, fpu.fpu_ds) << "\n";
9012 outs() << "\t fpu_rsrv3 " << format("0x%04" PRIx32, fpu.fpu_rsrv3);
9013 outs() << " fpu_mxcsr " << format("0x%08" PRIx32, fpu.fpu_mxcsr);
9014 outs() << " fpu_mxcsrmask " << format("0x%08" PRIx32, fpu.fpu_mxcsrmask);
9015 outs() << "\n";
9016 outs() << "\t fpu_stmm0:\n";
9017 Print_mmst_reg(fpu.fpu_stmm0);
9018 outs() << "\t fpu_stmm1:\n";
9019 Print_mmst_reg(fpu.fpu_stmm1);
9020 outs() << "\t fpu_stmm2:\n";
9021 Print_mmst_reg(fpu.fpu_stmm2);
9022 outs() << "\t fpu_stmm3:\n";
9023 Print_mmst_reg(fpu.fpu_stmm3);
9024 outs() << "\t fpu_stmm4:\n";
9025 Print_mmst_reg(fpu.fpu_stmm4);
9026 outs() << "\t fpu_stmm5:\n";
9027 Print_mmst_reg(fpu.fpu_stmm5);
9028 outs() << "\t fpu_stmm6:\n";
9029 Print_mmst_reg(fpu.fpu_stmm6);
9030 outs() << "\t fpu_stmm7:\n";
9031 Print_mmst_reg(fpu.fpu_stmm7);
9032 outs() << "\t fpu_xmm0:\n";
9033 Print_xmm_reg(fpu.fpu_xmm0);
9034 outs() << "\t fpu_xmm1:\n";
9035 Print_xmm_reg(fpu.fpu_xmm1);
9036 outs() << "\t fpu_xmm2:\n";
9037 Print_xmm_reg(fpu.fpu_xmm2);
9038 outs() << "\t fpu_xmm3:\n";
9039 Print_xmm_reg(fpu.fpu_xmm3);
9040 outs() << "\t fpu_xmm4:\n";
9041 Print_xmm_reg(fpu.fpu_xmm4);
9042 outs() << "\t fpu_xmm5:\n";
9043 Print_xmm_reg(fpu.fpu_xmm5);
9044 outs() << "\t fpu_xmm6:\n";
9045 Print_xmm_reg(fpu.fpu_xmm6);
9046 outs() << "\t fpu_xmm7:\n";
9047 Print_xmm_reg(fpu.fpu_xmm7);
9048 outs() << "\t fpu_xmm8:\n";
9049 Print_xmm_reg(fpu.fpu_xmm8);
9050 outs() << "\t fpu_xmm9:\n";
9051 Print_xmm_reg(fpu.fpu_xmm9);
9052 outs() << "\t fpu_xmm10:\n";
9053 Print_xmm_reg(fpu.fpu_xmm10);
9054 outs() << "\t fpu_xmm11:\n";
9055 Print_xmm_reg(fpu.fpu_xmm11);
9056 outs() << "\t fpu_xmm12:\n";
9057 Print_xmm_reg(fpu.fpu_xmm12);
9058 outs() << "\t fpu_xmm13:\n";
9059 Print_xmm_reg(fpu.fpu_xmm13);
9060 outs() << "\t fpu_xmm14:\n";
9061 Print_xmm_reg(fpu.fpu_xmm14);
9062 outs() << "\t fpu_xmm15:\n";
9063 Print_xmm_reg(fpu.fpu_xmm15);
9064 outs() << "\t fpu_rsrv4:\n";
9065 for (uint32_t f = 0; f < 6; f++) {
9066 outs() << "\t ";
9067 for (uint32_t g = 0; g < 16; g++)
Kevin Enderby66d51fc2015-01-08 00:25:24 +00009068 outs() << format("%02" PRIx32, fpu.fpu_rsrv4[f * g]) << " ";
Kevin Enderby48ef5342014-12-23 22:56:39 +00009069 outs() << "\n";
9070 }
9071 outs() << "\t fpu_reserved1 " << format("0x%08" PRIx32, fpu.fpu_reserved1);
9072 outs() << "\n";
9073}
9074
9075static void Print_x86_exception_state_t(MachO::x86_exception_state64_t &exc64) {
9076 outs() << "\t trapno " << format("0x%08" PRIx32, exc64.trapno);
9077 outs() << " err " << format("0x%08" PRIx32, exc64.err);
9078 outs() << " faultvaddr " << format("0x%016" PRIx64, exc64.faultvaddr) << "\n";
9079}
9080
Kevin Enderby41c9c002016-10-21 18:22:35 +00009081static void Print_arm_thread_state32_t(MachO::arm_thread_state32_t &cpu32) {
9082 outs() << "\t r0 " << format("0x%08" PRIx32, cpu32.r[0]);
9083 outs() << " r1 " << format("0x%08" PRIx32, cpu32.r[1]);
9084 outs() << " r2 " << format("0x%08" PRIx32, cpu32.r[2]);
9085 outs() << " r3 " << format("0x%08" PRIx32, cpu32.r[3]) << "\n";
9086 outs() << "\t r4 " << format("0x%08" PRIx32, cpu32.r[4]);
9087 outs() << " r5 " << format("0x%08" PRIx32, cpu32.r[5]);
9088 outs() << " r6 " << format("0x%08" PRIx32, cpu32.r[6]);
9089 outs() << " r7 " << format("0x%08" PRIx32, cpu32.r[7]) << "\n";
9090 outs() << "\t r8 " << format("0x%08" PRIx32, cpu32.r[8]);
9091 outs() << " r9 " << format("0x%08" PRIx32, cpu32.r[9]);
9092 outs() << " r10 " << format("0x%08" PRIx32, cpu32.r[10]);
9093 outs() << " r11 " << format("0x%08" PRIx32, cpu32.r[11]) << "\n";
9094 outs() << "\t r12 " << format("0x%08" PRIx32, cpu32.r[12]);
9095 outs() << " sp " << format("0x%08" PRIx32, cpu32.sp);
9096 outs() << " lr " << format("0x%08" PRIx32, cpu32.lr);
9097 outs() << " pc " << format("0x%08" PRIx32, cpu32.pc) << "\n";
9098 outs() << "\t cpsr " << format("0x%08" PRIx32, cpu32.cpsr) << "\n";
9099}
9100
Kevin Enderby7747cb52016-11-03 20:51:28 +00009101static void Print_arm_thread_state64_t(MachO::arm_thread_state64_t &cpu64) {
9102 outs() << "\t x0 " << format("0x%016" PRIx64, cpu64.x[0]);
9103 outs() << " x1 " << format("0x%016" PRIx64, cpu64.x[1]);
9104 outs() << " x2 " << format("0x%016" PRIx64, cpu64.x[2]) << "\n";
9105 outs() << "\t x3 " << format("0x%016" PRIx64, cpu64.x[3]);
9106 outs() << " x4 " << format("0x%016" PRIx64, cpu64.x[4]);
9107 outs() << " x5 " << format("0x%016" PRIx64, cpu64.x[5]) << "\n";
9108 outs() << "\t x6 " << format("0x%016" PRIx64, cpu64.x[6]);
9109 outs() << " x7 " << format("0x%016" PRIx64, cpu64.x[7]);
9110 outs() << " x8 " << format("0x%016" PRIx64, cpu64.x[8]) << "\n";
9111 outs() << "\t x9 " << format("0x%016" PRIx64, cpu64.x[9]);
9112 outs() << " x10 " << format("0x%016" PRIx64, cpu64.x[10]);
9113 outs() << " x11 " << format("0x%016" PRIx64, cpu64.x[11]) << "\n";
9114 outs() << "\t x12 " << format("0x%016" PRIx64, cpu64.x[12]);
9115 outs() << " x13 " << format("0x%016" PRIx64, cpu64.x[13]);
9116 outs() << " x14 " << format("0x%016" PRIx64, cpu64.x[14]) << "\n";
9117 outs() << "\t x15 " << format("0x%016" PRIx64, cpu64.x[15]);
9118 outs() << " x16 " << format("0x%016" PRIx64, cpu64.x[16]);
9119 outs() << " x17 " << format("0x%016" PRIx64, cpu64.x[17]) << "\n";
9120 outs() << "\t x18 " << format("0x%016" PRIx64, cpu64.x[18]);
9121 outs() << " x19 " << format("0x%016" PRIx64, cpu64.x[19]);
9122 outs() << " x20 " << format("0x%016" PRIx64, cpu64.x[20]) << "\n";
9123 outs() << "\t x21 " << format("0x%016" PRIx64, cpu64.x[21]);
9124 outs() << " x22 " << format("0x%016" PRIx64, cpu64.x[22]);
9125 outs() << " x23 " << format("0x%016" PRIx64, cpu64.x[23]) << "\n";
9126 outs() << "\t x24 " << format("0x%016" PRIx64, cpu64.x[24]);
9127 outs() << " x25 " << format("0x%016" PRIx64, cpu64.x[25]);
9128 outs() << " x26 " << format("0x%016" PRIx64, cpu64.x[26]) << "\n";
9129 outs() << "\t x27 " << format("0x%016" PRIx64, cpu64.x[27]);
9130 outs() << " x28 " << format("0x%016" PRIx64, cpu64.x[28]);
9131 outs() << " fp " << format("0x%016" PRIx64, cpu64.fp) << "\n";
9132 outs() << "\t lr " << format("0x%016" PRIx64, cpu64.lr);
9133 outs() << " sp " << format("0x%016" PRIx64, cpu64.sp);
9134 outs() << " pc " << format("0x%016" PRIx64, cpu64.pc) << "\n";
9135 outs() << "\t cpsr " << format("0x%08" PRIx32, cpu64.cpsr) << "\n";
9136}
9137
Kevin Enderby48ef5342014-12-23 22:56:39 +00009138static void PrintThreadCommand(MachO::thread_command t, const char *Ptr,
9139 bool isLittleEndian, uint32_t cputype) {
9140 if (t.cmd == MachO::LC_THREAD)
9141 outs() << " cmd LC_THREAD\n";
9142 else if (t.cmd == MachO::LC_UNIXTHREAD)
9143 outs() << " cmd LC_UNIXTHREAD\n";
9144 else
9145 outs() << " cmd " << t.cmd << " (unknown)\n";
9146 outs() << " cmdsize " << t.cmdsize;
9147 if (t.cmdsize < sizeof(struct MachO::thread_command) + 2 * sizeof(uint32_t))
9148 outs() << " Incorrect size\n";
9149 else
9150 outs() << "\n";
9151
9152 const char *begin = Ptr + sizeof(struct MachO::thread_command);
9153 const char *end = Ptr + t.cmdsize;
9154 uint32_t flavor, count, left;
Kevin Enderbyc3a035d2017-01-23 21:13:29 +00009155 if (cputype == MachO::CPU_TYPE_I386) {
9156 while (begin < end) {
9157 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9158 memcpy((char *)&flavor, begin, sizeof(uint32_t));
9159 begin += sizeof(uint32_t);
9160 } else {
9161 flavor = 0;
9162 begin = end;
9163 }
9164 if (isLittleEndian != sys::IsLittleEndianHost)
9165 sys::swapByteOrder(flavor);
9166 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9167 memcpy((char *)&count, begin, sizeof(uint32_t));
9168 begin += sizeof(uint32_t);
9169 } else {
9170 count = 0;
9171 begin = end;
9172 }
9173 if (isLittleEndian != sys::IsLittleEndianHost)
9174 sys::swapByteOrder(count);
9175 if (flavor == MachO::x86_THREAD_STATE32) {
9176 outs() << " flavor i386_THREAD_STATE\n";
9177 if (count == MachO::x86_THREAD_STATE32_COUNT)
9178 outs() << " count i386_THREAD_STATE_COUNT\n";
9179 else
9180 outs() << " count " << count
9181 << " (not x86_THREAD_STATE32_COUNT)\n";
9182 MachO::x86_thread_state32_t cpu32;
9183 left = end - begin;
9184 if (left >= sizeof(MachO::x86_thread_state32_t)) {
9185 memcpy(&cpu32, begin, sizeof(MachO::x86_thread_state32_t));
9186 begin += sizeof(MachO::x86_thread_state32_t);
9187 } else {
9188 memset(&cpu32, '\0', sizeof(MachO::x86_thread_state32_t));
9189 memcpy(&cpu32, begin, left);
9190 begin += left;
9191 }
9192 if (isLittleEndian != sys::IsLittleEndianHost)
9193 swapStruct(cpu32);
9194 Print_x86_thread_state32_t(cpu32);
9195 } else if (flavor == MachO::x86_THREAD_STATE) {
9196 outs() << " flavor x86_THREAD_STATE\n";
9197 if (count == MachO::x86_THREAD_STATE_COUNT)
9198 outs() << " count x86_THREAD_STATE_COUNT\n";
9199 else
9200 outs() << " count " << count
9201 << " (not x86_THREAD_STATE_COUNT)\n";
9202 struct MachO::x86_thread_state_t ts;
9203 left = end - begin;
9204 if (left >= sizeof(MachO::x86_thread_state_t)) {
9205 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
9206 begin += sizeof(MachO::x86_thread_state_t);
9207 } else {
9208 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
9209 memcpy(&ts, begin, left);
9210 begin += left;
9211 }
9212 if (isLittleEndian != sys::IsLittleEndianHost)
9213 swapStruct(ts);
9214 if (ts.tsh.flavor == MachO::x86_THREAD_STATE32) {
9215 outs() << "\t tsh.flavor x86_THREAD_STATE32 ";
9216 if (ts.tsh.count == MachO::x86_THREAD_STATE32_COUNT)
9217 outs() << "tsh.count x86_THREAD_STATE32_COUNT\n";
9218 else
9219 outs() << "tsh.count " << ts.tsh.count
9220 << " (not x86_THREAD_STATE32_COUNT\n";
9221 Print_x86_thread_state32_t(ts.uts.ts32);
9222 } else {
9223 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
9224 << ts.tsh.count << "\n";
9225 }
9226 } else {
9227 outs() << " flavor " << flavor << " (unknown)\n";
9228 outs() << " count " << count << "\n";
9229 outs() << " state (unknown)\n";
9230 begin += count * sizeof(uint32_t);
9231 }
9232 }
9233 } else if (cputype == MachO::CPU_TYPE_X86_64) {
Kevin Enderby48ef5342014-12-23 22:56:39 +00009234 while (begin < end) {
9235 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9236 memcpy((char *)&flavor, begin, sizeof(uint32_t));
9237 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00009238 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00009239 flavor = 0;
9240 begin = end;
9241 }
9242 if (isLittleEndian != sys::IsLittleEndianHost)
9243 sys::swapByteOrder(flavor);
9244 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9245 memcpy((char *)&count, begin, sizeof(uint32_t));
9246 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00009247 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00009248 count = 0;
9249 begin = end;
9250 }
9251 if (isLittleEndian != sys::IsLittleEndianHost)
9252 sys::swapByteOrder(count);
9253 if (flavor == MachO::x86_THREAD_STATE64) {
9254 outs() << " flavor x86_THREAD_STATE64\n";
9255 if (count == MachO::x86_THREAD_STATE64_COUNT)
9256 outs() << " count x86_THREAD_STATE64_COUNT\n";
9257 else
9258 outs() << " count " << count
9259 << " (not x86_THREAD_STATE64_COUNT)\n";
9260 MachO::x86_thread_state64_t cpu64;
9261 left = end - begin;
9262 if (left >= sizeof(MachO::x86_thread_state64_t)) {
9263 memcpy(&cpu64, begin, sizeof(MachO::x86_thread_state64_t));
9264 begin += sizeof(MachO::x86_thread_state64_t);
9265 } else {
9266 memset(&cpu64, '\0', sizeof(MachO::x86_thread_state64_t));
9267 memcpy(&cpu64, begin, left);
9268 begin += left;
9269 }
9270 if (isLittleEndian != sys::IsLittleEndianHost)
9271 swapStruct(cpu64);
9272 Print_x86_thread_state64_t(cpu64);
9273 } else if (flavor == MachO::x86_THREAD_STATE) {
9274 outs() << " flavor x86_THREAD_STATE\n";
9275 if (count == MachO::x86_THREAD_STATE_COUNT)
9276 outs() << " count x86_THREAD_STATE_COUNT\n";
9277 else
9278 outs() << " count " << count
9279 << " (not x86_THREAD_STATE_COUNT)\n";
9280 struct MachO::x86_thread_state_t ts;
9281 left = end - begin;
9282 if (left >= sizeof(MachO::x86_thread_state_t)) {
9283 memcpy(&ts, begin, sizeof(MachO::x86_thread_state_t));
9284 begin += sizeof(MachO::x86_thread_state_t);
9285 } else {
9286 memset(&ts, '\0', sizeof(MachO::x86_thread_state_t));
9287 memcpy(&ts, begin, left);
9288 begin += left;
9289 }
9290 if (isLittleEndian != sys::IsLittleEndianHost)
9291 swapStruct(ts);
9292 if (ts.tsh.flavor == MachO::x86_THREAD_STATE64) {
9293 outs() << "\t tsh.flavor x86_THREAD_STATE64 ";
9294 if (ts.tsh.count == MachO::x86_THREAD_STATE64_COUNT)
9295 outs() << "tsh.count x86_THREAD_STATE64_COUNT\n";
9296 else
9297 outs() << "tsh.count " << ts.tsh.count
9298 << " (not x86_THREAD_STATE64_COUNT\n";
9299 Print_x86_thread_state64_t(ts.uts.ts64);
9300 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00009301 outs() << "\t tsh.flavor " << ts.tsh.flavor << " tsh.count "
9302 << ts.tsh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00009303 }
9304 } else if (flavor == MachO::x86_FLOAT_STATE) {
9305 outs() << " flavor x86_FLOAT_STATE\n";
9306 if (count == MachO::x86_FLOAT_STATE_COUNT)
9307 outs() << " count x86_FLOAT_STATE_COUNT\n";
9308 else
Kevin Enderby66d51fc2015-01-08 00:25:24 +00009309 outs() << " count " << count << " (not x86_FLOAT_STATE_COUNT)\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00009310 struct MachO::x86_float_state_t fs;
9311 left = end - begin;
9312 if (left >= sizeof(MachO::x86_float_state_t)) {
9313 memcpy(&fs, begin, sizeof(MachO::x86_float_state_t));
9314 begin += sizeof(MachO::x86_float_state_t);
9315 } else {
9316 memset(&fs, '\0', sizeof(MachO::x86_float_state_t));
9317 memcpy(&fs, begin, left);
9318 begin += left;
9319 }
9320 if (isLittleEndian != sys::IsLittleEndianHost)
9321 swapStruct(fs);
9322 if (fs.fsh.flavor == MachO::x86_FLOAT_STATE64) {
9323 outs() << "\t fsh.flavor x86_FLOAT_STATE64 ";
Kevin Enderby66d51fc2015-01-08 00:25:24 +00009324 if (fs.fsh.count == MachO::x86_FLOAT_STATE64_COUNT)
Kevin Enderby48ef5342014-12-23 22:56:39 +00009325 outs() << "fsh.count x86_FLOAT_STATE64_COUNT\n";
9326 else
9327 outs() << "fsh.count " << fs.fsh.count
9328 << " (not x86_FLOAT_STATE64_COUNT\n";
9329 Print_x86_float_state_t(fs.ufs.fs64);
9330 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00009331 outs() << "\t fsh.flavor " << fs.fsh.flavor << " fsh.count "
9332 << fs.fsh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00009333 }
9334 } else if (flavor == MachO::x86_EXCEPTION_STATE) {
9335 outs() << " flavor x86_EXCEPTION_STATE\n";
9336 if (count == MachO::x86_EXCEPTION_STATE_COUNT)
9337 outs() << " count x86_EXCEPTION_STATE_COUNT\n";
9338 else
Kevin Enderby66d51fc2015-01-08 00:25:24 +00009339 outs() << " count " << count
Kevin Enderby48ef5342014-12-23 22:56:39 +00009340 << " (not x86_EXCEPTION_STATE_COUNT)\n";
9341 struct MachO::x86_exception_state_t es;
9342 left = end - begin;
9343 if (left >= sizeof(MachO::x86_exception_state_t)) {
9344 memcpy(&es, begin, sizeof(MachO::x86_exception_state_t));
9345 begin += sizeof(MachO::x86_exception_state_t);
9346 } else {
9347 memset(&es, '\0', sizeof(MachO::x86_exception_state_t));
9348 memcpy(&es, begin, left);
9349 begin += left;
9350 }
9351 if (isLittleEndian != sys::IsLittleEndianHost)
9352 swapStruct(es);
9353 if (es.esh.flavor == MachO::x86_EXCEPTION_STATE64) {
9354 outs() << "\t esh.flavor x86_EXCEPTION_STATE64\n";
Kevin Enderby66d51fc2015-01-08 00:25:24 +00009355 if (es.esh.count == MachO::x86_EXCEPTION_STATE64_COUNT)
Kevin Enderby48ef5342014-12-23 22:56:39 +00009356 outs() << "\t esh.count x86_EXCEPTION_STATE64_COUNT\n";
9357 else
9358 outs() << "\t esh.count " << es.esh.count
9359 << " (not x86_EXCEPTION_STATE64_COUNT\n";
9360 Print_x86_exception_state_t(es.ues.es64);
9361 } else {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00009362 outs() << "\t esh.flavor " << es.esh.flavor << " esh.count "
9363 << es.esh.count << "\n";
Kevin Enderby48ef5342014-12-23 22:56:39 +00009364 }
Kevin Enderby299cd892018-03-08 23:10:38 +00009365 } else if (flavor == MachO::x86_EXCEPTION_STATE64) {
9366 outs() << " flavor x86_EXCEPTION_STATE64\n";
9367 if (count == MachO::x86_EXCEPTION_STATE64_COUNT)
9368 outs() << " count x86_EXCEPTION_STATE64_COUNT\n";
9369 else
9370 outs() << " count " << count
9371 << " (not x86_EXCEPTION_STATE64_COUNT)\n";
9372 struct MachO::x86_exception_state64_t es64;
9373 left = end - begin;
9374 if (left >= sizeof(MachO::x86_exception_state64_t)) {
9375 memcpy(&es64, begin, sizeof(MachO::x86_exception_state64_t));
9376 begin += sizeof(MachO::x86_exception_state64_t);
9377 } else {
9378 memset(&es64, '\0', sizeof(MachO::x86_exception_state64_t));
9379 memcpy(&es64, begin, left);
9380 begin += left;
9381 }
9382 if (isLittleEndian != sys::IsLittleEndianHost)
9383 swapStruct(es64);
9384 Print_x86_exception_state_t(es64);
Kevin Enderby48ef5342014-12-23 22:56:39 +00009385 } else {
9386 outs() << " flavor " << flavor << " (unknown)\n";
9387 outs() << " count " << count << "\n";
9388 outs() << " state (unknown)\n";
9389 begin += count * sizeof(uint32_t);
9390 }
9391 }
Kevin Enderby41c9c002016-10-21 18:22:35 +00009392 } else if (cputype == MachO::CPU_TYPE_ARM) {
9393 while (begin < end) {
9394 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9395 memcpy((char *)&flavor, begin, sizeof(uint32_t));
9396 begin += sizeof(uint32_t);
9397 } else {
9398 flavor = 0;
9399 begin = end;
9400 }
9401 if (isLittleEndian != sys::IsLittleEndianHost)
9402 sys::swapByteOrder(flavor);
9403 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9404 memcpy((char *)&count, begin, sizeof(uint32_t));
9405 begin += sizeof(uint32_t);
9406 } else {
9407 count = 0;
9408 begin = end;
9409 }
9410 if (isLittleEndian != sys::IsLittleEndianHost)
9411 sys::swapByteOrder(count);
9412 if (flavor == MachO::ARM_THREAD_STATE) {
9413 outs() << " flavor ARM_THREAD_STATE\n";
9414 if (count == MachO::ARM_THREAD_STATE_COUNT)
9415 outs() << " count ARM_THREAD_STATE_COUNT\n";
9416 else
9417 outs() << " count " << count
9418 << " (not ARM_THREAD_STATE_COUNT)\n";
9419 MachO::arm_thread_state32_t cpu32;
9420 left = end - begin;
9421 if (left >= sizeof(MachO::arm_thread_state32_t)) {
9422 memcpy(&cpu32, begin, sizeof(MachO::arm_thread_state32_t));
9423 begin += sizeof(MachO::arm_thread_state32_t);
9424 } else {
9425 memset(&cpu32, '\0', sizeof(MachO::arm_thread_state32_t));
9426 memcpy(&cpu32, begin, left);
9427 begin += left;
9428 }
9429 if (isLittleEndian != sys::IsLittleEndianHost)
9430 swapStruct(cpu32);
9431 Print_arm_thread_state32_t(cpu32);
9432 } else {
9433 outs() << " flavor " << flavor << " (unknown)\n";
9434 outs() << " count " << count << "\n";
9435 outs() << " state (unknown)\n";
9436 begin += count * sizeof(uint32_t);
9437 }
9438 }
Kevin Enderby7747cb52016-11-03 20:51:28 +00009439 } else if (cputype == MachO::CPU_TYPE_ARM64) {
9440 while (begin < end) {
9441 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9442 memcpy((char *)&flavor, begin, sizeof(uint32_t));
9443 begin += sizeof(uint32_t);
9444 } else {
9445 flavor = 0;
9446 begin = end;
9447 }
9448 if (isLittleEndian != sys::IsLittleEndianHost)
9449 sys::swapByteOrder(flavor);
9450 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9451 memcpy((char *)&count, begin, sizeof(uint32_t));
9452 begin += sizeof(uint32_t);
9453 } else {
9454 count = 0;
9455 begin = end;
9456 }
9457 if (isLittleEndian != sys::IsLittleEndianHost)
9458 sys::swapByteOrder(count);
9459 if (flavor == MachO::ARM_THREAD_STATE64) {
9460 outs() << " flavor ARM_THREAD_STATE64\n";
9461 if (count == MachO::ARM_THREAD_STATE64_COUNT)
9462 outs() << " count ARM_THREAD_STATE64_COUNT\n";
9463 else
9464 outs() << " count " << count
9465 << " (not ARM_THREAD_STATE64_COUNT)\n";
9466 MachO::arm_thread_state64_t cpu64;
9467 left = end - begin;
9468 if (left >= sizeof(MachO::arm_thread_state64_t)) {
9469 memcpy(&cpu64, begin, sizeof(MachO::arm_thread_state64_t));
9470 begin += sizeof(MachO::arm_thread_state64_t);
9471 } else {
9472 memset(&cpu64, '\0', sizeof(MachO::arm_thread_state64_t));
9473 memcpy(&cpu64, begin, left);
9474 begin += left;
9475 }
9476 if (isLittleEndian != sys::IsLittleEndianHost)
9477 swapStruct(cpu64);
9478 Print_arm_thread_state64_t(cpu64);
9479 } else {
9480 outs() << " flavor " << flavor << " (unknown)\n";
9481 outs() << " count " << count << "\n";
9482 outs() << " state (unknown)\n";
9483 begin += count * sizeof(uint32_t);
9484 }
9485 }
Kevin Enderby48ef5342014-12-23 22:56:39 +00009486 } else {
9487 while (begin < end) {
9488 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9489 memcpy((char *)&flavor, begin, sizeof(uint32_t));
9490 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00009491 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00009492 flavor = 0;
9493 begin = end;
9494 }
9495 if (isLittleEndian != sys::IsLittleEndianHost)
9496 sys::swapByteOrder(flavor);
9497 if (end - begin > (ptrdiff_t)sizeof(uint32_t)) {
9498 memcpy((char *)&count, begin, sizeof(uint32_t));
9499 begin += sizeof(uint32_t);
Kevin Enderby66d51fc2015-01-08 00:25:24 +00009500 } else {
Kevin Enderby48ef5342014-12-23 22:56:39 +00009501 count = 0;
9502 begin = end;
9503 }
9504 if (isLittleEndian != sys::IsLittleEndianHost)
9505 sys::swapByteOrder(count);
9506 outs() << " flavor " << flavor << "\n";
9507 outs() << " count " << count << "\n";
9508 outs() << " state (Unknown cputype/cpusubtype)\n";
9509 begin += count * sizeof(uint32_t);
9510 }
9511 }
9512}
Kevin Enderby66d51fc2015-01-08 00:25:24 +00009513
Kevin Enderby8ae63c12014-09-04 16:54:47 +00009514static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) {
9515 if (dl.cmd == MachO::LC_ID_DYLIB)
9516 outs() << " cmd LC_ID_DYLIB\n";
9517 else if (dl.cmd == MachO::LC_LOAD_DYLIB)
9518 outs() << " cmd LC_LOAD_DYLIB\n";
9519 else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB)
9520 outs() << " cmd LC_LOAD_WEAK_DYLIB\n";
9521 else if (dl.cmd == MachO::LC_REEXPORT_DYLIB)
9522 outs() << " cmd LC_REEXPORT_DYLIB\n";
9523 else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB)
9524 outs() << " cmd LC_LAZY_LOAD_DYLIB\n";
9525 else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
9526 outs() << " cmd LC_LOAD_UPWARD_DYLIB\n";
9527 else
9528 outs() << " cmd " << dl.cmd << " (unknown)\n";
9529 outs() << " cmdsize " << dl.cmdsize;
9530 if (dl.cmdsize < sizeof(struct MachO::dylib_command))
9531 outs() << " Incorrect size\n";
9532 else
9533 outs() << "\n";
9534 if (dl.dylib.name < dl.cmdsize) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009535 const char *P = (const char *)(Ptr) + dl.dylib.name;
Kevin Enderby8ae63c12014-09-04 16:54:47 +00009536 outs() << " name " << P << " (offset " << dl.dylib.name << ")\n";
9537 } else {
9538 outs() << " name ?(bad offset " << dl.dylib.name << ")\n";
9539 }
9540 outs() << " time stamp " << dl.dylib.timestamp << " ";
9541 time_t t = dl.dylib.timestamp;
9542 outs() << ctime(&t);
9543 outs() << " current version ";
9544 if (dl.dylib.current_version == 0xffffffff)
9545 outs() << "n/a\n";
9546 else
9547 outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "."
9548 << ((dl.dylib.current_version >> 8) & 0xff) << "."
9549 << (dl.dylib.current_version & 0xff) << "\n";
9550 outs() << "compatibility version ";
9551 if (dl.dylib.compatibility_version == 0xffffffff)
9552 outs() << "n/a\n";
9553 else
9554 outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
9555 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
9556 << (dl.dylib.compatibility_version & 0xff) << "\n";
9557}
9558
9559static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
9560 uint32_t object_size) {
9561 if (ld.cmd == MachO::LC_CODE_SIGNATURE)
Kevin Enderby1be37a32016-04-28 21:07:20 +00009562 outs() << " cmd LC_CODE_SIGNATURE\n";
Kevin Enderby8ae63c12014-09-04 16:54:47 +00009563 else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO)
9564 outs() << " cmd LC_SEGMENT_SPLIT_INFO\n";
9565 else if (ld.cmd == MachO::LC_FUNCTION_STARTS)
9566 outs() << " cmd LC_FUNCTION_STARTS\n";
9567 else if (ld.cmd == MachO::LC_DATA_IN_CODE)
9568 outs() << " cmd LC_DATA_IN_CODE\n";
9569 else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS)
9570 outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n";
9571 else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT)
9572 outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n";
9573 else
9574 outs() << " cmd " << ld.cmd << " (?)\n";
9575 outs() << " cmdsize " << ld.cmdsize;
9576 if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command))
9577 outs() << " Incorrect size\n";
9578 else
9579 outs() << "\n";
9580 outs() << " dataoff " << ld.dataoff;
9581 if (ld.dataoff > object_size)
9582 outs() << " (past end of file)\n";
9583 else
9584 outs() << "\n";
9585 outs() << " datasize " << ld.datasize;
9586 uint64_t big_size = ld.dataoff;
9587 big_size += ld.datasize;
9588 if (big_size > object_size)
9589 outs() << " (past end of file)\n";
9590 else
9591 outs() << "\n";
9592}
9593
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00009594static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t filetype,
9595 uint32_t cputype, bool verbose) {
Kevin Enderby956366c2014-08-29 22:30:52 +00009596 StringRef Buf = Obj->getData();
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00009597 unsigned Index = 0;
9598 for (const auto &Command : Obj->load_commands()) {
9599 outs() << "Load command " << Index++ << "\n";
Kevin Enderby956366c2014-08-29 22:30:52 +00009600 if (Command.C.cmd == MachO::LC_SEGMENT) {
9601 MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
9602 const char *sg_segname = SLC.segname;
9603 PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr,
9604 SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot,
9605 SLC.initprot, SLC.nsects, SLC.flags, Buf.size(),
9606 verbose);
9607 for (unsigned j = 0; j < SLC.nsects; j++) {
Kevin Enderbyc9713382014-12-16 01:14:45 +00009608 MachO::section S = Obj->getSection(Command, j);
Kevin Enderby956366c2014-08-29 22:30:52 +00009609 PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align,
9610 S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2,
9611 SLC.cmd, sg_segname, filetype, Buf.size(), verbose);
9612 }
9613 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
9614 MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command);
9615 const char *sg_segname = SLC_64.segname;
9616 PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname,
9617 SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff,
9618 SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot,
9619 SLC_64.nsects, SLC_64.flags, Buf.size(), verbose);
9620 for (unsigned j = 0; j < SLC_64.nsects; j++) {
9621 MachO::section_64 S_64 = Obj->getSection64(Command, j);
9622 PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size,
9623 S_64.offset, S_64.align, S_64.reloff, S_64.nreloc,
9624 S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd,
9625 sg_segname, filetype, Buf.size(), verbose);
9626 }
9627 } else if (Command.C.cmd == MachO::LC_SYMTAB) {
9628 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
David Majnemer73cc6ff2014-11-13 19:48:56 +00009629 PrintSymtabLoadCommand(Symtab, Obj->is64Bit(), Buf.size());
Kevin Enderby956366c2014-08-29 22:30:52 +00009630 } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
9631 MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
9632 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
David Majnemer73cc6ff2014-11-13 19:48:56 +00009633 PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(),
9634 Obj->is64Bit());
Kevin Enderby8ae63c12014-09-04 16:54:47 +00009635 } else if (Command.C.cmd == MachO::LC_DYLD_INFO ||
9636 Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
9637 MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command);
9638 PrintDyldInfoLoadCommand(DyldInfo, Buf.size());
9639 } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER ||
9640 Command.C.cmd == MachO::LC_ID_DYLINKER ||
9641 Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
9642 MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command);
9643 PrintDyldLoadCommand(Dyld, Command.Ptr);
9644 } else if (Command.C.cmd == MachO::LC_UUID) {
9645 MachO::uuid_command Uuid = Obj->getUuidCommand(Command);
9646 PrintUuidLoadCommand(Uuid);
Jean-Daniel Dupas00cc1f52014-12-04 07:37:02 +00009647 } else if (Command.C.cmd == MachO::LC_RPATH) {
9648 MachO::rpath_command Rpath = Obj->getRpathCommand(Command);
9649 PrintRpathLoadCommand(Rpath, Command.Ptr);
Kevin Enderby1ff0ecc2014-12-16 21:48:27 +00009650 } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX ||
Tim Northoverbfbfb122015-11-02 21:26:58 +00009651 Command.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS ||
9652 Command.C.cmd == MachO::LC_VERSION_MIN_TVOS ||
9653 Command.C.cmd == MachO::LC_VERSION_MIN_WATCHOS) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00009654 MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command);
9655 PrintVersionMinLoadCommand(Vd);
Kevin Enderbya4579c42017-01-19 17:36:31 +00009656 } else if (Command.C.cmd == MachO::LC_NOTE) {
9657 MachO::note_command Nt = Obj->getNoteLoadCommand(Command);
9658 PrintNoteLoadCommand(Nt);
Steven Wu5b54a422017-01-23 20:07:55 +00009659 } else if (Command.C.cmd == MachO::LC_BUILD_VERSION) {
9660 MachO::build_version_command Bv =
9661 Obj->getBuildVersionLoadCommand(Command);
9662 PrintBuildVersionLoadCommand(Obj, Bv);
Kevin Enderby8ae63c12014-09-04 16:54:47 +00009663 } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) {
9664 MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command);
9665 PrintSourceVersionCommand(Sd);
9666 } else if (Command.C.cmd == MachO::LC_MAIN) {
9667 MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command);
9668 PrintEntryPointCommand(Ep);
Kevin Enderby0804f4672014-12-16 23:25:52 +00009669 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00009670 MachO::encryption_info_command Ei =
9671 Obj->getEncryptionInfoCommand(Command);
Kevin Enderby0804f4672014-12-16 23:25:52 +00009672 PrintEncryptionInfoCommand(Ei, Buf.size());
Kevin Enderby57538292014-12-17 01:01:30 +00009673 } else if (Command.C.cmd == MachO::LC_ENCRYPTION_INFO_64) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00009674 MachO::encryption_info_command_64 Ei =
9675 Obj->getEncryptionInfoCommand64(Command);
Kevin Enderby57538292014-12-17 01:01:30 +00009676 PrintEncryptionInfoCommand64(Ei, Buf.size());
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00009677 } else if (Command.C.cmd == MachO::LC_LINKER_OPTION) {
Kevin Enderby66d51fc2015-01-08 00:25:24 +00009678 MachO::linker_option_command Lo =
9679 Obj->getLinkerOptionLoadCommand(Command);
Kevin Enderbyd0b6b7f2014-12-18 00:53:40 +00009680 PrintLinkerOptionCommand(Lo, Command.Ptr);
Kevin Enderbyb4b79312014-12-18 19:24:35 +00009681 } else if (Command.C.cmd == MachO::LC_SUB_FRAMEWORK) {
9682 MachO::sub_framework_command Sf = Obj->getSubFrameworkCommand(Command);
9683 PrintSubFrameworkCommand(Sf, Command.Ptr);
Kevin Enderbya2bd8d92014-12-18 23:13:26 +00009684 } else if (Command.C.cmd == MachO::LC_SUB_UMBRELLA) {
9685 MachO::sub_umbrella_command Sf = Obj->getSubUmbrellaCommand(Command);
9686 PrintSubUmbrellaCommand(Sf, Command.Ptr);
Kevin Enderby36c8d3a2014-12-19 19:48:16 +00009687 } else if (Command.C.cmd == MachO::LC_SUB_LIBRARY) {
9688 MachO::sub_library_command Sl = Obj->getSubLibraryCommand(Command);
9689 PrintSubLibraryCommand(Sl, Command.Ptr);
Kevin Enderby186eac32014-12-19 21:06:24 +00009690 } else if (Command.C.cmd == MachO::LC_SUB_CLIENT) {
9691 MachO::sub_client_command Sc = Obj->getSubClientCommand(Command);
9692 PrintSubClientCommand(Sc, Command.Ptr);
Kevin Enderby52e4ce42014-12-19 22:25:22 +00009693 } else if (Command.C.cmd == MachO::LC_ROUTINES) {
9694 MachO::routines_command Rc = Obj->getRoutinesCommand(Command);
9695 PrintRoutinesCommand(Rc);
9696 } else if (Command.C.cmd == MachO::LC_ROUTINES_64) {
9697 MachO::routines_command_64 Rc = Obj->getRoutinesCommand64(Command);
9698 PrintRoutinesCommand64(Rc);
Kevin Enderby48ef5342014-12-23 22:56:39 +00009699 } else if (Command.C.cmd == MachO::LC_THREAD ||
9700 Command.C.cmd == MachO::LC_UNIXTHREAD) {
9701 MachO::thread_command Tc = Obj->getThreadCommand(Command);
9702 PrintThreadCommand(Tc, Command.Ptr, Obj->isLittleEndian(), cputype);
Nick Kledzik15558912014-10-16 18:58:20 +00009703 } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
9704 Command.C.cmd == MachO::LC_ID_DYLIB ||
9705 Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
9706 Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
9707 Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
9708 Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
Kevin Enderby8ae63c12014-09-04 16:54:47 +00009709 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
9710 PrintDylibCommand(Dl, Command.Ptr);
9711 } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE ||
9712 Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO ||
9713 Command.C.cmd == MachO::LC_FUNCTION_STARTS ||
9714 Command.C.cmd == MachO::LC_DATA_IN_CODE ||
9715 Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS ||
9716 Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
9717 MachO::linkedit_data_command Ld =
9718 Obj->getLinkeditDataLoadCommand(Command);
9719 PrintLinkEditDataCommand(Ld, Buf.size());
Kevin Enderby956366c2014-08-29 22:30:52 +00009720 } else {
9721 outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd)
9722 << ")\n";
9723 outs() << " cmdsize " << Command.C.cmdsize << "\n";
9724 // TODO: get and print the raw bytes of the load command.
9725 }
9726 // TODO: print all the other kinds of load commands.
Kevin Enderby956366c2014-08-29 22:30:52 +00009727 }
9728}
9729
Kevin Enderby0ae163f2016-01-13 00:25:36 +00009730static void PrintMachHeader(const MachOObjectFile *Obj, bool verbose) {
Kevin Enderbyb76d3862014-08-22 20:35:18 +00009731 if (Obj->is64Bit()) {
9732 MachO::mach_header_64 H_64;
9733 H_64 = Obj->getHeader64();
9734 PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
9735 H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
9736 } else {
9737 MachO::mach_header H;
9738 H = Obj->getHeader();
9739 PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
9740 H.sizeofcmds, H.flags, verbose);
9741 }
9742}
9743
9744void llvm::printMachOFileHeader(const object::ObjectFile *Obj) {
9745 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
Kevin Enderby0ae163f2016-01-13 00:25:36 +00009746 PrintMachHeader(file, !NonVerbose);
9747}
9748
9749void llvm::printMachOLoadCommands(const object::ObjectFile *Obj) {
9750 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
Kevin Enderby956366c2014-08-29 22:30:52 +00009751 uint32_t filetype = 0;
9752 uint32_t cputype = 0;
Kevin Enderby0ae163f2016-01-13 00:25:36 +00009753 if (file->is64Bit()) {
9754 MachO::mach_header_64 H_64;
9755 H_64 = file->getHeader64();
9756 filetype = H_64.filetype;
9757 cputype = H_64.cputype;
9758 } else {
9759 MachO::mach_header H;
9760 H = file->getHeader();
9761 filetype = H.filetype;
9762 cputype = H.cputype;
9763 }
Alexey Samsonovd319c4f2015-06-03 22:19:36 +00009764 PrintLoadCommands(file, filetype, cputype, !NonVerbose);
Kevin Enderbyb76d3862014-08-22 20:35:18 +00009765}
Nick Kledzikd04bc352014-08-30 00:20:14 +00009766
9767//===----------------------------------------------------------------------===//
9768// export trie dumping
9769//===----------------------------------------------------------------------===//
9770
9771void llvm::printMachOExportsTrie(const object::MachOObjectFile *Obj) {
Kevin Enderby0d5ec112017-06-19 21:23:07 +00009772 uint64_t BaseSegmentAddress = 0;
9773 for (const auto &Command : Obj->load_commands()) {
9774 if (Command.C.cmd == MachO::LC_SEGMENT) {
9775 MachO::segment_command Seg = Obj->getSegmentLoadCommand(Command);
9776 if (Seg.fileoff == 0 && Seg.filesize != 0) {
9777 BaseSegmentAddress = Seg.vmaddr;
9778 break;
9779 }
9780 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
9781 MachO::segment_command_64 Seg = Obj->getSegment64LoadCommand(Command);
9782 if (Seg.fileoff == 0 && Seg.filesize != 0) {
9783 BaseSegmentAddress = Seg.vmaddr;
9784 break;
9785 }
9786 }
9787 }
Kevin Enderby3e95bd22017-07-20 23:08:41 +00009788 Error Err = Error::success();
Alexander Shaposhnikove5740342017-07-29 00:30:45 +00009789 for (const llvm::object::ExportEntry &Entry : Obj->exports(Err)) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009790 uint64_t Flags = Entry.flags();
Nick Kledzikd04bc352014-08-30 00:20:14 +00009791 bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
9792 bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
9793 bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
9794 MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL);
9795 bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
9796 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
9797 bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
9798 if (ReExport)
9799 outs() << "[re-export] ";
9800 else
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009801 outs() << format("0x%08llX ",
Kevin Enderby0d5ec112017-06-19 21:23:07 +00009802 Entry.address() + BaseSegmentAddress);
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009803 outs() << Entry.name();
Nick Kledzikd04bc352014-08-30 00:20:14 +00009804 if (WeakDef || ThreadLocal || Resolver || Abs) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009805 bool NeedsComma = false;
Nick Kledzik1d1ac4b2014-09-03 01:12:52 +00009806 outs() << " [";
Nick Kledzikd04bc352014-08-30 00:20:14 +00009807 if (WeakDef) {
9808 outs() << "weak_def";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009809 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00009810 }
9811 if (ThreadLocal) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009812 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00009813 outs() << ", ";
9814 outs() << "per-thread";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009815 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00009816 }
9817 if (Abs) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009818 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00009819 outs() << ", ";
9820 outs() << "absolute";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009821 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00009822 }
9823 if (Resolver) {
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009824 if (NeedsComma)
Nick Kledzikd04bc352014-08-30 00:20:14 +00009825 outs() << ", ";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009826 outs() << format("resolver=0x%08llX", Entry.other());
9827 NeedsComma = true;
Nick Kledzikd04bc352014-08-30 00:20:14 +00009828 }
9829 outs() << "]";
9830 }
9831 if (ReExport) {
9832 StringRef DylibName = "unknown";
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009833 int Ordinal = Entry.other() - 1;
9834 Obj->getLibraryShortNameByIndex(Ordinal, DylibName);
9835 if (Entry.otherName().empty())
Nick Kledzikd04bc352014-08-30 00:20:14 +00009836 outs() << " (from " << DylibName << ")";
9837 else
Nick Kledzikac7cbdc2014-09-02 18:50:24 +00009838 outs() << " (" << Entry.otherName() << " from " << DylibName << ")";
Nick Kledzikd04bc352014-08-30 00:20:14 +00009839 }
9840 outs() << "\n";
9841 }
Kevin Enderby3e95bd22017-07-20 23:08:41 +00009842 if (Err)
9843 report_error(Obj->getFileName(), std::move(Err));
Nick Kledzikd04bc352014-08-30 00:20:14 +00009844}
Nick Kledzikac431442014-09-12 21:34:15 +00009845
Nick Kledzikac431442014-09-12 21:34:15 +00009846//===----------------------------------------------------------------------===//
9847// rebase table dumping
9848//===----------------------------------------------------------------------===//
9849
Kevin Enderbya8d256c2017-03-20 19:46:55 +00009850void llvm::printMachORebaseTable(object::MachOObjectFile *Obj) {
Nick Kledzikac431442014-09-12 21:34:15 +00009851 outs() << "segment section address type\n";
Kevin Enderbya8d256c2017-03-20 19:46:55 +00009852 Error Err = Error::success();
9853 for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable(Err)) {
9854 StringRef SegmentName = Entry.segmentName();
9855 StringRef SectionName = Entry.sectionName();
9856 uint64_t Address = Entry.address();
Nick Kledzikac431442014-09-12 21:34:15 +00009857
9858 // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009859 outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n",
9860 SegmentName.str().c_str(), SectionName.str().c_str(),
9861 Address, Entry.typeName().str().c_str());
Nick Kledzikac431442014-09-12 21:34:15 +00009862 }
Kevin Enderbya8d256c2017-03-20 19:46:55 +00009863 if (Err)
9864 report_error(Obj->getFileName(), std::move(Err));
Nick Kledzikac431442014-09-12 21:34:15 +00009865}
Nick Kledzik56ebef42014-09-16 01:41:51 +00009866
9867static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
9868 StringRef DylibName;
9869 switch (Ordinal) {
9870 case MachO::BIND_SPECIAL_DYLIB_SELF:
9871 return "this-image";
9872 case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
9873 return "main-executable";
9874 case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
9875 return "flat-namespace";
9876 default:
Nick Kledzikabd29872014-09-16 22:03:13 +00009877 if (Ordinal > 0) {
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009878 std::error_code EC =
9879 Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName);
Nick Kledzikabd29872014-09-16 22:03:13 +00009880 if (EC)
Nick Kledzik51d2c2b2014-10-14 23:29:38 +00009881 return "<<bad library ordinal>>";
Nick Kledzikabd29872014-09-16 22:03:13 +00009882 return DylibName;
9883 }
Nick Kledzik56ebef42014-09-16 01:41:51 +00009884 }
Nick Kledzikabd29872014-09-16 22:03:13 +00009885 return "<<unknown special ordinal>>";
Nick Kledzik56ebef42014-09-16 01:41:51 +00009886}
9887
9888//===----------------------------------------------------------------------===//
9889// bind table dumping
9890//===----------------------------------------------------------------------===//
9891
Kevin Enderbya8d256c2017-03-20 19:46:55 +00009892void llvm::printMachOBindTable(object::MachOObjectFile *Obj) {
Nick Kledzik56ebef42014-09-16 01:41:51 +00009893 // Build table of sections so names can used in final output.
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009894 outs() << "segment section address type "
9895 "addend dylib symbol\n";
Kevin Enderbyfeb63b92017-02-28 21:47:07 +00009896 Error Err = Error::success();
9897 for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable(Err)) {
Kevin Enderbya8d256c2017-03-20 19:46:55 +00009898 StringRef SegmentName = Entry.segmentName();
9899 StringRef SectionName = Entry.sectionName();
9900 uint64_t Address = Entry.address();
Nick Kledzik56ebef42014-09-16 01:41:51 +00009901
9902 // Table lines look like:
9903 // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009904 StringRef Attr;
Nick Kledzik56ebef42014-09-16 01:41:51 +00009905 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT)
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009906 Attr = " (weak_import)";
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009907 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009908 << left_justify(SectionName, 18) << " "
9909 << format_hex(Address, 10, true) << " "
9910 << left_justify(Entry.typeName(), 8) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009911 << format_decimal(Entry.addend(), 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009912 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009913 << Entry.symbolName() << Attr << "\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00009914 }
Kevin Enderbyfeb63b92017-02-28 21:47:07 +00009915 if (Err)
9916 report_error(Obj->getFileName(), std::move(Err));
Nick Kledzik56ebef42014-09-16 01:41:51 +00009917}
9918
9919//===----------------------------------------------------------------------===//
9920// lazy bind table dumping
9921//===----------------------------------------------------------------------===//
9922
Kevin Enderbya8d256c2017-03-20 19:46:55 +00009923void llvm::printMachOLazyBindTable(object::MachOObjectFile *Obj) {
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009924 outs() << "segment section address "
9925 "dylib symbol\n";
Kevin Enderbyfeb63b92017-02-28 21:47:07 +00009926 Error Err = Error::success();
9927 for (const llvm::object::MachOBindEntry &Entry : Obj->lazyBindTable(Err)) {
Kevin Enderbya8d256c2017-03-20 19:46:55 +00009928 StringRef SegmentName = Entry.segmentName();
9929 StringRef SectionName = Entry.sectionName();
9930 uint64_t Address = Entry.address();
Nick Kledzik56ebef42014-09-16 01:41:51 +00009931
9932 // Table lines look like:
9933 // __DATA __got 0x00012010 libSystem ___stack_chk_guard
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009934 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009935 << left_justify(SectionName, 18) << " "
9936 << format_hex(Address, 10, true) << " "
9937 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
Nick Kledzik56ebef42014-09-16 01:41:51 +00009938 << Entry.symbolName() << "\n";
9939 }
Kevin Enderbyfeb63b92017-02-28 21:47:07 +00009940 if (Err)
9941 report_error(Obj->getFileName(), std::move(Err));
Nick Kledzik56ebef42014-09-16 01:41:51 +00009942}
9943
Nick Kledzik56ebef42014-09-16 01:41:51 +00009944//===----------------------------------------------------------------------===//
9945// weak bind table dumping
9946//===----------------------------------------------------------------------===//
9947
Kevin Enderbya8d256c2017-03-20 19:46:55 +00009948void llvm::printMachOWeakBindTable(object::MachOObjectFile *Obj) {
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009949 outs() << "segment section address "
9950 "type addend symbol\n";
Kevin Enderbyfeb63b92017-02-28 21:47:07 +00009951 Error Err = Error::success();
9952 for (const llvm::object::MachOBindEntry &Entry : Obj->weakBindTable(Err)) {
Nick Kledzik56ebef42014-09-16 01:41:51 +00009953 // Strong symbols don't have a location to update.
9954 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009955 outs() << " strong "
Nick Kledzik56ebef42014-09-16 01:41:51 +00009956 << Entry.symbolName() << "\n";
9957 continue;
9958 }
Kevin Enderbya8d256c2017-03-20 19:46:55 +00009959 StringRef SegmentName = Entry.segmentName();
9960 StringRef SectionName = Entry.sectionName();
9961 uint64_t Address = Entry.address();
Nick Kledzik56ebef42014-09-16 01:41:51 +00009962
9963 // Table lines look like:
9964 // __DATA __data 0x00001000 pointer 0 _foo
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009965 outs() << left_justify(SegmentName, 8) << " "
Nick Kledzik5ffacc12014-09-30 00:19:58 +00009966 << left_justify(SectionName, 18) << " "
9967 << format_hex(Address, 10, true) << " "
9968 << left_justify(Entry.typeName(), 8) << " "
Kevin Enderbyb28ed012014-10-29 21:28:24 +00009969 << format_decimal(Entry.addend(), 8) << " " << Entry.symbolName()
9970 << "\n";
Nick Kledzik56ebef42014-09-16 01:41:51 +00009971 }
Kevin Enderbyfeb63b92017-02-28 21:47:07 +00009972 if (Err)
9973 report_error(Obj->getFileName(), std::move(Err));
Nick Kledzik56ebef42014-09-16 01:41:51 +00009974}
9975
Kevin Enderby6f326ce2014-10-23 19:37:31 +00009976// get_dyld_bind_info_symbolname() is used for disassembly and passed an
9977// address, ReferenceValue, in the Mach-O file and looks in the dyld bind
9978// information for that address. If the address is found its binding symbol
9979// name is returned. If not nullptr is returned.
9980static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
9981 struct DisassembleInfo *info) {
Kevin Enderby078be602014-10-23 19:53:12 +00009982 if (info->bindtable == nullptr) {
Saleem Abdulrasool1d84d9a2017-01-08 19:14:15 +00009983 info->bindtable = llvm::make_unique<SymbolAddressMap>();
Kevin Enderbyfeb63b92017-02-28 21:47:07 +00009984 Error Err = Error::success();
9985 for (const llvm::object::MachOBindEntry &Entry : info->O->bindTable(Err)) {
Kevin Enderbya8d256c2017-03-20 19:46:55 +00009986 uint64_t Address = Entry.address();
Kevin Enderby6f326ce2014-10-23 19:37:31 +00009987 StringRef name = Entry.symbolName();
9988 if (!name.empty())
Saleem Abdulrasool1d84d9a2017-01-08 19:14:15 +00009989 (*info->bindtable)[Address] = name;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00009990 }
Kevin Enderbyfeb63b92017-02-28 21:47:07 +00009991 if (Err)
9992 report_error(info->O->getFileName(), std::move(Err));
Kevin Enderby6f326ce2014-10-23 19:37:31 +00009993 }
Saleem Abdulrasool1d84d9a2017-01-08 19:14:15 +00009994 auto name = info->bindtable->lookup(ReferenceValue);
9995 return !name.empty() ? name.data() : nullptr;
Kevin Enderby6f326ce2014-10-23 19:37:31 +00009996}
Michael Trent1854ecc2017-12-05 07:50:00 +00009997