1 /* JocStrap - Java/Objective-C Bootstrap
2 * Copyright (C) 2007 Jay Freeman (saurik)
6 * Redistribution and use in source and binary
7 * forms, with or without modification, are permitted
8 * provided that the following conditions are met:
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
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.
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.
38 #include "minimal/stdlib.h"
39 #include "minimal/string.h"
40 #include "minimal/mapping.h"
49 #include <sys/types.h>
57 #define FAT_MAGIC 0xcafebabe
58 #define FAT_CIGAM 0xbebafeca
78 #define MH_MAGIC 0xfeedface
79 #define MH_CIGAM 0xcefaedfe
81 #define MH_EXECUTE 0x2
84 #define MH_DYLIB_STUB 0x9
91 #define LC_REQ_DYLD 0x80000000
93 #define LC_LOAD_DYLIB 0x0c
94 #define LC_ID_DYLIB 0x0d
96 #define LC_CODE_SIGNATURE 0x1d
97 #define LC_REEXPORT_DYLIB (0x1f | LC_REQ_DYLD)
102 uint32_t current_version
;
103 uint32_t compatibility_version
;
106 struct dylib_command
{
112 struct uuid_command
{
118 struct linkedit_data_command
{
125 uint16_t Swap_(uint16_t value
) {
127 ((value
>> 8) & 0x00ff) |
128 ((value
<< 8) & 0xff00);
131 uint32_t Swap_(uint32_t value
) {
132 value
= ((value
>> 8) & 0x00ff00ff) |
133 ((value
<< 8) & 0xff00ff00);
134 value
= ((value
>> 16) & 0x0000ffff) |
135 ((value
<< 16) & 0xffff0000);
139 int16_t Swap_(int16_t value
) {
140 return Swap_(static_cast<uint16_t>(value
));
143 int32_t Swap_(int32_t value
) {
144 return Swap_(static_cast<uint32_t>(value
));
147 uint16_t Swap(uint16_t value
) {
148 return true ? Swap_(value
) : value
;
151 uint32_t Swap(uint32_t value
) {
152 return true ? Swap_(value
) : value
;
155 int16_t Swap(int16_t value
) {
156 return Swap(static_cast<uint16_t>(value
));
159 int32_t Swap(int32_t value
) {
160 return Swap(static_cast<uint32_t>(value
));
167 mach_header
*mach_header_
;
171 uint16_t Swap(uint16_t value
) const {
172 return swapped_
? Swap_(value
) : value
;
175 uint32_t Swap(uint32_t value
) const {
176 return swapped_
? Swap_(value
) : value
;
179 int16_t Swap(int16_t value
) const {
180 return Swap(static_cast<uint16_t>(value
));
183 int32_t Swap(int32_t value
) const {
184 return Swap(static_cast<uint32_t>(value
));
187 Framework(const char *framework_path
) :
190 base_
= map(framework_path
, 0, _not(size_t), &size_
, false);
191 fat_header
*fat_header
= reinterpret_cast<struct fat_header
*>(base_
);
193 if (Swap(fat_header
->magic
) == FAT_CIGAM
) {
194 swapped_
= !swapped_
;
196 } else if (Swap(fat_header
->magic
) != FAT_MAGIC
)
197 mach_header_
= (mach_header
*) base_
;
199 size_t fat_narch
= Swap(fat_header
->nfat_arch
);
200 fat_arch
*fat_arch
= reinterpret_cast<struct fat_arch
*>(fat_header
+ 1);
202 for (arch
= 0; arch
!= fat_narch
; ++arch
) {
203 uint32_t arch_offset
= Swap(fat_arch
->offset
);
204 mach_header_
= (mach_header
*) ((uint8_t *) base_
+ arch_offset
);
213 if (Swap(mach_header_
->magic
) == MH_CIGAM
)
214 swapped_
= !swapped_
;
215 else _assert(Swap(mach_header_
->magic
) == MH_MAGIC
);
218 Swap(mach_header_
->filetype
) == MH_EXECUTE
||
219 Swap(mach_header_
->filetype
) == MH_DYLIB
||
220 Swap(mach_header_
->filetype
) == MH_BUNDLE
224 struct mach_header
*operator ->() const {
236 std::vector
<struct load_command
*> GetLoadCommands() {
237 std::vector
<struct load_command
*> load_commands
;
239 struct load_command
*load_command
= reinterpret_cast<struct load_command
*>(mach_header_
+ 1);
240 for (uint32_t cmd
= 0; cmd
!= Swap(mach_header_
->ncmds
); ++cmd
) {
241 load_commands
.push_back(load_command
);
242 load_command
= (struct load_command
*) ((uint8_t *) load_command
+ Swap(load_command
->cmdsize
));
245 return load_commands
;
249 #define CSMAGIC_CODEDIRECTORY 0xfade0c02
250 #define CSMAGIC_EMBEDDED_SIGNATURE 0xfade0cc0
251 #define CSMAGIC_ENTITLEMENTS 0xfade7171
253 #define CSSLOT_CODEDIRECTORY 0
254 #define CSSLOT_REQUIREMENTS 2
255 #define CSSLOT_ENTITLEMENTS 5
270 struct BlobIndex index
[];
273 struct CodeDirectory
{
278 uint32_t identOffset
;
279 uint32_t nSpecialSlots
;
289 extern "C" uint32_t hash(uint8_t *k
, uint32_t length
, uint32_t initval
);
291 #define CODESIGN_ALLOCATE "arm-apple-darwin9-codesign_allocate"
293 void sha1(uint8_t *hash
, uint8_t *data
, size_t size
) {
296 SHA1Input(&context
, data
, size
);
297 SHA1Result(&context
, hash
);
300 int main(int argc
, const char *argv
[]) {
312 const void *xmld(NULL
);
315 std::vector
<std::string
> files
;
318 for (int argi(1); argi
!= argc
; ++argi
)
319 if (argv
[argi
][0] != '-')
320 files
.push_back(argv
[argi
]);
321 else switch (argv
[argi
][1]) {
322 case 'R': flag_R
= true; break;
323 case 't': flag_t
= true; break;
324 case 'u': flag_u
= true; break;
325 case 'p': flag_p
= true; break;
329 if (argv
[argi
][2] != '\0') {
330 const char *xml
= argv
[argi
] + 2;
331 xmld
= map(xml
, 0, _not(size_t), &xmls
, true);
337 if (argv
[argi
][2] == '-')
341 timev
= strtoul(argv
[argi
] + 2, &arge
, 0);
342 _assert(arge
== argv
[argi
] + strlen(argv
[argi
]));
351 if (files
.empty()) usage
: {
355 size_t filei(0), filee(0);
356 _foreach (file
, files
) try {
357 const char *path(file
->c_str());
358 const char *base
= strrchr(path
, '/');
359 char *temp(NULL
), *dir
;
363 dir
= strndup_(path
, base
++ - path
+ 1);
370 asprintf(&temp
, "%s.%s.cs", dir
, base
);
371 const char *allocate
= getenv("CODESIGN_ALLOCATE");
372 if (allocate
== NULL
)
373 allocate
= "codesign_allocate";
375 size_t size
= _not(size_t);
377 Framework
framework(path
);
378 _foreach (load_command
, framework
.GetLoadCommands()) {
379 uint32_t cmd(framework
.Swap((*load_command
)->cmd
));
380 if (cmd
== LC_CODE_SIGNATURE
) {
381 struct linkedit_data_command
*signature
= reinterpret_cast<struct linkedit_data_command
*>(*load_command
);
382 size
= framework
.Swap(signature
->dataoff
);
383 _assert(size
< framework
.GetSize());
388 if (size
== _not(size_t))
389 size
= framework
.GetSize();
391 switch (framework
->cputype
) {
392 case 12: switch (framework
->cpusubtype
) {
393 case 0: arch
= "arm"; break;
394 case 6: arch
= "armv6"; break;
395 default: arch
= NULL
; break;
398 default: arch
= NULL
; break;
402 _assert(arch
!= NULL
);
408 asprintf(&ssize
, "%u", (sizeof(struct SuperBlob
) + 2 * sizeof(struct BlobIndex
) + sizeof(struct CodeDirectory
) + strlen(base
) + 1 + (size
+ 0x1000 - 1) / 0x1000 * 0x14 + 0xc + (xmld
== NULL
? 0 : 0x10 + xmls
) + 15) / 16 * 16);
409 //printf("codesign_allocate -i %s -a %s %s -o %s\n", path, arch, ssize, temp);
410 execlp(allocate
, allocate
, "-i", path
, "-a", arch
, ssize
, "-o", temp
, NULL
);
415 _syscall(waitpid(pid
, &status
, 0));
416 _assert(WIFEXITED(status
));
417 _assert(WEXITSTATUS(status
) == 0);
420 Framework
framework(temp
== NULL
? path
: temp
);
421 struct linkedit_data_command
*signature(NULL
);
424 printf("path%zu='%s'\n", filei
, file
->c_str());
426 _foreach (load_command
, framework
.GetLoadCommands()) {
427 uint32_t cmd(framework
.Swap((*load_command
)->cmd
));
429 if (flag_R
&& cmd
== LC_REEXPORT_DYLIB
)
430 (*load_command
)->cmd
= framework
.Swap(LC_LOAD_DYLIB
);
431 else if (cmd
== LC_CODE_SIGNATURE
)
432 signature
= reinterpret_cast<struct linkedit_data_command
*>(*load_command
);
433 else if (cmd
== LC_UUID
) {
434 volatile struct uuid_command
*uuid_command(reinterpret_cast<struct uuid_command
*>(*load_command
));
437 printf("uuid%zu=%.2x%.2x%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x%.2x%.2x%.2x%.2x\n", filei
,
438 uuid_command
->uuid
[ 0], uuid_command
->uuid
[ 1], uuid_command
->uuid
[ 2], uuid_command
->uuid
[ 3],
439 uuid_command
->uuid
[ 4], uuid_command
->uuid
[ 5], uuid_command
->uuid
[ 6], uuid_command
->uuid
[ 7],
440 uuid_command
->uuid
[ 8], uuid_command
->uuid
[ 9], uuid_command
->uuid
[10], uuid_command
->uuid
[11],
441 uuid_command
->uuid
[12], uuid_command
->uuid
[13], uuid_command
->uuid
[14], uuid_command
->uuid
[15]
444 } else if (cmd
== LC_ID_DYLIB
) {
445 volatile struct dylib_command
*dylib_command(reinterpret_cast<struct dylib_command
*>(*load_command
));
448 printf("time%zu=0x%.8x\n", filei
, framework
.Swap(dylib_command
->dylib
.timestamp
));
456 dylib_command
->dylib
.timestamp
= 0;
457 timed
= hash(reinterpret_cast<uint8_t *>(framework
.GetBase()), framework
.GetSize(), timev
);
460 dylib_command
->dylib
.timestamp
= framework
.Swap(timed
);
466 _assert(signature
!= NULL
);
468 uint32_t data
= framework
.Swap(signature
->dataoff
);
469 uint32_t size
= framework
.Swap(signature
->datasize
);
471 uint8_t *top
= reinterpret_cast<uint8_t *>(framework
.GetBase());
472 uint8_t *blob
= top
+ data
;
473 struct SuperBlob
*super
= reinterpret_cast<struct SuperBlob
*>(blob
);
474 super
->blob
.magic
= Swap(CSMAGIC_EMBEDDED_SIGNATURE
);
476 uint32_t count
= xmld
== NULL
? 2 : 3;
477 uint32_t offset
= sizeof(struct SuperBlob
) + count
* sizeof(struct BlobIndex
);
479 super
->index
[0].type
= Swap(CSSLOT_CODEDIRECTORY
);
480 super
->index
[0].offset
= Swap(offset
);
482 uint32_t begin
= offset
;
483 struct CodeDirectory
*directory
= reinterpret_cast<struct CodeDirectory
*>(blob
+ begin
);
484 offset
+= sizeof(struct CodeDirectory
);
486 directory
->blob
.magic
= Swap(CSMAGIC_CODEDIRECTORY
);
487 directory
->version
= Swap(0x00020001);
488 directory
->flags
= Swap(0);
489 directory
->codeLimit
= Swap(data
);
490 directory
->hashSize
= 0x14;
491 directory
->hashType
= 0x01;
492 directory
->spare1
= 0x00;
493 directory
->pageSize
= 0x0c;
494 directory
->spare2
= Swap(0);
496 directory
->identOffset
= Swap(offset
- begin
);
497 strcpy(reinterpret_cast<char *>(blob
+ offset
), base
);
498 offset
+= strlen(base
) + 1;
500 uint8_t (*hashes
)[20] = reinterpret_cast<uint8_t (*)[20]>(blob
+ offset
);
501 uint32_t special
= 0;
503 uint32_t pages
= (data
+ 0x1000 - 1) / 0x1000;
504 directory
->nSpecialSlots
= Swap(special
);
505 directory
->nCodeSlots
= Swap(pages
);
508 for (size_t i
= 0; i
!= pages
- 1; ++i
)
509 sha1(hashes
[special
+ i
], top
+ 0x1000 * i
, 0x1000);
511 sha1(hashes
[special
+ pages
- 1], top
+ 0x1000 * (pages
- 1), data
% 0x1000);
513 directory
->hashOffset
= Swap(offset
- begin
);
514 offset
+= sizeof(*hashes
) * (special
+ pages
);
515 directory
->blob
.length
= Swap(offset
- begin
);
517 super
->index
[1].type
= Swap(CSSLOT_REQUIREMENTS
);
518 super
->index
[1].offset
= Swap(offset
);
520 memcpy(blob
+ offset
, "\xfa\xde\x0c\x01\x00\x00\x00\x0c\x00\x00\x00\x00", 0xc);
524 super
->index
[2].type
= Swap(CSSLOT_ENTITLEMENTS
);
525 super
->index
[2].offset
= Swap(offset
);
527 uint32_t begin
= offset
;
528 struct Blob
*entitlements
= reinterpret_cast<struct Blob
*>(blob
+ begin
);
529 offset
+= sizeof(struct Blob
);
531 memcpy(blob
+ offset
, xmld
, xmls
);
534 entitlements
->magic
= Swap(CSMAGIC_ENTITLEMENTS
);
535 entitlements
->length
= Swap(offset
- begin
);
538 super
->count
= Swap(count
);
539 super
->blob
.length
= Swap(offset
);
542 fprintf(stderr
, "offset (%zu) > size (%zu)\n", offset
, size
);
544 } //else fprintf(stderr, "offset (%zu) <= size (%zu)\n", offset, size);
546 memset(blob
+ offset
, 0, size
- offset
);
551 _syscall(stat(path
, &info
));
552 _syscall(chown(temp
, info
.st_uid
, info
.st_gid
));
553 _syscall(chmod(temp
, info
.st_mode
));
554 _syscall(unlink(path
));
555 _syscall(rename(temp
, path
));
561 } catch (const char *) {