]> git.saurik.com Git - ldid.git/blame - ldid.cpp
Support running ldid on ppc.
[ldid.git] / ldid.cpp
CommitLineData
a362a82f
JF
1/* JocStrap - Java/Objective-C Bootstrap
2 * Copyright (C) 2007 Jay Freeman (saurik)
3*/
4
5/*
6 * Redistribution and use in source and binary
7 * forms, with or without modification, are permitted
8 * provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the
11 * above copyright notice, this list of conditions
12 * and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the
14 * above copyright notice, this list of conditions
15 * and the following disclaimer in the documentation
16 * and/or other materials provided with the
17 * distribution.
18 * 3. The name of the author may not be used to endorse
19 * or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
33 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36*/
37
38#include "minimal/stdlib.h"
b8187ac7 39#include "minimal/string.h"
a362a82f
JF
40#include "minimal/mapping.h"
41
fdb119ef
JF
42#include "sha1.h"
43
a362a82f
JF
44#include <cstring>
45#include <string>
46#include <vector>
47
fdb119ef 48#include <sys/wait.h>
31ad673b
JF
49#include <sys/types.h>
50#include <sys/stat.h>
fdb119ef 51
a362a82f
JF
52struct fat_header {
53 uint32_t magic;
54 uint32_t nfat_arch;
c05d9758 55} _packed;
a362a82f
JF
56
57#define FAT_MAGIC 0xcafebabe
58#define FAT_CIGAM 0xbebafeca
59
60struct fat_arch {
61 uint32_t cputype;
62 uint32_t cpusubtype;
63 uint32_t offset;
64 uint32_t size;
65 uint32_t align;
c05d9758 66} _packed;
a362a82f
JF
67
68struct mach_header {
69 uint32_t magic;
70 uint32_t cputype;
04802ab1 71 uint32_t cpusubtype;
a362a82f
JF
72 uint32_t filetype;
73 uint32_t ncmds;
74 uint32_t sizeofcmds;
75 uint32_t flags;
c05d9758 76} _packed;
a362a82f 77
fdb119ef 78#define MH_MAGIC 0xfeedface
a362a82f
JF
79#define MH_CIGAM 0xcefaedfe
80
3cbc6463
JF
81#define MH_MAGIC_64 0xfeedfacf
82#define MH_CIGAM_64 0xcffaedfe
83
82813bde
JF
84#define MH_DYLDLINK 0x4
85
efd6cd4a
JF
86#define MH_EXECUTE 0x2
87#define MH_DYLIB 0x6
88#define MH_BUNDLE 0x8
89#define MH_DYLIB_STUB 0x9
a362a82f
JF
90
91struct load_command {
92 uint32_t cmd;
93 uint32_t cmdsize;
c05d9758 94} _packed;
a362a82f 95
645f8f12 96#define LC_REQ_DYLD uint32_t(0x80000000)
a362a82f 97
645f8f12
JF
98#define LC_SEGMENT uint32_t(0x01)
99#define LC_LOAD_DYLIB uint32_t(0x0c)
100#define LC_ID_DYLIB uint32_t(0x0d)
101#define LC_UUID uint32_t(0x1b)
102#define LC_CODE_SIGNATURE uint32_t(0x1d)
103#define LC_REEXPORT_DYLIB uint32_t(0x1f | LC_REQ_DYLD)
a362a82f
JF
104
105struct dylib {
106 uint32_t name;
107 uint32_t timestamp;
108 uint32_t current_version;
109 uint32_t compatibility_version;
c05d9758 110} _packed;
a362a82f
JF
111
112struct dylib_command {
113 uint32_t cmd;
114 uint32_t cmdsize;
115 struct dylib dylib;
c05d9758 116} _packed;
a362a82f
JF
117
118struct uuid_command {
119 uint32_t cmd;
120 uint32_t cmdsize;
121 uint8_t uuid[16];
c05d9758 122} _packed;
a362a82f 123
6e83315b
JF
124struct segment_command {
125 uint32_t cmd;
126 uint32_t cmdsize;
127 char segname[16];
128 uint32_t vmaddr;
129 uint32_t vmsize;
130 uint32_t fileoff;
131 uint32_t filesize;
132 uint32_t maxprot;
133 uint32_t initprot;
134 uint32_t nsects;
135 uint32_t flags;
136};
137
138struct section {
139 char sectname[16];
140 char segname[16];
141 uint32_t addr;
142 uint32_t size;
143 uint32_t offset;
144 uint32_t align;
145 uint32_t reloff;
146 uint32_t nreloc;
147 uint32_t flags;
148 uint32_t reserved1;
149 uint32_t reserved2;
150};
151
fdb119ef
JF
152struct linkedit_data_command {
153 uint32_t cmd;
154 uint32_t cmdsize;
155 uint32_t dataoff;
156 uint32_t datasize;
c05d9758 157} _packed;
fdb119ef
JF
158
159uint16_t Swap_(uint16_t value) {
160 return
161 ((value >> 8) & 0x00ff) |
162 ((value << 8) & 0xff00);
163}
164
165uint32_t Swap_(uint32_t value) {
166 value = ((value >> 8) & 0x00ff00ff) |
167 ((value << 8) & 0xff00ff00);
168 value = ((value >> 16) & 0x0000ffff) |
169 ((value << 16) & 0xffff0000);
170 return value;
171}
172
173int16_t Swap_(int16_t value) {
174 return Swap_(static_cast<uint16_t>(value));
175}
176
177int32_t Swap_(int32_t value) {
178 return Swap_(static_cast<uint32_t>(value));
179}
180
5525a5a7
JF
181bool little_(true);
182
fdb119ef 183uint16_t Swap(uint16_t value) {
5525a5a7 184 return little_ ? Swap_(value) : value;
fdb119ef
JF
185}
186
187uint32_t Swap(uint32_t value) {
5525a5a7 188 return little_ ? Swap_(value) : value;
fdb119ef
JF
189}
190
191int16_t Swap(int16_t value) {
192 return Swap(static_cast<uint16_t>(value));
193}
194
195int32_t Swap(int32_t value) {
196 return Swap(static_cast<uint32_t>(value));
197}
198
6e83315b
JF
199template <typename Target_>
200class Pointer;
201
a362a82f
JF
202class Framework {
203 private:
204 void *base_;
205 size_t size_;
3cbc6463
JF
206
207 struct mach_header *mach_header_;
208 struct load_command *load_command_;
209
a362a82f 210 bool swapped_;
3cbc6463 211 bool bits64_;
a362a82f
JF
212
213 public:
fdb119ef
JF
214 uint16_t Swap(uint16_t value) const {
215 return swapped_ ? Swap_(value) : value;
a362a82f
JF
216 }
217
fdb119ef
JF
218 uint32_t Swap(uint32_t value) const {
219 return swapped_ ? Swap_(value) : value;
a362a82f
JF
220 }
221
fdb119ef
JF
222 int16_t Swap(int16_t value) const {
223 return Swap(static_cast<uint16_t>(value));
a362a82f
JF
224 }
225
fdb119ef
JF
226 int32_t Swap(int32_t value) const {
227 return Swap(static_cast<uint32_t>(value));
a362a82f
JF
228 }
229
230 Framework(const char *framework_path) :
231 swapped_(false)
232 {
233 base_ = map(framework_path, 0, _not(size_t), &size_, false);
234 fat_header *fat_header = reinterpret_cast<struct fat_header *>(base_);
235
236 if (Swap(fat_header->magic) == FAT_CIGAM) {
237 swapped_ = !swapped_;
238 goto fat;
239 } else if (Swap(fat_header->magic) != FAT_MAGIC)
240 mach_header_ = (mach_header *) base_;
241 else fat: {
242 size_t fat_narch = Swap(fat_header->nfat_arch);
243 fat_arch *fat_arch = reinterpret_cast<struct fat_arch *>(fat_header + 1);
244 size_t arch;
245 for (arch = 0; arch != fat_narch; ++arch) {
246 uint32_t arch_offset = Swap(fat_arch->offset);
247 mach_header_ = (mach_header *) ((uint8_t *) base_ + arch_offset);
248 goto found;
249 ++fat_arch;
250 }
251
252 _assert(false);
253 }
254
255 found:
3cbc6463
JF
256 switch (Swap(mach_header_->magic)) {
257 case MH_CIGAM:
258 swapped_ = !swapped_;
259 case MH_MAGIC:
260 bits64_ = false;
261 break;
262
263 case MH_CIGAM_64:
264 swapped_ = !swapped_;
265 case MH_MAGIC_64:
266 bits64_ = true;
267 break;
268
269 default:
270 _assert(false);
271 }
272
273 void *post = mach_header_ + 1;
274 if (bits64_)
275 post = (uint32_t *) post + 1;
276 load_command_ = (struct load_command *) post;
a362a82f
JF
277
278 _assert(
279 Swap(mach_header_->filetype) == MH_EXECUTE ||
280 Swap(mach_header_->filetype) == MH_DYLIB ||
281 Swap(mach_header_->filetype) == MH_BUNDLE
282 );
283 }
284
afbb7c8e
JF
285 struct mach_header *operator ->() const {
286 return mach_header_;
287 }
288
a362a82f
JF
289 void *GetBase() {
290 return base_;
291 }
292
3cbc6463 293 size_t GetSize() const {
a362a82f
JF
294 return size_;
295 }
296
04802ab1
JF
297 uint32_t GetCPUType() const {
298 return Swap(mach_header_->cputype);
299 }
300
301 uint16_t GetCPUSubtype() const {
302 return Swap(mach_header_->cpusubtype) & 0xff;
303 }
304
a362a82f
JF
305 std::vector<struct load_command *> GetLoadCommands() {
306 std::vector<struct load_command *> load_commands;
307
3cbc6463 308 struct load_command *load_command = load_command_;
a362a82f
JF
309 for (uint32_t cmd = 0; cmd != Swap(mach_header_->ncmds); ++cmd) {
310 load_commands.push_back(load_command);
311 load_command = (struct load_command *) ((uint8_t *) load_command + Swap(load_command->cmdsize));
312 }
313
314 return load_commands;
315 }
6e83315b
JF
316
317 std::vector<segment_command *> GetSegments(const char *segment_name) {
318 std::vector<struct segment_command *> segment_commands;
319
320 _foreach (load_command, GetLoadCommands())
321 if (Swap((*load_command)->cmd) == LC_SEGMENT) {
322 segment_command *segment_command = reinterpret_cast<struct segment_command *>(*load_command);
323 if (strncmp(segment_command->segname, segment_name, 16) == 0)
324 segment_commands.push_back(segment_command);
325 }
326
327 return segment_commands;
328 }
329
330 std::vector<section *> GetSections(const char *segment_name, const char *section_name) {
331 std::vector<section *> sections;
332
333 _foreach (segment, GetSegments(segment_name)) {
334 section *section = (struct section *) (*segment + 1);
335
336 uint32_t sect;
337 for (sect = 0; sect != Swap((*segment)->nsects); ++sect) {
338 if (strncmp(section->sectname, section_name, 16) == 0)
339 sections.push_back(section);
340 ++section;
341 }
342 }
343
344 return sections;
345 }
346
347 template <typename Target_>
348 Pointer<Target_> GetPointer(uint32_t address, const char *segment_name = NULL) {
349 load_command *load_command = (struct load_command *) (mach_header_ + 1);
350 uint32_t cmd;
351
352 for (cmd = 0; cmd != Swap(mach_header_->ncmds); ++cmd) {
353 if (Swap(load_command->cmd) == LC_SEGMENT) {
354 segment_command *segment_command = (struct segment_command *) load_command;
355 if (segment_name != NULL && strncmp(segment_command->segname, segment_name, 16) != 0)
356 goto next_command;
357
358 section *sections = (struct section *) (segment_command + 1);
359
360 uint32_t sect;
361 for (sect = 0; sect != Swap(segment_command->nsects); ++sect) {
362 section *section = &sections[sect];
363 //printf("%s %u %p %p %u\n", segment_command->segname, sect, address, section->addr, section->size);
364 if (address >= Swap(section->addr) && address < Swap(section->addr) + Swap(section->size)) {
365 //printf("0x%.8x %s\n", address, segment_command->segname);
366 return Pointer<Target_>(this, reinterpret_cast<Target_ *>(address - Swap(section->addr) + Swap(section->offset) + (char *) mach_header_));
367 }
368 }
369 }
370
371 next_command:
372 load_command = (struct load_command *) ((char *) load_command + Swap(load_command->cmdsize));
373 }
374
375 return Pointer<Target_>(this);
376 }
377
378 template <typename Target_>
379 Pointer<Target_> GetOffset(uint32_t offset) {
380 return Pointer<Target_>(this, reinterpret_cast<Target_ *>(offset + (uint8_t *) mach_header_));
381 }
382};
383
384template <typename Target_>
385class Pointer {
386 private:
387 const Framework *framework_;
388 const Target_ *pointer_;
389
390 public:
391 Pointer(const Framework *framework = NULL, const Target_ *pointer = NULL) :
392 framework_(framework),
393 pointer_(pointer)
394 {
395 }
396
397 operator const Target_ *() const {
398 return pointer_;
399 }
400
401 const Target_ *operator ->() const {
402 return pointer_;
403 }
404
405 Pointer<Target_> &operator ++() {
406 ++pointer_;
407 return *this;
408 }
409
410 template <typename Value_>
411 Value_ Swap(Value_ value) {
412 return framework_->Swap(value);
413 }
a362a82f
JF
414};
415
645f8f12
JF
416#define CSMAGIC_CODEDIRECTORY uint32_t(0xfade0c02)
417#define CSMAGIC_EMBEDDED_SIGNATURE uint32_t(0xfade0cc0)
418#define CSMAGIC_ENTITLEMENTS uint32_t(0xfade7171)
c05d9758 419
645f8f12
JF
420#define CSSLOT_CODEDIRECTORY uint32_t(0)
421#define CSSLOT_REQUIREMENTS uint32_t(2)
422#define CSSLOT_ENTITLEMENTS uint32_t(5)
fdb119ef
JF
423
424struct BlobIndex {
425 uint32_t type;
426 uint32_t offset;
c05d9758 427} _packed;
fdb119ef 428
c05d9758 429struct Blob {
fdb119ef
JF
430 uint32_t magic;
431 uint32_t length;
c05d9758
JF
432} _packed;
433
434struct SuperBlob {
435 struct Blob blob;
fdb119ef
JF
436 uint32_t count;
437 struct BlobIndex index[];
c05d9758 438} _packed;
fdb119ef
JF
439
440struct CodeDirectory {
c05d9758 441 struct Blob blob;
fdb119ef
JF
442 uint32_t version;
443 uint32_t flags;
444 uint32_t hashOffset;
445 uint32_t identOffset;
446 uint32_t nSpecialSlots;
447 uint32_t nCodeSlots;
448 uint32_t codeLimit;
449 uint8_t hashSize;
450 uint8_t hashType;
451 uint8_t spare1;
452 uint8_t pageSize;
453 uint32_t spare2;
c05d9758 454} _packed;
fdb119ef 455
a362a82f
JF
456extern "C" uint32_t hash(uint8_t *k, uint32_t length, uint32_t initval);
457
fdb119ef
JF
458#define CODESIGN_ALLOCATE "arm-apple-darwin9-codesign_allocate"
459
460void sha1(uint8_t *hash, uint8_t *data, size_t size) {
461 SHA1Context context;
462 SHA1Reset(&context);
463 SHA1Input(&context, data, size);
464 SHA1Result(&context, hash);
465}
466
a362a82f 467int main(int argc, const char *argv[]) {
5525a5a7
JF
468 union {
469 uint16_t word;
470 uint8_t byte[2];
471 } endian = {1};
472
473 little_ = endian.byte[0];
474
a362a82f
JF
475 bool flag_R(false);
476 bool flag_t(false);
477 bool flag_p(false);
478 bool flag_u(false);
9c83be90 479 bool flag_e(false);
a362a82f
JF
480
481 bool flag_T(false);
20c5f1e8 482
fdb119ef 483 bool flag_S(false);
20c5f1e8 484 bool flag_s(false);
a362a82f
JF
485
486 bool timeh(false);
487 uint32_t timev(0);
488
c05d9758
JF
489 const void *xmld(NULL);
490 size_t xmls(0);
491
6e83315b
JF
492 uintptr_t noffset(_not(uintptr_t));
493 uintptr_t woffset(_not(uintptr_t));
494
a362a82f
JF
495 std::vector<std::string> files;
496
a960f392
JF
497 if (argc == 1) {
498 fprintf(stderr, "usage: %s -S[entitlements.xml] <binary>\n", argv[0]);
9c83be90 499 fprintf(stderr, " %s -e MobileSafari\n", argv[0]);
a960f392
JF
500 fprintf(stderr, " %s -S cat\n", argv[0]);
501 fprintf(stderr, " %s -Stfp.xml gdb\n", argv[0]);
502 exit(0);
503 }
504
a362a82f
JF
505 for (int argi(1); argi != argc; ++argi)
506 if (argv[argi][0] != '-')
507 files.push_back(argv[argi]);
508 else switch (argv[argi][1]) {
509 case 'R': flag_R = true; break;
510 case 't': flag_t = true; break;
511 case 'u': flag_u = true; break;
512 case 'p': flag_p = true; break;
9c83be90 513 case 'e': flag_e = true; break;
c05d9758 514
20c5f1e8
JF
515 case 's':
516 _assert(!flag_S);
517 flag_s = true;
518 break;
519
c05d9758 520 case 'S':
20c5f1e8 521 _assert(!flag_s);
c05d9758
JF
522 flag_S = true;
523 if (argv[argi][2] != '\0') {
524 const char *xml = argv[argi] + 2;
525 xmld = map(xml, 0, _not(size_t), &xmls, true);
526 }
527 break;
a362a82f
JF
528
529 case 'T': {
530 flag_T = true;
531 if (argv[argi][2] == '-')
532 timeh = true;
533 else {
534 char *arge;
535 timev = strtoul(argv[argi] + 2, &arge, 0);
536 _assert(arge == argv[argi] + strlen(argv[argi]));
537 }
538 } break;
539
6e83315b
JF
540 case 'n': {
541 char *arge;
542 noffset = strtoul(argv[argi] + 2, &arge, 0);
543 _assert(arge == argv[argi] + strlen(argv[argi]));
544 } break;
545
546 case 'w': {
547 char *arge;
548 woffset = strtoul(argv[argi] + 2, &arge, 0);
549 _assert(arge == argv[argi] + strlen(argv[argi]));
550 } break;
551
a362a82f
JF
552 default:
553 goto usage;
554 break;
555 }
556
557 if (files.empty()) usage: {
558 exit(0);
559 }
560
561 size_t filei(0), filee(0);
562 _foreach (file, files) try {
fdb119ef
JF
563 const char *path(file->c_str());
564 const char *base = strrchr(path, '/');
565 char *temp(NULL), *dir;
566
567 if (base != NULL)
b8187ac7 568 dir = strndup_(path, base++ - path + 1);
fdb119ef
JF
569 else {
570 dir = strdup("");
571 base = path;
572 }
573
574 if (flag_S) {
575 asprintf(&temp, "%s.%s.cs", dir, base);
576 const char *allocate = getenv("CODESIGN_ALLOCATE");
577 if (allocate == NULL)
578 allocate = "codesign_allocate";
579
afbb7c8e
JF
580 size_t size = _not(size_t);
581 const char *arch; {
fdb119ef 582 Framework framework(path);
82813bde
JF
583 framework->flags |= MH_DYLDLINK;
584
afbb7c8e
JF
585 _foreach (load_command, framework.GetLoadCommands()) {
586 uint32_t cmd(framework.Swap((*load_command)->cmd));
587 if (cmd == LC_CODE_SIGNATURE) {
588 struct linkedit_data_command *signature = reinterpret_cast<struct linkedit_data_command *>(*load_command);
589 size = framework.Swap(signature->dataoff);
590 _assert(size < framework.GetSize());
591 break;
592 }
593 }
594
595 if (size == _not(size_t))
596 size = framework.GetSize();
597
04802ab1
JF
598 switch (framework.GetCPUType()) {
599 case 7: switch (framework.GetCPUSubtype()) {
3cbc6463
JF
600 case 3: arch = "i386"; break;
601 default: arch = NULL; break;
602 } break;
603
04802ab1 604 case 12: switch (framework.GetCPUSubtype()) {
afbb7c8e
JF
605 case 0: arch = "arm"; break;
606 case 6: arch = "armv6"; break;
3cbc6463
JF
607 case 9: arch = "armv7"; break;
608 default: arch = NULL; break;
609 } break;
610
04802ab1
JF
611 case 18: switch (framework.GetCPUSubtype()) {
612 case 10: arch = "ppc7400"; break;
613 default: arch = NULL; break;
614 } break;
615
616 case 16777223: switch (framework.GetCPUSubtype()) {
3cbc6463 617 case 3: arch = "x86_64"; break;
afbb7c8e
JF
618 default: arch = NULL; break;
619 } break;
620
04802ab1
JF
621 case 16777234: switch (framework.GetCPUSubtype()) {
622 case 0: arch = "ppc64"; break;
623 default: arch = NULL; break;
624 } break;
625
afbb7c8e
JF
626 default: arch = NULL; break;
627 }
fdb119ef
JF
628 }
629
afbb7c8e
JF
630 _assert(arch != NULL);
631
fdb119ef
JF
632 pid_t pid = fork();
633 _syscall(pid);
634 if (pid == 0) {
635 char *ssize;
20c5f1e8
JF
636 asprintf(&ssize, "%u", (sizeof(struct SuperBlob) + 2 * sizeof(struct BlobIndex) + sizeof(struct CodeDirectory) + strlen(base) + 1 + ((xmld == NULL ? CSSLOT_REQUIREMENTS : CSSLOT_ENTITLEMENTS) + (size + 0x1000 - 1) / 0x1000) * 0x14 + 0xc + (xmld == NULL ? 0 : 0x10 + xmls) + 15) / 16 * 16);
637 //printf("%s -i %s -a %s %s -o %s\n", allocate, path, arch, ssize, temp);
afbb7c8e 638 execlp(allocate, allocate, "-i", path, "-a", arch, ssize, "-o", temp, NULL);
fdb119ef
JF
639 _assert(false);
640 }
641
642 int status;
643 _syscall(waitpid(pid, &status, 0));
644 _assert(WIFEXITED(status));
645 _assert(WEXITSTATUS(status) == 0);
646 }
647
648 Framework framework(temp == NULL ? path : temp);
649 struct linkedit_data_command *signature(NULL);
a362a82f
JF
650
651 if (flag_p)
652 printf("path%zu='%s'\n", filei, file->c_str());
653
6e83315b
JF
654 if (woffset != _not(uintptr_t)) {
655 Pointer<uint32_t> wvalue(framework.GetPointer<uint32_t>(woffset));
656 if (wvalue == NULL)
5567b35f 657 printf("(null) %p\n", reinterpret_cast<void *>(woffset));
6e83315b
JF
658 else
659 printf("0x%.08x\n", *wvalue);
660 }
661
662 if (noffset != _not(uintptr_t))
663 printf("%s\n", &*framework.GetPointer<char>(noffset));
664
a362a82f
JF
665 _foreach (load_command, framework.GetLoadCommands()) {
666 uint32_t cmd(framework.Swap((*load_command)->cmd));
667
668 if (flag_R && cmd == LC_REEXPORT_DYLIB)
669 (*load_command)->cmd = framework.Swap(LC_LOAD_DYLIB);
fdb119ef
JF
670 else if (cmd == LC_CODE_SIGNATURE)
671 signature = reinterpret_cast<struct linkedit_data_command *>(*load_command);
a362a82f
JF
672 else if (cmd == LC_UUID) {
673 volatile struct uuid_command *uuid_command(reinterpret_cast<struct uuid_command *>(*load_command));
674
675 if (flag_u) {
676 printf("uuid%zu=%.2x%.2x%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x%.2x%.2x%.2x%.2x\n", filei,
677 uuid_command->uuid[ 0], uuid_command->uuid[ 1], uuid_command->uuid[ 2], uuid_command->uuid[ 3],
678 uuid_command->uuid[ 4], uuid_command->uuid[ 5], uuid_command->uuid[ 6], uuid_command->uuid[ 7],
679 uuid_command->uuid[ 8], uuid_command->uuid[ 9], uuid_command->uuid[10], uuid_command->uuid[11],
680 uuid_command->uuid[12], uuid_command->uuid[13], uuid_command->uuid[14], uuid_command->uuid[15]
681 );
682 }
683 } else if (cmd == LC_ID_DYLIB) {
684 volatile struct dylib_command *dylib_command(reinterpret_cast<struct dylib_command *>(*load_command));
685
686 if (flag_t)
687 printf("time%zu=0x%.8x\n", filei, framework.Swap(dylib_command->dylib.timestamp));
688
689 if (flag_T) {
690 uint32_t timed;
691
692 if (!timeh)
693 timed = timev;
694 else {
695 dylib_command->dylib.timestamp = 0;
696 timed = hash(reinterpret_cast<uint8_t *>(framework.GetBase()), framework.GetSize(), timev);
697 }
698
699 dylib_command->dylib.timestamp = framework.Swap(timed);
700 }
701 }
702 }
703
9c83be90
JF
704 if (flag_e) {
705 _assert(signature != NULL);
706
707 uint32_t data = framework.Swap(signature->dataoff);
708 uint32_t size = framework.Swap(signature->datasize);
709
710 uint8_t *top = reinterpret_cast<uint8_t *>(framework.GetBase());
711 uint8_t *blob = top + data;
712 struct SuperBlob *super = reinterpret_cast<struct SuperBlob *>(blob);
713
714 for (size_t index(0); index != Swap(super->count); ++index)
715 if (Swap(super->index[index].type) == CSSLOT_ENTITLEMENTS) {
716 uint32_t begin = Swap(super->index[index].offset);
717 struct Blob *entitlements = reinterpret_cast<struct Blob *>(blob + begin);
718 fwrite(entitlements + 1, 1, Swap(entitlements->length) - sizeof(struct Blob), stdout);
719 }
720 }
721
20c5f1e8
JF
722 if (flag_s) {
723 _assert(signature != NULL);
724
725 uint32_t data = framework.Swap(signature->dataoff);
726 uint32_t size = framework.Swap(signature->datasize);
727
728 uint8_t *top = reinterpret_cast<uint8_t *>(framework.GetBase());
729 uint8_t *blob = top + data;
730 struct SuperBlob *super = reinterpret_cast<struct SuperBlob *>(blob);
731
732 for (size_t index(0); index != Swap(super->count); ++index)
733 if (Swap(super->index[index].type) == CSSLOT_CODEDIRECTORY) {
734 uint32_t begin = Swap(super->index[index].offset);
735 struct CodeDirectory *directory = reinterpret_cast<struct CodeDirectory *>(blob + begin);
736
737 uint8_t (*hashes)[20] = reinterpret_cast<uint8_t (*)[20]>(blob + begin + Swap(directory->hashOffset));
738 uint32_t pages = Swap(directory->nCodeSlots);
739
740 if (pages != 1)
741 for (size_t i = 0; i != pages - 1; ++i)
742 sha1(hashes[i], top + 0x1000 * i, 0x1000);
743 if (pages != 0)
ea6a0421 744 sha1(hashes[pages - 1], top + 0x1000 * (pages - 1), ((data - 1) % 0x1000) + 1);
20c5f1e8
JF
745 }
746 }
747
fdb119ef
JF
748 if (flag_S) {
749 _assert(signature != NULL);
750
751 uint32_t data = framework.Swap(signature->dataoff);
752 uint32_t size = framework.Swap(signature->datasize);
753
754 uint8_t *top = reinterpret_cast<uint8_t *>(framework.GetBase());
755 uint8_t *blob = top + data;
756 struct SuperBlob *super = reinterpret_cast<struct SuperBlob *>(blob);
c05d9758 757 super->blob.magic = Swap(CSMAGIC_EMBEDDED_SIGNATURE);
fdb119ef 758
c05d9758 759 uint32_t count = xmld == NULL ? 2 : 3;
fdb119ef
JF
760 uint32_t offset = sizeof(struct SuperBlob) + count * sizeof(struct BlobIndex);
761
762 super->index[0].type = Swap(CSSLOT_CODEDIRECTORY);
763 super->index[0].offset = Swap(offset);
764
765 uint32_t begin = offset;
766 struct CodeDirectory *directory = reinterpret_cast<struct CodeDirectory *>(blob + begin);
767 offset += sizeof(struct CodeDirectory);
768
c05d9758 769 directory->blob.magic = Swap(CSMAGIC_CODEDIRECTORY);
645f8f12
JF
770 directory->version = Swap(uint32_t(0x00020001));
771 directory->flags = Swap(uint32_t(0));
fdb119ef
JF
772 directory->codeLimit = Swap(data);
773 directory->hashSize = 0x14;
774 directory->hashType = 0x01;
775 directory->spare1 = 0x00;
776 directory->pageSize = 0x0c;
645f8f12 777 directory->spare2 = Swap(uint32_t(0));
fdb119ef
JF
778
779 directory->identOffset = Swap(offset - begin);
780 strcpy(reinterpret_cast<char *>(blob + offset), base);
781 offset += strlen(base) + 1;
782
20c5f1e8
JF
783 uint32_t special = xmld == NULL ? CSSLOT_REQUIREMENTS : CSSLOT_ENTITLEMENTS;
784 directory->nSpecialSlots = Swap(special);
785
fdb119ef 786 uint8_t (*hashes)[20] = reinterpret_cast<uint8_t (*)[20]>(blob + offset);
20c5f1e8
JF
787 memset(hashes, 0, sizeof(*hashes) * special);
788
789 offset += sizeof(*hashes) * special;
790 hashes += special;
fdb119ef
JF
791
792 uint32_t pages = (data + 0x1000 - 1) / 0x1000;
fdb119ef
JF
793 directory->nCodeSlots = Swap(pages);
794
795 if (pages != 1)
796 for (size_t i = 0; i != pages - 1; ++i)
20c5f1e8 797 sha1(hashes[i], top + 0x1000 * i, 0x1000);
fdb119ef 798 if (pages != 0)
ea6a0421 799 sha1(hashes[pages - 1], top + 0x1000 * (pages - 1), ((data - 1) % 0x1000) + 1);
fdb119ef
JF
800
801 directory->hashOffset = Swap(offset - begin);
20c5f1e8 802 offset += sizeof(*hashes) * pages;
c05d9758 803 directory->blob.length = Swap(offset - begin);
fdb119ef 804
afbb7c8e
JF
805 super->index[1].type = Swap(CSSLOT_REQUIREMENTS);
806 super->index[1].offset = Swap(offset);
807
808 memcpy(blob + offset, "\xfa\xde\x0c\x01\x00\x00\x00\x0c\x00\x00\x00\x00", 0xc);
809 offset += 0xc;
810
c05d9758
JF
811 if (xmld != NULL) {
812 super->index[2].type = Swap(CSSLOT_ENTITLEMENTS);
813 super->index[2].offset = Swap(offset);
814
815 uint32_t begin = offset;
816 struct Blob *entitlements = reinterpret_cast<struct Blob *>(blob + begin);
817 offset += sizeof(struct Blob);
818
819 memcpy(blob + offset, xmld, xmls);
820 offset += xmls;
821
822 entitlements->magic = Swap(CSMAGIC_ENTITLEMENTS);
823 entitlements->length = Swap(offset - begin);
824 }
825
20c5f1e8
JF
826 for (size_t index(0); index != count; ++index) {
827 uint32_t type = Swap(super->index[index].type);
828 if (type != 0 && type <= special) {
829 uint32_t offset = Swap(super->index[index].offset);
830 struct Blob *local = (struct Blob *) (blob + offset);
831 sha1((uint8_t *) (hashes - type), (uint8_t *) local, Swap(local->length));
832 }
833 }
834
fdb119ef 835 super->count = Swap(count);
c05d9758
JF
836 super->blob.length = Swap(offset);
837
838 if (offset > size) {
9c83be90 839 fprintf(stderr, "offset (%u) > size (%u)\n", offset, size);
c05d9758
JF
840 _assert(false);
841 } //else fprintf(stderr, "offset (%zu) <= size (%zu)\n", offset, size);
fdb119ef 842
fdb119ef
JF
843 memset(blob + offset, 0, size - offset);
844 }
845
122621c5
JF
846 if (flag_S) {
847 uint8_t *top = reinterpret_cast<uint8_t *>(framework.GetBase());
848 size_t size = framework.GetSize();
849
850 char *copy;
851 asprintf(&copy, "%s.%s.cp", dir, base);
852 FILE *file = fopen(copy, "w+");
853 size_t writ = fwrite(top, 1, size, file);
854 _assert(writ == size);
855 fclose(file);
856
857 _syscall(unlink(temp));
858 free(temp);
859 temp = copy;
860 }
861
fdb119ef 862 if (temp) {
31ad673b
JF
863 struct stat info;
864 _syscall(stat(path, &info));
865 _syscall(chown(temp, info.st_uid, info.st_gid));
866 _syscall(chmod(temp, info.st_mode));
fdb119ef
JF
867 _syscall(unlink(path));
868 _syscall(rename(temp, path));
869 free(temp);
870 }
871
872 free(dir);
a362a82f
JF
873 ++filei;
874 } catch (const char *) {
875 ++filee;
876 ++filei;
877 }
878
879 return filee;
880}