1#[must_use]
9pub fn wrap_fat_arm64_only(thin: &[u8]) -> Vec<u8> {
10 const FAT_MAGIC: u32 = 0xcafe_babe;
11 const CPU_TYPE_ARM64: u32 = 0x0100_000c;
12 const CPU_SUBTYPE_ARM64_ALL: u32 = 0;
13 const SLICE_OFF: usize = 32;
15 const ALIGN_POW2: u32 = 3; let thin_size = u32::try_from(thin.len()).unwrap_or(u32::MAX);
18 let mut buf = Vec::with_capacity(SLICE_OFF.saturating_add(thin.len()));
19 buf.extend_from_slice(&FAT_MAGIC.to_be_bytes());
20 buf.extend_from_slice(&1_u32.to_be_bytes()); buf.extend_from_slice(&CPU_TYPE_ARM64.to_be_bytes());
22 buf.extend_from_slice(&CPU_SUBTYPE_ARM64_ALL.to_be_bytes());
23 buf.extend_from_slice(&u32::try_from(SLICE_OFF).unwrap_or(0).to_be_bytes());
24 buf.extend_from_slice(&thin_size.to_be_bytes());
25 buf.extend_from_slice(&ALIGN_POW2.to_be_bytes());
26 while buf.len() < SLICE_OFF {
27 buf.push(0);
28 }
29 buf.extend_from_slice(thin);
30 buf
31}
32
33#[must_use]
46#[allow(
47 clippy::arithmetic_side_effects,
48 clippy::cast_possible_truncation,
49 clippy::items_after_statements
50)]
51pub fn arm64_execute_with_text(text: &[u8]) -> Vec<u8> {
52 const MH_MAGIC_64: u32 = 0xfeed_facf;
53 const CPU_TYPE_ARM64: u32 = 0x0100_000c;
54 const CPU_SUBTYPE_ARM64_ALL: u32 = 0;
55 const MH_EXECUTE: u32 = 0x2;
56 const MH_NOUNDEFS: u32 = 0x1;
57 const MH_PIE: u32 = 0x0020_0000;
58 const LC_SEGMENT_64: u32 = 0x19;
59 const LC_UUID: u32 = 0x1b;
60 const LC_LOAD_DYLIB: u32 = 0xc;
61 const LC_MAIN: u32 = 0x28 | 0x8000_0000;
62 const VM_PROT_READ: u32 = 0x1;
63 const VM_PROT_EXECUTE: u32 = 0x4;
64 const SEG_CMD: u32 = 72;
65 const SECT_64: u32 = 80;
66
67 let dylib_path = b"/usr/lib/libSystem.B.dylib\0";
68 let dylib_path_pad = align8(dylib_path.len());
69 let dylib_cmdsize = 24 + dylib_path_pad;
70
71 let text_seg_cmdsize = SEG_CMD + SECT_64;
72 let pagezero_cmdsize = SEG_CMD;
73 let uuid_cmdsize: u32 = 24;
74 let main_cmdsize: u32 = 24;
75
76 let ncmds = 5_u32;
77 let sizeofcmds = pagezero_cmdsize
78 + text_seg_cmdsize
79 + uuid_cmdsize
80 + u32::try_from(dylib_cmdsize).unwrap_or(0)
81 + main_cmdsize;
82
83 let header_size = 32_u64;
84 let load_end = header_size + u64::from(sizeofcmds);
85 let text_fileoff = align_u64(load_end, 16);
86 let text_filesize = u64::try_from(text.len()).unwrap_or(0);
87 let text_filesize = text_filesize.max(16);
89 let file_end = text_fileoff + text_filesize;
90
91 let text_vmaddr = 0x0000_0001_0000_0000_u64;
92 let text_vmsize = 0x4000_u64;
93 let entryoff = text_fileoff;
94
95 let mut buf = Vec::with_capacity(usize::try_from(file_end).unwrap_or(512));
96
97 write_u32(&mut buf, MH_MAGIC_64);
98 write_u32(&mut buf, CPU_TYPE_ARM64);
99 write_u32(&mut buf, CPU_SUBTYPE_ARM64_ALL);
100 write_u32(&mut buf, MH_EXECUTE);
101 write_u32(&mut buf, ncmds);
102 write_u32(&mut buf, sizeofcmds);
103 write_u32(&mut buf, MH_NOUNDEFS | MH_PIE);
104 write_u32(&mut buf, 0);
105
106 write_u32(&mut buf, LC_SEGMENT_64);
107 write_u32(&mut buf, pagezero_cmdsize);
108 write_segname(&mut buf, b"__PAGEZERO");
109 write_u64(&mut buf, 0);
110 write_u64(&mut buf, 0x0000_0001_0000_0000);
111 write_u64(&mut buf, 0);
112 write_u64(&mut buf, 0);
113 write_u32(&mut buf, 0);
114 write_u32(&mut buf, 0);
115 write_u32(&mut buf, 0);
116 write_u32(&mut buf, 0);
117
118 write_u32(&mut buf, LC_SEGMENT_64);
119 write_u32(&mut buf, text_seg_cmdsize);
120 write_segname(&mut buf, b"__TEXT");
121 write_u64(&mut buf, text_vmaddr);
122 write_u64(&mut buf, text_vmsize);
123 write_u64(&mut buf, 0);
124 write_u64(&mut buf, file_end);
125 write_u32(&mut buf, VM_PROT_READ | VM_PROT_EXECUTE);
126 write_u32(&mut buf, VM_PROT_READ | VM_PROT_EXECUTE);
127 write_u32(&mut buf, 1);
128 write_u32(&mut buf, 0);
129
130 write_sectname(&mut buf, b"__text");
131 write_segname(&mut buf, b"__TEXT");
132 write_u64(&mut buf, text_vmaddr);
133 write_u64(&mut buf, text_filesize);
134 write_u32(&mut buf, u32::try_from(text_fileoff).unwrap_or(0));
135 write_u32(&mut buf, 2);
136 write_u32(&mut buf, 0);
137 write_u32(&mut buf, 0);
138 write_u32(&mut buf, 0x8000_0400);
139 write_u32(&mut buf, 0);
140 write_u32(&mut buf, 0);
141 write_u32(&mut buf, 0);
142
143 write_u32(&mut buf, LC_UUID);
144 write_u32(&mut buf, uuid_cmdsize);
145 buf.extend_from_slice(&[
146 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE,
147 0xFF,
148 ]);
149
150 write_u32(&mut buf, LC_LOAD_DYLIB);
151 write_u32(&mut buf, u32::try_from(dylib_cmdsize).unwrap_or(0));
152 write_u32(&mut buf, 24);
153 write_u32(&mut buf, 2);
154 write_u32(&mut buf, 0x0001_0000);
155 write_u32(&mut buf, 0x0001_0000);
156 buf.extend_from_slice(dylib_path);
157 while !buf.len().is_multiple_of(8) {
158 buf.push(0);
159 }
160
161 write_u32(&mut buf, LC_MAIN);
162 write_u32(&mut buf, main_cmdsize);
163 write_u64(&mut buf, entryoff);
164 write_u64(&mut buf, 0);
165
166 let target = usize::try_from(text_fileoff).unwrap_or(buf.len());
167 if buf.len() < target {
168 buf.resize(target, 0);
169 }
170 buf.extend_from_slice(text);
171 let target = usize::try_from(file_end).unwrap_or(buf.len());
172 if buf.len() < target {
173 buf.resize(target, 0);
174 }
175 buf
176}
177
178pub const LIBKH_ADD_TEXT_VA: u64 = 0x0000_0002_0000_0000;
180
181pub const CALL_DYLIB_GOT_VA: u64 = 0x0000_0001_0000_4000;
183
184pub const LIBKH_ADD_INSTALL_NAME: &str = "@executable_path/libkh_add.dylib";
186
187pub const KH_ADD_SYMBOL: &str = "_kh_add";
189
190#[must_use]
194#[allow(
195 clippy::arithmetic_side_effects,
196 clippy::cast_possible_truncation,
197 clippy::items_after_statements,
198 clippy::too_many_lines
199)]
200pub fn arm64_dylib_add() -> Vec<u8> {
201 const MH_MAGIC_64: u32 = 0xfeed_facf;
202 const CPU_TYPE_ARM64: u32 = 0x0100_000c;
203 const MH_DYLIB: u32 = 0x6;
204 const MH_NOUNDEFS: u32 = 0x1;
205 const MH_DYLDLINK: u32 = 0x4;
206 const LC_SEGMENT_64: u32 = 0x19;
207 const LC_ID_DYLIB: u32 = 0xd;
208 const LC_SYMTAB: u32 = 0x2;
209 const LC_DYSYMTAB: u32 = 0xb;
210 const LC_UUID: u32 = 0x1b;
211 const VM_PROT_READ: u32 = 0x1;
212 const VM_PROT_EXECUTE: u32 = 0x4;
213 const SEG_CMD: u32 = 72;
214 const SECT_64: u32 = 80;
215 const NLIST64: u32 = 16;
216
217 let id_path = LIBKH_ADD_INSTALL_NAME.as_bytes();
218 let id_path_nul = {
219 let mut v = id_path.to_vec();
220 v.push(0);
221 v
222 };
223 let id_path_pad = align8(id_path_nul.len());
224 let id_cmdsize = 24 + id_path_pad;
225
226 let text_seg_cmdsize = SEG_CMD + SECT_64;
227 let uuid_cmdsize: u32 = 24;
228 let symtab_cmdsize: u32 = 24;
229 let dysymtab_cmdsize: u32 = 80;
230
231 let ncmds = 5_u32;
232 let sizeofcmds = text_seg_cmdsize
233 + u32::try_from(id_cmdsize).unwrap_or(0)
234 + uuid_cmdsize
235 + symtab_cmdsize
236 + dysymtab_cmdsize;
237
238 let header_size = 32_u64;
239 let load_end = header_size + u64::from(sizeofcmds);
240 let text_fileoff = align_u64(load_end, 16);
241 let text_code: [u8; 8] = {
243 let mut t = [0_u8; 8];
244 t[..4].copy_from_slice(&0x0b01_0000_u32.to_le_bytes()); t[4..].copy_from_slice(&0xd65f_03c0_u32.to_le_bytes()); t
247 };
248 let text_filesize = 16_u64; let text_end = text_fileoff + text_filesize;
250
251 let symoff = align_u64(text_end, 8);
253 let nsyms = 1_u32;
254 let stroff = symoff + u64::from(NLIST64) * u64::from(nsyms);
255 let mut strtab = vec![0_u8];
257 strtab.extend_from_slice(KH_ADD_SYMBOL.as_bytes());
258 strtab.push(0);
259 let strsize = u32::try_from(strtab.len()).unwrap_or(0);
260 let file_end = stroff + u64::from(strsize);
261
262 let text_vmaddr = LIBKH_ADD_TEXT_VA;
263 let text_vmsize = 0x4000_u64;
264 let text_sect_addr = text_vmaddr + text_fileoff;
266 let export_va = text_sect_addr; let mut buf = Vec::with_capacity(usize::try_from(file_end).unwrap_or(512));
269
270 write_u32(&mut buf, MH_MAGIC_64);
271 write_u32(&mut buf, CPU_TYPE_ARM64);
272 write_u32(&mut buf, 0); write_u32(&mut buf, MH_DYLIB);
274 write_u32(&mut buf, ncmds);
275 write_u32(&mut buf, sizeofcmds);
276 write_u32(&mut buf, MH_NOUNDEFS | MH_DYLDLINK);
277 write_u32(&mut buf, 0);
278
279 write_u32(&mut buf, LC_SEGMENT_64);
281 write_u32(&mut buf, text_seg_cmdsize);
282 write_segname(&mut buf, b"__TEXT");
283 write_u64(&mut buf, text_vmaddr);
284 write_u64(&mut buf, text_vmsize);
285 write_u64(&mut buf, 0); write_u64(&mut buf, file_end); write_u32(&mut buf, VM_PROT_READ | VM_PROT_EXECUTE);
288 write_u32(&mut buf, VM_PROT_READ | VM_PROT_EXECUTE);
289 write_u32(&mut buf, 1); write_u32(&mut buf, 0);
291
292 write_sectname(&mut buf, b"__text");
293 write_segname(&mut buf, b"__TEXT");
294 write_u64(&mut buf, text_sect_addr);
295 write_u64(&mut buf, text_filesize);
296 write_u32(&mut buf, u32::try_from(text_fileoff).unwrap_or(0));
297 write_u32(&mut buf, 2); write_u32(&mut buf, 0);
299 write_u32(&mut buf, 0);
300 write_u32(&mut buf, 0x8000_0400); write_u32(&mut buf, 0);
302 write_u32(&mut buf, 0);
303 write_u32(&mut buf, 0);
304
305 write_u32(&mut buf, LC_ID_DYLIB);
307 write_u32(&mut buf, u32::try_from(id_cmdsize).unwrap_or(0));
308 write_u32(&mut buf, 24); write_u32(&mut buf, 1); write_u32(&mut buf, 0x0001_0000);
311 write_u32(&mut buf, 0x0001_0000);
312 buf.extend_from_slice(&id_path_nul);
313 while !buf.len().is_multiple_of(8) {
314 buf.push(0);
315 }
316
317 write_u32(&mut buf, LC_UUID);
319 write_u32(&mut buf, uuid_cmdsize);
320 buf.extend_from_slice(&[
321 0xA1, 0xB2, 0xC3, 0xD4, 0xE5, 0xF6, 0x07, 0x18, 0x29, 0x3A, 0x4B, 0x5C, 0x6D, 0x7E, 0x8F,
322 0x90,
323 ]);
324
325 write_u32(&mut buf, LC_SYMTAB);
327 write_u32(&mut buf, symtab_cmdsize);
328 write_u32(&mut buf, u32::try_from(symoff).unwrap_or(0));
329 write_u32(&mut buf, nsyms);
330 write_u32(&mut buf, u32::try_from(stroff).unwrap_or(0));
331 write_u32(&mut buf, strsize);
332
333 write_u32(&mut buf, LC_DYSYMTAB);
335 write_u32(&mut buf, dysymtab_cmdsize);
336 for _ in 0..18 {
337 write_u32(&mut buf, 0);
338 }
339
340 let target = usize::try_from(text_fileoff).unwrap_or(buf.len());
342 if buf.len() < target {
343 buf.resize(target, 0);
344 }
345 buf.extend_from_slice(&text_code);
346 let target = usize::try_from(text_end).unwrap_or(buf.len());
347 if buf.len() < target {
348 buf.resize(target, 0);
349 }
350
351 let target = usize::try_from(symoff).unwrap_or(buf.len());
353 if buf.len() < target {
354 buf.resize(target, 0);
355 }
356 write_u32(&mut buf, 1); buf.push(0x0f); buf.push(1); write_u16(&mut buf, 0); write_u64(&mut buf, export_va);
361
362 let target = usize::try_from(stroff).unwrap_or(buf.len());
363 if buf.len() < target {
364 buf.resize(target, 0);
365 }
366 buf.extend_from_slice(&strtab);
367
368 let target = usize::try_from(file_end).unwrap_or(buf.len());
369 if buf.len() < target {
370 buf.resize(target, 0);
371 }
372 buf
373}
374
375#[must_use]
384#[allow(
385 clippy::arithmetic_side_effects,
386 clippy::cast_possible_truncation,
387 clippy::cast_possible_wrap,
388 clippy::cast_sign_loss,
389 clippy::items_after_statements,
390 clippy::too_many_lines
391)]
392pub fn call_dylib_exit() -> Vec<u8> {
393 const MH_MAGIC_64: u32 = 0xfeed_facf;
394 const CPU_TYPE_ARM64: u32 = 0x0100_000c;
395 const MH_EXECUTE: u32 = 0x2;
396 const MH_NOUNDEFS: u32 = 0x1;
397 const MH_PIE: u32 = 0x0020_0000;
398 const LC_SEGMENT_64: u32 = 0x19;
399 const LC_UUID: u32 = 0x1b;
400 const LC_LOAD_DYLIB: u32 = 0xc;
401 const LC_SYMTAB: u32 = 0x2;
402 const LC_DYLD_INFO_ONLY: u32 = 0x22 | 0x8000_0000;
403 const LC_MAIN: u32 = 0x28 | 0x8000_0000;
404 const VM_PROT_READ: u32 = 0x1;
405 const VM_PROT_WRITE: u32 = 0x2;
406 const VM_PROT_EXECUTE: u32 = 0x4;
407 const SEG_CMD: u32 = 72;
408 const SECT_64: u32 = 80;
409 const NLIST64: u32 = 16;
410 const DYLD_INFO_CMDSIZE: u32 = 48;
411
412 let kh_path = LIBKH_ADD_INSTALL_NAME.as_bytes();
413 let sys_path = b"/usr/lib/libSystem.B.dylib\0";
414 let kh_pad = align8(kh_path.len() + 1);
415 let sys_pad = align8(sys_path.len());
416 let kh_cmdsize = 24 + kh_pad;
417 let sys_cmdsize = 24 + sys_pad;
418
419 let pagezero_cmdsize = SEG_CMD;
420 let text_seg_cmdsize = SEG_CMD + SECT_64;
421 let data_seg_cmdsize = SEG_CMD + SECT_64;
422 let uuid_cmdsize: u32 = 24;
423 let symtab_cmdsize: u32 = 24;
424 let main_cmdsize: u32 = 24;
425
426 let bind_stream = crate::bind::encode_pointer_bind(1, KH_ADD_SYMBOL, 2, 0);
428 let bind_size = u32::try_from(bind_stream.len()).unwrap_or(0);
429
430 let ncmds = 9_u32;
431 let sizeofcmds = pagezero_cmdsize
432 + text_seg_cmdsize
433 + data_seg_cmdsize
434 + uuid_cmdsize
435 + u32::try_from(kh_cmdsize).unwrap_or(0)
436 + u32::try_from(sys_cmdsize).unwrap_or(0)
437 + DYLD_INFO_CMDSIZE
438 + symtab_cmdsize
439 + main_cmdsize;
440
441 let header_size = 32_u64;
442 let load_end = header_size + u64::from(sizeofcmds);
443 let text_fileoff = align_u64(load_end, 16);
444
445 let text_len = 32_u64;
454 let text_end = text_fileoff + text_len;
455
456 let data_fileoff = align_u64(text_end, 8);
457 let got_filesize = 8_u64;
458 let data_end = data_fileoff + got_filesize;
459
460 let bind_off = align_u64(data_end, 8);
461 let bind_end = bind_off + u64::from(bind_size);
462
463 let symoff = align_u64(bind_end, 8);
464 let nsyms = 1_u32;
465 let stroff = symoff + u64::from(NLIST64) * u64::from(nsyms);
466 let mut strtab = vec![0_u8];
467 strtab.extend_from_slice(KH_ADD_SYMBOL.as_bytes());
468 strtab.push(0);
469 let strsize = u32::try_from(strtab.len()).unwrap_or(0);
470 let file_end = stroff + u64::from(strsize);
471
472 let text_vmaddr = 0x0000_0001_0000_0000_u64;
473 let text_vmsize = 0x4000_u64;
474 let data_vmaddr = CALL_DYLIB_GOT_VA;
475 let data_vmsize = 0x4000_u64;
476 let entryoff = text_fileoff;
477 let entry_va = text_vmaddr + entryoff; let adrp_pc = entry_va + 8;
480 let adrp_insn = encode_adrp(16, adrp_pc, data_vmaddr);
481 let pageoff = u32::try_from(data_vmaddr & 0xfff).unwrap_or(0);
482 let ldr_insn = encode_ldr_x_uoff(16, 16, pageoff);
483
484 let mut text = Vec::with_capacity(32);
485 push_u32(&mut text, movz(0, 20));
486 push_u32(&mut text, movz(1, 22));
487 push_u32(&mut text, adrp_insn);
488 push_u32(&mut text, ldr_insn);
489 push_u32(&mut text, 0xd63f_0200); push_u32(&mut text, movz(0x10, 1)); push_u32(&mut text, svc80());
492 while text.len() < usize::try_from(text_len).unwrap_or(32) {
493 text.push(0);
494 }
495
496 let mut buf = Vec::with_capacity(usize::try_from(file_end).unwrap_or(512));
497
498 write_u32(&mut buf, MH_MAGIC_64);
499 write_u32(&mut buf, CPU_TYPE_ARM64);
500 write_u32(&mut buf, 0);
501 write_u32(&mut buf, MH_EXECUTE);
502 write_u32(&mut buf, ncmds);
503 write_u32(&mut buf, sizeofcmds);
504 write_u32(&mut buf, MH_NOUNDEFS | MH_PIE);
505 write_u32(&mut buf, 0);
506
507 write_u32(&mut buf, LC_SEGMENT_64);
509 write_u32(&mut buf, pagezero_cmdsize);
510 write_segname(&mut buf, b"__PAGEZERO");
511 write_u64(&mut buf, 0);
512 write_u64(&mut buf, 0x0000_0001_0000_0000);
513 write_u64(&mut buf, 0);
514 write_u64(&mut buf, 0);
515 write_u32(&mut buf, 0);
516 write_u32(&mut buf, 0);
517 write_u32(&mut buf, 0);
518 write_u32(&mut buf, 0);
519
520 write_u32(&mut buf, LC_SEGMENT_64);
522 write_u32(&mut buf, text_seg_cmdsize);
523 write_segname(&mut buf, b"__TEXT");
524 write_u64(&mut buf, text_vmaddr);
525 write_u64(&mut buf, text_vmsize);
526 write_u64(&mut buf, 0);
527 write_u64(&mut buf, data_fileoff); write_u32(&mut buf, VM_PROT_READ | VM_PROT_EXECUTE);
529 write_u32(&mut buf, VM_PROT_READ | VM_PROT_EXECUTE);
530 write_u32(&mut buf, 1);
531 write_u32(&mut buf, 0);
532
533 write_sectname(&mut buf, b"__text");
534 write_segname(&mut buf, b"__TEXT");
535 write_u64(&mut buf, text_vmaddr + text_fileoff);
536 write_u64(&mut buf, text_len);
537 write_u32(&mut buf, u32::try_from(text_fileoff).unwrap_or(0));
538 write_u32(&mut buf, 2);
539 write_u32(&mut buf, 0);
540 write_u32(&mut buf, 0);
541 write_u32(&mut buf, 0x8000_0400);
542 write_u32(&mut buf, 0);
543 write_u32(&mut buf, 0);
544 write_u32(&mut buf, 0);
545
546 write_u32(&mut buf, LC_SEGMENT_64);
548 write_u32(&mut buf, data_seg_cmdsize);
549 write_segname(&mut buf, b"__DATA");
550 write_u64(&mut buf, data_vmaddr);
551 write_u64(&mut buf, data_vmsize);
552 write_u64(&mut buf, data_fileoff);
553 write_u64(&mut buf, got_filesize);
554 write_u32(&mut buf, VM_PROT_READ | VM_PROT_WRITE);
555 write_u32(&mut buf, VM_PROT_READ | VM_PROT_WRITE);
556 write_u32(&mut buf, 1);
557 write_u32(&mut buf, 0);
558
559 write_sectname(&mut buf, b"__got");
560 write_segname(&mut buf, b"__DATA");
561 write_u64(&mut buf, data_vmaddr);
562 write_u64(&mut buf, got_filesize);
563 write_u32(&mut buf, u32::try_from(data_fileoff).unwrap_or(0));
564 write_u32(&mut buf, 3); write_u32(&mut buf, 0);
566 write_u32(&mut buf, 0);
567 write_u32(&mut buf, 0x6); write_u32(&mut buf, 0);
569 write_u32(&mut buf, 0);
570 write_u32(&mut buf, 0);
571
572 write_u32(&mut buf, LC_UUID);
574 write_u32(&mut buf, uuid_cmdsize);
575 buf.extend_from_slice(&[
576 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, 0x90, 0xA0, 0xB0, 0xC0, 0xD0, 0xE0, 0xF0,
577 0x01,
578 ]);
579
580 write_u32(&mut buf, LC_LOAD_DYLIB);
582 write_u32(&mut buf, u32::try_from(kh_cmdsize).unwrap_or(0));
583 write_u32(&mut buf, 24);
584 write_u32(&mut buf, 2);
585 write_u32(&mut buf, 0x0001_0000);
586 write_u32(&mut buf, 0x0001_0000);
587 buf.extend_from_slice(kh_path);
588 buf.push(0);
589 while !buf.len().is_multiple_of(8) {
590 buf.push(0);
591 }
592
593 write_u32(&mut buf, LC_LOAD_DYLIB);
595 write_u32(&mut buf, u32::try_from(sys_cmdsize).unwrap_or(0));
596 write_u32(&mut buf, 24);
597 write_u32(&mut buf, 2);
598 write_u32(&mut buf, 0x0001_0000);
599 write_u32(&mut buf, 0x0001_0000);
600 buf.extend_from_slice(sys_path);
601 while !buf.len().is_multiple_of(8) {
602 buf.push(0);
603 }
604
605 write_u32(&mut buf, LC_DYLD_INFO_ONLY);
607 write_u32(&mut buf, DYLD_INFO_CMDSIZE);
608 write_u32(&mut buf, 0); write_u32(&mut buf, 0); write_u32(&mut buf, u32::try_from(bind_off).unwrap_or(0));
611 write_u32(&mut buf, bind_size);
612 write_u32(&mut buf, 0); write_u32(&mut buf, 0);
614 write_u32(&mut buf, 0); write_u32(&mut buf, 0);
616 write_u32(&mut buf, 0); write_u32(&mut buf, 0);
618
619 write_u32(&mut buf, LC_SYMTAB);
621 write_u32(&mut buf, symtab_cmdsize);
622 write_u32(&mut buf, u32::try_from(symoff).unwrap_or(0));
623 write_u32(&mut buf, nsyms);
624 write_u32(&mut buf, u32::try_from(stroff).unwrap_or(0));
625 write_u32(&mut buf, strsize);
626
627 write_u32(&mut buf, LC_MAIN);
629 write_u32(&mut buf, main_cmdsize);
630 write_u64(&mut buf, entryoff);
631 write_u64(&mut buf, 0);
632
633 let target = usize::try_from(text_fileoff).unwrap_or(buf.len());
635 if buf.len() < target {
636 buf.resize(target, 0);
637 }
638 buf.extend_from_slice(&text);
639
640 let target = usize::try_from(data_fileoff).unwrap_or(buf.len());
642 if buf.len() < target {
643 buf.resize(target, 0);
644 }
645 buf.extend_from_slice(&[0_u8; 8]);
646
647 let target = usize::try_from(bind_off).unwrap_or(buf.len());
649 if buf.len() < target {
650 buf.resize(target, 0);
651 }
652 buf.extend_from_slice(&bind_stream);
653
654 let target = usize::try_from(symoff).unwrap_or(buf.len());
656 if buf.len() < target {
657 buf.resize(target, 0);
658 }
659 write_u32(&mut buf, 1); buf.push(0x01); buf.push(0); write_u16(&mut buf, 0);
663 write_u64(&mut buf, 0);
664
665 let target = usize::try_from(stroff).unwrap_or(buf.len());
666 if buf.len() < target {
667 buf.resize(target, 0);
668 }
669 buf.extend_from_slice(&strtab);
670
671 let target = usize::try_from(file_end).unwrap_or(buf.len());
672 if buf.len() < target {
673 buf.resize(target, 0);
674 }
675 buf
676}
677
678#[must_use]
685#[allow(
686 clippy::arithmetic_side_effects,
687 clippy::cast_possible_truncation,
688 clippy::cast_possible_wrap,
689 clippy::cast_sign_loss,
690 clippy::items_after_statements,
691 clippy::too_many_lines
692)]
693pub fn call_dylib_chained_exit() -> Vec<u8> {
694 const MH_MAGIC_64: u32 = 0xfeed_facf;
695 const CPU_TYPE_ARM64: u32 = 0x0100_000c;
696 const MH_EXECUTE: u32 = 0x2;
697 const MH_NOUNDEFS: u32 = 0x1;
698 const MH_PIE: u32 = 0x0020_0000;
699 const LC_SEGMENT_64: u32 = 0x19;
700 const LC_UUID: u32 = 0x1b;
701 const LC_LOAD_DYLIB: u32 = 0xc;
702 const LC_SYMTAB: u32 = 0x2;
703 const LC_DYLD_CHAINED_FIXUPS: u32 = 0x34 | 0x8000_0000;
704 const LC_MAIN: u32 = 0x28 | 0x8000_0000;
705 const VM_PROT_READ: u32 = 0x1;
706 const VM_PROT_WRITE: u32 = 0x2;
707 const VM_PROT_EXECUTE: u32 = 0x4;
708 const SEG_CMD: u32 = 72;
709 const SECT_64: u32 = 80;
710 const NLIST64: u32 = 16;
711 const LINKEDIT_CMDSIZE: u32 = 16;
712
713 let kh_path = LIBKH_ADD_INSTALL_NAME.as_bytes();
714 let sys_path = b"/usr/lib/libSystem.B.dylib\0";
715 let kh_pad = align8(kh_path.len() + 1);
716 let sys_pad = align8(sys_path.len());
717 let kh_cmdsize = 24 + kh_pad;
718 let sys_cmdsize = 24 + sys_pad;
719
720 let pagezero_cmdsize = SEG_CMD;
721 let text_seg_cmdsize = SEG_CMD + SECT_64;
722 let data_seg_cmdsize = SEG_CMD + SECT_64;
723 let uuid_cmdsize: u32 = 24;
724 let symtab_cmdsize: u32 = 24;
725 let main_cmdsize: u32 = 24;
726
727 let text_vmaddr = 0x0000_0001_0000_0000_u64;
728 let data_vmaddr = CALL_DYLIB_GOT_VA;
729 let preferred_base = text_vmaddr;
730 let segment_offset = data_vmaddr.wrapping_sub(preferred_base);
731
732 let fixups_blob = crate::chained::encode_chained_fixups_blob(
733 3,
734 2,
735 segment_offset,
736 0x4000,
737 0,
738 crate::chained::DYLD_CHAINED_PTR_64,
739 1,
740 KH_ADD_SYMBOL,
741 );
742 let fixups_size = u32::try_from(fixups_blob.len()).unwrap_or(0);
743 let got_chain = crate::chained::encode_ptr_64_bind(0, 0, 0);
744
745 let ncmds = 9_u32;
746 let sizeofcmds = pagezero_cmdsize
747 + text_seg_cmdsize
748 + data_seg_cmdsize
749 + uuid_cmdsize
750 + u32::try_from(kh_cmdsize).unwrap_or(0)
751 + u32::try_from(sys_cmdsize).unwrap_or(0)
752 + LINKEDIT_CMDSIZE
753 + symtab_cmdsize
754 + main_cmdsize;
755
756 let header_size = 32_u64;
757 let load_end = header_size + u64::from(sizeofcmds);
758 let text_fileoff = align_u64(load_end, 16);
759 let text_len = 32_u64;
760 let text_end = text_fileoff + text_len;
761
762 let data_fileoff = align_u64(text_end, 8);
763 let got_filesize = 8_u64;
764 let data_end = data_fileoff + got_filesize;
765
766 let fixups_off = align_u64(data_end, 8);
767 let fixups_end = fixups_off + u64::from(fixups_size);
768
769 let symoff = align_u64(fixups_end, 8);
770 let nsyms = 1_u32;
771 let stroff = symoff + u64::from(NLIST64) * u64::from(nsyms);
772 let mut strtab = vec![0_u8];
773 strtab.extend_from_slice(KH_ADD_SYMBOL.as_bytes());
774 strtab.push(0);
775 let strsize = u32::try_from(strtab.len()).unwrap_or(0);
776 let file_end = stroff + u64::from(strsize);
777
778 let text_vmsize = 0x4000_u64;
779 let data_vmsize = 0x4000_u64;
780 let entryoff = text_fileoff;
781 let entry_va = text_vmaddr + entryoff;
782 let adrp_pc = entry_va + 8;
783 let adrp_insn = encode_adrp(16, adrp_pc, data_vmaddr);
784 let pageoff = u32::try_from(data_vmaddr & 0xfff).unwrap_or(0);
785 let ldr_insn = encode_ldr_x_uoff(16, 16, pageoff);
786
787 let mut text = Vec::with_capacity(32);
788 push_u32(&mut text, movz(0, 20));
789 push_u32(&mut text, movz(1, 22));
790 push_u32(&mut text, adrp_insn);
791 push_u32(&mut text, ldr_insn);
792 push_u32(&mut text, 0xd63f_0200);
793 push_u32(&mut text, movz(0x10, 1));
794 push_u32(&mut text, svc80());
795 while text.len() < usize::try_from(text_len).unwrap_or(32) {
796 text.push(0);
797 }
798
799 let mut buf = Vec::with_capacity(usize::try_from(file_end).unwrap_or(512));
800
801 write_u32(&mut buf, MH_MAGIC_64);
802 write_u32(&mut buf, CPU_TYPE_ARM64);
803 write_u32(&mut buf, 0);
804 write_u32(&mut buf, MH_EXECUTE);
805 write_u32(&mut buf, ncmds);
806 write_u32(&mut buf, sizeofcmds);
807 write_u32(&mut buf, MH_NOUNDEFS | MH_PIE);
808 write_u32(&mut buf, 0);
809
810 write_u32(&mut buf, LC_SEGMENT_64);
811 write_u32(&mut buf, pagezero_cmdsize);
812 write_segname(&mut buf, b"__PAGEZERO");
813 write_u64(&mut buf, 0);
814 write_u64(&mut buf, 0x0000_0001_0000_0000);
815 write_u64(&mut buf, 0);
816 write_u64(&mut buf, 0);
817 write_u32(&mut buf, 0);
818 write_u32(&mut buf, 0);
819 write_u32(&mut buf, 0);
820 write_u32(&mut buf, 0);
821
822 write_u32(&mut buf, LC_SEGMENT_64);
823 write_u32(&mut buf, text_seg_cmdsize);
824 write_segname(&mut buf, b"__TEXT");
825 write_u64(&mut buf, text_vmaddr);
826 write_u64(&mut buf, text_vmsize);
827 write_u64(&mut buf, 0);
828 write_u64(&mut buf, data_fileoff);
829 write_u32(&mut buf, VM_PROT_READ | VM_PROT_EXECUTE);
830 write_u32(&mut buf, VM_PROT_READ | VM_PROT_EXECUTE);
831 write_u32(&mut buf, 1);
832 write_u32(&mut buf, 0);
833
834 write_sectname(&mut buf, b"__text");
835 write_segname(&mut buf, b"__TEXT");
836 write_u64(&mut buf, text_vmaddr + text_fileoff);
837 write_u64(&mut buf, text_len);
838 write_u32(&mut buf, u32::try_from(text_fileoff).unwrap_or(0));
839 write_u32(&mut buf, 2);
840 write_u32(&mut buf, 0);
841 write_u32(&mut buf, 0);
842 write_u32(&mut buf, 0x8000_0400);
843 write_u32(&mut buf, 0);
844 write_u32(&mut buf, 0);
845 write_u32(&mut buf, 0);
846
847 write_u32(&mut buf, LC_SEGMENT_64);
848 write_u32(&mut buf, data_seg_cmdsize);
849 write_segname(&mut buf, b"__DATA");
850 write_u64(&mut buf, data_vmaddr);
851 write_u64(&mut buf, data_vmsize);
852 write_u64(&mut buf, data_fileoff);
853 write_u64(&mut buf, got_filesize);
854 write_u32(&mut buf, VM_PROT_READ | VM_PROT_WRITE);
855 write_u32(&mut buf, VM_PROT_READ | VM_PROT_WRITE);
856 write_u32(&mut buf, 1);
857 write_u32(&mut buf, 0);
858
859 write_sectname(&mut buf, b"__got");
860 write_segname(&mut buf, b"__DATA");
861 write_u64(&mut buf, data_vmaddr);
862 write_u64(&mut buf, got_filesize);
863 write_u32(&mut buf, u32::try_from(data_fileoff).unwrap_or(0));
864 write_u32(&mut buf, 3);
865 write_u32(&mut buf, 0);
866 write_u32(&mut buf, 0);
867 write_u32(&mut buf, 0x6);
868 write_u32(&mut buf, 0);
869 write_u32(&mut buf, 0);
870 write_u32(&mut buf, 0);
871
872 write_u32(&mut buf, LC_UUID);
873 write_u32(&mut buf, uuid_cmdsize);
874 buf.extend_from_slice(&[
875 0xC1, 0xA1, 0xED, 0xF1, 0x50, 0x60, 0x70, 0x80, 0x90, 0xA0, 0xB0, 0xC0, 0xD0, 0xE0, 0xF0,
876 0x11,
877 ]);
878
879 write_u32(&mut buf, LC_LOAD_DYLIB);
880 write_u32(&mut buf, u32::try_from(kh_cmdsize).unwrap_or(0));
881 write_u32(&mut buf, 24);
882 write_u32(&mut buf, 2);
883 write_u32(&mut buf, 0x0001_0000);
884 write_u32(&mut buf, 0x0001_0000);
885 buf.extend_from_slice(kh_path);
886 buf.push(0);
887 while !buf.len().is_multiple_of(8) {
888 buf.push(0);
889 }
890
891 write_u32(&mut buf, LC_LOAD_DYLIB);
892 write_u32(&mut buf, u32::try_from(sys_cmdsize).unwrap_or(0));
893 write_u32(&mut buf, 24);
894 write_u32(&mut buf, 2);
895 write_u32(&mut buf, 0x0001_0000);
896 write_u32(&mut buf, 0x0001_0000);
897 buf.extend_from_slice(sys_path);
898 while !buf.len().is_multiple_of(8) {
899 buf.push(0);
900 }
901
902 write_u32(&mut buf, LC_DYLD_CHAINED_FIXUPS);
903 write_u32(&mut buf, LINKEDIT_CMDSIZE);
904 write_u32(&mut buf, u32::try_from(fixups_off).unwrap_or(0));
905 write_u32(&mut buf, fixups_size);
906
907 write_u32(&mut buf, LC_SYMTAB);
908 write_u32(&mut buf, symtab_cmdsize);
909 write_u32(&mut buf, u32::try_from(symoff).unwrap_or(0));
910 write_u32(&mut buf, nsyms);
911 write_u32(&mut buf, u32::try_from(stroff).unwrap_or(0));
912 write_u32(&mut buf, strsize);
913
914 write_u32(&mut buf, LC_MAIN);
915 write_u32(&mut buf, main_cmdsize);
916 write_u64(&mut buf, entryoff);
917 write_u64(&mut buf, 0);
918
919 let target = usize::try_from(text_fileoff).unwrap_or(buf.len());
920 if buf.len() < target {
921 buf.resize(target, 0);
922 }
923 buf.extend_from_slice(&text);
924
925 let target = usize::try_from(data_fileoff).unwrap_or(buf.len());
926 if buf.len() < target {
927 buf.resize(target, 0);
928 }
929 buf.extend_from_slice(&got_chain.to_le_bytes());
930
931 let target = usize::try_from(fixups_off).unwrap_or(buf.len());
932 if buf.len() < target {
933 buf.resize(target, 0);
934 }
935 buf.extend_from_slice(&fixups_blob);
936
937 let target = usize::try_from(symoff).unwrap_or(buf.len());
938 if buf.len() < target {
939 buf.resize(target, 0);
940 }
941 write_u32(&mut buf, 1);
942 buf.push(0x01);
943 buf.push(0);
944 write_u16(&mut buf, 0);
945 write_u64(&mut buf, 0);
946
947 let target = usize::try_from(stroff).unwrap_or(buf.len());
948 if buf.len() < target {
949 buf.resize(target, 0);
950 }
951 buf.extend_from_slice(&strtab);
952
953 let target = usize::try_from(file_end).unwrap_or(buf.len());
954 if buf.len() < target {
955 buf.resize(target, 0);
956 }
957 buf
958}
959
960#[allow(
962 clippy::arithmetic_side_effects,
963 clippy::as_conversions,
964 clippy::cast_possible_truncation,
965 clippy::cast_possible_wrap,
966 clippy::cast_sign_loss
967)]
968fn encode_adrp(rd: u32, pc: u64, target: u64) -> u32 {
969 let page_pc = pc & !0xfff;
970 let page_tgt = target & !0xfff;
971 let imm = (page_tgt as i64 - page_pc as i64) >> 12;
972 let imm_u = (imm as u32) & 0x001f_ffff;
973 let immlo = imm_u & 0x3;
974 let immhi = (imm_u >> 2) & 0x7_ffff;
975 0x9000_0000 | (immlo << 29) | (immhi << 5) | (rd & 0x1f)
976}
977
978#[allow(clippy::integer_division)]
980const fn encode_ldr_x_uoff(rt: u32, rn: u32, imm: u32) -> u32 {
981 let imm12 = (imm / 8) & 0xfff;
982 0xf940_0000 | (imm12 << 10) | ((rn & 0x1f) << 5) | (rt & 0x1f)
983}
984
985fn write_u16(buf: &mut Vec<u8>, v: u16) {
986 buf.extend_from_slice(&v.to_le_bytes());
987}
988
989pub const LIBKH_CTOR_TEXT_VA: u64 = 0x0000_0003_0000_0000;
991
992pub const LIBKH_CTOR_INSTALL_NAME: &str = "@executable_path/libkh_ctor.dylib";
994
995#[must_use]
1000#[allow(
1001 clippy::arithmetic_side_effects,
1002 clippy::cast_possible_truncation,
1003 clippy::integer_division,
1004 clippy::items_after_statements,
1005 clippy::too_many_lines
1006)]
1007pub fn arm64_dylib_ctor() -> Vec<u8> {
1008 const MH_MAGIC_64: u32 = 0xfeed_facf;
1009 const CPU_TYPE_ARM64: u32 = 0x0100_000c;
1010 const MH_DYLIB: u32 = 0x6;
1011 const MH_NOUNDEFS: u32 = 0x1;
1012 const MH_DYLDLINK: u32 = 0x4;
1013 const LC_SEGMENT_64: u32 = 0x19;
1014 const LC_ID_DYLIB: u32 = 0xd;
1015 const LC_UUID: u32 = 0x1b;
1016 const VM_PROT_READ: u32 = 0x1;
1017 const VM_PROT_WRITE: u32 = 0x2;
1018 const VM_PROT_EXECUTE: u32 = 0x4;
1019 const SEG_CMD: u32 = 72;
1020 const SECT_64: u32 = 80;
1021 const S_MOD_INIT_FUNC_POINTERS: u32 = 0x9;
1022
1023 let id_path = LIBKH_CTOR_INSTALL_NAME.as_bytes();
1024 let mut id_nul = id_path.to_vec();
1025 id_nul.push(0);
1026 let id_pad = align8(id_nul.len());
1027 let id_cmdsize = 24 + id_pad;
1028
1029 let text_seg_cmdsize = SEG_CMD + SECT_64;
1030 let data_seg_cmdsize = SEG_CMD + SECT_64;
1031 let uuid_cmdsize: u32 = 24;
1032
1033 let ncmds = 4_u32;
1034 let sizeofcmds =
1035 text_seg_cmdsize + data_seg_cmdsize + u32::try_from(id_cmdsize).unwrap_or(0) + uuid_cmdsize;
1036
1037 let header_size = 32_u64;
1038 let load_end = header_size + u64::from(sizeofcmds);
1039 let text_fileoff = align_u64(load_end, 16);
1040
1041 let mut text = Vec::with_capacity(48);
1044 push_u32(&mut text, movz(0, 1));
1045 let adr_idx = text.len() / 4;
1046 push_u32(&mut text, 0); push_u32(&mut text, movz(2, 5));
1048 push_u32(&mut text, movz(0x10, 4));
1049 push_u32(&mut text, svc80());
1050 push_u32(&mut text, 0xd65f_03c0); while !text.len().is_multiple_of(4) {
1052 text.push(0);
1053 }
1054 let msg_off = text.len();
1055 text.extend_from_slice(b"ctor\n");
1056 while !text.len().is_multiple_of(8) {
1057 text.push(0);
1058 }
1059 let path_imm =
1060 i32::try_from(msg_off).unwrap_or(0) - i32::try_from(adr_idx.saturating_mul(4)).unwrap_or(0);
1061 patch_u32(&mut text, adr_idx, adr(1, path_imm));
1062
1063 let text_len = u64::try_from(text.len()).unwrap_or(0);
1064 let text_end = text_fileoff + text_len;
1065 let data_fileoff = align_u64(text_end, 8);
1066 let init_filesize = 8_u64;
1067 let file_end = data_fileoff + init_filesize;
1068
1069 let text_vmaddr = LIBKH_CTOR_TEXT_VA;
1070 let text_vmsize = 0x4000_u64;
1071 let data_vmaddr = LIBKH_CTOR_TEXT_VA + 0x4000;
1072 let data_vmsize = 0x4000_u64;
1073 let ctor_va = text_vmaddr + text_fileoff;
1074
1075 let mut buf = Vec::with_capacity(usize::try_from(file_end).unwrap_or(512));
1076
1077 write_u32(&mut buf, MH_MAGIC_64);
1078 write_u32(&mut buf, CPU_TYPE_ARM64);
1079 write_u32(&mut buf, 0);
1080 write_u32(&mut buf, MH_DYLIB);
1081 write_u32(&mut buf, ncmds);
1082 write_u32(&mut buf, sizeofcmds);
1083 write_u32(&mut buf, MH_NOUNDEFS | MH_DYLDLINK);
1084 write_u32(&mut buf, 0);
1085
1086 write_u32(&mut buf, LC_SEGMENT_64);
1088 write_u32(&mut buf, text_seg_cmdsize);
1089 write_segname(&mut buf, b"__TEXT");
1090 write_u64(&mut buf, text_vmaddr);
1091 write_u64(&mut buf, text_vmsize);
1092 write_u64(&mut buf, 0);
1093 write_u64(&mut buf, data_fileoff);
1094 write_u32(&mut buf, VM_PROT_READ | VM_PROT_EXECUTE);
1095 write_u32(&mut buf, VM_PROT_READ | VM_PROT_EXECUTE);
1096 write_u32(&mut buf, 1);
1097 write_u32(&mut buf, 0);
1098
1099 write_sectname(&mut buf, b"__text");
1100 write_segname(&mut buf, b"__TEXT");
1101 write_u64(&mut buf, ctor_va);
1102 write_u64(&mut buf, text_len);
1103 write_u32(&mut buf, u32::try_from(text_fileoff).unwrap_or(0));
1104 write_u32(&mut buf, 2);
1105 write_u32(&mut buf, 0);
1106 write_u32(&mut buf, 0);
1107 write_u32(&mut buf, 0x8000_0400);
1108 write_u32(&mut buf, 0);
1109 write_u32(&mut buf, 0);
1110 write_u32(&mut buf, 0);
1111
1112 write_u32(&mut buf, LC_SEGMENT_64);
1114 write_u32(&mut buf, data_seg_cmdsize);
1115 write_segname(&mut buf, b"__DATA");
1116 write_u64(&mut buf, data_vmaddr);
1117 write_u64(&mut buf, data_vmsize);
1118 write_u64(&mut buf, data_fileoff);
1119 write_u64(&mut buf, init_filesize);
1120 write_u32(&mut buf, VM_PROT_READ | VM_PROT_WRITE);
1121 write_u32(&mut buf, VM_PROT_READ | VM_PROT_WRITE);
1122 write_u32(&mut buf, 1);
1123 write_u32(&mut buf, 0);
1124
1125 write_sectname(&mut buf, b"__mod_init_func");
1126 write_segname(&mut buf, b"__DATA");
1127 write_u64(&mut buf, data_vmaddr);
1128 write_u64(&mut buf, init_filesize);
1129 write_u32(&mut buf, u32::try_from(data_fileoff).unwrap_or(0));
1130 write_u32(&mut buf, 3);
1131 write_u32(&mut buf, 0);
1132 write_u32(&mut buf, 0);
1133 write_u32(&mut buf, S_MOD_INIT_FUNC_POINTERS);
1134 write_u32(&mut buf, 0);
1135 write_u32(&mut buf, 0);
1136 write_u32(&mut buf, 0);
1137
1138 write_u32(&mut buf, LC_ID_DYLIB);
1140 write_u32(&mut buf, u32::try_from(id_cmdsize).unwrap_or(0));
1141 write_u32(&mut buf, 24);
1142 write_u32(&mut buf, 1);
1143 write_u32(&mut buf, 0x0001_0000);
1144 write_u32(&mut buf, 0x0001_0000);
1145 buf.extend_from_slice(&id_nul);
1146 while !buf.len().is_multiple_of(8) {
1147 buf.push(0);
1148 }
1149
1150 write_u32(&mut buf, LC_UUID);
1152 write_u32(&mut buf, uuid_cmdsize);
1153 buf.extend_from_slice(&[
1154 0xC7, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
1155 0x0F,
1156 ]);
1157
1158 let target = usize::try_from(text_fileoff).unwrap_or(buf.len());
1159 if buf.len() < target {
1160 buf.resize(target, 0);
1161 }
1162 buf.extend_from_slice(&text);
1163
1164 let target = usize::try_from(data_fileoff).unwrap_or(buf.len());
1165 if buf.len() < target {
1166 buf.resize(target, 0);
1167 }
1168 write_u64(&mut buf, ctor_va);
1170
1171 let target = usize::try_from(file_end).unwrap_or(buf.len());
1172 if buf.len() < target {
1173 buf.resize(target, 0);
1174 }
1175 buf
1176}
1177
1178#[must_use]
1182#[allow(
1183 clippy::arithmetic_side_effects,
1184 clippy::cast_possible_truncation,
1185 clippy::items_after_statements,
1186 clippy::too_many_lines
1187)]
1188pub fn ctor_main_exit() -> Vec<u8> {
1189 const MH_MAGIC_64: u32 = 0xfeed_facf;
1190 const CPU_TYPE_ARM64: u32 = 0x0100_000c;
1191 const MH_EXECUTE: u32 = 0x2;
1192 const MH_NOUNDEFS: u32 = 0x1;
1193 const MH_PIE: u32 = 0x0020_0000;
1194 const LC_SEGMENT_64: u32 = 0x19;
1195 const LC_UUID: u32 = 0x1b;
1196 const LC_LOAD_DYLIB: u32 = 0xc;
1197 const LC_MAIN: u32 = 0x28 | 0x8000_0000;
1198 const VM_PROT_READ: u32 = 0x1;
1199 const VM_PROT_EXECUTE: u32 = 0x4;
1200 const SEG_CMD: u32 = 72;
1201 const SECT_64: u32 = 80;
1202
1203 let ctor_path = LIBKH_CTOR_INSTALL_NAME.as_bytes();
1204 let sys_path = b"/usr/lib/libSystem.B.dylib\0";
1205 let ctor_pad = align8(ctor_path.len() + 1);
1206 let sys_pad = align8(sys_path.len());
1207 let ctor_cmdsize = 24 + ctor_pad;
1208 let sys_cmdsize = 24 + sys_pad;
1209
1210 let pagezero_cmdsize = SEG_CMD;
1211 let text_seg_cmdsize = SEG_CMD + SECT_64;
1212 let uuid_cmdsize: u32 = 24;
1213 let main_cmdsize: u32 = 24;
1214
1215 let ncmds = 6_u32;
1216 let sizeofcmds = pagezero_cmdsize
1217 + text_seg_cmdsize
1218 + uuid_cmdsize
1219 + u32::try_from(ctor_cmdsize).unwrap_or(0)
1220 + u32::try_from(sys_cmdsize).unwrap_or(0)
1221 + main_cmdsize;
1222
1223 let header_size = 32_u64;
1224 let load_end = header_size + u64::from(sizeofcmds);
1225 let text_fileoff = align_u64(load_end, 16);
1226
1227 let mut text = Vec::with_capacity(16);
1229 push_u32(&mut text, movz(0, 0));
1230 push_u32(&mut text, movz(0x10, 1));
1231 push_u32(&mut text, svc80());
1232 while text.len() < 16 {
1233 text.push(0);
1234 }
1235 let text_len = u64::try_from(text.len()).unwrap_or(16);
1236 let file_end = text_fileoff + text_len;
1237
1238 let text_vmaddr = 0x0000_0001_0000_0000_u64;
1239 let text_vmsize = 0x4000_u64;
1240 let entryoff = text_fileoff;
1241
1242 let mut buf = Vec::with_capacity(usize::try_from(file_end).unwrap_or(512));
1243
1244 write_u32(&mut buf, MH_MAGIC_64);
1245 write_u32(&mut buf, CPU_TYPE_ARM64);
1246 write_u32(&mut buf, 0);
1247 write_u32(&mut buf, MH_EXECUTE);
1248 write_u32(&mut buf, ncmds);
1249 write_u32(&mut buf, sizeofcmds);
1250 write_u32(&mut buf, MH_NOUNDEFS | MH_PIE);
1251 write_u32(&mut buf, 0);
1252
1253 write_u32(&mut buf, LC_SEGMENT_64);
1254 write_u32(&mut buf, pagezero_cmdsize);
1255 write_segname(&mut buf, b"__PAGEZERO");
1256 write_u64(&mut buf, 0);
1257 write_u64(&mut buf, 0x0000_0001_0000_0000);
1258 write_u64(&mut buf, 0);
1259 write_u64(&mut buf, 0);
1260 write_u32(&mut buf, 0);
1261 write_u32(&mut buf, 0);
1262 write_u32(&mut buf, 0);
1263 write_u32(&mut buf, 0);
1264
1265 write_u32(&mut buf, LC_SEGMENT_64);
1266 write_u32(&mut buf, text_seg_cmdsize);
1267 write_segname(&mut buf, b"__TEXT");
1268 write_u64(&mut buf, text_vmaddr);
1269 write_u64(&mut buf, text_vmsize);
1270 write_u64(&mut buf, 0);
1271 write_u64(&mut buf, file_end);
1272 write_u32(&mut buf, VM_PROT_READ | VM_PROT_EXECUTE);
1273 write_u32(&mut buf, VM_PROT_READ | VM_PROT_EXECUTE);
1274 write_u32(&mut buf, 1);
1275 write_u32(&mut buf, 0);
1276
1277 write_sectname(&mut buf, b"__text");
1278 write_segname(&mut buf, b"__TEXT");
1279 write_u64(&mut buf, text_vmaddr + text_fileoff);
1280 write_u64(&mut buf, text_len);
1281 write_u32(&mut buf, u32::try_from(text_fileoff).unwrap_or(0));
1282 write_u32(&mut buf, 2);
1283 write_u32(&mut buf, 0);
1284 write_u32(&mut buf, 0);
1285 write_u32(&mut buf, 0x8000_0400);
1286 write_u32(&mut buf, 0);
1287 write_u32(&mut buf, 0);
1288 write_u32(&mut buf, 0);
1289
1290 write_u32(&mut buf, LC_UUID);
1291 write_u32(&mut buf, uuid_cmdsize);
1292 buf.extend_from_slice(&[
1293 0xD0, 0xE1, 0xF2, 0x03, 0x14, 0x25, 0x36, 0x47, 0x58, 0x69, 0x7A, 0x8B, 0x9C, 0xAD, 0xBE,
1294 0xCF,
1295 ]);
1296
1297 write_u32(&mut buf, LC_LOAD_DYLIB);
1298 write_u32(&mut buf, u32::try_from(ctor_cmdsize).unwrap_or(0));
1299 write_u32(&mut buf, 24);
1300 write_u32(&mut buf, 2);
1301 write_u32(&mut buf, 0x0001_0000);
1302 write_u32(&mut buf, 0x0001_0000);
1303 buf.extend_from_slice(ctor_path);
1304 buf.push(0);
1305 while !buf.len().is_multiple_of(8) {
1306 buf.push(0);
1307 }
1308
1309 write_u32(&mut buf, LC_LOAD_DYLIB);
1310 write_u32(&mut buf, u32::try_from(sys_cmdsize).unwrap_or(0));
1311 write_u32(&mut buf, 24);
1312 write_u32(&mut buf, 2);
1313 write_u32(&mut buf, 0x0001_0000);
1314 write_u32(&mut buf, 0x0001_0000);
1315 buf.extend_from_slice(sys_path);
1316 while !buf.len().is_multiple_of(8) {
1317 buf.push(0);
1318 }
1319
1320 write_u32(&mut buf, LC_MAIN);
1321 write_u32(&mut buf, main_cmdsize);
1322 write_u64(&mut buf, entryoff);
1323 write_u64(&mut buf, 0);
1324
1325 let target = usize::try_from(text_fileoff).unwrap_or(buf.len());
1326 if buf.len() < target {
1327 buf.resize(target, 0);
1328 }
1329 buf.extend_from_slice(&text);
1330 let target = usize::try_from(file_end).unwrap_or(buf.len());
1331 if buf.len() < target {
1332 buf.resize(target, 0);
1333 }
1334 buf
1335}
1336
1337pub const LIBSYSTEM_INSTALL_NAME: &str = "/usr/lib/libSystem.B.dylib";
1339
1340pub const LIBSYSTEM_TEXT_VA: u64 = 0x0000_0004_0000_0000;
1342
1343pub const CALL_LIBSYSTEM_GOT_VA: u64 = 0x0000_0001_0000_4000;
1345
1346pub const KH_BOTTLE_MARK_SYMBOL: &str = "_kh_bottle_mark";
1349
1350pub const LIBSYSTEM_WRITE_SYMBOL: &str = "_write";
1352
1353pub const LIBSYSTEM_EXIT_SYMBOL: &str = "__exit";
1355
1356pub const LIBSYSTEM_PUTS_SYMBOL: &str = "_puts";
1358
1359pub const LIBSYSTEM_PRINTF_SYMBOL: &str = "_printf";
1361
1362pub const KH_BOTTLE_MARK_VALUE: u32 = 77;
1364
1365const KH_HELPER_PUTS: u32 = 0x4B48_0001;
1367const KH_HELPER_PRINTF: u32 = 0x4B48_0002;
1369
1370#[must_use]
1384#[allow(
1385 clippy::arithmetic_side_effects,
1386 clippy::cast_possible_truncation,
1387 clippy::items_after_statements,
1388 clippy::too_many_lines
1389)]
1390pub fn arm64_libsystem_stub() -> Vec<u8> {
1391 const MH_MAGIC_64: u32 = 0xfeed_facf;
1392 const CPU_TYPE_ARM64: u32 = 0x0100_000c;
1393 const MH_DYLIB: u32 = 0x6;
1394 const MH_NOUNDEFS: u32 = 0x1;
1395 const MH_DYLDLINK: u32 = 0x4;
1396 const LC_SEGMENT_64: u32 = 0x19;
1397 const LC_ID_DYLIB: u32 = 0xd;
1398 const LC_SYMTAB: u32 = 0x2;
1399 const LC_DYSYMTAB: u32 = 0xb;
1400 const LC_UUID: u32 = 0x1b;
1401 const VM_PROT_READ: u32 = 0x1;
1402 const VM_PROT_EXECUTE: u32 = 0x4;
1403 const SEG_CMD: u32 = 72;
1404 const SECT_64: u32 = 80;
1405 const NLIST64: u32 = 16;
1406 const RET: u32 = 0xd65f_03c0;
1407
1408 let id_path = LIBSYSTEM_INSTALL_NAME.as_bytes();
1409 let id_path_nul = {
1410 let mut v = id_path.to_vec();
1411 v.push(0);
1412 v
1413 };
1414 let id_path_pad = align8(id_path_nul.len());
1415 let id_cmdsize = 24 + id_path_pad;
1416
1417 let text_seg_cmdsize = SEG_CMD + SECT_64;
1418 let uuid_cmdsize: u32 = 24;
1419 let symtab_cmdsize: u32 = 24;
1420 let dysymtab_cmdsize: u32 = 80;
1421
1422 let ncmds = 5_u32;
1423 let sizeofcmds = text_seg_cmdsize
1424 + u32::try_from(id_cmdsize).unwrap_or(0)
1425 + uuid_cmdsize
1426 + symtab_cmdsize
1427 + dysymtab_cmdsize;
1428
1429 let header_size = 32_u64;
1430 let load_end = header_size + u64::from(sizeofcmds);
1431 let text_fileoff = align_u64(load_end, 16);
1432
1433 let mut text_code = Vec::with_capacity(64);
1440 push_u32(&mut text_code, movz(16, 4)); push_u32(&mut text_code, svc80());
1442 push_u32(&mut text_code, RET);
1443 push_u32(&mut text_code, movz(16, 1)); push_u32(&mut text_code, svc80());
1445 push_u32(&mut text_code, movz(0, KH_BOTTLE_MARK_VALUE)); push_u32(&mut text_code, RET);
1447 push_helper_call(&mut text_code, KH_HELPER_PUTS); push_helper_call(&mut text_code, KH_HELPER_PRINTF); while text_code.len() < 64 {
1450 text_code.push(0);
1451 }
1452 let text_filesize = 64_u64;
1453 let text_end = text_fileoff + text_filesize;
1454
1455 let exports: [(&str, u64); 5] = [
1456 (LIBSYSTEM_WRITE_SYMBOL, 0),
1457 (LIBSYSTEM_EXIT_SYMBOL, 12),
1458 (KH_BOTTLE_MARK_SYMBOL, 20),
1459 (LIBSYSTEM_PUTS_SYMBOL, 28),
1460 (LIBSYSTEM_PRINTF_SYMBOL, 44),
1461 ];
1462 let nsyms = u32::try_from(exports.len()).unwrap_or(0);
1463
1464 let symoff = align_u64(text_end, 8);
1465 let stroff = symoff + u64::from(NLIST64) * u64::from(nsyms);
1466 let mut strtab = vec![0_u8];
1467 let mut strx: Vec<u32> = Vec::with_capacity(exports.len());
1468 for (name, _) in &exports {
1469 strx.push(u32::try_from(strtab.len()).unwrap_or(0));
1470 strtab.extend_from_slice(name.as_bytes());
1471 strtab.push(0);
1472 }
1473 let strsize = u32::try_from(strtab.len()).unwrap_or(0);
1474 let file_end = stroff + u64::from(strsize);
1475
1476 let text_vmaddr = LIBSYSTEM_TEXT_VA;
1477 let text_vmsize = 0x4000_u64;
1478 let text_sect_addr = text_vmaddr + text_fileoff;
1479
1480 let mut buf = Vec::with_capacity(usize::try_from(file_end).unwrap_or(512));
1481
1482 write_u32(&mut buf, MH_MAGIC_64);
1483 write_u32(&mut buf, CPU_TYPE_ARM64);
1484 write_u32(&mut buf, 0);
1485 write_u32(&mut buf, MH_DYLIB);
1486 write_u32(&mut buf, ncmds);
1487 write_u32(&mut buf, sizeofcmds);
1488 write_u32(&mut buf, MH_NOUNDEFS | MH_DYLDLINK);
1489 write_u32(&mut buf, 0);
1490
1491 write_u32(&mut buf, LC_SEGMENT_64);
1492 write_u32(&mut buf, text_seg_cmdsize);
1493 write_segname(&mut buf, b"__TEXT");
1494 write_u64(&mut buf, text_vmaddr);
1495 write_u64(&mut buf, text_vmsize);
1496 write_u64(&mut buf, 0);
1497 write_u64(&mut buf, file_end);
1498 write_u32(&mut buf, VM_PROT_READ | VM_PROT_EXECUTE);
1499 write_u32(&mut buf, VM_PROT_READ | VM_PROT_EXECUTE);
1500 write_u32(&mut buf, 1);
1501 write_u32(&mut buf, 0);
1502
1503 write_sectname(&mut buf, b"__text");
1504 write_segname(&mut buf, b"__TEXT");
1505 write_u64(&mut buf, text_sect_addr);
1506 write_u64(&mut buf, text_filesize);
1507 write_u32(&mut buf, u32::try_from(text_fileoff).unwrap_or(0));
1508 write_u32(&mut buf, 2);
1509 write_u32(&mut buf, 0);
1510 write_u32(&mut buf, 0);
1511 write_u32(&mut buf, 0x8000_0400);
1512 write_u32(&mut buf, 0);
1513 write_u32(&mut buf, 0);
1514 write_u32(&mut buf, 0);
1515
1516 write_u32(&mut buf, LC_ID_DYLIB);
1517 write_u32(&mut buf, u32::try_from(id_cmdsize).unwrap_or(0));
1518 write_u32(&mut buf, 24);
1519 write_u32(&mut buf, 1);
1520 write_u32(&mut buf, 0x0001_0000);
1521 write_u32(&mut buf, 0x0001_0000);
1522 buf.extend_from_slice(&id_path_nul);
1523 while !buf.len().is_multiple_of(8) {
1524 buf.push(0);
1525 }
1526
1527 write_u32(&mut buf, LC_UUID);
1528 write_u32(&mut buf, uuid_cmdsize);
1529 buf.extend_from_slice(&[
1530 0xB0, 0x77, 0x1E, 0x00, 0x11, 0xB5, 0x75, 0x7E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1531 0x08,
1532 ]);
1533
1534 write_u32(&mut buf, LC_SYMTAB);
1535 write_u32(&mut buf, symtab_cmdsize);
1536 write_u32(&mut buf, u32::try_from(symoff).unwrap_or(0));
1537 write_u32(&mut buf, nsyms);
1538 write_u32(&mut buf, u32::try_from(stroff).unwrap_or(0));
1539 write_u32(&mut buf, strsize);
1540
1541 write_u32(&mut buf, LC_DYSYMTAB);
1542 write_u32(&mut buf, dysymtab_cmdsize);
1543 write_u32(&mut buf, 0);
1545 write_u32(&mut buf, 0);
1546 write_u32(&mut buf, 0); write_u32(&mut buf, nsyms); for _ in 0..14 {
1549 write_u32(&mut buf, 0);
1550 }
1551
1552 let target = usize::try_from(text_fileoff).unwrap_or(buf.len());
1553 if buf.len() < target {
1554 buf.resize(target, 0);
1555 }
1556 buf.extend_from_slice(&text_code);
1557 let target = usize::try_from(text_end).unwrap_or(buf.len());
1558 if buf.len() < target {
1559 buf.resize(target, 0);
1560 }
1561
1562 let target = usize::try_from(symoff).unwrap_or(buf.len());
1563 if buf.len() < target {
1564 buf.resize(target, 0);
1565 }
1566 for (i, (_, code_off)) in exports.iter().enumerate() {
1567 let sx = strx.get(i).copied().unwrap_or(0);
1568 write_u32(&mut buf, sx);
1569 buf.push(0x0f); buf.push(1); write_u16(&mut buf, 0);
1572 write_u64(&mut buf, text_sect_addr.saturating_add(*code_off));
1573 }
1574
1575 let target = usize::try_from(stroff).unwrap_or(buf.len());
1576 if buf.len() < target {
1577 buf.resize(target, 0);
1578 }
1579 buf.extend_from_slice(&strtab);
1580
1581 let target = usize::try_from(file_end).unwrap_or(buf.len());
1582 if buf.len() < target {
1583 buf.resize(target, 0);
1584 }
1585 buf
1586}
1587
1588#[must_use]
1597#[allow(
1598 clippy::arithmetic_side_effects,
1599 clippy::cast_possible_truncation,
1600 clippy::cast_possible_wrap,
1601 clippy::cast_sign_loss,
1602 clippy::items_after_statements,
1603 clippy::too_many_lines
1604)]
1605pub fn call_libsystem_exit() -> Vec<u8> {
1606 const MH_MAGIC_64: u32 = 0xfeed_facf;
1607 const CPU_TYPE_ARM64: u32 = 0x0100_000c;
1608 const MH_EXECUTE: u32 = 0x2;
1609 const MH_NOUNDEFS: u32 = 0x1;
1610 const MH_PIE: u32 = 0x0020_0000;
1611 const LC_SEGMENT_64: u32 = 0x19;
1612 const LC_UUID: u32 = 0x1b;
1613 const LC_LOAD_DYLIB: u32 = 0xc;
1614 const LC_SYMTAB: u32 = 0x2;
1615 const LC_DYLD_INFO_ONLY: u32 = 0x22 | 0x8000_0000;
1616 const LC_MAIN: u32 = 0x28 | 0x8000_0000;
1617 const VM_PROT_READ: u32 = 0x1;
1618 const VM_PROT_WRITE: u32 = 0x2;
1619 const VM_PROT_EXECUTE: u32 = 0x4;
1620 const SEG_CMD: u32 = 72;
1621 const SECT_64: u32 = 80;
1622 const NLIST64: u32 = 16;
1623 const DYLD_INFO_CMDSIZE: u32 = 48;
1624
1625 let sys_path = LIBSYSTEM_INSTALL_NAME.as_bytes();
1626 let sys_pad = align8(sys_path.len() + 1);
1627 let sys_cmdsize = 24 + sys_pad;
1628
1629 let pagezero_cmdsize = SEG_CMD;
1630 let text_seg_cmdsize = SEG_CMD + SECT_64;
1631 let data_seg_cmdsize = SEG_CMD + SECT_64;
1632 let uuid_cmdsize: u32 = 24;
1633 let symtab_cmdsize: u32 = 24;
1634 let main_cmdsize: u32 = 24;
1635
1636 let bind_stream = crate::bind::encode_pointer_bind(1, KH_BOTTLE_MARK_SYMBOL, 2, 0);
1637 let bind_size = u32::try_from(bind_stream.len()).unwrap_or(0);
1638
1639 let ncmds = 8_u32;
1640 let sizeofcmds = pagezero_cmdsize
1641 + text_seg_cmdsize
1642 + data_seg_cmdsize
1643 + uuid_cmdsize
1644 + u32::try_from(sys_cmdsize).unwrap_or(0)
1645 + DYLD_INFO_CMDSIZE
1646 + symtab_cmdsize
1647 + main_cmdsize;
1648
1649 let header_size = 32_u64;
1650 let load_end = header_size + u64::from(sizeofcmds);
1651 let text_fileoff = align_u64(load_end, 16);
1652
1653 let text_len = 32_u64;
1660 let text_end = text_fileoff + text_len;
1661
1662 let data_fileoff = align_u64(text_end, 8);
1663 let got_filesize = 8_u64;
1664 let data_end = data_fileoff + got_filesize;
1665
1666 let bind_off = align_u64(data_end, 8);
1667 let bind_end = bind_off + u64::from(bind_size);
1668
1669 let symoff = align_u64(bind_end, 8);
1670 let nsyms = 1_u32;
1671 let stroff = symoff + u64::from(NLIST64) * u64::from(nsyms);
1672 let mut strtab = vec![0_u8];
1673 strtab.extend_from_slice(KH_BOTTLE_MARK_SYMBOL.as_bytes());
1674 strtab.push(0);
1675 let strsize = u32::try_from(strtab.len()).unwrap_or(0);
1676 let file_end = stroff + u64::from(strsize);
1677
1678 let text_vmaddr = 0x0000_0001_0000_0000_u64;
1679 let text_vmsize = 0x4000_u64;
1680 let data_vmaddr = CALL_LIBSYSTEM_GOT_VA;
1681 let data_vmsize = 0x4000_u64;
1682 let entryoff = text_fileoff;
1683 let entry_va = text_vmaddr + entryoff;
1684 let adrp_pc = entry_va;
1686 let adrp_insn = encode_adrp(16, adrp_pc, data_vmaddr);
1687 let pageoff = u32::try_from(data_vmaddr & 0xfff).unwrap_or(0);
1688 let ldr_insn = encode_ldr_x_uoff(16, 16, pageoff);
1689
1690 let mut text = Vec::with_capacity(32);
1691 push_u32(&mut text, adrp_insn);
1692 push_u32(&mut text, ldr_insn);
1693 push_u32(&mut text, 0xd63f_0200); push_u32(&mut text, movz(0x10, 1)); push_u32(&mut text, svc80());
1696 while text.len() < usize::try_from(text_len).unwrap_or(32) {
1697 text.push(0);
1698 }
1699
1700 let mut buf = Vec::with_capacity(usize::try_from(file_end).unwrap_or(512));
1701
1702 write_u32(&mut buf, MH_MAGIC_64);
1703 write_u32(&mut buf, CPU_TYPE_ARM64);
1704 write_u32(&mut buf, 0);
1705 write_u32(&mut buf, MH_EXECUTE);
1706 write_u32(&mut buf, ncmds);
1707 write_u32(&mut buf, sizeofcmds);
1708 write_u32(&mut buf, MH_NOUNDEFS | MH_PIE);
1709 write_u32(&mut buf, 0);
1710
1711 write_u32(&mut buf, LC_SEGMENT_64);
1712 write_u32(&mut buf, pagezero_cmdsize);
1713 write_segname(&mut buf, b"__PAGEZERO");
1714 write_u64(&mut buf, 0);
1715 write_u64(&mut buf, 0x0000_0001_0000_0000);
1716 write_u64(&mut buf, 0);
1717 write_u64(&mut buf, 0);
1718 write_u32(&mut buf, 0);
1719 write_u32(&mut buf, 0);
1720 write_u32(&mut buf, 0);
1721 write_u32(&mut buf, 0);
1722
1723 write_u32(&mut buf, LC_SEGMENT_64);
1724 write_u32(&mut buf, text_seg_cmdsize);
1725 write_segname(&mut buf, b"__TEXT");
1726 write_u64(&mut buf, text_vmaddr);
1727 write_u64(&mut buf, text_vmsize);
1728 write_u64(&mut buf, 0);
1729 write_u64(&mut buf, data_fileoff);
1730 write_u32(&mut buf, VM_PROT_READ | VM_PROT_EXECUTE);
1731 write_u32(&mut buf, VM_PROT_READ | VM_PROT_EXECUTE);
1732 write_u32(&mut buf, 1);
1733 write_u32(&mut buf, 0);
1734
1735 write_sectname(&mut buf, b"__text");
1736 write_segname(&mut buf, b"__TEXT");
1737 write_u64(&mut buf, text_vmaddr + text_fileoff);
1738 write_u64(&mut buf, text_len);
1739 write_u32(&mut buf, u32::try_from(text_fileoff).unwrap_or(0));
1740 write_u32(&mut buf, 2);
1741 write_u32(&mut buf, 0);
1742 write_u32(&mut buf, 0);
1743 write_u32(&mut buf, 0x8000_0400);
1744 write_u32(&mut buf, 0);
1745 write_u32(&mut buf, 0);
1746 write_u32(&mut buf, 0);
1747
1748 write_u32(&mut buf, LC_SEGMENT_64);
1749 write_u32(&mut buf, data_seg_cmdsize);
1750 write_segname(&mut buf, b"__DATA");
1751 write_u64(&mut buf, data_vmaddr);
1752 write_u64(&mut buf, data_vmsize);
1753 write_u64(&mut buf, data_fileoff);
1754 write_u64(&mut buf, got_filesize);
1755 write_u32(&mut buf, VM_PROT_READ | VM_PROT_WRITE);
1756 write_u32(&mut buf, VM_PROT_READ | VM_PROT_WRITE);
1757 write_u32(&mut buf, 1);
1758 write_u32(&mut buf, 0);
1759
1760 write_sectname(&mut buf, b"__got");
1761 write_segname(&mut buf, b"__DATA");
1762 write_u64(&mut buf, data_vmaddr);
1763 write_u64(&mut buf, got_filesize);
1764 write_u32(&mut buf, u32::try_from(data_fileoff).unwrap_or(0));
1765 write_u32(&mut buf, 3);
1766 write_u32(&mut buf, 0);
1767 write_u32(&mut buf, 0);
1768 write_u32(&mut buf, 0x6);
1769 write_u32(&mut buf, 0);
1770 write_u32(&mut buf, 0);
1771 write_u32(&mut buf, 0);
1772
1773 write_u32(&mut buf, LC_UUID);
1774 write_u32(&mut buf, uuid_cmdsize);
1775 buf.extend_from_slice(&[
1776 0xB0, 0x77, 0xCA, 0x11, 0x11, 0xB5, 0x75, 0x7E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1777 0x01,
1778 ]);
1779
1780 write_u32(&mut buf, LC_LOAD_DYLIB);
1781 write_u32(&mut buf, u32::try_from(sys_cmdsize).unwrap_or(0));
1782 write_u32(&mut buf, 24);
1783 write_u32(&mut buf, 2);
1784 write_u32(&mut buf, 0x0001_0000);
1785 write_u32(&mut buf, 0x0001_0000);
1786 buf.extend_from_slice(sys_path);
1787 buf.push(0);
1788 while !buf.len().is_multiple_of(8) {
1789 buf.push(0);
1790 }
1791
1792 write_u32(&mut buf, LC_DYLD_INFO_ONLY);
1793 write_u32(&mut buf, DYLD_INFO_CMDSIZE);
1794 write_u32(&mut buf, 0);
1795 write_u32(&mut buf, 0);
1796 write_u32(&mut buf, u32::try_from(bind_off).unwrap_or(0));
1797 write_u32(&mut buf, bind_size);
1798 write_u32(&mut buf, 0);
1799 write_u32(&mut buf, 0);
1800 write_u32(&mut buf, 0);
1801 write_u32(&mut buf, 0);
1802 write_u32(&mut buf, 0);
1803 write_u32(&mut buf, 0);
1804
1805 write_u32(&mut buf, LC_SYMTAB);
1806 write_u32(&mut buf, symtab_cmdsize);
1807 write_u32(&mut buf, u32::try_from(symoff).unwrap_or(0));
1808 write_u32(&mut buf, nsyms);
1809 write_u32(&mut buf, u32::try_from(stroff).unwrap_or(0));
1810 write_u32(&mut buf, strsize);
1811
1812 write_u32(&mut buf, LC_MAIN);
1813 write_u32(&mut buf, main_cmdsize);
1814 write_u64(&mut buf, entryoff);
1815 write_u64(&mut buf, 0);
1816
1817 let target = usize::try_from(text_fileoff).unwrap_or(buf.len());
1818 if buf.len() < target {
1819 buf.resize(target, 0);
1820 }
1821 buf.extend_from_slice(&text);
1822
1823 let target = usize::try_from(data_fileoff).unwrap_or(buf.len());
1824 if buf.len() < target {
1825 buf.resize(target, 0);
1826 }
1827 buf.extend_from_slice(&[0_u8; 8]);
1828
1829 let target = usize::try_from(bind_off).unwrap_or(buf.len());
1830 if buf.len() < target {
1831 buf.resize(target, 0);
1832 }
1833 buf.extend_from_slice(&bind_stream);
1834
1835 let target = usize::try_from(symoff).unwrap_or(buf.len());
1836 if buf.len() < target {
1837 buf.resize(target, 0);
1838 }
1839 write_u32(&mut buf, 1); buf.push(0x01); buf.push(0);
1842 write_u16(&mut buf, 0);
1843 write_u64(&mut buf, 0);
1844
1845 let target = usize::try_from(stroff).unwrap_or(buf.len());
1846 if buf.len() < target {
1847 buf.resize(target, 0);
1848 }
1849 buf.extend_from_slice(&strtab);
1850
1851 let target = usize::try_from(file_end).unwrap_or(buf.len());
1852 if buf.len() < target {
1853 buf.resize(target, 0);
1854 }
1855 buf
1856}
1857
1858#[must_use]
1860pub fn minimal_arm64_execute() -> Vec<u8> {
1861 let mut text = Vec::with_capacity(36);
1862 push_u32(&mut text, 0xd280_0020);
1864 push_u32(&mut text, 0x1000_00e1);
1866 push_u32(&mut text, 0xd280_0062);
1868 push_u32(&mut text, 0xd280_0090);
1870 push_u32(&mut text, 0xd400_0001 | (0x80_u32 << 5));
1872 push_u32(&mut text, 0xd280_0000);
1874 push_u32(&mut text, 0xd280_0030);
1876 push_u32(&mut text, 0xd400_0001 | (0x80_u32 << 5));
1878 text.extend_from_slice(b"kh\n");
1879 arm64_execute_with_text(&text)
1880}
1881
1882#[must_use]
1886pub fn errno_unknown_then_exit() -> Vec<u8> {
1887 let mut text = Vec::with_capacity(32);
1888 push_u32(&mut text, movz(0x10, 0xBEEF));
1890 push_u32(&mut text, svc80());
1892 push_u32(&mut text, movz(0x0, 0));
1894 push_u32(&mut text, movz(0x10, 1));
1896 push_u32(&mut text, svc80());
1898 arm64_execute_with_text(&text)
1899}
1900
1901#[must_use]
1907#[allow(
1908 clippy::arithmetic_side_effects,
1909 clippy::cast_possible_truncation,
1910 clippy::cast_possible_wrap,
1911 clippy::cast_sign_loss,
1912 clippy::as_conversions,
1913 clippy::too_many_lines
1914)]
1915pub fn bsdthread_create_join() -> Vec<u8> {
1916 let mut text: Vec<u8> = Vec::with_capacity(256);
1918 let emit = |text: &mut Vec<u8>, w: u32| push_u32(text, w);
1919 #[allow(clippy::integer_division)]
1920 let icount = |text: &[u8]| text.len() / 4;
1921
1922 emit(&mut text, sub_imm(31, 31, 16));
1925 emit(&mut text, str_x_uoff(31, 31, 0)); let adr_start_idx = icount(&text);
1929 emit(&mut text, 0); emit(&mut text, movz(1, 0));
1931 emit(&mut text, movz(2, 0));
1932 emit(&mut text, movz(3, 0));
1933 emit(&mut text, movz(4, 0));
1934 emit(&mut text, movz(5, 0));
1935 emit(&mut text, movz(6, 0));
1936 emit(&mut text, movz(0x10, 366));
1937 emit(&mut text, svc80());
1938 let bcs_reg = icount(&text);
1939 emit(&mut text, 0); emit(&mut text, movz(0, 0));
1943 emit(&mut text, movz(1, 0x4000));
1944 emit(&mut text, movz(2, 3));
1945 emit(&mut text, movz(3, 0x1002));
1946 emit(&mut text, 0x9280_0004); emit(&mut text, movz(5, 0));
1948 emit(&mut text, movz(0x10, 197));
1949 emit(&mut text, svc80());
1950 let bcs_map = icount(&text);
1951 emit(&mut text, 0); emit(&mut text, mov_reg(19, 0)); emit(&mut text, add_imm_lsl12(20, 19, 4)); emit(&mut text, sub_imm(2, 20, 0x20)); emit(&mut text, sub_imm(3, 20, 0x200)); let adr_worker_idx = icount(&text);
1963 emit(&mut text, 0); emit(&mut text, add_imm(1, 31, 0)); emit(&mut text, movz(4, 0));
1967 emit(&mut text, movz(0x10, 360));
1968 emit(&mut text, svc80());
1969 let bcs_create = icount(&text);
1970 emit(&mut text, 0); let spin_idx = icount(&text);
1974 emit(&mut text, ldrb_x(0, 31)); let cbz_idx = icount(&text);
1976 emit(&mut text, 0); emit(&mut text, movz(0, 0));
1980 emit(&mut text, movz(0x10, 1));
1981 emit(&mut text, svc80());
1982
1983 let fail_idx = icount(&text);
1985 emit(&mut text, movz(0, 1));
1986 emit(&mut text, movz(0x10, 1));
1987 emit(&mut text, svc80());
1988
1989 let worker_idx = icount(&text);
1991 emit(&mut text, mov_reg(19, 0)); emit(&mut text, movz(0, 1)); let adr_msg_idx = icount(&text);
1994 emit(&mut text, 0); emit(&mut text, movz(2, 2)); emit(&mut text, movz(0x10, 4)); emit(&mut text, svc80());
1998 emit(&mut text, 0x5280_0000 | (1_u32 << 5)); emit(&mut text, strb_x(0, 19)); emit(&mut text, 0xD503_3BBF); emit(&mut text, 0xD65F_03C0); let start_idx = icount(&text);
2005 emit(&mut text, 0xA9BF_7BFD); emit(&mut text, mov_reg(0, 3)); emit(&mut text, 0xD63F_0000 | (2_u32 << 5)); emit(&mut text, movz(0, 0));
2009 emit(&mut text, movz(1, 0));
2010 emit(&mut text, movz(2, 0));
2011 emit(&mut text, movz(3, 0));
2012 emit(&mut text, movz(0x10, 361)); emit(&mut text, svc80());
2014 emit(&mut text, 0x1400_0000); let msg_off = text.len();
2018 text.extend_from_slice(b"T\n");
2019 while !text.len().is_multiple_of(4) {
2020 text.push(0);
2021 }
2022
2023 let fail_imm = i32::try_from(fail_idx).unwrap_or(0) - i32::try_from(bcs_reg).unwrap_or(0);
2025 patch_u32(&mut text, bcs_reg, b_cond(0x2, fail_imm)); let fail_imm = i32::try_from(fail_idx).unwrap_or(0) - i32::try_from(bcs_map).unwrap_or(0);
2027 patch_u32(&mut text, bcs_map, b_cond(0x2, fail_imm));
2028 let fail_imm = i32::try_from(fail_idx).unwrap_or(0) - i32::try_from(bcs_create).unwrap_or(0);
2029 patch_u32(&mut text, bcs_create, b_cond(0x2, fail_imm));
2030
2031 let spin_imm = i32::try_from(spin_idx).unwrap_or(0) - i32::try_from(cbz_idx).unwrap_or(0);
2032 patch_u32(&mut text, cbz_idx, cbz_w(0, spin_imm));
2033
2034 let start_byte =
2035 (i32::try_from(start_idx).unwrap_or(0) - i32::try_from(adr_start_idx).unwrap_or(0)) * 4;
2036 patch_u32(&mut text, adr_start_idx, adr(0, start_byte));
2037
2038 let worker_byte =
2039 (i32::try_from(worker_idx).unwrap_or(0) - i32::try_from(adr_worker_idx).unwrap_or(0)) * 4;
2040 patch_u32(&mut text, adr_worker_idx, adr(0, worker_byte));
2041
2042 let msg_byte =
2043 i32::try_from(msg_off).unwrap_or(0) - (i32::try_from(adr_msg_idx).unwrap_or(0) * 4);
2044 patch_u32(&mut text, adr_msg_idx, adr(1, msg_byte));
2045
2046 arm64_execute_with_text(&text)
2047}
2048
2049#[must_use]
2053#[allow(clippy::arithmetic_side_effects)]
2054pub fn mmap_touch_exit() -> Vec<u8> {
2055 let mut text = Vec::with_capacity(80);
2058 push_u32(&mut text, movz(0x0, 0));
2059 push_u32(&mut text, movz(0x1, 0x1000));
2060 push_u32(&mut text, movz(0x2, 0x3));
2061 push_u32(&mut text, movz(0x3, 0x1002));
2062 push_u32(&mut text, 0x9280_0000 | 0x4);
2064 push_u32(&mut text, movz(0x5, 0));
2065 push_u32(&mut text, movz(0x10, 197));
2066 push_u32(&mut text, svc80());
2067 push_u32(&mut text, 0xaa00_03f3);
2069 push_u32(&mut text, 0x5280_0000 | (0x42_u32 << 5) | 0x14);
2071 push_u32(&mut text, 0x3900_0274);
2073 push_u32(&mut text, 0xaa13_03e0);
2075 push_u32(&mut text, movz(0x1, 0x1000));
2076 push_u32(&mut text, movz(0x10, 73));
2077 push_u32(&mut text, svc80());
2078 push_u32(&mut text, movz(0x0, 0));
2079 push_u32(&mut text, movz(0x10, 1));
2080 push_u32(&mut text, svc80());
2081 arm64_execute_with_text(&text)
2082}
2083
2084pub const ROUNDTRIP_PAYLOAD: &[u8] = b"KAKEMAP01\n";
2086
2087pub const ROUNDTRIP_PAYLOAD_PATH: &str = "tests/fixtures/roundtrip_payload.bin";
2089
2090#[must_use]
2095#[allow(unknown_lints)]
2097#[allow(
2098 clippy::arithmetic_side_effects,
2099 clippy::as_conversions,
2100 clippy::cast_possible_truncation,
2101 clippy::cast_possible_wrap,
2102 clippy::cast_sign_loss,
2103 clippy::decimal_bitwise_operands,
2104 clippy::indexing_slicing,
2105 clippy::integer_division,
2106 clippy::too_many_lines
2107)]
2108pub fn memory_file_roundtrip() -> Vec<u8> {
2109 let path = ROUNDTRIP_PAYLOAD_PATH.as_bytes();
2113 let key = b"hw.ncpu\0";
2114
2115 let mut text: Vec<u8> = Vec::with_capacity(512);
2116 let mut bcs: Vec<(usize, u8)> = Vec::new(); let mut bne: Vec<(usize, u8)> = Vec::new();
2118
2119 let emit = |text: &mut Vec<u8>, w: u32| {
2120 push_u32(text, w);
2121 };
2122 let icount = |text: &[u8]| text.len() / 4;
2123
2124 emit(&mut text, sub_imm(31, 31, 0x100));
2126
2127 let adr_path_idx = icount(&text);
2130 emit(&mut text, 0); emit(&mut text, movz(1, 2)); emit(&mut text, movz(0x10, 5)); emit(&mut text, svc80());
2134 bcs.push((icount(&text), 2));
2135 emit(&mut text, 0); emit(&mut text, mov_reg(19, 0)); emit(&mut text, mov_reg(0, 19));
2141 emit(&mut text, add_imm(1, 31, 0)); emit(&mut text, movz(0x10, 339));
2143 emit(&mut text, svc80());
2144 bcs.push((icount(&text), 3));
2145 emit(&mut text, 0);
2146
2147 emit(&mut text, ldr_x_uoff(21, 31, 96));
2149 emit(&mut text, cmp_x_imm(21, 9));
2151 bne.push((icount(&text), 3));
2152 emit(&mut text, 0); emit(&mut text, movz(0, 0));
2156 emit(&mut text, movz(1, 0x1000));
2157 emit(&mut text, movz(2, 3));
2158 emit(&mut text, movz(3, 1)); emit(&mut text, mov_reg(4, 19));
2160 emit(&mut text, movz(5, 0));
2161 emit(&mut text, movz(0x10, 197));
2162 emit(&mut text, svc80());
2163 bcs.push((icount(&text), 4));
2164 emit(&mut text, 0);
2165 emit(&mut text, mov_reg(20, 0)); emit(&mut text, ldrb_x(21, 20)); emit(&mut text, cmp_w_imm(21, u32::from(b'K')));
2170 bne.push((icount(&text), 5));
2171 emit(&mut text, 0); emit(&mut text, 0x5280_0000 | (u32::from(b'X') << 5) | 21); emit(&mut text, strb_x(21, 20)); emit(&mut text, mov_reg(0, 20));
2179 emit(&mut text, movz(1, 0x1000));
2180 emit(&mut text, movz(2, 1));
2181 emit(&mut text, movz(0x10, 74));
2182 emit(&mut text, svc80());
2183 bcs.push((icount(&text), 6));
2184 emit(&mut text, 0);
2185
2186 emit(&mut text, mov_reg(0, 20));
2188 emit(&mut text, movz(1, 0x1000));
2189 emit(&mut text, movz(2, 3));
2190 emit(&mut text, movz(0x10, 74));
2191 emit(&mut text, svc80());
2192 bcs.push((icount(&text), 6));
2193 emit(&mut text, 0);
2194
2195 emit(&mut text, mov_reg(0, 20));
2197 emit(&mut text, movz(1, 0x1000));
2198 emit(&mut text, movz(2, 0x10));
2199 emit(&mut text, movz(0x10, 65));
2200 emit(&mut text, svc80());
2201 bcs.push((icount(&text), 7));
2202 emit(&mut text, 0);
2203
2204 emit(&mut text, movz(0, 0));
2206 emit(&mut text, add_imm(1, 31, 0xA0)); emit(&mut text, movz(0x10, 266));
2208 emit(&mut text, svc80());
2209 bcs.push((icount(&text), 8));
2210 emit(&mut text, 0);
2211 emit(&mut text, ldr_x_uoff(21, 31, 0xA0));
2213 emit(&mut text, cmp_x_imm(21, 0));
2214 bne.push((icount(&text), 8));
2215 emit(&mut text, 0); let adr_key_idx = icount(&text);
2219 emit(&mut text, 0); emit(&mut text, movz(21, 8));
2222 emit(&mut text, str_x_uoff(21, 31, 0xB0));
2223 emit(&mut text, add_imm(1, 31, 0xC0)); emit(&mut text, add_imm(2, 31, 0xB0)); emit(&mut text, movz(3, 0));
2226 emit(&mut text, movz(4, 0));
2227 emit(&mut text, movz(0x10, 274));
2228 emit(&mut text, svc80());
2229 bcs.push((icount(&text), 9));
2230 emit(&mut text, 0);
2231 emit(&mut text, ldr_w_uoff(21, 31, 0xC0));
2233 emit(&mut text, cmp_w_imm(21, 0));
2234 bne.push((icount(&text), 9));
2235 emit(&mut text, 0); emit(&mut text, mov_reg(0, 20));
2239 emit(&mut text, movz(1, 0x1000));
2240 emit(&mut text, movz(0x10, 73));
2241 emit(&mut text, svc80());
2242 bcs.push((icount(&text), 10));
2243 emit(&mut text, 0);
2244
2245 emit(&mut text, mov_reg(0, 19));
2247 emit(&mut text, movz(0x10, 6));
2248 emit(&mut text, svc80());
2249 bcs.push((icount(&text), 11));
2250 emit(&mut text, 0);
2251
2252 emit(&mut text, movz(0, 0));
2254 emit(&mut text, movz(0x10, 1));
2255 emit(&mut text, svc80());
2256
2257 let mut fail_at = [0_usize; 16];
2260 for code in 2_u8..=11 {
2261 fail_at[usize::from(code)] = icount(&text);
2262 emit(&mut text, movz(0, u32::from(code)));
2263 emit(&mut text, movz(0x10, 1));
2264 emit(&mut text, svc80());
2265 }
2266
2267 for (idx, code) in &bcs {
2269 let target = fail_at[usize::from(*code)];
2270 let imm = i32::try_from(target).unwrap_or(0) - i32::try_from(*idx).unwrap_or(0);
2271 patch_u32(&mut text, *idx, b_cond(0x2, imm)); }
2273 let conds = [0xB_u32, 0x1, 0x0, 0x0]; for (i, (idx, code)) in bne.iter().enumerate() {
2282 let target = fail_at[usize::from(*code)];
2283 let imm = i32::try_from(target).unwrap_or(0) - i32::try_from(*idx).unwrap_or(0);
2284 let cond = conds.get(i).copied().unwrap_or(0x1);
2285 patch_u32(&mut text, *idx, b_cond(cond, imm));
2286 }
2287
2288 let path_off = text.len();
2290 text.extend_from_slice(path);
2291 text.push(0);
2292 while !text.len().is_multiple_of(4) {
2293 text.push(0);
2294 }
2295 let key_off = text.len();
2296 text.extend_from_slice(key);
2297 while !text.len().is_multiple_of(4) {
2298 text.push(0);
2299 }
2300
2301 let path_imm = i32::try_from(path_off).unwrap_or(0)
2304 - i32::try_from(adr_path_idx.saturating_mul(4)).unwrap_or(0);
2305 patch_u32(&mut text, adr_path_idx, adr(0, path_imm));
2306 let key_imm = i32::try_from(key_off).unwrap_or(0)
2307 - i32::try_from(adr_key_idx.saturating_mul(4)).unwrap_or(0);
2308 patch_u32(&mut text, adr_key_idx, adr(0, key_imm));
2309
2310 arm64_execute_with_text(&text)
2311}
2312
2313#[allow(
2315 clippy::integer_division,
2316 clippy::as_conversions,
2317 clippy::cast_sign_loss
2318)]
2319const fn sub_imm(rd: u32, rn: u32, imm12: u32) -> u32 {
2320 0xD100_0000 | ((imm12 & 0xFFF) << 10) | ((rn & 0x1F) << 5) | (rd & 0x1F)
2321}
2322
2323const fn add_imm(rd: u32, rn: u32, imm12: u32) -> u32 {
2325 0x9100_0000 | ((imm12 & 0xFFF) << 10) | ((rn & 0x1F) << 5) | (rd & 0x1F)
2326}
2327
2328const fn add_imm_lsl12(rd: u32, rn: u32, imm12: u32) -> u32 {
2330 0x9100_0000 | (1 << 22) | ((imm12 & 0xFFF) << 10) | ((rn & 0x1F) << 5) | (rd & 0x1F)
2331}
2332
2333#[allow(clippy::as_conversions, clippy::cast_sign_loss)]
2335const fn cbz_w(rt: u32, imm_instr: i32) -> u32 {
2336 let imm19 = imm_instr.cast_unsigned() & 0x7_FFFF;
2337 0x3400_0000 | (imm19 << 5) | (rt & 0x1F)
2338}
2339
2340const fn mov_reg(rd: u32, rm: u32) -> u32 {
2342 0xAA00_03E0 | ((rm & 0x1F) << 16) | (rd & 0x1F)
2343}
2344
2345#[allow(clippy::as_conversions, clippy::cast_sign_loss)]
2347const fn adr(rd: u32, imm: i32) -> u32 {
2348 let imm_u = imm.cast_unsigned();
2349 let immlo = imm_u & 0x3;
2350 let immhi = (imm_u >> 2) & 0x7_FFFF;
2351 0x1000_0000 | (immlo << 29) | (immhi << 5) | (rd & 0x1F)
2352}
2353
2354#[allow(clippy::as_conversions, clippy::cast_sign_loss)]
2356const fn b_cond(cond: u32, imm_instr: i32) -> u32 {
2357 let imm19 = imm_instr.cast_unsigned() & 0x7_FFFF;
2358 0x5400_0000 | (imm19 << 5) | (cond & 0xF)
2359}
2360
2361#[allow(clippy::integer_division)]
2363const fn ldr_x_uoff(rt: u32, rn: u32, imm: u32) -> u32 {
2364 let imm12 = (imm / 8) & 0xFFF;
2365 0xF940_0000 | (imm12 << 10) | ((rn & 0x1F) << 5) | (rt & 0x1F)
2366}
2367
2368#[allow(clippy::integer_division)]
2370const fn str_x_uoff(rt: u32, rn: u32, imm: u32) -> u32 {
2371 let imm12 = (imm / 8) & 0xFFF;
2372 0xF900_0000 | (imm12 << 10) | ((rn & 0x1F) << 5) | (rt & 0x1F)
2373}
2374
2375#[allow(clippy::integer_division)]
2377const fn ldr_w_uoff(rt: u32, rn: u32, imm: u32) -> u32 {
2378 let imm12 = (imm / 4) & 0xFFF;
2379 0xB940_0000 | (imm12 << 10) | ((rn & 0x1F) << 5) | (rt & 0x1F)
2380}
2381
2382const fn cmp_x_imm(rn: u32, imm12: u32) -> u32 {
2384 0xF100_0000 | ((imm12 & 0xFFF) << 10) | ((rn & 0x1F) << 5) | 31
2385}
2386
2387const fn cmp_w_imm(rn: u32, imm12: u32) -> u32 {
2389 0x7100_0000 | ((imm12 & 0xFFF) << 10) | ((rn & 0x1F) << 5) | 31
2390}
2391
2392const fn ldrb_x(rt: u32, rn: u32) -> u32 {
2394 0x3940_0000 | ((rn & 0x1F) << 5) | (rt & 0x1F)
2395}
2396
2397const fn strb_x(rt: u32, rn: u32) -> u32 {
2399 0x3900_0000 | ((rn & 0x1F) << 5) | (rt & 0x1F)
2400}
2401
2402fn patch_u32(buf: &mut [u8], instr_idx: usize, word: u32) {
2403 let off = instr_idx.saturating_mul(4);
2404 if let Some(slot) = buf.get_mut(off..off.saturating_add(4)) {
2405 slot.copy_from_slice(&word.to_le_bytes());
2406 }
2407}
2408
2409const fn movz(rd: u32, imm16: u32) -> u32 {
2411 0xd280_0000 | ((imm16 & 0xFFFF) << 5) | (rd & 0x1F)
2412}
2413
2414const fn movk_lsl16(rd: u32, imm16: u32) -> u32 {
2416 0xf2a0_0000 | ((imm16 & 0xFFFF) << 5) | (rd & 0x1F)
2417}
2418
2419const fn svc80() -> u32 {
2421 0xd400_0001 | (0x80_u32 << 5)
2422}
2423
2424fn push_u32(buf: &mut Vec<u8>, v: u32) {
2425 buf.extend_from_slice(&v.to_le_bytes());
2426}
2427
2428fn push_helper_call(buf: &mut Vec<u8>, helper: u32) {
2430 const RET: u32 = 0xd65f_03c0;
2431 let lo = helper & 0xFFFF;
2432 let hi = (helper >> 16) & 0xFFFF;
2433 push_u32(buf, movz(16, lo));
2434 push_u32(buf, movk_lsl16(16, hi));
2435 push_u32(buf, svc80());
2436 push_u32(buf, RET);
2437}
2438
2439fn align8(n: usize) -> usize {
2440 n.div_ceil(8).saturating_mul(8)
2441}
2442
2443#[allow(clippy::arithmetic_side_effects)]
2444fn align_u64(value: u64, align: u64) -> u64 {
2445 if align == 0 {
2446 return value;
2447 }
2448 let rem = value % align;
2449 if rem == 0 {
2450 value
2451 } else {
2452 value + (align - rem)
2453 }
2454}
2455
2456fn write_u32(buf: &mut Vec<u8>, v: u32) {
2457 buf.extend_from_slice(&v.to_le_bytes());
2458}
2459
2460fn write_u64(buf: &mut Vec<u8>, v: u64) {
2461 buf.extend_from_slice(&v.to_le_bytes());
2462}
2463
2464fn write_segname(buf: &mut Vec<u8>, name: &[u8]) {
2465 let mut field = [0_u8; 16];
2466 let n = name.len().min(16);
2467 if let Some(dst) = field.get_mut(..n)
2468 && let Some(src) = name.get(..n)
2469 {
2470 dst.copy_from_slice(src);
2471 }
2472 buf.extend_from_slice(&field);
2473}
2474
2475fn write_sectname(buf: &mut Vec<u8>, name: &[u8]) {
2476 write_segname(buf, name);
2477}
2478
2479#[cfg(test)]
2480#[allow(clippy::expect_used, clippy::panic)]
2481mod tests {
2482 use super::*;
2483 use goblin::mach::Mach;
2484
2485 #[test]
2486 fn goblin_accepts_minimal_fixture() {
2487 let bytes = minimal_arm64_execute();
2488 let mach = Mach::parse(&bytes).expect("goblin parse");
2489 match mach {
2490 Mach::Binary(m) => {
2491 assert!(m.is_64);
2492 assert_eq!(m.header.cputype(), 0x0100_000c);
2493 assert!(!m.libs.is_empty());
2494 }
2495 Mach::Fat(_) => panic!("expected thin binary"),
2496 }
2497 }
2498
2499 #[test]
2500 fn goblin_accepts_mmap_and_errno_fixtures() {
2501 for bytes in [errno_unknown_then_exit(), mmap_touch_exit()] {
2502 let mach = Mach::parse(&bytes).expect("goblin parse");
2503 assert!(matches!(mach, Mach::Binary(_)));
2504 }
2505 }
2506
2507 #[test]
2508 fn goblin_accepts_bsdthread_fixture() {
2509 let bytes = bsdthread_create_join();
2510 let mach = Mach::parse(&bytes).expect("goblin parse");
2511 assert!(matches!(mach, Mach::Binary(_)));
2512 assert!(bytes.len() > 100);
2513 }
2514
2515 #[test]
2516 fn goblin_accepts_memory_file_roundtrip() {
2517 let bytes = memory_file_roundtrip();
2518 let mach = Mach::parse(&bytes).expect("goblin parse");
2519 assert!(matches!(mach, Mach::Binary(_)));
2520 assert!(bytes.len() > 400);
2521 }
2522
2523 #[test]
2524 fn goblin_accepts_dylib_and_call_dylib() {
2525 let dylib = arm64_dylib_add();
2526 let main = call_dylib_exit();
2527 for bytes in [&dylib[..], &main[..]] {
2528 let mach = Mach::parse(bytes).expect("goblin parse");
2529 assert!(matches!(mach, Mach::Binary(_)));
2530 }
2531 let dylib_m = match Mach::parse(&dylib).expect("parse dylib") {
2532 Mach::Binary(m) => m,
2533 Mach::Fat(_) => panic!("thin dylib"),
2534 };
2535 assert_eq!(dylib_m.header.filetype, 0x6);
2536 let main_m = match Mach::parse(&main).expect("parse main") {
2537 Mach::Binary(m) => m,
2538 Mach::Fat(_) => panic!("thin main"),
2539 };
2540 assert!(main_m.libs.iter().any(|l| l.contains("libkh_add")));
2541 }
2542
2543 #[test]
2544 fn goblin_accepts_ctor_fixtures() {
2545 let dylib = arm64_dylib_ctor();
2546 let main = ctor_main_exit();
2547 for bytes in [&dylib[..], &main[..]] {
2548 assert!(matches!(
2549 Mach::parse(bytes).expect("parse"),
2550 Mach::Binary(_)
2551 ));
2552 }
2553 }
2554
2555 #[test]
2556 fn goblin_accepts_libsystem_bottle_fixtures() {
2557 let dylib = arm64_libsystem_stub();
2558 let main = call_libsystem_exit();
2559 for bytes in [&dylib[..], &main[..]] {
2560 assert!(matches!(
2561 Mach::parse(bytes).expect("parse"),
2562 Mach::Binary(_)
2563 ));
2564 }
2565 let dylib_m = match Mach::parse(&dylib).expect("parse stub") {
2566 Mach::Binary(m) => m,
2567 Mach::Fat(_) => panic!("thin dylib"),
2568 };
2569 assert_eq!(dylib_m.header.filetype, 0x6);
2570 assert!(
2571 dylib_m
2572 .name
2573 .as_ref()
2574 .is_some_and(|n| n.contains("libSystem")),
2575 "LC_ID_DYLIB should be libSystem, got {:?}",
2576 dylib_m.name
2577 );
2578 let main_m = match Mach::parse(&main).expect("parse main") {
2579 Mach::Binary(m) => m,
2580 Mach::Fat(_) => panic!("thin main"),
2581 };
2582 assert!(
2583 main_m.libs.iter().any(|l| l.contains("libSystem.B.dylib")),
2584 "main must load absolute libSystem: {:?}",
2585 main_m.libs
2586 );
2587 }
2588}