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