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/mapping.h"
54 #define FAT_MAGIC 0xcafebabe
55 #define FAT_CIGAM 0xbebafeca
75 #define MH_MAGIC 0xfeedface
76 #define MH_CIGAM 0xcefaedfe
78 #define MH_EXECUTE 0x2
87 #define LC_REQ_DYLD 0x80000000
89 #define LC_LOAD_DYLIB 0x0c
90 #define LC_ID_DYLIB 0x0d
92 #define LC_CODE_SIGNATURE 0x1d
93 #define LC_REEXPORT_DYLIB (0x1f | LC_REQ_DYLD)
98 uint32_t current_version
;
99 uint32_t compatibility_version
;
102 struct dylib_command
{
108 struct uuid_command
{
114 struct linkedit_data_command
{
121 uint16_t Swap_(uint16_t value
) {
123 ((value
>> 8) & 0x00ff) |
124 ((value
<< 8) & 0xff00);
127 uint32_t Swap_(uint32_t value
) {
128 value
= ((value
>> 8) & 0x00ff00ff) |
129 ((value
<< 8) & 0xff00ff00);
130 value
= ((value
>> 16) & 0x0000ffff) |
131 ((value
<< 16) & 0xffff0000);
135 int16_t Swap_(int16_t value
) {
136 return Swap_(static_cast<uint16_t>(value
));
139 int32_t Swap_(int32_t value
) {
140 return Swap_(static_cast<uint32_t>(value
));
143 uint16_t Swap(uint16_t value
) {
144 return true ? Swap_(value
) : value
;
147 uint32_t Swap(uint32_t value
) {
148 return true ? Swap_(value
) : value
;
151 int16_t Swap(int16_t value
) {
152 return Swap(static_cast<uint16_t>(value
));
155 int32_t Swap(int32_t value
) {
156 return Swap(static_cast<uint32_t>(value
));
163 mach_header
*mach_header_
;
167 uint16_t Swap(uint16_t value
) const {
168 return swapped_
? Swap_(value
) : value
;
171 uint32_t Swap(uint32_t value
) const {
172 return swapped_
? Swap_(value
) : value
;
175 int16_t Swap(int16_t value
) const {
176 return Swap(static_cast<uint16_t>(value
));
179 int32_t Swap(int32_t value
) const {
180 return Swap(static_cast<uint32_t>(value
));
183 Framework(const char *framework_path
) :
186 base_
= map(framework_path
, 0, _not(size_t), &size_
, false);
187 fat_header
*fat_header
= reinterpret_cast<struct fat_header
*>(base_
);
189 if (Swap(fat_header
->magic
) == FAT_CIGAM
) {
190 swapped_
= !swapped_
;
192 } else if (Swap(fat_header
->magic
) != FAT_MAGIC
)
193 mach_header_
= (mach_header
*) base_
;
195 size_t fat_narch
= Swap(fat_header
->nfat_arch
);
196 fat_arch
*fat_arch
= reinterpret_cast<struct fat_arch
*>(fat_header
+ 1);
198 for (arch
= 0; arch
!= fat_narch
; ++arch
) {
199 uint32_t arch_offset
= Swap(fat_arch
->offset
);
200 mach_header_
= (mach_header
*) ((uint8_t *) base_
+ arch_offset
);
209 if (Swap(mach_header_
->magic
) == MH_CIGAM
)
210 swapped_
= !swapped_
;
211 else _assert(Swap(mach_header_
->magic
) == MH_MAGIC
);
214 Swap(mach_header_
->filetype
) == MH_EXECUTE
||
215 Swap(mach_header_
->filetype
) == MH_DYLIB
||
216 Swap(mach_header_
->filetype
) == MH_BUNDLE
228 std::vector
<struct load_command
*> GetLoadCommands() {
229 std::vector
<struct load_command
*> load_commands
;
231 struct load_command
*load_command
= reinterpret_cast<struct load_command
*>(mach_header_
+ 1);
232 for (uint32_t cmd
= 0; cmd
!= Swap(mach_header_
->ncmds
); ++cmd
) {
233 load_commands
.push_back(load_command
);
234 load_command
= (struct load_command
*) ((uint8_t *) load_command
+ Swap(load_command
->cmdsize
));
237 return load_commands
;
241 #define CSMAGIC_CODEDIRECTORY 0xfade0c02
242 #define CSMAGIC_EMBEDDED_SIGNATURE 0xfade0cc0
243 #define CSSLOT_CODEDIRECTORY 0
254 struct BlobIndex index
[];
257 struct CodeDirectory
{
263 uint32_t identOffset
;
264 uint32_t nSpecialSlots
;
274 extern "C" uint32_t hash(uint8_t *k
, uint32_t length
, uint32_t initval
);
276 #define CODESIGN_ALLOCATE "arm-apple-darwin9-codesign_allocate"
278 void sha1(uint8_t *hash
, uint8_t *data
, size_t size
) {
281 SHA1Input(&context
, data
, size
);
282 SHA1Result(&context
, hash
);
285 int main(int argc
, const char *argv
[]) {
297 std::vector
<std::string
> files
;
300 for (int argi(1); argi
!= argc
; ++argi
)
301 if (argv
[argi
][0] != '-')
302 files
.push_back(argv
[argi
]);
303 else switch (argv
[argi
][1]) {
304 case 'R': flag_R
= true; break;
305 case 't': flag_t
= true; break;
306 case 'u': flag_u
= true; break;
307 case 'p': flag_p
= true; break;
308 case 'S': flag_S
= true; break;
312 if (argv
[argi
][2] == '-')
316 timev
= strtoul(argv
[argi
] + 2, &arge
, 0);
317 _assert(arge
== argv
[argi
] + strlen(argv
[argi
]));
326 if (files
.empty()) usage
: {
330 size_t filei(0), filee(0);
331 _foreach (file
, files
) try {
332 const char *path(file
->c_str());
333 const char *base
= strrchr(path
, '/');
334 char *temp(NULL
), *dir
;
337 dir
= strndup(path
, base
++ - path
+ 1);
344 asprintf(&temp
, "%s.%s.cs", dir
, base
);
345 const char *allocate
= getenv("CODESIGN_ALLOCATE");
346 if (allocate
== NULL
)
347 allocate
= "codesign_allocate";
350 Framework
framework(path
);
351 size
= framework
.GetSize();
358 asprintf(&ssize
, "%u", (sizeof(struct SuperBlob
) + sizeof(struct BlobIndex
) + sizeof(struct CodeDirectory
) + strlen(base
) + 1 + (size
+ 0x1000 - 1) / 0x1000 * 0x14 + 15) / 16 * 16);
359 printf("%s\n", ssize
);
360 execlp(allocate
, allocate
, "-i", path
, "-a", "arm", ssize
, "-o", temp
, NULL
);
365 _syscall(waitpid(pid
, &status
, 0));
366 _assert(WIFEXITED(status
));
367 _assert(WEXITSTATUS(status
) == 0);
370 Framework
framework(temp
== NULL
? path
: temp
);
371 struct linkedit_data_command
*signature(NULL
);
374 printf("path%zu='%s'\n", filei
, file
->c_str());
376 _foreach (load_command
, framework
.GetLoadCommands()) {
377 uint32_t cmd(framework
.Swap((*load_command
)->cmd
));
379 if (flag_R
&& cmd
== LC_REEXPORT_DYLIB
)
380 (*load_command
)->cmd
= framework
.Swap(LC_LOAD_DYLIB
);
381 else if (cmd
== LC_CODE_SIGNATURE
)
382 signature
= reinterpret_cast<struct linkedit_data_command
*>(*load_command
);
383 else if (cmd
== LC_UUID
) {
384 volatile struct uuid_command
*uuid_command(reinterpret_cast<struct uuid_command
*>(*load_command
));
387 printf("uuid%zu=%.2x%.2x%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x%.2x%.2x%.2x%.2x\n", filei
,
388 uuid_command
->uuid
[ 0], uuid_command
->uuid
[ 1], uuid_command
->uuid
[ 2], uuid_command
->uuid
[ 3],
389 uuid_command
->uuid
[ 4], uuid_command
->uuid
[ 5], uuid_command
->uuid
[ 6], uuid_command
->uuid
[ 7],
390 uuid_command
->uuid
[ 8], uuid_command
->uuid
[ 9], uuid_command
->uuid
[10], uuid_command
->uuid
[11],
391 uuid_command
->uuid
[12], uuid_command
->uuid
[13], uuid_command
->uuid
[14], uuid_command
->uuid
[15]
394 } else if (cmd
== LC_ID_DYLIB
) {
395 volatile struct dylib_command
*dylib_command(reinterpret_cast<struct dylib_command
*>(*load_command
));
398 printf("time%zu=0x%.8x\n", filei
, framework
.Swap(dylib_command
->dylib
.timestamp
));
406 dylib_command
->dylib
.timestamp
= 0;
407 timed
= hash(reinterpret_cast<uint8_t *>(framework
.GetBase()), framework
.GetSize(), timev
);
410 dylib_command
->dylib
.timestamp
= framework
.Swap(timed
);
416 _assert(signature
!= NULL
);
418 uint32_t data
= framework
.Swap(signature
->dataoff
);
419 uint32_t size
= framework
.Swap(signature
->datasize
);
421 uint8_t *top
= reinterpret_cast<uint8_t *>(framework
.GetBase());
422 uint8_t *blob
= top
+ data
;
423 struct SuperBlob
*super
= reinterpret_cast<struct SuperBlob
*>(blob
);
424 super
->magic
= Swap(CSMAGIC_EMBEDDED_SIGNATURE
);
427 uint32_t offset
= sizeof(struct SuperBlob
) + count
* sizeof(struct BlobIndex
);
429 super
->index
[0].type
= Swap(CSSLOT_CODEDIRECTORY
);
430 super
->index
[0].offset
= Swap(offset
);
432 uint32_t begin
= offset
;
433 struct CodeDirectory
*directory
= reinterpret_cast<struct CodeDirectory
*>(blob
+ begin
);
434 offset
+= sizeof(struct CodeDirectory
);
436 directory
->magic
= Swap(CSMAGIC_CODEDIRECTORY
);
437 directory
->version
= Swap(0x00020001);
438 directory
->flags
= Swap(0);
439 directory
->codeLimit
= Swap(data
);
440 directory
->hashSize
= 0x14;
441 directory
->hashType
= 0x01;
442 directory
->spare1
= 0x00;
443 directory
->pageSize
= 0x0c;
444 directory
->spare2
= Swap(0);
446 directory
->identOffset
= Swap(offset
- begin
);
447 strcpy(reinterpret_cast<char *>(blob
+ offset
), base
);
448 offset
+= strlen(base
) + 1;
450 uint8_t (*hashes
)[20] = reinterpret_cast<uint8_t (*)[20]>(blob
+ offset
);
451 uint32_t special
= 0;
453 uint32_t pages
= (data
+ 0x1000 - 1) / 0x1000;
454 directory
->nSpecialSlots
= Swap(special
);
455 directory
->nCodeSlots
= Swap(pages
);
458 for (size_t i
= 0; i
!= pages
- 1; ++i
)
459 sha1(hashes
[special
+ i
], top
+ 0x1000 * i
, 0x1000);
461 sha1(hashes
[special
+ pages
- 1], top
+ 0x1000 * (pages
- 1), data
% 0x1000);
463 directory
->hashOffset
= Swap(offset
- begin
);
464 offset
+= sizeof(*hashes
) * (special
+ pages
);
465 directory
->length
= Swap(offset
- begin
);
467 super
->count
= Swap(count
);
468 super
->length
= Swap(offset
);
470 _assert(offset
< size
);
471 memset(blob
+ offset
, 0, size
- offset
);
475 _syscall(unlink(path
));
476 _syscall(rename(temp
, path
));
482 } catch (const char *) {